Merged with trunk R1705.
[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 "color.h"
46 #include "time_axis_view.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, color_map[cMeterMarker], 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, color_map[cTempoMarker], 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::lazy_hide_and_draw_measures));
164                 } else {
165                         hide_measures ();
166                 }
167         }
168 }
169
170 void
171 Editor::hide_measures ()
172 {
173         for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
174                 (*i)->hide();
175                 free_measure_lines.push_back (*i);
176         }
177         used_measure_lines.clear ();
178 }
179
180 ArdourCanvas::SimpleLine *
181 Editor::get_time_line ()
182 {
183         ArdourCanvas::SimpleLine *line;
184
185         if (free_measure_lines.empty()) {
186                 line = new ArdourCanvas::SimpleLine (*time_line_group);
187                 used_measure_lines.push_back (line);
188         } else {
189                 line = free_measure_lines.front();
190                 free_measure_lines.erase (free_measure_lines.begin());
191                 used_measure_lines.push_back (line);
192         }
193
194         return line;
195 }
196
197 bool
198 Editor::lazy_hide_and_draw_measures ()
199 {
200         hide_measures ();
201         draw_measures ();
202         return false;
203 }
204
205 void
206 Editor::draw_measures ()
207 {
208         if (session == 0 || _show_measures == false) {
209                 return;
210         }
211
212         TempoMap::BBTPointList::iterator i;
213         ArdourCanvas::SimpleLine *line;
214         gdouble xpos;
215         double x1, x2, y1, y2, beat_density;
216
217         uint32_t beats = 0;
218         uint32_t bars = 0;
219         uint32_t color;
220
221         if (current_bbt_points == 0 || current_bbt_points->empty()) {
222                 return;
223         }
224
225         track_canvas.get_scroll_region (x1, y1, x2, y2);
226         y2 = TimeAxisView::hLargest*5000; // five thousand largest tracks should be enough.. :)
227
228         /* get the first bar spacing */
229
230         i = current_bbt_points->end();
231         i--;
232         bars = (*i).bar - (*current_bbt_points->begin()).bar;
233         beats = current_bbt_points->size() - bars;
234
235         beat_density =  (beats * 10.0f) / track_canvas.get_width ();
236
237         if (beat_density > 4.0f) {
238                 /* if the lines are too close together, they become useless
239                  */
240                 return;
241         }
242         
243         for (i = current_bbt_points->begin(); i != current_bbt_points->end(); ++i) {
244
245                 switch ((*i).type) {
246                 case TempoMap::Bar:
247                         break;
248
249                 case TempoMap::Beat:
250                         
251                         if ((*i).beat == 1) {
252                                 color = color_map[cMeasureLineBar];
253                         } else {
254                                 color = color_map[cMeasureLineBeat];
255
256                                 if (beat_density > 2.0) {
257                                         /* only draw beat lines if the gaps between beats are large.
258                                         */
259                                         break;
260                                 }
261                         }
262
263                         xpos = frame_to_unit ((*i).frame);
264                         line = get_time_line ();
265                         line->property_x1() = xpos;
266                         line->property_x2() = xpos;
267                         line->property_y2() = y2;
268                         line->property_color_rgba() = color;
269                         //line->raise_to_top();
270                         line->show();   
271                         break;
272                 }
273         }
274
275         /* the cursors are always on top of everything */
276
277         cursor_group->raise_to_top();
278         time_line_group->lower_to_bottom();
279         return;
280 }
281
282 void
283 Editor::mouse_add_new_tempo_event (nframes_t frame)
284 {
285         if (session == 0) {
286                 return;
287         }
288
289         TempoMap& map(session->tempo_map());
290         TempoDialog tempo_dialog (map, frame, _("add"));
291         
292         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
293         tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
294
295         ensure_float (tempo_dialog);
296
297         switch (tempo_dialog.run()) {
298         case RESPONSE_ACCEPT:
299                 break;
300         default:
301                 return;
302         }
303
304         double bpm = 0;
305         BBT_Time requested;
306         
307         bpm = tempo_dialog.get_bpm ();
308         bpm = max (0.01, bpm);
309         
310         tempo_dialog.get_bbt_time (requested);
311         
312         begin_reversible_command (_("add tempo mark"));
313         XMLNode &before = map.get_state();
314         map.add_tempo (Tempo (bpm), requested);
315         XMLNode &after = map.get_state();
316         session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
317         commit_reversible_command ();
318         
319         map.dump (cerr);
320 }
321
322 void
323 Editor::mouse_add_new_meter_event (nframes_t frame)
324 {
325         if (session == 0) {
326                 return;
327         }
328
329
330         TempoMap& map(session->tempo_map());
331         MeterDialog meter_dialog (map, frame, _("add"));
332
333         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
334         meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
335
336         ensure_float (meter_dialog);
337         
338         switch (meter_dialog.run ()) {
339         case RESPONSE_ACCEPT:
340                 break;
341         default:
342                 return;
343         }
344
345         double bpb = meter_dialog.get_bpb ();
346         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
347         
348         double note_type = meter_dialog.get_note_type ();
349         BBT_Time requested;
350         
351         meter_dialog.get_bbt_time (requested);
352         
353         begin_reversible_command (_("add meter mark"));
354         XMLNode &before = map.get_state();
355         map.add_meter (Meter (bpb, note_type), requested);
356         session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
357         commit_reversible_command ();
358         
359         map.dump (cerr);
360 }
361
362 void
363 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
364 {
365         Marker* marker;
366         TempoMarker* tempo_marker;
367
368         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
369                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
370                 /*NOTREACHED*/
371         }
372
373         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
374                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
375                 /*NOTREACHED*/
376         }               
377
378         if (tempo_marker->tempo().movable()) {
379           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
380         }
381 }
382
383 void
384 Editor::edit_meter_section (MeterSection* section)
385 {
386         MeterDialog meter_dialog (*section, _("done"));
387
388         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
389
390         ensure_float (meter_dialog);
391
392         switch (meter_dialog.run()) {
393         case RESPONSE_ACCEPT:
394                 break;
395         default:
396                 return;
397         }
398
399         double bpb = meter_dialog.get_bpb ();
400         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
401         
402         double note_type = meter_dialog.get_note_type ();
403
404         begin_reversible_command (_("replace tempo mark"));
405         XMLNode &before = session->tempo_map().get_state();
406         session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
407         XMLNode &after = session->tempo_map().get_state();
408         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
409         commit_reversible_command ();
410 }
411
412 void
413 Editor::edit_tempo_section (TempoSection* section)
414 {
415         TempoDialog tempo_dialog (*section, _("done"));
416
417         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
418
419         ensure_float (tempo_dialog);
420         
421         switch (tempo_dialog.run ()) {
422         case RESPONSE_ACCEPT:
423                 break;
424         default:
425                 return;
426         }
427
428         double bpm = tempo_dialog.get_bpm ();
429         BBT_Time when;
430         tempo_dialog.get_bbt_time(when);
431         bpm = max (0.01, bpm);
432         
433         begin_reversible_command (_("replace tempo mark"));
434         XMLNode &before = session->tempo_map().get_state();
435         session->tempo_map().replace_tempo (*section, Tempo (bpm));
436         session->tempo_map().move_tempo (*section, when);
437         XMLNode &after = session->tempo_map().get_state();
438         session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
439         commit_reversible_command ();
440 }
441
442 void
443 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
444 {
445         Marker* marker;
446         TempoMarker* tempo_marker;
447
448         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
449                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
450                 /*NOTREACHED*/
451         }
452
453         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
454                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
455                 /*NOTREACHED*/
456         }               
457
458         edit_tempo_section (&tempo_marker->tempo());
459 }
460
461 void
462 Editor::edit_meter_marker (ArdourCanvas::Item *item)
463 {
464         Marker* marker;
465         MeterMarker* meter_marker;
466
467         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
468                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
469                 /*NOTREACHED*/
470         }
471
472         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
473                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
474                 /*NOTREACHED*/
475         }               
476         
477         edit_meter_section (&meter_marker->meter());
478 }
479
480 gint
481 Editor::real_remove_tempo_marker (TempoSection *section)
482 {
483         begin_reversible_command (_("remove tempo mark"));
484         XMLNode &before = session->tempo_map().get_state();
485         session->tempo_map().remove_tempo (*section);
486         XMLNode &after = session->tempo_map().get_state();
487         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
488         commit_reversible_command ();
489
490         return FALSE;
491 }
492
493 void
494 Editor::remove_meter_marker (ArdourCanvas::Item* item)
495 {
496         Marker* marker;
497         MeterMarker* meter_marker;
498
499         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
500                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
501                 /*NOTREACHED*/
502         }
503
504         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
505                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
506                 /*NOTREACHED*/
507         }               
508
509         if (meter_marker->meter().movable()) {
510           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
511         }
512 }
513
514 gint
515 Editor::real_remove_meter_marker (MeterSection *section)
516 {
517         begin_reversible_command (_("remove tempo mark"));
518         XMLNode &before = session->tempo_map().get_state();
519         session->tempo_map().remove_meter (*section);
520         XMLNode &after = session->tempo_map().get_state();
521         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
522         commit_reversible_command ();
523
524         return FALSE;
525 }