use filechooser widget in export dialog, selected files set format combos, hide progr...
[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 */
19
20 #include <cstdio> // for sprintf, grrr 
21 #include <cstdlib>
22 #include <cmath>
23 #include <string>
24 #include <climits>
25
26 #include <libgnomecanvasmm.h>
27
28 #include <pbd/error.h>
29 #include <pbd/memento_command.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 "time_axis_view.h"
46 #include "ardour_ui.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, ARDOUR_UI::config()->canvasvar_MeterMarker.get(), 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, ARDOUR_UI::config()->canvasvar_TempoMarker.get(), buf, 
88                                                                  *(const_cast<TempoSection*>(ts))));
89                 }
90                 
91         }
92
93 }
94
95 void
96 Editor::tempo_map_changed (Change ignored)
97 {
98         if (!session) {
99                 return;
100         }
101
102         ENSURE_GUI_THREAD(bind (mem_fun (*this, &Editor::tempo_map_changed), ignored));
103
104         compute_current_bbt_points(leftmost_frame, leftmost_frame + (nframes_t)(canvas_width * frames_per_unit));
105         session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
106         update_tempo_based_rulers ();
107         if (tempo_map_change_idle_handler_id  < 0) {
108                         tempo_map_change_idle_handler_id = Glib::signal_idle().connect (mem_fun (*this, &Editor::redraw_measures));
109         }
110 }
111
112 void
113 Editor::redisplay_tempo (bool immediate_redraw)
114 {
115         if (!session) {
116                 return;
117         }
118         
119         compute_current_bbt_points (leftmost_frame, leftmost_frame + (nframes_t)(canvas_width * frames_per_unit)); // redraw rulers and measures
120
121         if (immediate_redraw) {
122
123                 hide_measures ();
124
125                 if (current_bbt_points) {
126                         draw_measures ();
127                 }
128
129         } else if (tempo_map_change_idle_handler_id  < 0) {
130
131                 tempo_map_change_idle_handler_id = Glib::signal_idle().connect (mem_fun (*this, &Editor::redraw_measures));
132
133         }
134
135         update_tempo_based_rulers ();
136 }
137
138 void
139 Editor::compute_current_bbt_points (nframes_t leftmost, nframes_t rightmost)
140 {
141         if (!session) {
142                 return;
143         }
144
145         BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
146
147         session->bbt_time(leftmost, previous_beat);
148         session->bbt_time(rightmost, next_beat);
149
150         if (previous_beat.beats > 1) {
151                 previous_beat.beats -= 1;
152         } else if (previous_beat.bars > 1) {
153                 previous_beat.bars--;
154                 previous_beat.beats += 1;
155         }
156         previous_beat.ticks = 0;
157
158         if (session->tempo_map().meter_at(rightmost).beats_per_bar () > next_beat.beats + 1) {
159                 next_beat.beats += 1;
160         } else {
161                 next_beat.bars += 1;
162                 next_beat.beats = 1;
163         }
164         next_beat.ticks = 0;
165         
166         if (current_bbt_points) {
167                 delete current_bbt_points;
168                 current_bbt_points = 0;
169         }
170
171         current_bbt_points = session->tempo_map().get_points (session->tempo_map().frame_time (previous_beat), session->tempo_map().frame_time (next_beat) + 1);
172 }
173
174 void
175 Editor::hide_measures ()
176 {
177         tempo_lines->hide();
178         marker_tempo_lines->hide();
179 }
180
181 bool
182 Editor::redraw_measures ()
183 {
184         hide_measures ();
185         draw_measures ();
186         tempo_map_change_idle_handler_id = -1;
187         return false;
188 }
189
190 void
191 Editor::draw_measures ()
192 {
193         if (session == 0 || _show_measures == false
194                         || !current_bbt_points || current_bbt_points->empty()) {
195                 return;
196         }
197
198         tempo_lines->draw(*current_bbt_points, frames_per_unit);
199         marker_tempo_lines->draw(*current_bbt_points, frames_per_unit);
200
201         /*time_line_group->raise_to_top();
202         time_line_group->lower(1);*/
203         marker_time_line_group->raise_to_top();
204         //marker_time_line_group->lower(1);
205
206         /* the cursors are always on top of everything */
207         cursor_group->raise_to_top();
208
209         return;
210 }
211
212 void
213 Editor::mouse_add_new_tempo_event (nframes_t frame)
214 {
215         if (session == 0) {
216                 return;
217         }
218
219         TempoMap& map(session->tempo_map());
220         TempoDialog tempo_dialog (map, frame, _("add"));
221         
222         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
223         tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
224
225         ensure_float (tempo_dialog);
226
227         switch (tempo_dialog.run()) {
228         case RESPONSE_ACCEPT:
229                 break;
230         default:
231                 return;
232         }
233
234         double bpm = 0;
235         BBT_Time requested;
236         
237         bpm = tempo_dialog.get_bpm ();
238         bpm = max (0.01, bpm);
239         
240         tempo_dialog.get_bbt_time (requested);
241         
242         begin_reversible_command (_("add tempo mark"));
243         XMLNode &before = map.get_state();
244         map.add_tempo (Tempo (bpm), requested);
245         XMLNode &after = map.get_state();
246         session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
247         commit_reversible_command ();
248         
249         map.dump (cerr);
250 }
251
252 void
253 Editor::mouse_add_new_meter_event (nframes_t frame)
254 {
255         if (session == 0) {
256                 return;
257         }
258
259
260         TempoMap& map(session->tempo_map());
261         MeterDialog meter_dialog (map, frame, _("add"));
262
263         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
264         meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
265
266         ensure_float (meter_dialog);
267         
268         switch (meter_dialog.run ()) {
269         case RESPONSE_ACCEPT:
270                 break;
271         default:
272                 return;
273         }
274
275         double bpb = meter_dialog.get_bpb ();
276         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
277         
278         double note_type = meter_dialog.get_note_type ();
279         BBT_Time requested;
280         
281         meter_dialog.get_bbt_time (requested);
282         
283         begin_reversible_command (_("add meter mark"));
284         XMLNode &before = map.get_state();
285         map.add_meter (Meter (bpb, note_type), requested);
286         session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
287         commit_reversible_command ();
288         
289         map.dump (cerr);
290 }
291
292 void
293 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
294 {
295         Marker* marker;
296         TempoMarker* tempo_marker;
297
298         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
299                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
300                 /*NOTREACHED*/
301         }
302
303         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
304                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
305                 /*NOTREACHED*/
306         }               
307
308         if (tempo_marker->tempo().movable()) {
309           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
310         }
311 }
312
313 void
314 Editor::edit_meter_section (MeterSection* section)
315 {
316         MeterDialog meter_dialog (*section, _("done"));
317
318         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
319
320         ensure_float (meter_dialog);
321
322         switch (meter_dialog.run()) {
323         case RESPONSE_ACCEPT:
324                 break;
325         default:
326                 return;
327         }
328
329         double bpb = meter_dialog.get_bpb ();
330         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
331         
332         double note_type = meter_dialog.get_note_type ();
333
334         begin_reversible_command (_("replace tempo mark"));
335         XMLNode &before = session->tempo_map().get_state();
336         session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
337         XMLNode &after = session->tempo_map().get_state();
338         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
339         commit_reversible_command ();
340 }
341
342 void
343 Editor::edit_tempo_section (TempoSection* section)
344 {
345         TempoDialog tempo_dialog (*section, _("done"));
346
347         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
348
349         ensure_float (tempo_dialog);
350         
351         switch (tempo_dialog.run ()) {
352         case RESPONSE_ACCEPT:
353                 break;
354         default:
355                 return;
356         }
357
358         double bpm = tempo_dialog.get_bpm ();
359         BBT_Time when;
360         tempo_dialog.get_bbt_time(when);
361         bpm = max (0.01, bpm);
362         
363         begin_reversible_command (_("replace tempo mark"));
364         XMLNode &before = session->tempo_map().get_state();
365         session->tempo_map().replace_tempo (*section, Tempo (bpm));
366         session->tempo_map().move_tempo (*section, when);
367         XMLNode &after = session->tempo_map().get_state();
368         session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
369         commit_reversible_command ();
370 }
371
372 void
373 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
374 {
375         Marker* marker;
376         TempoMarker* tempo_marker;
377
378         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
379                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
380                 /*NOTREACHED*/
381         }
382
383         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
384                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
385                 /*NOTREACHED*/
386         }               
387
388         edit_tempo_section (&tempo_marker->tempo());
389 }
390
391 void
392 Editor::edit_meter_marker (ArdourCanvas::Item *item)
393 {
394         Marker* marker;
395         MeterMarker* meter_marker;
396
397         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
398                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
399                 /*NOTREACHED*/
400         }
401
402         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
403                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
404                 /*NOTREACHED*/
405         }               
406         
407         edit_meter_section (&meter_marker->meter());
408 }
409
410 gint
411 Editor::real_remove_tempo_marker (TempoSection *section)
412 {
413         begin_reversible_command (_("remove tempo mark"));
414         XMLNode &before = session->tempo_map().get_state();
415         session->tempo_map().remove_tempo (*section);
416         XMLNode &after = session->tempo_map().get_state();
417         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
418         commit_reversible_command ();
419
420         return FALSE;
421 }
422
423 void
424 Editor::remove_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: meter 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         if (meter_marker->meter().movable()) {
440           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
441         }
442 }
443
444 gint
445 Editor::real_remove_meter_marker (MeterSection *section)
446 {
447         begin_reversible_command (_("remove tempo mark"));
448         XMLNode &before = session->tempo_map().get_state();
449         session->tempo_map().remove_meter (*section);
450         XMLNode &after = session->tempo_map().get_state();
451         session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
452         commit_reversible_command ();
453
454         return FALSE;
455 }