30aab87d05191a2dfef6b22b8593c592ffc00df3
[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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <cstdio> // for sprintf, grrr
25 #include <cstdlib>
26 #include <cmath>
27 #include <string>
28 #include <climits>
29
30 #include "pbd/error.h"
31 #include "pbd/memento_command.h"
32
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/gtk_ui.h>
35
36 #include "ardour/session.h"
37 #include "ardour/tempo.h"
38 #include <gtkmm2ext/doi.h>
39 #include <gtkmm2ext/utils.h>
40
41 #include "canvas/canvas.h"
42 #include "canvas/item.h"
43
44 #include "editor.h"
45 #include "marker.h"
46 #include "tempo_dialog.h"
47 #include "rgb_macros.h"
48 #include "gui_thread.h"
49 #include "time_axis_view.h"
50 #include "ardour_ui.h"
51 #include "tempo_lines.h"
52 #include "utils.h"
53
54 #include "i18n.h"
55
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Gtk;
60 using namespace Gtkmm2ext;
61 using namespace Editing;
62
63 void
64 Editor::remove_metric_marks ()
65 {
66         /* don't delete these while handling events, just punt till the GUI is idle */
67
68         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
69                 delete_when_idle (*x);
70         }
71         metric_marks.clear ();
72 }
73
74 void
75 Editor::draw_metric_marks (const Metrics& metrics)
76 {
77
78         const MeterSection *ms;
79         const TempoSection *ts;
80         char buf[64];
81
82         remove_metric_marks ();
83
84         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
85
86                 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
87                         snprintf (buf, sizeof(buf), "%g/%g", ms->divisions_per_bar(), ms->note_divisor ());
88                         metric_marks.push_back (new MeterMarker (*this, *meter_group, ARDOUR_UI::config()->canvasvar_MeterMarker.get(), buf,
89                                                                  *(const_cast<MeterSection*>(ms))));
90                 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
91                         if (Config->get_allow_non_quarter_pulse()) {
92                                 snprintf (buf, sizeof (buf), "%.2f/%.0f", ts->beats_per_minute(), ts->note_type());
93                         } else {
94                                 snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
95                         }
96                         metric_marks.push_back (new TempoMarker (*this, *tempo_group, ARDOUR_UI::config()->canvasvar_TempoMarker.get(), buf,
97                                                                  *(const_cast<TempoSection*>(ts))));
98                 }
99
100         }
101
102 }
103
104 void
105 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
106 {
107         if (!_session) {
108                 return;
109         }
110
111         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
112
113         if (tempo_lines) {
114                 tempo_lines->tempo_map_changed();
115         }
116
117         ARDOUR::TempoMap::BBTPointList::const_iterator begin;
118         ARDOUR::TempoMap::BBTPointList::const_iterator end;
119
120         compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_samples(), begin, end);
121         _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
122         redraw_measures ();
123         update_tempo_based_rulers (begin, end);
124 }
125
126 void
127 Editor::redisplay_tempo (bool immediate_redraw)
128 {
129         if (!_session) {
130                 return;
131         }
132
133         ARDOUR::TempoMap::BBTPointList::const_iterator current_bbt_points_begin;
134         ARDOUR::TempoMap::BBTPointList::const_iterator current_bbt_points_end;
135
136         compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_samples(),
137                                     current_bbt_points_begin, current_bbt_points_end);
138
139         if (immediate_redraw) {
140                 redraw_measures ();
141         } else {
142 #ifdef GTKOSX
143                 redraw_measures ();
144 #else
145                 Glib::signal_idle().connect (sigc::mem_fun (*this, &Editor::redraw_measures));
146 #endif
147         }
148         update_tempo_based_rulers (current_bbt_points_begin, current_bbt_points_end); // redraw rulers and measures
149 }
150
151 void
152 Editor::compute_current_bbt_points (framepos_t leftmost, framepos_t rightmost,
153                                     ARDOUR::TempoMap::BBTPointList::const_iterator& begin,
154                                     ARDOUR::TempoMap::BBTPointList::const_iterator& end)
155 {
156         if (!_session) {
157                 return;
158         }
159
160         /* prevent negative values of leftmost from creeping into tempomap
161          */
162
163         _session->tempo_map().get_grid (begin, end, max (leftmost, (framepos_t) 0), rightmost);
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         ARDOUR::TempoMap::BBTPointList::const_iterator begin;
177         ARDOUR::TempoMap::BBTPointList::const_iterator end;
178
179         compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_samples(), begin, end);
180         draw_measures (begin, end);
181
182         return false;
183 }
184
185 void
186 Editor::draw_measures (ARDOUR::TempoMap::BBTPointList::const_iterator& begin,
187                        ARDOUR::TempoMap::BBTPointList::const_iterator& end)
188 {
189         if (_session == 0 || _show_measures == false || distance (begin, end) == 0) {
190                 return;
191         }
192
193         if (tempo_lines == 0) {
194                 tempo_lines = new TempoLines (*_track_canvas_viewport, time_line_group, physical_screen_height(get_window()));
195         }
196
197         // CAIROCANVAS
198         // tempo_lines->draw (begin, end, frames_per_unit);
199 }
200
201 void
202 Editor::mouse_add_new_tempo_event (framepos_t frame)
203 {
204         if (_session == 0) {
205                 return;
206         }
207
208         TempoMap& map(_session->tempo_map());
209         TempoDialog tempo_dialog (map, frame, _("add"));
210
211         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
212         //this causes compiz to display no border.
213         //tempo_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
214
215         ensure_float (tempo_dialog);
216
217         switch (tempo_dialog.run()) {
218         case RESPONSE_ACCEPT:
219                 break;
220         default:
221                 return;
222         }
223
224         double bpm = 0;
225         Timecode::BBT_Time requested;
226
227         bpm = tempo_dialog.get_bpm ();
228         double nt = tempo_dialog.get_note_type();
229         bpm = max (0.01, bpm);
230
231         tempo_dialog.get_bbt_time (requested);
232
233         begin_reversible_command (_("add tempo mark"));
234         XMLNode &before = map.get_state();
235         map.add_tempo (Tempo (bpm,nt), requested);
236         XMLNode &after = map.get_state();
237         _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
238         commit_reversible_command ();
239
240         //map.dump (cerr);
241 }
242
243 void
244 Editor::mouse_add_new_meter_event (framepos_t frame)
245 {
246         if (_session == 0) {
247                 return;
248         }
249
250
251         TempoMap& map(_session->tempo_map());
252         MeterDialog meter_dialog (map, frame, _("add"));
253
254         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
255
256         //this causes compiz to display no border..
257         //meter_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
258
259         ensure_float (meter_dialog);
260
261         switch (meter_dialog.run ()) {
262         case RESPONSE_ACCEPT:
263                 break;
264         default:
265                 return;
266         }
267
268         double bpb = meter_dialog.get_bpb ();
269         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
270
271         double note_type = meter_dialog.get_note_type ();
272         Timecode::BBT_Time requested;
273
274         meter_dialog.get_bbt_time (requested);
275
276         begin_reversible_command (_("add meter mark"));
277         XMLNode &before = map.get_state();
278         map.add_meter (Meter (bpb, note_type), requested);
279         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
280         commit_reversible_command ();
281
282         //map.dump (cerr);
283 }
284
285 void
286 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
287 {
288         Marker* marker;
289         TempoMarker* tempo_marker;
290
291         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
292                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
293                 /*NOTREACHED*/
294         }
295
296         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
297                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
298                 /*NOTREACHED*/
299         }
300
301         if (tempo_marker->tempo().movable()) {
302                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
303         }
304 }
305
306 void
307 Editor::edit_meter_section (MeterSection* section)
308 {
309         MeterDialog meter_dialog (*section, _("done"));
310
311         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
312
313         ensure_float (meter_dialog);
314
315         switch (meter_dialog.run()) {
316         case RESPONSE_ACCEPT:
317                 break;
318         default:
319                 return;
320         }
321
322         double bpb = meter_dialog.get_bpb ();
323         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
324
325         double note_type = meter_dialog.get_note_type ();
326
327         Timecode::BBT_Time when;
328         meter_dialog.get_bbt_time(when);
329
330         begin_reversible_command (_("replace tempo mark"));
331         XMLNode &before = _session->tempo_map().get_state();
332         _session->tempo_map().replace_meter (*section, Meter (bpb, note_type), when);
333         XMLNode &after = _session->tempo_map().get_state();
334         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
335         commit_reversible_command ();
336 }
337
338 void
339 Editor::edit_tempo_section (TempoSection* section)
340 {
341         TempoDialog tempo_dialog (*section, _("done"));
342
343         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
344
345         ensure_float (tempo_dialog);
346
347         switch (tempo_dialog.run ()) {
348         case RESPONSE_ACCEPT:
349                 break;
350         default:
351                 return;
352         }
353
354         double bpm = tempo_dialog.get_bpm ();
355         double nt = tempo_dialog.get_note_type ();
356         Timecode::BBT_Time when;
357         tempo_dialog.get_bbt_time(when);
358         bpm = max (0.01, bpm);
359
360         begin_reversible_command (_("replace tempo mark"));
361         XMLNode &before = _session->tempo_map().get_state();
362         _session->tempo_map().replace_tempo (*section, Tempo (bpm, nt), when);
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, true);
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 (sigc::bind (sigc::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, true);
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 }