various tweaks to get clicks on processor "buttons" to work right and to keep track...
[ardour.git] / gtk2_ardour / editor_regions.cc
index 30fce08a932f5b6e8046ffcfabcef74d19e52660..c6c8f1e9149874165e16117c8a16b300337bb4de 100644 (file)
 #include "ardour/silentfilesource.h"
 #include "ardour/profile.h"
 
+#include "gtkmm2ext/choice.h"
 #include "gtkmm2ext/treeutils.h"
 
+#include "audio_clock.h"
 #include "editor.h"
 #include "editing.h"
 #include "keyboard.h"
@@ -63,15 +65,16 @@ EditorRegions::EditorRegions (Editor* e)
        , name_editable (0)
        , _menu (0)
        , _show_automatic_regions (true)
-       , _sort_type ((Editing::RegionListSortType) 0)
-       , _no_redisplay (false) 
        , ignore_region_list_selection_change (false)
        , ignore_selected_region_change (false)
+       , _no_redisplay (false)
+       , _sort_type ((Editing::RegionListSortType) 0)
        , expanded (false)
 {
        _display.set_size_request (100, -1);
        _display.set_name ("RegionListDisplay");
        _display.set_rules_hint (true);
+
        /* Try to prevent single mouse presses from initiating edits.
           This relies on a hack in gtktreeview.c:gtk_treeview_button_press()
        */
@@ -117,27 +120,31 @@ EditorRegions::EditorRegions (Editor* e)
        CellRendererToggle* locked_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (7));
        locked_cell->property_activatable() = true;
        locked_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRegions::locked_changed));
+
        TreeViewColumn* locked_col = _display.get_column (7);
        locked_col->add_attribute (locked_cell->property_visible(), _columns.property_toggles_visible);
 
        CellRendererToggle* glued_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (8));
        glued_cell->property_activatable() = true;
        glued_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRegions::glued_changed));
+
        TreeViewColumn* glued_col = _display.get_column (8);
        glued_col->add_attribute (glued_cell->property_visible(), _columns.property_toggles_visible);
 
        CellRendererToggle* muted_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (9));
        muted_cell->property_activatable() = true;
        muted_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRegions::muted_changed));
+
        TreeViewColumn* muted_col = _display.get_column (9);
        muted_col->add_attribute (muted_cell->property_visible(), _columns.property_toggles_visible);
 
        CellRendererToggle* opaque_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (10));
        opaque_cell->property_activatable() = true;
        opaque_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRegions::opaque_changed));
+
        TreeViewColumn* opaque_col = _display.get_column (10);
        opaque_col->add_attribute (opaque_cell->property_visible(), _columns.property_toggles_visible);
-       
+
        _display.get_selection()->set_mode (SELECTION_MULTIPLE);
        _display.add_object_drag (_columns.region.index(), "regions");
 
@@ -168,9 +175,12 @@ EditorRegions::EditorRegions (Editor* e)
        // _display.signal_popup_menu().connect (sigc::bind (sigc::mem_fun (*this, &Editor::show__display_context_menu), 1, 0));
 
        //ARDOUR_UI::instance()->secondary_clock.mode_changed.connect (sigc::mem_fun(*this, &Editor::redisplay_regions));
-       ARDOUR_UI::instance()->secondary_clock.mode_changed.connect (sigc::mem_fun(*this, &EditorRegions::update_all_rows));
+       ARDOUR_UI::instance()->secondary_clock->mode_changed.connect (sigc::mem_fun(*this, &EditorRegions::update_all_rows));
        ARDOUR::Region::RegionPropertyChanged.connect (region_property_connection, MISSING_INVALIDATOR, ui_bind (&EditorRegions::region_changed, this, _1, _2), gui_context());
        ARDOUR::RegionFactory::CheckNewRegion.connect (check_new_region_connection, MISSING_INVALIDATOR, ui_bind (&EditorRegions::add_region, this, _1), gui_context());
+
+       e->EditorFreeze.connect (editor_freeze_connection, MISSING_INVALIDATOR, ui_bind (&EditorRegions::freeze_tree_model, this), gui_context());
+       e->EditorThaw.connect (editor_thaw_connection, MISSING_INVALIDATOR, ui_bind (&EditorRegions::thaw_tree_model, this), gui_context());
 }
 
 bool
@@ -204,9 +214,13 @@ EditorRegions::focus_out (GdkEventFocus*)
 }
 
 bool
-EditorRegions::enter_notify (GdkEventCrossing* ev)
+EditorRegions::enter_notify (GdkEventCrossing*)
 {
-       /* arm counter so that ::selection_filter() will deny selecting anything for the 
+       if (name_editable) {
+               return true;
+       }
+
+       /* arm counter so that ::selection_filter() will deny selecting anything for the
           next two attempts to change selection status.
        */
        _scroller.grab_focus ();
@@ -222,7 +236,6 @@ EditorRegions::leave_notify (GdkEventCrossing*)
                old_focus = 0;
        }
 
-       name_editable = 0;
        Keyboard::magic_widget_drop_focus ();
        return false;
 }
@@ -234,14 +247,6 @@ EditorRegions::set_session (ARDOUR::Session* s)
        redisplay ();
 }
 
-void
-EditorRegions::add_regions (vector<boost::shared_ptr<Region> >& regions)
-{
-       for (vector<boost::shared_ptr<Region> >::iterator x = regions.begin(); x != regions.end(); ++x) {
-               add_region (*x);
-       }
-}
-
 void
 EditorRegions::add_region (boost::shared_ptr<Region> region)
 {
@@ -259,6 +264,7 @@ EditorRegions::add_region (boost::shared_ptr<Region> region)
        }
 
        if (region->hidden()) {
+
                TreeModel::iterator iter = _model->get_iter ("0");
                TreeModel::Row parent;
                TreeModel::Row child;
@@ -279,6 +285,7 @@ EditorRegions::add_region (boost::shared_ptr<Region> region)
                                parent = *iter;
                        }
                }
+
                row = *(_model->append (parent.children()));
 
        } else if (region->whole_file()) {
@@ -354,31 +361,28 @@ EditorRegions::add_region (boost::shared_ptr<Region> region)
                        }
                }
 
-               if (region->automatic()) {
-                       return;
-               }
+               region_row_map.insert(pair<boost::shared_ptr<ARDOUR::Region>, Gtk::TreeModel::RowReference>(region, TreeRowReference(_model, TreePath (row))) );
+               parent_regions_sources_map.insert(pair<string, Gtk::TreeModel::RowReference>(region->source_string(), TreeRowReference(_model, TreePath (row))) );
+
+               return;
 
        } else {
+               // find parent node, add as new child
+               TreeModel::iterator i;
 
-               /* find parent node, add as new child */
+               boost::unordered_map<string, Gtk::TreeModel::RowReference>::iterator it;
 
-               TreeModel::iterator i;
-               TreeModel::Children rows = _model->children();
-               bool found_parent = false;
+               it = parent_regions_sources_map.find (region->source_string());
 
-               for (i = rows.begin(); i != rows.end(); ++i) {
-                       boost::shared_ptr<Region> r = (*i)[_columns.region];
+               if (it != parent_regions_sources_map.end()){
 
-                       if (r && r->whole_file()) {
+                       TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
 
-                               if (region->source_equivalent (r)) {
-                                       found_parent = true;
-                               }
-                       }
-                        
                        TreeModel::iterator ii;
-                       TreeModel::Children subrows = (*i).children();
+                       TreeModel::Children subrows = (*j).children();
 
+                       /* XXXX: should we be accounting for all regions? */
+                       /*
                        for (ii = subrows.begin(); ii != subrows.end(); ++ii) {
                                boost::shared_ptr<Region> rr = (*ii)[_columns.region];
 
@@ -386,14 +390,11 @@ EditorRegions::add_region (boost::shared_ptr<Region> region)
                                        return;
                                }
                        }
+                       */
 
-                       if (found_parent) {
-                               row = *(_model->append ((*i).children()));
-                               break;
-                       }
-               }
-
-               if (!found_parent) {
+                       row = *(_model->insert (subrows.end()));
+                       
+               } else {
                        row = *(_model->append());
                }
 
@@ -402,9 +403,37 @@ EditorRegions::add_region (boost::shared_ptr<Region> region)
 
        row[_columns.region] = region;
 
+       region_row_map.insert(pair<boost::shared_ptr<ARDOUR::Region>, Gtk::TreeModel::RowReference>(region, TreeRowReference(_model, TreePath (row))) );
+
        populate_row(region, (*row));
 }
 
+void
+EditorRegions::delete_unused_regions ()
+{
+       vector<string> choices;
+       string prompt;
+
+       if (!_session) {
+               return;
+       }
+
+       prompt  = _("Do you really want to remove unused regions?"
+                   "\n(This is destructive and cannot be undone)");
+
+       choices.push_back (_("No, do nothing."));
+       choices.push_back (_("Yes, remove."));
+
+       Gtkmm2ext::Choice prompter (_("Remove unused regions"), prompt, choices);
+
+       if (prompter.run () == 1) {
+               _no_redisplay = true;
+               _session->cleanup_regions ();
+               _no_redisplay = false;
+               redisplay ();
+       }
+}
+
 void
 EditorRegions::region_changed (boost::shared_ptr<Region> r, const PropertyChange& what_changed)
 {
@@ -420,101 +449,43 @@ EditorRegions::region_changed (boost::shared_ptr<Region> r, const PropertyChange
        our_interests.add (ARDOUR::Properties::opaque);
        our_interests.add (ARDOUR::Properties::fade_in);
        our_interests.add (ARDOUR::Properties::fade_out);
-       
-       if (last_row != 0) {
-
-               TreeModel::iterator j = _model->get_iter (last_row.get_path());
-               boost::shared_ptr<Region> c = (*j)[_columns.region];
-
-               if (c == r) {
-                       populate_row (r, (*j));
-                       
-                       if (what_changed.contains (ARDOUR::Properties::hidden)) {
-                               redisplay ();
-                       }
-                       
-                       return;
-               }
-       }
 
        if (what_changed.contains (our_interests)) {
 
-               /* find the region in our model and update its row */
-               TreeModel::Children rows = _model->children ();
-               TreeModel::iterator i = rows.begin ();
-               
-               while (i != rows.end ()) {
+               if (last_row != 0) {
 
-                       TreeModel::Children children = (*i)->children ();
-                       TreeModel::iterator found = children.end ();
-                       
-                       boost::shared_ptr<Region> c = (*i)[_columns.region];
+                       TreeModel::iterator j = _model->get_iter (last_row.get_path());
+                       boost::shared_ptr<Region> c = (*j)[_columns.region];
 
                        if (c == r) {
+                               populate_row (r, (*j));
 
-                               /* check this row */
-                               last_row = TreeRowReference (_model, TreePath (i));
-                               found = i;
-
-                       } else {
-
-                               /* check its children */
-                               
-                               found = children.begin ();
-                               while (found != children.end()) {
-                         
-                                       boost::shared_ptr<Region> c = (*found)[_columns.region];
-                                       
-                                       if (c == r) {
-                                               last_row = TreeRowReference(_model, TreePath (found));
-                                               break;
-                                       }
-                                       ++found;
+                               if (what_changed.contains (ARDOUR::Properties::hidden)) {
+                                       redisplay ();
                                }
+
+                               return;
                        }
+               }
 
-                       if (found != children.end()) {
+               RegionRowMap::iterator it;
 
-                               boost::shared_ptr<AudioRegion> audioregion = boost::dynamic_pointer_cast<AudioRegion>(r);
-                               uint32_t used = _editor->get_regionview_count_from_region_list (r);
+               it = region_row_map.find (r);
 
-                               if (what_changed.contains (ARDOUR::Properties::name)) {
-                                       populate_row_name (r, *found);
-                               }
-                               if (what_changed.contains (ARDOUR::Properties::position)) {
-                                       populate_row_position (r, *found, used);
-                                       populate_row_end (r, *found, used);
-                               }
-                               if (what_changed.contains (ARDOUR::Properties::length)) {
-                                       populate_row_end (r, *found, used);
-                                       populate_row_length (r, *found);
-                               }
-                               if (what_changed.contains (ARDOUR::Properties::start)) {
-                                       populate_row_length (r, *found);
-                               }
-                               if (what_changed.contains (ARDOUR::Properties::locked)) {
-                                       populate_row_locked (r, *found, used);
-                               }
-                               if (what_changed.contains (ARDOUR::Properties::position_lock_style)) {
-                                       populate_row_glued (r, *found, used);
-                               }
-                               if (what_changed.contains (ARDOUR::Properties::muted)) {
-                                       populate_row_muted (r, *found, used);
-                               }
-                               if (what_changed.contains (ARDOUR::Properties::opaque)) {
-                                       populate_row_opaque (r, *found, used);
-                               }
-                               if (what_changed.contains (ARDOUR::Properties::fade_in)) {
-                                       populate_row_fade_in (r, *found, used, audioregion);
-                               }
-                               if (what_changed.contains (ARDOUR::Properties::fade_out)) {
-                                       populate_row_fade_out (r, *found, used, audioregion);
+               if (it != region_row_map.end()){
+
+                       TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
+                       boost::shared_ptr<Region> c = (*j)[_columns.region];
+
+                       if (c == r) {
+                               populate_row (r, (*j));
+
+                               if (what_changed.contains (ARDOUR::Properties::hidden)) {
+                                       redisplay ();
                                }
 
-                               break;
+                               return;
                        }
-
-                       ++i;
                }
        }
 
@@ -541,7 +512,7 @@ EditorRegions::selection_changed ()
 
                for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
 
-                       if (iter = _model->get_iter (*i)) { 
+                       if (iter = _model->get_iter (*i)) {
                                boost::shared_ptr<Region> region = (*iter)[_columns.region];
 
                                // they could have clicked on a row that is just a placeholder, like "Hidden"
@@ -549,7 +520,7 @@ EditorRegions::selection_changed ()
                                // since we need a region ptr.
 
                                if (region) {
-                                        
+
                                        if (region->automatic()) {
 
                                                _display.get_selection()->unselect(*i);
@@ -573,69 +544,21 @@ EditorRegions::selection_changed ()
 void
 EditorRegions::set_selected (RegionSelection& regions)
 {
-       TreeModel::Children rows = _model->children();
+       for (RegionSelection::iterator i = regions.begin(); i != regions.end(); ++i) {
 
-       for (RegionSelection::iterator iter = regions.begin(); iter != regions.end(); ++iter) {
+               boost::shared_ptr<Region> r ((*i)->region());
 
-               TreeModel::iterator i;
-               
-               boost::shared_ptr<Region> r ((*iter)->region());
-
-               for (i = rows.begin(); i != rows.end(); ++i) {
+               RegionRowMap::iterator it;
 
-                       boost::shared_ptr<Region> compared_region = (*i)[_columns.region];
+               it = region_row_map.find (r);
 
-                       if (r == compared_region) {
-                               _display.get_selection()->select(*i);
-                               break;
-                       }
-
-                       if (!(*i).children().empty()) {
-                               if (set_selected_in_subrow(r, (*i), 2)) {
-                                       break;
-                               }
-                       }
+               if (it != region_row_map.end()){
+                       TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
+                       _display.get_selection()->select(*j);
                }
        }
 }
 
-bool
-EditorRegions::set_selected_in_subrow (boost::shared_ptr<Region> region, TreeModel::Row const &parent_row, int level)
-{
-       TreeModel::iterator i;
-       TreeModel::Children subrows = (*parent_row).children();
-
-       for (i = subrows.begin(); i != subrows.end(); ++i) {
-
-               boost::shared_ptr<Region> compared_region = (*i)[_columns.region];
-
-               if (region == compared_region) {
-                       _display.get_selection()->select(*i);
-                       return true;
-               }
-
-               if (!(*i).children().empty()) {
-                       if (set_selected_in_subrow (region, (*i), level + 1)) {
-                               return true;
-                       }
-               }
-       }
-       
-       return false;
-}
-
-void
-EditorRegions::insert_into_tmp_regionlist(boost::shared_ptr<Region> region)
-{
-       /* keep all whole files at the beginning */
-
-       if (region->whole_file()) {
-               tmp_region_list.push_front (region);
-       } else {
-               tmp_region_list.push_back (region);
-       }
-}
-
 void
 EditorRegions::redisplay ()
 {
@@ -652,23 +575,36 @@ EditorRegions::redisplay ()
 
        _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
        _model->clear ();
+       _model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
 
-       /* now add everything we have, via a temporary list used to help with sorting */
 
-       tmp_region_list.clear();
+       region_row_map.clear();
+       parent_regions_sources_map.clear();
+
+       /* now add everything we have, via a temporary list used to help with sorting */
 
        const RegionFactory::RegionMap& regions (RegionFactory::regions());
+
        for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end(); ++i) {
-               insert_into_tmp_regionlist (i->second);
+
+               if ( i->second->whole_file()) {
+                       /* add automatic regions first so that children can find their parents as we add them */
+                       add_region (i->second);
+                       continue;
+               }
+
+               tmp_region_list.push_front (i->second);
        }
 
        for (list<boost::shared_ptr<Region> >::iterator r = tmp_region_list.begin(); r != tmp_region_list.end(); ++r) {
                add_region (*r);
        }
-       tmp_region_list.clear();
 
+       _model->set_sort_column (0, SORT_ASCENDING); // renabale sorting
        _display.set_model (_model);
 
+       tmp_region_list.clear();
+
        if (tree_expanded) {
                _display.expand_all();
        }
@@ -681,59 +617,15 @@ EditorRegions::update_row (boost::shared_ptr<Region> region)
                return;
        }
 
-       TreeModel::iterator i;
-       TreeModel::Children rows = _model->children();
-       
-       return;
-
-       for (i = rows.begin(); i != rows.end(); ++i) {
+       RegionRowMap::iterator it;
 
-//             cerr << "Level 1: Compare " << region->name() << " with parent " << (*i)[_columns.name] << "\n";
+       it = region_row_map.find (region);
 
-               boost::shared_ptr<Region> compared_region = (*i)[_columns.region];
+       if (it != region_row_map.end()){
 
-               if (region == compared_region) {
-//                     cerr << "Matched\n";
-                       populate_row(region, (*i));
-                       return;
-               }
-
-               if (!(*i).children().empty()) {
-                       if (update_subrows(region, (*i), 2)) {
-                               return;
-                       }
-               }
-       }
-
-//     cerr << "Returning - No match\n";
-}
-
-bool
-EditorRegions::update_subrows (boost::shared_ptr<Region> region, TreeModel::Row const &parent_row, int level)
-{
-       TreeModel::iterator i;
-       TreeModel::Children subrows = (*parent_row).children();
-
-       for (i = subrows.begin(); i != subrows.end(); ++i) {
-
-//             cerr << "Level " << level << ": Compare " << region->name() << " with child " << (*i)[_columns.name] << "\n";
-
-               boost::shared_ptr<Region> compared_region = (*i)[_columns.region];
-
-               if (region == compared_region) {
-                       populate_row(region, (*i));
-//                     cerr << "Matched\n";
-                       return true;
-               }
-
-               if (!(*i).children().empty()) {
-                       if (update_subrows (region, (*i), level + 1)) {
-                               return true;
-                       }
-               }
+               TreeModel::iterator j = _model->get_iter ((*it).second.get_path());
+               populate_row(region, (*j));
        }
-
-       return false;
 }
 
 void
@@ -742,40 +634,17 @@ EditorRegions::update_all_rows ()
        if (!_session) {
                return;
        }
-       
-       TreeModel::iterator i;
-       TreeModel::Children rows = _model->children();
 
-       for (i = rows.begin(); i != rows.end(); ++i) {
-
-               boost::shared_ptr<Region> region = (*i)[_columns.region];
-
-               if (!region->automatic()) {
-                       populate_row(region, (*i));
-               }
-
-               if (!(*i).children().empty()) {
-                       update_all_subrows ((*i), 2);
-               }
-       }
-}
+       RegionRowMap::iterator i;
 
-void
-EditorRegions::update_all_subrows (TreeModel::Row const &parent_row, int level)
-{
-       TreeModel::iterator i;
-       TreeModel::Children subrows = (*parent_row).children();
+       for (i = region_row_map.begin(); i != region_row_map.end(); ++i) {
 
-       for (i = subrows.begin(); i != subrows.end(); ++i) {
+               TreeModel::iterator j = _model->get_iter ((*i).second.get_path());
 
-               boost::shared_ptr<Region> region = (*i)[_columns.region];
+               boost::shared_ptr<Region> region = (*j)[_columns.region];
 
                if (!region->automatic()) {
-                       populate_row(region, (*i));
-               }
-
-               if (!(*i).children().empty()) {
-                       update_all_subrows ((*i), level + 1);
+                       populate_row(region, (*j));
                }
        }
 }
@@ -783,10 +652,10 @@ EditorRegions::update_all_subrows (TreeModel::Row const &parent_row, int level)
 void
 EditorRegions::format_position (framepos_t pos, char* buf, size_t bufsize)
 {
-       BBT_Time bbt;
+       Timecode::BBT_Time bbt;
        Timecode::Time timecode;
 
-       switch (ARDOUR_UI::instance()->secondary_clock.mode ()) {
+       switch (ARDOUR_UI::instance()->secondary_clock->mode ()) {
        case AudioClock::BBT:
                _session->tempo_map().bbt_time (pos, bbt);
                snprintf (buf, bufsize, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
@@ -812,7 +681,6 @@ EditorRegions::format_position (framepos_t pos, char* buf, size_t bufsize)
                break;
 
        case AudioClock::Timecode:
-       case AudioClock::Off: /* If the secondary clock is off, default to Timecode */
        default:
                _session->timecode_time (pos, timecode);
                snprintf (buf, bufsize, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
@@ -824,7 +692,9 @@ void
 EditorRegions::populate_row (boost::shared_ptr<Region> region, TreeModel::Row const &row)
 {
        boost::shared_ptr<AudioRegion> audioregion = boost::dynamic_pointer_cast<AudioRegion>(region);
-       uint32_t used = _session->playlists->region_use_count (region);
+       //uint32_t used = _session->playlists->region_use_count (region);
+       /* Presently a region is only used once so let's save on the sequential scan to determine use count */
+       uint32_t used = 1;
 
        populate_row_position (region, row, used);
        populate_row_end (region, row, used);
@@ -873,7 +743,7 @@ EditorRegions::populate_row (boost::shared_ptr<Region> region, TreeModel::Row co
 #endif
 
 void
-EditorRegions::populate_row_used (boost::shared_ptr<Region> region, TreeModel::Row const& row, uint32_t used)
+EditorRegions::populate_row_used (boost::shared_ptr<Region>, TreeModel::Row const& row, uint32_t used)
 {
        char buf[8];
        snprintf (buf, sizeof (buf), "%4d" , used);
@@ -949,7 +819,7 @@ EditorRegions::populate_row_fade_in (boost::shared_ptr<Region> region, TreeModel
                        char buf[16];
                        format_position (audioregion->fade_in()->back()->when, buf, sizeof (buf));
                        row[_columns.fadein] = buf;
-                        
+
                        if (audioregion->fade_in_active()) {
                                row[_columns.fadein] = string_compose("%1%2%3", " ", buf, " ");
                        } else {
@@ -970,16 +840,16 @@ EditorRegions::populate_row_fade_out (boost::shared_ptr<Region> region, TreeMode
                } else {
                        char buf[16];
                        format_position (audioregion->fade_out()->back()->when, buf, sizeof (buf));
-                        
+
                        if (audioregion->fade_out_active()) {
                                row[_columns.fadeout] = string_compose("%1%2%3", " ", buf, " ");
                        } else {
                                row[_columns.fadeout] = string_compose("%1%2%3", "(", buf, ")");
                        }
-               } 
+               }
        }
 }
-        
+
 void
 EditorRegions::populate_row_locked (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
 {
@@ -1034,7 +904,7 @@ EditorRegions::populate_row_name (boost::shared_ptr<Region> region, TreeModel::R
        } else {
                row[_columns.name] = region->name();
        }
-}        
+}
 
 void
 EditorRegions::populate_row_source (boost::shared_ptr<Region> region, TreeModel::Row const &row)
@@ -1088,7 +958,7 @@ EditorRegions::show_context_menu (int button, int time)
 
        bool have_shown = false;
        bool have_hidden = false;
-       
+
        TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
        for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
                TreeIter t = _model->get_iter (*i);
@@ -1116,7 +986,7 @@ EditorRegions::key_press (GdkEventKey* ev)
        switch (ev->keyval) {
        case GDK_Tab:
        case GDK_ISO_Left_Tab:
-                
+
                if (name_editable) {
                        name_editable->editing_done ();
                        name_editable = 0;
@@ -1361,7 +1231,7 @@ void
 EditorRegions::name_editing_started (CellEditable* ce, const Glib::ustring&)
 {
        name_editable = ce;
-        
+
        /* give it a special name */
 
        Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ce);
@@ -1370,7 +1240,7 @@ EditorRegions::name_editing_started (CellEditable* ce, const Glib::ustring&)
                e->set_name (X_("RegionNameEditorEntry"));
        }
 }
-                          
+
 void
 EditorRegions::name_edit (const std::string& path, const std::string& new_text)
 {
@@ -1410,7 +1280,7 @@ EditorRegions::get_dragged_region ()
        if (regions.empty()) {
                return boost::shared_ptr<Region> ();
        }
-       
+
        assert (regions.size() == 1);
        return regions.front ();
 }
@@ -1421,6 +1291,10 @@ EditorRegions::clear ()
        _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
        _model->clear ();
        _display.set_model (_model);
+
+       /* Clean up the maps */
+       region_row_map.clear();
+       parent_regions_sources_map.clear();
 }
 
 boost::shared_ptr<Region>
@@ -1445,6 +1319,25 @@ EditorRegions::get_single_selection ()
        return (*iter)[_columns.region];
 }
 
+void
+EditorRegions::freeze_tree_model (){
+
+       _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
+       _model->set_sort_column (-2, SORT_ASCENDING); //Disable sorting to gain performance
+
+}
+
+void
+EditorRegions::thaw_tree_model (){
+
+       _model->set_sort_column (0, SORT_ASCENDING); // renabale sorting
+       _display.set_model (_model);
+
+       if (toggle_full_action()->get_active()) {
+               _display.expand_all();
+       }
+}
+
 void
 EditorRegions::locked_changed (std::string const & path)
 {
@@ -1512,7 +1405,7 @@ EditorRegions::get_state () const
 
        return *node;
 }
-               
+
 void
 EditorRegions::set_state (const XMLNode & node)
 {
@@ -1523,28 +1416,35 @@ EditorRegions::set_state (const XMLNode & node)
        }
 
        XMLProperty const * p = node.property (X_("sort-type"));
+
        if (p) {
                Editing::RegionListSortType const t = static_cast<Editing::RegionListSortType> (string_2_enum (p->value(), _sort_type));
+
                if (_sort_type != t) {
                        changed = true;
                }
+
                reset_sort_type (t, true);
                RefPtr<RadioAction> ract = sort_type_action (t);
                ract->set_active ();
        }
 
        p = node.property (X_("sort-ascending"));
+
        if (p) {
                bool const yn = string_is_affirmative (p->value ());
                SortType old_sort_type;
                int old_sort_column;
 
                _model->get_sort_column_id (old_sort_column, old_sort_type);
+
                if (old_sort_type != (yn ? SORT_ASCENDING : SORT_DESCENDING)) {
                        changed = true;
                }
+
                reset_sort_direction (yn);
                RefPtr<Action> act;
+
                if (yn) {
                        act = ActionManager::get_action (X_("RegionList"), X_("SortAscending"));
                } else {
@@ -1557,9 +1457,11 @@ EditorRegions::set_state (const XMLNode & node)
        p = node.property (X_("show-all"));
        if (p) {
                bool const yn = string_is_affirmative (p->value ());
+
                if (expanded != yn) {
                        changed = true;
                }
+
                set_full (yn);
                toggle_full_action()->set_active (yn);
        }
@@ -1567,13 +1469,14 @@ EditorRegions::set_state (const XMLNode & node)
        p = node.property (X_("show-automatic-regions"));
        if (p) {
                bool const yn = string_is_affirmative (p->value ());
+
                if (yn != _show_automatic_regions) {
                        _show_automatic_regions = yn;
                        toggle_show_auto_regions_action()->set_active (yn);
                        changed = true;
                }
        }
-        
+
        if (changed) {
                redisplay ();
        }
@@ -1630,7 +1533,7 @@ RefPtr<Action>
 EditorRegions::hide_action () const
 {
        return ActionManager::get_action (X_("RegionList"), X_("rlHide"));
-       
+
 }
 
 RefPtr<Action>
@@ -1639,6 +1542,12 @@ EditorRegions::show_action () const
        return ActionManager::get_action (X_("RegionList"), X_("rlShow"));
 }
 
+RefPtr<Action>
+EditorRegions::delete_unused_regions_action () const
+{
+       return ActionManager::get_action (X_("RegionList"), X_("removeUnusedRegions"));
+}
+
 RefPtr<ToggleAction>
 EditorRegions::toggle_full_action () const
 {