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)
103 ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::tempo_map_changed), ignored));
105 BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
107 session->bbt_time(leftmost_frame, previous_beat);
108 session->bbt_time(leftmost_frame + current_page_frames(), next_beat);
110 if (previous_beat.beats > 1) {
111 previous_beat.beats -= 1;
112 } else if (previous_beat.bars > 1) {
113 previous_beat.bars--;
114 previous_beat.beats += 1;
116 previous_beat.ticks = 0;
118 if (session->tempo_map().meter_at(leftmost_frame + current_page_frames()).beats_per_bar () > next_beat.beats + 1) {
119 next_beat.beats += 1;
126 if (current_bbt_points) {
127 delete current_bbt_points;
128 current_bbt_points = 0;
132 current_bbt_points = session->tempo_map().get_points (session->tempo_map().frame_time (previous_beat), session->tempo_map().frame_time (next_beat));
133 update_tempo_based_rulers ();
135 current_bbt_points = 0;
142 Editor::redisplay_tempo ()
145 if (session && current_bbt_points) {
146 Glib::signal_idle().connect (mem_fun (*this, &Editor::lazy_hide_and_draw_measures));
153 Editor::hide_measures ()
155 for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
157 free_measure_lines.push_back (*i);
159 used_measure_lines.clear ();
162 ArdourCanvas::SimpleLine *
163 Editor::get_time_line ()
165 ArdourCanvas::SimpleLine *line;
167 if (free_measure_lines.empty()) {
168 line = new ArdourCanvas::SimpleLine (*time_line_group);
169 used_measure_lines.push_back (line);
171 line = free_measure_lines.front();
172 free_measure_lines.erase (free_measure_lines.begin());
173 used_measure_lines.push_back (line);
180 Editor::lazy_hide_and_draw_measures ()
188 Editor::draw_measures ()
190 if (session == 0 || _show_measures == false) {
194 TempoMap::BBTPointList::iterator i;
195 ArdourCanvas::SimpleLine *line;
197 double x1, x2, y1, y2, beat_density;
203 if (current_bbt_points == 0 || current_bbt_points->empty()) {
207 track_canvas.get_scroll_region (x1, y1, x2, y2);
208 y2 = TimeAxisView::hLargest*5000; // five thousand largest tracks should be enough.. :)
210 /* get the first bar spacing */
212 i = current_bbt_points->end();
214 bars = (*i).bar - (*current_bbt_points->begin()).bar;
215 beats = current_bbt_points->size() - bars;
217 beat_density = (beats * 10.0f) / track_canvas.get_width ();
219 if (beat_density > 4.0f) {
220 /* if the lines are too close together, they become useless
225 for (i = current_bbt_points->begin(); i != current_bbt_points->end(); ++i) {
233 if ((*i).beat == 1) {
234 color = color_map[cMeasureLineBeat];
236 color = color_map[cMeasureLineBar];
238 if (beat_density > 2.0) {
239 /* only draw beat lines if the gaps between beats are large.
245 xpos = frame_to_unit ((*i).frame);
246 line = get_time_line ();
247 line->property_x1() = xpos;
248 line->property_x2() = xpos;
249 line->property_y2() = y2;
250 line->property_color_rgba() = color;
251 //line->raise_to_top();
257 /* the cursors are always on top of everything */
259 cursor_group->raise_to_top();
260 time_line_group->lower_to_bottom();
265 Editor::mouse_add_new_tempo_event (nframes_t frame)
271 TempoMap& map(session->tempo_map());
272 TempoDialog tempo_dialog (map, frame, _("add"));
274 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
275 tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
277 ensure_float (tempo_dialog);
279 switch (tempo_dialog.run()) {
280 case RESPONSE_ACCEPT:
289 bpm = tempo_dialog.get_bpm ();
290 bpm = max (0.01, bpm);
292 tempo_dialog.get_bbt_time (requested);
294 begin_reversible_command (_("add tempo mark"));
295 XMLNode &before = map.get_state();
296 map.add_tempo (Tempo (bpm), requested);
297 XMLNode &after = map.get_state();
298 session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
299 commit_reversible_command ();
303 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
307 Editor::mouse_add_new_meter_event (nframes_t frame)
314 TempoMap& map(session->tempo_map());
315 MeterDialog meter_dialog (map, frame, _("add"));
317 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
318 meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
320 ensure_float (meter_dialog);
322 switch (meter_dialog.run ()) {
323 case RESPONSE_ACCEPT:
329 double bpb = meter_dialog.get_bpb ();
330 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
332 double note_type = meter_dialog.get_note_type ();
335 meter_dialog.get_bbt_time (requested);
337 begin_reversible_command (_("add meter mark"));
338 XMLNode &before = map.get_state();
339 map.add_meter (Meter (bpb, note_type), requested);
340 session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
341 commit_reversible_command ();
345 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
349 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
352 TempoMarker* tempo_marker;
354 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
355 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
359 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
360 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
364 if (tempo_marker->tempo().movable()) {
365 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
370 Editor::edit_meter_section (MeterSection* section)
372 MeterDialog meter_dialog (*section, _("done"));
374 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
376 ensure_float (meter_dialog);
378 switch (meter_dialog.run()) {
379 case RESPONSE_ACCEPT:
385 double bpb = meter_dialog.get_bpb ();
386 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
388 double note_type = meter_dialog.get_note_type ();
390 begin_reversible_command (_("replace tempo mark"));
391 XMLNode &before = session->tempo_map().get_state();
392 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
393 XMLNode &after = session->tempo_map().get_state();
394 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
395 commit_reversible_command ();
397 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
401 Editor::edit_tempo_section (TempoSection* section)
403 TempoDialog tempo_dialog (*section, _("done"));
405 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
407 ensure_float (tempo_dialog);
409 switch (tempo_dialog.run ()) {
410 case RESPONSE_ACCEPT:
416 double bpm = tempo_dialog.get_bpm ();
418 tempo_dialog.get_bbt_time(when);
419 bpm = max (0.01, bpm);
421 begin_reversible_command (_("replace tempo mark"));
422 XMLNode &before = session->tempo_map().get_state();
423 session->tempo_map().replace_tempo (*section, Tempo (bpm));
424 session->tempo_map().move_tempo (*section, when);
425 XMLNode &after = session->tempo_map().get_state();
426 session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
427 commit_reversible_command ();
429 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
433 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
436 TempoMarker* tempo_marker;
438 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
439 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
443 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
444 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
448 edit_tempo_section (&tempo_marker->tempo());
452 Editor::edit_meter_marker (ArdourCanvas::Item *item)
455 MeterMarker* meter_marker;
457 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
458 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
462 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
463 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
467 edit_meter_section (&meter_marker->meter());
471 Editor::real_remove_tempo_marker (TempoSection *section)
473 begin_reversible_command (_("remove tempo mark"));
474 XMLNode &before = session->tempo_map().get_state();
475 session->tempo_map().remove_tempo (*section);
476 XMLNode &after = session->tempo_map().get_state();
477 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
478 commit_reversible_command ();
480 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
486 Editor::remove_meter_marker (ArdourCanvas::Item* item)
489 MeterMarker* meter_marker;
491 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
492 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
496 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
497 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
501 if (meter_marker->meter().movable()) {
502 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
507 Editor::real_remove_meter_marker (MeterSection *section)
509 begin_reversible_command (_("remove tempo mark"));
510 XMLNode &before = session->tempo_map().get_state();
511 session->tempo_map().remove_meter (*section);
512 XMLNode &after = session->tempo_map().get_state();
513 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
514 commit_reversible_command ();
516 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);