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