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