allow to show/hide Monitor Section
[ardour.git] / gtk2_ardour / mixer_ui.cc
1 /*
2     Copyright (C) 2000-2004 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 <algorithm>
25 #include <map>
26 #include <sigc++/bind.h>
27
28 #include <gtkmm/accelmap.h>
29
30 #include "pbd/convert.h"
31 #include "pbd/unwind.h"
32
33 #include <glibmm/threads.h>
34
35 #include <gtkmm2ext/gtk_ui.h>
36 #include <gtkmm2ext/utils.h>
37 #include <gtkmm2ext/tearoff.h>
38 #include <gtkmm2ext/window_title.h>
39
40 #include "ardour/debug.h"
41 #include "ardour/midi_track.h"
42 #include "ardour/plugin_manager.h"
43 #include "ardour/route_group.h"
44 #include "ardour/route_sorters.h"
45 #include "ardour/session.h"
46
47 #include "keyboard.h"
48 #include "mixer_ui.h"
49 #include "mixer_strip.h"
50 #include "monitor_section.h"
51 #include "plugin_selector.h"
52 #include "public_editor.h"
53 #include "ardour_ui.h"
54 #include "prompter.h"
55 #include "utils.h"
56 #include "route_sorter.h"
57 #include "actions.h"
58 #include "gui_thread.h"
59 #include "mixer_group_tabs.h"
60 #include "timers.h"
61 #include "ui_config.h"
62
63 #include "i18n.h"
64
65 using namespace ARDOUR;
66 using namespace ARDOUR_UI_UTILS;
67 using namespace PBD;
68 using namespace Gtk;
69 using namespace Glib;
70 using namespace Gtkmm2ext;
71 using namespace std;
72
73 using PBD::atoi;
74 using PBD::Unwinder;
75
76 Mixer_UI* Mixer_UI::_instance = 0;
77
78 Mixer_UI*
79 Mixer_UI::instance ()
80 {
81         if (!_instance) {
82                 _instance  = new Mixer_UI;
83         }
84
85         return _instance;
86 }
87
88 Mixer_UI::Mixer_UI ()
89         : Window (Gtk::WINDOW_TOPLEVEL)
90         , VisibilityTracker (*((Gtk::Window*) this))
91         , _visible (false)
92         , no_track_list_redisplay (false)
93         , in_group_row_change (false)
94         , track_menu (0)
95         , _monitor_section (0)
96         , _plugin_selector (0)
97         , _strip_width (UIConfiguration::instance().get_default_narrow_ms() ? Narrow : Wide)
98         , ignore_reorder (false)
99         , _in_group_rebuild_or_clear (false)
100         , _route_deletion_in_progress (false)
101         , _following_editor_selection (false)
102         , _maximised (false)
103         , _show_mixer_list (true)
104 {
105         /* allow this window to become the key focus window */
106         set_flags (CAN_FOCUS);
107
108         Route::SyncOrderKeys.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::sync_treeview_from_order_keys, this), gui_context());
109
110         scroller.set_can_default (true);
111         set_default (scroller);
112
113         scroller_base.set_flags (Gtk::CAN_FOCUS);
114         scroller_base.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
115         scroller_base.set_name ("MixerWindow");
116         scroller_base.signal_button_release_event().connect (sigc::mem_fun(*this, &Mixer_UI::strip_scroller_button_release));
117         // add as last item of strip packer
118         strip_packer.pack_end (scroller_base, true, true);
119
120         _group_tabs = new MixerGroupTabs (this);
121         VBox* b = manage (new VBox);
122         b->pack_start (*_group_tabs, PACK_SHRINK);
123         b->pack_start (strip_packer);
124         b->show_all ();
125
126         scroller.add (*b);
127         scroller.set_policy (Gtk::POLICY_ALWAYS, Gtk::POLICY_AUTOMATIC);
128
129         setup_track_display ();
130
131         group_model = ListStore::create (group_columns);
132         group_display.set_model (group_model);
133         group_display.append_column (_("Group"), group_columns.text);
134         group_display.append_column (_("Show"), group_columns.visible);
135         group_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
136         group_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
137         group_display.get_column (0)->set_expand(true);
138         group_display.get_column (1)->set_expand(false);
139         group_display.set_name ("EditGroupList");
140         group_display.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
141         group_display.set_reorderable (true);
142         group_display.set_headers_visible (true);
143         group_display.set_rules_hint (true);
144         group_display.set_can_focus(false);
145
146         /* name is directly editable */
147
148         CellRendererText* name_cell = dynamic_cast<CellRendererText*>(group_display.get_column_cell_renderer (0));
149         name_cell->property_editable() = true;
150         name_cell->signal_edited().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_name_edit));
151
152         /* use checkbox for the active column */
153
154         CellRendererToggle* active_cell = dynamic_cast<CellRendererToggle*>(group_display.get_column_cell_renderer (1));
155         active_cell->property_activatable() = true;
156         active_cell->property_radio() = false;
157
158         group_model->signal_row_changed().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_row_change));
159         /* We use this to notice drag-and-drop reorders of the group list */
160         group_model->signal_row_deleted().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_row_deleted));
161         group_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::group_display_button_press), false);
162
163         group_display_scroller.add (group_display);
164         group_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
165
166         HBox* route_group_display_button_box = manage (new HBox());
167
168         Button* route_group_add_button = manage (new Button ());
169         Button* route_group_remove_button = manage (new Button ());
170
171         Widget* w;
172
173         w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
174         w->show();
175         route_group_add_button->add (*w);
176
177         w = manage (new Image (Stock::REMOVE, ICON_SIZE_BUTTON));
178         w->show();
179         route_group_remove_button->add (*w);
180
181         route_group_display_button_box->set_homogeneous (true);
182
183         route_group_add_button->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::new_route_group));
184         route_group_remove_button->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::remove_selected_route_group));
185
186         route_group_display_button_box->add (*route_group_add_button);
187         route_group_display_button_box->add (*route_group_remove_button);
188
189         group_display_vbox.pack_start (group_display_scroller, true, true);
190         group_display_vbox.pack_start (*route_group_display_button_box, false, false);
191
192         group_display_frame.set_name ("BaseFrame");
193         group_display_frame.set_shadow_type (Gtk::SHADOW_IN);
194         group_display_frame.add (group_display_vbox);
195
196         rhs_pane1.pack1 (track_display_frame);
197         rhs_pane1.pack2 (group_display_frame);
198
199         list_vpacker.pack_start (rhs_pane1, true, true);
200
201         global_hpacker.pack_start (scroller, true, true);
202 #ifdef GTKOSX
203         /* current gtk-quartz has dirty updates on borders like this one */
204         global_hpacker.pack_start (out_packer, false, false, 0);
205 #else
206         global_hpacker.pack_start (out_packer, false, false, 12);
207 #endif
208         list_hpane.pack1(list_vpacker, false, true);
209         list_hpane.pack2(global_hpacker, true, false);
210
211         rhs_pane1.signal_size_allocate().connect (sigc::bind (sigc::mem_fun(*this, &Mixer_UI::pane_allocation_handler),
212                                                         static_cast<Gtk::Paned*> (&rhs_pane1)));
213         list_hpane.signal_size_allocate().connect (sigc::bind (sigc::mem_fun(*this, &Mixer_UI::pane_allocation_handler),
214                                                          static_cast<Gtk::Paned*> (&list_hpane)));
215
216         global_vpacker.pack_start (list_hpane, true, true);
217
218         add (global_vpacker);
219         set_name ("MixerWindow");
220
221         update_title ();
222
223         set_wmclass (X_("ardour_mixer"), PROGRAM_NAME);
224
225         signal_delete_event().connect (sigc::mem_fun (*this, &Mixer_UI::hide_window));
226         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
227
228         signal_configure_event().connect (sigc::mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::configure_handler));
229
230         route_group_display_button_box->show();
231         route_group_add_button->show();
232         route_group_remove_button->show();
233
234         global_hpacker.show();
235         global_vpacker.show();
236         scroller.show();
237         scroller_base.show();
238         scroller_hpacker.show();
239         mixer_scroller_vpacker.show();
240         list_vpacker.show();
241         group_display_button_label.show();
242         group_display_button.show();
243         group_display_scroller.show();
244         group_display_vbox.show();
245         group_display_frame.show();
246         rhs_pane1.show();
247         strip_packer.show();
248         out_packer.show();
249         list_hpane.show();
250         group_display.show();
251
252         MixerStrip::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::remove_strip, this, _1), gui_context());
253
254 #ifndef DEFER_PLUGIN_SELECTOR_LOAD
255         _plugin_selector = new PluginSelector (PluginManager::instance ());
256 #endif
257 }
258
259 Mixer_UI::~Mixer_UI ()
260 {
261         if (_monitor_section) {
262                 monitor_section_detached ();
263                 delete _monitor_section;
264         }
265         delete _plugin_selector;
266 }
267
268 void
269 Mixer_UI::track_editor_selection ()
270 {
271         PublicEditor::instance().get_selection().TracksChanged.connect (sigc::mem_fun (*this, &Mixer_UI::follow_editor_selection));
272 }
273
274
275 void
276 Mixer_UI::ensure_float (Window& win)
277 {
278         win.set_transient_for (*this);
279 }
280
281 void
282 Mixer_UI::show_window ()
283 {
284         present ();
285         if (!_visible) {
286                 set_window_pos_and_size ();
287
288                 /* show/hide group tabs as required */
289                 parameter_changed ("show-group-tabs");
290
291                 /* now reset each strips width so the right widgets are shown */
292                 MixerStrip* ms;
293
294                 TreeModel::Children rows = track_model->children();
295                 TreeModel::Children::iterator ri;
296
297                 for (ri = rows.begin(); ri != rows.end(); ++ri) {
298                         ms = (*ri)[track_columns.strip];
299                         ms->set_width_enum (ms->get_width_enum (), ms->width_owner());
300                         /* Fix visibility of mixer strip stuff */
301                         ms->parameter_changed (X_("mixer-element-visibility"));
302                 }
303         }
304
305         /* force focus into main area */
306         scroller_base.grab_focus ();
307
308         _visible = true;
309 }
310
311 bool
312 Mixer_UI::hide_window (GdkEventAny *ev)
313 {
314         get_window_pos_and_size ();
315
316         _visible = false;
317         return just_hide_it(ev, static_cast<Gtk::Window *>(this));
318 }
319
320
321 void
322 Mixer_UI::add_strips (RouteList& routes)
323 {
324         bool from_scratch = track_model->children().size() == 0;
325         Gtk::TreeModel::Children::iterator insert_iter = track_model->children().end();
326
327         for (Gtk::TreeModel::Children::iterator it = track_model->children().begin(); it != track_model->children().end(); ++it) {
328                 boost::shared_ptr<Route> r = (*it)[track_columns.route];
329
330                 if (r->order_key() == (routes.front()->order_key() + routes.size())) {
331                         insert_iter = it;
332                         break;
333                 }
334         }
335
336         if(!from_scratch) {
337                 _selection.clear_routes ();
338         }
339
340         MixerStrip* strip;
341
342         try {
343                 no_track_list_redisplay = true;
344                 track_display.set_model (Glib::RefPtr<ListStore>());
345
346                 for (RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
347                         boost::shared_ptr<Route> route = (*x);
348
349                         if (route->is_auditioner()) {
350                                 continue;
351                         }
352
353                         if (route->is_monitor()) {
354
355                                 if (!_monitor_section) {
356                                         _monitor_section = new MonitorSection (_session);
357
358                                         XMLNode* mnode = ARDOUR_UI::instance()->tearoff_settings (X_("monitor-section"));
359                                         if (mnode) {
360                                                 _monitor_section->tearoff().set_state (*mnode);
361                                         }
362                                 }
363
364                                 out_packer.pack_end (_monitor_section->tearoff(), false, false);
365                                 _monitor_section->set_session (_session);
366                                 _monitor_section->tearoff().show_all ();
367
368                                 _monitor_section->tearoff().Detach.connect (sigc::mem_fun(*this, &Mixer_UI::monitor_section_detached));
369                                 _monitor_section->tearoff().Attach.connect (sigc::mem_fun(*this, &Mixer_UI::monitor_section_attached));
370
371                                 monitor_section_attached ();
372
373                                 route->DropReferences.connect (*this, invalidator(*this), boost::bind (&Mixer_UI::monitor_section_going_away, this), gui_context());
374
375                                 /* no regular strip shown for control out */
376
377                                 continue;
378                         }
379
380                         strip = new MixerStrip (*this, _session, route);
381                         strips.push_back (strip);
382
383                         UIConfiguration::instance().get_default_narrow_ms() ? _strip_width = Narrow : _strip_width = Wide;
384
385                         if (strip->width_owner() != strip) {
386                                 strip->set_width_enum (_strip_width, this);
387                         }
388
389                         show_strip (strip);
390
391                         TreeModel::Row row = *(track_model->insert(insert_iter));
392                         row[track_columns.text] = route->name();
393                         row[track_columns.visible] = strip->route()->is_master() ? true : strip->marked_for_display();
394                         row[track_columns.route] = route;
395                         row[track_columns.strip] = strip;
396
397                         if (!from_scratch) {
398                                 _selection.add (strip);
399                         }
400
401                         route->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::strip_property_changed, this, _1, strip), gui_context());
402
403                         strip->WidthChanged.connect (sigc::mem_fun(*this, &Mixer_UI::strip_width_changed));
404                         strip->signal_button_release_event().connect (sigc::bind (sigc::mem_fun(*this, &Mixer_UI::strip_button_release_event), strip));
405                 }
406
407         } catch (...) {
408         }
409
410         no_track_list_redisplay = false;
411         track_display.set_model (track_model);
412
413         sync_order_keys_from_treeview ();
414         redisplay_track_list ();
415 }
416
417 void
418 Mixer_UI::deselect_all_strip_processors ()
419 {
420         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
421                 (*i)->deselect_all_processors();
422         }
423 }
424
425 void
426 Mixer_UI::select_none ()
427 {
428         _selection.clear_routes();
429         deselect_all_strip_processors();
430 }
431
432 void
433 Mixer_UI::delete_processors ()
434 {
435         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
436                 (*i)->delete_processors();
437         }
438 }
439
440
441 void
442 Mixer_UI::remove_strip (MixerStrip* strip)
443 {
444         if (_session && _session->deletion_in_progress()) {
445                 /* its all being taken care of */
446                 return;
447         }
448
449         TreeModel::Children rows = track_model->children();
450         TreeModel::Children::iterator ri;
451         list<MixerStrip *>::iterator i;
452
453         if ((i = find (strips.begin(), strips.end(), strip)) != strips.end()) {
454                 strips.erase (i);
455         }
456
457         for (ri = rows.begin(); ri != rows.end(); ++ri) {
458                 if ((*ri)[track_columns.strip] == strip) {
459                         PBD::Unwinder<bool> uw (_route_deletion_in_progress, true);
460                         track_model->erase (ri);
461                         break;
462                 }
463         }
464 }
465
466 void
467 Mixer_UI::reset_remote_control_ids ()
468 {
469         if (Config->get_remote_model() == UserOrdered || !_session || _session->deletion_in_progress()) {
470                 return;
471         }
472
473         TreeModel::Children rows = track_model->children();
474
475         if (rows.empty()) {
476                 return;
477         }
478
479         DEBUG_TRACE (DEBUG::OrderKeys, "mixer resets remote control ids after remote model change\n");
480
481         TreeModel::Children::iterator ri;
482         bool rid_change = false;
483         uint32_t rid = 1;
484         uint32_t invisible_key = UINT32_MAX;
485
486         for (ri = rows.begin(); ri != rows.end(); ++ri) {
487
488                 /* skip two special values */
489
490                 if (rid == Route::MasterBusRemoteControlID) {
491                         rid++;
492                 }
493
494                 if (rid == Route::MonitorBusRemoteControlID) {
495                         rid++;
496                 }
497
498                 boost::shared_ptr<Route> route = (*ri)[track_columns.route];
499                 bool visible = (*ri)[track_columns.visible];
500
501                 if (!route->is_master() && !route->is_monitor()) {
502
503                         uint32_t new_rid = (visible ? rid : invisible_key--);
504
505                         if (new_rid != route->remote_control_id()) {
506                                 route->set_remote_control_id_explicit (new_rid);
507                                 rid_change = true;
508                         }
509
510                         if (visible) {
511                                 rid++;
512                         }
513                 }
514         }
515
516         if (rid_change) {
517                 /* tell the world that we changed the remote control IDs */
518                 _session->notify_remote_id_change ();
519         }
520 }
521
522 void
523 Mixer_UI::sync_order_keys_from_treeview ()
524 {
525         if (ignore_reorder || !_session || _session->deletion_in_progress()) {
526                 return;
527         }
528
529         TreeModel::Children rows = track_model->children();
530
531         if (rows.empty()) {
532                 return;
533         }
534
535         DEBUG_TRACE (DEBUG::OrderKeys, "mixer sync order keys from model\n");
536
537         TreeModel::Children::iterator ri;
538         bool changed = false;
539         bool rid_change = false;
540         uint32_t order = 0;
541         uint32_t rid = 1;
542         uint32_t invisible_key = UINT32_MAX;
543
544         for (ri = rows.begin(); ri != rows.end(); ++ri) {
545                 boost::shared_ptr<Route> route = (*ri)[track_columns.route];
546                 bool visible = (*ri)[track_columns.visible];
547
548                 uint32_t old_key = route->order_key ();
549
550                 if (order != old_key) {
551                         route->set_order_key (order);
552                         changed = true;
553                 }
554
555                 if ((Config->get_remote_model() == MixerOrdered) && !route->is_master() && !route->is_monitor()) {
556
557                         uint32_t new_rid = (visible ? rid : invisible_key--);
558
559                         if (new_rid != route->remote_control_id()) {
560                                 route->set_remote_control_id_explicit (new_rid);
561                                 rid_change = true;
562                         }
563
564                         if (visible) {
565                                 rid++;
566                         }
567
568                 }
569
570                 ++order;
571         }
572
573         if (changed) {
574                 /* tell everyone that we changed the mixer sort keys */
575                 _session->sync_order_keys ();
576         }
577
578         if (rid_change) {
579                 /* tell the world that we changed the remote control IDs */
580                 _session->notify_remote_id_change ();
581         }
582 }
583
584 void
585 Mixer_UI::sync_treeview_from_order_keys ()
586 {
587         if (!_session || _session->deletion_in_progress()) {
588                 return;
589         }
590
591         DEBUG_TRACE (DEBUG::OrderKeys, "mixer sync model from order keys.\n");
592
593         /* we could get here after either a change in the Mixer or Editor sort
594          * order, but either way, the mixer order keys reflect the intended
595          * order for the GUI, so reorder the treeview model to match it.
596          */
597
598         vector<int> neworder;
599         TreeModel::Children rows = track_model->children();
600         uint32_t old_order = 0;
601         bool changed = false;
602
603         if (rows.empty()) {
604                 return;
605         }
606
607         OrderKeySortedRoutes sorted_routes;
608
609         for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri, ++old_order) {
610                 boost::shared_ptr<Route> route = (*ri)[track_columns.route];
611                 sorted_routes.push_back (RoutePlusOrderKey (route, old_order, route->order_key ()));
612         }
613
614         SortByNewDisplayOrder cmp;
615
616         sort (sorted_routes.begin(), sorted_routes.end(), cmp);
617         neworder.assign (sorted_routes.size(), 0);
618
619         uint32_t n = 0;
620
621         for (OrderKeySortedRoutes::iterator sr = sorted_routes.begin(); sr != sorted_routes.end(); ++sr, ++n) {
622
623                 neworder[n] = sr->old_display_order;
624
625                 if (sr->old_display_order != n) {
626                         changed = true;
627                 }
628
629                 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("MIXER change order for %1 from %2 to %3\n",
630                                                                sr->route->name(), sr->old_display_order, n));
631         }
632
633         if (changed) {
634                 Unwinder<bool> uw (ignore_reorder, true);
635                 track_model->reorder (neworder);
636         }
637
638         redisplay_track_list ();
639 }
640
641 void
642 Mixer_UI::follow_editor_selection ()
643 {
644         if (_following_editor_selection) {
645                 return;
646         }
647
648         _following_editor_selection = true;
649         _selection.block_routes_changed (true);
650
651         TrackSelection& s (PublicEditor::instance().get_selection().tracks);
652
653         _selection.clear_routes ();
654
655         for (TrackViewList::iterator i = s.begin(); i != s.end(); ++i) {
656                 RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*i);
657                 if (rtav) {
658                         MixerStrip* ms = strip_by_route (rtav->route());
659                         if (ms) {
660                                 _selection.add (ms);
661                         }
662                 }
663         }
664
665         _following_editor_selection = false;
666         _selection.block_routes_changed (false);
667 }
668
669
670 MixerStrip*
671 Mixer_UI::strip_by_route (boost::shared_ptr<Route> r)
672 {
673         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
674                 if ((*i)->route() == r) {
675                         return (*i);
676                 }
677         }
678
679         return 0;
680 }
681
682 bool
683 Mixer_UI::strip_button_release_event (GdkEventButton *ev, MixerStrip *strip)
684 {
685         if (ev->button == 1) {
686                 if (_selection.selected (strip)) {
687                         /* primary-click: toggle selection state of strip */
688                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
689                                 _selection.remove (strip);
690                         } else if (_selection.routes.size() > 1) {
691                                 /* de-select others */
692                                 _selection.set (strip);
693                         }
694                 } else {
695                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
696                                 _selection.add (strip);
697                         } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::RangeSelectModifier)) {
698
699                                 if (!_selection.selected(strip)) {
700
701                                         /* extend selection */
702
703                                         vector<MixerStrip*> tmp;
704                                         bool accumulate = false;
705                                         bool found_another = false;
706
707                                         tmp.push_back (strip);
708
709                                         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
710                                                 if ((*i) == strip) {
711                                                         /* hit clicked strip, start accumulating till we hit the first
712                                                            selected strip
713                                                         */
714                                                         if (accumulate) {
715                                                                 /* done */
716                                                                 break;
717                                                         } else {
718                                                                 accumulate = true;
719                                                         }
720                                                 } else if (_selection.selected (*i)) {
721                                                         /* hit selected strip. if currently accumulating others,
722                                                            we're done. if not accumulating others, start doing so.
723                                                         */
724                                                         found_another = true;
725                                                         if (accumulate) {
726                                                                 /* done */
727                                                                 break;
728                                                         } else {
729                                                                 accumulate = true;
730                                                         }
731                                                 } else {
732                                                         if (accumulate) {
733                                                                 tmp.push_back (*i);
734                                                         }
735                                                 }
736                                         }
737
738                                         if (found_another) {
739                                                 for (vector<MixerStrip*>::iterator i = tmp.begin(); i != tmp.end(); ++i) {
740                                                         _selection.add (*i);
741                                                 }
742                                         } else
743                                                 _selection.set (strip);  //user wants to start a range selection, but there aren't any others selected yet
744                                 }
745
746                         } else {
747                                 _selection.set (strip);
748                         }
749                 }
750         }
751
752         return true;
753 }
754
755 void
756 Mixer_UI::set_session (Session* sess)
757 {
758         SessionHandlePtr::set_session (sess);
759
760         if (_plugin_selector) {
761                 _plugin_selector->set_session (_session);
762         }
763
764         _group_tabs->set_session (sess);
765
766         if (!_session) {
767                 return;
768         }
769
770         XMLNode* node = ARDOUR_UI::instance()->mixer_settings();
771         set_state (*node);
772
773         update_title ();
774
775         initial_track_display ();
776
777         _session->RouteAdded.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_strips, this, _1), gui_context());
778         _session->route_group_added.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_route_group, this, _1), gui_context());
779         _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::route_groups_changed, this), gui_context());
780         _session->route_groups_reordered.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::route_groups_changed, this), gui_context());
781         _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::parameter_changed, this, _1), gui_context());
782         _session->DirtyChanged.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::update_title, this), gui_context());
783         _session->StateSaved.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::update_title, this), gui_context());
784
785         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::parameter_changed, this, _1), gui_context ());
786
787         route_groups_changed ();
788
789         if (_visible) {
790                 show_window();
791         }
792
793         start_updating ();
794 }
795
796 void
797 Mixer_UI::session_going_away ()
798 {
799         ENSURE_GUI_THREAD (*this, &Mixer_UI::session_going_away);
800
801         _in_group_rebuild_or_clear = true;
802         group_model->clear ();
803         _in_group_rebuild_or_clear = false;
804
805         _selection.clear ();
806         track_model->clear ();
807
808         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
809                 delete (*i);
810         }
811
812         if (_monitor_section) {
813                 _monitor_section->tearoff().hide_visible ();
814         }
815
816         monitor_section_detached ();
817
818         strips.clear ();
819
820         stop_updating ();
821
822         SessionHandlePtr::session_going_away ();
823
824         _session = 0;
825         update_title ();
826 }
827
828 void
829 Mixer_UI::track_visibility_changed (std::string const & path)
830 {
831         if (_session && _session->deletion_in_progress()) {
832                 return;
833         }
834
835         TreeIter iter;
836
837         if ((iter = track_model->get_iter (path))) {
838                 MixerStrip* strip = (*iter)[track_columns.strip];
839                 if (strip) {
840                         bool visible = (*iter)[track_columns.visible];
841
842                         if (strip->set_marked_for_display (!visible)) {
843                                 update_track_visibility ();
844                         }
845                 }
846         }
847 }
848
849 void
850 Mixer_UI::update_track_visibility ()
851 {
852         TreeModel::Children rows = track_model->children();
853         TreeModel::Children::iterator i;
854
855         {
856                 Unwinder<bool> uw (no_track_list_redisplay, true);
857
858                 for (i = rows.begin(); i != rows.end(); ++i) {
859                         MixerStrip *strip = (*i)[track_columns.strip];
860                         (*i)[track_columns.visible] = strip->marked_for_display ();
861                 }
862
863                 /* force route order keys catch up with visibility changes
864                  */
865
866                 sync_order_keys_from_treeview ();
867         }
868
869         redisplay_track_list ();
870 }
871
872 void
873 Mixer_UI::show_strip (MixerStrip* ms)
874 {
875         TreeModel::Children rows = track_model->children();
876         TreeModel::Children::iterator i;
877
878         for (i = rows.begin(); i != rows.end(); ++i) {
879
880                 MixerStrip* strip = (*i)[track_columns.strip];
881                 if (strip == ms) {
882                         (*i)[track_columns.visible] = true;
883                         redisplay_track_list ();
884                         break;
885                 }
886         }
887 }
888
889 void
890 Mixer_UI::hide_strip (MixerStrip* ms)
891 {
892         TreeModel::Children rows = track_model->children();
893         TreeModel::Children::iterator i;
894
895         for (i = rows.begin(); i != rows.end(); ++i) {
896
897                 MixerStrip* strip = (*i)[track_columns.strip];
898                 if (strip == ms) {
899                         (*i)[track_columns.visible] = false;
900                         redisplay_track_list ();
901                         break;
902                 }
903         }
904 }
905
906 gint
907 Mixer_UI::start_updating ()
908 {
909     fast_screen_update_connection = Timers::super_rapid_connect (sigc::mem_fun(*this, &Mixer_UI::fast_update_strips));
910     return 0;
911 }
912
913 gint
914 Mixer_UI::stop_updating ()
915 {
916     fast_screen_update_connection.disconnect();
917     return 0;
918 }
919
920 void
921 Mixer_UI::fast_update_strips ()
922 {
923         if (is_mapped () && _session) {
924                 for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
925                         (*i)->fast_update ();
926                 }
927         }
928 }
929
930 void
931 Mixer_UI::set_all_strips_visibility (bool yn)
932 {
933         TreeModel::Children rows = track_model->children();
934         TreeModel::Children::iterator i;
935
936         {
937                 Unwinder<bool> uw (no_track_list_redisplay, true);
938
939                 for (i = rows.begin(); i != rows.end(); ++i) {
940
941                         TreeModel::Row row = (*i);
942                         MixerStrip* strip = row[track_columns.strip];
943
944                         if (strip == 0) {
945                                 continue;
946                         }
947
948                         if (strip->route()->is_master() || strip->route()->is_monitor()) {
949                                 continue;
950                         }
951
952                         (*i)[track_columns.visible] = yn;
953                 }
954         }
955
956         redisplay_track_list ();
957 }
958
959
960 void
961 Mixer_UI::set_all_audio_midi_visibility (int tracks, bool yn)
962 {
963         TreeModel::Children rows = track_model->children();
964         TreeModel::Children::iterator i;
965
966         {
967                 Unwinder<bool> uw (no_track_list_redisplay, true);
968
969                 for (i = rows.begin(); i != rows.end(); ++i) {
970                         TreeModel::Row row = (*i);
971                         MixerStrip* strip = row[track_columns.strip];
972
973                         if (strip == 0) {
974                                 continue;
975                         }
976
977                         if (strip->route()->is_master() || strip->route()->is_monitor()) {
978                                 continue;
979                         }
980
981                         boost::shared_ptr<AudioTrack> at = strip->audio_track();
982                         boost::shared_ptr<MidiTrack> mt = strip->midi_track();
983
984                         switch (tracks) {
985                         case 0:
986                                 (*i)[track_columns.visible] = yn;
987                                 break;
988
989                         case 1:
990                                 if (at) { /* track */
991                                         (*i)[track_columns.visible] = yn;
992                                 }
993                                 break;
994
995                         case 2:
996                                 if (!at && !mt) { /* bus */
997                                         (*i)[track_columns.visible] = yn;
998                                 }
999                                 break;
1000
1001                         case 3:
1002                                 if (mt) { /* midi-track */
1003                                         (*i)[track_columns.visible] = yn;
1004                                 }
1005                                 break;
1006                         }
1007                 }
1008         }
1009
1010         redisplay_track_list ();
1011 }
1012
1013 void
1014 Mixer_UI::hide_all_routes ()
1015 {
1016         set_all_strips_visibility (false);
1017 }
1018
1019 void
1020 Mixer_UI::show_all_routes ()
1021 {
1022         set_all_strips_visibility (true);
1023 }
1024
1025 void
1026 Mixer_UI::show_all_audiobus ()
1027 {
1028         set_all_audio_midi_visibility (2, true);
1029 }
1030 void
1031 Mixer_UI::hide_all_audiobus ()
1032 {
1033         set_all_audio_midi_visibility (2, false);
1034 }
1035
1036 void
1037 Mixer_UI::show_all_audiotracks()
1038 {
1039         set_all_audio_midi_visibility (1, true);
1040 }
1041 void
1042 Mixer_UI::hide_all_audiotracks ()
1043 {
1044         set_all_audio_midi_visibility (1, false);
1045 }
1046
1047 void
1048 Mixer_UI::show_all_miditracks()
1049 {
1050         set_all_audio_midi_visibility (3, true);
1051 }
1052 void
1053 Mixer_UI::hide_all_miditracks ()
1054 {
1055         set_all_audio_midi_visibility (3, false);
1056 }
1057
1058
1059 void
1060 Mixer_UI::track_list_reorder (const TreeModel::Path&, const TreeModel::iterator&, int* /*new_order*/)
1061 {
1062         DEBUG_TRACE (DEBUG::OrderKeys, "mixer UI treeview reordered\n");
1063         sync_order_keys_from_treeview ();
1064 }
1065
1066 void
1067 Mixer_UI::track_list_delete (const Gtk::TreeModel::Path&)
1068 {
1069         /* this happens as the second step of a DnD within the treeview as well
1070            as when a row/route is actually deleted.
1071
1072            if it was a deletion then we have to force a redisplay because
1073            order keys may not have changed.
1074         */
1075
1076         DEBUG_TRACE (DEBUG::OrderKeys, "mixer UI treeview row deleted\n");
1077         sync_order_keys_from_treeview ();
1078
1079         if (_route_deletion_in_progress) {
1080                 redisplay_track_list ();
1081         }
1082 }
1083
1084 void
1085 Mixer_UI::redisplay_track_list ()
1086 {
1087         TreeModel::Children rows = track_model->children();
1088         TreeModel::Children::iterator i;
1089
1090         if (no_track_list_redisplay) {
1091                 return;
1092         }
1093
1094         for (i = rows.begin(); i != rows.end(); ++i) {
1095
1096                 MixerStrip* strip = (*i)[track_columns.strip];
1097
1098                 if (strip == 0) {
1099                         /* we're in the middle of changing a row, don't worry */
1100                         continue;
1101                 }
1102
1103                 bool const visible = (*i)[track_columns.visible];
1104
1105                 if (visible) {
1106                         strip->set_gui_property ("visible", true);
1107
1108                         if (strip->packed()) {
1109
1110                                 if (strip->route()->is_master() || strip->route()->is_monitor()) {
1111                                         out_packer.reorder_child (*strip, -1);
1112
1113                                 } else {
1114                                         strip_packer.reorder_child (*strip, -1); /* put at end */
1115                                 }
1116
1117                         } else {
1118
1119                                 if (strip->route()->is_master() || strip->route()->is_monitor()) {
1120                                         out_packer.pack_start (*strip, false, false);
1121                                 } else {
1122                                         strip_packer.pack_start (*strip, false, false);
1123                                 }
1124                                 strip->set_packed (true);
1125                         }
1126
1127                 } else {
1128
1129                         strip->set_gui_property ("visible", false);
1130
1131                         if (strip->route()->is_master() || strip->route()->is_monitor()) {
1132                                 /* do nothing, these cannot be hidden */
1133                         } else {
1134                                 if (strip->packed()) {
1135                                         strip_packer.remove (*strip);
1136                                         strip->set_packed (false);
1137                                 }
1138                         }
1139                 }
1140         }
1141
1142         _group_tabs->set_dirty ();
1143 }
1144
1145 void
1146 Mixer_UI::strip_width_changed ()
1147 {
1148         _group_tabs->set_dirty ();
1149
1150 #ifdef GTKOSX
1151         TreeModel::Children rows = track_model->children();
1152         TreeModel::Children::iterator i;
1153         long order;
1154
1155         for (order = 0, i = rows.begin(); i != rows.end(); ++i, ++order) {
1156                 MixerStrip* strip = (*i)[track_columns.strip];
1157
1158                 if (strip == 0) {
1159                         continue;
1160                 }
1161
1162                 bool visible = (*i)[track_columns.visible];
1163
1164                 if (visible) {
1165                         strip->queue_draw();
1166                 }
1167         }
1168 #endif
1169
1170 }
1171
1172 void
1173 Mixer_UI::initial_track_display ()
1174 {
1175         boost::shared_ptr<RouteList> routes = _session->get_routes();
1176         RouteList copy (*routes);
1177         ARDOUR::SignalOrderRouteSorter sorter;
1178
1179         copy.sort (sorter);
1180
1181         {
1182                 Unwinder<bool> uw1 (no_track_list_redisplay, true);
1183                 Unwinder<bool> uw2 (ignore_reorder, true);
1184
1185                 track_model->clear ();
1186                 add_strips (copy);
1187         }
1188
1189         _session->sync_order_keys ();
1190
1191         redisplay_track_list ();
1192 }
1193
1194 void
1195 Mixer_UI::show_track_list_menu ()
1196 {
1197         if (track_menu == 0) {
1198                 build_track_menu ();
1199         }
1200
1201         track_menu->popup (1, gtk_get_current_event_time());
1202 }
1203
1204 bool
1205 Mixer_UI::track_display_button_press (GdkEventButton* ev)
1206 {
1207         if (Keyboard::is_context_menu_event (ev)) {
1208                 show_track_list_menu ();
1209                 return true;
1210         }
1211
1212         return false;
1213 }
1214
1215 void
1216 Mixer_UI::build_track_menu ()
1217 {
1218         using namespace Menu_Helpers;
1219         using namespace Gtk;
1220
1221         track_menu = new Menu;
1222         track_menu->set_name ("ArdourContextMenu");
1223         MenuList& items = track_menu->items();
1224
1225         items.push_back (MenuElem (_("Show All"), sigc::mem_fun(*this, &Mixer_UI::show_all_routes)));
1226         items.push_back (MenuElem (_("Hide All"), sigc::mem_fun(*this, &Mixer_UI::hide_all_routes)));
1227         items.push_back (MenuElem (_("Show All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiotracks)));
1228         items.push_back (MenuElem (_("Hide All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiotracks)));
1229         items.push_back (MenuElem (_("Show All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiobus)));
1230         items.push_back (MenuElem (_("Hide All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiobus)));
1231         items.push_back (MenuElem (_("Show All Midi Tracks"), sigc::mem_fun (*this, &Mixer_UI::show_all_miditracks)));
1232         items.push_back (MenuElem (_("Hide All Midi Tracks"), sigc::mem_fun (*this, &Mixer_UI::hide_all_miditracks)));
1233
1234 }
1235
1236 void
1237 Mixer_UI::strip_property_changed (const PropertyChange& what_changed, MixerStrip* mx)
1238 {
1239         if (!what_changed.contains (ARDOUR::Properties::name)) {
1240                 return;
1241         }
1242
1243         ENSURE_GUI_THREAD (*this, &Mixer_UI::strip_name_changed, what_changed, mx)
1244
1245         TreeModel::Children rows = track_model->children();
1246         TreeModel::Children::iterator i;
1247
1248         for (i = rows.begin(); i != rows.end(); ++i) {
1249                 if ((*i)[track_columns.strip] == mx) {
1250                         (*i)[track_columns.text] = mx->route()->name();
1251                         return;
1252                 }
1253         }
1254
1255         error << _("track display list item for renamed strip not found!") << endmsg;
1256 }
1257
1258 bool
1259 Mixer_UI::group_display_button_press (GdkEventButton* ev)
1260 {
1261         TreeModel::Path path;
1262         TreeViewColumn* column;
1263         int cellx;
1264         int celly;
1265
1266         if (!group_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1267                 _group_tabs->get_menu(0)->popup (1, ev->time);
1268                 return true;
1269         }
1270
1271         TreeIter iter = group_model->get_iter (path);
1272         if (!iter) {
1273                 _group_tabs->get_menu(0)->popup (1, ev->time);
1274                 return true;
1275         }
1276
1277         RouteGroup* group = (*iter)[group_columns.group];
1278
1279         if (Keyboard::is_context_menu_event (ev)) {
1280                 _group_tabs->get_menu(group)->popup (1, ev->time);
1281                 return true;
1282         }
1283
1284         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
1285         case 0:
1286                 if (Keyboard::is_edit_event (ev)) {
1287                         if (group) {
1288                                 // edit_route_group (group);
1289 #ifdef GTKOSX
1290                                 group_display.queue_draw();
1291 #endif
1292                                 return true;
1293                         }
1294                 }
1295                 break;
1296
1297         case 1:
1298         {
1299                 bool visible = (*iter)[group_columns.visible];
1300                 (*iter)[group_columns.visible] = !visible;
1301 #ifdef GTKOSX
1302                 group_display.queue_draw();
1303 #endif
1304                 return true;
1305         }
1306
1307         default:
1308                 break;
1309         }
1310
1311         return false;
1312  }
1313
1314 void
1315 Mixer_UI::activate_all_route_groups ()
1316 {
1317         _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), true));
1318 }
1319
1320 void
1321 Mixer_UI::disable_all_route_groups ()
1322 {
1323         _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), false));
1324 }
1325
1326 void
1327 Mixer_UI::route_groups_changed ()
1328 {
1329         ENSURE_GUI_THREAD (*this, &Mixer_UI::route_groups_changed);
1330
1331         _in_group_rebuild_or_clear = true;
1332
1333         /* just rebuild the while thing */
1334
1335         group_model->clear ();
1336
1337 #if 0
1338         /* this is currently not used,
1339          * Mixer_UI::group_display_button_press() has a case for it,
1340          * and a commented edit_route_group() but that's n/a since 2011.
1341          *
1342          * This code is left as reminder that
1343          * row[group_columns.group] = 0 has special meaning.
1344          */
1345         {
1346                 TreeModel::Row row;
1347                 row = *(group_model->append());
1348                 row[group_columns.visible] = true;
1349                 row[group_columns.text] = (_("-all-"));
1350                 row[group_columns.group] = 0;
1351         }
1352 #endif
1353
1354         _session->foreach_route_group (sigc::mem_fun (*this, &Mixer_UI::add_route_group));
1355
1356         _group_tabs->set_dirty ();
1357         _in_group_rebuild_or_clear = false;
1358 }
1359
1360 void
1361 Mixer_UI::new_route_group ()
1362 {
1363         RouteList rl;
1364
1365         _group_tabs->run_new_group_dialog (rl);
1366 }
1367
1368 void
1369 Mixer_UI::remove_selected_route_group ()
1370 {
1371         Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
1372         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
1373
1374         if (rows.empty()) {
1375                 return;
1376         }
1377
1378         TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
1379         TreeIter iter;
1380
1381         /* selection mode is single, so rows.begin() is it */
1382
1383         if ((iter = group_model->get_iter (*i))) {
1384
1385                 RouteGroup* rg = (*iter)[group_columns.group];
1386
1387                 if (rg) {
1388                         _session->remove_route_group (*rg);
1389                 }
1390         }
1391 }
1392
1393 void
1394 Mixer_UI::route_group_property_changed (RouteGroup* group, const PropertyChange& change)
1395 {
1396         if (in_group_row_change) {
1397                 return;
1398         }
1399
1400         /* force an update of any mixer strips that are using this group,
1401            otherwise mix group names don't change in mixer strips
1402         */
1403
1404         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
1405                 if ((*i)->route_group() == group) {
1406                         (*i)->route_group_changed();
1407                 }
1408         }
1409
1410         TreeModel::iterator i;
1411         TreeModel::Children rows = group_model->children();
1412         Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
1413
1414         in_group_row_change = true;
1415
1416         for (i = rows.begin(); i != rows.end(); ++i) {
1417                 if ((*i)[group_columns.group] == group) {
1418                         (*i)[group_columns.visible] = !group->is_hidden ();
1419                         (*i)[group_columns.text] = group->name ();
1420                         break;
1421                 }
1422         }
1423
1424         in_group_row_change = false;
1425
1426         if (change.contains (Properties::name)) {
1427                 _group_tabs->set_dirty ();
1428         }
1429
1430         for (list<MixerStrip*>::iterator j = strips.begin(); j != strips.end(); ++j) {
1431                 if ((*j)->route_group() == group) {
1432                         if (group->is_hidden ()) {
1433                                 hide_strip (*j);
1434                         } else {
1435                                 show_strip (*j);
1436                         }
1437                 }
1438         }
1439 }
1440
1441 void
1442 Mixer_UI::show_mixer_list (bool yn)
1443 {
1444         if (yn) {
1445                 list_vpacker.show ();
1446                 
1447                 //if user wants to show the pane, we should make sure that it is wide enough to be visible 
1448                 int width = list_hpane.get_position();
1449                 if (width < 40)
1450                         list_hpane.set_position(40);
1451         } else {
1452                 list_vpacker.hide ();
1453         }
1454         
1455         _show_mixer_list = yn;
1456 }
1457
1458 void
1459 Mixer_UI::show_monitor_section (bool yn)
1460 {
1461         if (!monitor_section()) {
1462                 return;
1463         }
1464         if (monitor_section()->tearoff().torn_off()) {
1465                 return;
1466         }
1467
1468         if (yn) {
1469                 monitor_section()->tearoff().show();
1470         } else {
1471                 monitor_section()->tearoff().hide();
1472         }
1473 }
1474
1475 void
1476 Mixer_UI::route_group_name_edit (const std::string& path, const std::string& new_text)
1477 {
1478         RouteGroup* group;
1479         TreeIter iter;
1480
1481         if ((iter = group_model->get_iter (path))) {
1482
1483                 if ((group = (*iter)[group_columns.group]) == 0) {
1484                         return;
1485                 }
1486
1487                 if (new_text != group->name()) {
1488                         group->set_name (new_text);
1489                 }
1490         }
1491 }
1492
1493 void
1494 Mixer_UI::route_group_row_change (const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator& iter)
1495 {
1496         RouteGroup* group;
1497
1498         if (in_group_row_change) {
1499                 return;
1500         }
1501
1502         if ((group = (*iter)[group_columns.group]) == 0) {
1503                 return;
1504         }
1505
1506         std::string name = (*iter)[group_columns.text];
1507
1508         if (name != group->name()) {
1509                 group->set_name (name);
1510         }
1511
1512         bool hidden = !(*iter)[group_columns.visible];
1513
1514         if (hidden != group->is_hidden ()) {
1515                 group->set_hidden (hidden, this);
1516         }
1517 }
1518
1519 /** Called when a group model row is deleted, but also when the model is
1520  *  reordered by a user drag-and-drop; the latter is what we are
1521  *  interested in here.
1522  */
1523 void
1524 Mixer_UI::route_group_row_deleted (Gtk::TreeModel::Path const &)
1525 {
1526         if (_in_group_rebuild_or_clear) {
1527                 return;
1528         }
1529
1530         /* Re-write the session's route group list so that the new order is preserved */
1531
1532         list<RouteGroup*> new_list;
1533
1534         Gtk::TreeModel::Children children = group_model->children();
1535         for (Gtk::TreeModel::Children::iterator i = children.begin(); i != children.end(); ++i) {
1536                 RouteGroup* g = (*i)[group_columns.group];
1537                 if (g) {
1538                         new_list.push_back (g);
1539                 }
1540         }
1541
1542         _session->reorder_route_groups (new_list);
1543 }
1544
1545
1546 void
1547 Mixer_UI::add_route_group (RouteGroup* group)
1548 {
1549         ENSURE_GUI_THREAD (*this, &Mixer_UI::add_route_group, group)
1550         bool focus = false;
1551
1552         in_group_row_change = true;
1553
1554         TreeModel::Row row = *(group_model->append());
1555         row[group_columns.visible] = !group->is_hidden ();
1556         row[group_columns.group] = group;
1557         if (!group->name().empty()) {
1558                 row[group_columns.text] = group->name();
1559         } else {
1560                 row[group_columns.text] = _("unnamed");
1561                 focus = true;
1562         }
1563
1564         group->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::route_group_property_changed, this, group, _1), gui_context());
1565
1566         if (focus) {
1567                 TreeViewColumn* col = group_display.get_column (0);
1568                 CellRendererText* name_cell = dynamic_cast<CellRendererText*>(group_display.get_column_cell_renderer (0));
1569                 group_display.set_cursor (group_model->get_path (row), *col, *name_cell, true);
1570         }
1571
1572         _group_tabs->set_dirty ();
1573
1574         in_group_row_change = false;
1575 }
1576
1577 bool
1578 Mixer_UI::strip_scroller_button_release (GdkEventButton* ev)
1579 {
1580         using namespace Menu_Helpers;
1581
1582         if (Keyboard::is_context_menu_event (ev)) {
1583                 ARDOUR_UI::instance()->add_route (this);
1584                 return true;
1585         }
1586
1587         return false;
1588 }
1589
1590 void
1591 Mixer_UI::set_strip_width (Width w, bool save)
1592 {
1593         _strip_width = w;
1594
1595         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
1596                 (*i)->set_width_enum (w, save ? (*i)->width_owner() : this);
1597         }
1598 }
1599
1600 void
1601 Mixer_UI::set_window_pos_and_size ()
1602 {
1603         resize (m_width, m_height);
1604         move (m_root_x, m_root_y);
1605 }
1606
1607         void
1608 Mixer_UI::get_window_pos_and_size ()
1609 {
1610         get_position(m_root_x, m_root_y);
1611         get_size(m_width, m_height);
1612 }
1613
1614 int
1615 Mixer_UI::set_state (const XMLNode& node)
1616 {
1617         const XMLProperty* prop;
1618         XMLNode* geometry;
1619
1620         m_width = default_width;
1621         m_height = default_height;
1622         m_root_x = 1;
1623         m_root_y = 1;
1624
1625         if ((geometry = find_named_node (node, "geometry")) != 0) {
1626
1627                 XMLProperty* prop;
1628
1629                 if ((prop = geometry->property("x_size")) == 0) {
1630                         prop = geometry->property ("x-size");
1631                 }
1632                 if (prop) {
1633                         m_width = atoi(prop->value());
1634                 }
1635                 if ((prop = geometry->property("y_size")) == 0) {
1636                         prop = geometry->property ("y-size");
1637                 }
1638                 if (prop) {
1639                         m_height = atoi(prop->value());
1640                 }
1641
1642                 if ((prop = geometry->property ("x_pos")) == 0) {
1643                         prop = geometry->property ("x-pos");
1644                 }
1645                 if (prop) {
1646                         m_root_x = atoi (prop->value());
1647
1648                 }
1649                 if ((prop = geometry->property ("y_pos")) == 0) {
1650                         prop = geometry->property ("y-pos");
1651                 }
1652                 if (prop) {
1653                         m_root_y = atoi (prop->value());
1654                 }
1655         }
1656
1657         set_window_pos_and_size ();
1658
1659         if ((prop = node.property ("narrow-strips"))) {
1660                 if (string_is_affirmative (prop->value())) {
1661                         set_strip_width (Narrow);
1662                 } else {
1663                         set_strip_width (Wide);
1664                 }
1665         }
1666
1667         if ((prop = node.property ("show-mixer"))) {
1668                 if (string_is_affirmative (prop->value())) {
1669                        _visible = true;
1670                 }
1671         }
1672
1673         if ((prop = node.property ("maximised"))) {
1674                 bool yn = string_is_affirmative (prop->value());
1675                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleMaximalMixer"));
1676                 assert (act);
1677                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
1678                 bool fs = tact && tact->get_active();
1679                 if (yn ^ fs) {
1680                         ActionManager::do_action ("Common",
1681                                         "ToggleMaximalMixer");
1682                 }
1683         }
1684
1685         if ((prop = node.property ("show-mixer-list"))) {
1686                 bool yn = string_is_affirmative (prop->value());
1687                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleMixerList"));
1688                 assert (act);
1689                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
1690
1691                 /* do it twice to force the change */
1692                 tact->set_active (!yn);
1693                 tact->set_active (yn);
1694         }
1695
1696
1697         return 0;
1698 }
1699
1700 XMLNode&
1701 Mixer_UI::get_state (void)
1702 {
1703         XMLNode* node = new XMLNode ("Mixer");
1704
1705         if (is_realized()) {
1706                 Glib::RefPtr<Gdk::Window> win = get_window();
1707
1708                 get_window_pos_and_size ();
1709
1710                 XMLNode* geometry = new XMLNode ("geometry");
1711                 char buf[32];
1712                 snprintf(buf, sizeof(buf), "%d", m_width);
1713                 geometry->add_property(X_("x_size"), string(buf));
1714                 snprintf(buf, sizeof(buf), "%d", m_height);
1715                 geometry->add_property(X_("y_size"), string(buf));
1716                 snprintf(buf, sizeof(buf), "%d", m_root_x);
1717                 geometry->add_property(X_("x_pos"), string(buf));
1718                 snprintf(buf, sizeof(buf), "%d", m_root_y);
1719                 geometry->add_property(X_("y_pos"), string(buf));
1720
1721                 // written only for compatibility, they are not used.
1722                 snprintf(buf, sizeof(buf), "%d", 0);
1723                 geometry->add_property(X_("x_off"), string(buf));
1724                 snprintf(buf, sizeof(buf), "%d", 0);
1725                 geometry->add_property(X_("y_off"), string(buf));
1726
1727                 snprintf(buf,sizeof(buf), "%d",gtk_paned_get_position (static_cast<Paned*>(&rhs_pane1)->gobj()));
1728                 geometry->add_property(X_("mixer_rhs_pane1_pos"), string(buf));
1729                 snprintf(buf,sizeof(buf), "%d",gtk_paned_get_position (static_cast<Paned*>(&list_hpane)->gobj()));
1730                 geometry->add_property(X_("mixer_list_hpane_pos"), string(buf));
1731
1732                 node->add_child_nocopy (*geometry);
1733         }
1734
1735         node->add_property ("narrow-strips", _strip_width == Narrow ? "yes" : "no");
1736
1737         node->add_property ("show-mixer", _visible ? "yes" : "no");
1738
1739         node->add_property ("show-mixer-list", _show_mixer_list ? "yes" : "no");
1740
1741         node->add_property ("maximised", _maximised ? "yes" : "no");
1742
1743         return *node;
1744 }
1745
1746
1747 void
1748 Mixer_UI::pane_allocation_handler (Allocation&, Gtk::Paned* which)
1749 {
1750         int pos;
1751         XMLProperty* prop = 0;
1752         char buf[32];
1753         XMLNode* node = ARDOUR_UI::instance()->mixer_settings();
1754         XMLNode* geometry;
1755         int height;
1756         static int32_t done[3] = { 0, 0, 0 };
1757
1758         height = default_height;
1759
1760         if ((geometry = find_named_node (*node, "geometry")) != 0) {
1761
1762                 if ((prop = geometry->property ("y_size")) == 0) {
1763                         prop = geometry->property ("y-size");
1764                 }
1765                 if (prop) {
1766                         height = atoi (prop->value());
1767                 }
1768         }
1769
1770         if (which == static_cast<Gtk::Paned*> (&rhs_pane1)) {
1771
1772                 if (done[0]) {
1773                         return;
1774                 }
1775
1776                 if (!geometry || (prop = geometry->property("mixer-rhs-pane1-pos")) == 0) {
1777                         pos = height / 3;
1778                         snprintf (buf, sizeof(buf), "%d", pos);
1779                 } else {
1780                         pos = atoi (prop->value());
1781                 }
1782
1783                 if ((done[0] = GTK_WIDGET(rhs_pane1.gobj())->allocation.height > pos)) {
1784                         rhs_pane1.set_position (pos);
1785                 }
1786
1787         } else if (which == static_cast<Gtk::Paned*> (&list_hpane)) {
1788
1789                 if (done[2]) {
1790                         return;
1791                 }
1792
1793                 if (!geometry || (prop = geometry->property("mixer-list-hpane-pos")) == 0) {
1794                         pos = 75;
1795                         snprintf (buf, sizeof(buf), "%d", pos);
1796                 } else {
1797                         pos = max (36, atoi (prop->value ()));
1798                 }
1799
1800                 if ((done[2] = GTK_WIDGET(list_hpane.gobj())->allocation.width > pos)) {
1801                         list_hpane.set_position (pos);
1802                 }
1803         }
1804 }
1805 void
1806 Mixer_UI::scroll_left ()
1807 {
1808         if (!scroller.get_hscrollbar()) return;
1809         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
1810         /* stupid GTK: can't rely on clamping across versions */
1811         scroller.get_hscrollbar()->set_value (max (adj->get_lower(), adj->get_value() - adj->get_step_increment()));
1812 }
1813
1814 void
1815 Mixer_UI::scroll_right ()
1816 {
1817         if (!scroller.get_hscrollbar()) return;
1818         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
1819         /* stupid GTK: can't rely on clamping across versions */
1820         scroller.get_hscrollbar()->set_value (min (adj->get_upper(), adj->get_value() + adj->get_step_increment()));
1821 }
1822
1823 bool
1824 Mixer_UI::on_key_press_event (GdkEventKey* ev)
1825 {
1826         /* focus widget gets first shot, then bindings, otherwise
1827            forward to main window
1828         */
1829
1830         if (gtk_window_propagate_key_event (GTK_WINDOW(gobj()), ev)) {
1831                 return true;
1832         }
1833
1834         KeyboardKey k (ev->state, ev->keyval);
1835
1836         if (bindings.activate (k, Bindings::Press)) {
1837                 return true;
1838         }
1839
1840         return forward_key_press (ev);
1841 }
1842
1843 bool
1844 Mixer_UI::on_key_release_event (GdkEventKey* ev)
1845 {
1846         if (gtk_window_propagate_key_event (GTK_WINDOW(gobj()), ev)) {
1847                 return true;
1848         }
1849
1850         KeyboardKey k (ev->state, ev->keyval);
1851
1852         if (bindings.activate (k, Bindings::Release)) {
1853                 return true;
1854         }
1855
1856         /* don't forward releases */
1857
1858         return true;
1859 }
1860
1861 bool
1862 Mixer_UI::on_scroll_event (GdkEventScroll* ev)
1863 {
1864         switch (ev->direction) {
1865         case GDK_SCROLL_LEFT:
1866                 scroll_left ();
1867                 return true;
1868         case GDK_SCROLL_UP:
1869                 if (ev->state & Keyboard::TertiaryModifier) {
1870                         scroll_left ();
1871                         return true;
1872                 }
1873                 return false;
1874
1875         case GDK_SCROLL_RIGHT:
1876                 scroll_right ();
1877                 return true;
1878
1879         case GDK_SCROLL_DOWN:
1880                 if (ev->state & Keyboard::TertiaryModifier) {
1881                         scroll_right ();
1882                         return true;
1883                 }
1884                 return false;
1885         }
1886
1887         return false;
1888 }
1889
1890
1891 void
1892 Mixer_UI::parameter_changed (string const & p)
1893 {
1894         if (p == "show-group-tabs") {
1895                 bool const s = _session->config.get_show_group_tabs ();
1896                 if (s) {
1897                         _group_tabs->show ();
1898                 } else {
1899                         _group_tabs->hide ();
1900                 }
1901         } else if (p == "default-narrow_ms") {
1902                 bool const s = UIConfiguration::instance().get_default_narrow_ms ();
1903                 for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
1904                         (*i)->set_width_enum (s ? Narrow : Wide, this);
1905                 }
1906         } else if (p == "remote-model") {
1907                 reset_remote_control_ids ();
1908         } else if (p == "use-monitor-bus") {
1909                 if (!_session->monitor_out()) {
1910                         monitor_section_detached ();
1911                 }
1912         }
1913 }
1914
1915 void
1916 Mixer_UI::set_route_group_activation (RouteGroup* g, bool a)
1917 {
1918         g->set_active (a, this);
1919 }
1920
1921 PluginSelector*
1922 Mixer_UI::plugin_selector()
1923 {
1924 #ifdef DEFER_PLUGIN_SELECTOR_LOAD
1925         if (!_plugin_selector)
1926                 _plugin_selector = new PluginSelector (PluginManager::instance());
1927 #endif
1928
1929         return _plugin_selector;
1930 }
1931
1932 void
1933 Mixer_UI::setup_track_display ()
1934 {
1935         track_model = ListStore::create (track_columns);
1936         track_display.set_model (track_model);
1937         track_display.append_column (_("Strips"), track_columns.text);
1938         track_display.append_column (_("Show"), track_columns.visible);
1939         track_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
1940         track_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
1941         track_display.get_column (0)->set_expand(true);
1942         track_display.get_column (1)->set_expand(false);
1943         track_display.get_column (0)->set_sizing (Gtk::TREE_VIEW_COLUMN_FIXED);
1944         track_display.set_name (X_("EditGroupList"));
1945         track_display.get_selection()->set_mode (Gtk::SELECTION_NONE);
1946         track_display.set_reorderable (true);
1947         track_display.set_headers_visible (true);
1948         track_display.set_can_focus(false);
1949
1950         track_model->signal_row_deleted().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_delete));
1951         track_model->signal_rows_reordered().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_reorder));
1952
1953         CellRendererToggle* track_list_visible_cell = dynamic_cast<CellRendererToggle*>(track_display.get_column_cell_renderer (1));
1954         track_list_visible_cell->property_activatable() = true;
1955         track_list_visible_cell->property_radio() = false;
1956         track_list_visible_cell->signal_toggled().connect (sigc::mem_fun (*this, &Mixer_UI::track_visibility_changed));
1957
1958         track_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::track_display_button_press), false);
1959
1960         track_display_scroller.add (track_display);
1961         track_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
1962
1963         VBox* v = manage (new VBox);
1964         v->show ();
1965         v->pack_start (track_display_scroller, true, true);
1966
1967         Button* b = manage (new Button);
1968         b->show ();
1969         Widget* w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
1970         w->show ();
1971         b->add (*w);
1972
1973         b->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::new_track_or_bus));
1974
1975         v->pack_start (*b, false, false);
1976
1977         track_display_frame.set_name("BaseFrame");
1978         track_display_frame.set_shadow_type (Gtk::SHADOW_IN);
1979         track_display_frame.add (*v);
1980
1981         track_display_scroller.show();
1982         track_display_frame.show();
1983         track_display.show();
1984 }
1985
1986 void
1987 Mixer_UI::new_track_or_bus ()
1988 {
1989         ARDOUR_UI::instance()->add_route (this);
1990 }
1991
1992
1993 void
1994 Mixer_UI::update_title ()
1995 {
1996         if (_session) {
1997                 string n;
1998
1999                 if (_session->snap_name() != _session->name()) {
2000                         n = _session->snap_name ();
2001                 } else {
2002                         n = _session->name ();
2003                 }
2004
2005                 if (_session->dirty ()) {
2006                         n = "*" + n;
2007                 }
2008
2009                 WindowTitle title (n);
2010                 title += S_("Window|Mixer");
2011                 title += Glib::get_application_name ();
2012                 set_title (title.get_string());
2013
2014         } else {
2015
2016                 WindowTitle title (S_("Window|Mixer"));
2017                 title += Glib::get_application_name ();
2018                 set_title (title.get_string());
2019         }
2020 }
2021
2022 MixerStrip*
2023 Mixer_UI::strip_by_x (int x)
2024 {
2025         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2026                 int x1, x2, y;
2027
2028                 (*i)->translate_coordinates (*this, 0, 0, x1, y);
2029                 x2 = x1 + (*i)->get_width();
2030
2031                 if (x >= x1 && x <= x2) {
2032                         return (*i);
2033                 }
2034         }
2035
2036         return 0;
2037 }
2038
2039 void
2040 Mixer_UI::set_route_targets_for_operation ()
2041 {
2042         _route_targets.clear ();
2043
2044         if (!_selection.empty()) {
2045                 _route_targets = _selection.routes;
2046                 return;
2047         }
2048
2049 //  removed "implicit" selections of strips, after discussion on IRC
2050
2051 }
2052
2053 void
2054 Mixer_UI::monitor_section_going_away ()
2055 {
2056         if (_monitor_section) {
2057                 monitor_section_detached ();
2058                 out_packer.remove (_monitor_section->tearoff());
2059                 _monitor_section->set_session (0);
2060         }
2061 }
2062
2063 void
2064 Mixer_UI::toggle_midi_input_active (bool flip_others)
2065 {
2066         boost::shared_ptr<RouteList> rl (new RouteList);
2067         bool onoff = false;
2068
2069         set_route_targets_for_operation ();
2070
2071         for (RouteUISelection::iterator r = _route_targets.begin(); r != _route_targets.end(); ++r) {
2072                 boost::shared_ptr<MidiTrack> mt = (*r)->midi_track();
2073
2074                 if (mt) {
2075                         rl->push_back ((*r)->route());
2076                         onoff = !mt->input_active();
2077                 }
2078         }
2079
2080         _session->set_exclusive_input_active (rl, onoff, flip_others);
2081 }
2082
2083 void
2084 Mixer_UI::maximise_mixer_space ()
2085 {
2086         if (_maximised) {
2087                 return;
2088         }
2089
2090         fullscreen ();
2091
2092         _maximised = true;
2093 }
2094
2095 void
2096 Mixer_UI::restore_mixer_space ()
2097 {
2098         if (!_maximised) {
2099                 return;
2100         }
2101
2102         unfullscreen();
2103
2104         _maximised = false;
2105 }
2106
2107 void
2108 Mixer_UI::monitor_section_attached ()
2109 {
2110         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2111         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2112         act->set_sensitive (true);
2113         tact->set_active ();
2114 }
2115
2116 void
2117 Mixer_UI::monitor_section_detached ()
2118 {
2119         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2120         act->set_sensitive (false);
2121 }