Clean up checks.
[ardour.git] / gtk2_ardour / editor_region_list.cc
index c9d2346539f18d8aad9f3a709b8fe2978510da92..338dc71221763678340ce037c9878c952f11662d 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2000 Paul Davis 
+    Copyright (C) 2000-2005 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
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <cstdlib>
 #include <cmath>
 #include <algorithm>
 #include <string>
+#include <sstream>
 
-#include <pbd/basename.h>
+#include "pbd/basename.h"
 
-#include <ardour/audioregion.h>
-#include <ardour/session_region.h>
+#include "ardour/audioregion.h"
+#include "ardour/audiofilesource.h"
+#include "ardour/silentfilesource.h"
+#include "ardour/session_region.h"
+#include "ardour/profile.h"
 
 #include <gtkmm2ext/stop_signal.h>
 
 #include "editor.h"
 #include "editing.h"
+#include "keyboard.h"
 #include "ardour_ui.h"
 #include "gui_thread.h"
+#include "actions.h"
+#include "region_view.h"
+#include "utils.h"
+
 
 #include "i18n.h"
 
 using namespace sigc;
 using namespace ARDOUR;
+using namespace PBD;
 using namespace Gtk;
+using namespace Glib;
 using namespace Editing;
 
-#define wave_cursor_width 43
-#define wave_cursor_height 61
-#define wave_cursor_x_hot 0
-#define wave_cursor_y_hot 25
-static const gchar wave_cursor_bits[] = {
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff,
-0x03,
-   0x02, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,
-0x02,
-   0x02, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,
-0x02,
-   0x02, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,
-0x02,
-   0x02, 0x04, 0x00, 0x00, 0x00, 0x02, 0x02, 0x04, 0x00, 0x04, 0x00,
-0x02,
-   0x02, 0x04, 0x00, 0x04, 0x00, 0x02, 0x02, 0x0c, 0x08, 0x0c, 0x00,
-0x02,
-   0x02, 0x1c, 0x08, 0x0c, 0x00, 0x02, 0x02, 0x1c, 0x08, 0x0c, 0x04,
-0x02,
-   0x02, 0x3c, 0x18, 0x0c, 0x04, 0x02, 0x02, 0x7c, 0x18, 0x1c, 0x0c,
-0x02,
-   0x82, 0xfc, 0x38, 0x1c, 0x0c, 0x02, 0xc2, 0xfc, 0x78, 0x3c, 0x1c,
-0x02,
-   0xe2, 0xfd, 0xf9, 0x7d, 0x1c, 0x02, 0xf2, 0xff, 0xfb, 0xff, 0x1c,
-0x02,
-   0xfa, 0xff, 0xfb, 0xff, 0x3f, 0x02, 0xfe, 0xff, 0xff, 0xff, 0xff,
-0x03,
-   0xfe, 0xff, 0xff, 0xff, 0xff, 0x03, 0xfa, 0xff, 0xff, 0xff, 0x3f,
-0x02,
-   0xf2, 0xff, 0xfb, 0xfd, 0x3c, 0x02, 0xe2, 0xfd, 0x7b, 0x7c, 0x1c,
-0x02,
-   0xc2, 0xfc, 0x39, 0x3c, 0x1c, 0x02, 0x82, 0xfc, 0x18, 0x1c, 0x1c,
-0x02,
-   0x02, 0xfc, 0x18, 0x1c, 0x0c, 0x02, 0x02, 0x7c, 0x18, 0x0c, 0x0c,
-0x02,
-   0x02, 0x3c, 0x08, 0x0c, 0x04, 0x02, 0x02, 0x1c, 0x08, 0x0c, 0x04,
-0x02,
-   0x02, 0x1c, 0x08, 0x0c, 0x00, 0x02, 0x02, 0x0c, 0x00, 0x04, 0x00,
-0x02,
-   0x02, 0x04, 0x00, 0x04, 0x00, 0x02, 0x02, 0x04, 0x00, 0x00, 0x00,
-0x02,
-   0x02, 0x04, 0x00, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,
-0x02,
-   0x02, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,
-0x02,
-   0x02, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,
-0x02,
-   0x02, 0x00, 0x00, 0x00, 0x00, 0x02, 0xfe, 0xff, 0xff, 0xff, 0xff,
-0x03,
-   0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
-
-#define wave_cursor_mask_width 43
-#define wave_cursor_mask_height 61
-#define wave_cursor_mask_x_hot 0
-#define wave_cursor_mask_y_hot 25
-static const gchar wave_cursor_mask_bits[] = {
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00,
-0x00,
-   0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0c, 0x08, 0x0c, 0x00,
-0x00,
-   0x00, 0x1c, 0x08, 0x0c, 0x00, 0x00, 0x00, 0x1c, 0x08, 0x0c, 0x04,
-0x00,
-   0x00, 0x3c, 0x18, 0x0c, 0x04, 0x00, 0x00, 0x7c, 0x18, 0x1c, 0x0c,
-0x00,
-   0x80, 0xfc, 0x38, 0x1c, 0x0c, 0x00, 0xc0, 0xfc, 0x78, 0x3c, 0x1c,
-0x00,
-   0xe0, 0xfd, 0xf9, 0x7d, 0x1c, 0x00, 0xf0, 0xff, 0xfb, 0xff, 0x1c,
-0x00,
-   0xf8, 0xff, 0xfb, 0xff, 0x3f, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
-0x07,
-   0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0xf8, 0xff, 0xff, 0xff, 0x3f,
-0x00,
-   0xf0, 0xff, 0xfb, 0xfd, 0x3c, 0x00, 0xe0, 0xfd, 0x7b, 0x7c, 0x1c,
-0x00,
-   0xc0, 0xfc, 0x39, 0x3c, 0x1c, 0x00, 0x80, 0xfc, 0x18, 0x1c, 0x1c,
-0x00,
-   0x00, 0xfc, 0x18, 0x1c, 0x0c, 0x00, 0x00, 0x7c, 0x18, 0x0c, 0x0c,
-0x00,
-   0x00, 0x3c, 0x08, 0x0c, 0x04, 0x00, 0x00, 0x1c, 0x08, 0x0c, 0x04,
-0x00,
-   0x00, 0x1c, 0x08, 0x0c, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x04, 0x00,
-0x00,
-   0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-0x00,
-   0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
-
-GdkCursor *wave_cursor = 0;
-
 void
-Editor::handle_audio_region_removed (AudioRegion* ignored)
+Editor::handle_region_removed (boost::weak_ptr<Region> wregion)
 {
+       ENSURE_GUI_THREAD (mem_fun (*this, &Editor::redisplay_regions));
        redisplay_regions ();
 }
 
 void
-Editor::handle_new_audio_region (AudioRegion *region)
+Editor::handle_new_regions (vector<boost::weak_ptr<Region> >& v)
 {
-       /* don't copy region - the one we are being notified
-          about belongs to the session, and so it will
-          never be edited.
-       */
-       add_audio_region_to_region_display (region);
+       ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::handle_new_regions), v));
+       add_regions_to_region_display (v);
 }
 
 void
-Editor::region_hidden (Region* r)
+Editor::region_hidden (boost::shared_ptr<Region> r)
 {
-       ENSURE_GUI_THREAD(bind (slot (*this, &Editor::region_hidden), r));      
-
+       ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::region_hidden), r));    
        redisplay_regions ();
 }
 
 void
-Editor::add_audio_region_to_region_display (AudioRegion *region)
+Editor::add_regions_to_region_display (vector<boost::weak_ptr<Region> >& regions)
 {
-       using namespace Gtk::CTree_Helpers;
+       for (vector<boost::weak_ptr<Region> >::iterator x = regions.begin(); x != regions.end(); ++x) {
+               boost::shared_ptr<Region> region ((*x).lock());
+               if (region) {
+                       add_region_to_region_display (region);
+               }
+       }
+}
 
-       vector<const char*> item;
-       RowList::iterator i;
-       RowList::iterator tmpi;
+void
+Editor::add_region_to_region_display (boost::shared_ptr<Region> region)
+{
+       if (!region || !session) {
+               return;
+       }
+       
        string str;
+       TreeModel::Row row;
+       Gdk::Color c;
+       bool missing_source = boost::dynamic_pointer_cast<SilentFileSource>(region->source());
 
        if (!show_automatic_regions_in_region_list && region->automatic()) {
                return;
        }
 
        if (region->hidden()) {
-
-               if (region_list_hidden_node == region_list_display.rows().end()) {
-                       item.clear ();
-                       item.push_back (_("hidden"));
-                       region_list_hidden_node = region_list_display.rows().insert (region_list_display.rows().end(),
-                                                                        Element (item));
-                       (*region_list_hidden_node).set_data (0);
-                       (*region_list_hidden_node).set_leaf (false);
-               }
-
-               item.clear ();
-               if (region->n_channels() > 1) {
-                       str = compose("%1  [%2]", region->name(), region->n_channels());
-                       item.push_back (str.c_str());
+               TreeModel::iterator iter = region_list_model->get_iter ("0");
+               TreeModel::Row parent;
+               TreeModel::Row child;
+
+               if (!iter) {
+                       parent = *(region_list_model->append());
+                       parent[region_list_columns.name] = _("Hidden");
+                       boost::shared_ptr<Region> proxy = parent[region_list_columns.region];
+                       proxy.reset ();
                } else {
-                       item.push_back (region->name().c_str());
+                       if ((*iter)[region_list_columns.name] != _("Hidden")) {
+                               parent = *(region_list_model->insert(iter));
+                               parent[region_list_columns.name] = _("Hidden");
+                               boost::shared_ptr<Region> proxy = parent[region_list_columns.region];
+                               proxy.reset ();
+                       } else {
+                               parent = *iter;
+                       }
                }
 
-               tmpi = region_list_hidden_node->subtree().insert (region_list_hidden_node->subtree().end(), 
-                                                                 Element (item));
-               (*tmpi).set_data (region);
-               return;
+               row = *(region_list_model->append (parent.children()));
 
        } else if (region->whole_file()) {
 
-               item.clear ();
+               TreeModel::iterator i;
+               TreeModel::Children rows = region_list_model->children();
 
-               if (region->source().name()[0] == '/') { // external file
+               for (i = rows.begin(); i != rows.end(); ++i) {
+                       boost::shared_ptr<Region> rr = (*i)[region_list_columns.region];
+                       
+                       if (rr && region->region_list_equivalent (rr)) {
+                               return;
+                       }
+               }
 
+               row = *(region_list_model->append());
+               
+               if (missing_source) {
+                       c.set_rgb(65535,0,0);     // FIXME: error color from style
+               
+               } else if (region->automatic()){
+                       c.set_rgb(0,65535,0);     // FIXME: error color from style
+               
+               } else {
+                       set_color(c, rgba_from_style ("RegionListWholeFile", 0xff, 0, 0, 0, "fg", Gtk::STATE_NORMAL, false ));
+               
+               }
+               
+               row[region_list_columns.color_] = c;
+
+               if (region->source()->name()[0] == '/') { // external file
+                       
                        if (region->whole_file()) {
+                               
+                               boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(region->source());
                                str = ".../";
-                               str += PBD::basename_nosuffix (region->source().name());
+
+                               if (afs) {
+                                       str = region_name_from_path (afs->path(), region->n_channels() > 1);
+                               } else {
+                                       str += region->source()->name();
+                               }
+
                        } else {
                                str = region->name();
                        }
 
                } else {
-
                        str = region->name();
-
                }
 
-               item.push_back (str.c_str());
-
-               tmpi = region_list_display.rows().insert (region_list_display.rows().end(), 
-                                                         Element (item));
-
-               (*tmpi).set_data (region);
-               (*tmpi).set_leaf (false);
+               if (region->n_channels() > 1) {
+                       std::stringstream foo;
+                       foo << region->n_channels ();
+                       str += " [";
+                       str += foo.str();
+                       str += ']';
+               }
 
-               return;
+               row[region_list_columns.name] = str;
+               row[region_list_columns.region] = region;
+               
+               if (missing_source) {
+                       row[region_list_columns.path] = _("(MISSING) ") + region->source()->name();
+               
+               } else {
+                       row[region_list_columns.path] = region->source()->name();
+               
+               } 
                
+               if (region->automatic()) {
+                       return;
+               }
+                       
        } else {
 
                /* find parent node, add as new child */
                
-               for (i = region_list_display.rows().begin(); i != region_list_display.rows().end(); ++i) {
-
-                       AudioRegion* r = static_cast<AudioRegion*> ((*i).get_data());
+               TreeModel::iterator i;
+               TreeModel::Children rows = region_list_model->children();
+               bool found_parent = false;
 
+               for (i = rows.begin(); i != rows.end(); ++i) {
+                       boost::shared_ptr<Region> rr = (*i)[region_list_columns.region];
+                       boost::shared_ptr<AudioRegion> r = boost::dynamic_pointer_cast<AudioRegion>(rr);
+                       
                        if (r && r->whole_file()) {
                                
-                               if (region->source_equivalent (*r)) {
+                               if (region->source_equivalent (r)) {
+                                       row = *(region_list_model->append ((*i).children()));
+                                       found_parent = true;
+                                       break;
+                               }
+                       }
+                       
+                       TreeModel::iterator ii;
+                       TreeModel::Children subrows = (*i).children();
 
-                                       item.clear ();
-                                       
-                                       if (region->n_channels() > 1) {
-                                               str = compose("%1  [%2]", region->name(), region->n_channels());
-                                               item.push_back (str.c_str());
-                                       } else {
-                                               item.push_back (region->name().c_str());
-                                       }
+                       for (ii = subrows.begin(); ii != subrows.end(); ++ii) {
+                               boost::shared_ptr<Region> rrr = (*ii)[region_list_columns.region];
 
-                                       
-                                       tmpi = i->subtree().insert (i->subtree().end(), Element (item));
-                                       (*tmpi).set_data (region);
-                                       
+                               if (region->region_list_equivalent (rrr)) {
                                        return;
+                               
                                }
                        }
                }
+               
+               if (!found_parent) {
+                       row = *(region_list_model->append());
+               }       
        }
        
-       item.clear ();
+       row[region_list_columns.region] = region;
        
-       if (region->n_channels() > 1) {
-               str = compose("%1  [%2]", region->name(), region->n_channels());
-               item.push_back (str.c_str());
+       populate_row(region, row);
+}
+
+
+void
+Editor::region_list_region_changed (Change what_changed, boost::weak_ptr<Region> region)
+{
+       ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::region_list_region_changed), what_changed, region));
+       
+       boost::shared_ptr<Region> r = region.lock ();
+       
+       if (!r) {
+               return;
+       }
+       
+       if (what_changed & ARDOUR::NameChanged) {
+               /* find the region in our model and change its name */
+               TreeModel::Children rows = region_list_model->children ();
+               TreeModel::iterator i = rows.begin ();
+               while (i != rows.end ()) {
+                       TreeModel::Children children = (*i)->children ();
+                       TreeModel::iterator j = children.begin ();
+                       while (j != children.end()) {
+                               boost::shared_ptr<Region> c = (*j)[region_list_columns.region];
+                               if (c == r) {
+                                       break;
+                               }
+                               ++j;
+                       }
+
+                       if (j != children.end()) {
+                               (*j)[region_list_columns.name] = r->name ();
+                               break;
+                       }
+                       
+                       ++i;
+               }
+
+       }
+}
+
+void
+Editor::region_list_selection_changed() 
+{
+       if (region_list_display.get_selection()->count_selected_rows() > 0) {
+               
+               TreeIter iter;
+               TreeView::Selection::ListHandle_Path rows = region_list_display.get_selection()->get_selected_rows ();
+               
+               deselect_all();
+               
+               for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
+                       
+                       if (iter = region_list_model->get_iter (*i)) {                                                                  // they could have clicked on a row that is just a placeholder, like "Hidden"
+                               boost::shared_ptr<Region> region = (*iter)[region_list_columns.region];
+                               
+                               if (region) {
+                                       
+                                       if (region->automatic()) {
+                                               region_list_display.get_selection()->unselect(*i);
+                                               
+                                       } else {
+                                               region_list_change_connection.block(true);
+                                               //editor_regions_selection_changed_connection.block(true);
+
+                                               set_selected_regionview_from_region_list (region, Selection::Add);
+
+                                               region_list_change_connection.block(false);
+                                               //editor_regions_selection_changed_connection.block(false);
+                                       }
+                               }
+                       }
+               }
        } else {
-               item.push_back (region->name().c_str());
+               deselect_all();
+       }
+}
+
+void
+Editor::set_selected_in_region_list(RegionSelection& regions)
+{
+       for (RegionSelection::iterator iter = regions.begin(); iter != regions.end(); ++iter) {
+       
+               TreeModel::iterator i;
+               TreeModel::Children rows = region_list_model->children();
+               boost::shared_ptr<Region> r ((*iter)->region());
+               
+               for (i = rows.begin(); i != rows.end(); ++i) {
+                       
+                       boost::shared_ptr<Region> compared_region = (*i)[region_list_columns.region];
+
+                       if (r == compared_region) {
+                               region_list_display.get_selection()->select(*i);;
+                               break;
+                       }
+                       
+                       if (!(*i).children().empty()) {
+                               if (set_selected_in_region_list_subrow(r, (*i), 2)) {
+                                       break;
+                               }
+                       }
+               }
        }
+}
+
+bool
+Editor::set_selected_in_region_list_subrow (boost::shared_ptr<Region> region, TreeModel::Row const &parent_row, int level)
+{
+       TreeModel::iterator i;
+       TreeModel::Children subrows = (*parent_row).children();
        
-       tmpi = region_list_display.rows().insert (region_list_display.rows().end(), Element (item));
-       (*tmpi).set_data (region);
-       (*tmpi).set_leaf (true);
+       for (i = subrows.begin(); i != subrows.end(); ++i) {
+               
+               boost::shared_ptr<Region> compared_region = (*i)[region_list_columns.region];
+               
+               if (region == compared_region) {
+                       region_list_display.get_selection()->select(*i);;
+                       return true;
+               }
+               
+               if (!(*i).children().empty()) {
+                       if (update_region_subrows(region, (*i), level + 1)) {
+                               return true;
+                       }
+               }
+       }
+       return false;
 }
 
 void
-Editor::insert_into_tmp_audio_regionlist(AudioRegion* region)
+Editor::insert_into_tmp_regionlist(boost::shared_ptr<Region> region)
 {
        /* keep all whole files at the beginning */
        
        if (region->whole_file()) {
-               tmp_audio_region_list.push_front (region);
+               tmp_region_list.push_front (region);
        } else {
-               tmp_audio_region_list.push_back (region);
+               tmp_region_list.push_back (region);
        }
 }
 
 void
 Editor::redisplay_regions ()
-{
-       if (session) {
-               region_list_display.freeze ();
-               region_list_clear ();
-               region_list_hidden_node = region_list_display.rows().end();
+{      
+       if (no_region_list_redisplay || !session) {
+               return;
+       }
+               
+       bool tree_expanded = false;
+       
+       if (toggle_full_region_list_action && toggle_full_region_list_action->get_active()) {   //If the list was expanded prior to rebuilding, 
+               tree_expanded = true;                                                                                                                           //expand it again afterwards
+       }
+       
+       region_list_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
+       region_list_model->clear ();
+
+       /* now add everything we have, via a temporary list used to help with
+               sorting.
+       */
+       
+       tmp_region_list.clear();
+       session->foreach_region (this, &Editor::insert_into_tmp_regionlist);
 
-               /* now add everything we have, via a temporary list used to help with
-                  sorting.
-               */
+       for (list<boost::shared_ptr<Region> >::iterator r = tmp_region_list.begin(); r != tmp_region_list.end(); ++r) {
+               add_region_to_region_display (*r);
+       }
+       tmp_region_list.clear();
+       
+       region_list_display.set_model (region_list_model);
+       
+       if (tree_expanded) {
+               region_list_display.expand_all();
+       }
+}
+
+void
+Editor::update_region_row (boost::shared_ptr<Region> region)
+{      
+       if (!region || !session) {
+               return;
+       }
+       
+       TreeModel::iterator i;
+       TreeModel::Children rows = region_list_model->children();
+       
+       for (i = rows.begin(); i != rows.end(); ++i) {
+               
+//             cerr << "Level 1: Compare " << region->name() << " with parent " << (*i)[region_list_columns.name] << "\n";
+               
+               boost::shared_ptr<Region> compared_region = (*i)[region_list_columns.region];
                
-               tmp_audio_region_list.clear();
-               session->foreach_audio_region (this, &Editor::insert_into_tmp_audio_regionlist);
+               if (region == compared_region) {
+//                     cerr << "Matched\n";
+                       populate_row(region, (*i));
+                       return;
+               }
+               
+               if (!(*i).children().empty()) {
+                       if (update_region_subrows(region, (*i), 2)) {
+                               return;
+                       }
+               }
+       }
+//     cerr << "Returning - No match\n";
+}
 
-               for (list<AudioRegion*>::iterator r = tmp_audio_region_list.begin(); r != tmp_audio_region_list.end(); ++r) {
-                       add_audio_region_to_region_display (*r);
+bool
+Editor::update_region_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)[region_list_columns.name] << "\n";
+               
+               boost::shared_ptr<Region> compared_region = (*i)[region_list_columns.region];
+               
+               if (region == compared_region) {
+                       populate_row(region, (*i));
+//                     cerr << "Matched\n";
+                       return true;
                }
                
-               region_list_display.sort ();
-               region_list_display.thaw ();
+               if (!(*i).children().empty()) {
+                       if (update_region_subrows(region, (*i), level + 1)) {
+                               return true;
+                       }
+               }
        }
+       return false;
 }
 
 void
-Editor::region_list_clear ()
+Editor::update_all_region_rows ()
 {
-       /* ---------------------------------------- */
-       /* XXX MAKE ME A FUNCTION (no CTree::clear() in gtkmm 1.2) */
-
-       gtk_ctree_remove_node (region_list_display.gobj(), NULL);
+       if (!session) {
+               return;
+       }
 
-       /* ---------------------------------------- */
+       TreeModel::iterator i;
+       TreeModel::Children rows = region_list_model->children();
+       
+       for (i = rows.begin(); i != rows.end(); ++i) {
+               
+               boost::shared_ptr<Region> region = (*i)[region_list_columns.region];
+       
+               if (!region->automatic()) {
+                       cerr << "level 1 : Updating " << region->name() << "\n";
+                       populate_row(region, (*i));
+               }
+               
+               if (!(*i).children().empty()) {
+                       update_all_region_subrows((*i), 2);
+               }
+       }
 }
 
 void
-Editor::region_list_column_click (gint col)
+Editor::update_all_region_subrows (TreeModel::Row const &parent_row, int level)
 {
-       bool sensitive;
+       TreeModel::iterator i;
+       TreeModel::Children subrows = (*parent_row).children();
+       
+       for (i = subrows.begin(); i != subrows.end(); ++i) {
+               
+               boost::shared_ptr<Region> region = (*i)[region_list_columns.region];
+               
+               if (!region->automatic()) {
+                       cerr << "level " << level << " : Updating " << region->name() << "\n";
+                       populate_row(region, (*i));
+               }
+                       
+               if (!(*i).children().empty()) {
+                       update_all_region_subrows((*i), level + 1);
+               }
+       }
+}
 
-       if (region_list_menu == 0) {
-               build_region_list_menu ();
+void
+Editor::populate_row (boost::shared_ptr<Region> region, TreeModel::Row const &row)
+{
+       char start_str[16];
+       char end_str[16];
+       char length_str[16];
+       char sync_str[16];
+       char fadein_str[16];
+       char fadeout_str[16];
+       char used_str[8];
+       int used;
+       BBT_Time bbt;                           // FIXME Why do these have to be declared here ?
+       SMPTE::Time smpte;                      // FIXME I would like them declared in the case statment where they are used.
+       
+       bool missing_source = boost::dynamic_pointer_cast<SilentFileSource>(region->source());
+       
+       boost::shared_ptr<AudioRegion> audioRegion = boost::dynamic_pointer_cast<AudioRegion>(region);
+       
+       bool fades_in_seconds = false;
+
+       start_str[0] = '\0';
+       end_str[0] = '\0';
+       length_str[0] = '\0';
+       sync_str[0] = '\0';
+       fadein_str[0] = '\0';
+       fadeout_str[0] = '\0';
+       used_str[0] = '\0';
+
+       used = get_regionview_count_from_region_list(region);
+       sprintf (used_str, "%4d" , used);
+       
+       switch (ARDOUR_UI::instance()->secondary_clock.mode ()) {
+       case AudioClock::SMPTE:
+       case AudioClock::Off:                                                                                           /* If the secondary clock is off, default to SMPTE */
+               session->smpte_time (region->position(), smpte);
+               sprintf (start_str, "%02d:%02d:%02d:%02d", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
+               session->smpte_time (region->position() + region->length() - 1, smpte);
+               sprintf (end_str, "%02d:%02d:%02d:%02d", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
+               session->smpte_time (region->length(), smpte);
+               sprintf (length_str, "%02d:%02d:%02d:%02d", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
+               session->smpte_time (region->sync_position() + region->position(), smpte);
+               sprintf (sync_str, "%02d:%02d:%02d:%02d", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
+               
+               if (audioRegion && !fades_in_seconds) { 
+                       session->smpte_time (audioRegion->fade_in()->back()->when, smpte);
+                       sprintf (fadein_str, "%02d:%02d:%02d:%02d", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
+                       session->smpte_time (audioRegion->fade_out()->back()->when, smpte);
+                       sprintf (fadeout_str, "%02d:%02d:%02d:%02d", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
+               }
+               
+               break;
+               
+       case AudioClock::BBT:
+               session->tempo_map().bbt_time (region->position(), bbt);
+               sprintf (start_str, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
+               session->tempo_map().bbt_time (region->position() + region->length() - 1, bbt);
+               sprintf (end_str, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
+               session->tempo_map().bbt_time (region->length(), bbt);
+               sprintf (length_str, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
+               session->tempo_map().bbt_time (region->sync_position() + region->position(), bbt);
+               sprintf (sync_str, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
+               
+               if (audioRegion && !fades_in_seconds) {
+                       session->tempo_map().bbt_time (audioRegion->fade_in()->back()->when, bbt);
+                       sprintf (fadein_str, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
+                       session->tempo_map().bbt_time (audioRegion->fade_out()->back()->when, bbt);
+                       sprintf (fadeout_str, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
+               } 
+               break;
+               
+       case AudioClock::MinSec:
+               nframes_t left;
+               int hrs;
+               int mins;
+               float secs;
+       
+               left = region->position();
+               hrs = (int) floor (left / (session->frame_rate() * 60.0f * 60.0f));
+               left -= (nframes_t) floor (hrs * session->frame_rate() * 60.0f * 60.0f);
+               mins = (int) floor (left / (session->frame_rate() * 60.0f));
+               left -= (nframes_t) floor (mins * session->frame_rate() * 60.0f);
+               secs = left / (float) session->frame_rate();
+               sprintf (start_str, "%02d:%02d:%06.3f", hrs, mins, secs);
+               
+               left = region->position() + region->length() - 1;
+               hrs = (int) floor (left / (session->frame_rate() * 60.0f * 60.0f));
+               left -= (nframes_t) floor (hrs * session->frame_rate() * 60.0f * 60.0f);
+               mins = (int) floor (left / (session->frame_rate() * 60.0f));
+               left -= (nframes_t) floor (mins * session->frame_rate() * 60.0f);
+               secs = left / (float) session->frame_rate();
+               sprintf (end_str, "%02d:%02d:%06.3f", hrs, mins, secs);
+               
+               left = region->length();
+               hrs = (int) floor (left / (session->frame_rate() * 60.0f * 60.0f));
+               left -= (nframes_t) floor (hrs * session->frame_rate() * 60.0f * 60.0f);
+               mins = (int) floor (left / (session->frame_rate() * 60.0f));
+               left -= (nframes_t) floor (mins * session->frame_rate() * 60.0f);
+               secs = left / (float) session->frame_rate();
+               sprintf (length_str, "%02d:%02d:%06.3f", hrs, mins, secs);
+               
+               left = region->sync_position() + region->position();
+               hrs = (int) floor (left / (session->frame_rate() * 60.0f * 60.0f));
+               left -= (nframes_t) floor (hrs * session->frame_rate() * 60.0f * 60.0f);
+               mins = (int) floor (left / (session->frame_rate() * 60.0f));
+               left -= (nframes_t) floor (mins * session->frame_rate() * 60.0f);
+               secs = left / (float) session->frame_rate();
+               sprintf (sync_str, "%02d:%02d:%06.3f", hrs, mins, secs);
+               
+               if (audioRegion && !fades_in_seconds) {
+                       left = audioRegion->fade_in()->back()->when;
+                       hrs = (int) floor (left / (session->frame_rate() * 60.0f * 60.0f));
+                       left -= (nframes_t) floor (hrs * session->frame_rate() * 60.0f * 60.0f);
+                       mins = (int) floor (left / (session->frame_rate() * 60.0f));
+                       left -= (nframes_t) floor (mins * session->frame_rate() * 60.0f);
+                       secs = left / (float) session->frame_rate();
+                       sprintf (fadein_str, "%02d:%02d:%06.3f", hrs, mins, secs);
+                       
+                       left = audioRegion->fade_out()->back()->when;
+                       hrs = (int) floor (left / (session->frame_rate() * 60.0f * 60.0f));
+                       left -= (nframes_t) floor (hrs * session->frame_rate() * 60.0f * 60.0f);
+                       mins = (int) floor (left / (session->frame_rate() * 60.0f));
+                       left -= (nframes_t) floor (mins * session->frame_rate() * 60.0f);
+                       secs = left / (float) session->frame_rate();
+                       sprintf (fadeout_str, "%02d:%02d:%06.3f", hrs, mins, secs);
+               }
+               
+               break;
+               
+       case AudioClock::Frames:
+               snprintf (start_str, sizeof (start_str), "%u", region->position());
+               snprintf (end_str, sizeof (end_str), "%u", (region->position() + region->length() - 1));
+               snprintf (length_str, sizeof (length_str), "%u", region->length());
+               snprintf (sync_str, sizeof (sync_str), "%u", region->sync_position() + region->position());
+               
+               if (audioRegion && !fades_in_seconds) {
+                       snprintf (fadein_str, sizeof (fadein_str), "%u", uint (audioRegion->fade_in()->back()->when));
+                       snprintf (fadeout_str, sizeof (fadeout_str), "%u", uint (audioRegion->fade_out()->back()->when));
+               }
+               
+               break;
+       
+       default:
+               break;
+       }
+       
+       if (audioRegion && fades_in_seconds) {
+                       
+               nframes_t left;
+               int mins;
+               int millisecs;
+               
+               left = audioRegion->fade_in()->back()->when;
+               mins = (int) floor (left / (session->frame_rate() * 60.0f));
+               left -= (nframes_t) floor (mins * session->frame_rate() * 60.0f);
+               millisecs = (int) floor ((left * 1000.0f) / session->frame_rate());
+               
+               if (audioRegion->fade_in()->back()->when >= session->frame_rate()) {
+                       sprintf (fadein_str, "%01dM %01dmS", mins, millisecs);
+               } else {
+                       sprintf (fadein_str, "%01dmS", millisecs);
+               }
+               
+               left = audioRegion->fade_out()->back()->when;
+               mins = (int) floor (left / (session->frame_rate() * 60.0f));
+               left -= (nframes_t) floor (mins * session->frame_rate() * 60.0f);
+               millisecs = (int) floor ((left * 1000.0f) / session->frame_rate());
+               
+               if (audioRegion->fade_out()->back()->when >= session->frame_rate()) {
+                       sprintf (fadeout_str, "%01dM %01dmS", mins, millisecs);
+               } else {
+                       sprintf (fadeout_str, "%01dmS", millisecs);
+               }
        }
+       
+       if (used > 1) {
+               row[region_list_columns.start] = _("Multiple");
+               row[region_list_columns.end] = _("Multiple");
+               row[region_list_columns.sync] = _("Multiple");
+               row[region_list_columns.fadein] = _("Multiple");
+               row[region_list_columns.fadeout] = _("Multiple");
+               row[region_list_columns.locked] = false;
+               row[region_list_columns.glued] = false;
+               row[region_list_columns.muted] = false;
+               row[region_list_columns.opaque] = false;
+       } else {
+               row[region_list_columns.start] = start_str;
+               row[region_list_columns.end] = end_str;
+               
+               if (region->sync_position() == region->position()) {
+                       row[region_list_columns.sync] = _("Start");
+               } else if (region->sync_position() == (region->position() + region->length() - 1)) {
+                       row[region_list_columns.sync] = _("End");
+               } else {
+                       row[region_list_columns.sync] = sync_str;
+               }
+       
+               if (audioRegion) {
+                       if (audioRegion->fade_in_active()) {
+                               row[region_list_columns.fadein] = string_compose("%1%2%3", " ", fadein_str, " ");
+                       } else {
+                               row[region_list_columns.fadein] = string_compose("%1%2%3", "(", fadein_str, ")");
+                       }
+               } else {
+                       row[region_list_columns.fadein] = "";
+               }
 
-       if (region_list_display.selection().size() != 0) {
-               sensitive = true;
+               if (audioRegion) {
+                       if (audioRegion->fade_out_active()) {
+                               row[region_list_columns.fadeout] = string_compose("%1%2%3", " ", fadeout_str, " ");
+                       } else {
+                               row[region_list_columns.fadeout] = string_compose("%1%2%3", "(", fadeout_str, ")");
+                       }
+               } else {
+                       row[region_list_columns.fadeout] = "";
+               }
+               
+               row[region_list_columns.locked] = region->locked();
+       
+               if (region->positional_lock_style() == Region::MusicTime) {
+                       row[region_list_columns.glued] = true;
+               } else {
+                       row[region_list_columns.glued] = false;
+               }
+               
+               row[region_list_columns.muted] = region->muted();
+               row[region_list_columns.opaque] = region->opaque();
+       }
+       
+       row[region_list_columns.length] = length_str;
+       row[region_list_columns.used] = used_str;
+       
+       if (missing_source) {
+               row[region_list_columns.path] = _("MISSING ") + region->source()->name();
        } else {
-               sensitive = false;
+               row[region_list_columns.path] = region->source()->name();
        }
-
-       for (vector<MenuItem*>::iterator i = rl_context_menu_region_items.begin(); i != rl_context_menu_region_items.end(); ++i) {
-               (*i)->set_sensitive (sensitive);
+       
+       if (region->n_channels() > 1) {
+               row[region_list_columns.name] = string_compose("%1  [%2]", region->name(), region->n_channels());
+       } else {
+               row[region_list_columns.name] = region->name();
        }
-
-       region_list_menu->popup (0, 0);
 }
 
 void
 Editor::build_region_list_menu ()
 {
-       using namespace Gtk::Menu_Helpers;
-
-       region_list_menu = new Menu;
-       
-       MenuList& items = region_list_menu->items();
-       region_list_menu->set_name ("ArdourContextMenu");
-
-       items.push_back (MenuElem (_("Audition"), slot (*this, &Editor::audition_region_from_region_list)));
-       rl_context_menu_region_items.push_back (items.back());
-       items.push_back (MenuElem (_("Hide"), slot (*this, &Editor::hide_region_from_region_list)));
-       rl_context_menu_region_items.push_back (items.back());
-       items.push_back (MenuElem (_("Remove"), slot (*this, &Editor::remove_region_from_region_list)));
-       rl_context_menu_region_items.push_back (items.back());
-
-
-       items.push_back (SeparatorElem());
-       
-
-       // items.push_back (MenuElem (_("Find")));
-       items.push_back (CheckMenuElem (_("Show all"), slot (*this, &Editor::toggle_full_region_list)));
-       toggle_full_region_list_item = static_cast<CheckMenuItem*> (items.back());
-       
-       Gtk::Menu *sort_menu = manage (new Menu);
-       MenuList& sort_items = sort_menu->items();
-       sort_menu->set_name ("ArdourContextMenu");
-       RadioMenuItem::Group sort_order_group;
-       RadioMenuItem::Group sort_type_group;
-
-       sort_items.push_back (RadioMenuElem (sort_order_group, _("Ascending"),
-                                            bind (slot (*this, &Editor::reset_region_list_sort_direction), true)));
-       sort_items.push_back (RadioMenuElem (sort_order_group, _("Descending"), 
-                                            bind (slot (*this, &Editor::reset_region_list_sort_direction), false)));
-       sort_items.push_back (SeparatorElem());
-
-       sort_items.push_back (RadioMenuElem (sort_type_group, _("By Region Name"),
-                                            bind (slot (*this, &Editor::reset_region_list_sort_type), ByName)));
-       sort_items.push_back (RadioMenuElem (sort_type_group, _("By Region Length"),
-                                            bind (slot (*this, &Editor::reset_region_list_sort_type), ByLength)));
-       sort_items.push_back (RadioMenuElem (sort_type_group, _("By Region Position"),
-                                            bind (slot (*this, &Editor::reset_region_list_sort_type), ByPosition)));
-       sort_items.push_back (RadioMenuElem (sort_type_group, _("By Region Timestamp"),
-                                            bind (slot (*this, &Editor::reset_region_list_sort_type), ByTimestamp)));
-       sort_items.push_back (RadioMenuElem (sort_type_group, _("By Region Start in File"),
-                                            bind (slot (*this, &Editor::reset_region_list_sort_type), ByStartInFile)));
-       sort_items.push_back (RadioMenuElem (sort_type_group, _("By Region End in File"),
-                                            bind (slot (*this, &Editor::reset_region_list_sort_type), ByEndInFile)));
-       sort_items.push_back (RadioMenuElem (sort_type_group, _("By Source File Name"),
-                                            bind (slot (*this, &Editor::reset_region_list_sort_type), BySourceFileName)));
-       sort_items.push_back (RadioMenuElem (sort_type_group, _("By Source File Length"),
-                                            bind (slot (*this, &Editor::reset_region_list_sort_type), BySourceFileLength)));
-       sort_items.push_back (RadioMenuElem (sort_type_group, _("By Source File Creation Date"),
-                                            bind (slot (*this, &Editor::reset_region_list_sort_type), BySourceFileCreationDate)));
-       sort_items.push_back (RadioMenuElem (sort_type_group, _("By Source Filesystem"),
-                                            bind (slot (*this, &Editor::reset_region_list_sort_type), BySourceFileFS)));
-       
-       items.push_back (MenuElem (_("Sorting"), *sort_menu));
-       items.push_back (SeparatorElem());
-
-//     items.push_back (CheckMenuElem (_("Display Automatic Regions"), slot (*this, &Editor::toggle_show_auto_regions)));
-//     toggle_auto_regions_item = static_cast<CheckMenuItem*> (items.back());
-//     toggle_auto_regions_item->set_active (show_automatic_regions_in_region_list);
-//     items.push_back (SeparatorElem());
-
-       items.push_back (MenuElem (_("Import audio (copy)"), bind (slot (*this, &Editor::import_audio), false)));
-       import_audio_item = items.back();
-       if (!session) {
-               import_audio_item->set_sensitive (false);
+       region_list_menu = dynamic_cast<Menu*>(ActionManager::get_widget ("/RegionListMenu"));
+                                              
+       /* now grab specific menu items that we need */
+
+       Glib::RefPtr<Action> act;
+
+       act = ActionManager::get_action (X_("RegionList"), X_("rlShowAll"));
+       if (act) {
+               toggle_full_region_list_action = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
        }
-       items.push_back (MenuElem (_("Embed audio (link)"), slot (*this, &Editor::embed_audio)));
-       embed_audio_item = items.back();
-       if (!session) {
-               embed_audio_item->set_sensitive (false);
+
+       act = ActionManager::get_action (X_("RegionList"), X_("rlShowAuto"));
+       if (act) {
+               toggle_show_auto_regions_action = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
        }
 }
 
 void
 Editor::toggle_show_auto_regions ()
 {
-       //show_automatic_regions_in_region_list = toggle_auto_regions_item->get_active();
-       show_automatic_regions_in_region_list = true;
+       show_automatic_regions_in_region_list = toggle_show_auto_regions_action->get_active();
        redisplay_regions ();
 }
 
 void
 Editor::toggle_full_region_list ()
 {
-       region_list_display.freeze ();
-       if (toggle_full_region_list_item->get_active()) {
-               for (CTree_Helpers::RowIterator r = region_list_display.rows().begin(); r != region_list_display.rows().end(); ++r) {
-                       r->expand_recursive ();
-               }
+       if (toggle_full_region_list_action->get_active()) {
+               region_list_display.expand_all ();
        } else {
-               for (CTree_Helpers::RowIterator r = region_list_display.rows().begin(); r != region_list_display.rows().end(); ++r) {
-                       r->collapse ();
-               }
+               region_list_display.collapse_all ();
+       }
+}
+
+void
+Editor::show_region_list_display_context_menu (int button, int time)
+{
+       if (region_list_menu == 0) {
+               build_region_list_menu ();
+       }
+
+       if (region_list_display.get_selection()->count_selected_rows() > 0) {
+               ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, true);
+       } else {
+               ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, false);
        }
-       region_list_display.thaw ();
+
+       region_list_menu->popup (button, time);
 }
 
-gint
+bool
 Editor::region_list_display_key_press (GdkEventKey* ev)
 {
-       return FALSE;
+       return false;
 }
 
-gint
+bool
 Editor::region_list_display_key_release (GdkEventKey* ev)
 {
        switch (ev->keyval) {
        case GDK_Delete:
-               remove_selected_regions_from_region_list ();
-               return TRUE;
+               remove_region_from_region_list ();
+               return true;
                break;
        default:
                break;
        }
 
-       return FALSE;
-
+       return false;
 }
 
-gint
+bool
 Editor::region_list_display_button_press (GdkEventButton *ev)
 {
-       int row, col;
-       AudioRegion *region;
-
-       if (Keyboard::is_delete_event (ev)) {
-               if (region_list_display.get_selection_info ((int)ev->x, (int)ev->y, &row, &col) != 0) {
-                       if ((region = (AudioRegion *) region_list_display.row(row).get_data()) != 0) {
-                               delete region;
-                       }
+       boost::shared_ptr<Region> region;
+       TreeIter iter;
+       TreeModel::Path path;
+       TreeViewColumn* column;
+       int cellx;
+       int celly;
+
+       if (region_list_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
+               if ((iter = region_list_model->get_iter (path))) {
+                       region = (*iter)[region_list_columns.region];
                }
-               return TRUE;
        }
 
        if (Keyboard::is_context_menu_event (ev)) {
-               region_list_column_click (-1);
-               return TRUE;
+               show_region_list_display_context_menu (ev->button, ev->time);
+               return true;
+       }
+
+       if (region == 0) {
+               region_list_display.get_selection()->unselect_all();
+               deselect_all();
+               return false;
        }
 
        switch (ev->button) {
        case 1:
-               if (region_list_display.get_selection_info ((int)ev->x, (int)ev->y, &row, &col) != 0) {
-                       if ((region = (AudioRegion *) region_list_display.row(row).get_data()) != 0) {
-
-                               if (wave_cursor == 0) {
-                                       GdkPixmap *source, *mask;
-                                       GdkColor fg = { 0, 65535, 0, 0 }; /* Red. */
-                                       GdkColor bg = { 0, 0, 0, 65535 }; /* Blue. */
-                                       
-                                       source = gdk_bitmap_create_from_data (NULL, wave_cursor_bits,
-                                                                             wave_cursor_width, wave_cursor_height);
-                                       mask = gdk_bitmap_create_from_data (NULL, wave_cursor_mask_bits,
-                                                                           wave_cursor_mask_width, wave_cursor_mask_height);
-
-                                       wave_cursor = gdk_cursor_new_from_pixmap (source, 
-                                                                                 mask,
-                                                                                 &fg, 
-                                                                                 &bg,
-                                                                                 wave_cursor_x_hot,
-                                                                                 wave_cursor_y_hot);
-                                       gdk_pixmap_unref (source);
-                                       gdk_pixmap_unref (mask);
-                               }
-                               region_list_display_drag_region = region;
-                               need_wave_cursor = 1;
-
-                               /* audition on double click */
-                               if (ev->type == GDK_2BUTTON_PRESS) {
-                                       consider_auditioning (region);
-                               }
-
-                               return TRUE;
-                       }
-                       
-               }
                break;
 
        case 2:
-               if (!Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
-                       if (region_list_display.get_selection_info ((int)ev->x, (int)ev->y, &row, &col) != 0) {
-                               if ((region = (AudioRegion *) region_list_display.get_row_data (row)) != 0) {
-                                       if (consider_auditioning (region)) {
-                                               region_list_display.row(row).select();
-                                       }
-                                       else {
-                                               region_list_display.row(row).unselect();
-                                       }
-                                       return TRUE;
-                               }
-                       }
-               } 
-
-               /* to prevent regular selection -- i dont think this is needed JLC */
-               return stop_signal (region_list_display, "button_press_event");
+               // audition on middle click (stop audition too)
+               if (!Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
+                       consider_auditioning (region);
+               }
+               return true;
                break;
 
-       case 3:
-               break;
        default:
                break; 
        }
 
-       return FALSE;
-}
+       return false;
+}      
 
-gint
+bool
 Editor::region_list_display_button_release (GdkEventButton *ev)
 {
-       int row, col;
-
-       if (region_list_display.get_selection_info ((int)ev->x, (int)ev->y, &row, &col) != 0) {
-               region_list_button_region = (AudioRegion *) region_list_display.get_row_data (row);
-       } else {
-               region_list_button_region = 0;
-       }
-
-       if (Keyboard::is_delete_event (ev)) {
-               remove_region_from_region_list ();
-               return TRUE;
-       }
-
-       switch (ev->button) {
-       case 1:
-               if (region_list_display_drag_region) {
-                       insert_region_list_drag (*region_list_display_drag_region);
-               }
-
-               track_canvas_scroller.get_window().set_cursor (current_canvas_cursor);
-               region_list_display.get_window().set_cursor (0);
-
-               region_list_display_drag_region = 0;
-               need_wave_cursor = 0;
-
-               return TRUE;
-               break;
-
-       case 3:
-               if (!Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
-
-                       if (region_list_menu == 0) {
-                               build_region_list_menu ();
-                       }
-                       
-                       bool sensitive;
-                       
-                       if (region_list_display.selection().size() != 0) {
-                               sensitive = true;
-                       } else {
-                               sensitive = false;
-                       }
-
-                       for (vector<MenuItem*>::iterator i = rl_context_menu_region_items.begin(); i != rl_context_menu_region_items.end(); ++i) {
-                               (*i)->set_sensitive (sensitive);
-                       }
-
-                       region_list_menu->popup (0, 0);
+       TreeIter iter;
+       TreeModel::Path path;
+       TreeViewColumn* column;
+       int cellx;
+       int celly;
+       boost::shared_ptr<Region> region;
+
+       if (region_list_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
+               if ((iter = region_list_model->get_iter (path))) {
+                       region = (*iter)[region_list_columns.region];
                }
-
-               return TRUE;
-               break;
-       default:
-               break;
        }
-       return FALSE;
-}
 
-gint
-Editor::region_list_display_motion (GdkEventMotion *ev)
-{
-       if (need_wave_cursor == 1) {
-               track_canvas_scroller.get_window().set_cursor (wave_cursor);
-               region_list_display.get_window().set_cursor (wave_cursor);
-               gdk_flush ();
-               need_wave_cursor = 2;
+       if (region && Keyboard::is_delete_event (ev)) {
+               session->remove_region_from_region_list (region);
+               return true;
        }
-       return FALSE;
-}
 
-void
-Editor::region_list_display_selected (gint row, gint col, GdkEvent *ev)
-{
-       AudioRegion* region = static_cast<AudioRegion *>(region_list_display.get_row_data (row));
-
-       if (session == 0 || region == 0) {
-               return;
-       }
-
-       set_selected_regionview_from_region_list (*region, false);
+       return false;
 }
 
 void
-Editor::region_list_display_unselected (gint row, gint col, GdkEvent *ev)
+Editor::consider_auditioning (boost::shared_ptr<Region> region)
 {
-}
+       boost::shared_ptr<AudioRegion> r = boost::dynamic_pointer_cast<AudioRegion> (region);
 
-bool
-Editor::consider_auditioning (AudioRegion *r)
-{
        if (r == 0) {
                session->cancel_audition ();
-               return false;
+               return;
        }
 
        if (session->is_auditioning()) {
                session->cancel_audition ();
                if (r == last_audition_region) {
-                       return false;
+                       return;
                }
        }
 
-       session->audition_region (*r);
+       session->audition_region (r);
        last_audition_region = r;
-
-       return true;
 }
 
-gint
-Editor::region_list_display_enter_notify (GdkEventCrossing *ev)
+int
+Editor::region_list_sorter (TreeModel::iterator a, TreeModel::iterator b)
 {
-       ARDOUR_UI::instance()->allow_focus (true);
-       region_list_display.grab_focus ();
-       return FALSE;
-}
+       int cmp = 0;
 
-gint
-Editor::region_list_display_leave_notify (GdkEventCrossing *ev)
-{
-       ARDOUR_UI::instance()->allow_focus (false);
-       return FALSE;
-}
+       boost::shared_ptr<Region> r1 = (*a)[region_list_columns.region];
+       boost::shared_ptr<Region> r2 = (*b)[region_list_columns.region];
 
-gint
-Editor::_region_list_sorter (GtkCList* clist, gconstpointer a, gconstpointer b)
-{
-       Editor* editor = static_cast<Editor*> (gtk_object_get_data (GTK_OBJECT(clist), "editor"));
-       return editor->region_list_sorter (a, b);
-}
+       /* handle rows without regions, like "Hidden" */
 
-gint
-Editor::region_list_sorter (gconstpointer a, gconstpointer b)
-{
-       GtkCListRow* row1 = (GtkCListRow *) a;
-       GtkCListRow* row2 = (GtkCListRow *) b;
+       if (r1 == 0) {
+               return -1;
+       }
 
-       AudioRegion* region1 = static_cast<AudioRegion*> (row1->data);
-       AudioRegion* region2 = static_cast<AudioRegion*> (row2->data);
+       if (r2 == 0) {
+               return 1;
+       }
+
+       boost::shared_ptr<AudioRegion> region1 = boost::dynamic_pointer_cast<AudioRegion> (r1);
+       boost::shared_ptr<AudioRegion> region2 = boost::dynamic_pointer_cast<AudioRegion> (r2);
 
        if (region1 == 0 || region2 == 0) {
+               Glib::ustring s1;
+               Glib::ustring s2;
                switch (region_list_sort_type) {
                case ByName:
-                       return true; /* XXX compare text in rows */
+                       s1 = (*a)[region_list_columns.name];
+                       s2 = (*b)[region_list_columns.name];
+                       return (s1.compare (s2));
                default:
-                       return true;
+                       return 0;
                }
        }
 
        switch (region_list_sort_type) {
        case ByName:
-               return strcasecmp (region1->name().c_str(), region2->name().c_str());
+               cmp = strcasecmp (region1->name().c_str(), region2->name().c_str());
                break;
 
        case ByLength:
-               return region1->length() - region2->length();
+               cmp = region1->length() - region2->length();
                break;
                
        case ByPosition:
-               return region1->position() - region2->position();
+               cmp = region1->position() - region2->position();
                break;
                
        case ByTimestamp:
-               return region1->source().timestamp() - region2->source().timestamp();
+               cmp = region1->source()->timestamp() - region2->source()->timestamp();
                break;
        
        case ByStartInFile:
-               return region1->start() - region2->start();
+               cmp = region1->start() - region2->start();
                break;
                
        case ByEndInFile:
-               return (region1->start() + region1->length()) - (region2->start() + region2->length());
+               cmp = (region1->start() + region1->length()) - (region2->start() + region2->length());
                break;
                
        case BySourceFileName:
-               return strcasecmp (region1->source().name().c_str(), region2->source().name().c_str());
+               cmp = strcasecmp (region1->source()->name().c_str(), region2->source()->name().c_str());
                break;
 
        case BySourceFileLength:
-               return region1->source().length() - region2->source().length();
+               cmp = region1->source_length(0) - region2->source_length(0);
                break;
                
        case BySourceFileCreationDate:
-               return region1->source().timestamp() - region2->source().timestamp();
+               cmp = region1->source()->timestamp() - region2->source()->timestamp();
                break;
 
        case BySourceFileFS:
-               if (region1->source().name() == region2->source().name()) {
-                       return strcasecmp (region1->name().c_str(),  region2->name().c_str());
+               if (region1->source()->name() == region2->source()->name()) {
+                       cmp = strcasecmp (region1->name().c_str(),  region2->name().c_str());
                } else {
-                       return strcasecmp (region1->source().name().c_str(),  region2->source().name().c_str());
+                       cmp = strcasecmp (region1->source()->name().c_str(),  region2->source()->name().c_str());
                }
                break;
        }
 
-       return FALSE;
+       if (cmp < 0) {
+               return -1;
+       } else if (cmp > 0) {
+               return 1;
+       } else {
+               return 0;
+       }
 }
 
 void
@@ -798,128 +1019,139 @@ Editor::reset_region_list_sort_type (RegionListSortType type)
 {
        if (type != region_list_sort_type) {
                region_list_sort_type = type;
-
-               switch (type) {
-               case ByName:
-                       region_list_display.set_column_title(0, _("Regions/name"));
-                       break;
-                       
-               case ByLength:
-                       region_list_display.set_column_title (0, _("Regions/length"));
-                       break;
-                       
-               case ByPosition:
-                       region_list_display.set_column_title (0, _("Regions/position"));
-                       break;
-                       
-               case ByTimestamp:
-                       region_list_display.set_column_title (0, _("Regions/creation"));
-                       break;
-                       
-               case ByStartInFile:
-                       region_list_display.set_column_title (0, _("Regions/start"));
-                       break;
-                       
-               case ByEndInFile:
-                       region_list_display.set_column_title (0, _("Regions/end"));
-                       break;
-                       
-               case BySourceFileName:
-                       region_list_display.set_column_title (0, _("Regions/file name"));
-                       break;
-                       
-               case BySourceFileLength:
-                       region_list_display.set_column_title (0, _("Regions/file size"));
-                       break;
-                       
-               case BySourceFileCreationDate:
-                       region_list_display.set_column_title (0, _("Regions/file date"));
-                       break;
-                       
-               case BySourceFileFS:
-                       region_list_display.set_column_title (0, _("Regions/file system"));
-                       break;
-               }
-                       
-               region_list_display.sort ();
+               region_list_model->set_sort_func (0, (mem_fun (*this, &Editor::region_list_sorter)));
        }
 }
 
 void
 Editor::reset_region_list_sort_direction (bool up)
 {
-       region_list_display.set_sort_type (up ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING);
-       region_list_display.sort ();
+       region_list_model->set_sort_column (0, up ? SORT_ASCENDING : SORT_DESCENDING);
 }
 
 void
-Editor::audition_region_from_region_list ()
+Editor::region_list_selection_mapover (slot<void,boost::shared_ptr<Region> > sl)
 {
-       if (region_list_button_region) {
-               consider_auditioning (dynamic_cast<AudioRegion*> (region_list_button_region));
+       Glib::RefPtr<TreeSelection> selection = region_list_display.get_selection();
+       TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
+       TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
+
+       if (selection->count_selected_rows() == 0 || session == 0) {
+               return;
+       }
+
+       for (; i != rows.end(); ++i) {
+               TreeIter iter;
+
+               if ((iter = region_list_model->get_iter (*i))) {
+
+                       /* some rows don't have a region associated with them, but can still be
+                          selected (XXX maybe prevent them from being selected)
+                       */
+
+                       boost::shared_ptr<Region> r = (*iter)[region_list_columns.region];
+
+                       if (r) {
+                               sl (r);
+                       }
+               }
        }
 }
 
 void
-Editor::hide_region_from_region_list ()
+Editor::hide_a_region (boost::shared_ptr<Region> r)
 {
-       if (session == 0 || region_list_button_region == 0) {
-               return;
-       }
+       r->set_hidden (true);
+}
 
-       region_list_button_region->set_hidden (true);
+void
+Editor::remove_a_region (boost::shared_ptr<Region> r)
+{
+       session->remove_region_from_region_list (r);
 }
 
 void
-Editor::remove_region_from_region_list ()
+Editor::audition_region_from_region_list ()
 {
-       if (session == 0 || region_list_button_region == 0) {
-               return;
-       }
+       region_list_selection_mapover (mem_fun (*this, &Editor::consider_auditioning));
+}
 
-       session->remove_region_from_region_list (*region_list_button_region);
+void
+Editor::hide_region_from_region_list ()
+{
+       region_list_selection_mapover (mem_fun (*this, &Editor::hide_a_region));
 }
 
 void
-Editor::remove_selected_regions_from_region_list ()
+Editor::remove_region_from_region_list ()
 {
-       using namespace Gtk::CTree_Helpers;
-       SelectionList& selected = region_list_display.selection();
+       region_list_selection_mapover (mem_fun (*this, &Editor::remove_a_region));
+}
 
-       /* called from idle context to avoid snafus with the list
-          state.
-       */
-       
-       if (selected.empty() || session == 0) {
+void  
+Editor::region_list_display_drag_data_received (const RefPtr<Gdk::DragContext>& context,
+                                                                                               int x, int y, 
+                                                                                               const SelectionData& data,
+                                                                                               guint info, guint time)
+{
+       vector<ustring> paths;
+
+       if (data.get_target() == "GTK_TREE_MODEL_ROW") {
+               region_list_display.on_drag_data_received (context, x, y, data, info, time);
                return;
        }
-       
-       vector<Region*> to_be_deleted;
 
-       for (SelectionList::iterator i = selected.begin(); i != selected.end(); ++i) {
-               to_be_deleted.push_back (static_cast<Region*> ((*i).get_data()));
+       if (convert_drop_to_paths (paths, context, x, y, data, info, time) == 0) {
+               nframes64_t pos = 0;
+               if (Profile->get_sae() || Config->get_only_copy_imported_files()) {
+                       do_import (paths, Editing::ImportDistinctFiles, Editing::ImportAsRegion, SrcBest, pos); 
+               } else {
+                       do_embed (paths, Editing::ImportDistinctFiles, ImportAsRegion, pos);
+               }
+               context->drag_finish (true, false, time);
        }
+}
 
-       for (vector<Region*>::iterator i = to_be_deleted.begin(); i != to_be_deleted.end(); ++i) {
-               session->remove_region_from_region_list (**i);
-       }
+bool
+Editor::region_list_selection_filter (const RefPtr<TreeModel>& model, const TreeModel::Path& path, bool yn)
+{
+       /* not possible to select rows that do not represent regions, like "Hidden" */
+       
+       TreeModel::iterator iter = model->get_iter (path);
+
+       if (iter) {
+               boost::shared_ptr<Region> r =(*iter)[region_list_columns.region];
+               if (!r) {
+                       return false;
+               }
+       } 
 
-       return;
+       return true;
 }
 
-void  
-Editor::region_list_display_drag_data_received  (GdkDragContext     *context,
-                                                gint                x,
-                                                gint                y,
-                                                GtkSelectionData   *data,
-                                                guint               info,
-                                                guint               time)
+void
+Editor::region_name_edit (const Glib::ustring& path, const Glib::ustring& new_text)
 {
-       vector<string> paths;
+       boost::shared_ptr<Region> region;
+       TreeIter iter;
+       
+       if ((iter = region_list_model->get_iter (path))) {
+               region = (*iter)[region_list_columns.region];
+               (*iter)[region_list_columns.name] = new_text;
+       }
+       
+       /* now mapover everything */
 
-       if (convert_drop_to_paths (paths, context, x, y, data, info, time) == 0) {
-               do_embed_sndfiles (paths, false);
+       if (region) {
+               vector<RegionView*> equivalents;
+               get_regions_corresponding_to (region, equivalents);
+
+               for (vector<RegionView*>::iterator i = equivalents.begin(); i != equivalents.end(); ++i) {
+                       if (new_text != (*i)->region()->name()) {
+                               (*i)->region()->set_name (new_text);
+                       }
+               }
        }
 
-       gtk_drag_finish (context, TRUE, FALSE, time);
 }
+