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