support for glade and new new session dialog
[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     $Id$
19 */
20
21 #include <ardour/tempo.h>
22
23 #include "marker.h"
24 #include "public_editor.h"
25 #include "canvas-simpleline.h"
26 #include "utils.h"
27
28 #include "i18n.h"
29
30 Marker::Marker (PublicEditor& ed, Gnome::Canvas::Group& parent, guint32 rgba, const string& annotation, 
31                 Type type, gint (*callback)(Gnome::Canvas::Item *, GdkEvent *, gpointer), jack_nframes_t frame)
32
33         : editor (ed), _type(type)
34 {
35         double label_offset = 0;
36
37         /* Shapes we use:
38
39           Mark:
40
41            (0,0) -> (6,0)
42              ^        |
43              |        V
44            (0,5)    (6,5)
45                \    / 
46                (3,10)
47
48
49            TempoMark:
50            MeterMark:
51
52                (3,0)
53               /      \
54            (0,5) -> (6,5)
55              ^        |
56              |        V
57            (0,10)<-(6,10)
58
59
60            Start:
61
62            0,0  -> 5,0 
63             |          \
64             |          10,5
65             |          /
66            0,10 -> 5,10
67
68            End:
69
70              5,0 -> 10,0
71              /       |
72            0,5       |
73              \       |
74              5,10 <-10,10
75
76              
77            TransportStart:
78
79              0,0->3,0
80               |    |
81               |    |
82               |    |
83               |   3,8 -> 7,8
84               |           |
85              0,11 ------ 7,11
86
87            TransportEnd:
88
89                       4,0->7,0
90                        |    |
91                        |    |
92                        |    |
93              0,8 ---- 4,8   |
94               |             |
95              0,11 -------- 7,11
96              
97
98              PunchIn:
99
100              0,0 ------> 8,0
101               |       /
102               |      /
103               |    4,5
104               |     |
105               |     |
106               |     |
107              0,11->4,11
108
109              PunchOut
110
111            0,0 -->-8,0
112             \       | 
113              \      |
114              4,5    |
115               |     |
116               |     |
117               |     |
118              4,11->8,11
119              
120            
121         */
122
123         switch (type) {
124         case Mark:
125                 points = new Gnome::Canvas::Points ();
126
127                 points->push_back (Gnome::Art::Point (0.0, 0.0));
128                 points->push_back (Gnome::Art::Point (6.0, 0.0));
129
130                 points->push_back (Gnome::Art::Point (6.0, 5.0));
131                 points->push_back (Gnome::Art::Point (3.0, 10.0));              
132
133                 points->push_back (Gnome::Art::Point (0.0, 5.0));               
134                 points->push_back (Gnome::Art::Point (0.0, 0.0));               
135                 
136                 shift = 3;
137                 label_offset = 8.0;
138                 break;
139
140         case Tempo:
141         case Meter:
142
143                 points = new Gnome::Canvas::Points ();
144                 points->push_back (Gnome::Art::Point (3.0, 0.0));
145                 points->push_back (Gnome::Art::Point (6.0, 5.0));               
146
147                 points->push_back (Gnome::Art::Point (6.0, 10.0));              
148                 points->push_back (Gnome::Art::Point (0.0, 10.0));              
149                 points->push_back (Gnome::Art::Point (0.0, 5.0));               
150                 points->push_back (Gnome::Art::Point (3.0, 0.0));               
151
152                 shift = 3;
153                 label_offset = 8.0;
154                 break;
155
156         case Start:
157                 points = new Gnome::Canvas::Points ();
158                 points->push_back (Gnome::Art::Point (0.0, 0.0)); 
159                 points->push_back (Gnome::Art::Point (5.0, 0.0));               
160                 points->push_back (Gnome::Art::Point (10.0, 5.0));              
161                 points->push_back (Gnome::Art::Point (5.0, 10.0));              
162                 points->push_back (Gnome::Art::Point (0.0, 10.0));              
163                 points->push_back (Gnome::Art::Point (0.0, 0.0));       
164
165                 shift = 10;
166                 label_offset = 12.0;
167                 break;
168
169         case End:
170                 points = new Gnome::Canvas::Points ();
171                 points->push_back (Gnome::Art::Point (5.0, 0.0));
172                 points->push_back (Gnome::Art::Point (10.0, 0.0));              
173                 points->push_back (Gnome::Art::Point (10.0, 10.0));             
174                 points->push_back (Gnome::Art::Point (5.0, 10.0));              
175                 points->push_back (Gnome::Art::Point (0.0, 5.0));               
176                 points->push_back (Gnome::Art::Point (5.0, 0.0));               
177                 
178                 shift = 0;
179                 label_offset = 12.0;
180                 break;
181
182         case LoopStart:
183                 points = new Gnome::Canvas::Points ();
184                 points->push_back (Gnome::Art::Point (0.0, 0.0));
185                 points->push_back (Gnome::Art::Point (4.0, 0.0));               
186                 points->push_back (Gnome::Art::Point (4.0, 8.0));               
187                 points->push_back (Gnome::Art::Point (8.0, 8.0));               
188                 points->push_back (Gnome::Art::Point (8.0, 11.0));              
189                 points->push_back (Gnome::Art::Point (0.0, 11.0));              
190                 points->push_back (Gnome::Art::Point (0.0, 0.0));               
191                 
192                 shift = 0;
193                 label_offset = 11.0;
194                 break;
195
196         case LoopEnd:
197                 points = new Gnome::Canvas::Points ();
198                 points->push_back (Gnome::Art::Point (8.0,  0.0));
199                 points->push_back (Gnome::Art::Point (8.0, 11.0));      
200                 points->push_back (Gnome::Art::Point (0.0, 11.0));              
201                 points->push_back (Gnome::Art::Point (0.0, 8.0));
202                 points->push_back (Gnome::Art::Point (4.0, 8.0));
203                 points->push_back (Gnome::Art::Point (4.0, 0.0));       
204                 points->push_back (Gnome::Art::Point (8.0, 0.0));
205                 
206                 shift = 8;
207                 label_offset = 11.0;
208                 break;
209
210         case  PunchIn:
211                 points = new Gnome::Canvas::Points ();
212                 points->push_back (Gnome::Art::Point (0.0, 0.0));
213                 points->push_back (Gnome::Art::Point (8.0, 0.0));               
214                 points->push_back (Gnome::Art::Point (4.0, 4.0));       
215                 points->push_back (Gnome::Art::Point (4.0, 11.0));      
216                 points->push_back (Gnome::Art::Point (0.0, 11.0));      
217                 points->push_back (Gnome::Art::Point (0.0, 0.0));       
218
219                 shift = 0;
220                 label_offset = 10.0;
221                 break;
222                 
223         case  PunchOut:
224                 points = new Gnome::Canvas::Points ();
225                 points->push_back (Gnome::Art::Point (0.0, 0.0));
226                 points->push_back (Gnome::Art::Point (8.0, 0.0));               
227                 points->push_back (Gnome::Art::Point (8.0, 11.0));              
228                 points->push_back (Gnome::Art::Point (4.0, 11.0));              
229                 points->push_back (Gnome::Art::Point (4.0, 4.0));               
230                 points->push_back (Gnome::Art::Point (0.0, 0.0));               
231
232                 shift = 8;
233                 label_offset = 11.0;
234                 break;
235                 
236         }
237
238         frame_position = frame;
239         unit_position = editor.frame_to_unit (frame);
240
241         /* adjust to properly locate the tip */
242
243         unit_position -= shift;
244
245         group = &parent;
246         group->set_property ("x", unit_position);
247         group->set_property ("y", 1.0);
248         // cerr << "set mark al points, nc = " << points->num_points << endl;
249         mark = new Gnome::Canvas::Polygon (*group);
250         mark->set_property ("points", points);
251         mark->set_property ("fill_color_rgba", rgba);
252         mark->set_property ("outline_color", Gdk::Color ("black"));
253
254         Pango::FontDescription font = get_font_for_style (N_("MarkerText"));
255
256         text = new Gnome::Canvas::Text (*group);
257         text->set_property ("text", annotation.c_str());
258         text->set_property ("x", label_offset);
259         text->set_property ("y", 0.0);
260         text->set_property ("fontdesc", font);
261         text->set_property ("anchor", Gtk::ANCHOR_NW);
262         text->set_property ("fill_color", Gdk::Color ("black"));
263
264         group->set_data ("marker", this);
265         gtk_signal_connect (GTK_OBJECT(group), "event", (GtkSignalFunc) callback, &editor);
266
267         editor.ZoomChanged.connect (mem_fun(*this, &Marker::reposition));
268 }
269
270 Marker::~Marker ()
271 {
272         /* destroying the group destroys its contents */
273         gtk_object_destroy (GTK_OBJECT(group));
274         gnome_canvas_points_unref (points->gobj());
275 }
276
277 void
278 Marker::set_name (const string& name)
279 {
280         text->set_property ("text", name.c_str());
281 }
282
283 void
284 Marker::set_position (jack_nframes_t frame)
285 {
286         double new_unit_position = editor.frame_to_unit (frame);
287         new_unit_position -= shift;
288         group->move (new_unit_position - unit_position, 0.0);
289         frame_position = frame;
290         unit_position = new_unit_position;
291 }
292
293 void
294 Marker::reposition ()
295 {
296         set_position (frame_position);
297 }       
298
299 void
300 Marker::show ()
301 {
302         group->show();
303 }
304
305 void
306 Marker::hide ()
307 {
308         group->hide();
309 }
310
311 void
312 Marker::set_color_rgba (uint32_t color)
313 {
314         mark->set_property ("fill_color_rgba", color);
315 }
316
317 /***********************************************************************/
318
319 TempoMarker::TempoMarker (PublicEditor& editor, Gnome::Canvas::Group& parent, guint32 rgba, const string& text, 
320                           ARDOUR::TempoSection& temp, 
321                           gint (*callback)(Gnome::Canvas::Item *, GdkEvent *, gpointer))
322         : Marker (editor, parent, rgba, text, Tempo, callback, 0),
323           _tempo (temp)
324 {
325         set_position (_tempo.frame());
326         group->set_data ("tempo_marker", this);
327 }
328
329 TempoMarker::~TempoMarker ()
330 {
331 }
332
333 /***********************************************************************/
334
335 MeterMarker::MeterMarker (PublicEditor& editor, Gnome::Canvas::Group& parent, guint32 rgba, const string& text, 
336                           ARDOUR::MeterSection& m, 
337                           gint (*callback)(Gnome::Canvas::Item *, GdkEvent *, gpointer))
338         : Marker (editor, parent, rgba, text, Meter, callback, 0),
339           _meter (m)
340 {
341         set_position (_meter.frame());
342         gtk_object_set_data (GTK_OBJECT(group), "meter_marker", this);
343 }
344
345 MeterMarker::~MeterMarker ()
346 {
347 }