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