various tweaks to get clicks on processor "buttons" to work right and to keep track...
[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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <cstdio> // for sprintf, grrr
25 #include <cstdlib>
26 #include <cmath>
27 #include <string>
28 #include <climits>
29
30 #include <libgnomecanvasmm.h>
31
32 #include "pbd/error.h"
33 #include "pbd/memento_command.h"
34
35 #include <gtkmm2ext/utils.h>
36 #include <gtkmm2ext/gtk_ui.h>
37
38 #include "ardour/session.h"
39 #include "ardour/tempo.h"
40 #include <gtkmm2ext/doi.h>
41 #include <gtkmm2ext/utils.h>
42
43 #include "editor.h"
44 #include "marker.h"
45 #include "simpleline.h"
46 #include "tempo_dialog.h"
47 #include "rgb_macros.h"
48 #include "gui_thread.h"
49 #include "time_axis_view.h"
50 #include "ardour_ui.h"
51 #include "tempo_lines.h"
52 #include "utils.h"
53
54 #include "i18n.h"
55
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Gtk;
60 using namespace Gtkmm2ext;
61 using namespace Editing;
62
63 void
64 Editor::remove_metric_marks ()
65 {
66         /* don't delete these while handling events, just punt till the GUI is idle */
67
68         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
69                 delete_when_idle (*x);
70         }
71         metric_marks.clear ();
72 }
73
74 void
75 Editor::draw_metric_marks (const Metrics& metrics)
76 {
77
78         const MeterSection *ms;
79         const TempoSection *ts;
80         char buf[64];
81
82         remove_metric_marks ();
83
84         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
85
86                 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
87                         snprintf (buf, sizeof(buf), "%g/%g", ms->beats_per_bar(), ms->note_divisor ());
88                         metric_marks.push_back (new MeterMarker (*this, *meter_group, ARDOUR_UI::config()->canvasvar_MeterMarker.get(), buf,
89                                                                  *(const_cast<MeterSection*>(ms))));
90                 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
91                         snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
92                         metric_marks.push_back (new TempoMarker (*this, *tempo_group, ARDOUR_UI::config()->canvasvar_TempoMarker.get(), buf,
93                                                                  *(const_cast<TempoSection*>(ts))));
94                 }
95
96         }
97
98 }
99
100 void
101 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
102 {
103         if (!_session) {
104                 return;
105         }
106
107         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
108
109         if (tempo_lines) {
110                 tempo_lines->tempo_map_changed();
111         }
112
113         compute_current_bbt_points(leftmost_frame, leftmost_frame + current_page_frames());
114         _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
115         redraw_measures ();
116         update_tempo_based_rulers ();
117 }
118
119 void
120 Editor::redisplay_tempo (bool immediate_redraw)
121 {
122         if (!_session) {
123                 return;
124         }
125
126         compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_frames()); // redraw rulers and measures
127
128         if (immediate_redraw) {
129                 redraw_measures ();
130         } else {
131 #ifdef GTKOSX
132                 redraw_measures ();
133 #else
134                 Glib::signal_idle().connect (sigc::mem_fun (*this, &Editor::redraw_measures));
135 #endif
136         }
137         update_tempo_based_rulers (); // redraw rulers and measures
138 }
139
140 void
141 Editor::compute_current_bbt_points (framepos_t leftmost, framepos_t rightmost)
142 {
143         if (!_session) {
144                 return;
145         }
146
147         Timecode::BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
148
149         _session->bbt_time(leftmost, previous_beat);
150         _session->bbt_time(rightmost, next_beat);
151
152         if (previous_beat.beats > 1) {
153                 previous_beat.beats -= 1;
154         } else if (previous_beat.bars > 1) {
155                 previous_beat.bars--;
156                 previous_beat.beats += 1;
157         }
158         previous_beat.ticks = 0;
159
160         if (_session->tempo_map().meter_at(rightmost).beats_per_bar () > next_beat.beats + 1) {
161                 next_beat.beats += 1;
162         } else {
163                 next_beat.bars += 1;
164                 next_beat.beats = 1;
165         }
166         next_beat.ticks = 0;
167
168         delete current_bbt_points;
169         current_bbt_points = 0;
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         if (tempo_lines)
178                 tempo_lines->hide();
179 }
180
181 bool
182 Editor::redraw_measures ()
183 {
184         draw_measures ();
185         return false;
186 }
187
188 void
189 Editor::draw_measures ()
190 {
191         if (_session == 0 || _show_measures == false ||
192             !current_bbt_points || current_bbt_points->empty()) {
193                 return;
194         }
195
196         if (tempo_lines == 0) {
197                 tempo_lines = new TempoLines(*track_canvas, time_line_group, physical_screen_height(get_window()));
198         }
199
200         tempo_lines->draw(*current_bbt_points, frames_per_unit);
201 }
202
203 void
204 Editor::mouse_add_new_tempo_event (framepos_t frame)
205 {
206         if (_session == 0) {
207                 return;
208         }
209
210         TempoMap& map(_session->tempo_map());
211         TempoDialog tempo_dialog (map, frame, _("add"));
212
213         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
214         //this causes compiz to display no border.
215         //tempo_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
216
217         ensure_float (tempo_dialog);
218
219         switch (tempo_dialog.run()) {
220         case RESPONSE_ACCEPT:
221                 break;
222         default:
223                 return;
224         }
225
226         double bpm = 0;
227         Timecode::BBT_Time requested;
228
229         bpm = tempo_dialog.get_bpm ();
230         double nt = tempo_dialog.get_note_type();
231         bpm = max (0.01, bpm);
232
233         tempo_dialog.get_bbt_time (requested);
234
235         begin_reversible_command (_("add tempo mark"));
236         XMLNode &before = map.get_state();
237         map.add_tempo (Tempo (bpm,nt), requested);
238         XMLNode &after = map.get_state();
239         _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
240         commit_reversible_command ();
241
242         //map.dump (cerr);
243 }
244
245 void
246 Editor::mouse_add_new_meter_event (framepos_t frame)
247 {
248         if (_session == 0) {
249                 return;
250         }
251
252
253         TempoMap& map(_session->tempo_map());
254         MeterDialog meter_dialog (map, frame, _("add"));
255
256         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
257
258         //this causes compiz to display no border..
259         //meter_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
260
261         ensure_float (meter_dialog);
262
263         switch (meter_dialog.run ()) {
264         case RESPONSE_ACCEPT:
265                 break;
266         default:
267                 return;
268         }
269
270         double bpb = meter_dialog.get_bpb ();
271         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
272
273         double note_type = meter_dialog.get_note_type ();
274         Timecode::BBT_Time requested;
275
276         meter_dialog.get_bbt_time (requested);
277
278         begin_reversible_command (_("add meter mark"));
279         XMLNode &before = map.get_state();
280         map.add_meter (Meter (bpb, note_type), requested);
281         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
282         commit_reversible_command ();
283
284         //map.dump (cerr);
285 }
286
287 void
288 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
289 {
290         Marker* marker;
291         TempoMarker* tempo_marker;
292
293         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
294                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
295                 /*NOTREACHED*/
296         }
297
298         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
299                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
300                 /*NOTREACHED*/
301         }
302
303         if (tempo_marker->tempo().movable()) {
304                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
305         }
306 }
307
308 void
309 Editor::edit_meter_section (MeterSection* section)
310 {
311         MeterDialog meter_dialog (*section, _("done"));
312
313         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
314
315         ensure_float (meter_dialog);
316
317         switch (meter_dialog.run()) {
318         case RESPONSE_ACCEPT:
319                 break;
320         default:
321                 return;
322         }
323
324         double bpb = meter_dialog.get_bpb ();
325         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
326
327         double note_type = meter_dialog.get_note_type ();
328
329         begin_reversible_command (_("replace tempo mark"));
330         XMLNode &before = _session->tempo_map().get_state();
331         _session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
332         XMLNode &after = _session->tempo_map().get_state();
333         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
334         commit_reversible_command ();
335 }
336
337 void
338 Editor::edit_tempo_section (TempoSection* section)
339 {
340         TempoDialog tempo_dialog (*section, _("done"));
341
342         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
343
344         ensure_float (tempo_dialog);
345
346         switch (tempo_dialog.run ()) {
347         case RESPONSE_ACCEPT:
348                 break;
349         default:
350                 return;
351         }
352
353         double bpm = tempo_dialog.get_bpm ();
354         double nt = tempo_dialog.get_note_type ();
355         Timecode::BBT_Time when;
356         tempo_dialog.get_bbt_time(when);
357         bpm = max (0.01, bpm);
358
359         cerr << "Editing tempo section to be at " << when << endl;
360         _session->tempo_map().dump (cerr);
361         begin_reversible_command (_("replace tempo mark"));
362         XMLNode &before = _session->tempo_map().get_state();
363         _session->tempo_map().replace_tempo (*section, Tempo (bpm,nt));
364         _session->tempo_map().dump (cerr);
365         _session->tempo_map().move_tempo (*section, when);
366         _session->tempo_map().dump (cerr);
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 (sigc::bind (sigc::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 }