merge with master, fixing conflicts in 3 wscript files
[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, guint32 rgba, const string& annotation,
49                 Type type, framepos_t frame, bool handle_events)
50
51         : editor (ed)
52         , _parent (&parent)
53         , _line (0)
54         , _type (type)
55         , _selected (false)
56         , _shown (false)
57         , _line_shown (false)
58         , _canvas_height (0)
59         , _color (rgba)
60         , _left_label_limit (DBL_MAX)
61         , _right_label_limit (DBL_MAX)
62         , _label_offset (0)
63
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,13)
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, 13.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         unit_position -= _shift;
237
238         group = new Group (parent, unit_position, 0);
239
240         _name_background = new ArdourCanvas::SimpleRect (*group);
241         _name_background->property_outline_pixels() = 1;
242
243         /* adjust to properly locate the tip */
244
245         mark = new ArdourCanvas::Polygon (*group);
246         mark->property_points() = *points;
247         set_color_rgba (rgba);
248         mark->property_width_pixels() = 1;
249
250         /* setup name pixbuf sizes */
251         name_font = get_font_for_style (N_("MarkerText"));
252
253         Gtk::Label foo;
254
255         Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (X_("Hg")); /* ascender + descender */
256         int width;
257
258         layout->set_font_description (name_font);
259         Gtkmm2ext::get_ink_pixel_size (layout, width, name_height);
260
261         name_pixbuf = new ArdourCanvas::Pixbuf(*group);
262         name_pixbuf->property_x() = _label_offset;
263         name_pixbuf->property_y() = (13/2) - (name_height/2);
264
265         set_name (annotation.c_str());
266
267         editor.ZoomChanged.connect (sigc::mem_fun (*this, &Marker::reposition));
268
269         mark->set_data ("marker", this);
270         _name_background->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 group;
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 (*group);
316                         _line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_EditPoint.get();
317
318                         _line->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), mark, this));
319                 }
320
321                 /* work out where to start the line from so that it extends from the top of the canvas */
322                 double yo = 0;
323                 double xo = 0;
324
325                 _line->i2w (xo, yo);
326
327                 _line->property_x1() = _shift;
328                 _line->property_x2() = _shift;
329                 _line->property_y1() = -yo; // zero in world coordinates, negative in item/parent coordinate space
330                 _line->property_y2() = -yo + _canvas_height;
331
332                 _line->property_color_rgba() = _selected ? ARDOUR_UI::config()->canvasvar_EditPoint.get() : _color;
333                 _line->raise_to_top ();
334                 _line->show ();
335
336         } else {
337                 if (_line) {
338                         _line->hide ();
339                 }
340         }
341 }
342
343 void
344 Marker::canvas_height_set (double h)
345 {
346         _canvas_height = h;
347         setup_line ();
348 }
349
350 ArdourCanvas::Item&
351 Marker::the_item() const
352 {
353         return *mark;
354 }
355
356 void
357 Marker::set_name (const string& new_name)
358 {
359         _name = new_name;
360
361         setup_name_display ();
362 }
363
364 /** @return true if our label is on the left of the mark, otherwise false */
365 bool
366 Marker::label_on_left () const
367 {
368         return (_type == SessionEnd || _type == RangeEnd || _type == LoopEnd || _type == PunchOut);
369 }
370
371 void
372 Marker::setup_name_display ()
373 {
374         double limit = DBL_MAX;
375
376         if (label_on_left ()) {
377                 limit = _left_label_limit;
378         } else {
379                 limit = _right_label_limit;
380         }
381
382         /* Work out how wide the name can be */
383         int name_width = min ((double) pixel_width (_name, name_font) + 2, limit);
384         if (name_width == 0) {
385                 name_width = 1;
386         }
387
388         if (label_on_left ()) {
389                 name_pixbuf->property_x() = -name_width;
390         }
391
392         name_pixbuf->property_pixbuf() = pixbuf_from_string (_name, name_font, name_width, name_height, Gdk::Color ("#000000"));
393
394         if (label_on_left ()) {
395                 _name_background->property_x1() = name_pixbuf->property_x() - 2;
396                 _name_background->property_x2() = name_pixbuf->property_x() + name_width + _shift;
397         } else {
398                 _name_background->property_x1() = name_pixbuf->property_x() - _label_offset + 2;
399                 _name_background->property_x2() = name_pixbuf->property_x() + name_width;
400         }
401
402         _name_background->property_y1() = 0;
403         _name_background->property_y2() = 13;
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
416 void
417 Marker::reposition ()
418 {
419         set_position (frame_position);
420 }
421
422 void
423 Marker::show ()
424 {
425         _shown = true;
426
427         group->show ();
428         setup_line ();
429 }
430
431 void
432 Marker::hide ()
433 {
434         _shown = false;
435
436         group->hide ();
437         setup_line ();
438 }
439
440 void
441 Marker::set_color_rgba (uint32_t c)
442 {
443         _color = c;
444         mark->property_fill_color_rgba() = _color;
445         mark->property_outline_color_rgba() = _color;
446
447         if (_line && !_selected) {
448                 _line->property_color_rgba() = _color;
449         }
450
451         _name_background->property_fill() = true;
452         _name_background->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (_color, 0x70);
453         _name_background->property_outline_color_rgba() = _color;
454 }
455
456 /** Set the number of pixels that are available for a label to the left of the centre of this marker */
457 void
458 Marker::set_left_label_limit (double p)
459 {
460         /* Account for the size of the marker */
461         _left_label_limit = p - 13;
462         if (_left_label_limit < 0) {
463                 _left_label_limit = 0;
464         }
465
466         if (label_on_left ()) {
467                 setup_name_display ();
468         }
469 }
470
471 /** Set the number of pixels that are available for a label to the right of the centre of this marker */
472 void
473 Marker::set_right_label_limit (double p)
474 {
475         /* Account for the size of the marker */
476         _right_label_limit = p - 13;
477         if (_right_label_limit < 0) {
478                 _right_label_limit = 0;
479         }
480
481         if (!label_on_left ()) {
482                 setup_name_display ();
483         }
484 }
485
486 /***********************************************************************/
487
488 TempoMarker::TempoMarker (PublicEditor& editor, ArdourCanvas::Group& parent, guint32 rgba, const string& text,
489                           ARDOUR::TempoSection& temp)
490         : Marker (editor, parent, rgba, text, Tempo, 0, false),
491           _tempo (temp)
492 {
493         set_position (_tempo.frame());
494         group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_marker_event), mark, this));
495 }
496
497 TempoMarker::~TempoMarker ()
498 {
499 }
500
501 /***********************************************************************/
502
503 MeterMarker::MeterMarker (PublicEditor& editor, ArdourCanvas::Group& parent, guint32 rgba, const string& text,
504                           ARDOUR::MeterSection& m)
505         : Marker (editor, parent, rgba, text, Meter, 0, false),
506           _meter (m)
507 {
508         set_position (_meter.frame());
509         group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_meter_marker_event), mark, this));
510 }
511
512 MeterMarker::~MeterMarker ()
513 {
514 }
515