Merge branch 'master' into saveas
[ardour.git] / gtk2_ardour / marker.cc
1 /*
2     Copyright (C) 2001 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <sigc++/bind.h>
21 #include "ardour/tempo.h"
22
23 #include "canvas/rectangle.h"
24 #include "canvas/container.h"
25 #include "canvas/line.h"
26 #include "canvas/polygon.h"
27 #include "canvas/text.h"
28 #include "canvas/canvas.h"
29 #include "canvas/scroll_group.h"
30 #include "canvas/debug.h"
31
32 #include "ardour_ui.h"
33 /*
34  * ardour_ui.h include was moved to the top of the list
35  * due to a conflicting definition of 'Rect' between
36  * Apple's MacTypes.h and GTK.
37  */
38
39 #include "marker.h"
40 #include "public_editor.h"
41 #include "utils.h"
42 #include "rgb_macros.h"
43
44 #include <gtkmm2ext/utils.h>
45
46 #include "i18n.h"
47
48 using namespace std;
49 using namespace ARDOUR;
50 using namespace ARDOUR_UI_UTILS;
51 using namespace Gtkmm2ext;
52
53 PBD::Signal1<void,Marker*> Marker::CatchDeletion;
54
55 static double marker_height = 13.0;
56
57 void Marker::setup_sizes(const double timebar_height)
58 {
59         marker_height = floor (timebar_height) - 2;
60 }
61
62 Marker::Marker (PublicEditor& ed, ArdourCanvas::Container& parent, guint32 rgba, const string& annotation,
63                 Type type, framepos_t frame, bool handle_events)
64
65         : editor (ed)
66         , _parent (&parent)
67         , _track_canvas_line (0)
68         , _type (type)
69         , _selected (false)
70         , _shown (false)
71         , _line_shown (false)
72         , _color (rgba)
73         , _left_label_limit (DBL_MAX)
74         , _right_label_limit (DBL_MAX)
75         , _label_offset (0)
76
77 {
78
79         const double MH = marker_height - 1;
80         /* Shapes we use:
81          *
82          * Mark:
83          *
84          *  (0,0)   ->  (6,0)
85          *    ^           |
86          *    |           V
87          * (0,MH*.4)  (6,MH*.4)
88          *     \         /
89          *        (3,MH)
90          *
91          *
92          * TempoMark:
93          * MeterMark:
94          *
95          *        (3,0)
96          *     /         \
97          * (0,MH*.6)  (6,MH.*.6)
98          *    ^           |
99          *    |           V
100          * (0,MH)   <-  (6,MH)
101          *
102          *
103          * SessionStart:
104          * RangeStart:
105          *
106          *       0,0\
107          *        |  \
108          *        |   \ 6,MH/2
109          *        |   /
110          *        |  /
111          *       0,MH
112          *
113          *
114          * SessionEnd:
115          * RangeEnd:
116          *
117          *         /12,0
118          *        /   |
119          * 6,MH/2/    |
120          *       \    |
121          *        \   |
122          *         \12,MH
123          *
124          *
125          * PunchIn:
126          *
127          *   0,0 ------> marker_height,0
128          *    |       /
129          *    |      /
130          *    |     /
131          *    |    /
132          *    |   /
133          *    |  /
134          *   0,marker_height
135          *
136          *
137          *   PunchOut
138          *
139          *   0,0 ------> marker_height,0
140          *    \        |
141          *     \       |
142          *      \      |
143          *       \     |
144          *        \    |
145          *         \   |
146          *          \  |
147          *   marker_height,marker_height
148          *
149          */
150
151         switch (type) {
152         case Mark:
153                 points = new ArdourCanvas::Points ();
154
155                 points->push_back (ArdourCanvas::Duple (0.0, 0.0));
156                 points->push_back (ArdourCanvas::Duple (6.0, 0.0));
157                 points->push_back (ArdourCanvas::Duple (6.0, MH * .4));
158                 points->push_back (ArdourCanvas::Duple (3.0, MH));
159                 points->push_back (ArdourCanvas::Duple (0.0, MH * .4));
160                 points->push_back (ArdourCanvas::Duple (0.0, 0.0));
161
162                 _shift = 3;
163                 _label_offset = 10.0;
164                 break;
165
166         case Tempo:
167         case Meter:
168                 points = new ArdourCanvas::Points ();
169                 points->push_back (ArdourCanvas::Duple (3.0, 0.0));
170                 points->push_back (ArdourCanvas::Duple (6.0, MH * .6));
171                 points->push_back (ArdourCanvas::Duple (6.0, MH));
172                 points->push_back (ArdourCanvas::Duple (0.0, MH));
173                 points->push_back (ArdourCanvas::Duple (0.0, MH * .6));
174                 points->push_back (ArdourCanvas::Duple (3.0, 0.0));
175
176                 _shift = 3;
177                 _label_offset = 8.0;
178                 break;
179
180         case SessionStart:
181         case RangeStart:
182                 points = new ArdourCanvas::Points ();
183                 points->push_back (ArdourCanvas::Duple (0.0, 0.0));
184                 points->push_back (ArdourCanvas::Duple (6.5, MH * .5));
185                 points->push_back (ArdourCanvas::Duple (0.0, MH));
186                 points->push_back (ArdourCanvas::Duple (0.0, 0.0));
187
188                 _shift = 0;
189                 _label_offset = 8.0;
190                 break;
191
192         case SessionEnd:
193         case RangeEnd:
194                 points = new ArdourCanvas::Points ();
195                 points->push_back (ArdourCanvas::Duple (6.0, 0.0));
196                 points->push_back (ArdourCanvas::Duple (6.0, MH));
197                 points->push_back (ArdourCanvas::Duple (0.0, MH * .5));
198                 points->push_back (ArdourCanvas::Duple (6.0, 0.0));
199
200                 _shift = 6.0;
201                 _label_offset = 0.0;
202                 break;
203
204         case LoopStart:
205                 points = new ArdourCanvas::Points ();
206                 points->push_back (ArdourCanvas::Duple (0.0, 0.0));
207                 points->push_back (ArdourCanvas::Duple (marker_height, marker_height));
208                 points->push_back (ArdourCanvas::Duple (0.0, marker_height));
209                 points->push_back (ArdourCanvas::Duple (0.0, 0.0));
210
211                 _shift = 0;
212                 _label_offset = marker_height;
213                 break;
214
215         case LoopEnd:
216                 points = new ArdourCanvas::Points ();
217                 points->push_back (ArdourCanvas::Duple (marker_height,  0.0));
218                 points->push_back (ArdourCanvas::Duple (marker_height, marker_height));
219                 points->push_back (ArdourCanvas::Duple (0.0, marker_height));
220                 points->push_back (ArdourCanvas::Duple (marker_height, 0.0));
221
222                 _shift = marker_height;
223                 _label_offset = 0.0;
224                 break;
225
226         case PunchIn:
227                 points = new ArdourCanvas::Points ();
228                 points->push_back (ArdourCanvas::Duple (0.0, 0.0));
229                 points->push_back (ArdourCanvas::Duple (marker_height, 0.0));
230                 points->push_back (ArdourCanvas::Duple (0.0, marker_height));
231                 points->push_back (ArdourCanvas::Duple (0.0, 0.0));
232
233                 _shift = 0;
234                 _label_offset = marker_height;
235                 break;
236
237         case PunchOut:
238                 points = new ArdourCanvas::Points ();
239                 points->push_back (ArdourCanvas::Duple (0.0, 0.0));
240                 points->push_back (ArdourCanvas::Duple (marker_height, 0.0));
241                 points->push_back (ArdourCanvas::Duple (marker_height, marker_height));
242                 points->push_back (ArdourCanvas::Duple (0.0, 0.0));
243
244                 _shift = marker_height;
245                 _label_offset = 0.0;
246                 break;
247
248         }
249
250         frame_position = frame;
251         unit_position = editor.sample_to_pixel (frame);
252         unit_position -= _shift;
253
254         group = new ArdourCanvas::Container (&parent, ArdourCanvas::Duple (unit_position, 0));
255 #ifdef CANVAS_DEBUG
256         group->name = string_compose ("Marker::group for %1", annotation);
257 #endif  
258
259         _name_background = new ArdourCanvas::Rectangle (group);
260 #ifdef CANVAS_DEBUG
261         _name_background->name = string_compose ("Marker::_name_background for %1", annotation);
262 #endif  
263
264         /* adjust to properly locate the tip */
265
266         mark = new ArdourCanvas::Polygon (group);
267         CANVAS_DEBUG_NAME (mark, string_compose ("Marker::mark for %1", annotation));
268
269         mark->set (*points);
270         set_color_rgba (rgba);
271
272         /* setup name pixbuf sizes */
273         name_font = get_font_for_style (N_("MarkerText"));
274
275         Gtk::Label foo;
276
277         Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (X_("Hg")); /* ascender + descender */
278         int width;
279
280         layout->set_font_description (name_font);
281         Gtkmm2ext::get_ink_pixel_size (layout, width, name_height);
282         
283         _name_item = new ArdourCanvas::Text (group);
284         CANVAS_DEBUG_NAME (_name_item, string_compose ("Marker::_name_item for %1", annotation));
285         _name_item->set_font_description (name_font);
286         _name_item->set_color (RGBA_TO_UINT (0,0,0,255));
287         _name_item->set_position (ArdourCanvas::Duple (_label_offset, (marker_height - name_height - 1) * .5 ));
288
289         set_name (annotation.c_str());
290
291         editor.ZoomChanged.connect (sigc::mem_fun (*this, &Marker::reposition));
292
293         /* events will be handled by both the group and the mark itself, so
294          * make sure they can both be used to lookup this object.
295          */
296
297         group->set_data ("marker", this);
298         mark->set_data ("marker", this);
299         
300         if (handle_events) {
301                 group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), group, this));
302         }
303 }
304
305 Marker::~Marker ()
306 {
307         CatchDeletion (this); /* EMIT SIGNAL */
308
309         /* destroying the parent group destroys its contents, namely any polygons etc. that we added */
310         delete group;
311         delete _track_canvas_line;
312 }
313
314 void Marker::reparent(ArdourCanvas::Container & parent)
315 {
316         group->reparent (&parent);
317         _parent = &parent;
318 }
319
320 void
321 Marker::set_selected (bool s)
322 {
323         _selected = s;
324         setup_line ();
325 }
326
327 void
328 Marker::set_show_line (bool s)
329 {
330         _line_shown = s;
331         setup_line ();
332 }
333
334 void
335 Marker::setup_line ()
336 {
337         if (_shown && (_selected || _line_shown)) {
338
339                 if (_track_canvas_line == 0) {
340
341                         _track_canvas_line = new ArdourCanvas::Line (editor.get_hscroll_group());
342                         _track_canvas_line->set_outline_color (ARDOUR_UI::config()->color ("edit point"));
343                         _track_canvas_line->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), group, this));
344                 }
345
346                 ArdourCanvas::Duple g = group->canvas_origin();
347                 ArdourCanvas::Duple d = _track_canvas_line->canvas_to_item (ArdourCanvas::Duple (g.x + _shift, 0));
348
349                 _track_canvas_line->set_x0 (d.x);
350                 _track_canvas_line->set_x1 (d.x);
351                 _track_canvas_line->set_y0 (d.y);
352                 _track_canvas_line->set_y1 (ArdourCanvas::COORD_MAX);
353                 _track_canvas_line->set_outline_color (_selected ? ARDOUR_UI::config()->color ("edit point") : _color);
354                 _track_canvas_line->raise_to_top ();
355                 _track_canvas_line->show ();
356
357         } else {
358                 if (_track_canvas_line) {
359                         _track_canvas_line->hide ();
360                 }
361         }
362 }
363
364 void
365 Marker::canvas_height_set (double h)
366 {
367         _canvas_height = h;
368         setup_line ();
369 }
370
371 ArdourCanvas::Item&
372 Marker::the_item() const
373 {
374         return *group;
375 }
376
377 void
378 Marker::set_name (const string& new_name)
379 {
380         _name = new_name;
381
382         setup_name_display ();
383 }
384
385 /** @return true if our label is on the left of the mark, otherwise false */
386 bool
387 Marker::label_on_left () const
388 {
389         return (_type == SessionEnd || _type == RangeEnd || _type == LoopEnd || _type == PunchOut);
390 }
391
392 void
393 Marker::setup_name_display ()
394 {
395         double limit = DBL_MAX;
396
397         if (label_on_left ()) {
398                 limit = _left_label_limit;
399         } else {
400                 limit = _right_label_limit;
401         }
402
403         /* Work out how wide the name can be */
404         int name_width = min ((double) pixel_width (_name, name_font) + 2, limit);
405
406         if (name_width == 0) {
407                 _name_item->hide ();
408         } else {
409                 _name_item->show ();
410
411                 if (label_on_left ()) {
412                         _name_item->set_x_position (-name_width);
413                 }
414                         
415                 _name_item->clamp_width (name_width);
416                 _name_item->set (_name);
417                 
418                 if (label_on_left ()) {
419                         /* adjust right edge of background to fit text */
420                         _name_background->set_x0 (_name_item->position().x - 2);
421                         _name_background->set_x1 (_name_item->position().x + name_width + _shift);
422                 } else {
423                         /* right edge remains at zero (group-relative). Add
424                          * arbitrary 2 pixels of extra padding at the end
425                          */
426                         _name_background->set_x1 (_name_item->position().x + name_width + 2.0);
427                 }
428         }
429
430         _name_background->set_y0 (0);
431         /* unfortunate hard coding - this has to * match the marker bars height */
432         _name_background->set_y1 (marker_height + 1.0); 
433 }
434
435 void
436 Marker::set_position (framepos_t frame)
437 {
438         unit_position = editor.sample_to_pixel (frame) - _shift;
439         group->set_x_position (unit_position);
440         setup_line ();
441         frame_position = frame;
442 }
443
444 void
445 Marker::reposition ()
446 {
447         set_position (frame_position);
448 }
449
450 void
451 Marker::show ()
452 {
453         _shown = true;
454
455         group->show ();
456         setup_line ();
457 }
458
459 void
460 Marker::hide ()
461 {
462         _shown = false;
463
464         group->hide ();
465         setup_line ();
466 }
467
468 void
469 Marker::set_color_rgba (uint32_t c)
470 {
471         _color = c;
472         mark->set_fill_color (_color);
473         mark->set_outline_color (_color);
474
475         if (_track_canvas_line && !_selected) {
476                 _track_canvas_line->set_outline_color (_color);
477         }
478
479         _name_background->set_fill (true);
480         _name_background->set_fill_color (UINT_RGBA_CHANGE_A (_color, 0x70));
481         _name_background->set_outline_color (_color);
482 }
483
484 /** Set the number of pixels that are available for a label to the left of the centre of this marker */
485 void
486 Marker::set_left_label_limit (double p)
487 {
488         /* Account for the size of the marker */
489         _left_label_limit = p - marker_height;
490         if (_left_label_limit < 0) {
491                 _left_label_limit = 0;
492         }
493
494         if (label_on_left ()) {
495                 setup_name_display ();
496         }
497 }
498
499 /** Set the number of pixels that are available for a label to the right of the centre of this marker */
500 void
501 Marker::set_right_label_limit (double p)
502 {
503         /* Account for the size of the marker */
504         _right_label_limit = p - marker_height;
505         if (_right_label_limit < 0) {
506                 _right_label_limit = 0;
507         }
508
509         if (!label_on_left ()) {
510                 setup_name_display ();
511         }
512 }
513
514 /***********************************************************************/
515
516 TempoMarker::TempoMarker (PublicEditor& editor, ArdourCanvas::Container& parent, guint32 rgba, const string& text,
517                           ARDOUR::TempoSection& temp)
518         : Marker (editor, parent, rgba, text, Tempo, 0, false),
519           _tempo (temp)
520 {
521         set_position (_tempo.frame());
522         group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_marker_event), group, this));
523 }
524
525 TempoMarker::~TempoMarker ()
526 {
527 }
528
529 /***********************************************************************/
530
531 MeterMarker::MeterMarker (PublicEditor& editor, ArdourCanvas::Container& parent, guint32 rgba, const string& text,
532                           ARDOUR::MeterSection& m)
533         : Marker (editor, parent, rgba, text, Meter, 0, false),
534           _meter (m)
535 {
536         set_position (_meter.frame());
537         group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_meter_marker_event), group, this));
538 }
539
540 MeterMarker::~MeterMarker ()
541 {
542 }
543