fix compiler warning
[ardour.git] / gtk2_ardour / mixer_ui.cc
index 216c005178b35d9217254b837395ded8e76fe803..18ad28d7744258d8100af4c98e622244b15d0aaa 100644 (file)
 #include <gtkmm/accelmap.h>
 
 #include "pbd/convert.h"
-#include "pbd/stacktrace.h"
-#include <glibmm/thread.h>
+#include "pbd/unwind.h"
+
+#include <glibmm/threads.h>
 
 #include <gtkmm2ext/gtk_ui.h>
 #include <gtkmm2ext/utils.h>
 #include <gtkmm2ext/tearoff.h>
 #include <gtkmm2ext/window_title.h>
 
-#include "ardour/audio_track.h"
+#include "ardour/debug.h"
+#include "ardour/midi_track.h"
 #include "ardour/plugin_manager.h"
 #include "ardour/route_group.h"
 #include "ardour/session.h"
-#include "ardour/session_route.h"
 
 #include "keyboard.h"
 #include "mixer_ui.h"
@@ -51,6 +52,7 @@
 #include "ardour_ui.h"
 #include "prompter.h"
 #include "utils.h"
+#include "route_sorter.h"
 #include "actions.h"
 #include "gui_thread.h"
 #include "mixer_group_tabs.h"
@@ -65,6 +67,7 @@ using namespace Gtkmm2ext;
 using namespace std;
 
 using PBD::atoi;
+using PBD::Unwinder;
 
 Mixer_UI* Mixer_UI::_instance = 0;
 
@@ -80,22 +83,19 @@ Mixer_UI::instance ()
 
 Mixer_UI::Mixer_UI ()
        : Window (Gtk::WINDOW_TOPLEVEL)
+       , _visible (false)
+       , no_track_list_redisplay (false)
+       , in_group_row_change (false)
+       , track_menu (0)
+       , _monitor_section (0)
+       , _strip_width (Config->get_default_narrow_ms() ? Narrow : Wide)
+       , ignore_reorder (false)
        , _following_editor_selection (false)
 {
        /* allow this window to become the key focus window */
        set_flags (CAN_FOCUS);
 
-       _strip_width = Config->get_default_narrow_ms() ? Narrow : Wide;
-       track_menu = 0;
-        _monitor_section = 0;
-       no_track_list_redisplay = false;
-       in_group_row_change = false;
-       _visible = false;
-       strip_redisplay_does_not_reset_order_keys = false;
-       strip_redisplay_does_not_sync_order_keys = false;
-       ignore_sync = false;
-
-       Route::SyncOrderKeys.connect (*this, invalidator (*this), ui_bind (&Mixer_UI::sync_order_keys, this, _1), gui_context());
+       Route::SyncOrderKeys.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::sync_treeview_from_order_keys, this, _1), gui_context());
 
        scroller_base.set_flags (Gtk::CAN_FOCUS);
        scroller_base.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
@@ -123,7 +123,7 @@ Mixer_UI::Mixer_UI ()
        group_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
        group_display.get_column (0)->set_expand(true);
        group_display.get_column (1)->set_expand(false);
-       group_display.set_name ("MixerGroupList");
+       group_display.set_name ("LHSList");
        group_display.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
        group_display.set_reorderable (true);
        group_display.set_headers_visible (true);
@@ -235,11 +235,9 @@ Mixer_UI::Mixer_UI ()
        list_hpane.show();
        group_display.show();
 
-       auto_rebinding = FALSE;
-
        _in_group_rebuild_or_clear = false;
 
-       MixerStrip::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&Mixer_UI::remove_strip, this, _1), gui_context());
+       MixerStrip::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::remove_strip, this, _1), gui_context());
 
         MonitorSection::setup_knob_images ();
 
@@ -250,6 +248,9 @@ Mixer_UI::Mixer_UI ()
 
 Mixer_UI::~Mixer_UI ()
 {
+       if (_monitor_section) {
+               delete _monitor_section;
+       }
 }
 
 void
@@ -306,74 +307,68 @@ Mixer_UI::hide_window (GdkEventAny *ev)
 
 
 void
-Mixer_UI::add_strip (RouteList& routes)
+Mixer_UI::add_strips (RouteList& routes)
 {
-       ENSURE_GUI_THREAD (*this, &Mixer_UI::add_strip, routes)
-
        MixerStrip* strip;
 
-       no_track_list_redisplay = true;
-       strip_redisplay_does_not_sync_order_keys = true;
-
-       for (RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
-               boost::shared_ptr<Route> route = (*x);
-
-               if (route->is_hidden()) {
-                       continue;
-               }
-
-                if (route->is_monitor()) {
-                        if (!_monitor_section) {
-                                _monitor_section = new MonitorSection (_session);
-                                out_packer.pack_end (_monitor_section->tearoff(), false, false);
-                        } else {
-                                _monitor_section->set_session (_session);
-                        }
-
-                        _monitor_section->tearoff().show_all ();
-
-                        XMLNode* mnode = ARDOUR_UI::instance()->tearoff_settings (X_("monitor-section"));
-                        if (mnode) {
-                                _monitor_section->tearoff().set_state (*mnode);
-                        }
-
-                        /* no regular strip shown for control out */
-
-                        continue;
-                }
-
-               strip = new MixerStrip (*this, _session, route);
-               strips.push_back (strip);
-
-               Config->get_default_narrow_ms() ? _strip_width = Narrow : _strip_width = Wide;
-
-               if (strip->width_owner() != strip) {
-                       strip->set_width_enum (_strip_width, this);
-               }
-
-               show_strip (strip);
-
-               TreeModel::Row row = *(track_model->append());
-               row[track_columns.text] = route->name();
-               row[track_columns.visible] = strip->route()->is_master() ? true : strip->marked_for_display();
-               row[track_columns.route] = route;
-               row[track_columns.strip] = strip;
-
-               if (route->order_key (N_("signal")) == -1) {
-                       route->set_order_key (N_("signal"), track_model->children().size()-1);
+       {
+               Unwinder<bool> uw (no_track_list_redisplay, true);
+               
+               for (RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
+                       boost::shared_ptr<Route> route = (*x);
+                       
+                       if (route->is_hidden()) {
+                               continue;
+                       }
+                       
+                       if (route->is_monitor()) {
+                               
+                               if (!_monitor_section) {
+                                       _monitor_section = new MonitorSection (_session);
+                                       
+                                       XMLNode* mnode = ARDOUR_UI::instance()->tearoff_settings (X_("monitor-section"));
+                                       if (mnode) {
+                                               _monitor_section->tearoff().set_state (*mnode);
+                                       }
+                               } 
+                               
+                               out_packer.pack_end (_monitor_section->tearoff(), false, false);
+                               _monitor_section->set_session (_session);
+                               _monitor_section->tearoff().show_all ();
+                               
+                               route->DropReferences.connect (*this, invalidator(*this), boost::bind (&Mixer_UI::monitor_section_going_away, this), gui_context());
+                               
+                               /* no regular strip shown for control out */
+                               
+                               continue;
+                       }
+                       
+                       strip = new MixerStrip (*this, _session, route);
+                       strips.push_back (strip);
+                       
+                       Config->get_default_narrow_ms() ? _strip_width = Narrow : _strip_width = Wide;
+                       
+                       if (strip->width_owner() != strip) {
+                               strip->set_width_enum (_strip_width, this);
+                       }
+                       
+                       show_strip (strip);
+                       
+                       TreeModel::Row row = *(track_model->append());
+                       row[track_columns.text] = route->name();
+                       row[track_columns.visible] = strip->route()->is_master() ? true : strip->marked_for_display();
+                       row[track_columns.route] = route;
+                       row[track_columns.strip] = strip;
+                       
+                       route->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::strip_property_changed, this, _1, strip), gui_context());
+                       
+                       strip->WidthChanged.connect (sigc::mem_fun(*this, &Mixer_UI::strip_width_changed));
+                       strip->signal_button_release_event().connect (sigc::bind (sigc::mem_fun(*this, &Mixer_UI::strip_button_release_event), strip));
                }
-
-               route->PropertyChanged.connect (*this, invalidator (*this), ui_bind (&Mixer_UI::strip_property_changed, this, _1, strip), gui_context());
-
-               strip->WidthChanged.connect (sigc::mem_fun(*this, &Mixer_UI::strip_width_changed));
-               strip->signal_button_release_event().connect (sigc::bind (sigc::mem_fun(*this, &Mixer_UI::strip_button_release_event), strip));
        }
 
-       no_track_list_redisplay = false;
-
+       sync_order_keys_from_treeview ();
        redisplay_track_list ();
-
-       strip_redisplay_does_not_sync_order_keys = false;
 }
 
 void
@@ -384,72 +379,204 @@ Mixer_UI::remove_strip (MixerStrip* strip)
                return;
        }
 
-       ENSURE_GUI_THREAD (*this, &Mixer_UI::remove_strip, strip);
-
        TreeModel::Children rows = track_model->children();
        TreeModel::Children::iterator ri;
        list<MixerStrip *>::iterator i;
-
+       
        if ((i = find (strips.begin(), strips.end(), strip)) != strips.end()) {
                strips.erase (i);
        }
-
-       strip_redisplay_does_not_sync_order_keys = true;
-
+       
        for (ri = rows.begin(); ri != rows.end(); ++ri) {
                if ((*ri)[track_columns.strip] == strip) {
                        track_model->erase (ri);
                        break;
                }
        }
-
-       strip_redisplay_does_not_sync_order_keys = false;
 }
 
 void
-Mixer_UI::sync_order_keys (string const & src)
+Mixer_UI::reset_remote_control_ids ()
 {
+       if (Config->get_remote_model() != MixerOrdered || !_session || _session->deletion_in_progress()) {
+               return;
+       }
+
        TreeModel::Children rows = track_model->children();
+       
+       if (rows.empty()) {
+               return;
+       }
+
+       DEBUG_TRACE (DEBUG::OrderKeys, "mixer resets remote control ids after remote model change\n");
+
        TreeModel::Children::iterator ri;
+       bool rid_change = false;
+       uint32_t rid = 1;
+       uint32_t invisible_key = UINT32_MAX;
+
+       for (ri = rows.begin(); ri != rows.end(); ++ri) {
+               boost::shared_ptr<Route> route = (*ri)[track_columns.route];
+               bool visible = (*ri)[track_columns.visible];
+
+               if (!route->is_master() && !route->is_monitor()) {
+                       
+                       uint32_t new_rid = (visible ? rid : invisible_key--);
+                       
+                       if (new_rid != route->remote_control_id()) {
+                               route->set_remote_control_id_from_order_key (MixerSort, new_rid);       
+                               rid_change = true;
+                       }
+                       
+                       if (visible) {
+                               rid++;
+                       }
+               }
+       }
+
+       if (rid_change) {
+               /* tell the world that we changed the remote control IDs */
+               _session->notify_remote_id_change ();
+       }
+}
+
+void
+Mixer_UI::sync_order_keys_from_treeview ()
+{
+       if (ignore_reorder || !_session || _session->deletion_in_progress()) {
+               return;
+       }
 
-       if (src == N_("signal") || !_session || (_session->state_of_the_state() & (Session::Loading|Session::Deletion)) || rows.empty()) {
+       TreeModel::Children rows = track_model->children();
+       
+       if (rows.empty()) {
                return;
        }
 
-       std::map<int,int> keys;
+       DEBUG_TRACE (DEBUG::OrderKeys, "mixer sync order keys from model\n");
 
+       TreeModel::Children::iterator ri;
        bool changed = false;
+       bool rid_change = false;
+       uint32_t order = 0;
+       uint32_t rid = 1;
+       uint32_t invisible_key = UINT32_MAX;
 
-       unsigned order = 0;
-       for (ri = rows.begin(); ri != rows.end(); ++ri, ++order) {
+       for (ri = rows.begin(); ri != rows.end(); ++ri) {
                boost::shared_ptr<Route> route = (*ri)[track_columns.route];
-               unsigned int old_key = order;
-               unsigned int new_key = route->order_key (N_("signal"));
+               bool visible = (*ri)[track_columns.visible];
 
-               keys[new_key] = old_key;
+               uint32_t old_key = route->order_key (MixerSort);
 
-               if (new_key != old_key) {
+               if (order != old_key) {
+                       route->set_order_key (MixerSort, order);
                        changed = true;
                }
+
+               if ((Config->get_remote_model() == MixerOrdered) && !route->is_master() && !route->is_monitor()) {
+
+                       uint32_t new_rid = (visible ? rid : invisible_key--);
+
+                       if (new_rid != route->remote_control_id()) {
+                               route->set_remote_control_id_from_order_key (MixerSort, new_rid);       
+                               rid_change = true;
+                       }
+                       
+                       if (visible) {
+                               rid++;
+                       }
+
+               }
+
+               ++order;
+       }
+
+       if (changed) {
+               /* tell everyone that we changed the mixer sort keys */
+               _session->sync_order_keys (MixerSort);
+       }
+
+       if (rid_change) {
+               /* tell the world that we changed the remote control IDs */
+               _session->notify_remote_id_change ();
+       }
+}
+
+void
+Mixer_UI::sync_treeview_from_order_keys (RouteSortOrderKey src)
+{
+       if (!_session || _session->deletion_in_progress()) {
+               return;
        }
 
-       if (keys.size() != rows.size()) {
-               PBD::stacktrace (cerr, 20);
+       DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("mixer sync model from order keys, src = %1\n", enum_2_string (src)));
+
+       if (src == EditorSort) {
+
+               if (!Config->get_sync_all_route_ordering()) {
+                       /* editor sort keys changed - we don't care */
+                       return;
+               }
+
+               DEBUG_TRACE (DEBUG::OrderKeys, "reset mixer order key to match editor\n");
+
+               /* editor sort keys were changed, update the mixer sort
+                * keys since "sync mixer+editor order" is enabled.
+                */
+
+               boost::shared_ptr<RouteList> r = _session->get_routes ();
+               
+               for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+                       (*i)->sync_order_keys (src);
+               }
        }
-       assert(keys.size() == rows.size());
 
-       // Remove any gaps in keys caused by automation children tracks
+       /* we could get here after either a change in the Mixer or Editor sort
+        * order, but either way, the mixer order keys reflect the intended
+        * order for the GUI, so reorder the treeview model to match it.
+        */
+
        vector<int> neworder;
-       for (std::map<int,int>::const_iterator i = keys.begin(); i != keys.end(); ++i) {
-               neworder.push_back(i->second);
+       TreeModel::Children rows = track_model->children();
+       uint32_t old_order = 0;
+       bool changed = false;
+
+       if (rows.empty()) {
+               return;
+       }
+
+       OrderKeySortedRoutes sorted_routes;
+
+       for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri, ++old_order) {
+               boost::shared_ptr<Route> route = (*ri)[track_columns.route];
+               sorted_routes.push_back (RoutePlusOrderKey (route, old_order, route->order_key (MixerSort)));
+       }
+
+       SortByNewDisplayOrder cmp;
+
+       sort (sorted_routes.begin(), sorted_routes.end(), cmp);
+       neworder.assign (sorted_routes.size(), 0);
+
+       uint32_t n = 0;
+       
+       for (OrderKeySortedRoutes::iterator sr = sorted_routes.begin(); sr != sorted_routes.end(); ++sr, ++n) {
+
+               neworder[n] = sr->old_display_order;
+
+               if (sr->old_display_order != n) {
+                       changed = true;
+               }
+
+               DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("MIXER change order for %1 from %2 to %3\n",
+                                                              sr->route->name(), sr->old_display_order, n));
        }
-       assert(neworder.size() == rows.size());
 
        if (changed) {
-               strip_redisplay_does_not_reset_order_keys = true;
+               Unwinder<bool> uw (ignore_reorder, true);
                track_model->reorder (neworder);
-               strip_redisplay_does_not_reset_order_keys = false;
        }
+
+       redisplay_track_list ();
 }
 
 void
@@ -580,20 +707,26 @@ Mixer_UI::set_session (Session* sess)
 
        initial_track_display ();
 
-       _session->RouteAdded.connect (_session_connections, invalidator (*this), ui_bind (&Mixer_UI::add_strip, this, _1), gui_context());
-       _session->route_group_added.connect (_session_connections, invalidator (*this), ui_bind (&Mixer_UI::add_route_group, this, _1), gui_context());
+       _session->RouteAdded.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_strips, this, _1), gui_context());
+       _session->route_group_added.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_route_group, this, _1), gui_context());
        _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::route_groups_changed, this), gui_context());
        _session->route_groups_reordered.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::route_groups_changed, this), gui_context());
-       _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), ui_bind (&Mixer_UI::parameter_changed, this, _1), gui_context());
+       _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::parameter_changed, this, _1), gui_context());
        _session->DirtyChanged.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::update_title, this), gui_context());
-       _session->StateSaved.connect (_session_connections, invalidator (*this), ui_bind (&Mixer_UI::update_title, this), gui_context());
+       _session->StateSaved.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::update_title, this), gui_context());
 
-       Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&Mixer_UI::parameter_changed, this, _1), gui_context ());
+       Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::parameter_changed, this, _1), gui_context ());
 
        route_groups_changed ();
 
        if (_visible) {
                show_window();
+
+               /* Bit of a hack; if we're here, we're opening the mixer because of our
+                  instant XML state having a show-mixer property.  Fix up the corresponding
+                  action state.
+               */
+               ActionManager::check_toggleaction ("<Actions>/Common/toggle-mixer");
        }
 
        start_updating ();
@@ -629,6 +762,50 @@ Mixer_UI::session_going_away ()
        update_title ();
 }
 
+void
+Mixer_UI::track_visibility_changed (std::string const & path)
+{
+       if (_session && _session->deletion_in_progress()) {
+               return;
+       }
+
+       TreeIter iter;
+
+       if ((iter = track_model->get_iter (path))) {
+               MixerStrip* strip = (*iter)[track_columns.strip];
+               if (strip) {
+                       bool visible = (*iter)[track_columns.visible];
+
+                       if (strip->set_marked_for_display (!visible)) {
+                               update_track_visibility ();
+                       }
+               }
+       }
+}
+
+void
+Mixer_UI::update_track_visibility ()
+{
+       TreeModel::Children rows = track_model->children();
+       TreeModel::Children::iterator i;
+
+       {
+               Unwinder<bool> uw (no_track_list_redisplay, true);
+               
+               for (i = rows.begin(); i != rows.end(); ++i) {
+                       MixerStrip *strip = (*i)[track_columns.strip];
+                       (*i)[track_columns.visible] = strip->marked_for_display ();
+               }
+               
+               /* force route order keys catch up with visibility changes
+                */
+               
+               sync_order_keys_from_treeview ();
+       }
+
+       redisplay_track_list ();
+}
+
 void
 Mixer_UI::show_strip (MixerStrip* ms)
 {
@@ -640,6 +817,7 @@ Mixer_UI::show_strip (MixerStrip* ms)
                MixerStrip* strip = (*i)[track_columns.strip];
                if (strip == ms) {
                        (*i)[track_columns.visible] = true;
+                       redisplay_track_list ();
                        break;
                }
        }
@@ -656,6 +834,7 @@ Mixer_UI::hide_strip (MixerStrip* ms)
                MixerStrip* strip = (*i)[track_columns.strip];
                if (strip == ms) {
                        (*i)[track_columns.visible] = false;
+                       redisplay_track_list ();
                        break;
                }
        }
@@ -691,25 +870,26 @@ Mixer_UI::set_all_strips_visibility (bool yn)
        TreeModel::Children rows = track_model->children();
        TreeModel::Children::iterator i;
 
-       no_track_list_redisplay = true;
-
-       for (i = rows.begin(); i != rows.end(); ++i) {
-
-               TreeModel::Row row = (*i);
-               MixerStrip* strip = row[track_columns.strip];
-
-               if (strip == 0) {
-                       continue;
-               }
-
-               if (strip->route()->is_master() || strip->route()->is_monitor()) {
-                       continue;
+       {
+               Unwinder<bool> uw (no_track_list_redisplay, true);
+               
+               for (i = rows.begin(); i != rows.end(); ++i) {
+                       
+                       TreeModel::Row row = (*i);
+                       MixerStrip* strip = row[track_columns.strip];
+                       
+                       if (strip == 0) {
+                               continue;
+                       }
+                       
+                       if (strip->route()->is_master() || strip->route()->is_monitor()) {
+                               continue;
+                       }
+                       
+                       (*i)[track_columns.visible] = yn;
                }
-
-               (*i)[track_columns.visible] = yn;
        }
 
-       no_track_list_redisplay = false;
        redisplay_track_list ();
 }
 
@@ -720,42 +900,43 @@ Mixer_UI::set_all_audio_visibility (int tracks, bool yn)
        TreeModel::Children rows = track_model->children();
        TreeModel::Children::iterator i;
 
-       no_track_list_redisplay = true;
-
-       for (i = rows.begin(); i != rows.end(); ++i) {
-               TreeModel::Row row = (*i);
-               MixerStrip* strip = row[track_columns.strip];
-
-               if (strip == 0) {
-                       continue;
-               }
-
-               if (strip->route()->is_master() || strip->route()->is_monitor()) {
-                       continue;
-               }
-
-               boost::shared_ptr<AudioTrack> at = strip->audio_track();
-
-               switch (tracks) {
-               case 0:
-                       (*i)[track_columns.visible] = yn;
-                       break;
-
-               case 1:
-                       if (at) { /* track */
-                               (*i)[track_columns.visible] = yn;
+       {
+               Unwinder<bool> uw (no_track_list_redisplay, true);
+               
+               for (i = rows.begin(); i != rows.end(); ++i) {
+                       TreeModel::Row row = (*i);
+                       MixerStrip* strip = row[track_columns.strip];
+                       
+                       if (strip == 0) {
+                               continue;
                        }
-                       break;
-
-               case 2:
-                       if (!at) { /* bus */
+                       
+                       if (strip->route()->is_master() || strip->route()->is_monitor()) {
+                               continue;
+                       }
+                       
+                       boost::shared_ptr<AudioTrack> at = strip->audio_track();
+                       
+                       switch (tracks) {
+                       case 0:
                                (*i)[track_columns.visible] = yn;
+                               break;
+                               
+                       case 1:
+                               if (at) { /* track */
+                                       (*i)[track_columns.visible] = yn;
+                               }
+                               break;
+                               
+                       case 2:
+                               if (!at) { /* bus */
+                                       (*i)[track_columns.visible] = yn;
+                               }
+                               break;
                        }
-                       break;
                }
        }
 
-       no_track_list_redisplay = false;
        redisplay_track_list ();
 }
 
@@ -796,30 +977,18 @@ Mixer_UI::hide_all_audiotracks ()
 void
 Mixer_UI::track_list_reorder (const TreeModel::Path&, const TreeModel::iterator&, int* /*new_order*/)
 {
-       strip_redisplay_does_not_sync_order_keys = true;
-       _session->set_remote_control_ids();
-       redisplay_track_list ();
-       strip_redisplay_does_not_sync_order_keys = false;
-}
-
-void
-Mixer_UI::track_list_change (const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator&)
-{
-       // never reset order keys because of a property change
-       strip_redisplay_does_not_reset_order_keys = true;
-       _session->set_remote_control_ids();
-       redisplay_track_list ();
-       strip_redisplay_does_not_reset_order_keys = false;
+       DEBUG_TRACE (DEBUG::OrderKeys, "mixer UI treeview reordered\n");
+       sync_order_keys_from_treeview ();
 }
 
 void
 Mixer_UI::track_list_delete (const Gtk::TreeModel::Path&)
 {
-       /* this could require an order sync */
-       if (_session && !_session->deletion_in_progress()) {
-               _session->set_remote_control_ids();
-               redisplay_track_list ();
-       }
+       /* this happens as the second step of a DnD within the treeview as well
+          as when a row/route is actually deleted.
+       */
+       DEBUG_TRACE (DEBUG::OrderKeys, "mixer UI treeview row deleted\n");
+       sync_order_keys_from_treeview ();
 }
 
 void
@@ -827,13 +996,13 @@ Mixer_UI::redisplay_track_list ()
 {
        TreeModel::Children rows = track_model->children();
        TreeModel::Children::iterator i;
-       long order;
-
+       
        if (no_track_list_redisplay) {
                return;
        }
 
-       for (order = 0, i = rows.begin(); i != rows.end(); ++i, ++order) {
+       for (i = rows.begin(); i != rows.end(); ++i) {
+
                MixerStrip* strip = (*i)[track_columns.strip];
 
                if (strip == 0) {
@@ -841,10 +1010,6 @@ Mixer_UI::redisplay_track_list ()
                        continue;
                }
 
-               if (!strip_redisplay_does_not_reset_order_keys) {
-                       strip->route()->set_order_key (N_("signal"), order);
-               }
-
                bool const visible = (*i)[track_columns.visible];
 
                if (visible) {
@@ -854,6 +1019,7 @@ Mixer_UI::redisplay_track_list ()
 
                                if (strip->route()->is_master() || strip->route()->is_monitor()) {
                                        out_packer.reorder_child (*strip, -1);
+
                                } else {
                                        strip_packer.reorder_child (*strip, -1); /* put at end */
                                }
@@ -883,16 +1049,6 @@ Mixer_UI::redisplay_track_list ()
                }
        }
 
-       if (!strip_redisplay_does_not_reset_order_keys && !strip_redisplay_does_not_sync_order_keys) {
-               _session->sync_order_keys (N_("signal"));
-       }
-
-       // Resigc::bind all of the midi controls automatically
-
-       if (auto_rebinding) {
-               auto_rebind_midi_controls ();
-       }
-
        _group_tabs->set_dirty ();
 }
 
@@ -923,92 +1079,19 @@ Mixer_UI::strip_width_changed ()
 
 }
 
-void
-Mixer_UI::set_auto_rebinding( bool val )
-{
-       if( val == TRUE )
-       {
-               auto_rebinding = TRUE;
-               Session::AutoBindingOff();
-       }
-       else
-       {
-               auto_rebinding = FALSE;
-               Session::AutoBindingOn();
-       }
-}
-
-void
-Mixer_UI::toggle_auto_rebinding()
-{
-       if (auto_rebinding)
-       {
-               set_auto_rebinding( FALSE );
-       }
-
-       else
-       {
-               set_auto_rebinding( TRUE );
-       }
-
-       auto_rebind_midi_controls();
-}
-
-void
-Mixer_UI::auto_rebind_midi_controls ()
-{
-       TreeModel::Children rows = track_model->children();
-       TreeModel::Children::iterator i;
-       int pos;
-
-       // Create bindings for all visible strips and remove those that are not visible
-       pos = 1;  // 0 is reserved for the master strip
-       for (i = rows.begin(); i != rows.end(); ++i) {
-               MixerStrip* strip = (*i)[track_columns.strip];
-
-               if ( (*i)[track_columns.visible] == true ) {  // add bindings for
-                       // make the actual binding
-                       //cout<<"Auto Binding:  Visible Strip Found: "<<strip->name()<<endl;
-
-                       int controlValue = pos;
-                       if( strip->route()->is_master() ) {
-                               controlValue = 0;
-                       }
-                       else {
-                               pos++;
-                       }
-
-                       PBD::Controllable::CreateBinding ( strip->solo_button->get_controllable().get(), controlValue, 0);
-                       PBD::Controllable::CreateBinding ( strip->mute_button->get_controllable().get(), controlValue, 1);
-
-                       if( strip->is_audio_track() ) {
-                               PBD::Controllable::CreateBinding ( strip->rec_enable_button->get_controllable().get(), controlValue, 2);
-                       }
-
-                       PBD::Controllable::CreateBinding ( strip->gpm.get_controllable().get(), controlValue, 3);
-                       PBD::Controllable::CreateBinding ( strip->panners.get_controllable().get(), controlValue, 4);
-
-               }
-               else {  // Remove any existing binding
-                       PBD::Controllable::DeleteBinding ( strip->solo_button->get_controllable().get() );
-                       PBD::Controllable::DeleteBinding ( strip->mute_button->get_controllable().get() );
-
-                       if( strip->is_audio_track() ) {
-                               PBD::Controllable::DeleteBinding ( strip->rec_enable_button->get_controllable().get() );
-                       }
-
-                       PBD::Controllable::DeleteBinding ( strip->gpm.get_controllable().get() );
-                       PBD::Controllable::DeleteBinding ( strip->panners.get_controllable().get() ); // This only takes the first panner if there are multiples...
-               }
-
-       } // for
-
-}
-
 struct SignalOrderRouteSorter {
     bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
-           /* use of ">" forces the correct sort order */
-           return a->order_key (N_("signal")) < b->order_key (N_("signal"));
+           if (a->is_master() || a->is_monitor()) {
+                   /* "a" is a special route (master, monitor, etc), and comes
+                    * last in the mixer ordering
+                    */
+                   return false;
+           } else if (b->is_master() || b->is_monitor()) {
+                   /* everything comes before b */
+                   return true;
+           }
+           return a->order_key (MixerSort) < b->order_key (MixerSort);
+
     }
 };
 
@@ -1021,13 +1104,15 @@ Mixer_UI::initial_track_display ()
 
        copy.sort (sorter);
 
-       no_track_list_redisplay = true;
-
-       track_model->clear ();
-
-       add_strip (copy);
+       {
+               Unwinder<bool> uw1 (no_track_list_redisplay, true);
+               Unwinder<bool> uw2 (ignore_reorder, true);
 
-       no_track_list_redisplay = false;
+               track_model->clear ();
+               add_strips (copy);
+       }
+       
+       _session->sync_order_keys (MixerSort);
 
        redisplay_track_list ();
 }
@@ -1050,46 +1135,9 @@ Mixer_UI::track_display_button_press (GdkEventButton* ev)
                return true;
        }
 
-       TreeIter iter;
-       TreeModel::Path path;
-       TreeViewColumn* column;
-       int cellx;
-       int celly;
-
-       if (!track_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
-               return false;
-       }
-
-       switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
-       case 0:
-               /* allow normal processing to occur */
-               return false;
-
-       case 1: /* visibility */
-
-               if ((iter = track_model->get_iter (path))) {
-                       MixerStrip* strip = (*iter)[track_columns.strip];
-                       if (strip) {
-
-                               if (!strip->route()->is_master() && !strip->route()->is_monitor()) {
-                                       bool visible = (*iter)[track_columns.visible];
-                                       (*iter)[track_columns.visible] = !visible;
-                               }
-#ifdef GTKOSX
-                               track_display.queue_draw();
-#endif
-                       }
-               }
-               return true;
-
-       default:
-               break;
-       }
-
        return false;
 }
 
-
 void
 Mixer_UI::build_track_menu ()
 {
@@ -1392,7 +1440,7 @@ Mixer_UI::add_route_group (RouteGroup* group)
                focus = true;
        }
 
-       group->PropertyChanged.connect (*this, invalidator (*this), ui_bind (&Mixer_UI::route_group_property_changed, this, group, _1), gui_context());
+       group->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::route_group_property_changed, this, group, _1), gui_context());
 
        if (focus) {
                TreeViewColumn* col = group_display.get_column (0);
@@ -1631,13 +1679,13 @@ Mixer_UI::on_key_press_event (GdkEventKey* ev)
        if (gtk_window_propagate_key_event (GTK_WINDOW(gobj()), ev)) {
                return true;
        }
-       
+
        KeyboardKey k (ev->state, ev->keyval);
-       
+
        if (bindings.activate (k, Bindings::Press)) {
                return true;
        }
-               
+
         return forward_key_press (ev);
 }
 
@@ -1704,6 +1752,8 @@ Mixer_UI::parameter_changed (string const & p)
                for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
                        (*i)->set_width_enum (s ? Narrow : Wide, this);
                }
+       } else if (p == "remote-model") {
+               reset_remote_control_ids ();
        }
 }
 
@@ -1735,18 +1785,18 @@ Mixer_UI::setup_track_display ()
        track_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
        track_display.get_column (0)->set_expand(true);
        track_display.get_column (1)->set_expand(false);
-       track_display.set_name (X_("MixerTrackDisplayList"));
+       track_display.set_name (X_("LHSList"));
        track_display.get_selection()->set_mode (Gtk::SELECTION_NONE);
        track_display.set_reorderable (true);
        track_display.set_headers_visible (true);
 
        track_model->signal_row_deleted().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_delete));
-       track_model->signal_row_changed().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_change));
        track_model->signal_rows_reordered().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_reorder));
 
        CellRendererToggle* track_list_visible_cell = dynamic_cast<CellRendererToggle*>(track_display.get_column_cell_renderer (1));
        track_list_visible_cell->property_activatable() = true;
        track_list_visible_cell->property_radio() = false;
+       track_list_visible_cell->signal_toggled().connect (sigc::mem_fun (*this, &Mixer_UI::track_visibility_changed));
 
        track_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::track_display_button_press), false);
 
@@ -1800,13 +1850,13 @@ Mixer_UI::update_title ()
                }
                
                WindowTitle title (n);
-               title += _("Mixer");
+               title += S_("Window|Mixer");
                title += Glib::get_application_name ();
                set_title (title.get_string());
 
        } else {
                
-               WindowTitle title (X_("Mixer"));
+               WindowTitle title (S_("Window|Mixer"));
                title += Glib::get_application_name ();
                set_title (title.get_string());
        }
@@ -1839,7 +1889,7 @@ Mixer_UI::set_route_targets_for_operation ()
                return;
        }
 
-       /* try to get mixer strip at mouse */
+       /* nothing selected ... try to get mixer strip at mouse */
 
        int x, y;
        get_pointer (x, y);
@@ -1850,3 +1900,32 @@ Mixer_UI::set_route_targets_for_operation ()
                _route_targets.insert (ms);
        }
 }
+
+void
+Mixer_UI::monitor_section_going_away ()
+{
+       if (_monitor_section) {
+               out_packer.remove (_monitor_section->tearoff());
+               _monitor_section->set_session (0);
+       }
+}
+
+void
+Mixer_UI::toggle_midi_input_active (bool flip_others)
+{
+       boost::shared_ptr<RouteList> rl (new RouteList);
+       bool onoff;
+
+       set_route_targets_for_operation ();
+
+       for (RouteUISelection::iterator r = _route_targets.begin(); r != _route_targets.end(); ++r) {
+               boost::shared_ptr<MidiTrack> mt = (*r)->midi_track();
+
+               if (mt) {
+                       rl->push_back ((*r)->route());
+                       onoff = !mt->input_active();
+               }
+       }
+       
+       _session->set_exclusive_input_active (rl, onoff, flip_others);
+}