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;
107 _initial_dragging_routes = routes_for_tab (t);
114 double const h = (t->from + t->to) / 2;
116 _drag_moving = t->from;
118 _drag_offset = p - t->from;
120 _drag_moving = t->to;
121 _drag_fixed = t->from;
122 _drag_offset = p - t->to;
125 } else if (ev->button == 3) {
127 RouteGroup* g = t ? t->group : 0;
128 Menu* m = get_menu (g);
130 m->popup (ev->button, ev->time);
140 GroupTabs::on_motion_notify_event (GdkEventMotion* ev)
142 if (_dragging == 0) {
146 double const p = primary_coordinate (ev->x, ev->y);
148 if (p != _drag_first) {
152 _drag_moving = p - _drag_offset;
154 _dragging->from = min (_drag_moving, _drag_fixed);
155 _dragging->to = max (_drag_moving, _drag_fixed);
157 _dragging->from = max (_dragging->from, _drag_min);
158 _dragging->to = min (_dragging->to, _drag_max);
168 GroupTabs::on_button_release_event (GdkEventButton* ev)
170 if (_dragging == 0) {
176 if (_dragging->group) {
178 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
181 RouteGroupDialog d (_dragging->group, false);
186 /* toggle active state */
187 _dragging->group->set_active (!_dragging->group->is_active (), this);
194 RouteList routes = routes_for_tab (_dragging);
196 if (!routes.empty()) {
197 if (_dragging_new_tab) {
198 RouteGroup* g = create_and_add_group ();
200 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
205 boost::shared_ptr<RouteList> r = _session->get_routes ();
206 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
208 bool const was_in_tab = find (
209 _initial_dragging_routes.begin(), _initial_dragging_routes.end(), *i
210 ) != _initial_dragging_routes.end ();
212 bool const now_in_tab = find (routes.begin(), routes.end(), *i) != routes.end();
214 if (was_in_tab && !now_in_tab) {
215 _dragging->group->remove (*i);
216 } else if (!was_in_tab && now_in_tab) {
217 _dragging->group->add (*i);
228 _initial_dragging_routes.clear ();
234 GroupTabs::render (cairo_t* cr)
236 if (_dragging == 0) {
237 _tabs = compute_tabs ();
242 cairo_set_source_rgb (cr, 0, 0, 0);
243 cairo_rectangle (cr, 0, 0, _width, _height);
248 for (list<Tab>::const_iterator i = _tabs.begin(); i != _tabs.end(); ++i) {
254 /** Convert a click position to a tab.
255 * @param c Click position.
256 * @param prev Filled in with the previous tab to the click, or _tabs.end().
257 * @param next Filled in with the next tab after the click, or _tabs.end().
258 * @return Tab under the click, or 0.
262 GroupTabs::click_to_tab (double c, list<Tab>::iterator* prev, list<Tab>::iterator* next)
264 *prev = *next = _tabs.end ();
267 list<Tab>::iterator i = _tabs.begin ();
268 while (i != _tabs.end()) {
281 if (i->from <= c && c < i->to) {
292 GroupTabs::get_menu (RouteGroup* g)
294 using namespace Menu_Helpers;
298 Menu* new_from = new Menu;
299 MenuList& f = new_from->items ();
300 f.push_back (MenuElem (_("Selection..."), sigc::mem_fun (*this, &GroupTabs::new_from_selection)));
301 f.push_back (MenuElem (_("Record Enabled..."), sigc::mem_fun (*this, &GroupTabs::new_from_rec_enabled)));
302 f.push_back (MenuElem (_("Soloed..."), sigc::mem_fun (*this, &GroupTabs::new_from_soloed)));
305 _menu->set_name ("ArdourContextMenu");
306 MenuList& items = _menu->items();
308 items.push_back (MenuElem (_("New..."), hide_return (sigc::mem_fun(*this, &GroupTabs::create_and_add_group))));
309 items.push_back (MenuElem (_("New From"), *new_from));
312 items.push_back (MenuElem (_("Edit..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::edit_group), g)));
313 items.push_back (MenuElem (_("Add New Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, false, PreFader)));
314 items.push_back (MenuElem (_("Add New Aux Bus (pre-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PreFader)));
315 items.push_back (MenuElem (_("Add New Aux Bus (post-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PostFader)));
316 items.push_back (MenuElem (_("Collect"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::collect), g)));
317 items.push_back (MenuElem (_("Remove"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::remove_group), g)));
320 add_menu_items (_menu, g);
322 items.push_back (SeparatorElem());
323 items.push_back (MenuElem (_("Activate All"), sigc::mem_fun(*this, &GroupTabs::activate_all)));
324 items.push_back (MenuElem (_("Disable All"), sigc::mem_fun(*this, &GroupTabs::disable_all)));
331 GroupTabs::new_from_selection ()
333 RouteList rl = selected_routes ();
338 run_new_group_dialog (rl);
342 GroupTabs::new_from_rec_enabled ()
344 boost::shared_ptr<RouteList> rl = _session->get_routes ();
346 RouteList rec_enabled;
348 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
349 if ((*i)->record_enabled()) {
350 rec_enabled.push_back (*i);
354 if (rec_enabled.empty()) {
358 run_new_group_dialog (rec_enabled);
362 GroupTabs::new_from_soloed ()
364 boost::shared_ptr<RouteList> rl = _session->get_routes ();
368 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
369 if (!(*i)->is_master() && (*i)->soloed()) {
370 soloed.push_back (*i);
374 if (soloed.empty()) {
378 run_new_group_dialog (soloed);
382 GroupTabs::run_new_group_dialog (RouteList const & rl)
384 RouteGroup* g = new RouteGroup (*_session, "");
385 g->apply_changes (default_properties ());
387 RouteGroupDialog d (g, true);
392 _session->add_route_group (g);
393 for (RouteList::const_iterator i = rl.begin(); i != rl.end(); ++i) {
400 GroupTabs::create_and_add_group () const
402 RouteGroup* g = new RouteGroup (*_session, "");
404 g->apply_changes (default_properties ());
406 RouteGroupDialog d (g, true);
413 _session->add_route_group (g);
418 GroupTabs::edit_group (RouteGroup* g)
420 RouteGroupDialog d (g, false);
425 GroupTabs::subgroup (RouteGroup* g, bool aux, Placement placement)
427 g->make_subgroup (aux, placement);
430 struct CollectSorter {
431 CollectSorter (std::string const & key) : _key (key) {}
433 bool operator () (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
434 return a->order_key (_key) < b->order_key (_key);
440 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
441 * @param g Group to collect.
444 GroupTabs::collect (RouteGroup* g)
446 boost::shared_ptr<RouteList> group_routes = g->route_list ();
447 group_routes->sort (CollectSorter (order_key ()));
448 int const N = group_routes->size ();
450 RouteList::iterator i = group_routes->begin ();
451 boost::shared_ptr<RouteList> routes = _session->get_routes ();
452 RouteList::const_iterator j = routes->begin ();
456 while (i != group_routes->end() && j != routes->end()) {
458 int const k = (*j)->order_key (order_key ());
469 (*j)->set_order_key (order_key (), coll);
476 (*j)->set_order_key (order_key (), k + diff);
487 GroupTabs::activate_all ()
489 _session->foreach_route_group (
490 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
495 GroupTabs::disable_all ()
497 _session->foreach_route_group (
498 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
503 GroupTabs::set_activation (RouteGroup* g, bool a)
505 g->set_active (a, this);
509 GroupTabs::remove_group (RouteGroup* g)
511 _session->remove_route_group (*g);
514 /** Set the color of the tab of a route group */
516 GroupTabs::set_group_color (RouteGroup* group, Gdk::Color color)
520 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
523 snprintf (buf, sizeof (buf), "%d:%d:%d", color.get_red(), color.get_green(), color.get_blue());
524 gui_state.set (group_gui_id (group), "color", buf);
526 /* This is a bit of a hack, but this might change
527 our route's effective color, so emit gui_changed
531 for (RouteList::iterator i = group->route_list()->begin(); i != group->route_list()->end(); ++i) {
532 (*i)->gui_changed (X_("color"), 0);
536 /** @return the ID string to use for the GUI state of a route group */
538 GroupTabs::group_gui_id (RouteGroup* group)
543 snprintf (buf, sizeof (buf), "route_group %s", group->id().to_s().c_str ());
548 /** @return the color to use for a route group tab */
550 GroupTabs::group_color (RouteGroup* group)
554 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
556 string const gui_id = group_gui_id (group);
559 string const color = gui_state.get_string (gui_id, "color", &empty);
561 /* no color has yet been set, so use a random one */
562 Gdk::Color const color = unique_random_color (_used_colors);
563 set_group_color (group, color);
571 sscanf (color.c_str(), "%d:%d:%d", &r, &g, &b);