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 "time_axis_view.h"
46 #include "ardour_ui.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 compute_current_bbt_points(leftmost_frame, leftmost_frame + (nframes_t)(edit_packer.get_width() * frames_per_unit));
105 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
106 update_tempo_based_rulers ();
107 if (tempo_map_change_idle_handler_id < 0) {
108 tempo_map_change_idle_handler_id = Glib::signal_idle().connect (mem_fun (*this, &Editor::redraw_measures));
113 Editor::redisplay_tempo (bool immediate_redraw)
119 compute_current_bbt_points (leftmost_frame, leftmost_frame + (nframes_t)(edit_packer.get_width() * frames_per_unit)); // redraw rulers and measures
121 if (immediate_redraw) {
125 if (current_bbt_points) {
129 } else if (tempo_map_change_idle_handler_id < 0) {
131 tempo_map_change_idle_handler_id = Glib::signal_idle().connect (mem_fun (*this, &Editor::redraw_measures));
135 update_tempo_based_rulers ();
139 Editor::compute_current_bbt_points (nframes_t leftmost, nframes_t rightmost)
145 BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
147 session->bbt_time(leftmost, previous_beat);
148 session->bbt_time(rightmost, next_beat);
150 if (previous_beat.beats > 1) {
151 previous_beat.beats -= 1;
152 } else if (previous_beat.bars > 1) {
153 previous_beat.bars--;
154 previous_beat.beats += 1;
156 previous_beat.ticks = 0;
158 if (session->tempo_map().meter_at(rightmost).beats_per_bar () > next_beat.beats + 1) {
159 next_beat.beats += 1;
166 if (current_bbt_points) {
167 delete current_bbt_points;
168 current_bbt_points = 0;
171 current_bbt_points = session->tempo_map().get_points (session->tempo_map().frame_time (previous_beat), session->tempo_map().frame_time (next_beat) + 1);
175 Editor::hide_measures ()
178 marker_tempo_lines->hide();
182 Editor::redraw_measures ()
186 tempo_map_change_idle_handler_id = -1;
191 Editor::draw_measures ()
193 if (session == 0 || _show_measures == false ||
194 !current_bbt_points || current_bbt_points->empty()) {
198 tempo_lines->draw(*current_bbt_points, frames_per_unit);
199 marker_tempo_lines->draw(*current_bbt_points, frames_per_unit);
201 /*time_line_group->raise_to_top();
202 time_line_group->lower(1);*/
203 marker_time_line_group->raise_to_top();
204 //marker_time_line_group->lower(1);
206 /* the cursors are always on top of everything */
207 cursor_group->raise_to_top();
213 Editor::mouse_add_new_tempo_event (nframes_t frame)
219 TempoMap& map(session->tempo_map());
220 TempoDialog tempo_dialog (map, frame, _("add"));
222 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
223 //this causes compiz to display no border.
224 //tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
226 ensure_float (tempo_dialog);
228 switch (tempo_dialog.run()) {
229 case RESPONSE_ACCEPT:
238 bpm = tempo_dialog.get_bpm ();
239 double nt = tempo_dialog.get_note_type();
240 bpm = max (0.01, bpm);
242 tempo_dialog.get_bbt_time (requested);
244 begin_reversible_command (_("add tempo mark"));
245 XMLNode &before = map.get_state();
246 map.add_tempo (Tempo (bpm,nt), requested);
247 XMLNode &after = map.get_state();
248 session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
249 commit_reversible_command ();
255 Editor::mouse_add_new_meter_event (nframes_t frame)
262 TempoMap& map(session->tempo_map());
263 MeterDialog meter_dialog (map, frame, _("add"));
265 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
267 //this causes compiz to display no border..
268 //meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
270 ensure_float (meter_dialog);
272 switch (meter_dialog.run ()) {
273 case RESPONSE_ACCEPT:
279 double bpb = meter_dialog.get_bpb ();
280 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
282 double note_type = meter_dialog.get_note_type ();
285 meter_dialog.get_bbt_time (requested);
287 begin_reversible_command (_("add meter mark"));
288 XMLNode &before = map.get_state();
289 map.add_meter (Meter (bpb, note_type), requested);
290 session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
291 commit_reversible_command ();
297 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
300 TempoMarker* tempo_marker;
302 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
303 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
307 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
308 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
312 if (tempo_marker->tempo().movable()) {
313 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
318 Editor::edit_meter_section (MeterSection* section)
320 MeterDialog meter_dialog (*section, _("done"));
322 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
324 ensure_float (meter_dialog);
326 switch (meter_dialog.run()) {
327 case RESPONSE_ACCEPT:
333 double bpb = meter_dialog.get_bpb ();
334 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
336 double note_type = meter_dialog.get_note_type ();
338 begin_reversible_command (_("replace tempo mark"));
339 XMLNode &before = session->tempo_map().get_state();
340 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
341 XMLNode &after = session->tempo_map().get_state();
342 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
343 commit_reversible_command ();
347 Editor::edit_tempo_section (TempoSection* section)
349 TempoDialog tempo_dialog (*section, _("done"));
351 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
353 ensure_float (tempo_dialog);
355 switch (tempo_dialog.run ()) {
356 case RESPONSE_ACCEPT:
362 double bpm = tempo_dialog.get_bpm ();
363 double nt = tempo_dialog.get_note_type ();
365 tempo_dialog.get_bbt_time(when);
366 bpm = max (0.01, bpm);
368 cerr << "Editing tempo section to be at " << when << endl;
369 session->tempo_map().dump (cerr);
370 begin_reversible_command (_("replace tempo mark"));
371 XMLNode &before = session->tempo_map().get_state();
372 session->tempo_map().replace_tempo (*section, Tempo (bpm,nt));
373 session->tempo_map().dump (cerr);
374 session->tempo_map().move_tempo (*section, when);
375 session->tempo_map().dump (cerr);
376 XMLNode &after = session->tempo_map().get_state();
377 session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
378 commit_reversible_command ();
382 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
385 TempoMarker* tempo_marker;
387 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
388 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
392 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
393 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
397 edit_tempo_section (&tempo_marker->tempo());
401 Editor::edit_meter_marker (ArdourCanvas::Item *item)
404 MeterMarker* meter_marker;
406 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
407 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
411 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
412 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
416 edit_meter_section (&meter_marker->meter());
420 Editor::real_remove_tempo_marker (TempoSection *section)
422 begin_reversible_command (_("remove tempo mark"));
423 XMLNode &before = session->tempo_map().get_state();
424 session->tempo_map().remove_tempo (*section);
425 XMLNode &after = session->tempo_map().get_state();
426 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
427 commit_reversible_command ();
433 Editor::remove_meter_marker (ArdourCanvas::Item* item)
436 MeterMarker* meter_marker;
438 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
439 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
443 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
444 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
448 if (meter_marker->meter().movable()) {
449 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
454 Editor::real_remove_meter_marker (MeterSection *section)
456 begin_reversible_command (_("remove tempo mark"));
457 XMLNode &before = session->tempo_map().get_state();
458 session->tempo_map().remove_meter (*section);
459 XMLNode &after = session->tempo_map().get_state();
460 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
461 commit_reversible_command ();