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, bool immediate_redraw)
103 ENSURE_GUI_THREAD(bind (mem_fun (*this, &Editor::tempo_map_changed), ignored, immediate_redraw));
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;
138 if (immediate_redraw) {
142 if (session && current_bbt_points) {
148 if (session && current_bbt_points) {
149 Glib::signal_idle().connect (mem_fun (*this, &Editor::lazy_hide_and_draw_measures));
157 Editor::redisplay_tempo ()
162 Editor::hide_measures ()
164 for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
166 free_measure_lines.push_back (*i);
168 used_measure_lines.clear ();
171 ArdourCanvas::SimpleLine *
172 Editor::get_time_line ()
174 ArdourCanvas::SimpleLine *line;
176 if (free_measure_lines.empty()) {
177 line = new ArdourCanvas::SimpleLine (*time_line_group);
178 used_measure_lines.push_back (line);
180 line = free_measure_lines.front();
181 free_measure_lines.erase (free_measure_lines.begin());
182 used_measure_lines.push_back (line);
189 Editor::lazy_hide_and_draw_measures ()
197 Editor::draw_measures ()
199 if (session == 0 || _show_measures == false) {
203 TempoMap::BBTPointList::iterator i;
204 ArdourCanvas::SimpleLine *line;
206 double x1, x2, y1, y2, beat_density;
212 if (current_bbt_points == 0 || current_bbt_points->empty()) {
216 track_canvas.get_scroll_region (x1, y1, x2, y2);
217 y2 = TimeAxisView::hLargest*5000; // five thousand largest tracks should be enough.. :)
219 /* get the first bar spacing */
221 i = current_bbt_points->end();
223 bars = (*i).bar - (*current_bbt_points->begin()).bar;
224 beats = current_bbt_points->size() - bars;
226 beat_density = (beats * 10.0f) / track_canvas.get_width ();
228 if (beat_density > 4.0f) {
229 /* if the lines are too close together, they become useless
234 for (i = current_bbt_points->begin(); i != current_bbt_points->end(); ++i) {
242 if ((*i).beat == 1) {
243 color = color_map[cMeasureLineBar];
245 color = color_map[cMeasureLineBeat];
247 if (beat_density > 2.0) {
248 /* only draw beat lines if the gaps between beats are large.
254 xpos = frame_to_unit ((*i).frame);
255 line = get_time_line ();
256 line->property_x1() = xpos;
257 line->property_x2() = xpos;
258 line->property_y2() = y2;
259 line->property_color_rgba() = color;
260 //line->raise_to_top();
266 /* the cursors are always on top of everything */
268 cursor_group->raise_to_top();
269 time_line_group->lower_to_bottom();
274 Editor::mouse_add_new_tempo_event (nframes_t frame)
280 TempoMap& map(session->tempo_map());
281 TempoDialog tempo_dialog (map, frame, _("add"));
283 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
284 tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
286 ensure_float (tempo_dialog);
288 switch (tempo_dialog.run()) {
289 case RESPONSE_ACCEPT:
298 bpm = tempo_dialog.get_bpm ();
299 bpm = max (0.01, bpm);
301 tempo_dialog.get_bbt_time (requested);
303 begin_reversible_command (_("add tempo mark"));
304 XMLNode &before = map.get_state();
305 map.add_tempo (Tempo (bpm), requested);
306 XMLNode &after = map.get_state();
307 session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
308 commit_reversible_command ();
312 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
316 Editor::mouse_add_new_meter_event (nframes_t frame)
323 TempoMap& map(session->tempo_map());
324 MeterDialog meter_dialog (map, frame, _("add"));
326 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
327 meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
329 ensure_float (meter_dialog);
331 switch (meter_dialog.run ()) {
332 case RESPONSE_ACCEPT:
338 double bpb = meter_dialog.get_bpb ();
339 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
341 double note_type = meter_dialog.get_note_type ();
344 meter_dialog.get_bbt_time (requested);
346 begin_reversible_command (_("add meter mark"));
347 XMLNode &before = map.get_state();
348 map.add_meter (Meter (bpb, note_type), requested);
349 session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
350 commit_reversible_command ();
354 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
358 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
361 TempoMarker* tempo_marker;
363 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
364 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
368 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
369 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
373 if (tempo_marker->tempo().movable()) {
374 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
379 Editor::edit_meter_section (MeterSection* section)
381 MeterDialog meter_dialog (*section, _("done"));
383 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
385 ensure_float (meter_dialog);
387 switch (meter_dialog.run()) {
388 case RESPONSE_ACCEPT:
394 double bpb = meter_dialog.get_bpb ();
395 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
397 double note_type = meter_dialog.get_note_type ();
399 begin_reversible_command (_("replace tempo mark"));
400 XMLNode &before = session->tempo_map().get_state();
401 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
402 XMLNode &after = session->tempo_map().get_state();
403 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
404 commit_reversible_command ();
406 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
410 Editor::edit_tempo_section (TempoSection* section)
412 TempoDialog tempo_dialog (*section, _("done"));
414 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
416 ensure_float (tempo_dialog);
418 switch (tempo_dialog.run ()) {
419 case RESPONSE_ACCEPT:
425 double bpm = tempo_dialog.get_bpm ();
427 tempo_dialog.get_bbt_time(when);
428 bpm = max (0.01, bpm);
430 begin_reversible_command (_("replace tempo mark"));
431 XMLNode &before = session->tempo_map().get_state();
432 session->tempo_map().replace_tempo (*section, Tempo (bpm));
433 session->tempo_map().move_tempo (*section, when);
434 XMLNode &after = session->tempo_map().get_state();
435 session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
436 commit_reversible_command ();
438 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
442 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
445 TempoMarker* tempo_marker;
447 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
448 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
452 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
453 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
457 edit_tempo_section (&tempo_marker->tempo());
461 Editor::edit_meter_marker (ArdourCanvas::Item *item)
464 MeterMarker* meter_marker;
466 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
467 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
471 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
472 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
476 edit_meter_section (&meter_marker->meter());
480 Editor::real_remove_tempo_marker (TempoSection *section)
482 begin_reversible_command (_("remove tempo mark"));
483 XMLNode &before = session->tempo_map().get_state();
484 session->tempo_map().remove_tempo (*section);
485 XMLNode &after = session->tempo_map().get_state();
486 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
487 commit_reversible_command ();
489 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
495 Editor::remove_meter_marker (ArdourCanvas::Item* item)
498 MeterMarker* meter_marker;
500 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
501 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
505 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
506 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
510 if (meter_marker->meter().movable()) {
511 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
516 Editor::real_remove_meter_marker (MeterSection *section)
518 begin_reversible_command (_("remove tempo mark"));
519 XMLNode &before = session->tempo_map().get_state();
520 session->tempo_map().remove_meter (*section);
521 XMLNode &after = session->tempo_map().get_state();
522 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
523 commit_reversible_command ();
525 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);