Fix thinkos in cubasish theme
[ardour.git] / gtk2_ardour / group_tabs.cc
1 /*
2  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <gtkmm/stock.h>
24
25 #include "ardour/session.h"
26 #include "ardour/route_group.h"
27 #include "ardour/route.h"
28 #include "ardour/vca_manager.h"
29 #include "ardour/vca.h"
30
31 #include "gtkmm2ext/doi.h"
32
33 #include "gui_thread.h"
34 #include "route_group_dialog.h"
35 #include "group_tabs.h"
36 #include "keyboard.h"
37 #include "pbd/i18n.h"
38 #include "ardour_ui.h"
39 #include "rgb_macros.h"
40 #include "ui_config.h"
41 #include "utils.h"
42
43 using namespace std;
44 using namespace Gtk;
45 using namespace ARDOUR;
46 using namespace ARDOUR_UI_UTILS;
47 using Gtkmm2ext::Keyboard;
48
49 list<Gdk::Color> GroupTabs::_used_colors;
50
51 GroupTabs::GroupTabs ()
52         : _menu (0)
53         , _dragging (0)
54         , _dragging_new_tab (0)
55 {
56         add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
57         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &GroupTabs::queue_draw));
58 }
59
60 GroupTabs::~GroupTabs ()
61 {
62         delete _menu;
63 }
64
65 void
66 GroupTabs::set_session (Session* s)
67 {
68         SessionHandlePtr::set_session (s);
69
70         if (_session) {
71                 _session->RouteGroupPropertyChanged.connect (
72                         _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_group_property_changed, this, _1), gui_context()
73                         );
74                 _session->RouteAddedToRouteGroup.connect (
75                         _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_added_to_route_group, this, _1, _2), gui_context()
76                         );
77                 _session->RouteRemovedFromRouteGroup.connect (
78                         _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_removed_from_route_group, this, _1, _2), gui_context()
79                         );
80
81                 _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&GroupTabs::set_dirty, this, (cairo_rectangle_t*)0), gui_context());
82         }
83 }
84
85
86 /** Handle a size request.
87  *  @param req GTK requisition
88  */
89 void
90 GroupTabs::on_size_request (Gtk::Requisition *req)
91 {
92         req->width = std::max (16.f, rintf (16.f * UIConfiguration::instance().get_ui_scale()));
93         req->height = std::max (16.f, rintf (16.f * UIConfiguration::instance().get_ui_scale()));
94 }
95
96 bool
97 GroupTabs::on_button_press_event (GdkEventButton* ev)
98 {
99         using namespace Menu_Helpers;
100
101         double const p = primary_coordinate (ev->x, ev->y);
102
103         list<Tab>::iterator prev;
104         list<Tab>::iterator next;
105         Tab* t = click_to_tab (p, &prev, &next);
106
107         _drag_min = prev != _tabs.end() ? prev->to : 0;
108         _drag_max = next != _tabs.end() ? next->from : extent ();
109
110         if (ev->button == 1) {
111
112                 if (t == 0) {
113                         Tab n;
114                         n.from = n.to = p;
115                         _dragging_new_tab = true;
116
117                         if (next == _tabs.end()) {
118                                 _tabs.push_back (n);
119                                 t = &_tabs.back ();
120                         } else {
121                                 list<Tab>::iterator j = _tabs.insert (next, n);
122                                 t = &(*j);
123                         }
124
125                 } else {
126                         _dragging_new_tab = false;
127                         _initial_dragging_routes = routes_for_tab (t);
128                 }
129
130                 _dragging = t;
131                 _drag_moved = false;
132                 _drag_first = p;
133
134                 double const h = (t->from + t->to) / 2;
135                 if (p < h) {
136                         _drag_moving = t->from;
137                         _drag_fixed = t->to;
138                         _drag_offset = p - t->from;
139                 } else {
140                         _drag_moving = t->to;
141                         _drag_fixed = t->from;
142                         _drag_offset = p - t->to;
143                 }
144
145         } else if (ev->button == 3) {
146
147                 RouteGroup* g = t ? t->group : 0;
148
149                 if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier) && g) {
150                         remove_group (g);
151                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier) && g) {
152                         edit_group (g);
153                 } else {
154                         Menu* m = get_menu (g, true);
155                         if (m) {
156                                 m->popup (ev->button, ev->time);
157                         }
158                 }
159         }
160
161         return true;
162 }
163
164
165 bool
166 GroupTabs::on_motion_notify_event (GdkEventMotion* ev)
167 {
168         if (_dragging == 0) {
169                 return false;
170         }
171
172         double const p = primary_coordinate (ev->x, ev->y);
173
174         if (p != _drag_first) {
175                 _drag_moved = true;
176         }
177
178         _drag_moving = p - _drag_offset;
179
180         _dragging->from = min (_drag_moving, _drag_fixed);
181         _dragging->to = max (_drag_moving, _drag_fixed);
182
183         _dragging->from = max (_dragging->from, _drag_min);
184         _dragging->to = min (_dragging->to, _drag_max);
185
186         set_dirty ();
187         queue_draw ();
188
189         gdk_event_request_motions(ev);
190
191         return true;
192 }
193
194
195 bool
196 GroupTabs::on_button_release_event (GdkEventButton*)
197 {
198         if (_dragging == 0) {
199                 return false;
200         }
201
202         if (!_drag_moved) {
203
204                 if (_dragging->group) {
205                         /* toggle active state */
206                         _dragging->group->set_active (!_dragging->group->is_active (), this);
207                 }
208
209         } else {
210                 /* finish drag */
211                 RouteList routes = routes_for_tab (_dragging);
212
213                 if (!routes.empty()) {
214                         if (_dragging_new_tab) {
215                                 run_new_group_dialog (&routes, false);
216                         } else {
217                                 boost::shared_ptr<RouteList> r = _session->get_routes ();
218                                 /* First add new ones, then remove old ones.
219                                  * We cannot allow the group to become temporarily empty, because
220                                  * Session::route_removed_from_route_group() will delete empty groups.
221                                  */
222                                 for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
223                                         /* RouteGroup::add () ignores routes already present in the set */
224                                         _dragging->group->add (*i);
225                                 }
226                                 for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
227
228                                         bool const was_in_tab = find (
229                                                 _initial_dragging_routes.begin(), _initial_dragging_routes.end(), *i
230                                                 ) != _initial_dragging_routes.end ();
231
232                                         bool const now_in_tab = find (routes.begin(), routes.end(), *i) != routes.end();
233
234                                         if (was_in_tab && !now_in_tab) {
235                                                 _dragging->group->remove (*i);
236                                         }
237                                 }
238
239                         }
240                 }
241
242                 set_dirty ();
243                 queue_draw ();
244         }
245
246         _dragging = 0;
247         _initial_dragging_routes.clear ();
248
249         return true;
250 }
251
252 void
253 GroupTabs::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
254 {
255         cairo_t* cr = ctx->cobj();
256         if (_dragging == 0) {
257                 _tabs = compute_tabs ();
258         }
259
260         /* background */
261
262         Gdk::Color c = get_style()->get_base (Gtk::STATE_NORMAL);
263
264         cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
265         cairo_rectangle (cr, 0, 0, get_width(), get_height());
266         cairo_fill (cr);
267
268         /* tabs */
269
270         for (list<Tab>::const_iterator i = _tabs.begin(); i != _tabs.end(); ++i) {
271                 draw_tab (cr, *i);
272         }
273 }
274
275
276 /** Convert a click position to a tab.
277  *  @param c Click position.
278  *  @param prev Filled in with the previous tab to the click, or _tabs.end().
279  *  @param next Filled in with the next tab after the click, or _tabs.end().
280  *  @return Tab under the click, or 0.
281  */
282
283 GroupTabs::Tab *
284 GroupTabs::click_to_tab (double c, list<Tab>::iterator* prev, list<Tab>::iterator* next)
285 {
286         *prev = *next = _tabs.end ();
287         Tab* under = 0;
288
289         list<Tab>::iterator i = _tabs.begin ();
290         while (i != _tabs.end()) {
291
292                 if (i->from > c) {
293                         *next = i;
294                         break;
295                 }
296
297                 if (i->to < c) {
298                         *prev = i;
299                         ++i;
300                         continue;
301                 }
302
303                 if (i->from <= c && c < i->to) {
304                         under = &(*i);
305                 }
306
307                 ++i;
308         }
309
310         return under;
311 }
312
313 void
314 GroupTabs::add_new_from_items (Menu_Helpers::MenuList& items)
315 {
316         using namespace Menu_Helpers;
317         Menu *new_from;
318
319         new_from = manage (new Menu);
320         {
321                 MenuList& f = new_from->items ();
322                 f.push_back (MenuElem (_("Selection..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_selection), false)));
323                 f.push_back (MenuElem (_("Record Enabled..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_rec_enabled), false)));
324                 f.push_back (MenuElem (_("Soloed..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_soloed), false)));
325         }
326         items.push_back (MenuElem (_("Create New Group From..."), *new_from));
327
328         new_from = manage (new Menu);
329         {
330                 MenuList& f = new_from->items ();
331                 f.push_back (MenuElem (_("Selection..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_selection), true)));
332                 f.push_back (MenuElem (_("Record Enabled..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_rec_enabled), true)));
333                 f.push_back (MenuElem (_("Soloed..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_soloed), true)));
334         }
335         items.push_back (MenuElem (_("Create New Group with Master From..."), *new_from));
336 }
337
338 Gtk::Menu*
339 GroupTabs::get_menu (RouteGroup* g, bool in_tab_area)
340 {
341         using namespace Menu_Helpers;
342
343         delete _menu;
344
345         _menu = new Menu;
346         _menu->set_name ("ArdourContextMenu");
347
348         MenuList& items = _menu->items();
349         Menu* vca_menu;
350
351         const VCAList vcas = _session->vca_manager().vcas ();
352
353         if (!in_tab_area) {
354                 /* context menu is not for a group tab, show the "create new
355                    from" items here
356                 */
357                 add_new_from_items (items);
358         }
359
360         if (g) {
361                 items.push_back (SeparatorElem());
362                 items.push_back (MenuElem (_("Edit Group..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::edit_group), g)));
363                 items.push_back (MenuElem (_("Collect Group"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::collect), g)));
364                 items.push_back (MenuElem (_("Remove Group"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::remove_group), g)));
365
366                 items.push_back (SeparatorElem());
367
368                 if (g->has_control_master()) {
369                         items.push_back (MenuElem (_("Drop Group from VCA..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::unassign_group_to_master), g->group_master_number(), g)));
370                 } else {
371                         vca_menu = manage (new Menu);
372                         MenuList& f (vca_menu->items());
373                         f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_group_to_master), 0, g, true)));
374
375                         for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
376                                 f.push_back (MenuElem ((*v)->name().empty() ? string_compose ("VCA %1", (*v)->number()) : (*v)->name(), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_group_to_master), (*v)->number(), g, true)));
377                         }
378                         items.push_back (MenuElem (_("Assign Group to VCA..."), *vca_menu));
379                 }
380
381                 items.push_back (SeparatorElem());
382
383                 bool can_subgroup = true;
384                 boost::shared_ptr<RouteList> rl = g->route_list();
385                 for (RouteList::const_iterator i = rl->begin(); i != rl->end(); ++i) {
386 #ifdef MIXBUS
387                         if ((*i)->mixbus ()) {
388                                 can_subgroup = false;
389                                 break;
390                         }
391 #endif
392                         if ((*i)->output()->n_ports().n_midi() != 0) {
393                                 can_subgroup = false;
394                                 break;
395                         }
396                 }
397
398                 if (g->has_subgroup ()) {
399                         items.push_back (MenuElem (_("Remove Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::un_subgroup), g)));
400                 } else if (can_subgroup) {
401                         items.push_back (MenuElem (_("Add New Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, false, PreFader)));
402                 }
403
404                 if (can_subgroup) {
405                         items.push_back (MenuElem (_("Add New Aux Bus (pre-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PreFader)));
406                         items.push_back (MenuElem (_("Add New Aux Bus (post-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PostFader)));
407                 }
408
409                 if (can_subgroup || g->has_subgroup ()) {
410                         items.push_back (SeparatorElem());
411                 }
412         }
413
414         add_menu_items (_menu, g);
415
416         if (in_tab_area) {
417                 /* context menu is for a group tab, show the "create new
418                    from" items here
419                 */
420                 add_new_from_items (items);
421         }
422
423         items.push_back (SeparatorElem());
424
425         vca_menu = manage (new Menu);
426         {
427                 MenuList& f (vca_menu->items());
428                 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), 0)));
429                 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
430                         f.push_back (MenuElem ((*v)->name().empty() ? string_compose ("VCA %1", (*v)->number()) : (*v)->name(), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), (*v)->number())));
431                 }
432         }
433
434         items.push_back (MenuElem (_("Assign Selection to VCA..."), *vca_menu));
435
436         vca_menu = manage (new Menu);
437         {
438                 MenuList& f (vca_menu->items());
439                 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_recenabled_to_master), 0)));
440                 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
441                         f.push_back (MenuElem ((*v)->name().empty() ? string_compose ("VCA %1", (*v)->number()) : (*v)->name(), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_recenabled_to_master), (*v)->number())));
442                 }
443
444         }
445         items.push_back (MenuElem (_("Assign Record Enabled to VCA..."), *vca_menu));
446
447         vca_menu = manage (new Menu);
448         {
449                 MenuList& f (vca_menu->items());
450                 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_soloed_to_master), 0)));
451                 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
452                         f.push_back (MenuElem ((*v)->name().empty() ? string_compose ("VCA %1", (*v)->number()) : (*v)->name(), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_soloed_to_master), (*v)->number())));
453                 }
454
455         }
456         items.push_back (MenuElem (_("Assign Soloed to VCA..."), *vca_menu));
457
458         items.push_back (SeparatorElem());
459         items.push_back (MenuElem (_("Enable All Groups"), sigc::mem_fun(*this, &GroupTabs::activate_all)));
460         items.push_back (MenuElem (_("Disable All Groups"), sigc::mem_fun(*this, &GroupTabs::disable_all)));
461
462         return _menu;
463 }
464
465 void
466 GroupTabs::assign_group_to_master (uint32_t which, RouteGroup* group, bool rename_master) const
467 {
468         if (!_session || !group) {
469                 return;
470         }
471
472         boost::shared_ptr<VCA> master;
473
474         if (which == 0) {
475                 if (_session->vca_manager().create_vca (1).empty ()) {
476                         /* error */
477                         return;
478                 }
479
480                 /* Get most recently created VCA... */
481                 which = _session->vca_manager().vcas().back()->number();
482         }
483
484         master = _session->vca_manager().vca_by_number (which);
485
486         if (!master) {
487                 /* should never happen; if it does, basically something deeply
488                    odd happened, no reason to tell user because there's no
489                    sensible explanation.
490                 */
491                 return;
492         }
493
494         group->assign_master (master);
495
496         if (rename_master){
497                 master->set_name (group->name());
498         }
499 }
500
501 void
502 GroupTabs::unassign_group_to_master (uint32_t which, RouteGroup* group) const
503 {
504         if (!_session || !group) {
505                 return;
506         }
507
508         boost::shared_ptr<VCA> master = _session->vca_manager().vca_by_number (which);
509
510         if (!master) {
511                 /* should never happen; if it does, basically something deeply
512                    odd happened, no reason to tell user because there's no
513                    sensible explanation.
514                 */
515                 return;
516         }
517
518         group->unassign_master (master);
519 }
520
521 void
522 GroupTabs::assign_some_to_master (uint32_t which, RouteList rl, std::string vcaname)
523 {
524         if (!_session) {
525                 return;
526         }
527
528         boost::shared_ptr<VCA> master;
529         bool set_name = false;
530
531         if (which == 0) {
532                 if (_session->vca_manager().create_vca (1).empty ()) {
533                         /* error */
534                         return;
535                 }
536                 set_name = true;
537
538                 /* Get most recently created VCA... */
539                 which = _session->vca_manager().vcas().back()->number();
540         }
541
542         master = _session->vca_manager().vca_by_number (which);
543
544         if (!master) {
545                 /* should never happen; if it does, basically something deeply
546                    odd happened, no reason to tell user because there's no
547                    sensible explanation.
548                 */
549                 return;
550         }
551
552
553         if (rl.empty()) {
554                 return;
555         }
556
557         for (RouteList::iterator r = rl.begin(); r != rl.end(); ++r) {
558                 (*r)->assign (master);
559         }
560         if (set_name && !vcaname.empty()) {
561                 master->set_name (vcaname);
562         }
563 }
564
565 RouteList
566 GroupTabs::get_rec_enabled ()
567 {
568         RouteList rec_enabled;
569
570         if (!_session) {
571                 return rec_enabled;
572         }
573
574         boost::shared_ptr<RouteList> rl = _session->get_routes ();
575
576         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
577                 boost::shared_ptr<Track> trk (boost::dynamic_pointer_cast<Track> (*i));
578                 if (trk && trk->rec_enable_control()->get_value()) {
579                         rec_enabled.push_back (*i);
580                 }
581         }
582
583         return rec_enabled;
584 }
585
586
587 RouteList
588 GroupTabs::get_soloed ()
589 {
590         RouteList rl = _session->get_routelist ();
591         RouteList soloed;
592
593         for (RouteList::iterator i = rl.begin(); i != rl.end(); ++i) {
594                 if (!(*i)->is_master() && (*i)->soloed()) {
595                         soloed.push_back (*i);
596                 }
597         }
598
599         return soloed;
600 }
601
602 void
603 GroupTabs::assign_selection_to_master (uint32_t which)
604 {
605         assign_some_to_master (which, selected_routes (), _("Selection"));
606 }
607
608 void
609 GroupTabs::assign_recenabled_to_master (uint32_t which)
610 {
611         assign_some_to_master (which, get_rec_enabled());
612 }
613
614 void
615 GroupTabs::assign_soloed_to_master (uint32_t which)
616 {
617         assign_some_to_master (which, get_soloed());
618 }
619
620 void
621 GroupTabs::new_from_selection (bool with_master)
622 {
623         RouteList rl (selected_routes());
624         run_new_group_dialog (&rl, with_master);
625 }
626
627 void
628 GroupTabs::new_from_rec_enabled (bool with_master)
629 {
630         RouteList rl (get_rec_enabled());
631         run_new_group_dialog (&rl, with_master);
632 }
633
634 void
635 GroupTabs::new_from_soloed (bool with_master)
636 {
637         RouteList rl (get_soloed());
638         run_new_group_dialog (&rl, with_master);
639 }
640
641 void
642 GroupTabs::run_new_group_dialog (RouteList const * rl, bool with_master)
643 {
644         if (rl && rl->empty()) {
645                 return;
646         }
647
648         RouteGroup* g = new RouteGroup (*_session, "");
649         RouteGroupDialog* d = new RouteGroupDialog (g, true);
650
651         d->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_group_dialog_finished), d, rl ? new RouteList (*rl): 0, with_master));
652         d->present ();
653 }
654
655 void
656 GroupTabs::new_group_dialog_finished (int r, RouteGroupDialog* d, RouteList const * rl, bool with_master) const
657 {
658         if (r == RESPONSE_OK) {
659
660                 if (!d->name_check()) {
661                         return;
662                 }
663
664                 _session->add_route_group (d->group());
665
666                 if (rl) {
667                         for (RouteList::const_iterator i = rl->begin(); i != rl->end(); ++i) {
668                                 d->group()->add (*i);
669                         }
670
671                         if (with_master) {
672                                 assign_group_to_master (0, d->group(), true); /* zero => new master */
673                         }
674                 }
675         } else {
676                 delete d->group ();
677         }
678
679         delete rl;
680         delete_when_idle (d);
681 }
682
683 void
684 GroupTabs::edit_group (RouteGroup* g)
685 {
686         RouteGroupDialog* d = new RouteGroupDialog (g, false);
687         d->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &GroupTabs::edit_group_dialog_finished), d));
688         d->present ();
689 }
690
691 void
692 GroupTabs::edit_group_dialog_finished (int r, RouteGroupDialog* d) const
693 {
694         delete_when_idle (d);
695 }
696
697 void
698 GroupTabs::subgroup (RouteGroup* g, bool aux, Placement placement)
699 {
700         g->make_subgroup (aux, placement);
701 }
702
703 void
704 GroupTabs::un_subgroup (RouteGroup* g)
705 {
706         g->destroy_subgroup ();
707 }
708
709 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
710  *  @param g Group to collect.
711  */
712 void
713 GroupTabs::collect (RouteGroup* g)
714 {
715         boost::shared_ptr<RouteList> group_routes = g->route_list ();
716         group_routes->sort (Stripable::Sorter());
717         int const N = group_routes->size ();
718
719         RouteList::iterator i = group_routes->begin ();
720         RouteList routes = _session->get_routelist ();
721         routes.sort (Stripable::Sorter());
722         RouteList::const_iterator j = routes.begin ();
723
724         int diff = 0;
725         int coll = -1;
726
727         PresentationInfo::ChangeSuspender cs;
728
729         while (i != group_routes->end() && j != routes.end()) {
730
731                 PresentationInfo::order_t const k = (*j)->presentation_info ().order();
732
733                 if (*i == *j) {
734
735                         if (coll == -1) {
736                                 coll = k;
737                                 diff = N - 1;
738                         } else {
739                                 --diff;
740                         }
741
742                         (*j)->set_presentation_order (coll);
743
744                         ++coll;
745                         ++i;
746
747                 } else {
748
749                         (*j)->set_presentation_order (k + diff);
750
751                 }
752
753                 ++j;
754         }
755 }
756
757 void
758 GroupTabs::activate_all ()
759 {
760         _session->foreach_route_group (
761                 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
762                 );
763 }
764
765 void
766 GroupTabs::disable_all ()
767 {
768         _session->foreach_route_group (
769                 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
770                 );
771 }
772
773 void
774 GroupTabs::set_activation (RouteGroup* g, bool a)
775 {
776         g->set_active (a, this);
777 }
778
779 void
780 GroupTabs::remove_group (RouteGroup* g)
781 {
782         boost::shared_ptr<RouteList> rl (g->route_list ());
783         _session->remove_route_group (*g);
784
785         emit_gui_changed_for_members (rl);
786 }
787
788 /** Set the color of the tab of a route group */
789 void
790 GroupTabs::set_group_color (RouteGroup* group, uint32_t color)
791 {
792         assert (group);
793         PresentationInfo::ChangeSuspender cs;
794         group->set_rgba (color);
795 }
796
797 /** @return the ID string to use for the GUI state of a route group */
798 string
799 GroupTabs::group_gui_id (RouteGroup* group)
800 {
801         assert (group);
802
803         char buf[64];
804         snprintf (buf, sizeof (buf), "route_group %s", group->id().to_s().c_str ());
805
806         return buf;
807 }
808
809 /** @return the color to use for a route group tab */
810 uint32_t
811 GroupTabs::group_color (RouteGroup* group)
812 {
813         assert (group);
814
815         /* prefer libardour color, if set */
816         uint32_t rgba = group->rgba ();
817         if (rgba != 0) {
818                 return rgba;
819         }
820
821         /* backwards compatibility, load old color */
822
823         GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
824         string const gui_id = group_gui_id (group);
825         bool empty;
826         string const color = gui_state.get_string (gui_id, "color", &empty);
827
828         if (empty) {
829                 /* no color has yet been set, so use a random one */
830                 uint32_t c = gdk_color_to_rgba (unique_random_color (_used_colors));
831                 set_group_color (group, c);
832                 return c;
833         }
834
835         int r, g, b;
836
837         /* for historical reasons, colors are stored as 16 bit values.  */
838
839         sscanf (color.c_str(), "%d:%d:%d", &r, &g, &b);
840
841         r /= 256;
842         g /= 256;
843         b /= 256;
844
845         group->migrate_rgba (RGBA_TO_UINT (r, g, b, 255));
846         gui_state.remove_node (gui_id);
847
848         return RGBA_TO_UINT (r, g, b, 255);
849 }
850
851 void
852 GroupTabs::route_group_property_changed (RouteGroup* rg)
853 {
854         /* This is a bit of a hack, but this might change
855            our route's effective color, so emit gui_changed
856            for our routes.
857         */
858
859         emit_gui_changed_for_members (rg->route_list ());
860
861         set_dirty ();
862 }
863
864 void
865 GroupTabs::route_added_to_route_group (RouteGroup*, boost::weak_ptr<Route> w)
866 {
867         /* Similarly-spirited hack as in route_group_property_changed */
868
869         boost::shared_ptr<Route> r = w.lock ();
870         if (!r) {
871                 return;
872         }
873
874         r->presentation_info().PropertyChanged (Properties::color);
875
876         set_dirty ();
877 }
878
879 void
880 GroupTabs::route_removed_from_route_group (RouteGroup*, boost::weak_ptr<Route> w)
881 {
882         /* Similarly-spirited hack as in route_group_property_changed */
883
884         boost::shared_ptr<Route> r = w.lock ();
885         if (!r) {
886                 return;
887         }
888
889         r->presentation_info().PropertyChanged (Properties::color);
890
891         set_dirty ();
892 }
893
894 void
895 GroupTabs::emit_gui_changed_for_members (boost::shared_ptr<RouteList> rl)
896 {
897         PresentationInfo::ChangeSuspender cs;
898
899         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
900                 (*i)->presentation_info().PropertyChanged (Properties::color);
901         }
902 }