audun's tempo redraw fix; sort-of cache time axis view item name widths and avoid...
[ardour.git] / gtk2_ardour / editor_rulers.cc
1 /*
2     Copyright (C) 2000 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 <cmath>
22
23 #include <string>
24
25 #include <ardour/tempo.h>
26 #include <gtkmm2ext/gtk_ui.h>
27
28 #include "editor.h"
29 #include "editing.h"
30 #include "gtk-custom-hruler.h"
31 #include "gui_thread.h"
32
33 #include "i18n.h"
34
35 using namespace sigc;
36 using namespace ARDOUR;
37 using namespace PBD;
38 using namespace Gtk;
39 using namespace Editing;
40
41 Editor *Editor::ruler_editor;
42
43 /* the order here must match the "metric" enums in editor.h */
44
45 GtkCustomMetric Editor::ruler_metrics[4] = {
46         {1, Editor::_metric_get_smpte },
47         {1, Editor::_metric_get_bbt },
48         {1, Editor::_metric_get_frames },
49         {1, Editor::_metric_get_minsec }
50 };
51
52 void
53 Editor::initialize_rulers ()
54 {
55         ruler_editor = this;
56         ruler_grabbed_widget = 0;
57         
58         _ruler_separator = new Gtk::HSeparator();
59         _ruler_separator->set_size_request(-1, 2);
60         _ruler_separator->show();
61
62         _smpte_ruler = gtk_custom_hruler_new ();
63         smpte_ruler = Glib::wrap (_smpte_ruler);
64         smpte_ruler->set_name ("SMPTERuler");
65         smpte_ruler->set_size_request (-1, (int)timebar_height);
66         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_smpte_ruler), &ruler_metrics[ruler_metric_smpte]);
67         ruler_shown[ruler_metric_smpte] = true;
68         
69         _bbt_ruler = gtk_custom_hruler_new ();
70         bbt_ruler = Glib::wrap (_bbt_ruler);
71         bbt_ruler->set_name ("BBTRuler");
72         bbt_ruler->set_size_request (-1, (int)timebar_height);
73         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_bbt_ruler), &ruler_metrics[ruler_metric_bbt]);
74         ruler_shown[ruler_metric_bbt] = true;
75
76         _frames_ruler = gtk_custom_hruler_new ();
77         frames_ruler = Glib::wrap (_frames_ruler);
78         frames_ruler->set_name ("FramesRuler");
79         frames_ruler->set_size_request (-1, (int)timebar_height);
80         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_frames_ruler), &ruler_metrics[ruler_metric_frames]);
81
82         _minsec_ruler = gtk_custom_hruler_new ();
83         minsec_ruler = Glib::wrap (_minsec_ruler);
84         minsec_ruler->set_name ("MinSecRuler");
85         minsec_ruler->set_size_request (-1, (int)timebar_height);
86         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_minsec_ruler), &ruler_metrics[ruler_metric_minsec]);
87
88         ruler_shown[ruler_time_meter] = true;
89         ruler_shown[ruler_time_tempo] = true;
90         ruler_shown[ruler_time_marker] = true;
91         ruler_shown[ruler_time_range_marker] = true;
92         ruler_shown[ruler_time_transport_marker] = true;
93         ruler_shown[ruler_metric_frames] = false;
94         ruler_shown[ruler_metric_minsec] = false;
95         
96         smpte_ruler->set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
97         bbt_ruler->set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
98         frames_ruler->set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
99         minsec_ruler->set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
100
101         smpte_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
102         bbt_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
103         frames_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
104         minsec_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
105
106         smpte_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
107         bbt_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
108         frames_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
109         minsec_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
110         
111         smpte_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
112         bbt_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
113         frames_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
114         minsec_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
115         
116         visible_timebars = 7; /* 4 here, 3 in time_canvas */
117         ruler_pressed_button = 0;
118 }
119
120
121 gint
122 Editor::ruler_button_press (GdkEventButton* ev)
123 {
124         if (session == 0) {
125                 return FALSE;
126         }
127
128         ruler_pressed_button = ev->button;
129
130         // jlc: grab ev->window ?
131         //Gtk::Main::grab_add (*minsec_ruler);
132         Widget * grab_widget = 0;
133
134         if (smpte_ruler->is_realized() && ev->window == smpte_ruler->get_window()->gobj()) grab_widget = smpte_ruler;
135         else if (bbt_ruler->is_realized() && ev->window == bbt_ruler->get_window()->gobj()) grab_widget = bbt_ruler;
136         else if (frames_ruler->is_realized() && ev->window == frames_ruler->get_window()->gobj()) grab_widget = frames_ruler;
137         else if (minsec_ruler->is_realized() && ev->window == minsec_ruler->get_window()->gobj()) grab_widget = minsec_ruler;
138
139         if (grab_widget) {
140                 grab_widget->add_modal_grab ();
141                 ruler_grabbed_widget = grab_widget;
142         }
143
144         gint x,y;
145         Gdk::ModifierType state;
146
147         /* need to use the correct x,y, the event lies */
148         time_canvas_event_box.get_window()->get_pointer (x, y, state);
149
150         nframes_t where = leftmost_frame + pixel_to_frame (x);
151
152         switch (ev->button) {
153         case 1:
154                 /* transport playhead */
155                 snap_to (where);
156                 session->request_locate (where);
157                 break;
158
159         case 2:
160                 /* edit cursor */
161                 if (snap_type != Editing::SnapToEditCursor) {
162                         snap_to (where);
163                 }
164                 edit_cursor->set_position (where);
165                 edit_cursor_clock.set (where);
166                 break;
167
168         default:
169                 break;
170         }
171
172         return TRUE;
173 }
174
175 gint
176 Editor::ruler_button_release (GdkEventButton* ev)
177 {
178         gint x,y;
179         Gdk::ModifierType state;
180
181         /* need to use the correct x,y, the event lies */
182         time_canvas_event_box.get_window()->get_pointer (x, y, state);
183
184         ruler_pressed_button = 0;
185         
186         if (session == 0) {
187                 return FALSE;
188         }
189
190         hide_verbose_canvas_cursor();
191         stop_canvas_autoscroll();
192         
193         nframes_t where = leftmost_frame + pixel_to_frame (x);
194
195         switch (ev->button) {
196         case 1:
197                 /* transport playhead */
198                 snap_to (where);
199                 session->request_locate (where);
200                 break;
201
202         case 2:
203                 /* edit cursor */
204                 if (snap_type != Editing::SnapToEditCursor) {
205                         snap_to (where);
206                 }
207                 edit_cursor->set_position (where);
208                 edit_cursor_clock.set (where);
209                 break;
210
211         case 3:
212                 /* popup menu */
213                 snap_to (where);
214                 popup_ruler_menu (where);
215                 
216                 break;
217         default:
218                 break;
219         }
220
221
222         if (ruler_grabbed_widget) {
223                 ruler_grabbed_widget->remove_modal_grab();
224                 ruler_grabbed_widget = 0;
225         }
226
227         return TRUE;
228 }
229
230 gint
231 Editor::ruler_label_button_release (GdkEventButton* ev)
232 {
233         if (ev->button == 3)
234         {
235                 popup_ruler_menu();
236         }
237         
238         return TRUE;
239 }
240
241
242 gint
243 Editor::ruler_mouse_motion (GdkEventMotion* ev)
244 {
245         if (session == 0 || !ruler_pressed_button) {
246                 return FALSE;
247         }
248         
249         double wcx=0,wcy=0;
250         double cx=0,cy=0;
251
252         gint x,y;
253         Gdk::ModifierType state;
254
255         /* need to use the correct x,y, the event lies */
256         time_canvas_event_box.get_window()->get_pointer (x, y, state);
257
258         
259         track_canvas.c2w (x, y, wcx, wcy);
260         track_canvas.w2c (wcx, wcy, cx, cy);
261         
262         nframes_t where = leftmost_frame + pixel_to_frame (x);
263
264
265         /// ripped from maybe_autoscroll, and adapted to work here
266         nframes_t one_page = (nframes_t) rint (canvas_width * frames_per_unit);
267         nframes_t rightmost_frame = leftmost_frame + one_page;
268
269         if (autoscroll_timeout_tag < 0) {
270                 if (where > rightmost_frame) {
271                         if (rightmost_frame < max_frames) {
272                                 start_canvas_autoscroll (1);
273                         }
274                 } else if (where <= leftmost_frame) {
275                         if (leftmost_frame > 0) {
276                                 start_canvas_autoscroll (-1);
277                         }
278                 } 
279         } else {
280                 if (where >= leftmost_frame && where < rightmost_frame) {
281                         stop_canvas_autoscroll ();
282                 }
283         }
284         //////  
285         
286         snap_to (where);
287
288         Cursor* cursor = 0;
289         
290         switch (ruler_pressed_button) {
291         case 1:
292                 /* transport playhead */
293                 cursor = playhead_cursor;
294                 break;
295
296         case 2:
297                 /* edit cursor */
298                 cursor = edit_cursor;
299                 break;
300
301         default:
302                 break;
303         }
304
305         if (cursor)
306         {
307                 cursor->set_position (where);
308                 
309                 if (cursor == edit_cursor) {
310                         edit_cursor_clock.set (where);
311                 }
312                 
313                 show_verbose_time_cursor (where, 10, cx, 0);
314         }
315         
316         return TRUE;
317 }
318
319
320 void
321 Editor::popup_ruler_menu (nframes_t where, ItemType t)
322 {
323         using namespace Menu_Helpers;
324
325         if (editor_ruler_menu == 0) {
326                 editor_ruler_menu = new Menu;
327                 editor_ruler_menu->set_name ("ArdourContextMenu");
328         }
329
330         // always build from scratch
331         MenuList& ruler_items = editor_ruler_menu->items();
332         editor_ruler_menu->set_name ("ArdourContextMenu");
333         ruler_items.clear();
334
335         CheckMenuItem * mitem;
336
337         no_ruler_shown_update = true;
338
339         switch (t) {
340         case MarkerBarItem:
341                 ruler_items.push_back (MenuElem (_("New location marker"), bind ( mem_fun(*this, &Editor::mouse_add_new_marker), where)));
342                 ruler_items.push_back (MenuElem (_("Clear all locations"), mem_fun(*this, &Editor::clear_markers)));
343                 ruler_items.push_back (MenuElem (_("Unhide locations"), mem_fun(*this, &Editor::unhide_markers)));
344                 ruler_items.push_back (SeparatorElem ());
345                 break;
346         case RangeMarkerBarItem:
347                 //ruler_items.push_back (MenuElem (_("New Range")));
348                 ruler_items.push_back (MenuElem (_("Clear all ranges"), mem_fun(*this, &Editor::clear_ranges)));
349                 ruler_items.push_back (MenuElem (_("Unhide ranges"), mem_fun(*this, &Editor::unhide_ranges)));
350                 ruler_items.push_back (SeparatorElem ());
351
352                 break;
353         case TransportMarkerBarItem:
354
355                 break;
356                 
357         case TempoBarItem:
358                 ruler_items.push_back (MenuElem (_("New Tempo"), bind ( mem_fun(*this, &Editor::mouse_add_new_tempo_event), where)));
359                 ruler_items.push_back (MenuElem (_("Clear tempo")));
360                 ruler_items.push_back (SeparatorElem ());
361                 break;
362
363         case MeterBarItem:
364                 ruler_items.push_back (MenuElem (_("New Meter"), bind ( mem_fun(*this, &Editor::mouse_add_new_meter_event), where)));
365                 ruler_items.push_back (MenuElem (_("Clear meter")));
366                 ruler_items.push_back (SeparatorElem ());
367                 break;
368
369         default:
370                 break;
371         }
372         
373         ruler_items.push_back (CheckMenuElem (_("Min:Secs"), bind (mem_fun(*this, &Editor::ruler_toggled), (int)ruler_metric_minsec)));
374         mitem = (CheckMenuItem *) &ruler_items.back(); 
375         if (ruler_shown[ruler_metric_minsec]) {
376                 mitem->set_active(true);
377         }
378
379         ruler_items.push_back (CheckMenuElem (X_("Timecode"), bind (mem_fun(*this, &Editor::ruler_toggled), (int)ruler_metric_smpte)));
380         mitem = (CheckMenuItem *) &ruler_items.back(); 
381         if (ruler_shown[ruler_metric_smpte]) {
382                 mitem->set_active(true);
383         }
384
385         ruler_items.push_back (CheckMenuElem (_("Frames"), bind (mem_fun(*this, &Editor::ruler_toggled), (int)ruler_metric_frames)));
386         mitem = (CheckMenuItem *) &ruler_items.back(); 
387         if (ruler_shown[ruler_metric_frames]) {
388                 mitem->set_active(true);
389         }
390
391         ruler_items.push_back (CheckMenuElem (_("Bars:Beats"), bind (mem_fun(*this, &Editor::ruler_toggled), (int)ruler_metric_bbt)));
392         mitem = (CheckMenuItem *) &ruler_items.back(); 
393         if (ruler_shown[ruler_metric_bbt]) {
394                 mitem->set_active(true);
395         }
396
397         ruler_items.push_back (SeparatorElem ());
398
399         ruler_items.push_back (CheckMenuElem (_("Meter"), bind (mem_fun(*this, &Editor::ruler_toggled), (int)ruler_time_meter)));
400         mitem = (CheckMenuItem *) &ruler_items.back(); 
401         if (ruler_shown[ruler_time_meter]) {
402                 mitem->set_active(true);
403         }
404
405         ruler_items.push_back (CheckMenuElem (_("Tempo"), bind (mem_fun(*this, &Editor::ruler_toggled), (int)ruler_time_tempo)));
406         mitem = (CheckMenuItem *) &ruler_items.back(); 
407         if (ruler_shown[ruler_time_tempo]) {
408                 mitem->set_active(true);
409         }
410
411         ruler_items.push_back (CheckMenuElem (_("Location Markers"), bind (mem_fun(*this, &Editor::ruler_toggled), (int)ruler_time_marker)));
412         mitem = (CheckMenuItem *) &ruler_items.back(); 
413         if (ruler_shown[ruler_time_marker]) {
414                 mitem->set_active(true);
415         }
416
417         ruler_items.push_back (CheckMenuElem (_("Range Markers"), bind (mem_fun(*this, &Editor::ruler_toggled), (int)ruler_time_range_marker)));
418         mitem = (CheckMenuItem *) &ruler_items.back(); 
419         if (ruler_shown[ruler_time_range_marker]) {
420                 mitem->set_active(true);
421         }
422
423         ruler_items.push_back (CheckMenuElem (_("Loop/Punch Ranges"), bind (mem_fun(*this, &Editor::ruler_toggled), (int)ruler_time_transport_marker)));
424         mitem = (CheckMenuItem *) &ruler_items.back(); 
425         if (ruler_shown[ruler_time_transport_marker]) {
426                 mitem->set_active(true);
427         }
428         
429         editor_ruler_menu->popup (1, gtk_get_current_event_time());
430
431         no_ruler_shown_update = false;
432 }
433
434 void
435 Editor::ruler_toggled (int ruler)
436 {
437         if (!session) return;
438         if (ruler < 0 || ruler >= (int) sizeof(ruler_shown)) return;
439
440         if (no_ruler_shown_update) return;
441
442         if (ruler_shown[ruler]) {
443                 if (visible_timebars <= 1) {
444                         // must always have 1 visible
445                         return;
446                 }
447         }
448         
449         ruler_shown[ruler] = !ruler_shown[ruler];
450
451         update_ruler_visibility ();
452
453         // update session extra RulerVisibility
454         store_ruler_visibility ();
455 }
456
457 void
458 Editor::store_ruler_visibility ()
459 {
460         XMLNode* node = new XMLNode(X_("RulerVisibility"));
461
462         node->add_property (X_("smpte"), ruler_shown[ruler_metric_smpte] ? "yes": "no");
463         node->add_property (X_("bbt"), ruler_shown[ruler_metric_bbt] ? "yes": "no");
464         node->add_property (X_("frames"), ruler_shown[ruler_metric_frames] ? "yes": "no");
465         node->add_property (X_("minsec"), ruler_shown[ruler_metric_minsec] ? "yes": "no");
466         node->add_property (X_("tempo"), ruler_shown[ruler_time_tempo] ? "yes": "no");
467         node->add_property (X_("meter"), ruler_shown[ruler_time_meter] ? "yes": "no");
468         node->add_property (X_("marker"), ruler_shown[ruler_time_marker] ? "yes": "no");
469         node->add_property (X_("rangemarker"), ruler_shown[ruler_time_range_marker] ? "yes": "no");
470         node->add_property (X_("transportmarker"), ruler_shown[ruler_time_transport_marker] ? "yes": "no");
471
472         session->add_extra_xml (*node);
473         session->set_dirty ();
474 }
475
476 void 
477 Editor::restore_ruler_visibility ()
478 {
479         XMLProperty* prop;
480         XMLNode * node = session->extra_xml (X_("RulerVisibility"));
481
482         if (node) {
483                 if ((prop = node->property ("smpte")) != 0) {
484                         if (prop->value() == "yes") 
485                                 ruler_shown[ruler_metric_smpte] = true;
486                         else 
487                                 ruler_shown[ruler_metric_smpte] = false;
488                 }
489                 if ((prop = node->property ("bbt")) != 0) {
490                         if (prop->value() == "yes") 
491                                 ruler_shown[ruler_metric_bbt] = true;
492                         else 
493                                 ruler_shown[ruler_metric_bbt] = false;
494                 }
495                 if ((prop = node->property ("frames")) != 0) {
496                         if (prop->value() == "yes") 
497                                 ruler_shown[ruler_metric_frames] = true;
498                         else 
499                                 ruler_shown[ruler_metric_frames] = false;
500                 }
501                 if ((prop = node->property ("minsec")) != 0) {
502                         if (prop->value() == "yes") 
503                                 ruler_shown[ruler_metric_minsec] = true;
504                         else 
505                                 ruler_shown[ruler_metric_minsec] = false;
506                 }
507                 if ((prop = node->property ("tempo")) != 0) {
508                         if (prop->value() == "yes") 
509                                 ruler_shown[ruler_time_tempo] = true;
510                         else 
511                                 ruler_shown[ruler_time_tempo] = false;
512                 }
513                 if ((prop = node->property ("meter")) != 0) {
514                         if (prop->value() == "yes") 
515                                 ruler_shown[ruler_time_meter] = true;
516                         else 
517                                 ruler_shown[ruler_time_meter] = false;
518                 }
519                 if ((prop = node->property ("marker")) != 0) {
520                         if (prop->value() == "yes") 
521                                 ruler_shown[ruler_time_marker] = true;
522                         else 
523                                 ruler_shown[ruler_time_marker] = false;
524                 }
525                 if ((prop = node->property ("rangemarker")) != 0) {
526                         if (prop->value() == "yes") 
527                                 ruler_shown[ruler_time_range_marker] = true;
528                         else 
529                                 ruler_shown[ruler_time_range_marker] = false;
530                 }
531                 if ((prop = node->property ("transportmarker")) != 0) {
532                         if (prop->value() == "yes") 
533                                 ruler_shown[ruler_time_transport_marker] = true;
534                         else 
535                                 ruler_shown[ruler_time_transport_marker] = false;
536                 }
537
538         }
539
540         update_ruler_visibility ();
541 }
542
543
544 void
545 Editor::update_ruler_visibility ()
546 {
547         using namespace Box_Helpers;
548         BoxList & lab_children =  time_button_vbox.children();
549         BoxList & ruler_children =  time_canvas_vbox.children();
550
551         visible_timebars = 0;
552
553         lab_children.clear();
554
555         // leave the last one (the time_canvas) intact
556         while (ruler_children.size() > 1) {
557                 ruler_children.pop_front();
558         }
559
560         BoxList::iterator canvaspos = ruler_children.begin();
561         
562         _smpte_ruler = gtk_custom_hruler_new ();
563         smpte_ruler = Glib::wrap (_smpte_ruler);
564         smpte_ruler->set_name ("SMPTERuler");
565         smpte_ruler->set_size_request (-1, (int)timebar_height);
566         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_smpte_ruler), &ruler_metrics[ruler_metric_smpte]);
567         
568         _bbt_ruler = gtk_custom_hruler_new ();
569         bbt_ruler = Glib::wrap (_bbt_ruler);
570         bbt_ruler->set_name ("BBTRuler");
571         bbt_ruler->set_size_request (-1, (int)timebar_height);
572         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_bbt_ruler), &ruler_metrics[ruler_metric_bbt]);
573
574         _frames_ruler = gtk_custom_hruler_new ();
575         frames_ruler = Glib::wrap (_frames_ruler);
576         frames_ruler->set_name ("FramesRuler");
577         frames_ruler->set_size_request (-1, (int)timebar_height);
578         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_frames_ruler), &ruler_metrics[ruler_metric_frames]);
579
580         _minsec_ruler = gtk_custom_hruler_new ();
581         minsec_ruler = Glib::wrap (_minsec_ruler);
582         minsec_ruler->set_name ("MinSecRuler");
583         minsec_ruler->set_size_request (-1, (int)timebar_height);
584         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_minsec_ruler), &ruler_metrics[ruler_metric_minsec]);
585
586         
587         smpte_ruler->set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
588         bbt_ruler->set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
589         frames_ruler->set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
590         minsec_ruler->set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
591
592         smpte_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
593         bbt_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
594         frames_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
595         minsec_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
596
597         smpte_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
598         bbt_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
599         frames_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
600         minsec_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
601         
602         smpte_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
603         bbt_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
604         frames_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
605         minsec_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
606
607         ruler_children.insert (canvaspos, Element(*_ruler_separator, PACK_SHRINK, PACK_START));
608         
609         if (ruler_shown[ruler_metric_minsec]) {
610                 lab_children.push_back (Element(minsec_label, PACK_SHRINK, PACK_START));
611                 ruler_children.insert (canvaspos, Element(*minsec_ruler, PACK_SHRINK, PACK_START));
612                 visible_timebars++;
613         }
614
615         if (ruler_shown[ruler_metric_smpte]) {
616                 lab_children.push_back (Element(smpte_label, PACK_SHRINK, PACK_START));
617                 ruler_children.insert (canvaspos, Element(*smpte_ruler, PACK_SHRINK, PACK_START));
618                 visible_timebars++;
619         }
620
621         if (ruler_shown[ruler_metric_frames]) {
622                 lab_children.push_back (Element(frame_label, PACK_SHRINK, PACK_START));
623                 ruler_children.insert (canvaspos, Element(*frames_ruler, PACK_SHRINK, PACK_START));
624                 visible_timebars++;
625         }
626
627         if (ruler_shown[ruler_metric_bbt]) {
628                 lab_children.push_back (Element(bbt_label, PACK_SHRINK, PACK_START));
629                 ruler_children.insert (canvaspos, Element(*bbt_ruler, PACK_SHRINK, PACK_START));
630                 visible_timebars++;
631         }
632
633         double tbpos = 1.0;
634         double old_unit_pos ;
635         
636         if (ruler_shown[ruler_time_meter]) {
637                 lab_children.push_back (Element(meter_label, PACK_SHRINK, PACK_START));
638
639                 old_unit_pos = meter_group->property_y();
640                 if (tbpos != old_unit_pos) {
641                         meter_group->move ( 0.0, tbpos - old_unit_pos);
642                 }
643                 meter_group->show();
644                 tbpos += timebar_height;
645                 visible_timebars++;
646         }
647         else {
648                 meter_group->hide();
649         }
650         
651         if (ruler_shown[ruler_time_tempo]) {
652                 lab_children.push_back (Element(tempo_label, PACK_SHRINK, PACK_START));
653                 old_unit_pos = tempo_group->property_y();
654                 if (tbpos != old_unit_pos) {
655                         tempo_group->move(0.0, tbpos - old_unit_pos);
656                 }
657                 tempo_group->show();
658                 tbpos += timebar_height;
659                 visible_timebars++;
660         }
661         else {
662                 tempo_group->hide();
663         }
664         
665         if (ruler_shown[ruler_time_marker]) {
666                 lab_children.push_back (Element(mark_label, PACK_SHRINK, PACK_START));
667                 old_unit_pos = marker_group->property_y();
668                 if (tbpos != old_unit_pos) {
669                         marker_group->move ( 0.0, tbpos - old_unit_pos);
670                 }
671                 marker_group->show();
672                 tbpos += timebar_height;
673                 visible_timebars++;
674         }
675         else {
676                 marker_group->hide();
677         }
678         
679         if (ruler_shown[ruler_time_range_marker]) {
680                 lab_children.push_back (Element(range_mark_label, PACK_SHRINK, PACK_START));
681                 old_unit_pos = range_marker_group->property_y();
682                 if (tbpos != old_unit_pos) {
683                         range_marker_group->move (0.0, tbpos - old_unit_pos);
684                 }
685                 range_marker_group->show();
686                 tbpos += timebar_height;
687                 visible_timebars++;
688         }
689         else {
690                 range_marker_group->hide();
691         }
692
693         if (ruler_shown[ruler_time_transport_marker]) {
694                 lab_children.push_back (Element(transport_mark_label, PACK_SHRINK, PACK_START));
695                 old_unit_pos = transport_marker_group->property_y();
696                 if (tbpos != old_unit_pos) {
697                         transport_marker_group->move ( 0.0, tbpos - old_unit_pos);
698                 }
699                 transport_marker_group->show();
700                 tbpos += timebar_height;
701                 visible_timebars++;
702         }
703         else {
704                 transport_marker_group->hide();
705         }
706         
707         time_canvas_vbox.set_size_request (-1, (int)(timebar_height * visible_timebars));
708         time_canvas_event_box.queue_resize();
709         
710         update_fixed_rulers();
711         //update_tempo_based_rulers();
712         redisplay_tempo (false);
713
714         time_canvas_event_box.show_all();
715         time_button_frame.show_all();
716 }
717
718 void
719 Editor::update_just_smpte ()
720 {
721         ENSURE_GUI_THREAD(mem_fun(*this, &Editor::update_just_smpte));
722         
723         if (session == 0) {
724                 return;
725         }
726
727         /* XXX Note the potential loss of accuracy here as we convert from
728            an uint32_t (or larger) to a float ... what to do ?
729         */
730
731         nframes_t page = (nframes_t) floor (canvas_width * frames_per_unit);
732         nframes_t rightmost_frame = leftmost_frame + page;
733
734         if (ruler_shown[ruler_metric_smpte]) {
735                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_smpte_ruler), leftmost_frame, rightmost_frame,
736                                             leftmost_frame, session->current_end_frame());
737         }
738 }
739
740 void
741 Editor::update_fixed_rulers ()
742 {
743         nframes_t rightmost_frame;
744
745         if (session == 0) {
746                 return;
747         }
748
749         /* XXX Note the potential loss of accuracy here as we convert from
750            an uint32_t (or larger) to a float ... what to do ?
751         */
752
753         nframes_t page = (nframes_t) floor (canvas_width * frames_per_unit);
754
755         ruler_metrics[ruler_metric_smpte].units_per_pixel = frames_per_unit;
756         ruler_metrics[ruler_metric_frames].units_per_pixel = frames_per_unit;
757         ruler_metrics[ruler_metric_minsec].units_per_pixel = frames_per_unit;
758
759         rightmost_frame = leftmost_frame + page;
760
761         /* these force a redraw, which in turn will force execution of the metric callbacks
762            to compute the relevant ticks to display.
763         */
764
765         if (ruler_shown[ruler_metric_smpte]) {
766                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_smpte_ruler), leftmost_frame, rightmost_frame,
767                                             leftmost_frame, session->current_end_frame());
768         }
769         
770         if (ruler_shown[ruler_metric_frames]) {
771                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_frames_ruler), leftmost_frame, rightmost_frame,
772                                             leftmost_frame, session->current_end_frame());
773         }
774         
775         if (ruler_shown[ruler_metric_minsec]) {
776                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_minsec_ruler), leftmost_frame, rightmost_frame,
777                                             leftmost_frame, session->current_end_frame());
778         }
779 }               
780
781 void
782 Editor::update_tempo_based_rulers ()
783 {
784         if (session == 0) {
785                 return;
786         }
787
788         /* XXX Note the potential loss of accuracy here as we convert from
789            an uint32_t (or larger) to a float ... what to do ?
790         */
791
792         nframes_t page = (nframes_t) floor (canvas_width * frames_per_unit);
793         ruler_metrics[ruler_metric_bbt].units_per_pixel = frames_per_unit;
794
795         if (ruler_shown[ruler_metric_bbt]) {
796                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_bbt_ruler), leftmost_frame, leftmost_frame+page, 
797                                             leftmost_frame, session->current_end_frame());
798         }
799 }
800
801 /* Mark generation */
802
803 gint
804 Editor::_metric_get_smpte (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
805 {
806         return ruler_editor->metric_get_smpte (marks, lower, upper, maxchars);
807 }
808
809 gint
810 Editor::_metric_get_bbt (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
811 {
812         return ruler_editor->metric_get_bbt (marks, lower, upper, maxchars);
813 }
814
815 gint
816 Editor::_metric_get_frames (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
817 {
818         return ruler_editor->metric_get_frames (marks, lower, upper, maxchars);
819 }
820
821 gint
822 Editor::_metric_get_minsec (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
823 {
824         return ruler_editor->metric_get_minsec (marks, lower, upper, maxchars);
825 }
826
827 gint
828 Editor::metric_get_smpte (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
829 {
830         nframes_t range;
831         nframes_t pos;
832         nframes_t spacer;
833         nframes_t fr;
834         SMPTE::Time smpte;
835         gchar buf[16];
836         gint nmarks = 0;
837         gint n;
838         bool show_bits = false;
839         bool show_frames = false;
840         bool show_seconds = false;
841         bool show_minutes = false;
842         bool show_hours = false;
843         int mark_modulo;
844
845         if (session == 0) {
846                 return 0;
847         }
848
849         fr = session->frame_rate();
850
851         if (lower > (spacer = (nframes_t)(128 * Editor::get_current_zoom ()))) {
852                 lower = lower - spacer;
853         } else {
854                 lower = 0;
855         }
856         upper = upper + spacer;
857         range = (nframes_t) floor (upper - lower);
858
859         if (range < (2 * session->frames_per_smpte_frame())) { /* 0 - 2 frames */
860                 show_bits = true;
861                 mark_modulo = 20;
862                 nmarks = 1 + (2 * Config->get_subframes_per_frame());
863         } else if (range <= (fr / 4)) { /* 2 frames - 0.250 second */
864                 show_frames = true;
865                 mark_modulo = 1;
866                 nmarks = 1 + (range / (nframes_t)session->frames_per_smpte_frame());
867         } else if (range <= (fr / 2)) { /* 0.25-0.5 second */
868                 show_frames = true;
869                 mark_modulo = 2;
870                 nmarks = 1 + (range / (nframes_t)session->frames_per_smpte_frame());
871         } else if (range <= fr) { /* 0.5-1 second */
872                 show_frames = true;
873                 mark_modulo = 5;
874                 nmarks = 1 + (range / (nframes_t)session->frames_per_smpte_frame());
875         } else if (range <= 2 * fr) { /* 1-2 seconds */
876                 show_frames = true;
877                 mark_modulo = 10;
878                 nmarks = 1 + (range / (nframes_t)session->frames_per_smpte_frame());
879         } else if (range <= 8 * fr) { /* 2-8 seconds */
880                 show_seconds = true;
881                 mark_modulo = 1;
882                 nmarks = 1 + (range / fr);
883         } else if (range <= 16 * fr) { /* 8-16 seconds */
884                 show_seconds = true;
885                 mark_modulo = 2;
886                 nmarks = 1 + (range / fr);
887         } else if (range <= 30 * fr) { /* 16-30 seconds */
888                 show_seconds = true;
889                 mark_modulo = 5;
890                 nmarks = 1 + (range / fr);
891         } else if (range <= 60 * fr) { /* 30-60 seconds */
892                 show_seconds = true;
893                 mark_modulo = 5;
894                 nmarks = 1 + (range / fr);
895         } else if (range <= 2 * 60 * fr) { /* 1-2 minutes */
896                 show_seconds = true;
897                 mark_modulo = 20;
898                 nmarks = 1 + (range / fr);
899         } else if (range <= 4 * 60 * fr) { /* 2-4 minutes */
900                 show_seconds = true;
901                 mark_modulo = 30;
902                 nmarks = 1 + (range / fr);
903         } else if (range <= 10 * 60 * fr) { /* 4-10 minutes */
904                 show_minutes = true;
905                 mark_modulo = 2;
906                 nmarks = 1 + 10;
907         } else if (range <= 30 * 60 * fr) { /* 10-30 minutes */
908                 show_minutes = true;
909                 mark_modulo = 5;
910                 nmarks = 1 + 30;
911         } else if (range <= 60 * 60 * fr) { /* 30 minutes - 1hr */
912                 show_minutes = true;
913                 mark_modulo = 10;
914                 nmarks = 1 + 60;
915         } else if (range <= 4 * 60 * 60 * fr) { /* 1 - 4 hrs*/
916                 show_minutes = true;
917                 mark_modulo = 30;
918                 nmarks = 1 + (60 * 4);
919         } else if (range <= 8 * 60 * 60 * fr) { /* 4 - 8 hrs*/
920                 show_hours = true;
921                 mark_modulo = 1;
922                 nmarks = 1 + 8;
923         } else if (range <= 16 * 60 * 60 * fr) { /* 16-24 hrs*/
924                 show_hours = true;
925                 mark_modulo = 1;
926                 nmarks = 1 + 24;
927         } else {
928     
929                 /* not possible if nframes_t is a 32 bit quantity */
930     
931                 show_hours = true;
932                 mark_modulo = 4;
933                 nmarks = 1 + 24;
934         }
935   
936         pos = (nframes_t) floor (lower);
937         
938         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);  
939         
940         if (show_bits) {
941                 // Find smpte time of this sample (pos) with subframe accuracy
942                 session->sample_to_smpte(pos, smpte, true /* use_offset */, true /* use_subframes */ );
943     
944                 for (n = 0; n < nmarks; n++) {
945                         session->smpte_to_sample(smpte, pos, true /* use_offset */, true /* use_subframes */ );
946                         if ((smpte.subframes % mark_modulo) == 0) {
947                                 if (smpte.subframes == 0) {
948                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
949                                         snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
950                                 } else {
951                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
952                                         snprintf (buf, sizeof(buf), ".%02u", smpte.subframes);
953                                 }
954                         } else {
955                                 snprintf (buf, sizeof(buf)," ");
956                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
957         
958                         }
959                         (*marks)[n].label = g_strdup (buf);
960                         (*marks)[n].position = pos;
961
962                         // Increment subframes by one
963                         SMPTE::increment_subframes( smpte );
964                 }
965         } else if (show_seconds) {
966                 // Find smpte time of this sample (pos)
967                 session->sample_to_smpte(pos, smpte, true /* use_offset */, false /* use_subframes */ );
968                 // Go to next whole second down
969                 SMPTE::seconds_floor( smpte );
970
971                 for (n = 0; n < nmarks; n++) {
972                         session->smpte_to_sample(smpte, pos, true /* use_offset */, false /* use_subframes */ );
973                         if ((smpte.seconds % mark_modulo) == 0) {
974                                 if (smpte.seconds == 0) {
975                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
976                                         (*marks)[n].position = pos;
977                                 } else {
978                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
979                                         (*marks)[n].position = pos;
980                                 }
981                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
982                         } else {
983                                 snprintf (buf, sizeof(buf)," ");
984                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
985                                 (*marks)[n].position = pos;
986         
987                         }
988                         (*marks)[n].label = g_strdup (buf);
989                         SMPTE::increment_seconds( smpte );
990                 }
991         } else if (show_minutes) {
992                 // Find smpte time of this sample (pos)
993                 session->sample_to_smpte(pos, smpte, true /* use_offset */, false /* use_subframes */ );
994                 // Go to next whole minute down
995                 SMPTE::minutes_floor( smpte );
996
997                 for (n = 0; n < nmarks; n++) {
998                         session->smpte_to_sample(smpte, pos, true /* use_offset */, false /* use_subframes */ );
999                         if ((smpte.minutes % mark_modulo) == 0) {
1000                                 if (smpte.minutes == 0) {
1001                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1002                                 } else {
1003                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1004                                 }
1005                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
1006                         } else {
1007                                 snprintf (buf, sizeof(buf)," ");
1008                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1009         
1010                         }
1011                         (*marks)[n].label = g_strdup (buf);
1012                         (*marks)[n].position = pos;
1013                         SMPTE::increment_minutes( smpte );
1014                 }
1015         } else if (show_hours) {
1016                 // Find smpte time of this sample (pos)
1017                 session->sample_to_smpte(pos, smpte, true /* use_offset */, false /* use_subframes */ );
1018                 // Go to next whole hour down
1019                 SMPTE::hours_floor( smpte );
1020
1021                 for (n = 0; n < nmarks; n++) {
1022                         session->smpte_to_sample(smpte, pos, true /* use_offset */, false /* use_subframes */ );
1023                         if ((smpte.hours % mark_modulo) == 0) {
1024                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1025                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
1026                         } else {
1027                                 snprintf (buf, sizeof(buf)," ");
1028                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1029         
1030                         }
1031                         (*marks)[n].label = g_strdup (buf);
1032                         (*marks)[n].position = pos;
1033
1034                         SMPTE::increment_hours( smpte );
1035                 }
1036         } else { // show_frames
1037                 // Find smpte time of this sample (pos)
1038                 session->sample_to_smpte(pos, smpte, true /* use_offset */, false /* use_subframes */ );
1039                 // Go to next whole frame down
1040                 SMPTE::frames_floor( smpte );
1041
1042                 for (n = 0; n < nmarks; n++) {
1043                         session->smpte_to_sample(smpte, pos, true /* use_offset */, false /* use_subframes */ );
1044                         if ((smpte.frames % mark_modulo) == 0)  {
1045                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1046                                 (*marks)[n].position = pos;
1047                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
1048                         } else {
1049                                 snprintf (buf, sizeof(buf)," ");
1050                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1051                                 (*marks)[n].position = pos;
1052         
1053                         }
1054                         (*marks)[n].label = g_strdup (buf);
1055                         SMPTE::increment( smpte );
1056                 }
1057         }
1058   
1059         return nmarks;
1060 }
1061
1062
1063 gint
1064 Editor::metric_get_bbt (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
1065 {
1066         if (session == 0) {
1067                 return 0;
1068         }
1069
1070         TempoMap::BBTPointList::iterator i;
1071
1072         uint32_t beats = 0;
1073         uint32_t bars = 0;
1074         uint32_t desirable_marks;
1075         uint32_t magic_accent_number = 1;
1076         gint nmarks;
1077         char buf[64];
1078         gint  n = 0;
1079         nframes_t pos;
1080         bool bar_helper_on = true;
1081        
1082         BBT_Time next_beat;
1083         nframes_t next_beat_pos;
1084
1085         if ((desirable_marks = maxchars / 7) == 0) {
1086                return 0;
1087         }
1088
1089         /* align the tick marks to whatever we're snapping to... */
1090
1091         switch (snap_type) {
1092         case SnapToAThirdBeat:
1093                 bbt_beat_subdivision = 3;
1094                 break;
1095         case SnapToAQuarterBeat:
1096                 bbt_beat_subdivision = 4;
1097                 break;
1098         case SnapToAEighthBeat:
1099                 bbt_beat_subdivision = 8;
1100                 magic_accent_number = 2;
1101                 break;
1102         case SnapToASixteenthBeat:
1103                 bbt_beat_subdivision = 16;
1104                 magic_accent_number = 4;
1105                 break;
1106         case SnapToAThirtysecondBeat:
1107                 bbt_beat_subdivision = 32;
1108                 magic_accent_number = 8;
1109                 break;
1110         default:
1111                bbt_beat_subdivision = 4;
1112                 break;
1113         }
1114
1115         if (current_bbt_points == 0 || current_bbt_points->empty()) {
1116                 return 0;
1117         }
1118
1119         i = current_bbt_points->end();
1120         i--;
1121         bars = (*i).bar - (*current_bbt_points->begin()).bar;
1122         beats = current_bbt_points->size() - bars;
1123
1124         /*Only show the bar helper if there aren't many bars on the screen */
1125         if (bars > 1) {
1126                 bar_helper_on = false;
1127         }
1128
1129         if (desirable_marks > (beats / 4)) {
1130
1131                 /* we're in beat land...*/
1132
1133                 uint32_t tick = 0;
1134                 uint32_t skip;
1135                 uint32_t t;
1136                 nframes_t frame_skip;
1137                 double frame_skip_error;
1138                 double accumulated_error;
1139                 double position_of_helper;
1140                 bool i_am_accented = false;
1141                 bool we_need_ticks = false;
1142                 bool helper_active = false;
1143         
1144                 position_of_helper = lower + (30 * Editor::get_current_zoom ());
1145
1146                 if (desirable_marks >= (beats)) {
1147                         nmarks = (beats * bbt_beat_subdivision) + 1;
1148                         we_need_ticks = true;
1149                 } else {
1150                         nmarks = beats + 1;
1151                 }
1152
1153                 *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1154
1155                 (*marks)[0].label = g_strdup(" ");
1156                 (*marks)[0].position = lower;
1157                 (*marks)[0].style = GtkCustomRulerMarkMicro;
1158                 
1159                 for (n = 1,   i = current_bbt_points->begin(); n < nmarks && i != current_bbt_points->end(); ++i) {
1160
1161                         if ((*i).frame < lower && (bar_helper_on)) {
1162                                         snprintf (buf, sizeof(buf), "<%" PRIu32 "|%" PRIu32, (*i).bar, (*i).beat);
1163                                         (*marks)[0].label = g_strdup (buf); 
1164                                         helper_active = true;
1165                         } else {
1166
1167                           if ((*i).type == TempoMap::Bar)  {
1168                             if (((*i).frame < position_of_helper) && helper_active) {
1169                               snprintf (buf, sizeof(buf), " ");
1170                             } else {
1171                               snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1172                             }
1173                             (*marks)[n].label = g_strdup (buf);
1174                             (*marks)[n].position = (*i).frame;
1175                             (*marks)[n].style = GtkCustomRulerMarkMajor;
1176                             n++;
1177                             
1178                           } else if (((*i).type == TempoMap::Beat) && ((*i).beat > 1)) {
1179                             ((((*i).frame < position_of_helper) && bar_helper_on) || !we_need_ticks) ?
1180                               snprintf (buf, sizeof(buf), " ") : snprintf (buf, sizeof(buf), "%" PRIu32, (*i).beat);
1181                             if (((*i).beat % 2 == 1) || we_need_ticks) {
1182                               (*marks)[n].style = GtkCustomRulerMarkMinor;
1183                             } else {
1184                               (*marks)[n].style = GtkCustomRulerMarkMicro;
1185                             }
1186                             (*marks)[n].label =  g_strdup (buf);
1187                             (*marks)[n].position = (*i).frame;
1188                             n++;
1189                           }
1190
1191                         }
1192
1193
1194                         /* Add the tick marks */
1195
1196                         if (we_need_ticks && (*i).type == TempoMap::Beat) {
1197
1198                                 /* Find the next beat */
1199
1200                                 next_beat.beats = (*i).beat;
1201                                 next_beat.bars = (*i).bar;
1202
1203                                 if ((*i).meter->beats_per_bar() > (next_beat.beats + 1)) {
1204                                   next_beat.beats += 1;
1205                                 } else {
1206                                   next_beat.bars += 1;
1207                                   next_beat.beats = 1;
1208                                 }
1209                                 
1210                                 next_beat_pos = session->tempo_map().frame_time(next_beat);
1211
1212                                 frame_skip = (nframes_t) floor (frame_skip_error = (session->frame_rate() *  60) / (bbt_beat_subdivision * (*i).tempo->beats_per_minute()));
1213                                 frame_skip_error -= frame_skip;
1214                                 skip = (uint32_t) (Meter::ticks_per_beat / bbt_beat_subdivision);
1215
1216                                 pos = (*i).frame + frame_skip;
1217                                 accumulated_error = frame_skip_error;
1218
1219                                 tick = skip;
1220
1221                                 for (t = 0; (tick < Meter::ticks_per_beat) && (n < nmarks) && (pos < next_beat_pos) ; pos += frame_skip, tick += skip, ++t) {
1222
1223                                         if (t % magic_accent_number == (magic_accent_number - 1)) {
1224                                                 i_am_accented = true;
1225                                         }
1226                                         if (Editor::get_current_zoom () > 32) {
1227                                                 snprintf (buf, sizeof(buf), " ");
1228                                         } else if ((Editor::get_current_zoom () > 8) && !i_am_accented) {
1229                                                 snprintf (buf, sizeof(buf), " ");
1230                                         } else  if (bar_helper_on && (pos < position_of_helper)) {
1231                                                 snprintf (buf, sizeof(buf), " ");
1232                                         } else {
1233                                                 snprintf (buf, sizeof(buf), "%" PRIu32, tick);
1234                                         }
1235
1236                                         (*marks)[n].label = g_strdup (buf);
1237
1238                                         /* Error compensation for float to nframes_t*/
1239                                         accumulated_error += frame_skip_error;
1240                                         if (accumulated_error > 1) {
1241                                                 pos += 1;
1242                                                 accumulated_error -= 1.0f;
1243                                         }
1244
1245                                         (*marks)[n].position = pos;
1246
1247                                         if ((bbt_beat_subdivision > 4) && i_am_accented) {
1248                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1249                                         } else {
1250                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1251                                         }
1252                                         i_am_accented = false;
1253                                         n++;
1254                                 
1255                                 }
1256                         }
1257                 }
1258                 return n; //return the actual number of marks made, since we might have skipped some fro fractional time signatures 
1259
1260        } else {
1261
1262                 /* we're in bar land */
1263
1264                 if (desirable_marks < (bars / 256)) {
1265                         nmarks = 1;
1266                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1267                         snprintf (buf, sizeof(buf), "too many bars... (currently %" PRIu32 ")", bars );
1268                         (*marks)[0].style = GtkCustomRulerMarkMajor;
1269                         (*marks)[0].label = g_strdup (buf);
1270                         (*marks)[0].position = lower;
1271                 } else if (desirable_marks < (uint32_t)(nmarks = (gint) (bars / 64) + 1)) {
1272                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1273                         for (n = 0,   i = current_bbt_points->begin(); i != current_bbt_points->end() && n < nmarks; i++) {
1274                                 if ((*i).type == TempoMap::Bar)  {
1275                                         if ((*i).bar % 64 == 1) {
1276                                                 if ((*i).bar % 256 == 1) {
1277                                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1278                                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1279                                                 } else {
1280                                                         snprintf (buf, sizeof(buf), " ");
1281                                                         if ((*i).bar % 256 == 129)  {
1282                                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1283                                                         } else {
1284                                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1285                                                         }
1286                                                 }
1287                                                 (*marks)[n].label = g_strdup (buf);
1288                                                 (*marks)[n].position = (*i).frame;
1289                                                 n++;
1290                                         }
1291                                 }
1292                         }
1293                 } else if (desirable_marks < (uint32_t)(nmarks = (bars / 16) + 1)) {
1294                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1295                         for (n = 0,  i = current_bbt_points->begin(); i != current_bbt_points->end() && n < nmarks; i++) {
1296                                 if ((*i).type == TempoMap::Bar)  {
1297                                         if ((*i).bar % 16 == 1) {
1298                                                 if ((*i).bar % 64 == 1) {
1299                                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1300                                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1301                                                 } else {
1302                                                         snprintf (buf, sizeof(buf), " ");
1303                                                         if ((*i).bar % 64 == 33)  {
1304                                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1305                                                         } else {
1306                                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1307                                                         }
1308                                                 }
1309                                                 (*marks)[n].label = g_strdup (buf);
1310                                                 (*marks)[n].position = (*i).frame;
1311                                                 n++;
1312                                         }
1313                                 }
1314                         }
1315                 } else if (desirable_marks < (uint32_t)(nmarks = (bars / 4) + 1)){
1316                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1317                         for (n = 0,   i = current_bbt_points->begin(); i != current_bbt_points->end() && n < nmarks; ++i) {
1318                                 if ((*i).type == TempoMap::Bar)  {
1319                                         if ((*i).bar % 4 == 1) {
1320                                                 if ((*i).bar % 16 == 1) {
1321                                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1322                                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1323                                                 } else {
1324                                                         snprintf (buf, sizeof(buf), " ");
1325                                                         if ((*i).bar % 16 == 9)  {
1326                                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1327                                                         } else {
1328                                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1329                                                         }
1330                                                 }
1331                                                 (*marks)[n].label = g_strdup (buf);
1332                                                 (*marks)[n].position = (*i).frame;
1333                                                 n++;
1334                                         }
1335                                 }
1336                         }
1337                 } else {
1338                         nmarks = bars + 1;
1339                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks );
1340                         for (n = 0,  i = current_bbt_points->begin(); i != current_bbt_points->end() && n < nmarks; i++) {
1341                                 if ((*i).type == TempoMap::Bar)  {
1342                                         if ((*i).bar % 4 == 1) {
1343                                                 snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1344                                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1345                                         } else {
1346                                                 snprintf (buf, sizeof(buf), " ");
1347                                                 if ((*i).bar % 4 == 3)  {
1348                                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1349                                                 } else {
1350                                                         (*marks)[n].style = GtkCustomRulerMarkMicro;
1351                                                 }
1352                                         }
1353                                         (*marks)[n].label = g_strdup (buf);
1354                                         (*marks)[n].position = (*i).frame;
1355                                         n++;
1356                                 }
1357                         }
1358                 }
1359                 return n;
1360         }
1361 }
1362
1363 gint
1364 Editor::metric_get_frames (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
1365 {
1366         nframes_t mark_interval;
1367         nframes_t pos;
1368         nframes_t ilower = (nframes_t) floor (lower);
1369         nframes_t iupper = (nframes_t) floor (upper);
1370         gchar buf[16];
1371         gint nmarks;
1372         gint n;
1373
1374         if (session == 0) {
1375                 return 0;
1376         }
1377
1378         mark_interval = (iupper - ilower) / 5;
1379         if (mark_interval > session->frame_rate()) {
1380                 mark_interval -= mark_interval % session->frame_rate();
1381         } else {
1382                 mark_interval = session->frame_rate() / (session->frame_rate() / mark_interval ) ;
1383         }
1384         nmarks = 5;
1385         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1386         for (n = 0, pos = ilower; n < nmarks; pos += mark_interval, ++n) {
1387                 snprintf (buf, sizeof(buf), "%u", pos);
1388                 (*marks)[n].label = g_strdup (buf);
1389                 (*marks)[n].position = pos;
1390                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1391         }
1392         
1393         return nmarks;
1394 }
1395
1396 static void
1397 sample_to_clock_parts ( nframes_t sample,
1398                         nframes_t sample_rate, 
1399                         long *hrs_p,
1400                         long *mins_p,
1401                         long *secs_p,
1402                         long *millisecs_p)
1403
1404 {
1405         nframes_t left;
1406         long hrs;
1407         long mins;
1408         long secs;
1409         long millisecs;
1410         
1411         left = sample;
1412         hrs = left / (sample_rate * 60 * 60);
1413         left -= hrs * sample_rate * 60 * 60;
1414         mins = left / (sample_rate * 60);
1415         left -= mins * sample_rate * 60;
1416         secs = left / sample_rate;
1417         left -= secs * sample_rate;
1418         millisecs = left * 1000 / sample_rate;
1419
1420         *millisecs_p = millisecs;
1421         *secs_p = secs;
1422         *mins_p = mins;
1423         *hrs_p = hrs;
1424
1425         return;
1426 }
1427
1428 gint
1429 Editor::metric_get_minsec (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
1430 {
1431         nframes_t range;
1432         nframes_t fr;
1433         nframes_t mark_interval;
1434         nframes_t pos;
1435         nframes_t spacer;
1436         long hrs, mins, secs, millisecs;
1437         gchar buf[16];
1438         gint nmarks;
1439         gint n;
1440         gint mark_modulo = 100;
1441         bool show_seconds = false;
1442         bool show_minutes = false;
1443         bool show_hours = false;
1444         nframes_t ilower = (nframes_t) floor (lower);
1445         nframes_t iupper = (nframes_t) floor (upper);
1446
1447         if (session == 0) {
1448                 return 0;
1449         }
1450
1451         fr = session->frame_rate();
1452
1453         /* to prevent 'flashing' */
1454         if (lower > (spacer = (nframes_t)(128 * Editor::get_current_zoom ()))) {
1455                 lower = lower - spacer;
1456         } else {
1457                 lower = 0;
1458         }
1459         upper = upper + spacer;
1460         range = iupper - ilower;
1461
1462         if (range <  (fr / 50)) {
1463                 mark_interval =  fr / 100; /* show 1/100 seconds */
1464                 mark_modulo = 10;
1465         } else if (range <= (fr / 10)) { /* 0-0.1 second */
1466                 mark_interval = fr / 50; /* show 1/50 seconds */
1467                 mark_modulo = 20;
1468         } else if (range <= (fr / 2)) { /* 0-0.5 second */
1469                 mark_interval = fr / 20;  /* show 1/20 seconds */
1470                 mark_modulo = 100;
1471         } else if (range <= fr) { /* 0-1 second */
1472                 mark_interval = fr / 10;  /* show 1/10 seconds */
1473                 mark_modulo = 200;
1474         } else if (range <= 2 * fr) { /* 1-2 seconds */
1475                 mark_interval = fr / 2; /* show 1/2 seconds */
1476                 mark_modulo = 500;
1477         } else if (range <= 8 * fr) { /* 2-5 seconds */
1478                 mark_interval =  fr / 5; /* show 2 seconds */
1479                 mark_modulo = 1000;
1480         } else if (range <= 16 * fr) { /* 8-16 seconds */
1481                 mark_interval =  fr; /* show 1 seconds */
1482                 show_seconds = true;
1483                 mark_modulo = 5;
1484         } else if (range <= 30 * fr) { /* 10-30 seconds */
1485                 mark_interval =  fr; /* show 10 seconds */
1486                 show_seconds = true;
1487                 mark_modulo = 5;
1488         } else if (range <= 60 * fr) { /* 30-60 seconds */
1489                 mark_interval = 5 * fr; /* show 5 seconds */
1490                 show_seconds = true;
1491                 mark_modulo = 3;
1492         } else if (range <= 2 * 60 * fr) { /* 1-2 minutes */
1493                 mark_interval = 5 * fr; /* show 5 seconds */
1494                 show_seconds = true;
1495                 mark_modulo = 3;
1496         } else if (range <= 4 * 60 * fr) { /* 4 minutes */
1497                 mark_interval = 10 * fr; /* show 10 seconds */
1498                 show_seconds = true;
1499                 mark_modulo = 30;
1500         } else if (range <= 10 * 60 * fr) { /* 10 minutes */
1501                 mark_interval = 30 * fr; /* show 30 seconds */
1502                 show_seconds = true;
1503                 mark_modulo = 60;
1504         } else if (range <= 30 * 60 * fr) { /* 10-30 minutes */
1505                 mark_interval =  60 * fr; /* show 1 minute */
1506                 show_minutes = true;
1507                 mark_modulo = 5;
1508         } else if (range <= 60 * 60 * fr) { /* 30 minutes - 1hr */
1509                 mark_interval = 2 * 60 * fr; /* show 2 minutes */
1510                 show_minutes = true;
1511                 mark_modulo = 10;
1512         } else if (range <= 4 * 60 * 60 * fr) { /* 1 - 4 hrs*/
1513                 mark_interval = 5 * 60 * fr; /* show 10 minutes */
1514                 show_minutes = true;
1515                 mark_modulo = 30;
1516         } else if (range <= 8 * 60 * 60 * fr) { /* 4 - 8 hrs*/
1517                 mark_interval = 20 * 60 * fr; /* show 20 minutes */
1518                 show_minutes = true;
1519                 mark_modulo = 60;
1520         } else if (range <= 16 * 60 * 60 * fr) { /* 16-24 hrs*/
1521                 mark_interval =  60 * 60 * fr; /* show 60 minutes */
1522                 show_hours = true;
1523                 mark_modulo = 2;
1524         } else {
1525                                                                                                                    
1526                 /* not possible if nframes_t is a 32 bit quantity */
1527                                                                                                                    
1528                 mark_interval = 4 * 60 * 60 * fr; /* show 4 hrs */
1529         }
1530
1531         nmarks = 1 + (range / mark_interval);
1532         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1533         pos = ((ilower + (mark_interval/2))/mark_interval) * mark_interval;
1534         
1535         if (show_seconds) {
1536                 for (n = 0; n < nmarks; pos += mark_interval, ++n) {
1537                         sample_to_clock_parts (pos, fr, &hrs, &mins, &secs, &millisecs);
1538                         if (secs % mark_modulo == 0) {
1539                                 if (secs == 0) {
1540                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1541                                 } else {
1542                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1543                                 }
1544                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1545                         } else {
1546                                 snprintf (buf, sizeof(buf), " ");
1547                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1548                         }
1549                         (*marks)[n].label = g_strdup (buf);
1550                         (*marks)[n].position = pos;
1551                 }
1552         } else if (show_minutes) {
1553                 for (n = 0; n < nmarks; pos += mark_interval, ++n) {
1554                         sample_to_clock_parts (pos, fr, &hrs, &mins, &secs, &millisecs);
1555                         if (mins % mark_modulo == 0) {
1556                                 if (mins == 0) {
1557                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1558                                 } else {
1559                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1560                                 }
1561                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1562                         } else {
1563                                 snprintf (buf, sizeof(buf), " ");
1564                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1565                         }
1566                         (*marks)[n].label = g_strdup (buf);
1567                         (*marks)[n].position = pos;
1568                 }
1569         } else if (show_hours) {
1570                  for (n = 0; n < nmarks; pos += mark_interval, ++n) {
1571                         sample_to_clock_parts (pos, fr, &hrs, &mins, &secs, &millisecs);
1572                         if (hrs % mark_modulo == 0) {
1573                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1574                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1575                         } else {
1576                                 snprintf (buf, sizeof(buf), " ");
1577                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1578                         }
1579                         (*marks)[n].label = g_strdup (buf);
1580                         (*marks)[n].position = pos;
1581                 }
1582         } else {
1583                 for (n = 0; n < nmarks; pos += mark_interval, ++n) {
1584                         sample_to_clock_parts (pos, fr, &hrs, &mins, &secs, &millisecs);
1585                         if (millisecs % mark_modulo == 0) {
1586                                 if (millisecs == 0) {
1587                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1588                                 } else {
1589                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1590                                 }
1591                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1592                         } else {
1593                                 snprintf (buf, sizeof(buf), " ");
1594                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1595                         }
1596                         (*marks)[n].label = g_strdup (buf);
1597                         (*marks)[n].position = pos;
1598                 }
1599         }
1600
1601         return nmarks;
1602 }