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 <cstdio> // for sprintf, grrr
27 #include <libgnomecanvasmm/libgnomecanvasmm.h>
29 #include <pbd/error.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>
40 #include "canvas-simpleline.h"
41 #include "tempo_dialog.h"
42 #include "rgb_macros.h"
43 #include "gui_thread.h"
49 using namespace ARDOUR;
51 using namespace Editing;
54 Editor::remove_metric_marks ()
56 /* don't delete these while handling events, just punt till the GUI is idle */
58 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
59 delete_when_idle (*x);
61 metric_marks.clear ();
65 Editor::draw_metric_marks (const Metrics& metrics)
67 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
68 const MeterSection *ms;
69 const TempoSection *ts;
72 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
73 snprintf (buf, sizeof(buf), "%g/%g", ms->beats_per_bar(), ms->note_divisor ());
74 metric_marks.push_back (new MeterMarker (*this, *meter_group, color_map[cMeterMarker], buf,
75 *(const_cast<MeterSection*>(ms)), PublicEditor::canvas_meter_marker_event));
76 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
77 snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
78 metric_marks.push_back (new TempoMarker (*this, *tempo_group, color_map[cTempoMarker], buf,
79 *(const_cast<TempoSection*>(ts)), PublicEditor::canvas_tempo_marker_event));
86 Editor::tempo_map_changed (Change ignored)
88 ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::tempo_map_changed), ignored));
90 if (current_bbt_points) {
91 delete current_bbt_points;
92 current_bbt_points = 0;
96 current_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
98 current_bbt_points = 0;
105 Editor::redisplay_tempo ()
107 update_tempo_based_rulers ();
109 remove_metric_marks ();
112 if (session && current_bbt_points) {
113 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
120 Editor::hide_measures ()
122 for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
124 free_measure_lines.push_back (*i);
126 used_measure_lines.clear ();
130 Editor::get_time_line ()
132 ArdourCanvas::Line *line;
134 if (free_measure_lines.empty()) {
135 line = new ArdourCanvas::Line (*time_line_group);
136 // cerr << "measure line @ " << line << endl;
137 used_measure_lines.push_back (line);
139 line = free_measure_lines.front();
140 free_measure_lines.erase (free_measure_lines.begin());
141 used_measure_lines.push_back (line);
148 Editor::draw_measures ()
150 if (session == 0 || _show_measures == false) {
154 TempoMap::BBTPointList::iterator i;
155 TempoMap::BBTPointList *all_bbt_points;
156 ArdourCanvas::Line *line;
157 gdouble xpos, last_xpos;
161 if (current_bbt_points == 0 || current_bbt_points->empty()) {
165 all_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
170 /* get the first bar spacing */
172 gdouble last_beat = DBL_MAX;
173 gdouble beat_spacing = 0;
175 for (i = all_bbt_points->begin(); i != all_bbt_points->end() && beat_spacing == 0; ++i) {
176 TempoMap::BBTPoint& p = (*i);
183 xpos = p.frame / (gdouble) frames_per_unit;
184 if (last_beat < xpos) {
185 beat_spacing = xpos - last_beat;
191 for (i = all_bbt_points->begin(); i != all_bbt_points->end(); ++i) {
193 TempoMap::BBTPoint& p = (*i);
200 xpos = p.frame / (gdouble) frames_per_unit;
203 color = color_map[cMeasureLineBeat];
205 color = color_map[cMeasureLineBar];
207 /* only draw beat lines if the gaps between beats
211 if (beat_spacing < 25.0) {
216 if (cnt == 0 || xpos - last_xpos > 4.0) {
217 line = get_time_line ();
218 line->set_property ("x1", xpos);
219 line->set_property ("x2", xpos);
220 line->set_property ("y2", (gdouble) canvas_height);
221 line->set_property ("color_rgba", color);
222 line->raise_to_top();
231 delete all_bbt_points;
233 /* the cursors are always on top of everything */
235 cursor_group->raise_to_top();
236 time_line_group->lower_to_bottom();
240 Editor::mouse_add_new_tempo_event (jack_nframes_t frame)
247 TempoMap& map(session->tempo_map());
248 TempoDialog tempo_dialog (map, frame, _("add"));
250 tempo_dialog.bpm_entry.signal_activate().connect (bind (mem_fun (tempo_dialog, &ArdourDialog::stop), 0));
251 tempo_dialog.ok_button.signal_clicked().connect (bind (mem_fun (tempo_dialog, &ArdourDialog::stop), 0));
252 tempo_dialog.cancel_button.signal_clicked().connect (bind (mem_fun (tempo_dialog, &ArdourDialog::stop), -1));
254 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
256 // tempo_dialog.realize ();
257 // tempo_dialog.get_window()->set_decorations (Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
259 ensure_float (tempo_dialog);
263 if (tempo_dialog.run_status() == 0) {
268 bpm = tempo_dialog.get_bpm ();
269 bpm = max (0.01, bpm);
271 tempo_dialog.get_bbt_time (requested);
273 begin_reversible_command (_("add tempo mark"));
274 session->add_undo (map.get_memento());
275 map.add_tempo (Tempo (bpm), requested);
276 session->add_redo_no_execute (map.get_memento());
277 commit_reversible_command ();
284 Editor::mouse_add_new_meter_event (jack_nframes_t frame)
291 TempoMap& map(session->tempo_map());
292 MeterDialog meter_dialog (map, frame, _("add"));
294 meter_dialog.ok_button.signal_clicked().connect (bind (mem_fun (meter_dialog, &ArdourDialog::stop), 0));
295 meter_dialog.cancel_button.signal_clicked().connect (bind (mem_fun (meter_dialog, &ArdourDialog::stop), -1));
297 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
299 // meter_dialog.realize ();
300 // meter_dialog.get_window()->set_decorations (Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
302 ensure_float (meter_dialog);
306 if (meter_dialog.run_status() == 0) {
308 double bpb = meter_dialog.get_bpb ();
309 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
311 double note_type = meter_dialog.get_note_type ();
314 meter_dialog.get_bbt_time (requested);
316 begin_reversible_command (_("add meter mark"));
317 session->add_undo (map.get_memento());
318 map.add_meter (Meter (bpb, note_type), requested);
319 session->add_redo_no_execute (map.get_memento());
320 commit_reversible_command ();
327 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
330 TempoMarker* tempo_marker;
332 if ((marker = reinterpret_cast<Marker *> (gtk_object_get_data (GTK_OBJECT(item), "marker"))) == 0) {
333 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
337 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
338 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
342 if (tempo_marker->tempo().movable()) {
343 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
348 Editor::edit_meter_section (MeterSection* section)
350 MeterDialog meter_dialog (*section, _("done"));
352 meter_dialog.ok_button.signal_clicked().connect (bind (mem_fun (meter_dialog, &ArdourDialog::stop), 0));
353 meter_dialog.cancel_button.signal_clicked().connect (bind (mem_fun (meter_dialog, &ArdourDialog::stop), -1));
355 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
357 // meter_dialog.realize ();
358 // meter_dialog.get_window()->set_decorations (Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
360 ensure_float (meter_dialog);
364 if (meter_dialog.run_status() == 0) {
366 double bpb = meter_dialog.get_bpb ();
367 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
369 double note_type = meter_dialog.get_note_type ();
371 begin_reversible_command (_("replace tempo mark"));
372 session->add_undo (session->tempo_map().get_memento());
373 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
374 session->add_redo_no_execute (session->tempo_map().get_memento());
375 commit_reversible_command ();
380 Editor::edit_tempo_section (TempoSection* section)
382 TempoDialog tempo_dialog (*section, _("done"));
384 tempo_dialog.bpm_entry.signal_activate().connect (bind (mem_fun (tempo_dialog, &ArdourDialog::stop), 0));
385 tempo_dialog.ok_button.signal_clicked().connect (bind (mem_fun (tempo_dialog, &ArdourDialog::stop), 0));
386 tempo_dialog.cancel_button.signal_clicked().connect (bind (mem_fun (tempo_dialog, &ArdourDialog::stop), -1));
388 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
390 // tempo_dialog.realize ();
391 // tempo_dialog.get_window()->set_decorations (Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
393 ensure_float (tempo_dialog);
397 if (tempo_dialog.run_status() == 0) {
399 double bpm = tempo_dialog.get_bpm ();
401 tempo_dialog.get_bbt_time(when);
402 bpm = max (0.01, bpm);
404 begin_reversible_command (_("replace tempo mark"));
405 session->add_undo (session->tempo_map().get_memento());
406 session->tempo_map().replace_tempo (*section, Tempo (bpm));
407 session->tempo_map().move_tempo (*section, when);
408 session->add_redo_no_execute (session->tempo_map().get_memento());
409 commit_reversible_command ();
414 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
417 TempoMarker* tempo_marker;
419 if ((marker = reinterpret_cast<Marker *> (gtk_object_get_data (GTK_OBJECT(item), "marker"))) == 0) {
420 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
424 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
425 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
429 edit_tempo_section (&tempo_marker->tempo());
433 Editor::edit_meter_marker (ArdourCanvas::Item *item)
436 MeterMarker* meter_marker;
438 if ((marker = reinterpret_cast<Marker *> (gtk_object_get_data (GTK_OBJECT(item), "marker"))) == 0) {
439 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
443 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
444 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
448 edit_meter_section (&meter_marker->meter());
452 Editor::real_remove_tempo_marker (TempoSection *section)
454 begin_reversible_command (_("remove tempo mark"));
455 session->add_undo (session->tempo_map().get_memento());
456 session->tempo_map().remove_tempo (*section);
457 session->add_redo_no_execute (session->tempo_map().get_memento());
458 commit_reversible_command ();
464 Editor::remove_meter_marker (ArdourCanvas::Item* item)
467 MeterMarker* meter_marker;
469 if ((marker = reinterpret_cast<Marker *> (gtk_object_get_data (GTK_OBJECT(item), "marker"))) == 0) {
470 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
474 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
475 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
479 if (meter_marker->meter().movable()) {
480 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
485 Editor::real_remove_meter_marker (MeterSection *section)
487 begin_reversible_command (_("remove tempo mark"));
488 session->add_undo (session->tempo_map().get_memento());
489 session->tempo_map().remove_meter (*section);
490 session->add_redo_no_execute (session->tempo_map().get_memento());
491 commit_reversible_command ();