Remove all use of nframes_t.
[ardour.git] / gtk2_ardour / editor_regions.cc
index 4d51c2539dcc75a917f8992ae778c65a6e789e7c..30fce08a932f5b6e8046ffcfabcef74d19e52660 100644 (file)
 #include "ardour/audiofilesource.h"
 #include "ardour/region_factory.h"
 #include "ardour/session.h"
+#include "ardour/session_playlists.h"
 #include "ardour/silentfilesource.h"
 #include "ardour/profile.h"
 
+#include "gtkmm2ext/treeutils.h"
+
 #include "editor.h"
 #include "editing.h"
 #include "keyboard.h"
@@ -56,12 +59,15 @@ using Gtkmm2ext::Keyboard;
 
 EditorRegions::EditorRegions (Editor* e)
        : EditorComponent (e)
+       , old_focus (0)
+       , 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)
+       , expanded (false)
 {
        _display.set_size_request (100, -1);
        _display.set_name ("RegionListDisplay");
@@ -76,8 +82,9 @@ EditorRegions::EditorRegions (Editor* e)
        _model->set_sort_column (0, SORT_ASCENDING);
 
        _display.set_model (_model);
+
        _display.append_column (_("Regions"), _columns.name);
-       _display.append_column (_("Start"), _columns.start);
+       _display.append_column (_("Position"), _columns.position);
        _display.append_column (_("End"), _columns.end);
        _display.append_column (_("Length"), _columns.length);
        _display.append_column (_("Sync"), _columns.sync);
@@ -87,14 +94,18 @@ EditorRegions::EditorRegions (Editor* e)
        _display.append_column (_("G"), _columns.glued);
        _display.append_column (_("M"), _columns.muted);
        _display.append_column (_("O"), _columns.opaque);
-       _display.append_column (_("Used"), _columns.used);
-       _display.append_column (_("Path"), _columns.path);
+       // _display.append_column (_("Used"), _columns.used);
+       // _display.append_column (_("Path"), _columns.path);
        _display.set_headers_visible (true);
        //_display.set_grid_lines (TREE_VIEW_GRID_LINES_BOTH);
 
+       /* show path as the row tooltip */
+       _display.set_tooltip_column (15); /* path */
+
        CellRendererText* region_name_cell = dynamic_cast<CellRendererText*>(_display.get_column_cell_renderer (0));
        region_name_cell->property_editable() = true;
        region_name_cell->signal_edited().connect (sigc::mem_fun (*this, &EditorRegions::name_edit));
+       region_name_cell->signal_editing_started().connect (sigc::mem_fun (*this, &EditorRegions::name_editing_started));
 
        _display.get_selection()->set_select_function (sigc::mem_fun (*this, &EditorRegions::selection_filter));
 
@@ -144,9 +155,16 @@ EditorRegions::EditorRegions (Editor* e)
        _scroller.add (_display);
        _scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
 
-       _display.signal_key_press_event().connect (sigc::mem_fun(*this, &EditorRegions::key_press));
        _display.signal_button_press_event().connect (sigc::mem_fun(*this, &EditorRegions::button_press), false);
        _change_connection = _display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &EditorRegions::selection_changed));
+
+       _scroller.signal_key_press_event().connect (sigc::mem_fun(*this, &EditorRegions::key_press), false);
+       _scroller.signal_focus_in_event().connect (sigc::mem_fun (*this, &EditorRegions::focus_in), false);
+       _scroller.signal_focus_out_event().connect (sigc::mem_fun (*this, &EditorRegions::focus_out));
+
+       _display.signal_enter_notify_event().connect (sigc::mem_fun (*this, &EditorRegions::enter_notify), false);
+       _display.signal_leave_notify_event().connect (sigc::mem_fun (*this, &EditorRegions::leave_notify), false);
+
        // _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));
@@ -155,6 +173,60 @@ EditorRegions::EditorRegions (Editor* e)
        ARDOUR::RegionFactory::CheckNewRegion.connect (check_new_region_connection, MISSING_INVALIDATOR, ui_bind (&EditorRegions::add_region, this, _1), gui_context());
 }
 
+bool
+EditorRegions::focus_in (GdkEventFocus*)
+{
+       Window* win = dynamic_cast<Window*> (_scroller.get_toplevel ());
+
+       if (win) {
+               old_focus = win->get_focus ();
+       } else {
+               old_focus = 0;
+       }
+
+       name_editable = 0;
+
+       /* try to do nothing on focus in (doesn't work, hence selection_count nonsense) */
+       return true;
+}
+
+bool
+EditorRegions::focus_out (GdkEventFocus*)
+{
+       if (old_focus) {
+               old_focus->grab_focus ();
+               old_focus = 0;
+       }
+
+       name_editable = 0;
+
+       return false;
+}
+
+bool
+EditorRegions::enter_notify (GdkEventCrossing* ev)
+{
+       /* arm counter so that ::selection_filter() will deny selecting anything for the 
+          next two attempts to change selection status.
+       */
+       _scroller.grab_focus ();
+       Keyboard::magic_widget_grab_focus ();
+       return false;
+}
+
+bool
+EditorRegions::leave_notify (GdkEventCrossing*)
+{
+       if (old_focus) {
+               old_focus->grab_focus ();
+               old_focus = 0;
+       }
+
+       name_editable = 0;
+       Keyboard::magic_widget_drop_focus ();
+       return false;
+}
+
 void
 EditorRegions::set_session (ARDOUR::Session* s)
 {
@@ -166,7 +238,7 @@ 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);
+               add_region (*x);
        }
 }
 
@@ -197,7 +269,7 @@ EditorRegions::add_region (boost::shared_ptr<Region> region)
                        boost::shared_ptr<Region> proxy = parent[_columns.region];
                        proxy.reset ();
                } else {
-                        string s = (*iter)[_columns.name];
+                       string s = (*iter)[_columns.name];
                        if (s != _("Hidden")) {
                                parent = *(_model->insert(iter));
                                parent[_columns.name] = _("Hidden");
@@ -207,7 +279,6 @@ EditorRegions::add_region (boost::shared_ptr<Region> region)
                                parent = *iter;
                        }
                }
-
                row = *(_model->append (parent.children()));
 
        } else if (region->whole_file()) {
@@ -275,8 +346,12 @@ EditorRegions::add_region (boost::shared_ptr<Region> region)
                        row[_columns.path] = _("(MISSING) ") + region->source()->name();
 
                } else {
-                       row[_columns.path] = region->source()->name();
-
+                       boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource>(region->source());
+                       if (fs) {
+                               row[_columns.path] = fs->path();
+                       } else {
+                               row[_columns.path] = region->source()->name();
+                       }
                }
 
                if (region->automatic()) {
@@ -292,29 +367,30 @@ EditorRegions::add_region (boost::shared_ptr<Region> region)
                bool found_parent = false;
 
                for (i = rows.begin(); i != rows.end(); ++i) {
-                       boost::shared_ptr<Region> rr = (*i)[_columns.region];
-                       boost::shared_ptr<AudioRegion> r = boost::dynamic_pointer_cast<AudioRegion>(rr);
+                       boost::shared_ptr<Region> r = (*i)[_columns.region];
 
                        if (r && r->whole_file()) {
 
                                if (region->source_equivalent (r)) {
-                                       row = *(_model->append ((*i).children()));
                                        found_parent = true;
-                                       break;
                                }
                        }
-
+                        
                        TreeModel::iterator ii;
                        TreeModel::Children subrows = (*i).children();
 
                        for (ii = subrows.begin(); ii != subrows.end(); ++ii) {
-                               boost::shared_ptr<Region> rrr = (*ii)[_columns.region];
+                               boost::shared_ptr<Region> rr = (*ii)[_columns.region];
 
-                               if (region->region_list_equivalent (rrr)) {
+                               if (region->region_list_equivalent (rr)) {
                                        return;
-
                                }
                        }
+
+                       if (found_parent) {
+                               row = *(_model->append ((*i).children()));
+                               break;
+                       }
                }
 
                if (!found_parent) {
@@ -332,38 +408,114 @@ EditorRegions::add_region (boost::shared_ptr<Region> region)
 void
 EditorRegions::region_changed (boost::shared_ptr<Region> r, const PropertyChange& what_changed)
 {
-       if (what_changed.contains (ARDOUR::Properties::name) ||
-            what_changed.contains (ARDOUR::Properties::start) ||
-            what_changed.contains (ARDOUR::Properties::position) ||
-            what_changed.contains (ARDOUR::Properties::length) ||
-           what_changed.contains (ARDOUR::Properties::locked) ||
-           what_changed.contains (ARDOUR::Properties::position_lock_style) ||
-           what_changed.contains (ARDOUR::Properties::muted) ||
-           what_changed.contains (ARDOUR::Properties::opaque) ||
-           what_changed.contains (ARDOUR::Properties::fade_in) ||
-           what_changed.contains (ARDOUR::Properties::fade_out)) {
+       PropertyChange our_interests;
+
+       our_interests.add (ARDOUR::Properties::name);
+       our_interests.add (ARDOUR::Properties::position);
+       our_interests.add (ARDOUR::Properties::length);
+       our_interests.add (ARDOUR::Properties::start);
+       our_interests.add (ARDOUR::Properties::locked);
+       our_interests.add (ARDOUR::Properties::position_lock_style);
+       our_interests.add (ARDOUR::Properties::muted);
+       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 ()) {
+
                        TreeModel::Children children = (*i)->children ();
-                       TreeModel::iterator j = children.begin ();
-                       while (j != children.end()) {
-                               boost::shared_ptr<Region> c = (*j)[_columns.region];
-                               if (c == r) {
-                                       break;
+                       TreeModel::iterator found = children.end ();
+                       
+                       boost::shared_ptr<Region> c = (*i)[_columns.region];
+
+                       if (c == r) {
+
+                               /* 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;
                                }
-                               ++j;
                        }
 
-                       if (j != children.end()) {
-                                populate_row (r, *j);
+                       if (found != children.end()) {
+
+                               boost::shared_ptr<AudioRegion> audioregion = boost::dynamic_pointer_cast<AudioRegion>(r);
+                               uint32_t used = _editor->get_regionview_count_from_region_list (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);
+                               }
+
+                               break;
                        }
 
                        ++i;
                }
-
        }
 
        if (what_changed.contains (ARDOUR::Properties::hidden)) {
@@ -378,12 +530,7 @@ EditorRegions::selection_changed ()
                return;
        }
 
-       /* We may have selected a region which is not displayed in the Editor.  If this happens, the
-          result will be no selected regions in the editor's Selection.  Without the following line,
-          this `no-selection' will be mapped back to our list, meaning that the selection will
-          appear not to take.
-       */
-       _editor->_block_region_list_update_if_empty = true;
+       _editor->_region_selection_change_updates_region_list = false;
 
        if (_display.get_selection()->count_selected_rows() > 0) {
 
@@ -397,10 +544,12 @@ EditorRegions::selection_changed ()
                        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"
+                               // they could have clicked on a row that is just a placeholder, like "Hidden"
+                               // although that is not allowed by our selection filter. check it anyway
+                               // since we need a region ptr.
 
                                if (region) {
-
+                                        
                                        if (region->automatic()) {
 
                                                _display.get_selection()->unselect(*i);
@@ -418,16 +567,18 @@ EditorRegions::selection_changed ()
                _editor->get_selection().clear_regions ();
        }
 
-       _editor->_block_region_list_update_if_empty = false;
+       _editor->_region_selection_change_updates_region_list = true;
 }
 
 void
 EditorRegions::set_selected (RegionSelection& regions)
 {
+       TreeModel::Children rows = _model->children();
+
        for (RegionSelection::iterator iter = regions.begin(); iter != regions.end(); ++iter) {
 
                TreeModel::iterator i;
-               TreeModel::Children rows = _model->children();
+               
                boost::shared_ptr<Region> r ((*iter)->region());
 
                for (i = rows.begin(); i != rows.end(); ++i) {
@@ -469,6 +620,7 @@ EditorRegions::set_selected_in_subrow (boost::shared_ptr<Region> region, TreeMod
                        }
                }
        }
+       
        return false;
 }
 
@@ -493,23 +645,22 @@ EditorRegions::redisplay ()
 
        bool tree_expanded = false;
 
-       if (_toggle_full_action && _toggle_full_action->get_active()) {   //If the list was expanded prior to rebuilding,
-               tree_expanded = true;                                                                                                                           //expand it again afterwards
+       /* If the list was expanded prior to rebuilding, expand it again afterwards */
+       if (toggle_full_action()->get_active()) {
+               tree_expanded = true;
        }
 
        _display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
        _model->clear ();
 
-       /* now add everything we have, via a temporary list used to help with
-               sorting.
-       */
+       /* now add everything we have, via a temporary list used to help with sorting */
 
        tmp_region_list.clear();
 
-        const RegionFactory::RegionMap& regions (RegionFactory::regions());
-        for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end(); ++i) {
-                insert_into_tmp_regionlist (i->second);
-        }
+       const RegionFactory::RegionMap& regions (RegionFactory::regions());
+       for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end(); ++i) {
+               insert_into_tmp_regionlist (i->second);
+       }
 
        for (list<boost::shared_ptr<Region> >::iterator r = tmp_region_list.begin(); r != tmp_region_list.end(); ++r) {
                add_region (*r);
@@ -630,159 +781,76 @@ EditorRegions::update_all_subrows (TreeModel::Row const &parent_row, int level)
 }
 
 void
-EditorRegions::populate_row (boost::shared_ptr<Region> region, TreeModel::Row const &row)
+EditorRegions::format_position (framepos_t pos, char* buf, size_t bufsize)
 {
-       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;
        Timecode::Time timecode;
 
-       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 = _editor->get_regionview_count_from_region_list (region);
-       sprintf (used_str, "%4d" , used);
-
        switch (ARDOUR_UI::instance()->secondary_clock.mode ()) {
-       case AudioClock::Timecode:
-       case AudioClock::Off:                                                                                           /* If the secondary clock is off, default to Timecode */
-               _session->timecode_time (region->position(), timecode);
-               sprintf (start_str, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
-               _session->timecode_time (region->position() + region->length() - 1, timecode);
-               sprintf (end_str, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
-               _session->timecode_time (region->length(), timecode);
-               sprintf (length_str, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
-               _session->timecode_time (region->sync_position(), timecode);
-               sprintf (sync_str, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
-
-               if (audioRegion && !fades_in_seconds) {
-                       _session->timecode_time (audioRegion->fade_in()->back()->when, timecode);
-                       sprintf (fadein_str, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
-                       _session->timecode_time (audioRegion->fade_out()->back()->when, timecode);
-                       sprintf (fadeout_str, "%02d:%02d:%02d:%02d", timecode.hours, timecode.minutes, timecode.seconds, timecode.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(), 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);
-               }
+               _session->tempo_map().bbt_time (pos, bbt);
+               snprintf (buf, bufsize, "%03d|%02d|%04d" , bbt.bars, bbt.beats, bbt.ticks);
                break;
 
        case AudioClock::MinSec:
-               nframes_t left;
+               framepos_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;
+               left = pos;
                hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
-               left -= (nframes_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
+               left -= (framecnt_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);
+               left -= (framecnt_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();
-               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);
-               }
-
+               snprintf (buf, bufsize, "%02d:%02d:%06.3f", hrs, mins, secs);
                break;
 
        case AudioClock::Frames:
-               snprintf (start_str, sizeof (start_str), "%" PRId64, region->position());
-               snprintf (end_str, sizeof (end_str), "%" PRId64, (region->last_frame()));
-               snprintf (length_str, sizeof (length_str), "%" PRId64, region->length());
-               snprintf (sync_str, sizeof (sync_str), "%" PRId64, region->sync_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));
-               }
-
+               snprintf (buf, bufsize, "%" PRId64, pos);
                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);
                break;
        }
+}
+
+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);
+
+       populate_row_position (region, row, used);
+       populate_row_end (region, row, used);
+       populate_row_sync (region, row, used);
+       populate_row_fade_in (region, row, used, audioregion);
+       populate_row_fade_out (region, row, used, audioregion);
+       populate_row_locked (region, row, used);
+       populate_row_glued (region, row, used);
+       populate_row_muted (region, row, used);
+       populate_row_opaque (region, row, used);
+       populate_row_length (region, row);
+       populate_row_source (region, row);
+       populate_row_name (region, row);
+       populate_row_used (region, row, used);
+}
 
+#if 0
        if (audioRegion && fades_in_seconds) {
 
-               nframes_t left;
+               framepos_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);
+               left -= (framepos_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()) {
@@ -793,7 +861,7 @@ EditorRegions::populate_row (boost::shared_ptr<Region> region, TreeModel::Row co
 
                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);
+               left -= (framepos_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()) {
@@ -802,114 +870,204 @@ EditorRegions::populate_row (boost::shared_ptr<Region> region, TreeModel::Row co
                        sprintf (fadeout_str, "%01dmS", millisecs);
                }
        }
+#endif
 
-       if (used > 1) {
-               row[_columns.start] = _("Multiple");
-               row[_columns.end] = _("Multiple");
-               row[_columns.sync] = _("Multiple");
-               row[_columns.fadein] = _("Multiple");
-               row[_columns.fadeout] = _("Multiple");
-               row[_columns.locked] = false;
-               row[_columns.glued] = false;
-               row[_columns.muted] = false;
-               row[_columns.opaque] = false;
+void
+EditorRegions::populate_row_used (boost::shared_ptr<Region> region, TreeModel::Row const& row, uint32_t used)
+{
+       char buf[8];
+       snprintf (buf, sizeof (buf), "%4d" , used);
+       row[_columns.used] = buf;
+}
+
+void
+EditorRegions::populate_row_length (boost::shared_ptr<Region> region, TreeModel::Row const &row)
+{
+       char buf[16];
+       format_position (region->length(), buf, sizeof (buf));
+       row[_columns.length] = buf;
+}
+
+void
+EditorRegions::populate_row_end (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
+{
+       if (region->whole_file()) {
+               row[_columns.end] = "";
+       } else if (used > 1) {
+               row[_columns.end] = _("Mult.");
+       } else {
+               char buf[16];
+               format_position (region->last_frame(), buf, sizeof (buf));
+               row[_columns.end] = buf;
+       }
+}
+
+void
+EditorRegions::populate_row_position (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
+{
+       if (region->whole_file()) {
+               row[_columns.position] = "";
+       } else if (used > 1) {
+               row[_columns.position] = _("Mult.");
        } else {
-               row[_columns.start] = start_str;
-               row[_columns.end] = end_str;
+               char buf[16];
+               format_position (region->position(), buf, sizeof (buf));
+               row[_columns.position] = buf;
+       }
+}
 
+void
+EditorRegions::populate_row_sync (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
+{
+       if (region->whole_file()) {
+               row[_columns.sync] = "";
+       } else if (used > 1) {
+               row[_columns.sync] = _("Mult."); /* translators: a short phrase for "multiple" as in "many" */
+       } else {
                if (region->sync_position() == region->position()) {
                        row[_columns.sync] = _("Start");
-               } else if (region->sync_position() == (region->position() + region->length() - 1)) {
+               } else if (region->sync_position() == (region->last_frame())) {
                        row[_columns.sync] = _("End");
                } else {
-                       row[_columns.sync] = sync_str;
+                       char buf[16];
+                       format_position (region->sync_position(), buf, sizeof (buf));
+                       row[_columns.sync] = buf;
                }
+       }
+}
 
-               if (audioRegion) {
-                       if (audioRegion->fade_in_active()) {
-                               row[_columns.fadein] = string_compose("%1%2%3", " ", fadein_str, " ");
-                       } else {
-                               row[_columns.fadein] = string_compose("%1%2%3", "(", fadein_str, ")");
-                       }
+void
+EditorRegions::populate_row_fade_in (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used, boost::shared_ptr<AudioRegion> audioregion)
+{
+       if (!audioregion || region->whole_file()) {
+               row[_columns.fadein] = "";
+       } else {
+               if (used > 1) {
+                       row[_columns.fadein] = _("Multiple");
                } else {
-                       row[_columns.fadein] = "";
-               }
 
-               if (audioRegion) {
-                       if (audioRegion->fade_out_active()) {
-                               row[_columns.fadeout] = string_compose("%1%2%3", " ", fadeout_str, " ");
+                       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 {
-                               row[_columns.fadeout] = string_compose("%1%2%3", "(", fadeout_str, ")");
+                               row[_columns.fadein] = string_compose("%1%2%3", "(", buf, ")");
                        }
-               } else {
-                       row[_columns.fadeout] = "";
                }
+       }
+}
 
+void
+EditorRegions::populate_row_fade_out (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used, boost::shared_ptr<AudioRegion> audioregion)
+{
+       if (!audioregion || region->whole_file()) {
+               row[_columns.fadeout] = "";
+       } else {
+               if (used > 1) {
+                       row[_columns.fadeout] = _("Multiple");
+               } 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)
+{
+       if (region->whole_file()) {
+               row[_columns.locked] = false;
+       } else if (used > 1) {
+               row[_columns.locked] = false;
+       } else {
                row[_columns.locked] = region->locked();
+       }
+}
 
+void
+EditorRegions::populate_row_glued (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
+{
+       if (region->whole_file() || used > 1) {
+               row[_columns.glued] = false;
+       } else {
                if (region->position_lock_style() == MusicTime) {
                        row[_columns.glued] = true;
                } else {
                        row[_columns.glued] = false;
                }
+       }
+}
 
+void
+EditorRegions::populate_row_muted (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
+{
+       if (region->whole_file() || used > 1) {
+               row[_columns.muted] = false;
+       } else {
                row[_columns.muted] = region->muted();
-               row[_columns.opaque] = region->opaque();
        }
+}
 
-       row[_columns.length] = length_str;
-       row[_columns.used] = used_str;
-
-       if (missing_source) {
-               row[_columns.path] = _("MISSING ") + region->source()->name();
+void
+EditorRegions::populate_row_opaque (boost::shared_ptr<Region> region, TreeModel::Row const &row, uint32_t used)
+{
+       if (region->whole_file() || used > 1) {
+               row[_columns.opaque] = false;
        } else {
-               row[_columns.path] = region->source()->name();
+               row[_columns.opaque] = region->opaque();
        }
+}
 
+void
+EditorRegions::populate_row_name (boost::shared_ptr<Region> region, TreeModel::Row const &row)
+{
        if (region->n_channels() > 1) {
                row[_columns.name] = string_compose("%1  [%2]", region->name(), region->n_channels());
        } else {
                row[_columns.name] = region->name();
        }
-}
+}        
 
 void
-EditorRegions::build_menu ()
+EditorRegions::populate_row_source (boost::shared_ptr<Region> region, TreeModel::Row const &row)
 {
-       _menu = dynamic_cast<Menu*>(ActionManager::get_widget ("/RegionListMenu"));
-
-       /* now grab specific menu items that we need */
-
-       Glib::RefPtr<Action> act;
-
-       _hide_action = ActionManager::get_action (X_("RegionList"), X_("rlHide"));
-       _show_action = ActionManager::get_action (X_("RegionList"), X_("rlShow"));
-
-       act = ActionManager::get_action (X_("RegionList"), X_("rlShowAll"));
-       if (act) {
-               _toggle_full_action = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
-       }
-
-       act = ActionManager::get_action (X_("RegionList"), X_("rlShowAuto"));
-       if (act) {
-               _toggle_show_auto_regions_action = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
+        if (boost::dynamic_pointer_cast<SilentFileSource>(region->source())) {
+               row[_columns.path] = _("MISSING ") + region->source()->name();
+       } else {
+               row[_columns.path] = region->source()->name();
        }
 }
 
 void
 EditorRegions::toggle_show_auto_regions ()
 {
-       _show_automatic_regions = _toggle_show_auto_regions_action->get_active();
+       _show_automatic_regions = toggle_show_auto_regions_action()->get_active();
        redisplay ();
 }
 
 void
 EditorRegions::toggle_full ()
 {
-       if (_toggle_full_action->get_active()) {
+       set_full (toggle_full_action()->get_active ());
+}
+
+void
+EditorRegions::set_full (bool f)
+{
+       if (f) {
                _display.expand_all ();
+                expanded = true;
        } else {
                _display.collapse_all ();
+                expanded = false;
        }
 }
 
@@ -917,7 +1075,7 @@ void
 EditorRegions::show_context_menu (int button, int time)
 {
        if (_menu == 0) {
-               build_menu ();
+               _menu = dynamic_cast<Menu*> (ActionManager::get_widget ("/RegionListMenu"));
        }
 
        if (_display.get_selection()->count_selected_rows() > 0) {
@@ -944,19 +1102,44 @@ EditorRegions::show_context_menu (int button, int time)
                }
        }
 
-       _hide_action->set_sensitive (have_shown);
-       _show_action->set_sensitive (have_hidden);
+       hide_action()->set_sensitive (have_shown);
+       show_action()->set_sensitive (have_hidden);
 
        _menu->popup (button, time);
 }
 
 bool
-EditorRegions::key_press (GdkEventKey* /*ev*/)
+EditorRegions::key_press (GdkEventKey* ev)
 {
+       TreeViewColumn *col;
+
+       switch (ev->keyval) {
+       case GDK_Tab:
+       case GDK_ISO_Left_Tab:
+                
+               if (name_editable) {
+                       name_editable->editing_done ();
+                       name_editable = 0;
+               }
+
+               col = _display.get_column (0); // select&focus on name column
+
+               if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
+                       treeview_select_previous (_display, _model, col);
+               } else {
+                       treeview_select_next (_display, _model, col);
+               }
+
+               return true;
+               break;
+
+       default:
+               break;
+       }
+
        return false;
 }
 
-
 bool
 EditorRegions::button_press (GdkEventButton *ev)
 {
@@ -1153,10 +1336,15 @@ EditorRegions::drag_data_received (const RefPtr<Gdk::DragContext>& context,
 }
 
 bool
-EditorRegions::selection_filter (const RefPtr<TreeModel>& model, const TreeModel::Path& path, bool /*yn*/)
+EditorRegions::selection_filter (const RefPtr<TreeModel>& model, const TreeModel::Path& path, bool already_selected)
 {
        /* not possible to select rows that do not represent regions, like "Hidden" */
 
+       if (already_selected) {
+               /* deselecting anything is OK with us */
+               return true;
+       }
+
        TreeModel::iterator iter = model->get_iter (path);
 
        if (iter) {
@@ -1169,9 +1357,25 @@ EditorRegions::selection_filter (const RefPtr<TreeModel>& model, const TreeModel
        return true;
 }
 
+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);
+
+       if (e) {
+               e->set_name (X_("RegionNameEditorEntry"));
+       }
+}
+                          
 void
 EditorRegions::name_edit (const std::string& path, const std::string& new_text)
 {
+       name_editable = 0;
+
        boost::shared_ptr<Region> region;
        TreeIter iter;
 
@@ -1292,3 +1496,161 @@ EditorRegions::opaque_changed (std::string const & path)
        }
 
 }
+
+XMLNode &
+EditorRegions::get_state () const
+{
+       XMLNode* node = new XMLNode (X_("RegionList"));
+
+       node->add_property (X_("sort-type"), enum_2_string (_sort_type));
+
+       RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), X_("SortAscending"));
+       bool const ascending = RefPtr<RadioAction>::cast_dynamic(act)->get_active ();
+       node->add_property (X_("sort-ascending"), ascending ? "yes" : "no");
+       node->add_property (X_("show-all"), toggle_full_action()->get_active() ? "yes" : "no");
+       node->add_property (X_("show-automatic-regions"), _show_automatic_regions ? "yes" : "no");
+
+       return *node;
+}
+               
+void
+EditorRegions::set_state (const XMLNode & node)
+{
+       bool changed = false;
+
+       if (node.name() != X_("RegionList")) {
+               return;
+       }
+
+       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 {
+                       act = ActionManager::get_action (X_("RegionList"), X_("SortDescending"));
+               }
+
+               RefPtr<RadioAction>::cast_dynamic(act)->set_active ();
+       }
+
+       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);
+       }
+
+       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 ();
+       }
+}
+
+RefPtr<RadioAction>
+EditorRegions::sort_type_action (Editing::RegionListSortType t) const
+{
+       const char* action = 0;
+
+       switch (t) {
+       case Editing::ByName:
+               action = X_("SortByRegionName");
+               break;
+       case Editing::ByLength:
+               action = X_("SortByRegionLength");
+               break;
+       case Editing::ByPosition:
+               action = X_("SortByRegionPosition");
+               break;
+       case Editing::ByTimestamp:
+               action = X_("SortByRegionTimestamp");
+               break;
+       case Editing::ByStartInFile:
+               action = X_("SortByRegionStartinFile");
+               break;
+       case Editing::ByEndInFile:
+               action = X_("SortByRegionEndinFile");
+               break;
+       case Editing::BySourceFileName:
+               action = X_("SortBySourceFileName");
+               break;
+       case Editing::BySourceFileLength:
+               action = X_("SortBySourceFileLength");
+               break;
+       case Editing::BySourceFileCreationDate:
+               action = X_("SortBySourceFileCreationDate");
+               break;
+       case Editing::BySourceFileFS:
+               action = X_("SortBySourceFilesystem");
+               break;
+       default:
+               fatal << string_compose (_("programming error: %1: %2"), "EditorRegions: impossible sort type", (int) t) << endmsg;
+               /*NOTREACHED*/
+       }
+
+       RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), action);
+       assert (act);
+
+       return RefPtr<RadioAction>::cast_dynamic (act);
+}
+
+RefPtr<Action>
+EditorRegions::hide_action () const
+{
+       return ActionManager::get_action (X_("RegionList"), X_("rlHide"));
+       
+}
+
+RefPtr<Action>
+EditorRegions::show_action () const
+{
+       return ActionManager::get_action (X_("RegionList"), X_("rlShow"));
+}
+
+RefPtr<ToggleAction>
+EditorRegions::toggle_full_action () const
+{
+       Glib::RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), X_("rlShowAll"));
+       assert (act);
+       return Glib::RefPtr<ToggleAction>::cast_dynamic (act);
+}
+
+RefPtr<ToggleAction>
+EditorRegions::toggle_show_auto_regions_action () const
+{
+       Glib::RefPtr<Action> act = ActionManager::get_action (X_("RegionList"), X_("rlShowAuto"));
+       assert (act);
+       return Glib::RefPtr<ToggleAction>::cast_dynamic (act);
+}