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