Mixer strip uses button 1 where possible, make mouse click behaviour more consistent...
[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     $Id$
19 */
20
21 #include <cstdio> // for sprintf, grrr 
22 #include <cstdlib>
23 #include <cmath>
24 #include <string>
25 #include <climits>
26
27 #include <libgnomecanvasmm.h>
28
29 #include <pbd/error.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 "color.h"
46
47 #include "i18n.h"
48
49 using namespace std;
50 using namespace sigc;
51 using namespace ARDOUR;
52 using namespace Gtk;
53 using namespace Gtkmm2ext;
54 using namespace Editing;
55
56 void
57 Editor::remove_metric_marks ()
58 {
59         /* don't delete these while handling events, just punt till the GUI is idle */
60
61         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
62                 delete_when_idle (*x);
63         }
64         metric_marks.clear ();
65 }       
66
67 void
68 Editor::draw_metric_marks (const Metrics& metrics)
69 {
70         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
71                 const MeterSection *ms;
72                 const TempoSection *ts;
73                 char buf[64];
74                 
75                 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
76                         snprintf (buf, sizeof(buf), "%g/%g", ms->beats_per_bar(), ms->note_divisor ());
77                         metric_marks.push_back (new MeterMarker (*this, *meter_group, color_map[cMeterMarker], buf, 
78                                                                  *(const_cast<MeterSection*>(ms))));
79                 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
80                         snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
81                         metric_marks.push_back (new TempoMarker (*this, *tempo_group, color_map[cTempoMarker], buf, 
82                                                                  *(const_cast<TempoSection*>(ts))));
83                 }
84                 
85         }
86 }
87
88 void
89 Editor::tempo_map_changed (Change ignored)
90 {
91         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::tempo_map_changed), ignored));
92         
93         if (current_bbt_points) {
94                 delete current_bbt_points;
95                 current_bbt_points = 0;
96         }
97
98         if (session) {
99                 current_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
100         } else {
101                 current_bbt_points = 0;
102         }
103
104         redisplay_tempo ();
105 }
106
107 void
108 Editor::redisplay_tempo ()
109 {
110         update_tempo_based_rulers ();
111
112         remove_metric_marks (); 
113         hide_measures ();
114
115         if (session && current_bbt_points) {
116                 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
117                 draw_measures ();
118         }
119         
120 }
121
122 void
123 Editor::hide_measures ()
124 {
125         for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
126                 (*i)->hide();
127                 free_measure_lines.push_back (*i);
128         }
129         used_measure_lines.clear ();
130 }
131
132 ArdourCanvas::SimpleLine *
133 Editor::get_time_line ()
134 {
135          ArdourCanvas::SimpleLine *line;
136
137         if (free_measure_lines.empty()) {
138                 line = new ArdourCanvas::SimpleLine (*time_line_group);
139                 used_measure_lines.push_back (line);
140         } else {
141                 line = free_measure_lines.front();
142                 free_measure_lines.erase (free_measure_lines.begin());
143                 used_measure_lines.push_back (line);
144         }
145
146         return line;
147 }
148
149 void
150 Editor::draw_measures ()
151 {
152         if (session == 0 || _show_measures == false) {
153                 return;
154         }
155
156         TempoMap::BBTPointList::iterator i;
157         TempoMap::BBTPointList *all_bbt_points;
158         ArdourCanvas::SimpleLine *line;
159         gdouble xpos, last_xpos;
160         uint32_t cnt;
161         uint32_t color;
162
163         if (current_bbt_points == 0 || current_bbt_points->empty()) {
164                 return;
165         }
166
167         all_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
168
169         cnt = 0;
170         last_xpos = 0;
171
172         /* get the first bar spacing */
173
174         gdouble last_beat = DBL_MAX;
175         gdouble beat_spacing = 0;
176
177         for (i = all_bbt_points->begin(); i != all_bbt_points->end() && beat_spacing == 0; ++i) {
178                 TempoMap::BBTPoint& p = (*i);
179
180                 switch (p.type) {
181                 case TempoMap::Bar:
182                         break;
183
184                 case TempoMap::Beat:
185                         xpos = frame_to_unit (p.frame);
186                         if (last_beat < xpos) {
187                                 beat_spacing = xpos - last_beat;
188                         }
189                         last_beat = xpos;
190                 }
191         }
192
193         double x1, x2, y1, y2;
194         track_canvas.get_scroll_region (x1, y1, x2, y2);
195
196         for (i = all_bbt_points->begin(); i != all_bbt_points->end(); ++i) {
197
198                 TempoMap::BBTPoint& p = (*i);
199
200                 switch (p.type) {
201                 case TempoMap::Bar:
202                         break;
203
204                 case TempoMap::Beat:
205                         xpos = frame_to_unit (p.frame);
206                         
207                         if (p.beat == 1) {
208                                 color = color_map[cMeasureLineBeat];
209                         } else {
210                                 color = color_map[cMeasureLineBar];
211
212                                 /* only draw beat lines if the gaps between beats
213                                    are large.
214                                 */
215
216                                 if (beat_spacing < 25.0) {
217                                         break;
218                                 }
219                         }
220                         
221                         if (cnt == 0 || xpos - last_xpos > 4.0) {
222                                 line = get_time_line ();
223                                 line->property_x1() = xpos;
224                                 line->property_x2() = xpos;
225                                 line->property_y2() = y2;
226                                 line->property_color_rgba() = color;
227                                 line->raise_to_top();
228                                 line->show();
229                                 last_xpos = xpos;       
230                                 ++cnt;
231                         } 
232                         break;
233                 }
234         }
235
236         delete all_bbt_points;
237
238         /* the cursors are always on top of everything */
239
240         cursor_group->raise_to_top();
241         time_line_group->lower_to_bottom();
242 }
243
244 void
245 Editor::mouse_add_new_tempo_event (jack_nframes_t frame)
246 {
247         if (session == 0) {
248                 return;
249         }
250
251         TempoMap& map(session->tempo_map());
252         TempoDialog tempo_dialog (map, frame, _("add"));
253         
254         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
255         tempo_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
256
257         ensure_float (tempo_dialog);
258
259         switch (tempo_dialog.run()) {
260         case RESPONSE_ACCEPT:
261                 break;
262         default:
263                 return;
264         }
265
266         double bpm = 0;
267         BBT_Time requested;
268         
269         bpm = tempo_dialog.get_bpm ();
270         bpm = max (0.01, bpm);
271         
272         tempo_dialog.get_bbt_time (requested);
273         
274         begin_reversible_command (_("add tempo mark"));
275         session->add_undo (map.get_memento());
276         map.add_tempo (Tempo (bpm), requested);
277         session->add_redo_no_execute (map.get_memento());
278         commit_reversible_command ();
279         
280         map.dump (cerr);
281 }
282
283 void
284 Editor::mouse_add_new_meter_event (jack_nframes_t frame)
285 {
286         if (session == 0) {
287                 return;
288         }
289
290
291         TempoMap& map(session->tempo_map());
292         MeterDialog meter_dialog (map, frame, _("add"));
293
294         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
295         meter_dialog.signal_realize().connect (bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
296
297         ensure_float (meter_dialog);
298         
299         switch (meter_dialog.run ()) {
300         case RESPONSE_ACCEPT:
301                 break;
302         default:
303                 return;
304         }
305
306         double bpb = meter_dialog.get_bpb ();
307         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
308         
309         double note_type = meter_dialog.get_note_type ();
310         BBT_Time requested;
311         
312         meter_dialog.get_bbt_time (requested);
313         
314         begin_reversible_command (_("add meter mark"));
315         session->add_undo (map.get_memento());
316         map.add_meter (Meter (bpb, note_type), requested);
317         session->add_redo_no_execute (map.get_memento());
318         commit_reversible_command ();
319         
320         map.dump (cerr);
321 }
322
323 void
324 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
325 {
326         Marker* marker;
327         TempoMarker* tempo_marker;
328
329         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
330                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
331                 /*NOTREACHED*/
332         }
333
334         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
335                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
336                 /*NOTREACHED*/
337         }               
338
339         if (tempo_marker->tempo().movable()) {
340           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
341         }
342 }
343
344 void
345 Editor::edit_meter_section (MeterSection* section)
346 {
347         MeterDialog meter_dialog (*section, _("done"));
348
349         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
350
351         ensure_float (meter_dialog);
352
353         switch (meter_dialog.run()) {
354         case RESPONSE_ACCEPT:
355                 break;
356         default:
357                 return;
358         }
359
360         double bpb = meter_dialog.get_bpb ();
361         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
362         
363         double note_type = meter_dialog.get_note_type ();
364
365         begin_reversible_command (_("replace tempo mark"));
366         session->add_undo (session->tempo_map().get_memento());
367         session->tempo_map().replace_meter (*section, Meter (bpb, note_type));
368         session->add_redo_no_execute (session->tempo_map().get_memento());
369         commit_reversible_command ();
370 }
371
372 void
373 Editor::edit_tempo_section (TempoSection* section)
374 {
375         TempoDialog tempo_dialog (*section, _("done"));
376
377         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
378
379         ensure_float (tempo_dialog);
380         
381         switch (tempo_dialog.run ()) {
382         case RESPONSE_ACCEPT:
383                 break;
384         default:
385                 return;
386         }
387
388         double bpm = tempo_dialog.get_bpm ();
389         BBT_Time when;
390         tempo_dialog.get_bbt_time(when);
391         bpm = max (0.01, bpm);
392         
393         begin_reversible_command (_("replace tempo mark"));
394         session->add_undo (session->tempo_map().get_memento());
395         session->tempo_map().replace_tempo (*section, Tempo (bpm));
396         session->tempo_map().move_tempo (*section, when);
397         session->add_redo_no_execute (session->tempo_map().get_memento());
398         commit_reversible_command ();
399 }
400
401 void
402 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
403 {
404         Marker* marker;
405         TempoMarker* tempo_marker;
406
407         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
408                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
409                 /*NOTREACHED*/
410         }
411
412         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
413                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
414                 /*NOTREACHED*/
415         }               
416
417         edit_tempo_section (&tempo_marker->tempo());
418 }
419
420 void
421 Editor::edit_meter_marker (ArdourCanvas::Item *item)
422 {
423         Marker* marker;
424         MeterMarker* meter_marker;
425
426         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
427                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
428                 /*NOTREACHED*/
429         }
430
431         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
432                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
433                 /*NOTREACHED*/
434         }               
435         
436         edit_meter_section (&meter_marker->meter());
437 }
438
439 gint
440 Editor::real_remove_tempo_marker (TempoSection *section)
441 {
442         begin_reversible_command (_("remove tempo mark"));
443         session->add_undo (session->tempo_map().get_memento());
444         session->tempo_map().remove_tempo (*section);
445         session->add_redo_no_execute (session->tempo_map().get_memento());
446         commit_reversible_command ();
447
448         return FALSE;
449 }
450
451 void
452 Editor::remove_meter_marker (ArdourCanvas::Item* item)
453 {
454         Marker* marker;
455         MeterMarker* meter_marker;
456
457         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
458                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
459                 /*NOTREACHED*/
460         }
461
462         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
463                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
464                 /*NOTREACHED*/
465         }               
466
467         if (meter_marker->meter().movable()) {
468           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
469         }
470 }
471
472 gint
473 Editor::real_remove_meter_marker (MeterSection *section)
474 {
475         begin_reversible_command (_("remove tempo mark"));
476         session->add_undo (session->tempo_map().get_memento());
477         session->tempo_map().remove_meter (*section);
478         session->add_redo_no_execute (session->tempo_map().get_memento());
479         commit_reversible_command ();
480         return FALSE;
481 }