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