Move RDF-based preset stuff into LadspaPlugin, to make way for a set of evil hacks...
[ardour.git] / gtk2_ardour / group_tabs.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <gtkmm/stock.h>
21 #include "ardour/session.h"
22 #include "ardour/route_group.h"
23 #include "ardour/route.h"
24
25 #include "gui_thread.h"
26 #include "route_group_dialog.h"
27 #include "group_tabs.h"
28 #include "keyboard.h"
29 #include "i18n.h"
30
31 using namespace std;
32 using namespace Gtk;
33 using namespace ARDOUR;
34 using Gtkmm2ext::Keyboard;
35
36 GroupTabs::GroupTabs ()
37         : _menu (0)
38         , _dragging (0)
39         , _dragging_new_tab (0)
40 {
41
42 }
43
44 GroupTabs::~GroupTabs ()
45 {
46         delete _menu;
47 }
48
49 void
50 GroupTabs::set_session (Session* s)
51 {
52         SessionHandlePtr::set_session (s);
53
54         if (_session) {
55                 _session->RouteGroupChanged.connect (_session_connections, invalidator (*this), boost::bind (&GroupTabs::set_dirty, this), gui_context());
56                 _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&GroupTabs::set_dirty, this), gui_context());
57         }
58 }
59
60
61 /** Handle a size request.
62  *  @param req GTK requisition
63  */
64 void
65 GroupTabs::on_size_request (Gtk::Requisition *req)
66 {
67         /* Use a dummy, small width and the actual height that we want */
68         req->width = 16;
69         req->height = 16;
70 }
71
72 bool
73 GroupTabs::on_button_press_event (GdkEventButton* ev)
74 {
75         using namespace Menu_Helpers;
76
77         double const p = primary_coordinate (ev->x, ev->y);
78
79         list<Tab>::iterator prev;
80         list<Tab>::iterator next;
81         Tab* t = click_to_tab (p, &prev, &next);
82
83         _drag_min = prev != _tabs.end() ? prev->to : 0;
84         _drag_max = next != _tabs.end() ? next->from : extent ();
85
86         if (ev->button == 1) {
87
88                 if (t == 0) {
89                         Tab n;
90                         n.from = n.to = p;
91                         _dragging_new_tab = true;
92
93                         if (next == _tabs.end()) {
94                                 _tabs.push_back (n);
95                                 t = &_tabs.back ();
96                         } else {
97                                 list<Tab>::iterator j = _tabs.insert (next, n);
98                                 t = &(*j);
99                         }
100                         
101                 } else {
102                         _dragging_new_tab = false;
103                 }
104
105                 _dragging = t;
106                 _drag_moved = false;
107                 _drag_first = p;
108
109                 double const h = (t->from + t->to) / 2;
110                 if (p < h) {
111                         _drag_moving = t->from;
112                         _drag_fixed = t->to;
113                         _drag_offset = p - t->from;
114                 } else {
115                         _drag_moving = t->to;
116                         _drag_fixed = t->from;
117                         _drag_offset = p - t->to;
118                 }
119
120         } else if (ev->button == 3) {
121
122                 RouteGroup* g = t ? t->group : 0;
123                 Menu* m = get_menu (g);
124                 if (m) {
125                         m->popup (ev->button, ev->time);
126                 }
127
128         }
129
130         return true;
131 }
132
133
134 bool
135 GroupTabs::on_motion_notify_event (GdkEventMotion* ev)
136 {
137         if (_dragging == 0) {
138                 return false;
139         }
140
141         double const p = primary_coordinate (ev->x, ev->y);
142
143         if (p != _drag_first) {
144                 _drag_moved = true;
145         }
146
147         _drag_moving = p - _drag_offset;
148
149         _dragging->from = min (_drag_moving, _drag_fixed);
150         _dragging->to = max (_drag_moving, _drag_fixed);
151
152         _dragging->from = max (_dragging->from, _drag_min);
153         _dragging->to = min (_dragging->to, _drag_max);
154
155         set_dirty ();
156         queue_draw ();
157
158         return true;
159 }
160
161
162 bool
163 GroupTabs::on_button_release_event (GdkEventButton* ev)
164 {
165         if (_dragging == 0) {
166                 return false;
167         }
168
169         if (!_drag_moved) {
170
171                 if (_dragging->group) {
172                         
173                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
174                                 
175                                 /* edit */
176                                 RouteGroupDialog d (_dragging->group, Gtk::Stock::APPLY);
177                                 d.do_run ();
178                                 
179                         } else {
180                                 
181                                 /* toggle active state */
182                                 _dragging->group->set_active (!_dragging->group->is_active (), this);
183                                 
184                         }
185                 }
186
187         } else {
188                 /* finish drag */
189                 RouteList routes = routes_for_tab (_dragging);
190
191                 if (!routes.empty()) {
192                         if (_dragging_new_tab) {
193                                 RouteGroup* g = create_and_add_group ();
194                                 if (g) {
195                                         for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
196                                                 g->add (*i);
197                                         }
198                                 }
199                         } else {
200                                 boost::shared_ptr<RouteList> r = _session->get_routes ();
201                                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
202
203                                         if (find (routes.begin(), routes.end(), *i) == routes.end()) {
204                                                 /* this route is not on the list of those that should be in _dragging's group */
205                                                 if ((*i)->route_group() == _dragging->group) {
206                                                         _dragging->group->remove (*i);
207                                                 }
208                                         } else {
209                                                 _dragging->group->add (*i);
210                                         }
211                                 }
212                         }
213                 }
214
215                 set_dirty ();
216                 queue_draw ();
217         }
218
219         _dragging = 0;
220
221         return true;
222 }
223
224 void
225 GroupTabs::render (cairo_t* cr)
226 {
227         if (_dragging == 0) {
228                 _tabs = compute_tabs ();
229         }
230
231         /* background */
232
233         cairo_set_source_rgb (cr, 0, 0, 0);
234         cairo_rectangle (cr, 0, 0, _width, _height);
235         cairo_fill (cr);
236
237         /* tabs */
238
239         for (list<Tab>::const_iterator i = _tabs.begin(); i != _tabs.end(); ++i) {
240                 draw_tab (cr, *i);
241         }
242 }
243
244
245 /** Convert a click position to a tab.
246  *  @param c Click position.
247  *  @param prev Filled in with the previous tab to the click, or 0.
248  *  @param next Filled in with the next tab after the click, or 0.
249  *  @return Tab under the click, or 0.
250  */
251
252 GroupTabs::Tab *
253 GroupTabs::click_to_tab (double c, list<Tab>::iterator* prev, list<Tab>::iterator* next)
254 {
255         *prev = *next = _tabs.end ();
256         Tab* under = 0;
257
258         list<Tab>::iterator i = _tabs.begin ();
259         while (i != _tabs.end()) {
260
261                 if (i->from > c) {
262                         break;
263                 }
264
265                 if (i->to < c) {
266                         *prev = i;
267                         ++i;
268                         continue;
269                 }
270
271                 if (i->from <= c && c < i->to) {
272                         under = &(*i);
273                 }
274
275                 ++i;
276         }
277
278         if (i != _tabs.end()) {
279                 *next = i;
280
281                 if (under) {
282                         *next++;
283                 }
284         }
285
286         return under;
287 }
288
289 Gtk::Menu*
290 GroupTabs::get_menu (RouteGroup* g)
291 {
292         using namespace Menu_Helpers;
293
294         delete _menu;
295
296         Menu* new_from = new Menu;
297         MenuList& f = new_from->items ();
298         f.push_back (MenuElem (_("Selection..."), sigc::mem_fun (*this, &GroupTabs::new_from_selection)));
299         f.push_back (MenuElem (_("Record Enabled..."), sigc::mem_fun (*this, &GroupTabs::new_from_rec_enabled)));
300         f.push_back (MenuElem (_("Soloed..."), sigc::mem_fun (*this, &GroupTabs::new_from_soloed)));
301
302         _menu = new Menu;
303         _menu->set_name ("ArdourContextMenu");
304         MenuList& items = _menu->items();
305
306         items.push_back (MenuElem (_("New..."), hide_return (sigc::mem_fun(*this, &GroupTabs::create_and_add_group))));
307         items.push_back (MenuElem (_("New From"), *new_from));
308         
309         if (g) {
310                 items.push_back (MenuElem (_("Edit..."), sigc::bind (sigc::mem_fun (*this, &GroupTabs::edit_group), g)));
311                 items.push_back (MenuElem (_("Subgroup"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g)));
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)));
314         }
315
316         add_menu_items (_menu, g);
317         
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)));
321
322         return _menu;
323         
324 }
325
326 void
327 GroupTabs::new_from_selection ()
328 {
329         RouteList rl = selected_routes ();
330         if (rl.empty()) {
331                 return;
332         }
333
334         run_new_group_dialog (rl);
335 }
336
337 void
338 GroupTabs::new_from_rec_enabled ()
339 {
340         boost::shared_ptr<RouteList> rl = _session->get_routes ();
341
342         RouteList rec_enabled;
343
344         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
345                 if ((*i)->record_enabled()) {
346                         rec_enabled.push_back (*i);
347                 }
348         }
349
350         if (rec_enabled.empty()) {
351                 return;
352         }
353
354         run_new_group_dialog (rec_enabled);
355 }
356
357 void
358 GroupTabs::new_from_soloed ()
359 {
360         boost::shared_ptr<RouteList> rl = _session->get_routes ();
361
362         RouteList soloed;
363
364         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
365                 if (!(*i)->is_master() && (*i)->soloed()) {
366                         soloed.push_back (*i);
367                 }
368         }
369
370         if (soloed.empty()) {
371                 return;
372         }
373
374         run_new_group_dialog (soloed);
375
376 }
377
378 void
379 GroupTabs::run_new_group_dialog (RouteList const & rl)
380 {
381         RouteGroup* g = new RouteGroup (*_session, "");
382         g->apply_changes (default_properties ());
383
384         RouteGroupDialog d (g, Gtk::Stock::NEW);
385         int const r = d.do_run ();
386
387         switch (r) {
388         case Gtk::RESPONSE_OK:
389         case Gtk::RESPONSE_ACCEPT:
390                 _session->add_route_group (g);
391                 for (RouteList::const_iterator i = rl.begin(); i != rl.end(); ++i) {
392                         g->add (*i);
393                 }
394                 break;
395         default:
396                 delete g;
397         }
398 }
399
400 RouteGroup *
401 GroupTabs::create_and_add_group () const
402 {
403         RouteGroup* g = new RouteGroup (*_session, "");
404
405         g->apply_changes (default_properties ());
406
407         RouteGroupDialog d (g, Gtk::Stock::NEW);
408         int const r = d.do_run ();
409
410         if (r != Gtk::RESPONSE_OK) {
411                 delete g;
412                 return 0;
413         }
414         
415         _session->add_route_group (g);
416         return g;
417 }
418
419 void
420 GroupTabs::edit_group (RouteGroup* g)
421 {
422         RouteGroupDialog d (g, Gtk::Stock::APPLY);
423         d.do_run ();
424 }
425
426 void
427 GroupTabs::subgroup (RouteGroup* g)
428 {
429         g->make_subgroup ();
430 }
431
432 struct CollectSorter {
433         CollectSorter (std::string const & key) : _key (key) {}
434         
435         bool operator () (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
436                 return a->order_key (_key) < b->order_key (_key);
437         }
438
439         std::string _key;
440 };
441
442 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
443  *  @param g Group to collect.
444  */
445 void
446 GroupTabs::collect (RouteGroup* g)
447 {
448         boost::shared_ptr<RouteList> group_routes = g->route_list ();
449         group_routes->sort (CollectSorter (order_key ()));
450         int const N = group_routes->size ();
451
452         RouteList::iterator i = group_routes->begin ();
453         boost::shared_ptr<RouteList> routes = _session->get_routes ();
454         RouteList::const_iterator j = routes->begin ();
455
456         int diff = 0;
457         int coll = -1;
458         while (i != group_routes->end() && j != routes->end()) {
459
460                 int const k = (*j)->order_key (order_key ());
461
462                 if (*i == *j) {
463
464                         if (coll == -1) {
465                                 coll = k;
466                                 diff = N - 1;
467                         } else {
468                                 --diff;
469                         }
470
471                         (*j)->set_order_key (order_key (), coll);
472
473                         ++coll;
474                         ++i;
475
476                 } else {
477                         
478                         (*j)->set_order_key (order_key (), k + diff);
479                         
480                 }
481
482                 ++j;
483         }
484
485         sync_order_keys ();
486 }
487
488 void
489 GroupTabs::activate_all ()
490 {
491         _session->foreach_route_group (
492                 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
493                 );
494 }
495
496 void
497 GroupTabs::disable_all ()
498 {
499         _session->foreach_route_group (
500                 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
501                 );
502 }
503
504 void
505 GroupTabs::set_activation (RouteGroup* g, bool a)
506 {
507         g->set_active (a, this);
508 }
509         
510 void
511 GroupTabs::remove_group (RouteGroup* g)
512 {
513         _session->remove_route_group (*g);
514 }