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>
21 #include "ardour/session.h"
22 #include "ardour/route_group.h"
23 #include "ardour/route.h"
25 #include "gui_thread.h"
26 #include "route_group_dialog.h"
27 #include "group_tabs.h"
30 #include "ardour_ui.h"
31 #include "rgb_macros.h"
36 using namespace ARDOUR;
37 using namespace ARDOUR_UI_UTILS;
38 using Gtkmm2ext::Keyboard;
40 list<Gdk::Color> GroupTabs::_used_colors;
42 GroupTabs::GroupTabs ()
45 , _dragging_new_tab (0)
47 add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
48 UIConfiguration::ColorsChanged.connect (sigc::mem_fun (*this, &GroupTabs::queue_draw));
51 GroupTabs::~GroupTabs ()
57 GroupTabs::set_session (Session* s)
59 SessionHandlePtr::set_session (s);
62 _session->RouteGroupPropertyChanged.connect (
63 _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_group_property_changed, this, _1), gui_context()
65 _session->RouteAddedToRouteGroup.connect (
66 _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_added_to_route_group, this, _1, _2), gui_context()
68 _session->RouteRemovedFromRouteGroup.connect (
69 _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_removed_from_route_group, this, _1, _2), gui_context()
72 _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&GroupTabs::set_dirty, this), gui_context());
77 /** Handle a size request.
78 * @param req GTK requisition
81 GroupTabs::on_size_request (Gtk::Requisition *req)
83 /* Use a dummy, small width and the actual height that we want */
89 GroupTabs::on_button_press_event (GdkEventButton* ev)
91 using namespace Menu_Helpers;
93 double const p = primary_coordinate (ev->x, ev->y);
95 list<Tab>::iterator prev;
96 list<Tab>::iterator next;
97 Tab* t = click_to_tab (p, &prev, &next);
99 _drag_min = prev != _tabs.end() ? prev->to : 0;
100 _drag_max = next != _tabs.end() ? next->from : extent ();
102 if (ev->button == 1) {
107 _dragging_new_tab = true;
109 if (next == _tabs.end()) {
113 list<Tab>::iterator j = _tabs.insert (next, n);
118 _dragging_new_tab = false;
119 _initial_dragging_routes = routes_for_tab (t);
126 double const h = (t->from + t->to) / 2;
128 _drag_moving = t->from;
130 _drag_offset = p - t->from;
132 _drag_moving = t->to;
133 _drag_fixed = t->from;
134 _drag_offset = p - t->to;
137 } else if (ev->button == 3) {
139 RouteGroup* g = t ? t->group : 0;
141 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier) && g) {
143 RouteGroupDialog d (g, false);
146 Menu* m = get_menu (g);
148 m->popup (ev->button, ev->time);
158 GroupTabs::on_motion_notify_event (GdkEventMotion* ev)
160 if (_dragging == 0) {
164 double const p = primary_coordinate (ev->x, ev->y);
166 if (p != _drag_first) {
170 _drag_moving = p - _drag_offset;
172 _dragging->from = min (_drag_moving, _drag_fixed);
173 _dragging->to = max (_drag_moving, _drag_fixed);
175 _dragging->from = max (_dragging->from, _drag_min);
176 _dragging->to = min (_dragging->to, _drag_max);
181 gdk_event_request_motions(ev);
188 GroupTabs::on_button_release_event (GdkEventButton*)
190 if (_dragging == 0) {
196 if (_dragging->group) {
197 /* toggle active state */
198 _dragging->group->set_active (!_dragging->group->is_active (), this);
203 RouteList routes = routes_for_tab (_dragging);
205 if (!routes.empty()) {
206 if (_dragging_new_tab) {
207 RouteGroup* g = create_and_add_group ();
209 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
214 boost::shared_ptr<RouteList> r = _session->get_routes ();
215 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
217 bool const was_in_tab = find (
218 _initial_dragging_routes.begin(), _initial_dragging_routes.end(), *i
219 ) != _initial_dragging_routes.end ();
221 bool const now_in_tab = find (routes.begin(), routes.end(), *i) != routes.end();
223 if (was_in_tab && !now_in_tab) {
224 _dragging->group->remove (*i);
225 } else if (!was_in_tab && now_in_tab) {
226 _dragging->group->add (*i);
237 _initial_dragging_routes.clear ();
243 GroupTabs::render (cairo_t* cr, cairo_rectangle_t*)
245 if (_dragging == 0) {
246 _tabs = compute_tabs ();
251 Gdk::Color c = get_style()->get_base (Gtk::STATE_NORMAL);
253 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
254 cairo_rectangle (cr, 0, 0, get_width(), get_height());
259 for (list<Tab>::const_iterator i = _tabs.begin(); i != _tabs.end(); ++i) {
265 /** Convert a click position to a tab.
266 * @param c Click position.
267 * @param prev Filled in with the previous tab to the click, or _tabs.end().
268 * @param next Filled in with the next tab after the click, or _tabs.end().
269 * @return Tab under the click, or 0.
273 GroupTabs::click_to_tab (double c, list<Tab>::iterator* prev, list<Tab>::iterator* next)
275 *prev = *next = _tabs.end ();
278 list<Tab>::iterator i = _tabs.begin ();
279 while (i != _tabs.end()) {
292 if (i->from <= c && c < i->to) {
303 GroupTabs::get_menu (RouteGroup* g)
305 using namespace Menu_Helpers;
309 Menu* new_from = new Menu;
310 MenuList& f = new_from->items ();
311 f.push_back (MenuElem (_("Selection..."), sigc::mem_fun (*this, &GroupTabs::new_from_selection)));
312 f.push_back (MenuElem (_("Record Enabled..."), sigc::mem_fun (*this, &GroupTabs::new_from_rec_enabled)));
313 f.push_back (MenuElem (_("Soloed..."), sigc::mem_fun (*this, &GroupTabs::new_from_soloed)));
316 _menu->set_name ("ArdourContextMenu");
317 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 From"), *new_from));
323 items.push_back (MenuElem (_("Edit Group..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::edit_group), g)));
324 items.push_back (MenuElem (_("Collect Group"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::collect), g)));
325 items.push_back (MenuElem (_("Remove Group"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::remove_group), g)));
326 items.push_back (SeparatorElem());
327 if (g->has_subgroup()) {
328 items.push_back (MenuElem (_("Remove Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::un_subgroup), g)));
330 items.push_back (MenuElem (_("Add New Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, false, PreFader)));
332 items.push_back (MenuElem (_("Add New Aux Bus (pre-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PreFader)));
333 items.push_back (MenuElem (_("Add New Aux Bus (post-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PostFader)));
336 add_menu_items (_menu, g);
338 items.push_back (SeparatorElem());
339 items.push_back (MenuElem (_("Enable All Groups"), sigc::mem_fun(*this, &GroupTabs::activate_all)));
340 items.push_back (MenuElem (_("Disable All Groups"), sigc::mem_fun(*this, &GroupTabs::disable_all)));
347 GroupTabs::new_from_selection ()
349 RouteList rl = selected_routes ();
354 run_new_group_dialog (rl);
358 GroupTabs::new_from_rec_enabled ()
360 boost::shared_ptr<RouteList> rl = _session->get_routes ();
362 RouteList rec_enabled;
364 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
365 if ((*i)->record_enabled()) {
366 rec_enabled.push_back (*i);
370 if (rec_enabled.empty()) {
374 run_new_group_dialog (rec_enabled);
378 GroupTabs::new_from_soloed ()
380 boost::shared_ptr<RouteList> rl = _session->get_routes ();
384 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
385 if (!(*i)->is_master() && (*i)->soloed()) {
386 soloed.push_back (*i);
390 if (soloed.empty()) {
394 run_new_group_dialog (soloed);
398 GroupTabs::default_properties () const
400 PBD::PropertyList plist;
402 plist.add (Properties::route_active, true);
403 plist.add (Properties::active, true);
404 plist.add (Properties::gain, true);
405 plist.add (Properties::relative, true);
406 plist.add (Properties::color, true);
407 plist.add (Properties::monitoring, true);
408 plist.add (Properties::select, true);
409 plist.add (Properties::mute, true);
410 plist.add (Properties::solo, true);
411 plist.add (Properties::gain, true);
412 plist.add (Properties::recenable, true);
418 GroupTabs::run_new_group_dialog (RouteList const & rl)
420 RouteGroup* g = new RouteGroup (*_session, "");
421 g->apply_changes (default_properties ());
423 RouteGroupDialog d (g, true);
428 _session->add_route_group (g);
429 for (RouteList::const_iterator i = rl.begin(); i != rl.end(); ++i) {
436 GroupTabs::create_and_add_group () const
438 RouteGroup* g = new RouteGroup (*_session, "");
440 g->apply_changes (default_properties ());
442 RouteGroupDialog d (g, true);
449 _session->add_route_group (g);
454 GroupTabs::edit_group (RouteGroup* g)
456 RouteGroupDialog d (g, false);
461 GroupTabs::subgroup (RouteGroup* g, bool aux, Placement placement)
463 g->make_subgroup (aux, placement);
467 GroupTabs::un_subgroup (RouteGroup* g)
469 g->destroy_subgroup ();
472 struct CollectSorter {
473 bool operator () (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
474 return a->order_key () < b->order_key ();
479 bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
480 return a->order_key () < b->order_key ();
484 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
485 * @param g Group to collect.
488 GroupTabs::collect (RouteGroup* g)
490 boost::shared_ptr<RouteList> group_routes = g->route_list ();
491 group_routes->sort (CollectSorter ());
492 int const N = group_routes->size ();
494 RouteList::iterator i = group_routes->begin ();
495 boost::shared_ptr<RouteList> routes = _session->get_routes ();
496 routes->sort (OrderSorter ());
497 RouteList::const_iterator j = routes->begin ();
501 while (i != group_routes->end() && j != routes->end()) {
503 int const k = (*j)->order_key ();
514 (*j)->set_order_key (coll);
521 (*j)->set_order_key (k + diff);
532 GroupTabs::activate_all ()
534 _session->foreach_route_group (
535 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
540 GroupTabs::disable_all ()
542 _session->foreach_route_group (
543 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
548 GroupTabs::set_activation (RouteGroup* g, bool a)
550 g->set_active (a, this);
554 GroupTabs::remove_group (RouteGroup* g)
556 _session->remove_route_group (*g);
559 /** Set the color of the tab of a route group */
561 GroupTabs::set_group_color (RouteGroup* group, uint32_t color)
566 UINT_TO_RGBA (color, &r, &g, &b, &a);
568 /* Hack to disallow black route groups; force a dark grey instead */
570 if (r == 0 && g == 0 && b == 0) {
576 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
580 /* for historical reasons the colors must be stored as 16 bit color
584 snprintf (buf, sizeof (buf), "%d:%d:%d", (r<<8), (g<<8), (b<<8));
585 gui_state.set_property (group_gui_id (group), "color", buf);
587 /* the group color change notification */
589 PBD::PropertyChange change;
590 change.add (Properties::color);
591 group->PropertyChanged (change);
593 /* This is a bit of a hack, but this might change
594 our route's effective color, so emit gui_changed
598 emit_gui_changed_for_members (group);
601 /** @return the ID string to use for the GUI state of a route group */
603 GroupTabs::group_gui_id (RouteGroup* group)
608 snprintf (buf, sizeof (buf), "route_group %s", group->id().to_s().c_str ());
613 /** @return the color to use for a route group tab */
615 GroupTabs::group_color (RouteGroup* group)
619 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
620 string const gui_id = group_gui_id (group);
622 string const color = gui_state.get_string (gui_id, "color", &empty);
625 /* no color has yet been set, so use a random one */
626 uint32_t c = gdk_color_to_rgba (unique_random_color (_used_colors));
627 set_group_color (group, c);
633 /* for historical reasons, colors are stored as 16 bit values.
636 sscanf (color.c_str(), "%d:%d:%d", &r, &g, &b);
642 return RGBA_TO_UINT (r, g, b, 255);
646 GroupTabs::route_group_property_changed (RouteGroup* rg)
648 /* This is a bit of a hack, but this might change
649 our route's effective color, so emit gui_changed
653 emit_gui_changed_for_members (rg);
659 GroupTabs::route_added_to_route_group (RouteGroup*, boost::weak_ptr<Route> w)
661 /* Similarly-spirited hack as in route_group_property_changed */
663 boost::shared_ptr<Route> r = w.lock ();
668 r->gui_changed (X_("color"), 0);
674 GroupTabs::route_removed_from_route_group (RouteGroup*, boost::weak_ptr<Route> w)
676 /* Similarly-spirited hack as in route_group_property_changed */
678 boost::shared_ptr<Route> r = w.lock ();
683 r->gui_changed (X_("color"), 0);
689 GroupTabs::emit_gui_changed_for_members (RouteGroup* rg)
691 for (RouteList::iterator i = rg->route_list()->begin(); i != rg->route_list()->end(); ++i) {
692 (*i)->gui_changed (X_("color"), 0);