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