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 "pbd/error.h"
31 #include "pbd/memento_command.h"
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/gtk_ui.h>
36 #include "ardour/session.h"
37 #include "ardour/tempo.h"
38 #include <gtkmm2ext/doi.h>
39 #include <gtkmm2ext/utils.h>
41 #include "canvas/canvas.h"
42 #include "canvas/item.h"
43 #include "canvas/line_set.h"
47 #include "tempo_dialog.h"
48 #include "rgb_macros.h"
49 #include "gui_thread.h"
50 #include "time_axis_view.h"
51 #include "tempo_lines.h"
52 #include "ui_config.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, UIConfiguration::instance().color ("meter marker"), buf,
89 *(const_cast<MeterSection*>(ms))));
90 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
91 if (UIConfiguration::instance().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, UIConfiguration::instance().color ("tempo marker"), buf,
97 *(const_cast<TempoSection*>(ts))));
106 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
112 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
115 tempo_lines->tempo_map_changed();
118 std::vector<TempoMap::BBTPoint> grid;
119 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
120 _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
121 draw_measures (grid);
122 update_tempo_based_rulers (grid);
126 Editor::marker_position_changed ()
132 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
135 tempo_lines->tempo_map_changed();
137 TempoMarker* tempo_marker;
138 MeterMarker* meter_marker;
139 const TempoSection *ts;
140 const MeterSection *ms;
142 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
143 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
144 if ((ts = &tempo_marker->tempo()) != 0) {
145 tempo_marker->set_position (ts->frame ());
147 snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
148 tempo_marker->set_name (buf);
151 if ((meter_marker = dynamic_cast<MeterMarker*> (*x)) != 0) {
152 if ((ms = &meter_marker->meter()) != 0) {
153 meter_marker->set_position (ms->frame ());
157 std::vector<TempoMap::BBTPoint> grid;
158 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
159 draw_measures (grid);
160 update_tempo_based_rulers (grid);
164 Editor::redisplay_tempo (bool immediate_redraw)
170 if (immediate_redraw) {
171 std::vector<TempoMap::BBTPoint> grid;
173 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
174 draw_measures (grid);
175 update_tempo_based_rulers (grid); // redraw rulers and measure lines
178 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_tempo), true), false));
182 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
184 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, framepos_t leftmost, framepos_t rightmost)
190 /* prevent negative values of leftmost from creeping into tempomap
192 _session->tempo_map().get_grid (grid, max (leftmost, (framepos_t) 0), rightmost);
196 Editor::hide_measures ()
204 Editor::draw_measures (std::vector<ARDOUR::TempoMap::BBTPoint>& grid)
206 if (_session == 0 || _show_measures == false || distance (grid.begin(), grid.end()) == 0) {
210 if (tempo_lines == 0) {
211 tempo_lines = new TempoLines (time_line_group, ArdourCanvas::LineSet::Vertical);
214 const unsigned divisions = get_grid_beat_divisions(leftmost_frame);
215 tempo_lines->draw (grid, divisions, leftmost_frame, _session->frame_rate());
219 Editor::mouse_add_new_tempo_event (framepos_t frame)
225 TempoMap& map(_session->tempo_map());
226 TempoDialog tempo_dialog (map, frame, _("add"));
228 //this causes compiz to display no border.
229 //tempo_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
231 switch (tempo_dialog.run()) {
232 case RESPONSE_ACCEPT:
239 Timecode::BBT_Time requested;
240 bpm = tempo_dialog.get_bpm ();
241 double nt = tempo_dialog.get_note_type();
242 bpm = max (0.01, bpm);
244 tempo_dialog.get_bbt_time (requested);
245 begin_reversible_command (_("add tempo mark"));
246 XMLNode &before = map.get_state();
247 if (tempo_dialog.get_lock_style() == MusicTime) {
248 map.add_tempo (Tempo (bpm,nt), map.pulse_at_beat (map.bbt_to_beats (requested)), tempo_dialog.get_tempo_type());
250 map.add_tempo (Tempo (bpm,nt), frame, tempo_dialog.get_tempo_type());
252 XMLNode &after = map.get_state();
253 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
254 commit_reversible_command ();
260 Editor::mouse_add_new_meter_event (framepos_t frame)
267 TempoMap& map(_session->tempo_map());
268 MeterDialog meter_dialog (map, frame, _("add"));
270 //this causes compiz to display no border..
271 //meter_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
273 switch (meter_dialog.run ()) {
274 case RESPONSE_ACCEPT:
280 double bpb = meter_dialog.get_bpb ();
281 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
283 double note_type = meter_dialog.get_note_type ();
284 Timecode::BBT_Time requested;
286 meter_dialog.get_bbt_time (requested);
288 begin_reversible_command (_("add meter mark"));
289 XMLNode &before = map.get_state();
290 if (meter_dialog.get_lock_style() == MusicTime) {
291 map.add_meter (Meter (bpb, note_type), map.bbt_to_beats (requested), requested);
293 map.add_meter (Meter (bpb, note_type), frame, meter_dialog.get_lock_style());
295 _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
296 commit_reversible_command ();
302 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
304 ArdourMarker* marker;
305 TempoMarker* tempo_marker;
307 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
308 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
309 abort(); /*NOTREACHED*/
312 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
313 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
314 abort(); /*NOTREACHED*/
317 if (tempo_marker->tempo().movable()) {
318 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
323 Editor::edit_meter_section (MeterSection* section)
325 MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
327 switch (meter_dialog.run()) {
328 case RESPONSE_ACCEPT:
334 double bpb = meter_dialog.get_bpb ();
335 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
337 double const note_type = meter_dialog.get_note_type ();
338 Timecode::BBT_Time when;
339 meter_dialog.get_bbt_time(when);
340 framepos_t const frame = _session->tempo_map().frame_at_beat (_session->tempo_map().bbt_to_beats (when));
342 begin_reversible_command (_("replace meter mark"));
343 XMLNode &before = _session->tempo_map().get_state();
344 section->set_position_lock_style (meter_dialog.get_lock_style());
345 if (meter_dialog.get_lock_style() == MusicTime) {
346 _session->tempo_map().replace_meter (*section, Meter (bpb, note_type), when);
348 _session->tempo_map().replace_meter (*section, Meter (bpb, note_type), frame);
350 XMLNode &after = _session->tempo_map().get_state();
351 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
352 commit_reversible_command ();
356 Editor::edit_tempo_section (TempoSection* section)
358 TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
360 switch (tempo_dialog.run ()) {
361 case RESPONSE_ACCEPT:
367 double bpm = tempo_dialog.get_bpm ();
368 double nt = tempo_dialog.get_note_type ();
369 Timecode::BBT_Time when;
371 tempo_dialog.get_bbt_time (when);
372 double const beat = _session->tempo_map().bbt_to_beats (when);
374 bpm = max (0.01, bpm);
376 begin_reversible_command (_("replace tempo mark"));
377 XMLNode &before = _session->tempo_map().get_state();
379 if (tempo_dialog.get_lock_style() == MusicTime) {
380 section->set_position_lock_style (MusicTime);
381 framepos_t const f = _session->tempo_map().predict_tempo_frame (section, when);
382 double const p = _session->tempo_map().predict_tempo_pulse (section, f);
383 _session->tempo_map().replace_tempo (*section, Tempo (bpm, nt), p, tempo_dialog.get_tempo_type());
385 framepos_t const f = _session->tempo_map().predict_tempo_frame (section, when);
386 _session->tempo_map().replace_tempo (*section, Tempo (bpm, nt), f, tempo_dialog.get_tempo_type());
389 XMLNode &after = _session->tempo_map().get_state();
390 _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
391 commit_reversible_command ();
395 Editor::edit_tempo_marker (TempoMarker& tm)
397 edit_tempo_section (&tm.tempo());
401 Editor::edit_meter_marker (MeterMarker& mm)
403 edit_meter_section (&mm.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, true);
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)
422 ArdourMarker* marker;
423 MeterMarker* meter_marker;
425 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
426 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
427 abort(); /*NOTREACHED*/
430 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
431 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
432 abort(); /*NOTREACHED*/
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, true);
446 XMLNode &after = _session->tempo_map().get_state();
447 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
448 commit_reversible_command ();