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 "gui_thread.h"
29 #include "route_group_dialog.h"
30 #include "group_tabs.h"
33 #include "ardour_ui.h"
34 #include "rgb_macros.h"
35 #include "ui_config.h"
40 using namespace ARDOUR;
41 using namespace ARDOUR_UI_UTILS;
42 using Gtkmm2ext::Keyboard;
44 list<Gdk::Color> GroupTabs::_used_colors;
46 GroupTabs::GroupTabs ()
49 , _dragging_new_tab (0)
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));
55 GroupTabs::~GroupTabs ()
61 GroupTabs::set_session (Session* s)
63 SessionHandlePtr::set_session (s);
66 _session->RouteGroupPropertyChanged.connect (
67 _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_group_property_changed, this, _1), gui_context()
69 _session->RouteAddedToRouteGroup.connect (
70 _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_added_to_route_group, this, _1, _2), gui_context()
72 _session->RouteRemovedFromRouteGroup.connect (
73 _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_removed_from_route_group, this, _1, _2), gui_context()
76 _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&GroupTabs::set_dirty, this, (cairo_rectangle_t*)0), gui_context());
81 /** Handle a size request.
82 * @param req GTK requisition
85 GroupTabs::on_size_request (Gtk::Requisition *req)
87 /* Use a dummy, small width and the actual height that we want */
93 GroupTabs::on_button_press_event (GdkEventButton* ev)
95 using namespace Menu_Helpers;
97 double const p = primary_coordinate (ev->x, ev->y);
99 list<Tab>::iterator prev;
100 list<Tab>::iterator next;
101 Tab* t = click_to_tab (p, &prev, &next);
103 _drag_min = prev != _tabs.end() ? prev->to : 0;
104 _drag_max = next != _tabs.end() ? next->from : extent ();
106 if (ev->button == 1) {
111 _dragging_new_tab = true;
113 if (next == _tabs.end()) {
117 list<Tab>::iterator j = _tabs.insert (next, n);
122 _dragging_new_tab = false;
123 _initial_dragging_routes = routes_for_tab (t);
130 double const h = (t->from + t->to) / 2;
132 _drag_moving = t->from;
134 _drag_offset = p - t->from;
136 _drag_moving = t->to;
137 _drag_fixed = t->from;
138 _drag_offset = p - t->to;
141 } else if (ev->button == 3) {
143 RouteGroup* g = t ? t->group : 0;
145 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier) && g) {
147 RouteGroupDialog d (g, false);
150 Menu* m = get_menu (g, true);
152 m->popup (ev->button, ev->time);
162 GroupTabs::on_motion_notify_event (GdkEventMotion* ev)
164 if (_dragging == 0) {
168 double const p = primary_coordinate (ev->x, ev->y);
170 if (p != _drag_first) {
174 _drag_moving = p - _drag_offset;
176 _dragging->from = min (_drag_moving, _drag_fixed);
177 _dragging->to = max (_drag_moving, _drag_fixed);
179 _dragging->from = max (_dragging->from, _drag_min);
180 _dragging->to = min (_dragging->to, _drag_max);
185 gdk_event_request_motions(ev);
192 GroupTabs::on_button_release_event (GdkEventButton*)
194 if (_dragging == 0) {
200 if (_dragging->group) {
201 /* toggle active state */
202 _dragging->group->set_active (!_dragging->group->is_active (), this);
207 RouteList routes = routes_for_tab (_dragging);
209 if (!routes.empty()) {
210 if (_dragging_new_tab) {
211 RouteGroup* g = create_and_add_group ();
213 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
218 boost::shared_ptr<RouteList> r = _session->get_routes ();
219 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
221 bool const was_in_tab = find (
222 _initial_dragging_routes.begin(), _initial_dragging_routes.end(), *i
223 ) != _initial_dragging_routes.end ();
225 bool const now_in_tab = find (routes.begin(), routes.end(), *i) != routes.end();
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);
241 _initial_dragging_routes.clear ();
247 GroupTabs::render (cairo_t* cr, cairo_rectangle_t*)
249 if (_dragging == 0) {
250 _tabs = compute_tabs ();
255 Gdk::Color c = get_style()->get_base (Gtk::STATE_NORMAL);
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());
263 for (list<Tab>::const_iterator i = _tabs.begin(); i != _tabs.end(); ++i) {
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.
277 GroupTabs::click_to_tab (double c, list<Tab>::iterator* prev, list<Tab>::iterator* next)
279 *prev = *next = _tabs.end ();
282 list<Tab>::iterator i = _tabs.begin ();
283 while (i != _tabs.end()) {
296 if (i->from <= c && c < i->to) {
307 GroupTabs::get_menu (RouteGroup* g, bool TabArea)
309 using namespace Menu_Helpers;
314 _menu->set_name ("ArdourContextMenu");
315 MenuList& items = _menu->items();
319 items.push_back (MenuElem (_("Create New Group ..."), hide_return (sigc::mem_fun(*this, &GroupTabs::create_and_add_group))));
320 items.push_back (MenuElem (_("Create New Group with Control Master ..."), hide_return (sigc::mem_fun(*this, &GroupTabs::create_and_add_group_with_master))));
325 MenuList& f = new_from->items ();
326 f.push_back (MenuElem (_("Selection..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_selection), false)));
327 f.push_back (MenuElem (_("Record Enabled..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_rec_enabled), false)));
328 f.push_back (MenuElem (_("Soloed..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_soloed), false)));
330 items.push_back (MenuElem (_("Create New Group From..."), *new_from));
334 MenuList& f = new_from->items ();
335 f.push_back (MenuElem (_("Selection..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_selection), true)));
336 f.push_back (MenuElem (_("Record Enabled..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_rec_enabled), true)));
337 f.push_back (MenuElem (_("Soloed..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::new_from_soloed), true)));
339 items.push_back (MenuElem (_("Create New Group with Master From..."), *new_from));
342 const VCAList vcas = _session->vca_manager().vcas ();
346 MenuList& f (vca_menu->items());
347 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), 0)));
348 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
349 f.push_back (MenuElem (string_compose ("VCA %1", (*v)->number()), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), (*v)->number())));
353 items.push_back (MenuElem (_("Assign Selection to Control Master..."), *vca_menu));
357 MenuList& f (vca_menu->items());
358 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), 0)));
359 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
360 f.push_back (MenuElem (string_compose ("VCA %1", (*v)->number()), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), (*v)->number())));
364 items.push_back (MenuElem (_("Assign Record Enabled to Control Master..."), *vca_menu));
368 MenuList& f (vca_menu->items());
369 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), 0)));
370 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
371 f.push_back (MenuElem (string_compose ("VCA %1", (*v)->number()), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_selection_to_master), (*v)->number())));
375 items.push_back (MenuElem (_("Assign Soloed to Control Master...")));
378 items.push_back (SeparatorElem());
379 items.push_back (MenuElem (_("Edit Group..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::edit_group), g)));
380 items.push_back (MenuElem (_("Collect Group"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::collect), g)));
381 items.push_back (MenuElem (_("Remove Group"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::remove_group), g)));
382 items.push_back (SeparatorElem());
383 if (g->has_subgroup()) {
384 items.push_back (MenuElem (_("Remove Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::un_subgroup), g)));
386 items.push_back (MenuElem (_("Add New Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, false, PreFader)));
388 items.push_back (MenuElem (_("Add New Aux Bus (pre-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PreFader)));
389 items.push_back (MenuElem (_("Add New Aux Bus (post-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PostFader)));
390 items.push_back (SeparatorElem());
393 MenuList& f (vca_menu->items());
394 f.push_back (MenuElem ("New", sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_group_to_master), 0, g)));
396 for (VCAList::const_iterator v = vcas.begin(); v != vcas.end(); ++v) {
397 f.push_back (MenuElem (string_compose ("VCA %1", (*v)->number()), sigc::bind (sigc::mem_fun (*this, &GroupTabs::assign_group_to_master), (*v)->number(), g)));
399 items.push_back (MenuElem (_("Assign Group to Control Master..."), *vca_menu));
402 add_menu_items (_menu, g);
404 items.push_back (SeparatorElem());
405 items.push_back (MenuElem (_("Enable All Groups"), sigc::mem_fun(*this, &GroupTabs::activate_all)));
406 items.push_back (MenuElem (_("Disable All Groups"), sigc::mem_fun(*this, &GroupTabs::disable_all)));
407 items.push_back (SeparatorElem());
413 GroupTabs::assign_group_to_master (uint32_t which, RouteGroup* group)
415 if (!_session || !group) {
419 boost::shared_ptr<VCA> master;
422 if (_session->vca_manager().create_vca (1)) {
427 /* VCAs use 1-based counting. Get most recently created VCA... */
428 which = _session->vca_manager().n_vcas();
431 master = _session->vca_manager().vca_by_number (which);
434 /* should never happen; if it does, basically something deeply
435 odd happened, no reason to tell user because there's no
436 sensible explanation.
441 group->assign_master (master);
445 GroupTabs::assign_some_to_master (uint32_t which, RouteList rl)
451 boost::shared_ptr<VCA> master;
454 if (_session->vca_manager().create_vca (1)) {
459 /* VCAs use 1-based counting. Get most recently created VCA... */
460 which = _session->vca_manager().n_vcas();
463 master = _session->vca_manager().vca_by_number (which);
466 /* should never happen; if it does, basically something deeply
467 odd happened, no reason to tell user because there's no
468 sensible explanation.
478 for (RouteList::iterator r = rl.begin(); r != rl.end(); ++r) {
479 (*r)->assign (master);
484 GroupTabs::get_rec_enabled ()
486 RouteList rec_enabled;
492 boost::shared_ptr<RouteList> rl = _session->get_routes ();
494 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
495 boost::shared_ptr<Track> trk (boost::dynamic_pointer_cast<Track> (*i));
496 if (trk && trk->rec_enable_control()->get_value()) {
497 rec_enabled.push_back (*i);
506 GroupTabs::get_soloed ()
508 boost::shared_ptr<RouteList> rl = _session->get_routes ();
512 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
513 if (!(*i)->is_master() && (*i)->soloed()) {
514 soloed.push_back (*i);
522 GroupTabs::assign_selection_to_master (uint32_t which)
524 assign_some_to_master (which, selected_routes ());
528 GroupTabs::assign_recenabled_to_master (uint32_t which)
530 assign_some_to_master (which, get_rec_enabled());
534 GroupTabs::assign_soloed_to_master (uint32_t which)
536 assign_some_to_master (which, get_soloed());
540 GroupTabs::new_from_selection (bool with_master)
542 run_new_group_dialog (selected_routes(), with_master);
546 GroupTabs::new_from_rec_enabled (bool with_master)
548 run_new_group_dialog (get_rec_enabled(), with_master);
552 GroupTabs::new_from_soloed (bool with_master)
554 run_new_group_dialog (get_soloed(), with_master);
558 GroupTabs::run_new_group_dialog (RouteList const & rl, bool with_master)
564 RouteGroup* g = new RouteGroup (*_session, "");
565 RouteGroupDialog d (g, true);
570 _session->add_route_group (g);
571 for (RouteList::const_iterator i = rl.begin(); i != rl.end(); ++i) {
578 GroupTabs::create_and_add_group () const
580 RouteGroup* g = new RouteGroup (*_session, "");
581 RouteGroupDialog d (g, true);
588 _session->add_route_group (g);
593 GroupTabs::create_and_add_master () const
599 GroupTabs::create_and_add_group_with_master () const
601 RouteGroup* g = new RouteGroup (*_session, "");
602 RouteGroupDialog d (g, true);
609 _session->add_route_group (g);
614 GroupTabs::edit_group (RouteGroup* g)
616 RouteGroupDialog d (g, false);
621 GroupTabs::subgroup (RouteGroup* g, bool aux, Placement placement)
623 g->make_subgroup (aux, placement);
627 GroupTabs::un_subgroup (RouteGroup* g)
629 g->destroy_subgroup ();
632 struct CollectSorter {
633 bool operator () (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
634 return a->presentation_info () < b->presentation_info();
639 bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
640 return a->presentation_info() < b->presentation_info();
644 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
645 * @param g Group to collect.
648 GroupTabs::collect (RouteGroup* g)
650 boost::shared_ptr<RouteList> group_routes = g->route_list ();
651 group_routes->sort (CollectSorter ());
652 int const N = group_routes->size ();
654 RouteList::iterator i = group_routes->begin ();
655 boost::shared_ptr<RouteList> routes = _session->get_routes ();
656 routes->sort (OrderSorter ());
657 RouteList::const_iterator j = routes->begin ();
661 while (i != group_routes->end() && j != routes->end()) {
663 PresentationInfo::order_t const k = (*j)->presentation_info ().group_order();
674 (*j)->set_presentation_group_order_explicit (coll);
681 (*j)->set_presentation_group_order_explicit (k + diff);
688 _session->notify_presentation_info_change ();
692 GroupTabs::activate_all ()
694 _session->foreach_route_group (
695 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
700 GroupTabs::disable_all ()
702 _session->foreach_route_group (
703 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
708 GroupTabs::set_activation (RouteGroup* g, bool a)
710 g->set_active (a, this);
714 GroupTabs::remove_group (RouteGroup* g)
716 _session->remove_route_group (*g);
719 /** Set the color of the tab of a route group */
721 GroupTabs::set_group_color (RouteGroup* group, uint32_t color)
726 UINT_TO_RGBA (color, &r, &g, &b, &a);
728 /* Hack to disallow black route groups; force a dark grey instead */
730 if (r == 0 && g == 0 && b == 0) {
736 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
740 /* for historical reasons the colors must be stored as 16 bit color
744 snprintf (buf, sizeof (buf), "%d:%d:%d", (r<<8), (g<<8), (b<<8));
745 gui_state.set_property (group_gui_id (group), "color", buf);
747 /* the group color change notification */
749 PBD::PropertyChange change;
750 change.add (Properties::color);
751 group->PropertyChanged (change);
753 /* This is a bit of a hack, but this might change
754 our route's effective color, so emit gui_changed
758 emit_gui_changed_for_members (group);
761 /** @return the ID string to use for the GUI state of a route group */
763 GroupTabs::group_gui_id (RouteGroup* group)
768 snprintf (buf, sizeof (buf), "route_group %s", group->id().to_s().c_str ());
773 /** @return the color to use for a route group tab */
775 GroupTabs::group_color (RouteGroup* group)
779 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
780 string const gui_id = group_gui_id (group);
782 string const color = gui_state.get_string (gui_id, "color", &empty);
785 /* no color has yet been set, so use a random one */
786 uint32_t c = gdk_color_to_rgba (unique_random_color (_used_colors));
787 set_group_color (group, c);
793 /* for historical reasons, colors are stored as 16 bit values.
796 sscanf (color.c_str(), "%d:%d:%d", &r, &g, &b);
802 return RGBA_TO_UINT (r, g, b, 255);
806 GroupTabs::route_group_property_changed (RouteGroup* rg)
808 /* This is a bit of a hack, but this might change
809 our route's effective color, so emit gui_changed
813 emit_gui_changed_for_members (rg);
819 GroupTabs::route_added_to_route_group (RouteGroup*, boost::weak_ptr<Route> w)
821 /* Similarly-spirited hack as in route_group_property_changed */
823 boost::shared_ptr<Route> r = w.lock ();
828 r->gui_changed (X_("color"), 0);
834 GroupTabs::route_removed_from_route_group (RouteGroup*, boost::weak_ptr<Route> w)
836 /* Similarly-spirited hack as in route_group_property_changed */
838 boost::shared_ptr<Route> r = w.lock ();
843 r->gui_changed (X_("color"), 0);
849 GroupTabs::emit_gui_changed_for_members (RouteGroup* rg)
851 for (RouteList::iterator i = rg->route_list()->begin(); i != rg->route_list()->end(); ++i) {
852 (*i)->gui_changed (X_("color"), 0);