meter-bridge details:
[ardour.git] / gtk2_ardour / meterbridge.cc
1 /*
2     Copyright (C) 2012 Paul Davis
3     Author: Robin Gareus
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
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifdef WAF_BUILD
22 #include "gtk2ardour-config.h"
23 #endif
24
25 #include <map>
26 #include <sigc++/bind.h>
27
28 #include <gtkmm/accelmap.h>
29
30 #include <glibmm/threads.h>
31
32 #include <gtkmm2ext/gtk_ui.h>
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/window_title.h>
35
36 #include "ardour/debug.h"
37 #include "ardour/midi_track.h"
38 #include "ardour/route_group.h"
39 #include "ardour/session.h"
40
41 #include "meterbridge.h"
42
43 #include "monitor_section.h"
44 #include "public_editor.h"
45 #include "ardour_ui.h"
46 #include "utils.h"
47 #include "route_sorter.h"
48 #include "actions.h"
49 #include "gui_thread.h"
50
51 #include "i18n.h"
52
53 using namespace ARDOUR;
54 using namespace PBD;
55 using namespace Gtk;
56 using namespace Glib;
57 using namespace Gtkmm2ext;
58 using namespace std;
59
60 using PBD::atoi;
61
62 Meterbridge* Meterbridge::_instance = 0;
63
64 Meterbridge*
65 Meterbridge::instance ()
66 {
67         if (!_instance) {
68                 _instance  = new Meterbridge;
69         }
70
71         return _instance;
72 }
73
74 /* copy from gtk2_ardour/mixer_ui.cc -- TODO consolidate
75  * used by Meterbridge::set_session() below
76  */
77 struct SignalOrderRouteSorter {
78         bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
79                 if (a->is_master() || a->is_monitor()) {
80                         /* "a" is a special route (master, monitor, etc), and comes
81                          * last in the mixer ordering
82                          */
83                         return false;
84                 } else if (b->is_master() || b->is_monitor()) {
85                         /* everything comes before b */
86                         return true;
87                 }
88                 return a->order_key (MixerSort) < b->order_key (MixerSort);
89         }
90 };
91
92 /* modified version of above
93  * used in Meterbridge::sync_order_keys()
94  */
95 struct MeterOrderRouteSorter {
96         bool operator() (MeterStrip *ma, MeterStrip *mb) {
97                 boost::shared_ptr<Route> a = ma->route();
98                 boost::shared_ptr<Route> b = mb->route();
99                 if (a->is_master() || a->is_monitor()) {
100                         /* "a" is a special route (master, monitor, etc), and comes
101                          * last in the mixer ordering
102                          */
103                         return false;
104                 } else if (b->is_master() || b->is_monitor()) {
105                         /* everything comes before b */
106                         return true;
107                 }
108                 return a->order_key (MixerSort) < b->order_key (MixerSort);
109         }
110 };
111
112
113 Meterbridge::Meterbridge ()
114         : Window (Gtk::WINDOW_TOPLEVEL)
115         , VisibilityTracker (*((Gtk::Window*) this))
116         , _visible (false)
117 {
118         set_name ("Meter Bridge");
119
120         set_wmclass (X_("ardour_mixer"), PROGRAM_NAME);
121
122         signal_delete_event().connect (sigc::mem_fun (*this, &Meterbridge::hide_window));
123         signal_configure_event().connect (sigc::mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::configure_handler));
124         Route::SyncOrderKeys.connect (*this, invalidator (*this), boost::bind (&Meterbridge::sync_order_keys, this, _1), gui_context());
125
126         MeterStrip::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&Meterbridge::remove_strip, this, _1), gui_context());
127
128         global_hpacker.set_spacing(1);
129         scroller.add (global_hpacker);
130         scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_NEVER);
131         global_vpacker.pack_start (scroller, true, true);
132         add (global_vpacker);
133
134         global_hpacker.show();
135         global_vpacker.show();
136         scroller.show();
137 }
138
139 Meterbridge::~Meterbridge ()
140 {
141 }
142
143 void
144 Meterbridge::show_window ()
145 {
146         present();
147         if (!_visible) {
148                 set_window_pos_and_size ();
149         }
150         _visible = true;
151 }
152
153 void
154 Meterbridge::set_window_pos_and_size ()
155 {
156         resize (m_width, m_height);
157         move (m_root_x, m_root_y);
158 }
159
160 void
161 Meterbridge::get_window_pos_and_size ()
162 {
163         get_position(m_root_x, m_root_y);
164         get_size(m_width, m_height);
165 }
166
167 bool
168 Meterbridge::hide_window (GdkEventAny *ev)
169 {
170         get_window_pos_and_size();
171         _visible = false;
172         return just_hide_it(ev, static_cast<Gtk::Window *>(this));
173 }
174
175 bool
176 Meterbridge::on_key_press_event (GdkEventKey* ev)
177 {
178         if (gtk_window_propagate_key_event (GTK_WINDOW(gobj()), ev)) {
179                 return true;
180         }
181         return forward_key_press (ev);
182 }
183
184 bool
185 Meterbridge::on_key_release_event (GdkEventKey* ev)
186 {
187         if (gtk_window_propagate_key_event (GTK_WINDOW(gobj()), ev)) {
188                 return true;
189         }
190         /* don't forward releases */
191         return true;
192 }
193
194 void
195 Meterbridge::set_session (Session* s)
196 {
197         SessionHandlePtr::set_session (s);
198
199         if (!_session) {
200                 return;
201         }
202
203         XMLNode* node = _session->instant_xml(X_("Meterbridge"));
204         if (node) {
205                 set_state (*node);
206         }
207
208         SignalOrderRouteSorter sorter;
209         boost::shared_ptr<RouteList> routes = _session->get_routes();
210
211         RouteList copy(*routes);
212         copy.sort(sorter);
213         add_strips(copy);
214
215         _session->RouteAdded.connect (_session_connections, invalidator (*this), boost::bind (&Meterbridge::add_strips, this, _1), gui_context());
216
217         if (_visible) {
218                 show_window();
219                 ActionManager::check_toggleaction ("<Actions>/Common/toggle-meterbridge");
220         }
221         start_updating ();
222 }
223
224 void
225 Meterbridge::session_going_away ()
226 {
227         ENSURE_GUI_THREAD (*this, &Meterbridge::session_going_away);
228
229         for (list<MeterStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
230                 delete (*i);
231         }
232
233         strips.clear ();
234         stop_updating ();
235
236         SessionHandlePtr::session_going_away ();
237
238         _session = 0;
239 }
240
241 int
242 Meterbridge::set_state (const XMLNode& node)
243 {
244         const XMLProperty* prop;
245         XMLNode* geometry;
246
247         m_width = default_width;
248         m_height = default_height;
249         m_root_x = 1;
250         m_root_y = 1;
251
252         if ((geometry = find_named_node (node, "geometry")) != 0) {
253
254                 XMLProperty* prop;
255
256                 if ((prop = geometry->property("x_size")) == 0) {
257                         prop = geometry->property ("x-size");
258                 }
259                 if (prop) {
260                         m_width = atoi(prop->value());
261                 }
262                 if ((prop = geometry->property("y_size")) == 0) {
263                         prop = geometry->property ("y-size");
264                 }
265                 if (prop) {
266                         m_height = atoi(prop->value());
267                 }
268
269                 if ((prop = geometry->property ("x_pos")) == 0) {
270                         prop = geometry->property ("x-pos");
271                 }
272                 if (prop) {
273                         m_root_x = atoi (prop->value());
274
275                 }
276                 if ((prop = geometry->property ("y_pos")) == 0) {
277                         prop = geometry->property ("y-pos");
278                 }
279                 if (prop) {
280                         m_root_y = atoi (prop->value());
281                 }
282         }
283
284         set_window_pos_and_size ();
285
286         if ((prop = node.property ("show-meterbridge"))) {
287                 if (string_is_affirmative (prop->value())) {
288                        _visible = true;
289                 }
290         }
291
292         return 0;
293 }
294
295 XMLNode&
296 Meterbridge::get_state (void)
297 {
298         XMLNode* node = new XMLNode ("Meterbridge");
299
300         if (is_realized()) {
301                 Glib::RefPtr<Gdk::Window> win = get_window();
302
303                 get_window_pos_and_size ();
304
305                 XMLNode* geometry = new XMLNode ("geometry");
306                 char buf[32];
307                 snprintf(buf, sizeof(buf), "%d", m_width);
308                 geometry->add_property(X_("x_size"), string(buf));
309                 snprintf(buf, sizeof(buf), "%d", m_height);
310                 geometry->add_property(X_("y_size"), string(buf));
311                 snprintf(buf, sizeof(buf), "%d", m_root_x);
312                 geometry->add_property(X_("x_pos"), string(buf));
313                 snprintf(buf, sizeof(buf), "%d", m_root_y);
314                 geometry->add_property(X_("y_pos"), string(buf));
315                 node->add_child_nocopy (*geometry);
316         }
317
318         node->add_property ("show-meterbridge", _visible ? "yes" : "no");
319         return *node;
320 }
321
322
323 gint
324 Meterbridge::start_updating ()
325 {
326         fast_screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (sigc::mem_fun(*this, &Meterbridge::fast_update_strips));
327         return 0;
328 }
329
330 gint
331 Meterbridge::stop_updating ()
332 {
333         fast_screen_update_connection.disconnect();
334         return 0;
335 }
336
337 void
338 Meterbridge::fast_update_strips ()
339 {
340         if (!is_mapped () || !_session) {
341                 return;
342         }
343         for (list<MeterStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
344                 (*i)->fast_update ();
345         }
346 }
347
348 void
349 Meterbridge::add_strips (RouteList& routes)
350 {
351         MeterStrip* strip;
352         for (RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
353                 boost::shared_ptr<Route> route = (*x);
354                 if (route->is_auditioner()) {
355                         continue;
356                 }
357                 if (route->is_monitor()) {
358                         continue;
359                 }
360
361                 strip = new MeterStrip (*this, _session, route);
362                 strips.push_back (strip);
363
364                 global_hpacker.pack_start (*strip, false, false);
365                 strip->show();
366         }
367
368         sync_order_keys(MixerSort);
369 }
370
371 void
372 Meterbridge::remove_strip (MeterStrip* strip)
373 {
374         if (_session && _session->deletion_in_progress()) {
375                 return;
376         }
377
378         list<MeterStrip *>::iterator i;
379         if ((i = find (strips.begin(), strips.end(), strip)) != strips.end()) {
380                 strips.erase (i);
381         }
382 }
383
384 void
385 Meterbridge::sync_order_keys (RouteSortOrderKey src)
386 {
387         MeterOrderRouteSorter sorter;
388         std::list<MeterStrip *> copy (strips);
389         copy.sort(sorter);
390
391         int pos = 0;
392         for (list<MeterStrip *>::iterator i = copy.begin(); i != copy.end(); ++i) {
393                 global_hpacker.reorder_child(*(*i), pos++);
394         }
395 }