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 Gtkmm2ext::Keyboard;
39 list<Gdk::Color> GroupTabs::_used_colors;
41 GroupTabs::GroupTabs ()
44 , _dragging_new_tab (0)
46 add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
49 GroupTabs::~GroupTabs ()
55 GroupTabs::set_session (Session* s)
57 SessionHandlePtr::set_session (s);
60 _session->RouteGroupPropertyChanged.connect (
61 _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_group_property_changed, this, _1), gui_context()
63 _session->RouteAddedToRouteGroup.connect (
64 _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_added_to_route_group, this, _1, _2), gui_context()
66 _session->RouteRemovedFromRouteGroup.connect (
67 _session_connections, invalidator (*this), boost::bind (&GroupTabs::route_removed_from_route_group, this, _1, _2), gui_context()
70 _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&GroupTabs::set_dirty, this), gui_context());
75 /** Handle a size request.
76 * @param req GTK requisition
79 GroupTabs::on_size_request (Gtk::Requisition *req)
81 /* Use a dummy, small width and the actual height that we want */
87 GroupTabs::on_button_press_event (GdkEventButton* ev)
89 using namespace Menu_Helpers;
91 double const p = primary_coordinate (ev->x, ev->y);
93 list<Tab>::iterator prev;
94 list<Tab>::iterator next;
95 Tab* t = click_to_tab (p, &prev, &next);
97 _drag_min = prev != _tabs.end() ? prev->to : 0;
98 _drag_max = next != _tabs.end() ? next->from : extent ();
100 if (ev->button == 1) {
105 _dragging_new_tab = true;
107 if (next == _tabs.end()) {
111 list<Tab>::iterator j = _tabs.insert (next, n);
116 _dragging_new_tab = false;
117 _initial_dragging_routes = routes_for_tab (t);
124 double const h = (t->from + t->to) / 2;
126 _drag_moving = t->from;
128 _drag_offset = p - t->from;
130 _drag_moving = t->to;
131 _drag_fixed = t->from;
132 _drag_offset = p - t->to;
135 } else if (ev->button == 3) {
137 RouteGroup* g = t ? t->group : 0;
139 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier) && g) {
141 RouteGroupDialog d (g, false);
144 Menu* m = get_menu (g);
146 m->popup (ev->button, ev->time);
156 GroupTabs::on_motion_notify_event (GdkEventMotion* ev)
158 if (_dragging == 0) {
162 double const p = primary_coordinate (ev->x, ev->y);
164 if (p != _drag_first) {
168 _drag_moving = p - _drag_offset;
170 _dragging->from = min (_drag_moving, _drag_fixed);
171 _dragging->to = max (_drag_moving, _drag_fixed);
173 _dragging->from = max (_dragging->from, _drag_min);
174 _dragging->to = min (_dragging->to, _drag_max);
179 gdk_event_request_motions(ev);
186 GroupTabs::on_button_release_event (GdkEventButton*)
188 if (_dragging == 0) {
194 if (_dragging->group) {
195 /* toggle active state */
196 _dragging->group->set_active (!_dragging->group->is_active (), this);
201 RouteList routes = routes_for_tab (_dragging);
203 if (!routes.empty()) {
204 if (_dragging_new_tab) {
205 RouteGroup* g = create_and_add_group ();
207 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
212 boost::shared_ptr<RouteList> r = _session->get_routes ();
213 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
215 bool const was_in_tab = find (
216 _initial_dragging_routes.begin(), _initial_dragging_routes.end(), *i
217 ) != _initial_dragging_routes.end ();
219 bool const now_in_tab = find (routes.begin(), routes.end(), *i) != routes.end();
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);
235 _initial_dragging_routes.clear ();
241 GroupTabs::render (cairo_t* cr, cairo_rectangle_t*)
243 if (_dragging == 0) {
244 _tabs = compute_tabs ();
249 Gdk::Color c = get_style()->get_base (Gtk::STATE_NORMAL);
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());
257 for (list<Tab>::const_iterator i = _tabs.begin(); i != _tabs.end(); ++i) {
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.
271 GroupTabs::click_to_tab (double c, list<Tab>::iterator* prev, list<Tab>::iterator* next)
273 *prev = *next = _tabs.end ();
276 list<Tab>::iterator i = _tabs.begin ();
277 while (i != _tabs.end()) {
290 if (i->from <= c && c < i->to) {
301 GroupTabs::get_menu (RouteGroup* g)
303 using namespace Menu_Helpers;
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)));
314 _menu->set_name ("ArdourContextMenu");
315 MenuList& items = _menu->items();
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));
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)));
328 items.push_back (MenuElem (_("Add New Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, false, PreFader)));
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)));
334 add_menu_items (_menu, g);
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)));
345 GroupTabs::new_from_selection ()
347 RouteList rl = selected_routes ();
352 run_new_group_dialog (rl);
356 GroupTabs::new_from_rec_enabled ()
358 boost::shared_ptr<RouteList> rl = _session->get_routes ();
360 RouteList rec_enabled;
362 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
363 if ((*i)->record_enabled()) {
364 rec_enabled.push_back (*i);
368 if (rec_enabled.empty()) {
372 run_new_group_dialog (rec_enabled);
376 GroupTabs::new_from_soloed ()
378 boost::shared_ptr<RouteList> rl = _session->get_routes ();
382 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
383 if (!(*i)->is_master() && (*i)->soloed()) {
384 soloed.push_back (*i);
388 if (soloed.empty()) {
392 run_new_group_dialog (soloed);
396 GroupTabs::run_new_group_dialog (RouteList const & rl)
398 RouteGroup* g = new RouteGroup (*_session, "");
399 g->apply_changes (default_properties ());
401 RouteGroupDialog d (g, true);
406 _session->add_route_group (g);
407 for (RouteList::const_iterator i = rl.begin(); i != rl.end(); ++i) {
414 GroupTabs::create_and_add_group () const
416 RouteGroup* g = new RouteGroup (*_session, "");
418 g->apply_changes (default_properties ());
420 RouteGroupDialog d (g, true);
427 _session->add_route_group (g);
432 GroupTabs::edit_group (RouteGroup* g)
434 RouteGroupDialog d (g, false);
439 GroupTabs::subgroup (RouteGroup* g, bool aux, Placement placement)
441 g->make_subgroup (aux, placement);
445 GroupTabs::un_subgroup (RouteGroup* g)
447 g->destroy_subgroup ();
450 struct CollectSorter {
451 bool operator () (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
452 return a->order_key () < b->order_key ();
457 bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
458 return a->order_key () < b->order_key ();
462 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
463 * @param g Group to collect.
466 GroupTabs::collect (RouteGroup* g)
468 boost::shared_ptr<RouteList> group_routes = g->route_list ();
469 group_routes->sort (CollectSorter ());
470 int const N = group_routes->size ();
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 ();
479 while (i != group_routes->end() && j != routes->end()) {
481 int const k = (*j)->order_key ();
492 (*j)->set_order_key (coll);
499 (*j)->set_order_key (k + diff);
510 GroupTabs::activate_all ()
512 _session->foreach_route_group (
513 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
518 GroupTabs::disable_all ()
520 _session->foreach_route_group (
521 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
526 GroupTabs::set_activation (RouteGroup* g, bool a)
528 g->set_active (a, this);
532 GroupTabs::remove_group (RouteGroup* g)
534 _session->remove_route_group (*g);
537 /** Set the color of the tab of a route group */
539 GroupTabs::set_group_color (RouteGroup* group, uint32_t color)
544 UINT_TO_RGBA (color, &r, &g, &b, &a);
546 /* Hack to disallow black route groups; force a dark grey instead */
548 if (r == 0 && g == 0 && b == 0) {
554 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
558 /* for historical reasons the colors must be stored as 16 bit color
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);
565 /* the group color change notification */
567 PBD::PropertyChange change;
568 change.add (Properties::color);
569 group->PropertyChanged (change);
571 /* This is a bit of a hack, but this might change
572 our route's effective color, so emit gui_changed
576 emit_gui_changed_for_members (group);
579 /** @return the ID string to use for the GUI state of a route group */
581 GroupTabs::group_gui_id (RouteGroup* group)
586 snprintf (buf, sizeof (buf), "route_group %s", group->id().to_s().c_str ());
591 /** @return the color to use for a route group tab */
593 GroupTabs::group_color (RouteGroup* group)
597 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
598 string const gui_id = group_gui_id (group);
600 string const color = gui_state.get_string (gui_id, "color", &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);
611 /* for historical reasons, colors are stored as 16 bit values.
614 sscanf (color.c_str(), "%d:%d:%d", &r, &g, &b);
620 return RGBA_TO_UINT (r, g, b, 255);
624 GroupTabs::route_group_property_changed (RouteGroup* rg)
626 /* This is a bit of a hack, but this might change
627 our route's effective color, so emit gui_changed
631 emit_gui_changed_for_members (rg);
637 GroupTabs::route_added_to_route_group (RouteGroup*, boost::weak_ptr<Route> w)
639 /* Similarly-spirited hack as in route_group_property_changed */
641 boost::shared_ptr<Route> r = w.lock ();
646 r->gui_changed (X_("color"), 0);
652 GroupTabs::route_removed_from_route_group (RouteGroup*, boost::weak_ptr<Route> w)
654 /* Similarly-spirited hack as in route_group_property_changed */
656 boost::shared_ptr<Route> r = w.lock ();
661 r->gui_changed (X_("color"), 0);
667 GroupTabs::emit_gui_changed_for_members (RouteGroup* rg)
669 for (RouteList::iterator i = rg->route_list()->begin(); i != rg->route_list()->end(); ++i) {
670 (*i)->gui_changed (X_("color"), 0);