change tooltip for group tab to suggest click-to-(de)activate
[ardour.git] / gtk2_ardour / editor_tempodisplay.cc
1 /*
2     Copyright (C) 2002 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cstdio> // for sprintf, grrr
21 #include <cstdlib>
22 #include <cmath>
23 #include <string>
24 #include <climits>
25
26 #include <libgnomecanvasmm.h>
27
28 #include "pbd/error.h"
29 #include "pbd/memento_command.h"
30
31 #include <gtkmm2ext/utils.h>
32 #include <gtkmm2ext/gtk_ui.h>
33
34 #include "ardour/session.h"
35 #include "ardour/tempo.h"
36 #include <gtkmm2ext/doi.h>
37 #include <gtkmm2ext/utils.h>
38
39 #include "editor.h"
40 #include "marker.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"
47 #include "tempo_lines.h"
48 #include "utils.h"
49
50 #include "i18n.h"
51
52 using namespace std;
53 using namespace ARDOUR;
54 using namespace PBD;
55 using namespace Gtk;
56 using namespace Gtkmm2ext;
57 using namespace Editing;
58
59 void
60 Editor::remove_metric_marks ()
61 {
62         /* don't delete these while handling events, just punt till the GUI is idle */
63
64         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
65                 delete_when_idle (*x);
66         }
67         metric_marks.clear ();
68 }
69
70 void
71 Editor::draw_metric_marks (const Metrics& metrics)
72 {
73
74         const MeterSection *ms;
75         const TempoSection *ts;
76         char buf[64];
77
78         remove_metric_marks ();
79
80         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
81
82                 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
83                         snprintf (buf, sizeof(buf), "%g/%g", ms->beats_per_bar(), ms->note_divisor ());
84                         metric_marks.push_back (new MeterMarker (*this, *meter_group, ARDOUR_UI::config()->canvasvar_MeterMarker.get(), buf,
85                                                                  *(const_cast<MeterSection*>(ms))));
86                 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
87                         snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
88                         metric_marks.push_back (new TempoMarker (*this, *tempo_group, ARDOUR_UI::config()->canvasvar_TempoMarker.get(), buf,
89                                                                  *(const_cast<TempoSection*>(ts))));
90                 }
91
92         }
93
94 }
95
96 void
97 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
98 {
99         if (!_session) {
100                 return;
101         }
102
103         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
104
105         if (tempo_lines) {
106                 tempo_lines->tempo_map_changed();
107         }
108
109         compute_current_bbt_points(leftmost_frame, leftmost_frame + current_page_frames());
110         _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
111         redraw_measures ();
112 }
113
114 void
115 Editor::redisplay_tempo (bool immediate_redraw)
116 {
117         if (!_session) {
118                 return;
119         }
120
121         compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_frames()); // redraw rulers and measures
122
123         compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_frames());
124         if (immediate_redraw) {
125                 redraw_measures ();
126         } else {
127 #ifdef GTKOSX
128                 redraw_measures ();
129 #else
130                 Glib::signal_idle().connect (sigc::mem_fun (*this, &Editor::redraw_measures));
131 #endif
132         }
133         update_tempo_based_rulers (); // redraw rulers and measures
134 }
135
136 void
137 Editor::compute_current_bbt_points (nframes_t leftmost, nframes_t rightmost)
138 {
139         if (!_session) {
140                 return;
141         }
142
143         BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
144
145         _session->bbt_time(leftmost, previous_beat);
146         _session->bbt_time(rightmost, next_beat);
147
148         if (previous_beat.beats > 1) {
149                 previous_beat.beats -= 1;
150         } else if (previous_beat.bars > 1) {
151                 previous_beat.bars--;
152                 previous_beat.beats += 1;
153         }
154         previous_beat.ticks = 0;
155
156         if (_session->tempo_map().meter_at(rightmost).beats_per_bar () > next_beat.beats + 1) {
157                 next_beat.beats += 1;
158         } else {
159                 next_beat.bars += 1;
160                 next_beat.beats = 1;
161         }
162         next_beat.ticks = 0;
163
164         delete current_bbt_points;
165         current_bbt_points = 0;
166
167         current_bbt_points = _session->tempo_map().get_points (_session->tempo_map().frame_time (previous_beat), _session->tempo_map().frame_time (next_beat) + 1);
168 }
169
170 void
171 Editor::hide_measures ()
172 {
173         if (tempo_lines)
174                 tempo_lines->hide();
175 }
176
177 bool
178 Editor::redraw_measures ()
179 {
180         draw_measures ();
181         return false;
182 }
183
184 void
185 Editor::draw_measures ()
186 {
187         if (_session == 0 || _show_measures == false ||
188             !current_bbt_points || current_bbt_points->empty()) {
189                 return;
190         }
191
192         if (tempo_lines == 0) {
193                 tempo_lines = new TempoLines(*track_canvas, time_line_group, physical_screen_height(get_window()));
194         }
195
196         tempo_lines->draw(*current_bbt_points, frames_per_unit);
197 }
198
199 void
200 Editor::mouse_add_new_tempo_event (framepos_t frame)
201 {
202         if (_session == 0) {
203                 return;
204         }
205
206         TempoMap& map(_session->tempo_map());
207         TempoDialog tempo_dialog (map, frame, _("add"));
208
209         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
210         //this causes compiz to display no border.
211         //tempo_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
212
213         ensure_float (tempo_dialog);
214
215         switch (tempo_dialog.run()) {
216         case RESPONSE_ACCEPT:
217                 break;
218         default:
219                 return;
220         }
221
222         double bpm = 0;
223         BBT_Time requested;
224
225         bpm = tempo_dialog.get_bpm ();
226         double nt = tempo_dialog.get_note_type();
227         bpm = max (0.01, bpm);
228
229         tempo_dialog.get_bbt_time (requested);
230
231         begin_reversible_command (_("add tempo mark"));
232         XMLNode &before = map.get_state();
233         map.add_tempo (Tempo (bpm,nt), requested);
234         XMLNode &after = map.get_state();
235         _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
236         commit_reversible_command ();
237
238         //map.dump (cerr);
239 }
240
241 void
242 Editor::mouse_add_new_meter_event (framepos_t frame)
243 {
244         if (_session == 0) {
245                 return;
246         }
247
248
249         TempoMap& map(_session->tempo_map());
250         MeterDialog meter_dialog (map, frame, _("add"));
251
252         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
253
254         //this causes compiz to display no border..
255         //meter_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
256
257         ensure_float (meter_dialog);
258
259         switch (meter_dialog.run ()) {
260         case RESPONSE_ACCEPT:
261                 break;
262         default:
263                 return;
264         }
265
266         double bpb = meter_dialog.get_bpb ();
267         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
268
269         double note_type = meter_dialog.get_note_type ();
270         BBT_Time requested;
271
272         meter_dialog.get_bbt_time (requested);
273
274         begin_reversible_command (_("add meter mark"));
275         XMLNode &before = map.get_state();
276         map.add_meter (Meter (bpb, note_type), requested);
277         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
278         commit_reversible_command ();
279
280         //map.dump (cerr);
281 }
282
283 void
284 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
285 {
286         Marker* marker;
287         TempoMarker* tempo_marker;
288
289         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
290                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
291                 /*NOTREACHED*/
292         }
293
294         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
295                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
296                 /*NOTREACHED*/
297         }
298
299         if (tempo_marker->tempo().movable()) {
300                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
301         }
302 }
303
304 void
305 Editor::edit_meter_section (MeterSection* section)
306 {
307         MeterDialog meter_dialog (*section, _("done"));
308
309         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
310
311         ensure_float (meter_dialog);
312
313         switch (meter_dialog.run()) {
314         case RESPONSE_ACCEPT:
315                 break;
316         default:
317                 return;
318         }
319
320         double bpb = meter_dialog.get_bpb ();
321         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
322
323         double note_type = meter_dialog.get_note_type ();
324
325         begin_reversible_command (_("replace tempo mark"));
326         XMLNode &before = _session->tempo_map().get_state();
327         _session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
328         XMLNode &after = _session->tempo_map().get_state();
329         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
330         commit_reversible_command ();
331 }
332
333 void
334 Editor::edit_tempo_section (TempoSection* section)
335 {
336         TempoDialog tempo_dialog (*section, _("done"));
337
338         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
339
340         ensure_float (tempo_dialog);
341
342         switch (tempo_dialog.run ()) {
343         case RESPONSE_ACCEPT:
344                 break;
345         default:
346                 return;
347         }
348
349         double bpm = tempo_dialog.get_bpm ();
350         double nt = tempo_dialog.get_note_type ();
351         BBT_Time when;
352         tempo_dialog.get_bbt_time(when);
353         bpm = max (0.01, bpm);
354
355         cerr << "Editing tempo section to be at " << when << endl;
356         _session->tempo_map().dump (cerr);
357         begin_reversible_command (_("replace tempo mark"));
358         XMLNode &before = _session->tempo_map().get_state();
359         _session->tempo_map().replace_tempo (*section, Tempo (bpm,nt));
360         _session->tempo_map().dump (cerr);
361         _session->tempo_map().move_tempo (*section, when);
362         _session->tempo_map().dump (cerr);
363         XMLNode &after = _session->tempo_map().get_state();
364         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
365         commit_reversible_command ();
366 }
367
368 void
369 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
370 {
371         Marker* marker;
372         TempoMarker* tempo_marker;
373
374         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
375                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
376                 /*NOTREACHED*/
377         }
378
379         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
380                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
381                 /*NOTREACHED*/
382         }
383
384         edit_tempo_section (&tempo_marker->tempo());
385 }
386
387 void
388 Editor::edit_meter_marker (ArdourCanvas::Item *item)
389 {
390         Marker* marker;
391         MeterMarker* meter_marker;
392
393         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
394                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
395                 /*NOTREACHED*/
396         }
397
398         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
399                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
400                 /*NOTREACHED*/
401         }
402
403         edit_meter_section (&meter_marker->meter());
404 }
405
406 gint
407 Editor::real_remove_tempo_marker (TempoSection *section)
408 {
409         begin_reversible_command (_("remove tempo mark"));
410         XMLNode &before = _session->tempo_map().get_state();
411         _session->tempo_map().remove_tempo (*section);
412         XMLNode &after = _session->tempo_map().get_state();
413         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
414         commit_reversible_command ();
415
416         return FALSE;
417 }
418
419 void
420 Editor::remove_meter_marker (ArdourCanvas::Item* item)
421 {
422         Marker* marker;
423         MeterMarker* meter_marker;
424
425         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
426                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
427                 /*NOTREACHED*/
428         }
429
430         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
431                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
432                 /*NOTREACHED*/
433         }
434
435         if (meter_marker->meter().movable()) {
436           Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
437         }
438 }
439
440 gint
441 Editor::real_remove_meter_marker (MeterSection *section)
442 {
443         begin_reversible_command (_("remove tempo mark"));
444         XMLNode &before = _session->tempo_map().get_state();
445         _session->tempo_map().remove_meter (*section);
446         XMLNode &after = _session->tempo_map().get_state();
447         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
448         commit_reversible_command ();
449
450         return FALSE;
451 }