put the issue of using a monitor section into ~/.config/ardour.rc, not the session...
[ardour2.git] / gtk2_ardour / editor_tempodisplay.cc
blobfd598b8c817d739f8bd6e4a65c596fac08bca4b1
1 /*
2 Copyright (C) 2002 Paul Davis
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.
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.
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.
20 #include <cstdio> // for sprintf, grrr
21 #include <cstdlib>
22 #include <cmath>
23 #include <string>
24 #include <climits>
26 #include <libgnomecanvasmm.h>
28 #include "pbd/error.h"
29 #include "pbd/memento_command.h"
31 #include <gtkmm2ext/utils.h>
32 #include <gtkmm2ext/gtk_ui.h>
34 #include "ardour/session.h"
35 #include "ardour/tempo.h"
36 #include <gtkmm2ext/doi.h>
37 #include <gtkmm2ext/utils.h>
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 #include "tempo_lines.h"
49 #include "i18n.h"
51 using namespace std;
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Gtk;
55 using namespace Gtkmm2ext;
56 using namespace Editing;
58 void
59 Editor::remove_metric_marks ()
61 /* don't delete these while handling events, just punt till the GUI is idle */
63 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
64 delete_when_idle (*x);
66 metric_marks.clear ();
69 void
70 Editor::draw_metric_marks (const Metrics& metrics)
73 const MeterSection *ms;
74 const TempoSection *ts;
75 char buf[64];
77 remove_metric_marks ();
79 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
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))));
95 void
96 Editor::tempo_map_changed (const PropertyChange& ignored)
98 if (!_session) {
99 return;
102 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
104 if (tempo_lines) {
105 tempo_lines->tempo_map_changed();
108 compute_current_bbt_points(leftmost_frame, leftmost_frame + current_page_frames());
109 _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
110 redraw_measures ();
113 void
114 Editor::redisplay_tempo (bool immediate_redraw)
116 if (!_session) {
117 return;
120 compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_frames()); // redraw rulers and measures
122 compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_frames());
123 if (immediate_redraw) {
124 redraw_measures ();
125 } else {
126 #ifdef GTKOSX
127 redraw_measures ();
128 #else
129 Glib::signal_idle().connect (sigc::mem_fun (*this, &Editor::redraw_measures));
130 #endif
132 update_tempo_based_rulers (); // redraw rulers and measures
135 void
136 Editor::compute_current_bbt_points (nframes_t leftmost, nframes_t rightmost)
138 if (!_session) {
139 return;
142 BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
144 _session->bbt_time(leftmost, previous_beat);
145 _session->bbt_time(rightmost, next_beat);
147 if (previous_beat.beats > 1) {
148 previous_beat.beats -= 1;
149 } else if (previous_beat.bars > 1) {
150 previous_beat.bars--;
151 previous_beat.beats += 1;
153 previous_beat.ticks = 0;
155 if (_session->tempo_map().meter_at(rightmost).beats_per_bar () > next_beat.beats + 1) {
156 next_beat.beats += 1;
157 } else {
158 next_beat.bars += 1;
159 next_beat.beats = 1;
161 next_beat.ticks = 0;
163 delete current_bbt_points;
164 current_bbt_points = 0;
166 current_bbt_points = _session->tempo_map().get_points (_session->tempo_map().frame_time (previous_beat), _session->tempo_map().frame_time (next_beat) + 1);
169 void
170 Editor::hide_measures ()
172 if (tempo_lines)
173 tempo_lines->hide();
176 bool
177 Editor::redraw_measures ()
179 draw_measures ();
180 return false;
183 void
184 Editor::draw_measures ()
186 if (_session == 0 || _show_measures == false ||
187 !current_bbt_points || current_bbt_points->empty()) {
188 return;
191 if (tempo_lines == 0) {
192 tempo_lines = new TempoLines(*track_canvas, time_line_group, physical_screen_height);
195 tempo_lines->draw(*current_bbt_points, frames_per_unit);
198 void
199 Editor::mouse_add_new_tempo_event (nframes64_t frame)
201 if (_session == 0) {
202 return;
205 TempoMap& map(_session->tempo_map());
206 TempoDialog tempo_dialog (map, frame, _("add"));
208 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
209 //this causes compiz to display no border.
210 //tempo_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
212 ensure_float (tempo_dialog);
214 switch (tempo_dialog.run()) {
215 case RESPONSE_ACCEPT:
216 break;
217 default:
218 return;
221 double bpm = 0;
222 BBT_Time requested;
224 bpm = tempo_dialog.get_bpm ();
225 double nt = tempo_dialog.get_note_type();
226 bpm = max (0.01, bpm);
228 tempo_dialog.get_bbt_time (requested);
230 begin_reversible_command (_("add tempo mark"));
231 XMLNode &before = map.get_state();
232 map.add_tempo (Tempo (bpm,nt), requested);
233 XMLNode &after = map.get_state();
234 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
235 commit_reversible_command ();
237 //map.dump (cerr);
240 void
241 Editor::mouse_add_new_meter_event (nframes64_t frame)
243 if (_session == 0) {
244 return;
248 TempoMap& map(_session->tempo_map());
249 MeterDialog meter_dialog (map, frame, _("add"));
251 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
253 //this causes compiz to display no border..
254 //meter_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
256 ensure_float (meter_dialog);
258 switch (meter_dialog.run ()) {
259 case RESPONSE_ACCEPT:
260 break;
261 default:
262 return;
265 double bpb = meter_dialog.get_bpb ();
266 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
268 double note_type = meter_dialog.get_note_type ();
269 BBT_Time requested;
271 meter_dialog.get_bbt_time (requested);
273 begin_reversible_command (_("add meter mark"));
274 XMLNode &before = map.get_state();
275 map.add_meter (Meter (bpb, note_type), requested);
276 _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
277 commit_reversible_command ();
279 //map.dump (cerr);
282 void
283 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
285 Marker* marker;
286 TempoMarker* tempo_marker;
288 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
289 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
290 /*NOTREACHED*/
293 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
294 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
295 /*NOTREACHED*/
298 if (tempo_marker->tempo().movable()) {
299 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
303 void
304 Editor::edit_meter_section (MeterSection* section)
306 MeterDialog meter_dialog (*section, _("done"));
308 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
310 ensure_float (meter_dialog);
312 switch (meter_dialog.run()) {
313 case RESPONSE_ACCEPT:
314 break;
315 default:
316 return;
319 double bpb = meter_dialog.get_bpb ();
320 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
322 double note_type = meter_dialog.get_note_type ();
324 begin_reversible_command (_("replace tempo mark"));
325 XMLNode &before = _session->tempo_map().get_state();
326 _session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
327 XMLNode &after = _session->tempo_map().get_state();
328 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
329 commit_reversible_command ();
332 void
333 Editor::edit_tempo_section (TempoSection* section)
335 TempoDialog tempo_dialog (*section, _("done"));
337 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
339 ensure_float (tempo_dialog);
341 switch (tempo_dialog.run ()) {
342 case RESPONSE_ACCEPT:
343 break;
344 default:
345 return;
348 double bpm = tempo_dialog.get_bpm ();
349 double nt = tempo_dialog.get_note_type ();
350 BBT_Time when;
351 tempo_dialog.get_bbt_time(when);
352 bpm = max (0.01, bpm);
354 cerr << "Editing tempo section to be at " << when << endl;
355 _session->tempo_map().dump (cerr);
356 begin_reversible_command (_("replace tempo mark"));
357 XMLNode &before = _session->tempo_map().get_state();
358 _session->tempo_map().replace_tempo (*section, Tempo (bpm,nt));
359 _session->tempo_map().dump (cerr);
360 _session->tempo_map().move_tempo (*section, when);
361 _session->tempo_map().dump (cerr);
362 XMLNode &after = _session->tempo_map().get_state();
363 _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
364 commit_reversible_command ();
367 void
368 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
370 Marker* marker;
371 TempoMarker* tempo_marker;
373 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
374 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
375 /*NOTREACHED*/
378 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
379 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
380 /*NOTREACHED*/
383 edit_tempo_section (&tempo_marker->tempo());
386 void
387 Editor::edit_meter_marker (ArdourCanvas::Item *item)
389 Marker* marker;
390 MeterMarker* meter_marker;
392 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
393 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
394 /*NOTREACHED*/
397 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
398 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
399 /*NOTREACHED*/
402 edit_meter_section (&meter_marker->meter());
405 gint
406 Editor::real_remove_tempo_marker (TempoSection *section)
408 begin_reversible_command (_("remove tempo mark"));
409 XMLNode &before = _session->tempo_map().get_state();
410 _session->tempo_map().remove_tempo (*section);
411 XMLNode &after = _session->tempo_map().get_state();
412 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
413 commit_reversible_command ();
415 return FALSE;
418 void
419 Editor::remove_meter_marker (ArdourCanvas::Item* item)
421 Marker* marker;
422 MeterMarker* meter_marker;
424 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
425 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
426 /*NOTREACHED*/
429 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
430 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
431 /*NOTREACHED*/
434 if (meter_marker->meter().movable()) {
435 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
439 gint
440 Editor::real_remove_meter_marker (MeterSection *section)
442 begin_reversible_command (_("remove tempo mark"));
443 XMLNode &before = _session->tempo_map().get_state();
444 _session->tempo_map().remove_meter (*section);
445 XMLNode &after = _session->tempo_map().get_state();
446 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
447 commit_reversible_command ();
449 return FALSE;