first vaguely working version using PresentationInfo
[ardour.git] / gtk2_ardour / vca_master_strip.cc
1 /*
2     Copyright (C) 2016 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <gtkmm/stock.h>
20
21 #include "pbd/convert.h"
22
23 #include "ardour/rc_configuration.h"
24 #include "ardour/session.h"
25 #include "ardour/vca.h"
26 #include "ardour/vca_manager.h"
27
28 #include "gtkmm2ext/doi.h"
29 #include "gtkmm2ext/keyboard.h"
30
31 #include "ardour_dialog.h"
32 #include "floating_text_entry.h"
33 #include "gui_thread.h"
34 #include "tooltips.h"
35 #include "vca_master_strip.h"
36
37 #include "i18n.h"
38
39 using namespace ARDOUR;
40 using namespace ARDOUR_UI_UTILS;
41 using namespace Gtkmm2ext;
42 using namespace Gtk;
43 using namespace PBD;
44 using std::string;
45
46 PBD::Signal1<void,VCAMasterStrip*> VCAMasterStrip::CatchDeletion;
47
48 VCAMasterStrip::VCAMasterStrip (Session* s, boost::shared_ptr<VCA> v)
49         : AxisView (s)
50         , _vca (v)
51         , gain_meter (s, 250)
52         , context_menu (0)
53         , delete_dialog (0)
54 {
55         gain_meter.set_controls (boost::shared_ptr<Route>(),
56                                  boost::shared_ptr<PeakMeter>(),
57                                  boost::shared_ptr<Amp>(),
58                                  _vca->gain_control());
59
60         solo_button.set_name ("solo button");
61         set_tooltip (solo_button, _("Solo slaves"));
62         solo_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::solo_release), false);
63
64         mute_button.set_name ("mute button");
65         mute_button.set_text (_("M"));
66         set_tooltip (mute_button, _("Mute slaves"));
67         mute_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::mute_release), false);
68
69         hide_button.set_icon (ArdourIcon::CloseCross);
70         set_tooltip (&hide_button, _("Hide this VCA strip"));
71
72         assign_button.set_name (X_("vca assign"));
73         set_tooltip (assign_button, _("Click to assign a VCA Master to this VCA"));
74         assign_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::vca_button_release), false);
75
76         hide_button.signal_clicked.connect (sigc::mem_fun(*this, &VCAMasterStrip::hide_clicked));
77
78         width_hide_box.pack_start (number_label, true, true);
79         width_hide_box.pack_end (hide_button, false, true);
80
81         solo_mute_box.set_spacing (2);
82         solo_mute_box.pack_start (mute_button, true, true);
83         solo_mute_box.pack_start (solo_button, true, true);
84
85         number_label.set_text (to_string (v->number(), std::dec));
86         number_label.set_elements((ArdourButton::Element)(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text|ArdourButton::Inactive));
87         number_label.set_no_show_all ();
88         number_label.set_name ("generic button");
89         number_label.set_alignment (.5, .5);
90         number_label.set_fallthrough_to_parent (true);
91
92         name_button.signal_button_press_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::name_button_press), false);
93
94         top_padding.set_size_request (-1, 16); /* must match height in GroupTabs::set_size_request() */
95         bottom_padding.set_size_request (-1, 50); /* this one is a hack. there's no trivial way to compute it */
96
97         global_vpacker.set_border_width (1);
98         global_vpacker.set_spacing (0);
99
100         global_vpacker.pack_start (top_padding, false, false);
101         global_vpacker.pack_start (width_hide_box, false, false);
102         global_vpacker.pack_start (name_button, false, false);
103         global_vpacker.pack_start (vertical_padding, true, true);
104         global_vpacker.pack_start (solo_mute_box, false, false);
105         global_vpacker.pack_start (gain_meter, false, false);
106         global_vpacker.pack_start (assign_button, false, false);
107         global_vpacker.pack_start (bottom_padding, false, false);
108
109         global_frame.add (global_vpacker);
110         global_frame.set_shadow_type (Gtk::SHADOW_IN);
111         global_frame.set_name ("BaseFrame");
112
113         add (global_frame);
114
115         global_vpacker.show ();
116         global_frame.show ();
117         top_padding.show ();
118         bottom_padding.show ();
119         vertical_padding.show ();
120         hide_button.show ();
121         number_label.show ();
122         width_hide_box.show ();
123         name_button.show ();
124         gain_meter.show ();
125         solo_mute_box.show_all ();
126         assign_button.show ();
127
128         /* force setting of visible selected status */
129
130         _selected = true;
131         set_selected (false);
132         set_solo_text ();
133         update_vca_display ();
134         update_vca_name ();
135         solo_changed ();
136         mute_changed ();
137
138         _vca->PropertyChanged.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::vca_property_changed, this, _1), gui_context());
139
140         _vca->solo_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::solo_changed, this), gui_context());
141         _vca->mute_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::mute_changed, this), gui_context());
142
143         /* only need to connect to one of these to update VCA status */
144
145         _vca->gain_control()->MasterStatusChange.connect (vca_connections,
146                                                   invalidator (*this),
147                                                   boost::bind (&VCAMasterStrip::update_vca_display, this),
148                                                   gui_context());
149
150
151         _vca->DropReferences.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::self_delete, this), gui_context());
152
153 }
154
155 VCAMasterStrip::~VCAMasterStrip ()
156 {
157         delete delete_dialog;
158         delete context_menu;
159
160         CatchDeletion (this); /* EMIT SIGNAL */
161 }
162
163 void
164 VCAMasterStrip::self_delete ()
165 {
166         delete_when_idle (this);
167 }
168
169 void
170 VCAMasterStrip::update_vca_display ()
171 {
172         VCAList vcas (_session->vca_manager().vcas());
173         string label;
174
175         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
176                 if (_vca->gain_control()->slaved_to ((*v)->gain_control())) {
177                         if (!label.empty()) {
178                                 label += ' ';
179                         }
180                         label += to_string ((*v)->number(), std::dec);
181                 }
182         }
183
184         if (label.empty()) {
185                 label = _("-vca-");
186                 assign_button.set_active_state (Gtkmm2ext::Off);
187         } else {
188                 assign_button.set_active_state (Gtkmm2ext::ExplicitActive);
189         }
190
191         assign_button.set_text (label);
192 }
193
194 string
195 VCAMasterStrip::name() const
196 {
197         return _vca->name();
198 }
199
200 void
201 VCAMasterStrip::hide_clicked ()
202 {
203         if (!delete_dialog) {
204                 delete_dialog = new MessageDialog (_("Removing a Master will deassign all slaves. Remove it anyway?"),
205                                                    true, MESSAGE_WARNING, BUTTONS_YES_NO, true);
206                 delete_dialog->signal_response().connect (sigc::mem_fun (*this, &VCAMasterStrip::hide_confirmation));
207         }
208
209         delete_dialog->set_position (Gtk::WIN_POS_MOUSE);
210         delete_dialog->present ();
211 }
212
213 void
214 VCAMasterStrip::hide_confirmation (int response)
215 {
216         delete_dialog->hide ();
217
218         switch (response) {
219         case RESPONSE_OK:
220                 /* get everything to deassign. This will also delete ourselves (when
221                  * idle) and that in turn will remove us from the Mixer GUI
222                  */
223                 _session->vca_manager().remove_vca (_vca);
224                 break;
225         default:
226                 break;
227         }
228 }
229
230 bool
231 VCAMasterStrip::width_button_pressed (GdkEventButton* ev)
232 {
233         return false;
234 }
235
236 void
237 VCAMasterStrip::set_selected (bool yn)
238 {
239         AxisView::set_selected (yn);
240
241         if (_selected) {
242                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
243                 global_frame.set_name ("MixerStripSelectedFrame");
244         } else {
245                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
246                 global_frame.set_name ("MixerStripFrame");
247         }
248
249         global_frame.queue_draw ();
250 }
251
252 bool
253 VCAMasterStrip::solo_release (GdkEventButton*)
254 {
255         /* We use NoGroup because VCA controls are never part of a group. This
256            is redundant, but clear.
257         */
258         _vca->solo_control()->set_value (_vca->solo_control()->self_soloed() ? 0.0 : 1.0, Controllable::NoGroup);
259         return true;
260 }
261
262 bool
263 VCAMasterStrip::mute_release (GdkEventButton*)
264 {
265         /* We use NoGroup because VCA controls are never part of a group. This
266            is redundant, but clear.
267         */
268         _vca->mute_control()->set_value (_vca->mute_control()->muted_by_self() ? 0.0 : 1.0, Controllable::NoGroup);
269         return true;
270 }
271
272 void
273 VCAMasterStrip::set_solo_text ()
274 {
275         if (Config->get_solo_control_is_listen_control ()) {
276                 switch (Config->get_listen_position()) {
277                 case AfterFaderListen:
278                         solo_button.set_text (_("A"));
279                         break;
280                 case PreFaderListen:
281                         solo_button.set_text (_("P"));
282                         break;
283                         }
284         } else {
285                 solo_button.set_text (_("S"));
286         }
287 }
288
289 void
290 VCAMasterStrip::mute_changed ()
291 {
292         if (_vca->mute_control()->muted_by_self()) {
293                 mute_button.set_active_state (ExplicitActive);
294         } else if (_vca->mute_control()->muted_by_masters ()) {
295                 mute_button.set_active_state (ImplicitActive);
296         } else {
297                 mute_button.set_active_state (Gtkmm2ext::Off);
298         }
299 }
300
301 void
302 VCAMasterStrip::solo_changed ()
303 {
304         if (_vca->solo_control()->self_soloed()) {
305                 solo_button.set_active_state (ExplicitActive);
306         } else if (_vca->solo_control()->soloed_by_masters ()) {
307                 solo_button.set_active_state (ImplicitActive);
308         } else {
309                 solo_button.set_active_state (Gtkmm2ext::Off);
310         }
311 }
312
313 void
314 VCAMasterStrip::vca_menu_toggle (CheckMenuItem* menuitem, uint32_t n)
315 {
316         boost::shared_ptr<VCA> vca = _session->vca_manager().vca_by_number (n);
317
318         if (!menuitem->get_active()) {
319                 if (!vca) {
320                         /* null VCA means drop all VCA assignments */
321                         _vca->unassign (boost::shared_ptr<VCA>());
322
323                 } else {
324                         _vca->unassign (vca);
325                 }
326         } else {
327                 if (vca) {
328                         _vca->assign (vca);
329                 }
330         }
331 }
332
333 void
334 VCAMasterStrip::unassign ()
335 {
336         _vca->unassign (boost::shared_ptr<VCA>());
337 }
338
339 bool
340 VCAMasterStrip::vca_button_release (GdkEventButton* ev)
341 {
342         using namespace Gtk::Menu_Helpers;
343
344         if (!_session) {
345                 return false;
346         }
347
348         /* primary click only */
349
350         if (ev->button != 1) {
351                 return false;
352         }
353
354         VCAList vcas (_session->vca_manager().vcas());
355
356         if (vcas.empty()) {
357                 /* XXX should probably show a message saying "No VCA masters" */
358                 return true;
359         }
360
361         Menu* menu = new Menu;
362         MenuList& items = menu->items();
363
364         items.push_back (MenuElem (_("Unassign"), sigc::mem_fun (*this, &VCAMasterStrip::unassign)));
365
366         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
367
368                 if (*v == _vca) {
369                         /* no self-mastering */
370                         continue;
371                 }
372
373                 items.push_back (CheckMenuElem ((*v)->name()));
374                 CheckMenuItem* item = dynamic_cast<CheckMenuItem*> (&items.back());
375                 if (_vca->gain_control()->slaved_to ((*v)->gain_control())) {
376                         item->set_active (true);
377                 }
378                 item->signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &VCAMasterStrip::vca_menu_toggle), item, (*v)->number()));
379         }
380
381         menu->popup (1, ev->time);
382
383         return true;
384 }
385
386 bool
387 VCAMasterStrip::name_button_press (GdkEventButton* ev)
388 {
389         if (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
390                 Gtk::Window* win = dynamic_cast<Gtk::Window*>(get_toplevel());
391                 FloatingTextEntry* fte = new FloatingTextEntry (win, _vca->name());
392                 fte->use_text.connect (sigc::mem_fun (*this, &VCAMasterStrip::finish_name_edit));
393                 fte->present ();
394                 return true;
395         }
396
397         if (Keyboard::is_context_menu_event (ev)) {
398                 if (!context_menu) {
399                         build_context_menu ();
400                 }
401                 context_menu->popup (1, ev->time);
402                 return true;
403         }
404
405         return false;
406 }
407
408 void
409 VCAMasterStrip::finish_name_edit (std::string str)
410 {
411         _vca->set_name (str);
412 }
413
414 void
415 VCAMasterStrip::vca_property_changed (PropertyChange const & what_changed)
416 {
417         if (what_changed.contains (ARDOUR::Properties::name)) {
418                 update_vca_name ();
419         }
420 }
421
422 void
423 VCAMasterStrip::update_vca_name ()
424 {
425         name_button.set_text (short_version (_vca->name(), 8));
426 }
427
428 void
429 VCAMasterStrip::build_context_menu ()
430 {
431         using namespace Gtk::Menu_Helpers;
432         context_menu = new Menu;
433         MenuList& items = context_menu->items();
434         items.push_back (MenuElem (_("Remove")));
435 }