Sort session routes before trying to do the group tabs
[ardour.git] / gtk2_ardour / group_tabs.cc
1 /*
2     Copyright (C) 2009 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
20 #include <gtkmm/stock.h>
21 #include "ardour/session.h"
22 #include "ardour/route_group.h"
23 #include "ardour/route.h"
24
25 #include "gui_thread.h"
26 #include "route_group_dialog.h"
27 #include "group_tabs.h"
28 #include "keyboard.h"
29 #include "i18n.h"
30 #include "ardour_ui.h"
31 #include "utils.h"
32
33 using namespace std;
34 using namespace Gtk;
35 using namespace ARDOUR;
36 using Gtkmm2ext::Keyboard;
37
38 list<Gdk::Color> GroupTabs::_used_colors;
39
40 GroupTabs::GroupTabs ()
41         : _menu (0)
42         , _dragging (0)
43         , _dragging_new_tab (0)
44 {
45
46 }
47
48 GroupTabs::~GroupTabs ()
49 {
50         delete _menu;
51 }
52
53 void
54 GroupTabs::set_session (Session* s)
55 {
56         SessionHandlePtr::set_session (s);
57
58         if (_session) {
59                 _session->RouteGroupPropertyChanged.connect (
60                         _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_group_property_changed, this, _1), gui_context()
61                         );
62                 _session->RouteAddedToRouteGroup.connect (
63                         _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_added_to_route_group, this, _1, _2), gui_context()
64                         );
65                 _session->RouteRemovedFromRouteGroup.connect (
66                         _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_removed_from_route_group, this, _1, _2), gui_context()
67                         );
68                 
69                 _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&GroupTabs::set_dirty, this), gui_context());
70         }
71 }
72
73
74 /** Handle a size request.
75  *  @param req GTK requisition
76  */
77 void
78 GroupTabs::on_size_request (Gtk::Requisition *req)
79 {
80         /* Use a dummy, small width and the actual height that we want */
81         req->width = 16;
82         req->height = 16;
83 }
84
85 bool
86 GroupTabs::on_button_press_event (GdkEventButton* ev)
87 {
88         using namespace Menu_Helpers;
89
90         double const p = primary_coordinate (ev->x, ev->y);
91
92         list<Tab>::iterator prev;
93         list<Tab>::iterator next;
94         Tab* t = click_to_tab (p, &prev, &next);
95
96         _drag_min = prev != _tabs.end() ? prev->to : 0;
97         _drag_max = next != _tabs.end() ? next->from : extent ();
98
99         if (ev->button == 1) {
100
101                 if (t == 0) {
102                         Tab n;
103                         n.from = n.to = p;
104                         _dragging_new_tab = true;
105
106                         if (next == _tabs.end()) {
107                                 _tabs.push_back (n);
108                                 t = &_tabs.back ();
109                         } else {
110                                 list<Tab>::iterator j = _tabs.insert (next, n);
111                                 t = &(*j);
112                         }
113
114                 } else {
115                         _dragging_new_tab = false;
116                         _initial_dragging_routes = routes_for_tab (t);
117                 }
118
119                 _dragging = t;
120                 _drag_moved = false;
121                 _drag_first = p;
122
123                 double const h = (t->from + t->to) / 2;
124                 if (p < h) {
125                         _drag_moving = t->from;
126                         _drag_fixed = t->to;
127                         _drag_offset = p - t->from;
128                 } else {
129                         _drag_moving = t->to;
130                         _drag_fixed = t->from;
131                         _drag_offset = p - t->to;
132                 }
133
134         } else if (ev->button == 3) {
135
136                 RouteGroup* g = t ? t->group : 0;
137                 
138                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier) && g) {
139                         /* edit */
140                         RouteGroupDialog d (g, false);
141                         d.do_run ();
142                 } else {
143                         Menu* m = get_menu (g);
144                         if (m) {
145                                 m->popup (ev->button, ev->time);
146                         }
147                 }
148         }
149
150         return true;
151 }
152
153
154 bool
155 GroupTabs::on_motion_notify_event (GdkEventMotion* ev)
156 {
157         if (_dragging == 0) {
158                 return false;
159         }
160
161         double const p = primary_coordinate (ev->x, ev->y);
162
163         if (p != _drag_first) {
164                 _drag_moved = true;
165         }
166
167         _drag_moving = p - _drag_offset;
168
169         _dragging->from = min (_drag_moving, _drag_fixed);
170         _dragging->to = max (_drag_moving, _drag_fixed);
171
172         _dragging->from = max (_dragging->from, _drag_min);
173         _dragging->to = min (_dragging->to, _drag_max);
174
175         set_dirty ();
176         queue_draw ();
177
178         return true;
179 }
180
181
182 bool
183 GroupTabs::on_button_release_event (GdkEventButton* ev)
184 {
185         if (_dragging == 0) {
186                 return false;
187         }
188         
189         if (!_drag_moved) {
190                 
191                 if (_dragging->group) {
192                         /* toggle active state */
193                         _dragging->group->set_active (!_dragging->group->is_active (), this);
194                 }
195                 
196         } else {
197                 /* finish drag */
198                 RouteList routes = routes_for_tab (_dragging);
199                 
200                 if (!routes.empty()) {
201                         if (_dragging_new_tab) {
202                                 RouteGroup* g = create_and_add_group ();
203                                 if (g) {
204                                         for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
205                                                 g->add (*i);
206                                         }
207                                 }
208                         } else {
209                                 boost::shared_ptr<RouteList> r = _session->get_routes ();
210                                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
211                                         
212                                         bool const was_in_tab = find (
213                                                 _initial_dragging_routes.begin(), _initial_dragging_routes.end(), *i
214                                                 ) != _initial_dragging_routes.end ();
215                                         
216                                         bool const now_in_tab = find (routes.begin(), routes.end(), *i) != routes.end();
217                                         
218                                         if (was_in_tab && !now_in_tab) {
219                                                 _dragging->group->remove (*i);
220                                         } else if (!was_in_tab && now_in_tab) {
221                                                 _dragging->group->add (*i);
222                                         }
223                                 }
224                         }
225                 }
226                 
227                 set_dirty ();
228                 queue_draw ();
229         }
230         
231         _dragging = 0;
232         _initial_dragging_routes.clear ();
233
234         return true;
235 }
236
237 void
238 GroupTabs::render (cairo_t* cr)
239 {
240         if (_dragging == 0) {
241                 _tabs = compute_tabs ();
242         }
243
244         /* background */
245
246         cairo_set_source_rgb (cr, 0, 0, 0);
247         cairo_rectangle (cr, 0, 0, get_width(), get_height());
248         cairo_fill (cr);
249
250         /* tabs */
251
252         for (list<Tab>::const_iterator i = _tabs.begin(); i != _tabs.end(); ++i) {
253                 draw_tab (cr, *i);
254         }
255 }
256
257
258 /** Convert a click position to a tab.
259  *  @param c Click position.
260  *  @param prev Filled in with the previous tab to the click, or _tabs.end().
261  *  @param next Filled in with the next tab after the click, or _tabs.end().
262  *  @return Tab under the click, or 0.
263  */
264
265 GroupTabs::Tab *
266 GroupTabs::click_to_tab (double c, list<Tab>::iterator* prev, list<Tab>::iterator* next)
267 {
268         *prev = *next = _tabs.end ();
269         Tab* under = 0;
270
271         list<Tab>::iterator i = _tabs.begin ();
272         while (i != _tabs.end()) {
273
274                 if (i->from > c) {
275                         *next = i;
276                         break;
277                 }
278
279                 if (i->to < c) {
280                         *prev = i;
281                         ++i;
282                         continue;
283                 }
284
285                 if (i->from <= c && c < i->to) {
286                         under = &(*i);
287                 }
288
289                 ++i;
290         }
291
292         return under;
293 }
294
295 Gtk::Menu*
296 GroupTabs::get_menu (RouteGroup* g)
297 {
298         using namespace Menu_Helpers;
299
300         delete _menu;
301
302         Menu* new_from = new Menu;
303         MenuList& f = new_from->items ();
304         f.push_back (MenuElem (_("Selection..."), sigc::mem_fun (*this, &GroupTabs::new_from_selection)));
305         f.push_back (MenuElem (_("Record Enabled..."), sigc::mem_fun (*this, &GroupTabs::new_from_rec_enabled)));
306         f.push_back (MenuElem (_("Soloed..."), sigc::mem_fun (*this, &GroupTabs::new_from_soloed)));
307
308         _menu = new Menu;
309         _menu->set_name ("ArdourContextMenu");
310         MenuList& items = _menu->items();
311
312         items.push_back (MenuElem (_("New..."), hide_return (sigc::mem_fun(*this, &GroupTabs::create_and_add_group))));
313         items.push_back (MenuElem (_("New From"), *new_from));
314
315         if (g) {
316                 items.push_back (MenuElem (_("Edit..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::edit_group), g)));
317                 items.push_back (MenuElem (_("Add New Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, false, PreFader)));
318                 items.push_back (MenuElem (_("Add New Aux Bus (pre-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PreFader)));
319                 items.push_back (MenuElem (_("Add New Aux Bus (post-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PostFader)));
320                 items.push_back (MenuElem (_("Collect"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::collect), g)));
321                 items.push_back (MenuElem (_("Remove"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::remove_group), g)));
322         }
323
324         add_menu_items (_menu, g);
325
326         items.push_back (SeparatorElem());
327         items.push_back (MenuElem (_("Activate All"), sigc::mem_fun(*this, &GroupTabs::activate_all)));
328         items.push_back (MenuElem (_("Disable All"), sigc::mem_fun(*this, &GroupTabs::disable_all)));
329
330         return _menu;
331
332 }
333
334 void
335 GroupTabs::new_from_selection ()
336 {
337         RouteList rl = selected_routes ();
338         if (rl.empty()) {
339                 return;
340         }
341
342         run_new_group_dialog (rl);
343 }
344
345 void
346 GroupTabs::new_from_rec_enabled ()
347 {
348         boost::shared_ptr<RouteList> rl = _session->get_routes ();
349
350         RouteList rec_enabled;
351
352         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
353                 if ((*i)->record_enabled()) {
354                         rec_enabled.push_back (*i);
355                 }
356         }
357
358         if (rec_enabled.empty()) {
359                 return;
360         }
361
362         run_new_group_dialog (rec_enabled);
363 }
364
365 void
366 GroupTabs::new_from_soloed ()
367 {
368         boost::shared_ptr<RouteList> rl = _session->get_routes ();
369
370         RouteList soloed;
371
372         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
373                 if (!(*i)->is_master() && (*i)->soloed()) {
374                         soloed.push_back (*i);
375                 }
376         }
377
378         if (soloed.empty()) {
379                 return;
380         }
381
382         run_new_group_dialog (soloed);
383 }
384
385 void
386 GroupTabs::run_new_group_dialog (RouteList const & rl)
387 {
388         RouteGroup* g = new RouteGroup (*_session, "");
389         g->apply_changes (default_properties ());
390
391         RouteGroupDialog d (g, true);
392
393         if (d.do_run ()) {
394                 delete g;
395         } else {
396                 _session->add_route_group (g);
397                 for (RouteList::const_iterator i = rl.begin(); i != rl.end(); ++i) {
398                         g->add (*i);
399                 }
400         }
401 }
402
403 RouteGroup *
404 GroupTabs::create_and_add_group () const
405 {
406         RouteGroup* g = new RouteGroup (*_session, "");
407
408         g->apply_changes (default_properties ());
409
410         RouteGroupDialog d (g, true);
411
412         if (d.do_run ()) {
413                 delete g;
414                 return 0;
415         }
416
417         _session->add_route_group (g);
418         return g;
419 }
420
421 void
422 GroupTabs::edit_group (RouteGroup* g)
423 {
424         RouteGroupDialog d (g, false);
425         d.do_run ();
426 }
427
428 void
429 GroupTabs::subgroup (RouteGroup* g, bool aux, Placement placement)
430 {
431         g->make_subgroup (aux, placement);
432 }
433
434 struct CollectSorter {
435         CollectSorter (string const & key) : _key (key) {}
436
437         bool operator () (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
438                 return a->order_key (_key) < b->order_key (_key);
439         }
440
441         string _key;
442 };
443
444 struct OrderSorter {
445         OrderSorter (string const & key) : _key (key) {}
446         
447         bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
448                 /* use of ">" forces the correct sort order */
449                 return a->order_key (_key) < b->order_key (_key);
450         }
451
452         string _key;
453 };
454
455 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
456  *  @param g Group to collect.
457  */
458 void
459 GroupTabs::collect (RouteGroup* g)
460 {
461         boost::shared_ptr<RouteList> group_routes = g->route_list ();
462         group_routes->sort (CollectSorter (order_key ()));
463         int const N = group_routes->size ();
464
465         RouteList::iterator i = group_routes->begin ();
466         boost::shared_ptr<RouteList> routes = _session->get_routes ();
467         routes->sort (OrderSorter (order_key ()));
468         RouteList::const_iterator j = routes->begin ();
469
470         int diff = 0;
471         int coll = -1;
472         while (i != group_routes->end() && j != routes->end()) {
473
474                 int const k = (*j)->order_key (order_key ());
475
476                 if (*i == *j) {
477
478                         if (coll == -1) {
479                                 coll = k;
480                                 diff = N - 1;
481                         } else {
482                                 --diff;
483                         }
484
485                         (*j)->set_order_key (order_key (), coll);
486
487                         ++coll;
488                         ++i;
489
490                 } else {
491
492                         (*j)->set_order_key (order_key (), k + diff);
493
494                 }
495
496                 ++j;
497         }
498
499         sync_order_keys ();
500 }
501
502 void
503 GroupTabs::activate_all ()
504 {
505         _session->foreach_route_group (
506                 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
507                 );
508 }
509
510 void
511 GroupTabs::disable_all ()
512 {
513         _session->foreach_route_group (
514                 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
515                 );
516 }
517
518 void
519 GroupTabs::set_activation (RouteGroup* g, bool a)
520 {
521         g->set_active (a, this);
522 }
523
524 void
525 GroupTabs::remove_group (RouteGroup* g)
526 {
527         _session->remove_route_group (*g);
528 }
529
530 /** Set the color of the tab of a route group */
531 void
532 GroupTabs::set_group_color (RouteGroup* group, Gdk::Color color)
533 {
534         assert (group);
535
536         /* Hack to disallow black route groups; force a dark grey instead */
537         if (color.get_red() == 0 && color.get_green() == 0 && color.get_blue() == 0) {
538                 color.set_grey_p (0.1);
539         }
540         
541         GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
542
543         char buf[64];
544         snprintf (buf, sizeof (buf), "%d:%d:%d", color.get_red(), color.get_green(), color.get_blue());
545         gui_state.set (group_gui_id (group), "color", buf);
546
547         /* This is a bit of a hack, but this might change
548            our route's effective color, so emit gui_changed
549            for our routes.
550         */
551
552         emit_gui_changed_for_members (group);
553 }
554
555 /** @return the ID string to use for the GUI state of a route group */
556 string
557 GroupTabs::group_gui_id (RouteGroup* group)
558 {
559         assert (group);
560
561         char buf[64];
562         snprintf (buf, sizeof (buf), "route_group %s", group->id().to_s().c_str ());
563
564         return buf;
565 }
566
567 /** @return the color to use for a route group tab */
568 Gdk::Color
569 GroupTabs::group_color (RouteGroup* group)
570 {
571         assert (group);
572         
573         GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
574
575         string const gui_id = group_gui_id (group);
576
577         bool empty;
578         string const color = gui_state.get_string (gui_id, "color", &empty);
579         if (empty) {
580                 /* no color has yet been set, so use a random one */
581                 Gdk::Color const color = unique_random_color (_used_colors);
582                 set_group_color (group, color);
583                 return color;
584         }
585
586         Gdk::Color c;
587
588         int r, g, b;
589
590         sscanf (color.c_str(), "%d:%d:%d", &r, &g, &b);
591
592         c.set_red (r);
593         c.set_green (g);
594         c.set_blue (b);
595         
596         return c;
597 }
598
599 void
600 GroupTabs::route_group_property_changed (RouteGroup* rg)
601 {
602         /* This is a bit of a hack, but this might change
603            our route's effective color, so emit gui_changed
604            for our routes.
605         */
606
607         emit_gui_changed_for_members (rg);
608         
609         set_dirty ();
610 }
611
612 void
613 GroupTabs::route_added_to_route_group (RouteGroup*, boost::weak_ptr<Route> w)
614 {
615         /* Similarly-spirited hack as in route_group_property_changed */
616
617         boost::shared_ptr<Route> r = w.lock ();
618         if (!r) {
619                 return;
620         }
621
622         r->gui_changed (X_("color"), 0);
623
624         set_dirty ();
625 }
626
627 void
628 GroupTabs::route_removed_from_route_group (RouteGroup*, boost::weak_ptr<Route> w)
629 {
630         /* Similarly-spirited hack as in route_group_property_changed */
631
632         boost::shared_ptr<Route> r = w.lock ();
633         if (!r) {
634                 return;
635         }
636
637         r->gui_changed (X_("color"), 0);
638
639         set_dirty ();
640 }
641
642 void
643 GroupTabs::emit_gui_changed_for_members (RouteGroup* rg)
644 {
645         for (RouteList::iterator i = rg->route_list()->begin(); i != rg->route_list()->end(); ++i) {
646                 (*i)->gui_changed (X_("color"), 0);
647         }
648 }