Make crossfades blue because blue = audio and it's prettier.
[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 #ifdef GTKOSX
162                         lazy_hide_and_draw_measures ();
163 #endif
164 }
165
166 void
167 Editor::hide_measures ()
168 {
169         if (tempo_lines)
170                 tempo_lines->hide();
171 }
172
173 bool
174 Editor::redraw_measures ()
175 {
176         draw_measures ();
177         return false;
178 }
179
180 void
181 Editor::draw_measures ()
182 {
183         if (session == 0 || _show_measures == false || 
184             !current_bbt_points || current_bbt_points->empty()) {
185                 return;
186         }
187
188         if (current_bbt_points == 0 || current_bbt_points->empty()) {
189                 return;
190         }
191
192         if (tempo_lines == 0) {
193                 tempo_lines = new TempoLines(*track_canvas, time_line_group);
194         }
195
196         tempo_lines->draw(*current_bbt_points, frames_per_unit);
197 }
198
199 void
200 Editor::mouse_add_new_tempo_event (nframes64_t frame)
201 {
202         if (session == 0) {
203                 return;
204         }
205
206         TempoMap& map(session->tempo_map());
207         TempoDialog tempo_dialog (map, frame, _("add"));
208         
209         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
210         //this causes compiz to display no border.
211         //tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
212
213         ensure_float (tempo_dialog);
214
215         switch (tempo_dialog.run()) {
216         case RESPONSE_ACCEPT:
217                 break;
218         default:
219                 return;
220         }
221
222         double bpm = 0;
223         BBT_Time requested;
224         
225         bpm = tempo_dialog.get_bpm ();
226         double nt = tempo_dialog.get_note_type();
227         bpm = max (0.01, bpm);
228         
229         tempo_dialog.get_bbt_time (requested);
230         
231         begin_reversible_command (_("add tempo mark"));
232         XMLNode &before = map.get_state();
233         map.add_tempo (Tempo (bpm,nt), requested);
234         XMLNode &after = map.get_state();
235         session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
236         commit_reversible_command ();
237         
238         //map.dump (cerr);
239 }
240
241 void
242 Editor::mouse_add_new_meter_event (nframes64_t frame)
243 {
244         if (session == 0) {
245                 return;
246         }
247
248
249         TempoMap& map(session->tempo_map());
250         MeterDialog meter_dialog (map, frame, _("add"));
251
252         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
253
254         //this causes compiz to display no border.. 
255         //meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
256
257         ensure_float (meter_dialog);
258         
259         switch (meter_dialog.run ()) {
260         case RESPONSE_ACCEPT:
261                 break;
262         default:
263                 return;
264         }
265
266         double bpb = meter_dialog.get_bpb ();
267         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
268         
269         double note_type = meter_dialog.get_note_type ();
270         BBT_Time requested;
271
272         meter_dialog.get_bbt_time (requested);
273
274         begin_reversible_command (_("add meter mark"));
275         XMLNode &before = map.get_state();
276         map.add_meter (Meter (bpb, note_type), requested);
277         session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
278         commit_reversible_command ();
279         
280         //map.dump (cerr);
281 }
282
283 void
284 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
285 {
286         Marker* marker;
287         TempoMarker* tempo_marker;
288
289         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
290                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
291                 /*NOTREACHED*/
292         }
293
294         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
295                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
296                 /*NOTREACHED*/
297         }               
298
299         if (tempo_marker->tempo().movable()) {
300           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
301         }
302 }
303
304 void
305 Editor::edit_meter_section (MeterSection* section)
306 {
307         MeterDialog meter_dialog (*section, _("done"));
308
309         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
310
311         ensure_float (meter_dialog);
312
313         switch (meter_dialog.run()) {
314         case RESPONSE_ACCEPT:
315                 break;
316         default:
317                 return;
318         }
319
320         double bpb = meter_dialog.get_bpb ();
321         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
322         
323         double note_type = meter_dialog.get_note_type ();
324
325         begin_reversible_command (_("replace tempo mark"));
326         XMLNode &before = session->tempo_map().get_state();
327         session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
328         XMLNode &after = session->tempo_map().get_state();
329         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
330         commit_reversible_command ();
331 }
332
333 void
334 Editor::edit_tempo_section (TempoSection* section)
335 {
336         TempoDialog tempo_dialog (*section, _("done"));
337
338         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
339
340         ensure_float (tempo_dialog);
341         
342         switch (tempo_dialog.run ()) {
343         case RESPONSE_ACCEPT:
344                 break;
345         default:
346                 return;
347         }
348
349         double bpm = tempo_dialog.get_bpm ();
350         double nt = tempo_dialog.get_note_type ();
351         BBT_Time when;
352         tempo_dialog.get_bbt_time(when);
353         bpm = max (0.01, bpm);
354         
355         cerr << "Editing tempo section to be at " << when << endl;
356         session->tempo_map().dump (cerr);
357         begin_reversible_command (_("replace tempo mark"));
358         XMLNode &before = session->tempo_map().get_state();
359         session->tempo_map().replace_tempo (*section, Tempo (bpm,nt));
360         session->tempo_map().dump (cerr);
361         session->tempo_map().move_tempo (*section, when);
362         session->tempo_map().dump (cerr);
363         XMLNode &after = session->tempo_map().get_state();
364         session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
365         commit_reversible_command ();
366 }
367
368 void
369 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
370 {
371         Marker* marker;
372         TempoMarker* tempo_marker;
373
374         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
375                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
376                 /*NOTREACHED*/
377         }
378
379         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
380                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
381                 /*NOTREACHED*/
382         }               
383
384         edit_tempo_section (&tempo_marker->tempo());
385 }
386
387 void
388 Editor::edit_meter_marker (ArdourCanvas::Item *item)
389 {
390         Marker* marker;
391         MeterMarker* meter_marker;
392
393         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
394                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
395                 /*NOTREACHED*/
396         }
397
398         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
399                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
400                 /*NOTREACHED*/
401         }               
402         
403         edit_meter_section (&meter_marker->meter());
404 }
405
406 gint
407 Editor::real_remove_tempo_marker (TempoSection *section)
408 {
409         begin_reversible_command (_("remove tempo mark"));
410         XMLNode &before = session->tempo_map().get_state();
411         session->tempo_map().remove_tempo (*section);
412         XMLNode &after = session->tempo_map().get_state();
413         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
414         commit_reversible_command ();
415
416         return FALSE;
417 }
418
419 void
420 Editor::remove_meter_marker (ArdourCanvas::Item* item)
421 {
422         Marker* marker;
423         MeterMarker* meter_marker;
424
425         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
426                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
427                 /*NOTREACHED*/
428         }
429
430         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
431                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
432                 /*NOTREACHED*/
433         }               
434
435         if (meter_marker->meter().movable()) {
436           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
437         }
438 }
439
440 gint
441 Editor::real_remove_meter_marker (MeterSection *section)
442 {
443         begin_reversible_command (_("remove tempo mark"));
444         XMLNode &before = session->tempo_map().get_state();
445         session->tempo_map().remove_meter (*section);
446         XMLNode &after = session->tempo_map().get_state();
447         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
448         commit_reversible_command ();
449
450         return FALSE;
451 }