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