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"
46 #include "time_axis_view.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, bool immediate_redraw)
102 ENSURE_GUI_THREAD(bind (mem_fun (*this, &Editor::tempo_map_changed), ignored, immediate_redraw));
104 BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
106 session->bbt_time(leftmost_frame, previous_beat);
107 session->bbt_time(leftmost_frame + current_page_frames(), next_beat);
109 if (previous_beat.beats > 1) {
110 previous_beat.beats -= 1;
111 } else if (previous_beat.bars > 1) {
112 previous_beat.bars--;
113 previous_beat.beats += 1;
115 previous_beat.ticks = 0;
117 if (session->tempo_map().meter_at(leftmost_frame + current_page_frames()).beats_per_bar () > next_beat.beats + 1) {
118 next_beat.beats += 1;
125 if (current_bbt_points) {
126 delete current_bbt_points;
127 current_bbt_points = 0;
131 current_bbt_points = session->tempo_map().get_points (session->tempo_map().frame_time (previous_beat), session->tempo_map().frame_time (next_beat));
132 update_tempo_based_rulers ();
134 current_bbt_points = 0;
137 if (immediate_redraw) {
141 if (session && current_bbt_points) {
147 if (session && current_bbt_points) {
148 Glib::signal_idle().connect (mem_fun (*this, &Editor::lazy_hide_and_draw_measures));
156 Editor::redisplay_tempo ()
161 Editor::hide_measures ()
163 for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
165 free_measure_lines.push_back (*i);
167 used_measure_lines.clear ();
170 ArdourCanvas::SimpleLine *
171 Editor::get_time_line ()
173 ArdourCanvas::SimpleLine *line;
175 if (free_measure_lines.empty()) {
176 line = new ArdourCanvas::SimpleLine (*time_line_group);
177 used_measure_lines.push_back (line);
179 line = free_measure_lines.front();
180 free_measure_lines.erase (free_measure_lines.begin());
181 used_measure_lines.push_back (line);
188 Editor::lazy_hide_and_draw_measures ()
196 Editor::draw_measures ()
198 if (session == 0 || _show_measures == false) {
202 TempoMap::BBTPointList::iterator i;
203 ArdourCanvas::SimpleLine *line;
205 double x1, x2, y1, y2, beat_density;
211 if (current_bbt_points == 0 || current_bbt_points->empty()) {
215 track_canvas.get_scroll_region (x1, y1, x2, y2);
216 y2 = TimeAxisView::hLargest*5000; // five thousand largest tracks should be enough.. :)
218 /* get the first bar spacing */
220 i = current_bbt_points->end();
222 bars = (*i).bar - (*current_bbt_points->begin()).bar;
223 beats = current_bbt_points->size() - bars;
225 beat_density = (beats * 10.0f) / track_canvas.get_width ();
227 if (beat_density > 4.0f) {
228 /* if the lines are too close together, they become useless
233 for (i = current_bbt_points->begin(); i != current_bbt_points->end(); ++i) {
241 if ((*i).beat == 1) {
242 color = color_map[cMeasureLineBar];
244 color = color_map[cMeasureLineBeat];
246 if (beat_density > 2.0) {
247 /* only draw beat lines if the gaps between beats are large.
253 xpos = frame_to_unit ((*i).frame);
254 line = get_time_line ();
255 line->property_x1() = xpos;
256 line->property_x2() = xpos;
257 line->property_y2() = y2;
258 line->property_color_rgba() = color;
259 //line->raise_to_top();
265 /* the cursors are always on top of everything */
267 cursor_group->raise_to_top();
268 time_line_group->lower_to_bottom();
273 Editor::mouse_add_new_tempo_event (nframes_t frame)
279 TempoMap& map(session->tempo_map());
280 TempoDialog tempo_dialog (map, frame, _("add"));
282 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
283 tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
285 ensure_float (tempo_dialog);
287 switch (tempo_dialog.run()) {
288 case RESPONSE_ACCEPT:
297 bpm = tempo_dialog.get_bpm ();
298 bpm = max (0.01, bpm);
300 tempo_dialog.get_bbt_time (requested);
302 begin_reversible_command (_("add tempo mark"));
303 XMLNode &before = map.get_state();
304 map.add_tempo (Tempo (bpm), requested);
305 XMLNode &after = map.get_state();
306 session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
307 commit_reversible_command ();
311 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
315 Editor::mouse_add_new_meter_event (nframes_t frame)
322 TempoMap& map(session->tempo_map());
323 MeterDialog meter_dialog (map, frame, _("add"));
325 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
326 meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
328 ensure_float (meter_dialog);
330 switch (meter_dialog.run ()) {
331 case RESPONSE_ACCEPT:
337 double bpb = meter_dialog.get_bpb ();
338 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
340 double note_type = meter_dialog.get_note_type ();
343 meter_dialog.get_bbt_time (requested);
345 begin_reversible_command (_("add meter mark"));
346 XMLNode &before = map.get_state();
347 map.add_meter (Meter (bpb, note_type), requested);
348 session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
349 commit_reversible_command ();
353 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
357 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
360 TempoMarker* tempo_marker;
362 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
363 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
367 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
368 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
372 if (tempo_marker->tempo().movable()) {
373 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
378 Editor::edit_meter_section (MeterSection* section)
380 MeterDialog meter_dialog (*section, _("done"));
382 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
384 ensure_float (meter_dialog);
386 switch (meter_dialog.run()) {
387 case RESPONSE_ACCEPT:
393 double bpb = meter_dialog.get_bpb ();
394 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
396 double note_type = meter_dialog.get_note_type ();
398 begin_reversible_command (_("replace tempo mark"));
399 XMLNode &before = session->tempo_map().get_state();
400 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
401 XMLNode &after = session->tempo_map().get_state();
402 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
403 commit_reversible_command ();
405 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
409 Editor::edit_tempo_section (TempoSection* section)
411 TempoDialog tempo_dialog (*section, _("done"));
413 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
415 ensure_float (tempo_dialog);
417 switch (tempo_dialog.run ()) {
418 case RESPONSE_ACCEPT:
424 double bpm = tempo_dialog.get_bpm ();
426 tempo_dialog.get_bbt_time(when);
427 bpm = max (0.01, bpm);
429 begin_reversible_command (_("replace tempo mark"));
430 XMLNode &before = session->tempo_map().get_state();
431 session->tempo_map().replace_tempo (*section, Tempo (bpm));
432 session->tempo_map().move_tempo (*section, when);
433 XMLNode &after = session->tempo_map().get_state();
434 session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
435 commit_reversible_command ();
437 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
441 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
444 TempoMarker* tempo_marker;
446 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
447 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
451 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
452 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
456 edit_tempo_section (&tempo_marker->tempo());
460 Editor::edit_meter_marker (ArdourCanvas::Item *item)
463 MeterMarker* meter_marker;
465 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
466 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
470 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
471 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
475 edit_meter_section (&meter_marker->meter());
479 Editor::real_remove_tempo_marker (TempoSection *section)
481 begin_reversible_command (_("remove tempo mark"));
482 XMLNode &before = session->tempo_map().get_state();
483 session->tempo_map().remove_tempo (*section);
484 XMLNode &after = session->tempo_map().get_state();
485 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
486 commit_reversible_command ();
488 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
494 Editor::remove_meter_marker (ArdourCanvas::Item* item)
497 MeterMarker* meter_marker;
499 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
500 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
504 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
505 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
509 if (meter_marker->meter().movable()) {
510 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
515 Editor::real_remove_meter_marker (MeterSection *section)
517 begin_reversible_command (_("remove tempo mark"));
518 XMLNode &before = session->tempo_map().get_state();
519 session->tempo_map().remove_meter (*section);
520 XMLNode &after = session->tempo_map().get_state();
521 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
522 commit_reversible_command ();
524 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);