Fix DSP load sorting with inactive plugins
[ardour.git] / gtk2_ardour / editor_route_groups.cc
1 /*
2  * Copyright (C) 2009-2011 David Robillard <d@drobilla.net>
3  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
4  * Copyright (C) 2009-2016 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef WAF_BUILD
23 #include "gtk2ardour-config.h"
24 #endif
25
26 #include <cstdlib>
27 #include <cmath>
28
29 #include "fix_carbon.h"
30
31 #include <gtkmm/stock.h>
32
33 #include "gtkmm2ext/gtk_ui.h"
34 #include "gtkmm2ext/cell_renderer_color_selector.h"
35
36 #include "widgets/tooltips.h"
37
38 #include "ardour/route_group.h"
39 #include "ardour/route.h"
40 #include "ardour/session.h"
41
42 #include "ardour_ui.h"
43 #include "editor.h"
44 #include "editor_group_tabs.h"
45 #include "editor_route_groups.h"
46 #include "editor_routes.h"
47 #include "gui_thread.h"
48 #include "keyboard.h"
49 #include "marker.h"
50 #include "route_group_dialog.h"
51 #include "route_time_axis.h"
52 #include "time_axis_view.h"
53 #include "utils.h"
54
55 #include "pbd/i18n.h"
56
57 using namespace std;
58 using namespace ARDOUR;
59 using namespace ARDOUR_UI_UTILS;
60 using namespace ArdourWidgets;
61 using namespace PBD;
62 using namespace Gtk;
63 using Gtkmm2ext::Keyboard;
64
65 struct ColumnInfo {
66         int         index;
67         const char* label;
68         const char* tooltip;
69 };
70
71 EditorRouteGroups::EditorRouteGroups (Editor* e)
72         : EditorComponent (e)
73         , _in_row_change (false)
74         , _in_rebuild (false)
75 {
76         _model = ListStore::create (_columns);
77         _display.set_model (_model);
78
79         Gtkmm2ext::CellRendererColorSelector* color_renderer = manage (new Gtkmm2ext::CellRendererColorSelector);
80         TreeViewColumn* color_column = manage (new TreeViewColumn ("", *color_renderer));
81
82         color_column->add_attribute (color_renderer->property_color(), _columns.gdkcolor);
83
84         _display.append_column (*color_column);
85
86         _display.append_column ("", _columns.text);
87         _display.append_column ("", _columns.is_visible);
88         _display.append_column ("", _columns.active_state);
89         _display.append_column ("", _columns.gain);
90         _display.append_column ("", _columns.gain_relative);
91         _display.append_column ("", _columns.mute);
92         _display.append_column ("", _columns.solo);
93         _display.append_column ("", _columns.record);
94         _display.append_column ("", _columns.monitoring);
95         _display.append_column ("", _columns.select);
96         _display.append_column ("", _columns.active_shared);
97
98         TreeViewColumn* col;
99         Gtk::Label* l;
100
101         ColumnInfo ci[] = {
102                 { 0,   _("Col"),            _("Group Tab Color") },
103                 { 1,   _("Name"),           _("Name of Group") },
104                 { 2,  S_("Visible|V"),      _("Group is visible?") },
105                 { 3,   _("On"),             _("Group is enabled?") },
106                 { 4,  S_("Group|G"),        _("Sharing Gain?") },
107                 { 5,  S_("Relative|Rel"),   _("Relative Gain Changes?") },
108                 { 6,  S_("Mute|M"),         _("Sharing Mute?") },
109                 { 7,  S_("Solo|S"),         _("Sharing Solo?") },
110                 { 8,   _("Rec"),            _("Sharing Record-enable Status?") },
111                 { 9,  S_("Monitoring|Mon"), _("Sharing Monitoring Choice?") },
112                 { 10, S_("Selection|Sel"),  _("Sharing Selected/Editing Status?") },
113                 { 11, S_("Active|A"),       _("Sharing Active Status?") },
114                 { -1, 0, 0 }
115         };
116
117
118         for (int i = 0; ci[i].index >= 0; ++i) {
119                 col = _display.get_column (ci[i].index);
120                 l = manage (new Label (ci[i].label));
121                 set_tooltip (*l, ci[i].tooltip);
122                 col->set_widget (*l);
123                 l->show ();
124
125                 col->set_data (X_("colnum"), GUINT_TO_POINTER(i));
126                 if (i == 1) {
127                         col->set_expand (true);
128                 } else {
129                         col->set_expand (false);
130                         col->set_alignment (ALIGN_CENTER);
131                 }
132         }
133
134         _display.set_headers_visible (true);
135
136         color_dialog.get_colorsel()->set_has_opacity_control (false);
137         color_dialog.get_colorsel()->set_has_palette (true);
138         color_dialog.get_ok_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
139         color_dialog.get_cancel_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
140
141         /* name is directly editable */
142
143         CellRendererText* name_cell = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (1));
144         name_cell->property_editable() = true;
145         name_cell->signal_edited().connect (sigc::mem_fun (*this, &EditorRouteGroups::name_edit));
146
147         for (int i = 1; ci[i].index >= 0; ++i) {
148                 CellRendererToggle* active_cell = dynamic_cast <CellRendererToggle*> (_display.get_column_cell_renderer (i));
149
150                 if (active_cell) {
151                         active_cell->property_activatable() = true;
152                         active_cell->property_radio() = false;
153                 }
154         }
155
156         _model->signal_row_changed().connect (sigc::mem_fun (*this, &EditorRouteGroups::row_change));
157         /* What signal would you guess was emitted when the rows of your treeview are reordered
158            by a drag and drop?  signal_rows_reordered?  That would be far too easy.
159            No, signal_row_deleted().
160          */
161         _model->signal_row_deleted().connect (sigc::mem_fun (*this, &EditorRouteGroups::row_deleted));
162
163         _display.set_name ("EditGroupList");
164         _display.get_selection()->set_mode (SELECTION_SINGLE);
165         _display.set_headers_visible (true);
166         _display.set_reorderable (false);
167         _display.set_rules_hint (true);
168
169         _scroller.add (_display);
170         _scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
171
172         _display.signal_button_press_event().connect (sigc::mem_fun(*this, &EditorRouteGroups::button_press_event), false);
173
174         HBox* button_box = manage (new HBox());
175         button_box->set_homogeneous (true);
176
177         Button* add_button = manage (new Button ());
178         Button* remove_button = manage (new Button ());
179
180         Widget* w;
181
182         w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
183         w->show();
184         add_button->add (*w);
185
186         w = manage (new Image (Stock::REMOVE, ICON_SIZE_BUTTON));
187         w->show();
188         remove_button->add (*w);
189
190         add_button->signal_clicked().connect (sigc::hide_return (sigc::mem_fun (*this, &EditorRouteGroups::run_new_group_dialog)));
191         remove_button->signal_clicked().connect (sigc::mem_fun (*this, &EditorRouteGroups::remove_selected));
192
193         button_box->pack_start (*add_button);
194         button_box->pack_start (*remove_button);
195
196         _display_packer.pack_start (_scroller, true, true);
197         _display_packer.pack_start (*button_box, false, false);
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() || _session->deletion_in_progress()) {
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                 color_dialog.get_colorsel()->set_previous_color (c);
273                 color_dialog.get_colorsel()->set_current_color (c);
274
275                 switch (color_dialog.run()) {
276                 case RESPONSE_CANCEL:
277                         break;
278                 case RESPONSE_ACCEPT:
279                         c = color_dialog.get_colorsel()->get_current_color();
280                         GroupTabs::set_group_color (group, gdk_color_to_rgba (c));
281                         break;
282
283                 default:
284                         break;
285
286                 }
287
288                 color_dialog.hide ();
289                 ret = true;
290                 break;
291
292         case 1:
293                 if (Keyboard::is_edit_event (ev) && group) {
294                         /* we'll be editing now ... */
295                         ret = true;
296                 }
297                 break;
298
299         case 2:
300                 val = (*iter)[_columns.is_visible];
301                 /* note subtle logic inverse here: we set the new value with
302                    "val", rather than !val, because we're using ::set_hidden()
303                    not a (non-existent) ::set_visible() call.
304                 */
305                 group->set_hidden (val, this);
306                 ret = true;
307                 break;
308
309
310         case 3:
311                 val = (*iter)[_columns.active_state];
312                 group->set_active (!val, this);
313                 ret = true;
314                 break;
315
316         case 4:
317                 val = (*iter)[_columns.gain];
318                 group->set_gain (!val);
319                 ret = true;
320                 break;
321
322         case 5:
323                 val = (*iter)[_columns.gain_relative];
324                 group->set_relative (!val, this);
325                 ret = true;
326                 break;
327
328         case 6:
329                 val = (*iter)[_columns.mute];
330                 group->set_mute (!val);
331                 ret = true;
332                 break;
333
334         case 7:
335                 val = (*iter)[_columns.solo];
336                 group->set_solo (!val);
337                 ret = true;
338                 break;
339
340         case 8:
341                 val = (*iter)[_columns.record];
342                 group->set_recenable (!val);
343                 ret = true;
344                 break;
345
346         case 9:
347                 val = (*iter)[_columns.monitoring];
348                 group->set_monitoring (!val);
349                 ret = true;
350                 break;
351
352         case 10:
353                 val = (*iter)[_columns.select];
354                 group->set_select (!val);
355                 ret = true;
356                 break;
357
358         case 11:
359                 val = (*iter)[_columns.active_shared];
360                 group->set_route_active (!val);
361                 ret = true;
362                 break;
363
364         default:
365                 break;
366         }
367
368         return ret;
369 }
370
371 void
372 EditorRouteGroups::row_change (const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator& iter)
373 {
374         RouteGroup* group;
375
376         if (_in_row_change) {
377                 return;
378         }
379
380         if ((group = (*iter)[_columns.routegroup]) == 0) {
381                 return;
382         }
383
384         PropertyList plist;
385         plist.add (Properties::name, string ((*iter)[_columns.text]));
386
387         bool val = (*iter)[_columns.gain];
388         plist.add (Properties::group_gain, val);
389         val = (*iter)[_columns.gain_relative];
390         plist.add (Properties::group_relative, val);
391         val = (*iter)[_columns.mute];
392         plist.add (Properties::group_mute, val);
393         val = (*iter)[_columns.solo];
394         plist.add (Properties::group_solo, val);
395         val = (*iter)[_columns.record];
396         plist.add (Properties::group_recenable, val);
397         val = (*iter)[_columns.monitoring];
398         plist.add (Properties::group_monitoring, val);
399         val = (*iter)[_columns.select];
400         plist.add (Properties::group_select, val);
401         val = (*iter)[_columns.active_shared];
402         plist.add (Properties::group_route_active, val);
403
404         val = (*iter)[_columns.active_state];
405         plist.add (Properties::active, val);
406         val = (*iter)[_columns.is_visible];
407         plist.add (Properties::hidden, !val);
408
409         group->apply_changes (plist);
410
411         GroupTabs::set_group_color ((*iter)[_columns.routegroup], gdk_color_to_rgba ((*iter)[_columns.gdkcolor]));
412 }
413
414 void
415 EditorRouteGroups::add (RouteGroup* group)
416 {
417         ENSURE_GUI_THREAD (*this, &EditorRouteGroups::add, group)
418         bool focus = false;
419
420         TreeModel::Row row = *(_model->append());
421
422         row[_columns.gain] = group->is_gain ();
423         row[_columns.gain_relative] = group->is_relative ();
424         row[_columns.mute] = group->is_mute ();
425         row[_columns.solo] = group->is_solo ();
426         row[_columns.record] = group->is_recenable();
427         row[_columns.monitoring] = group->is_monitoring();
428         row[_columns.select] = group->is_select ();
429         row[_columns.active_shared] = group->is_route_active ();
430         row[_columns.active_state] = group->is_active ();
431         row[_columns.is_visible] = !group->is_hidden();
432
433         Gdk::Color c;
434         set_color_from_rgba (c, GroupTabs::group_color (group));
435         row[_columns.gdkcolor] = c;
436
437         _in_row_change = true;
438
439         row[_columns.routegroup] = group;
440
441         if (!group->name().empty()) {
442                 row[_columns.text] = group->name();
443         } else {
444                 row[_columns.text] = _("unnamed");
445                 focus = true;
446         }
447
448         group->PropertyChanged.connect (_property_changed_connections, MISSING_INVALIDATOR, boost::bind (&EditorRouteGroups::property_changed, this, group, _1), gui_context());
449
450         if (focus) {
451                 TreeViewColumn* col = _display.get_column (0);
452                 CellRendererText* name_cell = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (1));
453                 _display.set_cursor (_model->get_path (row), *col, *name_cell, true);
454         }
455
456         _in_row_change = false;
457
458         _editor->_group_tabs->set_dirty ();
459 }
460
461 void
462 EditorRouteGroups::groups_changed ()
463 {
464         ENSURE_GUI_THREAD (*this, &EditorRouteGroups::groups_changed);
465
466         _in_rebuild = true;
467
468         /* just rebuild the while thing */
469
470         _model->clear ();
471
472         if (_session) {
473                 _session->foreach_route_group (sigc::mem_fun (*this, &EditorRouteGroups::add));
474         }
475
476         _in_rebuild = false;
477 }
478
479 void
480 EditorRouteGroups::property_changed (RouteGroup* group, const PropertyChange&)
481 {
482         assert(group);
483         _in_row_change = true;
484
485         Gtk::TreeModel::Children children = _model->children();
486
487         for(Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
488                 if (group == (*iter)[_columns.routegroup]) {
489
490                         /* we could check the PropertyChange and only set
491                          * appropriate fields. but the amount of saved by doing
492                          * that is pretty minimal, and this is nice and simple.
493                          */
494
495                         (*iter)[_columns.text] = group->name();
496                         (*iter)[_columns.gain] = group->is_gain ();
497                         (*iter)[_columns.gain_relative] = group->is_relative ();
498                         (*iter)[_columns.mute] = group->is_mute ();
499                         (*iter)[_columns.solo] = group->is_solo ();
500                         (*iter)[_columns.record] = group->is_recenable ();
501                         (*iter)[_columns.monitoring] = group->is_monitoring ();
502                         (*iter)[_columns.select] = group->is_select ();
503                         (*iter)[_columns.active_shared] = group->is_route_active ();
504                         (*iter)[_columns.active_state] = group->is_active ();
505                         (*iter)[_columns.is_visible] = !group->is_hidden();
506
507                         Gdk::Color c;
508                         set_color_from_rgba (c, GroupTabs::group_color (group));
509                         (*iter)[_columns.gdkcolor] = c;
510
511                         break;
512                 }
513         }
514
515         _in_row_change = false;
516
517         for (TrackViewList::const_iterator i = _editor->get_track_views().begin(); i != _editor->get_track_views().end(); ++i) {
518                 if ((*i)->route_group() == group) {
519                         if (group->is_hidden ()) {
520                                 _editor->hide_track_in_display (*i);
521                         } else {
522                                 _editor->_routes->show_track_in_display (**i);
523                         }
524                 }
525         }
526 }
527
528 void
529 EditorRouteGroups::name_edit (const std::string& path, const std::string& new_text)
530 {
531         RouteGroup* group;
532         TreeIter iter;
533
534         if ((iter = _model->get_iter (path))) {
535
536                 if ((group = (*iter)[_columns.routegroup]) == 0) {
537                         return;
538                 }
539
540                 if (new_text != group->name()) {
541                         group->set_name (new_text);
542                 }
543         }
544 }
545
546 void
547 EditorRouteGroups::clear ()
548 {
549         _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
550         _model->clear ();
551         _display.set_model (_model);
552 }
553
554 void
555 EditorRouteGroups::set_session (Session* s)
556 {
557         SessionHandlePtr::set_session (s);
558
559         if (_session) {
560
561                 _session->route_group_added.connect (_session_connections, MISSING_INVALIDATOR, boost::bind (&EditorRouteGroups::add, this, _1), gui_context());
562                 _session->route_group_removed.connect (
563                         _session_connections, MISSING_INVALIDATOR, boost::bind (&EditorRouteGroups::groups_changed, this), gui_context()
564                         );
565                 _session->route_groups_reordered.connect (
566                         _session_connections, MISSING_INVALIDATOR, boost::bind (&EditorRouteGroups::groups_changed, this), gui_context()
567                         );
568         }
569
570         PBD::PropertyChange pc;
571         pc.add (Properties::group_select);
572         pc.add (Properties::active);
573
574         groups_changed ();
575 }
576
577 void
578 EditorRouteGroups::run_new_group_dialog ()
579 {
580         return _editor->_group_tabs->run_new_group_dialog (0, false);
581 }
582
583 /** Called when a model row is deleted, but also when the model is
584  *  reordered by a user drag-and-drop; the latter is what we are
585  *  interested in here.
586  */
587 void
588 EditorRouteGroups::row_deleted (Gtk::TreeModel::Path const &)
589 {
590         if (_in_rebuild || !_session || _session->deletion_in_progress()) {
591                 /* We need to ignore this in cases where we're not doing a drag-and-drop
592                    re-order.
593                 */
594                 return;
595         }
596
597         /* Re-write the session's route group list so that the new order is preserved */
598
599         list<RouteGroup*> new_list;
600
601         Gtk::TreeModel::Children children = _model->children();
602         for (Gtk::TreeModel::Children::iterator i = children.begin(); i != children.end(); ++i) {
603                 new_list.push_back ((*i)[_columns.routegroup]);
604         }
605
606         _session->reorder_route_groups (new_list);
607 }
608
609