correct button press detection order for editor route groups
[ardour.git] / gtk2_ardour / editor_route_groups.cc
1 /*
2     Copyright (C) 2000 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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <cstdlib>
25 #include <cmath>
26
27 #include "gtkmm2ext/gtk_ui.h"
28 #include "gtkmm2ext/cell_renderer_color_selector.h"
29
30 #include "ardour/route_group.h"
31
32 #include "editor.h"
33 #include "keyboard.h"
34 #include "marker.h"
35 #include "time_axis_view.h"
36 #include "prompter.h"
37 #include "gui_thread.h"
38 #include "editor_group_tabs.h"
39 #include "route_group_dialog.h"
40 #include "route_time_axis.h"
41 #include "editor_routes.h"
42 #include "editor_route_groups.h"
43 #include "ardour_ui.h"
44
45 #include "ardour/route.h"
46 #include "ardour/session.h"
47
48 #include "i18n.h"
49
50 using namespace std;
51 using namespace ARDOUR;
52 using namespace PBD;
53 using namespace Gtk;
54 using Gtkmm2ext::Keyboard;
55
56 struct ColumnInfo {
57     int         index;
58     const char* label;
59     const char* tooltip;
60 };
61
62 EditorRouteGroups::EditorRouteGroups (Editor* e)
63         : EditorComponent (e)
64         , _all_group_active_button (_("No Selection = All Tracks?"))
65         , _in_row_change (false)
66         , _in_rebuild (false)
67 {
68         _model = ListStore::create (_columns);
69         _display.set_model (_model);
70
71         Gtkmm2ext::CellRendererColorSelector* color_renderer = manage (new Gtkmm2ext::CellRendererColorSelector);
72         TreeViewColumn* color_column = manage (new TreeViewColumn ("", *color_renderer));
73         color_column->add_attribute (color_renderer->property_color(), _columns.gdkcolor);
74         
75         _display.append_column (*color_column);
76
77         _display.append_column ("", _columns.text);
78         _display.append_column ("", _columns.gain);
79         _display.append_column ("", _columns.gain_relative);
80         _display.append_column ("", _columns.mute);
81         _display.append_column ("", _columns.solo);
82         _display.append_column ("", _columns.record);
83         _display.append_column ("", _columns.monitoring);
84         _display.append_column ("", _columns.select);
85         _display.append_column ("", _columns.edits);
86         _display.append_column ("", _columns.active_state);
87         _display.append_column ("", _columns.is_visible);
88
89         TreeViewColumn* col;
90         Gtk::Label* l;
91
92         ColumnInfo ci[] = {
93                 { 0, _("Col"), _("Group Tab Color") },
94                 { 1, _("Name"), _("Name of Group") },
95                 { 2, S_("group|G"), _("Sharing Gain?") },
96                 { 3, S_("relative|Rel"), _("Relevative Gain Changes?") },
97                 { 4, S_("mute|M"), _("Sharing Mute?") },
98                 { 5, S_("solo|S"), _("Sharing Solo?") },
99                 { 6, _("Rec"), _("Sharing Record-enable Status?") },
100                 { 7, S_("monitoring|Mon"), _("Sharing Monitoring Choice?") },
101                 { 8, S_("selection|Sel"), _("Sharing Selected Status?") },
102                 { 9, S_("editing|E"), _("Sharing Editing?") },
103                 { 10, S_("active|A"), _("Sharing Active Status?") },
104                 { 11, _("Show"), _("Group is visible?") },
105                 { -1, 0, 0 }
106         };
107
108
109         for (int i = 0; ci[i].index >= 0; ++i) {
110                 col = _display.get_column (ci[i].index);
111                 l = manage (new Label (ci[i].label));
112                 ARDOUR_UI::instance()->set_tip (*l, ci[i].tooltip);
113                 col->set_widget (*l);
114                 l->show ();
115
116                 col->set_data (X_("colnum"), GUINT_TO_POINTER(i));
117                 if (i == 1) {
118                         col->set_expand (true);
119                 } else {
120                         col->set_expand (false);
121                         col->set_alignment (ALIGN_CENTER);
122                 }
123         }
124
125         _display.set_headers_visible (true);
126
127         color_dialog.get_colorsel()->set_has_opacity_control (false);
128         color_dialog.get_colorsel()->set_has_palette (true);
129         color_dialog.get_ok_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
130         color_dialog.get_cancel_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
131
132         /* name is directly editable */
133
134         CellRendererText* name_cell = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (1));
135         name_cell->property_editable() = true;
136         name_cell->signal_edited().connect (sigc::mem_fun (*this, &EditorRouteGroups::name_edit));
137         
138         for (int i = 1; ci[i].index >= 0; ++i) {
139                 CellRendererToggle* active_cell = dynamic_cast <CellRendererToggle*> (_display.get_column_cell_renderer (i));
140
141                 if (active_cell) {
142                         active_cell->property_activatable() = true;
143                         active_cell->property_radio() = false;
144                 }
145         }
146
147         _model->signal_row_changed().connect (sigc::mem_fun (*this, &EditorRouteGroups::row_change));
148         /* What signal would you guess was emitted when the rows of your treeview are reordered
149            by a drag and drop?  signal_rows_reordered?  That would be far too easy.
150            No, signal_row_deleted().
151          */
152         _model->signal_row_deleted().connect (sigc::mem_fun (*this, &EditorRouteGroups::row_deleted));
153
154         _display.set_name ("EditGroupList");
155         _display.get_selection()->set_mode (SELECTION_SINGLE);
156         _display.set_headers_visible (true);
157         _display.set_reorderable (false);
158         _display.set_rules_hint (true);
159
160         _scroller.add (_display);
161         _scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
162
163         _display.signal_button_press_event().connect (sigc::mem_fun(*this, &EditorRouteGroups::button_press_event), false);
164
165         HBox* button_box = manage (new HBox());
166         button_box->set_homogeneous (true);
167
168         Button* add_button = manage (new Button ());
169         Button* remove_button = manage (new Button ());
170
171         Widget* w;
172
173         w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
174         w->show();
175         add_button->add (*w);
176
177         w = manage (new Image (Stock::REMOVE, ICON_SIZE_BUTTON));
178         w->show();
179         remove_button->add (*w);
180
181         add_button->signal_clicked().connect (sigc::hide_return (sigc::mem_fun (*this, &EditorRouteGroups::run_new_group_dialog)));
182         remove_button->signal_clicked().connect (sigc::mem_fun (*this, &EditorRouteGroups::remove_selected));
183
184         button_box->pack_start (*add_button);
185         button_box->pack_start (*remove_button);
186
187         _all_group_active_button.show ();
188
189         _display_packer.pack_start (_scroller, true, true);
190         _display_packer.pack_start (_all_group_active_button, false, false);
191         _display_packer.pack_start (*button_box, false, false);
192
193         _all_group_active_button.signal_toggled().connect (sigc::mem_fun (*this, &EditorRouteGroups::all_group_toggled));
194         _all_group_active_button.set_name (X_("EditorRouteGroupsAllGroupButton"));
195         ARDOUR_UI::instance()->set_tip (_all_group_active_button, _("Activate this button to operate on all tracks when none are selected."));
196 }
197
198 void
199 EditorRouteGroups::remove_selected ()
200 {
201         Glib::RefPtr<TreeSelection> selection = _display.get_selection();
202         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
203
204         if (rows.empty()) {
205                 return;
206         }
207
208         TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
209         TreeIter iter;
210
211         /* selection mode is single, so rows.begin() is it */
212
213         if ((iter = _model->get_iter (*i))) {
214
215                 RouteGroup* rg = (*iter)[_columns.routegroup];
216
217                 if (rg) {
218                         _session->remove_route_group (*rg);
219                 }
220         }
221 }
222
223 void
224 EditorRouteGroups::button_clicked ()
225 {
226         run_new_group_dialog ();
227 }
228
229 bool
230 EditorRouteGroups::button_press_event (GdkEventButton* ev)
231 {
232         TreeModel::Path path;
233         TreeIter iter;
234         RouteGroup* group = 0;
235         TreeViewColumn* column;
236         int cellx;
237         int celly;
238         bool ret = false;
239         Gdk::Color c;
240
241         bool const p = _display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly);
242
243         if (p) {
244                 iter = _model->get_iter (path);
245         }
246
247         if (iter) {
248                 group = (*iter)[_columns.routegroup];
249         } 
250
251         if (Keyboard::is_context_menu_event (ev)) {
252                 _editor->_group_tabs->get_menu(group)->popup (1, ev->time);
253                 return true;
254         } 
255
256         if (!p) {
257                 /* cancel selection */
258                 _display.get_selection()->unselect_all ();
259                 /* end any editing by grabbing focus */
260                 _display.grab_focus ();
261                 return true;
262         }
263
264         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
265         case 0: 
266                 c = (*iter)[_columns.gdkcolor];
267
268                 color_dialog.get_colorsel()->set_previous_color (c);
269                 color_dialog.get_colorsel()->set_current_color (c);
270
271                 switch (color_dialog.run()) {
272                 case RESPONSE_CANCEL:
273                         break;
274                 case RESPONSE_ACCEPT:
275                         c = color_dialog.get_colorsel()->get_current_color();
276                         (*iter)[_columns.gdkcolor] = c;
277                         GroupTabs::set_group_color ((*iter)[_columns.routegroup], c);
278                         ARDOUR_UI::config()->set_dirty ();
279                         break;
280                         
281                 default:
282                         break;
283                         
284                 }
285
286                 color_dialog.hide ();
287                 ret = true;
288                 break;
289
290         case 1:
291                 if (Keyboard::is_edit_event (ev)) {
292                         if ((iter = _model->get_iter (path))) {
293                                 if ((group = (*iter)[_columns.routegroup]) != 0) {
294                                         ret = true;
295                                 }
296                         }
297
298                 }
299                 break;
300
301         case 2:
302                 if ((iter = _model->get_iter (path))) {
303                         bool gain = (*iter)[_columns.gain];
304                         (*iter)[_columns.gain] = !gain;
305                         ret = true;
306                 }
307                 break;
308
309         case 3:
310                 if ((iter = _model->get_iter (path))) {
311                         bool gain_relative = (*iter)[_columns.gain_relative];
312                         (*iter)[_columns.gain_relative] = !gain_relative;
313                         ret = true;
314                 }
315                 break;
316
317         case 4:
318                 if ((iter = _model->get_iter (path))) {
319                         bool mute = (*iter)[_columns.mute];
320                         (*iter)[_columns.mute] = !mute;
321                         ret = true;
322                 }
323                 break;
324
325         case 5:
326                 if ((iter = _model->get_iter (path))) {
327                         bool solo = (*iter)[_columns.solo];
328                         (*iter)[_columns.solo] = !solo;
329                         ret = true;
330                 }
331                 break;
332
333         case 6:
334                 if ((iter = _model->get_iter (path))) {
335                         bool record = (*iter)[_columns.record];
336                         (*iter)[_columns.record] = !record;
337                         ret = true;
338                 }
339                 break;
340
341         case 7:
342                 if ((iter = _model->get_iter (path))) {
343                         bool monitoring = (*iter)[_columns.monitoring];
344                         (*iter)[_columns.monitoring] = !monitoring;
345                         ret = true;
346                 }
347                 break;
348
349         case 8:
350                 if ((iter = _model->get_iter (path))) {
351                         bool select = (*iter)[_columns.select];
352                         (*iter)[_columns.select] = !select;
353                         ret = true;
354                 }
355                 break;
356
357         case 9:
358                 if ((iter = _model->get_iter (path))) {
359                         bool edits = (*iter)[_columns.edits];
360                         (*iter)[_columns.edits] = !edits;
361                         ret = true;
362                 }
363                 break;
364
365         case 10:
366                 if ((iter = _model->get_iter (path))) {
367                         bool active_state = (*iter)[_columns.active_state];
368                         (*iter)[_columns.active_state] = !active_state;
369                         ret = true;
370                 }
371                 break;
372
373         case 11:
374                 if ((iter = _model->get_iter (path))) {
375                         bool is_visible = (*iter)[_columns.is_visible];
376                         (*iter)[_columns.is_visible] = !is_visible;
377                         ret = true;
378                 }
379                 break;
380
381                 
382         default:
383                 break;
384         }
385
386 #ifdef GTKOSX
387         if (ret) {
388                 _display.queue_draw();
389         }
390 #endif
391
392         return ret;
393 }
394
395 void
396 EditorRouteGroups::row_change (const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator& iter)
397 {
398         RouteGroup* group;
399
400         if (_in_row_change) {
401                 return;
402         }
403
404         if ((group = (*iter)[_columns.routegroup]) == 0) {
405                 return;
406         }
407
408         PropertyList plist;
409         plist.add (Properties::name, string ((*iter)[_columns.text]));
410         
411         bool val = (*iter)[_columns.gain];
412         plist.add (Properties::gain, val);
413         val = (*iter)[_columns.gain_relative];
414         plist.add (Properties::relative, val);
415         val = (*iter)[_columns.mute];
416         plist.add (Properties::mute, val);
417         val = (*iter)[_columns.solo];
418         plist.add (Properties::solo, val);
419         val = (*iter)[_columns.record];
420         plist.add (Properties::recenable, val);
421         val = (*iter)[_columns.monitoring];
422         plist.add (Properties::monitoring, val);
423         val = (*iter)[_columns.select];
424         plist.add (Properties::select, val);
425         val = (*iter)[_columns.edits];
426         plist.add (Properties::edit, val);
427         val = (*iter)[_columns.active_state];
428         plist.add (Properties::route_active, val);
429
430         GroupTabs::set_group_color ((*iter)[_columns.routegroup], (*iter)[_columns.gdkcolor]);
431         
432         group->set_hidden (!(*iter)[_columns.is_visible], this);
433
434         /* XXX set color here */
435
436         group->apply_changes (plist);
437 }
438
439 void
440 EditorRouteGroups::add (RouteGroup* group)
441 {
442         ENSURE_GUI_THREAD (*this, &EditorRouteGroups::add, group)
443         bool focus = false;
444
445         TreeModel::Row row = *(_model->append());
446
447         row[_columns.gain] = group->is_gain ();
448         row[_columns.gain_relative] = group->is_relative ();
449         row[_columns.mute] = group->is_mute ();
450         row[_columns.solo] = group->is_solo ();
451         row[_columns.record] = group->is_recenable();
452         row[_columns.monitoring] = group->is_monitoring();
453         row[_columns.select] = group->is_select ();
454         row[_columns.edits] = group->is_edit ();
455         row[_columns.active_state] = group->is_route_active ();
456         row[_columns.is_visible] = !group->is_hidden();
457         row[_columns.gdkcolor] = GroupTabs::group_color (group);
458         
459         _in_row_change = true;
460
461         row[_columns.routegroup] = group;
462
463         if (!group->name().empty()) {
464                 row[_columns.text] = group->name();
465         } else {
466                 row[_columns.text] = _("unnamed");
467                 focus = true;
468         }
469
470         group->PropertyChanged.connect (_property_changed_connections, MISSING_INVALIDATOR, ui_bind (&EditorRouteGroups::property_changed, this, group, _1), gui_context());
471
472         if (focus) {
473                 TreeViewColumn* col = _display.get_column (0);
474                 CellRendererText* name_cell = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (0));
475                 _display.set_cursor (_model->get_path (row), *col, *name_cell, true);
476         }
477
478         _in_row_change = false;
479
480         _editor->_group_tabs->set_dirty ();
481 }
482
483 void
484 EditorRouteGroups::groups_changed ()
485 {
486         ENSURE_GUI_THREAD (*this, &EditorRouteGroups::groups_changed);
487
488         _in_rebuild = true;
489
490         /* just rebuild the while thing */
491
492         _model->clear ();
493
494         if (_session) {
495                 _session->foreach_route_group (sigc::mem_fun (*this, &EditorRouteGroups::add));
496         }
497
498         _in_rebuild = false;
499 }
500
501 void
502 EditorRouteGroups::property_changed (RouteGroup* group, const PropertyChange& change)
503 {
504         _in_row_change = true;
505
506         Gtk::TreeModel::Children children = _model->children();
507
508         for(Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
509                 if (group == (*iter)[_columns.routegroup]) {
510                         (*iter)[_columns.text] = group->name();
511                         (*iter)[_columns.gain] = group->is_gain ();
512                         (*iter)[_columns.gain_relative] = group->is_relative ();
513                         (*iter)[_columns.mute] = group->is_mute ();
514                         (*iter)[_columns.solo] = group->is_solo ();
515                         (*iter)[_columns.record] = group->is_recenable ();
516                         (*iter)[_columns.monitoring] = group->is_monitoring ();
517                         (*iter)[_columns.select] = group->is_select ();
518                         (*iter)[_columns.edits] = group->is_edit ();
519                         (*iter)[_columns.active_state] = group->is_route_active ();
520                         (*iter)[_columns.is_visible] = !group->is_hidden();
521                         (*iter)[_columns.gdkcolor] = GroupTabs::group_color (group);
522                 }
523         }
524
525         _in_row_change = false;
526
527         if (change.contains (Properties::name) || change.contains (Properties::color) || change.contains (Properties::active)) {
528                 _editor->_group_tabs->set_dirty ();
529         }
530
531         for (TrackViewList::const_iterator i = _editor->get_track_views().begin(); i != _editor->get_track_views().end(); ++i) {
532                 if ((*i)->route_group() == group) {
533                         if (group->is_hidden ()) {
534                                 _editor->hide_track_in_display (*i);
535                         } else {
536                                 _editor->_routes->show_track_in_display (**i);
537                         }
538                 }
539         }
540 }
541
542 void
543 EditorRouteGroups::name_edit (const std::string& path, const std::string& new_text)
544 {
545         RouteGroup* group;
546         TreeIter iter;
547
548         if ((iter = _model->get_iter (path))) {
549
550                 if ((group = (*iter)[_columns.routegroup]) == 0) {
551                         return;
552                 }
553
554                 if (new_text != group->name()) {
555                         group->set_name (new_text);
556                 }
557         }
558 }
559
560 void
561 EditorRouteGroups::clear ()
562 {
563         _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
564         _model->clear ();
565         _display.set_model (_model);
566 }
567
568 void
569 EditorRouteGroups::set_session (Session* s)
570 {
571         SessionHandlePtr::set_session (s);
572
573         if (_session) {
574
575                 RouteGroup& arg (_session->all_route_group());
576
577                 arg.PropertyChanged.connect (all_route_groups_changed_connection, MISSING_INVALIDATOR, ui_bind (&EditorRouteGroups::all_group_changed, this, _1), gui_context());
578
579                 _session->route_group_added.connect (_session_connections, MISSING_INVALIDATOR, ui_bind (&EditorRouteGroups::add, this, _1), gui_context());
580                 _session->route_group_removed.connect (
581                         _session_connections, MISSING_INVALIDATOR, boost::bind (&EditorRouteGroups::groups_changed, this), gui_context()
582                         );
583                 _session->route_groups_reordered.connect (
584                         _session_connections, MISSING_INVALIDATOR, boost::bind (&EditorRouteGroups::groups_changed, this), gui_context()
585                         );
586         }
587
588         PBD::PropertyChange pc;
589         pc.add (Properties::select);
590         pc.add (Properties::active);
591         all_group_changed (pc);
592
593         groups_changed ();
594 }
595
596 void
597 EditorRouteGroups::run_new_group_dialog ()
598 {
599         RouteList rl;
600
601         return _editor->_group_tabs->run_new_group_dialog (rl);
602 }
603
604 void
605 EditorRouteGroups::all_group_toggled ()
606 {
607         if (_session) {
608                 _session->all_route_group().set_select (_all_group_active_button.get_active());
609         }
610 }
611
612 void
613 EditorRouteGroups::all_group_changed (const PropertyChange&)
614 {
615         if (_session) {
616                 RouteGroup& arg (_session->all_route_group());
617                 _all_group_active_button.set_active (arg.is_active() && arg.is_select());
618         } else {
619                 _all_group_active_button.set_active (false);
620         }
621 }
622
623 /** Called when a model row is deleted, but also when the model is
624  *  reordered by a user drag-and-drop; the latter is what we are
625  *  interested in here.
626  */
627 void
628 EditorRouteGroups::row_deleted (Gtk::TreeModel::Path const &)
629 {
630         if (_in_rebuild) {
631                 /* We need to ignore this in cases where we're not doing a drag-and-drop
632                    re-order.
633                 */
634                 return;
635         }
636
637         /* Re-write the session's route group list so that the new order is preserved */
638
639         list<RouteGroup*> new_list;
640
641         Gtk::TreeModel::Children children = _model->children();
642         for (Gtk::TreeModel::Children::iterator i = children.begin(); i != children.end(); ++i) {
643                 new_list.push_back ((*i)[_columns.routegroup]);
644         }
645
646         _session->reorder_route_groups (new_list);
647 }
648
649