first vaguely working version using PresentationInfo
[ardour.git] / gtk2_ardour / editor.cc
index 885f92645d61817ede4604712a910d85478e090b..3dbdd8a9925913cbfd3f09022ae688db71829bdc 100644 (file)
 #include "gtkmm2ext/bindings.h"
 #include "gtkmm2ext/grouped_buttons.h"
 #include "gtkmm2ext/gtk_ui.h"
-#include "gtkmm2ext/tearoff.h"
+#include <gtkmm2ext/keyboard.h>
 #include "gtkmm2ext/utils.h"
 #include "gtkmm2ext/window_title.h"
 #include "gtkmm2ext/choice.h"
 #include "gtkmm2ext/cell_renderer_pixbuf_toggle.h"
 
+#include "ardour/analysis_graph.h"
 #include "ardour/audio_track.h"
 #include "ardour/audioengine.h"
 #include "ardour/audioregion.h"
 #include "editor_routes.h"
 #include "editor_snapshots.h"
 #include "editor_summary.h"
+#include "export_report.h"
 #include "global_port_matrix.h"
 #include "gui_object.h"
 #include "gui_thread.h"
 #include "keyboard.h"
+#include "keyeditor.h"
+#include "luainstance.h"
 #include "marker.h"
 #include "midi_region_view.h"
 #include "midi_time_axis.h"
 #include "rgb_macros.h"
 #include "rhythm_ferret.h"
 #include "selection.h"
+#include "simple_progress_dialog.h"
 #include "sfdb_ui.h"
 #include "tempo_lines.h"
 #include "time_axis_view.h"
@@ -207,8 +212,8 @@ static const gchar *_zoom_focus_strings[] = {
        N_("Right"),
        N_("Center"),
        N_("Playhead"),
-       N_("Mouse"),
-       N_("Edit point"),
+       N_("Mouse"),
+       N_("Edit point"),
        0
 };
 
@@ -252,10 +257,84 @@ pane_size_watcher (Paned* pane)
 }
 
 Editor::Editor ()
-       : _join_object_range_state (JOIN_OBJECT_RANGE_NONE)
-
+       : PublicEditor (global_hpacker)
+       , editor_mixer_strip_width (Wide)
+       , constructed (false)
+       , _playlist_selector (0)
+       , no_save_visual (false)
+       , leftmost_frame (0)
+       , samples_per_pixel (2048)
+       , zoom_focus (ZoomFocusPlayhead)
+       , mouse_mode (MouseObject)
+       , pre_internal_snap_type (SnapToBeat)
+       , pre_internal_snap_mode (SnapOff)
+       , internal_snap_type (SnapToBeat)
+       , internal_snap_mode (SnapOff)
+       , _join_object_range_state (JOIN_OBJECT_RANGE_NONE)
+       , _notebook_shrunk (false)
+       , location_marker_color (0)
+       , location_range_color (0)
+       , location_loop_color (0)
+       , location_punch_color (0)
+       , location_cd_marker_color (0)
+       , entered_marker (0)
+       , _show_marker_lines (false)
+       , clicked_axisview (0)
+       , clicked_routeview (0)
+       , clicked_regionview (0)
+       , clicked_selection (0)
+       , clicked_control_point (0)
+       , button_release_can_deselect (true)
        , _mouse_changed_selection (false)
-         /* time display buttons */
+       , region_edit_menu_split_item (0)
+       , region_edit_menu_split_multichannel_item (0)
+       , track_region_edit_playlist_menu (0)
+       , track_edit_playlist_submenu (0)
+       , track_selection_edit_playlist_submenu (0)
+       , _popup_region_menu_item (0)
+       , _track_canvas (0)
+       , _track_canvas_viewport (0)
+       , within_track_canvas (false)
+       , _verbose_cursor (0)
+       , tempo_group (0)
+       , meter_group (0)
+       , marker_group (0)
+       , range_marker_group (0)
+       , transport_marker_group (0)
+       , cd_marker_group (0)
+       , _time_markers_group (0)
+       , hv_scroll_group (0)
+       , h_scroll_group (0)
+       , cursor_scroll_group (0)
+       , no_scroll_group (0)
+       , _trackview_group (0)
+       , _drag_motion_group (0)
+       , _canvas_drop_zone (0)
+       , no_ruler_shown_update (false)
+       ,  ruler_grabbed_widget (0)
+       , ruler_dialog (0)
+       , minsec_mark_interval (0)
+       , minsec_mark_modulo (0)
+       , minsec_nmarks (0)
+       , timecode_mark_modulo (0)
+       , timecode_nmarks (0)
+       , _samples_ruler_interval (0)
+       , bbt_bars (0)
+       , bbt_nmarks (0)
+       , bbt_bar_helper_on (0)
+       , bbt_accent_modulo (0)
+       , timecode_ruler (0)
+       , bbt_ruler (0)
+       , samples_ruler (0)
+       , minsec_ruler (0)
+       , visible_timebars (0)
+       , editor_ruler_menu (0)
+       , tempo_bar (0)
+       , meter_bar (0)
+       , marker_bar (0)
+       , range_marker_bar (0)
+       , transport_marker_bar (0)
+       , cd_marker_bar (0)
        , minsec_label (_("Mins:Secs"))
        , bbt_label (_("Bars:Beats"))
        , timecode_label (_("Timecode"))
@@ -267,72 +346,142 @@ Editor::Editor ()
        , transport_mark_label (_("Loop/Punch Ranges"))
        , cd_mark_label (_("CD Markers"))
        , videotl_label (_("Video Timeline"))
+       , videotl_group (0)
+       , playhead_cursor (0)
        , edit_packer (4, 4, true)
-
-         /* the values here don't matter: layout widgets
-            reset them as needed.
-         */
-
        , vertical_adjustment (0.0, 0.0, 10.0, 400.0)
        , horizontal_adjustment (0.0, 0.0, 1e16)
        , unused_adjustment (0.0, 0.0, 10.0, 400.0)
-
        , controls_layout (unused_adjustment, vertical_adjustment)
-
-         /* tool bar related */
-
-       , toolbar_selection_clock_table (2,3)
-       , _mouse_mode_tearoff (0)
-       , automation_mode_button (_("mode"))
-       , _zoom_tearoff (0)
-       , _tools_tearoff (0)
-
-       , _toolbar_viewport (*manage (new Gtk::Adjustment (0, 0, 1e10)), *manage (new Gtk::Adjustment (0, 0, 1e10)))
+       , _scroll_callbacks (0)
+       , _visible_canvas_width (0)
+       , _visible_canvas_height (0)
+       , _full_canvas_height (0)
+       , edit_controls_left_menu (0)
+       , edit_controls_right_menu (0)
+       , last_update_frame (0)
+       , cut_buffer_start (0)
+       , cut_buffer_length (0)
+       , button_bindings (0)
+       , last_paste_pos (0)
+       , paste_count (0)
+       , sfbrowser (0)
+       , current_interthread_info (0)
+       , analysis_window (0)
+       , select_new_marker (false)
+       , last_scrub_x (0)
+       , scrubbing_direction (0)
+       , scrub_reversals (0)
+       , scrub_reverse_distance (0)
+       , have_pending_keyboard_selection (false)
+       , pending_keyboard_selection_start (0)
+       , _snap_type (SnapToBeat)
+       , _snap_mode (SnapOff)
+       , snap_threshold (5.0)
+       , ignore_gui_changes (false)
+       , _drags (new DragManager (this))
+       , lock_dialog (0)
+         /* , last_event_time { 0, 0 } */ /* this initialization style requires C++11 */
+       , _dragging_playhead (false)
+       , _dragging_edit_point (false)
+       , _show_measures (true)
+       , _follow_playhead (true)
+       , _stationary_playhead (false)
+       , _maximised (false)
+       , tempo_lines (0)
+       , global_rect_group (0)
+       , time_line_group (0)
+       , tempo_marker_menu (0)
+       , meter_marker_menu (0)
+       , marker_menu (0)
+       , range_marker_menu (0)
+       , transport_marker_menu (0)
+       , new_transport_marker_menu (0)
+       , cd_marker_menu (0)
+       , marker_menu_item (0)
+       , bbt_beat_subdivision (4)
+       , _visible_track_count (-1)
+       ,  toolbar_selection_clock_table (2,3)
+       ,  automation_mode_button (_("mode"))
+       ,  _toolbar_viewport (*manage (new Gtk::Adjustment (0, 0, 1e10)), *manage (new Gtk::Adjustment (0, 0, 1e10)))
+       , selection (new Selection (this))
+       , cut_buffer (new Selection (this))
+       , _selection_memento (new SelectionMemento())
+       , _all_region_actions_sensitized (false)
+       , _ignore_region_action (false)
+       , _last_region_menu_was_main (false)
+       , _ignore_follow_edits (false)
+       , cd_marker_bar_drag_rect (0)
+       , range_bar_drag_rect (0)
+       , transport_bar_drag_rect (0)
+       , transport_bar_range_rect (0)
+       , transport_bar_preroll_rect (0)
+       , transport_bar_postroll_rect (0)
+       , transport_loop_range_rect (0)
+       , transport_punch_range_rect (0)
+       , transport_punchin_line (0)
+       , transport_punchout_line (0)
+       , transport_preroll_rect (0)
+       , transport_postroll_rect (0)
+       , temp_location (0)
+       , rubberband_rect (0)
+       , _route_groups (0)
+       , _routes (0)
+       , _regions (0)
+       , _snapshots (0)
+       , _locations (0)
+       , autoscroll_horizontal_allowed (false)
+       , autoscroll_vertical_allowed (false)
+       , autoscroll_cnt (0)
+       , autoscroll_widget (0)
+       , show_gain_after_trim (false)
        , selection_op_cmd_depth (0)
        , selection_op_history_it (0)
-
-         /* nudge */
-
-       , nudge_clock (new AudioClock (X_("nudge"), false, X_("nudge"), true, false, true))
-       , meters_running(false)
+       , no_save_instant (false)
+       , current_timefx (0)
+       , current_mixer_strip (0)
+       , show_editor_mixer_when_tracks_arrive (false)
+       ,  nudge_clock (new AudioClock (X_("nudge"), false, X_("nudge"), true, false, true))
+       , current_stepping_trackview (0)
+       , last_track_height_step_timestamp (0)
+       , entered_track (0)
+       , entered_regionview (0)
+       , clear_entered_track (false)
+       , _edit_point (EditAtMouse)
+       , meters_running (false)
+       , rhythm_ferret (0)
+       , _have_idled (false)
+       , resize_idle_id (-1)
+       , _pending_resize_amount (0)
+       , _pending_resize_view (0)
        , _pending_locate_request (false)
        , _pending_initial_locate (false)
+       , _summary (0)
+       , _group_tabs (0)
+       , _last_motion_y (0)
+       , layering_order_editor (0)
        , _last_cut_copy_source_track (0)
-
        , _region_selection_change_updates_region_list (true)
+       , _cursors (0)
        , _following_mixer_selection (false)
        , _control_point_toggled_on_press (false)
        , _stepping_axis_view (0)
        , quantize_dialog (0)
        , _main_menu_disabler (0)
+       , myactions (X_("editor"))
 {
-       constructed = false;
-
        /* we are a singleton */
 
        PublicEditor::_instance = this;
 
        _have_idled = false;
 
-       selection = new Selection (this);
-       cut_buffer = new Selection (this);
-       _selection_memento = new SelectionMemento ();
+       last_event_time.tv_sec = 0;
+       last_event_time.tv_usec = 0;
+
        selection_op_history.clear();
        before.clear();
 
-       clicked_regionview = 0;
-       clicked_axisview = 0;
-       clicked_routeview = 0;
-       clicked_control_point = 0;
-       last_update_frame = 0;
-       last_paste_pos = 0;
-       paste_count = 0;
-       _drags = new DragManager (this);
-       lock_dialog = 0;
-       ruler_dialog = 0;
-       current_mixer_strip = 0;
-       tempo_lines = 0;
-
        snap_type_strings =  I18N (_snap_type_strings);
        snap_mode_strings =  I18N (_snap_mode_strings);
        zoom_focus_strings = I18N (_zoom_focus_strings);
@@ -350,77 +499,17 @@ Editor::Editor ()
        build_snap_type_menu();
        build_edit_point_menu();
 
-       snap_threshold = 5.0;
-       bbt_beat_subdivision = 4;
-       _visible_canvas_width = 0;
-       _visible_canvas_height = 0;
-       autoscroll_horizontal_allowed = false;
-       autoscroll_vertical_allowed = false;
-       logo_item = 0;
-
-       analysis_window = 0;
-
-       current_interthread_info = 0;
-       _show_measures = true;
-       _maximised = false;
-       show_gain_after_trim = false;
-
-       have_pending_keyboard_selection = false;
-       _follow_playhead = true;
-        _stationary_playhead = false;
-       editor_ruler_menu = 0;
-       no_ruler_shown_update = false;
-       marker_menu = 0;
-       range_marker_menu = 0;
-       marker_menu_item = 0;
-       tempo_or_meter_marker_menu = 0;
-       transport_marker_menu = 0;
-       new_transport_marker_menu = 0;
-       editor_mixer_strip_width = Wide;
-       show_editor_mixer_when_tracks_arrive = false;
-       region_edit_menu_split_multichannel_item = 0;
-       region_edit_menu_split_item = 0;
-       temp_location = 0;
-       leftmost_frame = 0;
-       mouse_mode = MouseObject;
-       current_stepping_trackview = 0;
-       entered_track = 0;
-       entered_regionview = 0;
-       entered_marker = 0;
-       clear_entered_track = false;
-       current_timefx = 0;
-       playhead_cursor = 0;
-       button_release_can_deselect = true;
-       _dragging_playhead = false;
-       _dragging_edit_point = false;
-       select_new_marker = false;
-       rhythm_ferret = 0;
-       layering_order_editor = 0;
-       no_save_visual = false;
-       resize_idle_id = -1;
-       within_track_canvas = false;
-
-       scrubbing_direction = 0;
-
-       sfbrowser = 0;
-
        location_marker_color = UIConfiguration::instance().color ("location marker");
        location_range_color = UIConfiguration::instance().color ("location range");
        location_cd_marker_color = UIConfiguration::instance().color ("location cd marker");
        location_loop_color = UIConfiguration::instance().color ("location loop");
        location_punch_color = UIConfiguration::instance().color ("location punch");
 
-       zoom_focus = ZoomFocusPlayhead;
-       _edit_point = EditAtMouse;
-       _visible_track_count = -1;
-
-       samples_per_pixel = 2048; /* too early to use reset_zoom () */
+       timebar_height = std::max(12., ceil (15. * ARDOUR_UI::ui_scale));
 
-       timebar_height = std::max(12., ceil (15. * UIConfiguration::instance().get_ui_scale()));
        TimeAxisView::setup_sizes ();
        ArdourMarker::setup_sizes (timebar_height);
-
-       _scroll_callbacks = 0;
+       TempoCurve::setup_sizes (timebar_height);
 
        bbt_label.set_name ("EditorRulerLabel");
        bbt_label.set_size_request (-1, (int)timebar_height);
@@ -659,40 +748,20 @@ Editor::Editor ()
 
        global_vpacker.pack_start (top_hbox, false, false);
        global_vpacker.pack_start (*hbox, true, true);
-
        global_hpacker.pack_start (global_vpacker, true, true);
 
-       set_name ("EditorWindow");
-       add_accel_group (ActionManager::ui_manager->get_accel_group());
-
-       status_bar_hpacker.show ();
+       /* need to show the "contents" widget so that notebook will show if tab is switched to
+        */
 
-       vpacker.pack_end (status_bar_hpacker, false, false);
-       vpacker.pack_end (global_hpacker, true, true);
+       global_hpacker.show ();
 
        /* register actions now so that set_state() can find them and set toggles/checks etc */
 
        register_actions ();
-       /* when we start using our own keybinding system for the editor, this
-        * will be uncommented
-        */
-       // load_bindings ();
+       load_bindings ();
 
        setup_toolbar ();
 
-       set_zoom_focus (zoom_focus);
-       set_visible_track_count (_visible_track_count);
-       _snap_type = SnapToBeat;
-       set_snap_to (_snap_type);
-       _snap_mode = SnapOff;
-       set_snap_mode (_snap_mode);
-       set_mouse_mode (MouseObject, true);
-        pre_internal_snap_type = _snap_type;
-        pre_internal_snap_mode = _snap_mode;
-        internal_snap_type = _snap_type;
-        internal_snap_mode = _snap_mode;
-       set_edit_point_preference (EditAtMouse, true);
-
        _playlist_selector = new PlaylistSelector();
        _playlist_selector->signal_delete_event().connect (sigc::bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (_playlist_selector)));
 
@@ -708,39 +777,6 @@ Editor::Editor ()
 
        fade_context_menu.set_name ("ArdourContextMenu");
 
-       /* icons, titles, WM stuff */
-
-       list<Glib::RefPtr<Gdk::Pixbuf> > window_icons;
-       Glib::RefPtr<Gdk::Pixbuf> icon;
-
-       if ((icon = ::get_icon ("ardour_icon_16px")) != 0) {
-               window_icons.push_back (icon);
-       }
-       if ((icon = ::get_icon ("ardour_icon_22px")) != 0) {
-               window_icons.push_back (icon);
-       }
-       if ((icon = ::get_icon ("ardour_icon_32px")) != 0) {
-               window_icons.push_back (icon);
-       }
-       if ((icon = ::get_icon ("ardour_icon_48px")) != 0) {
-               window_icons.push_back (icon);
-       }
-       if (!window_icons.empty()) {
-               // set_icon_list (window_icons);
-               set_default_icon_list (window_icons);
-       }
-
-       WindowTitle title(Glib::get_application_name());
-       title += _("Editor");
-       set_title (title.get_string());
-       set_wmclass (X_("ardour_editor"), PROGRAM_NAME);
-
-       add (vpacker);
-       add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
-
-       signal_configure_event().connect (sigc::mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::configure_handler));
-       signal_delete_event().connect (sigc::mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::exit_on_main_window_close));
-
        Gtkmm2ext::Keyboard::the_keyboard().ZoomVerticalModifierReleased.connect (sigc::mem_fun (*this, &Editor::zoom_vertical_modifier_released));
 
        /* allow external control surfaces/protocols to do various things */
@@ -787,12 +823,12 @@ Editor::Editor ()
 
         /* Button bindings */
 
-        button_bindings = new Bindings;
+       button_bindings = new Bindings ("editor-mouse");
 
        XMLNode* node = button_settings();
         if (node) {
                 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
-                        button_bindings->load (**i);
+                        button_bindings->load_operation (**i);
                 }
         }
 
@@ -804,6 +840,9 @@ Editor::Editor ()
 
        setup_fade_images ();
 
+       LuaInstance::instance(); // instantiate
+       LuaInstance::instance()->ActionChanged.connect (sigc::mem_fun (*this, &Editor::set_script_action_name));
+
        instant_save ();
 }
 
@@ -841,25 +880,6 @@ Editor::button_settings () const
        return node;
 }
 
-void
-Editor::add_toplevel_menu (Container& cont)
-{
-       vpacker.pack_start (cont, false, false);
-       cont.show_all ();
-}
-
-void
-Editor::add_transport_frame (Container& cont)
-{
-       if(ARDOUR::Profile->get_mixbus()) {
-               global_vpacker.pack_start (cont, false, false);
-               global_vpacker.reorder_child (cont, 0);
-               cont.show_all ();
-       } else {
-               vpacker.pack_start (cont, false, false);
-       }
-}
-
 bool
 Editor::get_smart_mode () const
 {
@@ -929,48 +949,10 @@ Editor::set_entered_track (TimeAxisView* tav)
        }
 }
 
-void
-Editor::show_window ()
-{
-       if (!is_visible ()) {
-               DisplaySuspender ds;
-               show_all ();
-
-               /* XXX: this is a bit unfortunate; it would probably
-                  be nicer if we could just call show () above rather
-                  than needing the show_all ()
-               */
-
-               /* re-hide stuff if necessary */
-               editor_list_button_toggled ();
-               parameter_changed ("show-summary");
-               parameter_changed ("show-group-tabs");
-               parameter_changed ("show-zoom-tools");
-
-               /* now reset all audio_time_axis heights, because widgets might need
-                  to be re-hidden
-               */
-
-               TimeAxisView *tv;
-
-               for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
-                       tv = (static_cast<TimeAxisView*>(*i));
-                       tv->reset_height ();
-               }
-
-               if (current_mixer_strip) {
-                       current_mixer_strip->hide_things ();
-                       current_mixer_strip->parameter_changed ("mixer-element-visibility");
-               }
-       }
-
-       present ();
-}
-
 void
 Editor::instant_save ()
 {
-       if (!constructed || !ARDOUR_UI::instance()->session_loaded) {
+       if (!constructed || !ARDOUR_UI::instance()->session_loaded || no_save_instant) {
                return;
        }
 
@@ -1018,7 +1000,7 @@ Editor::control_unselect ()
 }
 
 void
-Editor::control_select (uint32_t rid, Selection::Operation op)
+Editor::control_select (uint16_t rid, Selection::Operation op)
 {
        /* handles the (static) signal from the ControlProtocol class that
         * requests setting the selected track to a given RID
@@ -1028,7 +1010,7 @@ Editor::control_select (uint32_t rid, Selection::Operation op)
                return;
        }
 
-       boost::shared_ptr<Route> r = _session->route_by_remote_id (rid);
+       boost::shared_ptr<Route> r = _session->get_remote_nth_route (rid);
 
        if (!r) {
                return;
@@ -1159,14 +1141,11 @@ Editor::access_action (std::string action_group, std::string action_item)
 void
 Editor::on_realize ()
 {
-       Window::on_realize ();
        Realized ();
 
        if (UIConfiguration::instance().get_lock_gui_after_seconds()) {
                start_lock_event_timing ();
        }
-
-       signal_event().connect (sigc::mem_fun (*this, &Editor::generic_event_handler));
 }
 
 void
@@ -1186,7 +1165,9 @@ Editor::generic_event_handler (GdkEvent* ev)
        case GDK_MOTION_NOTIFY:
        case GDK_KEY_PRESS:
        case GDK_KEY_RELEASE:
-               gettimeofday (&last_event_time, 0);
+               if (contents().is_mapped()) {
+                       gettimeofday (&last_event_time, 0);
+               }
                break;
 
        case GDK_LEAVE_NOTIFY:
@@ -1201,7 +1182,7 @@ Editor::generic_event_handler (GdkEvent* ev)
                        /* leaving window, so reset focus, thus ending any and
                           all text entry operations.
                        */
-                       reset_focus();
+                       reset_focus (&contents());
                        break;
                }
                break;
@@ -1283,7 +1264,11 @@ Editor::center_screen_internal (framepos_t frame, float page)
 void
 Editor::update_title ()
 {
-       ENSURE_GUI_THREAD (*this, &Editor::update_title)
+       ENSURE_GUI_THREAD (*this, &Editor::update_title);
+
+       if (!own_window()) {
+               return;
+       }
 
        if (_session) {
                bool dirty = _session->dirty();
@@ -1301,8 +1286,9 @@ Editor::update_title ()
                }
 
                WindowTitle title(session_name);
+               title += S_("Window|Editor");
                title += Glib::get_application_name();
-               set_title (title.get_string());
+               own_window()->set_title (title.get_string());
        } else {
                /* ::session_going_away() will have taken care of it */
        }
@@ -1381,6 +1367,7 @@ Editor::set_session (Session *t)
        _session->RouteAdded.connect (_session_connections, invalidator (*this), boost::bind (&Editor::add_routes, this, _1), gui_context());
        _session->DirtyChanged.connect (_session_connections, invalidator (*this), boost::bind (&Editor::update_title, this), gui_context());
        _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&Editor::tempo_map_changed, this, _1), gui_context());
+       _session->tempo_map().MetricPositionChanged.connect (_session_connections, invalidator (*this), boost::bind (&Editor::marker_position_changed, this), gui_context());
        _session->Located.connect (_session_connections, invalidator (*this), boost::bind (&Editor::located, this), gui_context());
        _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&Editor::parameter_changed, this, _1), gui_context());
        _session->StateSaved.connect (_session_connections, invalidator (*this), boost::bind (&Editor::session_state_saved, this, _1), gui_context());
@@ -1425,6 +1412,8 @@ Editor::set_session (Session *t)
 
        ActionManager::ui_manager->signal_pre_activate().connect (sigc::mem_fun (*this, &Editor::action_pre_activated));
 
+       LuaInstance::instance()->set_session(_session);
+
        start_updating_meters ();
 }
 
@@ -1684,7 +1673,7 @@ Editor::build_track_context_menu ()
 {
        using namespace Menu_Helpers;
 
-       MenuList& edit_items = track_context_menu.items();
+       MenuList& edit_items = track_context_menu.items();
        edit_items.clear();
 
        add_dstream_context_items (edit_items);
@@ -1696,7 +1685,7 @@ Editor::build_track_bus_context_menu ()
 {
        using namespace Menu_Helpers;
 
-       MenuList& edit_items = track_context_menu.items();
+       MenuList& edit_items = track_context_menu.items();
        edit_items.clear();
 
        add_bus_context_items (edit_items);
@@ -1733,7 +1722,103 @@ Editor::build_track_region_context_menu ()
 }
 
 void
-Editor::analyze_region_selection ()
+Editor::loudness_analyze_region_selection ()
+{
+       if (!_session) {
+               return;
+       }
+       Selection& s (PublicEditor::instance ().get_selection ());
+       RegionSelection ars = s.regions;
+       ARDOUR::AnalysisGraph ag (_session);
+       framecnt_t total_work = 0;
+
+       for (RegionSelection::iterator j = ars.begin (); j != ars.end (); ++j) {
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*j);
+               if (!arv) {
+                       continue;
+               }
+               if (!boost::dynamic_pointer_cast<AudioRegion> (arv->region ())) {
+                       continue;
+               }
+               assert (dynamic_cast<RouteTimeAxisView *> (&arv->get_time_axis_view ()));
+               total_work += arv->region ()->length ();
+       }
+
+       SimpleProgressDialog spd (_("Region Loudness Analysis"), sigc::mem_fun (ag, &AnalysisGraph::cancel));
+       ScopedConnection c;
+       ag.set_total_frames (total_work);
+       ag.Progress.connect_same_thread (c, boost::bind (&SimpleProgressDialog::update_progress, &spd, _1, _2));
+       spd.show();
+
+       for (RegionSelection::iterator j = ars.begin (); j != ars.end (); ++j) {
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*j);
+               if (!arv) {
+                       continue;
+               }
+               boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (arv->region ());
+               if (!ar) {
+                       continue;
+               }
+               ag.analyze_region (ar);
+       }
+       spd.hide();
+       if (!ag.canceled ()) {
+               ExportReport er (_("Audio Report/Analysis"), ag.results ());
+               er.run();
+       }
+}
+
+void
+Editor::loudness_analyze_range_selection ()
+{
+       if (!_session) {
+               return;
+       }
+       Selection& s (PublicEditor::instance ().get_selection ());
+       TimeSelection ts = s.time;
+       ARDOUR::AnalysisGraph ag (_session);
+       framecnt_t total_work = 0;
+
+       for (TrackSelection::iterator i = s.tracks.begin (); i != s.tracks.end (); ++i) {
+               boost::shared_ptr<AudioPlaylist> pl = boost::dynamic_pointer_cast<AudioPlaylist> ((*i)->playlist ());
+               if (!pl) {
+                       continue;
+               }
+               RouteUI *rui = dynamic_cast<RouteUI *> (*i);
+               if (!pl || !rui) {
+                       continue;
+               }
+               for (std::list<AudioRange>::iterator j = ts.begin (); j != ts.end (); ++j) {
+                       total_work += j->length ();
+               }
+       }
+
+       SimpleProgressDialog spd (_("Range Loudness Analysis"), sigc::mem_fun (ag, &AnalysisGraph::cancel));
+       ScopedConnection c;
+       ag.set_total_frames (total_work);
+       ag.Progress.connect_same_thread (c, boost::bind (&SimpleProgressDialog::update_progress, &spd, _1, _2));
+       spd.show();
+
+       for (TrackSelection::iterator i = s.tracks.begin (); i != s.tracks.end (); ++i) {
+               boost::shared_ptr<AudioPlaylist> pl = boost::dynamic_pointer_cast<AudioPlaylist> ((*i)->playlist ());
+               if (!pl) {
+                       continue;
+               }
+               RouteUI *rui = dynamic_cast<RouteUI *> (*i);
+               if (!pl || !rui) {
+                       continue;
+               }
+               ag.analyze_range (rui->route (), pl, ts);
+       }
+       spd.hide();
+       if (!ag.canceled ()) {
+               ExportReport er (_("Audio Report/Analysis"), ag.results ());
+               er.run();
+       }
+}
+
+void
+Editor::spectral_analyze_region_selection ()
 {
        if (analysis_window == 0) {
                analysis_window = new AnalysisWindow();
@@ -1751,7 +1836,7 @@ Editor::analyze_region_selection ()
 }
 
 void
-Editor::analyze_range_selection()
+Editor::spectral_analyze_range_selection()
 {
        if (analysis_window == 0) {
                analysis_window = new AnalysisWindow();
@@ -1845,7 +1930,8 @@ Editor::add_selection_context_items (Menu_Helpers::MenuList& edit_items)
        edit_items.push_back (MenuElem (_("Zoom to Range"), sigc::bind (sigc::mem_fun(*this, &Editor::temporal_zoom_selection), false)));
 
        edit_items.push_back (SeparatorElem());
-       edit_items.push_back (MenuElem (_("Spectral Analysis"), sigc::mem_fun(*this, &Editor::analyze_range_selection)));
+       edit_items.push_back (MenuElem (_("Loudness Analysis"), sigc::mem_fun(*this, &Editor::loudness_analyze_range_selection)));
+       edit_items.push_back (MenuElem (_("Spectral Analysis"), sigc::mem_fun(*this, &Editor::spectral_analyze_range_selection)));
 
        edit_items.push_back (SeparatorElem());
 
@@ -2052,6 +2138,37 @@ Editor::snap_type() const
        return _snap_type;
 }
 
+bool
+Editor::snap_musical() const
+{
+       switch (_snap_type) {
+       case SnapToBeatDiv128:
+       case SnapToBeatDiv64:
+       case SnapToBeatDiv32:
+       case SnapToBeatDiv28:
+       case SnapToBeatDiv24:
+       case SnapToBeatDiv20:
+       case SnapToBeatDiv16:
+       case SnapToBeatDiv14:
+       case SnapToBeatDiv12:
+       case SnapToBeatDiv10:
+       case SnapToBeatDiv8:
+       case SnapToBeatDiv7:
+       case SnapToBeatDiv6:
+       case SnapToBeatDiv5:
+       case SnapToBeatDiv4:
+       case SnapToBeatDiv3:
+       case SnapToBeatDiv2:
+       case SnapToBeat:
+       case SnapToBar:
+               return true;
+       default:
+               break;
+       }
+
+       return false;
+}
+
 SnapMode
 Editor::snap_mode() const
 {
@@ -2102,14 +2219,10 @@ Editor::set_snap_to (SnapType st)
        case SnapToBeatDiv4:
        case SnapToBeatDiv3:
        case SnapToBeatDiv2: {
-               ARDOUR::TempoMap::BBTPointList::const_iterator current_bbt_points_begin;
-               ARDOUR::TempoMap::BBTPointList::const_iterator current_bbt_points_end;
-
-               compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_samples(),
-                                           current_bbt_points_begin, current_bbt_points_end);
-               compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples(),
-                                        current_bbt_points_begin, current_bbt_points_end);
-               update_tempo_based_rulers (current_bbt_points_begin, current_bbt_points_end);
+               std::vector<TempoMap::BBTPoint> grid;
+               compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
+               compute_bbt_ruler_scale (grid, leftmost_frame, leftmost_frame + current_page_samples());
+               update_tempo_based_rulers (grid);
                break;
        }
 
@@ -2203,54 +2316,13 @@ Editor::set_edit_point_preference (EditPoint ep, bool force)
 }
 
 int
-Editor::set_state (const XMLNode& node, int /*version*/)
+Editor::set_state (const XMLNode& node, int version)
 {
-       const XMLProperty* prop;
-       XMLNode* geometry;
-       int x, y;
-       Gdk::Geometry g;
-
+       XMLProperty const * prop;
        set_id (node);
+       PBD::Unwinder<bool> nsi (no_save_instant, true);
 
-       g.base_width = default_width;
-       g.base_height = default_height;
-       x = 1;
-       y = 1;
-
-       if ((geometry = find_named_node (node, "geometry")) != 0) {
-
-               XMLProperty* prop;
-
-               if ((prop = geometry->property("x_size")) == 0) {
-                       prop = geometry->property ("x-size");
-               }
-               if (prop) {
-                       g.base_width = atoi(prop->value());
-               }
-               if ((prop = geometry->property("y_size")) == 0) {
-                       prop = geometry->property ("y-size");
-               }
-               if (prop) {
-                       g.base_height = atoi(prop->value());
-               }
-
-               if ((prop = geometry->property ("x_pos")) == 0) {
-                       prop = geometry->property ("x-pos");
-               }
-               if (prop) {
-                       x = atoi (prop->value());
-
-               }
-               if ((prop = geometry->property ("y_pos")) == 0) {
-                       prop = geometry->property ("y-pos");
-               }
-               if (prop) {
-                       y = atoi (prop->value());
-               }
-       }
-
-       set_default_size (g.base_width, g.base_height);
-       move (x, y);
+       Tabbable::set_state (node, version);
 
        if (_session && (prop = node.property ("playhead"))) {
                framepos_t pos;
@@ -2287,10 +2359,16 @@ Editor::set_state (const XMLNode& node, int /*version*/)
 
        if ((prop = node.property ("snap-to"))) {
                snap_type_selection_done ((SnapType) string_2_enum (prop->value(), _snap_type));
+               set_snap_to ((SnapType) string_2_enum (prop->value(), _snap_type));
        }
 
        if ((prop = node.property ("snap-mode"))) {
                snap_mode_selection_done((SnapMode) string_2_enum (prop->value(), _snap_mode));
+               /* set text of Dropdown. in case _snap_mode == SnapOff (default)
+                * snap_mode_selection_done() will only mark an already active item as active
+                * which does not trigger set_text().
+                */
+               set_snap_mode ((SnapMode) string_2_enum (prop->value(), _snap_mode));
        }
 
        if ((prop = node.property ("internal-snap-to"))) {
@@ -2469,43 +2547,25 @@ Editor::set_state (const XMLNode& node, int /*version*/)
                }
        }
 
-       return 0;
+       return LuaInstance::instance()->set_state(node);
 }
 
 XMLNode&
 Editor::get_state ()
 {
-       XMLNode* node = new XMLNode ("Editor");
+       XMLNode* node = new XMLNode (X_("Editor"));
        char buf[32];
 
        id().print (buf, sizeof (buf));
        node->add_property ("id", buf);
 
-       if (is_realized()) {
-               Glib::RefPtr<Gdk::Window> win = get_window();
+       node->add_child_nocopy (Tabbable::get_state());
 
-               int x, y, width, height;
-               win->get_root_origin(x, y);
-               win->get_size(width, height);
-
-               XMLNode* geometry = new XMLNode ("geometry");
-
-               snprintf(buf, sizeof(buf), "%d", width);
-               geometry->add_property("x-size", string(buf));
-               snprintf(buf, sizeof(buf), "%d", height);
-               geometry->add_property("y-size", string(buf));
-               snprintf(buf, sizeof(buf), "%d", x);
-               geometry->add_property("x-pos", string(buf));
-               snprintf(buf, sizeof(buf), "%d", y);
-               geometry->add_property("y-pos", string(buf));
-               snprintf(buf,sizeof(buf), "%d",gtk_paned_get_position (static_cast<Paned*>(&edit_pane)->gobj()));
-               geometry->add_property("edit-horizontal-pane-pos", string(buf));
-               geometry->add_property("notebook-shrunk", _notebook_shrunk ? "1" : "0");
-               snprintf(buf,sizeof(buf), "%d",gtk_paned_get_position (static_cast<Paned*>(&editor_summary_pane)->gobj()));
-               geometry->add_property("edit-vertical-pane-pos", string(buf));
-
-               node->add_child_nocopy (*geometry);
-       }
+       snprintf(buf,sizeof(buf), "%f", paned_position_as_fraction (edit_pane, false));
+       node->add_property("edit-horizontal-pane-pos", string(buf));
+       node->add_property("notebook-shrunk", _notebook_shrunk ? "1" : "0");
+       snprintf(buf,sizeof(buf), "%f", paned_position_as_fraction (editor_summary_pane, true));
+       node->add_property("edit-vertical-pane-pos", string(buf));
 
        maybe_add_mixer_strip_width (*node);
 
@@ -2567,6 +2627,9 @@ Editor::get_state ()
        snprintf (buf, sizeof (buf), "%" PRId64, nudge_clock->current_duration());
        node->add_property ("nudge-clock-value", buf);
 
+       node->add_child_nocopy (LuaInstance::instance()->get_action_state());
+       node->add_child_nocopy (LuaInstance::instance()->get_hook_state());
+
        return *node;
 }
 
@@ -2971,24 +3034,8 @@ Editor::setup_toolbar ()
        if (!ARDOUR::Profile->get_trx()) {
                mode_box->pack_start (edit_mode_selector, false, false);
        }
-       mode_box->pack_start (*mouse_mode_box, false, false);
-
-       _mouse_mode_tearoff = manage (new TearOff (*mode_box));
-       _mouse_mode_tearoff->set_name ("MouseModeBase");
-       _mouse_mode_tearoff->tearoff_window().signal_key_press_event().connect (sigc::bind (sigc::ptr_fun (relay_key_press), &_mouse_mode_tearoff->tearoff_window()), false);
-
-       if (Profile->get_sae() || Profile->get_mixbus() ) {
-               _mouse_mode_tearoff->set_can_be_torn_off (false);
-       }
 
-       _mouse_mode_tearoff->Detach.connect (sigc::bind (sigc::mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox),
-                                                        &_mouse_mode_tearoff->tearoff_window()));
-       _mouse_mode_tearoff->Attach.connect (sigc::bind (sigc::mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox),
-                                                        &_mouse_mode_tearoff->tearoff_window(), 1));
-       _mouse_mode_tearoff->Hidden.connect (sigc::bind (sigc::mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox),
-                                                        &_mouse_mode_tearoff->tearoff_window()));
-       _mouse_mode_tearoff->Visible.connect (sigc::bind (sigc::mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox),
-                                                         &_mouse_mode_tearoff->tearoff_window(), 1));
+       mode_box->pack_start (*mouse_mode_box, false, false);
 
        /* Zoom */
 
@@ -3060,23 +3107,6 @@ Editor::setup_toolbar ()
                _zoom_box.pack_start (tav_expand_button);
        }
 
-       if (!ARDOUR::Profile->get_trx()) {
-               _zoom_tearoff = manage (new TearOff (_zoom_box));
-
-               _zoom_tearoff->Detach.connect (sigc::bind (sigc::mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox),
-                                                          &_zoom_tearoff->tearoff_window()));
-               _zoom_tearoff->Attach.connect (sigc::bind (sigc::mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox),
-                                                          &_zoom_tearoff->tearoff_window(), 0));
-               _zoom_tearoff->Hidden.connect (sigc::bind (sigc::mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox),
-                                                          &_zoom_tearoff->tearoff_window()));
-               _zoom_tearoff->Visible.connect (sigc::bind (sigc::mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox),
-                                                           &_zoom_tearoff->tearoff_window(), 0));
-       }
-
-       if (Profile->get_sae() || Profile->get_mixbus() ) {
-               _zoom_tearoff->set_can_be_torn_off (false);
-       }
-
        snap_box.set_spacing (2);
        snap_box.set_border_width (2);
 
@@ -3109,37 +3139,19 @@ Editor::setup_toolbar ()
        HBox* hbox = manage (new HBox);
        hbox->set_spacing(2);
 
-       _tools_tearoff = manage (new TearOff (*hbox));
-       _tools_tearoff->set_name ("MouseModeBase");
-       _tools_tearoff->tearoff_window().signal_key_press_event().connect (sigc::bind (sigc::ptr_fun (relay_key_press), &_tools_tearoff->tearoff_window()), false);
-
-       if (Profile->get_sae() || Profile->get_mixbus()) {
-               _tools_tearoff->set_can_be_torn_off (false);
-       }
-
-       _tools_tearoff->Detach.connect (sigc::bind (sigc::mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox),
-                                                   &_tools_tearoff->tearoff_window()));
-       _tools_tearoff->Attach.connect (sigc::bind (sigc::mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox),
-                                                   &_tools_tearoff->tearoff_window(), 0));
-       _tools_tearoff->Hidden.connect (sigc::bind (sigc::mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox),
-                                                   &_tools_tearoff->tearoff_window()));
-       _tools_tearoff->Visible.connect (sigc::bind (sigc::mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox),
-                                                    &_tools_tearoff->tearoff_window(), 0));
-
        toolbar_hbox.set_spacing (2);
        toolbar_hbox.set_border_width (1);
 
-       toolbar_hbox.pack_start (*_mouse_mode_tearoff, false, false);
+       toolbar_hbox.pack_start (*mode_box, false, false);
        if (!ARDOUR::Profile->get_trx()) {
-               toolbar_hbox.pack_start (*_zoom_tearoff, false, false);
-               toolbar_hbox.pack_start (*_tools_tearoff, false, false);
+               toolbar_hbox.pack_start (_zoom_box, false, false);
+               toolbar_hbox.pack_start (*hbox, false, false);
        }
 
        if (!ARDOUR::Profile->get_trx()) {
                hbox->pack_start (snap_box, false, false);
                hbox->pack_start (*nudge_box, false, false);
        }
-       hbox->pack_start (panic_box, false, false);
 
        hbox->show_all ();
 
@@ -3623,11 +3635,7 @@ Editor::cycle_edit_mode ()
 {
        switch (Config->get_edit_mode()) {
        case Slide:
-               if (Profile->get_sae()) {
-                       Config->set_edit_mode (Lock);
-               } else {
-                       Config->set_edit_mode (Ripple);
-               }
+               Config->set_edit_mode (Ripple);
                break;
        case Splice:
        case Ripple:
@@ -3835,7 +3843,7 @@ bool
 Editor::edit_controls_button_release (GdkEventButton* ev)
 {
        if (Keyboard::is_context_menu_event (ev)) {
-               ARDOUR_UI::instance()->add_route (this);
+               ARDOUR_UI::instance()->add_route ();
        } else if (ev->button == 1) {
                selection->clear_tracks ();
        }
@@ -3895,23 +3903,18 @@ Editor::cycle_zoom_focus ()
        }
 }
 
-void
-Editor::ensure_float (Window& win)
-{
-       win.set_transient_for (*this);
-}
-
 void
 Editor::pane_allocation_handler (Allocation &alloc, Paned* which)
 {
        /* recover or initialize pane positions. do this here rather than earlier because
           we don't want the positions to change the child allocations, which they seem to do.
+
+          See comments in mixer_ui.cc about how this works.
         */
 
-       int pos;
+       float pos;
        XMLProperty* prop;
-       char buf[32];
-       XMLNode* node = ARDOUR_UI::instance()->editor_settings();
+       XMLNode* geometry = ARDOUR_UI::instance()->editor_settings();
 
        enum Pane {
                Horizontal = 0x1,
@@ -3920,8 +3923,6 @@ Editor::pane_allocation_handler (Allocation &alloc, Paned* which)
 
        static Pane done;
 
-       XMLNode* geometry = find_named_node (*node, "geometry");
-
        if (which == static_cast<Paned*> (&edit_pane)) {
 
                if (done & Horizontal) {
@@ -3935,17 +3936,23 @@ Editor::pane_allocation_handler (Allocation &alloc, Paned* which)
                if (!geometry || (prop = geometry->property ("edit-horizontal-pane-pos")) == 0) {
                        /* initial allocation is 90% to canvas, 10% to notebook */
                        pos = (int) floor (alloc.get_width() * 0.90f);
-                       snprintf (buf, sizeof(buf), "%d", pos);
                } else {
-                       pos = atoi (prop->value());
+                       pos = atof (prop->value());
                }
 
-               if (GTK_WIDGET(edit_pane.gobj())->allocation.width > pos) {
-                       edit_pane.set_position (pos);
+               if (pos > 1.0f) {
+                       /* older versions of Ardour stored absolute position */
+                       if (alloc.get_width() > pos) {
+                               edit_pane.set_position (pos);
+                               done = (Pane) (done | Horizontal);
+                       }
+               } else {
+                       if (alloc.get_width() > 1.0/pos) {
+                               paned_set_position_as_fraction (edit_pane, pos, false);
+                               done = (Pane) (done | Horizontal);
+                       }
                }
 
-               done = (Pane) (done | Horizontal);
-
        } else if (which == static_cast<Paned*> (&editor_summary_pane)) {
 
                if (done & Vertical) {
@@ -3955,35 +3962,23 @@ Editor::pane_allocation_handler (Allocation &alloc, Paned* which)
                if (!geometry || (prop = geometry->property ("edit-vertical-pane-pos")) == 0) {
                        /* initial allocation is 90% to canvas, 10% to summary */
                        pos = (int) floor (alloc.get_height() * 0.90f);
-                       snprintf (buf, sizeof(buf), "%d", pos);
                } else {
 
-                       pos = atoi (prop->value());
+                       pos = atof (prop->value());
                }
 
-               if (GTK_WIDGET(editor_summary_pane.gobj())->allocation.height > pos) {
-                       editor_summary_pane.set_position (pos);
+               if (pos > 1.0f) {
+                       /* older versions of Ardour stored absolute position */
+                       if (alloc.get_height() > pos) {
+                               editor_summary_pane.set_position (pos);
+                               done = (Pane) (done | Vertical);
+                       }
+               } else {
+                       if (alloc.get_height() > 1.0/pos) {
+                               paned_set_position_as_fraction (editor_summary_pane, pos, true);
+                               done = (Pane) (done | Vertical);
+                       }
                }
-
-               done = (Pane) (done | Vertical);
-       }
-}
-
-void
-Editor::detach_tearoff (Box* /*b*/, Window* /*w*/)
-{
-       if ((_tools_tearoff->torn_off() || !_tools_tearoff->visible()) &&
-           (_mouse_mode_tearoff->torn_off() || !_mouse_mode_tearoff->visible()) &&
-           (_zoom_tearoff && (_zoom_tearoff->torn_off() || !_zoom_tearoff->visible()))) {
-               top_hbox.remove (toolbar_frame);
-       }
-}
-
-void
-Editor::reattach_tearoff (Box* /*b*/, Window* /*w*/, int32_t /*n*/)
-{
-       if (toolbar_frame.get_parent() == 0) {
-               top_hbox.pack_end (toolbar_frame);
        }
 }
 
@@ -3998,11 +3993,9 @@ Editor::set_show_measures (bool yn)
                                tempo_lines->show();
                        }
 
-                       ARDOUR::TempoMap::BBTPointList::const_iterator begin;
-                       ARDOUR::TempoMap::BBTPointList::const_iterator end;
-
-                       compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_samples(), begin, end);
-                       draw_measures (begin, end);
+                       std::vector<TempoMap::BBTPoint> grid;
+                       compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
+                       draw_measures (grid);
                }
 
                instant_save ();
@@ -4123,7 +4116,7 @@ Editor::get_grid_type_as_beats (bool& success, framepos_t position)
                return Evoral::Beats(1.0);
        case SnapToBar:
                if (_session) {
-                       return Evoral::Beats(_session->tempo_map().meter_at (position).divisions_per_bar());
+                       return Evoral::Beats(_session->tempo_map().meter_at_frame (position).divisions_per_bar());
                }
                break;
        default:
@@ -4161,10 +4154,13 @@ Editor::playlist_deletion_dialog (boost::shared_ptr<Playlist> pl)
 
        dialog.add_button (_("Delete All Unused"), RESPONSE_YES); // needs clarification. this and all remaining ones
        dialog.add_button (_("Delete Playlist"), RESPONSE_ACCEPT);
-       dialog.add_button (_("Keep Playlist"), RESPONSE_REJECT);
+       Button* keep = dialog.add_button (_("Keep Playlist"), RESPONSE_REJECT);
        dialog.add_button (_("Keep Remaining"), RESPONSE_NO); // ditto
        dialog.add_button (_("Cancel"), RESPONSE_CANCEL);
 
+       // by default gtk uses the left most button
+       keep->grab_focus ();
+
        switch (dialog.run ()) {
        case RESPONSE_NO:
                /* keep this and all remaining ones */
@@ -4263,25 +4259,6 @@ Editor::session_state_saved (string)
        _snapshots->redisplay ();
 }
 
-void
-Editor::update_tearoff_visibility()
-{
-       bool visible = UIConfiguration::instance().get_keep_tearoffs();
-       _mouse_mode_tearoff->set_visible (visible);
-       _tools_tearoff->set_visible (visible);
-       if (_zoom_tearoff) {
-               _zoom_tearoff->set_visible (visible);
-       }
-}
-
-void
-Editor::reattach_all_tearoffs ()
-{
-       if (_mouse_mode_tearoff) _mouse_mode_tearoff->put_it_back ();
-       if (_tools_tearoff) _tools_tearoff->put_it_back ();
-       if (_zoom_tearoff) _zoom_tearoff->put_it_back ();
-}
-
 void
 Editor::maximise_editing_space ()
 {
@@ -4289,9 +4266,12 @@ Editor::maximise_editing_space ()
                return;
        }
 
-       fullscreen ();
+       Gtk::Window* toplevel = current_toplevel();
 
-       _maximised = true;
+       if (toplevel) {
+               toplevel->fullscreen ();
+               _maximised = true;
+       }
 }
 
 void
@@ -4301,9 +4281,12 @@ Editor::restore_editing_space ()
                return;
        }
 
-       unfullscreen();
+       Gtk::Window* toplevel = current_toplevel();
 
-       _maximised = false;
+       if (toplevel) {
+               toplevel->unfullscreen();
+               _maximised = false;
+       }
 }
 
 /**
@@ -4371,19 +4354,6 @@ Editor::mapped_clear_playlist (RouteTimeAxisView& atv, uint32_t /*sz*/)
        atv.clear_playlist ();
 }
 
-bool
-Editor::on_key_press_event (GdkEventKey* ev)
-{
-       return key_press_focus_accelerator_handler (*this, ev);
-}
-
-bool
-Editor::on_key_release_event (GdkEventKey* ev)
-{
-       return Gtk::Window::on_key_release_event (ev);
-       // return key_press_focus_accelerator_handler (*this, ev);
-}
-
 double
 Editor::get_y_origin () const
 {
@@ -4651,14 +4621,10 @@ Editor::visual_changer (const VisualChange& vc)
 
                compute_fixed_ruler_scale ();
 
-               ARDOUR::TempoMap::BBTPointList::const_iterator current_bbt_points_begin;
-               ARDOUR::TempoMap::BBTPointList::const_iterator current_bbt_points_end;
-
-               compute_current_bbt_points (vc.time_origin, pending_visual_change.time_origin + current_page_samples(),
-                                           current_bbt_points_begin, current_bbt_points_end);
-               compute_bbt_ruler_scale (vc.time_origin, pending_visual_change.time_origin + current_page_samples(),
-                                        current_bbt_points_begin, current_bbt_points_end);
-               update_tempo_based_rulers (current_bbt_points_begin, current_bbt_points_end);
+               std::vector<TempoMap::BBTPoint> grid;
+               compute_current_bbt_points (grid, vc.time_origin, pending_visual_change.time_origin + current_page_samples());
+               compute_bbt_ruler_scale (grid, vc.time_origin, pending_visual_change.time_origin + current_page_samples());
+               update_tempo_based_rulers (grid);
 
                update_video_timeline();
        }
@@ -4730,10 +4696,7 @@ Editor::get_preferred_edit_position (EditIgnoreOption ignore, bool from_context_
        switch (ep) {
        case EditAtPlayhead:
                if (_dragging_playhead) {
-                       if (!mouse_frame (where, ignored)) {
-                               /* XXX not right but what can we do ? */
-                               return 0;
-                       }
+                       where = *_control_scroll_target;
                } else {
                        where = _session->audible_frame();
                }
@@ -4979,7 +4942,7 @@ Editor::get_regions_from_selection_and_mouse (framepos_t pos)
  */
 
 RegionSelection
-Editor::get_regions_from_selection_and_entered ()
+Editor::get_regions_from_selection_and_entered () const
 {
        RegionSelection regions = selection->regions;
 
@@ -5091,7 +5054,6 @@ Editor::first_idle ()
        if (track_views.size() > 1) {
                Timers::TimerSuspender t;
                dialog = new MessageDialog (
-                       *this,
                        string_compose (_("Please wait while %1 loads visual data."), PROGRAM_NAME),
                        true
                        );
@@ -5664,7 +5626,7 @@ Editor::super_rapid_screen_update ()
        /* METERING / MIXER STRIPS */
 
        /* update track meters, if required */
-       if (is_mapped() && meters_running) {
+       if (contents().is_mapped() && meters_running) {
                RouteTimeAxisView* rtv;
                for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
                        if ((rtv = dynamic_cast<RouteTimeAxisView*>(*i)) != 0) {
@@ -5715,9 +5677,9 @@ Editor::super_rapid_screen_update ()
 
                } else {
 
-                       if (!_dragging_playhead && _session->requested_return_frame() < 0 && !pending_visual_change.being_handled) {
+                       if (!_dragging_playhead && _follow_playhead && _session->requested_return_frame() < 0 && !pending_visual_change.being_handled) {
                                framepos_t const frame = playhead_cursor->current_frame ();
-                               double target = ((double)frame - (double)current_page_samples()/3.0);
+                               double target = ((double)frame - (double)current_page_samples()/2.0);
                                if (target <= 0.0) {
                                        target = 0.0;
                                }
@@ -5794,16 +5756,42 @@ Editor::session_going_away ()
 
        stop_step_editing ();
 
-       /* get rid of any existing editor mixer strip */
+       if (own_window()) {
 
-       WindowTitle title(Glib::get_application_name());
-       title += _("Editor");
+               /* get rid of any existing editor mixer strip */
 
-       set_title (title.get_string());
+               WindowTitle title(Glib::get_application_name());
+               title += _("Editor");
+
+               own_window()->set_title (title.get_string());
+       }
 
        SessionHandlePtr::session_going_away ();
 }
 
+void
+Editor::trigger_script (int i)
+{
+       LuaInstance::instance()-> call_action (i);
+}
+
+void
+Editor::set_script_action_name (int i, const std::string& n)
+{
+       string const a = string_compose (X_("script-action-%1"), i + 1);
+       Glib::RefPtr<Action> act = ActionManager::get_action(X_("Editor"), a.c_str());
+       assert (act);
+       if (n.empty ()) {
+               act->set_label (string_compose (_("Unset #%1"), i + 1));
+               act->set_tooltip (_("no action bound"));
+               act->set_sensitive (false);
+       } else {
+               act->set_label (n);
+               act->set_tooltip (n);
+               act->set_sensitive (true);
+       }
+       KeyEditor::UpdateBindings ();
+}
 
 void
 Editor::show_editor_list (bool yn)
@@ -6031,3 +6019,56 @@ Editor::ui_parameter_changed (string parameter)
                }
        }
 }
+
+Gtk::Window*
+Editor::use_own_window (bool and_fill_it)
+{
+       bool new_window = !own_window();
+
+       Gtk::Window* win = Tabbable::use_own_window (and_fill_it);
+
+       if (win && new_window) {
+               win->set_name ("EditorWindow");
+
+               ARDOUR_UI::instance()->setup_toplevel_window (*win, _("Editor"), this);
+
+               // win->signal_realize().connect (*this, &Editor::on_realize);
+               win->signal_event().connect (sigc::bind (sigc::ptr_fun (&Keyboard::catch_user_event_for_pre_dialog_focus), win));
+               win->signal_event().connect (sigc::mem_fun (*this, &Editor::generic_event_handler));
+               win->set_data ("ardour-bindings", bindings);
+
+               update_title ();
+       }
+
+       DisplaySuspender ds;
+       contents().show_all ();
+
+       /* XXX: this is a bit unfortunate; it would probably
+          be nicer if we could just call show () above rather
+          than needing the show_all ()
+       */
+
+       /* re-hide stuff if necessary */
+       editor_list_button_toggled ();
+       parameter_changed ("show-summary");
+       parameter_changed ("show-group-tabs");
+       parameter_changed ("show-zoom-tools");
+
+       /* now reset all audio_time_axis heights, because widgets might need
+          to be re-hidden
+       */
+
+       TimeAxisView *tv;
+
+       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+               tv = (static_cast<TimeAxisView*>(*i));
+               tv->reset_height ();
+       }
+
+       if (current_mixer_strip) {
+               current_mixer_strip->hide_things ();
+               current_mixer_strip->parameter_changed ("mixer-element-visibility");
+       }
+
+       return win;
+}