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 <gtk-canvas.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, GTK_CANVAS_GROUP(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, GTK_CANVAS_GROUP(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 (slot (*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) {
123 gtk_canvas_item_hide (*i);
124 free_measure_lines.push_back (*i);
126 used_measure_lines.clear ();
130 Editor::get_time_line ()
134 if (free_measure_lines.empty()) {
135 line = gtk_canvas_item_new (GTK_CANVAS_GROUP(time_line_group),
136 gtk_canvas_simpleline_get_type(),
138 // cerr << "measure line @ " << line << endl;
139 used_measure_lines.push_back (line);
141 line = free_measure_lines.front();
142 free_measure_lines.erase (free_measure_lines.begin());
143 used_measure_lines.push_back (line);
150 Editor::draw_measures ()
152 if (session == 0 || _show_measures == false) {
156 TempoMap::BBTPointList::iterator i;
157 TempoMap::BBTPointList *all_bbt_points;
159 gdouble xpos, last_xpos;
163 if (current_bbt_points == 0 || current_bbt_points->empty()) {
167 all_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
172 /* get the first bar spacing */
174 gdouble last_beat = DBL_MAX;
175 gdouble beat_spacing = 0;
177 for (i = all_bbt_points->begin(); i != all_bbt_points->end() && beat_spacing == 0; ++i) {
178 TempoMap::BBTPoint& p = (*i);
185 xpos = p.frame / (gdouble) frames_per_unit;
186 if (last_beat < xpos) {
187 beat_spacing = xpos - last_beat;
193 for (i = all_bbt_points->begin(); i != all_bbt_points->end(); ++i) {
195 TempoMap::BBTPoint& p = (*i);
202 xpos = p.frame / (gdouble) frames_per_unit;
205 color = color_map[cMeasureLineBeat];
207 color = color_map[cMeasureLineBar];
209 /* only draw beat lines if the gaps between beats
213 if (beat_spacing < 25.0) {
218 if (cnt == 0 || xpos - last_xpos > 4.0) {
219 line = get_time_line ();
220 gtk_object_set (GTK_OBJECT(line),
223 "y2", (gdouble) canvas_height,
226 gtk_canvas_item_raise_to_top (line);
227 gtk_canvas_item_show (line);
235 delete all_bbt_points;
237 /* the cursors are always on top of everything */
239 gtk_canvas_item_raise_to_top (cursor_group);
240 gtk_canvas_item_lower_to_bottom (time_line_group);
244 Editor::mouse_add_new_tempo_event (jack_nframes_t frame)
251 TempoMap& map(session->tempo_map());
252 TempoDialog tempo_dialog (map, frame, _("add"));
254 tempo_dialog.bpm_entry.activate.connect (bind (slot (tempo_dialog, &ArdourDialog::stop), 0));
255 tempo_dialog.ok_button.signal_clicked().connect (bind (slot (tempo_dialog, &ArdourDialog::stop), 0));
256 tempo_dialog.cancel_button.signal_clicked().connect (bind (slot (tempo_dialog, &ArdourDialog::stop), -1));
258 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
259 tempo_dialog.realize ();
260 tempo_dialog.get_window().set_decorations (GdkWMDecoration (GDK_DECOR_BORDER|GDK_DECOR_RESIZEH));
262 ensure_float (tempo_dialog);
266 if (tempo_dialog.run_status() == 0) {
271 bpm = tempo_dialog.get_bpm ();
272 bpm = max (0.01, bpm);
274 tempo_dialog.get_bbt_time (requested);
276 begin_reversible_command (_("add tempo mark"));
277 session->add_undo (map.get_memento());
278 map.add_tempo (Tempo (bpm), requested);
279 session->add_redo_no_execute (map.get_memento());
280 commit_reversible_command ();
287 Editor::mouse_add_new_meter_event (jack_nframes_t frame)
294 TempoMap& map(session->tempo_map());
295 MeterDialog meter_dialog (map, frame, _("add"));
297 meter_dialog.ok_button.signal_clicked().connect (bind (slot (meter_dialog, &ArdourDialog::stop), 0));
298 meter_dialog.cancel_button.signal_clicked().connect (bind (slot (meter_dialog, &ArdourDialog::stop), -1));
300 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
301 meter_dialog.realize ();
302 meter_dialog.get_window().set_decorations (GdkWMDecoration (GDK_DECOR_BORDER|GDK_DECOR_RESIZEH));
304 ensure_float (meter_dialog);
308 if (meter_dialog.run_status() == 0) {
310 double bpb = meter_dialog.get_bpb ();
311 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
313 double note_type = meter_dialog.get_note_type ();
316 meter_dialog.get_bbt_time (requested);
318 begin_reversible_command (_("add meter mark"));
319 session->add_undo (map.get_memento());
320 map.add_meter (Meter (bpb, note_type), requested);
321 session->add_redo_no_execute (map.get_memento());
322 commit_reversible_command ();
329 Editor::remove_tempo_marker (GtkCanvasItem* item)
332 TempoMarker* tempo_marker;
334 if ((marker = reinterpret_cast<Marker *> (gtk_object_get_data (GTK_OBJECT(item), "marker"))) == 0) {
335 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
339 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
340 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
344 if (tempo_marker->tempo().movable()) {
345 Gtk::Main::idle.connect (bind (slot (*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
350 Editor::edit_meter_section (MeterSection* section)
352 MeterDialog meter_dialog (*section, _("done"));
354 meter_dialog.ok_button.signal_clicked().connect (bind (slot (meter_dialog, &ArdourDialog::stop), 0));
355 meter_dialog.cancel_button.signal_clicked().connect (bind (slot (meter_dialog, &ArdourDialog::stop), -1));
357 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
358 meter_dialog.realize ();
359 meter_dialog.get_window().set_decorations (GdkWMDecoration (GDK_DECOR_BORDER|GDK_DECOR_RESIZEH));
361 ensure_float (meter_dialog);
365 if (meter_dialog.run_status() == 0) {
367 double bpb = meter_dialog.get_bpb ();
368 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
370 double note_type = meter_dialog.get_note_type ();
372 begin_reversible_command (_("replace tempo mark"));
373 session->add_undo (session->tempo_map().get_memento());
374 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
375 session->add_redo_no_execute (session->tempo_map().get_memento());
376 commit_reversible_command ();
381 Editor::edit_tempo_section (TempoSection* section)
383 TempoDialog tempo_dialog (*section, _("done"));
385 tempo_dialog.bpm_entry.activate.connect (bind (slot (tempo_dialog, &ArdourDialog::stop), 0));
386 tempo_dialog.ok_button.signal_clicked().connect (bind (slot (tempo_dialog, &ArdourDialog::stop), 0));
387 tempo_dialog.cancel_button.signal_clicked().connect (bind (slot (tempo_dialog, &ArdourDialog::stop), -1));
389 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
390 tempo_dialog.realize ();
391 tempo_dialog.get_window().set_decorations (GdkWMDecoration (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 (GtkCanvasItem *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 (GtkCanvasItem *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 (GtkCanvasItem* 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 Gtk::Main::idle.connect (bind (slot (*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 ();