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