fix up duplicate dialog
[ardour.git] / gtk2_ardour / editor.cc
1 /*
2     Copyright (C) 2000-2006 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 #include <unistd.h>
21 #include <cstdlib>
22 #include <cmath>
23 #include <string>
24 #include <algorithm>
25
26 #include <sigc++/bind.h>
27
28 #include <pbd/convert.h>
29 #include <pbd/error.h>
30 #include <pbd/stacktrace.h>
31 #include <pbd/memento_command.h>
32
33 #include <glibmm/miscutils.h>
34 #include <gtkmm/image.h>
35 #include <gdkmm/color.h>
36 #include <gdkmm/bitmap.h>
37
38 #include <gtkmm2ext/grouped_buttons.h>
39 #include <gtkmm2ext/gtk_ui.h>
40 #include <gtkmm2ext/tearoff.h>
41 #include <gtkmm2ext/utils.h>
42 #include <gtkmm2ext/window_title.h>
43
44 #include <ardour/audio_track.h>
45 #include <ardour/audio_diskstream.h>
46 #include <ardour/plugin_manager.h>
47 #include <ardour/location.h>
48 #include <ardour/audioplaylist.h>
49 #include <ardour/audioregion.h>
50 #include <ardour/region.h>
51 #include <ardour/session_route.h>
52 #include <ardour/tempo.h>
53 #include <ardour/utils.h>
54
55 #include <control_protocol/control_protocol.h>
56
57 #include "ardour_ui.h"
58 #include "editor.h"
59 #include "keyboard.h"
60 #include "marker.h"
61 #include "playlist_selector.h"
62 #include "audio_region_view.h"
63 #include "rgb_macros.h"
64 #include "selection.h"
65 #include "audio_streamview.h"
66 #include "time_axis_view.h"
67 #include "audio_time_axis.h"
68 #include "utils.h"
69 #include "crossfade_view.h"
70 #include "editing.h"
71 #include "public_editor.h"
72 #include "crossfade_edit.h"
73 #include "canvas_impl.h"
74 #include "actions.h"
75 #include "gui_thread.h"
76
77 #ifdef FFT_ANALYSIS
78 #include "analysis_window.h"
79 #endif
80
81 #include "i18n.h"
82
83 /* <CMT Additions> */
84 #include "imageframe_socket_handler.h"
85 /* </CMT Additions> */
86
87 using namespace std;
88 using namespace sigc;
89 using namespace ARDOUR;
90 using namespace PBD;
91 using namespace Gtk;
92 using namespace Glib;
93 using namespace Gtkmm2ext;
94 using namespace Editing;
95
96 using PBD::atoi;
97
98 const double Editor::timebar_height = 15.0;
99
100 #include "editor_xpms"
101
102 static const gchar *_snap_type_strings[] = {
103         N_("None"),
104         N_("CD Frames"),
105         N_("SMPTE Frames"),
106         N_("SMPTE Seconds"),
107         N_("SMPTE Minutes"),
108         N_("Seconds"),
109         N_("Minutes"),
110         N_("Beats/32"),
111         N_("Beats/16"),
112         N_("Beats/8"),
113         N_("Beats/4"),
114         N_("Beats/3"),
115         N_("Beats"),
116         N_("Bars"),
117         N_("Marks"),
118         N_("Edit Cursor"),
119         N_("Region starts"),
120         N_("Region ends"),
121         N_("Region syncs"),
122         N_("Region bounds"),
123         0
124 };
125
126 static const gchar *_snap_mode_strings[] = {
127         N_("Normal"),
128         N_("Magnetic"),
129         0
130 };
131
132 static const gchar *_zoom_focus_strings[] = {
133         N_("Left"),
134         N_("Right"),
135         N_("Center"),
136         N_("Playhead"),
137         N_("Edit Cursor"),
138         0
139 };
140
141 /* Soundfile  drag-n-drop */
142
143 Gdk::Cursor* Editor::cross_hair_cursor = 0;
144 Gdk::Cursor* Editor::selector_cursor = 0;
145 Gdk::Cursor* Editor::trimmer_cursor = 0;
146 Gdk::Cursor* Editor::grabber_cursor = 0;
147 Gdk::Cursor* Editor::zoom_cursor = 0;
148 Gdk::Cursor* Editor::time_fx_cursor = 0;
149 Gdk::Cursor* Editor::fader_cursor = 0;
150 Gdk::Cursor* Editor::speaker_cursor = 0;
151 Gdk::Cursor* Editor::wait_cursor = 0;
152 Gdk::Cursor* Editor::timebar_cursor = 0;
153
154 void
155 show_me_the_size (Requisition* r, const char* what)
156 {
157         cerr << "size of " << what << " = " << r->width << " x " << r->height << endl;
158 }
159
160 void 
161 check_adjustment (Gtk::Adjustment* adj)
162 {
163         cerr << "CHANGE adj  = " 
164              << adj->get_lower () <<  ' '
165              << adj->get_upper () <<  ' '
166              << adj->get_value () <<  ' '
167              << adj->get_step_increment () <<  ' '
168              << adj->get_page_increment () <<  ' '
169              << adj->get_page_size () <<  ' '
170              << endl;
171
172 }
173
174 Editor::Editor ()
175         : 
176           /* time display buttons */
177
178           minsec_label (_("Mins:Secs")),
179           bbt_label (_("Bars:Beats")),
180           smpte_label (_("Timecode")),
181           frame_label (_("Frames")),
182           tempo_label (_("Tempo")),
183           meter_label (_("Meter")),
184           mark_label (_("Location Markers")),
185           range_mark_label (_("Range Markers")),
186           transport_mark_label (_("Loop/Punch Ranges")),
187
188           edit_packer (3, 3, false),
189
190           /* the values here don't matter: layout widgets
191              reset them as needed.
192           */
193
194           vertical_adjustment (0.0, 0.0, 10.0, 400.0),
195           horizontal_adjustment (0.0, 0.0, 20.0, 1200.0),
196
197           /* tool bar related */
198
199           edit_cursor_clock (X_("editcursor"), false, X_("EditCursorClock"), true),
200           zoom_range_clock (X_("zoomrange"), false, X_("ZoomRangeClock"), true, true),
201           
202           toolbar_selection_clock_table (2,3),
203           
204           automation_mode_button (_("mode")),
205           global_automation_button (_("automation")),
206
207           /* <CMT Additions> */
208           image_socket_listener(0),
209           /* </CMT Additions> */
210
211           /* nudge */
212
213           nudge_clock (X_("nudge"), false, X_("NudgeClock"), true, true)
214
215 {
216         constructed = false;
217
218         /* we are a singleton */
219
220         PublicEditor::_instance = this;
221
222         session = 0;
223
224         selection = new Selection;
225         cut_buffer = new Selection;
226
227         selection->TimeChanged.connect (mem_fun(*this, &Editor::time_selection_changed));
228         selection->TracksChanged.connect (mem_fun(*this, &Editor::track_selection_changed));
229         selection->RegionsChanged.connect (mem_fun(*this, &Editor::region_selection_changed));
230         selection->PointsChanged.connect (mem_fun(*this, &Editor::point_selection_changed));
231
232         clicked_regionview = 0;
233         clicked_trackview = 0;
234         clicked_audio_trackview = 0;
235         clicked_crossfadeview = 0;
236         clicked_control_point = 0;
237         latest_regionview = 0;
238         last_update_frame = 0;
239         drag_info.item = 0;
240         current_mixer_strip = 0;
241         current_bbt_points = 0;
242
243         snap_type_strings = I18N (_snap_type_strings);
244         snap_mode_strings = I18N (_snap_mode_strings);
245         zoom_focus_strings = I18N(_zoom_focus_strings);
246
247         snap_type = SnapToFrame;
248         set_snap_to (snap_type);
249         snap_mode = SnapNormal;
250         set_snap_mode (snap_mode);
251         snap_threshold = 5.0;
252         bbt_beat_subdivision = 4;
253         canvas_width = 0;
254         canvas_height = 0;
255         autoscroll_active = false;
256         autoscroll_timeout_tag = -1;
257         interthread_progress_window = 0;
258
259 #ifdef FFT_ANALYSIS
260         analysis_window = 0;
261 #endif
262
263         current_interthread_info = 0;
264         _show_measures = true;
265         _show_waveforms = true;
266         _show_waveforms_recording = true;
267         first_action_message = 0;
268         export_dialog = 0;
269         show_gain_after_trim = false;
270         ignore_route_list_reorder = false;
271         no_route_list_redisplay = false;
272         verbose_cursor_on = true;
273         route_removal = false;
274         show_automatic_regions_in_region_list = true;
275         region_list_sort_type = (Editing::RegionListSortType) 0; 
276         have_pending_keyboard_selection = false;
277         _follow_playhead = true;
278         _xfade_visibility = true;
279         editor_ruler_menu = 0;
280         no_ruler_shown_update = false;
281         edit_group_list_menu = 0;
282         route_list_menu = 0;
283         region_list_menu = 0;
284         marker_menu = 0;
285         start_end_marker_menu = 0;
286         range_marker_menu = 0;
287         marker_menu_item = 0;
288         tm_marker_menu = 0;
289         transport_marker_menu = 0;
290         new_transport_marker_menu = 0;
291         editor_mixer_strip_width = Wide;
292         show_editor_mixer_when_tracks_arrive = false;
293         region_edit_menu_split_item = 0;
294         temp_location = 0;
295         region_edit_menu_split_multichannel_item = 0;
296         leftmost_frame = 0;
297         ignore_mouse_mode_toggle = false;
298         current_stepping_trackview = 0;
299         entered_track = 0;
300         entered_regionview = 0;
301         clear_entered_track = false;
302         _new_regionviews_show_envelope = false;
303         current_timestretch = 0;
304         in_edit_group_row_change = false;
305         last_canvas_frame = 0;
306         edit_cursor = 0;
307         playhead_cursor = 0;
308         button_release_can_deselect = true;
309         canvas_idle_queued = false;
310         _dragging_playhead = false;
311         _dragging_hscrollbar = false;
312
313         location_marker_color = color_map[cLocationMarker];
314         location_range_color = color_map[cLocationRange];
315         location_cd_marker_color = color_map[cLocationCDMarker];
316         location_loop_color = color_map[cLocationLoop];
317         location_punch_color = color_map[cLocationPunch];
318
319         range_marker_drag_rect = 0;
320         marker_drag_line = 0;
321         
322         set_mouse_mode (MouseObject, true);
323
324         frames_per_unit = 2048; /* too early to use reset_zoom () */
325         reset_hscrollbar_stepping ();
326         
327         zoom_focus = ZoomFocusLeft;
328         set_zoom_focus (ZoomFocusLeft);
329         zoom_range_clock.ValueChanged.connect (mem_fun(*this, &Editor::zoom_adjustment_changed));
330
331         initialize_rulers ();
332         initialize_canvas ();
333
334         edit_controls_vbox.set_spacing (0);
335         horizontal_adjustment.signal_value_changed().connect (mem_fun(*this, &Editor::canvas_horizontally_scrolled));
336         vertical_adjustment.signal_value_changed().connect (mem_fun(*this, &Editor::tie_vertical_scrolling));
337         
338         track_canvas.set_hadjustment (horizontal_adjustment);
339         track_canvas.set_vadjustment (vertical_adjustment);
340         time_canvas.set_hadjustment (horizontal_adjustment);
341
342         track_canvas.signal_map_event().connect (mem_fun (*this, &Editor::track_canvas_map_handler));
343         time_canvas.signal_map_event().connect (mem_fun (*this, &Editor::time_canvas_map_handler));
344         
345         controls_layout.add (edit_controls_vbox);
346         controls_layout.set_name ("EditControlsBase");
347         controls_layout.add_events (Gdk::SCROLL_MASK);
348         controls_layout.signal_scroll_event().connect (mem_fun(*this, &Editor::control_layout_scroll), false);
349         
350         controls_layout.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK);
351         controls_layout.signal_button_release_event().connect (mem_fun(*this, &Editor::edit_controls_button_release));
352         controls_layout.signal_size_request().connect (mem_fun (*this, &Editor::controls_layout_size_request));
353
354         edit_vscrollbar.set_adjustment (vertical_adjustment);
355         edit_hscrollbar.set_adjustment (horizontal_adjustment);
356
357         edit_hscrollbar.signal_button_press_event().connect (mem_fun(*this, &Editor::hscrollbar_button_press), false);
358         edit_hscrollbar.signal_button_release_event().connect (mem_fun(*this, &Editor::hscrollbar_button_release), false);
359         edit_hscrollbar.signal_size_allocate().connect (mem_fun(*this, &Editor::hscrollbar_allocate));
360
361         edit_hscrollbar.set_name ("EditorHScrollbar");
362
363         build_cursors ();
364         setup_toolbar ();
365
366         edit_cursor_clock.ValueChanged.connect (mem_fun(*this, &Editor::edit_cursor_clock_changed));
367         
368         time_canvas_vbox.pack_start (*_ruler_separator, false, false);
369         time_canvas_vbox.pack_start (*minsec_ruler, false, false);
370         time_canvas_vbox.pack_start (*smpte_ruler, false, false);
371         time_canvas_vbox.pack_start (*frames_ruler, false, false);
372         time_canvas_vbox.pack_start (*bbt_ruler, false, false);
373         time_canvas_vbox.pack_start (time_canvas, true, true);
374         time_canvas_vbox.set_size_request (-1, (int)(timebar_height * visible_timebars) + 2);
375
376         bbt_label.set_name ("EditorTimeButton");
377         bbt_label.set_size_request (-1, (int)timebar_height);
378         bbt_label.set_alignment (1.0, 0.5);
379         bbt_label.set_padding (5,0);
380         minsec_label.set_name ("EditorTimeButton");
381         minsec_label.set_size_request (-1, (int)timebar_height);
382         minsec_label.set_alignment (1.0, 0.5);
383         minsec_label.set_padding (5,0);
384         smpte_label.set_name ("EditorTimeButton");
385         smpte_label.set_size_request (-1, (int)timebar_height);
386         smpte_label.set_alignment (1.0, 0.5);
387         smpte_label.set_padding (5,0);
388         frame_label.set_name ("EditorTimeButton");
389         frame_label.set_size_request (-1, (int)timebar_height);
390         frame_label.set_alignment (1.0, 0.5);
391         frame_label.set_padding (5,0);
392         tempo_label.set_name ("EditorTimeButton");
393         tempo_label.set_size_request (-1, (int)timebar_height);
394         tempo_label.set_alignment (1.0, 0.5);
395         tempo_label.set_padding (5,0);
396         meter_label.set_name ("EditorTimeButton");
397         meter_label.set_size_request (-1, (int)timebar_height);
398         meter_label.set_alignment (1.0, 0.5);
399         meter_label.set_padding (5,0);
400         mark_label.set_name ("EditorTimeButton");
401         mark_label.set_size_request (-1, (int)timebar_height);
402         mark_label.set_alignment (1.0, 0.5);
403         mark_label.set_padding (5,0);
404         range_mark_label.set_name ("EditorTimeButton");
405         range_mark_label.set_size_request (-1, (int)timebar_height);
406         range_mark_label.set_alignment (1.0, 0.5);
407         range_mark_label.set_padding (5,0);
408         transport_mark_label.set_name ("EditorTimeButton");
409         transport_mark_label.set_size_request (-1, (int)timebar_height);
410         transport_mark_label.set_alignment (1.0, 0.5);
411         transport_mark_label.set_padding (5,0);
412         
413         time_button_vbox.pack_start (minsec_label, false, false);
414         time_button_vbox.pack_start (smpte_label, false, false);
415         time_button_vbox.pack_start (frame_label, false, false);
416         time_button_vbox.pack_start (bbt_label, false, false);
417         time_button_vbox.pack_start (meter_label, false, false);
418         time_button_vbox.pack_start (tempo_label, false, false);
419         time_button_vbox.pack_start (mark_label, false, false);
420
421         time_button_event_box.add (time_button_vbox);
422         
423         time_button_event_box.set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
424         time_button_event_box.set_name ("TimebarLabelBase");
425         time_button_event_box.signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_label_button_release));
426
427         time_button_frame.add(time_button_event_box);
428         time_button_frame.property_shadow_type() = Gtk::SHADOW_OUT;
429
430         /* these enable us to have a dedicated window (for cursor setting, etc.) 
431            for the canvas areas.
432         */
433
434         track_canvas_event_box.add (track_canvas);
435
436         time_canvas_event_box.add (time_canvas_vbox);
437         time_canvas_event_box.set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
438         
439         edit_packer.set_col_spacings (0);
440         edit_packer.set_row_spacings (0);
441         edit_packer.set_homogeneous (false);
442         edit_packer.set_border_width (0);
443         edit_packer.set_name ("EditorWindow");
444         
445         edit_packer.attach (edit_vscrollbar,         0, 1, 1, 3,    FILL,        FILL|EXPAND, 0, 0);
446
447         edit_packer.attach (time_button_frame,       0, 2, 0, 1,    FILL,        FILL, 0, 0);
448         edit_packer.attach (time_canvas_event_box,   2, 3, 0, 1,    FILL|EXPAND, FILL, 0, 0);
449
450         edit_packer.attach (controls_layout,         1, 2, 1, 2,    FILL,        FILL|EXPAND, 0, 0);
451         edit_packer.attach (track_canvas_event_box,  2, 3, 1, 2,    FILL|EXPAND, FILL|EXPAND, 0, 0);
452
453         edit_packer.attach (zoom_box,                1, 2, 2, 3,    FILL,         FILL, 0, 0);
454         edit_packer.attach (edit_hscrollbar,         2, 3, 2, 3,    FILL|EXPAND,  FILL, 0, 0);
455
456         bottom_hbox.set_border_width (2);
457         bottom_hbox.set_spacing (3);
458
459         route_display_model = ListStore::create(route_display_columns);
460         route_list_display.set_model (route_display_model);
461         route_list_display.append_column (_("Show"), route_display_columns.visible);
462         route_list_display.append_column (_("Name"), route_display_columns.text);
463         route_list_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
464         route_list_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
465         route_list_display.set_headers_visible (true);
466         route_list_display.set_name ("TrackListDisplay");
467         route_list_display.get_selection()->set_mode (SELECTION_NONE);
468         route_list_display.set_reorderable (true);
469         route_list_display.set_size_request (100,-1);
470
471         CellRendererToggle* route_list_visible_cell = dynamic_cast<CellRendererToggle*>(route_list_display.get_column_cell_renderer (0));
472         route_list_visible_cell->property_activatable() = true;
473         route_list_visible_cell->property_radio() = false;
474         
475         route_display_model->signal_row_deleted().connect (mem_fun (*this, &Editor::route_list_delete));
476         route_display_model->signal_row_changed().connect (mem_fun (*this, &Editor::route_list_change));
477
478         route_list_display.signal_button_press_event().connect (mem_fun (*this, &Editor::route_list_display_button_press), false);
479
480         route_list_scroller.add (route_list_display);
481         route_list_scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
482
483         group_model = ListStore::create(group_columns);
484         edit_group_display.set_model (group_model);
485         edit_group_display.append_column (_("Name"), group_columns.text);
486         edit_group_display.append_column (_("Active"), group_columns.is_active);
487         edit_group_display.append_column (_("Show"), group_columns.is_visible);
488         edit_group_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
489         edit_group_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
490         edit_group_display.get_column (2)->set_data (X_("colnum"), GUINT_TO_POINTER(2));
491         edit_group_display.get_column (0)->set_expand (true);
492         edit_group_display.get_column (1)->set_expand (false);
493         edit_group_display.get_column (2)->set_expand (false);
494         edit_group_display.set_headers_visible (true);
495
496         /* name is directly editable */
497
498         CellRendererText* name_cell = dynamic_cast<CellRendererText*>(edit_group_display.get_column_cell_renderer (0));
499         name_cell->property_editable() = true;
500         name_cell->signal_edited().connect (mem_fun (*this, &Editor::edit_group_name_edit));
501
502         /* use checkbox for the active + visible columns */
503
504         CellRendererToggle* active_cell = dynamic_cast<CellRendererToggle*>(edit_group_display.get_column_cell_renderer (1));
505         active_cell->property_activatable() = true;
506         active_cell->property_radio() = false;
507
508         active_cell = dynamic_cast<CellRendererToggle*>(edit_group_display.get_column_cell_renderer (1));
509         active_cell->property_activatable() = true;
510         active_cell->property_radio() = false;
511
512         group_model->signal_row_changed().connect (mem_fun (*this, &Editor::edit_group_row_change));
513
514         edit_group_display.set_name ("EditGroupList");
515         edit_group_display.get_selection()->set_mode (SELECTION_SINGLE);
516         edit_group_display.set_headers_visible (true);
517         edit_group_display.set_reorderable (false);
518         edit_group_display.set_rules_hint (true);
519         edit_group_display.set_size_request (75, -1);
520
521         edit_group_display_scroller.add (edit_group_display);
522         edit_group_display_scroller.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC);
523
524         edit_group_display.signal_button_press_event().connect (mem_fun(*this, &Editor::edit_group_list_button_press_event), false);
525
526         VBox* edit_group_display_packer = manage (new VBox());
527         HBox* edit_group_display_button_box = manage (new HBox());
528         edit_group_display_button_box->set_homogeneous (true);
529
530         Button* edit_group_add_button = manage (new Button ());
531         Button* edit_group_remove_button = manage (new Button ());
532
533         Widget* w;
534
535         w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
536         w->show();
537         edit_group_add_button->add (*w);
538
539         w = manage (new Image (Stock::REMOVE, ICON_SIZE_BUTTON));
540         w->show();
541         edit_group_remove_button->add (*w);
542
543         edit_group_add_button->signal_clicked().connect (mem_fun (*this, &Editor::new_edit_group));
544         edit_group_remove_button->signal_clicked().connect (mem_fun (*this, &Editor::remove_selected_edit_group));
545         
546         edit_group_display_button_box->pack_start (*edit_group_add_button);
547         edit_group_display_button_box->pack_start (*edit_group_remove_button);
548
549         edit_group_display_packer->pack_start (edit_group_display_scroller, true, true);
550         edit_group_display_packer->pack_start (*edit_group_display_button_box, false, false);
551
552         region_list_display.set_size_request (100, -1);
553         region_list_display.set_name ("RegionListDisplay");
554
555         region_list_model = TreeStore::create (region_list_columns);
556         region_list_model->set_sort_func (0, mem_fun (*this, &Editor::region_list_sorter));
557         region_list_model->set_sort_column (0, SORT_ASCENDING);
558
559         region_list_display.set_model (region_list_model);
560         region_list_display.append_column (_("Regions"), region_list_columns.name);
561         region_list_display.set_headers_visible (false);
562
563         region_list_display.get_selection()->set_select_function (mem_fun (*this, &Editor::region_list_selection_filter));
564         
565         TreeViewColumn* tv_col = region_list_display.get_column(0);
566         CellRendererText* renderer = dynamic_cast<CellRendererText*>(region_list_display.get_column_cell_renderer (0));
567         tv_col->add_attribute(renderer->property_text(), region_list_columns.name);
568         tv_col->add_attribute(renderer->property_foreground_gdk(), region_list_columns.color_);
569         
570         region_list_display.get_selection()->set_mode (SELECTION_MULTIPLE);
571         region_list_display.add_object_drag (region_list_columns.region.index(), "regions");
572
573         /* setup DnD handling */
574         
575         list<TargetEntry> region_list_target_table;
576         
577         region_list_target_table.push_back (TargetEntry ("text/plain"));
578         region_list_target_table.push_back (TargetEntry ("text/uri-list"));
579         region_list_target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
580         
581         region_list_display.add_drop_targets (region_list_target_table);
582         region_list_display.signal_drag_data_received().connect (mem_fun(*this, &Editor::region_list_display_drag_data_received));
583
584         region_list_scroller.add (region_list_display);
585         region_list_scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
586
587         region_list_display.signal_key_press_event().connect (mem_fun(*this, &Editor::region_list_display_key_press));
588         region_list_display.signal_key_release_event().connect (mem_fun(*this, &Editor::region_list_display_key_release));
589         region_list_display.signal_button_press_event().connect (mem_fun(*this, &Editor::region_list_display_button_press), false);
590         region_list_display.signal_button_release_event().connect (mem_fun(*this, &Editor::region_list_display_button_release));
591         region_list_display.get_selection()->signal_changed().connect (mem_fun(*this, &Editor::region_list_selection_changed));
592         // region_list_display.signal_popup_menu().connect (bind (mem_fun (*this, &Editor::show_region_list_display_context_menu), 1, 0));
593         
594         named_selection_scroller.add (named_selection_display);
595         named_selection_scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
596
597         named_selection_model = TreeStore::create (named_selection_columns);
598         named_selection_display.set_model (named_selection_model);
599         named_selection_display.append_column (_("Chunks"), named_selection_columns.text);
600         named_selection_display.set_headers_visible (false);
601         named_selection_display.set_size_request (100, -1);
602         named_selection_display.set_name ("NamedSelectionDisplay");
603         
604         named_selection_display.get_selection()->set_mode (SELECTION_SINGLE);
605         named_selection_display.set_size_request (100, -1);
606         named_selection_display.signal_button_release_event().connect (mem_fun(*this, &Editor::named_selection_display_button_release), false);
607         named_selection_display.signal_key_release_event().connect (mem_fun(*this, &Editor::named_selection_display_key_release), false);
608         named_selection_display.get_selection()->signal_changed().connect (mem_fun (*this, &Editor::named_selection_display_selection_changed));
609
610         /* SNAPSHOTS */
611
612         snapshot_display_model = ListStore::create (snapshot_display_columns);
613         snapshot_display.set_model (snapshot_display_model);
614         snapshot_display.append_column (X_("snapshot"), snapshot_display_columns.visible_name);
615         snapshot_display.set_name ("SnapshotDisplay");
616         snapshot_display.set_size_request (75, -1);
617         snapshot_display.set_headers_visible (false);
618         snapshot_display.set_reorderable (false);
619         snapshot_display_scroller.add (snapshot_display);
620         snapshot_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
621
622         snapshot_display.get_selection()->signal_changed().connect (mem_fun(*this, &Editor::snapshot_display_selection_changed));
623         snapshot_display.signal_button_press_event().connect (mem_fun (*this, &Editor::snapshot_display_button_press), false);
624
625         Gtk::Label* nlabel;
626
627         nlabel = manage (new Label (_("Regions")));
628         nlabel->set_angle (-90);
629         the_notebook.append_page (region_list_scroller, *nlabel);
630         nlabel = manage (new Label (_("Tracks/Busses")));
631         nlabel->set_angle (-90);
632         the_notebook.append_page (route_list_scroller, *nlabel);
633         nlabel = manage (new Label (_("Snapshots")));
634         nlabel->set_angle (-90);
635         the_notebook.append_page (snapshot_display_scroller, *nlabel);
636         nlabel = manage (new Label (_("Edit Groups")));
637         nlabel->set_angle (-90);
638         the_notebook.append_page (*edit_group_display_packer, *nlabel);
639         nlabel = manage (new Label (_("Chunks")));
640         nlabel->set_angle (-90);
641         the_notebook.append_page (named_selection_scroller, *nlabel);
642
643         the_notebook.set_show_tabs (true);
644         the_notebook.set_scrollable (true);
645         the_notebook.popup_enable ();
646         the_notebook.set_tab_pos (Gtk::POS_RIGHT);
647
648         post_maximal_editor_width = 0;
649         post_maximal_pane_position = 0;
650         edit_pane.pack1 (edit_packer, true, true);
651         edit_pane.pack2 (the_notebook, false, true);
652         
653         edit_pane.signal_size_allocate().connect (bind (mem_fun(*this, &Editor::pane_allocation_handler), static_cast<Paned*> (&edit_pane)));
654
655         top_hbox.pack_start (toolbar_frame, true, true);
656
657         HBox *hbox = manage (new HBox);
658         hbox->pack_start (edit_pane, true, true);
659
660         global_vpacker.pack_start (top_hbox, false, false);
661         global_vpacker.pack_start (*hbox, true, true);
662
663         global_hpacker.pack_start (global_vpacker, true, true);
664
665         set_name ("EditorWindow");
666         add_accel_group (ActionManager::ui_manager->get_accel_group());
667
668         vpacker.pack_end (global_hpacker, true, true);
669
670         /* register actions now so that set_state() can find them and set toggles/checks etc */
671         
672         register_actions ();
673         
674         XMLNode* node = ARDOUR_UI::instance()->editor_settings();
675         set_state (*node);
676
677         _playlist_selector = new PlaylistSelector();
678         _playlist_selector->signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (_playlist_selector)));
679
680         RegionView::RegionViewGoingAway.connect (mem_fun(*this, &Editor::catch_vanishing_regionview));
681
682         /* nudge stuff */
683
684         nudge_forward_button.add (*(manage (new Image (::get_icon("nudge_right")))));
685         nudge_backward_button.add (*(manage (new Image (::get_icon("nudge_left")))));
686
687         ARDOUR_UI::instance()->tooltips().set_tip (nudge_forward_button, _("Nudge Region/Selection Forwards"));
688         ARDOUR_UI::instance()->tooltips().set_tip (nudge_backward_button, _("Nudge Region/Selection Backwards"));
689
690         nudge_forward_button.set_name ("TransportButton");
691         nudge_backward_button.set_name ("TransportButton");
692
693         fade_context_menu.set_name ("ArdourContextMenu");
694
695         /* icons, titles, WM stuff */
696
697         list<Glib::RefPtr<Gdk::Pixbuf> > window_icons;
698         Glib::RefPtr<Gdk::Pixbuf> icon;
699
700         if ((icon = ::get_icon ("ardour_icon_16px")) != 0) {
701                 window_icons.push_back (icon);
702         }
703         if ((icon = ::get_icon ("ardour_icon_22px")) != 0) {
704                 window_icons.push_back (icon);
705         }
706         if ((icon = ::get_icon ("ardour_icon_32px")) != 0) {
707                 window_icons.push_back (icon);
708         }
709         if ((icon = ::get_icon ("ardour_icon_48px")) != 0) {
710                 window_icons.push_back (icon);
711         }
712         if (!window_icons.empty()) {
713                 set_icon_list (window_icons);
714                 set_default_icon_list (window_icons);
715         }
716
717         WindowTitle title(Glib::get_application_name());
718         title += _("Editor");
719         set_title (title.get_string());
720         set_wmclass (X_("ardour_editor"), "Ardour");
721
722         add (vpacker);
723         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
724
725         signal_configure_event().connect (mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::configure_handler));
726         signal_delete_event().connect (mem_fun (*ARDOUR_UI::instance(), &ARDOUR_UI::exit_on_main_window_close));
727
728         /* allow external control surfaces/protocols to do various things */
729
730         ControlProtocol::ZoomToSession.connect (mem_fun (*this, &Editor::temporal_zoom_session));
731         ControlProtocol::ZoomIn.connect (bind (mem_fun (*this, &Editor::temporal_zoom_step), false));
732         ControlProtocol::ZoomOut.connect (bind (mem_fun (*this, &Editor::temporal_zoom_step), true));
733         ControlProtocol::ScrollTimeline.connect (mem_fun (*this, &Editor::control_scroll));
734
735         Config->ParameterChanged.connect (mem_fun (*this, &Editor::parameter_changed));
736
737         constructed = true;
738         instant_save ();
739 }
740
741 Editor::~Editor()
742 {
743         /* <CMT Additions> */
744         if(image_socket_listener)
745         {
746                 if(image_socket_listener->is_connected())
747                 {
748                         image_socket_listener->close_connection() ;
749                 }
750                 
751                 delete image_socket_listener ;
752                 image_socket_listener = 0 ;
753         }
754         /* </CMT Additions> */
755 }
756
757 void
758 Editor::add_toplevel_controls (Container& cont)
759 {
760         vpacker.pack_start (cont, false, false);
761         cont.show_all ();
762 }
763
764 void
765 Editor::catch_vanishing_regionview (RegionView *rv)
766 {
767         /* note: the selection will take care of the vanishing
768            audioregionview by itself.
769         */
770
771         if (clicked_regionview == rv) {
772                 clicked_regionview = 0;
773         }
774
775         if (entered_regionview == rv) {
776                 set_entered_regionview (0);
777         }
778 }
779
780 void
781 Editor::set_entered_regionview (RegionView* rv)
782 {
783         if (rv == entered_regionview) {
784                 return;
785         }
786
787         if (entered_regionview) {
788                 entered_regionview->exited ();
789         }
790
791         if ((entered_regionview = rv) != 0) {
792                 entered_regionview->entered ();
793         }
794 }
795
796 void
797 Editor::set_entered_track (TimeAxisView* tav)
798 {
799         if (entered_track) {
800                 entered_track->exited ();
801         }
802
803         if ((entered_track = tav) != 0) {
804                 entered_track->entered ();
805         }
806 }
807
808 void
809 Editor::show_window ()
810 {
811         show_all ();
812         present ();
813
814         /* now reset all audio_time_axis heights, because widgets might need
815            to be re-hidden
816         */
817         
818         TimeAxisView *tv;
819         
820         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
821                 tv = (static_cast<TimeAxisView*>(*i));
822                 tv->reset_height ();
823         }
824 }
825
826 void
827 Editor::tie_vertical_scrolling ()
828 {
829         double y1 = vertical_adjustment.get_value();
830         controls_layout.get_vadjustment()->set_value (y1);
831         playhead_cursor->set_y_axis(y1);
832         edit_cursor->set_y_axis(y1);
833 }
834
835 void
836 Editor::instant_save ()
837 {
838         if (!constructed || !ARDOUR_UI::instance()->session_loaded) {
839                 return;
840         }
841
842         if (session) {
843                 session->add_instant_xml(get_state(), session->path());
844         } else {
845                 Config->add_instant_xml(get_state(), get_user_ardour_path());
846         }
847 }
848
849 void
850 Editor::edit_cursor_clock_changed()
851 {
852         if (edit_cursor->current_frame != edit_cursor_clock.current_time()) {
853                 edit_cursor->set_position (edit_cursor_clock.current_time());
854         }
855 }
856
857
858 void
859 Editor::zoom_adjustment_changed ()
860 {
861         if (session == 0) {
862                 return;
863         }
864
865         double fpu = zoom_range_clock.current_duration() / canvas_width;
866
867         if (fpu < 1.0) {
868                 fpu = 1.0;
869                 zoom_range_clock.set ((nframes_t) floor (fpu * canvas_width));
870         } else if (fpu > session->current_end_frame() / canvas_width) {
871                 fpu = session->current_end_frame() / canvas_width;
872                 zoom_range_clock.set ((nframes_t) floor (fpu * canvas_width));
873         }
874         
875         temporal_zoom (fpu);
876 }
877
878 void
879 Editor::control_scroll (float fraction)
880 {
881         ENSURE_GUI_THREAD(bind (mem_fun (*this, &Editor::control_scroll), fraction));
882
883         if (!session) {
884                 return;
885         }
886
887         double step = fraction * current_page_frames();
888         nframes_t target;
889
890         if ((fraction < 0.0f) && (session->transport_frame() < (nframes_t) fabs(step))) {
891                 target = 0;
892         } else if ((fraction > 0.0f) && (max_frames - session->transport_frame() < step)) {
893                 target = (max_frames - (current_page_frames()*2)); // allow room for slop in where the PH is on the screen
894         } else {
895                 target = (session->transport_frame() + (nframes_t) floor ((fraction * current_page_frames())));
896         }
897
898         /* move visuals, we'll catch up with it later */
899
900         playhead_cursor->set_position (target);
901
902         if (target > (current_page_frames() / 2)) {
903                 /* try to center PH in window */
904                 reset_x_origin (target - (current_page_frames()/2));
905         } else {
906                 reset_x_origin (0);
907         }
908
909         /* cancel the existing */
910
911         control_scroll_connection.disconnect ();
912
913         /* add the next one */
914
915         control_scroll_connection = Glib::signal_timeout().connect (bind (mem_fun (*this, &Editor::deferred_control_scroll), target), 50);
916 }
917
918 bool
919 Editor::deferred_control_scroll (nframes_t target)
920 {
921         session->request_locate (target);
922         return false;
923 }
924
925 void
926 Editor::on_realize ()
927 {
928         Window::on_realize ();
929         Realized ();
930 }
931
932 void
933 Editor::start_scrolling ()
934 {
935         scroll_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect 
936                 (mem_fun(*this, &Editor::update_current_screen));
937 }
938
939 void
940 Editor::stop_scrolling ()
941 {
942         scroll_connection.disconnect ();
943 }
944
945 void
946 Editor::map_position_change (nframes_t frame)
947 {
948         ENSURE_GUI_THREAD (bind (mem_fun(*this, &Editor::map_position_change), frame));
949
950         if (session == 0 || !_follow_playhead) {
951                 return;
952         }
953
954         center_screen (frame);
955         playhead_cursor->set_position (frame);
956 }       
957
958 void
959 Editor::center_screen (nframes_t frame)
960 {
961         double page = canvas_width * frames_per_unit;
962
963         /* if we're off the page, then scroll.
964          */
965         
966         if (frame < leftmost_frame || frame >= leftmost_frame + page) {
967                 center_screen_internal (frame, page);
968         }
969 }
970
971 void
972 Editor::center_screen_internal (nframes_t frame, float page)
973 {
974         page /= 2;
975                 
976         if (frame > page) {
977                 frame -= (nframes_t) page;
978         } else {
979                 frame = 0;
980         }
981
982         reset_x_origin (frame);
983 }
984
985 void
986 Editor::handle_new_duration ()
987 {
988         ENSURE_GUI_THREAD (mem_fun (*this, &Editor::handle_new_duration));
989
990         nframes_t new_end = session->get_maximum_extent() + (nframes_t) floorf (current_page_frames() * 0.10f);
991                                   
992         if (new_end > last_canvas_frame) {
993                 last_canvas_frame = new_end;
994                 horizontal_adjustment.set_upper (last_canvas_frame / frames_per_unit);
995                 reset_scrolling_region ();
996         }
997
998         horizontal_adjustment.set_value (leftmost_frame/frames_per_unit);
999 }
1000
1001 void
1002 Editor::update_title_s (const string & snap_name)
1003 {
1004         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::update_title_s), snap_name));
1005         
1006         update_title ();
1007 }
1008
1009 void
1010 Editor::update_title ()
1011 {
1012         ENSURE_GUI_THREAD (mem_fun(*this, &Editor::update_title));
1013
1014         if (session) {
1015                 bool dirty = session->dirty();
1016
1017                 string session_name;
1018
1019                 if (session->snap_name() != session->name()) {
1020                         session_name = session->snap_name();
1021                 } else {
1022                         session_name = session->name();
1023                 }
1024
1025                 if (dirty) {
1026                         session_name = "*" + session_name;
1027                 }
1028
1029                 WindowTitle title(session_name);
1030                 title += Glib::get_application_name();
1031                 set_title (title.get_string());
1032         }
1033 }
1034
1035 void
1036 Editor::connect_to_session (Session *t)
1037 {
1038         session = t;
1039
1040         XMLNode* node = ARDOUR_UI::instance()->editor_settings();
1041         set_state (*node);
1042
1043         /* catch up with the playhead */
1044
1045         session->request_locate (playhead_cursor->current_frame);
1046
1047         if (first_action_message) {
1048                 first_action_message->hide();
1049         }
1050
1051         update_title ();
1052
1053         session->GoingAway.connect (mem_fun(*this, &Editor::session_going_away));
1054         session->history().Changed.connect (mem_fun (*this, &Editor::history_changed));
1055
1056         /* These signals can all be emitted by a non-GUI thread. Therefore the
1057            handlers for them must not attempt to directly interact with the GUI,
1058            but use Gtkmm2ext::UI::instance()->call_slot();
1059         */
1060
1061         session_connections.push_back (session->TransportStateChange.connect (mem_fun(*this, &Editor::map_transport_state)));
1062         session_connections.push_back (session->PositionChanged.connect (mem_fun(*this, &Editor::map_position_change)));
1063         session_connections.push_back (session->RouteAdded.connect (mem_fun(*this, &Editor::handle_new_route)));
1064         session_connections.push_back (session->AudioRegionAdded.connect (mem_fun(*this, &Editor::handle_new_audio_region)));
1065         session_connections.push_back (session->AudioRegionRemoved.connect (mem_fun(*this, &Editor::handle_audio_region_removed)));
1066         session_connections.push_back (session->DurationChanged.connect (mem_fun(*this, &Editor::handle_new_duration)));
1067         session_connections.push_back (session->edit_group_added.connect (mem_fun(*this, &Editor::add_edit_group)));
1068         session_connections.push_back (session->edit_group_removed.connect (mem_fun(*this, &Editor::edit_groups_changed)));
1069         session_connections.push_back (session->NamedSelectionAdded.connect (mem_fun(*this, &Editor::handle_new_named_selection)));
1070         session_connections.push_back (session->NamedSelectionRemoved.connect (mem_fun(*this, &Editor::handle_new_named_selection)));
1071         session_connections.push_back (session->DirtyChanged.connect (mem_fun(*this, &Editor::update_title)));
1072         session_connections.push_back (session->StateSaved.connect (mem_fun(*this, &Editor::update_title_s)));
1073         session_connections.push_back (session->AskAboutPlaylistDeletion.connect (mem_fun(*this, &Editor::playlist_deletion_dialog)));
1074         session_connections.push_back (session->RegionHiddenChange.connect (mem_fun(*this, &Editor::region_hidden)));
1075
1076         session_connections.push_back (session->SMPTEOffsetChanged.connect (mem_fun(*this, &Editor::update_just_smpte)));
1077
1078         session_connections.push_back (session->tempo_map().StateChanged.connect (bind (mem_fun(*this, &Editor::tempo_map_changed), false)));
1079
1080         edit_groups_changed ();
1081
1082         edit_cursor_clock.set_session (session);
1083         zoom_range_clock.set_session (session);
1084         _playlist_selector->set_session (session);
1085         nudge_clock.set_session (session);
1086
1087 #ifdef FFT_ANALYSIS
1088         if (analysis_window != 0)
1089                 analysis_window->set_session (session);
1090 #endif
1091
1092         Location* loc = session->locations()->auto_loop_location();
1093         if (loc == 0) {
1094                 loc = new Location (0, session->current_end_frame(), _("Loop"),(Location::Flags) (Location::IsAutoLoop | Location::IsHidden));
1095                 if (loc->start() == loc->end()) {
1096                         loc->set_end (loc->start() + 1);
1097                 }
1098                 session->locations()->add (loc, false);
1099                 session->set_auto_loop_location (loc);
1100         } else {
1101                 // force name
1102                 loc->set_name (_("Loop"));
1103         }
1104         
1105         loc = session->locations()->auto_punch_location();
1106         if (loc == 0) {
1107                 loc = new Location (0, session->current_end_frame(), _("Punch"), (Location::Flags) (Location::IsAutoPunch | Location::IsHidden));
1108                 if (loc->start() == loc->end()) {
1109                         loc->set_end (loc->start() + 1);
1110                 }
1111                 session->locations()->add (loc, false);
1112                 session->set_auto_punch_location (loc);
1113         } else {
1114                 // force name
1115                 loc->set_name (_("Punch"));
1116         }
1117
1118         Config->map_parameters (mem_fun (*this, &Editor::parameter_changed));
1119         
1120         session->StateSaved.connect (mem_fun(*this, &Editor::session_state_saved));
1121         
1122         refresh_location_display ();
1123         session->locations()->added.connect (mem_fun(*this, &Editor::add_new_location));
1124         session->locations()->removed.connect (mem_fun(*this, &Editor::location_gone));
1125         session->locations()->changed.connect (mem_fun(*this, &Editor::refresh_location_display));
1126         session->locations()->StateChanged.connect (mem_fun(*this, &Editor::refresh_location_display_s));
1127         session->locations()->end_location()->changed.connect (mem_fun(*this, &Editor::end_location_changed));
1128
1129         handle_new_duration ();
1130
1131         redisplay_regions ();
1132         redisplay_named_selections ();
1133         redisplay_snapshots ();
1134
1135         initial_route_list_display ();
1136
1137         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
1138                 (static_cast<TimeAxisView*>(*i))->set_samples_per_unit (frames_per_unit);
1139         }
1140
1141         restore_ruler_visibility ();
1142         //tempo_map_changed (Change (0));
1143         session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
1144
1145         start_scrolling ();
1146
1147         /* don't show master bus in a new session */
1148
1149         if (ARDOUR_UI::instance()->session_is_new ()) {
1150
1151                 TreeModel::Children rows = route_display_model->children();
1152                 TreeModel::Children::iterator i;
1153         
1154                 no_route_list_redisplay = true;
1155                 
1156                 for (i = rows.begin(); i != rows.end(); ++i) {
1157                         TimeAxisView *tv =  (*i)[route_display_columns.tv];
1158                         AudioTimeAxisView *atv;
1159                         
1160                         if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
1161                                 if (atv->route()->master()) {
1162                                         route_list_display.get_selection()->unselect (i);
1163                                 }
1164                         }
1165                 }
1166                 
1167                 no_route_list_redisplay = false;
1168                 redisplay_route_list ();
1169         }
1170
1171         /* register for undo history */
1172
1173         session->register_with_memento_command_factory(_id, this);
1174 }
1175
1176 void
1177 Editor::build_cursors ()
1178 {
1179         using namespace Gdk;
1180         
1181         Gdk::Color mbg ("#000000" ); /* Black */
1182         Gdk::Color mfg ("#0000ff" ); /* Blue. */
1183
1184         {
1185                 RefPtr<Bitmap> source, mask;
1186                 source = Bitmap::create (mag_bits, mag_width, mag_height);
1187                 mask = Bitmap::create (magmask_bits, mag_width, mag_height);
1188                 zoom_cursor = new Gdk::Cursor (source, mask, mfg, mbg, mag_x_hot, mag_y_hot);
1189         }
1190
1191         Gdk::Color fbg ("#ffffff" );
1192         Gdk::Color ffg  ("#000000" );
1193         
1194         {
1195                 RefPtr<Bitmap> source, mask;
1196                 
1197                 source = Bitmap::create (fader_cursor_bits, fader_cursor_width, fader_cursor_height);
1198                 mask = Bitmap::create (fader_cursor_mask_bits, fader_cursor_width, fader_cursor_height);
1199                 fader_cursor = new Gdk::Cursor (source, mask, ffg, fbg, fader_cursor_x_hot, fader_cursor_y_hot);
1200         }
1201         
1202         { 
1203                 RefPtr<Bitmap> source, mask;
1204                 source = Bitmap::create (speaker_cursor_bits, speaker_cursor_width, speaker_cursor_height);
1205                 mask = Bitmap::create (speaker_cursor_mask_bits, speaker_cursor_width, speaker_cursor_height);
1206                 speaker_cursor = new Gdk::Cursor (source, mask, ffg, fbg, speaker_cursor_x_hot, speaker_cursor_y_hot);
1207         }
1208
1209         grabber_cursor = new Gdk::Cursor (HAND2);
1210         cross_hair_cursor = new Gdk::Cursor (CROSSHAIR);
1211         trimmer_cursor =  new Gdk::Cursor (SB_H_DOUBLE_ARROW);
1212         selector_cursor = new Gdk::Cursor (XTERM);
1213         time_fx_cursor = new Gdk::Cursor (SIZING);
1214         wait_cursor = new Gdk::Cursor  (WATCH);
1215         timebar_cursor = new Gdk::Cursor(LEFT_PTR);
1216 }
1217
1218 void
1219 Editor::popup_fade_context_menu (int button, int32_t time, ArdourCanvas::Item* item, ItemType item_type)
1220 {
1221         using namespace Menu_Helpers;
1222         AudioRegionView* arv = static_cast<AudioRegionView*> (item->get_data ("regionview"));
1223
1224         if (arv == 0) {
1225                 fatal << _("programming error: fade in canvas item has no regionview data pointer!") << endmsg;
1226                 /*NOTREACHED*/
1227         }
1228
1229         MenuList& items (fade_context_menu.items());
1230
1231         items.clear ();
1232
1233         switch (item_type) {
1234         case FadeInItem:
1235         case FadeInHandleItem:
1236                 if (arv->audio_region()->fade_in_active()) {
1237                         items.push_back (MenuElem (_("Deactivate"), bind (mem_fun (*this, &Editor::set_fade_in_active), false)));
1238                 } else {
1239                         items.push_back (MenuElem (_("Activate"), bind (mem_fun (*this, &Editor::set_fade_in_active), true)));
1240                 }
1241                 
1242                 items.push_back (SeparatorElem());
1243                 
1244                 items.push_back (MenuElem (_("Linear"), bind (mem_fun (*this, &Editor::set_fade_in_shape), AudioRegion::Linear)));
1245                 items.push_back (MenuElem (_("Slowest"), bind (mem_fun (*this, &Editor::set_fade_in_shape), AudioRegion::Fast)));
1246                 items.push_back (MenuElem (_("Slow"), bind (mem_fun (*this, &Editor::set_fade_in_shape), AudioRegion::LogB)));
1247                 items.push_back (MenuElem (_("Fast"), bind (mem_fun (*this, &Editor::set_fade_in_shape), AudioRegion::LogA)));
1248                 items.push_back (MenuElem (_("Fastest"), bind (mem_fun (*this, &Editor::set_fade_in_shape), AudioRegion::Slow)));
1249                 break;
1250
1251         case FadeOutItem:
1252         case FadeOutHandleItem:
1253                 if (arv->audio_region()->fade_out_active()) {
1254                         items.push_back (MenuElem (_("Deactivate"), bind (mem_fun (*this, &Editor::set_fade_out_active), false)));
1255                 } else {
1256                         items.push_back (MenuElem (_("Activate"), bind (mem_fun (*this, &Editor::set_fade_out_active), true)));
1257                 }
1258                 
1259                 items.push_back (SeparatorElem());
1260                 
1261                 items.push_back (MenuElem (_("Linear"), bind (mem_fun (*this, &Editor::set_fade_out_shape), AudioRegion::Linear)));
1262                 items.push_back (MenuElem (_("Slowest"), bind (mem_fun (*this, &Editor::set_fade_out_shape), AudioRegion::Slow)));
1263                 items.push_back (MenuElem (_("Slow"), bind (mem_fun (*this, &Editor::set_fade_out_shape), AudioRegion::LogA)));
1264                 items.push_back (MenuElem (_("Fast"), bind (mem_fun (*this, &Editor::set_fade_out_shape), AudioRegion::LogB)));
1265                 items.push_back (MenuElem (_("Fastest"), bind (mem_fun (*this, &Editor::set_fade_out_shape), AudioRegion::Fast)));
1266
1267                 break;
1268
1269         default:
1270                 fatal << _("programming error: ")
1271                       << X_("non-fade canvas item passed to popup_fade_context_menu()")
1272                       << endmsg;
1273                 /*NOTREACHED*/
1274         }
1275
1276         fade_context_menu.popup (button, time);
1277 }
1278
1279 void
1280 Editor::popup_track_context_menu (int button, int32_t time, ItemType item_type, bool with_selection, nframes_t frame)
1281 {
1282         using namespace Menu_Helpers;
1283         Menu* (Editor::*build_menu_function)(nframes_t);
1284         Menu *menu;
1285
1286         switch (item_type) {
1287         case RegionItem:
1288         case RegionViewName:
1289         case RegionViewNameHighlight:
1290                 if (with_selection) {
1291                         build_menu_function = &Editor::build_track_selection_context_menu;
1292                 } else {
1293                         build_menu_function = &Editor::build_track_region_context_menu;
1294                 }
1295                 break;
1296
1297         case SelectionItem:
1298                 if (with_selection) {
1299                         build_menu_function = &Editor::build_track_selection_context_menu;
1300                 } else {
1301                         build_menu_function = &Editor::build_track_context_menu;
1302                 }
1303                 break;
1304
1305         case CrossfadeViewItem:
1306                 build_menu_function = &Editor::build_track_crossfade_context_menu;
1307                 break;
1308
1309         case StreamItem:
1310                 if (clicked_audio_trackview->get_diskstream()) {
1311                         build_menu_function = &Editor::build_track_context_menu;
1312                 } else {
1313                         build_menu_function = &Editor::build_track_bus_context_menu;
1314                 }
1315                 break;
1316
1317         default:
1318                 /* probably shouldn't happen but if it does, we don't care */
1319                 return;
1320         }
1321
1322         menu = (this->*build_menu_function)(frame);
1323         menu->set_name ("ArdourContextMenu");
1324         
1325         /* now handle specific situations */
1326
1327         switch (item_type) {
1328         case RegionItem:
1329         case RegionViewName:
1330         case RegionViewNameHighlight:
1331                 if (!with_selection) {
1332                         if (region_edit_menu_split_item) {
1333                                 if (clicked_regionview && clicked_regionview->region()->covers (edit_cursor->current_frame)) {
1334                                         ActionManager::set_sensitive (ActionManager::edit_cursor_in_region_sensitive_actions, true);
1335                                 } else {
1336                                         ActionManager::set_sensitive (ActionManager::edit_cursor_in_region_sensitive_actions, false);
1337                                 }
1338                         }
1339                         /*
1340                         if (region_edit_menu_split_multichannel_item) {
1341                                 if (clicked_regionview && clicked_regionview->region().n_channels() > 1) {
1342                                         // GTK2FIX find the action, change its sensitivity
1343                                         // region_edit_menu_split_multichannel_item->set_sensitive (true);
1344                                 } else {
1345                                         // GTK2FIX see above
1346                                         // region_edit_menu_split_multichannel_item->set_sensitive (false);
1347                                 }
1348                         }*/
1349                 }
1350                 break;
1351
1352         case SelectionItem:
1353                 break;
1354
1355         case CrossfadeViewItem:
1356                 break;
1357
1358         case StreamItem:
1359                 break;
1360
1361         default:
1362                 /* probably shouldn't happen but if it does, we don't care */
1363                 return;
1364         }
1365
1366         if (clicked_audio_trackview && clicked_audio_trackview->audio_track()) {
1367
1368                 /* Bounce to disk */
1369                 
1370                 using namespace Menu_Helpers;
1371                 MenuList& edit_items  = menu->items();
1372                 
1373                 edit_items.push_back (SeparatorElem());
1374
1375                 switch (clicked_audio_trackview->audio_track()->freeze_state()) {
1376                 case AudioTrack::NoFreeze:
1377                         edit_items.push_back (MenuElem (_("Freeze"), mem_fun(*this, &Editor::freeze_route)));
1378                         break;
1379
1380                 case AudioTrack::Frozen:
1381                         edit_items.push_back (MenuElem (_("Unfreeze"), mem_fun(*this, &Editor::unfreeze_route)));
1382                         break;
1383                         
1384                 case AudioTrack::UnFrozen:
1385                         edit_items.push_back (MenuElem (_("Freeze"), mem_fun(*this, &Editor::freeze_route)));
1386                         break;
1387                 }
1388
1389         }
1390
1391         menu->popup (button, time);
1392 }
1393
1394 Menu*
1395 Editor::build_track_context_menu (nframes_t ignored)
1396 {
1397         using namespace Menu_Helpers;
1398
1399         MenuList& edit_items = track_context_menu.items();
1400         edit_items.clear();
1401
1402         add_dstream_context_items (edit_items);
1403         return &track_context_menu;
1404 }
1405
1406 Menu*
1407 Editor::build_track_bus_context_menu (nframes_t ignored)
1408 {
1409         using namespace Menu_Helpers;
1410
1411         MenuList& edit_items = track_context_menu.items();
1412         edit_items.clear();
1413
1414         add_bus_context_items (edit_items);
1415         return &track_context_menu;
1416 }
1417
1418 Menu*
1419 Editor::build_track_region_context_menu (nframes_t frame)
1420 {
1421         using namespace Menu_Helpers;
1422         MenuList& edit_items  = track_region_context_menu.items();
1423         edit_items.clear();
1424
1425         AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*> (clicked_trackview);
1426
1427         if (atv) {
1428                 boost::shared_ptr<Diskstream> ds;
1429                 boost::shared_ptr<Playlist> pl;
1430                 
1431                 if ((ds = atv->get_diskstream()) && ((pl = ds->playlist()))) {
1432                         Playlist::RegionList* regions = pl->regions_at ((nframes_t) floor ( (double)frame * ds->speed()));
1433                         for (Playlist::RegionList::iterator i = regions->begin(); i != regions->end(); ++i) {
1434                                 add_region_context_items (atv->audio_view(), (*i), edit_items);
1435                         }
1436                         delete regions;
1437                 }
1438         }
1439
1440         add_dstream_context_items (edit_items);
1441
1442         return &track_region_context_menu;
1443 }
1444
1445 Menu*
1446 Editor::build_track_crossfade_context_menu (nframes_t frame)
1447 {
1448         using namespace Menu_Helpers;
1449         MenuList& edit_items  = track_crossfade_context_menu.items();
1450         edit_items.clear ();
1451
1452         AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*> (clicked_trackview);
1453
1454         if (atv) {
1455                 boost::shared_ptr<Diskstream> ds;
1456                 boost::shared_ptr<Playlist> pl;
1457                 boost::shared_ptr<AudioPlaylist> apl;
1458
1459                 if ((ds = atv->get_diskstream()) && ((pl = ds->playlist()) != 0) && ((apl = boost::dynamic_pointer_cast<AudioPlaylist> (pl)) != 0)) {
1460
1461                         Playlist::RegionList* regions = pl->regions_at (frame);
1462                         AudioPlaylist::Crossfades xfades;
1463
1464                         apl->crossfades_at (frame, xfades);
1465
1466                         bool many = xfades.size() > 1;
1467
1468                         for (AudioPlaylist::Crossfades::iterator i = xfades.begin(); i != xfades.end(); ++i) {
1469                                 add_crossfade_context_items (atv->audio_view(), (*i), edit_items, many);
1470                         }
1471
1472                         for (Playlist::RegionList::iterator i = regions->begin(); i != regions->end(); ++i) {
1473                                 add_region_context_items (atv->audio_view(), (*i), edit_items);
1474                         }
1475
1476                         delete regions;
1477                 }
1478         }
1479
1480         add_dstream_context_items (edit_items);
1481
1482         return &track_crossfade_context_menu;
1483 }
1484
1485 #ifdef FFT_ANALYSIS
1486 void
1487 Editor::analyze_region_selection()
1488 {
1489         if (analysis_window == 0) {
1490                 analysis_window = new AnalysisWindow();
1491
1492                 if (session != 0)
1493                         analysis_window->set_session(session);
1494
1495                 analysis_window->show_all();
1496         }
1497
1498         analysis_window->set_regionmode();
1499         analysis_window->analyze();
1500         
1501         analysis_window->present();
1502 }
1503
1504 void
1505 Editor::analyze_range_selection()
1506 {
1507         if (analysis_window == 0) {
1508                 analysis_window = new AnalysisWindow();
1509
1510                 if (session != 0)
1511                         analysis_window->set_session(session);
1512
1513                 analysis_window->show_all();
1514         }
1515
1516         analysis_window->set_rangemode();
1517         analysis_window->analyze();
1518         
1519         analysis_window->present();
1520 }
1521 #endif /* FFT_ANALYSIS */
1522
1523
1524
1525 Menu*
1526 Editor::build_track_selection_context_menu (nframes_t ignored)
1527 {
1528         using namespace Menu_Helpers;
1529         MenuList& edit_items  = track_selection_context_menu.items();
1530         edit_items.clear ();
1531
1532         add_selection_context_items (edit_items);
1533         add_dstream_context_items (edit_items);
1534
1535         return &track_selection_context_menu;
1536 }
1537
1538 void
1539 Editor::add_crossfade_context_items (AudioStreamView* view, boost::shared_ptr<Crossfade> xfade, Menu_Helpers::MenuList& edit_items, bool many)
1540 {
1541         using namespace Menu_Helpers;
1542         Menu     *xfade_menu = manage (new Menu);
1543         MenuList& items       = xfade_menu->items();
1544         xfade_menu->set_name ("ArdourContextMenu");
1545         string str;
1546
1547         if (xfade->active()) {
1548                 str = _("Mute");
1549         } else { 
1550                 str = _("Unmute");
1551         }
1552
1553         items.push_back (MenuElem (str, bind (mem_fun(*this, &Editor::toggle_xfade_active), boost::weak_ptr<Crossfade> (xfade))));
1554         items.push_back (MenuElem (_("Edit"), bind (mem_fun(*this, &Editor::edit_xfade), boost::weak_ptr<Crossfade> (xfade))));
1555
1556         if (xfade->can_follow_overlap()) {
1557
1558                 if (xfade->following_overlap()) {
1559                         str = _("Convert to short");
1560                 } else {
1561                         str = _("Convert to full");
1562                 }
1563
1564                 items.push_back (MenuElem (str, bind (mem_fun(*this, &Editor::toggle_xfade_length), xfade)));
1565         }
1566
1567         if (many) {
1568                 str = xfade->out()->name();
1569                 str += "->";
1570                 str += xfade->in()->name();
1571         } else {
1572                 str = _("Crossfade");
1573         }
1574
1575         edit_items.push_back (MenuElem (str, *xfade_menu));
1576         edit_items.push_back (SeparatorElem());
1577 }
1578
1579 void
1580 Editor::xfade_edit_left_region ()
1581 {
1582         if (clicked_crossfadeview) {
1583                 clicked_crossfadeview->left_view.show_region_editor ();
1584         }
1585 }
1586
1587 void
1588 Editor::xfade_edit_right_region ()
1589 {
1590         if (clicked_crossfadeview) {
1591                 clicked_crossfadeview->right_view.show_region_editor ();
1592         }
1593 }
1594
1595 void
1596 Editor::add_region_context_items (AudioStreamView* sv, boost::shared_ptr<Region> region, Menu_Helpers::MenuList& edit_items)
1597 {
1598         using namespace Menu_Helpers;
1599         Menu     *region_menu = manage (new Menu);
1600         MenuList& items       = region_menu->items();
1601         region_menu->set_name ("ArdourContextMenu");
1602         
1603         boost::shared_ptr<AudioRegion> ar;
1604
1605         if (region) {
1606                 ar = boost::dynamic_pointer_cast<AudioRegion> (region);
1607         }
1608
1609         /* when this particular menu pops up, make the relevant region 
1610            become selected.
1611         */
1612
1613         region_menu->signal_map_event().connect (bind (mem_fun(*this, &Editor::set_selected_regionview_from_map_event), sv, boost::weak_ptr<Region>(region)));
1614
1615         items.push_back (MenuElem (_("Popup region editor"), mem_fun(*this, &Editor::edit_region)));
1616         items.push_back (MenuElem (_("Raise to top layer"), mem_fun(*this, &Editor::raise_region_to_top)));
1617         items.push_back (MenuElem (_("Lower to bottom layer"), mem_fun  (*this, &Editor::lower_region_to_bottom)));
1618         items.push_back (SeparatorElem());
1619         items.push_back (MenuElem (_("Define sync point"), mem_fun(*this, &Editor::set_region_sync_from_edit_cursor)));
1620         items.push_back (MenuElem (_("Remove sync point"), mem_fun(*this, &Editor::remove_region_sync)));
1621         items.push_back (SeparatorElem());
1622
1623         items.push_back (MenuElem (_("Audition"), mem_fun(*this, &Editor::audition_selected_region)));
1624         items.push_back (MenuElem (_("Export"), mem_fun(*this, &Editor::export_region)));
1625         items.push_back (MenuElem (_("Bounce"), mem_fun(*this, &Editor::bounce_region_selection)));
1626
1627 #ifdef FFT_ANALYSIS
1628         items.push_back (MenuElem (_("Analyze region"), mem_fun(*this, &Editor::analyze_region_selection)));
1629 #endif
1630
1631         items.push_back (SeparatorElem());
1632
1633         items.push_back (CheckMenuElem (_("Lock"), mem_fun(*this, &Editor::toggle_region_lock)));
1634         region_lock_item = static_cast<CheckMenuItem*>(&items.back());
1635         if (region->locked()) {
1636                 region_lock_item->set_active();
1637         }
1638         items.push_back (CheckMenuElem (_("Mute"), mem_fun(*this, &Editor::toggle_region_mute)));
1639         region_mute_item = static_cast<CheckMenuItem*>(&items.back());
1640         if (region->muted()) {
1641                 region_mute_item->set_active();
1642         }
1643         items.push_back (CheckMenuElem (_("Opaque"), mem_fun(*this, &Editor::toggle_region_opaque)));
1644         region_opaque_item = static_cast<CheckMenuItem*>(&items.back());
1645         if (region->opaque()) {
1646                 region_opaque_item->set_active();
1647         }
1648
1649         items.push_back (CheckMenuElem (_("Original position"), mem_fun(*this, &Editor::naturalize)));
1650         if (region->at_natural_position()) {
1651                 items.back().set_sensitive (false);
1652         }
1653
1654         items.push_back (SeparatorElem());
1655
1656         if (ar) {
1657                 
1658                 RegionView* rv = sv->find_view (ar);
1659                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(rv);
1660
1661                 items.push_back (MenuElem (_("Reset Envelope"), mem_fun(*this, &Editor::reset_region_gain_envelopes)));
1662                 
1663                 items.push_back (CheckMenuElem (_("Envelope Visible"), mem_fun(*this, &Editor::toggle_gain_envelope_visibility)));
1664                 region_envelope_visible_item = static_cast<CheckMenuItem*> (&items.back());
1665
1666                 if (arv->envelope_visible()) {
1667                         region_envelope_visible_item->set_active (true);
1668                 }
1669
1670                 items.push_back (CheckMenuElem (_("Envelope Active"), mem_fun(*this, &Editor::toggle_gain_envelope_active)));
1671                 region_envelope_active_item = static_cast<CheckMenuItem*> (&items.back());
1672
1673                 if (ar->envelope_active()) {
1674                         region_envelope_active_item->set_active (true);
1675                 }
1676
1677                 items.push_back (SeparatorElem());
1678
1679                 if (ar->scale_amplitude() != 1.0f) {
1680                         items.push_back (MenuElem (_("DeNormalize"), mem_fun(*this, &Editor::denormalize_region)));
1681                 } else {
1682                         items.push_back (MenuElem (_("Normalize"), mem_fun(*this, &Editor::normalize_region)));
1683                 }
1684         }
1685         items.push_back (MenuElem (_("Reverse"), mem_fun(*this, &Editor::reverse_region)));
1686         items.push_back (SeparatorElem());
1687
1688
1689         /* range related stuff */
1690
1691         items.push_back (MenuElem (_("Add Range Markers"), mem_fun (*this, &Editor::add_location_from_audio_region)));
1692         items.push_back (MenuElem (_("Set Range Selection"), mem_fun (*this, &Editor::set_selection_from_audio_region)));
1693         items.push_back (SeparatorElem());
1694                          
1695         /* Nudge region */
1696
1697         Menu *nudge_menu = manage (new Menu());
1698         MenuList& nudge_items = nudge_menu->items();
1699         nudge_menu->set_name ("ArdourContextMenu");
1700         
1701         nudge_items.push_back (MenuElem (_("Nudge fwd"), (bind (mem_fun(*this, &Editor::nudge_forward), false))));
1702         nudge_items.push_back (MenuElem (_("Nudge bwd"), (bind (mem_fun(*this, &Editor::nudge_backward), false))));
1703         nudge_items.push_back (MenuElem (_("Nudge fwd by capture offset"), (mem_fun(*this, &Editor::nudge_forward_capture_offset))));
1704         nudge_items.push_back (MenuElem (_("Nudge bwd by capture offset"), (mem_fun(*this, &Editor::nudge_backward_capture_offset))));
1705
1706         items.push_back (MenuElem (_("Nudge"), *nudge_menu));
1707         items.push_back (SeparatorElem());
1708
1709         Menu *trim_menu = manage (new Menu);
1710         MenuList& trim_items = trim_menu->items();
1711         trim_menu->set_name ("ArdourContextMenu");
1712         
1713         trim_items.push_back (MenuElem (_("Start to edit cursor"), mem_fun(*this, &Editor::trim_region_from_edit_cursor)));
1714         trim_items.push_back (MenuElem (_("Edit cursor to end"), mem_fun(*this, &Editor::trim_region_to_edit_cursor)));
1715                              
1716         items.push_back (MenuElem (_("Trim"), *trim_menu));
1717         items.push_back (SeparatorElem());
1718
1719         items.push_back (MenuElem (_("Split"), (mem_fun(*this, &Editor::split_region))));
1720         region_edit_menu_split_item = &items.back();
1721
1722         items.push_back (MenuElem (_("Make mono regions"), (mem_fun(*this, &Editor::split_multichannel_region))));
1723         region_edit_menu_split_multichannel_item = &items.back();
1724
1725         items.push_back (MenuElem (_("Duplicate"), (bind (mem_fun(*this, &Editor::duplicate_dialog), true))));
1726         items.push_back (MenuElem (_("Fill Track"), (mem_fun(*this, &Editor::region_fill_track))));
1727         items.push_back (SeparatorElem());
1728         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &Editor::remove_clicked_region)));
1729
1730         /* OK, stick the region submenu at the top of the list, and then add
1731            the standard items.
1732         */
1733
1734         /* we have to hack up the region name because "_" has a special
1735            meaning for menu titles.
1736         */
1737
1738         string::size_type pos = 0;
1739         string menu_item_name = region->name();
1740
1741         while ((pos = menu_item_name.find ("_", pos)) != string::npos) {
1742                 menu_item_name.replace (pos, 1, "__");
1743                 pos += 2;
1744         }
1745         
1746         edit_items.push_back (MenuElem (menu_item_name, *region_menu));
1747         edit_items.push_back (SeparatorElem());
1748 }
1749
1750 void
1751 Editor::add_selection_context_items (Menu_Helpers::MenuList& edit_items)
1752 {
1753         using namespace Menu_Helpers;
1754         Menu     *selection_menu = manage (new Menu);
1755         MenuList& items       = selection_menu->items();
1756         selection_menu->set_name ("ArdourContextMenu");
1757
1758         items.push_back (MenuElem (_("Play range"), mem_fun(*this, &Editor::play_selection)));
1759         items.push_back (MenuElem (_("Loop range"), mem_fun(*this, &Editor::set_route_loop_selection)));
1760
1761 #ifdef FFT_ANALYSIS
1762         items.push_back (SeparatorElem());
1763         items.push_back (MenuElem (_("Analyze range"), mem_fun(*this, &Editor::analyze_range_selection)));
1764 #endif
1765         
1766         items.push_back (SeparatorElem());
1767         items.push_back (MenuElem (_("Separate range to track"), mem_fun(*this, &Editor::separate_region_from_selection)));
1768         items.push_back (MenuElem (_("Separate range to region list"), mem_fun(*this, &Editor::new_region_from_selection)));
1769         
1770         items.push_back (SeparatorElem());
1771         items.push_back (MenuElem (_("Select all in range"), mem_fun(*this, &Editor::select_all_selectables_using_time_selection)));
1772         items.push_back (SeparatorElem());
1773         items.push_back (MenuElem (_("Add Range Markers"), mem_fun (*this, &Editor::add_location_from_selection)));
1774         items.push_back (MenuElem (_("Set range to loop range"), mem_fun(*this, &Editor::set_selection_from_loop)));
1775         items.push_back (MenuElem (_("Set range to punch range"), mem_fun(*this, &Editor::set_selection_from_punch)));
1776         items.push_back (SeparatorElem());
1777         items.push_back (MenuElem (_("Crop region to range"), mem_fun(*this, &Editor::crop_region_to_selection)));
1778         items.push_back (MenuElem (_("Fill range with region"), mem_fun(*this, &Editor::region_fill_selection)));
1779         items.push_back (MenuElem (_("Duplicate range"), bind (mem_fun(*this, &Editor::duplicate_dialog), false)));
1780         items.push_back (MenuElem (_("Create chunk from range"), mem_fun(*this, &Editor::create_named_selection)));
1781         items.push_back (SeparatorElem());
1782         items.push_back (MenuElem (_("Bounce range"), mem_fun(*this, &Editor::bounce_range_selection)));
1783         items.push_back (MenuElem (_("Export range"), mem_fun(*this, &Editor::export_selection)));
1784
1785         edit_items.push_back (MenuElem (_("Range"), *selection_menu));
1786         edit_items.push_back (SeparatorElem());
1787 }
1788
1789 void
1790 Editor::add_dstream_context_items (Menu_Helpers::MenuList& edit_items)
1791 {
1792         using namespace Menu_Helpers;
1793
1794         /* Playback */
1795
1796         Menu *play_menu = manage (new Menu);
1797         MenuList& play_items = play_menu->items();
1798         play_menu->set_name ("ArdourContextMenu");
1799         
1800         play_items.push_back (MenuElem (_("Play from edit cursor"), mem_fun(*this, &Editor::play_from_edit_cursor)));
1801         play_items.push_back (MenuElem (_("Play from start"), mem_fun(*this, &Editor::play_from_start)));
1802         play_items.push_back (MenuElem (_("Play region"), mem_fun(*this, &Editor::play_selected_region)));
1803         play_items.push_back (SeparatorElem());
1804         play_items.push_back (MenuElem (_("Loop Region"), mem_fun(*this, &Editor::loop_selected_region)));
1805         
1806         edit_items.push_back (MenuElem (_("Play"), *play_menu));
1807
1808         /* Selection */
1809
1810         Menu *select_menu = manage (new Menu);
1811         MenuList& select_items = select_menu->items();
1812         select_menu->set_name ("ArdourContextMenu");
1813         
1814         select_items.push_back (MenuElem (_("Select All in track"), bind (mem_fun(*this, &Editor::select_all_in_track), Selection::Set)));
1815         select_items.push_back (MenuElem (_("Select All"), bind (mem_fun(*this, &Editor::select_all), Selection::Set)));
1816         select_items.push_back (MenuElem (_("Invert selection in track"), mem_fun(*this, &Editor::invert_selection_in_track)));
1817         select_items.push_back (MenuElem (_("Invert selection"), mem_fun(*this, &Editor::invert_selection)));
1818         select_items.push_back (SeparatorElem());
1819         select_items.push_back (MenuElem (_("Set range to loop range"), mem_fun(*this, &Editor::set_selection_from_loop)));
1820         select_items.push_back (MenuElem (_("Set range to punch range"), mem_fun(*this, &Editor::set_selection_from_punch)));
1821         select_items.push_back (SeparatorElem());
1822         select_items.push_back (MenuElem (_("Select all after edit cursor"), bind (mem_fun(*this, &Editor::select_all_selectables_using_cursor), edit_cursor, true)));
1823         select_items.push_back (MenuElem (_("Select all before edit cursor"), bind (mem_fun(*this, &Editor::select_all_selectables_using_cursor), edit_cursor, false)));
1824         select_items.push_back (MenuElem (_("Select all after playhead"), bind (mem_fun(*this, &Editor::select_all_selectables_using_cursor), playhead_cursor, true)));
1825         select_items.push_back (MenuElem (_("Select all before playhead"), bind (mem_fun(*this, &Editor::select_all_selectables_using_cursor), playhead_cursor, false)));
1826         select_items.push_back (MenuElem (_("Select all between cursors"), bind (mem_fun(*this, &Editor::select_all_selectables_between_cursors), playhead_cursor, edit_cursor)));
1827         select_items.push_back (SeparatorElem());
1828
1829         edit_items.push_back (MenuElem (_("Select"), *select_menu));
1830
1831         /* Cut-n-Paste */
1832
1833         Menu *cutnpaste_menu = manage (new Menu);
1834         MenuList& cutnpaste_items = cutnpaste_menu->items();
1835         cutnpaste_menu->set_name ("ArdourContextMenu");
1836         
1837         cutnpaste_items.push_back (MenuElem (_("Cut"), mem_fun(*this, &Editor::cut)));
1838         cutnpaste_items.push_back (MenuElem (_("Copy"), mem_fun(*this, &Editor::copy)));
1839         cutnpaste_items.push_back (MenuElem (_("Paste at edit cursor"), bind (mem_fun(*this, &Editor::paste), 1.0f)));
1840         cutnpaste_items.push_back (MenuElem (_("Paste at mouse"), mem_fun(*this, &Editor::mouse_paste)));
1841
1842         cutnpaste_items.push_back (SeparatorElem());
1843
1844         cutnpaste_items.push_back (MenuElem (_("Align"), bind (mem_fun(*this, &Editor::align), ARDOUR::SyncPoint)));
1845         cutnpaste_items.push_back (MenuElem (_("Align Relative"), bind (mem_fun(*this, &Editor::align_relative), ARDOUR::SyncPoint)));
1846
1847         cutnpaste_items.push_back (SeparatorElem());
1848
1849         cutnpaste_items.push_back (MenuElem (_("Insert chunk"), bind (mem_fun(*this, &Editor::paste_named_selection), 1.0f)));
1850
1851         edit_items.push_back (MenuElem (_("Edit"), *cutnpaste_menu));
1852
1853         /* Adding new material */
1854         
1855         edit_items.push_back (SeparatorElem());
1856         edit_items.push_back (MenuElem (_("Insert Selected Region"), bind (mem_fun(*this, &Editor::insert_region_list_selection), 1.0f)));
1857         edit_items.push_back (MenuElem (_("Insert Existing Audio"), bind (mem_fun(*this, &Editor::add_external_audio_action), ImportToTrack)));
1858
1859         /* Nudge track */
1860
1861         Menu *nudge_menu = manage (new Menu());
1862         MenuList& nudge_items = nudge_menu->items();
1863         nudge_menu->set_name ("ArdourContextMenu");
1864         
1865         edit_items.push_back (SeparatorElem());
1866         nudge_items.push_back (MenuElem (_("Nudge entire track fwd"), (bind (mem_fun(*this, &Editor::nudge_track), false, true))));
1867         nudge_items.push_back (MenuElem (_("Nudge track after edit cursor fwd"), (bind (mem_fun(*this, &Editor::nudge_track), true, true))));
1868         nudge_items.push_back (MenuElem (_("Nudge entire track bwd"), (bind (mem_fun(*this, &Editor::nudge_track), false, false))));
1869         nudge_items.push_back (MenuElem (_("Nudge track after edit cursor bwd"), (bind (mem_fun(*this, &Editor::nudge_track), true, false))));
1870
1871         edit_items.push_back (MenuElem (_("Nudge"), *nudge_menu));
1872 }
1873
1874 void
1875 Editor::add_bus_context_items (Menu_Helpers::MenuList& edit_items)
1876 {
1877         using namespace Menu_Helpers;
1878
1879         /* Playback */
1880
1881         Menu *play_menu = manage (new Menu);
1882         MenuList& play_items = play_menu->items();
1883         play_menu->set_name ("ArdourContextMenu");
1884         
1885         play_items.push_back (MenuElem (_("Play from edit cursor"), mem_fun(*this, &Editor::play_from_edit_cursor)));
1886         play_items.push_back (MenuElem (_("Play from start"), mem_fun(*this, &Editor::play_from_start)));
1887         edit_items.push_back (MenuElem (_("Play"), *play_menu));
1888
1889         /* Selection */
1890
1891         Menu *select_menu = manage (new Menu);
1892         MenuList& select_items = select_menu->items();
1893         select_menu->set_name ("ArdourContextMenu");
1894         
1895         select_items.push_back (MenuElem (_("Select All in track"), bind (mem_fun(*this, &Editor::select_all_in_track), Selection::Set)));
1896         select_items.push_back (MenuElem (_("Select All"), bind (mem_fun(*this, &Editor::select_all), Selection::Set)));
1897         select_items.push_back (MenuElem (_("Invert selection in track"), mem_fun(*this, &Editor::invert_selection_in_track)));
1898         select_items.push_back (MenuElem (_("Invert selection"), mem_fun(*this, &Editor::invert_selection)));
1899         select_items.push_back (SeparatorElem());
1900         select_items.push_back (MenuElem (_("Select all after edit cursor"), bind (mem_fun(*this, &Editor::select_all_selectables_using_cursor), edit_cursor, true)));
1901         select_items.push_back (MenuElem (_("Select all before edit cursor"), bind (mem_fun(*this, &Editor::select_all_selectables_using_cursor), edit_cursor, false)));
1902         select_items.push_back (MenuElem (_("Select all after playhead"), bind (mem_fun(*this, &Editor::select_all_selectables_using_cursor), playhead_cursor, true)));
1903         select_items.push_back (MenuElem (_("Select all before playhead"), bind (mem_fun(*this, &Editor::select_all_selectables_using_cursor), playhead_cursor, false)));
1904
1905         edit_items.push_back (MenuElem (_("Select"), *select_menu));
1906
1907         /* Cut-n-Paste */
1908
1909         Menu *cutnpaste_menu = manage (new Menu);
1910         MenuList& cutnpaste_items = cutnpaste_menu->items();
1911         cutnpaste_menu->set_name ("ArdourContextMenu");
1912         
1913         cutnpaste_items.push_back (MenuElem (_("Cut"), mem_fun(*this, &Editor::cut)));
1914         cutnpaste_items.push_back (MenuElem (_("Copy"), mem_fun(*this, &Editor::copy)));
1915         cutnpaste_items.push_back (MenuElem (_("Paste"), bind (mem_fun(*this, &Editor::paste), 1.0f)));
1916
1917         Menu *nudge_menu = manage (new Menu());
1918         MenuList& nudge_items = nudge_menu->items();
1919         nudge_menu->set_name ("ArdourContextMenu");
1920         
1921         edit_items.push_back (SeparatorElem());
1922         nudge_items.push_back (MenuElem (_("Nudge entire track fwd"), (bind (mem_fun(*this, &Editor::nudge_track), false, true))));
1923         nudge_items.push_back (MenuElem (_("Nudge track after edit cursor fwd"), (bind (mem_fun(*this, &Editor::nudge_track), true, true))));
1924         nudge_items.push_back (MenuElem (_("Nudge entire track bwd"), (bind (mem_fun(*this, &Editor::nudge_track), false, false))));
1925         nudge_items.push_back (MenuElem (_("Nudge track after edit cursor bwd"), (bind (mem_fun(*this, &Editor::nudge_track), true, false))));
1926
1927         edit_items.push_back (MenuElem (_("Nudge"), *nudge_menu));
1928 }
1929
1930 /* CURSOR SETTING AND MARKS AND STUFF */
1931
1932 void
1933 Editor::set_snap_to (SnapType st)
1934 {
1935         snap_type = st;
1936         string str = snap_type_strings[(int) st];
1937
1938         if (str != snap_type_selector.get_active_text()) {
1939                 snap_type_selector.set_active_text (str);
1940         }
1941
1942         instant_save ();
1943
1944         switch (snap_type) {
1945         case SnapToAThirtysecondBeat:
1946         case SnapToASixteenthBeat:
1947         case SnapToAEighthBeat:
1948         case SnapToAQuarterBeat:
1949         case SnapToAThirdBeat:
1950                 update_tempo_based_rulers ();
1951         default:
1952                 /* relax */
1953                 break;
1954     }
1955 }
1956
1957 void
1958 Editor::set_snap_mode (SnapMode mode)
1959 {
1960         snap_mode = mode;
1961         string str = snap_mode_strings[(int)mode];
1962
1963         if (str != snap_mode_selector.get_active_text ()) {
1964                 snap_mode_selector.set_active_text (str);
1965         }
1966
1967         instant_save ();
1968 }
1969
1970 int
1971 Editor::set_state (const XMLNode& node)
1972 {
1973         const XMLProperty* prop;
1974         XMLNode* geometry;
1975         int x, y, xoff, yoff;
1976         Gdk::Geometry g;
1977
1978         if ((prop = node.property ("id")) != 0) {
1979                 _id = prop->value ();
1980         }
1981
1982         if ((geometry = find_named_node (node, "geometry")) == 0) {
1983
1984                 g.base_width = default_width;
1985                 g.base_height = default_height;
1986                 x = 1;
1987                 y = 1;
1988                 xoff = 0;
1989                 yoff = 21;
1990
1991         } else {
1992
1993                 g.base_width = atoi(geometry->property("x_size")->value());
1994                 g.base_height = atoi(geometry->property("y_size")->value());
1995                 x = atoi(geometry->property("x_pos")->value());
1996                 y = atoi(geometry->property("y_pos")->value());
1997                 xoff = atoi(geometry->property("x_off")->value());
1998                 yoff = atoi(geometry->property("y_off")->value());
1999         }
2000
2001         set_default_size (g.base_width, g.base_height);
2002         move (x, y);
2003
2004         if (session && (prop = node.property ("playhead"))) {
2005                 nframes_t pos = atol (prop->value().c_str());
2006                 playhead_cursor->set_position (pos);
2007         } else {
2008                 playhead_cursor->set_position (0);
2009
2010                 /* reset_x_origin() doesn't work right here, since the old
2011                    position may be zero already, and it does nothing in such
2012                    circumstances.
2013                 */
2014                 
2015                 leftmost_frame = 0;
2016                 horizontal_adjustment.set_value (0);
2017         }
2018
2019         if (session && (prop = node.property ("edit-cursor"))) {
2020                 nframes_t pos = atol (prop->value().c_str());
2021                 edit_cursor->set_position (pos);
2022         } else {
2023                 edit_cursor->set_position (0);
2024         }
2025
2026         if ((prop = node.property ("zoom-focus"))) {
2027                 set_zoom_focus ((ZoomFocus) atoi (prop->value()));
2028         }
2029
2030         if ((prop = node.property ("zoom"))) {
2031                 reset_zoom (PBD::atof (prop->value()));
2032         }
2033
2034         if ((prop = node.property ("snap-to"))) {
2035                 set_snap_to ((SnapType) atoi (prop->value()));
2036         }
2037
2038         if ((prop = node.property ("snap-mode"))) {
2039                 set_snap_mode ((SnapMode) atoi (prop->value()));
2040         }
2041
2042         if ((prop = node.property ("mouse-mode"))) {
2043                 MouseMode m = str2mousemode(prop->value());
2044                 mouse_mode = MouseMode ((int) m + 1); /* lie, force mode switch */
2045                 set_mouse_mode (m, true);
2046         } else {
2047                 mouse_mode = MouseGain; /* lie, to force the mode switch */
2048                 set_mouse_mode (MouseObject, true);
2049         }
2050
2051         if ((prop = node.property ("show-waveforms"))) {
2052                 bool yn = (prop->value() == "yes");
2053                 _show_waveforms = !yn;
2054                 RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("ToggleWaveformVisibility"));
2055                 if (act) {
2056                         RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
2057                         /* do it twice to force the change */
2058                         tact->set_active (!yn);
2059                         tact->set_active (yn);
2060                 }
2061         }
2062
2063         if ((prop = node.property ("show-waveforms-recording"))) {
2064                 bool yn = (prop->value() == "yes");
2065                 _show_waveforms_recording = !yn;
2066                 RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("ToggleWaveformsWhileRecording"));
2067                 if (act) {
2068                         RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
2069                         /* do it twice to force the change */
2070                         tact->set_active (!yn);
2071                         tact->set_active (yn);
2072                 }
2073         }
2074         
2075         if ((prop = node.property ("show-measures"))) {
2076                 bool yn = (prop->value() == "yes");
2077                 _show_measures = !yn;
2078                 RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("ToggleMeasureVisibility"));
2079                 if (act) {
2080                         RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
2081                         /* do it twice to force the change */
2082                         tact->set_active (!yn);
2083                         tact->set_active (yn);
2084                 }
2085         }
2086
2087         if ((prop = node.property ("follow-playhead"))) {
2088                 bool yn = (prop->value() == "yes");
2089                 RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("toggle-follow-playhead"));
2090                 if (act) {
2091                         RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
2092                         /* do it twice to force the change */
2093                         tact->set_active (!yn);
2094                         tact->set_active (yn);
2095                 }
2096         }
2097
2098
2099         if ((prop = node.property ("region-list-sort-type"))) {
2100                 region_list_sort_type = (Editing::RegionListSortType) -1; // force change 
2101                 reset_region_list_sort_type(str2regionlistsorttype(prop->value()));
2102         }
2103
2104         if ((prop = node.property ("xfades-visible"))) {
2105                 bool yn = (prop->value() == "yes");
2106                 _xfade_visibility = !yn;
2107                 // set_xfade_visibility (yn);
2108         }
2109
2110         if ((prop = node.property ("show-editor-mixer"))) {
2111
2112                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
2113                 if (act) {
2114
2115                         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2116                         bool yn = (prop->value() == X_("yes"));
2117
2118                         /* do it twice to force the change */
2119                         
2120                         tact->set_active (!yn);
2121                         tact->set_active (yn);
2122                 }
2123         }
2124
2125
2126         return 0;
2127 }
2128
2129 XMLNode&
2130 Editor::get_state ()
2131 {
2132         XMLNode* node = new XMLNode ("Editor");
2133         char buf[32];
2134
2135         _id.print (buf, sizeof (buf));
2136         node->add_property ("id", buf);
2137         
2138         if (is_realized()) {
2139                 Glib::RefPtr<Gdk::Window> win = get_window();
2140                 
2141                 int x, y, xoff, yoff, width, height;
2142                 win->get_root_origin(x, y);
2143                 win->get_position(xoff, yoff);
2144                 win->get_size(width, height);
2145                 
2146                 XMLNode* geometry = new XMLNode ("geometry");
2147
2148                 snprintf(buf, sizeof(buf), "%d", width);
2149                 geometry->add_property("x_size", string(buf));
2150                 snprintf(buf, sizeof(buf), "%d", height);
2151                 geometry->add_property("y_size", string(buf));
2152                 snprintf(buf, sizeof(buf), "%d", x);
2153                 geometry->add_property("x_pos", string(buf));
2154                 snprintf(buf, sizeof(buf), "%d", y);
2155                 geometry->add_property("y_pos", string(buf));
2156                 snprintf(buf, sizeof(buf), "%d", xoff);
2157                 geometry->add_property("x_off", string(buf));
2158                 snprintf(buf, sizeof(buf), "%d", yoff);
2159                 geometry->add_property("y_off", string(buf));
2160                 snprintf(buf,sizeof(buf), "%d",gtk_paned_get_position (static_cast<Paned*>(&edit_pane)->gobj()));
2161                 geometry->add_property("edit_pane_pos", string(buf));
2162
2163                 node->add_child_nocopy (*geometry);
2164         }
2165
2166         snprintf (buf, sizeof(buf), "%d", (int) zoom_focus);
2167         node->add_property ("zoom-focus", buf);
2168         snprintf (buf, sizeof(buf), "%f", frames_per_unit);
2169         node->add_property ("zoom", buf);
2170         snprintf (buf, sizeof(buf), "%d", (int) snap_type);
2171         node->add_property ("snap-to", buf);
2172         snprintf (buf, sizeof(buf), "%d", (int) snap_mode);
2173         node->add_property ("snap-mode", buf);
2174
2175         snprintf (buf, sizeof (buf), "%" PRIu32, playhead_cursor->current_frame);
2176         node->add_property ("playhead", buf);
2177         snprintf (buf, sizeof (buf), "%" PRIu32, edit_cursor->current_frame);
2178         node->add_property ("edit-cursor", buf);
2179
2180         node->add_property ("show-waveforms", _show_waveforms ? "yes" : "no");
2181         node->add_property ("show-waveforms-recording", _show_waveforms_recording ? "yes" : "no");
2182         node->add_property ("show-measures", _show_measures ? "yes" : "no");
2183         node->add_property ("follow-playhead", _follow_playhead ? "yes" : "no");
2184         node->add_property ("xfades-visible", _xfade_visibility ? "yes" : "no");
2185         node->add_property ("region-list-sort-type", enum2str(region_list_sort_type));
2186         node->add_property ("mouse-mode", enum2str(mouse_mode));
2187         
2188         Glib::RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
2189         if (act) {
2190                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2191                 node->add_property (X_("show-editor-mixer"), tact->get_active() ? "yes" : "no");
2192         }
2193
2194         return *node;
2195 }
2196
2197
2198
2199 TimeAxisView *
2200 Editor::trackview_by_y_position (double y)
2201 {
2202         for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
2203
2204                 TimeAxisView *tv;
2205
2206                 if ((tv = (*iter)->covers_y_position (y)) != 0) {
2207                         return tv;
2208                 }
2209         }
2210
2211         return 0;
2212 }
2213
2214 void
2215 Editor::snap_to (nframes_t& start, int32_t direction, bool for_mark)
2216 {
2217         Location* before = 0;
2218         Location* after = 0;
2219
2220         if (!session) {
2221                 return;
2222         }
2223
2224         const nframes_t one_second = session->frame_rate();
2225         const nframes_t one_minute = session->frame_rate() * 60;
2226         const nframes_t one_smpte_second = (nframes_t)(rint(session->smpte_frames_per_second()) * session->frames_per_smpte_frame());
2227         nframes_t one_smpte_minute = (nframes_t)(rint(session->smpte_frames_per_second()) * session->frames_per_smpte_frame() * 60);
2228         nframes_t presnap = start;
2229
2230         switch (snap_type) {
2231         case SnapToFrame:
2232                 break;
2233
2234         case SnapToCDFrame:
2235                 if (direction) {
2236                         start = (nframes_t) ceil ((double) start / (one_second / 75)) * (one_second / 75);
2237                 } else {
2238                         start = (nframes_t) floor ((double) start / (one_second / 75)) * (one_second / 75);
2239                 }
2240                 break;
2241
2242         case SnapToSMPTEFrame:
2243                 if (fmod((double)start, (double)session->frames_per_smpte_frame()) > (session->frames_per_smpte_frame() / 2)) {
2244                         start = (nframes_t) (ceil ((double) start / session->frames_per_smpte_frame()) * session->frames_per_smpte_frame());
2245                 } else {
2246                         start = (nframes_t) (floor ((double) start / session->frames_per_smpte_frame()) *  session->frames_per_smpte_frame());
2247                 }
2248                 break;
2249
2250         case SnapToSMPTESeconds:
2251                 if (session->smpte_offset_negative())
2252                 {
2253                         start += session->smpte_offset ();
2254                 } else {
2255                         start -= session->smpte_offset ();
2256                 }    
2257                 if (start % one_smpte_second > one_smpte_second / 2) {
2258                         start = (nframes_t) ceil ((double) start / one_smpte_second) * one_smpte_second;
2259                 } else {
2260                         start = (nframes_t) floor ((double) start / one_smpte_second) * one_smpte_second;
2261                 }
2262                 
2263                 if (session->smpte_offset_negative())
2264                 {
2265                         start -= session->smpte_offset ();
2266                 } else {
2267                         start += session->smpte_offset ();
2268                 }
2269                 break;
2270                 
2271         case SnapToSMPTEMinutes:
2272                 if (session->smpte_offset_negative())
2273                 {
2274                         start += session->smpte_offset ();
2275                 } else {
2276                         start -= session->smpte_offset ();
2277                 }
2278                 if (start % one_smpte_minute > one_smpte_minute / 2) {
2279                         start = (nframes_t) ceil ((double) start / one_smpte_minute) * one_smpte_minute;
2280                 } else {
2281                         start = (nframes_t) floor ((double) start / one_smpte_minute) * one_smpte_minute;
2282                 }
2283                 if (session->smpte_offset_negative())
2284                 {
2285                         start -= session->smpte_offset ();
2286                 } else {
2287                         start += session->smpte_offset ();
2288                 }
2289                 break;
2290                 
2291         case SnapToSeconds:
2292                 if (start % one_second > one_second / 2) {
2293                         start = (nframes_t) ceil ((double) start / one_second) * one_second;
2294                 } else {
2295                         start = (nframes_t) floor ((double) start / one_second) * one_second;
2296                 }
2297                 break;
2298                 
2299         case SnapToMinutes:
2300                 if (start % one_minute > one_minute / 2) {
2301                         start = (nframes_t) ceil ((double) start / one_minute) * one_minute;
2302                 } else {
2303                         start = (nframes_t) floor ((double) start / one_minute) * one_minute;
2304                 }
2305                 break;
2306
2307         case SnapToBar:
2308                 start = session->tempo_map().round_to_bar (start, direction);
2309                 break;
2310
2311         case SnapToBeat:
2312                 start = session->tempo_map().round_to_beat (start, direction);
2313                 break;
2314
2315         case SnapToAThirtysecondBeat:
2316                 start = session->tempo_map().round_to_beat_subdivision (start, 32);
2317                 break;
2318
2319         case SnapToASixteenthBeat:
2320                 start = session->tempo_map().round_to_beat_subdivision (start, 16);
2321                 break;
2322
2323         case SnapToAEighthBeat:
2324                 start = session->tempo_map().round_to_beat_subdivision (start, 8);
2325                 break;
2326
2327         case SnapToAQuarterBeat:
2328                 start = session->tempo_map().round_to_beat_subdivision (start, 4);
2329                 break;
2330
2331         case SnapToAThirdBeat:
2332                 start = session->tempo_map().round_to_beat_subdivision (start, 3);
2333                 break;
2334
2335         case SnapToEditCursor:
2336                 start = edit_cursor->current_frame;
2337                 break;
2338
2339         case SnapToMark:
2340                 if (for_mark) {
2341                         return;
2342                 }
2343
2344                 before = session->locations()->first_location_before (start);
2345                 after = session->locations()->first_location_after (start);
2346
2347                 if (direction < 0) {
2348                         if (before) {
2349                                 start = before->start();
2350                         } else {
2351                                 start = 0;
2352                         }
2353                 } else if (direction > 0) {
2354                         if (after) {
2355                                 start = after->start();
2356                         } else {
2357                                 start = session->current_end_frame();
2358                         }
2359                 } else {
2360                         if (before) {
2361                                 if (after) {
2362                                         /* find nearest of the two */
2363                                         if ((start - before->start()) < (after->start() - start)) {
2364                                                 start = before->start();
2365                                         } else {
2366                                                 start = after->start();
2367                                         }
2368                                 } else {
2369                                         start = before->start();
2370                                 }
2371                         } else if (after) {
2372                                 start = after->start();
2373                         } else {
2374                                 /* relax */
2375                         }
2376                 }
2377                 break;
2378
2379         case SnapToRegionStart:
2380         case SnapToRegionEnd:
2381         case SnapToRegionSync:
2382         case SnapToRegionBoundary:
2383                 if (!region_boundary_cache.empty()) {
2384                         vector<nframes_t>::iterator i;
2385
2386                         if (direction > 0) {
2387                                 i = std::upper_bound (region_boundary_cache.begin(), region_boundary_cache.end(), start);
2388                         } else {
2389                                 i = std::lower_bound (region_boundary_cache.begin(), region_boundary_cache.end(), start);
2390                         }
2391                         
2392                         if (i != region_boundary_cache.end()) {
2393                                 start = *i;
2394                         } else {
2395                                 start = region_boundary_cache.back();
2396                         }
2397                 }
2398                 break;
2399         }
2400
2401         switch (snap_mode) {
2402         case SnapNormal:
2403                 return;                 
2404                 
2405         case SnapMagnetic:
2406                 
2407                 if (presnap > start) {
2408                         if (presnap > (start + unit_to_frame(snap_threshold))) {
2409                                 start = presnap;
2410                         }
2411                         
2412                 } else if (presnap < start) {
2413                         if (presnap < (start - unit_to_frame(snap_threshold))) {
2414                                 start = presnap;
2415                         }
2416                 }
2417                 
2418         default:
2419                 return;
2420                 
2421         }
2422 }
2423
2424 void
2425 Editor::setup_toolbar ()
2426 {
2427         string pixmap_path;
2428
2429         const guint32 FUDGE = 18; // Combo's are stupid - they steal space from the entry for the button
2430
2431
2432         /* Mode Buttons (tool selection) */
2433
2434         vector<ToggleButton *> mouse_mode_buttons;
2435
2436         mouse_move_button.add (*(manage (new Image (::get_icon("tool_object")))));
2437         mouse_move_button.set_relief(Gtk::RELIEF_NONE);
2438         mouse_mode_buttons.push_back (&mouse_move_button);
2439         mouse_select_button.add (*(manage (new Image (get_xpm("tool_range.xpm")))));
2440         mouse_select_button.set_relief(Gtk::RELIEF_NONE);
2441         mouse_mode_buttons.push_back (&mouse_select_button);
2442         mouse_gain_button.add (*(manage (new Image (::get_icon("tool_gain")))));
2443         mouse_gain_button.set_relief(Gtk::RELIEF_NONE);
2444         mouse_mode_buttons.push_back (&mouse_gain_button);
2445         mouse_zoom_button.add (*(manage (new Image (::get_icon("tool_zoom")))));
2446         mouse_zoom_button.set_relief(Gtk::RELIEF_NONE);
2447         mouse_mode_buttons.push_back (&mouse_zoom_button);
2448         mouse_timefx_button.add (*(manage (new Image (::get_icon("tool_stretch")))));
2449         mouse_timefx_button.set_relief(Gtk::RELIEF_NONE);
2450         mouse_mode_buttons.push_back (&mouse_timefx_button);
2451         mouse_audition_button.add (*(manage (new Image (::get_icon("tool_audition")))));
2452         mouse_audition_button.set_relief(Gtk::RELIEF_NONE);
2453         mouse_mode_buttons.push_back (&mouse_audition_button);
2454         
2455         mouse_mode_button_set = new GroupedButtons (mouse_mode_buttons);
2456
2457         HBox* mode_box = manage(new HBox);
2458         mode_box->set_border_width (2);
2459         mode_box->set_spacing(4);
2460         mouse_mode_button_box.set_spacing(1);
2461         mouse_mode_button_box.pack_start(mouse_move_button, true, true);
2462         mouse_mode_button_box.pack_start(mouse_select_button, true, true);
2463         mouse_mode_button_box.pack_start(mouse_zoom_button, true, true);
2464         mouse_mode_button_box.pack_start(mouse_gain_button, true, true);
2465         mouse_mode_button_box.pack_start(mouse_timefx_button, true, true);
2466         mouse_mode_button_box.pack_start(mouse_audition_button, true, true);
2467         mouse_mode_button_box.set_homogeneous(true);
2468
2469         vector<string> edit_mode_strings;
2470         edit_mode_strings.push_back (edit_mode_to_string (Splice));
2471         edit_mode_strings.push_back (edit_mode_to_string (Slide));
2472
2473         edit_mode_selector.set_name ("EditModeSelector");
2474         Gtkmm2ext::set_size_request_to_display_given_text (edit_mode_selector, longest (edit_mode_strings).c_str(), 2+FUDGE, 10);
2475         set_popdown_strings (edit_mode_selector, edit_mode_strings);
2476         edit_mode_selector.signal_changed().connect (mem_fun(*this, &Editor::edit_mode_selection_done));
2477
2478         mode_box->pack_start(edit_mode_selector);
2479         mode_box->pack_start(mouse_mode_button_box);
2480         
2481         mouse_mode_tearoff = manage (new TearOff (*mode_box));
2482         mouse_mode_tearoff->set_name ("MouseModeBase");
2483
2484         mouse_mode_tearoff->Detach.connect (bind (mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox), 
2485                                                   &mouse_mode_tearoff->tearoff_window()));
2486         mouse_mode_tearoff->Attach.connect (bind (mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox), 
2487                                                   &mouse_mode_tearoff->tearoff_window(), 1));
2488         mouse_mode_tearoff->Hidden.connect (bind (mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox), 
2489                                                   &mouse_mode_tearoff->tearoff_window()));
2490         mouse_mode_tearoff->Visible.connect (bind (mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox), 
2491                                                    &mouse_mode_tearoff->tearoff_window(), 1));
2492
2493         mouse_move_button.set_name ("MouseModeButton");
2494         mouse_select_button.set_name ("MouseModeButton");
2495         mouse_gain_button.set_name ("MouseModeButton");
2496         mouse_zoom_button.set_name ("MouseModeButton");
2497         mouse_timefx_button.set_name ("MouseModeButton");
2498         mouse_audition_button.set_name ("MouseModeButton");
2499
2500         ARDOUR_UI::instance()->tooltips().set_tip (mouse_move_button, _("Select/Move Objects"));
2501         ARDOUR_UI::instance()->tooltips().set_tip (mouse_select_button, _("Select/Move Ranges"));
2502         ARDOUR_UI::instance()->tooltips().set_tip (mouse_gain_button, _("Draw Gain Automation"));
2503         ARDOUR_UI::instance()->tooltips().set_tip (mouse_zoom_button, _("Select Zoom Range"));
2504         ARDOUR_UI::instance()->tooltips().set_tip (mouse_timefx_button, _("Stretch/Shrink Regions"));
2505         ARDOUR_UI::instance()->tooltips().set_tip (mouse_audition_button, _("Listen to Specific Regions"));
2506
2507         mouse_move_button.unset_flags (CAN_FOCUS);
2508         mouse_select_button.unset_flags (CAN_FOCUS);
2509         mouse_gain_button.unset_flags (CAN_FOCUS);
2510         mouse_zoom_button.unset_flags (CAN_FOCUS);
2511         mouse_timefx_button.unset_flags (CAN_FOCUS);
2512         mouse_audition_button.unset_flags (CAN_FOCUS);
2513
2514         mouse_select_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseRange));
2515         mouse_select_button.signal_button_release_event().connect (mem_fun(*this, &Editor::mouse_select_button_release));
2516
2517         mouse_move_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseObject));
2518         mouse_gain_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseGain));
2519         mouse_zoom_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseZoom));
2520         mouse_timefx_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseTimeFX));
2521         mouse_audition_button.signal_toggled().connect (bind (mem_fun(*this, &Editor::mouse_mode_toggled), Editing::MouseAudition));
2522
2523         // mouse_move_button.set_active (true);
2524         
2525
2526         /* Zoom */
2527         
2528         zoom_box.set_spacing (1);
2529         zoom_box.set_border_width (2);
2530
2531         zoom_in_button.set_name ("EditorTimeButton");
2532         zoom_in_button.set_size_request(-1,16);
2533         zoom_in_button.add (*(manage (new Image (::get_icon("zoom_in")))));
2534         zoom_in_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::temporal_zoom_step), false));
2535         ARDOUR_UI::instance()->tooltips().set_tip (zoom_in_button, _("Zoom In"));
2536         
2537         zoom_out_button.set_name ("EditorTimeButton");
2538         zoom_out_button.set_size_request(-1,16);
2539         zoom_out_button.add (*(manage (new Image (::get_icon("zoom_out")))));
2540         zoom_out_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::temporal_zoom_step), true));
2541         ARDOUR_UI::instance()->tooltips().set_tip (zoom_out_button, _("Zoom Out"));
2542
2543         zoom_out_full_button.set_name ("EditorTimeButton");
2544         zoom_out_full_button.set_size_request(-1,16);
2545         zoom_out_full_button.add (*(manage (new Image (::get_icon("zoom_full")))));
2546         zoom_out_full_button.signal_clicked().connect (mem_fun(*this, &Editor::temporal_zoom_session));
2547         ARDOUR_UI::instance()->tooltips().set_tip (zoom_out_full_button, _("Zoom to Session"));
2548
2549         zoom_focus_selector.set_name ("ZoomFocusSelector");
2550         Gtkmm2ext::set_size_request_to_display_given_text (zoom_focus_selector, "Edit Cursor", FUDGE, 0);
2551         set_popdown_strings (zoom_focus_selector, zoom_focus_strings);
2552         zoom_focus_selector.signal_changed().connect (mem_fun(*this, &Editor::zoom_focus_selection_done));
2553         ARDOUR_UI::instance()->tooltips().set_tip (zoom_focus_selector, _("Zoom focus"));
2554
2555         zoom_box.pack_start (zoom_focus_selector, true, true);
2556         zoom_box.pack_start (zoom_out_button, false, false);
2557         zoom_box.pack_start (zoom_in_button, false, false);
2558         zoom_box.pack_start (zoom_out_full_button, false, false);
2559
2560         /* Edit Cursor / Snap */
2561
2562         snap_box.set_spacing (1);
2563         snap_box.set_border_width (2);
2564
2565         snap_type_selector.set_name ("SnapTypeSelector");
2566         Gtkmm2ext::set_size_request_to_display_given_text (snap_type_selector, "SMPTE Seconds", 2+FUDGE, 10);
2567         set_popdown_strings (snap_type_selector, snap_type_strings);
2568         snap_type_selector.signal_changed().connect (mem_fun(*this, &Editor::snap_type_selection_done));
2569         ARDOUR_UI::instance()->tooltips().set_tip (snap_type_selector, _("Unit to snap cursors and ranges to"));
2570
2571         snap_mode_selector.set_name ("SnapModeSelector");
2572         Gtkmm2ext::set_size_request_to_display_given_text (snap_mode_selector, "Magnetic Snap", 2+FUDGE, 10);
2573         set_popdown_strings (snap_mode_selector, snap_mode_strings);
2574         snap_mode_selector.signal_changed().connect (mem_fun(*this, &Editor::snap_mode_selection_done));
2575
2576         snap_box.pack_start (edit_cursor_clock, false, false);
2577         snap_box.pack_start (snap_mode_selector, false, false);
2578         snap_box.pack_start (snap_type_selector, false, false);
2579
2580
2581         /* Nudge */
2582
2583         HBox *nudge_box = manage (new HBox);
2584         nudge_box->set_spacing(1);
2585         nudge_box->set_border_width (2);
2586
2587         nudge_forward_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::nudge_forward), false));
2588         nudge_backward_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::nudge_backward), false));
2589
2590         nudge_box->pack_start (nudge_backward_button, false, false);
2591         nudge_box->pack_start (nudge_forward_button, false, false);
2592         nudge_box->pack_start (nudge_clock, false, false);
2593
2594
2595         /* Pack everything in... */
2596
2597         HBox* hbox = new HBox;
2598         hbox->set_spacing(10);
2599
2600         tools_tearoff = new TearOff (*hbox);
2601         tools_tearoff->set_name ("MouseModeBase");
2602
2603         tools_tearoff->Detach.connect (bind (mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox), 
2604                                              &tools_tearoff->tearoff_window()));
2605         tools_tearoff->Attach.connect (bind (mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox), 
2606                                              &tools_tearoff->tearoff_window(), 0));
2607         tools_tearoff->Hidden.connect (bind (mem_fun(*this, &Editor::detach_tearoff), static_cast<Box*>(&toolbar_hbox), 
2608                                              &tools_tearoff->tearoff_window()));
2609         tools_tearoff->Visible.connect (bind (mem_fun(*this, &Editor::reattach_tearoff), static_cast<Box*> (&toolbar_hbox), 
2610                                               &tools_tearoff->tearoff_window(), 0));
2611
2612         toolbar_hbox.set_spacing (10);
2613         toolbar_hbox.set_border_width (1);
2614
2615         toolbar_hbox.pack_start (*mouse_mode_tearoff, false, false);
2616         toolbar_hbox.pack_start (*tools_tearoff, false, false);
2617
2618         
2619         hbox->pack_start (snap_box, false, false);
2620         // hbox->pack_start (zoom_box, false, false); 
2621         hbox->pack_start (*nudge_box, false, false);
2622
2623         hbox->show_all ();
2624         
2625         toolbar_base.set_name ("ToolBarBase");
2626         toolbar_base.add (toolbar_hbox);
2627
2628         toolbar_frame.set_shadow_type (SHADOW_OUT);
2629         toolbar_frame.set_name ("BaseFrame");
2630         toolbar_frame.add (toolbar_base);
2631 }
2632
2633 int
2634 Editor::convert_drop_to_paths (vector<ustring>& paths, 
2635                                const RefPtr<Gdk::DragContext>& context,
2636                                gint                x,
2637                                gint                y,
2638                                const SelectionData& data,
2639                                guint               info,
2640                                guint               time)                               
2641
2642 {       
2643         if (session == 0) {
2644                 return -1;
2645         }
2646
2647         vector<ustring> uris = data.get_uris();
2648
2649         if (uris.empty()) {
2650                 
2651                 /* This is seriously fucked up. Nautilus doesn't say that its URI lists
2652                    are actually URI lists. So do it by hand.
2653                 */
2654
2655                 if (data.get_target() != "text/plain") {
2656                         return -1;
2657                 }
2658   
2659                 /* Parse the "uri-list" format that Nautilus provides, 
2660                    where each pathname is delimited by \r\n
2661                 */
2662         
2663                 const char* p = data.get_text().c_str();
2664                 const char* q;
2665
2666                 while (p)
2667                 {
2668                         if (*p != '#')
2669                         {
2670                                 while (g_ascii_isspace (*p))
2671                                         p++;
2672                                 
2673                                 q = p;
2674                                 while (*q && (*q != '\n') && (*q != '\r'))
2675                                         q++;
2676                                 
2677                                 if (q > p)
2678                                 {
2679                                         q--;
2680                                         while (q > p && g_ascii_isspace (*q))
2681                                                 q--;
2682                                         
2683                                         if (q > p)
2684                                         {
2685                                                 uris.push_back (ustring (p, q - p + 1));
2686                                         }
2687                                 }
2688                         }
2689                         p = strchr (p, '\n');
2690                         if (p)
2691                                 p++;
2692                 }
2693
2694                 if (uris.empty()) {
2695                         return -1;
2696                 }
2697         }
2698         
2699         for (vector<ustring>::iterator i = uris.begin(); i != uris.end(); ++i) {
2700                 if ((*i).substr (0,7) == "file://") {
2701                         string p = *i;
2702                         PBD::url_decode (p);
2703                         paths.push_back (p.substr (7));
2704                 }
2705         }
2706
2707         return 0;
2708 }
2709
2710 void
2711 Editor::new_tempo_section ()
2712
2713 {
2714 }
2715
2716 void
2717 Editor::map_transport_state ()
2718 {
2719         ENSURE_GUI_THREAD (mem_fun(*this, &Editor::map_transport_state));
2720
2721         if (session->transport_stopped()) {
2722                 have_pending_keyboard_selection = false;
2723         }
2724
2725         update_loop_range_view (true);
2726 }
2727
2728 /* UNDO/REDO */
2729
2730 Editor::State::State ()
2731 {
2732         selection = new Selection;
2733 }
2734
2735 Editor::State::~State ()
2736 {
2737         delete selection;
2738 }
2739
2740 UndoAction
2741 Editor::get_memento () const
2742 {
2743         State *state = new State;
2744
2745         store_state (*state);
2746         return bind (mem_fun (*(const_cast<Editor*>(this)), &Editor::restore_state), state);
2747 }
2748
2749 void
2750 Editor::store_state (State& state) const
2751 {
2752         *state.selection = *selection;
2753 }
2754
2755 void
2756 Editor::restore_state (State *state)
2757 {
2758         if (*selection == *state->selection) {
2759                 return;
2760         }
2761
2762         *selection = *state->selection;
2763         time_selection_changed ();
2764         region_selection_changed ();   
2765
2766         /* XXX other selection change handlers? */
2767 }
2768
2769 void
2770 Editor::begin_reversible_command (string name)
2771 {
2772         if (session) {
2773                 before = &get_state();
2774                 session->begin_reversible_command (name);
2775         }
2776 }
2777
2778 void
2779 Editor::commit_reversible_command ()
2780 {
2781         if (session) {
2782                 session->commit_reversible_command (new MementoCommand<Editor>(*this, before, &get_state()));
2783         }
2784 }
2785
2786 void
2787 Editor::set_edit_group_solo (Route& route, bool yn)
2788 {
2789         RouteGroup *edit_group;
2790
2791         if ((edit_group = route.edit_group()) != 0) {
2792                 edit_group->apply (&Route::set_solo, yn, this);
2793         } else {
2794                 route.set_solo (yn, this);
2795         }
2796 }
2797
2798 void
2799 Editor::set_edit_group_mute (Route& route, bool yn)
2800 {
2801         RouteGroup *edit_group = 0;
2802
2803         if ((edit_group == route.edit_group()) != 0) {
2804                 edit_group->apply (&Route::set_mute, yn, this);
2805         } else {
2806                 route.set_mute (yn, this);
2807         }
2808 }
2809                 
2810 void
2811 Editor::history_changed ()
2812 {
2813         string label;
2814
2815         if (undo_action && session) {
2816                 if (session->undo_depth() == 0) {
2817                         label = _("Undo");
2818                 } else {
2819                         label = string_compose(_("Undo (%1)"), session->next_undo());
2820                 }
2821                 undo_action->property_label() = label;
2822         }
2823
2824         if (redo_action && session) {
2825                 if (session->redo_depth() == 0) {
2826                         label = _("Redo");
2827                 } else {
2828                         label = string_compose(_("Redo (%1)"), session->next_redo());
2829                 }
2830                 redo_action->property_label() = label;
2831         }
2832 }
2833
2834 void
2835 Editor::duplicate_dialog (bool dup_region)
2836 {
2837         if (selection->regions.empty() && (selection->time.length() == 0)) {
2838                 return;
2839         }
2840
2841         ArdourDialog win ("duplicate dialog");
2842         Label  label (_("Duplicate how many times?"));
2843         Adjustment adjustment (1.0, 1.0, 1000000.0, 1.0, 5.0);
2844         SpinButton spinner (adjustment);
2845
2846         win.get_vbox()->set_spacing (12);
2847         win.get_vbox()->pack_start (label);
2848
2849         /* dialogs have ::add_action_widget() but that puts the spinner in the wrong
2850            place, visually. so do this by hand.
2851         */
2852
2853         win.get_vbox()->pack_start (spinner);
2854         spinner.signal_activate().connect (sigc::bind (mem_fun (win, &ArdourDialog::response), RESPONSE_ACCEPT));
2855
2856         label.show ();
2857         spinner.show ();
2858
2859         win.add_button (Stock::OK, RESPONSE_ACCEPT);
2860         win.add_button (Stock::CANCEL, RESPONSE_CANCEL);
2861
2862         win.set_position (WIN_POS_MOUSE);
2863
2864         spinner.grab_focus ();
2865
2866         switch (win.run ()) {
2867         case RESPONSE_ACCEPT:
2868                 break;
2869         default:
2870                 return;
2871         }
2872
2873         float times = adjustment.get_value();
2874
2875         if (!selection->regions.empty()) {
2876                 duplicate_some_regions (selection->regions, times);
2877         } else {
2878                 duplicate_selection (times);
2879         }
2880 }
2881
2882 void
2883 Editor::show_verbose_canvas_cursor ()
2884 {
2885         verbose_canvas_cursor->raise_to_top();
2886         verbose_canvas_cursor->show();
2887         verbose_cursor_visible = true;
2888 }
2889
2890 void
2891 Editor::hide_verbose_canvas_cursor ()
2892 {
2893         verbose_canvas_cursor->hide();
2894         verbose_cursor_visible = false;
2895 }
2896
2897 void
2898 Editor::set_verbose_canvas_cursor (const string & txt, double x, double y)
2899 {
2900         /* XXX get origin of canvas relative to root window,
2901            add x and y and check compared to gdk_screen_{width,height}
2902         */
2903         verbose_canvas_cursor->property_text() = txt.c_str();
2904         verbose_canvas_cursor->property_x() = x;
2905         verbose_canvas_cursor->property_y() = y;
2906 }
2907
2908 void
2909 Editor::set_verbose_canvas_cursor_text (const string & txt)
2910 {
2911         verbose_canvas_cursor->property_text() = txt.c_str();
2912 }
2913
2914 void
2915 Editor::edit_mode_selection_done ()
2916 {
2917         if (session == 0) {
2918                 return;
2919         }
2920
2921         string choice = edit_mode_selector.get_active_text();
2922         EditMode mode = Slide;
2923
2924         if (choice == _("Splice Edit")) {
2925                 mode = Splice;
2926         } else if (choice == _("Slide Edit")) {
2927                 mode = Slide;
2928         }
2929
2930         Config->set_edit_mode (mode);
2931 }       
2932
2933 void
2934 Editor::snap_type_selection_done ()
2935 {
2936         string choice = snap_type_selector.get_active_text();
2937         SnapType snaptype = SnapToFrame;
2938
2939         if (choice == _("Beats/3")) {
2940                 snaptype = SnapToAThirdBeat;
2941         } else if (choice == _("Beats/4")) {
2942                 snaptype = SnapToAQuarterBeat;
2943         } else if (choice == _("Beats/8")) {
2944                 snaptype = SnapToAEighthBeat;
2945         } else if (choice == _("Beats/16")) {
2946                 snaptype = SnapToASixteenthBeat;
2947         } else if (choice == _("Beats/32")) {
2948                 snaptype = SnapToAThirtysecondBeat;
2949         } else if (choice == _("Beats")) {
2950                 snaptype = SnapToBeat;
2951         } else if (choice == _("Bars")) {
2952                 snaptype = SnapToBar;
2953         } else if (choice == _("Marks")) {
2954                 snaptype = SnapToMark;
2955         } else if (choice == _("Edit Cursor")) {
2956                 snaptype = SnapToEditCursor;
2957         } else if (choice == _("Region starts")) {
2958                 snaptype = SnapToRegionStart;
2959         } else if (choice == _("Region ends")) {
2960                 snaptype = SnapToRegionEnd;
2961         } else if (choice == _("Region bounds")) {
2962                 snaptype = SnapToRegionBoundary;
2963         } else if (choice == _("Region syncs")) {
2964                 snaptype = SnapToRegionSync;
2965         } else if (choice == _("CD Frames")) {
2966                 snaptype = SnapToCDFrame;
2967         } else if (choice == _("SMPTE Frames")) {
2968                 snaptype = SnapToSMPTEFrame;
2969         } else if (choice == _("SMPTE Seconds")) {
2970                 snaptype = SnapToSMPTESeconds;
2971         } else if (choice == _("SMPTE Minutes")) {
2972                 snaptype = SnapToSMPTEMinutes;
2973         } else if (choice == _("Seconds")) {
2974                 snaptype = SnapToSeconds;
2975         } else if (choice == _("Minutes")) {
2976                 snaptype = SnapToMinutes;
2977         } else if (choice == _("None")) {
2978                 snaptype = SnapToFrame;
2979         }
2980
2981         RefPtr<RadioAction> ract = snap_type_action (snaptype);
2982         if (ract) {
2983                 ract->set_active ();
2984         }
2985 }       
2986
2987 void
2988 Editor::snap_mode_selection_done ()
2989 {
2990         string choice = snap_mode_selector.get_active_text();
2991         SnapMode mode = SnapNormal;
2992
2993         if (choice == _("Normal")) {
2994                 mode = SnapNormal;
2995         } else if (choice == _("Magnetic")) {
2996                 mode = SnapMagnetic;
2997         }
2998
2999         RefPtr<RadioAction> ract = snap_mode_action (mode);
3000
3001         if (ract) {
3002                 ract->set_active (true);
3003         }
3004 }
3005
3006 void
3007 Editor::zoom_focus_selection_done ()
3008 {
3009         string choice = zoom_focus_selector.get_active_text();
3010         ZoomFocus focus_type = ZoomFocusLeft;
3011
3012         if (choice == _("Left")) {
3013                 focus_type = ZoomFocusLeft;
3014         } else if (choice == _("Right")) {
3015                 focus_type = ZoomFocusRight;
3016         } else if (choice == _("Center")) {
3017                 focus_type = ZoomFocusCenter;
3018         } else if (choice == _("Playhead")) {
3019                 focus_type = ZoomFocusPlayhead;
3020         } else if (choice == _("Edit Cursor")) {
3021                 focus_type = ZoomFocusEdit;
3022         } 
3023         
3024         RefPtr<RadioAction> ract = zoom_focus_action (focus_type);
3025
3026         if (ract) {
3027                 ract->set_active ();
3028         }
3029 }       
3030
3031 gint
3032 Editor::edit_controls_button_release (GdkEventButton* ev)
3033 {
3034         if (Keyboard::is_context_menu_event (ev)) {
3035                 ARDOUR_UI::instance()->add_route (this);
3036         }
3037         return TRUE;
3038 }
3039
3040 gint
3041 Editor::mouse_select_button_release (GdkEventButton* ev)
3042 {
3043         /* this handles just right-clicks */
3044
3045         if (ev->button != 3) {
3046                 return false;
3047         }
3048
3049         return true;
3050 }
3051
3052 Editor::TrackViewList *
3053 Editor::get_valid_views (TimeAxisView* track, RouteGroup* group)
3054 {
3055         TrackViewList *v;
3056         TrackViewList::iterator i;
3057
3058         v = new TrackViewList;
3059
3060         if (track == 0 && group == 0) {
3061
3062                 /* all views */
3063
3064                 for (i = track_views.begin(); i != track_views.end (); ++i) {
3065                         v->push_back (*i);
3066                 }
3067
3068         } else if (track != 0 && group == 0 || (track != 0 && group != 0 && !group->is_active())) {
3069                 
3070                 /* just the view for this track
3071                  */
3072
3073                 v->push_back (track);
3074
3075         } else {
3076                 
3077                 /* views for all tracks in the edit group */
3078                 
3079                 for (i  = track_views.begin(); i != track_views.end (); ++i) {
3080
3081                         if (group == 0 || (*i)->edit_group() == group) {
3082                                 v->push_back (*i);
3083                         }
3084                 }
3085         }
3086         
3087         return v;
3088 }
3089
3090 void
3091 Editor::set_zoom_focus (ZoomFocus f)
3092 {
3093         string str = zoom_focus_strings[(int)f];
3094
3095         if (str != zoom_focus_selector.get_active_text()) {
3096                 zoom_focus_selector.set_active_text (str);
3097         }
3098         
3099         if (zoom_focus != f) {
3100                 zoom_focus = f;
3101
3102                 ZoomFocusChanged (); /* EMIT_SIGNAL */
3103
3104                 instant_save ();
3105         }
3106 }
3107
3108 void
3109 Editor::ensure_float (Window& win)
3110 {
3111         win.set_transient_for (*this);
3112 }
3113
3114 void 
3115 Editor::pane_allocation_handler (Allocation &alloc, Paned* which)
3116 {
3117         /* recover or initialize pane positions. do this here rather than earlier because
3118            we don't want the positions to change the child allocations, which they seem to do.
3119          */
3120
3121         int pos;
3122         XMLProperty* prop;
3123         char buf[32];
3124         XMLNode* node = ARDOUR_UI::instance()->editor_settings();
3125         int width, height;
3126         static int32_t done;
3127         XMLNode* geometry;
3128
3129         if ((geometry = find_named_node (*node, "geometry")) == 0) {
3130                 width = default_width;
3131                 height = default_height;
3132         } else {
3133                 width = atoi(geometry->property("x_size")->value());
3134                 height = atoi(geometry->property("y_size")->value());
3135         }
3136
3137         if (which == static_cast<Paned*> (&edit_pane)) {
3138
3139                 if (done) {
3140                         return;
3141                 }
3142
3143                 if (!geometry || (prop = geometry->property ("edit_pane_pos")) == 0) {
3144                         /* initial allocation is 90% to canvas, 10% to notebook */
3145                         pos = (int) floor (alloc.get_width() * 0.90f);
3146                         snprintf (buf, sizeof(buf), "%d", pos);
3147                 } else {
3148                         pos = atoi (prop->value());
3149                 }
3150                 
3151                 if ((done = GTK_WIDGET(edit_pane.gobj())->allocation.width > pos)) {
3152                         edit_pane.set_position (pos);
3153                         pre_maximal_pane_position = pos;
3154                 }
3155         }
3156 }
3157
3158 void
3159 Editor::detach_tearoff (Box* b, Window* w)
3160 {
3161         if (tools_tearoff->torn_off() && 
3162             mouse_mode_tearoff->torn_off()) {
3163                 top_hbox.remove (toolbar_frame);
3164         }
3165 }
3166
3167 void
3168 Editor::reattach_tearoff (Box* b, Window* w, int32_t n)
3169 {
3170         if (toolbar_frame.get_parent() == 0) {
3171                 top_hbox.pack_end (toolbar_frame);
3172         }
3173 }
3174
3175 void
3176 Editor::set_show_measures (bool yn)
3177 {
3178         if (_show_measures != yn) {
3179                 hide_measures ();
3180
3181                 if ((_show_measures = yn) == true) {
3182                         draw_measures ();
3183                 }
3184                 instant_save ();
3185         }
3186 }
3187
3188 void
3189 Editor::toggle_follow_playhead ()
3190 {
3191         RefPtr<Action> act = ActionManager::get_action (X_("Editor"), X_("toggle-follow-playhead"));
3192         if (act) {
3193                 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
3194                 set_follow_playhead (tact->get_active());
3195         }
3196 }
3197
3198 void
3199 Editor::set_follow_playhead (bool yn)
3200 {
3201         if (_follow_playhead != yn) {
3202                 if ((_follow_playhead = yn) == true) {
3203                         /* catch up */
3204                         update_current_screen ();
3205                 }
3206                 instant_save ();
3207         }
3208 }
3209
3210 void
3211 Editor::toggle_xfade_active (boost::weak_ptr<Crossfade> wxfade)
3212 {
3213         boost::shared_ptr<Crossfade> xfade (wxfade.lock());
3214         if (xfade) {
3215                 xfade->set_active (!xfade->active());
3216         }
3217 }
3218
3219 void
3220 Editor::toggle_xfade_length (boost::weak_ptr<Crossfade> wxfade)
3221 {
3222         boost::shared_ptr<Crossfade> xfade (wxfade.lock());
3223         if (xfade) {
3224                 xfade->set_follow_overlap (!xfade->following_overlap());
3225         }
3226 }
3227
3228 void
3229 Editor::edit_xfade (boost::weak_ptr<Crossfade> wxfade)
3230 {
3231         boost::shared_ptr<Crossfade> xfade (wxfade.lock());
3232
3233         if (!xfade) {
3234                 return;
3235         }
3236
3237         CrossfadeEditor cew (*session, xfade, xfade->fade_in().get_min_y(), 1.0);
3238                 
3239         ensure_float (cew);
3240         
3241         switch (cew.run ()) {
3242         case RESPONSE_ACCEPT:
3243                 break;
3244         default:
3245                 return;
3246         }
3247         
3248         cew.apply ();
3249         xfade->StateChanged (Change (~0));
3250 }
3251
3252 PlaylistSelector&
3253 Editor::playlist_selector () const
3254 {
3255         return *_playlist_selector;
3256 }
3257
3258 nframes_t
3259 Editor::get_nudge_distance (nframes_t pos, nframes_t& next)
3260 {
3261         nframes_t ret;
3262
3263         ret = nudge_clock.current_duration (pos);
3264         next = ret + 1; /* XXXX fix me */
3265
3266         return ret;
3267 }
3268
3269 void
3270 Editor::end_location_changed (Location* location)
3271 {
3272         ENSURE_GUI_THREAD (bind (mem_fun(*this, &Editor::end_location_changed), location));
3273         reset_scrolling_region ();
3274 }
3275
3276 int
3277 Editor::playlist_deletion_dialog (boost::shared_ptr<Playlist> pl)
3278 {
3279         ArdourDialog dialog ("playlist deletion dialog");
3280         Label  label (string_compose (_("Playlist %1 is currently unused.\n"
3281                                         "If left alone, no audio files used by it will be cleaned.\n"
3282                                         "If deleted, audio files used by it alone by will cleaned."),
3283                                       pl->name()));
3284         
3285         dialog.set_position (WIN_POS_CENTER);
3286         dialog.get_vbox()->pack_start (label);
3287
3288         label.show ();
3289
3290         dialog.add_button (_("Delete playlist"), RESPONSE_ACCEPT);
3291         dialog.add_button (_("Keep playlist"), RESPONSE_REJECT);
3292         dialog.add_button (_("Cancel"), RESPONSE_CANCEL);
3293
3294         switch (dialog.run ()) {
3295         case RESPONSE_ACCEPT:
3296                 /* delete the playlist */
3297                 return 0;
3298                 break;
3299
3300         case RESPONSE_REJECT:
3301                 /* keep the playlist */
3302                 return 1;
3303                 break;
3304
3305         default:
3306                 break;
3307         }
3308
3309         return -1;
3310 }
3311
3312 bool
3313 Editor::audio_region_selection_covers (nframes_t where)
3314 {
3315         for (RegionSelection::iterator a = selection->regions.begin(); a != selection->regions.end(); ++a) {
3316                 if ((*a)->region()->covers (where)) {
3317                         return true;
3318                 }
3319         }
3320
3321         return false;
3322 }
3323
3324 void
3325 Editor::prepare_for_cleanup ()
3326 {
3327         cut_buffer->clear_regions ();
3328         cut_buffer->clear_playlists ();
3329
3330         selection->clear_regions ();
3331         selection->clear_playlists ();
3332 }
3333
3334 Location*
3335 Editor::transport_loop_location()
3336 {
3337         if (session) {
3338                 return session->locations()->auto_loop_location();
3339         } else {
3340                 return 0;
3341         }
3342 }
3343
3344 Location*
3345 Editor::transport_punch_location()
3346 {
3347         if (session) {
3348                 return session->locations()->auto_punch_location();
3349         } else {
3350                 return 0;
3351         }
3352 }
3353
3354 bool
3355 Editor::control_layout_scroll (GdkEventScroll* ev)
3356 {
3357         switch (ev->direction) {
3358         case GDK_SCROLL_UP:
3359                 scroll_tracks_up_line ();
3360                 return true;
3361                 break;
3362
3363         case GDK_SCROLL_DOWN:
3364                 scroll_tracks_down_line ();
3365                 return true;
3366                 
3367         default:
3368                 /* no left/right handling yet */
3369                 break;
3370         }
3371
3372         return false;
3373 }
3374
3375 void
3376 Editor::snapshot_display_selection_changed ()
3377 {
3378         if (snapshot_display.get_selection()->count_selected_rows() > 0) {
3379
3380                 TreeModel::iterator i = snapshot_display.get_selection()->get_selected();
3381                 
3382                 Glib::ustring snap_name = (*i)[snapshot_display_columns.real_name];
3383
3384                 if (snap_name.length() == 0) {
3385                         return;
3386                 }
3387                 
3388                 if (session->snap_name() == snap_name) {
3389                         return;
3390                 }
3391                 
3392                 ARDOUR_UI::instance()->load_session(session->path(), string (snap_name));
3393         }
3394 }
3395
3396 bool
3397 Editor::snapshot_display_button_press (GdkEventButton* ev)
3398 {
3399          return false;
3400 }
3401
3402 void
3403 Editor::redisplay_snapshots ()
3404 {
3405         if (session == 0) {
3406                 return;
3407         }
3408
3409         vector<string*>* states;
3410
3411         if ((states = session->possible_states()) == 0) {
3412                 return;
3413         }
3414
3415         snapshot_display_model->clear ();
3416
3417         for (vector<string*>::iterator i = states->begin(); i != states->end(); ++i) {
3418                 string statename = *(*i);
3419                 TreeModel::Row row = *(snapshot_display_model->append());
3420                 
3421                 /* this lingers on in case we ever want to change the visible
3422                    name of the snapshot.
3423                 */
3424                 
3425                 string display_name;
3426                 display_name = statename;
3427
3428                 if (statename == session->snap_name()) {
3429                         snapshot_display.get_selection()->select(row);
3430                 } 
3431                 
3432                 row[snapshot_display_columns.visible_name] = display_name;
3433                 row[snapshot_display_columns.real_name] = statename;
3434         }
3435
3436         delete states;
3437 }
3438
3439 void
3440 Editor::session_state_saved (string snap_name)
3441 {
3442         ENSURE_GUI_THREAD (bind (mem_fun(*this, &Editor::session_state_saved), snap_name));
3443         redisplay_snapshots ();
3444 }
3445
3446 void
3447 Editor::maximise_editing_space ()
3448 {
3449         initial_ruler_update_required = true;
3450
3451         mouse_mode_tearoff->set_visible (false);
3452         tools_tearoff->set_visible (false);
3453
3454         pre_maximal_pane_position = edit_pane.get_position();
3455         pre_maximal_editor_width = this->get_width();
3456
3457         if(post_maximal_pane_position == 0) {
3458                 post_maximal_pane_position = edit_pane.get_width();
3459         }
3460
3461
3462         fullscreen();
3463         if(post_maximal_editor_width) {
3464                 edit_pane.set_position (post_maximal_pane_position - 
3465                         abs(post_maximal_editor_width - pre_maximal_editor_width));
3466         } else {
3467                 edit_pane.set_position (post_maximal_pane_position);
3468         }
3469 }
3470
3471 void
3472 Editor::restore_editing_space ()
3473 {
3474         initial_ruler_update_required = true;
3475
3476         // user changed width of pane during fullscreen
3477         if(post_maximal_pane_position != edit_pane.get_position()) {
3478                 post_maximal_pane_position = edit_pane.get_position();
3479         }
3480
3481         unfullscreen();
3482
3483         mouse_mode_tearoff->set_visible (true);
3484         tools_tearoff->set_visible (true);
3485         post_maximal_editor_width = this->get_width();
3486
3487
3488         edit_pane.set_position (
3489                 pre_maximal_pane_position + abs(this->get_width() - pre_maximal_editor_width)
3490         );
3491 }
3492
3493 void 
3494 Editor::new_playlists ()
3495 {
3496         begin_reversible_command (_("new playlists"));
3497         mapover_audio_tracks (mem_fun (*this, &Editor::mapped_use_new_playlist));
3498         commit_reversible_command ();
3499 }
3500
3501 void
3502 Editor::copy_playlists ()
3503 {
3504         begin_reversible_command (_("copy playlists"));
3505         mapover_audio_tracks (mem_fun (*this, &Editor::mapped_use_copy_playlist));
3506         commit_reversible_command ();
3507 }
3508
3509 void 
3510 Editor::clear_playlists ()
3511 {
3512         begin_reversible_command (_("clear playlists"));
3513         mapover_audio_tracks (mem_fun (*this, &Editor::mapped_clear_playlist));
3514         commit_reversible_command ();
3515 }
3516
3517 void 
3518 Editor::mapped_use_new_playlist (AudioTimeAxisView& atv, uint32_t sz)
3519 {
3520         atv.use_new_playlist (sz > 1 ? false : true);
3521 }
3522
3523 void
3524 Editor::mapped_use_copy_playlist (AudioTimeAxisView& atv, uint32_t sz)
3525 {
3526         atv.use_copy_playlist (sz > 1 ? false : true);
3527 }
3528
3529 void 
3530 Editor::mapped_clear_playlist (AudioTimeAxisView& atv, uint32_t sz)
3531 {
3532         atv.clear_playlist ();
3533 }
3534
3535 bool
3536 Editor::on_key_press_event (GdkEventKey* ev)
3537 {
3538         return key_press_focus_accelerator_handler (*this, ev);
3539 }
3540
3541 void
3542 Editor::reset_x_origin (nframes_t frame)
3543 {
3544         queue_visual_change (frame);
3545 }
3546
3547 void
3548 Editor::reset_zoom (double fpu)
3549 {
3550         queue_visual_change (fpu);
3551 }
3552
3553 void
3554 Editor::reposition_and_zoom (nframes_t frame, double fpu)
3555 {
3556         reset_x_origin (frame);
3557         reset_zoom (fpu);
3558 }
3559
3560 void
3561 Editor::set_frames_per_unit (double fpu)
3562 {
3563         nframes_t frames;
3564
3565         /* this is the core function that controls the zoom level of the canvas. it is called
3566            whenever one or more calls are made to reset_zoom(). it executes in an idle handler.
3567         */
3568
3569         if (fpu == frames_per_unit) {
3570                 return;
3571         }
3572
3573         if (fpu < 2.0) {
3574                 fpu = 2.0;
3575         }
3576
3577         // convert fpu to frame count
3578
3579         frames = (nframes_t) floor (fpu * canvas_width);
3580         
3581         /* don't allow zooms that fit more than the maximum number
3582            of frames into an 800 pixel wide space.
3583         */
3584
3585         if (max_frames / fpu < 800.0) {
3586                 return;
3587         }
3588
3589         if (fpu == frames_per_unit) {
3590                 return;
3591         }
3592
3593         frames_per_unit = fpu;
3594
3595         if (frames != zoom_range_clock.current_duration()) {
3596                 zoom_range_clock.set (frames);
3597         }
3598
3599         if (mouse_mode == MouseRange && selection->time.start () != selection->time.end_frame ()) {
3600                 if (!selection->tracks.empty()) {
3601                         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
3602                                 (*i)->reshow_selection (selection->time);
3603                         }
3604                 } else {
3605                         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
3606                                 (*i)->reshow_selection (selection->time);
3607                         }
3608                 }
3609         }
3610
3611         ZoomChanged (); /* EMIT_SIGNAL */
3612
3613         reset_hscrollbar_stepping ();
3614         reset_scrolling_region ();
3615         
3616         if (edit_cursor) edit_cursor->set_position (edit_cursor->current_frame);
3617         if (playhead_cursor) playhead_cursor->set_position (playhead_cursor->current_frame);
3618
3619         instant_save ();
3620 }
3621
3622 void
3623 Editor::queue_visual_change (nframes_t where)
3624 {
3625         pending_visual_change.pending = VisualChange::Type (pending_visual_change.pending | VisualChange::TimeOrigin);
3626         pending_visual_change.time_origin = where;
3627
3628         if (pending_visual_change.idle_handler_id < 0) {
3629                 pending_visual_change.idle_handler_id = g_idle_add (_idle_visual_changer, this);
3630         }
3631 }
3632
3633 void
3634 Editor::queue_visual_change (double fpu)
3635 {
3636         pending_visual_change.pending = VisualChange::Type (pending_visual_change.pending | VisualChange::ZoomLevel);
3637         pending_visual_change.frames_per_unit = fpu;
3638
3639         if (pending_visual_change.idle_handler_id < 0) {
3640                 pending_visual_change.idle_handler_id = g_idle_add_full (G_PRIORITY_HIGH_IDLE, _idle_visual_changer, this, 0);
3641         }
3642 }
3643
3644 int
3645 Editor::_idle_visual_changer (void* arg)
3646 {
3647         return static_cast<Editor*>(arg)->idle_visual_changer ();
3648 }
3649
3650 int
3651 Editor::idle_visual_changer ()
3652 {
3653         VisualChange::Type p = pending_visual_change.pending;
3654
3655         pending_visual_change.pending = (VisualChange::Type) 0;
3656         pending_visual_change.idle_handler_id = -1;
3657         
3658         if (p & VisualChange::ZoomLevel) {
3659                 set_frames_per_unit (pending_visual_change.frames_per_unit);
3660         }
3661
3662         if (p & VisualChange::TimeOrigin) {
3663                 if (pending_visual_change.time_origin != leftmost_frame) {
3664                         horizontal_adjustment.set_value (pending_visual_change.time_origin/frames_per_unit);
3665                         /* the signal handler will do the rest */
3666                 } else {
3667                         update_fixed_rulers();
3668                         tempo_map_changed (Change (0), true);
3669                 }
3670         }
3671
3672         return 0;
3673 }
3674
3675 struct EditorOrderTimeAxisSorter {
3676     bool operator() (const TimeAxisView* a, const TimeAxisView* b) const {
3677             return a->order < b->order;
3678     }
3679 };
3680         
3681 void
3682 Editor::sort_track_selection ()
3683 {
3684         EditorOrderTimeAxisSorter cmp;
3685         selection->tracks.sort (cmp);
3686 }
3687
3688 nframes_t
3689 Editor::edit_cursor_position(bool sync)
3690 {
3691         if (sync && edit_cursor->current_frame != edit_cursor_clock.current_time()) {
3692                 edit_cursor_clock.set(edit_cursor->current_frame, true);
3693         }
3694
3695         return edit_cursor->current_frame;
3696 }