use ArdourButton for the vertical space/button/label in VCAMasterStrip
[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 "ui_config.h"
36 #include "vca_master_strip.h"
37
38 #include "i18n.h"
39
40 using namespace ARDOUR;
41 using namespace ARDOUR_UI_UTILS;
42 using namespace Gtkmm2ext;
43 using namespace Gtk;
44 using namespace PBD;
45 using std::string;
46
47 PBD::Signal1<void,VCAMasterStrip*> VCAMasterStrip::CatchDeletion;
48
49 static string
50 verticalize (string const & str)
51 {
52         return str;
53 #if 0
54         string ret;
55         string::const_iterator s = str.begin();
56         ret = *s;
57         ret += '\n';
58         ++s;
59
60         while (s != str.end()) {
61                 ret += *s;
62                 ret += '\n';
63                 ++s;
64         }
65
66         /* remove terminal newline */
67
68         ret.erase (ret.length() - 1, string::npos);
69         return ret;
70 #endif
71 }
72
73 VCAMasterStrip::VCAMasterStrip (Session* s, boost::shared_ptr<VCA> v)
74         : AxisView (s)
75         , _vca (v)
76         , gain_meter (s, 250)
77         , context_menu (0)
78         , delete_dialog (0)
79 {
80         gain_meter.set_controls (boost::shared_ptr<Route>(),
81                                  boost::shared_ptr<PeakMeter>(),
82                                  boost::shared_ptr<Amp>(),
83                                  _vca->gain_control());
84
85         solo_button.set_name ("solo button");
86         set_tooltip (solo_button, _("Solo slaves"));
87         solo_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::solo_release), false);
88
89         mute_button.set_name ("mute button");
90         mute_button.set_text (_("M"));
91         set_tooltip (mute_button, _("Mute slaves"));
92         mute_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::mute_release), false);
93
94         hide_button.set_icon (ArdourIcon::CloseCross);
95         set_tooltip (&hide_button, _("Hide this VCA strip"));
96
97         assign_button.set_name (X_("vca assign"));
98         set_tooltip (assign_button, _("Click to assign a VCA Master to this VCA"));
99         assign_button.signal_button_release_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::vca_button_release), false);
100
101         hide_button.signal_clicked.connect (sigc::mem_fun(*this, &VCAMasterStrip::hide_clicked));
102
103         width_hide_box.pack_start (number_label, true, true);
104         width_hide_box.pack_end (hide_button, false, true);
105
106         solo_mute_box.set_spacing (2);
107         solo_mute_box.pack_start (mute_button, true, true);
108         solo_mute_box.pack_start (solo_button, true, true);
109
110         number_label.set_text (to_string (v->number(), std::dec));
111         number_label.set_elements((ArdourButton::Element)(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text|ArdourButton::Inactive));
112         number_label.set_no_show_all ();
113         number_label.set_name ("generic button");
114         number_label.set_alignment (.5, .5);
115         number_label.set_fallthrough_to_parent (true);
116
117         name_button.signal_button_press_event().connect (sigc::mem_fun (*this, &VCAMasterStrip::name_button_press), false);
118
119         top_padding.set_size_request (-1, 16); /* must match height in GroupTabs::set_size_request() */
120         bottom_padding.set_size_request (-1, 50); /* this one is a hack. there's no trivial way to compute it */
121
122         //Glib::RefPtr<Pango::Layout> layout = vertical_button.get_layout ();
123         // layout->set_justify (JUSTIFY_CENTER);
124         /* horizontally centered, with a little space (5%) at the top */
125         vertical_button.set_angle (90);
126         vertical_button.set_layout_font (UIConfiguration::instance().get_NormalBoldFont());
127
128         global_vpacker.set_border_width (1);
129         global_vpacker.set_spacing (0);
130
131         global_vpacker.pack_start (top_padding, false, false);
132         global_vpacker.pack_start (width_hide_box, false, false);
133         global_vpacker.pack_start (name_button, false, false);
134         global_vpacker.pack_start (vertical_button, true, true);
135         global_vpacker.pack_start (solo_mute_box, false, false);
136         global_vpacker.pack_start (gain_meter, false, false);
137         global_vpacker.pack_start (assign_button, false, false);
138         global_vpacker.pack_start (bottom_padding, false, false);
139
140         global_frame.add (global_vpacker);
141         global_frame.set_shadow_type (Gtk::SHADOW_IN);
142         global_frame.set_name ("BaseFrame");
143
144         add (global_frame);
145
146         global_vpacker.show ();
147         global_frame.show ();
148         top_padding.show ();
149         bottom_padding.show ();
150         vertical_button.show ();
151         hide_button.show ();
152         number_label.show ();
153         width_hide_box.show ();
154         name_button.show ();
155         gain_meter.show ();
156         solo_mute_box.show_all ();
157         assign_button.show ();
158
159         /* force setting of visible selected status */
160
161         _selected = true;
162         set_selected (false);
163         set_solo_text ();
164         update_vca_display ();
165         update_vca_name ();
166         solo_changed ();
167         mute_changed ();
168
169         /* this remains unchanged as the name changes */
170         name_button.set_text (string_compose (X_("VCA %1"), _vca->number()));
171
172         _vca->PropertyChanged.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::vca_property_changed, this, _1), gui_context());
173
174         _vca->solo_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::solo_changed, this), gui_context());
175         _vca->mute_control()->Changed.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::mute_changed, this), gui_context());
176
177         /* only need to connect to one of these to update VCA status */
178
179         _vca->gain_control()->MasterStatusChange.connect (vca_connections,
180                                                   invalidator (*this),
181                                                   boost::bind (&VCAMasterStrip::update_vca_display, this),
182                                                   gui_context());
183
184
185         _vca->DropReferences.connect (vca_connections, invalidator (*this), boost::bind (&VCAMasterStrip::self_delete, this), gui_context());
186
187 }
188
189 VCAMasterStrip::~VCAMasterStrip ()
190 {
191         delete delete_dialog;
192         delete context_menu;
193
194         CatchDeletion (this); /* EMIT SIGNAL */
195 }
196
197 void
198 VCAMasterStrip::self_delete ()
199 {
200         delete_when_idle (this);
201 }
202
203 void
204 VCAMasterStrip::update_vca_display ()
205 {
206         VCAList vcas (_session->vca_manager().vcas());
207         string label;
208
209         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
210                 if (_vca->gain_control()->slaved_to ((*v)->gain_control())) {
211                         if (!label.empty()) {
212                                 label += ' ';
213                         }
214                         label += to_string ((*v)->number(), std::dec);
215                 }
216         }
217
218         if (label.empty()) {
219                 label = _("-vca-");
220                 assign_button.set_active_state (Gtkmm2ext::Off);
221         } else {
222                 assign_button.set_active_state (Gtkmm2ext::ExplicitActive);
223         }
224
225         assign_button.set_text (label);
226 }
227
228 string
229 VCAMasterStrip::name() const
230 {
231         return _vca->name();
232 }
233
234 void
235 VCAMasterStrip::hide_clicked ()
236 {
237         if (!delete_dialog) {
238                 delete_dialog = new MessageDialog (_("Removing a Master will deassign all slaves. Remove it anyway?"),
239                                                    true, MESSAGE_WARNING, BUTTONS_YES_NO, true);
240                 delete_dialog->signal_response().connect (sigc::mem_fun (*this, &VCAMasterStrip::hide_confirmation));
241         }
242
243         delete_dialog->set_position (Gtk::WIN_POS_MOUSE);
244         delete_dialog->present ();
245 }
246
247 void
248 VCAMasterStrip::hide_confirmation (int response)
249 {
250         delete_dialog->hide ();
251
252         switch (response) {
253         case RESPONSE_OK:
254                 /* get everything to deassign. This will also delete ourselves (when
255                  * idle) and that in turn will remove us from the Mixer GUI
256                  */
257                 _session->vca_manager().remove_vca (_vca);
258                 break;
259         default:
260                 break;
261         }
262 }
263
264 bool
265 VCAMasterStrip::width_button_pressed (GdkEventButton* ev)
266 {
267         return false;
268 }
269
270 void
271 VCAMasterStrip::set_selected (bool yn)
272 {
273         AxisView::set_selected (yn);
274
275         if (_selected) {
276                 global_frame.set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
277                 global_frame.set_name ("MixerStripSelectedFrame");
278         } else {
279                 global_frame.set_shadow_type (Gtk::SHADOW_IN);
280                 global_frame.set_name ("MixerStripFrame");
281         }
282
283         global_frame.queue_draw ();
284 }
285
286 bool
287 VCAMasterStrip::solo_release (GdkEventButton*)
288 {
289         /* We use NoGroup because VCA controls are never part of a group. This
290            is redundant, but clear.
291         */
292         _vca->solo_control()->set_value (_vca->solo_control()->self_soloed() ? 0.0 : 1.0, Controllable::NoGroup);
293         return true;
294 }
295
296 bool
297 VCAMasterStrip::mute_release (GdkEventButton*)
298 {
299         /* We use NoGroup because VCA controls are never part of a group. This
300            is redundant, but clear.
301         */
302         _vca->mute_control()->set_value (_vca->mute_control()->muted_by_self() ? 0.0 : 1.0, Controllable::NoGroup);
303         return true;
304 }
305
306 void
307 VCAMasterStrip::set_solo_text ()
308 {
309         if (Config->get_solo_control_is_listen_control ()) {
310                 switch (Config->get_listen_position()) {
311                 case AfterFaderListen:
312                         solo_button.set_text (_("A"));
313                         break;
314                 case PreFaderListen:
315                         solo_button.set_text (_("P"));
316                         break;
317                         }
318         } else {
319                 solo_button.set_text (_("S"));
320         }
321 }
322
323 void
324 VCAMasterStrip::mute_changed ()
325 {
326         if (_vca->mute_control()->muted_by_self()) {
327                 mute_button.set_active_state (ExplicitActive);
328         } else if (_vca->mute_control()->muted_by_masters ()) {
329                 mute_button.set_active_state (ImplicitActive);
330         } else {
331                 mute_button.set_active_state (Gtkmm2ext::Off);
332         }
333 }
334
335 void
336 VCAMasterStrip::solo_changed ()
337 {
338         if (_vca->solo_control()->self_soloed()) {
339                 solo_button.set_active_state (ExplicitActive);
340         } else if (_vca->solo_control()->soloed_by_masters ()) {
341                 solo_button.set_active_state (ImplicitActive);
342         } else {
343                 solo_button.set_active_state (Gtkmm2ext::Off);
344         }
345 }
346
347 void
348 VCAMasterStrip::vca_menu_toggle (CheckMenuItem* menuitem, uint32_t n)
349 {
350         boost::shared_ptr<VCA> vca = _session->vca_manager().vca_by_number (n);
351
352         if (!menuitem->get_active()) {
353                 if (!vca) {
354                         /* null VCA means drop all VCA assignments */
355                         _vca->unassign (boost::shared_ptr<VCA>());
356
357                 } else {
358                         _vca->unassign (vca);
359                 }
360         } else {
361                 if (vca) {
362                         _vca->assign (vca);
363                 }
364         }
365 }
366
367 void
368 VCAMasterStrip::unassign ()
369 {
370         _vca->unassign (boost::shared_ptr<VCA>());
371 }
372
373 bool
374 VCAMasterStrip::vca_button_release (GdkEventButton* ev)
375 {
376         using namespace Gtk::Menu_Helpers;
377
378         if (!_session) {
379                 return false;
380         }
381
382         /* primary click only */
383
384         if (ev->button != 1) {
385                 return false;
386         }
387
388         VCAList vcas (_session->vca_manager().vcas());
389
390         if (vcas.empty()) {
391                 /* XXX should probably show a message saying "No VCA masters" */
392                 return true;
393         }
394
395         Menu* menu = new Menu;
396         MenuList& items = menu->items();
397
398         items.push_back (MenuElem (_("Unassign"), sigc::mem_fun (*this, &VCAMasterStrip::unassign)));
399
400         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
401
402                 if (*v == _vca) {
403                         /* no self-mastering */
404                         continue;
405                 }
406
407                 items.push_back (CheckMenuElem ((*v)->name()));
408                 CheckMenuItem* item = dynamic_cast<CheckMenuItem*> (&items.back());
409                 if (_vca->gain_control()->slaved_to ((*v)->gain_control())) {
410                         item->set_active (true);
411                 }
412                 item->signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &VCAMasterStrip::vca_menu_toggle), item, (*v)->number()));
413         }
414
415         menu->popup (1, ev->time);
416
417         return true;
418 }
419
420 bool
421 VCAMasterStrip::vertical_box_press (GdkEventButton* ev)
422 {
423         if (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
424                 start_name_edit ();
425                 return true;
426         }
427
428         if (Keyboard::is_context_menu_event (ev)) {
429                 if (!context_menu) {
430                         build_context_menu ();
431                 }
432                 context_menu->popup (1, ev->time);
433                 return true;
434         }
435
436         if (ev->button == 1) {
437                 // spill ();
438         }
439
440         return true;
441 }
442
443 bool
444 VCAMasterStrip::name_button_press (GdkEventButton* ev)
445 {
446         if (ev->button == 1 && ev->type == GDK_2BUTTON_PRESS) {
447                 start_name_edit ();
448                 return true;
449         }
450
451         if (Keyboard::is_context_menu_event (ev)) {
452                 if (!context_menu) {
453                         build_context_menu ();
454                 }
455                 context_menu->popup (1, ev->time);
456                 return true;
457         }
458
459         return false;
460 }
461
462 void
463 VCAMasterStrip::start_name_edit ()
464 {
465         Gtk::Window* win = dynamic_cast<Gtk::Window*>(get_toplevel());
466         FloatingTextEntry* fte = new FloatingTextEntry (win, _vca->name());
467         fte->use_text.connect (sigc::mem_fun (*this, &VCAMasterStrip::finish_name_edit));
468         fte->present ();
469 }
470
471 void
472 VCAMasterStrip::finish_name_edit (std::string str)
473 {
474         _vca->set_name (str);
475 }
476
477 void
478 VCAMasterStrip::vca_property_changed (PropertyChange const & what_changed)
479 {
480         if (what_changed.contains (ARDOUR::Properties::name)) {
481                 update_vca_name ();
482         }
483 }
484
485 void
486 VCAMasterStrip::update_vca_name ()
487 {
488         vertical_button.set_text (verticalize (short_version (_vca->name(), 15)));
489 }
490
491 void
492 VCAMasterStrip::build_context_menu ()
493 {
494         using namespace Gtk::Menu_Helpers;
495         context_menu = new Menu;
496         MenuList& items = context_menu->items();
497         items.push_back (MenuElem (_("Rename"), sigc::mem_fun (*this, &VCAMasterStrip::start_name_edit)));
498         items.push_back (MenuElem (_("Remove")));
499 }