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 "ardour_ui.h"
52 #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()->color ("meter marker"), buf,
89 *(const_cast<MeterSection*>(ms))));
90 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
91 if (ARDOUR_UI::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()->color ("tempo marker"), 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 ARDOUR::TempoMap::BBTPointList::const_iterator begin;
118 ARDOUR::TempoMap::BBTPointList::const_iterator end;
120 compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_samples(), begin, end);
121 _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
122 draw_measures (begin, end);
123 update_tempo_based_rulers (begin, end);
127 Editor::redisplay_tempo (bool immediate_redraw)
133 if (immediate_redraw) {
134 ARDOUR::TempoMap::BBTPointList::const_iterator current_bbt_points_begin;
135 ARDOUR::TempoMap::BBTPointList::const_iterator current_bbt_points_end;
137 compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_samples(),
138 current_bbt_points_begin, current_bbt_points_end);
139 draw_measures (current_bbt_points_begin, current_bbt_points_end);
140 update_tempo_based_rulers (current_bbt_points_begin, current_bbt_points_end); // redraw rulers and measures
143 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_tempo), true), false));
148 Editor::compute_current_bbt_points (framepos_t leftmost, framepos_t rightmost,
149 ARDOUR::TempoMap::BBTPointList::const_iterator& begin,
150 ARDOUR::TempoMap::BBTPointList::const_iterator& end)
156 /* prevent negative values of leftmost from creeping into tempomap
159 _session->tempo_map().get_grid (begin, end, max (leftmost, (framepos_t) 0), rightmost);
163 Editor::hide_measures ()
171 Editor::draw_measures (ARDOUR::TempoMap::BBTPointList::const_iterator& begin,
172 ARDOUR::TempoMap::BBTPointList::const_iterator& end)
174 if (_session == 0 || _show_measures == false || distance (begin, end) == 0) {
178 if (tempo_lines == 0) {
179 tempo_lines = new TempoLines (time_line_group, ArdourCanvas::LineSet::Vertical);
182 const unsigned divisions = get_grid_beat_divisions(leftmost_frame);
183 tempo_lines->draw (begin, end, divisions, leftmost_frame, _session->frame_rate());
187 Editor::mouse_add_new_tempo_event (framepos_t frame)
193 TempoMap& map(_session->tempo_map());
194 TempoDialog tempo_dialog (map, frame, _("add"));
196 //this causes compiz to display no border.
197 //tempo_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
199 ensure_float (tempo_dialog);
201 switch (tempo_dialog.run()) {
202 case RESPONSE_ACCEPT:
209 Timecode::BBT_Time requested;
211 bpm = tempo_dialog.get_bpm ();
212 double nt = tempo_dialog.get_note_type();
213 bpm = max (0.01, bpm);
215 tempo_dialog.get_bbt_time (requested);
217 begin_reversible_command (_("add tempo mark"));
218 XMLNode &before = map.get_state();
219 map.add_tempo (Tempo (bpm,nt), requested);
220 XMLNode &after = map.get_state();
221 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
222 commit_reversible_command ();
228 Editor::mouse_add_new_meter_event (framepos_t frame)
235 TempoMap& map(_session->tempo_map());
236 MeterDialog meter_dialog (map, frame, _("add"));
238 //this causes compiz to display no border..
239 //meter_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
241 ensure_float (meter_dialog);
243 switch (meter_dialog.run ()) {
244 case RESPONSE_ACCEPT:
250 double bpb = meter_dialog.get_bpb ();
251 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
253 double note_type = meter_dialog.get_note_type ();
254 Timecode::BBT_Time requested;
256 meter_dialog.get_bbt_time (requested);
258 begin_reversible_command (_("add meter mark"));
259 XMLNode &before = map.get_state();
260 map.add_meter (Meter (bpb, note_type), requested);
261 _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
262 commit_reversible_command ();
268 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
271 TempoMarker* tempo_marker;
273 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
274 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
275 abort(); /*NOTREACHED*/
278 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
279 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
280 abort(); /*NOTREACHED*/
283 if (tempo_marker->tempo().movable()) {
284 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
289 Editor::edit_meter_section (MeterSection* section)
291 MeterDialog meter_dialog (*section, _("done"));
293 ensure_float (meter_dialog);
295 switch (meter_dialog.run()) {
296 case RESPONSE_ACCEPT:
302 double bpb = meter_dialog.get_bpb ();
303 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
305 double note_type = meter_dialog.get_note_type ();
307 Timecode::BBT_Time when;
308 meter_dialog.get_bbt_time(when);
310 begin_reversible_command (_("replace tempo mark"));
311 XMLNode &before = _session->tempo_map().get_state();
312 _session->tempo_map().replace_meter (*section, Meter (bpb, note_type), when);
313 XMLNode &after = _session->tempo_map().get_state();
314 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
315 commit_reversible_command ();
319 Editor::edit_tempo_section (TempoSection* section)
321 TempoDialog tempo_dialog (*section, _("done"));
323 ensure_float (tempo_dialog);
325 switch (tempo_dialog.run ()) {
326 case RESPONSE_ACCEPT:
332 double bpm = tempo_dialog.get_bpm ();
333 double nt = tempo_dialog.get_note_type ();
334 Timecode::BBT_Time when;
335 tempo_dialog.get_bbt_time(when);
336 bpm = max (0.01, bpm);
338 begin_reversible_command (_("replace tempo mark"));
339 XMLNode &before = _session->tempo_map().get_state();
340 _session->tempo_map().replace_tempo (*section, Tempo (bpm, nt), when);
341 XMLNode &after = _session->tempo_map().get_state();
342 _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
343 commit_reversible_command ();
347 Editor::edit_tempo_marker (TempoMarker& tm)
349 edit_tempo_section (&tm.tempo());
353 Editor::edit_meter_marker (MeterMarker& mm)
355 edit_meter_section (&mm.meter());
359 Editor::real_remove_tempo_marker (TempoSection *section)
361 begin_reversible_command (_("remove tempo mark"));
362 XMLNode &before = _session->tempo_map().get_state();
363 _session->tempo_map().remove_tempo (*section, true);
364 XMLNode &after = _session->tempo_map().get_state();
365 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
366 commit_reversible_command ();
372 Editor::remove_meter_marker (ArdourCanvas::Item* item)
375 MeterMarker* meter_marker;
377 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
378 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
379 abort(); /*NOTREACHED*/
382 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
383 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
384 abort(); /*NOTREACHED*/
387 if (meter_marker->meter().movable()) {
388 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
393 Editor::real_remove_meter_marker (MeterSection *section)
395 begin_reversible_command (_("remove tempo mark"));
396 XMLNode &before = _session->tempo_map().get_state();
397 _session->tempo_map().remove_meter (*section, true);
398 XMLNode &after = _session->tempo_map().get_state();
399 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
400 commit_reversible_command ();