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);
198 for (i = all_bbt_points->begin(); i != all_bbt_points->end(); ++i) {
200 TempoMap::BBTPoint& p = (*i);
207 xpos = frame_to_unit (p.frame);
210 color = color_map[cMeasureLineBeat];
212 color = color_map[cMeasureLineBar];
214 /* only draw beat lines if the gaps between beats
218 if (beat_spacing < 25.0) {
223 if (cnt == 0 || xpos - last_xpos > 4.0) {
224 line = get_time_line ();
225 line->property_x1() = xpos;
226 line->property_x2() = xpos;
227 line->property_y2() = y2;
228 line->property_color_rgba() = color;
229 line->raise_to_top();
238 delete all_bbt_points;
240 /* the cursors are always on top of everything */
242 cursor_group->raise_to_top();
243 time_line_group->lower_to_bottom();
247 Editor::mouse_add_new_tempo_event (jack_nframes_t frame)
253 TempoMap& map(session->tempo_map());
254 TempoDialog tempo_dialog (map, frame, _("add"));
256 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
257 tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
259 ensure_float (tempo_dialog);
261 switch (tempo_dialog.run()) {
262 case RESPONSE_ACCEPT:
271 bpm = tempo_dialog.get_bpm ();
272 bpm = max (0.01, bpm);
274 tempo_dialog.get_bbt_time (requested);
276 begin_reversible_command (_("add tempo mark"));
277 XMLNode &before = map.get_state();
278 map.add_tempo (Tempo (bpm), requested);
279 XMLNode &after = map.get_state();
280 session->add_command(new MementoCommand<TempoMap>(map, before, after));
281 commit_reversible_command ();
287 Editor::mouse_add_new_meter_event (jack_nframes_t frame)
294 TempoMap& map(session->tempo_map());
295 MeterDialog meter_dialog (map, frame, _("add"));
297 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
298 meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
300 ensure_float (meter_dialog);
302 switch (meter_dialog.run ()) {
303 case RESPONSE_ACCEPT:
309 double bpb = meter_dialog.get_bpb ();
310 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
312 double note_type = meter_dialog.get_note_type ();
315 meter_dialog.get_bbt_time (requested);
317 begin_reversible_command (_("add meter mark"));
318 XMLNode &before = map.get_state();
319 map.add_meter (Meter (bpb, note_type), requested);
320 session->add_command(new MementoCommand<TempoMap>(map, before, map.get_state()));
321 commit_reversible_command ();
327 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
330 TempoMarker* tempo_marker;
332 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
333 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
337 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
338 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
342 if (tempo_marker->tempo().movable()) {
343 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
348 Editor::edit_meter_section (MeterSection* section)
350 MeterDialog meter_dialog (*section, _("done"));
352 meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
354 ensure_float (meter_dialog);
356 switch (meter_dialog.run()) {
357 case RESPONSE_ACCEPT:
363 double bpb = meter_dialog.get_bpb ();
364 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
366 double note_type = meter_dialog.get_note_type ();
368 begin_reversible_command (_("replace tempo mark"));
369 XMLNode &before = session->tempo_map().get_state();
370 session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
371 XMLNode &after = session->tempo_map().get_state();
372 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), before, after));
373 commit_reversible_command ();
377 Editor::edit_tempo_section (TempoSection* section)
379 TempoDialog tempo_dialog (*section, _("done"));
381 tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
383 ensure_float (tempo_dialog);
385 switch (tempo_dialog.run ()) {
386 case RESPONSE_ACCEPT:
392 double bpm = tempo_dialog.get_bpm ();
394 tempo_dialog.get_bbt_time(when);
395 bpm = max (0.01, bpm);
397 begin_reversible_command (_("replace tempo mark"));
398 XMLNode &before = session->tempo_map().get_state();
399 session->tempo_map().replace_tempo (*section, Tempo (bpm));
400 session->tempo_map().move_tempo (*section, when);
401 XMLNode &after = session->tempo_map().get_state();
402 session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), before, after));
403 commit_reversible_command ();
407 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
410 TempoMarker* tempo_marker;
412 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
413 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
417 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
418 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
422 edit_tempo_section (&tempo_marker->tempo());
426 Editor::edit_meter_marker (ArdourCanvas::Item *item)
429 MeterMarker* meter_marker;
431 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
432 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
436 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
437 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
441 edit_meter_section (&meter_marker->meter());
445 Editor::real_remove_tempo_marker (TempoSection *section)
447 begin_reversible_command (_("remove tempo mark"));
448 XMLNode &before = session->tempo_map().get_state();
449 session->tempo_map().remove_tempo (*section);
450 XMLNode &after = session->tempo_map().get_state();
451 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), before, after));
452 commit_reversible_command ();
458 Editor::remove_meter_marker (ArdourCanvas::Item* item)
461 MeterMarker* meter_marker;
463 if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
464 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
468 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
469 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
473 if (meter_marker->meter().movable()) {
474 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
479 Editor::real_remove_meter_marker (MeterSection *section)
481 begin_reversible_command (_("remove tempo mark"));
482 XMLNode &before = session->tempo_map().get_state();
483 session->tempo_map().remove_meter (*section);
484 XMLNode &after = session->tempo_map().get_state();
485 session->add_command(new MementoCommand<TempoMap>(session->tempo_map(), before, after));
486 commit_reversible_command ();