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