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"
47 #include "time_axis_view.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, color_map[cMeterMarker], 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, color_map[cTempoMarker], buf,
89 *(const_cast<TempoSection*>(ts))));
97 Editor::tempo_map_changed (Change ignored)
99 ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::tempo_map_changed), ignored));
101 BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
103 session->bbt_time(leftmost_frame, previous_beat);
104 session->bbt_time(leftmost_frame + current_page_frames(), next_beat);
106 if (previous_beat.beats > 1) {
107 previous_beat.beats -= 1;
108 } else if (previous_beat.bars > 1) {
109 previous_beat.bars--;
110 previous_beat.beats += 1;
112 previous_beat.ticks = 0;
114 if (session->tempo_map().meter_at(leftmost_frame + current_page_frames()).beats_per_bar () > next_beat.beats + 1) {
115 next_beat.beats += 1;
122 if (current_bbt_points) {
123 delete current_bbt_points;
124 current_bbt_points = 0;
128 current_bbt_points = session->tempo_map().get_points (session->tempo_map().frame_time (previous_beat), session->tempo_map().frame_time (next_beat));
129 update_tempo_based_rulers ();
131 current_bbt_points = 0;
138 Editor::redisplay_tempo ()
141 if (session && current_bbt_points) {
142 Glib::signal_idle().connect (mem_fun (*this, &Editor::lazy_hide_and_draw_measures));
149 Editor::hide_measures ()
151 for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
153 free_measure_lines.push_back (*i);
155 used_measure_lines.clear ();
158 ArdourCanvas::SimpleLine *
159 Editor::get_time_line ()
161 ArdourCanvas::SimpleLine *line;
163 if (free_measure_lines.empty()) {
164 line = new ArdourCanvas::SimpleLine (*time_line_group);
165 used_measure_lines.push_back (line);
167 line = free_measure_lines.front();
168 free_measure_lines.erase (free_measure_lines.begin());
169 used_measure_lines.push_back (line);
176 Editor::lazy_hide_and_draw_measures ()
184 Editor::draw_measures ()
186 if (session == 0 || _show_measures == false) {
190 TempoMap::BBTPointList::iterator i;
191 ArdourCanvas::SimpleLine *line;
193 double x1, x2, y1, y2, beat_density;
199 if (current_bbt_points == 0 || current_bbt_points->empty()) {
203 track_canvas.get_scroll_region (x1, y1, x2, y2);
204 y2 = TimeAxisView::hLargest*5000; // five thousand largest tracks should be enough.. :)
206 /* get the first bar spacing */
208 i = current_bbt_points->end();
210 bars = (*i).bar - (*current_bbt_points->begin()).bar;
211 beats = current_bbt_points->size() - bars;
213 beat_density = (beats * 10.0f) / track_canvas.get_width ();
215 if (beat_density > 4.0f) {
216 /* if the lines are too close together, they become useless
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 if (beat_density > 2.0) {
235 /* only draw beat lines if the gaps between beats are large.
241 xpos = frame_to_unit ((*i).frame);
242 line = get_time_line ();
243 line->property_x1() = xpos;
244 line->property_x2() = xpos;
245 line->property_y2() = y2;
246 line->property_color_rgba() = color;
247 //line->raise_to_top();
253 /* the cursors are always on top of everything */
255 cursor_group->raise_to_top();
256 time_line_group->lower_to_bottom();
261 Editor::mouse_add_new_tempo_event (nframes_t frame)
267 TempoMap& map(session->tempo_map());
268 TempoDialog tempo_dialog (map, frame, _("add"));
270 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
271 tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
273 ensure_float (tempo_dialog);
275 switch (tempo_dialog.run()) {
276 case RESPONSE_ACCEPT:
285 bpm = tempo_dialog.get_bpm ();
286 bpm = max (0.01, bpm);
288 tempo_dialog.get_bbt_time (requested);
290 begin_reversible_command (_("add tempo mark"));
291 XMLNode &before = map.get_state();
292 map.add_tempo (Tempo (bpm), requested);
293 XMLNode &after = map.get_state();
294 session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
295 commit_reversible_command ();
299 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
303 Editor::mouse_add_new_meter_event (nframes_t frame)
310 TempoMap& map(session->tempo_map());
311 MeterDialog meter_dialog (map, frame, _("add"));
313 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
314 meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
316 ensure_float (meter_dialog);
318 switch (meter_dialog.run ()) {
319 case RESPONSE_ACCEPT:
325 double bpb = meter_dialog.get_bpb ();
326 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
328 double note_type = meter_dialog.get_note_type ();
331 meter_dialog.get_bbt_time (requested);
333 begin_reversible_command (_("add meter mark"));
334 XMLNode &before = map.get_state();
335 map.add_meter (Meter (bpb, note_type), requested);
336 session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
337 commit_reversible_command ();
341 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
345 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
348 TempoMarker* tempo_marker;
350 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
351 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
355 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
356 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
360 if (tempo_marker->tempo().movable()) {
361 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
366 Editor::edit_meter_section (MeterSection* section)
368 MeterDialog meter_dialog (*section, _("done"));
370 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
372 ensure_float (meter_dialog);
374 switch (meter_dialog.run()) {
375 case RESPONSE_ACCEPT:
381 double bpb = meter_dialog.get_bpb ();
382 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
384 double note_type = meter_dialog.get_note_type ();
386 begin_reversible_command (_("replace tempo mark"));
387 XMLNode &before = session->tempo_map().get_state();
388 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
389 XMLNode &after = session->tempo_map().get_state();
390 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
391 commit_reversible_command ();
393 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
397 Editor::edit_tempo_section (TempoSection* section)
399 TempoDialog tempo_dialog (*section, _("done"));
401 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
403 ensure_float (tempo_dialog);
405 switch (tempo_dialog.run ()) {
406 case RESPONSE_ACCEPT:
412 double bpm = tempo_dialog.get_bpm ();
414 tempo_dialog.get_bbt_time(when);
415 bpm = max (0.01, bpm);
417 begin_reversible_command (_("replace tempo mark"));
418 XMLNode &before = session->tempo_map().get_state();
419 session->tempo_map().replace_tempo (*section, Tempo (bpm));
420 session->tempo_map().move_tempo (*section, when);
421 XMLNode &after = session->tempo_map().get_state();
422 session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
423 commit_reversible_command ();
425 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
429 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
432 TempoMarker* tempo_marker;
434 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
435 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
439 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
440 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
444 edit_tempo_section (&tempo_marker->tempo());
448 Editor::edit_meter_marker (ArdourCanvas::Item *item)
451 MeterMarker* meter_marker;
453 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
454 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
458 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
459 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
463 edit_meter_section (&meter_marker->meter());
467 Editor::real_remove_tempo_marker (TempoSection *section)
469 begin_reversible_command (_("remove tempo mark"));
470 XMLNode &before = session->tempo_map().get_state();
471 session->tempo_map().remove_tempo (*section);
472 XMLNode &after = session->tempo_map().get_state();
473 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
474 commit_reversible_command ();
476 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
482 Editor::remove_meter_marker (ArdourCanvas::Item* item)
485 MeterMarker* meter_marker;
487 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
488 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
492 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
493 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
497 if (meter_marker->meter().movable()) {
498 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
503 Editor::real_remove_meter_marker (MeterSection *section)
505 begin_reversible_command (_("remove tempo mark"));
506 XMLNode &before = session->tempo_map().get_state();
507 session->tempo_map().remove_meter (*section);
508 XMLNode &after = session->tempo_map().get_state();
509 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
510 commit_reversible_command ();
512 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);