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