incomplete changes based on karsten's megapatch
[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 <libgnomecanvas/libgnomecanvas.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, GNOME_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, GNOME_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 (mem_fun(*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                 gnome_canvas_item_hide (*i);
124                 free_measure_lines.push_back (*i);
125         }
126         used_measure_lines.clear ();
127 }
128
129 GnomeCanvasItem *
130 Editor::get_time_line ()
131 {
132         GnomeCanvasItem *line;
133
134         if (free_measure_lines.empty()) {
135                 line = gnome_canvas_item_new (GNOME_CANVAS_GROUP(time_line_group),
136                                             gnome_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         GnomeCanvasItem *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                                 gnome_canvas_item_raise_to_top (line);
227                                 gnome_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         cursor_group->raise_to_top();
240         time_line_group->lower_to_bottom();
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.signal_activate().connect (bind (mem_fun (tempo_dialog, &ArdourDialog::stop), 0));
255         tempo_dialog.ok_button.signal_clicked().connect (bind (mem_fun (tempo_dialog, &ArdourDialog::stop), 0));
256         tempo_dialog.cancel_button.signal_clicked().connect (bind (mem_fun (tempo_dialog, &ArdourDialog::stop), -1));
257
258         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
259         // GTK2FIX
260         // tempo_dialog.realize ();
261         // tempo_dialog.get_window()->set_decorations (Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
262
263         ensure_float (tempo_dialog);
264
265         tempo_dialog.run();
266
267         if (tempo_dialog.run_status() == 0) {
268                 
269                 double bpm = 0;
270                 BBT_Time requested;
271
272                 bpm = tempo_dialog.get_bpm ();
273                 bpm = max (0.01, bpm);
274
275                 tempo_dialog.get_bbt_time (requested);
276
277                 begin_reversible_command (_("add tempo mark"));
278                 session->add_undo (map.get_memento());
279                 map.add_tempo (Tempo (bpm), requested);
280                 session->add_redo_no_execute (map.get_memento());
281                 commit_reversible_command ();
282
283                 map.dump (cerr);
284         }
285 }
286
287 void
288 Editor::mouse_add_new_meter_event (jack_nframes_t frame)
289 {
290         if (session == 0) {
291                 return;
292         }
293
294
295         TempoMap& map(session->tempo_map());
296         MeterDialog meter_dialog (map, frame, _("add"));
297
298         meter_dialog.ok_button.signal_clicked().connect (bind (mem_fun (meter_dialog, &ArdourDialog::stop), 0));
299         meter_dialog.cancel_button.signal_clicked().connect (bind (mem_fun (meter_dialog, &ArdourDialog::stop), -1));
300
301         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
302         // GTK2FIX
303         // meter_dialog.realize ();
304         // meter_dialog.get_window()->set_decorations (Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
305
306         ensure_float (meter_dialog);
307         
308         meter_dialog.run ();
309         
310         if (meter_dialog.run_status() == 0) {
311                 
312                 double bpb = meter_dialog.get_bpb ();
313                 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
314
315                 double note_type = meter_dialog.get_note_type ();
316                 BBT_Time requested;
317
318                 meter_dialog.get_bbt_time (requested);
319
320                 begin_reversible_command (_("add meter mark"));
321                 session->add_undo (map.get_memento());
322                 map.add_meter (Meter (bpb, note_type), requested);
323                 session->add_redo_no_execute (map.get_memento());
324                 commit_reversible_command ();
325
326                 map.dump (cerr);
327         }
328 }
329
330 void
331 Editor::remove_tempo_marker (GnomeCanvasItem* item)
332 {
333         Marker* marker;
334         TempoMarker* tempo_marker;
335
336         if ((marker = reinterpret_cast<Marker *> (gtk_object_get_data (GTK_OBJECT(item), "marker"))) == 0) {
337                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
338                 /*NOTREACHED*/
339         }
340
341         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
342                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
343                 /*NOTREACHED*/
344         }               
345
346         if (tempo_marker->tempo().movable()) {
347           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
348         }
349 }
350
351 void
352 Editor::edit_meter_section (MeterSection* section)
353 {
354         MeterDialog meter_dialog (*section, _("done"));
355
356         meter_dialog.ok_button.signal_clicked().connect (bind (mem_fun (meter_dialog, &ArdourDialog::stop), 0));
357         meter_dialog.cancel_button.signal_clicked().connect (bind (mem_fun (meter_dialog, &ArdourDialog::stop), -1));
358
359         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
360         // GTK2FIX
361         // meter_dialog.realize ();
362         // meter_dialog.get_window()->set_decorations (Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
363
364         ensure_float (meter_dialog);
365
366         meter_dialog.run ();
367
368         if (meter_dialog.run_status() == 0) {
369
370                 double bpb = meter_dialog.get_bpb ();
371                 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
372
373                 double note_type = meter_dialog.get_note_type ();
374
375                 begin_reversible_command (_("replace tempo mark"));
376                 session->add_undo (session->tempo_map().get_memento());
377                 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
378                 session->add_redo_no_execute (session->tempo_map().get_memento());
379                 commit_reversible_command ();
380         }
381 }
382
383 void
384 Editor::edit_tempo_section (TempoSection* section)
385 {
386         TempoDialog tempo_dialog (*section, _("done"));
387
388         tempo_dialog.bpm_entry.signal_activate().connect (bind (mem_fun (tempo_dialog, &ArdourDialog::stop), 0));
389         tempo_dialog.ok_button.signal_clicked().connect (bind (mem_fun (tempo_dialog, &ArdourDialog::stop), 0));
390         tempo_dialog.cancel_button.signal_clicked().connect (bind (mem_fun (tempo_dialog, &ArdourDialog::stop), -1));
391
392         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
393         // GTK2FIX
394         // tempo_dialog.realize ();
395         // tempo_dialog.get_window()->set_decorations (Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
396
397         ensure_float (tempo_dialog);
398         
399         tempo_dialog.run ();
400
401         if (tempo_dialog.run_status() == 0) {
402
403                 double bpm = tempo_dialog.get_bpm ();
404                 BBT_Time when;
405                 tempo_dialog.get_bbt_time(when);
406                 bpm = max (0.01, bpm);
407
408                 begin_reversible_command (_("replace tempo mark"));
409                 session->add_undo (session->tempo_map().get_memento());
410                 session->tempo_map().replace_tempo (*section, Tempo (bpm));
411                 session->tempo_map().move_tempo (*section, when);
412                 session->add_redo_no_execute (session->tempo_map().get_memento());
413                 commit_reversible_command ();
414         }
415 }
416
417 void
418 Editor::edit_tempo_marker (GnomeCanvasItem *item)
419 {
420         Marker* marker;
421         TempoMarker* tempo_marker;
422
423         if ((marker = reinterpret_cast<Marker *> (gtk_object_get_data (GTK_OBJECT(item), "marker"))) == 0) {
424                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
425                 /*NOTREACHED*/
426         }
427
428         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
429                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
430                 /*NOTREACHED*/
431         }               
432
433         edit_tempo_section (&tempo_marker->tempo());
434 }
435
436 void
437 Editor::edit_meter_marker (GnomeCanvasItem *item)
438 {
439         Marker* marker;
440         MeterMarker* meter_marker;
441
442         if ((marker = reinterpret_cast<Marker *> (gtk_object_get_data (GTK_OBJECT(item), "marker"))) == 0) {
443                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
444                 /*NOTREACHED*/
445         }
446
447         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
448                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
449                 /*NOTREACHED*/
450         }               
451         
452         edit_meter_section (&meter_marker->meter());
453 }
454
455 gint
456 Editor::real_remove_tempo_marker (TempoSection *section)
457 {
458         begin_reversible_command (_("remove tempo mark"));
459         session->add_undo (session->tempo_map().get_memento());
460         session->tempo_map().remove_tempo (*section);
461         session->add_redo_no_execute (session->tempo_map().get_memento());
462         commit_reversible_command ();
463
464         return FALSE;
465 }
466
467 void
468 Editor::remove_meter_marker (GnomeCanvasItem* item)
469 {
470         Marker* marker;
471         MeterMarker* meter_marker;
472
473         if ((marker = reinterpret_cast<Marker *> (gtk_object_get_data (GTK_OBJECT(item), "marker"))) == 0) {
474                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
475                 /*NOTREACHED*/
476         }
477
478         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
479                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
480                 /*NOTREACHED*/
481         }               
482
483         if (meter_marker->meter().movable()) {
484           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
485         }
486 }
487
488 gint
489 Editor::real_remove_meter_marker (MeterSection *section)
490 {
491         begin_reversible_command (_("remove tempo mark"));
492         session->add_undo (session->tempo_map().get_memento());
493         session->tempo_map().remove_meter (*section);
494         session->add_redo_no_execute (session->tempo_map().get_memento());
495         commit_reversible_command ();
496         return FALSE;
497 }