introduce the notion that note additions and property changes can cause the removal...
[ardour.git] / gtk2_ardour / editor_regions.cc
index b3ca136bcf61f54bdb8a9438a3c4e3a85d30b1ae..dca382415a2e651a4c6aae2fd7adb016c791fce5 100644 (file)
 
 #include "ardour/audioregion.h"
 #include "ardour/audiofilesource.h"
+#include "ardour/region_factory.h"
+#include "ardour/session.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"
@@ -43,6 +42,7 @@
 #include "region_view.h"
 #include "utils.h"
 #include "editor_regions.h"
+#include "editor_drag.h"
 
 #include "i18n.h"
 
@@ -120,73 +120,28 @@ EditorRegions::EditorRegions (Editor* e)
        _scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
 
        _display.signal_key_press_event().connect (sigc::mem_fun(*this, &EditorRegions::key_press));
-       _display.signal_key_release_event().connect (sigc::mem_fun(*this, &EditorRegions::key_release));
        _display.signal_button_press_event().connect (sigc::mem_fun(*this, &EditorRegions::button_press), false);
-       _display.signal_button_release_event().connect (sigc::mem_fun(*this, &EditorRegions::button_release));
        _change_connection = _display.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &EditorRegions::selection_changed));
        // _display.signal_popup_menu().connect (sigc::bind (sigc::mem_fun (*this, &Editor::show__display_context_menu), 1, 0));
 
        //ARDOUR_UI::instance()->secondary_clock.mode_changed.connect (sigc::mem_fun(*this, &Editor::redisplay_regions));
        ARDOUR_UI::instance()->secondary_clock.mode_changed.connect (sigc::mem_fun(*this, &EditorRegions::update_all_rows));
-       ARDOUR::Region::RegionPropertyChanged.connect (region_property_connection, ui_bind (&EditorRegions::update_row, this, _1), gui_context());
-
+       ARDOUR::Region::RegionPropertyChanged.connect (region_property_connection, MISSING_INVALIDATOR, ui_bind (&EditorRegions::region_changed, this, _1, _2), gui_context());
+       ARDOUR::RegionFactory::CheckNewRegion.connect (check_new_region_connection, MISSING_INVALIDATOR, ui_bind (&EditorRegions::add_region, this, _1), gui_context());
 }
 
 void
 EditorRegions::set_session (ARDOUR::Session* s)
 {
        EditorComponent::set_session (s);
-
-       if (_session) {
-               _session->RegionsAdded.connect (_session_connections, ui_bind (&EditorRegions::handle_new_regions, this, _1), gui_context());
-               _session->RegionRemoved.connect (_session_connections, ui_bind (&EditorRegions::handle_region_removed, this, _1), gui_context());
-               _session->RegionHiddenChange.connect (_session_connections, ui_bind (&EditorRegions::region_hidden, this, _1), gui_context());
-       }
-
-       redisplay ();
-}
-
-void
-EditorRegions::handle_region_removed (boost::weak_ptr<Region> wregion)
-{
-       ENSURE_GUI_THREAD (*this, &EditorRegions::handle_region_removed, wregion)
-
        redisplay ();
 }
 
 void
-EditorRegions::handle_new_regions (vector<boost::weak_ptr<Region> >& v)
+EditorRegions::add_regions (vector<boost::shared_ptr<Region> >& regions)
 {
-       ENSURE_GUI_THREAD (*this, &EditorRegions::handle_new_regions, v)
-       add_regions (v);
-}
-
-void
-EditorRegions::region_hidden_weak (boost::weak_ptr<Region> wr)
-{
-       boost::shared_ptr<Region> r (wr.lock());
-
-       if (r) {
-               region_hidden (r);
-       }
-}
-
-void
-EditorRegions::region_hidden (boost::shared_ptr<Region> r)
-{
-       ENSURE_GUI_THREAD (*this, &EditorRegions::region_hidden, r)
-       redisplay ();
-}
-
-
-void
-EditorRegions::add_regions (vector<boost::weak_ptr<Region> >& regions)
-{
-       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 (region);
-               }
+       for (vector<boost::shared_ptr<Region> >::iterator x = regions.begin(); x != regions.end(); ++x) {
+                add_region (*x);
        }
 }
 
@@ -345,20 +300,15 @@ EditorRegions::add_region (boost::shared_ptr<Region> region)
        populate_row(region, (*row));
 }
 
-
 void
-EditorRegions::region_changed (PropertyChange what_changed, boost::weak_ptr<Region> region)
+EditorRegions::region_changed (boost::shared_ptr<Region> r, const PropertyChange& what_changed)
 {
-       ENSURE_GUI_THREAD (*this, &EditorRegions::region_changed, what_changed, region)
-
-       boost::shared_ptr<Region> r = region.lock ();
-
-       if (!r) {
-               return;
-       }
+       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)) {
 
-       if (what_changed & ARDOUR::NameChanged) {
-               /* find the region in our model and change its name */
+               /* find the region in our model and update its row */
                TreeModel::Children rows = _model->children ();
                TreeModel::iterator i = rows.begin ();
                while (i != rows.end ()) {
@@ -373,14 +323,17 @@ EditorRegions::region_changed (PropertyChange what_changed, boost::weak_ptr<Regi
                        }
 
                        if (j != children.end()) {
-                               (*j)[_columns.name] = r->name ();
-                               break;
+                                populate_row (r, *j);
                        }
 
                        ++i;
                }
 
        }
+
+       if (what_changed.contains (ARDOUR::Properties::hidden)) {
+               redisplay ();
+       }
 }
 
 void
@@ -511,7 +464,11 @@ EditorRegions::redisplay ()
        */
 
        tmp_region_list.clear();
-       _session->foreach_region (this, &EditorRegions::insert_into_tmp_regionlist);
+
+        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);
@@ -762,10 +719,10 @@ EditorRegions::populate_row (boost::shared_ptr<Region> region, TreeModel::Row co
                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());
+               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() + region->position());
 
                if (audioRegion && !fades_in_seconds) {
                        snprintf (fadein_str, sizeof (fadein_str), "%u", uint (audioRegion->fade_in()->back()->when));
@@ -886,6 +843,9 @@ EditorRegions::build_menu ()
 
        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);
@@ -927,6 +887,27 @@ EditorRegions::show_context_menu (int button, int time)
                ActionManager::set_sensitive (ActionManager::region_list_selection_sensitive_actions, false);
        }
 
+       /* Enable the "Show" option if any selected regions are hidden, and vice versa for "Hide" */
+
+       bool have_shown = false;
+       bool have_hidden = false;
+       
+       TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
+       for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
+               TreeIter t = _model->get_iter (*i);
+               boost::shared_ptr<Region> r = (*t)[_columns.region];
+               if (r) {
+                       if (r->hidden ()) {
+                               have_hidden = true;
+                       } else {
+                               have_shown = true;
+                       }
+               }
+       }
+
+       _hide_action->set_sensitive (have_shown);
+       _show_action->set_sensitive (have_hidden);
+
        _menu->popup (button, time);
 }
 
@@ -936,20 +917,6 @@ EditorRegions::key_press (GdkEventKey* /*ev*/)
        return false;
 }
 
-bool
-EditorRegions::key_release (GdkEventKey* ev)
-{
-       switch (ev->keyval) {
-       case GDK_Delete:
-               remove_region ();
-               return true;
-               break;
-       default:
-               break;
-       }
-
-       return false;
-}
 
 bool
 EditorRegions::button_press (GdkEventButton *ev)
@@ -969,7 +936,7 @@ EditorRegions::button_press (GdkEventButton *ev)
 
        if (Keyboard::is_context_menu_event (ev)) {
                show_context_menu (ev->button, ev->time);
-               return true;
+               return false;
        }
 
        if (region != 0 && Keyboard::is_button2_event (ev)) {
@@ -983,30 +950,6 @@ EditorRegions::button_press (GdkEventButton *ev)
        return false;
 }
 
-bool
-EditorRegions::button_release (GdkEventButton *ev)
-{
-       TreeIter iter;
-       TreeModel::Path path;
-       TreeViewColumn* column;
-       int cellx;
-       int celly;
-       boost::shared_ptr<Region> region;
-
-       if (_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
-               if ((iter = _model->get_iter (path))) {
-                       region = (*iter)[_columns.region];
-               }
-       }
-
-       if (region && Keyboard::is_delete_event (ev)) {
-               _session->remove_region_from_region_list (region);
-               return true;
-       }
-
-       return false;
-}
-
 int
 EditorRegions::sorter (TreeModel::iterator a, TreeModel::iterator b)
 {
@@ -1144,12 +1087,6 @@ EditorRegions::selection_mapover (sigc::slot<void,boost::shared_ptr<Region> > sl
 }
 
 
-void
-EditorRegions::remove_region ()
-{
-       selection_mapover (sigc::mem_fun (*_editor, &Editor::remove_a_region));
-}
-
 void
 EditorRegions::drag_data_received (const RefPtr<Gdk::DragContext>& context,
                                   int x, int y,
@@ -1159,6 +1096,8 @@ EditorRegions::drag_data_received (const RefPtr<Gdk::DragContext>& context,
        vector<ustring> paths;
 
        if (data.get_target() == "GTK_TREE_MODEL_ROW") {
+               /* something is being dragged over the region list */
+               _editor->_drags->abort ();
                _display.on_drag_data_received (context, x, y, data, info, time);
                return;
        }
@@ -1217,12 +1156,18 @@ EditorRegions::name_edit (const Glib::ustring& path, const Glib::ustring& new_te
 
 }
 
+/** @return Region that has been dragged out of the list, or 0 */
 boost::shared_ptr<Region>
 EditorRegions::get_dragged_region ()
 {
        list<boost::shared_ptr<Region> > regions;
        TreeView* source;
        _display.get_object_drag_data (regions, &source);
+
+       if (regions.empty()) {
+               return boost::shared_ptr<Region> ();
+       }
+       
        assert (regions.size() == 1);
        return regions.front ();
 }