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"
45 #include "ardour_ui.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, ARDOUR_UI::config()->canvasvar_MeterMarker.get(), 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, ARDOUR_UI::config()->canvasvar_TempoMarker.get(), buf,
88 *(const_cast<TempoSection*>(ts))));
96 Editor::tempo_map_changed (Change ignored)
102 ENSURE_GUI_THREAD(bind (mem_fun (*this, &Editor::tempo_map_changed), ignored));
104 redisplay_tempo (false); // redraw rulers and measures
105 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
109 * This code was originally in tempo_map_changed, but this is called every time the canvas scrolls horizontally.
110 * That's why this is moved in here. The new tempo_map_changed is called when the ARDOUR::TempoMap actually changed.
113 Editor::redisplay_tempo (bool immediate_redraw)
119 BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
121 session->bbt_time(leftmost_frame, previous_beat);
122 session->bbt_time(leftmost_frame + current_page_frames(), next_beat);
124 if (previous_beat.beats > 1) {
125 previous_beat.beats -= 1;
126 } else if (previous_beat.bars > 1) {
127 previous_beat.bars--;
128 previous_beat.beats += 1;
130 previous_beat.ticks = 0;
132 if (session->tempo_map().meter_at(leftmost_frame + current_page_frames()).beats_per_bar () > next_beat.beats + 1) {
133 next_beat.beats += 1;
140 if (current_bbt_points) {
141 delete current_bbt_points;
142 current_bbt_points = 0;
146 current_bbt_points = session->tempo_map().get_points (session->tempo_map().frame_time (previous_beat), session->tempo_map().frame_time (next_beat));
147 update_tempo_based_rulers ();
149 current_bbt_points = 0;
152 if (immediate_redraw) {
156 if (session && current_bbt_points) {
162 if (session && current_bbt_points) {
163 Glib::signal_idle().connect (mem_fun (*this, &Editor::lazy_hide_and_draw_measures));
171 Editor::hide_measures ()
173 for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
175 free_measure_lines.push_back (*i);
177 used_measure_lines.clear ();
180 ArdourCanvas::SimpleLine *
181 Editor::get_time_line ()
183 ArdourCanvas::SimpleLine *line;
185 if (free_measure_lines.empty()) {
186 line = new ArdourCanvas::SimpleLine (*time_line_group);
187 used_measure_lines.push_back (line);
189 line = free_measure_lines.front();
190 free_measure_lines.erase (free_measure_lines.begin());
191 used_measure_lines.push_back (line);
198 Editor::lazy_hide_and_draw_measures ()
206 Editor::draw_measures ()
208 if (session == 0 || _show_measures == false) {
212 TempoMap::BBTPointList::iterator i;
213 ArdourCanvas::SimpleLine *line;
221 if (current_bbt_points == 0 || current_bbt_points->empty()) {
225 /* get the first bar spacing */
227 i = current_bbt_points->end();
229 bars = (*i).bar - (*current_bbt_points->begin()).bar;
230 beats = current_bbt_points->size() - bars;
232 beat_density = (beats * 10.0f) / track_canvas->get_width ();
234 if (beat_density > 4.0f) {
235 /* if the lines are too close together, they become useless
240 for (i = current_bbt_points->begin(); i != current_bbt_points->end(); ++i) {
248 if ((*i).beat == 1) {
249 color = ARDOUR_UI::config()->canvasvar_MeasureLineBar.get();
251 color = ARDOUR_UI::config()->canvasvar_MeasureLineBeat.get();
253 if (beat_density > 2.0) {
254 /* only draw beat lines if the gaps between beats are large.
260 xpos = frame_to_unit ((nframes64_t) (*i).frame);
261 line = get_time_line ();
262 line->property_x1() = xpos;
263 line->property_x2() = xpos;
264 line->property_y2() = canvas_height;
265 line->property_color_rgba() = color;
266 //line->raise_to_top();
273 logo_item->lower_to_bottom ();
279 Editor::mouse_add_new_tempo_event (nframes64_t frame)
285 TempoMap& map(session->tempo_map());
286 TempoDialog tempo_dialog (map, frame, _("add"));
288 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
289 //this causes compiz to display no border.
290 //tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
292 ensure_float (tempo_dialog);
294 switch (tempo_dialog.run()) {
295 case RESPONSE_ACCEPT:
304 bpm = tempo_dialog.get_bpm ();
305 double nt = tempo_dialog.get_note_type();
306 bpm = max (0.01, bpm);
308 tempo_dialog.get_bbt_time (requested);
310 begin_reversible_command (_("add tempo mark"));
311 XMLNode &before = map.get_state();
312 map.add_tempo (Tempo (bpm,nt), requested);
313 XMLNode &after = map.get_state();
314 session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
315 commit_reversible_command ();
321 Editor::mouse_add_new_meter_event (nframes64_t frame)
328 TempoMap& map(session->tempo_map());
329 MeterDialog meter_dialog (map, frame, _("add"));
331 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
333 //this causes compiz to display no border..
334 //meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
336 ensure_float (meter_dialog);
338 switch (meter_dialog.run ()) {
339 case RESPONSE_ACCEPT:
345 double bpb = meter_dialog.get_bpb ();
346 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
348 double note_type = meter_dialog.get_note_type ();
351 meter_dialog.get_bbt_time (requested);
353 begin_reversible_command (_("add meter mark"));
354 XMLNode &before = map.get_state();
355 map.add_meter (Meter (bpb, note_type), requested);
356 session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
357 commit_reversible_command ();
363 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
366 TempoMarker* tempo_marker;
368 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
369 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
373 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
374 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
378 if (tempo_marker->tempo().movable()) {
379 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
384 Editor::edit_meter_section (MeterSection* section)
386 MeterDialog meter_dialog (*section, _("done"));
388 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
390 ensure_float (meter_dialog);
392 switch (meter_dialog.run()) {
393 case RESPONSE_ACCEPT:
399 double bpb = meter_dialog.get_bpb ();
400 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
402 double note_type = meter_dialog.get_note_type ();
404 begin_reversible_command (_("replace tempo mark"));
405 XMLNode &before = session->tempo_map().get_state();
406 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
407 XMLNode &after = session->tempo_map().get_state();
408 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
409 commit_reversible_command ();
413 Editor::edit_tempo_section (TempoSection* section)
415 TempoDialog tempo_dialog (*section, _("done"));
417 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
419 ensure_float (tempo_dialog);
421 switch (tempo_dialog.run ()) {
422 case RESPONSE_ACCEPT:
428 double bpm = tempo_dialog.get_bpm ();
429 double nt = tempo_dialog.get_note_type ();
431 tempo_dialog.get_bbt_time(when);
432 bpm = max (0.01, bpm);
434 cerr << "Editing tempo section to be at " << when << endl;
435 session->tempo_map().dump (cerr);
436 begin_reversible_command (_("replace tempo mark"));
437 XMLNode &before = session->tempo_map().get_state();
438 session->tempo_map().replace_tempo (*section, Tempo (bpm,nt));
439 session->tempo_map().dump (cerr);
440 session->tempo_map().move_tempo (*section, when);
441 session->tempo_map().dump (cerr);
442 XMLNode &after = session->tempo_map().get_state();
443 session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
444 commit_reversible_command ();
448 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
451 TempoMarker* tempo_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 ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
459 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
463 edit_tempo_section (&tempo_marker->tempo());
467 Editor::edit_meter_marker (ArdourCanvas::Item *item)
470 MeterMarker* meter_marker;
472 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
473 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
477 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
478 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
482 edit_meter_section (&meter_marker->meter());
486 Editor::real_remove_tempo_marker (TempoSection *section)
488 begin_reversible_command (_("remove tempo mark"));
489 XMLNode &before = session->tempo_map().get_state();
490 session->tempo_map().remove_tempo (*section);
491 XMLNode &after = session->tempo_map().get_state();
492 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
493 commit_reversible_command ();
499 Editor::remove_meter_marker (ArdourCanvas::Item* item)
502 MeterMarker* meter_marker;
504 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
505 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
509 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
510 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
514 if (meter_marker->meter().movable()) {
515 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
520 Editor::real_remove_meter_marker (MeterSection *section)
522 begin_reversible_command (_("remove tempo mark"));
523 XMLNode &before = session->tempo_map().get_state();
524 session->tempo_map().remove_meter (*section);
525 XMLNode &after = session->tempo_map().get_state();
526 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
527 commit_reversible_command ();