Pull snapshots code out of Editor class.
[ardour.git] / gtk2_ardour / editor.cc
1 /*
2     Copyright (C) 2000-2009 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /* Note: public Editor methods are documented in public_editor.h */
21
22 #define __STDC_LIMIT_MACROS 1
23 #include <stdint.h>
24 #include <unistd.h>
25 #include <cstdlib>
26 #include <cmath>
27 #include <string>
28 #include <algorithm>
29 #include <map>
30
31 #include <boost/none.hpp>
32
33 #include <sigc++/bind.h>
34
35 #include "pbd/convert.h"
36 #include "pbd/error.h"
37 #include "pbd/enumwriter.h"
38 #include "pbd/memento_command.h"
39 #include "pbd/unknown_type.h"
40
41 #include <glibmm/miscutils.h>
42 #include <gtkmm/image.h>
43 #include <gdkmm/color.h>
44 #include <gdkmm/bitmap.h>
45
46 #include <gtkmm2ext/grouped_buttons.h>
47 #include <gtkmm2ext/gtk_ui.h>
48 #include <gtkmm2ext/tearoff.h>
49 #include <gtkmm2ext/utils.h>
50 #include <gtkmm2ext/window_title.h>
51 #include <gtkmm2ext/choice.h>
52 #include <gtkmm2ext/cell_renderer_pixbuf_toggle.h>
53
54 #include "ardour/audio_diskstream.h"
55 #include "ardour/audio_track.h"
56 #include "ardour/audioplaylist.h"
57 #include "ardour/audioregion.h"
58 #include "ardour/location.h"
59 #include "ardour/midi_region.h"
60 #include "ardour/plugin_manager.h"
61 #include "ardour/profile.h"
62 #include "ardour/route_group.h"
63 #include "ardour/session_directory.h"
64 #include "ardour/session_route.h"
65 #include "ardour/session_state_utils.h"
66 #include "ardour/tempo.h"
67 #include "ardour/utils.h"
68
69 #include "control_protocol/control_protocol.h"
70
71 #include "ardour_ui.h"
72 #include "editor.h"
73 #include "keyboard.h"
74 #include "marker.h"
75 #include "playlist_selector.h"
76 #include "audio_region_view.h"
77 #include "rgb_macros.h"
78 #include "selection.h"
79 #include "audio_streamview.h"
80 #include "time_axis_view.h"
81 #include "audio_time_axis.h"
82 #include "utils.h"
83 #include "crossfade_view.h"
84 #include "editing.h"
85 #include "public_editor.h"
86 #include "crossfade_edit.h"
87 #include "canvas_impl.h"
88 #include "actions.h"
89 #include "sfdb_ui.h"
90 #include "gui_thread.h"
91 #include "simpleline.h"
92 #include "rhythm_ferret.h"
93 #include "actions.h"
94 #include "tempo_lines.h"
95 #include "analysis_window.h"
96 #include "bundle_manager.h"
97 #include "global_port_matrix.h"
98 #include "editor_drag.h"
99 #include "editor_group_tabs.h"
100 #include "automation_time_axis.h"
101 #include "editor_routes.h"
102 #include "midi_time_axis.h"
103 #include "mixer_strip.h"
104 #include "editor_route_groups.h"
105 #include "editor_regions.h"
106 #include "editor_snapshots.h"
107
108 #include "i18n.h"
109
110 #ifdef WITH_CMT
111 #include "imageframe_socket_handler.h"
112 #endif
113
114 using namespace std;
115 using namespace sigc;
116 using namespace ARDOUR;
117 using namespace PBD;
118 using namespace Gtk;
119 using namespace Glib;
120 using namespace Gtkmm2ext;
121 using namespace Editing;
122
123 using PBD::internationalize;
124 using PBD::atoi;
125
126 const double Editor::timebar_height = 15.0;
127
128 #include "editor_xpms"
129
130 static const gchar *_snap_type_strings[] = {
131         N_("CD Frames"),
132         N_("SMPTE Frames"),
133         N_("SMPTE Seconds"),
134         N_("SMPTE Minutes"),
135         N_("Seconds"),
136         N_("Minutes"),
137         N_("Beats/32"),
138         N_("Beats/16"),
139         N_("Beats/8"),
140         N_("Beats/4"),
141         N_("Beats/3"),
142         N_("Beats"),
143         N_("Bars"),
144         N_("Marks"),
145         N_("Region starts"),
146         N_("Region ends"),
147         N_("Region syncs"),
148         N_("Region bounds"),
149         0
150 };
151
152 static const gchar *_snap_mode_strings[] = {
153         N_("No Grid"),
154         N_("Grid"),
155         N_("Magnetic"),
156         0
157 };
158
159 static const gchar *_edit_point_strings[] = {
160         N_("Playhead"),
161         N_("Marker"),
162         N_("Mouse"),
163         0
164 };
165
166 static const gchar *_zoom_focus_strings[] = {
167         N_("Left"),
168         N_("Right"),
169         N_("Center"),
170         N_("Playhead"),
171         N_("Mouse"),
172         N_("Active Mark"),
173         0
174 };
175
176 #ifdef USE_RUBBERBAND
177 static const gchar *_rb_opt_strings[] = {
178         N_("Mushy"),
179         N_("Smooth"),
180         N_("Balanced multitimbral mixture"),
181         N_("Unpitched percussion with stable notes"),
182         N_("Crisp monophonic instrumental"),
183         N_("Unpitched solo percussion"),
184         0
185 };
186 #endif
187
188 /* Soundfile  drag-n-drop */
189
190 Gdk::Cursor* Editor::cross_hair_cursor = 0;
191 Gdk::Cursor* Editor::selector_cursor = 0;
192 Gdk::Cursor* Editor::trimmer_cursor = 0;
193 Gdk::Cursor* Editor::grabber_cursor = 0;
194 Gdk::Cursor* Editor::grabber_edit_point_cursor = 0;
195 Gdk::Cursor* Editor::zoom_cursor = 0;
196 Gdk::Cursor* Editor::time_fx_cursor = 0;
197 Gdk::Cursor* Editor::fader_cursor = 0;
198 Gdk::Cursor* Editor::speaker_cursor = 0;
199 Gdk::Cursor* Editor::midi_pencil_cursor = 0;
200 Gdk::Cursor* Editor::midi_select_cursor = 0;
201 Gdk::Cursor* Editor::midi_resize_cursor = 0;
202 Gdk::Cursor* Editor::midi_erase_cursor = 0;
203 Gdk::Cursor* Editor::wait_cursor = 0;
204 Gdk::Cursor* Editor::timebar_cursor = 0;
205 Gdk::Cursor* Editor::transparent_cursor = 0;
206
207 void
208 show_me_the_size (Requisition* r, const char* what)
209 {
210         cerr << "size of " << what << " = " << r->width << " x " << r->height << endl;
211 }
212
213 Editor::Editor ()
214         : 
215           /* time display buttons */
216
217           minsec_label (_("Mins:Secs")),
218           bbt_label (_("Bars:Beats")),
219           smpte_label (_("Timecode")),
220           frame_label (_("Samples")),
221           tempo_label (_("Tempo")),
222           meter_label (_("Meter")),
223           mark_label (_("Location Markers")),
224           range_mark_label (_("Range Markers")),
225           transport_mark_label (_("Loop/Punch Ranges")),
226           cd_mark_label (_("CD Markers")),
227           edit_packer (4, 4, true),
228
229           /* the values here don't matter: layout widgets
230              reset them as needed.
231           */
232
233           vertical_adjustment (0.0, 0.0, 10.0, 400.0),
234           horizontal_adjustment (0.0, 0.0, 20.0, 1200.0),
235
236           /* tool bar related */
237
238           edit_point_clock (X_("editpoint"), false, X_("EditPointClock"), true),
239           zoom_range_clock (X_("zoomrange"), false, X_("ZoomRangeClock"), true, true),
240           
241           toolbar_selection_clock_table (2,3),
242           
243           automation_mode_button (_("mode")),
244           global_automation_button (_("automation")),
245
246           midi_panic_button (_("Panic")),
247           midi_tools_tearoff (0),
248
249 #ifdef WITH_CMT
250           image_socket_listener(0),
251 #endif
252
253           /* nudge */
254
255           nudge_clock (X_("nudge"), false, X_("NudgeClock"), true, true),
256           meters_running(false),
257           _pending_locate_request (false)
258
259 {
260         constructed = false;
261
262         /* we are a singleton */
263
264         PublicEditor::_instance = this;
265
266         session = 0;
267         _have_idled = false;
268
269         selection = new Selection (this);
270         cut_buffer = new Selection (this);
271
272         clicked_regionview = 0;
273         clicked_axisview = 0;
274         clicked_routeview = 0;
275         clicked_crossfadeview = 0;
276         clicked_control_point = 0;
277         last_update_frame = 0;
278         _drag = 0;
279         current_mixer_strip = 0;
280         current_bbt_points = 0;
281         tempo_lines = 0;
282         
283         snap_type_strings =  I18N (_snap_type_strings);
284         snap_mode_strings =  I18N (_snap_mode_strings);
285         zoom_focus_strings = I18N (_zoom_focus_strings);
286         edit_point_strings = I18N (_edit_point_strings);
287 #ifdef USE_RUBBERBAND
288         rb_opt_strings = I18N (_rb_opt_strings);
289 #endif
290         
291         snap_threshold = 5.0;
292         bbt_beat_subdivision = 4;
293         _canvas_width = 0;
294         _canvas_height = 0;
295         last_autoscroll_x = 0;
296         last_autoscroll_y = 0;
297         autoscroll_active = false;
298         autoscroll_timeout_tag = -1;
299         interthread_progress_window = 0;
300         logo_item = 0;
301
302         analysis_window = 0;
303
304         current_interthread_info = 0;
305         _show_measures = true;
306         _show_waveforms_recording = true;
307         show_gain_after_trim = false;
308         verbose_cursor_on = true;
309         route_removal = false;
310         last_item_entered = 0;
311         last_item_entered_n = 0;
312
313         have_pending_keyboard_selection = false;
314         _follow_playhead = true;
315         _xfade_visibility = true;
316         editor_ruler_menu = 0;
317         no_ruler_shown_update = false;
318         marker_menu = 0;
319         start_end_marker_menu = 0;
320         range_marker_menu = 0;
321         marker_menu_item = 0;
322         tm_marker_menu = 0;
323         transport_marker_menu = 0;
324         new_transport_marker_menu = 0;
325         editor_mixer_strip_width = Wide;
326         show_editor_mixer_when_tracks_arrive = false;
327         region_edit_menu_split_multichannel_item = 0;
328         region_edit_menu_split_item = 0;
329         temp_location = 0;
330         leftmost_frame = 0;
331         ignore_mouse_mode_toggle = false;
332         ignore_midi_edit_mode_toggle = false;
333         current_stepping_trackview = 0;
334         entered_track = 0;
335         entered_regionview = 0;
336         entered_marker = 0;
337         clear_entered_track = false;
338         _new_regionviews_show_envelope = false;
339         current_timefx = 0;
340         playhead_cursor = 0;
341         button_release_can_deselect = true;
342         _dragging_playhead = false;
343         _dragging_edit_point = false;
344         _dragging_hscrollbar = false;
345         select_new_marker = false;
346         zoomed_to_region = false;
347         rhythm_ferret = 0;
348         _bundle_manager = 0;
349         for (ARDOUR::DataType::iterator i = ARDOUR::DataType::begin(); i != ARDOUR::DataType::end(); ++i) {
350                 _global_port_matrix[*i] = 0;
351         }
352         allow_vertical_scroll = false;
353         no_save_visual = false;
354         resize_idle_id = -1;
355
356         scrubbing_direction = 0;
357
358         sfbrowser = 0;
359
360         location_marker_color = ARDOUR_UI::config()->canvasvar_LocationMarker.get();
361         location_range_color = ARDOUR_UI::config()->canvasvar_LocationRange.get();
362         location_cd_marker_color = ARDOUR_UI::config()->canvasvar_LocationCDMarker.get();
363         location_loop_color = ARDOUR_UI::config()->canvasvar_LocationLoop.get();
364         location_punch_color = ARDOUR_UI::config()->canvasvar_LocationPunch.get();
365
366         set_midi_edit_mode (MidiEditPencil, true);
367         _edit_point = EditAtMouse;
368         set_mouse_mode (MouseObject, true);
369
370         frames_per_unit = 2048; /* too early to use reset_zoom () */
371         reset_hscrollbar_stepping ();
372         
373         zoom_focus = ZoomFocusLeft;
374         set_zoom_focus (ZoomFocusLeft);
375         zoom_range_clock.ValueChanged.connect (mem_fun(*this, &Editor::zoom_adjustment_changed));
376
377         bbt_label.set_name ("EditorTimeButton");
378         bbt_label.set_size_request (-1, (int)timebar_height);
379         bbt_label.set_alignment (1.0, 0.5);
380         bbt_label.set_padding (5,0);
381         bbt_label.hide ();
382         bbt_label.set_no_show_all();
383         minsec_label.set_name ("EditorTimeButton");
384         minsec_label.set_size_request (-1, (int)timebar_height);
385         minsec_label.set_alignment (1.0, 0.5);
386         minsec_label.set_padding (5,0);
387         minsec_label.hide ();
388         minsec_label.set_no_show_all();
389         smpte_label.set_name ("EditorTimeButton");
390         smpte_label.set_size_request (-1, (int)timebar_height);
391         smpte_label.set_alignment (1.0, 0.5);
392         smpte_label.set_padding (5,0);
393         smpte_label.hide ();
394         smpte_label.set_no_show_all();
395         frame_label.set_name ("EditorTimeButton");
396         frame_label.set_size_request (-1, (int)timebar_height);
397         frame_label.set_alignment (1.0, 0.5);
398         frame_label.set_padding (5,0);
399         frame_label.hide ();
400         frame_label.set_no_show_all();
401
402         tempo_label.set_name ("EditorTimeButton");
403         tempo_label.set_size_request (-1, (int)timebar_height);
404         tempo_label.set_alignment (1.0, 0.5);
405         tempo_label.set_padding (5,0);
406         tempo_label.hide();
407         tempo_label.set_no_show_all();
408         meter_label.set_name ("EditorTimeButton");
409         meter_label.set_size_request (-1, (int)timebar_height);
410         meter_label.set_alignment (1.0, 0.5);
411         meter_label.set_padding (5,0);
412         meter_label.hide();
413         meter_label.set_no_show_all();
414         mark_label.set_name ("EditorTimeButton");
415         mark_label.set_size_request (-1, (int)timebar_height);
416         mark_label.set_alignment (1.0, 0.5);
417         mark_label.set_padding (5,0);
418         mark_label.hide();
419         mark_label.set_no_show_all();
420         cd_mark_label.set_name ("EditorTimeButton");
421         cd_mark_label.set_size_request (-1, (int)timebar_height);
422         cd_mark_label.set_alignment (1.0, 0.5);
423         cd_mark_label.set_padding (5,0);
424         cd_mark_label.hide();
425         cd_mark_label.set_no_show_all();
426         range_mark_label.set_name ("EditorTimeButton");
427         range_mark_label.set_size_request (-1, (int)timebar_height);
428         range_mark_label.set_alignment (1.0, 0.5);
429         range_mark_label.set_padding (5,0);
430         range_mark_label.hide();
431         range_mark_label.set_no_show_all();
432         transport_mark_label.set_name ("EditorTimeButton");
433         transport_mark_label.set_size_request (-1, (int)timebar_height);
434         transport_mark_label.set_alignment (1.0, 0.5);
435         transport_mark_label.set_padding (5,0);
436         transport_mark_label.hide();
437         transport_mark_label.set_no_show_all();
438
439         initialize_rulers ();
440         _summary = new EditorSummary (this);
441         initialize_canvas ();
442
443         selection->TimeChanged.connect (mem_fun(*this, &Editor::time_selection_changed));
444         selection->TracksChanged.connect (mem_fun(*this, &Editor::track_selection_changed));
445         editor_regions_selection_changed_connection = selection->RegionsChanged.connect (mem_fun(*this, &Editor::region_selection_changed));
446         selection->PointsChanged.connect (mem_fun(*this, &Editor::point_selection_changed));
447         selection->MarkersChanged.connect (mem_fun(*this, &Editor::marker_selection_changed));
448
449         edit_controls_vbox.set_spacing (0);
450         horizontal_adjustment.signal_value_changed().connect (mem_fun(*this, &Editor::scroll_canvas_horizontally), false);
451         vertical_adjustment.signal_value_changed().connect (mem_fun(*this, &Editor::tie_vertical_scrolling), true);
452         track_canvas->signal_map_event().connect (mem_fun (*this, &Editor::track_canvas_map_handler));
453
454         HBox* h = manage (new HBox);
455         _group_tabs = new EditorGroupTabs (this);
456         h->pack_start (*_group_tabs, PACK_SHRINK);
457         h->pack_start (edit_controls_vbox);
458         controls_layout.add (*h);
459         
460         controls_layout.set_name ("EditControlsBase");
461         controls_layout.add_events (Gdk::SCROLL_MASK);
462         controls_layout.signal_scroll_event().connect (mem_fun(*this, &Editor::control_layout_scroll), false);
463         
464         controls_layout.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
465         controls_layout.signal_button_release_event().connect (mem_fun(*this, &Editor::edit_controls_button_release));
466         controls_layout_size_request_connection = controls_layout.signal_size_request().connect (mem_fun (*this, &Editor::controls_layout_size_request));
467
468         edit_vscrollbar.set_adjustment (vertical_adjustment);
469         edit_hscrollbar.set_adjustment (horizontal_adjustment);
470
471         edit_hscrollbar.signal_button_press_event().connect (mem_fun(*this, &Editor::hscrollbar_button_press), false);
472         edit_hscrollbar.signal_button_release_event().connect (mem_fun(*this, &Editor::hscrollbar_button_release), false);
473         edit_hscrollbar.signal_size_allocate().connect (mem_fun(*this, &Editor::hscrollbar_allocate));
474
475         edit_hscrollbar.set_name ("EditorHScrollbar");
476
477         build_cursors ();
478         setup_toolbar ();
479         setup_midi_toolbar ();
480
481         edit_point_clock.ValueChanged.connect (mem_fun(*this, &Editor::edit_point_clock_changed));
482         
483         ArdourCanvas::Canvas* time_pad = manage(new ArdourCanvas::Canvas());
484         ArdourCanvas::SimpleLine* pad_line_1 = manage(new ArdourCanvas::SimpleLine(*time_pad->root(),
485                         0.0, 1.0, 100.0, 1.0));
486         pad_line_1->property_color_rgba() = 0xFF0000FF;
487         pad_line_1->show();
488         time_pad->show();
489
490         time_canvas_vbox.set_size_request (-1, (int)(timebar_height * visible_timebars) + 2);
491         time_canvas_vbox.set_size_request (-1, -1);
492
493         ruler_label_event_box.add (ruler_label_vbox);   
494         ruler_label_event_box.set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
495         ruler_label_event_box.signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_label_button_release));
496
497         time_button_event_box.add (time_button_vbox);
498         time_button_event_box.set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
499         time_button_event_box.signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_label_button_release));
500
501         /* these enable us to have a dedicated window (for cursor setting, etc.) 
502            for the canvas areas.
503         */
504
505         track_canvas_event_box.add (*track_canvas);
506
507         time_canvas_event_box.add (time_canvas_vbox);
508         time_canvas_event_box.set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
509
510         edit_packer.set_col_spacings (0);
511         edit_packer.set_row_spacings (0);
512         edit_packer.set_homogeneous (false);
513         edit_packer.set_border_width (0);
514         edit_packer.set_name ("EditorWindow");
515
516         edit_packer.attach (zoom_vbox,               0, 1, 0, 2,    SHRINK,        FILL, 0, 0);
517         /* labels for the rulers */
518         edit_packer.attach (ruler_label_event_box,   1, 2, 0, 1,    FILL,        SHRINK, 0, 0);
519         /* labels for the marker "tracks" */
520         edit_packer.attach (time_button_event_box,   1, 2, 1, 2,    FILL,        SHRINK, 0, 0);
521         /* the rulers */
522         edit_packer.attach (time_canvas_event_box,   2, 3, 0, 1,    FILL|EXPAND, FILL, 0, 0);
523         /* track controls */
524         edit_packer.attach (controls_layout,         0, 2, 2, 3,    FILL,        FILL|EXPAND, 0, 0);
525         /* main canvas */
526         edit_packer.attach (track_canvas_event_box,  2, 3, 1, 3,    FILL|EXPAND, FILL|EXPAND, 0, 0);
527
528         bottom_hbox.set_border_width (2);
529         bottom_hbox.set_spacing (3);
530
531         _route_groups = new EditorRouteGroups (this);
532         _routes = new EditorRoutes (this);
533         _regions = new EditorRegions (this);
534         _snapshots = new EditorSnapshots (this);
535
536         named_selection_scroller.add (named_selection_display);
537         named_selection_scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
538
539         named_selection_model = TreeStore::create (named_selection_columns);
540         named_selection_display.set_model (named_selection_model);
541         named_selection_display.append_column (_("Chunks"), named_selection_columns.text);
542         named_selection_display.set_headers_visible (false);
543         named_selection_display.set_size_request (100, -1);
544         named_selection_display.set_name ("NamedSelectionDisplay");
545         
546         named_selection_display.get_selection()->set_mode (SELECTION_SINGLE);
547         named_selection_display.set_size_request (100, -1);
548         named_selection_display.signal_button_release_event().connect (mem_fun(*this, &Editor::named_selection_display_button_release), false);
549         named_selection_display.signal_key_release_event().connect (mem_fun(*this, &Editor::named_selection_display_key_release), false);
550         named_selection_display.get_selection()->signal_changed().connect (mem_fun (*this, &Editor::named_selection_display_selection_changed));
551
552         Gtk::Label* nlabel;
553
554         nlabel = manage (new Label (_("Regions")));
555         nlabel->set_angle (-90);
556         the_notebook.append_page (_regions->widget (), *nlabel);
557         nlabel = manage (new Label (_("Tracks/Busses")));
558         nlabel->set_angle (-90);
559         the_notebook.append_page (_routes->widget (), *nlabel);
560         nlabel = manage (new Label (_("Snapshots")));
561         nlabel->set_angle (-90);
562         the_notebook.append_page (_snapshots->widget (), *nlabel);
563         nlabel = manage (new Label (_("Route Groups")));
564         nlabel->set_angle (-90);
565         the_notebook.append_page (_route_groups->widget (), *nlabel);
566         
567         if (!Profile->get_sae()) {
568                 nlabel = manage (new Label (_("Chunks")));
569                 nlabel->set_angle (-90);
570                 the_notebook.append_page (named_selection_scroller, *nlabel);
571         }
572
573         the_notebook.set_show_tabs (true);
574         the_notebook.set_scrollable (true);
575         the_notebook.popup_enable ();
576         the_notebook.set_tab_pos (Gtk::POS_RIGHT);
577         the_notebook.show_all ();
578
579         post_maximal_editor_width = 0;
580         post_maximal_pane_position = 0;
581
582         VPaned *editor_summary_pane = manage(new VPaned());
583         editor_summary_pane->pack1(edit_packer);
584         editor_summary_pane->pack2(*_summary);
585
586         edit_pane.pack1 (*editor_summary_pane, true, true);
587         edit_pane.pack2 (the_notebook, false, true);
588         
589         edit_pane.signal_size_allocate().connect (bind (mem_fun(*this, &Editor::pane_allocation_handler), static_cast<Paned*> (&edit_pane)));
590
591         top_hbox.pack_start (toolbar_frame, false, true);
592         top_hbox.pack_start (midi_toolbar_frame, false, true);
593
594         HBox *hbox = manage (new HBox);
595         hbox->pack_start (edit_pane, true, true);
596
597         global_vpacker.pack_start (top_hbox, false, false);
598         global_vpacker.pack_start (*hbox, true, true);
599
600         global_hpacker.pack_start (global_vpacker, true, true);
601
602         set_name ("EditorWindow");
603         add_accel_group (ActionManager::ui_manager->get_accel_group());
604
605         status_bar_hpacker.show ();
606
607         vpacker.pack_end (status_bar_hpacker, false, false);
608         vpacker.pack_end (global_hpacker, true, true);
609
610         /* register actions now so that set_state() can find them and set toggles/checks etc */
611         
612         register_actions ();
613
614         snap_type = SnapToBeat;
615         set_snap_to (snap_type);
616         snap_mode = SnapOff;
617         set_snap_mode (snap_mode);
618         set_edit_point_preference (EditAtMouse, true);
619
620         XMLNode* node = ARDOUR_UI::instance()->editor_settings();
621         set_state (*node);
622
623         _playlist_selector = new PlaylistSelector();
624         _playlist_selector->signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (_playlist_selector)));
625
626         RegionView::RegionViewGoingAway.connect (mem_fun(*this, &Editor::catch_vanishing_regionview));
627
628         /* nudge stuff */
629
630         nudge_forward_button.add (*(manage (new Image (::get_icon("nudge_right")))));
631         nudge_backward_button.add (*(manage (new Image (::get_icon("nudge_left")))));
632
633         ARDOUR_UI::instance()->tooltips().set_tip (nudge_forward_button, _("Nudge Region/Selection Forwards"));
634         ARDOUR_UI::instance()->tooltips().set_tip (nudge_backward_button, _("Nudge Region/Selection Backwards"));
635
636         nudge_forward_button.set_name ("TransportButton");
637         nudge_backward_button.set_name ("TransportButton");
638
639         fade_context_menu.set_name ("ArdourContextMenu");
640
641         /* icons, titles, WM stuff */
642
643         list<Glib::RefPtr<Gdk::Pixbuf> > window_icons;
644         Glib::RefPtr<Gdk::Pixbuf> icon;
645
646         if ((icon = ::get_icon ("ardour_icon_16px")) != 0) {
647                 window_icons.push_back (icon);
648         }
649         if ((icon = ::get_icon ("ardour_icon_22px")) != 0) {
650                 window_icons.push_back (icon);
651         }
652         if ((icon = ::get_icon ("ardour_icon_32px")) != 0) {
653                 window_icons.push_back (icon);
654         }
655         if ((icon = ::get_icon ("ardour_icon_48px")) != 0) {
656                 window_icons.push_back (icon);
657         }
658         if (!window_icons.empty()) {
659                 set_icon_list (window_icons);
660                 set_default_icon_list (window_icons);
661         }
662
663         WindowTitle title(Glib::get_application_name());
664         title += _("Editor");
665         set_title (title.get_string());
666         set_wmclass (X_("ardour_editor"), "Ardour");
667
668         add (vpacker);
669         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
670
671         signal_configure_event().connect (mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::configure_handler));
672         signal_delete_event().connect (mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::exit_on_main_window_close));
673
674         /* allow external control surfaces/protocols to do various things */
675
676         ControlProtocol::ZoomToSession.connect (mem_fun (*this, &Editor::temporal_zoom_session));
677         ControlProtocol::ZoomIn.connect (bind (mem_fun (*this, &Editor::temporal_zoom_step), false));
678         ControlProtocol::ZoomOut.connect (bind (mem_fun (*this, &Editor::temporal_zoom_step), true));
679         ControlProtocol::ScrollTimeline.connect (mem_fun (*this, &Editor::control_scroll));
680         BasicUI::AccessAction.connect (mem_fun (*this, &Editor::access_action));
681
682         Config->ParameterChanged.connect (mem_fun (*this, &Editor::parameter_changed));
683
684         _last_normalization_value = 0;
685
686         constructed = true;
687         instant_save ();
688 }
689
690 Editor::~Editor()
691 {
692 #ifdef WITH_CMT
693         if(image_socket_listener) {
694                 if(image_socket_listener->is_connected())
695                 {
696                         image_socket_listener->close_connection() ;
697                 }
698                 
699                 delete image_socket_listener ;
700                 image_socket_listener = 0 ;
701         }
702 #endif
703
704         delete _routes;
705         delete _route_groups;
706         delete track_canvas;
707         delete _drag;
708 }
709
710 void
711 Editor::add_toplevel_controls (Container& cont)
712 {
713         vpacker.pack_start (cont, false, false);
714         cont.show_all ();
715 }
716
717 void
718 Editor::catch_vanishing_regionview (RegionView *rv)
719 {
720         /* note: the selection will take care of the vanishing
721            audioregionview by itself.
722         */
723
724         if (_drag && rv->get_canvas_group() == _drag->item() && !_drag->ending()) {
725                 _drag->end_grab (0);
726                 delete _drag;
727                 _drag = 0;
728         }
729
730         if (clicked_regionview == rv) {
731                 clicked_regionview = 0;
732         }
733
734         if (entered_regionview == rv) {
735                 set_entered_regionview (0);
736         }
737 }
738
739 void
740 Editor::set_entered_regionview (RegionView* rv)
741 {
742         if (rv == entered_regionview) {
743                 return;
744         }
745
746         if (entered_regionview) {
747                 entered_regionview->exited ();
748         }
749
750         if ((entered_regionview = rv) != 0) {
751                 entered_regionview->entered ();
752         }
753 }
754
755 void
756 Editor::set_entered_track (TimeAxisView* tav)
757 {
758         if (entered_track) {
759                 entered_track->exited ();
760         }
761
762         if ((entered_track = tav) != 0) {
763                 entered_track->entered ();
764         }
765 }
766
767 void
768 Editor::show_window ()
769 {
770         if (! is_visible ()) {
771                 show_all ();
772
773                 /* re-hide editor list if necessary */
774                 editor_list_button_toggled ();
775
776                 /* re-hide summary widget if necessary */
777                 parameter_changed ("show-summary");
778
779                 parameter_changed ("show-edit-group-tabs");
780
781                 /* now reset all audio_time_axis heights, because widgets might need
782                    to be re-hidden
783                 */
784
785                 TimeAxisView *tv;
786         
787                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
788                         tv = (static_cast<TimeAxisView*>(*i));
789                         tv->reset_height ();
790                 }
791         }
792
793         present ();
794 }
795
796 void
797 Editor::instant_save ()
798 {
799         if (!constructed || !ARDOUR_UI::instance()->session_loaded) {
800                 return;
801         }
802
803         if (session) {
804                 session->add_instant_xml(get_state());
805         } else {
806                 Config->add_instant_xml(get_state());
807         }
808 }
809
810 void
811 Editor::edit_point_clock_changed()
812 {
813         if (_dragging_edit_point) {
814                 return;
815         }
816
817         if (selection->markers.empty()) {
818                 return;
819         }
820
821         bool ignored;
822         Location* loc = find_location_from_marker (selection->markers.front(), ignored);
823
824         if (!loc) {
825                 return;
826         }
827
828         loc->move_to (edit_point_clock.current_time());
829 }
830
831 void
832 Editor::zoom_adjustment_changed ()
833 {
834         if (session == 0) {
835                 return;
836         }
837
838         double fpu = zoom_range_clock.current_duration() / _canvas_width;
839
840         if (fpu < 1.0) {
841                 fpu = 1.0;
842                 zoom_range_clock.set ((nframes64_t) floor (fpu * _canvas_width));
843         } else if (fpu > session->current_end_frame() / _canvas_width) {
844                 fpu = session->current_end_frame() / _canvas_width;
845                 zoom_range_clock.set ((nframes64_t) floor (fpu * _canvas_width));
846         }
847         
848         temporal_zoom (fpu);
849 }
850
851 void
852 Editor::control_scroll (float fraction)
853 {
854         ENSURE_GUI_THREAD(bind (mem_fun (*this, &Editor::control_scroll), fraction));
855
856         if (!session) {
857                 return;
858         }
859
860         double step = fraction * current_page_frames();
861
862         /*
863                 _control_scroll_target is an optional<T>
864         
865                 it acts like a pointer to an nframes64_t, with
866                 a operator conversion to boolean to check
867                 that it has a value could possibly use
868                 playhead_cursor->current_frame to store the
869                 value and a boolean in the class to know
870                 when it's out of date
871         */
872
873         if (!_control_scroll_target) {
874                 _control_scroll_target = session->transport_frame();
875                 _dragging_playhead = true;
876         }
877
878         if ((fraction < 0.0f) && (*_control_scroll_target < (nframes64_t) fabs(step))) {
879                 *_control_scroll_target = 0;
880         } else if ((fraction > 0.0f) && (max_frames - *_control_scroll_target < step)) {
881                 *_control_scroll_target = max_frames - (current_page_frames()*2); // allow room for slop in where the PH is on the screen
882         } else {
883                 *_control_scroll_target += (nframes64_t) floor (step);
884         }
885
886         /* move visuals, we'll catch up with it later */
887
888         playhead_cursor->set_position (*_control_scroll_target);
889         UpdateAllTransportClocks (*_control_scroll_target);
890         
891         if (*_control_scroll_target > (current_page_frames() / 2)) {
892                 /* try to center PH in window */
893                 reset_x_origin (*_control_scroll_target - (current_page_frames()/2));
894         } else {
895                 reset_x_origin (0);
896         }
897
898         /*
899                 Now we do a timeout to actually bring the session to the right place
900                 according to the playhead. This is to avoid reading disk buffers on every
901                 call to control_scroll, which is driven by ScrollTimeline and therefore
902                 probably by a control surface wheel which can generate lots of events.
903         */
904         /* cancel the existing timeout */
905
906         control_scroll_connection.disconnect ();
907
908         /* add the next timeout */
909
910         control_scroll_connection = Glib::signal_timeout().connect (bind (mem_fun (*this, &Editor::deferred_control_scroll), *_control_scroll_target), 250);
911 }
912
913 bool
914 Editor::deferred_control_scroll (nframes64_t target)
915 {
916         session->request_locate (*_control_scroll_target, session->transport_rolling());
917         // reset for next stream
918         _control_scroll_target = boost::none;
919         _dragging_playhead = false;
920         return false;
921 }
922
923 void
924 Editor::access_action (std::string action_group, std::string action_item)
925 {
926         if (!session) {
927                 return;
928         }
929
930         ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::access_action), action_group, action_item));
931
932         RefPtr<Action> act;
933         act = ActionManager::get_action( action_group.c_str(), action_item.c_str() );
934
935         if (act) {
936                 act->activate();
937         }
938                 
939
940 }
941
942 void
943 Editor::on_realize ()
944 {
945         Window::on_realize ();
946         Realized ();
947 }
948
949 void
950 Editor::start_scrolling ()
951 {
952         scroll_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect 
953                 (mem_fun(*this, &Editor::update_current_screen));
954
955 }
956
957 void
958 Editor::stop_scrolling ()
959 {
960         scroll_connection.disconnect ();
961 }
962
963 void
964 Editor::map_position_change (nframes64_t frame)
965 {
966         ENSURE_GUI_THREAD (bind (mem_fun(*this, &Editor::map_position_change), frame));
967
968         if (session == 0 || !_follow_playhead) {
969                 return;
970         }
971
972         center_screen (frame);
973         playhead_cursor->set_position (frame);
974 }       
975
976 void
977 Editor::center_screen (nframes64_t frame)
978 {
979         double page = _canvas_width * frames_per_unit;
980
981         /* if we're off the page, then scroll.
982          */
983         
984         if (frame < leftmost_frame || frame >= leftmost_frame + page) {
985                 center_screen_internal (frame, page);
986         }
987 }
988
989 void
990 Editor::center_screen_internal (nframes64_t frame, float page)
991 {
992         page /= 2;
993                 
994         if (frame > page) {
995                 frame -= (nframes64_t) page;
996         } else {
997                 frame = 0;
998         }
999
1000         reset_x_origin (frame);
1001 }
1002
1003 void
1004 Editor::handle_new_duration ()
1005 {
1006         if (!session) {
1007                 return;
1008         }
1009
1010         ENSURE_GUI_THREAD (mem_fun (*this, &Editor::handle_new_duration));
1011         nframes64_t new_end = session->current_end_frame() + (nframes64_t) floorf (current_page_frames() * 0.10f);
1012
1013         horizontal_adjustment.set_upper (new_end / frames_per_unit);
1014         horizontal_adjustment.set_page_size (current_page_frames()/frames_per_unit);
1015
1016         if (horizontal_adjustment.get_value() + _canvas_width > horizontal_adjustment.get_upper()) {
1017                 horizontal_adjustment.set_value (horizontal_adjustment.get_upper() - _canvas_width);
1018         }
1019         //cerr << "Editor::handle_new_duration () called ha v:l:u:ps:lcf = " << horizontal_adjustment.get_value() << ":" << horizontal_adjustment.get_lower() << ":" << horizontal_adjustment.get_upper() << ":" << horizontal_adjustment.get_page_size() << ":" << endl;//DEBUG
1020 }
1021
1022 void
1023 Editor::update_title_s (const string & snap_name)
1024 {
1025         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::update_title_s), snap_name));
1026         
1027         update_title ();
1028 }
1029
1030 void
1031 Editor::update_title ()
1032 {
1033         ENSURE_GUI_THREAD (mem_fun(*this, &Editor::update_title));
1034
1035         if (session) {
1036                 bool dirty = session->dirty();
1037
1038                 string session_name;
1039
1040                 if (session->snap_name() != session->name()) {
1041                         session_name = session->snap_name();
1042                 } else {
1043                         session_name = session->name();
1044                 }
1045
1046                 if (dirty) {
1047                         session_name = "*" + session_name;
1048                 }
1049
1050                 WindowTitle title(session_name);
1051                 title += Glib::get_application_name();
1052                 set_title (title.get_string());
1053         }
1054 }
1055
1056 void
1057 Editor::connect_to_session (Session *t)
1058 {
1059         session = t;
1060
1061         compute_fixed_ruler_scale ();
1062
1063         /* there are never any selected regions at startup */
1064
1065         sensitize_the_right_region_actions (false);
1066
1067         XMLNode* node = ARDOUR_UI::instance()->editor_settings();
1068         set_state (*node);
1069
1070         /* catch up with the playhead */
1071
1072         session->request_locate (playhead_cursor->current_frame);
1073
1074         update_title ();
1075
1076         session->GoingAway.connect (mem_fun(*this, &Editor::session_going_away));
1077         session->history().Changed.connect (mem_fun (*this, &Editor::history_changed));
1078
1079         /* These signals can all be emitted by a non-GUI thread. Therefore the
1080            handlers for them must not attempt to directly interact with the GUI,
1081            but use Gtkmm2ext::UI::instance()->call_slot();
1082         */
1083
1084         session_connections.push_back (session->TransportStateChange.connect (mem_fun(*this, &Editor::map_transport_state)));
1085         session_connections.push_back (session->PositionChanged.connect (mem_fun(*this, &Editor::map_position_change)));
1086         session_connections.push_back (session->RouteAdded.connect (mem_fun(*this, &Editor::handle_new_route)));
1087         session_connections.push_back (session->DurationChanged.connect (mem_fun(*this, &Editor::handle_new_duration)));
1088         session_connections.push_back (session->NamedSelectionAdded.connect (mem_fun(*this, &Editor::handle_new_named_selection)));
1089         session_connections.push_back (session->NamedSelectionRemoved.connect (mem_fun(*this, &Editor::handle_new_named_selection)));
1090         session_connections.push_back (session->DirtyChanged.connect (mem_fun(*this, &Editor::update_title)));
1091         session_connections.push_back (session->StateSaved.connect (mem_fun(*this, &Editor::update_title_s)));
1092         session_connections.push_back (session->AskAboutPlaylistDeletion.connect (mem_fun(*this, &Editor::playlist_deletion_dialog)));
1093
1094         session_connections.push_back (session->SMPTEOffsetChanged.connect (mem_fun(*this, &Editor::update_just_smpte)));
1095
1096         session_connections.push_back (session->tempo_map().StateChanged.connect (mem_fun(*this, &Editor::tempo_map_changed)));
1097
1098         session_connections.push_back (session->Located.connect (mem_fun (*this, &Editor::located)));
1099         session_connections.push_back (session->config.ParameterChanged.connect (mem_fun (*this, &Editor::parameter_changed)));
1100
1101         edit_point_clock.set_mode(AudioClock::BBT);
1102         edit_point_clock.set_session (session);
1103         zoom_range_clock.set_session (session);
1104         _playlist_selector->set_session (session);
1105         nudge_clock.set_session (session);
1106         if (Profile->get_sae()) {
1107                 BBT_Time bbt;
1108                 bbt.bars = 0;
1109                 bbt.beats = 0;
1110                 bbt.ticks = 120;
1111                 nframes_t pos = session->tempo_map().bbt_duration_at (0, bbt, 1);
1112                 nudge_clock.set_mode(AudioClock::BBT);
1113                 nudge_clock.set (pos, true, 0, AudioClock::BBT);
1114                 
1115         } else {
1116                 nudge_clock.set (session->frame_rate() * 5, true, 0, AudioClock::SMPTE); // default of 5 seconds
1117         }
1118
1119         playhead_cursor->canvas_item.show ();
1120
1121         if (rhythm_ferret) {
1122                 rhythm_ferret->set_session (session);
1123         }
1124
1125         if (analysis_window != 0)
1126                 analysis_window->set_session (session);
1127
1128         Location* loc = session->locations()->auto_loop_location();
1129         if (loc == 0) {
1130                 loc = new Location (0, session->current_end_frame(), _("Loop"),(Location::Flags) (Location::IsAutoLoop | Location::IsHidden));
1131                 if (loc->start() == loc->end()) {
1132                         loc->set_end (loc->start() + 1);
1133                 }
1134                 session->locations()->add (loc, false);
1135                 session->set_auto_loop_location (loc);
1136         } else {
1137                 // force name
1138                 loc->set_name (_("Loop"));
1139         }
1140         
1141         loc = session->locations()->auto_punch_location();
1142         if (loc == 0) {
1143                 loc = new Location (0, session->current_end_frame(), _("Punch"), (Location::Flags) (Location::IsAutoPunch | Location::IsHidden));
1144                 if (loc->start() == loc->end()) {
1145                         loc->set_end (loc->start() + 1);
1146                 }
1147                 session->locations()->add (loc, false);
1148                 session->set_auto_punch_location (loc);
1149         } else {
1150                 // force name
1151                 loc->set_name (_("Punch"));
1152         }
1153
1154         Config->map_parameters (mem_fun (*this, &Editor::parameter_changed));
1155         session->config.map_parameters (mem_fun (*this, &Editor::parameter_changed));
1156         
1157         session->StateSaved.connect (mem_fun(*this, &Editor::session_state_saved));
1158         
1159         refresh_location_display ();
1160         session->locations()->added.connect (mem_fun(*this, &Editor::add_new_location));
1161         session->locations()->removed.connect (mem_fun(*this, &Editor::location_gone));
1162         session->locations()->changed.connect (mem_fun(*this, &Editor::refresh_location_display));
1163         session->locations()->StateChanged.connect (mem_fun(*this, &Editor::refresh_location_display_s));
1164         session->locations()->end_location()->changed.connect (mem_fun(*this, &Editor::end_location_changed));
1165
1166         if (sfbrowser) {
1167                 sfbrowser->set_session (session);
1168         }
1169
1170         handle_new_duration ();
1171
1172         redisplay_named_selections ();
1173
1174         restore_ruler_visibility ();
1175         //tempo_map_changed (Change (0));
1176         session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
1177
1178         _routes->initial_display ();
1179
1180         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
1181                 (static_cast<TimeAxisView*>(*i))->set_samples_per_unit (frames_per_unit);
1182         }
1183
1184         start_scrolling ();
1185
1186         switch (snap_type) {
1187         case SnapToRegionStart:
1188         case SnapToRegionEnd:
1189         case SnapToRegionSync:
1190         case SnapToRegionBoundary:
1191                 build_region_boundary_cache ();
1192                 break;
1193
1194         default:
1195                 break;
1196         }
1197
1198         /* register for undo history */
1199         session->register_with_memento_command_factory(_id, this);
1200
1201         _summary->connect_to_session (session);
1202         _group_tabs->connect_to_session (session);
1203         _route_groups->connect_to_session (session);
1204         _regions->connect_to_session (session);
1205         _snapshots->connect_to_session (session);
1206         
1207         start_updating ();
1208 }
1209
1210 void
1211 Editor::build_cursors ()
1212 {
1213         using namespace Gdk;
1214         
1215         Gdk::Color mbg ("#000000" ); /* Black */
1216         Gdk::Color mfg ("#0000ff" ); /* Blue. */
1217
1218         {
1219                 RefPtr<Bitmap> source, mask;
1220                 source = Bitmap::create (mag_bits, mag_width, mag_height);
1221                 mask = Bitmap::create (magmask_bits, mag_width, mag_height);
1222                 zoom_cursor = new Gdk::Cursor (source, mask, mfg, mbg, mag_x_hot, mag_y_hot);
1223         }
1224
1225         Gdk::Color fbg ("#ffffff" );
1226         Gdk::Color ffg  ("#000000" );
1227         
1228         {
1229                 RefPtr<Bitmap> source, mask;
1230                 
1231                 source = Bitmap::create (fader_cursor_bits, fader_cursor_width, fader_cursor_height);
1232                 mask = Bitmap::create (fader_cursor_mask_bits, fader_cursor_width, fader_cursor_height);
1233                 fader_cursor = new Gdk::Cursor (source, mask, ffg, fbg, fader_cursor_x_hot, fader_cursor_y_hot);
1234         }
1235         
1236         { 
1237                 RefPtr<Bitmap> source, mask;
1238                 source = Bitmap::create (speaker_cursor_bits, speaker_cursor_width, speaker_cursor_height);
1239                 mask = Bitmap::create (speaker_cursor_mask_bits, speaker_cursor_width, speaker_cursor_height);
1240                 speaker_cursor = new Gdk::Cursor (source, mask, ffg, fbg, speaker_cursor_x_hot, speaker_cursor_y_hot);
1241         }
1242         
1243         { 
1244                 RefPtr<Bitmap> bits;
1245                 char pix[4] = { 0, 0, 0, 0 };
1246                 bits = Bitmap::create (pix, 2, 2);
1247                 Gdk::Color c;
1248                 transparent_cursor = new Gdk::Cursor (bits, bits, c, c, 0, 0);
1249         }
1250
1251         { 
1252                 RefPtr<Bitmap> bits;
1253                 char pix[4] = { 0, 0, 0, 0 };
1254                 bits = Bitmap::create (pix, 2, 2);
1255                 Gdk::Color c;
1256                 transparent_cursor = new Gdk::Cursor (bits, bits, c, c, 0, 0);
1257         }
1258         
1259
1260         grabber_cursor = new Gdk::Cursor (HAND2);
1261         
1262         {
1263                 Glib::RefPtr<Gdk::Pixbuf> grabber_edit_point_pixbuf (::get_icon ("grabber_edit_point"));
1264                 grabber_edit_point_cursor = new Gdk::Cursor (Gdk::Display::get_default(), grabber_edit_point_pixbuf, 5, 17);
1265         }
1266
1267         cross_hair_cursor = new Gdk::Cursor (CROSSHAIR);
1268         trimmer_cursor =  new Gdk::Cursor (SB_H_DOUBLE_ARROW);
1269         selector_cursor = new Gdk::Cursor (XTERM);
1270         time_fx_cursor = new Gdk::Cursor (SIZING);
1271         wait_cursor = new Gdk::Cursor  (WATCH);
1272         timebar_cursor = new Gdk::Cursor(LEFT_PTR);
1273         midi_pencil_cursor = new Gdk::Cursor (PENCIL);
1274         midi_select_cursor = new Gdk::Cursor (CENTER_PTR);
1275         midi_resize_cursor = new Gdk::Cursor (SIZING);
1276         midi_erase_cursor = new Gdk::Cursor (DRAPED_BOX);
1277 }
1278
1279 /** Pop up a context menu for when the user clicks on a fade in or fade out */
1280 void
1281 Editor::popup_fade_context_menu (int button, int32_t time, ArdourCanvas::Item* item, ItemType item_type)
1282 {
1283         using namespace Menu_Helpers;
1284         AudioRegionView* arv = static_cast<AudioRegionView*> (item->get_data ("regionview"));
1285
1286         if (arv == 0) {
1287                 fatal << _("programming error: fade in canvas item has no regionview data pointer!") << endmsg;
1288                 /*NOTREACHED*/
1289         }
1290
1291         MenuList& items (fade_context_menu.items());
1292
1293         items.clear ();
1294
1295         switch (item_type) {
1296         case FadeInItem:
1297         case FadeInHandleItem:
1298                 if (arv->audio_region()->fade_in_active()) {
1299                         items.push_back (MenuElem (_("Deactivate"), bind (mem_fun (*this, &Editor::set_fade_in_active), false)));
1300                 } else {
1301                         items.push_back (MenuElem (_("Activate"), bind (mem_fun (*this, &Editor::set_fade_in_active), true)));
1302                 }
1303                 
1304                 items.push_back (SeparatorElem());
1305
1306                 if (Profile->get_sae()) {
1307                         items.push_back (MenuElem (_("Linear"), bind (mem_fun (*this, &Editor::set_fade_in_shape), AudioRegion::Linear)));
1308                         items.push_back (MenuElem (_("Slowest"), bind (mem_fun (*this, &Editor::set_fade_in_shape), AudioRegion::Fast)));
1309                 } else {
1310                         items.push_back (MenuElem (_("Linear"), bind (mem_fun (*this, &Editor::set_fade_in_shape), AudioRegion::Linear)));
1311                         items.push_back (MenuElem (_("Slowest"), bind (mem_fun (*this, &Editor::set_fade_in_shape), AudioRegion::Fast)));
1312                         items.push_back (MenuElem (_("Slow"), bind (mem_fun (*this, &Editor::set_fade_in_shape), AudioRegion::LogB)));
1313                         items.push_back (MenuElem (_("Fast"), bind (mem_fun (*this, &Editor::set_fade_in_shape), AudioRegion::LogA)));
1314                         items.push_back (MenuElem (_("Fastest"), bind (mem_fun (*this, &Editor::set_fade_in_shape), AudioRegion::Slow)));
1315                 }
1316
1317                 break;
1318
1319         case FadeOutItem:
1320         case FadeOutHandleItem:
1321                 if (arv->audio_region()->fade_out_active()) {
1322                         items.push_back (MenuElem (_("Deactivate"), bind (mem_fun (*this, &Editor::set_fade_out_active), false)));
1323                 } else {
1324                         items.push_back (MenuElem (_("Activate"), bind (mem_fun (*this, &Editor::set_fade_out_active), true)));
1325                 }
1326                 
1327                 items.push_back (SeparatorElem());
1328                 
1329                 if (Profile->get_sae()) {
1330                         items.push_back (MenuElem (_("Linear"), bind (mem_fun (*this, &Editor::set_fade_out_shape), AudioRegion::Linear)));
1331                         items.push_back (MenuElem (_("Slowest"), bind (mem_fun (*this, &Editor::set_fade_out_shape), AudioRegion::Slow)));
1332                 } else {
1333                         items.push_back (MenuElem (_("Linear"), bind (mem_fun (*this, &Editor::set_fade_out_shape), AudioRegion::Linear)));
1334                         items.push_back (MenuElem (_("Slowest"), bind (mem_fun (*this, &Editor::set_fade_out_shape), AudioRegion::Slow)));
1335                         items.push_back (MenuElem (_("Slow"), bind (mem_fun (*this, &Editor::set_fade_out_shape), AudioRegion::LogA)));
1336                         items.push_back (MenuElem (_("Fast"), bind (mem_fun (*this, &Editor::set_fade_out_shape), AudioRegion::LogB)));
1337                         items.push_back (MenuElem (_("Fastest"), bind (mem_fun (*this, &Editor::set_fade_out_shape), AudioRegion::Fast)));
1338                 }
1339
1340                 break;
1341
1342         default:
1343                 fatal << _("programming error: ")
1344                       << X_("non-fade canvas item passed to popup_fade_context_menu()")
1345                       << endmsg;
1346                 /*NOTREACHED*/
1347         }
1348
1349         fade_context_menu.popup (button, time);
1350 }
1351
1352 void
1353 Editor::popup_track_context_menu (int button, int32_t time, ItemType item_type, bool with_selection, nframes64_t frame)
1354 {
1355         using namespace Menu_Helpers;
1356         Menu* (Editor::*build_menu_function)(nframes64_t);
1357         Menu *menu;
1358
1359         switch (item_type) {
1360         case RegionItem:
1361         case RegionViewName:
1362         case RegionViewNameHighlight:
1363                 if (with_selection) {
1364                         build_menu_function = &Editor::build_track_selection_context_menu;
1365                 } else {
1366                         build_menu_function = &Editor::build_track_region_context_menu;
1367                 }
1368                 break;
1369
1370         case SelectionItem:
1371                 if (with_selection) {
1372                         build_menu_function = &Editor::build_track_selection_context_menu;
1373                 } else {
1374                         build_menu_function = &Editor::build_track_context_menu;
1375                 }
1376                 break;
1377
1378         case CrossfadeViewItem:
1379                 build_menu_function = &Editor::build_track_crossfade_context_menu;
1380                 break;
1381
1382         case StreamItem:
1383                 if (clicked_routeview->get_diskstream()) {
1384                         build_menu_function = &Editor::build_track_context_menu;
1385                 } else {
1386                         build_menu_function = &Editor::build_track_bus_context_menu;
1387                 }
1388                 break;
1389
1390         default:
1391                 /* probably shouldn't happen but if it does, we don't care */
1392                 return;
1393         }
1394
1395         menu = (this->*build_menu_function)(frame);
1396         menu->set_name ("ArdourContextMenu");
1397         
1398         /* now handle specific situations */
1399
1400         switch (item_type) {
1401         case RegionItem:
1402         case RegionViewName:
1403         case RegionViewNameHighlight:
1404                 if (!with_selection) {
1405                         if (region_edit_menu_split_item) {
1406                                 if (clicked_regionview && clicked_regionview->region()->covers (get_preferred_edit_position())) {
1407                                         ActionManager::set_sensitive (ActionManager::edit_point_in_region_sensitive_actions, true);
1408                                 } else {
1409                                         ActionManager::set_sensitive (ActionManager::edit_point_in_region_sensitive_actions, false);
1410                                 }
1411                         }
1412                         /*
1413                         if (region_edit_menu_split_multichannel_item) {
1414                                 if (clicked_regionview && clicked_regionview->region().n_channels() > 1) {
1415                                         // GTK2FIX find the action, change its sensitivity
1416                                         // region_edit_menu_split_multichannel_item->set_sensitive (true);
1417                                 } else {
1418                                         // GTK2FIX see above
1419                                         // region_edit_menu_split_multichannel_item->set_sensitive (false);
1420                                 }
1421                         }*/
1422                 }
1423                 break;
1424
1425         case SelectionItem:
1426                 break;
1427
1428         case CrossfadeViewItem:
1429                 break;
1430
1431         case StreamItem:
1432                 break;
1433
1434         default:
1435                 /* probably shouldn't happen but if it does, we don't care */
1436                 return;
1437         }
1438
1439         if (item_type != SelectionItem && clicked_routeview && clicked_routeview->audio_track()) {
1440
1441                 /* Bounce to disk */
1442                 
1443                 using namespace Menu_Helpers;
1444                 MenuList& edit_items  = menu->items();
1445                 
1446                 edit_items.push_back (SeparatorElem());
1447
1448                 switch (clicked_routeview->audio_track()->freeze_state()) {
1449                 case AudioTrack::NoFreeze:
1450                         edit_items.push_back (MenuElem (_("Freeze"), mem_fun(*this, &Editor::freeze_route)));
1451                         break;
1452
1453                 case AudioTrack::Frozen:
1454                         edit_items.push_back (MenuElem (_("Unfreeze"), mem_fun(*this, &Editor::unfreeze_route)));
1455                         break;
1456                         
1457                 case AudioTrack::UnFrozen:
1458                         edit_items.push_back (MenuElem (_("Freeze"), mem_fun(*this, &Editor::freeze_route)));
1459                         break;
1460                 }
1461
1462         }
1463
1464         if (item_type == StreamItem && clicked_routeview) {
1465                 clicked_routeview->build_underlay_menu(menu);
1466         }
1467
1468         menu->popup (button, time);
1469 }
1470
1471 Menu*
1472 Editor::build_track_context_menu (nframes64_t ignored)
1473 {
1474         using namespace Menu_Helpers;
1475
1476         MenuList& edit_items = track_context_menu.items();
1477         edit_items.clear();
1478
1479         add_dstream_context_items (edit_items);
1480         return &track_context_menu;
1481 }
1482
1483 Menu*
1484 Editor::build_track_bus_context_menu (nframes64_t ignored)
1485 {
1486         using namespace Menu_Helpers;
1487
1488         MenuList& edit_items = track_context_menu.items();
1489         edit_items.clear();
1490
1491         add_bus_context_items (edit_items);
1492         return &track_context_menu;
1493 }
1494
1495 Menu*
1496 Editor::build_track_region_context_menu (nframes64_t frame)
1497 {
1498         using namespace Menu_Helpers;
1499         MenuList& edit_items  = track_region_context_menu.items();
1500         edit_items.clear();
1501
1502         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (clicked_axisview);
1503
1504         if (rtv) {
1505                 boost::shared_ptr<Diskstream> ds;
1506                 boost::shared_ptr<Playlist> pl;
1507                 
1508                 if ((ds = rtv->get_diskstream()) && ((pl = ds->playlist()))) {
1509                         Playlist::RegionList* regions = pl->regions_at ((nframes64_t) floor ( (double)frame * ds->speed()));
1510
1511                         if (selection->regions.size() > 1) {
1512                                 // there's already a multiple selection: just add a 
1513                                 // single region context menu that will act on all 
1514                                 // selected regions
1515                                 boost::shared_ptr<Region> dummy_region; // = NULL               
1516                                 add_region_context_items (rtv->view(), dummy_region, edit_items);                       
1517                         } else {
1518                                 for (Playlist::RegionList::iterator i = regions->begin(); i != regions->end(); ++i) {
1519                                         add_region_context_items (rtv->view(), (*i), edit_items);
1520                                 }
1521                         }
1522
1523                         delete regions;
1524                 }
1525         }
1526
1527         add_dstream_context_items (edit_items);
1528
1529         return &track_region_context_menu;
1530 }
1531
1532 Menu*
1533 Editor::build_track_crossfade_context_menu (nframes64_t frame)
1534 {
1535         using namespace Menu_Helpers;
1536         MenuList& edit_items  = track_crossfade_context_menu.items();
1537         edit_items.clear ();
1538
1539         AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*> (clicked_axisview);
1540
1541         if (atv) {
1542                 boost::shared_ptr<Diskstream> ds;
1543                 boost::shared_ptr<Playlist> pl;
1544                 boost::shared_ptr<AudioPlaylist> apl;
1545
1546                 if ((ds = atv->get_diskstream()) && ((pl = ds->playlist()) != 0) && ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl)) != 0)) {
1547
1548                         Playlist::RegionList* regions = pl->regions_at (frame);
1549                         AudioPlaylist::Crossfades xfades;
1550
1551                         apl->crossfades_at (frame, xfades);
1552
1553                         bool many = xfades.size() > 1;
1554
1555                         for (AudioPlaylist::Crossfades::iterator i = xfades.begin(); i != xfades.end(); ++i) {
1556                                 add_crossfade_context_items (atv->audio_view(), (*i), edit_items, many);
1557                         }
1558
1559                         if (selection->regions.size() > 1) {
1560                                 // there's already a multiple selection: just add a 
1561                                 // single region context menu that will act on all 
1562                                 // selected regions
1563                                 boost::shared_ptr<Region> dummy_region; // = NULL               
1564                                 add_region_context_items (atv->audio_view(), dummy_region, edit_items);                 
1565                         } else {
1566                                 for (Playlist::RegionList::iterator i = regions->begin(); i != regions->end(); ++i) {
1567                                         add_region_context_items (atv->audio_view(), (*i), edit_items);
1568                                 }
1569                         }
1570                         delete regions;
1571                 }
1572         }
1573
1574         add_dstream_context_items (edit_items);
1575
1576         return &track_crossfade_context_menu;
1577 }
1578
1579 void
1580 Editor::analyze_region_selection()
1581 {
1582         if (analysis_window == 0) {
1583                 analysis_window = new AnalysisWindow();
1584
1585                 if (session != 0)
1586                         analysis_window->set_session(session);
1587
1588                 analysis_window->show_all();
1589         }
1590
1591         analysis_window->set_regionmode();
1592         analysis_window->analyze();
1593         
1594         analysis_window->present();
1595 }
1596
1597 void
1598 Editor::analyze_range_selection()
1599 {
1600         if (analysis_window == 0) {
1601                 analysis_window = new AnalysisWindow();
1602
1603                 if (session != 0)
1604                         analysis_window->set_session(session);
1605
1606                 analysis_window->show_all();
1607         }
1608
1609         analysis_window->set_rangemode();
1610         analysis_window->analyze();
1611         
1612         analysis_window->present();
1613 }
1614
1615 Menu*
1616 Editor::build_track_selection_context_menu (nframes64_t ignored)
1617 {
1618         using namespace Menu_Helpers;
1619         MenuList& edit_items  = track_selection_context_menu.items();
1620         edit_items.clear ();
1621
1622         add_selection_context_items (edit_items);
1623         // edit_items.push_back (SeparatorElem());
1624         // add_dstream_context_items (edit_items);
1625
1626         return &track_selection_context_menu;
1627 }
1628
1629 /** Add context menu items relevant to crossfades.
1630  * @param edit_items List to add the items to.
1631  */
1632 void
1633 Editor::add_crossfade_context_items (AudioStreamView* view, boost::shared_ptr<Crossfade> xfade, Menu_Helpers::MenuList& edit_items, bool many)
1634 {
1635         using namespace Menu_Helpers;
1636         Menu     *xfade_menu = manage (new Menu);
1637         MenuList& items       = xfade_menu->items();
1638         xfade_menu->set_name ("ArdourContextMenu");
1639         string str;
1640
1641         if (xfade->active()) {
1642                 str = _("Mute");
1643         } else { 
1644                 str = _("Unmute");
1645         }
1646
1647         items.push_back (MenuElem (str, bind (mem_fun(*this, &Editor::toggle_xfade_active), boost::weak_ptr<Crossfade> (xfade))));
1648         items.push_back (MenuElem (_("Edit"), bind (mem_fun(*this, &Editor::edit_xfade), boost::weak_ptr<Crossfade> (xfade))));
1649
1650         if (xfade->can_follow_overlap()) {
1651
1652                 if (xfade->following_overlap()) {
1653                         str = _("Convert to short");
1654                 } else {
1655                         str = _("Convert to full");
1656                 }
1657
1658                 items.push_back (MenuElem (str, bind (mem_fun(*this, &Editor::toggle_xfade_length), xfade)));
1659         }
1660
1661         if (many) {
1662                 str = xfade->out()->name();
1663                 str += "->";
1664                 str += xfade->in()->name();
1665         } else {
1666                 str = _("Crossfade");
1667         }
1668
1669         edit_items.push_back (MenuElem (str, *xfade_menu));
1670         edit_items.push_back (SeparatorElem());
1671 }
1672
1673 void
1674 Editor::xfade_edit_left_region ()
1675 {
1676         if (clicked_crossfadeview) {
1677                 clicked_crossfadeview->left_view.show_region_editor ();
1678         }
1679 }
1680
1681 void
1682 Editor::xfade_edit_right_region ()
1683 {
1684         if (clicked_crossfadeview) {
1685                 clicked_crossfadeview->right_view.show_region_editor ();
1686         }
1687 }
1688
1689 void
1690 Editor::add_region_context_items (StreamView* sv, boost::shared_ptr<Region> region, Menu_Helpers::MenuList& edit_items)
1691 {
1692         using namespace Menu_Helpers;
1693         Gtk::MenuItem* foo_item;
1694         Menu     *region_menu = manage (new Menu);
1695         MenuList& items       = region_menu->items();
1696         region_menu->set_name ("ArdourContextMenu");
1697
1698         boost::shared_ptr<AudioRegion> ar;
1699         boost::shared_ptr<MidiRegion>  mr;
1700
1701         if (region) {
1702                 ar = boost::dynamic_pointer_cast<AudioRegion> (region);
1703                 mr = boost::dynamic_pointer_cast<MidiRegion> (region);
1704
1705                 /* when this particular menu pops up, make the relevant region 
1706                    become selected.
1707                 */
1708
1709                 region_menu->signal_map_event().connect (
1710                         bind (mem_fun(*this, &Editor::set_selected_regionview_from_map_event), sv, boost::weak_ptr<Region>(region)));
1711                 
1712                 items.push_back (MenuElem (_("Rename"), mem_fun(*this, &Editor::rename_region)));
1713                 items.push_back (MenuElem (_("Popup region editor"), mem_fun(*this, &Editor::edit_region)));
1714         }
1715
1716         items.push_back (MenuElem (_("Raise to top layer"), mem_fun(*this, &Editor::raise_region_to_top)));
1717         items.push_back (MenuElem (_("Lower to bottom layer"), mem_fun  (*this, &Editor::lower_region_to_bottom)));
1718         items.push_back (SeparatorElem());
1719         items.push_back (MenuElem (_("Define sync point"), mem_fun(*this, &Editor::set_region_sync_from_edit_point)));
1720         items.push_back (MenuElem (_("Remove sync point"), mem_fun(*this, &Editor::remove_region_sync)));
1721         items.push_back (SeparatorElem());
1722
1723         items.push_back (MenuElem (_("Audition"), mem_fun(*this, &Editor::play_selected_region)));
1724         items.push_back (MenuElem (_("Export"), mem_fun(*this, &Editor::export_region)));
1725         items.push_back (MenuElem (_("Bounce"), mem_fun(*this, &Editor::bounce_region_selection)));
1726
1727         if (ar) {
1728                 items.push_back (MenuElem (_("Spectral Analysis"), mem_fun(*this, &Editor::analyze_region_selection)));
1729         }
1730
1731         items.push_back (SeparatorElem());
1732
1733         sigc::connection fooc;
1734         boost::shared_ptr<Region> region_to_check;
1735
1736         if (region) {
1737                 region_to_check = region;
1738         } else {
1739                 region_to_check = selection->regions.front()->region();
1740         }
1741
1742         items.push_back (CheckMenuElem (_("Lock")));
1743         CheckMenuItem* region_lock_item = static_cast<CheckMenuItem*>(&items.back());
1744         if (region_to_check->locked()) {
1745                 region_lock_item->set_active();
1746         }
1747         region_lock_item->signal_activate().connect (mem_fun(*this, &Editor::toggle_region_lock));
1748         
1749         items.push_back (CheckMenuElem (_("Glue to Bars&Beats")));
1750         CheckMenuItem* bbt_glue_item = static_cast<CheckMenuItem*>(&items.back());
1751         
1752         switch (region_to_check->positional_lock_style()) {
1753         case Region::MusicTime:
1754                 bbt_glue_item->set_active (true);
1755                 break;
1756         default:
1757                 bbt_glue_item->set_active (false);
1758                 break;
1759         }
1760         
1761         bbt_glue_item->signal_activate().connect (bind (mem_fun (*this, &Editor::set_region_lock_style), Region::MusicTime));
1762         
1763         items.push_back (CheckMenuElem (_("Mute")));
1764         CheckMenuItem* region_mute_item = static_cast<CheckMenuItem*>(&items.back());
1765         fooc = region_mute_item->signal_activate().connect (mem_fun(*this, &Editor::toggle_region_mute));
1766         if (region_to_check->muted()) {
1767                 fooc.block (true);
1768                 region_mute_item->set_active();
1769                 fooc.block (false);
1770         }
1771         
1772         if (!Profile->get_sae()) {
1773                 items.push_back (CheckMenuElem (_("Opaque")));
1774                 CheckMenuItem* region_opaque_item = static_cast<CheckMenuItem*>(&items.back());
1775                 fooc = region_opaque_item->signal_activate().connect (mem_fun(*this, &Editor::toggle_region_opaque));
1776                 if (region_to_check->opaque()) {
1777                         fooc.block (true);
1778                         region_opaque_item->set_active();
1779                         fooc.block (false);
1780                 }
1781         }
1782         
1783         items.push_back (CheckMenuElem (_("Original position"), mem_fun(*this, &Editor::naturalize)));
1784         if (region_to_check->at_natural_position()) {
1785                 items.back().set_sensitive (false);
1786         }
1787         
1788         items.push_back (SeparatorElem());
1789         
1790         if (ar) {
1791                 
1792                 RegionView* rv = sv->find_view (ar);
1793                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(rv);
1794                 
1795                 if (!Profile->get_sae()) {
1796                         items.push_back (MenuElem (_("Reset Envelope"), mem_fun(*this, &Editor::reset_region_gain_envelopes)));
1797
1798                         items.push_back (CheckMenuElem (_("Envelope Visible")));
1799                         CheckMenuItem* region_envelope_visible_item = static_cast<CheckMenuItem*> (&items.back());
1800                         fooc = region_envelope_visible_item->signal_activate().connect (mem_fun(*this, &Editor::toggle_gain_envelope_visibility));
1801                         if (arv->envelope_visible()) {
1802                                 fooc.block (true);
1803                                 region_envelope_visible_item->set_active (true);
1804                                 fooc.block (false);
1805                         }
1806                 
1807                         items.push_back (CheckMenuElem (_("Envelope Active")));
1808                         CheckMenuItem* region_envelope_active_item = static_cast<CheckMenuItem*> (&items.back());
1809                         fooc = region_envelope_active_item->signal_activate().connect (mem_fun(*this, &Editor::toggle_gain_envelope_active));
1810                         
1811                         if (ar->envelope_active()) {
1812                                 fooc.block (true);
1813                                 region_envelope_active_item->set_active (true);
1814                                 fooc.block (false);
1815                         }
1816
1817                         items.push_back (SeparatorElem());
1818                 }
1819
1820                 if (ar->scale_amplitude() != 1.0f) {
1821                         items.push_back (MenuElem (_("DeNormalize"), mem_fun(*this, &Editor::denormalize_region)));
1822                 } else {
1823                         items.push_back (MenuElem (_("Normalize"), mem_fun(*this, &Editor::normalize_region)));
1824                 }
1825
1826         } else if (mr) {
1827                 items.push_back (MenuElem (_("Quantize"), mem_fun(*this, &Editor::quantize_region)));
1828                 items.push_back (SeparatorElem());
1829         }
1830
1831         items.push_back (MenuElem (_("Strip silence..."), mem_fun (*this, &Editor::strip_region_silence)));
1832         items.push_back (MenuElem (_("Reverse"), mem_fun(*this, &Editor::reverse_region)));
1833         items.push_back (SeparatorElem());
1834
1835         /* range related stuff */
1836
1837         items.push_back (MenuElem (_("Add Single Range"), mem_fun (*this, &Editor::add_location_from_audio_region)));
1838         items.push_back (MenuElem (_("Add Range Markers"), mem_fun (*this, &Editor::add_locations_from_audio_region)));
1839         if (selection->regions.size() < 2) {
1840                 items.back().set_sensitive (false);
1841         }
1842
1843         items.push_back (MenuElem (_("Set Range Selection"), mem_fun (*this, &Editor::set_selection_from_region)));
1844         items.push_back (SeparatorElem());
1845                          
1846         /* Nudge region */
1847
1848         Menu *nudge_menu = manage (new Menu());
1849         MenuList& nudge_items = nudge_menu->items();
1850         nudge_menu->set_name ("ArdourContextMenu");
1851         
1852         nudge_items.push_back (MenuElem (_("Nudge fwd"), (bind (mem_fun(*this, &Editor::nudge_forward), false, false))));
1853         nudge_items.push_back (MenuElem (_("Nudge bwd"), (bind (mem_fun(*this, &Editor::nudge_backward), false, false))));
1854         nudge_items.push_back (MenuElem (_("Nudge fwd by capture offset"), (mem_fun(*this, &Editor::nudge_forward_capture_offset))));
1855         nudge_items.push_back (MenuElem (_("Nudge bwd by capture offset"), (mem_fun(*this, &Editor::nudge_backward_capture_offset))));
1856
1857         items.push_back (MenuElem (_("Nudge"), *nudge_menu));
1858         items.push_back (SeparatorElem());
1859
1860         Menu *trim_menu = manage (new Menu);
1861         MenuList& trim_items = trim_menu->items();
1862         trim_menu->set_name ("ArdourContextMenu");
1863         
1864         trim_items.push_back (MenuElem (_("Start to edit point"), mem_fun(*this, &Editor::trim_region_from_edit_point)));
1865         foo_item = &trim_items.back();
1866         if (_edit_point == EditAtMouse) {
1867                 foo_item->set_sensitive (false);
1868         }
1869         trim_items.push_back (MenuElem (_("Edit point to end"), mem_fun(*this, &Editor::trim_region_to_edit_point)));
1870         foo_item = &trim_items.back();
1871         if (_edit_point == EditAtMouse) {
1872                 foo_item->set_sensitive (false);
1873         }
1874         trim_items.push_back (MenuElem (_("Trim To Loop"), mem_fun(*this, &Editor::trim_region_to_loop)));
1875         trim_items.push_back (MenuElem (_("Trim To Punch"), mem_fun(*this, &Editor::trim_region_to_punch)));
1876                              
1877         items.push_back (MenuElem (_("Trim"), *trim_menu));
1878         items.push_back (SeparatorElem());
1879
1880         items.push_back (MenuElem (_("Split"), (mem_fun(*this, &Editor::split_region))));
1881         region_edit_menu_split_item = &items.back();
1882         
1883         if (_edit_point == EditAtMouse) {
1884                 region_edit_menu_split_item->set_sensitive (false);
1885         }
1886
1887         items.push_back (MenuElem (_("Make mono regions"), (mem_fun(*this, &Editor::split_multichannel_region))));
1888         region_edit_menu_split_multichannel_item = &items.back();
1889
1890         items.push_back (MenuElem (_("Duplicate"), (bind (mem_fun(*this, &Editor::duplicate_dialog), false))));
1891         items.push_back (MenuElem (_("Multi-Duplicate"), (bind (mem_fun(*this, &Editor::duplicate_dialog), true))));
1892         items.push_back (MenuElem (_("Fill Track"), (mem_fun(*this, &Editor::region_fill_track))));
1893         items.push_back (SeparatorElem());
1894         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &Editor::remove_selected_regions)));
1895
1896         /* OK, stick the region submenu at the top of the list, and then add
1897            the standard items.
1898         */
1899
1900         /* we have to hack up the region name because "_" has a special
1901            meaning for menu titles.
1902         */
1903
1904         string::size_type pos = 0;
1905         string menu_item_name = (region) ? region->name() : _("Selected regions");
1906
1907         while ((pos = menu_item_name.find ("_", pos)) != string::npos) {
1908                 menu_item_name.replace (pos, 1, "__");
1909                 pos += 2;
1910         }
1911         
1912         edit_items.push_back (MenuElem (menu_item_name, *region_menu));
1913         edit_items.push_back (SeparatorElem());
1914 }
1915
1916 /** Add context menu items relevant to selection ranges.
1917  * @param edit_items List to add the items to.
1918  */
1919 void
1920 Editor::add_selection_context_items (Menu_Helpers::MenuList& edit_items)
1921 {
1922         using namespace Menu_Helpers;
1923
1924         edit_items.push_back (MenuElem (_("Play range"), mem_fun(*this, &Editor::play_selection)));
1925         edit_items.push_back (MenuElem (_("Loop range"), bind (mem_fun(*this, &Editor::set_loop_from_selection), true)));
1926
1927         edit_items.push_back (SeparatorElem());
1928         edit_items.push_back (MenuElem (_("Spectral Analysis"), mem_fun(*this, &Editor::analyze_range_selection)));
1929         
1930         edit_items.push_back (SeparatorElem());
1931         edit_items.push_back (MenuElem (_("Extend Range to End of Region"), bind (mem_fun(*this, &Editor::extend_selection_to_end_of_region), false)));
1932         edit_items.push_back (MenuElem (_("Extend Range to Start of Region"), bind (mem_fun(*this, &Editor::extend_selection_to_start_of_region), false)));
1933
1934         edit_items.push_back (SeparatorElem());
1935         edit_items.push_back (MenuElem (_("Convert to region in-place"), mem_fun(*this, &Editor::separate_region_from_selection)));
1936         edit_items.push_back (MenuElem (_("Convert to region in region list"), mem_fun(*this, &Editor::new_region_from_selection)));
1937         
1938         edit_items.push_back (SeparatorElem());
1939         edit_items.push_back (MenuElem (_("Select all in range"), mem_fun(*this, &Editor::select_all_selectables_using_time_selection)));
1940
1941         edit_items.push_back (SeparatorElem());
1942         edit_items.push_back (MenuElem (_("Set loop from selection"), bind (mem_fun(*this, &Editor::set_loop_from_selection), false)));
1943         edit_items.push_back (MenuElem (_("Set punch from selection"), mem_fun(*this, &Editor::set_punch_from_selection)));
1944         
1945         edit_items.push_back (SeparatorElem());
1946         edit_items.push_back (MenuElem (_("Add Range Markers"), mem_fun (*this, &Editor::add_location_from_selection)));
1947         edit_items.push_back (SeparatorElem());
1948         edit_items.push_back (MenuElem (_("Crop region to range"), mem_fun(*this, &Editor::crop_region_to_selection)));
1949         edit_items.push_back (MenuElem (_("Fill range with region"), mem_fun(*this, &Editor::region_fill_selection)));
1950         edit_items.push_back (MenuElem (_("Duplicate range"), bind (mem_fun(*this, &Editor::duplicate_dialog), false)));
1951         edit_items.push_back (MenuElem (_("Create chunk from range"), mem_fun(*this, &Editor::create_named_selection)));
1952         edit_items.push_back (SeparatorElem());
1953         edit_items.push_back (MenuElem (_("Consolidate range"), bind (mem_fun(*this, &Editor::bounce_range_selection), true, false)));
1954         edit_items.push_back (MenuElem (_("Consolidate range with processing"), bind (mem_fun(*this, &Editor::bounce_range_selection), true, true)));
1955         edit_items.push_back (MenuElem (_("Bounce range to region list"), bind (mem_fun(*this, &Editor::bounce_range_selection), false, false)));
1956         edit_items.push_back (MenuElem (_("Bounce range to region list with processing"), bind (mem_fun(*this, &Editor::bounce_range_selection), false, true)));
1957         edit_items.push_back (MenuElem (_("Export range"), mem_fun(*this, &Editor::export_range)));
1958 }
1959
1960         
1961 void
1962 Editor::add_dstream_context_items (Menu_Helpers::MenuList& edit_items)
1963 {
1964         using namespace Menu_Helpers;
1965
1966         /* Playback */
1967
1968         Menu *play_menu = manage (new Menu);
1969         MenuList& play_items = play_menu->items();
1970         play_menu->set_name ("ArdourContextMenu");
1971         
1972         play_items.push_back (MenuElem (_("Play from edit point"), mem_fun(*this, &Editor::play_from_edit_point)));
1973         play_items.push_back (MenuElem (_("Play from start"), mem_fun(*this, &Editor::play_from_start)));
1974         play_items.push_back (MenuElem (_("Play region"), mem_fun(*this, &Editor::play_selected_region)));
1975         play_items.push_back (SeparatorElem());
1976         play_items.push_back (MenuElem (_("Loop Region"), mem_fun(*this, &Editor::loop_selected_region)));
1977         
1978         edit_items.push_back (MenuElem (_("Play"), *play_menu));
1979
1980         /* Selection */
1981
1982         Menu *select_menu = manage (new Menu);
1983         MenuList& select_items = select_menu->items();
1984         select_menu->set_name ("ArdourContextMenu");
1985         
1986         select_items.push_back (MenuElem (_("Select All in track"), bind (mem_fun(*this, &Editor::select_all_in_track), Selection::Set)));
1987         select_items.push_back (MenuElem (_("Select All"), bind (mem_fun(*this, &Editor::select_all), Selection::Set)));
1988         select_items.push_back (MenuElem (_("Invert selection in track"), mem_fun(*this, &Editor::invert_selection_in_track)));
1989         select_items.push_back (MenuElem (_("Invert selection"), mem_fun(*this, &Editor::invert_selection)));
1990         select_items.push_back (SeparatorElem());
1991         select_items.push_back (MenuElem (_("Set range to loop range"), mem_fun(*this, &Editor::set_selection_from_loop)));
1992         select_items.push_back (MenuElem (_("Set range to punch range"), mem_fun(*this, &Editor::set_selection_from_punch)));
1993         select_items.push_back (SeparatorElem());
1994         select_items.push_back (MenuElem (_("Select All After Edit Point"), bind (mem_fun(*this, &Editor::select_all_selectables_using_edit), true)));
1995         select_items.push_back (MenuElem (_("Select All Before Edit Point"), bind (mem_fun(*this, &Editor::select_all_selectables_using_edit), false)));
1996         select_items.push_back (MenuElem (_("Select All After Playhead"), bind (mem_fun(*this, &Editor::select_all_selectables_using_cursor), playhead_cursor, true)));
1997         select_items.push_back (MenuElem (_("Select All Before Playhead"), bind (mem_fun(*this, &Editor::select_all_selectables_using_cursor), playhead_cursor, false)));
1998         select_items.push_back (MenuElem (_("Select All Between Playhead & Edit Point"), bind (mem_fun(*this, &Editor::select_all_selectables_between), false)));
1999         select_items.push_back (MenuElem (_("Select All Within Playhead & Edit Point"), bind (mem_fun(*this, &Editor::select_all_selectables_between), true)));
2000         select_items.push_back (MenuElem (_("Select Range Between Playhead & Edit Point"), mem_fun(*this, &Editor::select_range_between)));
2001
2002         select_items.push_back (SeparatorElem());
2003
2004         edit_items.push_back (MenuElem (_("Select"), *select_menu));
2005
2006         /* Cut-n-Paste */
2007
2008         Menu *cutnpaste_menu = manage (new Menu);
2009         MenuList& cutnpaste_items = cutnpaste_menu->items();
2010         cutnpaste_menu->set_name ("ArdourContextMenu");
2011         
2012         cutnpaste_items.push_back (MenuElem (_("Cut"), mem_fun(*this, &Editor::cut)));
2013         cutnpaste_items.push_back (MenuElem (_("Copy"), mem_fun(*this, &Editor::copy)));
2014         cutnpaste_items.push_back (MenuElem (_("Paste"), bind (mem_fun(*this, &Editor::paste), 1.0f)));
2015
2016         cutnpaste_items.push_back (SeparatorElem());
2017
2018         cutnpaste_items.push_back (MenuElem (_("Align"), bind (mem_fun(*this, &Editor::align), ARDOUR::SyncPoint)));
2019         cutnpaste_items.push_back (MenuElem (_("Align Relative"), bind (mem_fun(*this, &Editor::align_relative), ARDOUR::SyncPoint)));
2020
2021         cutnpaste_items.push_back (SeparatorElem());
2022
2023         cutnpaste_items.push_back (MenuElem (_("Insert chunk"), bind (mem_fun(*this, &Editor::paste_named_selection), 1.0f)));
2024
2025         edit_items.push_back (MenuElem (_("Edit"), *cutnpaste_menu));
2026
2027         /* Adding new material */
2028         
2029         edit_items.push_back (SeparatorElem());
2030         edit_items.push_back (MenuElem (_("Insert Selected Region"), bind (mem_fun(*this, &Editor::insert_region_list_selection), 1.0f)));
2031         edit_items.push_back (MenuElem (_("Insert Existing Media"), bind (mem_fun(*this, &Editor::add_external_audio_action), ImportToTrack)));
2032
2033         /* Nudge track */
2034
2035         Menu *nudge_menu = manage (new Menu());
2036         MenuList& nudge_items = nudge_menu->items();
2037         nudge_menu->set_name ("ArdourContextMenu");
2038         
2039         edit_items.push_back (SeparatorElem());
2040         nudge_items.push_back (MenuElem (_("Nudge entire track fwd"), (bind (mem_fun(*this, &Editor::nudge_track), false, true))));
2041         nudge_items.push_back (MenuElem (_("Nudge track after edit point fwd"), (bind (mem_fun(*this, &Editor::nudge_track), true, true))));
2042         nudge_items.push_back (MenuElem (_("Nudge entire track bwd"), (bind (mem_fun(*this, &Editor::nudge_track), false, false))));
2043         nudge_items.push_back (MenuElem (_("Nudge track after edit point bwd"), (bind (mem_fun(*this, &Editor::nudge_track), true, false))));
2044
2045         edit_items.push_back (MenuElem (_("Nudge"), *nudge_menu));
2046 }
2047
2048 void
2049 Editor::add_bus_context_items (Menu_Helpers::MenuList& edit_items)
2050 {
2051         using namespace Menu_Helpers;
2052
2053         /* Playback */
2054
2055         Menu *play_menu = manage (new Menu);
2056         MenuList& play_items = play_menu->items();
2057         play_menu->set_name ("ArdourContextMenu");
2058         
2059         play_items.push_back (MenuElem (_("Play from edit point"), mem_fun(*this, &Editor::play_from_edit_point)));
2060         play_items.push_back (MenuElem (_("Play from start"), mem_fun(*this, &Editor::play_from_start)));
2061         edit_items.push_back (MenuElem (_("Play"), *play_menu));
2062
2063         /* Selection */
2064
2065         Menu *select_menu = manage (new Menu);
2066         MenuList& select_items = select_menu->items();
2067         select_menu->set_name ("ArdourContextMenu");
2068         
2069         select_items.push_back (MenuElem (_("Select All in track"), bind (mem_fun(*this, &Editor::select_all_in_track), Selection::Set)));
2070         select_items.push_back (MenuElem (_("Select All"), bind (mem_fun(*this, &Editor::select_all), Selection::Set)));
2071         select_items.push_back (MenuElem (_("Invert selection in track"), mem_fun(*this, &Editor::invert_selection_in_track)));
2072         select_items.push_back (MenuElem (_("Invert selection"), mem_fun(*this, &Editor::invert_selection)));
2073         select_items.push_back (SeparatorElem());
2074         select_items.push_back (MenuElem (_("Select all after edit point"), bind (mem_fun(*this, &Editor::select_all_selectables_using_edit), true)));
2075         select_items.push_back (MenuElem (_("Select all before edit point"), bind (mem_fun(*this, &Editor::select_all_selectables_using_edit), false)));
2076         select_items.push_back (MenuElem (_("Select all after playhead"), bind (mem_fun(*this, &Editor::select_all_selectables_using_cursor), playhead_cursor, true)));
2077         select_items.push_back (MenuElem (_("Select all before playhead"), bind (mem_fun(*this, &Editor::select_all_selectables_using_cursor), playhead_cursor, false)));
2078
2079         edit_items.push_back (MenuElem (_("Select"), *select_menu));
2080
2081         /* Cut-n-Paste */
2082
2083         Menu *cutnpaste_menu = manage (new Menu);
2084         MenuList& cutnpaste_items = cutnpaste_menu->items();
2085         cutnpaste_menu->set_name ("ArdourContextMenu");
2086         
2087         cutnpaste_items.push_back (MenuElem (_("Cut"), mem_fun(*this, &Editor::cut)));
2088         cutnpaste_items.push_back (MenuElem (_("Copy"), mem_fun(*this, &Editor::copy)));
2089         cutnpaste_items.push_back (MenuElem (_("Paste"), bind (mem_fun(*this, &Editor::paste), 1.0f)));
2090
2091         Menu *nudge_menu = manage (new Menu());
2092         MenuList& nudge_items = nudge_menu->items();
2093         nudge_menu->set_name ("ArdourContextMenu");
2094         
2095         edit_items.push_back (SeparatorElem());
2096         nudge_items.push_back (MenuElem (_("Nudge entire track fwd"), (bind (mem_fun(*this, &Editor::nudge_track), false, true))));
2097         nudge_items.push_back (MenuElem (_("Nudge track after edit point fwd"), (bind (mem_fun(*this, &Editor::nudge_track), true, true))));
2098         nudge_items.push_back (MenuElem (_("Nudge entire track bwd"), (bind (mem_fun(*this, &Editor::nudge_track), false, false))));
2099         nudge_items.push_back (MenuElem (_("Nudge track after edit point bwd"), (bind (mem_fun(*this, &Editor::nudge_track), true, false))));
2100
2101         edit_items.push_back (MenuElem (_("Nudge"), *nudge_menu));
2102 }
2103
2104 void
2105 Editor::set_snap_to (SnapType st)
2106 {
2107         unsigned int snap_ind = (unsigned int)st;
2108
2109         snap_type = st;
2110         
2111         if (snap_ind > snap_type_strings.size() - 1) {
2112                 snap_ind = 0;
2113                 snap_type = (SnapType)snap_ind;
2114         }
2115         
2116         string str = snap_type_strings[snap_ind];
2117
2118         if (str != snap_type_selector.get_active_text()) {
2119                 snap_type_selector.set_active_text (str);
2120         }
2121
2122         instant_save ();
2123
2124         switch (snap_type) {
2125         case SnapToAThirtysecondBeat:
2126         case SnapToASixteenthBeat:
2127         case SnapToAEighthBeat:
2128         case SnapToAQuarterBeat:
2129         case SnapToAThirdBeat:
2130                 compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_frames());
2131                 update_tempo_based_rulers ();
2132                 break;
2133
2134         case SnapToRegionStart:
2135         case SnapToRegionEnd:
2136         case SnapToRegionSync:
2137         case SnapToRegionBoundary:
2138                 build_region_boundary_cache ();
2139                 break;
2140
2141         default:
2142                 /* relax */
2143                 break;
2144     }
2145 }
2146
2147 void
2148 Editor::set_snap_mode (SnapMode mode)
2149 {
2150         snap_mode = mode;
2151         string str = snap_mode_strings[(int)mode];
2152
2153         if (str != snap_mode_selector.get_active_text ()) {
2154                 snap_mode_selector.set_active_text (str);
2155         }
2156
2157         instant_save ();
2158 }
2159 void
2160 Editor::set_edit_point_preference (EditPoint ep, bool force)
2161 {
2162         bool changed = (_edit_point != ep);
2163
2164         _edit_point = ep;
2165         string str = edit_point_strings[(int)ep];
2166
2167         if (str != edit_point_selector.get_active_text ()) {
2168                 edit_point_selector.set_active_text (str);
2169         }
2170
2171         set_canvas_cursor ();
2172
2173         if (!force && !changed) {
2174                 return;
2175         }
2176
2177         switch (zoom_focus) {
2178         case ZoomFocusMouse:
2179         case ZoomFocusPlayhead:
2180         case ZoomFocusEdit:
2181                 switch (_edit_point) {
2182                 case EditAtMouse:
2183                         set_zoom_focus (ZoomFocusMouse);
2184                         break;
2185                 case EditAtPlayhead:
2186                         set_zoom_focus (ZoomFocusPlayhead);
2187                         break;
2188                 case EditAtSelectedMarker:
2189                         set_zoom_focus (ZoomFocusEdit);
2190                         break;
2191                 }
2192                 break;
2193         default:
2194                 break;
2195         }
2196
2197         const char* action=NULL;
2198
2199         switch (_edit_point) {
2200         case EditAtPlayhead:
2201                 action = "edit-at-playhead";
2202                 break;
2203         case EditAtSelectedMarker:
2204                 action = "edit-at-marker";
2205                 break;
2206         case EditAtMouse:
2207                 action = "edit-at-mouse";
2208                 break;
2209         }
2210
2211         Glib::RefPtr<Action> act = ActionManager::get_action ("Editor", action);
2212         if (act) {
2213                 Glib::RefPtr<RadioAction>::cast_dynamic(act)->set_active (true);
2214         }
2215
2216         nframes64_t foo;
2217         bool in_track_canvas;
2218
2219         if (!mouse_frame (foo, in_track_canvas)) {
2220                 in_track_canvas = false;
2221         }
2222
2223         reset_canvas_action_sensitivity (in_track_canvas);
2224
2225         instant_save ();
2226 }
2227
2228 int
2229 Editor::set_state (const XMLNode& node)
2230 {
2231         const XMLProperty* prop;
2232         XMLNode* geometry;
2233         int x, y, xoff, yoff;
2234         Gdk::Geometry g;
2235
2236         if ((prop = node.property ("id")) != 0) {
2237                 _id = prop->value ();
2238         }
2239
2240         g.base_width = default_width;
2241         g.base_height = default_height;
2242         x = 1;
2243         y = 1;
2244         xoff = 0;
2245         yoff = 21;
2246
2247         if ((geometry = find_named_node (node, "geometry")) != 0) {
2248
2249                 XMLProperty* prop;
2250
2251                 if ((prop = geometry->property("x_size")) == 0) {
2252                         prop = geometry->property ("x-size");
2253                 }
2254                 if (prop) {
2255                         g.base_width = atoi(prop->value());
2256                 }
2257                 if ((prop = geometry->property("y_size")) == 0) {
2258                         prop = geometry->property ("y-size");
2259                 }
2260                 if (prop) {
2261                         g.base_height = atoi(prop->value());
2262                 }
2263
2264                 if ((prop = geometry->property ("x_pos")) == 0) {
2265                         prop = geometry->property ("x-pos");
2266                 }
2267                 if (prop) {
2268                         x = atoi (prop->value());
2269
2270                 }
2271                 if ((prop = geometry->property ("y_pos")) == 0) {
2272                         prop = geometry->property ("y-pos");
2273                 }
2274                 if (prop) {
2275                         y = atoi (prop->value());
2276                 }
2277
2278                 if ((prop = geometry->property ("x_off")) == 0) {
2279                         prop = geometry->property ("x-off");
2280                 }
2281                 if (prop) {
2282                         xoff = atoi (prop->value());
2283                 }
2284                 if ((prop = geometry->property ("y_off")) == 0) {
2285                         prop = geometry->property ("y-off");
2286                 }
2287                 if (prop) {
2288                         yoff = atoi (prop->value());
2289                 }
2290         }
2291
2292         set_default_size (g.base_width, g.base_height);
2293         move (x, y);
2294
2295         if (session && (prop = node.property ("playhead"))) {
2296                 nframes64_t pos = atol (prop->value().c_str());
2297                 playhead_cursor->set_position (pos);
2298         } else {
2299                 playhead_cursor->set_position (0);
2300
2301                 /* reset_x_origin() doesn't work right here, since the old
2302                    position may be zero already, and it does nothing in such
2303                    circumstances.
2304                 */
2305                 
2306                 leftmost_frame = 0;
2307                 horizontal_adjustment.set_value (0);
2308         }
2309
2310         if ((prop = node.property ("mixer-width"))) {
2311                 editor_mixer_strip_width = Width (string_2_enum (prop->value(), editor_mixer_strip_width));
2312         }
2313
2314         if ((prop = node.property ("zoom-focus"))) {
2315                 set_zoom_focus ((ZoomFocus) atoi (prop->value()));
2316         }
2317
2318         if ((prop = node.property ("zoom"))) {
2319                 reset_zoom (PBD::atof (prop->value()));
2320         }
2321
2322         if ((prop = node.property ("snap-to"))) {
2323                 set_snap_to ((SnapType) atoi (prop->value()));
2324         }
2325
2326         if ((prop = node.property ("snap-mode"))) {
2327                 set_snap_mode ((SnapMode) atoi (prop->value()));
2328         }
2329
2330         if ((prop = node.property ("edit-point"))) {
2331                 set_edit_point_preference ((EditPoint) string_2_enum (prop->value(), _edit_point), true);
2332         }
2333
2334         if ((prop = node.property ("mouse-mode"))) {
2335                 MouseMode m = str2mousemode(prop->value());
2336                 mouse_mode = MouseMode ((int) m + 1); /* lie, force mode switch */
2337                 set_mouse_mode (m, true);
2338         } else {
2339                 mouse_mode = MouseGain; /* lie, to force the mode switch */
2340                 set_mouse_mode (MouseObject, true);
2341         }
2342
2343         if ((prop = node.property ("show-waveforms-recording"))) {
2344                 bool yn = (prop->value() == "yes");
2345                 _show_waveforms_recording = !yn;
2346                 RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("ToggleWaveformsWhileRecording"));
2347                 if (act) {
2348                         RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
2349                         /* do it twice to force the change */
2350                         tact->set_active (!yn);
2351                         tact->set_active (yn);
2352                 }
2353         }
2354         
2355         if ((prop = node.property ("show-measures"))) {
2356                 bool yn = (prop->value() == "yes");
2357                 _show_measures = !yn;
2358                 RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("ToggleMeasureVisibility"));
2359                 if (act) {
2360                         RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
2361                         /* do it twice to force the change */
2362                         tact->set_active (!yn);
2363                         tact->set_active (yn);
2364                 }
2365         }
2366
2367         if ((prop = node.property ("follow-playhead"))) {
2368                 bool yn = (prop->value() == "yes");
2369                 set_follow_playhead (yn);
2370                 RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("toggle-follow-playhead"));
2371                 if (act) {
2372                         RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
2373                         if (tact->get_active() != yn) {
2374                                 tact->set_active (yn);
2375                         }
2376                 }
2377         }
2378
2379         if ((prop = node.property ("region-list-sort-type"))) {
2380                 _regions->reset_sort_type (str2regionlistsorttype(prop->value()), true);
2381         }
2382
2383         if ((prop = node.property ("xfades-visible"))) {
2384                 bool yn = (prop->value() == "yes");
2385                 _xfade_visibility = !yn;
2386                 // set_xfade_visibility (yn);
2387         }
2388
2389         if ((prop = node.property ("show-editor-mixer"))) {
2390
2391                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
2392                 if (act) {
2393
2394                         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2395                         bool yn = (prop->value() == X_("yes"));
2396
2397                         /* do it twice to force the change */
2398                         
2399                         tact->set_active (!yn);
2400                         tact->set_active (yn);
2401                 }
2402         }
2403         
2404         if ((prop = node.property ("show-editor-list"))) {
2405
2406                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-list"));
2407                 assert(act);
2408                 if (act) {
2409
2410                         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2411                         bool yn = (prop->value() == X_("yes"));
2412
2413                         /* do it twice to force the change */
2414                         
2415                         tact->set_active (!yn);
2416                         tact->set_active (yn);
2417                 }
2418         }
2419
2420
2421         return 0;
2422 }
2423
2424 XMLNode&
2425 Editor::get_state ()
2426 {
2427         XMLNode* node = new XMLNode ("Editor");
2428         char buf[32];
2429
2430         _id.print (buf, sizeof (buf));
2431         node->add_property ("id", buf);
2432         
2433         if (is_realized()) {
2434                 Glib::RefPtr<Gdk::Window> win = get_window();
2435                 
2436                 int x, y, xoff, yoff, width, height;
2437                 win->get_root_origin(x, y);
2438                 win->get_position(xoff, yoff);
2439                 win->get_size(width, height);
2440                 
2441                 XMLNode* geometry = new XMLNode ("geometry");
2442
2443                 snprintf(buf, sizeof(buf), "%d", width);
2444                 geometry->add_property("x-size", string(buf));
2445                 snprintf(buf, sizeof(buf), "%d", height);
2446                 geometry->add_property("y-size", string(buf));
2447                 snprintf(buf, sizeof(buf), "%d", x);
2448                 geometry->add_property("x-pos", string(buf));
2449                 snprintf(buf, sizeof(buf), "%d", y);
2450                 geometry->add_property("y-pos", string(buf));
2451                 snprintf(buf, sizeof(buf), "%d", xoff);
2452                 geometry->add_property("x-off", string(buf));
2453                 snprintf(buf, sizeof(buf), "%d", yoff);
2454                 geometry->add_property("y-off", string(buf));
2455                 snprintf(buf,sizeof(buf), "%d",gtk_paned_get_position (static_cast<Paned*>(&edit_pane)->gobj()));
2456                 geometry->add_property("edit_pane_pos", string(buf));
2457
2458                 node->add_child_nocopy (*geometry);
2459         }
2460
2461         maybe_add_mixer_strip_width (*node);
2462         
2463         snprintf (buf, sizeof(buf), "%d", (int) zoom_focus);
2464         node->add_property ("zoom-focus", buf);
2465         snprintf (buf, sizeof(buf), "%f", frames_per_unit);
2466         node->add_property ("zoom", buf);
2467         snprintf (buf, sizeof(buf), "%d", (int) snap_type);
2468         node->add_property ("snap-to", buf);
2469         snprintf (buf, sizeof(buf), "%d", (int) snap_mode);
2470         node->add_property ("snap-mode", buf);
2471
2472         node->add_property ("edit-point", enum_2_string (_edit_point));
2473
2474         snprintf (buf, sizeof (buf), "%" PRIi64, playhead_cursor->current_frame);
2475         node->add_property ("playhead", buf);
2476
2477         node->add_property ("show-waveforms-recording", _show_waveforms_recording ? "yes" : "no");
2478         node->add_property ("show-measures", _show_measures ? "yes" : "no");
2479         node->add_property ("follow-playhead", _follow_playhead ? "yes" : "no");
2480         node->add_property ("xfades-visible", _xfade_visibility ? "yes" : "no");
2481         node->add_property ("region-list-sort-type", enum2str (_regions->sort_type ()));
2482         node->add_property ("mouse-mode", enum2str(mouse_mode));
2483         
2484         Glib::RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
2485         if (act) {
2486                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2487                 node->add_property (X_("show-editor-mixer"), tact->get_active() ? "yes" : "no");
2488         }
2489         
2490         act = ActionManager::get_action (X_("Editor"), X_("show-editor-list"));
2491         if (act) {
2492                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2493                 node->add_property (X_("show-editor-list"), tact->get_active() ? "yes" : "no");
2494         }
2495
2496         return *node;
2497 }
2498
2499
2500
2501 /** @param y y offset from the top of all trackviews.
2502  *  @return pair: TimeAxisView that y is over, layer index.
2503  *  TimeAxisView may be 0.  Layer index is the layer number if the TimeAxisView is valid and is
2504  *  in stacked region display mode, otherwise 0.
2505  */
2506 std::pair<TimeAxisView *, layer_t>
2507 Editor::trackview_by_y_position (double y)
2508 {
2509         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
2510
2511                 std::pair<TimeAxisView*, int> const r = (*iter)->covers_y_position (y);
2512                 if (r.first) {
2513                         return r;
2514                 }
2515         }
2516
2517         return std::make_pair ( (TimeAxisView *) 0, 0);
2518 }
2519
2520 void
2521 Editor::snap_to (nframes64_t& start, int32_t direction, bool for_mark)
2522 {
2523         if (!session || snap_mode == SnapOff) {
2524                 return;
2525         }
2526
2527         snap_to_internal (start, direction, for_mark);
2528 }
2529
2530 void
2531 Editor::snap_to_internal (nframes64_t& start, int32_t direction, bool for_mark)
2532 {
2533         Location* before = 0;
2534         Location* after = 0;
2535
2536         const nframes64_t one_second = session->frame_rate();
2537         const nframes64_t one_minute = session->frame_rate() * 60;
2538         const nframes64_t one_smpte_second = (nframes64_t)(rint(session->smpte_frames_per_second()) * session->frames_per_smpte_frame());
2539         nframes64_t one_smpte_minute = (nframes64_t)(rint(session->smpte_frames_per_second()) * session->frames_per_smpte_frame() * 60);
2540         nframes64_t presnap = start;
2541
2542         switch (snap_type) {
2543         case SnapToCDFrame:
2544                 if (((direction == 0) && (start % (one_second/75) > (one_second/75) / 2)) || (direction > 0)) {
2545                         start = (nframes64_t) ceil ((double) start / (one_second / 75)) * (one_second / 75);
2546                 } else {
2547                         start = (nframes64_t) floor ((double) start / (one_second / 75)) * (one_second / 75);
2548                 }
2549                 break;
2550
2551         case SnapToSMPTEFrame:
2552                 if (((direction == 0) && (fmod((double)start, (double)session->frames_per_smpte_frame()) > (session->frames_per_smpte_frame() / 2))) || (direction > 0)) {
2553                         start = (nframes64_t) (ceil ((double) start / session->frames_per_smpte_frame()) * session->frames_per_smpte_frame());
2554                 } else {
2555                         start = (nframes64_t) (floor ((double) start / session->frames_per_smpte_frame()) *  session->frames_per_smpte_frame());
2556                 }
2557                 break;
2558
2559         case SnapToSMPTESeconds:
2560                 if (session->smpte_offset_negative())
2561                 {
2562                         start += session->smpte_offset ();
2563                 } else {
2564                         start -= session->smpte_offset ();
2565                 }    
2566                 if (((direction == 0) && (start % one_smpte_second > one_smpte_second / 2)) || direction > 0) {
2567                         start = (nframes64_t) ceil ((double) start / one_smpte_second) * one_smpte_second;
2568                 } else {
2569                         start = (nframes64_t) floor ((double) start / one_smpte_second) * one_smpte_second;
2570                 }
2571                 
2572                 if (session->smpte_offset_negative())
2573                 {
2574                         start -= session->smpte_offset ();
2575                 } else {
2576                         start += session->smpte_offset ();
2577                 }
2578                 break;
2579                 
2580         case SnapToSMPTEMinutes:
2581                 if (session->smpte_offset_negative())
2582                 {
2583                         start += session->smpte_offset ();
2584                 } else {
2585                         start -= session->smpte_offset ();
2586                 }
2587                 if (((direction == 0) && (start % one_smpte_minute > one_smpte_minute / 2)) || direction > 0) {
2588                         start = (nframes64_t) ceil ((double) start / one_smpte_minute) * one_smpte_minute;
2589                 } else {
2590                         start = (nframes64_t) floor ((double) start / one_smpte_minute) * one_smpte_minute;
2591                 }
2592                 if (session->smpte_offset_negative())
2593                 {
2594                         start -= session->smpte_offset ();
2595                 } else {
2596                         start += session->smpte_offset ();
2597                 }
2598                 break;
2599                 
2600         case SnapToSeconds:
2601                 if (((direction == 0) && (start % one_second > one_second / 2)) || (direction > 0)) {
2602                         start = (nframes64_t) ceil ((double) start / one_second) * one_second;
2603                 } else {
2604                         start = (nframes64_t) floor ((double) start / one_second) * one_second;
2605                 }
2606                 break;
2607                 
2608         case SnapToMinutes:
2609                 if (((direction == 0) && (start % one_minute > one_minute / 2)) || (direction > 0)) {
2610                         start = (nframes64_t) ceil ((double) start / one_minute) * one_minute;
2611                 } else {
2612                         start = (nframes64_t) floor ((double) start / one_minute) * one_minute;
2613                 }
2614                 break;
2615
2616         case SnapToBar:
2617                 start = session->tempo_map().round_to_bar (start, direction);
2618                 break;
2619
2620         case SnapToBeat:
2621                 start = session->tempo_map().round_to_beat (start, direction);
2622                 break;
2623
2624         case SnapToAThirtysecondBeat:
2625                 start = session->tempo_map().round_to_beat_subdivision (start, 32);
2626                 break;
2627
2628         case SnapToASixteenthBeat:
2629                 start = session->tempo_map().round_to_beat_subdivision (start, 16);
2630                 break;
2631
2632         case SnapToAEighthBeat:
2633                 start = session->tempo_map().round_to_beat_subdivision (start, 8);
2634                 break;
2635
2636         case SnapToAQuarterBeat:
2637                 start = session->tempo_map().round_to_beat_subdivision (start, 4);
2638                 break;
2639
2640         case SnapToAThirdBeat:
2641                 start = session->tempo_map().round_to_beat_subdivision (start, 3);
2642                 break;
2643
2644         case SnapToMark:
2645                 if (for_mark) {
2646                         return;
2647                 }
2648
2649                 before = session->locations()->first_location_before (start);
2650                 after = session->locations()->first_location_after (start);
2651
2652                 if (direction < 0) {
2653                         if (before) {
2654                                 start = before->start();
2655                         } else {
2656                                 start = 0;
2657                         }
2658                 } else if (direction > 0) {
2659                         if (after) {
2660                                 start = after->start();
2661                         } else {
2662                                 start = session->current_end_frame();
2663                         }
2664                 } else {
2665                         if (before) {
2666                                 if (after) {
2667                                         /* find nearest of the two */
2668                                         if ((start - before->start()) < (after->start() - start)) {
2669                                                 start = before->start();
2670                                         } else {
2671                                                 start = after->start();
2672                                         }
2673                                 } else {
2674                                         start = before->start();
2675                                 }
2676                         } else if (after) {
2677                                 start = after->start();
2678                         } else {
2679                                 /* relax */
2680                         }
2681                 }
2682                 break;
2683
2684         case SnapToRegionStart:
2685         case SnapToRegionEnd:
2686         case SnapToRegionSync:
2687         case SnapToRegionBoundary:
2688                 if (!region_boundary_cache.empty()) {
2689                         vector<nframes64_t>::iterator i;
2690
2691                         if (direction > 0) {
2692                                 i = std::upper_bound (region_boundary_cache.begin(), region_boundary_cache.end(), start);
2693                         } else {
2694                                 i = std::lower_bound (region_boundary_cache.begin(), region_boundary_cache.end(), start);
2695                         }
2696                         
2697                         if (i != region_boundary_cache.end()) {
2698
2699                                 /* lower bound doesn't quite to the right thing for our purposes */
2700
2701                                 if (direction < 0 && i != region_boundary_cache.begin()) {
2702                                         --i;
2703                                 }
2704
2705                                 start = *i;
2706
2707                         } else {
2708                                 start = region_boundary_cache.back();
2709                         }
2710                 } 
2711                 break;
2712         }
2713
2714         switch (snap_mode) {
2715         case SnapNormal:
2716                 return;                 
2717                 
2718         case SnapMagnetic:
2719                 
2720                 if (presnap > start) {
2721                         if (presnap > (start + unit_to_frame(snap_threshold))) {
2722                                 start = presnap;
2723                         }
2724                         
2725                 } else if (presnap < start) {
2726                         if (presnap < (start - unit_to_frame(snap_threshold))) {
2727                                 start = presnap;
2728                         }
2729                 }
2730                 
2731         default:
2732                 /* handled at entry */
2733                 return;
2734                 
2735         }
2736 }
2737
2738 double
2739 Editor::snap_length_beats (nframes64_t start)
2740 {
2741         if (!session) {
2742                 return 1.0;
2743         }
2744
2745         /* FIXME: This could/should also work with non-tempo based snap settings (ie seconds) */
2746
2747         switch (snap_type) {
2748         case SnapToBar:
2749                 return session->tempo_map().meter_at(start).beats_per_bar();
2750
2751         case SnapToBeat:
2752                 return 1.0;
2753
2754         case SnapToAThirtysecondBeat:
2755                 return 1.0 / (double)32.0;
2756                 break;
2757
2758         case SnapToASixteenthBeat:
2759                 return 1.0 / (double)16.0;
2760                 break;
2761
2762         case SnapToAEighthBeat:
2763                 return 1.0 / (double)8.0;
2764                 break;
2765
2766         case SnapToAQuarterBeat:
2767                 return 1.0 / (double)4.0;
2768                 break;
2769
2770         case SnapToAThirdBeat:
2771                 return 1.0 / (double)3.0;
2772
2773         default:
2774                 return 1.0;
2775         }
2776 }
2777
2778 void
2779 Editor::setup_toolbar ()
2780 {
2781         string pixmap_path;
2782
2783         /* Mode Buttons (tool selection) */
2784
2785         vector<ToggleButton *> mouse_mode_buttons;
2786
2787         mouse_move_button.add (*(manage (new Image (::get_icon("tool_object")))));
2788         mouse_move_button.set_relief(Gtk::RELIEF_NONE);
2789         mouse_mode_buttons.push_back (&mouse_move_button);
2790
2791         if (!Profile->get_sae()) {
2792                 mouse_select_button.add (*(manage (new Image (get_xpm("tool_range.xpm")))));
2793                 mouse_select_button.set_relief(Gtk::RELIEF_NONE);
2794                 mouse_mode_buttons.push_back (&mouse_select_button);
2795
2796                 mouse_gain_button.add (*(manage (new Image (::get_icon("tool_gain")))));
2797                 mouse_gain_button.set_relief(Gtk::RELIEF_NONE);
2798                 mouse_mode_buttons.push_back (&mouse_gain_button);
2799         }
2800
2801         mouse_zoom_button.add (*(manage (new Image (::get_icon("tool_zoom")))));
2802         mouse_zoom_button.set_relief(Gtk::RELIEF_NONE);
2803         mouse_mode_buttons.push_back (&mouse_zoom_button);
2804         mouse_timefx_button.add (*(manage (new Image (::get_icon("tool_stretch")))));
2805         mouse_timefx_button.set_relief(Gtk::RELIEF_NONE);
2806         mouse_mode_buttons.push_back (&mouse_timefx_button);
2807         mouse_audition_button.add (*(manage (new Image (::get_icon("tool_audition")))));
2808         mouse_audition_button.set_relief(Gtk::RELIEF_NONE);
2809         mouse_note_button.add (*(manage (new Image (::get_icon("tool_note")))));
2810         mouse_note_button.set_relief(Gtk::RELIEF_NONE);
2811         mouse_mode_buttons.push_back (&mouse_note_button);
2812         mouse_mode_buttons.push_back (&mouse_audition_button);
2813         
2814         mouse_mode_button_set = new GroupedButtons (mouse_mode_buttons);
2815
2816         HBox* mode_box = manage(new HBox);
2817         mode_box->set_border_width (2);
2818         mode_box->set_spacing(4);
2819         mouse_mode_button_box.set_spacing(1);
2820         mouse_mode_button_box.pack_start(mouse_move_button, true, true);
2821         if (!Profile->get_sae()) {
2822                 mouse_mode_button_box.pack_start(mouse_select_button, true, true);
2823         }
2824         mouse_mode_button_box.pack_start(mouse_zoom_button, true, true);
2825         if (!Profile->get_sae()) {
2826                 mouse_mode_button_box.pack_start(mouse_gain_button, true, true);
2827         }
2828         mouse_mode_button_box.pack_start(mouse_timefx_button, true, true);
2829         mouse_mode_button_box.pack_start(mouse_audition_button, true, true);
2830         mouse_mode_button_box.pack_start(mouse_note_button, true, true);
2831         mouse_mode_button_box.set_homogeneous(true);
2832
2833         vector<string> edit_mode_strings;
2834         edit_mode_strings.push_back (edit_mode_to_string (Slide));
2835         if (!Profile->get_sae()) {
2836                 edit_mode_strings.push_back (edit_mode_to_string (Splice));
2837         }
2838         edit_mode_strings.push_back (edit_mode_to_string (Lock));
2839
2840         edit_mode_selector.set_name ("EditModeSelector");
2841         set_popdown_strings (edit_mode_selector, edit_mode_strings, true);
2842         edit_mode_selector.signal_changed().connect (mem_fun(*this, &Editor::edit_mode_selection_done));
2843
2844         mode_box->pack_start(edit_mode_selector);
2845         mode_box->pack_start(mouse_mode_button_box);
2846         
2847         mouse_mode_tearoff = manage (new TearOff (*mode_box));
2848         mouse_mode_tearoff->set_name ("MouseModeBase");
2849
2850         if (Profile->get_sae()) {
2851                 mouse_mode_tearoff->set_can_be_torn_off (false);
2852         }
2853
2854         mouse_mode_tearoff->Detach.connect (bind (mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox), 
2855                                                   &mouse_mode_tearoff->tearoff_window()));
2856         mouse_mode_tearoff->Attach.connect (bind (mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox), 
2857                                                   &mouse_mode_tearoff->tearoff_window(), 1));
2858         mouse_mode_tearoff->Hidden.connect (bind (mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox), 
2859                                                   &mouse_mode_tearoff->tearoff_window()));
2860         mouse_mode_tearoff->Visible.connect (bind (mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox), 
2861                                                    &mouse_mode_tearoff->tearoff_window(), 1));
2862
2863         mouse_move_button.set_name ("MouseModeButton");
2864         mouse_select_button.set_name ("MouseModeButton");
2865         mouse_gain_button.set_name ("MouseModeButton");
2866         mouse_zoom_button.set_name ("MouseModeButton");
2867         mouse_timefx_button.set_name ("MouseModeButton");
2868         mouse_audition_button.set_name ("MouseModeButton");
2869         mouse_note_button.set_name ("MouseModeButton");
2870
2871         ARDOUR_UI::instance()->tooltips().set_tip (mouse_move_button, _("Select/Move Objects"));
2872         ARDOUR_UI::instance()->tooltips().set_tip (mouse_select_button, _("Select/Move Ranges"));
2873         ARDOUR_UI::instance()->tooltips().set_tip (mouse_gain_button, _("Draw Gain Automation"));
2874         ARDOUR_UI::instance()->tooltips().set_tip (mouse_zoom_button, _("Select Zoom Range"));
2875         ARDOUR_UI::instance()->tooltips().set_tip (mouse_timefx_button, _("Stretch/Shrink Regions"));
2876         ARDOUR_UI::instance()->tooltips().set_tip (mouse_audition_button, _("Listen to Specific Regions"));
2877         ARDOUR_UI::instance()->tooltips().set_tip (mouse_note_button, _("Edit MIDI Notes"));
2878
2879         mouse_move_button.unset_flags (CAN_FOCUS);
2880         mouse_select_button.unset_flags (CAN_FOCUS);
2881         mouse_gain_button.unset_flags (CAN_FOCUS);
2882         mouse_zoom_button.unset_flags (CAN_FOCUS);
2883         mouse_timefx_button.unset_flags (CAN_FOCUS);
2884         mouse_audition_button.unset_flags (CAN_FOCUS);
2885         mouse_note_button.unset_flags (CAN_FOCUS);
2886
2887         mouse_select_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseRange));
2888         mouse_select_button.signal_button_release_event().connect (mem_fun(*this, &Editor::mouse_select_button_release));
2889
2890         mouse_move_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseObject));
2891         mouse_gain_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseGain));
2892         mouse_zoom_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseZoom));
2893         mouse_timefx_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseTimeFX));
2894         mouse_audition_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseAudition));
2895         mouse_note_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseNote));
2896
2897         // mouse_move_button.set_active (true);
2898         
2899
2900         /* Zoom */
2901         
2902         zoom_box.set_spacing (1);
2903         zoom_box.set_border_width (0);
2904
2905         zoom_in_button.set_name ("EditorTimeButton");
2906         zoom_in_button.set_size_request(-1,16);
2907         zoom_in_button.add (*(manage (new Image (::get_icon("zoom_in")))));
2908         zoom_in_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::temporal_zoom_step), false));
2909         ARDOUR_UI::instance()->tooltips().set_tip (zoom_in_button, _("Zoom In"));
2910         
2911         zoom_out_button.set_name ("EditorTimeButton");
2912         zoom_out_button.set_size_request(-1,16);
2913         zoom_out_button.add (*(manage (new Image (::get_icon("zoom_out")))));
2914         zoom_out_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::temporal_zoom_step), true));
2915         ARDOUR_UI::instance()->tooltips().set_tip (zoom_out_button, _("Zoom Out"));
2916
2917         zoom_out_full_button.set_name ("EditorTimeButton");
2918         zoom_out_full_button.set_size_request(-1,16);
2919         zoom_out_full_button.add (*(manage (new Image (::get_icon("zoom_full")))));
2920         zoom_out_full_button.signal_clicked().connect (mem_fun(*this, &Editor::temporal_zoom_session));
2921         ARDOUR_UI::instance()->tooltips().set_tip (zoom_out_full_button, _("Zoom to Session"));
2922
2923         zoom_focus_selector.set_name ("ZoomFocusSelector");
2924         set_popdown_strings (zoom_focus_selector, zoom_focus_strings, true);
2925         zoom_focus_selector.signal_changed().connect (mem_fun(*this, &Editor::zoom_focus_selection_done));
2926         ARDOUR_UI::instance()->tooltips().set_tip (zoom_focus_selector, _("Zoom focus"));
2927
2928         zoom_box.pack_start (zoom_out_button, false, false);
2929         zoom_box.pack_start (zoom_in_button, false, false);
2930         zoom_box.pack_start (zoom_out_full_button, false, false);
2931
2932         HBox* zbc = manage (new HBox);
2933         zbc->pack_start (zoom_focus_selector, PACK_SHRINK);
2934         zoom_vbox.pack_start (*zbc, PACK_SHRINK);
2935         zoom_vbox.pack_start (zoom_box, PACK_SHRINK);
2936
2937         snap_box.set_spacing (1);
2938         snap_box.set_border_width (2);
2939
2940         snap_type_selector.set_name ("SnapTypeSelector");
2941         set_popdown_strings (snap_type_selector, snap_type_strings, true);
2942         snap_type_selector.signal_changed().connect (mem_fun(*this, &Editor::snap_type_selection_done));
2943         ARDOUR_UI::instance()->tooltips().set_tip (snap_type_selector, _("Snap/Grid Units"));
2944
2945         snap_mode_selector.set_name ("SnapModeSelector");
2946         set_popdown_strings (snap_mode_selector, snap_mode_strings, true);
2947         snap_mode_selector.signal_changed().connect (mem_fun(*this, &Editor::snap_mode_selection_done));
2948         ARDOUR_UI::instance()->tooltips().set_tip (snap_mode_selector, _("Snap/Grid Mode"));
2949
2950         edit_point_selector.set_name ("EditPointSelector");
2951         set_popdown_strings (edit_point_selector, edit_point_strings, true);
2952         edit_point_selector.signal_changed().connect (mem_fun(*this, &Editor::edit_point_selection_done));
2953         ARDOUR_UI::instance()->tooltips().set_tip (edit_point_selector, _("Edit point"));
2954
2955         snap_box.pack_start (edit_point_clock, false, false);
2956         snap_box.pack_start (snap_mode_selector, false, false);
2957         snap_box.pack_start (snap_type_selector, false, false);
2958         snap_box.pack_start (edit_point_selector, false, false);
2959
2960         /* Nudge */
2961
2962         HBox *nudge_box = manage (new HBox);
2963         nudge_box->set_spacing(1);
2964         nudge_box->set_border_width (2);
2965
2966         nudge_forward_button.signal_button_release_event().connect (mem_fun(*this, &Editor::nudge_forward_release), false);
2967         nudge_backward_button.signal_button_release_event().connect (mem_fun(*this, &Editor::nudge_backward_release), false);
2968
2969         nudge_box->pack_start (nudge_backward_button, false, false);
2970         nudge_box->pack_start (nudge_forward_button, false, false);
2971         nudge_box->pack_start (nudge_clock, false, false);
2972
2973
2974         /* Pack everything in... */
2975
2976         HBox* hbox = manage (new HBox);
2977         hbox->set_spacing(10);
2978
2979         tools_tearoff = manage (new TearOff (*hbox));
2980         tools_tearoff->set_name ("MouseModeBase");
2981
2982         if (Profile->get_sae()) {
2983                 tools_tearoff->set_can_be_torn_off (false);
2984         }
2985
2986         tools_tearoff->Detach.connect (bind (mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox), 
2987                                              &tools_tearoff->tearoff_window()));
2988         tools_tearoff->Attach.connect (bind (mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox), 
2989                                              &tools_tearoff->tearoff_window(), 0));
2990         tools_tearoff->Hidden.connect (bind (mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox), 
2991                                              &tools_tearoff->tearoff_window()));
2992         tools_tearoff->Visible.connect (bind (mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox), 
2993                                               &tools_tearoff->tearoff_window(), 0));
2994
2995         toolbar_hbox.set_spacing (10);
2996         toolbar_hbox.set_border_width (1);
2997
2998         toolbar_hbox.pack_start (*mouse_mode_tearoff, false, false);
2999         toolbar_hbox.pack_start (*tools_tearoff, false, false);
3000
3001         
3002         hbox->pack_start (snap_box, false, false);
3003         // hbox->pack_start (zoom_box, false, false); 
3004         hbox->pack_start (*nudge_box, false, false);
3005
3006         hbox->show_all ();
3007         
3008         toolbar_base.set_name ("ToolBarBase");
3009         toolbar_base.add (toolbar_hbox);
3010
3011         toolbar_frame.set_shadow_type (SHADOW_OUT);
3012         toolbar_frame.set_name ("BaseFrame");
3013         toolbar_frame.add (toolbar_base);
3014 }
3015
3016 void
3017 Editor::midi_panic_button_pressed ()
3018 {
3019         if (session) {
3020                 session->midi_panic();
3021         }
3022 }
3023
3024 void
3025 Editor::setup_midi_toolbar ()
3026 {
3027         string pixmap_path;
3028
3029         /* Mode Buttons (tool selection) */
3030
3031         vector<ToggleButton *> midi_tool_buttons;
3032
3033         midi_tool_pencil_button.add (*(manage (new Image (::get_icon("midi_tool_pencil")))));
3034         midi_tool_pencil_button.set_relief(Gtk::RELIEF_NONE);
3035         midi_tool_buttons.push_back (&midi_tool_pencil_button);
3036         midi_tool_select_button.add (*(manage (new Image (::get_icon("midi_tool_select")))));
3037         midi_tool_select_button.set_relief(Gtk::RELIEF_NONE);
3038         midi_tool_buttons.push_back (&midi_tool_select_button);
3039         midi_tool_resize_button.add (*(manage (new Image (::get_icon("strip_width")))));
3040         midi_tool_resize_button.set_relief(Gtk::RELIEF_NONE);
3041         midi_tool_buttons.push_back (&midi_tool_resize_button);
3042         midi_tool_erase_button.add (*(manage (new Image (::get_icon("midi_tool_erase")))));
3043         midi_tool_erase_button.set_relief(Gtk::RELIEF_NONE);
3044         midi_tool_buttons.push_back (&midi_tool_erase_button);
3045
3046         midi_tool_pencil_button.set_active(true);
3047         
3048         midi_tool_button_set = new GroupedButtons (midi_tool_buttons);
3049
3050         midi_tool_button_box.set_border_width (2);
3051         midi_tool_button_box.set_spacing(1);
3052         midi_tool_button_box.pack_start(midi_tool_pencil_button, true, true);
3053         midi_tool_button_box.pack_start(midi_tool_select_button, true, true);
3054         midi_tool_button_box.pack_start(midi_tool_resize_button, true, true);
3055         midi_tool_button_box.pack_start(midi_tool_erase_button , true, true);
3056         midi_tool_button_box.set_homogeneous(true);
3057
3058         midi_tool_pencil_button.set_name ("MouseModeButton");
3059         midi_tool_select_button.set_name ("MouseModeButton");
3060         midi_tool_resize_button.set_name ("MouseModeButton");
3061         midi_tool_erase_button .set_name ("MouseModeButton");
3062
3063         ARDOUR_UI::instance()->tooltips().set_tip (midi_tool_pencil_button, _("Add/Move/Stretch Notes"));
3064         ARDOUR_UI::instance()->tooltips().set_tip (midi_tool_select_button, _("Select/Move Notes"));
3065         ARDOUR_UI::instance()->tooltips().set_tip (midi_tool_resize_button, _("Resize Notes"));
3066         ARDOUR_UI::instance()->tooltips().set_tip (midi_tool_erase_button,  _("Erase Notes"));
3067
3068         midi_tool_pencil_button.unset_flags (CAN_FOCUS);
3069         midi_tool_select_button.unset_flags (CAN_FOCUS);
3070         midi_tool_resize_button.unset_flags (CAN_FOCUS);
3071         midi_tool_erase_button.unset_flags (CAN_FOCUS);
3072         
3073         midi_tool_pencil_button.signal_toggled().connect (bind (mem_fun(*this,
3074                                 &Editor::midi_edit_mode_toggled), Editing::MidiEditPencil));
3075         midi_tool_select_button.signal_toggled().connect (bind (mem_fun(*this,
3076                                 &Editor::midi_edit_mode_toggled), Editing::MidiEditSelect));
3077         midi_tool_resize_button.signal_toggled().connect (bind (mem_fun(*this,
3078                                 &Editor::midi_edit_mode_toggled), Editing::MidiEditResize));
3079         midi_tool_erase_button.signal_toggled().connect (bind (mem_fun(*this,
3080                                 &Editor::midi_edit_mode_toggled), Editing::MidiEditErase));
3081
3082         
3083         /* Midi sound notes */
3084         midi_sound_notes.add (*(manage (new Image (::get_icon("midi_sound_notes")))));
3085         midi_sound_notes.set_relief(Gtk::RELIEF_NONE);
3086         ARDOUR_UI::instance()->tooltips().set_tip (midi_sound_notes, _("Sound Notes"));
3087         midi_sound_notes.unset_flags (CAN_FOCUS);
3088         
3089         /* Panic */
3090         
3091         HBox* panic_box = manage (new HBox);
3092         midi_panic_button.set_name("MidiPanicButton");
3093         midi_panic_button.signal_pressed().connect (
3094                         mem_fun(this, &Editor::midi_panic_button_pressed));
3095         panic_box->pack_start (midi_sound_notes , true, true);
3096         panic_box->pack_start (midi_panic_button, true, true);
3097         
3098         /* Pack everything in... */
3099
3100         midi_tools_tearoff = manage (new TearOff (midi_tool_button_box));
3101         midi_tools_tearoff->set_name ("MouseModeBase");
3102
3103         /*
3104         midi_tools_tearoff->Detach.connect (bind (mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&midi_toolbar_hbox), 
3105                                              &midi_tools_tearoff->tearoff_window()));
3106         midi_tools_tearoff->Attach.connect (bind (mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&midi_toolbar_hbox), 
3107                                              &midi_tools_tearoff->tearoff_window(), 0));
3108         midi_tools_tearoff->Hidden.connect (bind (mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&midi_toolbar_hbox), 
3109                                              &midi_tools_tearoff->tearoff_window()));
3110         midi_tools_tearoff->Visible.connect (bind (mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&midi_toolbar_hbox), 
3111                                               &midi_tools_tearoff->tearoff_window(), 0));
3112         */
3113
3114         midi_toolbar_hbox.set_spacing (10);
3115         midi_toolbar_hbox.set_border_width (1);
3116
3117         midi_toolbar_hbox.pack_start (*midi_tools_tearoff, false, true);
3118         
3119         midi_toolbar_hbox.pack_start(*panic_box, false, true, 4);
3120
3121         midi_tool_button_box.show_all ();
3122         midi_toolbar_hbox.show_all();
3123         midi_tools_tearoff->show_all();
3124         
3125         midi_toolbar_base.set_name ("ToolBarBase");
3126         midi_toolbar_base.add (midi_toolbar_hbox);
3127
3128         midi_toolbar_frame.set_shadow_type (SHADOW_OUT);
3129         midi_toolbar_frame.set_name ("BaseFrame");
3130         midi_toolbar_frame.add (midi_toolbar_base);
3131 }
3132
3133 int
3134 Editor::convert_drop_to_paths (
3135                 vector<ustring>&                paths, 
3136                 const RefPtr<Gdk::DragContext>& context,
3137                 gint                            x,
3138                 gint                            y,
3139                 const SelectionData&            data,
3140                 guint                           info,
3141                 guint                           time)
3142 {       
3143         if (session == 0) {
3144                 return -1;
3145         }
3146
3147         vector<ustring> uris = data.get_uris();
3148
3149         if (uris.empty()) {
3150
3151                 /* This is seriously fucked up. Nautilus doesn't say that its URI lists
3152                    are actually URI lists. So do it by hand.
3153                 */
3154
3155                 if (data.get_target() != "text/plain") {
3156                         return -1;
3157                 }
3158   
3159                 /* Parse the "uri-list" format that Nautilus provides, 
3160                    where each pathname is delimited by \r\n.
3161
3162                    THERE MAY BE NO NULL TERMINATING CHAR!!!
3163                 */
3164
3165                 ustring txt = data.get_text();
3166                 const char* p;
3167                 const char* q;
3168
3169                 p = (const char *) malloc (txt.length() + 1);
3170                 txt.copy ((char *) p, txt.length(), 0);
3171                 ((char*)p)[txt.length()] = '\0';
3172
3173                 while (p)
3174                 {
3175                         if (*p != '#')
3176                         {
3177                                 while (g_ascii_isspace (*p))
3178                                         p++;
3179                                 
3180                                 q = p;
3181                                 while (*q && (*q != '\n') && (*q != '\r')) {
3182                                         q++;
3183                                 }
3184                                 
3185                                 if (q > p)
3186                                 {
3187                                         q--;
3188                                         while (q > p && g_ascii_isspace (*q))
3189                                                 q--;
3190                                         
3191                                         if (q > p)
3192                                         {
3193                                                 uris.push_back (ustring (p, q - p + 1));
3194                                         }
3195                                 }
3196                         }
3197                         p = strchr (p, '\n');
3198                         if (p)
3199                                 p++;
3200                 }
3201
3202                 free ((void*)p);
3203                 
3204                 if (uris.empty()) {
3205                         return -1;
3206                 }
3207         }
3208         
3209         for (vector<ustring>::iterator i = uris.begin(); i != uris.end(); ++i) {
3210
3211                 if ((*i).substr (0,7) == "file://") {
3212                         
3213                         ustring p = *i;
3214                         PBD::url_decode (p);
3215
3216                         // scan forward past three slashes
3217                         
3218                         ustring::size_type slashcnt = 0;
3219                         ustring::size_type n = 0;
3220                         ustring::iterator x = p.begin();
3221
3222                         while (slashcnt < 3 && x != p.end()) {
3223                                 if ((*x) == '/') {
3224                                         slashcnt++;
3225                                 } else if (slashcnt == 3) {
3226                                         break;
3227                                 }
3228                                 ++n;
3229                                 ++x;
3230                         }
3231
3232                         if (slashcnt != 3 || x == p.end()) {
3233                                 error << _("malformed URL passed to drag-n-drop code") << endmsg;
3234                                 continue;
3235                         }
3236
3237                         paths.push_back (p.substr (n - 1));
3238                 }
3239         }
3240
3241         return 0;
3242 }
3243
3244 void
3245 Editor::new_tempo_section ()
3246
3247 {
3248 }
3249
3250 void
3251 Editor::map_transport_state ()
3252 {
3253         ENSURE_GUI_THREAD (mem_fun(*this, &Editor::map_transport_state));
3254
3255         if (session->transport_stopped()) {
3256                 have_pending_keyboard_selection = false;
3257         }
3258
3259         update_loop_range_view (true);
3260 }
3261
3262 /* UNDO/REDO */
3263
3264 Editor::State::State (PublicEditor const * e)
3265 {
3266         selection = new Selection (e);
3267 }
3268
3269 Editor::State::~State ()
3270 {
3271         delete selection;
3272 }
3273
3274 void
3275 Editor::store_state (State& state) const
3276 {
3277         *state.selection = *selection;
3278 }
3279
3280 void
3281 Editor::restore_state (State *state)
3282 {
3283         if (*selection == *state->selection) {
3284                 return;
3285         }
3286
3287         *selection = *state->selection;
3288         time_selection_changed ();
3289         region_selection_changed ();
3290
3291         /* XXX other selection change handlers? */
3292 }
3293
3294 void
3295 Editor::begin_reversible_command (string name)
3296 {
3297         if (session) {
3298                 before = &get_state();
3299                 session->begin_reversible_command (name);
3300         }
3301 }
3302
3303 void
3304 Editor::commit_reversible_command ()
3305 {
3306         if (session) {
3307                 session->commit_reversible_command (new MementoCommand<Editor>(*this, before, &get_state()));
3308         }
3309 }
3310
3311 void
3312 Editor::set_route_group_solo (Route& route, bool yn)
3313 {
3314         RouteGroup *route_group;
3315
3316         if ((route_group = route.route_group()) != 0) {
3317                 route_group->apply (&Route::set_solo, yn, this);
3318         } else {
3319                 route.set_solo (yn, this);
3320         }
3321 }
3322
3323 void
3324 Editor::set_route_group_mute (Route& route, bool yn)
3325 {
3326         RouteGroup *route_group = 0;
3327
3328         if ((route_group == route.route_group()) != 0) {
3329                 route_group->apply (&Route::set_mute, yn, this);
3330         } else {
3331                 route.set_mute (yn, this);
3332         }
3333 }
3334                 
3335 void
3336 Editor::history_changed ()
3337 {
3338         string label;
3339
3340         if (undo_action && session) {
3341                 if (session->undo_depth() == 0) {
3342                         label = _("Undo");
3343                 } else {
3344                         label = string_compose(_("Undo (%1)"), session->next_undo());
3345                 }
3346                 undo_action->property_label() = label;
3347         }
3348
3349         if (redo_action && session) {
3350                 if (session->redo_depth() == 0) {
3351                         label = _("Redo");
3352                 } else {
3353                         label = string_compose(_("Redo (%1)"), session->next_redo());
3354                 }
3355                 redo_action->property_label() = label;
3356         }
3357 }
3358
3359 void
3360 Editor::duplicate_dialog (bool with_dialog)
3361 {
3362         float times = 1.0f;
3363
3364         if (mouse_mode == MouseRange) {
3365                 if (selection->time.length() == 0) {
3366                         return;
3367                 }
3368         }
3369
3370         RegionSelection rs;
3371         get_regions_for_action (rs);
3372         
3373         if (mouse_mode != MouseRange) {
3374
3375                 if (rs.empty()) {
3376                         return;
3377                 }
3378         }
3379
3380         if (with_dialog) {
3381
3382                 ArdourDialog win ("Duplication Dialog");
3383                 Label  label (_("Number of Duplications:"));
3384                 Adjustment adjustment (1.0, 1.0, 1000000.0, 1.0, 5.0);
3385                 SpinButton spinner (adjustment, 0.0, 1);
3386                 HBox hbox;
3387                 
3388                 win.get_vbox()->set_spacing (12);
3389                 win.get_vbox()->pack_start (hbox);
3390                 hbox.set_border_width (6);
3391                 hbox.pack_start (label, PACK_EXPAND_PADDING, 12);
3392                 
3393                 /* dialogs have ::add_action_widget() but that puts the spinner in the wrong
3394                    place, visually. so do this by hand.
3395                 */
3396                 
3397                 hbox.pack_start (spinner, PACK_EXPAND_PADDING, 12);
3398                 spinner.signal_activate().connect (sigc::bind (mem_fun (win, &ArdourDialog::response), RESPONSE_ACCEPT));
3399                 spinner.grab_focus();
3400
3401                 hbox.show ();
3402                 label.show ();
3403                 spinner.show ();
3404                 
3405                 win.add_button (Stock::CANCEL, RESPONSE_CANCEL);
3406                 win.add_button (_("Duplicate"), RESPONSE_ACCEPT);
3407                 win.set_default_response (RESPONSE_ACCEPT);
3408                 
3409                 win.set_position (WIN_POS_MOUSE);
3410                 
3411                 spinner.grab_focus ();
3412                 
3413                 switch (win.run ()) {
3414                 case RESPONSE_ACCEPT:
3415                         break;
3416                 default:
3417                         return;
3418                 }
3419                 
3420                 times = adjustment.get_value();
3421         }
3422
3423         if (mouse_mode == MouseRange) {
3424                 duplicate_selection (times);
3425         } else {
3426                 duplicate_some_regions (rs, times);
3427         }
3428 }
3429
3430 void
3431 Editor::show_verbose_canvas_cursor ()
3432 {
3433         verbose_canvas_cursor->raise_to_top();
3434         verbose_canvas_cursor->show();
3435         verbose_cursor_visible = true;
3436 }
3437
3438 void
3439 Editor::hide_verbose_canvas_cursor ()
3440 {
3441         verbose_canvas_cursor->hide();
3442         verbose_cursor_visible = false;
3443 }
3444
3445 double
3446 Editor::clamp_verbose_cursor_x (double x)
3447 {
3448         if (x < 0) {
3449                 x = 0;
3450         } else {
3451                 x = min (_canvas_width - 200.0, x);
3452         }
3453         return x;
3454 }
3455
3456 double
3457 Editor::clamp_verbose_cursor_y (double y)
3458 {
3459         if (y < canvas_timebars_vsize) {
3460                 y = canvas_timebars_vsize;
3461         } else {
3462                 y = min (_canvas_height - 50, y);
3463         }
3464         return y;
3465 }
3466
3467 void
3468 Editor::set_verbose_canvas_cursor (const string & txt, double x, double y)
3469 {
3470         verbose_canvas_cursor->property_text() = txt.c_str();
3471         /* don't get too close to the edge */
3472         verbose_canvas_cursor->property_x() = clamp_verbose_cursor_x (x);
3473         verbose_canvas_cursor->property_y() = clamp_verbose_cursor_y (y);
3474 }
3475
3476 void
3477 Editor::set_verbose_canvas_cursor_text (const string & txt)
3478 {
3479         verbose_canvas_cursor->property_text() = txt.c_str();
3480 }
3481
3482 void
3483 Editor::set_edit_mode (EditMode m)
3484 {
3485         Config->set_edit_mode (m);
3486 }
3487
3488 void
3489 Editor::cycle_edit_mode ()
3490 {
3491         switch (Config->get_edit_mode()) {
3492         case Slide:
3493                 if (Profile->get_sae()) {
3494                         Config->set_edit_mode (Lock);
3495                 } else {
3496                         Config->set_edit_mode (Splice);
3497                 }
3498                 break;
3499         case Splice:
3500                 Config->set_edit_mode (Lock);
3501                 break;
3502         case Lock:
3503                 Config->set_edit_mode (Slide);
3504                 break;
3505         }
3506 }
3507
3508 void
3509 Editor::edit_mode_selection_done ()
3510 {
3511         if (session == 0) {
3512                 return;
3513         }
3514
3515         string choice = edit_mode_selector.get_active_text();
3516         EditMode mode = Slide;
3517
3518         if (choice == _("Splice Edit")) {
3519                 mode = Splice;
3520         } else if (choice == _("Slide Edit")) {
3521                 mode = Slide;
3522         } else if (choice == _("Lock Edit")) {
3523                 mode = Lock;
3524         }
3525
3526         Config->set_edit_mode (mode);
3527 }       
3528
3529 void
3530 Editor::snap_type_selection_done ()
3531 {
3532         string choice = snap_type_selector.get_active_text();
3533         SnapType snaptype = SnapToBeat;
3534
3535         if (choice == _("Beats/3")) {
3536                 snaptype = SnapToAThirdBeat;
3537         } else if (choice == _("Beats/4")) {
3538                 snaptype = SnapToAQuarterBeat;
3539         } else if (choice == _("Beats/8")) {
3540                 snaptype = SnapToAEighthBeat;
3541         } else if (choice == _("Beats/16")) {
3542                 snaptype = SnapToASixteenthBeat;
3543         } else if (choice == _("Beats/32")) {
3544                 snaptype = SnapToAThirtysecondBeat;
3545         } else if (choice == _("Beats")) {
3546                 snaptype = SnapToBeat;
3547         } else if (choice == _("Bars")) {
3548                 snaptype = SnapToBar;
3549         } else if (choice == _("Marks")) {
3550                 snaptype = SnapToMark;
3551         } else if (choice == _("Region starts")) {
3552                 snaptype = SnapToRegionStart;
3553         } else if (choice == _("Region ends")) {
3554                 snaptype = SnapToRegionEnd;
3555         } else if (choice == _("Region bounds")) {
3556                 snaptype = SnapToRegionBoundary;
3557         } else if (choice == _("Region syncs")) {
3558                 snaptype = SnapToRegionSync;
3559         } else if (choice == _("CD Frames")) {
3560                 snaptype = SnapToCDFrame;
3561         } else if (choice == _("SMPTE Frames")) {
3562                 snaptype = SnapToSMPTEFrame;
3563         } else if (choice == _("SMPTE Seconds")) {
3564                 snaptype = SnapToSMPTESeconds;
3565         } else if (choice == _("SMPTE Minutes")) {
3566                 snaptype = SnapToSMPTEMinutes;
3567         } else if (choice == _("Seconds")) {
3568                 snaptype = SnapToSeconds;
3569         } else if (choice == _("Minutes")) {
3570                 snaptype = SnapToMinutes;
3571         }
3572
3573         RefPtr<RadioAction> ract = snap_type_action (snaptype);
3574         if (ract) {
3575                 ract->set_active ();
3576         }
3577 }       
3578
3579 void
3580 Editor::snap_mode_selection_done ()
3581 {
3582         string choice = snap_mode_selector.get_active_text();
3583         SnapMode mode = SnapNormal;
3584
3585         if (choice == _("No Grid")) {
3586                 mode = SnapOff;
3587         } else if (choice == _("Grid")) {
3588                 mode = SnapNormal;
3589         } else if (choice == _("Magnetic")) {
3590                 mode = SnapMagnetic;
3591         }
3592
3593         RefPtr<RadioAction> ract = snap_mode_action (mode);
3594
3595         if (ract) {
3596                 ract->set_active (true);
3597         }
3598 }
3599
3600 void
3601 Editor::cycle_edit_point (bool with_marker)
3602 {
3603         switch (_edit_point) {
3604         case EditAtMouse:
3605                 set_edit_point_preference (EditAtPlayhead);
3606                 break;
3607         case EditAtPlayhead:
3608                 if (with_marker) {
3609                         set_edit_point_preference (EditAtSelectedMarker);
3610                 } else {
3611                         set_edit_point_preference (EditAtMouse);
3612                 }
3613                 break;
3614         case EditAtSelectedMarker:
3615                 set_edit_point_preference (EditAtMouse);
3616                 break;
3617         }
3618 }
3619
3620 void
3621 Editor::edit_point_selection_done ()
3622 {
3623         string choice = edit_point_selector.get_active_text();
3624         EditPoint ep = EditAtSelectedMarker;
3625
3626         if (choice == _("Marker")) {
3627                 set_edit_point_preference (EditAtSelectedMarker);
3628         } else if (choice == _("Playhead")) {
3629                 set_edit_point_preference (EditAtPlayhead);
3630         } else {
3631                 set_edit_point_preference (EditAtMouse);
3632         }
3633
3634         RefPtr<RadioAction> ract = edit_point_action (ep);
3635
3636         if (ract) {
3637                 ract->set_active (true);
3638         }
3639 }
3640
3641 void
3642 Editor::zoom_focus_selection_done ()
3643 {
3644         string choice = zoom_focus_selector.get_active_text();
3645         ZoomFocus focus_type = ZoomFocusLeft;
3646
3647         if (choice == _("Left")) {
3648                 focus_type = ZoomFocusLeft;
3649         } else if (choice == _("Right")) {
3650                 focus_type = ZoomFocusRight;
3651         } else if (choice == _("Center")) {
3652                 focus_type = ZoomFocusCenter;
3653         } else if (choice == _("Play")) {
3654                 focus_type = ZoomFocusPlayhead;
3655         } else if (choice == _("Edit")) {
3656                 focus_type = ZoomFocusEdit;
3657         } else if (choice == _("Active Mark")) {
3658                 focus_type = ZoomFocusEdit;
3659         } else if (choice == _("Active Mark")) {
3660                 focus_type = ZoomFocusEdit;
3661         } else {
3662                 focus_type = ZoomFocusMouse;
3663         } 
3664         
3665         RefPtr<RadioAction> ract = zoom_focus_action (focus_type);
3666
3667         if (ract) {
3668                 ract->set_active ();
3669         }
3670 }       
3671
3672 gint
3673 Editor::edit_controls_button_release (GdkEventButton* ev)
3674 {
3675         if (Keyboard::is_context_menu_event (ev)) {
3676                 ARDOUR_UI::instance()->add_route (this);
3677         }
3678         return TRUE;
3679 }
3680
3681 gint
3682 Editor::mouse_select_button_release (GdkEventButton* ev)
3683 {
3684         /* this handles just right-clicks */
3685
3686         if (ev->button != 3) {
3687                 return false;
3688         }
3689
3690         return true;
3691 }
3692
3693 Editor::TrackViewList *
3694 Editor::get_valid_views (TimeAxisView* track, RouteGroup* group)
3695 {
3696         TrackViewList::iterator i;
3697
3698         TrackViewList* v = new TrackViewList;
3699
3700         if (track == 0 && group == 0) {
3701
3702                 /* all views */
3703
3704                 for (i = track_views.begin(); i != track_views.end (); ++i) {
3705                         v->push_back (*i);
3706                 }
3707
3708         } else if ((track != 0 && group == 0) || (track != 0 && group != 0 && !group->active_property (RouteGroup::Select))) {
3709                 
3710                 /* just the view for this track
3711                  */
3712
3713                 v->push_back (track);
3714
3715         } else {
3716                 
3717                 /* views for all tracks in the route group */
3718                 
3719                 for (i = track_views.begin(); i != track_views.end (); ++i) {
3720
3721                         if (group == 0 || ((*i)->route_group() == group && group->active_property (RouteGroup::Select))) {
3722                                 v->push_back (*i);
3723                         }
3724                 }
3725         }
3726         
3727         return v;
3728 }
3729
3730 void
3731 Editor::set_zoom_focus (ZoomFocus f)
3732 {
3733         string str = zoom_focus_strings[(int)f];
3734
3735         if (str != zoom_focus_selector.get_active_text()) {
3736                 zoom_focus_selector.set_active_text (str);
3737         }
3738         
3739         if (zoom_focus != f) {
3740                 zoom_focus = f;
3741
3742                 ZoomFocusChanged (); /* EMIT_SIGNAL */
3743
3744                 instant_save ();
3745         }
3746 }
3747
3748 void
3749 Editor::ensure_float (Window& win)
3750 {
3751         win.set_transient_for (*this);
3752 }
3753
3754 void 
3755 Editor::pane_allocation_handler (Allocation &alloc, Paned* which)
3756 {
3757         /* recover or initialize pane positions. do this here rather than earlier because
3758            we don't want the positions to change the child allocations, which they seem to do.
3759          */
3760
3761         int pos;
3762         XMLProperty* prop;
3763         char buf[32];
3764         XMLNode* node = ARDOUR_UI::instance()->editor_settings();
3765         int width, height;
3766         static int32_t done;
3767         XMLNode* geometry;
3768
3769         width = default_width;
3770         height = default_height;
3771
3772         if ((geometry = find_named_node (*node, "geometry")) != 0) {
3773
3774                 if ((prop = geometry->property ("x_size")) == 0) {
3775                         prop = geometry->property ("x-size");
3776                 }
3777                 if (prop) {
3778                         width = atoi (prop->value());
3779                 }
3780                 if ((prop = geometry->property ("y_size")) == 0) {
3781                         prop = geometry->property ("y-size");
3782                 }
3783                 if (prop) {
3784                         height = atoi (prop->value());
3785                 }
3786         }
3787
3788         if (which == static_cast<Paned*> (&edit_pane)) {
3789
3790                 if (done) {
3791                         return;
3792                 }
3793
3794                 if (!geometry || (prop = geometry->property ("edit-pane-pos")) == 0) {
3795                         /* initial allocation is 90% to canvas, 10% to notebook */
3796                         pos = (int) floor (alloc.get_width() * 0.90f);
3797                         snprintf (buf, sizeof(buf), "%d", pos);
3798                 } else {
3799                         pos = atoi (prop->value());
3800                 }
3801
3802                 if ((done = GTK_WIDGET(edit_pane.gobj())->allocation.width > pos)) {
3803                         edit_pane.set_position (pos);
3804                         pre_maximal_pane_position = pos;
3805                 }
3806         }
3807 }
3808
3809 void
3810 Editor::detach_tearoff (Box* b, Window* w)
3811 {
3812         if (tools_tearoff->torn_off() && 
3813             mouse_mode_tearoff->torn_off()) {
3814                 top_hbox.remove (toolbar_frame);
3815         }
3816 }
3817
3818 void
3819 Editor::reattach_tearoff (Box* b, Window* w, int32_t n)
3820 {
3821         if (toolbar_frame.get_parent() == 0) {
3822                 top_hbox.pack_end (toolbar_frame);
3823         }
3824 }
3825
3826 void
3827 Editor::set_show_measures (bool yn)
3828 {
3829         if (_show_measures != yn) {
3830                 hide_measures ();
3831
3832                 if ((_show_measures = yn) == true) {
3833                         if (tempo_lines)
3834                                 tempo_lines->show();
3835                         draw_measures ();
3836                 }
3837                 instant_save ();
3838         }
3839 }
3840
3841 void
3842 Editor::toggle_follow_playhead ()
3843 {
3844         RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("toggle-follow-playhead"));
3845         if (act) {
3846                 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
3847                 set_follow_playhead (tact->get_active());
3848         }
3849 }
3850
3851 void
3852 Editor::set_follow_playhead (bool yn)
3853 {
3854         if (_follow_playhead != yn) {
3855                 if ((_follow_playhead = yn) == true) {
3856                         /* catch up */
3857                         update_current_screen ();
3858                 }
3859                 instant_save ();
3860         }
3861 }
3862
3863 void
3864 Editor::toggle_xfade_active (boost::weak_ptr<Crossfade> wxfade)
3865 {
3866         boost::shared_ptr<Crossfade> xfade (wxfade.lock());
3867         if (xfade) {
3868                 xfade->set_active (!xfade->active());
3869         }
3870 }
3871
3872 void
3873 Editor::toggle_xfade_length (boost::weak_ptr<Crossfade> wxfade)
3874 {
3875         boost::shared_ptr<Crossfade> xfade (wxfade.lock());
3876         if (xfade) {
3877                 xfade->set_follow_overlap (!xfade->following_overlap());
3878         }
3879 }
3880
3881 void
3882 Editor::edit_xfade (boost::weak_ptr<Crossfade> wxfade)
3883 {
3884         boost::shared_ptr<Crossfade> xfade (wxfade.lock());
3885
3886         if (!xfade) {
3887                 return;
3888         }
3889
3890         CrossfadeEditor cew (*session, xfade, xfade->fade_in().get_min_y(), 1.0);
3891                 
3892         ensure_float (cew);
3893         
3894         switch (cew.run ()) {
3895         case RESPONSE_ACCEPT:
3896                 break;
3897         default:
3898                 return;
3899         }
3900         
3901         cew.apply ();
3902         xfade->StateChanged (Change (~0));
3903 }
3904
3905 PlaylistSelector&
3906 Editor::playlist_selector () const
3907 {
3908         return *_playlist_selector;
3909 }
3910
3911 nframes64_t
3912 Editor::get_nudge_distance (nframes64_t pos, nframes64_t& next)
3913 {
3914         nframes64_t ret;
3915
3916         ret = nudge_clock.current_duration (pos);
3917         next = ret + 1; /* XXXX fix me */
3918
3919         return ret;
3920 }
3921
3922 void
3923 Editor::end_location_changed (Location* location)
3924 {
3925         ENSURE_GUI_THREAD (bind (mem_fun(*this, &Editor::end_location_changed), location));
3926         //reset_scrolling_region ();
3927         nframes64_t session_span = location->start() + (nframes64_t) floorf (current_page_frames() * 0.10f);
3928         horizontal_adjustment.set_upper (session_span / frames_per_unit);
3929 }
3930
3931 int
3932 Editor::playlist_deletion_dialog (boost::shared_ptr<Playlist> pl)
3933 {
3934         ArdourDialog dialog ("playlist deletion dialog");
3935         Label  label (string_compose (_("Playlist %1 is currently unused.\n"
3936                                         "If left alone, no audio files used by it will be cleaned.\n"
3937                                         "If deleted, audio files used by it alone by will cleaned."),
3938                                       pl->name()));
3939         
3940         dialog.set_position (WIN_POS_CENTER);
3941         dialog.get_vbox()->pack_start (label);
3942
3943         label.show ();
3944
3945         dialog.add_button (_("Delete playlist"), RESPONSE_ACCEPT);
3946         dialog.add_button (_("Keep playlist"), RESPONSE_REJECT);
3947         dialog.add_button (_("Cancel"), RESPONSE_CANCEL);
3948
3949         switch (dialog.run ()) {
3950         case RESPONSE_ACCEPT:
3951                 /* delete the playlist */
3952                 return 0;
3953                 break;
3954
3955         case RESPONSE_REJECT:
3956                 /* keep the playlist */
3957                 return 1;
3958                 break;
3959
3960         default:
3961                 break;
3962         }
3963
3964         return -1;
3965 }
3966
3967 bool
3968 Editor::audio_region_selection_covers (nframes64_t where)
3969 {
3970         for (RegionSelection::iterator a = selection->regions.begin(); a != selection->regions.end(); ++a) {
3971                 if ((*a)->region()->covers (where)) {
3972                         return true;
3973                 }
3974         }
3975
3976         return false;
3977 }
3978
3979 void
3980 Editor::prepare_for_cleanup ()
3981 {
3982         cut_buffer->clear_regions ();
3983         cut_buffer->clear_playlists ();
3984
3985         selection->clear_regions ();
3986         selection->clear_playlists ();
3987
3988         _regions->suspend_redisplay ();
3989 }
3990
3991 void
3992 Editor::finish_cleanup ()
3993 {
3994         _regions->resume_redisplay ();
3995 }
3996
3997 Location*
3998 Editor::transport_loop_location()
3999 {
4000         if (session) {
4001                 return session->locations()->auto_loop_location();
4002         } else {
4003                 return 0;
4004         }
4005 }
4006
4007 Location*
4008 Editor::transport_punch_location()
4009 {
4010         if (session) {
4011                 return session->locations()->auto_punch_location();
4012         } else {
4013                 return 0;
4014         }
4015 }
4016
4017 bool
4018 Editor::control_layout_scroll (GdkEventScroll* ev)
4019 {
4020         switch (ev->direction) {
4021         case GDK_SCROLL_UP:
4022                 scroll_tracks_up_line ();
4023                 return true;
4024                 break;
4025
4026         case GDK_SCROLL_DOWN:
4027                 scroll_tracks_down_line ();
4028                 return true;
4029                 
4030         default:
4031                 /* no left/right handling yet */
4032                 break;
4033         }
4034
4035         return false;
4036 }
4037
4038 void
4039 Editor::session_state_saved (string snap_name)
4040 {
4041         ENSURE_GUI_THREAD (bind (mem_fun(*this, &Editor::session_state_saved), snap_name));
4042         
4043         _snapshots->redisplay ();
4044 }
4045
4046 void
4047 Editor::maximise_editing_space ()
4048 {
4049         mouse_mode_tearoff->set_visible (false);
4050         tools_tearoff->set_visible (false);
4051
4052         pre_maximal_pane_position = edit_pane.get_position();
4053         pre_maximal_editor_width = this->get_width();
4054
4055         if(post_maximal_pane_position == 0) {
4056                 post_maximal_pane_position = edit_pane.get_width();
4057         }
4058
4059         fullscreen();
4060
4061         if(post_maximal_editor_width) {
4062                 edit_pane.set_position (post_maximal_pane_position - 
4063                         abs(post_maximal_editor_width - pre_maximal_editor_width));
4064         } else {
4065                 edit_pane.set_position (post_maximal_pane_position);
4066         }
4067 }
4068
4069 void
4070 Editor::restore_editing_space ()
4071 {
4072         // user changed width of pane during fullscreen
4073
4074         if(post_maximal_pane_position != edit_pane.get_position()) {
4075                 post_maximal_pane_position = edit_pane.get_position();
4076         }
4077
4078         unfullscreen();
4079
4080         mouse_mode_tearoff->set_visible (true);
4081         tools_tearoff->set_visible (true);
4082         post_maximal_editor_width = this->get_width();
4083
4084         edit_pane.set_position (pre_maximal_pane_position + abs(this->get_width() - pre_maximal_editor_width));
4085 }
4086
4087 /**
4088  *  Make new playlists for a given track and also any others that belong
4089  *  to the same active route group with the `edit' property.
4090  *  @param v Track.
4091  */
4092
4093 void 
4094 Editor::new_playlists (TimeAxisView* v)
4095 {
4096         begin_reversible_command (_("new playlists"));
4097         vector<boost::shared_ptr<ARDOUR::Playlist> > playlists;
4098         session->get_playlists(playlists);
4099         mapover_tracks (bind (mem_fun (*this, &Editor::mapped_use_new_playlist), playlists), v, RouteGroup::Edit);
4100         commit_reversible_command ();
4101 }
4102
4103 /**
4104  *  Use a copy of the current playlist for a given track and also any others that belong
4105  *  to the same active route group with the `edit' property.
4106  *  @param v Track.
4107  */
4108
4109 void
4110 Editor::copy_playlists (TimeAxisView* v)
4111 {
4112         begin_reversible_command (_("copy playlists"));
4113         vector<boost::shared_ptr<ARDOUR::Playlist> > playlists;
4114         session->get_playlists(playlists);
4115         mapover_tracks (bind (mem_fun (*this, &Editor::mapped_use_copy_playlist), playlists), v, RouteGroup::Edit);
4116         commit_reversible_command ();
4117 }
4118
4119 /** Clear the current playlist for a given track and also any others that belong
4120  *  to the same active route group with the `edit' property.
4121  *  @param v Track.
4122  */
4123
4124 void 
4125 Editor::clear_playlists (TimeAxisView* v)
4126 {
4127         begin_reversible_command (_("clear playlists"));
4128         vector<boost::shared_ptr<ARDOUR::Playlist> > playlists;
4129         session->get_playlists(playlists);
4130         mapover_tracks (mem_fun (*this, &Editor::mapped_clear_playlist), v, RouteGroup::Edit);
4131         commit_reversible_command ();
4132 }
4133
4134 void 
4135 Editor::mapped_use_new_playlist (RouteTimeAxisView& atv, uint32_t sz, vector<boost::shared_ptr<ARDOUR::Playlist> > const & playlists)
4136 {
4137         atv.use_new_playlist (sz > 1 ? false : true, playlists);
4138 }
4139
4140 void
4141 Editor::mapped_use_copy_playlist (RouteTimeAxisView& atv, uint32_t sz, vector<boost::shared_ptr<ARDOUR::Playlist> > const & playlists)
4142 {
4143         atv.use_copy_playlist (sz > 1 ? false : true, playlists);
4144 }
4145
4146 void 
4147 Editor::mapped_clear_playlist (RouteTimeAxisView& atv, uint32_t sz)
4148 {
4149         atv.clear_playlist ();
4150 }
4151
4152 bool
4153 Editor::on_key_press_event (GdkEventKey* ev)
4154 {
4155         return key_press_focus_accelerator_handler (*this, ev);
4156 }
4157
4158 bool
4159 Editor::on_key_release_event (GdkEventKey* ev)
4160 {
4161         return Gtk::Window::on_key_release_event (ev);
4162         // return key_press_focus_accelerator_handler (*this, ev);
4163 }
4164
4165 void
4166 Editor::reset_x_origin (nframes64_t frame)
4167 {
4168         //cerr << "resetting x origin" << endl;
4169         queue_visual_change (frame);
4170 }
4171
4172 void
4173 Editor::reset_y_origin (double y)
4174 {
4175         queue_visual_change_y (y);
4176 }
4177
4178 void
4179 Editor::reset_zoom (double fpu)
4180 {
4181         queue_visual_change (fpu);
4182 }
4183
4184 void
4185 Editor::reposition_and_zoom (nframes64_t frame, double fpu)
4186 {
4187         //cerr << "Editor::reposition_and_zoom () called ha v:l:u:ps:fpu = " << horizontal_adjustment.get_value() << ":" << horizontal_adjustment.get_lower() << ":" << horizontal_adjustment.get_upper() << ":" << horizontal_adjustment.get_page_size() << ":" << frames_per_unit << endl;//DEBUG
4188         reset_x_origin (frame);
4189         reset_zoom (fpu);
4190
4191         if (!no_save_visual) {
4192                 undo_visual_stack.push_back (current_visual_state(false));
4193         }
4194 }
4195
4196 Editor::VisualState*
4197 Editor::current_visual_state (bool with_tracks)
4198 {
4199         VisualState* vs = new VisualState;
4200         vs->y_position = vertical_adjustment.get_value();
4201         vs->frames_per_unit = frames_per_unit;
4202         vs->leftmost_frame = leftmost_frame;
4203         vs->zoom_focus = zoom_focus;
4204         vs->zoomed_to_region = zoomed_to_region;
4205
4206         if (with_tracks) {
4207                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
4208                         vs->track_states.push_back (TAVState ((*i), &(*i)->get_state()));
4209                 }
4210         }
4211         
4212         return vs;
4213 }
4214
4215 void
4216 Editor::undo_visual_state ()
4217 {
4218         if (undo_visual_stack.empty()) {
4219                 return;
4220         }
4221
4222         VisualState* vs = undo_visual_stack.back();
4223         undo_visual_stack.pop_back();
4224         use_visual_state (*vs);
4225         redo_visual_stack.push_back (vs);
4226 }
4227
4228 void
4229 Editor::redo_visual_state ()
4230 {
4231         if (redo_visual_stack.empty()) {
4232                 return;
4233         }
4234
4235         VisualState* vs = redo_visual_stack.back();
4236         redo_visual_stack.pop_back();
4237         use_visual_state (*vs);
4238         undo_visual_stack.push_back (vs);
4239 }
4240
4241 void
4242 Editor::swap_visual_state ()
4243 {
4244         if (undo_visual_stack.empty()) {
4245                 redo_visual_state ();
4246         } else {
4247                 undo_visual_state ();
4248         }
4249 }
4250
4251 void
4252 Editor::use_visual_state (VisualState& vs)
4253 {
4254         no_save_visual = true;
4255
4256         _routes->suspend_redisplay ();
4257
4258         vertical_adjustment.set_value (vs.y_position);
4259
4260         set_zoom_focus (vs.zoom_focus);
4261         reposition_and_zoom (vs.leftmost_frame, vs.frames_per_unit);
4262         zoomed_to_region = vs.zoomed_to_region;
4263         
4264         for (list<TAVState>::iterator i = vs.track_states.begin(); i != vs.track_states.end(); ++i) {
4265                 TrackViewList::iterator t;
4266
4267                 /* check if the track still exists - it could have been deleted */
4268
4269                 if ((t = find (track_views.begin(), track_views.end(), i->first)) != track_views.end()) {
4270                         (*t)->set_state (*(i->second));
4271                 }
4272         }
4273
4274
4275         if (!vs.track_states.empty()) {
4276                 _routes->update_visibility ();
4277         } 
4278
4279         _routes->resume_redisplay ();
4280
4281         no_save_visual = false;
4282 }
4283
4284 void
4285 Editor::set_frames_per_unit (double fpu)
4286 {
4287         /* this is the core function that controls the zoom level of the canvas. it is called
4288            whenever one or more calls are made to reset_zoom(). it executes in an idle handler.
4289         */
4290
4291         if (fpu == frames_per_unit) {
4292                 return;
4293         }
4294
4295         if (fpu < 2.0) {
4296                 fpu = 2.0;
4297         }
4298
4299         
4300         /* don't allow zooms that fit more than the maximum number
4301            of frames into an 800 pixel wide space.
4302         */
4303
4304         if (max_frames / fpu < 800.0) {
4305                 return;
4306         }
4307         
4308         if (tempo_lines)
4309                 tempo_lines->tempo_map_changed();
4310
4311         frames_per_unit = fpu;
4312         post_zoom ();
4313 }
4314
4315 void
4316 Editor::post_zoom ()
4317 {
4318         nframes64_t cef = 0;
4319
4320         // convert fpu to frame count
4321
4322         nframes64_t frames = (nframes64_t) floor (frames_per_unit * _canvas_width);
4323
4324         if (frames_per_unit != zoom_range_clock.current_duration()) {
4325                 zoom_range_clock.set (frames);
4326         }
4327
4328         if (mouse_mode == MouseRange && selection->time.start () != selection->time.end_frame ()) {
4329                 if (!selection->tracks.empty()) {
4330                         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
4331                                 (*i)->reshow_selection (selection->time);
4332                         }
4333                 } else {
4334                         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
4335                                 (*i)->reshow_selection (selection->time);
4336                         }
4337                 }
4338         }
4339
4340         leftmost_frame = (nframes64_t) floor (horizontal_adjustment.get_value() * frames_per_unit);
4341
4342         ZoomChanged (); /* EMIT_SIGNAL */
4343
4344         reset_hscrollbar_stepping ();
4345
4346         if (session) {
4347                 cef = session->current_end_frame() + (current_page_frames() / 10);// Add a little extra so we can see the end marker
4348         }
4349         horizontal_adjustment.set_upper (cef / frames_per_unit);
4350
4351         //reset_scrolling_region ();
4352
4353         if (playhead_cursor) {
4354                 playhead_cursor->set_position (playhead_cursor->current_frame);
4355         }
4356
4357         refresh_location_display();
4358         _summary->set_overlays_dirty ();
4359
4360         instant_save ();
4361 }
4362
4363 void
4364 Editor::queue_visual_change (nframes64_t where)
4365 {
4366         pending_visual_change.add (VisualChange::TimeOrigin);
4367         
4368         /* if we're moving beyond the end, make sure the upper limit of the horizontal adjustment
4369            can reach.
4370         */
4371         
4372         if (where > session->current_end_frame()) {
4373                 horizontal_adjustment.set_upper ((where + current_page_frames()) / frames_per_unit);
4374         }
4375         
4376         pending_visual_change.time_origin = where;
4377         
4378         ensure_visual_change_idle_handler ();
4379 }
4380
4381 void
4382 Editor::queue_visual_change (double fpu)
4383 {
4384         pending_visual_change.add (VisualChange::ZoomLevel);
4385         pending_visual_change.frames_per_unit = fpu;
4386
4387         ensure_visual_change_idle_handler ();
4388         
4389 }
4390
4391 void
4392 Editor::queue_visual_change_y (double y)
4393 {
4394         pending_visual_change.add (VisualChange::YOrigin);
4395         pending_visual_change.y_origin = y;
4396
4397         ensure_visual_change_idle_handler ();
4398 }
4399
4400 void
4401 Editor::ensure_visual_change_idle_handler ()
4402 {
4403         if (pending_visual_change.idle_handler_id < 0) {
4404                 pending_visual_change.idle_handler_id = g_idle_add (_idle_visual_changer, this);
4405         }
4406 }
4407
4408 int
4409 Editor::_idle_visual_changer (void* arg)
4410 {
4411         return static_cast<Editor*>(arg)->idle_visual_changer ();
4412 }
4413
4414 int
4415 Editor::idle_visual_changer ()
4416 {
4417         VisualChange::Type p = pending_visual_change.pending;
4418         pending_visual_change.pending = (VisualChange::Type) 0;
4419
4420 #ifdef FIX_THIS_FOR_V3
4421         double last_time_origin = horizontal_adjustment.get_value();
4422 #endif
4423
4424         if (p & VisualChange::ZoomLevel) {
4425                 set_frames_per_unit (pending_visual_change.frames_per_unit);
4426
4427                 compute_fixed_ruler_scale ();
4428                 compute_current_bbt_points(pending_visual_change.time_origin, pending_visual_change.time_origin + current_page_frames());
4429                 compute_bbt_ruler_scale (pending_visual_change.time_origin, pending_visual_change.time_origin + current_page_frames());
4430                 update_tempo_based_rulers ();
4431         }
4432         if (p & VisualChange::TimeOrigin) {
4433                 horizontal_adjustment.set_value (pending_visual_change.time_origin / frames_per_unit);
4434         }
4435         if (p & VisualChange::YOrigin) {
4436                 vertical_adjustment.set_value (pending_visual_change.y_origin);
4437         }
4438         
4439         nframes64_t csf=0, cef=0;
4440         nframes64_t current_time_origin = (nframes64_t) floor (horizontal_adjustment.get_value() * frames_per_unit);
4441         
4442         if (session) {
4443                 csf = session->current_start_frame();
4444                 cef = session->current_end_frame();
4445         }
4446         
4447         /* if we seek beyond the current end of the canvas, move the end */
4448
4449 #ifdef FIX_THIS_FOR_V3
4450         if (last_time_origin == horizontal_adjustment.get_value() ) {
4451                 /* changed signal not emitted */
4452                 update_fixed_rulers ();
4453                 redisplay_tempo (true);
4454         }
4455 #endif
4456         
4457         if (current_time_origin != pending_visual_change.time_origin) {
4458                 cef += current_page_frames() / 10; // Add a little extra so we can see the end marker
4459                 horizontal_adjustment.set_upper (cef / frames_per_unit);
4460                 horizontal_adjustment.set_value (pending_visual_change.time_origin / frames_per_unit);
4461         } else {
4462                 update_fixed_rulers();
4463                 redisplay_tempo (true);
4464         }
4465
4466         _summary->set_overlays_dirty ();
4467         
4468         //cerr << "Editor::idle_visual_changer () called ha v:l:u:ps:fpu = " << horizontal_adjustment.get_value() << ":" << horizontal_adjustment.get_lower() << ":" << horizontal_adjustment.get_upper() << ":" << horizontal_adjustment.get_page_size() << ":" << frames_per_unit << endl;//DEBUG
4469         pending_visual_change.idle_handler_id = -1;
4470         return 0; /* this is always a one-shot call */
4471 }
4472
4473 struct EditorOrderTimeAxisSorter {
4474     bool operator() (const TimeAxisView* a, const TimeAxisView* b) const {
4475             return a->order () < b->order ();
4476     }
4477 };
4478         
4479 void
4480 Editor::sort_track_selection (TrackSelection* sel)
4481 {
4482         EditorOrderTimeAxisSorter cmp;
4483
4484         if (sel) {
4485                 sel->sort (cmp);
4486         } else {
4487                 selection->tracks.sort (cmp);
4488         }
4489 }
4490
4491 nframes64_t
4492 Editor::get_preferred_edit_position (bool ignore_playhead)
4493 {
4494         bool ignored;
4495         nframes64_t where = 0;
4496         EditPoint ep = _edit_point;
4497
4498         if (entered_marker) {
4499                 return entered_marker->position();
4500         }
4501
4502         if (ignore_playhead && ep == EditAtPlayhead) {
4503                 ep = EditAtSelectedMarker;
4504         }
4505
4506         switch (ep) {
4507         case EditAtPlayhead:
4508                 where = session->audible_frame();
4509                 break;
4510                 
4511         case EditAtSelectedMarker:
4512                 if (!selection->markers.empty()) {
4513                         bool is_start;
4514                         Location* loc = find_location_from_marker (selection->markers.front(), is_start);
4515                         if (loc) {
4516                                 if (is_start) {
4517                                         where =  loc->start();
4518                                 } else {
4519                                         where = loc->end();
4520                                 }
4521                                 break;
4522                         }
4523                 } 
4524                 /* fallthru */
4525                 
4526         default:
4527         case EditAtMouse:
4528                 if (!mouse_frame (where, ignored)) {
4529                         /* XXX not right but what can we do ? */
4530                         return 0;
4531                 }
4532                 snap_to (where);
4533                 break;
4534         }
4535
4536         return where;
4537 }
4538
4539 void
4540 Editor::set_loop_range (nframes64_t start, nframes64_t end, string cmd)
4541 {
4542         if (!session) return;
4543
4544         begin_reversible_command (cmd);
4545         
4546         Location* tll;
4547
4548         if ((tll = transport_loop_location()) == 0) {
4549                 Location* loc = new Location (start, end, _("Loop"),  Location::IsAutoLoop);
4550                 XMLNode &before = session->locations()->get_state();
4551                 session->locations()->add (loc, true);
4552                 session->set_auto_loop_location (loc);
4553                 XMLNode &after = session->locations()->get_state();
4554                 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
4555         } else {
4556                 XMLNode &before = tll->get_state();
4557                 tll->set_hidden (false, this);
4558                 tll->set (start, end);
4559                 XMLNode &after = tll->get_state();
4560                 session->add_command (new MementoCommand<Location>(*tll, &before, &after));
4561         }
4562         
4563         commit_reversible_command ();
4564 }
4565
4566 void
4567 Editor::set_punch_range (nframes64_t start, nframes64_t end, string cmd)
4568 {
4569         if (!session) return;
4570
4571         begin_reversible_command (cmd);
4572         
4573         Location* tpl;
4574
4575         if ((tpl = transport_punch_location()) == 0) {
4576                 Location* loc = new Location (start, end, _("Loop"),  Location::IsAutoPunch);
4577                 XMLNode &before = session->locations()->get_state();
4578                 session->locations()->add (loc, true);
4579                 session->set_auto_loop_location (loc);
4580                 XMLNode &after = session->locations()->get_state();
4581                 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
4582         }
4583         else {
4584                 XMLNode &before = tpl->get_state();
4585                 tpl->set_hidden (false, this);
4586                 tpl->set (start, end);
4587                 XMLNode &after = tpl->get_state();
4588                 session->add_command (new MementoCommand<Location>(*tpl, &before, &after));
4589         }
4590         
4591         commit_reversible_command ();
4592 }
4593
4594 void
4595 Editor::get_regions_at (RegionSelection& rs, nframes64_t where, const TrackSelection& ts) const
4596 {
4597         const TrackSelection* tracks;
4598
4599         if (ts.empty()) {
4600                 tracks = &track_views;
4601         } else {
4602                 tracks = &ts;
4603         }
4604
4605         for (TrackSelection::const_iterator t = tracks->begin(); t != tracks->end(); ++t) {
4606                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(*t);
4607                 if (rtv) {
4608                         boost::shared_ptr<Diskstream> ds;
4609                         boost::shared_ptr<Playlist> pl;
4610                         
4611                         if ((ds = rtv->get_diskstream()) && ((pl = ds->playlist()))) {
4612
4613                                 Playlist::RegionList* regions = pl->regions_at (
4614                                                 (nframes64_t) floor ( (double)where * ds->speed()));
4615
4616                                 for (Playlist::RegionList::iterator i = regions->begin(); i != regions->end(); ++i) {
4617                                         RegionView* rv = rtv->view()->find_view (*i);
4618                                         if (rv) {
4619                                                 rs.add (rv);
4620                                         }
4621                                 }
4622
4623                                 delete regions;
4624                         }
4625                 }
4626         }
4627 }
4628
4629 void
4630 Editor::get_regions_after (RegionSelection& rs, nframes64_t where, const TrackSelection& ts) const
4631 {
4632         const TrackSelection* tracks;
4633
4634         if (ts.empty()) {
4635                 tracks = &track_views;
4636         } else {
4637                 tracks = &ts;
4638         }
4639
4640         for (TrackSelection::const_iterator t = tracks->begin(); t != tracks->end(); ++t) {
4641                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(*t);
4642                 if (rtv) {
4643                         boost::shared_ptr<Diskstream> ds;
4644                         boost::shared_ptr<Playlist> pl;
4645                         
4646                         if ((ds = rtv->get_diskstream()) && ((pl = ds->playlist()))) {
4647
4648                                 Playlist::RegionList* regions = pl->regions_touched (
4649                                                 (nframes64_t) floor ( (double)where * ds->speed()), max_frames);
4650
4651                                 for (Playlist::RegionList::iterator i = regions->begin(); i != regions->end(); ++i) {
4652
4653                                         RegionView* rv = rtv->view()->find_view (*i);
4654
4655                                         if (rv) {
4656                                                 rs.push_back (rv);
4657                                         }
4658                                 }
4659
4660                                 delete regions;
4661                         }
4662                 }
4663         }
4664 }
4665
4666 void
4667 Editor::get_regions_for_action (RegionSelection& rs, bool allow_entered)
4668 {
4669         rs = selection->regions;
4670
4671         if (allow_entered && entered_regionview) {
4672                 rs.add (entered_regionview);
4673         }
4674
4675         TrackSelection tracks = selection->tracks;
4676
4677         RegionSelection to_map = rs;
4678
4679         for (RegionSelection::iterator i = to_map.begin (); i != to_map.end(); ++i) {
4680
4681                 RouteGroup* g = (*i)->get_time_axis_view().route_group ();
4682                 if (g && g->active_property (RouteGroup::Edit)) {
4683                         tracks.add (axis_views_from_routes (g->route_list()));
4684                 }
4685
4686                 nframes64_t const where = get_preferred_edit_position ();
4687                 get_regions_at (rs, where, tracks);
4688         }
4689 }
4690
4691 void
4692 Editor::get_regions_corresponding_to (boost::shared_ptr<Region> region, vector<RegionView*>& regions)
4693 {
4694
4695         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
4696                 
4697                 RouteTimeAxisView* tatv;
4698                 
4699                 if ((tatv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
4700                         
4701                         boost::shared_ptr<Playlist> pl;
4702                         vector<boost::shared_ptr<Region> > results;
4703                         RegionView* marv;
4704                         boost::shared_ptr<Diskstream> ds;
4705                         
4706                         if ((ds = tatv->get_diskstream()) == 0) {
4707                                 /* bus */
4708                                 continue;
4709                         }
4710                         
4711                         if ((pl = (ds->playlist())) != 0) {
4712                                 pl->get_region_list_equivalent_regions (region, results);
4713                         }
4714                         
4715                         for (vector<boost::shared_ptr<Region> >::iterator ir = results.begin(); ir != results.end(); ++ir) {
4716                                 if ((marv = tatv->view()->find_view (*ir)) != 0) {
4717                                         regions.push_back (marv);
4718                                 }
4719                         }
4720                         
4721                 }
4722         }
4723 }       
4724
4725 void
4726 Editor::show_rhythm_ferret ()
4727 {
4728         if (rhythm_ferret == 0) {
4729                 rhythm_ferret = new RhythmFerret(*this);
4730         }
4731
4732         rhythm_ferret->set_session (session);
4733         rhythm_ferret->show ();
4734         rhythm_ferret->present ();
4735 }
4736
4737 void
4738 Editor::show_bundle_manager ()
4739 {
4740         if (_bundle_manager == 0) {
4741                 _bundle_manager = new BundleManager (*session);
4742         }
4743
4744         _bundle_manager->show ();
4745 }
4746
4747 void
4748 Editor::show_global_port_matrix (ARDOUR::DataType t)
4749 {
4750         if (_global_port_matrix[t] == 0) {
4751                 _global_port_matrix[t] = new GlobalPortMatrixWindow (*session, t);
4752         }
4753
4754         _global_port_matrix[t]->show ();
4755 }
4756
4757 void
4758 Editor::first_idle ()
4759 {
4760         MessageDialog* dialog = 0;
4761
4762         if (track_views.size() > 1) { 
4763                 dialog = new MessageDialog (*this, 
4764                                             _("Please wait while Ardour loads visual data"),
4765                                             true,
4766                                             Gtk::MESSAGE_INFO,
4767                                             Gtk::BUTTONS_NONE);
4768                 dialog->present ();
4769                 ARDOUR_UI::instance()->flush_pending ();
4770         }
4771
4772         for (TrackViewList::iterator t = track_views.begin(); t != track_views.end(); ++t) {
4773                 (*t)->first_idle();
4774         }
4775
4776         // first idle adds route children (automation tracks), so we need to redisplay here
4777         _routes->redisplay ();
4778         
4779         delete dialog;
4780
4781         _have_idled = true;
4782 }
4783
4784 static gboolean
4785 _idle_resizer (gpointer arg)
4786 {
4787         return ((Editor*)arg)->idle_resize ();
4788 }
4789
4790 void
4791 Editor::add_to_idle_resize (TimeAxisView* view, int32_t h)
4792 {
4793         if (resize_idle_id < 0) {
4794                 resize_idle_id = g_idle_add (_idle_resizer, this);
4795                 _pending_resize_amount = 0;
4796         }
4797
4798         /* make a note of the smallest resulting height, so that we can clamp the
4799            lower limit at TimeAxisView::hSmall */
4800
4801         int32_t min_resulting = INT32_MAX;
4802
4803         _pending_resize_amount += h;
4804         _pending_resize_view = view;
4805
4806         min_resulting = min (min_resulting, int32_t (_pending_resize_view->current_height()) + _pending_resize_amount);
4807
4808         if (selection->tracks.contains (_pending_resize_view)) {
4809                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
4810                         min_resulting = min (min_resulting, int32_t ((*i)->current_height()) + _pending_resize_amount);
4811                 }
4812         }
4813
4814         if (min_resulting < 0) {
4815                 min_resulting = 0;
4816         }
4817
4818         /* clamp */
4819         if (uint32_t (min_resulting) < TimeAxisView::hSmall) {
4820                 _pending_resize_amount += TimeAxisView::hSmall - min_resulting;
4821         }
4822 }
4823
4824 /** Handle pending resizing of tracks */
4825 bool
4826 Editor::idle_resize ()
4827 {
4828         _pending_resize_view->idle_resize (_pending_resize_view->current_height() + _pending_resize_amount);
4829
4830         if (dynamic_cast<AutomationTimeAxisView*> (_pending_resize_view) == 0 &&
4831             selection->tracks.contains (_pending_resize_view)) {
4832                 
4833                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
4834                         if (*i != _pending_resize_view) {
4835                                 (*i)->idle_resize ((*i)->current_height() + _pending_resize_amount);
4836                         }
4837                 }
4838         }
4839         
4840         flush_canvas ();
4841         _group_tabs->set_dirty ();
4842         resize_idle_id = -1;
4843         
4844         return false;
4845 }
4846
4847 void
4848 Editor::located ()
4849 {
4850         ENSURE_GUI_THREAD (mem_fun (*this, &Editor::located));
4851
4852         _pending_locate_request = false;
4853 }
4854
4855 void
4856 Editor::region_view_added (RegionView *)
4857 {
4858         _summary->set_dirty ();
4859 }
4860
4861 void
4862 Editor::streamview_height_changed ()
4863 {
4864         _summary->set_dirty ();
4865 }
4866
4867 TrackSelection
4868 Editor::axis_views_from_routes (list<Route*> r) const
4869 {
4870         TrackSelection t;
4871         
4872         for (list<Route*>::const_iterator i = r.begin(); i != r.end(); ++i) {
4873                 TrackViewList::const_iterator j = track_views.begin ();
4874                 while (j != track_views.end()) {
4875                         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*j);
4876                         if (rtv && rtv->route().get() == *i) {
4877                                 t.push_back (rtv);
4878                         }
4879                         ++j;
4880                 }
4881         }
4882
4883         return t;
4884 }
4885
4886
4887 void
4888 Editor::handle_new_route (RouteList& routes)
4889 {
4890         ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::handle_new_route), routes));
4891         
4892         RouteTimeAxisView *rtv;
4893         list<RouteTimeAxisView*> new_views;
4894
4895         for (RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
4896                 boost::shared_ptr<Route> route = (*x);
4897
4898                 if (route->is_hidden()) {
4899                         continue;
4900                 }
4901
4902                 DataType dt = route->input()->default_type();
4903
4904                 if (dt == ARDOUR::DataType::AUDIO) {
4905                         rtv = new AudioTimeAxisView (*this, *session, route, *track_canvas);
4906                 } else if (dt == ARDOUR::DataType::MIDI) {
4907                         rtv = new MidiTimeAxisView (*this, *session, route, *track_canvas);
4908                 } else {
4909                         throw unknown_type();
4910                 }
4911
4912                 new_views.push_back (rtv);
4913                 track_views.push_back (rtv);
4914                 
4915                 rtv->effective_gain_display ();
4916                 
4917                 rtv->view()->RegionViewAdded.connect (mem_fun (*this, &Editor::region_view_added));
4918                 rtv->view()->HeightChanged.connect (mem_fun (*this, &Editor::streamview_height_changed));
4919                 
4920                 rtv->GoingAway.connect (bind (mem_fun(*this, &Editor::remove_route), rtv));
4921         }
4922
4923         _routes->routes_added (new_views);
4924
4925         if (show_editor_mixer_when_tracks_arrive) {
4926                 show_editor_mixer (true);
4927         }
4928
4929         editor_list_button.set_sensitive (true);
4930
4931         _summary->set_dirty ();
4932 }
4933
4934 void
4935 Editor::remove_route (TimeAxisView *tv)
4936 {
4937         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::remove_route), tv));
4938
4939         TrackViewList::iterator i;
4940         boost::shared_ptr<Route> route;
4941         TimeAxisView* next_tv;
4942
4943         if (tv == entered_track) {
4944                 entered_track = 0;
4945         }
4946
4947         if ((i = find (track_views.begin(), track_views.end(), tv)) != track_views.end()) {
4948
4949                i = track_views.erase (i);
4950
4951                if (track_views.empty()) {
4952                        next_tv = 0;
4953                } else if (i == track_views.end()) {
4954                        next_tv = track_views.front();
4955                } else {
4956                       next_tv = (*i);
4957                }
4958         }
4959         
4960         if (current_mixer_strip && current_mixer_strip->route() == route) {
4961
4962                if (next_tv) {
4963                        set_selected_mixer_strip (*next_tv);
4964                } else {
4965                        /* make the editor mixer strip go away setting the
4966                         * button to inactive (which also unticks the menu option)
4967                         */
4968
4969                        ActionManager::uncheck_toggleaction ("<Actions>/Editor/show-editor-mixer");
4970                }
4971         } 
4972 }
4973
4974 void
4975 Editor::hide_track_in_display (TimeAxisView& tv, bool temponly)
4976 {
4977         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&tv);
4978
4979         if (rtv && current_mixer_strip && (rtv->route() == current_mixer_strip->route())) {
4980                 // this will hide the mixer strip
4981                 set_selected_mixer_strip (tv);
4982         }
4983
4984         _routes->hide_track_in_display (tv);
4985 }
4986
4987 bool
4988 Editor::sync_track_view_list_and_routes ()
4989 {
4990         track_views = TrackSelection (_routes->views ());
4991         
4992         _summary->set_dirty ();
4993         _group_tabs->set_dirty ();
4994        
4995         return false; // do not call again (until needed)
4996 }
4997
4998 void
4999 Editor::foreach_time_axis_view (sigc::slot<void,TimeAxisView&> theslot)
5000 {
5001         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
5002                 theslot (**i);
5003         }
5004 }
5005
5006 RouteTimeAxisView*
5007 Editor::get_route_view_by_id (PBD::ID& id)
5008 {
5009         RouteTimeAxisView* v;
5010
5011         for(TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
5012                 if((v = dynamic_cast<RouteTimeAxisView*>(*i)) != 0) {
5013                         if(v->route()->id() == id) {
5014                                 return v;
5015                         }
5016                 }
5017         }
5018
5019         return 0;
5020 }
5021
5022 void
5023 Editor::fit_route_group (RouteGroup *g)
5024 {
5025         TrackSelection ts = axis_views_from_routes (g->route_list ());
5026         fit_tracks (ts);
5027 }
5028
5029 void
5030 Editor::consider_auditioning (boost::shared_ptr<Region> region)
5031 {
5032         boost::shared_ptr<AudioRegion> r = boost::dynamic_pointer_cast<AudioRegion> (region);
5033
5034         if (r == 0) {
5035                 session->cancel_audition ();
5036                 return;
5037         }
5038
5039         if (session->is_auditioning()) {
5040                 session->cancel_audition ();
5041                 if (r == last_audition_region) {
5042                         return;
5043                 }
5044         }
5045
5046         session->audition_region (r);
5047         last_audition_region = r;
5048 }
5049
5050
5051 void
5052 Editor::hide_a_region (boost::shared_ptr<Region> r)
5053 {
5054         r->set_hidden (true);
5055 }
5056
5057 void
5058 Editor::remove_a_region (boost::shared_ptr<Region> r)
5059 {
5060         session->remove_region_from_region_list (r);
5061 }
5062
5063 void
5064 Editor::audition_region_from_region_list ()
5065 {
5066         _regions->selection_mapover (mem_fun (*this, &Editor::consider_auditioning));
5067 }
5068
5069 void
5070 Editor::hide_region_from_region_list ()
5071 {
5072         _regions->selection_mapover (mem_fun (*this, &Editor::hide_a_region));
5073 }
5074