Merge branch 'master' into cairocanvas
[ardour.git] / gtk2_ardour / editor_ops.cc
index d6cae9a8e29f85eb5087e6a9bf09ff103bb87248..c4f084170abd1f757dbbba3650fb18a82ebfaa23 100644 (file)
 #include "ardour/dB.h"
 #include "ardour/location.h"
 #include "ardour/midi_region.h"
+#include "ardour/midi_track.h"
 #include "ardour/operations.h"
 #include "ardour/playlist_factory.h"
 #include "ardour/quantize.h"
 #include "ardour/region_factory.h"
 #include "ardour/reverse.h"
-#include "ardour/route_group.h"
 #include "ardour/session.h"
 #include "ardour/session_playlists.h"
 #include "ardour/strip_silence.h"
 #include "ardour/transient_detector.h"
-#include "ardour/utils.h"
+
+#include "canvas/canvas.h"
 
 #include "ardour_ui.h"
 #include "debug.h"
@@ -358,8 +359,8 @@ Editor::nudge_forward (bool next, bool force_playhead)
                commit_reversible_command ();
 
        } else {
-               distance = get_nudge_distance (playhead_cursor->current_frame, next_distance);
-               _session->request_locate (playhead_cursor->current_frame + distance);
+               distance = get_nudge_distance (playhead_cursor->current_frame (), next_distance);
+               _session->request_locate (playhead_cursor->current_frame () + distance);
        }
 }
 
@@ -447,10 +448,10 @@ Editor::nudge_backward (bool next, bool force_playhead)
 
        } else {
 
-               distance = get_nudge_distance (playhead_cursor->current_frame, next_distance);
+               distance = get_nudge_distance (playhead_cursor->current_frame (), next_distance);
 
-               if (playhead_cursor->current_frame > distance) {
-                       _session->request_locate (playhead_cursor->current_frame - distance);
+               if (playhead_cursor->current_frame () > distance) {
+                       _session->request_locate (playhead_cursor->current_frame () - distance);
                } else {
                        _session->goto_start();
                }
@@ -773,7 +774,7 @@ Editor::get_region_boundary (framepos_t pos, int32_t dir, bool with_selection, b
 void
 Editor::cursor_to_region_boundary (bool with_selection, int32_t dir)
 {
-       framepos_t pos = playhead_cursor->current_frame;
+       framepos_t pos = playhead_cursor->current_frame ();
        framepos_t target;
 
        if (!_session) {
@@ -808,7 +809,7 @@ void
 Editor::cursor_to_region_point (EditorCursor* cursor, RegionPoint point, int32_t dir)
 {
        boost::shared_ptr<Region> r;
-       framepos_t pos = cursor->current_frame;
+       framepos_t pos = cursor->current_frame ();
 
        if (!_session) {
                return;
@@ -1144,7 +1145,7 @@ Editor::selected_marker_to_selection_end ()
 void
 Editor::scroll_playhead (bool forward)
 {
-       framepos_t pos = playhead_cursor->current_frame;
+       framepos_t pos = playhead_cursor->current_frame ();
        framecnt_t delta = (framecnt_t) floor (current_page_frames() / 0.8);
 
        if (forward) {
@@ -1198,10 +1199,10 @@ Editor::cursor_align (bool playhead_to_edit)
                        Location* loc = find_location_from_marker (*i, ignored);
 
                        if (loc->is_mark()) {
-                               loc->set_start (playhead_cursor->current_frame);
+                               loc->set_start (playhead_cursor->current_frame ());
                        } else {
-                               loc->set (playhead_cursor->current_frame,
-                                         playhead_cursor->current_frame + loc->length());
+                               loc->set (playhead_cursor->current_frame (),
+                                         playhead_cursor->current_frame () + loc->length());
                        }
                }
        }
@@ -1210,7 +1211,7 @@ Editor::cursor_align (bool playhead_to_edit)
 void
 Editor::scroll_backward (float pages)
 {
-       framepos_t const one_page = (framepos_t) rint (_canvas_width * frames_per_unit);
+       framepos_t const one_page = (framepos_t) rint (_visible_canvas_width * frames_per_pixel);
        framepos_t const cnt = (framepos_t) floor (pages * one_page);
 
        framepos_t frame;
@@ -1226,7 +1227,7 @@ Editor::scroll_backward (float pages)
 void
 Editor::scroll_forward (float pages)
 {
-       framepos_t const one_page = (framepos_t) rint (_canvas_width * frames_per_unit);
+       framepos_t const one_page = (framepos_t) rint (_visible_canvas_width * frames_per_pixel);
        framepos_t const cnt = (framepos_t) floor (pages * one_page);
 
        framepos_t frame;
@@ -1243,8 +1244,8 @@ void
 Editor::scroll_tracks_down ()
 {
        double vert_value = vertical_adjustment.get_value() + vertical_adjustment.get_page_size();
-       if (vert_value > vertical_adjustment.get_upper() - _canvas_height) {
-               vert_value = vertical_adjustment.get_upper() - _canvas_height;
+       if (vert_value > vertical_adjustment.get_upper() - _visible_canvas_height) {
+               vert_value = vertical_adjustment.get_upper() - _visible_canvas_height;
        }
 
        vertical_adjustment.set_value (vert_value);
@@ -1261,8 +1262,8 @@ Editor::scroll_tracks_down_line ()
 {
        double vert_value = vertical_adjustment.get_value() + 60;
 
-       if (vert_value > vertical_adjustment.get_upper() - _canvas_height) {
-               vert_value = vertical_adjustment.get_upper() - _canvas_height;
+       if (vert_value > vertical_adjustment.get_upper() - _visible_canvas_height) {
+               vert_value = vertical_adjustment.get_upper() - _visible_canvas_height;
        }
 
        vertical_adjustment.set_value (vert_value);
@@ -1329,28 +1330,46 @@ Editor::tav_zoom_smooth (bool coarser, bool force_all)
        _routes->resume_redisplay ();
 }
 
+bool
+Editor::clamp_frames_per_pixel (double& fpp) const
+{
+       bool clamped = false;
+       
+       if (fpp < 2.0) {
+               fpp = 2.0;
+               clamped = true;
+       }
+
+       if (max_framepos / fpp < 800) {
+               fpp = max_framepos / 800.0;
+               clamped = true;
+       }
+
+       return clamped;
+}
+
 void
 Editor::temporal_zoom_step (bool coarser)
 {
        ENSURE_GUI_THREAD (*this, &Editor::temporal_zoom_step, coarser)
 
-       double nfpu;
-
-       nfpu = frames_per_unit;
+       double nfpp = frames_per_pixel;
 
        if (coarser) {
-               nfpu *= 1.61803399;
+               nfpp = min (9e6, nfpp * 1.61803399);
        } else {
-               nfpu = max(1.0,(nfpu/1.61803399));
+               nfpp = max (1.0, nfpp / 1.61803399);
        }
 
-       temporal_zoom (nfpu);
+       temporal_zoom (nfpp);
 }
 
 void
-Editor::temporal_zoom (gdouble fpu)
+Editor::temporal_zoom (double fpp)
 {
-       if (!_session) return;
+       if (!_session) {
+               return;
+       }
 
        framepos_t current_page = current_page_frames();
        framepos_t current_leftmost = leftmost_frame;
@@ -1361,18 +1380,24 @@ Editor::temporal_zoom (gdouble fpu)
        framepos_t leftmost_after_zoom = 0;
        framepos_t where;
        bool in_track_canvas;
-       double nfpu;
+       double nfpp;
        double l;
 
-       /* XXX this limit is also in ::set_frames_per_unit() */
-
-       if (frames_per_unit <= 1.0 && fpu <= frames_per_unit) {
+       clamp_frames_per_pixel (fpp);
+       if (fpp == frames_per_pixel) {
                return;
        }
 
-       nfpu = fpu;
+       nfpp = fpp;
+       
+       // Imposing an arbitrary limit to zoom out as too much zoom out produces 
+       // segfaults for lack of memory. If somebody decides this is not high enough I
+       // believe it can be raisen to higher values but some limit must be in place.
+       if (nfpp > 8e+08) {
+               nfpp = 8e+08;
+       }
 
-       new_page_size = (framepos_t) floor (_canvas_width * nfpu);
+       new_page_size = (framepos_t) floor (_visible_canvas_width * nfpp);
        half_page_size = new_page_size / 2;
 
        switch (zoom_focus) {
@@ -1400,7 +1425,7 @@ Editor::temporal_zoom (gdouble fpu)
 
        case ZoomFocusPlayhead:
                /* centre playhead */
-               l = playhead_cursor->current_frame - (new_page_size * 0.5);
+               l = playhead_cursor->current_frame () - (new_page_size * 0.5);
 
                if (l < 0) {
                        leftmost_after_zoom = 0;
@@ -1416,7 +1441,7 @@ Editor::temporal_zoom (gdouble fpu)
 
                if (!mouse_frame (where, in_track_canvas)) {
                        /* use playhead instead */
-                       where = playhead_cursor->current_frame;
+                       where = playhead_cursor->current_frame ();
 
                        if (where < half_page_size) {
                                leftmost_after_zoom = 0;
@@ -1465,7 +1490,7 @@ Editor::temporal_zoom (gdouble fpu)
 
        // leftmost_after_zoom = min (leftmost_after_zoom, _session->current_end_frame());
 
-       reposition_and_zoom (leftmost_after_zoom, nfpu);
+       reposition_and_zoom (leftmost_after_zoom, nfpp);
 }
 
 void
@@ -1510,8 +1535,8 @@ Editor::temporal_zoom_region (bool both_axes)
        }
 
        framepos_t range = end - start;
-       double new_fpu = (double)range / (double)_canvas_width;
-       framepos_t extra_samples = (framepos_t) floor (one_centimeter_in_pixels * new_fpu);
+       double new_fpp = (double) range / (double) _visible_canvas_width;
+       framepos_t extra_samples = (framepos_t) floor (one_centimeter_in_pixels * new_fpp);
 
        if (start > extra_samples) {
                start -= extra_samples;
@@ -1535,7 +1560,7 @@ Editor::temporal_zoom_region (bool both_axes)
        temporal_zoom_by_frame (start, end);
        
        if (both_axes) {
-               uint32_t per_track_height = (uint32_t) floor ((_canvas_height - canvas_timebars_vsize - 10.0) / tracks.size());
+               uint32_t per_track_height = (uint32_t) floor ((_visible_canvas_height - 10.0) / tracks.size());
 
                /* set visible track heights appropriately */
 
@@ -1609,11 +1634,11 @@ Editor::temporal_zoom_by_frame (framepos_t start, framepos_t end)
 
        framepos_t range = end - start;
 
-       double new_fpu = (double)range / (double)_canvas_width;
+       double const new_fpp = (double) range / (double) _visible_canvas_width;
 
-       framepos_t new_page = (framepos_t) floor (_canvas_width * new_fpu);
-       framepos_t middle = (framepos_t) floor( (double)start + ((double)range / 2.0f ));
-       framepos_t new_leftmost = (framepos_t) floor( (double)middle - ((double)new_page/2.0f));
+       framepos_t new_page = (framepos_t) floor (_visible_canvas_width * new_fpp);
+       framepos_t middle = (framepos_t) floor ((double) start + ((double) range / 2.0f));
+       framepos_t new_leftmost = (framepos_t) floor ((double) middle - ((double) new_page / 2.0f));
 
        if (new_leftmost > middle) {
                new_leftmost = 0;
@@ -1623,7 +1648,7 @@ Editor::temporal_zoom_by_frame (framepos_t start, framepos_t end)
                new_leftmost = 0;
        }
 
-       reposition_and_zoom (new_leftmost, new_fpu);
+       reposition_and_zoom (new_leftmost, new_fpp);
 }
 
 void
@@ -1633,19 +1658,19 @@ Editor::temporal_zoom_to_frame (bool coarser, framepos_t frame)
                return;
        }
        double range_before = frame - leftmost_frame;
-       double new_fpu;
+       double new_fpp;
 
-       new_fpu = frames_per_unit;
+       new_fpp = frames_per_pixel;
 
        if (coarser) {
-               new_fpu *= 1.61803399;
+               new_fpp *= 1.61803399;
                range_before *= 1.61803399;
        } else {
-               new_fpu = max(1.0,(new_fpu/1.61803399));
+               new_fpp = max(1.0,(new_fpp/1.61803399));
                range_before /= 1.61803399;
        }
 
-       if (new_fpu == frames_per_unit)  {
+       if (new_fpp == frames_per_pixel)  {
                return;
        }
 
@@ -1659,7 +1684,7 @@ Editor::temporal_zoom_to_frame (bool coarser, framepos_t frame)
                new_leftmost = 0;
        }
 
-       reposition_and_zoom (new_leftmost, new_fpu);
+       reposition_and_zoom (new_leftmost, new_fpp);
 }
 
 
@@ -1824,13 +1849,13 @@ Editor::jump_forward_to_mark ()
                return;
        }
 
-       Location *location = _session->locations()->first_location_after (playhead_cursor->current_frame);
+       framepos_t pos = _session->locations()->first_mark_after (playhead_cursor->current_frame());
 
-       if (location) {
-               _session->request_locate (location->start(), _session->transport_rolling());
-       } else {
-               _session->request_locate (_session->current_end_frame());
+       if (pos < 0) {
+               return;
        }
+       
+       _session->request_locate (pos, _session->transport_rolling());
 }
 
 void
@@ -1840,13 +1865,13 @@ Editor::jump_backward_to_mark ()
                return;
        }
 
-       Location *location = _session->locations()->first_location_before (playhead_cursor->current_frame);
+       framepos_t pos = _session->locations()->first_mark_before (playhead_cursor->current_frame());
 
-       if (location) {
-               _session->request_locate (location->start(), _session->transport_rolling());
-       } else {
-               _session->goto_start ();
+       if (pos < 0) {
+               return;
        }
+
+       _session->request_locate (pos, _session->transport_rolling());
 }
 
 void
@@ -1945,7 +1970,7 @@ Editor::insert_region_list_drag (boost::shared_ptr<Region> region, int x, int y)
        RouteTimeAxisView *rtv = 0;
        boost::shared_ptr<Playlist> playlist;
 
-       track_canvas->window_to_world (x, y, wx, wy);
+       _track_canvas_viewport->window_to_canvas (x, y, wx, wy);
 
        GdkEvent event;
        event.type = GDK_BUTTON_RELEASE;
@@ -1989,9 +2014,7 @@ Editor::insert_route_list_drag (boost::shared_ptr<Route> route, int x, int y)
        RouteTimeAxisView *dest_rtv = 0;
        RouteTimeAxisView *source_rtv = 0;
 
-       track_canvas->window_to_world (x, y, wx, wy);
-       wx += horizontal_position ();
-       wy += vertical_adjustment.get_value();
+       _track_canvas_viewport->window_to_canvas (x, y, wx, wy);
 
        GdkEvent event;
        event.type = GDK_BUTTON_RELEASE;
@@ -2084,7 +2107,7 @@ Editor::transition_to_rolling (bool fwd)
        }
 
        if (_session->config.get_external_sync()) {
-               switch (_session->config.get_sync_source()) {
+               switch (Config->get_sync_source()) {
                case JACK:
                        break;
                default:
@@ -2147,6 +2170,55 @@ Editor::play_selection ()
        _session->request_play_range (&selection->time, true);
 }
 
+framepos_t
+Editor::get_preroll ()
+{
+       return 1.0 /*Config->get_edit_preroll_seconds()*/ * _session->frame_rate();
+}
+
+
+void
+Editor::maybe_locate_with_edit_preroll ( framepos_t location )
+{
+       if ( _session->transport_rolling() || !Config->get_always_play_range() )
+               return;
+
+       location -= get_preroll();
+       
+       //don't try to locate before the beginning of time
+       if ( location < 0 ) 
+               location = 0;
+               
+       //if follow_playhead is on, keep the playhead on the screen
+       if ( _follow_playhead )
+               if ( location < leftmost_frame ) 
+                       location = leftmost_frame;
+
+       _session->request_locate( location );
+}
+
+void
+Editor::play_with_preroll ()
+{
+       if (selection->time.empty()) {
+               return;
+       } else {
+               framepos_t preroll = get_preroll();
+               
+               framepos_t start = 0;
+               if (selection->time[clicked_selection].start > preroll)
+                       start = selection->time[clicked_selection].start - preroll;
+               
+               framepos_t end = selection->time[clicked_selection].end + preroll;
+               
+               AudioRange ar (start, end, 0);
+               list<AudioRange> lar;
+               lar.push_back (ar);
+
+               _session->request_play_range (&lar, true);
+       }
+}
+
 void
 Editor::play_location (Location& location)
 {
@@ -2654,7 +2726,7 @@ Editor::separate_region_from_selection ()
           returns a single range.
        */
 
-       if (mouse_mode == MouseRange && !selection->time.empty()) {
+       if (!selection->time.empty()) {
 
                separate_regions_between (selection->time);
 
@@ -3215,8 +3287,10 @@ Editor::trim_region (bool front)
 
                        if (front) {
                                (*i)->region()->trim_front (where);
+                               maybe_locate_with_edit_preroll ( where );
                        } else {
                                (*i)->region()->trim_end (where);
+                               maybe_locate_with_edit_preroll ( where );
                        }
 
                        _session->add_command (new StatefulDiffCommand ((*i)->region()));
@@ -3388,7 +3462,6 @@ Editor::freeze_thread ()
        /* create event pool because we may need to talk to the session */
        SessionEvent::create_per_thread_pool ("freeze events", 64);
        /* create per-thread buffers for process() tree to use */
-       current_interthread_info->process_thread.init ();
        current_interthread_info->process_thread.get_buffers ();
        clicked_routeview->audio_track()->freeze_me (*current_interthread_info);
        current_interthread_info->done = true;
@@ -3479,7 +3552,7 @@ Editor::bounce_range_selection (bool replace, bool enable_processing)
                        if (rtv && rtv->track() && replace && enable_processing && !rtv->track()->bounceable (rtv->track()->main_outs(), false)) {
                                MessageDialog d (
                                        _("You can't perform this operation because the processing of the signal "
-                                         "will cause one or more of the tracks will end up with a region with more channels than this track has inputs.\n\n"
+                                         "will cause one or more of the tracks to end up with a region with more channels than this track has inputs.\n\n"
                                          "You can do this without processing, which is a different operation.")
                                        );
                                d.set_title (_("Cannot bounce"));
@@ -3569,7 +3642,7 @@ Editor::copy ()
 bool
 Editor::can_cut_copy () const
 {
-       switch (current_mouse_mode()) {
+       switch (effective_mouse_mode()) {
 
        case MouseObject:
                if (!selection->regions.empty() || !selection->points.empty()) {
@@ -3628,7 +3701,8 @@ Editor::cut_copy (CutCopyOp op)
                }
        }
 
-       cut_buffer->clear ();
+       if ( op != Clear )  //"Delete" doesn't change copy/paste buf
+               cut_buffer->clear ();
 
        if (entered_marker) {
 
@@ -3647,7 +3721,7 @@ Editor::cut_copy (CutCopyOp op)
 
        if (internal_editing()) {
 
-               switch (current_mouse_mode()) {
+               switch (effective_mouse_mode()) {
                case MouseObject:
                case MouseRange:
                        cut_copy_midi (op);
@@ -3658,19 +3732,62 @@ Editor::cut_copy (CutCopyOp op)
 
        } else {
 
-               RegionSelection rs;
+       RegionSelection rs; 
 
-               /* we only want to cut regions if some are selected */
+       /* we only want to cut regions if some are selected */
 
-               if (doing_object_stuff()) {
-                       rs = get_regions_from_selection ();
-                       if (!rs.empty() || !selection->points.empty()) {
+       if (!selection->regions.empty()) {
+               rs = selection->regions;
+       }
 
+       switch (effective_mouse_mode()) {
+/*
+ *             case MouseGain: {
+                       //find regions's gain line
+                       AudioRegionView *rview = dynamic_cast<AudioRegionView*>(clicked_regionview);
+                               AutomationTimeAxisView *tview = dynamic_cast<AutomationTimeAxisView*>(clicked_trackview);
+                       if (rview) {
+                               AudioRegionGainLine *line = rview->get_gain_line();
+                               if (!line) break;
+                               
+                               //cut region gain points in the selection
+                               AutomationList& alist (line->the_list());
+                               XMLNode &before = alist.get_state();
+                               AutomationList* what_we_got = 0;
+                               if ((what_we_got = alist.cut (selection->time.front().start - rview->audio_region()->position(), selection->time.front().end - rview->audio_region()->position())) != 0) {
+                                       session->add_command(new MementoCommand<AutomationList>(alist, &before, &alist.get_state()));
+                                       delete what_we_got;
+                                       what_we_got = 0;
+                               }
+                               
+                               rview->set_envelope_visible(true);
+                               rview->audio_region()->set_envelope_active(true);
+                               
+                       } else if (tview) {
+                               AutomationLine *line = *(tview->lines.begin());
+                               if (!line) break;
+                               
+                               //cut auto points in the selection
+                               AutomationList& alist (line->the_list());
+                               XMLNode &before = alist.get_state();
+                               AutomationList* what_we_got = 0;
+                               if ((what_we_got = alist.cut (selection->time.front().start, selection->time.front().end)) != 0) {
+                                       session->add_command(new MementoCommand<AutomationList>(alist, &before, &alist.get_state()));
+                                       delete what_we_got;
+                                       what_we_got = 0;
+                               }               
+                       } else
+                               break;
+               } break;
+*/                     
+               case MouseObject: 
+               case MouseRange:
+                       if (!rs.empty() || !selection->points.empty()) {
                                begin_reversible_command (opname + _(" objects"));
 
                                if (!rs.empty()) {
                                        cut_copy_regions (op, rs);
-
+                                       
                                        if (op == Cut || op == Delete) {
                                                selection->clear_regions ();
                                        }
@@ -3683,16 +3800,11 @@ Editor::cut_copy (CutCopyOp op)
                                                selection->clear_points ();
                                        }
                                }
-                               commit_reversible_command ();
-                               goto out;
-                       }
-                       if (!selection->time.empty() && (_join_object_range_state == JOIN_OBJECT_RANGE_NONE)) {
-                               /* don't cause suprises */
-                               goto out;
-                       }
-               }
 
-               if (doing_range_stuff()) {
+                               commit_reversible_command ();   
+                               break;
+                       } 
+                       
                        if (selection->time.empty()) {
                                framepos_t start, end;
                                if (!get_edit_op_range (start, end)) {
@@ -3700,18 +3812,22 @@ Editor::cut_copy (CutCopyOp op)
                                }
                                selection->set (start, end);
                        }
-
+                               
                        begin_reversible_command (opname + _(" range"));
                        cut_copy_ranges (op);
                        commit_reversible_command ();
-
+                       
                        if (op == Cut || op == Delete) {
                                selection->clear_time ();
                        }
+
+                       break;
+                       
+               default:
+                       break;
                }
        }
 
-  out:
        if (op == Delete || op == Cut || op == Clear) {
                _drags->abort ();
        }
@@ -4166,12 +4282,15 @@ Editor::paste_internal (framepos_t position, float times)
 
        /* get everything in the correct order */
 
-       if (!selection->tracks.empty()) {
-               /* there are some selected tracks, so paste to them */
+       if (_edit_point == Editing::EditAtMouse && entered_track) {
+               /* With the mouse edit point, paste onto the track under the mouse */
+               ts.push_back (entered_track);
+       } else if (!selection->tracks.empty()) {
+               /* Otherwise, if there are some selected tracks, paste to them */
                ts = selection->tracks.filter_to_unique_playlists ();
                sort_track_selection (ts);
        } else if (_last_cut_copy_source_track) {
-               /* otherwise paste to the track that the cut/copy came from;
+               /* Otherwise paste to the track that the cut/copy came from;
                   see discussion in mantis #3333.
                */
                ts.push_back (_last_cut_copy_source_track);
@@ -4306,14 +4425,14 @@ Editor::reset_point_selection ()
 void
 Editor::center_playhead ()
 {
-       float page = _canvas_width * frames_per_unit;
-       center_screen_internal (playhead_cursor->current_frame, page);
+       float const page = _visible_canvas_width * frames_per_pixel;
+       center_screen_internal (playhead_cursor->current_frame (), page);
 }
 
 void
 Editor::center_edit_point ()
 {
-       float page = _canvas_width * frames_per_unit;
+       float const page = _visible_canvas_width * frames_per_pixel;
        center_screen_internal (get_preferred_edit_position(), page);
 }
 
@@ -4722,8 +4841,14 @@ Editor::insert_patch_change (bool from_context)
 
        const framepos_t p = get_preferred_edit_position (false, from_context);
 
+       /* XXX: bit of a hack; use the MIDNAM from the first selected region;
+          there may be more than one, but the PatchChangeDialog can only offer
+          one set of patch menus.
+       */
+       MidiRegionView* first = dynamic_cast<MidiRegionView*> (rs.front ());
+
        Evoral::PatchChange<Evoral::MusicalTime> empty (0, 0, 0, 0);
-       PatchChangeDialog d (0, _session, empty, Gtk::Stock::ADD);
+        PatchChangeDialog d (0, _session, empty, first->instrument_info(), Gtk::Stock::ADD);
 
        if (d.run() == RESPONSE_CANCEL) {
                return;
@@ -4853,16 +4978,16 @@ Editor::reset_region_gain_envelopes ()
 }
 
 void
-Editor::set_region_gain_visibility (RegionView* rv, bool yn)
+Editor::set_region_gain_visibility (RegionView* rv)
 {
        AudioRegionView* arv = dynamic_cast<AudioRegionView*> (rv);
        if (arv) {
-               arv->set_envelope_visible (yn);
+               arv->update_envelope_visibility();
        }
 }
 
 void
-Editor::set_gain_envelope_visibility (bool yn)
+Editor::set_gain_envelope_visibility ()
 {
        if (!_session) {
                return;
@@ -4871,7 +4996,7 @@ Editor::set_gain_envelope_visibility (bool yn)
        for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
                AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
                if (v) {
-                       v->audio_view()->foreach_regionview (sigc::bind (sigc::mem_fun (this, &Editor::set_region_gain_visibility), yn));
+                       v->audio_view()->foreach_regionview (sigc::mem_fun (this, &Editor::set_region_gain_visibility));
                }
        }
 }
@@ -4927,6 +5052,30 @@ Editor::toggle_region_lock ()
        _session->commit_reversible_command ();
 }
 
+void
+Editor::toggle_region_video_lock ()
+{
+       if (_ignore_region_action) {
+               return;
+       }
+
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+
+       if (!_session || rs.empty()) {
+               return;
+       }
+
+       _session->begin_reversible_command (_("Toggle Video Lock"));
+
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               (*i)->region()->clear_changes ();
+               (*i)->region()->set_video_locked (!(*i)->region()->video_locked());
+               _session->add_command (new StatefulDiffCommand ((*i)->region()));
+       }
+
+       _session->commit_reversible_command ();
+}
+
 void
 Editor::toggle_region_lock_style ()
 {
@@ -5354,14 +5503,15 @@ Editor::set_playhead_cursor ()
                        _session->request_locate (where, _session->transport_rolling());
                }
        }
+
+       if ( Config->get_always_play_range() )
+               cancel_time_selection();
 }
 
 void
 Editor::split_region ()
 {
-       if (((mouse_mode == MouseRange) ||
-            (mouse_mode != MouseObject && _join_object_range_state == JOIN_OBJECT_RANGE_RANGE)) &&
-           !selection->time.empty()) {
+       if ( !selection->time.empty()) {
                separate_regions_between (selection->time);
                return;
        }
@@ -5377,28 +5527,9 @@ Editor::split_region ()
        split_regions_at (where, rs);
 }
 
-void
-Editor::ensure_entered_track_selected (bool op_really_wants_one_track_if_none_are_selected)
-{
-       if (entered_track && mouse_mode == MouseObject) {
-               if (!selection->tracks.empty()) {
-                       if (!selection->selected (entered_track)) {
-                               selection->add (entered_track);
-                       }
-               } else {
-                       /* there is no selection, but this operation requires/prefers selected objects */
-
-                       if (op_really_wants_one_track_if_none_are_selected) {
-                               selection->set (entered_track);
-                       }
-               }
-       }
-}
-
 struct EditorOrderRouteSorter {
     bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
-           /* use of ">" forces the correct sort order */
-           return a->order_key ("editor") < b->order_key ("editor");
+           return a->order_key (EditorSort) < b->order_key (EditorSort);
     }
 };
 
@@ -5472,7 +5603,7 @@ Editor::ensure_track_visible(TimeAxisView *track)
                return;
 
        double const current_view_min_y = vertical_adjustment.get_value();
-       double const current_view_max_y = vertical_adjustment.get_value() + vertical_adjustment.get_page_size() - canvas_timebars_vsize;
+       double const current_view_max_y = vertical_adjustment.get_value() + vertical_adjustment.get_page_size();
 
        double const track_min_y = track->y_position ();
        double const track_max_y = track->y_position () + track->effective_height ();
@@ -5489,7 +5620,7 @@ Editor::ensure_track_visible(TimeAxisView *track)
                new_value = track_min_y;
        } else {
                // Track is below the current view
-               new_value = track->y_position () + track->effective_height() + canvas_timebars_vsize - vertical_adjustment.get_page_size();
+               new_value = track->y_position () + track->effective_height() - vertical_adjustment.get_page_size();
        }
 
        vertical_adjustment.set_value(new_value);
@@ -5763,7 +5894,9 @@ Editor::define_one_bar (framepos_t start, framepos_t end)
        } else if (t.frame() == start) {
                _session->tempo_map().change_existing_tempo_at (start, beats_per_minute, t.note_type());
        } else {
-               _session->tempo_map().add_tempo (Tempo (beats_per_minute, t.note_type()), start);
+               Timecode::BBT_Time bbt;
+               _session->tempo_map().bbt_time (start, bbt);
+               _session->tempo_map().add_tempo (Tempo (beats_per_minute, t.note_type()), bbt);
        }
 
        XMLNode& after (_session->tempo_map().get_state());
@@ -5918,6 +6051,10 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
                plist.add (ARDOUR::Properties::layer, 0);
 
                boost::shared_ptr<Region> nr = RegionFactory::create (r->sources(), plist, false);
+               /* because we set annouce to false, manually add the new region to the
+                  RegionFactory map
+               */
+               RegionFactory::map_add (nr);
 
                pl->add_region (nr, r->position() + pos);
 
@@ -5942,6 +6079,10 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
        plist.add (ARDOUR::Properties::layer, 0);
 
        boost::shared_ptr<Region> nr = RegionFactory::create (r->sources(), plist, false);
+       /* because we set annouce to false, manually add the new region to the
+          RegionFactory map
+       */
+       RegionFactory::map_add (nr);
        pl->add_region (nr, r->position() + pos);
 
        if (select_new) {
@@ -6061,8 +6202,7 @@ Editor::close_region_gaps ()
        Table table (2, 3);
        table.set_spacings (12);
        table.set_border_width (12);
-       Label* l = manage (new Label (_("Crossfade length")));
-       l->set_alignment (0, 0.5);
+       Label* l = manage (left_aligned_label (_("Crossfade length")));
        table.attach (*l, 0, 1, 0, 1);
 
        SpinButton spin_crossfade (1, 0);
@@ -6073,8 +6213,7 @@ Editor::close_region_gaps ()
 
        table.attach (*manage (new Label (_("ms"))), 2, 3, 0, 1);
 
-       l = manage (new Label (_("Pull-back length")));
-       l->set_alignment (0, 0.5);
+       l = manage (left_aligned_label (_("Pull-back length")));
        table.attach (*l, 0, 1, 1, 2);
 
        SpinButton spin_pullback (1, 0);
@@ -6229,8 +6368,11 @@ Editor::tab_to_transient (bool forward)
 void
 Editor::playhead_forward_to_grid ()
 {
-       if (!_session) return;
-       framepos_t pos = playhead_cursor->current_frame;
+       if (!_session) {
+               return;
+       }
+       
+       framepos_t pos = playhead_cursor->current_frame ();
        if (pos < max_framepos - 1) {
                pos += 2;
                snap_to_internal (pos, 1, false);
@@ -6242,8 +6384,11 @@ Editor::playhead_forward_to_grid ()
 void
 Editor::playhead_backward_to_grid ()
 {
-       if (!_session) return;
-       framepos_t pos = playhead_cursor->current_frame;
+       if (!_session) {
+               return;
+       }
+       
+       framepos_t pos = playhead_cursor->current_frame ();
        if (pos > 2) {
                pos -= 2;
                snap_to_internal (pos, -1, false);
@@ -6594,7 +6739,7 @@ Editor::fit_tracks (TrackViewList & tracks)
                ++visible_tracks;
        }
 
-       uint32_t h = (uint32_t) floor ((_canvas_height - child_heights - canvas_timebars_vsize) / visible_tracks);
+       uint32_t h = (uint32_t) floor ((_visible_canvas_height - child_heights) / visible_tracks);
        double first_y_pos = DBL_MAX;
 
        if (h < TimeAxisView::preset_height (HeightSmall)) {
@@ -6656,7 +6801,7 @@ Editor::fit_tracks (TrackViewList & tracks)
           request signal handler will cause the vertical adjustment setting to fail
        */
 
-       controls_layout.property_height () = full_canvas_height - canvas_timebars_vsize;
+       controls_layout.property_height () = _full_canvas_height;
        vertical_adjustment.set_value (first_y_pos);
 
        redo_visual_stack.push_back (current_visual_state (true));
@@ -6809,3 +6954,26 @@ Editor::uncombine_regions ()
        commit_reversible_command ();
 }
 
+void
+Editor::toggle_midi_input_active (bool flip_others)
+{
+       bool onoff;
+       boost::shared_ptr<RouteList> rl (new RouteList);
+
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+               RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView *>(*i);
+
+               if (!rtav) {
+                       continue;
+               }
+
+               boost::shared_ptr<MidiTrack> mt = rtav->midi_track();
+
+               if (mt) {
+                       rl->push_back (rtav->route());
+                       onoff = !mt->input_active();
+               }
+       }
+       
+       _session->set_exclusive_input_active (rl, onoff, flip_others);
+}