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