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