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