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.
21 #include "gtk2ardour-config.h"
24 #include <cstdio> // for sprintf, grrr
30 #include <libgnomecanvasmm.h>
32 #include "pbd/error.h"
33 #include "pbd/memento_command.h"
35 #include <gtkmm2ext/utils.h>
36 #include <gtkmm2ext/gtk_ui.h>
38 #include "ardour/session.h"
39 #include "ardour/tempo.h"
40 #include <gtkmm2ext/doi.h>
41 #include <gtkmm2ext/utils.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"
57 using namespace ARDOUR;
60 using namespace Gtkmm2ext;
61 using namespace Editing;
64 Editor::remove_metric_marks ()
66 /* don't delete these while handling events, just punt till the GUI is idle */
68 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
69 delete_when_idle (*x);
71 metric_marks.clear ();
75 Editor::draw_metric_marks (const Metrics& metrics)
78 const MeterSection *ms;
79 const TempoSection *ts;
82 remove_metric_marks ();
84 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
86 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
87 snprintf (buf, sizeof(buf), "%g/%g", ms->divisions_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 if (Config->get_allow_non_quarter_pulse()) {
92 snprintf (buf, sizeof (buf), "%.2f/%.0f", ts->beats_per_minute(), ts->note_type());
94 snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
96 metric_marks.push_back (new TempoMarker (*this, *tempo_group, ARDOUR_UI::config()->canvasvar_TempoMarker.get(), buf,
97 *(const_cast<TempoSection*>(ts))));
105 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
111 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
114 tempo_lines->tempo_map_changed();
117 compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_frames());
118 _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
120 update_tempo_based_rulers ();
124 Editor::redisplay_tempo (bool immediate_redraw)
130 compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_frames()); // redraw rulers and measures
132 if (immediate_redraw) {
138 Glib::signal_idle().connect (sigc::mem_fun (*this, &Editor::redraw_measures));
141 update_tempo_based_rulers (); // redraw rulers and measures
145 Editor::compute_current_bbt_points (framepos_t leftmost, framepos_t rightmost)
151 Timecode::BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
153 _session->bbt_time(leftmost, previous_beat);
154 _session->bbt_time(rightmost, next_beat);
156 if (previous_beat.beats > 1) {
157 previous_beat.beats -= 1;
158 } else if (previous_beat.bars > 1) {
159 previous_beat.bars--;
160 previous_beat.beats += 1;
162 previous_beat.ticks = 0;
164 if (_session->tempo_map().meter_at(rightmost).divisions_per_bar () > next_beat.beats + 1) {
165 next_beat.beats += 1;
172 current_bbt_points.clear();
173 _session->tempo_map().map (current_bbt_points, _session->tempo_map().frame_time (previous_beat), _session->tempo_map().frame_time (next_beat) + 1);
177 Editor::hide_measures ()
184 Editor::redraw_measures ()
191 Editor::draw_measures ()
193 if (_session == 0 || _show_measures == false || current_bbt_points.empty()) {
197 if (tempo_lines == 0) {
198 tempo_lines = new TempoLines(*track_canvas, time_line_group, physical_screen_height(get_window()));
201 tempo_lines->draw (current_bbt_points, frames_per_unit);
205 Editor::mouse_add_new_tempo_event (framepos_t frame)
211 TempoMap& map(_session->tempo_map());
212 TempoDialog tempo_dialog (map, frame, _("add"));
214 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
215 //this causes compiz to display no border.
216 //tempo_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
218 ensure_float (tempo_dialog);
220 switch (tempo_dialog.run()) {
221 case RESPONSE_ACCEPT:
228 Timecode::BBT_Time requested;
230 bpm = tempo_dialog.get_bpm ();
231 double nt = tempo_dialog.get_note_type();
232 bpm = max (0.01, bpm);
234 tempo_dialog.get_bbt_time (requested);
236 begin_reversible_command (_("add tempo mark"));
237 XMLNode &before = map.get_state();
238 map.add_tempo (Tempo (bpm,nt), requested);
239 XMLNode &after = map.get_state();
240 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
241 commit_reversible_command ();
247 Editor::mouse_add_new_meter_event (framepos_t frame)
254 TempoMap& map(_session->tempo_map());
255 MeterDialog meter_dialog (map, frame, _("add"));
257 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
259 //this causes compiz to display no border..
260 //meter_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
262 ensure_float (meter_dialog);
264 switch (meter_dialog.run ()) {
265 case RESPONSE_ACCEPT:
271 double bpb = meter_dialog.get_bpb ();
272 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
274 double note_type = meter_dialog.get_note_type ();
275 Timecode::BBT_Time requested;
277 meter_dialog.get_bbt_time (requested);
279 begin_reversible_command (_("add meter mark"));
280 XMLNode &before = map.get_state();
281 map.add_meter (Meter (bpb, note_type), requested);
282 _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
283 commit_reversible_command ();
289 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
292 TempoMarker* tempo_marker;
294 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
295 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
299 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
300 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
304 if (tempo_marker->tempo().movable()) {
305 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
310 Editor::edit_meter_section (MeterSection* section)
312 MeterDialog meter_dialog (*section, _("done"));
314 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
316 ensure_float (meter_dialog);
318 switch (meter_dialog.run()) {
319 case RESPONSE_ACCEPT:
325 double bpb = meter_dialog.get_bpb ();
326 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
328 double note_type = meter_dialog.get_note_type ();
330 Timecode::BBT_Time when;
331 meter_dialog.get_bbt_time(when);
333 begin_reversible_command (_("replace tempo mark"));
334 XMLNode &before = _session->tempo_map().get_state();
335 _session->tempo_map().replace_meter (*section, Meter (bpb, note_type), when);
336 XMLNode &after = _session->tempo_map().get_state();
337 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
338 commit_reversible_command ();
342 Editor::edit_tempo_section (TempoSection* section)
344 TempoDialog tempo_dialog (*section, _("done"));
346 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
348 ensure_float (tempo_dialog);
350 switch (tempo_dialog.run ()) {
351 case RESPONSE_ACCEPT:
357 double bpm = tempo_dialog.get_bpm ();
358 double nt = tempo_dialog.get_note_type ();
359 Timecode::BBT_Time when;
360 tempo_dialog.get_bbt_time(when);
361 bpm = max (0.01, bpm);
363 begin_reversible_command (_("replace tempo mark"));
364 XMLNode &before = _session->tempo_map().get_state();
365 _session->tempo_map().replace_tempo (*section, Tempo (bpm, nt), when);
366 XMLNode &after = _session->tempo_map().get_state();
367 _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
368 commit_reversible_command ();
372 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
375 TempoMarker* tempo_marker;
377 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
378 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
382 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
383 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
387 edit_tempo_section (&tempo_marker->tempo());
391 Editor::edit_meter_marker (ArdourCanvas::Item *item)
394 MeterMarker* meter_marker;
396 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
397 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
401 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
402 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
406 edit_meter_section (&meter_marker->meter());
410 Editor::real_remove_tempo_marker (TempoSection *section)
412 begin_reversible_command (_("remove tempo mark"));
413 XMLNode &before = _session->tempo_map().get_state();
414 _session->tempo_map().remove_tempo (*section, true);
415 XMLNode &after = _session->tempo_map().get_state();
416 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
417 commit_reversible_command ();
423 Editor::remove_meter_marker (ArdourCanvas::Item* item)
426 MeterMarker* meter_marker;
428 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
429 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
433 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
434 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
438 if (meter_marker->meter().movable()) {
439 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
444 Editor::real_remove_meter_marker (MeterSection *section)
446 begin_reversible_command (_("remove tempo mark"));
447 XMLNode &before = _session->tempo_map().get_state();
448 _session->tempo_map().remove_meter (*section, true);
449 XMLNode &after = _session->tempo_map().get_state();
450 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
451 commit_reversible_command ();