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