2 Copyright (C) 2009 Paul Davis
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.
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.
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.
20 #include <gtkmm/stock.h>
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"
28 #include "gtkmm2ext/doi.h"
30 #include "gui_thread.h"
31 #include "route_group_dialog.h"
32 #include "group_tabs.h"
35 #include "ardour_ui.h"
36 #include "rgb_macros.h"
37 #include "ui_config.h"
42 using namespace ARDOUR;
43 using namespace ARDOUR_UI_UTILS;
44 using Gtkmm2ext::Keyboard;
46 list<Gdk::Color> GroupTabs::_used_colors;
48 GroupTabs::GroupTabs ()
51 , _dragging_new_tab (0)
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));
57 GroupTabs::~GroupTabs ()
63 GroupTabs::set_session (Session* s)
65 SessionHandlePtr::set_session (s);
68 _session->RouteGroupPropertyChanged.connect (
69 _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_group_property_changed, this, _1), gui_context()
71 _session->RouteAddedToRouteGroup.connect (
72 _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_added_to_route_group, this, _1, _2), gui_context()
74 _session->RouteRemovedFromRouteGroup.connect (
75 _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_removed_from_route_group, this, _1, _2), gui_context()
78 _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&GroupTabs::set_dirty, this, (cairo_rectangle_t*)0), gui_context());
83 /** Handle a size request.
84 * @param req GTK requisition
87 GroupTabs::on_size_request (Gtk::Requisition *req)
89 /* Use a dummy, small width and the actual height that we want */
95 GroupTabs::on_button_press_event (GdkEventButton* ev)
97 using namespace Menu_Helpers;
99 double const p = primary_coordinate (ev->x, ev->y);
101 list<Tab>::iterator prev;
102 list<Tab>::iterator next;
103 Tab* t = click_to_tab (p, &prev, &next);
105 _drag_min = prev != _tabs.end() ? prev->to : 0;
106 _drag_max = next != _tabs.end() ? next->from : extent ();
108 if (ev->button == 1) {
113 _dragging_new_tab = true;
115 if (next == _tabs.end()) {
119 list<Tab>::iterator j = _tabs.insert (next, n);
124 _dragging_new_tab = false;
125 _initial_dragging_routes = routes_for_tab (t);
132 double const h = (t->from + t->to) / 2;
134 _drag_moving = t->from;
136 _drag_offset = p - t->from;
138 _drag_moving = t->to;
139 _drag_fixed = t->from;
140 _drag_offset = p - t->to;
143 } else if (ev->button == 3) {
145 RouteGroup* g = t ? t->group : 0;
147 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier) && g) {
149 RouteGroupDialog d (g, false);
152 Menu* m = get_menu (g, true);
154 m->popup (ev->button, ev->time);
164 GroupTabs::on_motion_notify_event (GdkEventMotion* ev)
166 if (_dragging == 0) {
170 double const p = primary_coordinate (ev->x, ev->y);
172 if (p != _drag_first) {
176 _drag_moving = p - _drag_offset;
178 _dragging->from = min (_drag_moving, _drag_fixed);
179 _dragging->to = max (_drag_moving, _drag_fixed);
181 _dragging->from = max (_dragging->from, _drag_min);
182 _dragging->to = min (_dragging->to, _drag_max);
187 gdk_event_request_motions(ev);
194 GroupTabs::on_button_release_event (GdkEventButton*)
196 if (_dragging == 0) {
202 if (_dragging->group) {
203 /* toggle active state */
204 _dragging->group->set_active (!_dragging->group->is_active (), this);
209 RouteList routes = routes_for_tab (_dragging);
211 if (!routes.empty()) {
212 if (_dragging_new_tab) {
213 run_new_group_dialog (&routes, false);
215 boost::shared_ptr<RouteList> r = _session->get_routes ();
216 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
218 bool const was_in_tab = find (
219 _initial_dragging_routes.begin(), _initial_dragging_routes.end(), *i
220 ) != _initial_dragging_routes.end ();
222 bool const now_in_tab = find (routes.begin(), routes.end(), *i) != routes.end();
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);
238 _initial_dragging_routes.clear ();
244 GroupTabs::render (cairo_t* cr, cairo_rectangle_t*)
246 if (_dragging == 0) {
247 _tabs = compute_tabs ();
252 Gdk::Color c = get_style()->get_base (Gtk::STATE_NORMAL);
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());
260 for (list<Tab>::const_iterator i = _tabs.begin(); i != _tabs.end(); ++i) {
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.
274 GroupTabs::click_to_tab (double c, list<Tab>::iterator* prev, list<Tab>::iterator* next)
276 *prev = *next = _tabs.end ();
279 list<Tab>::iterator i = _tabs.begin ();
280 while (i != _tabs.end()) {
293 if (i->from <= c && c < i->to) {
304 GroupTabs::add_new_from_items (Menu_Helpers::MenuList& items)
306 using namespace Menu_Helpers;
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)));
316 items.push_back (MenuElem (_("Create New Group From..."), *new_from));
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)));
325 items.push_back (MenuElem (_("Create New Group with Master From..."), *new_from));
329 GroupTabs::get_menu (RouteGroup* g, bool in_tab_area)
331 using namespace Menu_Helpers;
336 _menu->set_name ("ArdourContextMenu");
338 MenuList& items = _menu->items();
341 const VCAList vcas = _session->vca_manager().vcas ();
344 items.push_back (MenuElem (_("Create New Group ..."), sigc::bind (sigc::mem_fun(*this, &GroupTabs::run_new_group_dialog), (RouteList const *) 0, false)));
345 items.push_back (MenuElem (_("Create New Group with Control Master ..."), sigc::bind (sigc::mem_fun(*this, &GroupTabs::run_new_group_dialog), (RouteList const *) 0, true)));
347 /* context menu is not for a group tab, show the "create new
350 add_new_from_items (items);
354 items.push_back (SeparatorElem());
355 items.push_back (MenuElem (_("Edit Group..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::edit_group), g)));
356 items.push_back (MenuElem (_("Collect Group"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::collect), g)));
357 items.push_back (MenuElem (_("Remove Group"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::remove_group), g)));
359 items.push_back (SeparatorElem());
362 MenuList& f (vca_menu->items());
363 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_group_to_master), 0, g, true)));
365 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
366 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)));
368 items.push_back (MenuElem (_("Assign Group to Control Master..."), *vca_menu));
371 items.push_back (SeparatorElem());
373 if (g->has_subgroup()) {
374 items.push_back (MenuElem (_("Remove Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::un_subgroup), g)));
376 items.push_back (MenuElem (_("Add New Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, false, PreFader)));
378 items.push_back (MenuElem (_("Add New Aux Bus (pre-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PreFader)));
379 items.push_back (MenuElem (_("Add New Aux Bus (post-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PostFader)));
380 items.push_back (SeparatorElem());
384 add_menu_items (_menu, g);
387 /* context menu is for a group tab, show the "create new
390 add_new_from_items (items);
393 items.push_back (SeparatorElem());
397 MenuList& f (vca_menu->items());
398 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), 0)));
399 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
400 f.push_back (MenuElem (string_compose ("VCA %1", (*v)->number()), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), (*v)->number())));
404 items.push_back (MenuElem (_("Assign Selection to Control Master..."), *vca_menu));
408 MenuList& f (vca_menu->items());
409 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), 0)));
410 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
411 f.push_back (MenuElem (string_compose ("VCA %1", (*v)->number()), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), (*v)->number())));
415 items.push_back (MenuElem (_("Assign Record Enabled to Control Master..."), *vca_menu));
419 MenuList& f (vca_menu->items());
420 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), 0)));
421 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
422 f.push_back (MenuElem (string_compose ("VCA %1", (*v)->number()), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), (*v)->number())));
426 items.push_back (MenuElem (_("Assign Soloed to Control Master...")));
428 items.push_back (SeparatorElem());
429 items.push_back (MenuElem (_("Enable All Groups"), sigc::mem_fun(*this, &GroupTabs::activate_all)));
430 items.push_back (MenuElem (_("Disable All Groups"), sigc::mem_fun(*this, &GroupTabs::disable_all)));
436 GroupTabs::assign_group_to_master (uint32_t which, RouteGroup* group, bool rename_master) const
438 if (!_session || !group) {
442 boost::shared_ptr<VCA> master;
445 if (_session->vca_manager().create_vca (1)) {
450 /* VCAs use 1-based counting. Get most recently created VCA... */
451 which = _session->vca_manager().n_vcas();
454 master = _session->vca_manager().vca_by_number (which);
457 /* should never happen; if it does, basically something deeply
458 odd happened, no reason to tell user because there's no
459 sensible explanation.
464 group->assign_master (master);
467 master->set_name (group->name());
472 GroupTabs::assign_some_to_master (uint32_t which, RouteList rl)
478 boost::shared_ptr<VCA> master;
481 if (_session->vca_manager().create_vca (1)) {
486 /* VCAs use 1-based counting. Get most recently created VCA... */
487 which = _session->vca_manager().n_vcas();
490 master = _session->vca_manager().vca_by_number (which);
493 /* should never happen; if it does, basically something deeply
494 odd happened, no reason to tell user because there's no
495 sensible explanation.
505 for (RouteList::iterator r = rl.begin(); r != rl.end(); ++r) {
506 (*r)->assign (master);
511 GroupTabs::get_rec_enabled ()
513 RouteList rec_enabled;
519 boost::shared_ptr<RouteList> rl = _session->get_routes ();
521 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
522 boost::shared_ptr<Track> trk (boost::dynamic_pointer_cast<Track> (*i));
523 if (trk && trk->rec_enable_control()->get_value()) {
524 rec_enabled.push_back (*i);
533 GroupTabs::get_soloed ()
535 boost::shared_ptr<RouteList> rl = _session->get_routes ();
539 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
540 if (!(*i)->is_master() && (*i)->soloed()) {
541 soloed.push_back (*i);
549 GroupTabs::assign_selection_to_master (uint32_t which)
551 assign_some_to_master (which, selected_routes ());
555 GroupTabs::assign_recenabled_to_master (uint32_t which)
557 assign_some_to_master (which, get_rec_enabled());
561 GroupTabs::assign_soloed_to_master (uint32_t which)
563 assign_some_to_master (which, get_soloed());
567 GroupTabs::new_from_selection (bool with_master)
569 RouteList rl (selected_routes());
570 run_new_group_dialog (&rl, with_master);
574 GroupTabs::new_from_rec_enabled (bool with_master)
576 RouteList rl (get_rec_enabled());
577 run_new_group_dialog (&rl, with_master);
581 GroupTabs::new_from_soloed (bool with_master)
583 RouteList rl (get_soloed());
584 run_new_group_dialog (&rl, with_master);
588 GroupTabs::run_new_group_dialog (RouteList const * rl, bool with_master)
590 if (rl && rl->empty()) {
594 RouteGroup* g = new RouteGroup (*_session, "");
595 RouteGroupDialog* d = new RouteGroupDialog (g, true);
597 d->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_group_dialog_finished), d, rl ? new RouteList (*rl): 0, with_master));
602 GroupTabs::new_group_dialog_finished (int r, RouteGroupDialog* d, RouteList const * rl, bool with_master) const
604 if (r == RESPONSE_OK) {
606 if (!d->name_check()) {
610 _session->add_route_group (d->group());
613 for (RouteList::const_iterator i = rl->begin(); i != rl->end(); ++i) {
614 d->group()->add (*i);
618 assign_group_to_master (0, d->group(), true); /* zero => new master */
626 delete_when_idle (d);
630 GroupTabs::edit_group (RouteGroup* g)
632 RouteGroupDialog* d = new RouteGroupDialog (g, false);
633 d->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &GroupTabs::edit_group_dialog_finished), d));
638 GroupTabs::edit_group_dialog_finished (int r, RouteGroupDialog* d) const
640 delete_when_idle (d);
644 GroupTabs::subgroup (RouteGroup* g, bool aux, Placement placement)
646 g->make_subgroup (aux, placement);
650 GroupTabs::un_subgroup (RouteGroup* g)
652 g->destroy_subgroup ();
655 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
656 * @param g Group to collect.
659 GroupTabs::collect (RouteGroup* g)
661 boost::shared_ptr<RouteList> group_routes = g->route_list ();
662 group_routes->sort (Stripable::PresentationOrderSorter());
663 int const N = group_routes->size ();
665 RouteList::iterator i = group_routes->begin ();
666 boost::shared_ptr<RouteList> routes = _session->get_routes ();
667 routes->sort (Stripable::PresentationOrderSorter());
668 RouteList::const_iterator j = routes->begin ();
672 while (i != group_routes->end() && j != routes->end()) {
674 PresentationInfo::order_t const k = (*j)->presentation_info ().order();
685 (*j)->set_presentation_order (coll, false);
692 (*j)->set_presentation_order (k + diff, false);
699 _session->notify_presentation_info_change ();
703 GroupTabs::activate_all ()
705 _session->foreach_route_group (
706 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
711 GroupTabs::disable_all ()
713 _session->foreach_route_group (
714 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
719 GroupTabs::set_activation (RouteGroup* g, bool a)
721 g->set_active (a, this);
725 GroupTabs::remove_group (RouteGroup* g)
727 _session->remove_route_group (*g);
730 /** Set the color of the tab of a route group */
732 GroupTabs::set_group_color (RouteGroup* group, uint32_t color)
737 UINT_TO_RGBA (color, &r, &g, &b, &a);
739 /* Hack to disallow black route groups; force a dark grey instead */
740 const uint32_t dark_gray = 25;
742 if (r < dark_gray && g < dark_gray && b < dark_gray) {
748 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
752 /* for historical reasons the colors must be stored as 16 bit color
756 snprintf (buf, sizeof (buf), "%d:%d:%d", (r<<8), (g<<8), (b<<8));
757 gui_state.set_property (group_gui_id (group), "color", buf);
759 /* the group color change notification */
761 PBD::PropertyChange change;
762 change.add (Properties::color);
763 group->PropertyChanged (change);
765 /* This is a bit of a hack, but this might change
766 our route's effective color, so emit gui_changed
770 emit_gui_changed_for_members (group);
773 /** @return the ID string to use for the GUI state of a route group */
775 GroupTabs::group_gui_id (RouteGroup* group)
780 snprintf (buf, sizeof (buf), "route_group %s", group->id().to_s().c_str ());
785 /** @return the color to use for a route group tab */
787 GroupTabs::group_color (RouteGroup* group)
791 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
792 string const gui_id = group_gui_id (group);
794 string const color = gui_state.get_string (gui_id, "color", &empty);
797 /* no color has yet been set, so use a random one */
798 uint32_t c = gdk_color_to_rgba (unique_random_color (_used_colors));
799 set_group_color (group, c);
805 /* for historical reasons, colors are stored as 16 bit values.
808 sscanf (color.c_str(), "%d:%d:%d", &r, &g, &b);
814 return RGBA_TO_UINT (r, g, b, 255);
818 GroupTabs::route_group_property_changed (RouteGroup* rg)
820 /* This is a bit of a hack, but this might change
821 our route's effective color, so emit gui_changed
825 emit_gui_changed_for_members (rg);
831 GroupTabs::route_added_to_route_group (RouteGroup*, boost::weak_ptr<Route> w)
833 /* Similarly-spirited hack as in route_group_property_changed */
835 boost::shared_ptr<Route> r = w.lock ();
840 r->presentation_info().PropertyChanged (Properties::color);
846 GroupTabs::route_removed_from_route_group (RouteGroup*, boost::weak_ptr<Route> w)
848 /* Similarly-spirited hack as in route_group_property_changed */
850 boost::shared_ptr<Route> r = w.lock ();
855 r->presentation_info().PropertyChanged (Properties::color);
861 GroupTabs::emit_gui_changed_for_members (RouteGroup* rg)
863 for (RouteList::iterator i = rg->route_list()->begin(); i != rg->route_list()->end(); ++i) {
864 (*i)->gui_changed (X_("color"), 0);