r191@gandalf: fugalh | 2006-07-24 19:50:10 -0600
[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
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
47 #include "i18n.h"
48
49 using namespace std;
50 using namespace sigc;
51 using namespace ARDOUR;
52 using namespace PBD;
53 using namespace Gtk;
54 using namespace Gtkmm2ext;
55 using namespace Editing;
56
57 void
58 Editor::remove_metric_marks ()
59 {
60         /* don't delete these while handling events, just punt till the GUI is idle */
61
62         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
63                 delete_when_idle (*x);
64         }
65         metric_marks.clear ();
66 }       
67
68 void
69 Editor::draw_metric_marks (const Metrics& metrics)
70 {
71         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
72                 const MeterSection *ms;
73                 const TempoSection *ts;
74                 char buf[64];
75                 
76                 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
77                         snprintf (buf, sizeof(buf), "%g/%g", ms->beats_per_bar(), ms->note_divisor ());
78                         metric_marks.push_back (new MeterMarker (*this, *meter_group, color_map[cMeterMarker], buf, 
79                                                                  *(const_cast<MeterSection*>(ms))));
80                 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
81                         snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
82                         metric_marks.push_back (new TempoMarker (*this, *tempo_group, color_map[cTempoMarker], buf, 
83                                                                  *(const_cast<TempoSection*>(ts))));
84                 }
85                 
86         }
87 }
88
89 void
90 Editor::tempo_map_changed (Change ignored)
91 {
92         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::tempo_map_changed), ignored));
93         
94         if (current_bbt_points) {
95                 delete current_bbt_points;
96                 current_bbt_points = 0;
97         }
98
99         if (session) {
100                 current_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
101         } else {
102                 current_bbt_points = 0;
103         }
104
105         redisplay_tempo ();
106 }
107
108 void
109 Editor::redisplay_tempo ()
110 {
111         update_tempo_based_rulers ();
112
113         remove_metric_marks (); 
114         hide_measures ();
115
116         if (session && current_bbt_points) {
117                 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
118                 draw_measures ();
119         }
120         
121 }
122
123 void
124 Editor::hide_measures ()
125 {
126         for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
127                 (*i)->hide();
128                 free_measure_lines.push_back (*i);
129         }
130         used_measure_lines.clear ();
131 }
132
133 ArdourCanvas::SimpleLine *
134 Editor::get_time_line ()
135 {
136          ArdourCanvas::SimpleLine *line;
137
138         if (free_measure_lines.empty()) {
139                 line = new ArdourCanvas::SimpleLine (*time_line_group);
140                 used_measure_lines.push_back (line);
141         } else {
142                 line = free_measure_lines.front();
143                 free_measure_lines.erase (free_measure_lines.begin());
144                 used_measure_lines.push_back (line);
145         }
146
147         return line;
148 }
149
150 void
151 Editor::draw_measures ()
152 {
153         if (session == 0 || _show_measures == false) {
154                 return;
155         }
156
157         TempoMap::BBTPointList::iterator i;
158         TempoMap::BBTPointList *all_bbt_points;
159         ArdourCanvas::SimpleLine *line;
160         gdouble xpos, last_xpos;
161         uint32_t cnt;
162         uint32_t color;
163
164         if (current_bbt_points == 0 || current_bbt_points->empty()) {
165                 return;
166         }
167
168         all_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
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 = all_bbt_points->begin(); i != all_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
197         for (i = all_bbt_points->begin(); i != all_bbt_points->end(); ++i) {
198
199                 TempoMap::BBTPoint& p = (*i);
200
201                 switch (p.type) {
202                 case TempoMap::Bar:
203                         break;
204
205                 case TempoMap::Beat:
206                         xpos = frame_to_unit (p.frame);
207                         
208                         if (p.beat == 1) {
209                                 color = color_map[cMeasureLineBeat];
210                         } else {
211                                 color = color_map[cMeasureLineBar];
212
213                                 /* only draw beat lines if the gaps between beats
214                                    are large.
215                                 */
216
217                                 if (beat_spacing < 25.0) {
218                                         break;
219                                 }
220                         }
221                         
222                         if (cnt == 0 || xpos - last_xpos > 4.0) {
223                                 line = get_time_line ();
224                                 line->property_x1() = xpos;
225                                 line->property_x2() = xpos;
226                                 line->property_y2() = y2;
227                                 line->property_color_rgba() = color;
228                                 line->raise_to_top();
229                                 line->show();
230                                 last_xpos = xpos;       
231                                 ++cnt;
232                         } 
233                         break;
234                 }
235         }
236
237         delete all_bbt_points;
238
239         /* the cursors are always on top of everything */
240
241         cursor_group->raise_to_top();
242         time_line_group->lower_to_bottom();
243 }
244
245 void
246 Editor::mouse_add_new_tempo_event (jack_nframes_t frame)
247 {
248         if (session == 0) {
249                 return;
250         }
251
252         TempoMap& map(session->tempo_map());
253         TempoDialog tempo_dialog (map, frame, _("add"));
254         
255         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
256         tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
257
258         ensure_float (tempo_dialog);
259
260         switch (tempo_dialog.run()) {
261         case RESPONSE_ACCEPT:
262                 break;
263         default:
264                 return;
265         }
266
267         double bpm = 0;
268         BBT_Time requested;
269         
270         bpm = tempo_dialog.get_bpm ();
271         bpm = max (0.01, bpm);
272         
273         tempo_dialog.get_bbt_time (requested);
274         
275         begin_reversible_command (_("add tempo mark"));
276         XMLNode &before = map.get_state();
277         map.add_tempo (Tempo (bpm), requested);
278         XMLNode &after = map.get_state();
279         session->add_command(MementoCommand<TempoMap>(map, before, after));
280         commit_reversible_command ();
281         
282         map.dump (cerr);
283 }
284
285 void
286 Editor::mouse_add_new_meter_event (jack_nframes_t frame)
287 {
288         if (session == 0) {
289                 return;
290         }
291
292
293         TempoMap& map(session->tempo_map());
294         MeterDialog meter_dialog (map, frame, _("add"));
295
296         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
297         meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
298
299         ensure_float (meter_dialog);
300         
301         switch (meter_dialog.run ()) {
302         case RESPONSE_ACCEPT:
303                 break;
304         default:
305                 return;
306         }
307
308         double bpb = meter_dialog.get_bpb ();
309         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
310         
311         double note_type = meter_dialog.get_note_type ();
312         BBT_Time requested;
313         
314         meter_dialog.get_bbt_time (requested);
315         
316         begin_reversible_command (_("add meter mark"));
317         XMLNode &before = map.get_state();
318         map.add_meter (Meter (bpb, note_type), requested);
319         session->add_command(MementoCommand<TempoMap>(map, before, map.get_state()));
320         commit_reversible_command ();
321         
322         map.dump (cerr);
323 }
324
325 void
326 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
327 {
328         Marker* marker;
329         TempoMarker* tempo_marker;
330
331         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
332                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
333                 /*NOTREACHED*/
334         }
335
336         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
337                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
338                 /*NOTREACHED*/
339         }               
340
341         if (tempo_marker->tempo().movable()) {
342           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
343         }
344 }
345
346 void
347 Editor::edit_meter_section (MeterSection* section)
348 {
349         MeterDialog meter_dialog (*section, _("done"));
350
351         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
352
353         ensure_float (meter_dialog);
354
355         switch (meter_dialog.run()) {
356         case RESPONSE_ACCEPT:
357                 break;
358         default:
359                 return;
360         }
361
362         double bpb = meter_dialog.get_bpb ();
363         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
364         
365         double note_type = meter_dialog.get_note_type ();
366
367         begin_reversible_command (_("replace tempo mark"));
368         XMLNode &before = session->tempo_map().get_state();
369         session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
370         XMLNode &before = session->tempo_map().get_state();
371         session->add_command(MementoCommand<TempoMap>(session->tempo_map(), before, after));
372         commit_reversible_command ();
373 }
374
375 void
376 Editor::edit_tempo_section (TempoSection* section)
377 {
378         TempoDialog tempo_dialog (*section, _("done"));
379
380         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
381
382         ensure_float (tempo_dialog);
383         
384         switch (tempo_dialog.run ()) {
385         case RESPONSE_ACCEPT:
386                 break;
387         default:
388                 return;
389         }
390
391         double bpm = tempo_dialog.get_bpm ();
392         BBT_Time when;
393         tempo_dialog.get_bbt_time(when);
394         bpm = max (0.01, bpm);
395         
396         begin_reversible_command (_("replace tempo mark"));
397         XMLNode &before = session->tempo_map().get_state();
398         session->tempo_map().replace_tempo (*section, Tempo (bpm));
399         session->tempo_map().move_tempo (*section, when);
400         XMLNode &after = session->tempo_map().get_state();
401         session->add_command (MementoCommand<TempoMap>(session->tempo_map(), before, after));
402         commit_reversible_command ();
403 }
404
405 void
406 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
407 {
408         Marker* marker;
409         TempoMarker* tempo_marker;
410
411         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
412                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
413                 /*NOTREACHED*/
414         }
415
416         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
417                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
418                 /*NOTREACHED*/
419         }               
420
421         edit_tempo_section (&tempo_marker->tempo());
422 }
423
424 void
425 Editor::edit_meter_marker (ArdourCanvas::Item *item)
426 {
427         Marker* marker;
428         MeterMarker* meter_marker;
429
430         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
431                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
432                 /*NOTREACHED*/
433         }
434
435         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
436                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
437                 /*NOTREACHED*/
438         }               
439         
440         edit_meter_section (&meter_marker->meter());
441 }
442
443 gint
444 Editor::real_remove_tempo_marker (TempoSection *section)
445 {
446         begin_reversible_command (_("remove tempo mark"));
447         XMLNode &before = session->tempo_map().get_state();
448         session->tempo_map().remove_tempo (*section);
449         XMLNode &after = session->tempo_map().get_state();
450         session->add_command(MementoCommand<TempoMap>(session->tempo_map(), before, after));
451         commit_reversible_command ();
452
453         return FALSE;
454 }
455
456 void
457 Editor::remove_meter_marker (ArdourCanvas::Item* item)
458 {
459         Marker* marker;
460         MeterMarker* meter_marker;
461
462         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
463                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
464                 /*NOTREACHED*/
465         }
466
467         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
468                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
469                 /*NOTREACHED*/
470         }               
471
472         if (meter_marker->meter().movable()) {
473           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
474         }
475 }
476
477 gint
478 Editor::real_remove_meter_marker (MeterSection *section)
479 {
480         begin_reversible_command (_("remove tempo mark"));
481         XMLNode &before = session->tempo_map().get_state();
482         session->tempo_map().remove_meter (*section);
483         XMLNode &after = session->tempo_map().get_state();
484         session->add_command(MementoCommand<TempoMap>(session->tempo_map(), before, after));
485         commit_reversible_command ();
486         return FALSE;
487 }