b636fccc9cf4ac82d430a9299bbe59c5e7da53da
[ardour.git] / gtk2_ardour / editor_canvas.cc
1 /*
2     Copyright (C) 2005 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 <libgnomecanvasmm/init.h>
22 #include <jack/types.h>
23 #include <gtkmm2ext/utils.h>
24
25 #include <ardour/audioregion.h>
26
27 #include "ardour_ui.h"
28 #include "editor.h"
29 #include "waveview.h"
30 #include "simplerect.h"
31 #include "simpleline.h"
32 #include "imageframe.h"
33 #include "waveview_p.h"
34 #include "simplerect_p.h"
35 #include "simpleline_p.h"
36 #include "imageframe_p.h"
37 #include "canvas_impl.h"
38 #include "editing.h"
39 #include "rgb_macros.h"
40 #include "utils.h"
41 #include "time_axis_view.h"
42 #include "audio_time_axis.h"
43
44 #include "i18n.h"
45
46 using namespace std;
47 using namespace sigc;
48 using namespace ARDOUR;
49 using namespace Gtk;
50 using namespace Glib;
51 using namespace Gtkmm2ext;
52 using namespace Editing;
53
54 /* XXX this is a hack. it ought to be the maximum value of an jack_nframes_t */
55
56 const double max_canvas_coordinate = (double) JACK_MAX_FRAMES;
57
58 extern "C"
59 {
60
61 GType gnome_canvas_simpleline_get_type(void);
62 GType gnome_canvas_simplerect_get_type(void);
63 GType gnome_canvas_waveview_get_type(void);
64 GType gnome_canvas_imageframe_get_type(void);
65
66 }
67
68 static void ardour_canvas_type_init() 
69 {
70         // Map gtypes to gtkmm wrapper-creation functions:
71         
72         Glib::wrap_register(gnome_canvas_simpleline_get_type(), &Gnome::Canvas::SimpleLine_Class::wrap_new);
73         Glib::wrap_register(gnome_canvas_simplerect_get_type(), &Gnome::Canvas::SimpleRect_Class::wrap_new);
74         Glib::wrap_register(gnome_canvas_waveview_get_type(), &Gnome::Canvas::WaveView_Class::wrap_new);
75         Glib::wrap_register(gnome_canvas_imageframe_get_type(), &Gnome::Canvas::ImageFrame_Class::wrap_new);
76         
77         // Register the gtkmm gtypes:
78
79         (void) Gnome::Canvas::WaveView::get_type();
80         (void) Gnome::Canvas::SimpleLine::get_type();
81         (void) Gnome::Canvas::SimpleRect::get_type();
82         (void) Gnome::Canvas::ImageFrame::get_type();
83
84
85 void
86 Editor::initialize_canvas ()
87 {
88         ArdourCanvas::init ();
89         ardour_canvas_type_init ();
90
91         /* don't try to center the canvas */
92
93         track_canvas.set_center_scroll_region (false);
94
95         track_canvas.signal_event().connect (bind (mem_fun (*this, &Editor::track_canvas_event), (ArdourCanvas::Item*) 0));
96         track_canvas.set_name ("EditorMainCanvas");
97         track_canvas.add_events (Gdk::POINTER_MOTION_HINT_MASK|Gdk::SCROLL_MASK);
98         track_canvas.signal_leave_notify_event().connect (mem_fun(*this, &Editor::left_track_canvas));
99         track_canvas.set_flags (CAN_FOCUS);
100
101         /* set up drag-n-drop */
102
103         vector<TargetEntry> target_table;
104         
105         // Drag-N-Drop from the region list can generate this target
106         target_table.push_back (TargetEntry ("regions"));
107
108         target_table.push_back (TargetEntry ("text/plain"));
109         target_table.push_back (TargetEntry ("text/uri-list"));
110         target_table.push_back (TargetEntry ("application/x-rootwin-drop"));
111
112         track_canvas.drag_dest_set (target_table);
113         track_canvas.signal_drag_data_received().connect (mem_fun(*this, &Editor::track_canvas_drag_data_received));
114
115         /* stuff for the verbose canvas cursor */
116
117         Pango::FontDescription font = get_font_for_style (N_("VerboseCanvasCursor"));
118
119         verbose_canvas_cursor = new ArdourCanvas::Text (*track_canvas.root());
120         verbose_canvas_cursor->property_font_desc() = font;
121         verbose_canvas_cursor->property_anchor() = ANCHOR_NW;
122         verbose_canvas_cursor->property_fill_color_rgba() = color_map[cVerboseCanvasCursor];
123         
124         verbose_cursor_visible = false;
125         
126         /* a group to hold time (measure) lines */
127         
128         time_line_group = new ArdourCanvas::Group (*track_canvas.root(), 0.0, 0.0);
129         cursor_group = new ArdourCanvas::Group (*track_canvas.root(), 0.0, 0.0);
130         
131         time_canvas.set_name ("EditorTimeCanvas");
132         time_canvas.add_events (Gdk::POINTER_MOTION_HINT_MASK);
133         time_canvas.set_flags (CAN_FOCUS);
134         time_canvas.set_center_scroll_region (false);
135         
136         meter_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, 0.0);
137         tempo_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, timebar_height);
138         marker_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, timebar_height * 2.0);
139         range_marker_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, timebar_height * 3.0);
140         transport_marker_group = new ArdourCanvas::Group (*time_canvas.root(), 0.0, timebar_height * 4.0);
141         
142         tempo_bar = new ArdourCanvas::SimpleRect (*tempo_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
143         tempo_bar->property_fill_color_rgba() = color_map[cTempoBar];
144         tempo_bar->property_outline_pixels() = 0;
145         
146         meter_bar = new ArdourCanvas::SimpleRect (*meter_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
147         meter_bar->property_fill_color_rgba() = color_map[cMeterBar];
148         meter_bar->property_outline_pixels() = 0;
149         
150         marker_bar = new ArdourCanvas::SimpleRect (*marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
151         marker_bar->property_fill_color_rgba() = color_map[cMarkerBar];
152         marker_bar->property_outline_pixels() = 0;
153         
154         range_marker_bar = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
155         range_marker_bar->property_fill_color_rgba() = color_map[cRangeMarkerBar];
156         range_marker_bar->property_outline_pixels() = 0;
157         
158         transport_marker_bar = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
159         transport_marker_bar->property_fill_color_rgba() = color_map[cTransportMarkerBar];
160         transport_marker_bar->property_outline_pixels() = 0;
161         
162         range_bar_drag_rect = new ArdourCanvas::SimpleRect (*range_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
163         range_bar_drag_rect->property_fill_color_rgba() = color_map[cRangeDragBarRectFill];
164         range_bar_drag_rect->property_outline_color_rgba() = color_map[cRangeDragBarRect];
165         range_bar_drag_rect->property_outline_pixels() = 0;
166         range_bar_drag_rect->hide ();
167         
168         transport_bar_drag_rect = new ArdourCanvas::SimpleRect (*transport_marker_group, 0.0, 0.0, max_canvas_coordinate, timebar_height);
169         transport_bar_drag_rect ->property_fill_color_rgba() = color_map[cTransportDragRectFill];
170         transport_bar_drag_rect->property_outline_color_rgba() = color_map[cTransportDragRect];
171         transport_bar_drag_rect->property_outline_pixels() = 0;
172         transport_bar_drag_rect->hide ();
173         
174         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
175         marker_drag_line_points.push_back(Gnome::Art::Point(0.0, 0.0));
176
177         marker_drag_line = new ArdourCanvas::Line (*track_canvas.root());
178         marker_drag_line->property_width_pixels() = 1;
179         marker_drag_line->property_fill_color_rgba() = color_map[cMarkerDragLine];
180         marker_drag_line->property_points() = marker_drag_line_points;
181         marker_drag_line->hide();
182
183         range_marker_drag_rect = new ArdourCanvas::SimpleRect (*track_canvas.root(), 0.0, 0.0, 0.0, 0.0);
184         range_marker_drag_rect->property_fill_color_rgba() = color_map[cRangeDragRectFill];
185         range_marker_drag_rect->property_outline_color_rgba() = color_map[cRangeDragRect];
186         range_marker_drag_rect->hide ();
187         
188         transport_loop_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
189         transport_loop_range_rect->property_fill_color_rgba() = color_map[cTransportLoopRectFill];
190         transport_loop_range_rect->property_outline_color_rgba() = color_map[cTransportLoopRect];
191         transport_loop_range_rect->property_outline_pixels() = 1;
192         transport_loop_range_rect->hide();
193
194         transport_punch_range_rect = new ArdourCanvas::SimpleRect (*time_line_group, 0.0, 0.0, 0.0, 0.0);
195         transport_punch_range_rect->property_fill_color_rgba() = color_map[cTransportPunchRectFill];
196         transport_punch_range_rect->property_outline_color_rgba() = color_map[cTransportPunchRect];
197         transport_punch_range_rect->property_outline_pixels() = 0;
198         transport_punch_range_rect->hide();
199         
200         transport_loop_range_rect->lower_to_bottom (); // loop on the bottom
201
202         transport_punchin_line = new ArdourCanvas::SimpleLine (*time_line_group);
203         transport_punchin_line->property_x1() = 0.0;
204         transport_punchin_line->property_y1() = 0.0;
205         transport_punchin_line->property_x2() = 0.0;
206         transport_punchin_line->property_y2() = 0.0;
207         transport_punchin_line->property_color_rgba() = color_map[cPunchInLine];
208         transport_punchin_line->hide ();
209         
210         transport_punchout_line  = new ArdourCanvas::SimpleLine (*time_line_group);
211         transport_punchout_line->property_x1() = 0.0;
212         transport_punchout_line->property_y1() = 0.0;
213         transport_punchout_line->property_x2() = 0.0;
214         transport_punchout_line->property_y2() = 0.0;
215         transport_punchout_line->property_color_rgba() = color_map[cPunchOutLine];
216         transport_punchout_line->hide();
217         
218         // used to show zoom mode active zooming
219         zoom_rect = new ArdourCanvas::SimpleRect (*track_canvas.root(), 0.0, 0.0, 0.0, 0.0);
220         zoom_rect->property_fill_color_rgba() = color_map[cZoomRectFill];
221         zoom_rect->property_outline_color_rgba() = color_map[cZoomRect];
222         zoom_rect->property_outline_pixels() = 1;
223         zoom_rect->hide();
224         
225         zoom_rect->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_zoom_rect_event), (ArdourCanvas::Item*) 0));
226         
227         // used as rubberband rect
228         rubberband_rect = new ArdourCanvas::SimpleRect (*track_canvas.root(), 0.0, 0.0, 0.0, 0.0);
229         rubberband_rect->property_outline_color_rgba() = color_map[cRubberBandRect];
230         rubberband_rect->property_fill_color_rgba() = (guint32) color_map[cRubberBandRectFill];
231         rubberband_rect->property_outline_pixels() = 1;
232         rubberband_rect->hide();
233         
234         tempo_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_tempo_bar_event), tempo_bar));
235         meter_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_meter_bar_event), meter_bar));
236         marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_marker_bar_event), marker_bar));
237         range_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_range_marker_bar_event), range_marker_bar));
238         transport_marker_bar->signal_event().connect (bind (mem_fun (*this, &Editor::canvas_transport_marker_bar_event), transport_marker_bar));
239         
240         /* separator lines */
241         
242         tempo_line = new ArdourCanvas::SimpleLine (*tempo_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
243         tempo_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
244
245         meter_line = new ArdourCanvas::SimpleLine (*meter_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
246         meter_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
247
248         marker_line = new ArdourCanvas::SimpleLine (*marker_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
249         marker_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
250         
251         range_marker_line = new ArdourCanvas::SimpleLine (*range_marker_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
252         range_marker_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
253
254         transport_marker_line = new ArdourCanvas::SimpleLine (*transport_marker_group, 0, timebar_height, max_canvas_coordinate, timebar_height);
255         transport_marker_line->property_color_rgba() = RGBA_TO_UINT (0,0,0,255);
256
257         ZoomChanged.connect (bind (mem_fun(*this, &Editor::update_loop_range_view), false));
258         ZoomChanged.connect (bind (mem_fun(*this, &Editor::update_punch_range_view), false));
259         
260         double time_height = timebar_height * 5;
261         double time_width = FLT_MAX/frames_per_unit;
262         time_canvas.set_scroll_region(0.0, 0.0, time_width, time_height);
263         
264         edit_cursor = new Cursor (*this, "blue", &Editor::canvas_edit_cursor_event);
265         playhead_cursor = new Cursor (*this, "red", &Editor::canvas_playhead_cursor_event);
266         
267         track_canvas.signal_size_allocate().connect (mem_fun(*this, &Editor::track_canvas_allocate));
268 }
269
270 void
271 Editor::track_canvas_allocate (Gtk::Allocation alloc)
272 {
273         static bool first_time = true;
274
275         canvas_width = alloc.get_width();
276         canvas_height = alloc.get_height();
277
278         zoom_range_clock.set ((jack_nframes_t) floor ((canvas_width * frames_per_unit)));
279         edit_cursor->set_position (edit_cursor->current_frame);
280         playhead_cursor->set_position (playhead_cursor->current_frame);
281
282         reset_hscrollbar_stepping ();
283         reset_scrolling_region ();
284
285         if (edit_cursor) edit_cursor->set_length (canvas_height);
286         if (playhead_cursor) playhead_cursor->set_length (canvas_height);
287
288         if (marker_drag_line) {
289                 marker_drag_line_points.back().set_x(canvas_height);
290                 marker_drag_line->property_points() = marker_drag_line_points;
291         }
292
293         if (range_marker_drag_rect) {
294                 range_marker_drag_rect->property_y1() = 0.0;
295                 range_marker_drag_rect->property_y2() = canvas_height;
296         }
297
298         if (transport_loop_range_rect) {
299                 transport_loop_range_rect->property_y1() = 0.0;
300                 transport_loop_range_rect->property_y2() = canvas_height;
301         }
302
303         if (transport_punch_range_rect) {
304                 transport_punch_range_rect->property_y1() = 0.0;
305                 transport_punch_range_rect->property_y2() = canvas_height;
306         }
307
308         if (transport_punchin_line) {
309                 transport_punchin_line->property_y1() = 0.0;
310                 transport_punchin_line->property_y2() = canvas_height;
311         }
312
313         if (transport_punchout_line) {
314                 transport_punchout_line->property_y1() = 0.0;
315                 transport_punchout_line->property_y2() = canvas_height;
316         }
317                 
318         update_fixed_rulers ();
319
320         if (is_visible() && first_time) {
321                 tempo_map_changed (Change (0));
322                 first_time = false;
323         } else {
324                 redisplay_tempo ();
325         }
326         
327         Resized (); /* EMIT_SIGNAL */
328 }
329
330 void
331 Editor::reset_scrolling_region (Gtk::Allocation* alloc)
332 {
333         TreeModel::Children rows = route_display_model->children();
334         TreeModel::Children::iterator i;
335         double pos;
336
337         for (pos = 0, i = rows.begin(); i != rows.end(); ++i) {
338                 TimeAxisView *tv = (*i)[route_display_columns.tv];
339                 if (tv != 0 && !tv->hidden()) {
340                         pos += tv->effective_height;
341                         pos += track_spacing;
342                 }
343         }
344
345         double last_canvas_unit =  last_canvas_frame / frames_per_unit;
346
347         track_canvas.set_scroll_region (0.0, 0.0, max (last_canvas_unit, canvas_width), pos);
348
349         // XXX what is the correct height value for the time canvas ? this overstates it
350         time_canvas.set_scroll_region ( 0.0, 0.0, max (last_canvas_unit, canvas_width), canvas_height);
351
352         controls_layout.queue_resize();
353 }
354
355 void
356 Editor::controls_layout_size_request (Requisition* req)
357 {
358         TreeModel::Children rows = route_display_model->children();
359         TreeModel::Children::iterator i;
360         double pos;
361
362         for (pos = 0, i = rows.begin(); i != rows.end(); ++i) {
363                 TimeAxisView *tv = (*i)[route_display_columns.tv];
364                 if (tv != 0) {
365                         pos += tv->effective_height;
366                         pos += track_spacing;
367                 }
368         }
369
370         RefPtr<Gdk::Screen> screen = get_screen();
371
372         if (!screen) {
373                 screen = Gdk::Screen::get_default();
374         }
375
376         /* never let the width of the controls area shrink horizontally */
377
378         edit_controls_vbox.check_resize();
379
380         req->width = max (edit_controls_vbox.get_width(),  controls_layout.get_width());
381         
382         /* don't get too big. the fudge factors here are just guesses */
383         
384         req->width = min (req->width, screen->get_width() - 300);
385         req->height = min ((gint) pos, (screen->get_height() - 400));
386
387         /* this one is important: it determines how big the layout thinks it really is, as 
388            opposed to what it displays on the screen
389         */
390
391         controls_layout.set_size (req->width, (gint) pos);
392 }
393
394 bool
395 Editor::track_canvas_map_handler (GdkEventAny* ev)
396 {
397         track_canvas.get_window()->set_cursor (*current_canvas_cursor);
398         return false;
399 }
400
401 bool
402 Editor::time_canvas_map_handler (GdkEventAny* ev)
403 {
404         time_canvas.get_window()->set_cursor (*timebar_cursor);
405         return false;
406 }
407
408 void  
409 Editor::track_canvas_drag_data_received (const RefPtr<Gdk::DragContext>& context,
410                                          int x, int y, 
411                                          const SelectionData& data,
412                                          guint info, guint time)
413 {
414         if (data.get_target() == "regions") {
415                 drop_regions (context, x, y, data, info, time);
416         } else {
417                 drop_paths (context, x, y, data, info, time);
418         }
419 }
420
421 void
422 Editor::drop_paths (const RefPtr<Gdk::DragContext>& context,
423                     int x, int y, 
424                     const SelectionData& data,
425                     guint info, guint time)
426 {
427         TimeAxisView* tvp;
428         AudioTimeAxisView* tv;
429         double cy;
430         vector<ustring> paths;
431         string spath;
432         GdkEvent ev;
433         jack_nframes_t frame;
434
435         if (convert_drop_to_paths (paths, context, x, y, data, info, time)) {
436                 goto out;
437         }
438
439         /* D-n-D coordinates are window-relative, so convert to "world" coordinates
440          */
441
442         double wx;
443         double wy;
444
445         track_canvas.window_to_world (x, y, wx, wy);
446         wx += horizontal_adjustment.get_value();
447         wy += vertical_adjustment.get_value();
448         
449         ev.type = GDK_BUTTON_RELEASE;
450         ev.button.x = wx;
451         ev.button.y = wy;
452
453         frame = event_frame (&ev, 0, &cy);
454
455         snap_to (frame);
456
457         if ((tvp = trackview_by_y_position (cy)) == 0) {
458
459                 /* drop onto canvas background: create new tracks */
460
461                 jack_nframes_t pos = 0;
462                 do_embed (paths, false, ImportAsTrack, 0, pos, false);
463                 
464         } else if ((tv = dynamic_cast<AudioTimeAxisView*>(tvp)) != 0) {
465
466                 /* check that its an audio track, not a bus */
467                 
468                 if (tv->get_diskstream()) {
469                         do_embed (paths, false, ImportToTrack, tv->audio_track(), frame, true);
470                 }
471         }
472
473   out:
474         context->drag_finish (true, false, time);
475 }
476
477 void
478 Editor::drop_regions (const RefPtr<Gdk::DragContext>& context,
479                       int x, int y, 
480                       const SelectionData& data,
481                       guint info, guint time)
482 {
483         const DnDTreeView::SerializedObjectPointers* sr = reinterpret_cast<const DnDTreeView::SerializedObjectPointers*> (data.get_data());
484
485         for (uint32_t i = 0; i < sr->cnt; ++i) {
486
487                 Region* r = reinterpret_cast<Region*> (sr->ptr[i]);
488                 AudioRegion* ar;
489
490                 if ((ar = dynamic_cast<AudioRegion*>(r)) != 0) {
491                         insert_region_list_drag (*ar, x, y);
492                 }
493         }
494
495         context->drag_finish (true, false, time);
496 }
497