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