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