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.h>
29 #include <pbd/error.h>
30 #include <pbd/memento_command.h>
32 #include <gtkmm2ext/utils.h>
33 #include <gtkmm2ext/gtk_ui.h>
35 #include <ardour/session.h>
36 #include <ardour/tempo.h>
37 #include <gtkmm2ext/doi.h>
38 #include <gtkmm2ext/utils.h>
42 #include "simpleline.h"
43 #include "tempo_dialog.h"
44 #include "rgb_macros.h"
45 #include "gui_thread.h"
52 using namespace ARDOUR;
55 using namespace Gtkmm2ext;
56 using namespace Editing;
59 Editor::remove_metric_marks ()
61 /* don't delete these while handling events, just punt till the GUI is idle */
63 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
64 delete_when_idle (*x);
66 metric_marks.clear ();
70 Editor::draw_metric_marks (const Metrics& metrics)
73 const MeterSection *ms;
74 const TempoSection *ts;
77 remove_metric_marks ();
79 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
81 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
82 snprintf (buf, sizeof(buf), "%g/%g", ms->beats_per_bar(), ms->note_divisor ());
83 metric_marks.push_back (new MeterMarker (*this, *meter_group, color_map[cMeterMarker], buf,
84 *(const_cast<MeterSection*>(ms))));
85 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
86 snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
87 metric_marks.push_back (new TempoMarker (*this, *tempo_group, color_map[cTempoMarker], buf,
88 *(const_cast<TempoSection*>(ts))));
96 Editor::tempo_map_changed (Change ignored)
98 ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::tempo_map_changed), ignored));
100 BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
102 session->bbt_time(leftmost_frame, previous_beat);
103 session->bbt_time(leftmost_frame + current_page_frames(), next_beat);
105 if (previous_beat.beats > 1) {
106 previous_beat.beats -= 1;
107 } else if (previous_beat.bars > 1) {
108 previous_beat.bars--;
109 previous_beat.beats += 1;
111 previous_beat.ticks = 0;
113 if (session->tempo_map().meter_at(leftmost_frame + current_page_frames()).beats_per_bar () > next_beat.beats + 1) {
114 next_beat.beats += 1;
121 if (current_bbt_points) {
122 delete current_bbt_points;
123 current_bbt_points = 0;
127 current_bbt_points = session->tempo_map().get_points (session->tempo_map().frame_time (previous_beat), session->tempo_map().frame_time (next_beat));
128 update_tempo_based_rulers ();
130 current_bbt_points = 0;
137 Editor::redisplay_tempo ()
140 if (session && current_bbt_points) {
141 Glib::signal_idle().connect (mem_fun (*this, &Editor::lazy_hide_and_draw_measures));
148 Editor::hide_measures ()
150 for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
152 free_measure_lines.push_back (*i);
154 used_measure_lines.clear ();
157 ArdourCanvas::SimpleLine *
158 Editor::get_time_line ()
160 ArdourCanvas::SimpleLine *line;
162 if (free_measure_lines.empty()) {
163 line = new ArdourCanvas::SimpleLine (*time_line_group);
164 used_measure_lines.push_back (line);
166 line = free_measure_lines.front();
167 free_measure_lines.erase (free_measure_lines.begin());
168 used_measure_lines.push_back (line);
175 Editor::lazy_hide_and_draw_measures ()
183 Editor::draw_measures ()
185 if (session == 0 || _show_measures == false) {
189 TempoMap::BBTPointList::iterator i;
190 ArdourCanvas::SimpleLine *line;
192 double x1, x2, y1, y2, beat_density;
198 if (current_bbt_points == 0 || current_bbt_points->empty()) {
202 track_canvas.get_scroll_region (x1, y1, x2, y2);
204 /* get the first bar spacing */
206 i = current_bbt_points->end();
208 bars = (*i).bar - (*current_bbt_points->begin()).bar;
209 beats = current_bbt_points->size() - bars;
211 beat_density = (beats * 10.0f) / track_canvas.get_width ();
213 if (beat_density > 2.0f) {
215 if the lines are too close together,
221 for (i = current_bbt_points->begin(); i != current_bbt_points->end(); ++i) {
229 if ((*i).beat == 1) {
230 color = color_map[cMeasureLineBeat];
232 color = color_map[cMeasureLineBar];
234 /* only draw beat lines if the gaps between beats
238 if (beat_density > 0.25) {
243 xpos = frame_to_unit ((*i).frame);
244 line = get_time_line ();
245 line->property_x1() = xpos;
246 line->property_x2() = xpos;
247 line->property_y2() = y2;
248 line->property_color_rgba() = color;
249 //line->raise_to_top();
255 /* the cursors are always on top of everything */
257 cursor_group->raise_to_top();
258 time_line_group->lower_to_bottom();
263 Editor::mouse_add_new_tempo_event (nframes_t frame)
269 TempoMap& map(session->tempo_map());
270 TempoDialog tempo_dialog (map, frame, _("add"));
272 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
273 tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
275 ensure_float (tempo_dialog);
277 switch (tempo_dialog.run()) {
278 case RESPONSE_ACCEPT:
287 bpm = tempo_dialog.get_bpm ();
288 bpm = max (0.01, bpm);
290 tempo_dialog.get_bbt_time (requested);
292 begin_reversible_command (_("add tempo mark"));
293 XMLNode &before = map.get_state();
294 map.add_tempo (Tempo (bpm), requested);
295 XMLNode &after = map.get_state();
296 session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
297 commit_reversible_command ();
301 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
305 Editor::mouse_add_new_meter_event (nframes_t frame)
312 TempoMap& map(session->tempo_map());
313 MeterDialog meter_dialog (map, frame, _("add"));
315 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
316 meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
318 ensure_float (meter_dialog);
320 switch (meter_dialog.run ()) {
321 case RESPONSE_ACCEPT:
327 double bpb = meter_dialog.get_bpb ();
328 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
330 double note_type = meter_dialog.get_note_type ();
333 meter_dialog.get_bbt_time (requested);
335 begin_reversible_command (_("add meter mark"));
336 XMLNode &before = map.get_state();
337 map.add_meter (Meter (bpb, note_type), requested);
338 session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
339 commit_reversible_command ();
343 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
347 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
350 TempoMarker* tempo_marker;
352 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
353 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
357 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
358 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
362 if (tempo_marker->tempo().movable()) {
363 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
368 Editor::edit_meter_section (MeterSection* section)
370 MeterDialog meter_dialog (*section, _("done"));
372 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
374 ensure_float (meter_dialog);
376 switch (meter_dialog.run()) {
377 case RESPONSE_ACCEPT:
383 double bpb = meter_dialog.get_bpb ();
384 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
386 double note_type = meter_dialog.get_note_type ();
388 begin_reversible_command (_("replace tempo mark"));
389 XMLNode &before = session->tempo_map().get_state();
390 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
391 XMLNode &after = session->tempo_map().get_state();
392 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
393 commit_reversible_command ();
395 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
399 Editor::edit_tempo_section (TempoSection* section)
401 TempoDialog tempo_dialog (*section, _("done"));
403 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
405 ensure_float (tempo_dialog);
407 switch (tempo_dialog.run ()) {
408 case RESPONSE_ACCEPT:
414 double bpm = tempo_dialog.get_bpm ();
416 tempo_dialog.get_bbt_time(when);
417 bpm = max (0.01, bpm);
419 begin_reversible_command (_("replace tempo mark"));
420 XMLNode &before = session->tempo_map().get_state();
421 session->tempo_map().replace_tempo (*section, Tempo (bpm));
422 session->tempo_map().move_tempo (*section, when);
423 XMLNode &after = session->tempo_map().get_state();
424 session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
425 commit_reversible_command ();
427 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
431 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
434 TempoMarker* tempo_marker;
436 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
437 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
441 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
442 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
446 edit_tempo_section (&tempo_marker->tempo());
450 Editor::edit_meter_marker (ArdourCanvas::Item *item)
453 MeterMarker* meter_marker;
455 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
456 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
460 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
461 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
465 edit_meter_section (&meter_marker->meter());
469 Editor::real_remove_tempo_marker (TempoSection *section)
471 begin_reversible_command (_("remove tempo mark"));
472 XMLNode &before = session->tempo_map().get_state();
473 session->tempo_map().remove_tempo (*section);
474 XMLNode &after = session->tempo_map().get_state();
475 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
476 commit_reversible_command ();
478 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
484 Editor::remove_meter_marker (ArdourCanvas::Item* item)
487 MeterMarker* meter_marker;
489 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
490 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
494 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
495 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
499 if (meter_marker->meter().movable()) {
500 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
505 Editor::real_remove_meter_marker (MeterSection *section)
507 begin_reversible_command (_("remove tempo mark"));
508 XMLNode &before = session->tempo_map().get_state();
509 session->tempo_map().remove_meter (*section);
510 XMLNode &after = session->tempo_map().get_state();
511 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
512 commit_reversible_command ();
514 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);