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