merge with master, including manual merge conflict resolution
[ardour.git] / gtk2_ardour / editor_routes.cc
index eaf0c2500aedc853b2e194ef81f5be885914d30f..bf629ea27dd54a533194abc7f257ec388757bc53 100644 (file)
@@ -45,6 +45,7 @@
 #include "gui_thread.h"
 #include "actions.h"
 #include "utils.h"
+#include "route_sorter.h"
 #include "editor_group_tabs.h"
 #include "editor_routes.h"
 
@@ -68,6 +69,7 @@ EditorRoutes::EditorRoutes (Editor* e)
        : EditorComponent (e)
         , _ignore_reorder (false)
         , _no_redisplay (false)
+        , _adding_routes (false)
         , _menu (0)
         , old_focus (0)
         , selection_countdown (0)
@@ -221,6 +223,7 @@ EditorRoutes::EditorRoutes (Editor* e)
        _display.get_selection()->set_mode (SELECTION_SINGLE);
        _display.get_selection()->set_select_function (sigc::mem_fun (*this, &EditorRoutes::selection_filter));
        _display.set_reorderable (true);
+       _display.set_name (X_("EditGroupList"));
        _display.set_rules_hint (true);
        _display.set_size_request (100, -1);
        _display.add_object_drag (_columns.route.index(), "routes");
@@ -281,7 +284,7 @@ EditorRoutes::EditorRoutes (Editor* e)
 
         _display.set_enable_search (false);
 
-       Route::SyncOrderKeys.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::sync_model_from_order_keys, this, _1), gui_context());
+       Route::SyncOrderKeys.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::sync_treeview_from_order_keys, this, _1), gui_context());
 }
 
 bool
@@ -531,15 +534,15 @@ EditorRoutes::redisplay ()
 
         _editor->reset_controls_layout_height (position);
         _editor->reset_controls_layout_width ();
-       _editor->full_canvas_height = position + _editor->canvas_timebars_vsize;
-       _editor->vertical_adjustment.set_upper (_editor->full_canvas_height);
+       _editor->_full_canvas_height = position;
+       _editor->vertical_adjustment.set_upper (_editor->_full_canvas_height);
 
-       if ((_editor->vertical_adjustment.get_value() + _editor->_canvas_height) > _editor->vertical_adjustment.get_upper()) {
+       if ((_editor->vertical_adjustment.get_value() + _editor->_visible_canvas_height) > _editor->vertical_adjustment.get_upper()) {
                /*
                   We're increasing the size of the canvas while the bottom is visible.
                   We scroll down to keep in step with the controls layout.
                */
-               _editor->vertical_adjustment.set_value (_editor->full_canvas_height - _editor->_canvas_height);
+               _editor->vertical_adjustment.set_value (_editor->_full_canvas_height - _editor->_visible_canvas_height);
        }
 }
 
@@ -550,14 +553,14 @@ EditorRoutes::route_deleted (Gtk::TreeModel::Path const &)
           as when a row/route is actually deleted.
        */
        DEBUG_TRACE (DEBUG::OrderKeys, "editor routes treeview row deleted\n");
-       sync_order_keys_from_model ();
+       sync_order_keys_from_treeview ();
 }
 
 void
 EditorRoutes::reordered (TreeModel::Path const &, TreeModel::iterator const &, int* /*what*/)
 {
        DEBUG_TRACE (DEBUG::OrderKeys, "editor routes treeview reordered\n");
-       sync_order_keys_from_model ();
+       sync_order_keys_from_treeview ();
 }
 
 void
@@ -598,9 +601,12 @@ void
 EditorRoutes::routes_added (list<RouteTimeAxisView*> routes)
 {
        TreeModel::Row row;
+       PBD::Unwinder<bool> at (_adding_routes, true);
 
        suspend_redisplay ();
 
+       _display.set_model (Glib::RefPtr<ListStore>());
+
        for (list<RouteTimeAxisView*>::iterator x = routes.begin(); x != routes.end(); ++x) {
 
                boost::shared_ptr<MidiTrack> midi_trk = boost::dynamic_pointer_cast<MidiTrack> ((*x)->route());
@@ -651,6 +657,7 @@ EditorRoutes::routes_added (list<RouteTimeAxisView*> routes)
                (*x)->route()->solo_isolated_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_isolate_display, this), gui_context());
                (*x)->route()->solo_safe_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_safe_display, this), gui_context());
                (*x)->route()->active_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_active_display, this), gui_context ());
+
        }
 
        update_rec_display ();
@@ -660,17 +667,21 @@ EditorRoutes::routes_added (list<RouteTimeAxisView*> routes)
        update_solo_safe_display ();
        update_input_active_display ();
        update_active_display ();
+
        resume_redisplay ();
+       _display.set_model (_model);
 
        /* now update route order keys from the treeview/track display order */
 
-       sync_order_keys_from_model ();
+       sync_order_keys_from_treeview ();
 }
 
 void
 EditorRoutes::handle_gui_changes (string const & what, void*)
 {
-       ENSURE_GUI_THREAD (*this, &EditorRoutes::handle_gui_changes, what, src)
+       if (_adding_routes) {
+               return;
+       }
 
        if (what == "track_height") {
                /* Optional :make tracks change height while it happens, instead
@@ -760,7 +771,7 @@ EditorRoutes::update_visibility ()
        /* force route order keys catch up with visibility changes
         */
 
-       sync_order_keys_from_model ();
+       sync_order_keys_from_treeview ();
 
        resume_redisplay ();
 }
@@ -799,7 +810,57 @@ EditorRoutes::show_track_in_display (TimeAxisView& tv)
 }
 
 void
-EditorRoutes::sync_order_keys_from_model ()
+EditorRoutes::reset_remote_control_ids ()
+{
+       if (Config->get_remote_model() != EditorOrdered || !_session || _session->deletion_in_progress()) {
+               return;
+       }
+
+       TreeModel::Children rows = _model->children();
+       
+       if (rows.empty()) {
+               return;
+       }
+
+       
+       DEBUG_TRACE (DEBUG::OrderKeys, "editor reset remote control ids\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)[_columns.route];
+               bool visible = (*ri)[_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 (EditorSort, 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
+EditorRoutes::sync_order_keys_from_treeview ()
 {
        if (_ignore_reorder || !_session || _session->deletion_in_progress()) {
                return;
@@ -811,42 +872,65 @@ EditorRoutes::sync_order_keys_from_model ()
                return;
        }
 
-       DEBUG_TRACE (DEBUG::OrderKeys, "editor sync order keys from model\n");
+       
+       DEBUG_TRACE (DEBUG::OrderKeys, "editor sync order keys from treeview\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;
 
        for (ri = rows.begin(); ri != rows.end(); ++ri) {
+
                boost::shared_ptr<Route> route = (*ri)[_columns.route];
                bool visible = (*ri)[_columns.visible];
 
                uint32_t old_key = route->order_key (EditorSort);
-               uint32_t new_key;
 
-               if (!visible) {
-                       new_key = UINT_MAX;
-               } else {
-                       new_key = order;
-               }
+               if (order != old_key) {
+                       route->set_order_key (EditorSort, order);
 
-               if (old_key != new_key) {
-                       route->set_order_key (EditorSort, new_key);
                        changed = true;
                }
 
-               order++;
+               if ((Config->get_remote_model() == EditorOrdered) && !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 (EditorSort, new_rid);      
+                               rid_change = true;
+                       }
+                       
+                       if (visible) {
+                               rid++;
+                       }
+
+               }
+
+               ++order;
        }
        
        if (changed) {
                /* tell the world that we changed the editor sort keys */
                _session->sync_order_keys (EditorSort);
        }
+
+       if (rid_change) {
+               /* tell the world that we changed the remote control IDs */
+               _session->notify_remote_id_change ();
+       }
 }
 
 void
-EditorRoutes::sync_model_from_order_keys (RouteSortOrderKey src)
+EditorRoutes::sync_treeview_from_order_keys (RouteSortOrderKey src)
 {
+       /* Some route order key(s) for `src' has been changed, make sure that 
+          we update out tree/list model and GUI to reflect the change.
+       */
+
        if (!_session || _session->deletion_in_progress()) {
                return;
        }
@@ -880,22 +964,40 @@ EditorRoutes::sync_model_from_order_keys (RouteSortOrderKey src)
 
        vector<int> neworder;
        TreeModel::Children rows = _model->children();
-       uint32_t n = 0;
+       uint32_t old_order = 0;
+       bool changed = false;
 
        if (rows.empty()) {
                return;
        }
 
-       neworder.assign (rows.size(), 0);
+       OrderKeySortedRoutes sorted_routes;
 
-       for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri, ++n) {
+       for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri, ++old_order) {
                boost::shared_ptr<Route> route = (*ri)[_columns.route];
-               neworder[route->order_key (EditorSort)] = n;
-               DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("editor change order for %1 to %2\n",
-                                                              route->name(), route->order_key (MixerSort)));
+               sorted_routes.push_back (RoutePlusOrderKey (route, old_order, route->order_key (EditorSort)));
+       }
+
+       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 ("EDITOR change order for %1 from %2 to %3\n",
+                                                              sr->route->name(), sr->old_display_order, n));
        }
 
-       {
+       if (changed) {
                Unwinder<bool> uw (_ignore_reorder, true);
                _model->reorder (neworder);
        }
@@ -949,13 +1051,14 @@ EditorRoutes::set_all_tracks_visibility (bool yn)
                        continue;
                }
 
+               tv->set_marked_for_display (yn);
                (*i)[_columns.visible] = yn;
        }
 
        /* force route order keys catch up with visibility changes
         */
 
-       sync_order_keys_from_model ();
+       sync_order_keys_from_treeview ();
 
        resume_redisplay ();
 }
@@ -1017,7 +1120,7 @@ EditorRoutes::set_all_audio_midi_visibility (int tracks, bool yn)
        /* force route order keys catch up with visibility changes
         */
 
-       sync_order_keys_from_model ();
+       sync_order_keys_from_treeview ();
 
        resume_redisplay ();
 }
@@ -1104,10 +1207,12 @@ EditorRoutes::key_press (GdkEventKey* ev)
                 break;
 
         case 's':
-               if (Config->get_solo_control_is_listen_control()) {
-                       _session->set_listen (rl, !rl->front()->listening_via_monitor(), Session::rt_cleanup);
-               } else {
-                       _session->set_solo (rl, !rl->front()->self_soloed(), Session::rt_cleanup);
+                if (get_relevant_routes (rl)) {
+                       if (Config->get_solo_control_is_listen_control()) {
+                               _session->set_listen (rl, !rl->front()->listening_via_monitor(), Session::rt_cleanup);
+                       } else {
+                               _session->set_solo (rl, !rl->front()->self_soloed(), Session::rt_cleanup);
+                       }
                }
                 return true;
                 break;
@@ -1200,8 +1305,8 @@ EditorRoutes::button_press (GdkEventButton* ev)
                int y_pos = tv->y_position();
 
                //Clamp the y pos so that we do not extend beyond the canvas full height.
-               if (_editor->full_canvas_height - y_pos < _editor->_canvas_height){
-                   y_pos = _editor->full_canvas_height - _editor->_canvas_height;
+               if (_editor->_full_canvas_height - y_pos < _editor->_visible_canvas_height){
+                   y_pos = _editor->_full_canvas_height - _editor->_visible_canvas_height;
                }
 
                //Only scroll to if the track is visible