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