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>
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"
51 using namespace ARDOUR;
54 using namespace Gtkmm2ext;
55 using namespace Editing;
58 Editor::remove_metric_marks ()
60 /* don't delete these while handling events, just punt till the GUI is idle */
62 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
63 delete_when_idle (*x);
65 metric_marks.clear ();
69 Editor::draw_metric_marks (const Metrics& metrics)
71 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
72 const MeterSection *ms;
73 const TempoSection *ts;
76 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
77 snprintf (buf, sizeof(buf), "%g/%g", ms->beats_per_bar(), ms->note_divisor ());
78 metric_marks.push_back (new MeterMarker (*this, *meter_group, color_map[cMeterMarker], buf,
79 *(const_cast<MeterSection*>(ms))));
80 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
81 snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
82 metric_marks.push_back (new TempoMarker (*this, *tempo_group, color_map[cTempoMarker], buf,
83 *(const_cast<TempoSection*>(ts))));
90 Editor::tempo_map_changed (Change ignored)
92 ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::tempo_map_changed), ignored));
94 if (current_bbt_points) {
95 delete current_bbt_points;
96 current_bbt_points = 0;
100 current_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
102 current_bbt_points = 0;
109 Editor::redisplay_tempo ()
111 update_tempo_based_rulers ();
113 remove_metric_marks ();
116 if (session && current_bbt_points) {
117 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
124 Editor::hide_measures ()
126 for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
128 free_measure_lines.push_back (*i);
130 used_measure_lines.clear ();
133 ArdourCanvas::SimpleLine *
134 Editor::get_time_line ()
136 ArdourCanvas::SimpleLine *line;
138 if (free_measure_lines.empty()) {
139 line = new ArdourCanvas::SimpleLine (*time_line_group);
140 used_measure_lines.push_back (line);
142 line = free_measure_lines.front();
143 free_measure_lines.erase (free_measure_lines.begin());
144 used_measure_lines.push_back (line);
151 Editor::draw_measures ()
153 if (session == 0 || _show_measures == false) {
157 TempoMap::BBTPointList::iterator i;
158 TempoMap::BBTPointList *all_bbt_points;
159 ArdourCanvas::SimpleLine *line;
160 gdouble xpos, last_xpos;
164 if (current_bbt_points == 0 || current_bbt_points->empty()) {
168 all_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
173 /* get the first bar spacing */
175 gdouble last_beat = DBL_MAX;
176 gdouble beat_spacing = 0;
178 for (i = all_bbt_points->begin(); i != all_bbt_points->end() && beat_spacing == 0; ++i) {
179 TempoMap::BBTPoint& p = (*i);
186 xpos = frame_to_unit (p.frame);
187 if (last_beat < xpos) {
188 beat_spacing = xpos - last_beat;
194 double x1, x2, y1, y2;
195 track_canvas.get_scroll_region (x1, y1, x2, y2);
197 for (i = all_bbt_points->begin(); i != all_bbt_points->end(); ++i) {
199 TempoMap::BBTPoint& p = (*i);
206 xpos = frame_to_unit (p.frame);
209 color = color_map[cMeasureLineBeat];
211 color = color_map[cMeasureLineBar];
213 /* only draw beat lines if the gaps between beats
217 if (beat_spacing < 25.0) {
222 if (cnt == 0 || xpos - last_xpos > 4.0) {
223 line = get_time_line ();
224 line->property_x1() = xpos;
225 line->property_x2() = xpos;
226 line->property_y2() = y2;
227 line->property_color_rgba() = color;
228 line->raise_to_top();
237 delete all_bbt_points;
239 /* the cursors are always on top of everything */
241 cursor_group->raise_to_top();
242 time_line_group->lower_to_bottom();
246 Editor::mouse_add_new_tempo_event (jack_nframes_t frame)
252 TempoMap& map(session->tempo_map());
253 TempoDialog tempo_dialog (map, frame, _("add"));
255 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
256 tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
258 ensure_float (tempo_dialog);
260 switch (tempo_dialog.run()) {
261 case RESPONSE_ACCEPT:
270 bpm = tempo_dialog.get_bpm ();
271 bpm = max (0.01, bpm);
273 tempo_dialog.get_bbt_time (requested);
275 begin_reversible_command (_("add tempo mark"));
276 XMLNode &before = map.get_state();
277 map.add_tempo (Tempo (bpm), requested);
278 XMLNode &after = map.get_state();
279 session->add_command(MementoCommand<TempoMap>(map, before, after));
280 commit_reversible_command ();
286 Editor::mouse_add_new_meter_event (jack_nframes_t frame)
293 TempoMap& map(session->tempo_map());
294 MeterDialog meter_dialog (map, frame, _("add"));
296 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
297 meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
299 ensure_float (meter_dialog);
301 switch (meter_dialog.run ()) {
302 case RESPONSE_ACCEPT:
308 double bpb = meter_dialog.get_bpb ();
309 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
311 double note_type = meter_dialog.get_note_type ();
314 meter_dialog.get_bbt_time (requested);
316 begin_reversible_command (_("add meter mark"));
317 XMLNode &before = map.get_state();
318 map.add_meter (Meter (bpb, note_type), requested);
319 session->add_command(MementoCommand<TempoMap>(map, before, map.get_state()));
320 commit_reversible_command ();
326 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
329 TempoMarker* tempo_marker;
331 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
332 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
336 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
337 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
341 if (tempo_marker->tempo().movable()) {
342 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
347 Editor::edit_meter_section (MeterSection* section)
349 MeterDialog meter_dialog (*section, _("done"));
351 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
353 ensure_float (meter_dialog);
355 switch (meter_dialog.run()) {
356 case RESPONSE_ACCEPT:
362 double bpb = meter_dialog.get_bpb ();
363 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
365 double note_type = meter_dialog.get_note_type ();
367 begin_reversible_command (_("replace tempo mark"));
368 XMLNode &before = session->tempo_map().get_state();
369 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
370 XMLNode &before = session->tempo_map().get_state();
371 session->add_command(MementoCommand<TempoMap>(session->tempo_map(), before, after));
372 commit_reversible_command ();
376 Editor::edit_tempo_section (TempoSection* section)
378 TempoDialog tempo_dialog (*section, _("done"));
380 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
382 ensure_float (tempo_dialog);
384 switch (tempo_dialog.run ()) {
385 case RESPONSE_ACCEPT:
391 double bpm = tempo_dialog.get_bpm ();
393 tempo_dialog.get_bbt_time(when);
394 bpm = max (0.01, bpm);
396 begin_reversible_command (_("replace tempo mark"));
397 XMLNode &before = session->tempo_map().get_state();
398 session->tempo_map().replace_tempo (*section, Tempo (bpm));
399 session->tempo_map().move_tempo (*section, when);
400 XMLNode &after = session->tempo_map().get_state();
401 session->add_command (MementoCommand<TempoMap>(session->tempo_map(), before, after));
402 commit_reversible_command ();
406 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
409 TempoMarker* tempo_marker;
411 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
412 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
416 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
417 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
421 edit_tempo_section (&tempo_marker->tempo());
425 Editor::edit_meter_marker (ArdourCanvas::Item *item)
428 MeterMarker* meter_marker;
430 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
431 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
435 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
436 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
440 edit_meter_section (&meter_marker->meter());
444 Editor::real_remove_tempo_marker (TempoSection *section)
446 begin_reversible_command (_("remove tempo mark"));
447 XMLNode &before = session->tempo_map().get_state();
448 session->tempo_map().remove_tempo (*section);
449 XMLNode &after = session->tempo_map().get_state();
450 session->add_command(MementoCommand<TempoMap>(session->tempo_map(), before, after));
451 commit_reversible_command ();
457 Editor::remove_meter_marker (ArdourCanvas::Item* item)
460 MeterMarker* meter_marker;
462 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
463 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
467 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
468 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
472 if (meter_marker->meter().movable()) {
473 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
478 Editor::real_remove_meter_marker (MeterSection *section)
480 begin_reversible_command (_("remove tempo mark"));
481 XMLNode &before = session->tempo_map().get_state();
482 session->tempo_map().remove_meter (*section);
483 XMLNode &after = session->tempo_map().get_state();
484 session->add_command(MementoCommand<TempoMap>(session->tempo_map(), before, after));
485 commit_reversible_command ();