Fix broken meter style on MIDI tracks.
[ardour2.git] / gtk2_ardour / marker.cc
blobf1797c39c9e42b4bf0bad0263cdf8396fc0e01b8
1 /*
2 Copyright (C) 2001 Paul Davis
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.
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.
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.
20 #include <sigc++/bind.h>
21 #include "ardour/tempo.h"
23 #include "ardour_ui.h"
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.
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"
38 #include <gtkmm2ext/utils.h>
40 #include "i18n.h"
42 using namespace std;
43 using namespace ARDOUR;
44 using namespace Gtkmm2ext;
46 PBD::Signal1<void,Marker*> Marker::CatchDeletion;
48 Marker::Marker (PublicEditor& ed, ArdourCanvas::Group& parent, guint32 rgba, const string& annotation,
49 Type type, framepos_t frame, bool handle_events)
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)
65 /* Shapes we use:
67 Mark:
69 (0,0) -> (6,0)
70 ^ |
71 | V
72 (0,5) (6,5)
73 \ /
74 (3,13)
77 TempoMark:
78 MeterMark:
80 (3,0)
81 / \
82 (0,5) -> (6,5)
83 ^ |
84 | V
85 (0,10)<-(6,10)
88 Start:
90 0,0\
91 | \
92 | \ 6,6
93 | /
94 | /
95 0,12
97 End:
99 /12,0
102 6,6 |
106 12,12
108 PunchIn:
110 0,0 ------> 13,0
117 0,13
119 PunchOut
121 0,0 -->-13,0
128 13,13
133 switch (type) {
134 case Mark:
135 points = new ArdourCanvas::Points ();
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));
144 _shift = 3;
145 _label_offset = 8.0;
146 break;
148 case Tempo:
149 case Meter:
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));
159 _shift = 3;
160 _label_offset = 8.0;
161 break;
163 case SessionStart:
164 case RangeStart:
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));
172 _shift = 0;
173 _label_offset = 13.0;
174 break;
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));
184 _shift = 13;
185 _label_offset = 6.0;
186 break;
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));
195 _shift = 0;
196 _label_offset = 12.0;
197 break;
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));
206 _shift = 13;
207 _label_offset = 0.0;
208 break;
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));
217 _shift = 0;
218 _label_offset = 13.0;
219 break;
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));
228 _shift = 13;
229 _label_offset = 0.0;
230 break;
234 frame_position = frame;
235 unit_position = editor.frame_to_unit (frame);
236 unit_position -= _shift;
238 group = new Group (parent, unit_position, 1.0);
240 _name_background = new ArdourCanvas::SimpleRect (*group);
241 _name_background->property_outline_pixels() = 1;
243 /* adjust to properly locate the tip */
245 mark = new Polygon (*group);
246 mark->property_points() = *points;
247 set_color_rgba (rgba);
248 mark->property_width_pixels() = 1;
250 /* setup name pixbuf sizes */
251 name_font = get_font_for_style (N_("MarkerText"));
253 Gtk::Label foo;
255 Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (X_("Hg")); /* ascender + descender */
256 int width;
258 layout->set_font_description (*name_font);
259 Gtkmm2ext::get_ink_pixel_size (layout, width, name_height);
261 name_pixbuf = new ArdourCanvas::Pixbuf(*group);
262 name_pixbuf->property_x() = _label_offset;
263 name_pixbuf->property_y() = (13/2) - (name_height/2);
265 set_name (annotation.c_str());
267 editor.ZoomChanged.connect (sigc::mem_fun (*this, &Marker::reposition));
269 mark->set_data ("marker", this);
270 _name_background->set_data ("marker", this);
272 if (handle_events) {
273 group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), mark, this));
279 Marker::~Marker ()
281 CatchDeletion (this); /* EMIT SIGNAL */
283 /* destroying the parent group destroys its contents, namely any polygons etc. that we added */
284 delete group;
285 delete _line;
288 void Marker::reparent(ArdourCanvas::Group & parent)
290 group->reparent (parent);
291 _parent = &parent;
294 void
295 Marker::set_selected (bool s)
297 _selected = s;
298 setup_line ();
301 void
302 Marker::set_show_line (bool s)
304 _line_shown = s;
305 setup_line ();
308 void
309 Marker::setup_line ()
311 if (_shown && (_selected || _line_shown)) {
313 if (_line == 0) {
315 _line = new ArdourCanvas::SimpleLine (*group);
316 _line->property_color_rgba() = ARDOUR_UI::config()->canvasvar_EditPoint.get();
318 _line->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), mark, this));
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;
325 _line->i2w (xo, yo);
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;
332 _line->property_color_rgba() = _selected ? ARDOUR_UI::config()->canvasvar_EditPoint.get() : _color;
333 _line->raise_to_top ();
334 _line->show ();
336 } else {
337 if (_line) {
338 _line->hide ();
343 void
344 Marker::canvas_height_set (double h)
346 _canvas_height = h;
347 setup_line ();
350 ArdourCanvas::Item&
351 Marker::the_item() const
353 return *mark;
356 void
357 Marker::set_name (const string& new_name)
359 _name = new_name;
361 setup_name_display ();
364 /** @return true if our label is on the left of the mark, otherwise false */
365 bool
366 Marker::label_on_left () const
368 return (_type == SessionEnd || _type == RangeEnd || _type == LoopEnd || _type == PunchOut);
371 void
372 Marker::setup_name_display ()
374 double limit = DBL_MAX;
376 if (label_on_left ()) {
377 limit = _left_label_limit;
378 } else {
379 limit = _right_label_limit;
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;
388 if (label_on_left ()) {
389 name_pixbuf->property_x() = -name_width;
392 name_pixbuf->property_pixbuf() = pixbuf_from_string (_name, name_font, name_width, name_height, Gdk::Color ("#000000"));
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;
402 _name_background->property_y1() = 0;
403 _name_background->property_y2() = 13;
406 void
407 Marker::set_position (framepos_t frame)
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;
416 void
417 Marker::reposition ()
419 set_position (frame_position);
422 void
423 Marker::show ()
425 _shown = true;
427 group->show ();
428 setup_line ();
431 void
432 Marker::hide ()
434 _shown = false;
436 group->hide ();
437 setup_line ();
440 void
441 Marker::set_color_rgba (uint32_t c)
443 _color = c;
444 mark->property_fill_color_rgba() = _color;
445 mark->property_outline_color_rgba() = _color;
447 if (_line && !_selected) {
448 _line->property_color_rgba() = _color;
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;
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)
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;
466 if (label_on_left ()) {
467 setup_name_display ();
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)
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;
481 if (!label_on_left ()) {
482 setup_name_display ();
486 /***********************************************************************/
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)
493 set_position (_tempo.frame());
494 group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_marker_event), mark, this));
497 TempoMarker::~TempoMarker ()
501 /***********************************************************************/
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)
508 set_position (_meter.frame());
509 group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_meter_marker_event), mark, this));
512 MeterMarker::~MeterMarker ()