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