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