Compiles, but doesn't link. The link errors are mostly expected and are
[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 #include <pbd/memento_command.h>
31
32 #include <gtkmm2ext/utils.h>
33 #include <gtkmm2ext/gtk_ui.h>
34
35 #include <ardour/session.h>
36 #include <ardour/tempo.h>
37 #include <gtkmm2ext/doi.h>
38 #include <gtkmm2ext/utils.h>
39
40 #include "editor.h"
41 #include "marker.h"
42 #include "simpleline.h"
43 #include "tempo_dialog.h"
44 #include "rgb_macros.h"
45 #include "gui_thread.h"
46 #include "color.h"
47
48 #include "i18n.h"
49
50 using namespace std;
51 using namespace sigc;
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Gtk;
55 using namespace Gtkmm2ext;
56 using namespace Editing;
57
58 void
59 Editor::remove_metric_marks ()
60 {
61         /* don't delete these while handling events, just punt till the GUI is idle */
62
63         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
64                 delete_when_idle (*x);
65         }
66         metric_marks.clear ();
67 }       
68
69 void
70 Editor::draw_metric_marks (const Metrics& metrics)
71 {
72         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
73                 const MeterSection *ms;
74                 const TempoSection *ts;
75                 char buf[64];
76                 
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))));
85                 }
86                 
87         }
88 }
89
90 void
91 Editor::tempo_map_changed (Change ignored)
92 {
93         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::tempo_map_changed), ignored));
94         
95         if (current_bbt_points) {
96                 delete current_bbt_points;
97                 current_bbt_points = 0;
98         }
99
100         if (session) {
101                 current_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
102         } else {
103                 current_bbt_points = 0;
104         }
105
106         redisplay_tempo ();
107 }
108
109 void
110 Editor::redisplay_tempo ()
111 {
112         update_tempo_based_rulers ();
113
114         remove_metric_marks (); 
115         hide_measures ();
116
117         if (session && current_bbt_points) {
118                 session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks);
119                 draw_measures ();
120         }
121         
122 }
123
124 void
125 Editor::hide_measures ()
126 {
127         for (TimeLineList::iterator i = used_measure_lines.begin(); i != used_measure_lines.end(); ++i) {
128                 (*i)->hide();
129                 free_measure_lines.push_back (*i);
130         }
131         used_measure_lines.clear ();
132 }
133
134 ArdourCanvas::SimpleLine *
135 Editor::get_time_line ()
136 {
137          ArdourCanvas::SimpleLine *line;
138
139         if (free_measure_lines.empty()) {
140                 line = new ArdourCanvas::SimpleLine (*time_line_group);
141                 used_measure_lines.push_back (line);
142         } else {
143                 line = free_measure_lines.front();
144                 free_measure_lines.erase (free_measure_lines.begin());
145                 used_measure_lines.push_back (line);
146         }
147
148         return line;
149 }
150
151 void
152 Editor::draw_measures ()
153 {
154         if (session == 0 || _show_measures == false) {
155                 return;
156         }
157
158         TempoMap::BBTPointList::iterator i;
159         TempoMap::BBTPointList *all_bbt_points;
160         ArdourCanvas::SimpleLine *line;
161         gdouble xpos, last_xpos;
162         uint32_t cnt;
163         uint32_t color;
164
165         if (current_bbt_points == 0 || current_bbt_points->empty()) {
166                 return;
167         }
168
169         all_bbt_points = session->tempo_map().get_points (leftmost_frame, leftmost_frame + current_page_frames());
170
171         cnt = 0;
172         last_xpos = 0;
173
174         /* get the first bar spacing */
175
176         gdouble last_beat = DBL_MAX;
177         gdouble beat_spacing = 0;
178
179         for (i = all_bbt_points->begin(); i != all_bbt_points->end() && beat_spacing == 0; ++i) {
180                 TempoMap::BBTPoint& p = (*i);
181
182                 switch (p.type) {
183                 case TempoMap::Bar:
184                         break;
185
186                 case TempoMap::Beat:
187                         xpos = frame_to_unit (p.frame);
188                         if (last_beat < xpos) {
189                                 beat_spacing = xpos - last_beat;
190                         }
191                         last_beat = xpos;
192                 }
193         }
194
195         double x1, x2, y1, y2;
196         track_canvas.get_scroll_region (x1, y1, x2, y2);
197
198         for (i = all_bbt_points->begin(); i != all_bbt_points->end(); ++i) {
199
200                 TempoMap::BBTPoint& p = (*i);
201
202                 switch (p.type) {
203                 case TempoMap::Bar:
204                         break;
205
206                 case TempoMap::Beat:
207                         xpos = frame_to_unit (p.frame);
208                         
209                         if (p.beat == 1) {
210                                 color = color_map[cMeasureLineBeat];
211                         } else {
212                                 color = color_map[cMeasureLineBar];
213
214                                 /* only draw beat lines if the gaps between beats
215                                    are large.
216                                 */
217
218                                 if (beat_spacing < 25.0) {
219                                         break;
220                                 }
221                         }
222                         
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();
230                                 line->show();
231                                 last_xpos = xpos;       
232                                 ++cnt;
233                         } 
234                         break;
235                 }
236         }
237
238         delete all_bbt_points;
239
240         /* the cursors are always on top of everything */
241
242         cursor_group->raise_to_top();
243         time_line_group->lower_to_bottom();
244 }
245
246 void
247 Editor::mouse_add_new_tempo_event (jack_nframes_t frame)
248 {
249         if (session == 0) {
250                 return;
251         }
252
253         TempoMap& map(session->tempo_map());
254         TempoDialog tempo_dialog (map, frame, _("add"));
255         
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)));
258
259         ensure_float (tempo_dialog);
260
261         switch (tempo_dialog.run()) {
262         case RESPONSE_ACCEPT:
263                 break;
264         default:
265                 return;
266         }
267
268         double bpm = 0;
269         BBT_Time requested;
270         
271         bpm = tempo_dialog.get_bpm ();
272         bpm = max (0.01, bpm);
273         
274         tempo_dialog.get_bbt_time (requested);
275         
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 ();
282         
283         map.dump (cerr);
284 }
285
286 void
287 Editor::mouse_add_new_meter_event (jack_nframes_t frame)
288 {
289         if (session == 0) {
290                 return;
291         }
292
293
294         TempoMap& map(session->tempo_map());
295         MeterDialog meter_dialog (map, frame, _("add"));
296
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)));
299
300         ensure_float (meter_dialog);
301         
302         switch (meter_dialog.run ()) {
303         case RESPONSE_ACCEPT:
304                 break;
305         default:
306                 return;
307         }
308
309         double bpb = meter_dialog.get_bpb ();
310         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
311         
312         double note_type = meter_dialog.get_note_type ();
313         BBT_Time requested;
314         
315         meter_dialog.get_bbt_time (requested);
316         
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 ();
322         
323         map.dump (cerr);
324 }
325
326 void
327 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
328 {
329         Marker* marker;
330         TempoMarker* tempo_marker;
331
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;
334                 /*NOTREACHED*/
335         }
336
337         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
338                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
339                 /*NOTREACHED*/
340         }               
341
342         if (tempo_marker->tempo().movable()) {
343           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
344         }
345 }
346
347 void
348 Editor::edit_meter_section (MeterSection* section)
349 {
350         MeterDialog meter_dialog (*section, _("done"));
351
352         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
353
354         ensure_float (meter_dialog);
355
356         switch (meter_dialog.run()) {
357         case RESPONSE_ACCEPT:
358                 break;
359         default:
360                 return;
361         }
362
363         double bpb = meter_dialog.get_bpb ();
364         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
365         
366         double note_type = meter_dialog.get_note_type ();
367
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 ();
374 }
375
376 void
377 Editor::edit_tempo_section (TempoSection* section)
378 {
379         TempoDialog tempo_dialog (*section, _("done"));
380
381         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
382
383         ensure_float (tempo_dialog);
384         
385         switch (tempo_dialog.run ()) {
386         case RESPONSE_ACCEPT:
387                 break;
388         default:
389                 return;
390         }
391
392         double bpm = tempo_dialog.get_bpm ();
393         BBT_Time when;
394         tempo_dialog.get_bbt_time(when);
395         bpm = max (0.01, bpm);
396         
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 ();
404 }
405
406 void
407 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
408 {
409         Marker* marker;
410         TempoMarker* tempo_marker;
411
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;
414                 /*NOTREACHED*/
415         }
416
417         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
418                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
419                 /*NOTREACHED*/
420         }               
421
422         edit_tempo_section (&tempo_marker->tempo());
423 }
424
425 void
426 Editor::edit_meter_marker (ArdourCanvas::Item *item)
427 {
428         Marker* marker;
429         MeterMarker* meter_marker;
430
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;
433                 /*NOTREACHED*/
434         }
435
436         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
437                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
438                 /*NOTREACHED*/
439         }               
440         
441         edit_meter_section (&meter_marker->meter());
442 }
443
444 gint
445 Editor::real_remove_tempo_marker (TempoSection *section)
446 {
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 ();
453
454         return FALSE;
455 }
456
457 void
458 Editor::remove_meter_marker (ArdourCanvas::Item* item)
459 {
460         Marker* marker;
461         MeterMarker* meter_marker;
462
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;
465                 /*NOTREACHED*/
466         }
467
468         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
469                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
470                 /*NOTREACHED*/
471         }               
472
473         if (meter_marker->meter().movable()) {
474           Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
475         }
476 }
477
478 gint
479 Editor::real_remove_meter_marker (MeterSection *section)
480 {
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 ();
487         return FALSE;
488 }