1 /*
2  * Copyright (C) 2016-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2016-2019 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include <string>
21 
22 #include <gtkmm/menu.h>
23 
24 #include "pbd/string_convert.h"
25 
26 #include "ardour/session.h"
27 #include "ardour/stripable.h"
28 #include "ardour/types.h"
29 #include "ardour/vca.h"
30 #include "ardour/vca_manager.h"
31 
32 #include "gtkmm2ext/gtk_ui.h"
33 #include "gtkmm2ext/utils.h"
34 
35 #include "control_slave_ui.h"
36 #include "gui_thread.h"
37 
38 #include "pbd/i18n.h"
39 
40 using namespace ARDOUR;
41 using namespace ArdourWidgets;
42 using namespace Gtk;
43 using std::string;
44 
ControlSlaveUI(Session * s)45 ControlSlaveUI::ControlSlaveUI (Session* s)
46 	: SessionHandlePtr (s)
47 	, initial_button (ArdourButton::default_elements)
48 	, context_menu (0)
49 {
50 	set_no_show_all (true);
51 
52 	Gtkmm2ext::UI::instance()->set_tip (*this, _("VCA Assign"));
53 
54 	initial_button.set_no_show_all (true);
55 	initial_button.set_name (X_("vca assign button"));
56 	initial_button.set_text (_("-VCAs-"));
57 	initial_button.show ();
58 	initial_button.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
59 	initial_button.signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &ControlSlaveUI::vca_button_release), 0), false);
60 
61 	pack_start (initial_button, true, true);
62 }
63 
~ControlSlaveUI()64 ControlSlaveUI::~ControlSlaveUI ()
65 {
66 	delete context_menu;
67 }
68 
69 void
set_stripable(boost::shared_ptr<Stripable> s)70 ControlSlaveUI::set_stripable (boost::shared_ptr<Stripable> s)
71 {
72 	connections.drop_connections ();
73 
74 	stripable = s;
75 
76 	if (stripable) {
77 		boost::shared_ptr<GainControl> ac = stripable->gain_control();
78 		assert (ac);
79 
80 		ac->MasterStatusChange.connect (connections,
81 		                                invalidator (*this),
82 		                                boost::bind (&ControlSlaveUI::update_vca_display, this),
83 		                                gui_context());
84 
85 		stripable->DropReferences.connect (connections, invalidator (*this), boost::bind (&ControlSlaveUI::set_stripable, this, boost::shared_ptr<Stripable>()), gui_context());
86 	}
87 
88 	update_vca_display ();
89 }
90 
91 void
update_vca_display()92 ControlSlaveUI::update_vca_display ()
93 {
94 	if (!_session || _session->deletion_in_progress()) {
95 		return;
96 	}
97 
98 	VCAList vcas (_session->vca_manager().vcas());
99 	bool any = false;
100 
101 	Gtkmm2ext::container_clear (*this);
102 	master_connections.drop_connections ();
103 
104 	if (stripable) {
105 		for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
106 			if (stripable->slaved_to (*v)) {
107 				add_vca_button (*v);
108 				any = true;
109 			}
110 		}
111 	}
112 
113 	if (!any) {
114 		pack_start (initial_button, true, true);
115 		initial_button.show ();
116 	}
117 
118 	show ();
119 }
120 
121 void
vca_menu_toggle(Gtk::CheckMenuItem * menuitem,uint32_t n)122 ControlSlaveUI::vca_menu_toggle (Gtk::CheckMenuItem* menuitem, uint32_t n)
123 {
124 	boost::shared_ptr<VCA> vca = _session->vca_manager().vca_by_number (n);
125 
126 	if (!vca) {
127 		return;
128 	}
129 
130 	boost::shared_ptr<Slavable> sl = boost::dynamic_pointer_cast<Slavable> (stripable);
131 
132 	if (!sl) {
133 		return;
134 	}
135 
136 	if (!menuitem->get_active()) {
137 		sl->unassign (vca);
138 	} else {
139 		sl->assign (vca);
140 	}
141 }
142 
143 void
unassign_all()144 ControlSlaveUI::unassign_all ()
145 {
146 	boost::shared_ptr<Slavable> sl = boost::dynamic_pointer_cast<Slavable> (stripable);
147 
148 	if (!sl) {
149 		return;
150 	}
151 
152 	sl->unassign (boost::shared_ptr<VCA>());
153 }
154 
155 bool
specific_vca_button_release(GdkEventButton * ev,uint32_t n)156 ControlSlaveUI::specific_vca_button_release (GdkEventButton* ev, uint32_t n)
157 {
158 	return vca_button_release  (ev, n);
159 }
160 
161 bool
vca_button_release(GdkEventButton * ev,uint32_t n)162 ControlSlaveUI::vca_button_release (GdkEventButton* ev, uint32_t n)
163 {
164 	using namespace Gtk::Menu_Helpers;
165 
166 	if (!_session) {
167 		return false;
168 	}
169 
170 	/* primary click only */
171 
172 	if (ev->button != 1) {
173 		return false;
174 	}
175 
176 	if (!stripable) {
177 		/* no route - nothing to do */
178 		return false;
179 	}
180 
181 	VCAList vcas (_session->vca_manager().vcas());
182 
183 	if (vcas.empty()) {
184 		/* the button should not have been visible under these conditions */
185 		return false;
186 	}
187 
188 	delete context_menu;
189 	context_menu = new Menu;
190 	MenuList& items = context_menu->items();
191 	bool slaved = false;
192 
193 	for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
194 
195 		if (stripable->assigned_to (_session->vca_manager_ptr (), *v)) {
196 			/* master(stripable) is directly or indirectly controlled by slave (v) */
197 			continue;
198 		}
199 
200 		items.push_back (CheckMenuElem ((*v)->name()));
201 		Gtk::CheckMenuItem* item = dynamic_cast<Gtk::CheckMenuItem*> (&items.back());
202 
203 		if (stripable->slaved_to (*v)) {
204 			item->set_active (true);
205 			slaved = true;
206 		}
207 
208 		item->signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &ControlSlaveUI::vca_menu_toggle), item, (*v)->number()));
209 	}
210 
211 	if (slaved) {
212 		items.push_back (MenuElem (_("Unassign All"), sigc::mem_fun (*this, &ControlSlaveUI::unassign_all)));
213 	}
214 
215 	if (!items.empty()) {
216 		context_menu->popup (1, ev->time);
217 		return true;
218 	}
219 
220 	return false;
221 }
222 
223 void
add_vca_button(boost::shared_ptr<VCA> vca)224 ControlSlaveUI::add_vca_button (boost::shared_ptr<VCA> vca)
225 {
226 	ArdourButton* vca_button = manage (new ArdourButton (ArdourButton::default_elements));
227 
228 	vca_button->set_no_show_all (true);
229 	vca_button->set_name (X_("vca assign button"));
230 	vca_button->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
231 	vca_button->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &ControlSlaveUI::specific_vca_button_release), vca->number()), false);
232 	vca_button->set_text (PBD::to_string (vca->number()));
233 	vca_button->set_fixed_colors (vca->presentation_info().color(), vca->presentation_info().color ());
234 
235 	vca->presentation_info().PropertyChanged.connect (master_connections, invalidator (*this), boost::bind (&ControlSlaveUI::master_property_changed, this, _1), gui_context());
236 
237 	pack_start (*vca_button);
238 	vca_button->show ();
239 }
240 
241 void
master_property_changed(PBD::PropertyChange const &)242 ControlSlaveUI::master_property_changed (PBD::PropertyChange const& /* what_changed */)
243 {
244 	update_vca_display ();
245 }
246