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 contained in the tab we are dragging ... */
209 if ((*i)->route_group() != _dragging->group) {
210 /* and it's not in the dragged tab's group either */
211 _dragging->group->remove (*i);
214 _dragging->group->add (*i);
230 GroupTabs::render (cairo_t* cr)
232 if (_dragging == 0) {
233 _tabs = compute_tabs ();
238 cairo_set_source_rgb (cr, 0, 0, 0);
239 cairo_rectangle (cr, 0, 0, _width, _height);
244 for (list<Tab>::const_iterator i = _tabs.begin(); i != _tabs.end(); ++i) {
250 /** Convert a click position to a tab.
251 * @param c Click position.
252 * @param prev Filled in with the previous tab to the click, or _tabs.end().
253 * @param next Filled in with the next tab after the click, or _tabs.end().
254 * @return Tab under the click, or 0.
258 GroupTabs::click_to_tab (double c, list<Tab>::iterator* prev, list<Tab>::iterator* next)
260 *prev = *next = _tabs.end ();
263 list<Tab>::iterator i = _tabs.begin ();
264 while (i != _tabs.end()) {
277 if (i->from <= c && c < i->to) {
288 GroupTabs::get_menu (RouteGroup* g)
290 using namespace Menu_Helpers;
294 Menu* new_from = new Menu;
295 MenuList& f = new_from->items ();
296 f.push_back (MenuElem (_("Selection..."), sigc::mem_fun (*this, &GroupTabs::new_from_selection)));
297 f.push_back (MenuElem (_("Record Enabled..."), sigc::mem_fun (*this, &GroupTabs::new_from_rec_enabled)));
298 f.push_back (MenuElem (_("Soloed..."), sigc::mem_fun (*this, &GroupTabs::new_from_soloed)));
301 _menu->set_name ("ArdourContextMenu");
302 MenuList& items = _menu->items();
304 items.push_back (MenuElem (_("New..."), hide_return (sigc::mem_fun(*this, &GroupTabs::create_and_add_group))));
305 items.push_back (MenuElem (_("New From"), *new_from));
308 items.push_back (MenuElem (_("Edit..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::edit_group), g)));
309 items.push_back (MenuElem (_("Add New Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, false, PreFader)));
310 items.push_back (MenuElem (_("Add New Aux Bus (pre-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PreFader)));
311 items.push_back (MenuElem (_("Add New Aux Bus (post-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PostFader)));
312 items.push_back (MenuElem (_("Collect"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::collect), g)));
313 items.push_back (MenuElem (_("Remove"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::remove_group), g)));
316 add_menu_items (_menu, g);
318 items.push_back (SeparatorElem());
319 items.push_back (MenuElem (_("Activate All"), sigc::mem_fun(*this, &GroupTabs::activate_all)));
320 items.push_back (MenuElem (_("Disable All"), sigc::mem_fun(*this, &GroupTabs::disable_all)));
327 GroupTabs::new_from_selection ()
329 RouteList rl = selected_routes ();
334 run_new_group_dialog (rl);
338 GroupTabs::new_from_rec_enabled ()
340 boost::shared_ptr<RouteList> rl = _session->get_routes ();
342 RouteList rec_enabled;
344 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
345 if ((*i)->record_enabled()) {
346 rec_enabled.push_back (*i);
350 if (rec_enabled.empty()) {
354 run_new_group_dialog (rec_enabled);
358 GroupTabs::new_from_soloed ()
360 boost::shared_ptr<RouteList> rl = _session->get_routes ();
364 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
365 if (!(*i)->is_master() && (*i)->soloed()) {
366 soloed.push_back (*i);
370 if (soloed.empty()) {
374 run_new_group_dialog (soloed);
378 GroupTabs::run_new_group_dialog (RouteList const & rl)
380 RouteGroup* g = new RouteGroup (*_session, "");
381 g->apply_changes (default_properties ());
383 RouteGroupDialog d (g, true);
388 _session->add_route_group (g);
389 for (RouteList::const_iterator i = rl.begin(); i != rl.end(); ++i) {
396 GroupTabs::create_and_add_group () const
398 RouteGroup* g = new RouteGroup (*_session, "");
400 g->apply_changes (default_properties ());
402 RouteGroupDialog d (g, true);
409 _session->add_route_group (g);
414 GroupTabs::edit_group (RouteGroup* g)
416 RouteGroupDialog d (g, false);
421 GroupTabs::subgroup (RouteGroup* g, bool aux, Placement placement)
423 g->make_subgroup (aux, placement);
426 struct CollectSorter {
427 CollectSorter (std::string const & key) : _key (key) {}
429 bool operator () (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
430 return a->order_key (_key) < b->order_key (_key);
436 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
437 * @param g Group to collect.
440 GroupTabs::collect (RouteGroup* g)
442 boost::shared_ptr<RouteList> group_routes = g->route_list ();
443 group_routes->sort (CollectSorter (order_key ()));
444 int const N = group_routes->size ();
446 RouteList::iterator i = group_routes->begin ();
447 boost::shared_ptr<RouteList> routes = _session->get_routes ();
448 RouteList::const_iterator j = routes->begin ();
452 while (i != group_routes->end() && j != routes->end()) {
454 int const k = (*j)->order_key (order_key ());
465 (*j)->set_order_key (order_key (), coll);
472 (*j)->set_order_key (order_key (), k + diff);
483 GroupTabs::activate_all ()
485 _session->foreach_route_group (
486 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
491 GroupTabs::disable_all ()
493 _session->foreach_route_group (
494 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
499 GroupTabs::set_activation (RouteGroup* g, bool a)
501 g->set_active (a, this);
505 GroupTabs::remove_group (RouteGroup* g)
507 _session->remove_route_group (*g);
510 /** Set the color of the tab of a route group */
512 GroupTabs::set_group_color (RouteGroup* group, Gdk::Color color)
516 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
519 snprintf (buf, sizeof (buf), "%d:%d:%d", color.get_red(), color.get_green(), color.get_blue());
520 gui_state.set (group_gui_id (group), "color", buf);
522 /* This is a bit of a hack, but this might change
523 our route's effective color, so emit gui_changed
527 for (RouteList::iterator i = group->route_list()->begin(); i != group->route_list()->end(); ++i) {
528 (*i)->gui_changed (X_("color"), 0);
532 /** @return the ID string to use for the GUI state of a route group */
534 GroupTabs::group_gui_id (RouteGroup* group)
539 snprintf (buf, sizeof (buf), "route_group %s", group->id().to_s().c_str ());
544 /** @return the color to use for a route group tab */
546 GroupTabs::group_color (RouteGroup* group)
550 GUIObjectState& gui_state = *ARDOUR_UI::instance()->gui_object_state;
552 string const gui_id = group_gui_id (group);
555 string const color = gui_state.get_string (gui_id, "color", &empty);
557 /* no color has yet been set, so use a random one */
558 Gdk::Color const color = unique_random_color (_used_colors);
559 set_group_color (group, color);
567 sscanf (color.c_str(), "%d:%d:%d", &r, &g, &b);