x-fade uses cairo-generated icons, remove XPM
[ardour.git] / gtk2_ardour / editor_ops.cc
index 7ac2e0839025c6e6afbc6434c3608e77f40dc44b..c01a8c5fe9735d969e744fc9c80b1c089f5869eb 100644 (file)
 #include "ardour/legatize.h"
 #include "ardour/region_factory.h"
 #include "ardour/reverse.h"
+#include "ardour/selection.h"
 #include "ardour/session.h"
 #include "ardour/session_playlists.h"
 #include "ardour/strip_silence.h"
 #include "ardour/transient_detector.h"
+#include "ardour/transport_master_manager.h"
 #include "ardour/transpose.h"
 #include "ardour/vca_manager.h"
 
 #include "canvas/canvas.h"
 
 #include "actions.h"
+#include "ardour_ui.h"
 #include "audio_region_view.h"
 #include "audio_streamview.h"
 #include "audio_time_axis.h"
 #include "transpose_dialog.h"
 #include "transform_dialog.h"
 #include "ui_config.h"
+#include "utils.h"
 #include "vca_time_axis.h"
 
 #include "pbd/i18n.h"
@@ -177,9 +181,6 @@ Editor::split_regions_at (MusicSample where, RegionSelection& regions)
 {
        bool frozen = false;
 
-       RegionSelection pre_selected_regions = selection->regions;
-       bool working_on_selection = !pre_selected_regions.empty();
-
        list<boost::shared_ptr<Playlist> > used_playlists;
        list<RouteTimeAxisView*> used_trackviews;
 
@@ -267,22 +268,23 @@ Editor::split_regions_at (MusicSample where, RegionSelection& regions)
                EditorThaw(); /* Emit Signal */
        }
 
-       if (working_on_selection) {
-               // IFF we were working on selected regions, try to reinstate the other region selections that existed before the freeze/thaw.
-
-               RegionSelectionAfterSplit rsas = Config->get_region_selection_after_split();
-               /* There are three classes of regions that we might want selected after
-                  splitting selected regions:
-                   - regions selected before the split operation, and unaffected by it
-                   - newly-created regions before the split
-                   - newly-created regions after the split
-                */
-
-               if (rsas & Existing) {
-                       // region selections that existed before the split.
-                       selection->add (pre_selected_regions);
-               }
+       RegionSelectionAfterSplit rsas = Config->get_region_selection_after_split();
 
+       //if the user has "Clear Selection" as their post-split behavior, then clear the selection
+       if (!latest_regionviews.empty() && (rsas == None)) {
+               selection->clear_objects();
+               selection->clear_time();
+               //but leave track selection intact
+       }
+       
+       //if the user doesn't want to preserve the "Existing" selection, then clear the selection
+       if (!(rsas & Existing)) {
+               selection->clear_objects();
+               selection->clear_time();
+       }
+       
+       //if the user wants newly-created regions to be selected, then select them:
+       if (mouse_mode == MouseObject) {
                for (RegionSelection::iterator ri = latest_regionviews.begin(); ri != latest_regionviews.end(); ri++) {
                        if ((*ri)->region()->position() < where.sample) {
                                // new regions created before the split
@@ -439,7 +441,7 @@ Editor::nudge_forward (bool next, bool force_playhead)
                                                loc->set_end (max_samplepos, false, true, divisions);
                                        }
                                        if (loc->is_session_range()) {
-                                               _session->set_end_is_free (false);
+                                               _session->set_session_range_is_free (false);
                                        }
                                }
                                if (!in_command) {
@@ -533,7 +535,7 @@ Editor::nudge_backward (bool next, bool force_playhead)
                                                loc->set_end (loc->length(), false, true, get_grid_music_divisions(0));
                                        }
                                        if (loc->is_session_range()) {
-                                               _session->set_end_is_free (false);
+                                               _session->set_session_range_is_free (false);
                                        }
                                }
                                if (!in_command) {
@@ -729,6 +731,7 @@ Editor::build_region_boundary_cache ()
 
        /* if no snap selections are set, boundary cache should be left empty */
        if ( interesting_points.empty() ) {
+               _region_boundary_cache_dirty = false;
                return;
        }
 
@@ -748,6 +751,11 @@ Editor::build_region_boundary_cache ()
                }
        }
 
+       //allow regions to snap to the video start (if any) as if it were a "region"
+       if (ARDOUR_UI::instance()->video_timeline) {
+               region_boundary_cache.push_back (ARDOUR_UI::instance()->video_timeline->get_video_start_offset());
+       }
+
        std::pair<samplepos_t, samplepos_t> ext = session_gui_extents (false);
        samplepos_t session_end = ext.second;
 
@@ -2143,7 +2151,7 @@ Editor::temporal_zoom_to_sample (bool coarser, samplepos_t sample)
 
 
 bool
-Editor::choose_new_marker_name(string &name) {
+Editor::choose_new_marker_name(string &name, bool is_range) {
 
        if (!UIConfiguration::instance().get_name_new_markers()) {
                /* don't prompt user for a new name */
@@ -2154,7 +2162,11 @@ Editor::choose_new_marker_name(string &name) {
 
        dialog.set_prompt (_("New Name:"));
 
-       dialog.set_title (_("New Location Marker"));
+       if (is_range) {
+               dialog.set_title(_("New Range"));
+       } else {
+               dialog.set_title (_("New Location Marker"));
+       }
 
        dialog.set_name ("MarkNameWindow");
        dialog.set_size_request (250, -1);
@@ -2195,6 +2207,9 @@ Editor::add_location_from_selection ()
        samplepos_t end = selection->time[clicked_selection].end;
 
        _session->locations()->next_available_name(rangename,"selection");
+       if (!choose_new_marker_name(rangename, true)) {
+               return;
+       }
        Location *location = new Location (*_session, start, end, rangename, Location::IsRangeMarker, get_grid_music_divisions(0));
 
        begin_reversible_command (_("add marker"));
@@ -2251,6 +2266,8 @@ Editor::set_session_start_from_playhead ()
 
                commit_reversible_command ();
        }
+
+       _session->set_session_range_is_free (false);
 }
 
 void
@@ -2276,7 +2293,7 @@ Editor::set_session_end_from_playhead ()
                commit_reversible_command ();
        }
 
-       _session->set_end_is_free (false);
+       _session->set_session_range_is_free (false);
 }
 
 
@@ -2587,7 +2604,7 @@ Editor::transition_to_rolling (bool fwd)
        }
 
        if (_session->config.get_external_sync()) {
-               switch (Config->get_sync_source()) {
+               switch (TransportMasterManager::instance().current()->type()) {
                case Engine:
                        break;
                default:
@@ -3193,7 +3210,17 @@ Editor::separate_regions_between (const TimeSelection& ts)
        }
 
        if (in_command) {
-//             selection->set (new_selection);
+
+               RangeSelectionAfterSplit rsas = Config->get_range_selection_after_split();
+
+               //if our config preference says to clear the selection, clear the Range selection
+               if (rsas == ClearSel) {
+                       selection->clear_time();
+                       //but leave track selection intact
+               } else if (rsas == ForceSel) {
+                       //note: forcing the regions to be selected *might* force a tool-change to Object here
+                       selection->set(new_selection);  
+               }
 
                commit_reversible_command ();
        }
@@ -6399,7 +6426,30 @@ Editor::split_region ()
        //if no range was selected, try to find some regions to split
        if (current_mouse_mode() == MouseObject || current_mouse_mode() == MouseRange ) {  //don't try this for Internal Edit, Stretch, Draw, etc.
 
-               RegionSelection rs = get_regions_from_selection_and_edit_point ();
+               RegionSelection rs;
+
+               //new behavior:  the Split action will prioritize the entered_regionview rather than selected regions.
+               //this fixes the unexpected case where you point at a region, but
+               //  * nothing happens OR
+               //  * some other region (maybe off-screen) is split.
+               if (_edit_point == EditAtMouse && entered_regionview) {
+                       rs.add (entered_regionview);
+               } else {
+                       rs = selection->regions;   //might be empty
+               }
+
+               if (rs.empty()) {
+                       TrackViewList tracks = selection->tracks;
+
+                       if (!tracks.empty()) {
+                               /* no region selected or entered, but some selected tracks:
+                                * act on all regions on the selected tracks at the edit point
+                                */
+                               samplepos_t const where = get_preferred_edit_position (Editing::EDIT_IGNORE_NONE, false, false);
+                               get_regions_at(rs, where, tracks);
+                       }
+               }
+
                const samplepos_t pos = get_preferred_edit_position();
                const int32_t division = get_grid_music_divisions (0);
                MusicSample where (pos, division);
@@ -6409,86 +6459,19 @@ Editor::split_region ()
                }
 
                split_regions_at (where, rs);
-
        }
 }
 
 void
 Editor::select_next_stripable (bool routes_only)
 {
-       if (selection->tracks.empty()) {
-               selection->set (track_views.front());
-               return;
-       }
-
-       TimeAxisView* current = selection->tracks.front();
-
-       bool valid;
-       do {
-               for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
-
-                       if (*i == current) {
-                               ++i;
-                               if (i != track_views.end()) {
-                                       current = (*i);
-                               } else {
-                                       current = (*(track_views.begin()));
-                                       //selection->set (*(track_views.begin()));
-                               }
-                               break;
-                       }
-               }
-
-               if (routes_only) {
-                       RouteUI* rui = dynamic_cast<RouteUI *>(current);
-                       valid = rui && rui->route()->active();
-               } else {
-                       valid = 0 != current->stripable ().get();
-               }
-
-       } while (current->hidden() || !valid);
-
-       selection->set (current);
-
-       ensure_time_axis_view_is_visible (*current, false);
+       _session->selection().select_next_stripable (false, routes_only);
 }
 
 void
 Editor::select_prev_stripable (bool routes_only)
 {
-       if (selection->tracks.empty()) {
-               selection->set (track_views.front());
-               return;
-       }
-
-       TimeAxisView* current = selection->tracks.front();
-
-       bool valid;
-       do {
-               for (TrackViewList::reverse_iterator i = track_views.rbegin(); i != track_views.rend(); ++i) {
-
-                       if (*i == current) {
-                               ++i;
-                               if (i != track_views.rend()) {
-                                       current = (*i);
-                               } else {
-                                       current = *(track_views.rbegin());
-                               }
-                               break;
-                       }
-               }
-               if (routes_only) {
-                       RouteUI* rui = dynamic_cast<RouteUI *>(current);
-                       valid = rui && rui->route()->active();
-               } else {
-                       valid = 0 != current->stripable ().get();
-               }
-
-       } while (current->hidden() || !valid);
-
-       selection->set (current);
-
-       ensure_time_axis_view_is_visible (*current, false);
+       _session->selection().select_prev_stripable (false, routes_only);
 }
 
 void
@@ -6619,7 +6602,7 @@ Editor::set_session_extents_from_selection ()
                commit_reversible_command ();
        }
 
-       _session->set_end_is_free (false);
+       _session->set_session_range_is_free (false);
 }
 
 void
@@ -7145,7 +7128,7 @@ Editor::snap_regions_to_grid ()
                (*r)->region()->clear_changes ();
 
                MusicSample start ((*r)->region()->first_sample (), 0);
-               snap_to (start, RoundNearest, SnapToGrid);
+               snap_to (start, RoundNearest, SnapToGrid_Unscaled, true);
                (*r)->region()->set_position (start.sample, start.division);
                _session->add_command(new StatefulDiffCommand ((*r)->region()));
        }
@@ -7350,12 +7333,28 @@ Editor::playhead_forward_to_grid ()
                return;
        }
 
-       MusicSample pos (playhead_cursor->current_sample (), 0);
+       MusicSample pos  (playhead_cursor->current_sample (), 0);
 
-       if (pos.sample < max_samplepos - 1) {
-               pos.sample += 2;
-               snap_to_internal (pos, RoundUpAlways, SnapToGrid, false, true);
-               _session->request_locate (pos.sample);
+       if ( _grid_type == GridTypeNone) {
+               if (pos.sample < max_samplepos - current_page_samples()*0.1) {
+                       pos.sample += current_page_samples()*0.1;
+                       _session->request_locate (pos.sample);
+               } else {
+                       _session->request_locate (0);
+               }
+       } else {
+
+               if (pos.sample < max_samplepos - 1) {
+                       pos.sample += 2;
+                       pos = snap_to_grid (pos, RoundUpAlways, SnapToGrid_Scaled);
+                       _session->request_locate (pos.sample);
+               }
+       }
+
+
+       /* keep PH visible in window */
+       if (pos.sample > (_leftmost_sample + current_page_samples() *0.9)) {
+               reset_x_origin (pos.sample - (current_page_samples()*0.9));
        }
 }
 
@@ -7369,10 +7368,34 @@ Editor::playhead_backward_to_grid ()
 
        MusicSample pos  (playhead_cursor->current_sample (), 0);
 
-       if (pos.sample > 2) {
-               pos.sample -= 2;
-               snap_to_internal (pos, RoundDownAlways, SnapToGrid, false, true);
-               _session->request_locate (pos.sample);
+       if ( _grid_type == GridTypeNone) {
+               if ( pos.sample > current_page_samples()*0.1 ) {
+                       pos.sample -= current_page_samples()*0.1;
+                       _session->request_locate (pos.sample);
+               } else {
+                       _session->request_locate (0);
+               }
+       } else {
+
+               if (pos.sample > 2) {
+                       pos.sample -= 2;
+                       pos = snap_to_grid (pos, RoundDownAlways, SnapToGrid_Scaled);
+               }
+
+               //handle the case where we are rolling, and we're less than one-half second past the mark, we want to go to the prior mark...
+               //also see:  jump_backward_to_mark
+               if (_session->transport_rolling()) {
+                       if ((playhead_cursor->current_sample() - pos.sample) < _session->sample_rate()/2) {
+                               pos = snap_to_grid (pos, RoundDownAlways, SnapToGrid_Scaled);
+                       }
+               }
+
+               _session->request_locate (pos.sample, _session->transport_rolling());
+       }
+
+       /* keep PH visible in window */
+       if (pos.sample < (_leftmost_sample + current_page_samples() *0.1)) {
+               reset_x_origin (pos.sample - (current_page_samples()*0.1));
        }
 }
 
@@ -7437,6 +7460,10 @@ Editor::_remove_tracks ()
                return;
        }
 
+       if (!ARDOUR_UI_UTILS::engine_is_running ()) {
+               return;
+       }
+
        vector<string> choices;
        string prompt;
        int ntracks = 0;