09d9b39c735dcf80d4ad93387ab69aaf30950552
[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()->get_canvasvar_MeterMarker(), 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()->get_canvasvar_TempoMarker(), 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         draw_measures (begin, end);
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         if (immediate_redraw) {
134                 ARDOUR::TempoMap::BBTPointList::const_iterator current_bbt_points_begin;
135                 ARDOUR::TempoMap::BBTPointList::const_iterator current_bbt_points_end;
136                 
137                 compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_samples(),
138                                             current_bbt_points_begin, current_bbt_points_end);
139                 draw_measures (current_bbt_points_begin, current_bbt_points_end);
140                 update_tempo_based_rulers (current_bbt_points_begin, current_bbt_points_end); // redraw rulers and measures
141         
142         } else {
143                 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_tempo), true), false));
144         }
145 }
146
147 void
148 Editor::compute_current_bbt_points (framepos_t leftmost, framepos_t rightmost,
149                                     ARDOUR::TempoMap::BBTPointList::const_iterator& begin,
150                                     ARDOUR::TempoMap::BBTPointList::const_iterator& end)
151 {
152         if (!_session) {
153                 return;
154         }
155
156         /* prevent negative values of leftmost from creeping into tempomap
157          */
158
159         _session->tempo_map().get_grid (begin, end, max (leftmost, (framepos_t) 0), rightmost);
160 }
161
162 void
163 Editor::hide_measures ()
164 {
165         if (tempo_lines)
166                 tempo_lines->hide();
167 }
168
169 void
170 Editor::draw_measures (ARDOUR::TempoMap::BBTPointList::const_iterator& begin,
171                        ARDOUR::TempoMap::BBTPointList::const_iterator& end)
172 {
173         if (_session == 0 || _show_measures == false || distance (begin, end) == 0) {
174                 return;
175         }
176
177         if (tempo_lines == 0) {
178                 tempo_lines = new TempoLines (*_track_canvas, time_line_group, physical_screen_height(get_window()));
179         }
180         
181         tempo_lines->draw (begin, end, samples_per_pixel);
182 }
183
184 void
185 Editor::mouse_add_new_tempo_event (framepos_t frame)
186 {
187         if (_session == 0) {
188                 return;
189         }
190
191         TempoMap& map(_session->tempo_map());
192         TempoDialog tempo_dialog (map, frame, _("add"));
193
194         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
195         //this causes compiz to display no border.
196         //tempo_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
197
198         ensure_float (tempo_dialog);
199
200         switch (tempo_dialog.run()) {
201         case RESPONSE_ACCEPT:
202                 break;
203         default:
204                 return;
205         }
206
207         double bpm = 0;
208         Timecode::BBT_Time requested;
209
210         bpm = tempo_dialog.get_bpm ();
211         double nt = tempo_dialog.get_note_type();
212         bpm = max (0.01, bpm);
213
214         tempo_dialog.get_bbt_time (requested);
215
216         begin_reversible_command (_("add tempo mark"));
217         XMLNode &before = map.get_state();
218         map.add_tempo (Tempo (bpm,nt), requested);
219         XMLNode &after = map.get_state();
220         _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
221         commit_reversible_command ();
222
223         //map.dump (cerr);
224 }
225
226 void
227 Editor::mouse_add_new_meter_event (framepos_t frame)
228 {
229         if (_session == 0) {
230                 return;
231         }
232
233
234         TempoMap& map(_session->tempo_map());
235         MeterDialog meter_dialog (map, frame, _("add"));
236
237         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
238
239         //this causes compiz to display no border..
240         //meter_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
241
242         ensure_float (meter_dialog);
243
244         switch (meter_dialog.run ()) {
245         case RESPONSE_ACCEPT:
246                 break;
247         default:
248                 return;
249         }
250
251         double bpb = meter_dialog.get_bpb ();
252         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
253
254         double note_type = meter_dialog.get_note_type ();
255         Timecode::BBT_Time requested;
256
257         meter_dialog.get_bbt_time (requested);
258
259         begin_reversible_command (_("add meter mark"));
260         XMLNode &before = map.get_state();
261         map.add_meter (Meter (bpb, note_type), requested);
262         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
263         commit_reversible_command ();
264
265         //map.dump (cerr);
266 }
267
268 void
269 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
270 {
271         Marker* marker;
272         TempoMarker* tempo_marker;
273
274         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
275                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
276                 /*NOTREACHED*/
277         }
278
279         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
280                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
281                 /*NOTREACHED*/
282         }
283
284         if (tempo_marker->tempo().movable()) {
285                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
286         }
287 }
288
289 void
290 Editor::edit_meter_section (MeterSection* section)
291 {
292         MeterDialog meter_dialog (*section, _("done"));
293
294         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
295
296         ensure_float (meter_dialog);
297
298         switch (meter_dialog.run()) {
299         case RESPONSE_ACCEPT:
300                 break;
301         default:
302                 return;
303         }
304
305         double bpb = meter_dialog.get_bpb ();
306         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
307
308         double note_type = meter_dialog.get_note_type ();
309
310         Timecode::BBT_Time when;
311         meter_dialog.get_bbt_time(when);
312
313         begin_reversible_command (_("replace tempo mark"));
314         XMLNode &before = _session->tempo_map().get_state();
315         _session->tempo_map().replace_meter (*section, Meter (bpb, note_type), when);
316         XMLNode &after = _session->tempo_map().get_state();
317         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
318         commit_reversible_command ();
319 }
320
321 void
322 Editor::edit_tempo_section (TempoSection* section)
323 {
324         TempoDialog tempo_dialog (*section, _("done"));
325
326         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
327
328         ensure_float (tempo_dialog);
329
330         switch (tempo_dialog.run ()) {
331         case RESPONSE_ACCEPT:
332                 break;
333         default:
334                 return;
335         }
336
337         double bpm = tempo_dialog.get_bpm ();
338         double nt = tempo_dialog.get_note_type ();
339         Timecode::BBT_Time when;
340         tempo_dialog.get_bbt_time(when);
341         bpm = max (0.01, bpm);
342
343         begin_reversible_command (_("replace tempo mark"));
344         XMLNode &before = _session->tempo_map().get_state();
345         _session->tempo_map().replace_tempo (*section, Tempo (bpm, nt), when);
346         XMLNode &after = _session->tempo_map().get_state();
347         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
348         commit_reversible_command ();
349 }
350
351 void
352 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
353 {
354         Marker* marker;
355         TempoMarker* tempo_marker;
356
357         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
358                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
359                 /*NOTREACHED*/
360         }
361
362         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
363                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
364                 /*NOTREACHED*/
365         }
366
367         edit_tempo_section (&tempo_marker->tempo());
368 }
369
370 void
371 Editor::edit_meter_marker (ArdourCanvas::Item *item)
372 {
373         Marker* marker;
374         MeterMarker* meter_marker;
375
376         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
377                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
378                 /*NOTREACHED*/
379         }
380
381         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
382                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
383                 /*NOTREACHED*/
384         }
385
386         edit_meter_section (&meter_marker->meter());
387 }
388
389 gint
390 Editor::real_remove_tempo_marker (TempoSection *section)
391 {
392         begin_reversible_command (_("remove tempo mark"));
393         XMLNode &before = _session->tempo_map().get_state();
394         _session->tempo_map().remove_tempo (*section, true);
395         XMLNode &after = _session->tempo_map().get_state();
396         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
397         commit_reversible_command ();
398
399         return FALSE;
400 }
401
402 void
403 Editor::remove_meter_marker (ArdourCanvas::Item* item)
404 {
405         Marker* marker;
406         MeterMarker* meter_marker;
407
408         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
409                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
410                 /*NOTREACHED*/
411         }
412
413         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
414                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
415                 /*NOTREACHED*/
416         }
417
418         if (meter_marker->meter().movable()) {
419           Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
420         }
421 }
422
423 gint
424 Editor::real_remove_meter_marker (MeterSection *section)
425 {
426         begin_reversible_command (_("remove tempo mark"));
427         XMLNode &before = _session->tempo_map().get_state();
428         _session->tempo_map().remove_meter (*section, true);
429         XMLNode &after = _session->tempo_map().get_state();
430         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
431         commit_reversible_command ();
432
433         return FALSE;
434 }