Add some comments to the RegionSelection class.
[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         redisplay_tempo (false); // redraw rulers and measures
105         session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
106 }
107
108 /**
109  * This code was originally in tempo_map_changed, but this is called every time the canvas scrolls horizontally. 
110  * That's why this is moved in here. The new tempo_map_changed is called when the ARDOUR::TempoMap actually changed.
111  */
112 void
113 Editor::redisplay_tempo (bool immediate_redraw)
114 {
115         if (!session) {
116                 return;
117         }
118
119         BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
120
121         session->bbt_time(leftmost_frame, previous_beat);
122         session->bbt_time(leftmost_frame + current_page_frames(), next_beat);
123
124         if (previous_beat.beats > 1) {
125                 previous_beat.beats -= 1;
126         } else if (previous_beat.bars > 1) {
127                 previous_beat.bars--;
128                 previous_beat.beats += 1;
129         }
130         previous_beat.ticks = 0;
131
132         if (session->tempo_map().meter_at(leftmost_frame + current_page_frames()).beats_per_bar () > next_beat.beats + 1) {
133                 next_beat.beats += 1;
134         } else {
135                 next_beat.bars += 1;
136                 next_beat.beats = 1;
137         }
138         next_beat.ticks = 0;
139         
140         if (current_bbt_points) {
141                 delete current_bbt_points;
142                 current_bbt_points = 0;
143         }
144
145         if (session) {
146                 current_bbt_points = session->tempo_map().get_points (session->tempo_map().frame_time (previous_beat), session->tempo_map().frame_time (next_beat));
147                 update_tempo_based_rulers ();
148         } else {
149                 current_bbt_points = 0;
150         }
151
152         if (immediate_redraw) {
153
154                 hide_measures ();
155
156                 if (session && current_bbt_points) {
157                         draw_measures ();
158                 }
159
160         } else {
161
162                 if (session && current_bbt_points) {
163                         Glib::signal_idle().connect (mem_fun (*this, &Editor::redraw_measures));
164                 } else {
165                         hide_measures ();
166                 }
167         }
168 }
169
170 void
171 Editor::hide_measures ()
172 {
173         tempo_lines->hide();
174         marker_tempo_lines->hide();
175 }
176
177 bool
178 Editor::redraw_measures ()
179 {
180         hide_measures ();
181         draw_measures ();
182         return false;
183 }
184
185 void
186 Editor::draw_measures ()
187 {
188         if (session == 0 || _show_measures == false
189                         || !current_bbt_points || current_bbt_points->empty()) {
190                 return;
191         }
192
193         tempo_lines->draw(*current_bbt_points, frames_per_unit);
194         marker_tempo_lines->draw(*current_bbt_points, frames_per_unit);
195
196         /*time_line_group->raise_to_top();
197         time_line_group->lower(1);*/
198         marker_time_line_group->raise_to_top();
199         //marker_time_line_group->lower(1);
200
201         /* the cursors are always on top of everything */
202         cursor_group->raise_to_top();
203
204         return;
205 }
206
207 void
208 Editor::mouse_add_new_tempo_event (nframes_t frame)
209 {
210         if (session == 0) {
211                 return;
212         }
213
214         TempoMap& map(session->tempo_map());
215         TempoDialog tempo_dialog (map, frame, _("add"));
216         
217         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
218         tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
219
220         ensure_float (tempo_dialog);
221
222         switch (tempo_dialog.run()) {
223         case RESPONSE_ACCEPT:
224                 break;
225         default:
226                 return;
227         }
228
229         double bpm = 0;
230         BBT_Time requested;
231         
232         bpm = tempo_dialog.get_bpm ();
233         bpm = max (0.01, bpm);
234         
235         tempo_dialog.get_bbt_time (requested);
236         
237         begin_reversible_command (_("add tempo mark"));
238         XMLNode &before = map.get_state();
239         map.add_tempo (Tempo (bpm), requested);
240         XMLNode &after = map.get_state();
241         session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
242         commit_reversible_command ();
243         
244         map.dump (cerr);
245 }
246
247 void
248 Editor::mouse_add_new_meter_event (nframes_t frame)
249 {
250         if (session == 0) {
251                 return;
252         }
253
254
255         TempoMap& map(session->tempo_map());
256         MeterDialog meter_dialog (map, frame, _("add"));
257
258         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
259         meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
260
261         ensure_float (meter_dialog);
262         
263         switch (meter_dialog.run ()) {
264         case RESPONSE_ACCEPT:
265                 break;
266         default:
267                 return;
268         }
269
270         double bpb = meter_dialog.get_bpb ();
271         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
272         
273         double note_type = meter_dialog.get_note_type ();
274         BBT_Time requested;
275         
276         meter_dialog.get_bbt_time (requested);
277         
278         begin_reversible_command (_("add meter mark"));
279         XMLNode &before = map.get_state();
280         map.add_meter (Meter (bpb, note_type), requested);
281         session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
282         commit_reversible_command ();
283         
284         map.dump (cerr);
285 }
286
287 void
288 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
289 {
290         Marker* marker;
291         TempoMarker* tempo_marker;
292
293         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
294                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
295                 /*NOTREACHED*/
296         }
297
298         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
299                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
300                 /*NOTREACHED*/
301         }               
302
303         if (tempo_marker->tempo().movable()) {
304           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
305         }
306 }
307
308 void
309 Editor::edit_meter_section (MeterSection* section)
310 {
311         MeterDialog meter_dialog (*section, _("done"));
312
313         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
314
315         ensure_float (meter_dialog);
316
317         switch (meter_dialog.run()) {
318         case RESPONSE_ACCEPT:
319                 break;
320         default:
321                 return;
322         }
323
324         double bpb = meter_dialog.get_bpb ();
325         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
326         
327         double note_type = meter_dialog.get_note_type ();
328
329         begin_reversible_command (_("replace tempo mark"));
330         XMLNode &before = session->tempo_map().get_state();
331         session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
332         XMLNode &after = session->tempo_map().get_state();
333         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
334         commit_reversible_command ();
335 }
336
337 void
338 Editor::edit_tempo_section (TempoSection* section)
339 {
340         TempoDialog tempo_dialog (*section, _("done"));
341
342         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
343
344         ensure_float (tempo_dialog);
345         
346         switch (tempo_dialog.run ()) {
347         case RESPONSE_ACCEPT:
348                 break;
349         default:
350                 return;
351         }
352
353         double bpm = tempo_dialog.get_bpm ();
354         BBT_Time when;
355         tempo_dialog.get_bbt_time(when);
356         bpm = max (0.01, bpm);
357         
358         begin_reversible_command (_("replace tempo mark"));
359         XMLNode &before = session->tempo_map().get_state();
360         session->tempo_map().replace_tempo (*section, Tempo (bpm));
361         session->tempo_map().move_tempo (*section, when);
362         XMLNode &after = session->tempo_map().get_state();
363         session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
364         commit_reversible_command ();
365 }
366
367 void
368 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
369 {
370         Marker* marker;
371         TempoMarker* tempo_marker;
372
373         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
374                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
375                 /*NOTREACHED*/
376         }
377
378         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
379                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
380                 /*NOTREACHED*/
381         }               
382
383         edit_tempo_section (&tempo_marker->tempo());
384 }
385
386 void
387 Editor::edit_meter_marker (ArdourCanvas::Item *item)
388 {
389         Marker* marker;
390         MeterMarker* meter_marker;
391
392         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
393                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
394                 /*NOTREACHED*/
395         }
396
397         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
398                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
399                 /*NOTREACHED*/
400         }               
401         
402         edit_meter_section (&meter_marker->meter());
403 }
404
405 gint
406 Editor::real_remove_tempo_marker (TempoSection *section)
407 {
408         begin_reversible_command (_("remove tempo mark"));
409         XMLNode &before = session->tempo_map().get_state();
410         session->tempo_map().remove_tempo (*section);
411         XMLNode &after = session->tempo_map().get_state();
412         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
413         commit_reversible_command ();
414
415         return FALSE;
416 }
417
418 void
419 Editor::remove_meter_marker (ArdourCanvas::Item* item)
420 {
421         Marker* marker;
422         MeterMarker* meter_marker;
423
424         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
425                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
426                 /*NOTREACHED*/
427         }
428
429         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
430                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
431                 /*NOTREACHED*/
432         }               
433
434         if (meter_marker->meter().movable()) {
435           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
436         }
437 }
438
439 gint
440 Editor::real_remove_meter_marker (MeterSection *section)
441 {
442         begin_reversible_command (_("remove tempo mark"));
443         XMLNode &before = session->tempo_map().get_state();
444         session->tempo_map().remove_meter (*section);
445         XMLNode &after = session->tempo_map().get_state();
446         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
447         commit_reversible_command ();
448
449         return FALSE;
450 }