stupid hack to fix issues with OS X Carbon headers
[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
34 #include "editor.h"
35 #include "keyboard.h"
36 #include "marker.h"
37 #include "time_axis_view.h"
38 #include "prompter.h"
39 #include "gui_thread.h"
40 #include "editor_group_tabs.h"
41 #include "route_group_dialog.h"
42 #include "route_time_axis.h"
43 #include "editor_routes.h"
44 #include "editor_route_groups.h"
45 #include "ardour_ui.h"
46
47 #include "ardour/route.h"
48 #include "ardour/session.h"
49
50 #include "i18n.h"
51
52 using namespace std;
53 using namespace ARDOUR;
54 using namespace PBD;
55 using namespace Gtk;
56 using Gtkmm2ext::Keyboard;
57
58 struct ColumnInfo {
59     int         index;
60     const char* label;
61     const char* tooltip;
62 };
63
64 EditorRouteGroups::EditorRouteGroups (Editor* e)
65         : EditorComponent (e)
66         , _all_group_active_button (_("No Selection = All Tracks?"))
67         , _in_row_change (false)
68         , _in_rebuild (false)
69 {
70         _model = ListStore::create (_columns);
71         _display.set_model (_model);
72
73         Gtkmm2ext::CellRendererColorSelector* color_renderer = manage (new Gtkmm2ext::CellRendererColorSelector);
74         TreeViewColumn* color_column = manage (new TreeViewColumn ("", *color_renderer));
75         color_column->add_attribute (color_renderer->property_color(), _columns.gdkcolor);
76         
77         _display.append_column (*color_column);
78
79         _display.append_column ("", _columns.text);
80         _display.append_column ("", _columns.is_visible);
81         _display.append_column ("", _columns.active_state);
82         _display.append_column ("", _columns.gain);
83         _display.append_column ("", _columns.gain_relative);
84         _display.append_column ("", _columns.mute);
85         _display.append_column ("", _columns.solo);
86         _display.append_column ("", _columns.record);
87         _display.append_column ("", _columns.monitoring);
88         _display.append_column ("", _columns.select);
89         _display.append_column ("", _columns.edits);
90         _display.append_column ("", _columns.active_shared);
91
92         TreeViewColumn* col;
93         Gtk::Label* l;
94
95         ColumnInfo ci[] = {
96                 { 0, _("Col"), _("Group Tab Color") },
97                 { 1, _("Name"), _("Name of Group") },
98                 { 2, _("V"), _("Group is visible?") },
99                 { 3, _("On"), _("Group is enabled?") },
100                 { 4, S_("group|G"), _("Sharing Gain?") },
101                 { 5, S_("relative|Rel"), _("Relevative Gain Changes?") },
102                 { 6, S_("mute|M"), _("Sharing Mute?") },
103                 { 7, S_("solo|S"), _("Sharing Solo?") },
104                 { 8, _("Rec"), _("Sharing Record-enable Status?") },
105                 { 9, S_("monitoring|Mon"), _("Sharing Monitoring Choice?") },
106                 { 10, S_("selection|Sel"), _("Sharing Selected Status?") },
107                 { 11, S_("editing|E"), _("Sharing Editing?") },
108                 { 12, 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                 ARDOUR_UI::instance()->set_tip (*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         _all_group_active_button.show ();
192
193         _display_packer.pack_start (_scroller, true, true);
194         _display_packer.pack_start (_all_group_active_button, false, false);
195         _display_packer.pack_start (*button_box, false, false);
196
197         _all_group_active_button.signal_toggled().connect (sigc::mem_fun (*this, &EditorRouteGroups::all_group_toggled));
198         _all_group_active_button.set_name (X_("EditorRouteGroupsAllGroupButton"));
199         ARDOUR_UI::instance()->set_tip (_all_group_active_button, _("Activate this button to operate on all tracks when none are selected."));
200 }
201
202 void
203 EditorRouteGroups::remove_selected ()
204 {
205         Glib::RefPtr<TreeSelection> selection = _display.get_selection();
206         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
207
208         if (rows.empty()) {
209                 return;
210         }
211
212         TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
213         TreeIter iter;
214
215         /* selection mode is single, so rows.begin() is it */
216
217         if ((iter = _model->get_iter (*i))) {
218
219                 RouteGroup* rg = (*iter)[_columns.routegroup];
220
221                 if (rg) {
222                         _session->remove_route_group (*rg);
223                 }
224         }
225 }
226
227 void
228 EditorRouteGroups::button_clicked ()
229 {
230         run_new_group_dialog ();
231 }
232
233 bool
234 EditorRouteGroups::button_press_event (GdkEventButton* ev)
235 {
236         TreeModel::Path path;
237         TreeIter iter;
238         RouteGroup* group = 0;
239         TreeViewColumn* column;
240         int cellx;
241         int celly;
242         bool ret = false;
243         Gdk::Color c;
244         bool val;
245
246         bool const p = _display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly);
247
248         if (p) {
249                 iter = _model->get_iter (path);
250         }
251
252         if (iter) {
253                 group = (*iter)[_columns.routegroup];
254         } 
255
256         if (Keyboard::is_context_menu_event (ev)) {
257                 _editor->_group_tabs->get_menu(group)->popup (1, ev->time);
258                 return true;
259         } 
260
261         if (!p) {
262                 /* cancel selection */
263                 _display.get_selection()->unselect_all ();
264                 /* end any editing by grabbing focus */
265                 _display.grab_focus ();
266                 return true;
267         }
268
269         group = (*iter)[_columns.routegroup];
270
271         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
272         case 0: 
273                 c = (*iter)[_columns.gdkcolor];
274
275                 color_dialog.get_colorsel()->set_previous_color (c);
276                 color_dialog.get_colorsel()->set_current_color (c);
277
278                 switch (color_dialog.run()) {
279                 case RESPONSE_CANCEL:
280                         break;
281                 case RESPONSE_ACCEPT:
282                         c = color_dialog.get_colorsel()->get_current_color();
283                         GroupTabs::set_group_color (group, c);
284                         ARDOUR_UI::config()->set_dirty ();
285                         break;
286                         
287                 default:
288                         break;
289                         
290                 }
291
292                 color_dialog.hide ();
293                 ret = true;
294                 break;
295
296         case 1:
297                 if (Keyboard::is_edit_event (ev) && group) {
298                         /* we'll be editing now ... */
299                         ret = true;
300                 }
301                 break;
302
303         case 2:
304                 val = (*iter)[_columns.is_visible];
305                 /* note subtle logic inverse here: we set the new value with
306                    "val", rather than !val, because we're using ::set_hidden()
307                    not a (non-existent) ::set_visible() call.
308                 */
309                 group->set_hidden (val, this);
310                 ret = true;
311                 break;
312
313                 
314         case 3:
315                 val = (*iter)[_columns.active_state];
316                 group->set_active (!val, this);
317                 ret = true;
318                 break;
319
320         case 4:
321                 val = (*iter)[_columns.gain];
322                 group->set_gain (!val);
323                 ret = true;
324                 break;
325
326         case 5:
327                 val = (*iter)[_columns.gain_relative];
328                 group->set_relative (!val, this);
329                 ret = true;
330                 break;
331
332         case 6:
333                 val = (*iter)[_columns.mute];
334                 group->set_mute (!val);
335                 ret = true;
336                 break;
337
338         case 7:
339                 val = (*iter)[_columns.solo];
340                 group->set_solo (!val);
341                 ret = true;
342                 break;
343
344         case 8:
345                 val = (*iter)[_columns.record];
346                 group->set_recenable (!val);
347                 ret = true;
348                 break;
349
350         case 9:
351                 val = (*iter)[_columns.monitoring];
352                 group->set_monitoring (!val);
353                 ret = true;
354                 break;
355
356         case 10:
357                 val = (*iter)[_columns.select];
358                 group->set_select (!val);
359                 ret = true;
360                 break;
361
362         case 11:
363                 val = (*iter)[_columns.edits];
364                 group->set_edit (!val);
365                 ret = true;
366                 break;
367
368         case 12:
369                 val = (*iter)[_columns.active_shared];
370                 cerr << "set group active to " << !val << endl;
371                 group->set_route_active (!val);
372                 ret = true;
373                 break;
374
375         default:
376                 break;
377         }
378
379         return ret;
380 }
381
382 void
383 EditorRouteGroups::row_change (const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator& iter)
384 {
385         RouteGroup* group;
386
387         if (_in_row_change) {
388                 return;
389         }
390
391         if ((group = (*iter)[_columns.routegroup]) == 0) {
392                 return;
393         }
394
395         PropertyList plist;
396         plist.add (Properties::name, string ((*iter)[_columns.text]));
397
398         bool val = (*iter)[_columns.gain];
399         plist.add (Properties::gain, val);
400         val = (*iter)[_columns.gain_relative];
401         plist.add (Properties::relative, val);
402         val = (*iter)[_columns.mute];
403         plist.add (Properties::mute, val);
404         val = (*iter)[_columns.solo];
405         plist.add (Properties::solo, val);
406         val = (*iter)[_columns.record];
407         plist.add (Properties::recenable, val);
408         val = (*iter)[_columns.monitoring];
409         plist.add (Properties::monitoring, val);
410         val = (*iter)[_columns.select];
411         plist.add (Properties::select, val);
412         val = (*iter)[_columns.edits];
413         plist.add (Properties::edit, val);
414         val = (*iter)[_columns.active_shared];
415         plist.add (Properties::route_active, val);
416         val = (*iter)[_columns.active_state];
417         plist.add (Properties::active, val);
418         val = (*iter)[_columns.is_visible];
419         plist.add (Properties::hidden, !val);
420
421         group->apply_changes (plist);
422
423         GroupTabs::set_group_color ((*iter)[_columns.routegroup], (*iter)[_columns.gdkcolor]);
424 }
425
426 void
427 EditorRouteGroups::add (RouteGroup* group)
428 {
429         ENSURE_GUI_THREAD (*this, &EditorRouteGroups::add, group)
430         bool focus = false;
431
432         TreeModel::Row row = *(_model->append());
433
434         row[_columns.gain] = group->is_gain ();
435         row[_columns.gain_relative] = group->is_relative ();
436         row[_columns.mute] = group->is_mute ();
437         row[_columns.solo] = group->is_solo ();
438         row[_columns.record] = group->is_recenable();
439         row[_columns.monitoring] = group->is_monitoring();
440         row[_columns.select] = group->is_select ();
441         row[_columns.edits] = group->is_edit ();
442         row[_columns.active_shared] = group->is_route_active ();
443         row[_columns.active_state] = group->is_active ();
444         row[_columns.is_visible] = !group->is_hidden();
445         row[_columns.gdkcolor] = GroupTabs::group_color (group);
446         
447         _in_row_change = true;
448
449         row[_columns.routegroup] = group;
450
451         if (!group->name().empty()) {
452                 row[_columns.text] = group->name();
453         } else {
454                 row[_columns.text] = _("unnamed");
455                 focus = true;
456         }
457
458         group->PropertyChanged.connect (_property_changed_connections, MISSING_INVALIDATOR, ui_bind (&EditorRouteGroups::property_changed, this, group, _1), gui_context());
459
460         if (focus) {
461                 TreeViewColumn* col = _display.get_column (0);
462                 CellRendererText* name_cell = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (1));
463                 _display.set_cursor (_model->get_path (row), *col, *name_cell, true);
464         }
465
466         _in_row_change = false;
467
468         _editor->_group_tabs->set_dirty ();
469 }
470
471 void
472 EditorRouteGroups::groups_changed ()
473 {
474         ENSURE_GUI_THREAD (*this, &EditorRouteGroups::groups_changed);
475
476         _in_rebuild = true;
477
478         /* just rebuild the while thing */
479
480         _model->clear ();
481
482         if (_session) {
483                 _session->foreach_route_group (sigc::mem_fun (*this, &EditorRouteGroups::add));
484         }
485
486         _in_rebuild = false;
487 }
488
489 void
490 EditorRouteGroups::property_changed (RouteGroup* group, const PropertyChange& change)
491 {
492         _in_row_change = true;
493
494         Gtk::TreeModel::Children children = _model->children();
495
496         for(Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
497                 if (group == (*iter)[_columns.routegroup]) {
498
499                         /* we could check the PropertyChange and only set
500                          * appropriate fields. but the amount of saved by doing
501                          * that is pretty minimal, and this is nice and simple.
502                          */
503
504                         (*iter)[_columns.text] = group->name();
505                         (*iter)[_columns.gain] = group->is_gain ();
506                         (*iter)[_columns.gain_relative] = group->is_relative ();
507                         (*iter)[_columns.mute] = group->is_mute ();
508                         (*iter)[_columns.solo] = group->is_solo ();
509                         (*iter)[_columns.record] = group->is_recenable ();
510                         (*iter)[_columns.monitoring] = group->is_monitoring ();
511                         (*iter)[_columns.select] = group->is_select ();
512                         (*iter)[_columns.edits] = group->is_edit ();
513                         (*iter)[_columns.active_shared] = group->is_route_active ();
514                         (*iter)[_columns.active_state] = group->is_active ();
515                         (*iter)[_columns.is_visible] = !group->is_hidden();
516                         (*iter)[_columns.gdkcolor] = GroupTabs::group_color (group);
517
518                         break;
519                 }
520         }
521         
522         _in_row_change = false;
523
524         for (TrackViewList::const_iterator i = _editor->get_track_views().begin(); i != _editor->get_track_views().end(); ++i) {
525                 if ((*i)->route_group() == group) {
526                         if (group->is_hidden ()) {
527                                 _editor->hide_track_in_display (*i);
528                         } else {
529                                 _editor->_routes->show_track_in_display (**i);
530                         }
531                 }
532         }
533 }
534
535 void
536 EditorRouteGroups::name_edit (const std::string& path, const std::string& new_text)
537 {
538         RouteGroup* group;
539         TreeIter iter;
540
541         if ((iter = _model->get_iter (path))) {
542
543                 if ((group = (*iter)[_columns.routegroup]) == 0) {
544                         return;
545                 }
546
547                 if (new_text != group->name()) {
548                         group->set_name (new_text);
549                 }
550         }
551 }
552
553 void
554 EditorRouteGroups::clear ()
555 {
556         _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
557         _model->clear ();
558         _display.set_model (_model);
559 }
560
561 void
562 EditorRouteGroups::set_session (Session* s)
563 {
564         SessionHandlePtr::set_session (s);
565
566         if (_session) {
567
568                 RouteGroup& arg (_session->all_route_group());
569
570                 arg.PropertyChanged.connect (all_route_groups_changed_connection, MISSING_INVALIDATOR, ui_bind (&EditorRouteGroups::all_group_changed, this, _1), gui_context());
571
572                 _session->route_group_added.connect (_session_connections, MISSING_INVALIDATOR, ui_bind (&EditorRouteGroups::add, this, _1), gui_context());
573                 _session->route_group_removed.connect (
574                         _session_connections, MISSING_INVALIDATOR, boost::bind (&EditorRouteGroups::groups_changed, this), gui_context()
575                         );
576                 _session->route_groups_reordered.connect (
577                         _session_connections, MISSING_INVALIDATOR, boost::bind (&EditorRouteGroups::groups_changed, this), gui_context()
578                         );
579         }
580
581         PBD::PropertyChange pc;
582         pc.add (Properties::select);
583         pc.add (Properties::active);
584         all_group_changed (pc);
585
586         groups_changed ();
587 }
588
589 void
590 EditorRouteGroups::run_new_group_dialog ()
591 {
592         RouteList rl;
593
594         return _editor->_group_tabs->run_new_group_dialog (rl);
595 }
596
597 void
598 EditorRouteGroups::all_group_toggled ()
599 {
600         if (_session) {
601                 _session->all_route_group().set_select (_all_group_active_button.get_active());
602         }
603 }
604
605 void
606 EditorRouteGroups::all_group_changed (const PropertyChange&)
607 {
608         if (_session) {
609                 RouteGroup& arg (_session->all_route_group());
610                 _all_group_active_button.set_active (arg.is_active() && arg.is_select());
611         } else {
612                 _all_group_active_button.set_active (false);
613         }
614 }
615
616 /** Called when a model row is deleted, but also when the model is
617  *  reordered by a user drag-and-drop; the latter is what we are
618  *  interested in here.
619  */
620 void
621 EditorRouteGroups::row_deleted (Gtk::TreeModel::Path const &)
622 {
623         if (_in_rebuild) {
624                 /* We need to ignore this in cases where we're not doing a drag-and-drop
625                    re-order.
626                 */
627                 return;
628         }
629
630         /* Re-write the session's route group list so that the new order is preserved */
631
632         list<RouteGroup*> new_list;
633
634         Gtk::TreeModel::Children children = _model->children();
635         for (Gtk::TreeModel::Children::iterator i = children.begin(); i != children.end(); ++i) {
636                 new_list.push_back ((*i)[_columns.routegroup]);
637         }
638
639         _session->reorder_route_groups (new_list);
640 }
641
642