c9bd50607b8289be03490cd50188331497353d83
[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 */
19
20 #include <cstdio> // for sprintf, grrr 
21 #include <cstdlib>
22 #include <cmath>
23 #include <string>
24 #include <climits>
25
26 #include <libgnomecanvasmm.h>
27
28 #include <pbd/error.h>
29 #include <pbd/memento_command.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 "time_axis_view.h"
46 #include "ardour_ui.h"
47
48 #include "i18n.h"
49
50 using namespace std;
51 using namespace sigc;
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Gtk;
55 using namespace Gtkmm2ext;
56 using namespace Editing;
57
58 void
59 Editor::remove_metric_marks ()
60 {
61         /* don't delete these while handling events, just punt till the GUI is idle */
62
63         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
64                 delete_when_idle (*x);
65         }
66         metric_marks.clear ();
67 }       
68
69 void
70 Editor::draw_metric_marks (const Metrics& metrics)
71 {
72
73         const MeterSection *ms;
74         const TempoSection *ts;
75         char buf[64];
76         
77         remove_metric_marks ();
78         
79         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
80                 
81                 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
82                         snprintf (buf, sizeof(buf), "%g/%g", ms->beats_per_bar(), ms->note_divisor ());
83                         metric_marks.push_back (new MeterMarker (*this, *meter_group, ARDOUR_UI::config()->canvasvar_MeterMarker.get(), buf, 
84                                                                  *(const_cast<MeterSection*>(ms))));
85                 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
86                         snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
87                         metric_marks.push_back (new TempoMarker (*this, *tempo_group, ARDOUR_UI::config()->canvasvar_TempoMarker.get(), buf, 
88                                                                  *(const_cast<TempoSection*>(ts))));
89                 }
90                 
91         }
92
93 }
94
95 void
96 Editor::tempo_map_changed (Change ignored)
97 {
98         if (!session) {
99                 return;
100         }
101
102         ENSURE_GUI_THREAD(bind (mem_fun (*this, &Editor::tempo_map_changed), ignored));
103
104         compute_current_bbt_points(leftmost_frame, leftmost_frame + (nframes_t)(edit_packer.get_width() * frames_per_unit));
105         session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
106         update_tempo_based_rulers ();
107         if (tempo_map_change_idle_handler_id  < 0) {
108                         tempo_map_change_idle_handler_id = Glib::signal_idle().connect (mem_fun (*this, &Editor::redraw_measures));
109         }
110 }
111
112 void
113 Editor::redisplay_tempo (bool immediate_redraw)
114 {
115         if (!session) {
116                 return;
117         }
118         
119         compute_current_bbt_points (leftmost_frame, leftmost_frame + (nframes_t)(edit_packer.get_width() * frames_per_unit)); // redraw rulers and measures
120
121         if (immediate_redraw) {
122
123                 hide_measures ();
124
125                 if (current_bbt_points) {
126                         draw_measures ();
127                 }
128
129         } else if (tempo_map_change_idle_handler_id  < 0) {
130
131                 tempo_map_change_idle_handler_id = Glib::signal_idle().connect (mem_fun (*this, &Editor::redraw_measures));
132
133         }
134
135         update_tempo_based_rulers ();
136 }
137
138 void
139 Editor::compute_current_bbt_points (nframes_t leftmost, nframes_t rightmost)
140 {
141         if (!session) {
142                 return;
143         }
144
145         BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
146
147         session->bbt_time(leftmost, previous_beat);
148         session->bbt_time(rightmost, next_beat);
149
150         if (previous_beat.beats > 1) {
151                 previous_beat.beats -= 1;
152         } else if (previous_beat.bars > 1) {
153                 previous_beat.bars--;
154                 previous_beat.beats += 1;
155         }
156         previous_beat.ticks = 0;
157
158         if (session->tempo_map().meter_at(rightmost).beats_per_bar () > next_beat.beats + 1) {
159                 next_beat.beats += 1;
160         } else {
161                 next_beat.bars += 1;
162                 next_beat.beats = 1;
163         }
164         next_beat.ticks = 0;
165         
166         if (current_bbt_points) {
167                 delete current_bbt_points;
168                 current_bbt_points = 0;
169         }
170
171         current_bbt_points = session->tempo_map().get_points (session->tempo_map().frame_time (previous_beat), session->tempo_map().frame_time (next_beat) + 1);
172 }
173
174 void
175 Editor::hide_measures ()
176 {
177         tempo_lines->hide();
178         marker_tempo_lines->hide();
179 }
180
181 bool
182 Editor::redraw_measures ()
183 {
184         hide_measures ();
185         draw_measures ();
186         tempo_map_change_idle_handler_id = -1;
187         return false;
188 }
189
190 void
191 Editor::draw_measures ()
192 {
193         if (session == 0 || _show_measures == false || 
194             !current_bbt_points || current_bbt_points->empty()) {
195                 return;
196         }
197
198         tempo_lines->draw(*current_bbt_points, frames_per_unit);
199         marker_tempo_lines->draw(*current_bbt_points, frames_per_unit);
200
201         /*time_line_group->raise_to_top();
202         time_line_group->lower(1);*/
203         marker_time_line_group->raise_to_top();
204         //marker_time_line_group->lower(1);
205         
206         /* the cursors are always on top of everything */
207         cursor_group->raise_to_top();
208
209         return;
210 }
211
212 void
213 Editor::mouse_add_new_tempo_event (nframes_t frame)
214 {
215         if (session == 0) {
216                 return;
217         }
218
219         TempoMap& map(session->tempo_map());
220         TempoDialog tempo_dialog (map, frame, _("add"));
221         
222         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
223         //this causes compiz to display no border.
224         //tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
225
226         ensure_float (tempo_dialog);
227
228         switch (tempo_dialog.run()) {
229         case RESPONSE_ACCEPT:
230                 break;
231         default:
232                 return;
233         }
234
235         double bpm = 0;
236         BBT_Time requested;
237         
238         bpm = tempo_dialog.get_bpm ();
239         double nt = tempo_dialog.get_note_type();
240         bpm = max (0.01, bpm);
241         
242         tempo_dialog.get_bbt_time (requested);
243         
244         begin_reversible_command (_("add tempo mark"));
245         XMLNode &before = map.get_state();
246         map.add_tempo (Tempo (bpm,nt), requested);
247         XMLNode &after = map.get_state();
248         session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
249         commit_reversible_command ();
250         
251         //map.dump (cerr);
252 }
253
254 void
255 Editor::mouse_add_new_meter_event (nframes_t frame)
256 {
257         if (session == 0) {
258                 return;
259         }
260
261
262         TempoMap& map(session->tempo_map());
263         MeterDialog meter_dialog (map, frame, _("add"));
264
265         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
266
267         //this causes compiz to display no border.. 
268         //meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
269
270         ensure_float (meter_dialog);
271         
272         switch (meter_dialog.run ()) {
273         case RESPONSE_ACCEPT:
274                 break;
275         default:
276                 return;
277         }
278
279         double bpb = meter_dialog.get_bpb ();
280         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
281         
282         double note_type = meter_dialog.get_note_type ();
283         BBT_Time requested;
284
285         meter_dialog.get_bbt_time (requested);
286
287         begin_reversible_command (_("add meter mark"));
288         XMLNode &before = map.get_state();
289         map.add_meter (Meter (bpb, note_type), requested);
290         session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
291         commit_reversible_command ();
292         
293         //map.dump (cerr);
294 }
295
296 void
297 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
298 {
299         Marker* marker;
300         TempoMarker* tempo_marker;
301
302         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
303                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
304                 /*NOTREACHED*/
305         }
306
307         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
308                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
309                 /*NOTREACHED*/
310         }               
311
312         if (tempo_marker->tempo().movable()) {
313           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
314         }
315 }
316
317 void
318 Editor::edit_meter_section (MeterSection* section)
319 {
320         MeterDialog meter_dialog (*section, _("done"));
321
322         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
323
324         ensure_float (meter_dialog);
325
326         switch (meter_dialog.run()) {
327         case RESPONSE_ACCEPT:
328                 break;
329         default:
330                 return;
331         }
332
333         double bpb = meter_dialog.get_bpb ();
334         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
335         
336         double note_type = meter_dialog.get_note_type ();
337
338         begin_reversible_command (_("replace tempo mark"));
339         XMLNode &before = session->tempo_map().get_state();
340         session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
341         XMLNode &after = session->tempo_map().get_state();
342         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
343         commit_reversible_command ();
344 }
345
346 void
347 Editor::edit_tempo_section (TempoSection* section)
348 {
349         TempoDialog tempo_dialog (*section, _("done"));
350
351         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
352
353         ensure_float (tempo_dialog);
354         
355         switch (tempo_dialog.run ()) {
356         case RESPONSE_ACCEPT:
357                 break;
358         default:
359                 return;
360         }
361
362         double bpm = tempo_dialog.get_bpm ();
363         double nt = tempo_dialog.get_note_type ();
364         BBT_Time when;
365         tempo_dialog.get_bbt_time(when);
366         bpm = max (0.01, bpm);
367         
368         cerr << "Editing tempo section to be at " << when << endl;
369         session->tempo_map().dump (cerr);
370         begin_reversible_command (_("replace tempo mark"));
371         XMLNode &before = session->tempo_map().get_state();
372         session->tempo_map().replace_tempo (*section, Tempo (bpm,nt));
373         session->tempo_map().dump (cerr);
374         session->tempo_map().move_tempo (*section, when);
375         session->tempo_map().dump (cerr);
376         XMLNode &after = session->tempo_map().get_state();
377         session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
378         commit_reversible_command ();
379 }
380
381 void
382 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
383 {
384         Marker* marker;
385         TempoMarker* tempo_marker;
386
387         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
388                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
389                 /*NOTREACHED*/
390         }
391
392         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
393                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
394                 /*NOTREACHED*/
395         }               
396
397         edit_tempo_section (&tempo_marker->tempo());
398 }
399
400 void
401 Editor::edit_meter_marker (ArdourCanvas::Item *item)
402 {
403         Marker* marker;
404         MeterMarker* meter_marker;
405
406         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
407                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
408                 /*NOTREACHED*/
409         }
410
411         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
412                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
413                 /*NOTREACHED*/
414         }               
415         
416         edit_meter_section (&meter_marker->meter());
417 }
418
419 gint
420 Editor::real_remove_tempo_marker (TempoSection *section)
421 {
422         begin_reversible_command (_("remove tempo mark"));
423         XMLNode &before = session->tempo_map().get_state();
424         session->tempo_map().remove_tempo (*section);
425         XMLNode &after = session->tempo_map().get_state();
426         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
427         commit_reversible_command ();
428
429         return FALSE;
430 }
431
432 void
433 Editor::remove_meter_marker (ArdourCanvas::Item* item)
434 {
435         Marker* marker;
436         MeterMarker* meter_marker;
437
438         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
439                 fatal << _("programming error: meter 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         if (meter_marker->meter().movable()) {
449           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
450         }
451 }
452
453 gint
454 Editor::real_remove_meter_marker (MeterSection *section)
455 {
456         begin_reversible_command (_("remove tempo mark"));
457         XMLNode &before = session->tempo_map().get_state();
458         session->tempo_map().remove_meter (*section);
459         XMLNode &after = session->tempo_map().get_state();
460         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
461         commit_reversible_command ();
462
463         return FALSE;
464 }