Add backgrounds to marker labels.
[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 "ardour_ui.h"
24 /*
25  * ardour_ui.h include was moved to the top of the list
26  * due to a conflicting definition of 'Rect' between
27  * Apple's MacTypes.h and GTK.
28  */
29
30 #include "marker.h"
31 #include "public_editor.h"
32 #include "utils.h"
33 #include "canvas_impl.h"
34 #include "simpleline.h"
35 #include "simplerect.h"
36 #include "rgb_macros.h"
37
38 #include <gtkmm2ext/utils.h>
39
40 #include "i18n.h"
41
42 using namespace std;
43 using namespace ARDOUR;
44 using namespace Gtkmm2ext;
45
46 PBD::Signal1<void,Marker*> Marker::CatchDeletion;
47
48 Marker::Marker (PublicEditor& ed, ArdourCanvas::Group& parent, ArdourCanvas::Group& line_parent, guint32 rgba, const string& annotation,
49                 Type type, nframes_t frame, bool handle_events)
50
51         : editor (ed)
52         , _parent (&parent)
53         , _line_parent (&line_parent)
54         , _line (0)
55         , _type (type)
56         , _selected (false)
57         , _shown (false)
58         , _line_shown (false)
59         , _canvas_height (0)
60         , _color (rgba)
61         , _left_label_limit (DBL_MAX)
62         , _right_label_limit (DBL_MAX)
63         , _label_offset (0)
64
65 {
66         /* Shapes we use:
67
68           Mark:
69
70            (0,0) -> (6,0)
71              ^        |
72              |        V
73            (0,5)    (6,5)
74                \    /
75                (3,13)
76
77
78            TempoMark:
79            MeterMark:
80
81                (3,0)
82               /      \
83            (0,5) -> (6,5)
84              ^        |
85              |        V
86            (0,10)<-(6,10)
87
88
89            Start:
90
91            0,0\
92             |  \
93             |   \ 6,6
94             |   /
95             |  /
96            0,12
97
98            End:
99
100                /12,0
101               /     |
102              /      |
103            6,6      |
104              \      |
105               \     |
106                \    |
107                12,12
108
109              PunchIn:
110
111              0,0 ------> 13,0
112               |       /
113               |      /
114               |     /
115               |    /
116               |   /
117               |  /
118              0,13
119
120              PunchOut
121
122            0,0 -->-13,0
123             \       |
124              \      |
125               \     |
126                \    |
127                 \   |
128                  \  |
129                  13,13
130
131
132         */
133
134         switch (type) {
135         case Mark:
136                 points = new ArdourCanvas::Points ();
137
138                 points->push_back (Gnome::Art::Point (0.0, 0.0));
139                 points->push_back (Gnome::Art::Point (6.0, 0.0));
140                 points->push_back (Gnome::Art::Point (6.0, 5.0));
141                 points->push_back (Gnome::Art::Point (3.0, 13.0));
142                 points->push_back (Gnome::Art::Point (0.0, 5.0));
143                 points->push_back (Gnome::Art::Point (0.0, 0.0));
144
145                 _shift = 3;
146                 _label_offset = 8.0;
147                 break;
148
149         case Tempo:
150         case Meter:
151
152                 points = new ArdourCanvas::Points ();
153                 points->push_back (Gnome::Art::Point (3.0, 0.0));
154                 points->push_back (Gnome::Art::Point (6.0, 5.0));
155                 points->push_back (Gnome::Art::Point (6.0, 10.0));
156                 points->push_back (Gnome::Art::Point (0.0, 10.0));
157                 points->push_back (Gnome::Art::Point (0.0, 5.0));
158                 points->push_back (Gnome::Art::Point (3.0, 0.0));
159
160                 _shift = 3;
161                 _label_offset = 8.0;
162                 break;
163
164         case SessionStart:
165         case RangeStart:
166                 
167                 points = new ArdourCanvas::Points ();
168                 points->push_back (Gnome::Art::Point (0.0, 0.0));
169                 points->push_back (Gnome::Art::Point (6.5, 6.5));
170                 points->push_back (Gnome::Art::Point (0.0, 13.0));
171                 points->push_back (Gnome::Art::Point (0.0, 0.0));
172
173                 _shift = 0;
174                 _label_offset = 13.0;
175                 break;
176
177         case SessionEnd:
178         case RangeEnd:
179                 points = new ArdourCanvas::Points ();
180                 points->push_back (Gnome::Art::Point (6.5, 6.5));
181                 points->push_back (Gnome::Art::Point (13.0, 0.0));
182                 points->push_back (Gnome::Art::Point (13.0, 13.0));
183                 points->push_back (Gnome::Art::Point (6.5, 6.5));
184
185                 _shift = 13;
186                 _label_offset = 6.0;
187                 break;
188
189         case LoopStart:
190                 points = new ArdourCanvas::Points ();
191                 points->push_back (Gnome::Art::Point (0.0, 0.0));
192                 points->push_back (Gnome::Art::Point (13.0, 13.0));
193                 points->push_back (Gnome::Art::Point (0.0, 13.0));
194                 points->push_back (Gnome::Art::Point (0.0, 0.0));
195
196                 _shift = 0;
197                 _label_offset = 12.0;
198                 break;
199
200         case LoopEnd:
201                 points = new ArdourCanvas::Points ();
202                 points->push_back (Gnome::Art::Point (13.0,  0.0));
203                 points->push_back (Gnome::Art::Point (13.0, 13.0));
204                 points->push_back (Gnome::Art::Point (0.0, 13.0));
205                 points->push_back (Gnome::Art::Point (13.0, 0.0));
206
207                 _shift = 13;
208                 _label_offset = 0.0;
209                 break;
210
211         case  PunchIn:
212                 points = new ArdourCanvas::Points ();
213                 points->push_back (Gnome::Art::Point (0.0, 0.0));
214                 points->push_back (Gnome::Art::Point (13.0, 0.0));
215                 points->push_back (Gnome::Art::Point (0.0, 13.0));
216                 points->push_back (Gnome::Art::Point (0.0, 0.0));
217
218                 _shift = 0;
219                 _label_offset = 13.0;
220                 break;
221
222         case  PunchOut:
223                 points = new ArdourCanvas::Points ();
224                 points->push_back (Gnome::Art::Point (0.0, 0.0));
225                 points->push_back (Gnome::Art::Point (12.0, 0.0));
226                 points->push_back (Gnome::Art::Point (12.0, 12.0));
227                 points->push_back (Gnome::Art::Point (0.0, 0.0));
228
229                 _shift = 13;
230                 _label_offset = 0.0;
231                 break;
232
233         }
234
235         frame_position = frame;
236         unit_position = editor.frame_to_unit (frame);
237         unit_position -= _shift;
238         
239         group = new Group (parent, unit_position, 1.0);
240
241         _name_background = new ArdourCanvas::SimpleRect (*group);
242         _name_background->property_outline_pixels() = 1;
243
244         /* adjust to properly locate the tip */
245
246         mark = new Polygon (*group);
247         mark->property_points() = *points;
248         set_color_rgba (rgba);
249         mark->property_width_pixels() = 1;
250
251         /* setup name pixbuf sizes */
252         name_font = get_font_for_style (N_("MarkerText"));
253
254         Gtk::Label foo;
255
256         Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (X_("Hg")); /* ascender + descender */
257         int width;
258
259         layout->set_font_description (*name_font);
260         Gtkmm2ext::get_ink_pixel_size (layout, width, name_height);
261
262         name_pixbuf = new ArdourCanvas::Pixbuf(*group);
263         name_pixbuf->property_x() = _label_offset;
264         name_pixbuf->property_y() = (13/2) - (name_height/2);
265
266         set_name (annotation.c_str());
267
268         editor.ZoomChanged.connect (sigc::mem_fun (*this, &Marker::reposition));
269
270         mark->set_data ("marker", this);
271
272         if (handle_events) {
273                 group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), mark, this));
274         }
275
276 }
277
278
279 Marker::~Marker ()
280 {
281         CatchDeletion (this); /* EMIT SIGNAL */
282
283         /* destroying the parent group destroys its contents, namely any polygons etc. that we added */
284         delete name_pixbuf;
285         delete mark;
286         delete points;
287
288         delete _line;
289 }
290
291 void Marker::reparent(ArdourCanvas::Group & parent)
292 {
293         group->reparent(parent);
294         _parent = &parent;
295 }
296
297 void
298 Marker::set_selected (bool s)
299 {
300         _selected = s;
301         setup_line ();
302 }
303
304 void
305 Marker::set_show_line (bool s)
306 {
307         _line_shown = s;
308         setup_line ();
309 }
310
311 void
312 Marker::setup_line ()
313 {
314         if (_shown && (_selected || _line_shown)) {
315
316                 if (_line == 0) {
317
318                         _line = new ArdourCanvas::SimpleLine (*_line_parent);
319                         _line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_EditPoint.get();
320
321                         setup_line_x ();
322
323                         _line->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), mark, this));
324                 }
325                 
326                 double yo = 0;
327                 if (!_selected) {
328                         /* work out where to start the line from so that it extends only as far as the mark */
329                         double x = 0;
330                         _parent->i2w (x, yo);
331                         _line_parent->w2i (x, yo);
332                 }
333
334                 _line->property_y1() = yo + 10;
335                 _line->property_y2() = yo + 10 + _canvas_height;
336
337                 _line->property_color_rgba() = _selected ? ARDOUR_UI::config()->canvasvar_EditPoint.get() : _color;
338                 _line->raise_to_top ();
339                 _line->show ();
340
341         } else {
342                 if (_line) {
343                         _line->hide ();
344                 }
345         }
346 }
347
348 void
349 Marker::canvas_height_set (double h)
350 {
351         _canvas_height = h;
352         setup_line ();
353 }
354
355 ArdourCanvas::Item&
356 Marker::the_item() const
357 {
358         return *mark;
359 }
360
361 void
362 Marker::set_name (const string& new_name)
363 {
364         _name = new_name;
365
366         setup_name_display ();
367 }
368
369 /** @return true if our label is on the left of the mark, otherwise false */
370 bool
371 Marker::label_on_left () const
372 {
373         return (_type == SessionEnd || _type == RangeEnd || _type == LoopEnd || _type == PunchOut);
374 }
375
376 void
377 Marker::setup_name_display ()
378 {
379         double limit = DBL_MAX;
380         
381         if (label_on_left ()) {
382                 limit = _left_label_limit;
383         } else {
384                 limit = _right_label_limit;
385         }
386
387         /* Work out how wide the name can be */
388         int name_width = min ((double) pixel_width (_name, *name_font) + 2, limit);
389         if (name_width == 0) {
390                 name_width = 1;
391         }
392
393         if (label_on_left ()) {
394                 name_pixbuf->property_x() = -name_width;
395         }
396
397         name_pixbuf->property_pixbuf() = pixbuf_from_string (_name, name_font, name_width, name_height, Gdk::Color ("#000000"));
398
399         if (label_on_left ()) {
400                 _name_background->property_x1() = name_pixbuf->property_x() - 2;
401                 _name_background->property_x2() = name_pixbuf->property_x() + name_width + _shift;
402         } else {
403                 _name_background->property_x1() = name_pixbuf->property_x() - _label_offset + 2;
404                 _name_background->property_x2() = name_pixbuf->property_x() + name_width;
405         }
406         
407         _name_background->property_y1() = 0;
408         _name_background->property_y2() = 13;
409 }
410
411 void
412 Marker::setup_line_x ()
413 {
414         if (_line) {
415                 _line->property_x1() = unit_position + _shift - 0.5;
416                 _line->property_x2() = unit_position + _shift - 0.5;
417         }
418 }
419
420 void
421 Marker::set_position (framepos_t frame)
422 {
423         double new_unit_position = editor.frame_to_unit (frame);
424         new_unit_position -= _shift;
425         group->move (new_unit_position - unit_position, 0.0);
426         frame_position = frame;
427         unit_position = new_unit_position;
428
429         setup_line_x ();
430 }
431
432 void
433 Marker::reposition ()
434 {
435         set_position (frame_position);
436 }
437
438 void
439 Marker::show ()
440 {
441         _shown = true;
442         
443         group->show ();
444         setup_line ();
445 }
446
447 void
448 Marker::hide ()
449 {
450         _shown = false;
451         
452         group->hide ();
453         setup_line ();
454 }
455
456 void
457 Marker::set_color_rgba (uint32_t c)
458 {
459         _color = c;
460         mark->property_fill_color_rgba() = _color;
461         mark->property_outline_color_rgba() = _color;
462
463         if (_line && !_selected) {
464                 _line->property_color_rgba() = _color;
465
466                 /* For reasons unknown this is necessary to ensure that the line colour
467                    gets updated.
468                 */
469                 _line->hide ();
470                 _line->show ();
471         }
472
473         _name_background->property_fill() = true;
474         _name_background->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (_color, 0x70);
475         _name_background->property_outline_color_rgba() = _color;
476 }
477
478 /** Set the number of pixels that are available for a label to the left of the centre of this marker */
479 void
480 Marker::set_left_label_limit (double p)
481 {
482         /* Account for the size of the marker */
483         _left_label_limit = p - 13;
484         if (_left_label_limit < 0) {
485                 _left_label_limit = 0;
486         }
487         
488         if (label_on_left ()) {
489                 setup_name_display ();
490         }
491 }
492
493 /** Set the number of pixels that are available for a label to the right of the centre of this marker */
494 void
495 Marker::set_right_label_limit (double p)
496 {
497         /* Account for the size of the marker */
498         _right_label_limit = p - 13;
499         if (_right_label_limit < 0) {
500                 _right_label_limit = 0;
501         }
502         
503         if (!label_on_left ()) {
504                 setup_name_display ();
505         }
506 }
507
508 /***********************************************************************/
509
510 TempoMarker::TempoMarker (PublicEditor& editor, ArdourCanvas::Group& parent, ArdourCanvas::Group& line_parent, guint32 rgba, const string& text,
511                           ARDOUR::TempoSection& temp)
512         : Marker (editor, parent, line_parent, rgba, text, Tempo, 0, false),
513           _tempo (temp)
514 {
515         set_position (_tempo.frame());
516         group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_marker_event), mark, this));
517 }
518
519 TempoMarker::~TempoMarker ()
520 {
521 }
522
523 /***********************************************************************/
524
525 MeterMarker::MeterMarker (PublicEditor& editor, ArdourCanvas::Group& parent, ArdourCanvas::Group& line_parent, guint32 rgba, const string& text,
526                           ARDOUR::MeterSection& m)
527         : Marker (editor, parent, line_parent, rgba, text, Meter, 0, false),
528           _meter (m)
529 {
530         set_position (_meter.frame());
531         group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_meter_marker_event), mark, this));
532 }
533
534 MeterMarker::~MeterMarker ()
535 {
536 }
537