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