5f998ada9fa5a0602c39cb874a790d9b8e5ffb60
[ardour.git] / gtk2_ardour / editor_tempodisplay.cc
1 /*
2     Copyright (C) 2002 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 <cstdio> // for sprintf, grrr 
22 #include <cstdlib>
23 #include <cmath>
24 #include <string>
25 #include <climits>
26
27 #include <libgnomecanvasmm.h>
28
29 #include <pbd/error.h>
30
31 #include <gtkmm2ext/utils.h>
32 #include <gtkmm2ext/gtk_ui.h>
33
34 #include <ardour/session.h>
35 #include <ardour/tempo.h>
36 #include <gtkmm2ext/doi.h>
37 #include <gtkmm2ext/utils.h>
38
39 #include "editor.h"
40 #include "marker.h"
41 #include "simpleline.h"
42 #include "tempo_dialog.h"
43 #include "rgb_macros.h"
44 #include "gui_thread.h"
45 #include "color.h"
46
47 #include "i18n.h"
48
49 using namespace std;
50 using namespace sigc;
51 using namespace ARDOUR;
52 using namespace PBD;
53 using namespace Gtk;
54 using namespace Gtkmm2ext;
55 using namespace Editing;
56
57 void
58 Editor::remove_metric_marks ()
59 {
60         /* don't delete these while handling events, just punt till the GUI is idle */
61
62         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
63                 delete_when_idle (*x);
64         }
65         metric_marks.clear ();
66 }       
67
68 void
69 Editor::draw_metric_marks (const Metrics& metrics)
70 {
71         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
72                 const MeterSection *ms;
73                 const TempoSection *ts;
74                 char buf[64];
75                 
76                 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
77                         snprintf (buf, sizeof(buf), "%g/%g", ms->beats_per_bar(), ms->note_divisor ());
78                         metric_marks.push_back (new MeterMarker (*this, *meter_group, color_map[cMeterMarker], buf, 
79                                                                  *(const_cast<MeterSection*>(ms))));
80                 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
81                         snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
82                         metric_marks.push_back (new TempoMarker (*this, *tempo_group, color_map[cTempoMarker], buf, 
83                                                                  *(const_cast<TempoSection*>(ts))));
84                 }
85                 
86         }
87 }
88
89 void
90 Editor::tempo_map_changed (Change ignored)
91 {
92         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::tempo_map_changed), ignored));
93         
94         if (current_bbt_points) {
95                 delete current_bbt_points;
96                 current_bbt_points = 0;
97         }
98
99         if (session) {
100                 current_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
101         } else {
102                 current_bbt_points = 0;
103         }
104
105         redisplay_tempo ();
106 }
107
108 void
109 Editor::redisplay_tempo ()
110 {
111         update_tempo_based_rulers ();
112
113         remove_metric_marks (); 
114         hide_measures ();
115
116         if (session && current_bbt_points) {
117                 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
118                 draw_measures ();
119         }
120         
121 }
122
123 void
124 Editor::hide_measures ()
125 {
126         for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
127                 (*i)->hide();
128                 free_measure_lines.push_back (*i);
129         }
130         used_measure_lines.clear ();
131 }
132
133 ArdourCanvas::SimpleLine *
134 Editor::get_time_line ()
135 {
136          ArdourCanvas::SimpleLine *line;
137
138         if (free_measure_lines.empty()) {
139                 line = new ArdourCanvas::SimpleLine (*time_line_group);
140                 used_measure_lines.push_back (line);
141         } else {
142                 line = free_measure_lines.front();
143                 free_measure_lines.erase (free_measure_lines.begin());
144                 used_measure_lines.push_back (line);
145         }
146
147         return line;
148 }
149
150 void
151 Editor::draw_measures ()
152 {
153         if (session == 0 || _show_measures == false) {
154                 return;
155         }
156
157         TempoMap::BBTPointList::iterator i;
158         TempoMap::BBTPointList *all_bbt_points;
159         ArdourCanvas::SimpleLine *line;
160         gdouble xpos, last_xpos;
161         uint32_t cnt;
162         uint32_t color;
163
164         if (current_bbt_points == 0 || current_bbt_points->empty()) {
165                 return;
166         }
167
168         all_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
169
170         cnt = 0;
171         last_xpos = 0;
172
173         /* get the first bar spacing */
174
175         gdouble last_beat = DBL_MAX;
176         gdouble beat_spacing = 0;
177
178         for (i = all_bbt_points->begin(); i != all_bbt_points->end() && beat_spacing == 0; ++i) {
179                 TempoMap::BBTPoint& p = (*i);
180
181                 switch (p.type) {
182                 case TempoMap::Bar:
183                         break;
184
185                 case TempoMap::Beat:
186                         xpos = frame_to_unit (p.frame);
187                         if (last_beat < xpos) {
188                                 beat_spacing = xpos - last_beat;
189                         }
190                         last_beat = xpos;
191                 }
192         }
193
194         double x1, x2, y1, y2;
195         track_canvas.get_scroll_region (x1, y1, x2, y2);
196
197         for (i = all_bbt_points->begin(); i != all_bbt_points->end(); ++i) {
198
199                 TempoMap::BBTPoint& p = (*i);
200
201                 switch (p.type) {
202                 case TempoMap::Bar:
203                         break;
204
205                 case TempoMap::Beat:
206                         xpos = frame_to_unit (p.frame);
207                         
208                         if (p.beat == 1) {
209                                 color = color_map[cMeasureLineBeat];
210                         } else {
211                                 color = color_map[cMeasureLineBar];
212
213                                 /* only draw beat lines if the gaps between beats
214                                    are large.
215                                 */
216
217                                 if (beat_spacing < 25.0) {
218                                         break;
219                                 }
220                         }
221                         
222                         if (cnt == 0 || xpos - last_xpos > 4.0) {
223                                 line = get_time_line ();
224                                 line->property_x1() = xpos;
225                                 line->property_x2() = xpos;
226                                 line->property_y2() = y2;
227                                 line->property_color_rgba() = color;
228                                 line->raise_to_top();
229                                 line->show();
230                                 last_xpos = xpos;       
231                                 ++cnt;
232                         } 
233                         break;
234                 }
235         }
236
237         delete all_bbt_points;
238
239         /* the cursors are always on top of everything */
240
241         cursor_group->raise_to_top();
242         time_line_group->lower_to_bottom();
243 }
244
245 void
246 Editor::mouse_add_new_tempo_event (jack_nframes_t frame)
247 {
248         if (session == 0) {
249                 return;
250         }
251
252         TempoMap& map(session->tempo_map());
253         TempoDialog tempo_dialog (map, frame, _("add"));
254         
255         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
256         tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
257
258         ensure_float (tempo_dialog);
259
260         switch (tempo_dialog.run()) {
261         case RESPONSE_ACCEPT:
262                 break;
263         default:
264                 return;
265         }
266
267         double bpm = 0;
268         BBT_Time requested;
269         
270         bpm = tempo_dialog.get_bpm ();
271         bpm = max (0.01, bpm);
272         
273         tempo_dialog.get_bbt_time (requested);
274         
275         begin_reversible_command (_("add tempo mark"));
276         session->add_undo (map.get_memento());
277         map.add_tempo (Tempo (bpm), requested);
278         session->add_redo_no_execute (map.get_memento());
279         commit_reversible_command ();
280         
281         map.dump (cerr);
282 }
283
284 void
285 Editor::mouse_add_new_meter_event (jack_nframes_t frame)
286 {
287         if (session == 0) {
288                 return;
289         }
290
291
292         TempoMap& map(session->tempo_map());
293         MeterDialog meter_dialog (map, frame, _("add"));
294
295         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
296         meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
297
298         ensure_float (meter_dialog);
299         
300         switch (meter_dialog.run ()) {
301         case RESPONSE_ACCEPT:
302                 break;
303         default:
304                 return;
305         }
306
307         double bpb = meter_dialog.get_bpb ();
308         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
309         
310         double note_type = meter_dialog.get_note_type ();
311         BBT_Time requested;
312         
313         meter_dialog.get_bbt_time (requested);
314         
315         begin_reversible_command (_("add meter mark"));
316         session->add_undo (map.get_memento());
317         map.add_meter (Meter (bpb, note_type), requested);
318         session->add_redo_no_execute (map.get_memento());
319         commit_reversible_command ();
320         
321         map.dump (cerr);
322 }
323
324 void
325 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
326 {
327         Marker* marker;
328         TempoMarker* tempo_marker;
329
330         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
331                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
332                 /*NOTREACHED*/
333         }
334
335         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
336                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
337                 /*NOTREACHED*/
338         }               
339
340         if (tempo_marker->tempo().movable()) {
341           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
342         }
343 }
344
345 void
346 Editor::edit_meter_section (MeterSection* section)
347 {
348         MeterDialog meter_dialog (*section, _("done"));
349
350         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
351
352         ensure_float (meter_dialog);
353
354         switch (meter_dialog.run()) {
355         case RESPONSE_ACCEPT:
356                 break;
357         default:
358                 return;
359         }
360
361         double bpb = meter_dialog.get_bpb ();
362         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
363         
364         double note_type = meter_dialog.get_note_type ();
365
366         begin_reversible_command (_("replace tempo mark"));
367         session->add_undo (session->tempo_map().get_memento());
368         session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
369         session->add_redo_no_execute (session->tempo_map().get_memento());
370         commit_reversible_command ();
371 }
372
373 void
374 Editor::edit_tempo_section (TempoSection* section)
375 {
376         TempoDialog tempo_dialog (*section, _("done"));
377
378         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
379
380         ensure_float (tempo_dialog);
381         
382         switch (tempo_dialog.run ()) {
383         case RESPONSE_ACCEPT:
384                 break;
385         default:
386                 return;
387         }
388
389         double bpm = tempo_dialog.get_bpm ();
390         BBT_Time when;
391         tempo_dialog.get_bbt_time(when);
392         bpm = max (0.01, bpm);
393         
394         begin_reversible_command (_("replace tempo mark"));
395         session->add_undo (session->tempo_map().get_memento());
396         session->tempo_map().replace_tempo (*section, Tempo (bpm));
397         session->tempo_map().move_tempo (*section, when);
398         session->add_redo_no_execute (session->tempo_map().get_memento());
399         commit_reversible_command ();
400 }
401
402 void
403 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
404 {
405         Marker* marker;
406         TempoMarker* tempo_marker;
407
408         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
409                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
410                 /*NOTREACHED*/
411         }
412
413         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
414                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
415                 /*NOTREACHED*/
416         }               
417
418         edit_tempo_section (&tempo_marker->tempo());
419 }
420
421 void
422 Editor::edit_meter_marker (ArdourCanvas::Item *item)
423 {
424         Marker* marker;
425         MeterMarker* meter_marker;
426
427         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
428                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
429                 /*NOTREACHED*/
430         }
431
432         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
433                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
434                 /*NOTREACHED*/
435         }               
436         
437         edit_meter_section (&meter_marker->meter());
438 }
439
440 gint
441 Editor::real_remove_tempo_marker (TempoSection *section)
442 {
443         begin_reversible_command (_("remove tempo mark"));
444         session->add_undo (session->tempo_map().get_memento());
445         session->tempo_map().remove_tempo (*section);
446         session->add_redo_no_execute (session->tempo_map().get_memento());
447         commit_reversible_command ();
448
449         return FALSE;
450 }
451
452 void
453 Editor::remove_meter_marker (ArdourCanvas::Item* item)
454 {
455         Marker* marker;
456         MeterMarker* meter_marker;
457
458         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
459                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
460                 /*NOTREACHED*/
461         }
462
463         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
464                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
465                 /*NOTREACHED*/
466         }               
467
468         if (meter_marker->meter().movable()) {
469           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
470         }
471 }
472
473 gint
474 Editor::real_remove_meter_marker (MeterSection *section)
475 {
476         begin_reversible_command (_("remove tempo mark"));
477         session->add_undo (session->tempo_map().get_memento());
478         session->tempo_map().remove_meter (*section);
479         session->add_redo_no_execute (session->tempo_map().get_memento());
480         commit_reversible_command ();
481         return FALSE;
482 }