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"
35 using namespace ARDOUR;
36 using Gtkmm2ext::Keyboard;
38 list<Gdk::Color> GroupTabs::_used_colors;
40 GroupTabs::GroupTabs ()
43 , _dragging_new_tab (0)
48 GroupTabs::~GroupTabs ()
54 GroupTabs::set_session (Session* s)
56 SessionHandlePtr::set_session (s);
59 _session->RouteGroupChanged.connect (_session_connections, invalidator (*this), boost::bind (&GroupTabs::set_dirty, this), gui_context());
60 _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&GroupTabs::set_dirty, this), gui_context());
65 /** Handle a size request.
66 * @param req GTK requisition
69 GroupTabs::on_size_request (Gtk::Requisition *req)
71 /* Use a dummy, small width and the actual height that we want */
77 GroupTabs::on_button_press_event (GdkEventButton* ev)
79 using namespace Menu_Helpers;
81 double const p = primary_coordinate (ev->x, ev->y);
83 list<Tab>::iterator prev;
84 list<Tab>::iterator next;
85 Tab* t = click_to_tab (p, &prev, &next);
87 _drag_min = prev != _tabs.end() ? prev->to : 0;
88 _drag_max = next != _tabs.end() ? next->from : extent ();
90 if (ev->button == 1) {
95 _dragging_new_tab = true;
97 if (next == _tabs.end()) {
101 list<Tab>::iterator j = _tabs.insert (next, n);
106 _dragging_new_tab = false;
113 double const h = (t->from + t->to) / 2;
115 _drag_moving = t->from;
117 _drag_offset = p - t->from;
119 _drag_moving = t->to;
120 _drag_fixed = t->from;
121 _drag_offset = p - t->to;
124 } else if (ev->button == 3) {
126 RouteGroup* g = t ? t->group : 0;
127 Menu* m = get_menu (g);
129 m->popup (ev->button, ev->time);
139 GroupTabs::on_motion_notify_event (GdkEventMotion* ev)
141 if (_dragging == 0) {
145 double const p = primary_coordinate (ev->x, ev->y);
147 if (p != _drag_first) {
151 _drag_moving = p - _drag_offset;
153 _dragging->from = min (_drag_moving, _drag_fixed);
154 _dragging->to = max (_drag_moving, _drag_fixed);
156 _dragging->from = max (_dragging->from, _drag_min);
157 _dragging->to = min (_dragging->to, _drag_max);
167 GroupTabs::on_button_release_event (GdkEventButton* ev)
169 if (_dragging == 0) {
175 if (_dragging->group) {
177 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
180 RouteGroupDialog d (_dragging->group, false);
185 /* toggle active state */
186 _dragging->group->set_active (!_dragging->group->is_active (), this);
193 RouteList routes = routes_for_tab (_dragging);
195 if (!routes.empty()) {
196 if (_dragging_new_tab) {
197 RouteGroup* g = create_and_add_group ();
199 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
204 boost::shared_ptr<RouteList> r = _session->get_routes ();
205 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
207 if (find (routes.begin(), routes.end(), *i) == routes.end()) {
208 /* this route is not on the list of those that should be in _dragging's group */
209 if ((*i)->route_group() == _dragging->group) {
210 _dragging->group->remove (*i);
213 _dragging->group->add (*i);
229 GroupTabs::render (cairo_t* cr)
231 if (_dragging == 0) {
232 _tabs = compute_tabs ();
237 cairo_set_source_rgb (cr, 0, 0, 0);
238 cairo_rectangle (cr, 0, 0, _width, _height);
243 for (list<Tab>::const_iterator i = _tabs.begin(); i != _tabs.end(); ++i) {
249 /** Convert a click position to a tab.
250 * @param c Click position.
251 * @param prev Filled in with the previous tab to the click, or _tabs.end().
252 * @param next Filled in with the next tab after the click, or _tabs.end().
253 * @return Tab under the click, or 0.
257 GroupTabs::click_to_tab (double c, list<Tab>::iterator* prev, list<Tab>::iterator* next)
259 *prev = *next = _tabs.end ();
262 list<Tab>::iterator i = _tabs.begin ();
263 while (i != _tabs.end()) {
276 if (i->from <= c && c < i->to) {
287 GroupTabs::get_menu (RouteGroup* g)
289 using namespace Menu_Helpers;
293 Menu* new_from = new Menu;
294 MenuList& f = new_from->items ();
295 f.push_back (MenuElem (_("Selection..."), sigc::mem_fun (*this, &GroupTabs::new_from_selection)));
296 f.push_back (MenuElem (_("Record Enabled..."), sigc::mem_fun (*this, &GroupTabs::new_from_rec_enabled)));
297 f.push_back (MenuElem (_("Soloed..."), sigc::mem_fun (*this, &GroupTabs::new_from_soloed)));
300 _menu->set_name ("ArdourContextMenu");
301 MenuList& items = _menu->items();
303 items.push_back (MenuElem (_("New..."), hide_return (sigc::mem_fun(*this, &GroupTabs::create_and_add_group))));
304 items.push_back (MenuElem (_("New From"), *new_from));
307 items.push_back (MenuElem (_("Edit..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::edit_group), g)));
308 items.push_back (MenuElem (_("Add New Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, false, PreFader)));
309 items.push_back (MenuElem (_("Add New Aux Bus (pre-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PreFader)));
310 items.push_back (MenuElem (_("Add New Aux Bus (post-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PostFader)));
311 items.push_back (MenuElem (_("Collect"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::collect), g)));
312 items.push_back (MenuElem (_("Remove"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::remove_group), g)));
315 add_menu_items (_menu, g);
317 items.push_back (SeparatorElem());
318 items.push_back (MenuElem (_("Activate All"), sigc::mem_fun(*this, &GroupTabs::activate_all)));
319 items.push_back (MenuElem (_("Disable All"), sigc::mem_fun(*this, &GroupTabs::disable_all)));
326 GroupTabs::new_from_selection ()
328 RouteList rl = selected_routes ();
333 run_new_group_dialog (rl);
337 GroupTabs::new_from_rec_enabled ()
339 boost::shared_ptr<RouteList> rl = _session->get_routes ();
341 RouteList rec_enabled;
343 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
344 if ((*i)->record_enabled()) {
345 rec_enabled.push_back (*i);
349 if (rec_enabled.empty()) {
353 run_new_group_dialog (rec_enabled);
357 GroupTabs::new_from_soloed ()
359 boost::shared_ptr<RouteList> rl = _session->get_routes ();
363 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
364 if (!(*i)->is_master() && (*i)->soloed()) {
365 soloed.push_back (*i);
369 if (soloed.empty()) {
373 run_new_group_dialog (soloed);
377 GroupTabs::run_new_group_dialog (RouteList const & rl)
379 RouteGroup* g = new RouteGroup (*_session, "");
380 g->apply_changes (default_properties ());
382 RouteGroupDialog d (g, true);
387 _session->add_route_group (g);
388 for (RouteList::const_iterator i = rl.begin(); i != rl.end(); ++i) {
395 GroupTabs::create_and_add_group () const
397 RouteGroup* g = new RouteGroup (*_session, "");
399 g->apply_changes (default_properties ());
401 RouteGroupDialog d (g, true);
408 _session->add_route_group (g);
413 GroupTabs::edit_group (RouteGroup* g)
415 RouteGroupDialog d (g, false);
420 GroupTabs::subgroup (RouteGroup* g, bool aux, Placement placement)
422 g->make_subgroup (aux, placement);
425 struct CollectSorter {
426 CollectSorter (std::string const & key) : _key (key) {}
428 bool operator () (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
429 return a->order_key (_key) < b->order_key (_key);
435 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
436 * @param g Group to collect.
439 GroupTabs::collect (RouteGroup* g)
441 boost::shared_ptr<RouteList> group_routes = g->route_list ();
442 group_routes->sort (CollectSorter (order_key ()));
443 int const N = group_routes->size ();
445 RouteList::iterator i = group_routes->begin ();
446 boost::shared_ptr<RouteList> routes = _session->get_routes ();
447 RouteList::const_iterator j = routes->begin ();
451 while (i != group_routes->end() && j != routes->end()) {
453 int const k = (*j)->order_key (order_key ());
464 (*j)->set_order_key (order_key (), coll);
471 (*j)->set_order_key (order_key (), k + diff);
482 GroupTabs::activate_all ()
484 _session->foreach_route_group (
485 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
490 GroupTabs::disable_all ()
492 _session->foreach_route_group (
493 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
498 GroupTabs::set_activation (RouteGroup* g, bool a)
500 g->set_active (a, this);
504 GroupTabs::remove_group (RouteGroup* g)
506 _session->remove_route_group (*g);
509 /** Set the color of the tab of a route group */
511 GroupTabs::set_group_color (RouteGroup* group, Gdk::Color color)
515 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
518 snprintf (buf, sizeof (buf), "%d:%d:%d", color.get_red(), color.get_green(), color.get_blue());
519 gui_state.set (group_gui_id (group), "color", buf);
521 /* This is a bit of a hack, but this might change
522 our route's effective color, so emit gui_changed
526 for (RouteList::iterator i = group->route_list()->begin(); i != group->route_list()->end(); ++i) {
527 (*i)->gui_changed (X_("color"), 0);
531 /** @return the ID string to use for the GUI state of a route group */
533 GroupTabs::group_gui_id (RouteGroup* group)
538 snprintf (buf, sizeof (buf), "route_group %s", group->id().to_s().c_str ());
543 /** @return the color to use for a route group tab */
545 GroupTabs::group_color (RouteGroup* group)
549 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
551 string const gui_id = group_gui_id (group);
554 string const color = gui_state.get_string (gui_id, "color", &empty);
556 /* no color has yet been set, so use a random one */
557 Gdk::Color const color = unique_random_color (_used_colors);
558 set_group_color (group, color);
566 sscanf (color.c_str(), "%d:%d:%d", &r, &g, &b);