use system/gtk default font on OS X, rather than specifying Lucida Grande; make large...
[ardour.git] / gtk2_ardour / mixer_ui.cc
index 89d7ef5cc4558f860944088ccdfbaa5c5f8b286e..474cef239517fe67881710697fc72154448e82cf 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2000-2004 Paul Davis 
+    Copyright (C) 2000-2004 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 
 */
 
+#ifdef WAF_BUILD
+#include "gtk2ardour-config.h"
+#endif
+
 #include <algorithm>
+#include <map>
 #include <sigc++/bind.h>
 
 #include <gtkmm/accelmap.h>
 
-#include <pbd/convert.h>
+#include "pbd/convert.h"
+#include "pbd/stacktrace.h"
 #include <glibmm/thread.h>
 
 #include <gtkmm2ext/gtk_ui.h>
 #include <gtkmm2ext/utils.h>
-#include <gtkmm2ext/stop_signal.h>
+#include <gtkmm2ext/tearoff.h>
 #include <gtkmm2ext/window_title.h>
 
-#include <ardour/session.h>
-#include <ardour/audio_track.h>
-#include <ardour/session_route.h>
-#include <ardour/audio_diskstream.h>
-#include <ardour/plugin_manager.h>
+#include "ardour/audio_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"
 #include "mixer_strip.h"
+#include "monitor_section.h"
 #include "plugin_selector.h"
+#include "public_editor.h"
 #include "ardour_ui.h"
 #include "prompter.h"
 #include "utils.h"
 #include "actions.h"
 #include "gui_thread.h"
+#include "mixer_group_tabs.h"
 
 #include "i18n.h"
 
@@ -53,66 +62,67 @@ using namespace PBD;
 using namespace Gtk;
 using namespace Glib;
 using namespace Gtkmm2ext;
-using namespace sigc;
 using namespace std;
 
 using PBD::atoi;
 
+Mixer_UI* Mixer_UI::_instance = 0;
+
+Mixer_UI*
+Mixer_UI::instance () 
+{
+       if (!_instance) {
+               _instance  = new Mixer_UI;
+       } 
+
+       return _instance;
+}
+
 Mixer_UI::Mixer_UI ()
        : Window (Gtk::WINDOW_TOPLEVEL)
+       , _following_editor_selection (false)
 {
-       _strip_width = Wide;
+       /* 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;
-       mix_group_context_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;
 
-       scroller_base.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
-       scroller_base.set_name ("MixerWindow");
-       scroller_base.signal_button_release_event().connect (mem_fun(*this, &Mixer_UI::strip_scroller_button_release));
+       Route::SyncOrderKeys.connect (*this, invalidator (*this), ui_bind (&Mixer_UI::sync_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);
+       scroller_base.set_name ("MixerWindow");
+       scroller_base.signal_button_release_event().connect (sigc::mem_fun(*this, &Mixer_UI::strip_scroller_button_release));
        // add as last item of strip packer
        strip_packer.pack_end (scroller_base, true, true);
 
-       scroller.add (strip_packer);
-       scroller.set_policy (Gtk::POLICY_ALWAYS, Gtk::POLICY_AUTOMATIC);
-
-       track_model = ListStore::create (track_columns);
-       track_display.set_model (track_model);
-       track_display.append_column (_("Strips"), track_columns.text);
-       track_display.append_column (_("Show"), track_columns.visible);
-       track_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
-       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.get_selection()->set_mode (Gtk::SELECTION_NONE);
-       track_display.set_reorderable (true);
-       track_display.set_headers_visible (true);
-
-       track_model->signal_row_deleted().connect (mem_fun (*this, &Mixer_UI::track_list_delete));
-       track_model->signal_row_changed().connect (mem_fun (*this, &Mixer_UI::track_list_change));
-
-       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;
+       _group_tabs = new MixerGroupTabs (this);
+       VBox* b = manage (new VBox);
+       b->pack_start (*_group_tabs, PACK_SHRINK);
+       b->pack_start (strip_packer);
+       b->show_all ();
 
-       track_display.signal_button_press_event().connect (mem_fun (*this, &Mixer_UI::track_display_button_press), false);
+       scroller.add (*b);
+       scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
 
-       track_display_scroller.add (track_display);
-       track_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
+       setup_track_display ();
 
        group_model = ListStore::create (group_columns);
        group_display.set_model (group_model);
        group_display.append_column (_("Group"), group_columns.text);
-       group_display.append_column (_("Active"), group_columns.active);
        group_display.append_column (_("Show"), group_columns.visible);
        group_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
        group_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
-       group_display.get_column (2)->set_data (X_("colnum"), GUINT_TO_POINTER(2));
        group_display.get_column (0)->set_expand(true);
        group_display.get_column (1)->set_expand(false);
-       group_display.get_column (2)->set_expand(false);
        group_display.set_name ("MixerGroupList");
        group_display.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
        group_display.set_reorderable (true);
@@ -123,7 +133,7 @@ Mixer_UI::Mixer_UI ()
 
        CellRendererText* name_cell = dynamic_cast<CellRendererText*>(group_display.get_column_cell_renderer (0));
        name_cell->property_editable() = true;
-       name_cell->signal_edited().connect (mem_fun (*this, &Mixer_UI::mix_group_name_edit));
+       name_cell->signal_edited().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_name_edit));
 
        /* use checkbox for the active column */
 
@@ -131,48 +141,39 @@ Mixer_UI::Mixer_UI ()
        active_cell->property_activatable() = true;
        active_cell->property_radio() = false;
 
-       /* use checkbox for the visible column */
-
-       active_cell = dynamic_cast<CellRendererToggle*>(group_display.get_column_cell_renderer (2));
-       active_cell->property_activatable() = true;
-       active_cell->property_radio() = false;
-
-       group_model->signal_row_changed().connect (mem_fun (*this, &Mixer_UI::mix_group_row_change));
-
-       group_display.signal_button_press_event().connect (mem_fun (*this, &Mixer_UI::group_display_button_press), false);
+       group_model->signal_row_changed().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_row_change));
+       /* We use this to notice drag-and-drop reorders of the group list */
+       group_model->signal_row_deleted().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_row_deleted));
+       group_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::group_display_button_press), false);
 
        group_display_scroller.add (group_display);
        group_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
 
-       HBox* mix_group_display_button_box = manage (new HBox());
+       HBox* route_group_display_button_box = manage (new HBox());
 
-       Button* mix_group_add_button = manage (new Button ());
-       Button* mix_group_remove_button = manage (new Button ());
+       Button* route_group_add_button = manage (new Button ());
+       Button* route_group_remove_button = manage (new Button ());
 
        Widget* w;
 
        w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
        w->show();
-       mix_group_add_button->add (*w);
+       route_group_add_button->add (*w);
 
        w = manage (new Image (Stock::REMOVE, ICON_SIZE_BUTTON));
        w->show();
-       mix_group_remove_button->add (*w);
+       route_group_remove_button->add (*w);
 
-       mix_group_display_button_box->set_homogeneous (true);
+       route_group_display_button_box->set_homogeneous (true);
 
-       mix_group_add_button->signal_clicked().connect (mem_fun (*this, &Mixer_UI::new_mix_group));
-       mix_group_remove_button->signal_clicked().connect (mem_fun (*this, &Mixer_UI::remove_selected_mix_group));
+       route_group_add_button->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::new_route_group));
+       route_group_remove_button->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::remove_selected_route_group));
 
-       mix_group_display_button_box->add (*mix_group_remove_button);
-       mix_group_display_button_box->add (*mix_group_add_button);
+       route_group_display_button_box->add (*route_group_add_button);
+       route_group_display_button_box->add (*route_group_remove_button);
 
        group_display_vbox.pack_start (group_display_scroller, true, true);
-       group_display_vbox.pack_start (*mix_group_display_button_box, false, false);
-
-       track_display_frame.set_name("BaseFrame");
-       track_display_frame.set_shadow_type (Gtk::SHADOW_IN);
-       track_display_frame.add(track_display_scroller);
+       group_display_vbox.pack_start (*route_group_display_button_box, false, false);
 
        group_display_frame.set_name ("BaseFrame");
        group_display_frame.set_shadow_type (Gtk::SHADOW_IN);
@@ -184,45 +185,37 @@ Mixer_UI::Mixer_UI ()
        list_vpacker.pack_start (rhs_pane1, true, true);
 
        global_hpacker.pack_start (scroller, true, true);
+#ifdef GTKOSX
+       /* current gtk-quartz has dirty updates on borders like this one */
+       global_hpacker.pack_start (out_packer, false, false, 0);
+#else
        global_hpacker.pack_start (out_packer, false, false, 12);
+#endif
+       list_hpane.pack1(list_vpacker, true, true);
+       list_hpane.pack2(global_hpacker, true, false);
 
-       list_hpane.add1(list_vpacker);
-       list_hpane.add2(global_hpacker);
-
-       rhs_pane1.signal_size_allocate().connect (bind (mem_fun(*this, &Mixer_UI::pane_allocation_handler), 
+       rhs_pane1.signal_size_allocate().connect (sigc::bind (sigc::mem_fun(*this, &Mixer_UI::pane_allocation_handler),
                                                        static_cast<Gtk::Paned*> (&rhs_pane1)));
-       list_hpane.signal_size_allocate().connect (bind (mem_fun(*this, &Mixer_UI::pane_allocation_handler), 
+       list_hpane.signal_size_allocate().connect (sigc::bind (sigc::mem_fun(*this, &Mixer_UI::pane_allocation_handler),
                                                         static_cast<Gtk::Paned*> (&list_hpane)));
-       
-
-       rhs_pane1.set_data ("collapse-direction", (gpointer) 0);
-       list_hpane.set_data ("collapse-direction", (gpointer) 1);
 
        global_vpacker.pack_start (list_hpane, true, true);
 
        add (global_vpacker);
        set_name ("MixerWindow");
-       
-       WindowTitle title(Glib::get_application_name());
-       title += _("Mixer");
-       set_title (title.get_string());
 
-       set_wmclass (X_("ardour_mixer"), "Ardour");
+       update_title ();
 
-       add_accel_group (ActionManager::ui_manager->get_accel_group());
+       set_wmclass (X_("ardour_mixer"), PROGRAM_NAME);
 
-       signal_delete_event().connect (mem_fun (*this, &Mixer_UI::hide_window));
+       signal_delete_event().connect (sigc::mem_fun (*this, &Mixer_UI::hide_window));
        add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
 
-       _plugin_selector = new PluginSelector (PluginManager::the_manager());
-
-       signal_configure_event().connect (mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::configure_handler));
+       signal_configure_event().connect (sigc::mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::configure_handler));
 
-       _selection.RoutesChanged.connect (mem_fun(*this, &Mixer_UI::follow_strip_selection));
-       
-       mix_group_display_button_box->show();
-       mix_group_add_button->show();
-       mix_group_remove_button->show();
+       route_group_display_button_box->show();
+       route_group_add_button->show();
+       route_group_remove_button->show();
 
        global_hpacker.show();
        global_vpacker.show();
@@ -233,23 +226,39 @@ Mixer_UI::Mixer_UI ()
        list_vpacker.show();
        group_display_button_label.show();
        group_display_button.show();
-       track_display_scroller.show();
        group_display_scroller.show();
        group_display_vbox.show();
-       track_display_frame.show();
        group_display_frame.show();
        rhs_pane1.show();
        strip_packer.show();
        out_packer.show();
        list_hpane.show();
-       track_display.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());
+
+        MonitorSection::setup_knob_images ();
+
+#ifndef DEFER_PLUGIN_SELECTOR_LOAD
+       _plugin_selector = new PluginSelector (PluginManager::instance ());
+#endif
 }
 
 Mixer_UI::~Mixer_UI ()
 {
 }
 
+void
+Mixer_UI::track_editor_selection ()
+{
+       PublicEditor::instance().get_selection().TracksChanged.connect (sigc::mem_fun (*this, &Mixer_UI::follow_editor_selection));
+}
+
+
 void
 Mixer_UI::ensure_float (Window& win)
 {
@@ -260,19 +269,29 @@ void
 Mixer_UI::show_window ()
 {
        present ();
+       if (!_visible) {
+               set_window_pos_and_size ();
 
-       set_window_pos_and_size ();
+               /* show/hide group tabs as required */
+               parameter_changed ("show-group-tabs");
 
-       /* now reset each strips width so the right widgets are shown */
-       MixerStrip* ms;
+               /* now reset each strips width so the right widgets are shown */
+               MixerStrip* ms;
 
-       TreeModel::Children rows = track_model->children();
-       TreeModel::Children::iterator ri;
+               TreeModel::Children rows = track_model->children();
+               TreeModel::Children::iterator ri;
 
-       for (ri = rows.begin(); ri != rows.end(); ++ri) {
-               ms = (*ri)[track_columns.strip];
-               ms->set_width (ms->get_width(), ms->width_owner());
+               for (ri = rows.begin(); ri != rows.end(); ++ri) {
+                       ms = (*ri)[track_columns.strip];
+                       ms->set_width_enum (ms->get_width_enum (), ms->width_owner());
+                       /* Fix visibility of mixer strip stuff */
+                       ms->parameter_changed (X_("mixer-strip-visibility"));
+               }
        }
+       
+       /* force focus into main area */
+       scroller_base.grab_focus ();
+
        _visible = true;
 }
 
@@ -287,52 +306,86 @@ Mixer_UI::hide_window (GdkEventAny *ev)
 
 
 void
-Mixer_UI::add_strip (Session::RouteList& routes)
+Mixer_UI::add_strip (RouteList& routes)
 {
-       ENSURE_GUI_THREAD(bind (mem_fun(*this, &Mixer_UI::add_strip), routes));
-       
+       ENSURE_GUI_THREAD (*this, &Mixer_UI::add_strip, routes)
+
        MixerStrip* strip;
 
-       for (Session::RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
+       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()) {
-                       return;
+                       continue;
                }
-               
-               strip = new MixerStrip (*this, *session, route);
+
+                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 (_strip_width, this);
+                       strip->set_width_enum (_strip_width, this);
                }
 
                show_strip (strip);
-               
-               no_track_list_redisplay = true;
-               
+
                TreeModel::Row row = *(track_model->append());
                row[track_columns.text] = route->name();
-               
-               row[track_columns.visible] = strip->marked_for_display();
+               row[track_columns.visible] = strip->route()->is_master() ? true : strip->marked_for_display();
                row[track_columns.route] = route;
                row[track_columns.strip] = strip;
-               
-               no_track_list_redisplay = false;
-               redisplay_track_list ();
-               
-               route->NameChanged.connect (bind (mem_fun(*this, &Mixer_UI::strip_name_changed), strip));
-               strip->GoingAway.connect (bind (mem_fun(*this, &Mixer_UI::remove_strip), strip));
-               
-               strip->signal_button_release_event().connect (bind (mem_fun(*this, &Mixer_UI::strip_button_release_event), strip));
+
+               if (route->order_key (N_("signal")) == -1) {
+                       route->set_order_key (N_("signal"), track_model->children().size()-1);
+               }
+
+               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;
+
+       redisplay_track_list ();
+
+       strip_redisplay_does_not_sync_order_keys = false;
 }
 
 void
 Mixer_UI::remove_strip (MixerStrip* strip)
 {
-       ENSURE_GUI_THREAD(bind (mem_fun(*this, &Mixer_UI::remove_strip), strip));
-       
+       if (_session && _session->deletion_in_progress()) {
+               /* its all being taken care of */
+               return;
+       }
+
+       ENSURE_GUI_THREAD (*this, &Mixer_UI::remove_strip, strip);
+
        TreeModel::Children rows = track_model->children();
        TreeModel::Children::iterator ri;
        list<MixerStrip *>::iterator i;
@@ -341,39 +394,163 @@ Mixer_UI::remove_strip (MixerStrip* strip)
                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)
+{
+       TreeModel::Children rows = track_model->children();
+       TreeModel::Children::iterator ri;
+
+       if (src == N_("signal") || !_session || (_session->state_of_the_state() & (Session::Loading|Session::Deletion)) || rows.empty()) {
+               return;
+       }
+
+       std::map<int,int> keys;
+
+       bool changed = false;
+
+       unsigned order = 0;
+       for (ri = rows.begin(); ri != rows.end(); ++ri, ++order) {
+               boost::shared_ptr<Route> route = (*ri)[track_columns.route];
+               unsigned int old_key = order;
+               unsigned int new_key = route->order_key (N_("signal"));
+
+               keys[new_key] = old_key;
+
+               if (new_key != old_key) {
+                       changed = true;
+               }
+       }
+
+       if (keys.size() != rows.size()) {
+               PBD::stacktrace (cerr, 20);
+       }
+       assert(keys.size() == rows.size());
+
+       // Remove any gaps in keys caused by automation children tracks
+       vector<int> neworder;
+       for (std::map<int,int>::const_iterator i = keys.begin(); i != keys.end(); ++i) {
+               neworder.push_back(i->second);
+       }
+       assert(neworder.size() == rows.size());
+
+       if (changed) {
+               strip_redisplay_does_not_reset_order_keys = true;
+               track_model->reorder (neworder);
+               strip_redisplay_does_not_reset_order_keys = false;
+       }
 }
 
 void
-Mixer_UI::follow_strip_selection ()
+Mixer_UI::follow_editor_selection ()
+{
+       if (!Config->get_link_editor_and_mixer_selection() || _following_editor_selection) {
+               return;
+       }
+
+       _following_editor_selection = true;
+       _selection.block_routes_changed (true);
+       
+       TrackSelection& s (PublicEditor::instance().get_selection().tracks);
+
+       _selection.clear_routes ();
+
+       for (TrackViewList::iterator i = s.begin(); i != s.end(); ++i) {
+               RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*i);
+               if (rtav) {
+                       MixerStrip* ms = strip_by_route (rtav->route());
+                       if (ms) {
+                               _selection.add (ms);
+                       }
+               }
+       }
+
+       _following_editor_selection = false;
+       _selection.block_routes_changed (false);
+}
+
+
+MixerStrip*
+Mixer_UI::strip_by_route (boost::shared_ptr<Route> r)
 {
        for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
-               (*i)->set_selected (_selection.selected ((*i)->route()));
+               if ((*i)->route() == r) {
+                       return (*i);
+               }
        }
+
+       return 0;
 }
 
 bool
 Mixer_UI::strip_button_release_event (GdkEventButton *ev, MixerStrip *strip)
 {
        if (ev->button == 1) {
-
-               /* this allows the user to click on the strip to terminate comment
-                  editing. XXX it needs improving so that we don't select the strip
-                  at the same time.
-               */
-               
-               if (_selection.selected (strip->route())) {
-                       _selection.remove (strip->route());
+               if (_selection.selected (strip)) {
+                       /* primary-click: toggle selection state of strip */
+                       if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
+                               _selection.remove (strip);
+                       } 
                } else {
-                       if (Keyboard::modifier_state_equals (ev->state, Keyboard::Shift)) {
-                               _selection.add (strip->route());
+                       if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
+                               _selection.add (strip);
+                       } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::RangeSelectModifier)) {
+
+                               if (!_selection.selected(strip)) {
+                               
+                                       /* extend selection */
+                                       
+                                       vector<MixerStrip*> tmp;
+                                       bool accumulate = false;
+                                       
+                                       tmp.push_back (strip);
+
+                                       for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
+                                               if ((*i) == strip) {
+                                                       /* hit clicked strip, start accumulating till we hit the first 
+                                                          selected strip
+                                                       */
+                                                       if (accumulate) {
+                                                               /* done */
+                                                               break;
+                                                       } else {
+                                                               accumulate = true;
+                                                       }
+                                               } else if (_selection.selected (*i)) {
+                                                       /* hit selected strip. if currently accumulating others,
+                                                          we're done. if not accumulating others, start doing so.
+                                                       */
+                                                       if (accumulate) {
+                                                               /* done */
+                                                               break;
+                                                       } else {
+                                                               accumulate = true;
+                                                       }
+                                               } else {
+                                                       if (accumulate) {
+                                                               tmp.push_back (*i);
+                                                       }
+                                               }
+                                       }
+
+                                       for (vector<MixerStrip*>::iterator i = tmp.begin(); i != tmp.end(); ++i) {
+                                               _selection.add (*i);
+                                       }
+                               }
+
                        } else {
-                               _selection.set (strip->route());
+                               _selection.set (strip);
                        }
                }
        }
@@ -382,51 +559,74 @@ Mixer_UI::strip_button_release_event (GdkEventButton *ev, MixerStrip *strip)
 }
 
 void
-Mixer_UI::connect_to_session (Session* sess)
+Mixer_UI::set_session (Session* sess)
 {
+       SessionHandlePtr::set_session (sess);
+
+       if (_plugin_selector) {
+               _plugin_selector->set_session (_session);
+       }
 
-       session = sess;
+       _group_tabs->set_session (sess);
+
+       if (!_session) {
+               return;
+       }
 
        XMLNode* node = ARDOUR_UI::instance()->mixer_settings();
        set_state (*node);
 
-       WindowTitle title(session->name());
-       title += _("Mixer");
-       title += Glib::get_application_name();
-
-       set_title (title.get_string());
+       update_title ();
 
        initial_track_display ();
 
-       session->GoingAway.connect (mem_fun(*this, &Mixer_UI::disconnect_from_session));
-       session->RouteAdded.connect (mem_fun(*this, &Mixer_UI::add_strip));
-       session->mix_group_added.connect (mem_fun(*this, &Mixer_UI::add_mix_group));
-       session->mix_group_removed.connect (mem_fun(*this, &Mixer_UI::mix_groups_changed));
+       _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->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->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());
 
-       mix_groups_changed ();
-       
-       _plugin_selector->set_session (session);
+       Config->ParameterChanged.connect (*this, invalidator (*this), ui_bind (&Mixer_UI::parameter_changed, this, _1), gui_context ());
+
+       route_groups_changed ();
 
        if (_visible) {
-              show_window();
+               show_window();
        }
 
        start_updating ();
 }
 
 void
-Mixer_UI::disconnect_from_session ()
+Mixer_UI::session_going_away ()
 {
-       ENSURE_GUI_THREAD(mem_fun(*this, &Mixer_UI::disconnect_from_session));
-       
+       ENSURE_GUI_THREAD (*this, &Mixer_UI::session_going_away);
+
+       _in_group_rebuild_or_clear = true;
        group_model->clear ();
+       _in_group_rebuild_or_clear = false;
+
        _selection.clear ();
+       track_model->clear ();
+
+       for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
+               delete (*i);
+       }
+
+        if (_monitor_section) {
+                _monitor_section->tearoff().hide_visible ();
+        }
+
+       strips.clear ();
 
-       WindowTitle title(Glib::get_application_name());
-       title += _("Mixer");
-       set_title (title.get_string());
-       
        stop_updating ();
+
+       SessionHandlePtr::session_going_away ();
+
+       _session = 0;
+       update_title ();
 }
 
 void
@@ -434,9 +634,9 @@ Mixer_UI::show_strip (MixerStrip* ms)
 {
        TreeModel::Children rows = track_model->children();
        TreeModel::Children::iterator i;
-       
+
        for (i = rows.begin(); i != rows.end(); ++i) {
-       
+
                MixerStrip* strip = (*i)[track_columns.strip];
                if (strip == ms) {
                        (*i)[track_columns.visible] = true;
@@ -450,9 +650,9 @@ Mixer_UI::hide_strip (MixerStrip* ms)
 {
        TreeModel::Children rows = track_model->children();
        TreeModel::Children::iterator i;
-       
+
        for (i = rows.begin(); i != rows.end(); ++i) {
-               
+
                MixerStrip* strip = (*i)[track_columns.strip];
                if (strip == ms) {
                        (*i)[track_columns.visible] = false;
@@ -464,7 +664,7 @@ Mixer_UI::hide_strip (MixerStrip* ms)
 gint
 Mixer_UI::start_updating ()
 {
-    fast_screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun(*this, &Mixer_UI::fast_update_strips));
+    fast_screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (sigc::mem_fun(*this, &Mixer_UI::fast_update_strips));
     return 0;
 }
 
@@ -478,7 +678,7 @@ Mixer_UI::stop_updating ()
 void
 Mixer_UI::fast_update_strips ()
 {
-       if (is_mapped () && session) {
+       if (is_mapped () && _session) {
                for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
                        (*i)->fast_update ();
                }
@@ -497,12 +697,12 @@ Mixer_UI::set_all_strips_visibility (bool yn)
 
                TreeModel::Row row = (*i);
                MixerStrip* strip = row[track_columns.strip];
-               
+
                if (strip == 0) {
                        continue;
                }
-               
-               if (strip->route()->is_master() || strip->route()->is_control()) {
+
+               if (strip->route()->is_master() || strip->route()->is_monitor()) {
                        continue;
                }
 
@@ -515,9 +715,9 @@ Mixer_UI::set_all_strips_visibility (bool yn)
 
 
 void
-Mixer_UI::set_all_audio_visibility (int tracks, bool yn) 
+Mixer_UI::set_all_audio_visibility (int tracks, bool yn)
 {
-        TreeModel::Children rows = track_model->children();
+       TreeModel::Children rows = track_model->children();
        TreeModel::Children::iterator i;
 
        no_track_list_redisplay = true;
@@ -530,7 +730,7 @@ Mixer_UI::set_all_audio_visibility (int tracks, bool yn)
                        continue;
                }
 
-               if (strip->route()->is_master() || strip->route()->is_control()) {
+               if (strip->route()->is_master() || strip->route()->is_monitor()) {
                        continue;
                }
 
@@ -540,13 +740,13 @@ Mixer_UI::set_all_audio_visibility (int tracks, bool yn)
                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;
@@ -594,17 +794,32 @@ Mixer_UI::hide_all_audiotracks ()
 }
 
 void
-Mixer_UI::track_list_change (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter)
+Mixer_UI::track_list_reorder (const TreeModel::Path&, const TreeModel::iterator&, int* /*new_order*/)
 {
-       session->set_remote_control_ids();
+       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_delete (const Gtk::TreeModel::Path& path)
+Mixer_UI::track_list_change (const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator&)
 {
-       session->set_remote_control_ids();
+       // 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;
+}
+
+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 ();
+       }
 }
 
 void
@@ -626,15 +841,18 @@ Mixer_UI::redisplay_track_list ()
                        continue;
                }
 
-               bool visible = (*i)[track_columns.visible];
+               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) {
-                       strip->set_marked_for_display (true);
-                       strip->route()->set_order_key (N_("signal"), order);
+                       strip->set_gui_property ("visible", true);
 
                        if (strip->packed()) {
 
-                               if (strip->route()->is_master() || strip->route()->is_control()) {
+                               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 */
@@ -642,18 +860,19 @@ Mixer_UI::redisplay_track_list ()
 
                        } else {
 
-                               if (strip->route()->is_master() || strip->route()->is_control()) {
+                               if (strip->route()->is_master() || strip->route()->is_monitor()) {
                                        out_packer.pack_start (*strip, false, false);
                                } else {
                                        strip_packer.pack_start (*strip, false, false);
                                }
                                strip->set_packed (true);
-                               //strip->show();
                        }
 
                } else {
 
-                       if (strip->route()->is_master() || strip->route()->is_control()) {
+                       strip->set_gui_property ("visible", false);
+
+                       if (strip->route()->is_master() || strip->route()->is_monitor()) {
                                /* do nothing, these cannot be hidden */
                        } else {
                                if (strip->packed()) {
@@ -663,67 +882,188 @@ Mixer_UI::redisplay_track_list ()
                        }
                }
        }
-}
 
-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 ("signal") < b->order_key ("signal");
-    }
-};
+       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 ();
+}
 
 void
-Mixer_UI::initial_track_display ()
+Mixer_UI::strip_width_changed ()
 {
-       boost::shared_ptr<Session::RouteList> routes = session->get_routes();
-       Session::RouteList copy (*routes);
-       SignalOrderRouteSorter sorter;
+       _group_tabs->set_dirty ();
 
-       copy.sort (sorter);
-       
-       no_track_list_redisplay = true;
+#ifdef GTKOSX
+       TreeModel::Children rows = track_model->children();
+       TreeModel::Children::iterator i;
+       long order;
 
-       track_model->clear ();
+       for (order = 0, i = rows.begin(); i != rows.end(); ++i, ++order) {
+               MixerStrip* strip = (*i)[track_columns.strip];
 
-       add_strip (copy);
+               if (strip == 0) {
+                       continue;
+               }
 
-       no_track_list_redisplay = false;
+               bool visible = (*i)[track_columns.visible];
+
+               if (visible) {
+                       strip->queue_draw();
+               }
+       }
+#endif
 
-       redisplay_track_list ();
 }
 
 void
-Mixer_UI::show_track_list_menu ()
+Mixer_UI::set_auto_rebinding( bool val )
 {
-       if (track_menu == 0) {
-               build_track_menu ();
+       if( val == TRUE )
+       {
+               auto_rebinding = TRUE;
+               Session::AutoBindingOff();
+       }
+       else
+       {
+               auto_rebinding = FALSE;
+               Session::AutoBindingOn();
        }
-
-       track_menu->popup (1, gtk_get_current_event_time());
 }
 
-bool
-Mixer_UI::track_display_button_press (GdkEventButton* ev)
+void
+Mixer_UI::toggle_auto_rebinding()
 {
-       if (Keyboard::is_context_menu_event (ev)) {
-               show_track_list_menu ();
-               return true;
+       if (auto_rebinding)
+       {
+               set_auto_rebinding( FALSE );
        }
 
-       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;
+       else
+       {
+               set_auto_rebinding( TRUE );
        }
 
-       switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
-       case 0:
-               /* allow normal processing to occur */
-               return false;
+       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"));
+    }
+};
+
+void
+Mixer_UI::initial_track_display ()
+{
+       boost::shared_ptr<RouteList> routes = _session->get_routes();
+       RouteList copy (*routes);
+       SignalOrderRouteSorter sorter;
+
+       copy.sort (sorter);
+
+       no_track_list_redisplay = true;
+
+       track_model->clear ();
+
+       add_strip (copy);
+
+       no_track_list_redisplay = false;
+
+       redisplay_track_list ();
+}
+
+void
+Mixer_UI::show_track_list_menu ()
+{
+       if (track_menu == 0) {
+               build_track_menu ();
+       }
+
+       track_menu->popup (1, gtk_get_current_event_time());
+}
+
+bool
+Mixer_UI::track_display_button_press (GdkEventButton* ev)
+{
+       if (Keyboard::is_context_menu_event (ev)) {
+               show_track_list_menu ();
+               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 */
 
@@ -731,10 +1071,13 @@ Mixer_UI::track_display_button_press (GdkEventButton* ev)
                        MixerStrip* strip = (*iter)[track_columns.strip];
                        if (strip) {
 
-                               if (!strip->route()->is_master() && !strip->route()->is_control()) {
+                               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;
@@ -756,65 +1099,41 @@ Mixer_UI::build_track_menu ()
        track_menu = new Menu;
        track_menu->set_name ("ArdourContextMenu");
        MenuList& items = track_menu->items();
-       
-       items.push_back (MenuElem (_("Show All"), mem_fun(*this, &Mixer_UI::show_all_routes)));
-       items.push_back (MenuElem (_("Hide All"), mem_fun(*this, &Mixer_UI::hide_all_routes)));
-       items.push_back (MenuElem (_("Show All Audio Tracks"), mem_fun(*this, &Mixer_UI::show_all_audiotracks)));
-       items.push_back (MenuElem (_("Hide All Audio Tracks"), mem_fun(*this, &Mixer_UI::hide_all_audiotracks)));
-       items.push_back (MenuElem (_("Show All Audio Busses"), mem_fun(*this, &Mixer_UI::show_all_audiobus)));
-       items.push_back (MenuElem (_("Hide All Audio Busses"), mem_fun(*this, &Mixer_UI::hide_all_audiobus)));
+
+       items.push_back (MenuElem (_("Show All"), sigc::mem_fun(*this, &Mixer_UI::show_all_routes)));
+       items.push_back (MenuElem (_("Hide All"), sigc::mem_fun(*this, &Mixer_UI::hide_all_routes)));
+       items.push_back (MenuElem (_("Show All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiotracks)));
+       items.push_back (MenuElem (_("Hide All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiotracks)));
+       items.push_back (MenuElem (_("Show All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiobus)));
+       items.push_back (MenuElem (_("Hide All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiobus)));
 
 }
 
 void
-Mixer_UI::strip_name_changed (MixerStrip* mx)
+Mixer_UI::strip_property_changed (const PropertyChange& what_changed, MixerStrip* mx)
 {
-       ENSURE_GUI_THREAD(bind (mem_fun(*this, &Mixer_UI::strip_name_changed), mx));
-       
+       if (!what_changed.contains (ARDOUR::Properties::name)) {
+               return;
+       }
+
+       ENSURE_GUI_THREAD (*this, &Mixer_UI::strip_name_changed, what_changed, mx)
+
        TreeModel::Children rows = track_model->children();
        TreeModel::Children::iterator i;
-       
+
        for (i = rows.begin(); i != rows.end(); ++i) {
                if ((*i)[track_columns.strip] == mx) {
                        (*i)[track_columns.text] = mx->route()->name();
                        return;
                }
-       } 
+       }
 
        error << _("track display list item for renamed strip not found!") << endmsg;
 }
 
-
-void
-Mixer_UI::build_mix_group_context_menu ()
-{
-       using namespace Gtk::Menu_Helpers;
-
-       mix_group_context_menu = new Menu;
-       mix_group_context_menu->set_name ("ArdourContextMenu");
-       MenuList& items = mix_group_context_menu->items();
-
-       items.push_back (MenuElem (_("Activate All"), mem_fun(*this, &Mixer_UI::activate_all_mix_groups)));
-       items.push_back (MenuElem (_("Disable All"), mem_fun(*this, &Mixer_UI::disable_all_mix_groups)));
-       items.push_back (SeparatorElem());
-       items.push_back (MenuElem (_("Add group"), mem_fun(*this, &Mixer_UI::new_mix_group)));
-       
-}
-
 bool
 Mixer_UI::group_display_button_press (GdkEventButton* ev)
 {
-       if (Keyboard::is_context_menu_event (ev)) {
-               if (mix_group_context_menu == 0) {
-                       build_mix_group_context_menu ();
-               }
-               mix_group_context_menu->popup (1, ev->time);
-               return true;
-       }
-
-
-        RouteGroup* group;
-       TreeIter iter;
        TreeModel::Path path;
        TreeViewColumn* column;
        int cellx;
@@ -824,64 +1143,66 @@ Mixer_UI::group_display_button_press (GdkEventButton* ev)
                return false;
        }
 
+       TreeIter iter = group_model->get_iter (path);
+       if (!iter) {
+               return false;
+       }
+
+       RouteGroup* group = (*iter)[group_columns.group];
+
+       if (Keyboard::is_context_menu_event (ev)) {
+               _group_tabs->get_menu(group)->popup (1, ev->time);
+               return true;
+       }
+
        switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
        case 0:
                if (Keyboard::is_edit_event (ev)) {
-                       if ((iter = group_model->get_iter (path))) {
-                               if ((group = (*iter)[group_columns.group]) != 0) {
-                                       // edit_mix_group (group);
-                                       return true;
-                               }
+                       if (group) {
+                               // edit_route_group (group);
+#ifdef GTKOSX
+                               group_display.queue_draw();
+#endif
+                               return true;
                        }
-                       
-               } 
+               }
                break;
 
        case 1:
-               if ((iter = group_model->get_iter (path))) {
-                       bool active = (*iter)[group_columns.active];
-                       (*iter)[group_columns.active] = !active;
-                       return true;
-               }
-               break;
-               
-       case 2:
-               if ((iter = group_model->get_iter (path))) {
-                       bool visible = (*iter)[group_columns.visible];
-                       (*iter)[group_columns.visible] = !visible;
-                       return true;
-               }
-               break;
+       {
+               bool visible = (*iter)[group_columns.visible];
+               (*iter)[group_columns.visible] = !visible;
+#ifdef GTKOSX
+               group_display.queue_draw();
+#endif
+               return true;
+       }
 
        default:
                break;
        }
-       
+
        return false;
  }
 
 void
-Mixer_UI::activate_all_mix_groups ()
+Mixer_UI::activate_all_route_groups ()
 {
-        Gtk::TreeModel::Children children = group_model->children();
-       for(Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
-               (*iter)[group_columns.active] = true;
-       }
+       _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), true));
 }
 
 void
-Mixer_UI::disable_all_mix_groups ()
+Mixer_UI::disable_all_route_groups ()
 {
-        Gtk::TreeModel::Children children = group_model->children();
-       for(Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
-               (*iter)[group_columns.active] = false;
-       }
+       _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), false));
 }
 
 void
-Mixer_UI::mix_groups_changed ()
+Mixer_UI::route_groups_changed ()
 {
-       ENSURE_GUI_THREAD (mem_fun (*this, &Mixer_UI::mix_groups_changed));
+       ENSURE_GUI_THREAD (*this, &Mixer_UI::route_groups_changed);
+
+       _in_group_rebuild_or_clear = true;
 
        /* just rebuild the while thing */
 
@@ -890,23 +1211,27 @@ Mixer_UI::mix_groups_changed ()
        {
                TreeModel::Row row;
                row = *(group_model->append());
-               row[group_columns.active] = false;
                row[group_columns.visible] = true;
                row[group_columns.text] = (_("-all-"));
                row[group_columns.group] = 0;
        }
 
-       session->foreach_mix_group (mem_fun (*this, &Mixer_UI::add_mix_group));
+       _session->foreach_route_group (sigc::mem_fun (*this, &Mixer_UI::add_route_group));
+
+       _group_tabs->set_dirty ();
+       _in_group_rebuild_or_clear = false;
 }
 
 void
-Mixer_UI::new_mix_group ()
+Mixer_UI::new_route_group ()
 {
-       session->add_mix_group ("");
+       RouteList rl;
+
+       _group_tabs->run_new_group_dialog (rl);
 }
 
 void
-Mixer_UI::remove_selected_mix_group ()
+Mixer_UI::remove_selected_route_group ()
 {
        Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
        TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
@@ -917,7 +1242,7 @@ Mixer_UI::remove_selected_mix_group ()
 
        TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
        TreeIter iter;
-       
+
        /* selection mode is single, so rows.begin() is it */
 
        if ((iter = group_model->get_iter (*i))) {
@@ -925,68 +1250,79 @@ Mixer_UI::remove_selected_mix_group ()
                RouteGroup* rg = (*iter)[group_columns.group];
 
                if (rg) {
-                       session->remove_mix_group (*rg);
+                       _session->remove_route_group (*rg);
                }
        }
 }
 
 void
-Mixer_UI::group_flags_changed (void* src, RouteGroup* group)
+Mixer_UI::route_group_property_changed (RouteGroup* group, const PropertyChange& change)
 {
        if (in_group_row_change) {
                return;
        }
 
-       ENSURE_GUI_THREAD(bind (mem_fun(*this, &Mixer_UI::group_flags_changed), src, group));
-
        /* force an update of any mixer strips that are using this group,
-          otherwise mix group names don't change in mixer strips 
+          otherwise mix group names don't change in mixer strips
        */
 
        for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
-               if ((*i)->mix_group() == group) {
-                       (*i)->mix_group_changed(0);
+               if ((*i)->route_group() == group) {
+                       (*i)->route_group_changed();
                }
        }
-       
+
        TreeModel::iterator i;
        TreeModel::Children rows = group_model->children();
        Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
 
        in_group_row_change = true;
-       
+
        for (i = rows.begin(); i != rows.end(); ++i) {
                if ((*i)[group_columns.group] == group) {
                        (*i)[group_columns.visible] = !group->is_hidden ();
-                       (*i)[group_columns.active] = group->is_active ();
                        (*i)[group_columns.text] = group->name ();
                        break;
                }
        }
 
        in_group_row_change = false;
+
+       if (change.contains (Properties::name)) {
+               _group_tabs->set_dirty ();
+       }
+
+       for (list<MixerStrip*>::iterator j = strips.begin(); j != strips.end(); ++j) {
+               if ((*j)->route_group() == group) {
+                       if (group->is_hidden ()) {
+                               hide_strip (*j);
+                       } else {
+                               show_strip (*j);
+                       }
+               }
+       }
 }
 
 void
-Mixer_UI::mix_group_name_edit (const Glib::ustring& path, const Glib::ustring& new_text)
+Mixer_UI::route_group_name_edit (const std::string& path, const std::string& new_text)
 {
        RouteGroup* group;
        TreeIter iter;
 
        if ((iter = group_model->get_iter (path))) {
-       
+
                if ((group = (*iter)[group_columns.group]) == 0) {
                        return;
                }
-               
+
                if (new_text != group->name()) {
                        group->set_name (new_text);
                }
        }
 }
 
-void 
-Mixer_UI::mix_group_row_change (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter)
+void
+Mixer_UI::route_group_row_change (const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator& iter)
 {
        RouteGroup* group;
 
@@ -998,42 +1334,56 @@ Mixer_UI::mix_group_row_change (const Gtk::TreeModel::Path& path,const Gtk::Tree
                return;
        }
 
-       if ((*iter)[group_columns.visible]) {
-               for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
-                       if ((*i)->mix_group() == group) {
-                               show_strip (*i);
-                       }
-               }
-       } else {
-               for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
-                       if ((*i)->mix_group() == group) {
-                               hide_strip (*i);
-                       }
-               }
-       } 
-
-       bool active = (*iter)[group_columns.active];
-       group->set_active (active, this);
-
-       Glib::ustring name = (*iter)[group_columns.text];
+       std::string name = (*iter)[group_columns.text];
 
        if (name != group->name()) {
                group->set_name (name);
        }
+
+       bool hidden = !(*iter)[group_columns.visible];
+
+       if (hidden != group->is_hidden ()) {
+               group->set_hidden (hidden, this);
+       }
 }
 
+/** Called when a group model row is deleted, but also when the model is
+ *  reordered by a user drag-and-drop; the latter is what we are
+ *  interested in here.
+ */
 void
-Mixer_UI::add_mix_group (RouteGroup* group)
+Mixer_UI::route_group_row_deleted (Gtk::TreeModel::Path const &)
+{
+       if (_in_group_rebuild_or_clear) {
+               return;
+       }
+
+       /* Re-write the session's route group list so that the new order is preserved */
 
+       list<RouteGroup*> new_list;
+
+       Gtk::TreeModel::Children children = group_model->children();
+       for (Gtk::TreeModel::Children::iterator i = children.begin(); i != children.end(); ++i) {
+               RouteGroup* g = (*i)[group_columns.group];
+               if (g) {
+                       new_list.push_back (g);
+               }
+       }
+
+       _session->reorder_route_groups (new_list);
+}
+
+
+void
+Mixer_UI::add_route_group (RouteGroup* group)
 {
-       ENSURE_GUI_THREAD(bind (mem_fun(*this, &Mixer_UI::add_mix_group), group));
+       ENSURE_GUI_THREAD (*this, &Mixer_UI::add_route_group, group)
        bool focus = false;
 
        in_group_row_change = true;
 
        TreeModel::Row row = *(group_model->append());
-       row[group_columns.active] = group->is_active();
-       row[group_columns.visible] = true;
+       row[group_columns.visible] = !group->is_hidden ();
        row[group_columns.group] = group;
        if (!group->name().empty()) {
                row[group_columns.text] = group->name();
@@ -1042,14 +1392,16 @@ Mixer_UI::add_mix_group (RouteGroup* group)
                focus = true;
        }
 
-       group->FlagsChanged.connect (bind (mem_fun(*this, &Mixer_UI::group_flags_changed), group));
-       
+       group->PropertyChanged.connect (*this, invalidator (*this), ui_bind (&Mixer_UI::route_group_property_changed, this, group, _1), gui_context());
+
        if (focus) {
                TreeViewColumn* col = group_display.get_column (0);
                CellRendererText* name_cell = dynamic_cast<CellRendererText*>(group_display.get_column_cell_renderer (0));
                group_display.set_cursor (group_model->get_path (row), *col, *name_cell, true);
        }
 
+       _group_tabs->set_dirty ();
+
        in_group_row_change = false;
 }
 
@@ -1072,7 +1424,7 @@ Mixer_UI::set_strip_width (Width w)
        _strip_width = w;
 
        for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
-               (*i)->set_width (w, this);
+               (*i)->set_width_enum (w, this);
        }
 }
 
@@ -1095,26 +1447,48 @@ Mixer_UI::set_state (const XMLNode& node)
 {
        const XMLProperty* prop;
        XMLNode* geometry;
-       
-       if ((geometry = find_named_node (node, "geometry")) == 0) {
 
-               m_width = default_width;
-               m_height = default_height;
-               m_root_x = 1;
-               m_root_y = 1;
+       m_width = default_width;
+       m_height = default_height;
+       m_root_x = 1;
+       m_root_y = 1;
 
-       } else {
+       if ((geometry = find_named_node (node, "geometry")) != 0) {
+
+               XMLProperty* prop;
 
-               m_width = atoi(geometry->property("x_size")->value().c_str());
-               m_height = atoi(geometry->property("y_size")->value().c_str());
-               m_root_x = atoi(geometry->property("x_pos")->value().c_str());
-               m_root_y = atoi(geometry->property("y_pos")->value().c_str());
+               if ((prop = geometry->property("x_size")) == 0) {
+                       prop = geometry->property ("x-size");
+               }
+               if (prop) {
+                       m_width = atoi(prop->value());
+               }
+               if ((prop = geometry->property("y_size")) == 0) {
+                       prop = geometry->property ("y-size");
+               }
+               if (prop) {
+                       m_height = atoi(prop->value());
+               }
+
+               if ((prop = geometry->property ("x_pos")) == 0) {
+                       prop = geometry->property ("x-pos");
+               }
+               if (prop) {
+                       m_root_x = atoi (prop->value());
+
+               }
+               if ((prop = geometry->property ("y_pos")) == 0) {
+                       prop = geometry->property ("y-pos");
+               }
+               if (prop) {
+                       m_root_y = atoi (prop->value());
+               }
        }
 
        set_window_pos_and_size ();
 
        if ((prop = node.property ("narrow-strips"))) {
-               if (prop->value() == "yes") {
+               if (string_is_affirmative (prop->value())) {
                        set_strip_width (Narrow);
                } else {
                        set_strip_width (Wide);
@@ -1122,7 +1496,7 @@ Mixer_UI::set_state (const XMLNode& node)
        }
 
        if ((prop = node.property ("show-mixer"))) {
-               if (prop->value() == "yes") {
+               if (string_is_affirmative (prop->value())) {
                       _visible = true;
                }
        }
@@ -1137,7 +1511,7 @@ Mixer_UI::get_state (void)
 
        if (is_realized()) {
                Glib::RefPtr<Gdk::Window> win = get_window();
-       
+
                get_window_pos_and_size ();
 
                XMLNode* geometry = new XMLNode ("geometry");
@@ -1150,7 +1524,7 @@ Mixer_UI::get_state (void)
                geometry->add_property(X_("x_pos"), string(buf));
                snprintf(buf, sizeof(buf), "%d", m_root_y);
                geometry->add_property(X_("y_pos"), string(buf));
-               
+
                // written only for compatibility, they are not used.
                snprintf(buf, sizeof(buf), "%d", 0);
                geometry->add_property(X_("x_off"), string(buf));
@@ -1173,23 +1547,27 @@ Mixer_UI::get_state (void)
 }
 
 
-void 
-Mixer_UI::pane_allocation_handler (Allocation& alloc, Gtk::Paned* which)
+void
+Mixer_UI::pane_allocation_handler (Allocation&, Gtk::Paned* which)
 {
        int pos;
        XMLProperty* prop = 0;
        char buf[32];
        XMLNode* node = ARDOUR_UI::instance()->mixer_settings();
        XMLNode* geometry;
-       int width, height;
+       int height;
        static int32_t done[3] = { 0, 0, 0 };
 
-       if ((geometry = find_named_node (*node, "geometry")) == 0) {
-               width = default_width;
-               height = default_height;
-       } else {
-               width = atoi(geometry->property("x_size")->value());
-               height = atoi(geometry->property("y_size")->value());
+       height = default_height;
+
+       if ((geometry = find_named_node (*node, "geometry")) != 0) {
+
+               if ((prop = geometry->property ("y_size")) == 0) {
+                       prop = geometry->property ("y-size");
+               }
+               if (prop) {
+                       height = atoi (prop->value());
+               }
        }
 
        if (which == static_cast<Gtk::Paned*> (&rhs_pane1)) {
@@ -1198,7 +1576,7 @@ Mixer_UI::pane_allocation_handler (Allocation& alloc, Gtk::Paned* which)
                        return;
                }
 
-               if (!geometry || (prop = geometry->property("mixer_rhs_pane1_pos")) == 0) {
+               if (!geometry || (prop = geometry->property("mixer-rhs-pane1-pos")) == 0) {
                        pos = height / 3;
                        snprintf (buf, sizeof(buf), "%d", pos);
                } else {
@@ -1215,7 +1593,7 @@ Mixer_UI::pane_allocation_handler (Allocation& alloc, Gtk::Paned* which)
                        return;
                }
 
-               if (!geometry || (prop = geometry->property("mixer_list_hpane_pos")) == 0) {
+               if (!geometry || (prop = geometry->property("mixer-list-hpane-pos")) == 0) {
                        pos = 75;
                        snprintf (buf, sizeof(buf), "%d", pos);
                } else {
@@ -1227,9 +1605,248 @@ Mixer_UI::pane_allocation_handler (Allocation& alloc, Gtk::Paned* which)
                }
        }
 }
+void
+Mixer_UI::scroll_left ()
+{
+       Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
+       /* stupid GTK: can't rely on clamping across versions */
+       scroller.get_hscrollbar()->set_value (max (adj->get_lower(), adj->get_value() - adj->get_step_increment()));
+}
+
+void
+Mixer_UI::scroll_right ()
+{
+       Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
+       /* stupid GTK: can't rely on clamping across versions */
+       scroller.get_hscrollbar()->set_value (min (adj->get_upper(), adj->get_value() + adj->get_step_increment()));
+}
 
 bool
 Mixer_UI::on_key_press_event (GdkEventKey* ev)
 {
-       return key_press_focus_accelerator_handler (*this, ev);
+        /* focus widget gets first shot, then bindings, otherwise
+           forward to main window
+        */
+
+       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);
+}
+
+bool
+Mixer_UI::on_key_release_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::Release)) {
+               return true;
+       }
+
+        /* don't forward releases */
+
+        return true;
+}
+
+bool
+Mixer_UI::on_scroll_event (GdkEventScroll* ev)
+{
+       switch (ev->direction) {
+       case GDK_SCROLL_LEFT:
+               scroll_left ();
+               return true;
+       case GDK_SCROLL_UP:
+               if (ev->state & Keyboard::TertiaryModifier) {
+                       scroll_left ();
+                       return true;
+               }
+               return false;
+
+       case GDK_SCROLL_RIGHT:
+               scroll_right ();
+               return true;
+
+       case GDK_SCROLL_DOWN:
+               if (ev->state & Keyboard::TertiaryModifier) {
+                       scroll_right ();
+                       return true;
+               }
+               return false;
+       }
+
+       return false;
+}
+
+
+void
+Mixer_UI::parameter_changed (string const & p)
+{
+       if (p == "show-group-tabs") {
+               bool const s = _session->config.get_show_group_tabs ();
+               if (s) {
+                       _group_tabs->show ();
+               } else {
+                       _group_tabs->hide ();
+               }
+       } else if (p == "default-narrow_ms") {
+               bool const s = Config->get_default_narrow_ms ();
+               for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
+                       (*i)->set_width_enum (s ? Narrow : Wide, this);
+               }
+       }
+}
+
+void
+Mixer_UI::set_route_group_activation (RouteGroup* g, bool a)
+{
+       g->set_active (a, this);
+}
+
+PluginSelector*
+Mixer_UI::plugin_selector()
+{
+#ifdef DEFER_PLUGIN_SELECTOR_LOAD
+       if (!_plugin_selector)
+               _plugin_selector = new PluginSelector (PluginManager::instance());
+#endif
+
+       return _plugin_selector;
+}
+
+void
+Mixer_UI::setup_track_display ()
+{
+       track_model = ListStore::create (track_columns);
+       track_display.set_model (track_model);
+       track_display.append_column (_("Strips"), track_columns.text);
+       track_display.append_column (_("Show"), track_columns.visible);
+       track_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
+       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.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_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::track_display_button_press), false);
+
+       track_display_scroller.add (track_display);
+       track_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
+
+       VBox* v = manage (new VBox);
+       v->show ();
+       v->pack_start (track_display_scroller, true, true);
+
+       Button* b = manage (new Button);
+       b->show ();
+       Widget* w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
+       w->show ();
+       b->add (*w);
+
+       b->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::new_track_or_bus));
+
+       v->pack_start (*b, false, false);
+
+       track_display_frame.set_name("BaseFrame");
+       track_display_frame.set_shadow_type (Gtk::SHADOW_IN);
+       track_display_frame.add (*v);
+
+       track_display_scroller.show();
+       track_display_frame.show();
+       track_display.show();
+}
+
+void
+Mixer_UI::new_track_or_bus ()
+{
+       ARDOUR_UI::instance()->add_route (this);
+}
+
+
+void
+Mixer_UI::update_title ()
+{
+       if (_session) {
+               string n;
+               
+               if (_session->snap_name() != _session->name()) {
+                       n = _session->snap_name ();
+               } else {
+                       n = _session->name ();
+               }
+
+               if (_session->dirty ()) {
+                       n = "*" + n;
+               }
+               
+               WindowTitle title (n);
+               title += S_("Window|Mixer");
+               title += Glib::get_application_name ();
+               set_title (title.get_string());
+
+       } else {
+               
+               WindowTitle title (S_("Window|Mixer"));
+               title += Glib::get_application_name ();
+               set_title (title.get_string());
+       }
+}
+
+MixerStrip*
+Mixer_UI::strip_by_x (int x)
+{
+       for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
+               int x1, x2, y;
+
+               (*i)->translate_coordinates (*this, 0, 0, x1, y);
+               x2 = x1 + (*i)->get_width();
+
+               if (x >= x1 && x <= x2) {
+                       return (*i);
+               }
+       }
+
+       return 0;
+}
+
+void
+Mixer_UI::set_route_targets_for_operation ()
+{
+       _route_targets.clear ();
+
+       if (!_selection.empty()) {
+               _route_targets = _selection.routes;
+               return;
+       }
+
+       /* try to get mixer strip at mouse */
+
+       int x, y;
+       get_pointer (x, y);
+       
+       MixerStrip* ms = strip_by_x (x);
+       
+       if (ms) {
+               _route_targets.insert (ms);
+       }
 }