Rename menu items on advice from Chris G.
[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 (_("Add New Subgroup Bus"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, false, PreFader)));
312                 items.push_back (MenuElem (_("Add New Aux Bus (pre-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PreFader)));
313                 items.push_back (MenuElem (_("Add New Aux Bus (post-fader)"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::subgroup), g, true, PostFader)));
314                 items.push_back (MenuElem (_("Collect"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::collect), g)));
315                 items.push_back (MenuElem (_("Remove"), sigc::bind (sigc::mem_fun (*this, &GroupTabs::remove_group), g)));
316         }
317
318         add_menu_items (_menu, g);
319         
320         items.push_back (SeparatorElem());
321         items.push_back (MenuElem (_("Activate All"), sigc::mem_fun(*this, &GroupTabs::activate_all)));
322         items.push_back (MenuElem (_("Disable All"), sigc::mem_fun(*this, &GroupTabs::disable_all)));
323
324         return _menu;
325         
326 }
327
328 void
329 GroupTabs::new_from_selection ()
330 {
331         RouteList rl = selected_routes ();
332         if (rl.empty()) {
333                 return;
334         }
335
336         run_new_group_dialog (rl);
337 }
338
339 void
340 GroupTabs::new_from_rec_enabled ()
341 {
342         boost::shared_ptr<RouteList> rl = _session->get_routes ();
343
344         RouteList rec_enabled;
345
346         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
347                 if ((*i)->record_enabled()) {
348                         rec_enabled.push_back (*i);
349                 }
350         }
351
352         if (rec_enabled.empty()) {
353                 return;
354         }
355
356         run_new_group_dialog (rec_enabled);
357 }
358
359 void
360 GroupTabs::new_from_soloed ()
361 {
362         boost::shared_ptr<RouteList> rl = _session->get_routes ();
363
364         RouteList soloed;
365
366         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
367                 if (!(*i)->is_master() && (*i)->soloed()) {
368                         soloed.push_back (*i);
369                 }
370         }
371
372         if (soloed.empty()) {
373                 return;
374         }
375
376         run_new_group_dialog (soloed);
377
378 }
379
380 void
381 GroupTabs::run_new_group_dialog (RouteList const & rl)
382 {
383         RouteGroup* g = new RouteGroup (*_session, "");
384         g->apply_changes (default_properties ());
385
386         RouteGroupDialog d (g, Gtk::Stock::NEW);
387         int const r = d.do_run ();
388
389         switch (r) {
390         case Gtk::RESPONSE_OK:
391         case Gtk::RESPONSE_ACCEPT:
392                 _session->add_route_group (g);
393                 for (RouteList::const_iterator i = rl.begin(); i != rl.end(); ++i) {
394                         g->add (*i);
395                 }
396                 break;
397         default:
398                 delete g;
399         }
400 }
401
402 RouteGroup *
403 GroupTabs::create_and_add_group () const
404 {
405         RouteGroup* g = new RouteGroup (*_session, "");
406
407         g->apply_changes (default_properties ());
408
409         RouteGroupDialog d (g, Gtk::Stock::NEW);
410         int const r = d.do_run ();
411
412         if (r != Gtk::RESPONSE_OK) {
413                 delete g;
414                 return 0;
415         }
416         
417         _session->add_route_group (g);
418         return g;
419 }
420
421 void
422 GroupTabs::edit_group (RouteGroup* g)
423 {
424         RouteGroupDialog d (g, Gtk::Stock::APPLY);
425         d.do_run ();
426 }
427
428 void
429 GroupTabs::subgroup (RouteGroup* g, bool aux, Placement placement)
430 {
431         g->make_subgroup (aux, placement);
432 }
433
434 struct CollectSorter {
435         CollectSorter (std::string const & key) : _key (key) {}
436         
437         bool operator () (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
438                 return a->order_key (_key) < b->order_key (_key);
439         }
440
441         std::string _key;
442 };
443
444 /** Collect all members of a RouteGroup so that they are together in the Editor or Mixer.
445  *  @param g Group to collect.
446  */
447 void
448 GroupTabs::collect (RouteGroup* g)
449 {
450         boost::shared_ptr<RouteList> group_routes = g->route_list ();
451         group_routes->sort (CollectSorter (order_key ()));
452         int const N = group_routes->size ();
453
454         RouteList::iterator i = group_routes->begin ();
455         boost::shared_ptr<RouteList> routes = _session->get_routes ();
456         RouteList::const_iterator j = routes->begin ();
457
458         int diff = 0;
459         int coll = -1;
460         while (i != group_routes->end() && j != routes->end()) {
461
462                 int const k = (*j)->order_key (order_key ());
463
464                 if (*i == *j) {
465
466                         if (coll == -1) {
467                                 coll = k;
468                                 diff = N - 1;
469                         } else {
470                                 --diff;
471                         }
472
473                         (*j)->set_order_key (order_key (), coll);
474
475                         ++coll;
476                         ++i;
477
478                 } else {
479                         
480                         (*j)->set_order_key (order_key (), k + diff);
481                         
482                 }
483
484                 ++j;
485         }
486
487         sync_order_keys ();
488 }
489
490 void
491 GroupTabs::activate_all ()
492 {
493         _session->foreach_route_group (
494                 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), true)
495                 );
496 }
497
498 void
499 GroupTabs::disable_all ()
500 {
501         _session->foreach_route_group (
502                 sigc::bind (sigc::mem_fun (*this, &GroupTabs::set_activation), false)
503                 );
504 }
505
506 void
507 GroupTabs::set_activation (RouteGroup* g, bool a)
508 {
509         g->set_active (a, this);
510 }
511         
512 void
513 GroupTabs::remove_group (RouteGroup* g)
514 {
515         _session->remove_route_group (*g);
516 }