f7ceb9e9961421b7ac57e8e0d6c46b53e27b4116
[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 void
307 GroupTabs::add_new_from_items (Menu_Helpers::MenuList& items)
308 {
309         using namespace Menu_Helpers;
310         Menu *new_from;
311
312         new_from = new Menu;
313         {
314                 MenuList& f = new_from->items ();
315                 f.push_back (MenuElem (_("Selection..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_selection), false)));
316                 f.push_back (MenuElem (_("Record Enabled..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_rec_enabled), false)));
317                 f.push_back (MenuElem (_("Soloed..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_soloed), false)));
318         }
319         items.push_back (MenuElem (_("Create New Group From..."), *new_from));
320
321         new_from = new Menu;
322         {
323                 MenuList& f = new_from->items ();
324                 f.push_back (MenuElem (_("Selection..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_selection), true)));
325                 f.push_back (MenuElem (_("Record Enabled..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_rec_enabled), true)));
326                 f.push_back (MenuElem (_("Soloed..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_soloed), true)));
327         }
328         items.push_back (MenuElem (_("Create New Group with Master From..."), *new_from));
329 }
330
331 Gtk::Menu*
332 GroupTabs::get_menu (RouteGroup* g, bool in_tab_area)
333 {
334         using namespace Menu_Helpers;
335
336         delete _menu;
337
338         _menu = new Menu;
339         _menu->set_name ("ArdourContextMenu");
340
341         MenuList& items = _menu->items();
342         Menu* vca_menu;
343
344         const VCAList vcas = _session->vca_manager().vcas ();
345
346         if (!in_tab_area) {
347                 items.push_back (MenuElem (_("Create New Group ..."), hide_return (sigc::mem_fun(*this, &GroupTabs::create_and_add_group))));
348                 items.push_back (MenuElem (_("Create New Group with Control Master ..."), hide_return (sigc::mem_fun(*this, &GroupTabs::create_and_add_group_with_master))));
349
350                 /* context menu is not for a group tab, show the "create new
351                    from" items here
352                 */
353                 add_new_from_items (items);
354         }
355
356         if (g) {
357                 items.push_back (SeparatorElem());
358                 items.push_back (MenuElem (_("Edit Group..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::edit_group), g)));
359                 items.push_back (MenuElem (_("Collect Group"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::collect), g)));
360                 items.push_back (MenuElem (_("Remove Group"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::remove_group), g)));
361
362                 items.push_back (SeparatorElem());
363
364                 vca_menu = new Menu;
365                 MenuList& f (vca_menu->items());
366                 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_group_to_master), 0, g, true)));
367
368                 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
369                         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)));
370                 }
371                 items.push_back (MenuElem (_("Assign Group to Control Master..."), *vca_menu));
372
373
374                 items.push_back (SeparatorElem());
375
376                 if (g->has_subgroup()) {
377                         items.push_back (MenuElem (_("Remove Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::un_subgroup), g)));
378                 } else {
379                         items.push_back (MenuElem (_("Add New Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, false, PreFader)));
380                 }
381                 items.push_back (MenuElem (_("Add New Aux Bus (pre-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PreFader)));
382                 items.push_back (MenuElem (_("Add New Aux Bus (post-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PostFader)));
383                 items.push_back (SeparatorElem());
384
385         }
386
387         add_menu_items (_menu, g);
388
389         if (in_tab_area) {
390                 /* context menu is for a group tab, show the "create new
391                    from" items here
392                 */
393                 add_new_from_items (items);
394         }
395
396         items.push_back (SeparatorElem());
397
398         vca_menu = new Menu;
399         {
400                 MenuList& f (vca_menu->items());
401                 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), 0)));
402                 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
403                         f.push_back (MenuElem (string_compose ("VCA %1", (*v)->number()), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), (*v)->number())));
404                 }
405         }
406
407         items.push_back (MenuElem (_("Assign Selection to Control Master..."), *vca_menu));
408
409         vca_menu = new Menu;
410         {
411                 MenuList& f (vca_menu->items());
412                 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), 0)));
413                 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
414                         f.push_back (MenuElem (string_compose ("VCA %1", (*v)->number()), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), (*v)->number())));
415                 }
416
417         }
418         items.push_back (MenuElem (_("Assign Record Enabled to Control Master..."), *vca_menu));
419
420         vca_menu = new Menu;
421         {
422                 MenuList& f (vca_menu->items());
423                 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), 0)));
424                 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
425                         f.push_back (MenuElem (string_compose ("VCA %1", (*v)->number()), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), (*v)->number())));
426                 }
427
428         }
429         items.push_back (MenuElem (_("Assign Soloed to Control Master...")));
430
431         items.push_back (SeparatorElem());
432         items.push_back (MenuElem (_("Enable All Groups"), sigc::mem_fun(*this, &GroupTabs::activate_all)));
433         items.push_back (MenuElem (_("Disable All Groups"), sigc::mem_fun(*this, &GroupTabs::disable_all)));
434
435         return _menu;
436 }
437
438 void
439 GroupTabs::assign_group_to_master (uint32_t which, RouteGroup* group, bool rename_master)
440 {
441         if (!_session || !group) {
442                 return;
443         }
444
445         boost::shared_ptr<VCA> master;
446
447         if (which == 0) {
448                 if (_session->vca_manager().create_vca (1)) {
449                         /* error */
450                         return;
451                 }
452
453                 /* VCAs use 1-based counting. Get most recently created VCA... */
454                 which = _session->vca_manager().n_vcas();
455         }
456
457         master = _session->vca_manager().vca_by_number (which);
458
459         if (!master) {
460                 /* should never happen; if it does, basically something deeply
461                    odd happened, no reason to tell user because there's no
462                    sensible explanation.
463                 */
464                 return;
465         }
466
467         group->assign_master (master);
468
469         if (rename_master){
470                 master->set_name (group->name());
471         }
472 }
473
474 void
475 GroupTabs::assign_some_to_master (uint32_t which, RouteList rl)
476 {
477         if (!_session) {
478                 return;
479         }
480
481         boost::shared_ptr<VCA> master;
482
483         if (which == 0) {
484                 if (_session->vca_manager().create_vca (1)) {
485                         /* error */
486                         return;
487                 }
488
489                 /* VCAs use 1-based counting. Get most recently created VCA... */
490                 which = _session->vca_manager().n_vcas();
491         }
492
493         master = _session->vca_manager().vca_by_number (which);
494
495         if (!master) {
496                 /* should never happen; if it does, basically something deeply
497                    odd happened, no reason to tell user because there's no
498                    sensible explanation.
499                 */
500                 return;
501         }
502
503
504         if (rl.empty()) {
505                 return;
506         }
507
508         for (RouteList::iterator r = rl.begin(); r != rl.end(); ++r) {
509                 (*r)->assign (master);
510         }
511 }
512
513 RouteList
514 GroupTabs::get_rec_enabled ()
515 {
516         RouteList rec_enabled;
517
518         if (!_session) {
519                 return rec_enabled;
520         }
521
522         boost::shared_ptr<RouteList> rl = _session->get_routes ();
523
524         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
525                 boost::shared_ptr<Track> trk (boost::dynamic_pointer_cast<Track> (*i));
526                 if (trk && trk->rec_enable_control()->get_value()) {
527                         rec_enabled.push_back (*i);
528                 }
529         }
530
531         return rec_enabled;
532 }
533
534
535 RouteList
536 GroupTabs::get_soloed ()
537 {
538         boost::shared_ptr<RouteList> rl = _session->get_routes ();
539
540         RouteList soloed;
541
542         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
543                 if (!(*i)->is_master() && (*i)->soloed()) {
544                         soloed.push_back (*i);
545                 }
546         }
547
548         return soloed;
549 }
550
551 void
552 GroupTabs::assign_selection_to_master (uint32_t which)
553 {
554         assign_some_to_master (which, selected_routes ());
555 }
556
557 void
558 GroupTabs::assign_recenabled_to_master (uint32_t which)
559 {
560         assign_some_to_master (which, get_rec_enabled());
561 }
562
563 void
564 GroupTabs::assign_soloed_to_master (uint32_t which)
565 {
566         assign_some_to_master (which, get_soloed());
567 }
568
569 void
570 GroupTabs::new_from_selection (bool with_master)
571 {
572         run_new_group_dialog (selected_routes(), with_master);
573 }
574
575 void
576 GroupTabs::new_from_rec_enabled (bool with_master)
577 {
578         run_new_group_dialog (get_rec_enabled(), with_master);
579 }
580
581 void
582 GroupTabs::new_from_soloed (bool with_master)
583 {
584         run_new_group_dialog (get_soloed(), with_master);
585 }
586
587 void
588 GroupTabs::run_new_group_dialog (RouteList const & rl, bool with_master)
589 {
590         if (rl.empty()) {
591                 return;
592         }
593
594         RouteGroup* g = new RouteGroup (*_session, "");
595         RouteGroupDialog d (g, true);
596
597         if (d.do_run ()) {
598                 delete g;
599         } else {
600                 _session->add_route_group (g);
601                 for (RouteList::const_iterator i = rl.begin(); i != rl.end(); ++i) {
602                         g->add (*i);
603                 }
604
605                 if (with_master) {
606                         assign_group_to_master (0, g, true); /* zero => new master */
607                 }
608         }
609 }
610
611 RouteGroup *
612 GroupTabs::create_and_add_group () const
613 {
614         RouteGroup* g = new RouteGroup (*_session, "");
615         RouteGroupDialog d (g, true);
616
617         if (d.do_run ()) {
618                 delete g;
619                 return 0;
620         }
621
622         _session->add_route_group (g);
623         return g;
624 }
625
626 RouteGroup *
627 GroupTabs::create_and_add_master () const
628 {
629         return 0;
630 }
631
632 RouteGroup *
633 GroupTabs::create_and_add_group_with_master () const
634 {
635         RouteGroup* g = new RouteGroup (*_session, "");
636         RouteGroupDialog d (g, true);
637
638         if (d.do_run ()) {
639                 delete g;
640                 return 0;
641         }
642
643         _session->add_route_group (g);
644         return g;
645 }
646
647 void
648 GroupTabs::edit_group (RouteGroup* g)
649 {
650         RouteGroupDialog d (g, false);
651         d.do_run ();
652 }
653
654 void
655 GroupTabs::subgroup (RouteGroup* g, bool aux, Placement placement)
656 {
657         g->make_subgroup (aux, placement);
658 }
659
660 void
661 GroupTabs::un_subgroup (RouteGroup* g)
662 {
663         g->destroy_subgroup ();
664 }
665
666 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
667  *  @param g Group to collect.
668  */
669 void
670 GroupTabs::collect (RouteGroup* g)
671 {
672         boost::shared_ptr<RouteList> group_routes = g->route_list ();
673         group_routes->sort (Stripable::PresentationOrderSorter());
674         int const N = group_routes->size ();
675
676         RouteList::iterator i = group_routes->begin ();
677         boost::shared_ptr<RouteList> routes = _session->get_routes ();
678         routes->sort (Stripable::PresentationOrderSorter());
679         RouteList::const_iterator j = routes->begin ();
680
681         int diff = 0;
682         int coll = -1;
683         while (i != group_routes->end() && j != routes->end()) {
684
685                 PresentationInfo::order_t const k = (*j)->presentation_info ().order();
686
687                 if (*i == *j) {
688
689                         if (coll == -1) {
690                                 coll = k;
691                                 diff = N - 1;
692                         } else {
693                                 --diff;
694                         }
695
696                         (*j)->set_presentation_order_explicit (coll);
697
698                         ++coll;
699                         ++i;
700
701                 } else {
702
703                         (*j)->set_presentation_order_explicit (k + diff);
704
705                 }
706
707                 ++j;
708         }
709
710         _session->notify_presentation_info_change ();
711 }
712
713 void
714 GroupTabs::activate_all ()
715 {
716         _session->foreach_route_group (
717                 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
718                 );
719 }
720
721 void
722 GroupTabs::disable_all ()
723 {
724         _session->foreach_route_group (
725                 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
726                 );
727 }
728
729 void
730 GroupTabs::set_activation (RouteGroup* g, bool a)
731 {
732         g->set_active (a, this);
733 }
734
735 void
736 GroupTabs::remove_group (RouteGroup* g)
737 {
738         _session->remove_route_group (*g);
739 }
740
741 /** Set the color of the tab of a route group */
742 void
743 GroupTabs::set_group_color (RouteGroup* group, uint32_t color)
744 {
745         assert (group);
746         uint32_t r, g, b, a;
747
748         UINT_TO_RGBA (color, &r, &g, &b, &a);
749
750         /* Hack to disallow black route groups; force a dark grey instead */
751         const uint32_t dark_gray = 25;
752
753         if (r < dark_gray && g < dark_gray && b < dark_gray) {
754                 r = dark_gray;
755                 g = dark_gray;
756                 b = dark_gray;
757         }
758
759         GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
760
761         char buf[64];
762
763         /* for historical reasons the colors must be stored as 16 bit color
764          * values. Ugh.
765          */
766
767         snprintf (buf, sizeof (buf), "%d:%d:%d", (r<<8), (g<<8), (b<<8));
768         gui_state.set_property (group_gui_id (group), "color", buf);
769
770         /* the group color change notification */
771
772         PBD::PropertyChange change;
773         change.add (Properties::color);
774         group->PropertyChanged (change);
775
776         /* This is a bit of a hack, but this might change
777            our route's effective color, so emit gui_changed
778            for our routes.
779         */
780
781         emit_gui_changed_for_members (group);
782 }
783
784 /** @return the ID string to use for the GUI state of a route group */
785 string
786 GroupTabs::group_gui_id (RouteGroup* group)
787 {
788         assert (group);
789
790         char buf[64];
791         snprintf (buf, sizeof (buf), "route_group %s", group->id().to_s().c_str ());
792
793         return buf;
794 }
795
796 /** @return the color to use for a route group tab */
797 uint32_t
798 GroupTabs::group_color (RouteGroup* group)
799 {
800         assert (group);
801
802         GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
803         string const gui_id = group_gui_id (group);
804         bool empty;
805         string const color = gui_state.get_string (gui_id, "color", &empty);
806
807         if (empty) {
808                 /* no color has yet been set, so use a random one */
809                 uint32_t c = gdk_color_to_rgba (unique_random_color (_used_colors));
810                 set_group_color (group, c);
811                 return c;
812         }
813
814         int r, g, b;
815
816         /* for historical reasons, colors are stored as 16 bit values.
817          */
818
819         sscanf (color.c_str(), "%d:%d:%d", &r, &g, &b);
820
821         r /= 256;
822         g /= 256;
823         b /= 256;
824
825         return RGBA_TO_UINT (r, g, b, 255);
826 }
827
828 void
829 GroupTabs::route_group_property_changed (RouteGroup* rg)
830 {
831         /* This is a bit of a hack, but this might change
832            our route's effective color, so emit gui_changed
833            for our routes.
834         */
835
836         emit_gui_changed_for_members (rg);
837
838         set_dirty ();
839 }
840
841 void
842 GroupTabs::route_added_to_route_group (RouteGroup*, boost::weak_ptr<Route> w)
843 {
844         /* Similarly-spirited hack as in route_group_property_changed */
845
846         boost::shared_ptr<Route> r = w.lock ();
847         if (!r) {
848                 return;
849         }
850
851         r->presentation_info().PropertyChanged (Properties::color);
852
853         set_dirty ();
854 }
855
856 void
857 GroupTabs::route_removed_from_route_group (RouteGroup*, boost::weak_ptr<Route> w)
858 {
859         /* Similarly-spirited hack as in route_group_property_changed */
860
861         boost::shared_ptr<Route> r = w.lock ();
862         if (!r) {
863                 return;
864         }
865
866         r->presentation_info().PropertyChanged (Properties::color);
867
868         set_dirty ();
869 }
870
871 void
872 GroupTabs::emit_gui_changed_for_members (RouteGroup* rg)
873 {
874         for (RouteList::iterator i = rg->route_list()->begin(); i != rg->route_list()->end(); ++i) {
875                 (*i)->gui_changed (X_("color"), 0);
876         }
877 }