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