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