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, 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, ARDOUR_UI::config()->canvasvar_TempoMarker.get(), buf,
89 *(const_cast<TempoSection*>(ts))));
97 Editor::tempo_map_changed (Change ignored)
103 ENSURE_GUI_THREAD(bind (mem_fun (*this, &Editor::tempo_map_changed), ignored));
106 tempo_lines->tempo_map_changed();
108 compute_current_bbt_points(leftmost_frame, leftmost_frame + (nframes_t)(edit_packer.get_width() * frames_per_unit));
109 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
110 update_tempo_based_rulers ();
115 Editor::redisplay_tempo (bool immediate_redraw)
121 compute_current_bbt_points (leftmost_frame, leftmost_frame + (nframes_t)(edit_packer.get_width() * frames_per_unit)); // redraw rulers and measures
124 update_tempo_based_rulers ();
128 Editor::compute_current_bbt_points (nframes_t leftmost, nframes_t rightmost)
134 BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
136 session->bbt_time(leftmost, previous_beat);
137 session->bbt_time(rightmost, next_beat);
139 if (previous_beat.beats > 1) {
140 previous_beat.beats -= 1;
141 } else if (previous_beat.bars > 1) {
142 previous_beat.bars--;
143 previous_beat.beats += 1;
145 previous_beat.ticks = 0;
147 if (session->tempo_map().meter_at(rightmost).beats_per_bar () > next_beat.beats + 1) {
148 next_beat.beats += 1;
155 if (current_bbt_points) {
156 delete current_bbt_points;
157 current_bbt_points = 0;
160 current_bbt_points = session->tempo_map().get_points (session->tempo_map().frame_time (previous_beat), session->tempo_map().frame_time (next_beat) + 1);
164 Editor::hide_measures ()
171 Editor::redraw_measures ()
178 Editor::draw_measures ()
180 if (session == 0 || _show_measures == false ||
181 !current_bbt_points || current_bbt_points->empty()) {
185 if (current_bbt_points == 0 || current_bbt_points->empty()) {
189 if (tempo_lines == 0) {
190 tempo_lines = new TempoLines(*track_canvas, time_line_group);
193 tempo_lines->draw(*current_bbt_points, frames_per_unit);
197 Editor::mouse_add_new_tempo_event (nframes64_t frame)
203 TempoMap& map(session->tempo_map());
204 TempoDialog tempo_dialog (map, frame, _("add"));
206 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
207 //this causes compiz to display no border.
208 //tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
210 ensure_float (tempo_dialog);
212 switch (tempo_dialog.run()) {
213 case RESPONSE_ACCEPT:
222 bpm = tempo_dialog.get_bpm ();
223 double nt = tempo_dialog.get_note_type();
224 bpm = max (0.01, bpm);
226 tempo_dialog.get_bbt_time (requested);
228 begin_reversible_command (_("add tempo mark"));
229 XMLNode &before = map.get_state();
230 map.add_tempo (Tempo (bpm,nt), requested);
231 XMLNode &after = map.get_state();
232 session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
233 commit_reversible_command ();
239 Editor::mouse_add_new_meter_event (nframes64_t frame)
246 TempoMap& map(session->tempo_map());
247 MeterDialog meter_dialog (map, frame, _("add"));
249 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
251 //this causes compiz to display no border..
252 //meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
254 ensure_float (meter_dialog);
256 switch (meter_dialog.run ()) {
257 case RESPONSE_ACCEPT:
263 double bpb = meter_dialog.get_bpb ();
264 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
266 double note_type = meter_dialog.get_note_type ();
269 meter_dialog.get_bbt_time (requested);
271 begin_reversible_command (_("add meter mark"));
272 XMLNode &before = map.get_state();
273 map.add_meter (Meter (bpb, note_type), requested);
274 session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
275 commit_reversible_command ();
281 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
284 TempoMarker* tempo_marker;
286 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
287 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
291 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
292 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
296 if (tempo_marker->tempo().movable()) {
297 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
302 Editor::edit_meter_section (MeterSection* section)
304 MeterDialog meter_dialog (*section, _("done"));
306 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
308 ensure_float (meter_dialog);
310 switch (meter_dialog.run()) {
311 case RESPONSE_ACCEPT:
317 double bpb = meter_dialog.get_bpb ();
318 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
320 double note_type = meter_dialog.get_note_type ();
322 begin_reversible_command (_("replace tempo mark"));
323 XMLNode &before = session->tempo_map().get_state();
324 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
325 XMLNode &after = session->tempo_map().get_state();
326 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
327 commit_reversible_command ();
331 Editor::edit_tempo_section (TempoSection* section)
333 TempoDialog tempo_dialog (*section, _("done"));
335 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
337 ensure_float (tempo_dialog);
339 switch (tempo_dialog.run ()) {
340 case RESPONSE_ACCEPT:
346 double bpm = tempo_dialog.get_bpm ();
347 double nt = tempo_dialog.get_note_type ();
349 tempo_dialog.get_bbt_time(when);
350 bpm = max (0.01, bpm);
352 cerr << "Editing tempo section to be at " << when << endl;
353 session->tempo_map().dump (cerr);
354 begin_reversible_command (_("replace tempo mark"));
355 XMLNode &before = session->tempo_map().get_state();
356 session->tempo_map().replace_tempo (*section, Tempo (bpm,nt));
357 session->tempo_map().dump (cerr);
358 session->tempo_map().move_tempo (*section, when);
359 session->tempo_map().dump (cerr);
360 XMLNode &after = session->tempo_map().get_state();
361 session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
362 commit_reversible_command ();
366 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
369 TempoMarker* tempo_marker;
371 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
372 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
376 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
377 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
381 edit_tempo_section (&tempo_marker->tempo());
385 Editor::edit_meter_marker (ArdourCanvas::Item *item)
388 MeterMarker* meter_marker;
390 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
391 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
395 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
396 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
400 edit_meter_section (&meter_marker->meter());
404 Editor::real_remove_tempo_marker (TempoSection *section)
406 begin_reversible_command (_("remove tempo mark"));
407 XMLNode &before = session->tempo_map().get_state();
408 session->tempo_map().remove_tempo (*section);
409 XMLNode &after = session->tempo_map().get_state();
410 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
411 commit_reversible_command ();
417 Editor::remove_meter_marker (ArdourCanvas::Item* item)
420 MeterMarker* meter_marker;
422 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
423 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
427 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
428 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
432 if (meter_marker->meter().movable()) {
433 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
438 Editor::real_remove_meter_marker (MeterSection *section)
440 begin_reversible_command (_("remove tempo mark"));
441 XMLNode &before = session->tempo_map().get_state();
442 session->tempo_map().remove_meter (*section);
443 XMLNode &after = session->tempo_map().get_state();
444 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
445 commit_reversible_command ();