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
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>
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"
53 using namespace ARDOUR;
56 using namespace Gtkmm2ext;
57 using namespace Editing;
60 Editor::remove_metric_marks ()
62 /* don't delete these while handling events, just punt till the GUI is idle */
64 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
65 delete_when_idle (*x);
67 metric_marks.clear ();
71 Editor::draw_metric_marks (const Metrics& metrics)
74 const MeterSection *ms;
75 const TempoSection *ts;
78 remove_metric_marks ();
80 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
82 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
83 snprintf (buf, sizeof(buf), "%g/%g", ms->beats_per_bar(), ms->note_divisor ());
84 metric_marks.push_back (new MeterMarker (*this, *meter_group, *cursor_group, ARDOUR_UI::config()->canvasvar_MeterMarker.get(), buf,
85 *(const_cast<MeterSection*>(ms))));
86 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
87 snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
88 metric_marks.push_back (new TempoMarker (*this, *tempo_group, *cursor_group, ARDOUR_UI::config()->canvasvar_TempoMarker.get(), buf,
89 *(const_cast<TempoSection*>(ts))));
97 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
103 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
106 tempo_lines->tempo_map_changed();
109 compute_current_bbt_points(leftmost_frame, leftmost_frame + current_page_frames());
110 _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
115 Editor::redisplay_tempo (bool immediate_redraw)
121 compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_frames()); // redraw rulers and measures
123 compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_frames());
124 if (immediate_redraw) {
130 Glib::signal_idle().connect (sigc::mem_fun (*this, &Editor::redraw_measures));
133 update_tempo_based_rulers (); // redraw rulers and measures
137 Editor::compute_current_bbt_points (nframes_t leftmost, nframes_t rightmost)
143 BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
145 _session->bbt_time(leftmost, previous_beat);
146 _session->bbt_time(rightmost, next_beat);
148 if (previous_beat.beats > 1) {
149 previous_beat.beats -= 1;
150 } else if (previous_beat.bars > 1) {
151 previous_beat.bars--;
152 previous_beat.beats += 1;
154 previous_beat.ticks = 0;
156 if (_session->tempo_map().meter_at(rightmost).beats_per_bar () > next_beat.beats + 1) {
157 next_beat.beats += 1;
164 delete current_bbt_points;
165 current_bbt_points = 0;
167 current_bbt_points = _session->tempo_map().get_points (_session->tempo_map().frame_time (previous_beat), _session->tempo_map().frame_time (next_beat) + 1);
171 Editor::hide_measures ()
178 Editor::redraw_measures ()
185 Editor::draw_measures ()
187 if (_session == 0 || _show_measures == false ||
188 !current_bbt_points || current_bbt_points->empty()) {
192 if (tempo_lines == 0) {
193 tempo_lines = new TempoLines(*track_canvas, time_line_group, physical_screen_height(get_window()));
196 tempo_lines->draw(*current_bbt_points, frames_per_unit);
200 Editor::mouse_add_new_tempo_event (framepos_t frame)
206 TempoMap& map(_session->tempo_map());
207 TempoDialog tempo_dialog (map, frame, _("add"));
209 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
210 //this causes compiz to display no border.
211 //tempo_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
213 ensure_float (tempo_dialog);
215 switch (tempo_dialog.run()) {
216 case RESPONSE_ACCEPT:
225 bpm = tempo_dialog.get_bpm ();
226 double nt = tempo_dialog.get_note_type();
227 bpm = max (0.01, bpm);
229 tempo_dialog.get_bbt_time (requested);
231 begin_reversible_command (_("add tempo mark"));
232 XMLNode &before = map.get_state();
233 map.add_tempo (Tempo (bpm,nt), requested);
234 XMLNode &after = map.get_state();
235 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
236 commit_reversible_command ();
242 Editor::mouse_add_new_meter_event (framepos_t frame)
249 TempoMap& map(_session->tempo_map());
250 MeterDialog meter_dialog (map, frame, _("add"));
252 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
254 //this causes compiz to display no border..
255 //meter_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
257 ensure_float (meter_dialog);
259 switch (meter_dialog.run ()) {
260 case RESPONSE_ACCEPT:
266 double bpb = meter_dialog.get_bpb ();
267 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
269 double note_type = meter_dialog.get_note_type ();
272 meter_dialog.get_bbt_time (requested);
274 begin_reversible_command (_("add meter mark"));
275 XMLNode &before = map.get_state();
276 map.add_meter (Meter (bpb, note_type), requested);
277 _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
278 commit_reversible_command ();
284 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
287 TempoMarker* tempo_marker;
289 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
290 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
294 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
295 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
299 if (tempo_marker->tempo().movable()) {
300 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
305 Editor::edit_meter_section (MeterSection* section)
307 MeterDialog meter_dialog (*section, _("done"));
309 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
311 ensure_float (meter_dialog);
313 switch (meter_dialog.run()) {
314 case RESPONSE_ACCEPT:
320 double bpb = meter_dialog.get_bpb ();
321 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
323 double note_type = meter_dialog.get_note_type ();
325 begin_reversible_command (_("replace tempo mark"));
326 XMLNode &before = _session->tempo_map().get_state();
327 _session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
328 XMLNode &after = _session->tempo_map().get_state();
329 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
330 commit_reversible_command ();
334 Editor::edit_tempo_section (TempoSection* section)
336 TempoDialog tempo_dialog (*section, _("done"));
338 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
340 ensure_float (tempo_dialog);
342 switch (tempo_dialog.run ()) {
343 case RESPONSE_ACCEPT:
349 double bpm = tempo_dialog.get_bpm ();
350 double nt = tempo_dialog.get_note_type ();
352 tempo_dialog.get_bbt_time(when);
353 bpm = max (0.01, bpm);
355 cerr << "Editing tempo section to be at " << when << endl;
356 _session->tempo_map().dump (cerr);
357 begin_reversible_command (_("replace tempo mark"));
358 XMLNode &before = _session->tempo_map().get_state();
359 _session->tempo_map().replace_tempo (*section, Tempo (bpm,nt));
360 _session->tempo_map().dump (cerr);
361 _session->tempo_map().move_tempo (*section, when);
362 _session->tempo_map().dump (cerr);
363 XMLNode &after = _session->tempo_map().get_state();
364 _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
365 commit_reversible_command ();
369 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
372 TempoMarker* tempo_marker;
374 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
375 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
379 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
380 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
384 edit_tempo_section (&tempo_marker->tempo());
388 Editor::edit_meter_marker (ArdourCanvas::Item *item)
391 MeterMarker* meter_marker;
393 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
394 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
398 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
399 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
403 edit_meter_section (&meter_marker->meter());
407 Editor::real_remove_tempo_marker (TempoSection *section)
409 begin_reversible_command (_("remove tempo mark"));
410 XMLNode &before = _session->tempo_map().get_state();
411 _session->tempo_map().remove_tempo (*section);
412 XMLNode &after = _session->tempo_map().get_state();
413 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
414 commit_reversible_command ();
420 Editor::remove_meter_marker (ArdourCanvas::Item* item)
423 MeterMarker* meter_marker;
425 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
426 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
430 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
431 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
435 if (meter_marker->meter().movable()) {
436 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
441 Editor::real_remove_meter_marker (MeterSection *section)
443 begin_reversible_command (_("remove tempo mark"));
444 XMLNode &before = _session->tempo_map().get_state();
445 _session->tempo_map().remove_meter (*section);
446 XMLNode &after = _session->tempo_map().get_state();
447 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
448 commit_reversible_command ();