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"
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)
72 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
73 const MeterSection *ms;
74 const TempoSection *ts;
77 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
78 snprintf (buf, sizeof(buf), "%g/%g", ms->beats_per_bar(), ms->note_divisor ());
79 metric_marks.push_back (new MeterMarker (*this, *meter_group, color_map[cMeterMarker], buf,
80 *(const_cast<MeterSection*>(ms))));
81 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
82 snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
83 metric_marks.push_back (new TempoMarker (*this, *tempo_group, color_map[cTempoMarker], buf,
84 *(const_cast<TempoSection*>(ts))));
91 Editor::tempo_map_changed (Change ignored)
93 ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::tempo_map_changed), ignored));
95 if (current_bbt_points) {
96 delete current_bbt_points;
97 current_bbt_points = 0;
101 current_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
103 current_bbt_points = 0;
110 Editor::redisplay_tempo ()
112 update_tempo_based_rulers ();
114 remove_metric_marks ();
117 if (session && current_bbt_points) {
118 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
125 Editor::hide_measures ()
127 for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
129 free_measure_lines.push_back (*i);
131 used_measure_lines.clear ();
134 ArdourCanvas::SimpleLine *
135 Editor::get_time_line ()
137 ArdourCanvas::SimpleLine *line;
139 if (free_measure_lines.empty()) {
140 line = new ArdourCanvas::SimpleLine (*time_line_group);
141 used_measure_lines.push_back (line);
143 line = free_measure_lines.front();
144 free_measure_lines.erase (free_measure_lines.begin());
145 used_measure_lines.push_back (line);
152 Editor::draw_measures ()
154 if (session == 0 || _show_measures == false) {
158 TempoMap::BBTPointList::iterator i;
159 TempoMap::BBTPointList *all_bbt_points;
160 ArdourCanvas::SimpleLine *line;
161 gdouble xpos, last_xpos;
165 if (current_bbt_points == 0 || current_bbt_points->empty()) {
169 all_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
174 /* get the first bar spacing */
176 gdouble last_beat = DBL_MAX;
177 gdouble beat_spacing = 0;
179 for (i = all_bbt_points->begin(); i != all_bbt_points->end() && beat_spacing == 0; ++i) {
180 TempoMap::BBTPoint& p = (*i);
187 xpos = frame_to_unit (p.frame);
188 if (last_beat < xpos) {
189 beat_spacing = xpos - last_beat;
195 double x1, x2, y1, y2;
196 track_canvas.get_scroll_region (x1, y1, x2, y2);
199 for (i = all_bbt_points->begin(); i != all_bbt_points->end(); ++i) {
201 TempoMap::BBTPoint& p = (*i);
208 xpos = frame_to_unit (p.frame);
211 color = color_map[cMeasureLineBeat];
213 color = color_map[cMeasureLineBar];
215 /* only draw beat lines if the gaps between beats
219 if (beat_spacing < 25.0) {
224 if (cnt == 0 || xpos - last_xpos > 4.0) {
225 line = get_time_line ();
226 line->property_x1() = xpos;
227 line->property_x2() = xpos;
228 line->property_y2() = y2;
229 line->property_color_rgba() = color;
230 line->raise_to_top();
239 delete all_bbt_points;
241 /* the cursors are always on top of everything */
243 cursor_group->raise_to_top();
244 time_line_group->lower_to_bottom();
248 Editor::mouse_add_new_tempo_event (jack_nframes_t frame)
254 TempoMap& map(session->tempo_map());
255 TempoDialog tempo_dialog (map, frame, _("add"));
257 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
258 tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
260 ensure_float (tempo_dialog);
262 switch (tempo_dialog.run()) {
263 case RESPONSE_ACCEPT:
272 bpm = tempo_dialog.get_bpm ();
273 bpm = max (0.01, bpm);
275 tempo_dialog.get_bbt_time (requested);
277 begin_reversible_command (_("add tempo mark"));
278 XMLNode &before = map.get_state();
279 map.add_tempo (Tempo (bpm), requested);
280 XMLNode &after = map.get_state();
281 session->add_command(new MementoCommand<TempoMap>(map, before, after));
282 commit_reversible_command ();
288 Editor::mouse_add_new_meter_event (jack_nframes_t frame)
295 TempoMap& map(session->tempo_map());
296 MeterDialog meter_dialog (map, frame, _("add"));
298 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
299 meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
301 ensure_float (meter_dialog);
303 switch (meter_dialog.run ()) {
304 case RESPONSE_ACCEPT:
310 double bpb = meter_dialog.get_bpb ();
311 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
313 double note_type = meter_dialog.get_note_type ();
316 meter_dialog.get_bbt_time (requested);
318 begin_reversible_command (_("add meter mark"));
319 XMLNode &before = map.get_state();
320 map.add_meter (Meter (bpb, note_type), requested);
321 session->add_command(new MementoCommand<TempoMap>(map, before, map.get_state()));
322 commit_reversible_command ();
328 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
331 TempoMarker* tempo_marker;
333 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
334 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
338 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
339 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
343 if (tempo_marker->tempo().movable()) {
344 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
349 Editor::edit_meter_section (MeterSection* section)
351 MeterDialog meter_dialog (*section, _("done"));
353 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
355 ensure_float (meter_dialog);
357 switch (meter_dialog.run()) {
358 case RESPONSE_ACCEPT:
364 double bpb = meter_dialog.get_bpb ();
365 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
367 double note_type = meter_dialog.get_note_type ();
369 begin_reversible_command (_("replace tempo mark"));
370 XMLNode &before = session->tempo_map().get_state();
371 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
372 XMLNode &after = session->tempo_map().get_state();
373 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), before, after));
374 commit_reversible_command ();
378 Editor::edit_tempo_section (TempoSection* section)
380 TempoDialog tempo_dialog (*section, _("done"));
382 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
384 ensure_float (tempo_dialog);
386 switch (tempo_dialog.run ()) {
387 case RESPONSE_ACCEPT:
393 double bpm = tempo_dialog.get_bpm ();
395 tempo_dialog.get_bbt_time(when);
396 bpm = max (0.01, bpm);
398 begin_reversible_command (_("replace tempo mark"));
399 XMLNode &before = session->tempo_map().get_state();
400 session->tempo_map().replace_tempo (*section, Tempo (bpm));
401 session->tempo_map().move_tempo (*section, when);
402 XMLNode &after = session->tempo_map().get_state();
403 session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), before, after));
404 commit_reversible_command ();
408 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
411 TempoMarker* tempo_marker;
413 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
414 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
418 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
419 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
423 edit_tempo_section (&tempo_marker->tempo());
427 Editor::edit_meter_marker (ArdourCanvas::Item *item)
430 MeterMarker* meter_marker;
432 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
433 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
437 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
438 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
442 edit_meter_section (&meter_marker->meter());
446 Editor::real_remove_tempo_marker (TempoSection *section)
448 begin_reversible_command (_("remove tempo mark"));
449 XMLNode &before = session->tempo_map().get_state();
450 session->tempo_map().remove_tempo (*section);
451 XMLNode &after = session->tempo_map().get_state();
452 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), before, after));
453 commit_reversible_command ();
459 Editor::remove_meter_marker (ArdourCanvas::Item* item)
462 MeterMarker* meter_marker;
464 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
465 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
469 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
470 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
474 if (meter_marker->meter().movable()) {
475 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
480 Editor::real_remove_meter_marker (MeterSection *section)
482 begin_reversible_command (_("remove tempo mark"));
483 XMLNode &before = session->tempo_map().get_state();
484 session->tempo_map().remove_meter (*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 ();