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::add_new_from_items (Menu_Helpers::MenuList& items)
309 using namespace Menu_Helpers;
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)));
319 items.push_back (MenuElem (_("Create New Group From..."), *new_from));
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)));
328 items.push_back (MenuElem (_("Create New Group with Master From..."), *new_from));
332 GroupTabs::get_menu (RouteGroup* g, bool in_tab_area)
334 using namespace Menu_Helpers;
339 _menu->set_name ("ArdourContextMenu");
341 MenuList& items = _menu->items();
344 const VCAList vcas = _session->vca_manager().vcas ();
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))));
350 /* context menu is not for a group tab, show the "create new
353 add_new_from_items (items);
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)));
362 items.push_back (SeparatorElem());
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)));
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)));
371 items.push_back (MenuElem (_("Assign Group to Control Master..."), *vca_menu));
374 items.push_back (SeparatorElem());
376 if (g->has_subgroup()) {
377 items.push_back (MenuElem (_("Remove Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::un_subgroup), g)));
379 items.push_back (MenuElem (_("Add New Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, false, PreFader)));
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());
387 add_menu_items (_menu, g);
390 /* context menu is for a group tab, show the "create new
393 add_new_from_items (items);
396 items.push_back (SeparatorElem());
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())));
407 items.push_back (MenuElem (_("Assign Selection to Control Master..."), *vca_menu));
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())));
418 items.push_back (MenuElem (_("Assign Record Enabled to Control Master..."), *vca_menu));
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())));
429 items.push_back (MenuElem (_("Assign Soloed to Control Master...")));
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)));
439 GroupTabs::assign_group_to_master (uint32_t which, RouteGroup* group, bool rename_master)
441 if (!_session || !group) {
445 boost::shared_ptr<VCA> master;
448 if (_session->vca_manager().create_vca (1)) {
453 /* VCAs use 1-based counting. Get most recently created VCA... */
454 which = _session->vca_manager().n_vcas();
457 master = _session->vca_manager().vca_by_number (which);
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.
467 group->assign_master (master);
470 master->set_name (group->name());
475 GroupTabs::assign_some_to_master (uint32_t which, RouteList rl)
481 boost::shared_ptr<VCA> master;
484 if (_session->vca_manager().create_vca (1)) {
489 /* VCAs use 1-based counting. Get most recently created VCA... */
490 which = _session->vca_manager().n_vcas();
493 master = _session->vca_manager().vca_by_number (which);
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.
508 for (RouteList::iterator r = rl.begin(); r != rl.end(); ++r) {
509 (*r)->assign (master);
514 GroupTabs::get_rec_enabled ()
516 RouteList rec_enabled;
522 boost::shared_ptr<RouteList> rl = _session->get_routes ();
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);
536 GroupTabs::get_soloed ()
538 boost::shared_ptr<RouteList> rl = _session->get_routes ();
542 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
543 if (!(*i)->is_master() && (*i)->soloed()) {
544 soloed.push_back (*i);
552 GroupTabs::assign_selection_to_master (uint32_t which)
554 assign_some_to_master (which, selected_routes ());
558 GroupTabs::assign_recenabled_to_master (uint32_t which)
560 assign_some_to_master (which, get_rec_enabled());
564 GroupTabs::assign_soloed_to_master (uint32_t which)
566 assign_some_to_master (which, get_soloed());
570 GroupTabs::new_from_selection (bool with_master)
572 run_new_group_dialog (selected_routes(), with_master);
576 GroupTabs::new_from_rec_enabled (bool with_master)
578 run_new_group_dialog (get_rec_enabled(), with_master);
582 GroupTabs::new_from_soloed (bool with_master)
584 run_new_group_dialog (get_soloed(), with_master);
588 GroupTabs::run_new_group_dialog (RouteList const & rl, bool with_master)
594 RouteGroup* g = new RouteGroup (*_session, "");
595 RouteGroupDialog d (g, true);
600 _session->add_route_group (g);
601 for (RouteList::const_iterator i = rl.begin(); i != rl.end(); ++i) {
606 assign_group_to_master (0, g, true); /* zero => new master */
612 GroupTabs::create_and_add_group () const
614 RouteGroup* g = new RouteGroup (*_session, "");
615 RouteGroupDialog d (g, true);
622 _session->add_route_group (g);
627 GroupTabs::create_and_add_master () const
633 GroupTabs::create_and_add_group_with_master () const
635 RouteGroup* g = new RouteGroup (*_session, "");
636 RouteGroupDialog d (g, true);
643 _session->add_route_group (g);
648 GroupTabs::edit_group (RouteGroup* g)
650 RouteGroupDialog d (g, false);
655 GroupTabs::subgroup (RouteGroup* g, bool aux, Placement placement)
657 g->make_subgroup (aux, placement);
661 GroupTabs::un_subgroup (RouteGroup* g)
663 g->destroy_subgroup ();
666 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
667 * @param g Group to collect.
670 GroupTabs::collect (RouteGroup* g)
672 boost::shared_ptr<RouteList> group_routes = g->route_list ();
673 group_routes->sort (Stripable::PresentationOrderSorter());
674 int const N = group_routes->size ();
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 ();
683 while (i != group_routes->end() && j != routes->end()) {
685 PresentationInfo::order_t const k = (*j)->presentation_info ().order();
696 (*j)->set_presentation_order_explicit (coll);
703 (*j)->set_presentation_order_explicit (k + diff);
710 _session->notify_presentation_info_change ();
714 GroupTabs::activate_all ()
716 _session->foreach_route_group (
717 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
722 GroupTabs::disable_all ()
724 _session->foreach_route_group (
725 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
730 GroupTabs::set_activation (RouteGroup* g, bool a)
732 g->set_active (a, this);
736 GroupTabs::remove_group (RouteGroup* g)
738 _session->remove_route_group (*g);
741 /** Set the color of the tab of a route group */
743 GroupTabs::set_group_color (RouteGroup* group, uint32_t color)
748 UINT_TO_RGBA (color, &r, &g, &b, &a);
750 /* Hack to disallow black route groups; force a dark grey instead */
751 const uint32_t dark_gray = 25;
753 if (r < dark_gray && g < dark_gray && b < dark_gray) {
759 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
763 /* for historical reasons the colors must be stored as 16 bit color
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);
770 /* the group color change notification */
772 PBD::PropertyChange change;
773 change.add (Properties::color);
774 group->PropertyChanged (change);
776 /* This is a bit of a hack, but this might change
777 our route's effective color, so emit gui_changed
781 emit_gui_changed_for_members (group);
784 /** @return the ID string to use for the GUI state of a route group */
786 GroupTabs::group_gui_id (RouteGroup* group)
791 snprintf (buf, sizeof (buf), "route_group %s", group->id().to_s().c_str ());
796 /** @return the color to use for a route group tab */
798 GroupTabs::group_color (RouteGroup* group)
802 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
803 string const gui_id = group_gui_id (group);
805 string const color = gui_state.get_string (gui_id, "color", &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);
816 /* for historical reasons, colors are stored as 16 bit values.
819 sscanf (color.c_str(), "%d:%d:%d", &r, &g, &b);
825 return RGBA_TO_UINT (r, g, b, 255);
829 GroupTabs::route_group_property_changed (RouteGroup* rg)
831 /* This is a bit of a hack, but this might change
832 our route's effective color, so emit gui_changed
836 emit_gui_changed_for_members (rg);
842 GroupTabs::route_added_to_route_group (RouteGroup*, boost::weak_ptr<Route> w)
844 /* Similarly-spirited hack as in route_group_property_changed */
846 boost::shared_ptr<Route> r = w.lock ();
851 r->presentation_info().PropertyChanged (Properties::color);
857 GroupTabs::route_removed_from_route_group (RouteGroup*, boost::weak_ptr<Route> w)
859 /* Similarly-spirited hack as in route_group_property_changed */
861 boost::shared_ptr<Route> r = w.lock ();
866 r->presentation_info().PropertyChanged (Properties::color);
872 GroupTabs::emit_gui_changed_for_members (RouteGroup* rg)
874 for (RouteList::iterator i = rg->route_list()->begin(); i != rg->route_list()->end(); ++i) {
875 (*i)->gui_changed (X_("color"), 0);