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