color management and handling for VCAs
[ardour.git] / gtk2_ardour / control_slave_ui.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 <string>
20
21 #include <gtkmm/menu.h>
22
23 #include "pbd/convert.h"
24
25 #include "ardour/session.h"
26 #include "ardour/stripable.h"
27 #include "ardour/types.h"
28 #include "ardour/vca.h"
29 #include "ardour/vca_manager.h"
30
31 #include "gtkmm2ext/gtk_ui.h"
32 #include "gtkmm2ext/utils.h"
33
34 #include "ardour_button.h"
35 #include "control_slave_ui.h"
36 #include "gui_thread.h"
37
38 #include "i18n.h"
39
40 using namespace ARDOUR;
41 using namespace Gtk;
42 using std::string;
43
44 ControlSlaveUI::ControlSlaveUI (Session* s)
45         : SessionHandlePtr (s)
46         , initial_button (ArdourButton::default_elements)
47 {
48         set_no_show_all (true);
49
50         Gtkmm2ext::UI::instance()->set_tip (*this, _("Control Masters"));
51
52         initial_button.set_no_show_all (true);
53         initial_button.set_name (X_("vca assign"));
54         initial_button.set_text (_("-vca-"));
55         initial_button.show ();
56         initial_button.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
57         initial_button.signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &ControlSlaveUI::vca_button_release), 0), false);
58
59         pack_start (initial_button, true, true);
60 }
61
62 void
63 ControlSlaveUI::set_stripable (boost::shared_ptr<Stripable> s)
64 {
65         connections.drop_connections ();
66
67         stripable = s;
68
69         if (stripable) {
70                 boost::shared_ptr<GainControl> ac = stripable->gain_control();
71                 assert (ac);
72
73                 ac->MasterStatusChange.connect (connections,
74                                                 invalidator (*this),
75                                                 boost::bind (&ControlSlaveUI::update_vca_display, this),
76                                                 gui_context());
77
78                 stripable->DropReferences.connect (connections, invalidator (*this), boost::bind (&ControlSlaveUI::set_stripable, this, boost::shared_ptr<Stripable>()), gui_context());
79         }
80
81         update_vca_display ();
82 }
83
84 void
85 ControlSlaveUI::update_vca_display ()
86 {
87         if (!_session || _session->deletion_in_progress()) {
88                 return;
89         }
90
91         VCAList vcas (_session->vca_manager().vcas());
92         bool any = false;
93
94         Gtkmm2ext::container_clear (*this);
95         master_connections.drop_connections ();
96
97         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
98                 if (stripable->gain_control()->slaved_to ((*v)->gain_control())) {
99                         add_vca_button (*v);
100                         any = true;
101                 }
102         }
103
104         if (!any) {
105                 pack_start (initial_button, true, true);
106         }
107
108         show ();
109 }
110
111 void
112 ControlSlaveUI::vca_menu_toggle (Gtk::CheckMenuItem* menuitem, uint32_t n)
113 {
114         boost::shared_ptr<VCA> vca = _session->vca_manager().vca_by_number (n);
115
116         if (!vca) {
117                 return;
118         }
119
120         boost::shared_ptr<Slavable> sl = boost::dynamic_pointer_cast<Slavable> (stripable);
121
122         if (!sl) {
123                 return;
124         }
125
126         if (!menuitem->get_active()) {
127                 sl->unassign (vca);
128         } else {
129                 sl->assign (vca);
130         }
131 }
132
133 void
134 ControlSlaveUI::unassign_all ()
135 {
136         boost::shared_ptr<Slavable> sl = boost::dynamic_pointer_cast<Slavable> (stripable);
137
138         if (!sl) {
139                 return;
140         }
141
142         sl->unassign (boost::shared_ptr<VCA>());
143 }
144
145 bool
146 ControlSlaveUI::specific_vca_button_release (GdkEventButton* ev, uint32_t n)
147 {
148         return vca_button_release  (ev, n);
149 }
150
151 bool
152 ControlSlaveUI::vca_button_release (GdkEventButton* ev, uint32_t n)
153 {
154         using namespace Gtk::Menu_Helpers;
155
156         if (!_session) {
157                 return false;
158         }
159
160         /* primary click only */
161
162         if (ev->button != 1) {
163                 return false;
164         }
165
166         if (!stripable) {
167                 /* no route - nothing to do */
168                 return false;
169         }
170
171         VCAList vcas (_session->vca_manager().vcas());
172
173         if (vcas.empty()) {
174                 /* the button should not have been visible under these conditions */
175                 return true;
176         }
177
178         Menu* menu = new Menu;
179         MenuList& items = menu->items();
180         bool slaved = false;
181
182         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
183
184                 boost::shared_ptr<GainControl> gcs = stripable->gain_control();
185                 boost::shared_ptr<GainControl> gcm = (*v)->gain_control();
186
187                 if (gcs == gcm) {
188                         /* asked to slave to self. not ok */
189                         continue;
190                 }
191
192                 if (gcm->slaved_to (gcs)) {
193                         /* master is already slaved to slave */
194                         continue;
195                 }
196
197                 items.push_back (CheckMenuElem ((*v)->name()));
198                 Gtk::CheckMenuItem* item = dynamic_cast<Gtk::CheckMenuItem*> (&items.back());
199
200                 if (gcs->slaved_to (gcm)) {
201                         item->set_active (true);
202                         slaved = true;
203                 }
204
205                 item->signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &ControlSlaveUI::vca_menu_toggle), item, (*v)->number()));
206         }
207
208         if (slaved) {
209                 items.push_back (MenuElem (_("Unassign All"), sigc::mem_fun (*this, &ControlSlaveUI::unassign_all)));
210         }
211
212         if (!items.empty()) {
213                 menu->popup (1, ev->time);
214         }
215
216         return true;
217 }
218
219 void
220 ControlSlaveUI::add_vca_button (boost::shared_ptr<VCA> vca)
221 {
222         ArdourButton* vca_button = manage (new ArdourButton (ArdourButton::default_elements));
223
224         vca_button->set_no_show_all (true);
225         vca_button->set_name (X_("vca assign"));
226         vca_button->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
227         vca_button->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &ControlSlaveUI::specific_vca_button_release), vca->number()), false);
228         vca_button->set_text (PBD::to_string (vca->number(), std::dec));
229         vca_button->set_fixed_colors (vca->presentation_info().color(), vca->presentation_info().color ());
230
231         vca->presentation_info().PropertyChanged.connect (master_connections, invalidator (*this), boost::bind (&ControlSlaveUI::master_property_changed, this, _1), gui_context());
232
233         pack_start (*vca_button);
234         vca_button->show ();
235 }
236
237 void
238 ControlSlaveUI::master_property_changed (PBD::PropertyChange const& /* what_changed */)
239 {
240         update_vca_display ();
241 }