Move RDF-based preset stuff into LadspaPlugin, to make way for a set of evil hacks...
[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, framepos_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         _name_background->set_data ("marker", this);
272
273         if (handle_events) {
274                 group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), mark, this));
275         }
276
277 }
278
279
280 Marker::~Marker ()
281 {
282         CatchDeletion (this); /* EMIT SIGNAL */
283
284         /* destroying the parent group destroys its contents, namely any polygons etc. that we added */
285         delete group;
286         delete _line;
287 }
288
289 void Marker::reparent(ArdourCanvas::Group & parent)
290 {
291         group->reparent(parent);
292         _parent = &parent;
293 }
294
295 void
296 Marker::set_selected (bool s)
297 {
298         _selected = s;
299         setup_line ();
300 }
301
302 void
303 Marker::set_show_line (bool s)
304 {
305         _line_shown = s;
306         setup_line ();
307 }
308
309 void
310 Marker::setup_line ()
311 {
312         if (_shown && (_selected || _line_shown)) {
313
314                 if (_line == 0) {
315
316                         _line = new ArdourCanvas::SimpleLine (*_line_parent);
317                         _line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_EditPoint.get();
318
319                         setup_line_x ();
320
321                         _line->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), mark, this));
322                 }
323                 
324                 double yo = 0;
325                 if (!_selected) {
326                         /* work out where to start the line from so that it extends only as far as the mark */
327                         double x = 0;
328                         _parent->i2w (x, yo);
329                         _line_parent->w2i (x, yo);
330                 }
331
332                 _line->property_y1() = yo + 10;
333                 _line->property_y2() = yo + 10 + _canvas_height;
334
335                 _line->property_color_rgba() = _selected ? ARDOUR_UI::config()->canvasvar_EditPoint.get() : _color;
336                 _line->raise_to_top ();
337                 _line->show ();
338
339         } else {
340                 if (_line) {
341                         _line->hide ();
342                 }
343         }
344 }
345
346 void
347 Marker::canvas_height_set (double h)
348 {
349         _canvas_height = h;
350         setup_line ();
351 }
352
353 ArdourCanvas::Item&
354 Marker::the_item() const
355 {
356         return *mark;
357 }
358
359 void
360 Marker::set_name (const string& new_name)
361 {
362         _name = new_name;
363
364         setup_name_display ();
365 }
366
367 /** @return true if our label is on the left of the mark, otherwise false */
368 bool
369 Marker::label_on_left () const
370 {
371         return (_type == SessionEnd || _type == RangeEnd || _type == LoopEnd || _type == PunchOut);
372 }
373
374 void
375 Marker::setup_name_display ()
376 {
377         double limit = DBL_MAX;
378         
379         if (label_on_left ()) {
380                 limit = _left_label_limit;
381         } else {
382                 limit = _right_label_limit;
383         }
384
385         /* Work out how wide the name can be */
386         int name_width = min ((double) pixel_width (_name, *name_font) + 2, limit);
387         if (name_width == 0) {
388                 name_width = 1;
389         }
390
391         if (label_on_left ()) {
392                 name_pixbuf->property_x() = -name_width;
393         }
394
395         name_pixbuf->property_pixbuf() = pixbuf_from_string (_name, name_font, name_width, name_height, Gdk::Color ("#000000"));
396
397         if (label_on_left ()) {
398                 _name_background->property_x1() = name_pixbuf->property_x() - 2;
399                 _name_background->property_x2() = name_pixbuf->property_x() + name_width + _shift;
400         } else {
401                 _name_background->property_x1() = name_pixbuf->property_x() - _label_offset + 2;
402                 _name_background->property_x2() = name_pixbuf->property_x() + name_width;
403         }
404         
405         _name_background->property_y1() = 0;
406         _name_background->property_y2() = 13;
407 }
408
409 void
410 Marker::setup_line_x ()
411 {
412         if (_line) {
413                 _line->property_x1() = unit_position + _shift - 0.5;
414                 _line->property_x2() = unit_position + _shift - 0.5;
415         }
416 }
417
418 void
419 Marker::set_position (framepos_t frame)
420 {
421         double new_unit_position = editor.frame_to_unit (frame);
422         new_unit_position -= _shift;
423         group->move (new_unit_position - unit_position, 0.0);
424         frame_position = frame;
425         unit_position = new_unit_position;
426
427         setup_line_x ();
428 }
429
430 void
431 Marker::reposition ()
432 {
433         set_position (frame_position);
434 }
435
436 void
437 Marker::show ()
438 {
439         _shown = true;
440         
441         group->show ();
442         setup_line ();
443 }
444
445 void
446 Marker::hide ()
447 {
448         _shown = false;
449         
450         group->hide ();
451         setup_line ();
452 }
453
454 void
455 Marker::set_color_rgba (uint32_t c)
456 {
457         _color = c;
458         mark->property_fill_color_rgba() = _color;
459         mark->property_outline_color_rgba() = _color;
460
461         if (_line && !_selected) {
462                 _line->property_color_rgba() = _color;
463
464                 /* For reasons unknown this is necessary to ensure that the line colour
465                    gets updated.
466                 */
467                 _line->hide ();
468                 _line->show ();
469         }
470
471         _name_background->property_fill() = true;
472         _name_background->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (_color, 0x70);
473         _name_background->property_outline_color_rgba() = _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 - 13;
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 - 13;
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::Group& parent, ArdourCanvas::Group& line_parent, guint32 rgba, const string& text,
509                           ARDOUR::TempoSection& temp)
510         : Marker (editor, parent, line_parent, rgba, text, Tempo, 0, false),
511           _tempo (temp)
512 {
513         set_position (_tempo.frame());
514         group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_marker_event), mark, this));
515 }
516
517 TempoMarker::~TempoMarker ()
518 {
519 }
520
521 /***********************************************************************/
522
523 MeterMarker::MeterMarker (PublicEditor& editor, ArdourCanvas::Group& parent, ArdourCanvas::Group& line_parent, guint32 rgba, const string& text,
524                           ARDOUR::MeterSection& m)
525         : Marker (editor, parent, line_parent, rgba, text, Meter, 0, false),
526           _meter (m)
527 {
528         set_position (_meter.frame());
529         group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_meter_marker_event), mark, this));
530 }
531
532 MeterMarker::~MeterMarker ()
533 {
534 }
535