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