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