Make sure minsec ruler works when it is first enabled.
[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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <cstdio> // for sprintf, grrr
25 #include <cmath>
26 #include <inttypes.h>
27
28 #include <string>
29
30 #include <gtk/gtkaction.h>
31
32 #include "ardour/session.h"
33 #include "ardour/tempo.h"
34 #include "ardour/profile.h"
35 #include <gtkmm2ext/gtk_ui.h>
36
37 #include "editor.h"
38 #include "editing.h"
39 #include "actions.h"
40 #include "gtk-custom-hruler.h"
41 #include "gui_thread.h"
42 #include "time_axis_view.h"
43 #include "editor_drag.h"
44 #include "editor_cursors.h"
45
46 #include "i18n.h"
47
48 using namespace ARDOUR;
49 using namespace PBD;
50 using namespace Gtk;
51 using namespace Editing;
52
53 Editor *Editor::ruler_editor;
54
55 /* the order here must match the "metric" enums in editor.h */
56
57 GtkCustomMetric Editor::ruler_metrics[4] = {
58         {1, Editor::_metric_get_timecode },
59         {1, Editor::_metric_get_bbt },
60         {1, Editor::_metric_get_frames },
61         {1, Editor::_metric_get_minsec }
62 };
63
64 void
65 Editor::initialize_rulers ()
66 {
67         ruler_editor = this;
68         ruler_grabbed_widget = 0;
69
70         _ruler_separator = new Gtk::HSeparator();
71         _ruler_separator->set_size_request(-1, 2);
72         _ruler_separator->set_name("TimebarPadding");
73         _ruler_separator->show();
74
75         _minsec_ruler = gtk_custom_hruler_new ();
76         minsec_ruler = Glib::wrap (_minsec_ruler);
77         minsec_ruler->set_name ("MinSecRuler");
78         minsec_ruler->set_size_request (-1, (int)timebar_height);
79         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_minsec_ruler), &ruler_metrics[ruler_metric_minsec]);
80         minsec_ruler->hide ();
81         minsec_ruler->set_no_show_all();
82
83         _timecode_ruler = gtk_custom_hruler_new ();
84         timecode_ruler = Glib::wrap (_timecode_ruler);
85         timecode_ruler->set_name ("TimecodeRuler");
86         timecode_ruler->set_size_request (-1, (int)timebar_height);
87         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_timecode_ruler), &ruler_metrics[ruler_metric_timecode]);
88         timecode_ruler->hide ();
89         timecode_ruler->set_no_show_all();
90         timecode_nmarks = 0;
91
92         _bbt_ruler = gtk_custom_hruler_new ();
93         bbt_ruler = Glib::wrap (_bbt_ruler);
94         bbt_ruler->set_name ("BBTRuler");
95         bbt_ruler->set_size_request (-1, (int)timebar_height);
96         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_bbt_ruler), &ruler_metrics[ruler_metric_bbt]);
97         bbt_ruler->hide ();
98         bbt_ruler->set_no_show_all();
99         bbt_nmarks = 0;
100
101         _frames_ruler = gtk_custom_hruler_new ();
102         frames_ruler = Glib::wrap (_frames_ruler);
103         frames_ruler->set_name ("FramesRuler");
104         frames_ruler->set_size_request (-1, (int)timebar_height);
105         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_frames_ruler), &ruler_metrics[ruler_metric_frames]);
106         frames_ruler->hide ();
107         frames_ruler->set_no_show_all();
108
109         _bbt_ruler = gtk_custom_hruler_new ();
110         bbt_ruler = Glib::wrap (_bbt_ruler);
111         bbt_ruler->set_name ("BBTRuler");
112         bbt_ruler->set_size_request (-1, (int)timebar_height);
113         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_bbt_ruler), &ruler_metrics[ruler_metric_bbt]);
114         bbt_ruler->hide ();
115         bbt_ruler->set_no_show_all();
116         minsec_ruler->hide ();
117         minsec_ruler->set_no_show_all();
118         minsec_nmarks = 0;
119
120         using namespace Box_Helpers;
121         BoxList & ruler_lab_children =  ruler_label_vbox.children();
122         BoxList & ruler_children =  time_canvas_vbox.children();
123         BoxList & lab_children =  time_button_vbox.children();
124
125         BoxList::iterator canvaspos = ruler_children.begin();
126
127         lab_children.push_back (Element(meter_label, PACK_SHRINK, PACK_START));
128         lab_children.push_back (Element(tempo_label, PACK_SHRINK, PACK_START));
129         lab_children.push_back (Element(range_mark_label, PACK_SHRINK, PACK_START));
130         lab_children.push_back (Element(transport_mark_label, PACK_SHRINK, PACK_START));
131         lab_children.push_back (Element(cd_mark_label, PACK_SHRINK, PACK_START));
132         lab_children.push_back (Element(mark_label, PACK_SHRINK, PACK_START));
133
134         ruler_lab_children.push_back (Element(minsec_label, PACK_SHRINK, PACK_START));
135         ruler_children.insert (canvaspos, Element(*minsec_ruler, PACK_SHRINK, PACK_START));
136         ruler_lab_children.push_back (Element(timecode_label, PACK_SHRINK, PACK_START));
137         ruler_children.insert (canvaspos, Element(*timecode_ruler, PACK_SHRINK, PACK_START));
138         ruler_lab_children.push_back (Element(frame_label, PACK_SHRINK, PACK_START));
139         ruler_children.insert (canvaspos, Element(*frames_ruler, PACK_SHRINK, PACK_START));
140         ruler_lab_children.push_back (Element(bbt_label, PACK_SHRINK, PACK_START));
141         ruler_children.insert (canvaspos, Element(*bbt_ruler, PACK_SHRINK, PACK_START));
142
143         timecode_ruler->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
144         bbt_ruler->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
145         frames_ruler->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
146         minsec_ruler->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
147
148         timecode_ruler->signal_button_release_event().connect (sigc::mem_fun(*this, &Editor::ruler_button_release));
149         bbt_ruler->signal_button_release_event().connect (sigc::mem_fun(*this, &Editor::ruler_button_release));
150         frames_ruler->signal_button_release_event().connect (sigc::mem_fun(*this, &Editor::ruler_button_release));
151         minsec_ruler->signal_button_release_event().connect (sigc::mem_fun(*this, &Editor::ruler_button_release));
152
153         timecode_ruler->signal_button_press_event().connect (sigc::mem_fun(*this, &Editor::ruler_button_press));
154         bbt_ruler->signal_button_press_event().connect (sigc::mem_fun(*this, &Editor::ruler_button_press));
155         frames_ruler->signal_button_press_event().connect (sigc::mem_fun(*this, &Editor::ruler_button_press));
156         minsec_ruler->signal_button_press_event().connect (sigc::mem_fun(*this, &Editor::ruler_button_press));
157
158         timecode_ruler->signal_motion_notify_event().connect (sigc::mem_fun(*this, &Editor::ruler_mouse_motion));
159         bbt_ruler->signal_motion_notify_event().connect (sigc::mem_fun(*this, &Editor::ruler_mouse_motion));
160         frames_ruler->signal_motion_notify_event().connect (sigc::mem_fun(*this, &Editor::ruler_mouse_motion));
161         minsec_ruler->signal_motion_notify_event().connect (sigc::mem_fun(*this, &Editor::ruler_mouse_motion));
162
163         timecode_ruler->signal_scroll_event().connect (sigc::mem_fun(*this, &Editor::ruler_scroll));
164         bbt_ruler->signal_scroll_event().connect (sigc::mem_fun(*this, &Editor::ruler_scroll));
165         frames_ruler->signal_scroll_event().connect (sigc::mem_fun(*this, &Editor::ruler_scroll));
166         minsec_ruler->signal_scroll_event().connect (sigc::mem_fun(*this, &Editor::ruler_scroll));
167
168         visible_timebars = 0; /*this will be changed below */
169         canvas_timebars_vsize = 0;
170 }
171
172 bool
173 Editor::ruler_scroll (GdkEventScroll* event)
174 {
175         framepos_t xdelta;
176         int direction = event->direction;
177         bool handled = false;
178
179         switch (direction) {
180         case GDK_SCROLL_UP:
181                 temporal_zoom_step (true);
182                 handled = true;
183                 break;
184
185         case GDK_SCROLL_DOWN:
186                 temporal_zoom_step (false);
187                 handled = true;
188                 break;
189
190         case GDK_SCROLL_LEFT:
191                 xdelta = (current_page_frames() / 2);
192                 if (leftmost_frame > xdelta) {
193                         reset_x_origin (leftmost_frame - xdelta);
194                 } else {
195                         reset_x_origin (0);
196                 }
197                 handled = true;
198                 break;
199
200         case GDK_SCROLL_RIGHT:
201                 xdelta = (current_page_frames() / 2);
202                 if (max_framepos - xdelta > leftmost_frame) {
203                         reset_x_origin (leftmost_frame + xdelta);
204                 } else {
205                         reset_x_origin (max_framepos - current_page_frames());
206                 }
207                 handled = true;
208                 break;
209
210         default:
211                 /* what? */
212                 break;
213         }
214
215         return handled;
216 }
217
218
219 gint
220 Editor::ruler_button_press (GdkEventButton* ev)
221 {
222         if (_session == 0) {
223                 return FALSE;
224         }
225
226         // jlc: grab ev->window ?
227         //Gtk::Main::grab_add (*minsec_ruler);
228         Widget * grab_widget = 0;
229
230         if (timecode_ruler->is_realized() && ev->window == timecode_ruler->get_window()->gobj()) {
231                 grab_widget = timecode_ruler;
232         } else if (bbt_ruler->is_realized() && ev->window == bbt_ruler->get_window()->gobj()) {
233                 grab_widget = bbt_ruler;
234         } else if (frames_ruler->is_realized() && ev->window == frames_ruler->get_window()->gobj()) {
235                 grab_widget = frames_ruler;
236         } else if (minsec_ruler->is_realized() && ev->window == minsec_ruler->get_window()->gobj()) {
237                 grab_widget = minsec_ruler;
238         }
239
240         if (grab_widget) {
241                 grab_widget->add_modal_grab ();
242                 ruler_grabbed_widget = grab_widget;
243         }
244
245         if (ev->button == 1) {
246                 // Since we will locate the playhead on button release, cancel any running
247                 // auditions.
248                 if (_session->is_auditioning()) {
249                         _session->cancel_audition ();
250                 }
251
252                 /* playhead cursor */
253                 _drags->set (new CursorDrag (this, &playhead_cursor->canvas_item, false), reinterpret_cast<GdkEvent *> (ev));
254                 _dragging_playhead = true;
255         }
256
257         return TRUE;
258 }
259
260 gint
261 Editor::ruler_button_release (GdkEventButton* ev)
262 {
263         if (_session == 0) {
264                 return FALSE;
265         }
266
267         gint x,y;
268         Gdk::ModifierType state;
269
270         if (_drags->active ()) {
271                 _drags->end_grab (reinterpret_cast<GdkEvent*> (ev));
272                 _dragging_playhead = false;
273         }
274
275         if (ev->button == 3) {
276                 /* need to use the correct x,y, the event lies */
277                 time_canvas_event_box.get_window()->get_pointer (x, y, state);
278
279                 stop_canvas_autoscroll();
280
281                 framepos_t where = leftmost_frame + pixel_to_frame (x);
282                 snap_to (where);
283                 popup_ruler_menu (where);
284         }
285
286         if (ruler_grabbed_widget) {
287                 ruler_grabbed_widget->remove_modal_grab();
288                 ruler_grabbed_widget = 0;
289         }
290
291         return TRUE;
292 }
293
294 gint
295 Editor::ruler_label_button_release (GdkEventButton* ev)
296 {
297         if (ev->button == 3) {
298                 Gtk::Menu* m= dynamic_cast<Gtk::Menu*> (ActionManager::get_widget (X_("/RulerMenuPopup")));
299                 if (m) {
300                         m->popup (1, ev->time);
301                 }
302         }
303
304         return TRUE;
305 }
306
307
308 gint
309 Editor::ruler_mouse_motion (GdkEventMotion* ev)
310 {
311         if (_session == 0) {
312                 return FALSE;
313         }
314
315         if (_drags->active ()) {
316                 _drags->motion_handler (reinterpret_cast<GdkEvent*> (ev), false);
317         }
318
319         return TRUE;
320 }
321
322
323 void
324 Editor::popup_ruler_menu (framepos_t where, ItemType t)
325 {
326         using namespace Menu_Helpers;
327
328         if (editor_ruler_menu == 0) {
329                 editor_ruler_menu = new Menu;
330                 editor_ruler_menu->set_name ("ArdourContextMenu");
331         }
332
333         // always build from scratch
334         MenuList& ruler_items = editor_ruler_menu->items();
335         editor_ruler_menu->set_name ("ArdourContextMenu");
336         ruler_items.clear();
337
338         switch (t) {
339         case MarkerBarItem:
340                 ruler_items.push_back (MenuElem (_("New location marker"), sigc::bind ( sigc::mem_fun(*this, &Editor::mouse_add_new_marker), where, false, false)));
341                 ruler_items.push_back (MenuElem (_("Clear all locations"), sigc::mem_fun(*this, &Editor::clear_markers)));
342                 ruler_items.push_back (MenuElem (_("Unhide locations"), sigc::mem_fun(*this, &Editor::unhide_markers)));
343                 ruler_items.push_back (SeparatorElem ());
344                 break;
345         case RangeMarkerBarItem:
346                 //ruler_items.push_back (MenuElem (_("New Range")));
347                 ruler_items.push_back (MenuElem (_("Clear all ranges"), sigc::mem_fun(*this, &Editor::clear_ranges)));
348                 ruler_items.push_back (MenuElem (_("Unhide ranges"), sigc::mem_fun(*this, &Editor::unhide_ranges)));
349                 ruler_items.push_back (SeparatorElem ());
350
351                 break;
352         case TransportMarkerBarItem:
353
354                 break;
355
356         case CdMarkerBarItem:
357                 // TODO
358                 ruler_items.push_back (MenuElem (_("New CD track marker"), sigc::bind ( sigc::mem_fun(*this, &Editor::mouse_add_new_marker), where, true, false)));
359                 break;
360
361
362         case TempoBarItem:
363                 ruler_items.push_back (MenuElem (_("New Tempo"), sigc::bind ( sigc::mem_fun(*this, &Editor::mouse_add_new_tempo_event), where)));
364                 ruler_items.push_back (MenuElem (_("Clear tempo")));
365                 ruler_items.push_back (SeparatorElem ());
366                 break;
367
368         case MeterBarItem:
369                 ruler_items.push_back (MenuElem (_("New Meter"), sigc::bind ( sigc::mem_fun(*this, &Editor::mouse_add_new_meter_event), where)));
370                 ruler_items.push_back (MenuElem (_("Clear meter")));
371                 ruler_items.push_back (SeparatorElem ());
372                 break;
373
374         default:
375                 break;
376         }
377
378         Glib::RefPtr<Action> action;
379
380         action = ActionManager::get_action ("Rulers", "toggle-minsec-ruler");
381         if (action) {
382                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
383         }
384         if (!Profile->get_sae()) {
385                 action = ActionManager::get_action ("Rulers", "toggle-timecode-ruler");
386                 if (action) {
387                         ruler_items.push_back (MenuElem (*action->create_menu_item()));
388                 }
389         }
390         action = ActionManager::get_action ("Rulers", "toggle-samples-ruler");
391         if (action) {
392                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
393         }
394         action = ActionManager::get_action ("Rulers", "toggle-bbt-ruler");
395         if (action) {
396                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
397         }
398         action = ActionManager::get_action ("Rulers", "toggle-meter-ruler");
399         if (action) {
400                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
401         }
402         action = ActionManager::get_action ("Rulers", "toggle-tempo-ruler");
403         if (action) {
404                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
405         }
406         if (!Profile->get_sae()) {
407                 action = ActionManager::get_action ("Rulers", "toggle-range-ruler");
408                 if (action) {
409                         ruler_items.push_back (MenuElem (*action->create_menu_item()));
410                 }
411         }
412         action = ActionManager::get_action ("Rulers", "toggle-loop-punch-ruler");
413         if (action) {
414                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
415         }
416         action = ActionManager::get_action ("Rulers", "toggle-cd-marker-ruler");
417         if (action) {
418                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
419         }
420         action = ActionManager::get_action ("Rulers", "toggle-marker-ruler");
421         if (action) {
422                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
423         }
424
425         editor_ruler_menu->popup (1, gtk_get_current_event_time());
426
427         no_ruler_shown_update = false;
428 }
429
430 void
431 Editor::store_ruler_visibility ()
432 {
433         XMLNode* node = new XMLNode(X_("RulerVisibility"));
434
435         node->add_property (X_("timecode"), ruler_timecode_action->get_active() ? "yes": "no");
436         node->add_property (X_("bbt"), ruler_bbt_action->get_active() ? "yes": "no");
437         node->add_property (X_("frames"), ruler_samples_action->get_active() ? "yes": "no");
438         node->add_property (X_("minsec"), ruler_minsec_action->get_active() ? "yes": "no");
439         node->add_property (X_("tempo"), ruler_tempo_action->get_active() ? "yes": "no");
440         node->add_property (X_("meter"), ruler_meter_action->get_active() ? "yes": "no");
441         node->add_property (X_("marker"), ruler_marker_action->get_active() ? "yes": "no");
442         node->add_property (X_("rangemarker"), ruler_range_action->get_active() ? "yes": "no");
443         node->add_property (X_("transportmarker"), ruler_loop_punch_action->get_active() ? "yes": "no");
444         node->add_property (X_("cdmarker"), ruler_cd_marker_action->get_active() ? "yes": "no");
445
446         _session->add_extra_xml (*node);
447         _session->set_dirty ();
448 }
449
450 void
451 Editor::restore_ruler_visibility ()
452 {
453         XMLProperty* prop;
454         XMLNode * node = _session->extra_xml (X_("RulerVisibility"));
455
456         no_ruler_shown_update = true;
457
458         if (node) {
459                 if ((prop = node->property ("timecode")) != 0) {
460                         if (string_is_affirmative (prop->value())) {
461                                 ruler_timecode_action->set_active (true);
462                         } else {
463                                 ruler_timecode_action->set_active (false);
464                         }
465                 }
466                 if ((prop = node->property ("bbt")) != 0) {
467                         if (string_is_affirmative (prop->value())) {
468                                 ruler_bbt_action->set_active (true);
469                         } else {
470                                 ruler_bbt_action->set_active (false);
471                         }
472                 }
473                 if ((prop = node->property ("frames")) != 0) {
474                         if (string_is_affirmative (prop->value())) {
475                                 ruler_samples_action->set_active (true);
476                         } else {
477                                 ruler_samples_action->set_active (false);
478                         }
479                 }
480                 if ((prop = node->property ("minsec")) != 0) {
481                         if (string_is_affirmative (prop->value())) {
482                                 ruler_minsec_action->set_active (true);
483                         } else {
484                                 ruler_minsec_action->set_active (false);
485                         }
486                 }
487                 if ((prop = node->property ("tempo")) != 0) {
488                         if (string_is_affirmative (prop->value())) {
489                                 ruler_tempo_action->set_active (true);
490                         } else {
491                                 ruler_tempo_action->set_active (false);
492                         }
493                 }
494                 if ((prop = node->property ("meter")) != 0) {
495                         if (string_is_affirmative (prop->value())) {
496                                 ruler_meter_action->set_active (true);
497                         } else {
498                                 ruler_meter_action->set_active (false);
499                         }
500                 }
501                 if ((prop = node->property ("marker")) != 0) {
502                         if (string_is_affirmative (prop->value())) {
503                                 ruler_marker_action->set_active (true);
504                         } else {
505                                 ruler_marker_action->set_active (false);
506                         }
507                 }
508                 if ((prop = node->property ("rangemarker")) != 0) {
509                         if (string_is_affirmative (prop->value())) {
510                                 ruler_range_action->set_active (true);
511                         } else {
512                                 ruler_range_action->set_active (false);
513                         }
514                 }
515
516                 if ((prop = node->property ("transportmarker")) != 0) {
517                         if (string_is_affirmative (prop->value())) {
518                                 ruler_loop_punch_action->set_active (true);
519                         } else {
520                                 ruler_loop_punch_action->set_active (false);
521                         }
522                 }
523
524                 if ((prop = node->property ("cdmarker")) != 0) {
525                         if (string_is_affirmative (prop->value())) {
526                                 ruler_cd_marker_action->set_active (true);
527                         } else {
528                                 ruler_cd_marker_action->set_active (false);
529                         }
530
531                 } else {
532                         // this _session doesn't yet know about the cdmarker ruler
533                         // as a benefit to the user who doesn't know the feature exists, show the ruler if
534                         // any cd marks exist
535                         ruler_cd_marker_action->set_active (false);
536                         const Locations::LocationList & locs = _session->locations()->list();
537                         for (Locations::LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
538                                 if ((*i)->is_cd_marker()) {
539                                         ruler_cd_marker_action->set_active (true);
540                                         break;
541                                 }
542                         }
543                 }
544
545         }
546
547         no_ruler_shown_update = false;
548         update_ruler_visibility ();
549 }
550
551 void
552 Editor::update_ruler_visibility ()
553 {
554         int visible_rulers = 0;
555
556         if (no_ruler_shown_update) {
557                 return;
558         }
559
560         visible_timebars = 0;
561
562         if (ruler_minsec_action->get_active()) {
563                 visible_rulers++;
564                 minsec_label.show ();
565                 minsec_ruler->show ();
566         } else {
567                 minsec_label.hide ();
568                 minsec_ruler->hide ();
569         }
570
571         if (ruler_timecode_action->get_active()) {
572                 visible_rulers++;
573                 timecode_label.show ();
574                 timecode_ruler->show ();
575         } else {
576                 timecode_label.hide ();
577                 timecode_ruler->hide ();
578         }
579
580         if (ruler_samples_action->get_active()) {
581                 visible_rulers++;
582                 frame_label.show ();
583                 frames_ruler->show ();
584         } else {
585                 frame_label.hide ();
586                 frames_ruler->hide ();
587         }
588
589         if (ruler_bbt_action->get_active()) {
590                 visible_rulers++;
591                 bbt_label.show ();
592                 bbt_ruler->show ();
593         } else {
594                 bbt_label.hide ();
595                 bbt_ruler->hide ();
596         }
597
598         double tbpos = 0.0;
599         double tbgpos = 0.0;
600         double old_unit_pos;
601
602 #ifdef GTKOSX
603         /* gtk update probs require this (damn) */
604         meter_label.hide();
605         tempo_label.hide();
606         range_mark_label.hide();
607         transport_mark_label.hide();
608         cd_mark_label.hide();
609         mark_label.hide();
610 #endif
611         if (ruler_meter_action->get_active()) {
612                 old_unit_pos = meter_group->property_y();
613                 if (tbpos != old_unit_pos) {
614                         meter_group->move ( 0.0, tbpos - old_unit_pos);
615                 }
616                 old_unit_pos = meter_bar_group->property_y();
617                 if (tbgpos != old_unit_pos) {
618                         meter_bar_group->move ( 0.0, tbgpos - old_unit_pos);
619                 }
620                 meter_bar_group->show();
621                 meter_group->show();
622                 meter_label.show();
623                 tbpos += timebar_height;
624                 tbgpos += timebar_height;
625                 visible_timebars++;
626         } else {
627                 meter_bar_group->hide();
628                 meter_group->hide();
629                 meter_label.hide();
630         }
631
632         if (ruler_tempo_action->get_active()) {
633                 old_unit_pos = tempo_group->property_y();
634                 if (tbpos != old_unit_pos) {
635                         tempo_group->move(0.0, tbpos - old_unit_pos);
636                 }
637                 old_unit_pos = tempo_bar_group->property_y();
638                 if (tbgpos != old_unit_pos) {
639                         tempo_bar_group->move ( 0.0, tbgpos - old_unit_pos);
640                 }
641                 tempo_bar_group->show();
642                 tempo_group->show();
643                 tempo_label.show();
644                 tbpos += timebar_height;
645                 tbgpos += timebar_height;
646                 visible_timebars++;
647         } else {
648                 tempo_bar_group->hide();
649                 tempo_group->hide();
650                 tempo_label.hide();
651         }
652
653         if (!Profile->get_sae() && ruler_range_action->get_active()) {
654                 old_unit_pos = range_marker_group->property_y();
655                 if (tbpos != old_unit_pos) {
656                         range_marker_group->move (0.0, tbpos - old_unit_pos);
657                 }
658                 old_unit_pos = range_marker_bar_group->property_y();
659                 if (tbgpos != old_unit_pos) {
660                         range_marker_bar_group->move (0.0, tbgpos - old_unit_pos);
661                 }
662                 range_marker_bar_group->show();
663                 range_marker_group->show();
664                 range_mark_label.show();
665
666                 tbpos += timebar_height;
667                 tbgpos += timebar_height;
668                 visible_timebars++;
669         } else {
670                 range_marker_bar_group->hide();
671                 range_marker_group->hide();
672                 range_mark_label.hide();
673         }
674
675         if (ruler_loop_punch_action->get_active()) {
676                 old_unit_pos = transport_marker_group->property_y();
677                 if (tbpos != old_unit_pos) {
678                         transport_marker_group->move ( 0.0, tbpos - old_unit_pos);
679                 }
680                 old_unit_pos = transport_marker_bar_group->property_y();
681                 if (tbgpos != old_unit_pos) {
682                         transport_marker_bar_group->move ( 0.0, tbgpos - old_unit_pos);
683                 }
684                 transport_marker_bar_group->show();
685                 transport_marker_group->show();
686                 transport_mark_label.show();
687                 tbpos += timebar_height;
688                 tbgpos += timebar_height;
689                 visible_timebars++;
690         } else {
691                 transport_marker_bar_group->hide();
692                 transport_marker_group->hide();
693                 transport_mark_label.hide();
694         }
695
696         if (ruler_cd_marker_action->get_active()) {
697                 old_unit_pos = cd_marker_group->property_y();
698                 if (tbpos != old_unit_pos) {
699                         cd_marker_group->move (0.0, tbpos - old_unit_pos);
700                 }
701                 old_unit_pos = cd_marker_bar_group->property_y();
702                 if (tbgpos != old_unit_pos) {
703                         cd_marker_bar_group->move (0.0, tbgpos - old_unit_pos);
704                 }
705                 cd_marker_bar_group->show();
706                 cd_marker_group->show();
707                 cd_mark_label.show();
708                 tbpos += timebar_height;
709                 tbgpos += timebar_height;
710                 visible_timebars++;
711                 // make sure all cd markers show up in their respective places
712                 update_cd_marker_display();
713         } else {
714                 cd_marker_bar_group->hide();
715                 cd_marker_group->hide();
716                 cd_mark_label.hide();
717                 // make sure all cd markers show up in their respective places
718                 update_cd_marker_display();
719         }
720
721         if (ruler_marker_action->get_active()) {
722                 old_unit_pos = marker_group->property_y();
723                 if (tbpos != old_unit_pos) {
724                         marker_group->move ( 0.0, tbpos - old_unit_pos);
725                 }
726                 old_unit_pos = marker_bar_group->property_y();
727                 if (tbgpos != old_unit_pos) {
728                         marker_bar_group->move ( 0.0, tbgpos - old_unit_pos);
729                 }
730                 marker_bar_group->show();
731                 marker_group->show();
732                 mark_label.show();
733                 tbpos += timebar_height;
734                 tbgpos += timebar_height;
735                 visible_timebars++;
736         } else {
737                 marker_bar_group->hide();
738                 marker_group->hide();
739                 mark_label.hide();
740         }
741
742         gdouble old_canvas_timebars_vsize = canvas_timebars_vsize;
743         canvas_timebars_vsize = (timebar_height * visible_timebars) - 1;
744         gdouble vertical_pos_delta = canvas_timebars_vsize - old_canvas_timebars_vsize;
745         vertical_adjustment.set_upper(vertical_adjustment.get_upper() + vertical_pos_delta);
746         full_canvas_height += vertical_pos_delta;
747
748         if (vertical_adjustment.get_value() != 0 && (vertical_adjustment.get_value() + _canvas_height >= full_canvas_height)) {
749                 /*if we're at the bottom of the canvas, don't move the _trackview_group*/
750                 vertical_adjustment.set_value (full_canvas_height - _canvas_height + 1);
751         } else {
752                 _trackview_group->property_y () = - get_trackview_group_vertical_offset ();
753                 _background_group->property_y () = - get_trackview_group_vertical_offset ();
754                 _trackview_group->move (0, 0);
755                 _background_group->move (0, 0);
756                 last_trackview_group_vertical_offset = get_trackview_group_vertical_offset ();
757         }
758
759         gdouble bottom_track_pos = vertical_adjustment.get_value() + _canvas_height - canvas_timebars_vsize;
760         std::pair<TimeAxisView*, int> const p = trackview_by_y_position (bottom_track_pos);
761         if (p.first) {
762                 p.first->clip_to_viewport ();
763         }
764
765         ruler_label_vbox.set_size_request (-1, (int)(timebar_height * visible_rulers));
766         time_canvas_vbox.set_size_request (-1,-1);
767
768         compute_fixed_ruler_scale ();
769         update_fixed_rulers();
770         redisplay_tempo (false);
771
772         /* Changing ruler visibility means that any lines on markers might need updating */
773         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
774                 i->second->setup_lines ();
775         }
776 }
777
778 void
779 Editor::update_just_timecode ()
780 {
781         ENSURE_GUI_THREAD (*this, &Editor::update_just_timecode)
782
783         if (_session == 0) {
784                 return;
785         }
786
787         framepos_t rightmost_frame = leftmost_frame + current_page_frames();
788
789         if (ruler_timecode_action->get_active()) {
790                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_timecode_ruler), leftmost_frame, rightmost_frame,
791                                             leftmost_frame, _session->current_end_frame());
792         }
793 }
794
795 void
796 Editor::compute_fixed_ruler_scale ()
797 {
798         if (_session == 0) {
799                 return;
800         }
801
802         if (ruler_timecode_action->get_active()) {
803                 set_timecode_ruler_scale (leftmost_frame, leftmost_frame + current_page_frames() );
804         }
805
806         if (ruler_minsec_action->get_active()) {
807                 set_minsec_ruler_scale (leftmost_frame, leftmost_frame + current_page_frames() );
808         }
809 }
810
811 void
812 Editor::update_fixed_rulers ()
813 {
814         framepos_t rightmost_frame;
815
816         if (_session == 0) {
817                 return;
818         }
819
820         ruler_metrics[ruler_metric_timecode].units_per_pixel = frames_per_unit;
821         ruler_metrics[ruler_metric_frames].units_per_pixel = frames_per_unit;
822         ruler_metrics[ruler_metric_minsec].units_per_pixel = frames_per_unit;
823
824         rightmost_frame = leftmost_frame + current_page_frames();
825
826         /* these force a redraw, which in turn will force execution of the metric callbacks
827            to compute the relevant ticks to display.
828         */
829
830         if (ruler_timecode_action->get_active()) {
831                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_timecode_ruler), leftmost_frame, rightmost_frame,
832                                             leftmost_frame, _session->current_end_frame());
833         }
834
835         if (ruler_samples_action->get_active()) {
836                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_frames_ruler), leftmost_frame, rightmost_frame,
837                                             leftmost_frame, _session->current_end_frame());
838         }
839
840         if (ruler_minsec_action->get_active()) {
841                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_minsec_ruler), leftmost_frame, rightmost_frame,
842                                             leftmost_frame, _session->current_end_frame());
843         }
844 }
845
846 void
847 Editor::update_tempo_based_rulers ()
848 {
849         if (_session == 0) {
850                 return;
851         }
852
853         ruler_metrics[ruler_metric_bbt].units_per_pixel = frames_per_unit;
854
855         if (ruler_bbt_action->get_active()) {
856                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_bbt_ruler), leftmost_frame, leftmost_frame+current_page_frames(),
857                                             leftmost_frame, _session->current_end_frame());
858         }
859 }
860
861 /* Mark generation */
862
863 gint
864 Editor::_metric_get_timecode (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
865 {
866         return ruler_editor->metric_get_timecode (marks, lower, upper, maxchars);
867 }
868
869 gint
870 Editor::_metric_get_bbt (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
871 {
872         return ruler_editor->metric_get_bbt (marks, lower, upper, maxchars);
873 }
874
875 gint
876 Editor::_metric_get_frames (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
877 {
878         return ruler_editor->metric_get_frames (marks, lower, upper, maxchars);
879 }
880
881 gint
882 Editor::_metric_get_minsec (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
883 {
884         return ruler_editor->metric_get_minsec (marks, lower, upper, maxchars);
885 }
886
887 void
888 Editor::set_timecode_ruler_scale (gdouble lower, gdouble upper)
889 {
890         framepos_t range;
891         framepos_t spacer;
892         framepos_t fr;
893
894         if (_session == 0) {
895                 return;
896         }
897
898         fr = _session->frame_rate();
899
900         if (lower > (spacer = (framepos_t)(128 * Editor::get_current_zoom ()))) {
901                 lower = lower - spacer;
902         } else {
903                 lower = 0;
904         }
905         upper = upper + spacer;
906         range = (framepos_t) floor (upper - lower);
907
908         if (range < (2 * _session->frames_per_timecode_frame())) { /* 0 - 2 frames */
909                 timecode_ruler_scale = timecode_show_bits;
910                 timecode_mark_modulo = 20;
911                 timecode_nmarks = 2 + (2 * _session->config.get_subframes_per_frame());
912         } else if (range <= (fr / 4)) { /* 2 frames - 0.250 second */
913                 timecode_ruler_scale = timecode_show_frames;
914                 timecode_mark_modulo = 1;
915                 timecode_nmarks = 2 + (range / (framepos_t)_session->frames_per_timecode_frame());
916         } else if (range <= (fr / 2)) { /* 0.25-0.5 second */
917                 timecode_ruler_scale = timecode_show_frames;
918                 timecode_mark_modulo = 2;
919                 timecode_nmarks = 2 + (range / (framepos_t)_session->frames_per_timecode_frame());
920         } else if (range <= fr) { /* 0.5-1 second */
921                 timecode_ruler_scale = timecode_show_frames;
922                 timecode_mark_modulo = 5;
923                 timecode_nmarks = 2 + (range / (framepos_t)_session->frames_per_timecode_frame());
924         } else if (range <= 2 * fr) { /* 1-2 seconds */
925                 timecode_ruler_scale = timecode_show_frames;
926                 timecode_mark_modulo = 10;
927                 timecode_nmarks = 2 + (range / (framepos_t)_session->frames_per_timecode_frame());
928         } else if (range <= 8 * fr) { /* 2-8 seconds */
929                 timecode_ruler_scale = timecode_show_seconds;
930                 timecode_mark_modulo = 1;
931                 timecode_nmarks = 2 + (range / fr);
932         } else if (range <= 16 * fr) { /* 8-16 seconds */
933                 timecode_ruler_scale = timecode_show_seconds;
934                 timecode_mark_modulo = 2;
935                 timecode_nmarks = 2 + (range / fr);
936         } else if (range <= 30 * fr) { /* 16-30 seconds */
937                 timecode_ruler_scale = timecode_show_seconds;
938                 timecode_mark_modulo = 5;
939                 timecode_nmarks = 2 + (range / fr);
940         } else if (range <= 60 * fr) { /* 30-60 seconds */
941                 timecode_ruler_scale = timecode_show_seconds;
942                 timecode_mark_modulo = 5;
943                 timecode_nmarks = 2 + (range / fr);
944         } else if (range <= 2 * 60 * fr) { /* 1-2 minutes */
945                 timecode_ruler_scale = timecode_show_seconds;
946                 timecode_mark_modulo = 15;
947                 timecode_nmarks = 2 + (range / fr);
948         } else if (range <= 4 * 60 * fr) { /* 2-4 minutes */
949                 timecode_ruler_scale = timecode_show_seconds;
950                 timecode_mark_modulo = 30;
951                 timecode_nmarks = 2 + (range / fr);
952         } else if (range <= 10 * 60 * fr) { /* 4-10 minutes */
953                 timecode_ruler_scale = timecode_show_minutes;
954                 timecode_mark_modulo = 2;
955                 timecode_nmarks = 2 + 10;
956         } else if (range <= 30 * 60 * fr) { /* 10-30 minutes */
957                 timecode_ruler_scale = timecode_show_minutes;
958                 timecode_mark_modulo = 5;
959                 timecode_nmarks = 2 + 30;
960         } else if (range <= 60 * 60 * fr) { /* 30 minutes - 1hr */
961                 timecode_ruler_scale = timecode_show_minutes;
962                 timecode_mark_modulo = 10;
963                 timecode_nmarks = 2 + 60;
964         } else if (range <= 4 * 60 * 60 * fr) { /* 1 - 4 hrs*/
965                 timecode_ruler_scale = timecode_show_minutes;
966                 timecode_mark_modulo = 30;
967                 timecode_nmarks = 2 + (60 * 4);
968         } else if (range <= 8 * 60 * 60 * fr) { /* 4 - 8 hrs*/
969                 timecode_ruler_scale = timecode_show_hours;
970                 timecode_mark_modulo = 1;
971                 timecode_nmarks = 2 + 8;
972         } else if (range <= 16 * 60 * 60 * fr) { /* 16-24 hrs*/
973                 timecode_ruler_scale = timecode_show_hours;
974                 timecode_mark_modulo = 1;
975                 timecode_nmarks = 2 + 24;
976         } else {
977
978                 /* not possible if framepos_t is a 32 bit quantity */
979
980                 timecode_ruler_scale = timecode_show_hours;
981                 timecode_mark_modulo = 4;
982                 timecode_nmarks = 2 + 24;
983         }
984
985 }
986
987 gint
988 Editor::metric_get_timecode (GtkCustomRulerMark **marks, gdouble lower, gdouble /*upper*/, gint /*maxchars*/)
989 {
990         framepos_t pos;
991         framecnt_t spacer;
992         Timecode::Time timecode;
993         gchar buf[16];
994         gint n;
995
996         if (_session == 0) {
997                 return 0;
998         }
999
1000         if (lower > (spacer = (framecnt_t)(128 * Editor::get_current_zoom ()))) {
1001                 lower = lower - spacer;
1002         } else {
1003                 lower = 0;
1004         }
1005
1006         pos = (framecnt_t) floor (lower);
1007
1008         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * timecode_nmarks);
1009         switch (timecode_ruler_scale) {
1010         case timecode_show_bits:
1011
1012                 // Find timecode time of this sample (pos) with subframe accuracy
1013                 _session->sample_to_timecode(pos, timecode, true /* use_offset */, true /* use_subframes */ );
1014
1015                 for (n = 0; n < timecode_nmarks; n++) {
1016                         _session->timecode_to_sample(timecode, pos, true /* use_offset */, true /* use_subframes */ );
1017                         if ((timecode.subframes % timecode_mark_modulo) == 0) {
1018                                 if (timecode.subframes == 0) {
1019                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1020                                         snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", timecode.negative ? "-" : "", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
1021                                 } else {
1022                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1023                                         snprintf (buf, sizeof(buf), ".%02u", timecode.subframes);
1024                                 }
1025                         } else {
1026                                 snprintf (buf, sizeof(buf)," ");
1027                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1028
1029                         }
1030                         (*marks)[n].label = g_strdup (buf);
1031                         (*marks)[n].position = pos;
1032
1033                         // Increment subframes by one
1034                         Timecode::increment_subframes( timecode, _session->config.get_subframes_per_frame() );
1035                 }
1036           break;
1037         case timecode_show_seconds:
1038                 // Find timecode time of this sample (pos)
1039                 _session->sample_to_timecode(pos, timecode, true /* use_offset */, false /* use_subframes */ );
1040                 // Go to next whole second down
1041                 Timecode::seconds_floor( timecode );
1042
1043                 for (n = 0; n < timecode_nmarks; n++) {
1044                         _session->timecode_to_sample(timecode, pos, true /* use_offset */, false /* use_subframes */ );
1045                         if ((timecode.seconds % timecode_mark_modulo) == 0) {
1046                                 if (timecode.seconds == 0) {
1047                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1048                                         (*marks)[n].position = pos;
1049                                 } else {
1050                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1051                                         (*marks)[n].position = pos;
1052                                 }
1053                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", timecode.negative ? "-" : "", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
1054                         } else {
1055                                 snprintf (buf, sizeof(buf)," ");
1056                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1057                                 (*marks)[n].position = pos;
1058
1059                         }
1060                         (*marks)[n].label = g_strdup (buf);
1061                         Timecode::increment_seconds( timecode, _session->config.get_subframes_per_frame() );
1062                 }
1063           break;
1064         case timecode_show_minutes:
1065                 // Find timecode time of this sample (pos)
1066                 _session->sample_to_timecode(pos, timecode, true /* use_offset */, false /* use_subframes */ );
1067                 // Go to next whole minute down
1068                 Timecode::minutes_floor( timecode );
1069
1070                 for (n = 0; n < timecode_nmarks; n++) {
1071                         _session->timecode_to_sample(timecode, pos, true /* use_offset */, false /* use_subframes */ );
1072                         if ((timecode.minutes % timecode_mark_modulo) == 0) {
1073                                 if (timecode.minutes == 0) {
1074                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1075                                 } else {
1076                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1077                                 }
1078                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", timecode.negative ? "-" : "", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
1079                         } else {
1080                                 snprintf (buf, sizeof(buf)," ");
1081                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1082
1083                         }
1084                         (*marks)[n].label = g_strdup (buf);
1085                         (*marks)[n].position = pos;
1086                         Timecode::increment_minutes( timecode, _session->config.get_subframes_per_frame() );
1087                 }
1088
1089           break;
1090         case timecode_show_hours:
1091                 // Find timecode time of this sample (pos)
1092                 _session->sample_to_timecode(pos, timecode, true /* use_offset */, false /* use_subframes */ );
1093                 // Go to next whole hour down
1094                 Timecode::hours_floor( timecode );
1095
1096                 for (n = 0; n < timecode_nmarks; n++) {
1097                         _session->timecode_to_sample(timecode, pos, true /* use_offset */, false /* use_subframes */ );
1098                         if ((timecode.hours % timecode_mark_modulo) == 0) {
1099                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1100                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", timecode.negative ? "-" : "", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
1101                         } else {
1102                                 snprintf (buf, sizeof(buf)," ");
1103                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1104
1105                         }
1106                         (*marks)[n].label = g_strdup (buf);
1107                         (*marks)[n].position = pos;
1108
1109                         Timecode::increment_hours( timecode, _session->config.get_subframes_per_frame() );
1110                 }
1111           break;
1112         case timecode_show_frames:
1113                 // Find timecode time of this sample (pos)
1114                 _session->sample_to_timecode(pos, timecode, true /* use_offset */, false /* use_subframes */ );
1115                 // Go to next whole frame down
1116                 Timecode::frames_floor( timecode );
1117
1118                 for (n = 0; n < timecode_nmarks; n++) {
1119                         _session->timecode_to_sample(timecode, pos, true /* use_offset */, false /* use_subframes */ );
1120                         if ((timecode.frames % timecode_mark_modulo) == 0)  {
1121                                 if (timecode.frames == 0) {
1122                                   (*marks)[n].style = GtkCustomRulerMarkMajor;
1123                                 } else {
1124                                   (*marks)[n].style = GtkCustomRulerMarkMinor;
1125                                 }
1126                                 (*marks)[n].position = pos;
1127                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", timecode.negative ? "-" : "", timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
1128                         } else {
1129                                 snprintf (buf, sizeof(buf)," ");
1130                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1131                                 (*marks)[n].position = pos;
1132
1133                         }
1134                         (*marks)[n].label = g_strdup (buf);
1135                         Timecode::increment( timecode, _session->config.get_subframes_per_frame() );
1136                 }
1137
1138           break;
1139         }
1140
1141         return timecode_nmarks;
1142 }
1143
1144
1145 void
1146 Editor::compute_bbt_ruler_scale (framepos_t lower, framepos_t upper)
1147 {
1148         if (_session == 0) {
1149                 return;
1150         }
1151         TempoMap::BBTPointList::iterator i;
1152         Timecode::BBT_Time lower_beat, upper_beat; // the beats at each end of the ruler
1153
1154         _session->bbt_time (lower, lower_beat);
1155         _session->bbt_time (upper, upper_beat);
1156         uint32_t beats = 0;
1157
1158         bbt_accent_modulo = 1;
1159         bbt_bar_helper_on = false;
1160         bbt_bars = 0;
1161         bbt_nmarks = 1;
1162
1163         bbt_ruler_scale =  bbt_over;
1164
1165         switch (_snap_type) {
1166         case SnapToBeatDiv2:
1167                 bbt_beat_subdivision = 2;
1168                 break;
1169         case SnapToBeatDiv3:
1170                 bbt_beat_subdivision = 3;
1171                 break;
1172         case SnapToBeatDiv4:
1173                 bbt_beat_subdivision = 4;
1174                 break;
1175         case SnapToBeatDiv5:
1176                 bbt_beat_subdivision = 5;
1177                 bbt_accent_modulo = 2; // XXX YIKES
1178                 break;
1179         case SnapToBeatDiv6:
1180                 bbt_beat_subdivision = 6;
1181                 bbt_accent_modulo = 2; // XXX YIKES
1182                 break;
1183         case SnapToBeatDiv7:
1184                 bbt_beat_subdivision = 7;
1185                 bbt_accent_modulo = 2; // XXX YIKES
1186                 break;
1187         case SnapToBeatDiv8:
1188                 bbt_beat_subdivision = 8;
1189                 bbt_accent_modulo = 2;
1190                 break;
1191         case SnapToBeatDiv10:
1192                 bbt_beat_subdivision = 10;
1193                 bbt_accent_modulo = 2; // XXX YIKES
1194                 break;
1195         case SnapToBeatDiv12:
1196                 bbt_beat_subdivision = 12;
1197                 bbt_accent_modulo = 3;
1198                 break;
1199         case SnapToBeatDiv14:
1200                 bbt_beat_subdivision = 14;
1201                 bbt_accent_modulo = 3; // XXX YIKES!
1202                 break;
1203         case SnapToBeatDiv16:
1204                 bbt_beat_subdivision = 16;
1205                 bbt_accent_modulo = 4;
1206                 break;
1207         case SnapToBeatDiv20:
1208                 bbt_beat_subdivision = 20;
1209                 bbt_accent_modulo = 5;
1210                 break;
1211         case SnapToBeatDiv24:
1212                 bbt_beat_subdivision = 24;
1213                 bbt_accent_modulo = 6;
1214                 break;
1215         case SnapToBeatDiv28:
1216                 bbt_beat_subdivision = 28;
1217                 bbt_accent_modulo = 7;
1218                 break;
1219         case SnapToBeatDiv32:
1220                 bbt_beat_subdivision = 32;
1221                 bbt_accent_modulo = 8;
1222                 break;
1223         default:
1224                 bbt_beat_subdivision = 4;
1225                 break;
1226         }
1227
1228         if (current_bbt_points == 0 || current_bbt_points->empty()) {
1229                 return;
1230         }
1231
1232         i = current_bbt_points->end();
1233         i--;
1234         if ((*i).beat >= (*current_bbt_points->begin()).beat) {
1235           bbt_bars = (*i).bar - (*current_bbt_points->begin()).bar;
1236         } else {
1237           bbt_bars = (*i).bar - (*current_bbt_points->begin()).bar - 1;
1238         }
1239         beats = current_bbt_points->size() - bbt_bars;
1240
1241         /*Only show the bar helper if there aren't many bars on the screen */
1242         if ((bbt_bars < 2) || (beats < 5)) {
1243                 bbt_bar_helper_on = true;
1244         }
1245
1246         if (bbt_bars > 8192) {
1247           bbt_ruler_scale =  bbt_over;
1248         } else if (bbt_bars > 1024) {
1249           bbt_ruler_scale = bbt_show_64;
1250         } else if (bbt_bars > 256) {
1251           bbt_ruler_scale = bbt_show_16;
1252         } else if (bbt_bars > 64) {
1253           bbt_ruler_scale = bbt_show_4;
1254         } else if (bbt_bars > 10) {
1255           bbt_ruler_scale =  bbt_show_1;
1256         } else if (bbt_bars > 2) {
1257           bbt_ruler_scale =  bbt_show_beats;
1258         } else  if (bbt_bars > 0) {
1259           bbt_ruler_scale =  bbt_show_ticks;
1260         } else {
1261           bbt_ruler_scale =  bbt_show_ticks_detail;
1262         }
1263
1264         if ((bbt_ruler_scale == bbt_show_ticks_detail) && (lower_beat.beats == upper_beat.beats) && (upper_beat.ticks - lower_beat.ticks <= Timecode::BBT_Time::ticks_per_beat / 4)) {
1265                 bbt_ruler_scale =  bbt_show_ticks_super_detail;
1266         }
1267 }
1268
1269 gint
1270 Editor::metric_get_bbt (GtkCustomRulerMark **marks, gdouble lower, gdouble /*upper*/, gint /*maxchars*/)
1271 {
1272         if (_session == 0) {
1273                 return 0;
1274         }
1275
1276         TempoMap::BBTPointList::iterator i;
1277
1278         char buf[64];
1279         gint  n = 0;
1280         framepos_t pos;
1281         Timecode::BBT_Time next_beat;
1282         framepos_t next_beat_pos;
1283         uint32_t beats = 0;
1284
1285         uint32_t tick = 0;
1286         uint32_t skip;
1287         uint32_t t;
1288         framepos_t frame_skip;
1289         double frame_skip_error;
1290         double bbt_position_of_helper;
1291         double accumulated_error;
1292         bool i_am_accented = false;
1293         bool helper_active = false;
1294
1295         if (current_bbt_points == 0 || current_bbt_points->empty()) {
1296                 return 0;
1297         }
1298
1299         switch (bbt_ruler_scale) {
1300
1301         case bbt_show_beats:
1302                 beats = current_bbt_points->size();
1303                 bbt_nmarks = beats + 2;
1304
1305                 *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * bbt_nmarks);
1306
1307                 (*marks)[0].label = g_strdup(" ");
1308                 (*marks)[0].position = lower;
1309                 (*marks)[0].style = GtkCustomRulerMarkMicro;
1310
1311                 for (n = 1,   i = current_bbt_points->begin(); n < bbt_nmarks && i != current_bbt_points->end(); ++i) {
1312                         if  ((*i).type != TempoMap::Beat) {
1313                                 continue;
1314                         }
1315                         if ((*i).frame < lower && (bbt_bar_helper_on)) {
1316                                 snprintf (buf, sizeof(buf), "<%" PRIu32 "|%" PRIu32, (*i).bar, (*i).beat);
1317                                 (*marks)[0].label = g_strdup (buf);
1318                                 helper_active = true;
1319                         } else {
1320
1321                                 if ((*i).beat == 1) {
1322                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1323                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1324                                 } else if (((*i).beat % 2 == 1)) {
1325                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1326                                         snprintf (buf, sizeof(buf), " ");
1327                                 } else {
1328                                         (*marks)[n].style = GtkCustomRulerMarkMicro;
1329                                         snprintf (buf, sizeof(buf), " ");
1330                                 }
1331                                 (*marks)[n].label =  g_strdup (buf);
1332                                 (*marks)[n].position = (*i).frame;
1333                                 n++;
1334                         }
1335                 }
1336                 break;
1337
1338         case bbt_show_ticks:
1339
1340                 beats = current_bbt_points->size();
1341                 bbt_nmarks = (beats + 2) * bbt_beat_subdivision;
1342
1343                 bbt_position_of_helper = lower + (30 * Editor::get_current_zoom ());
1344                 *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * bbt_nmarks);
1345
1346                 (*marks)[0].label = g_strdup(" ");
1347                 (*marks)[0].position = lower;
1348                 (*marks)[0].style = GtkCustomRulerMarkMicro;
1349
1350                 for (n = 1,   i = current_bbt_points->begin(); n < bbt_nmarks && i != current_bbt_points->end(); ++i) {
1351                         if  ((*i).type != TempoMap::Beat) {
1352                                 continue;
1353                         }
1354                         if ((*i).frame < lower && (bbt_bar_helper_on)) {
1355                                 snprintf (buf, sizeof(buf), "<%" PRIu32 "|%" PRIu32, (*i).bar, (*i).beat);
1356                                 (*marks)[0].label = g_strdup (buf);
1357                                 helper_active = true;
1358                         } else {
1359
1360                                 if ((*i).beat == 1) {
1361                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1362                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1363                                 } else {
1364                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1365                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).beat);
1366                                 }
1367                                 if (((*i).frame < bbt_position_of_helper) && helper_active) {
1368                                         snprintf (buf, sizeof(buf), " ");
1369                                 }
1370                                 (*marks)[n].label =  g_strdup (buf);
1371                                 (*marks)[n].position = (*i).frame;
1372                                 n++;
1373                         }
1374
1375                         /* Add the tick marks */
1376
1377                         /* Find the next beat */
1378                         next_beat.beats = (*i).beat;
1379                         next_beat.bars = (*i).bar;
1380                         next_beat.ticks = 0;
1381
1382                         if ((*i).meter->beats_per_bar() > (next_beat.beats + 1)) {
1383                                   next_beat.beats += 1;
1384                         } else {
1385                                   next_beat.bars += 1;
1386                                   next_beat.beats = 1;
1387                         }
1388
1389                         next_beat_pos = _session->tempo_map().frame_time(next_beat);
1390
1391                         frame_skip = (framepos_t) floor (frame_skip_error = (_session->frame_rate() *  60) / (bbt_beat_subdivision * (*i).tempo->beats_per_minute()));
1392                         frame_skip_error -= frame_skip;
1393                         skip = (uint32_t) (Timecode::BBT_Time::ticks_per_beat / bbt_beat_subdivision);
1394
1395                         pos = (*i).frame + frame_skip;
1396                         accumulated_error = frame_skip_error;
1397
1398                         tick = skip;
1399
1400                         for (t = 0; (tick < Timecode::BBT_Time::ticks_per_beat) && (n < bbt_nmarks) && (pos < next_beat_pos) ; pos += frame_skip, tick += skip, ++t) {
1401
1402                                 if (t % bbt_accent_modulo == (bbt_accent_modulo - 1)) {
1403                                         i_am_accented = true;
1404                                 }
1405
1406                                 snprintf (buf, sizeof(buf), " ");
1407                                 (*marks)[n].label = g_strdup (buf);
1408
1409                                 /* Error compensation for float to framepos_t*/
1410                                 accumulated_error += frame_skip_error;
1411                                 if (accumulated_error > 1) {
1412                                         pos += 1;
1413                                         accumulated_error -= 1.0f;
1414                                 }
1415
1416                                 (*marks)[n].position = pos;
1417
1418                                 if ((bbt_beat_subdivision > 4) && i_am_accented) {
1419                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1420                                 } else {
1421                                         (*marks)[n].style = GtkCustomRulerMarkMicro;
1422                                 }
1423                                 i_am_accented = false;
1424                                 n++;
1425                         }
1426                 }
1427
1428           break;
1429
1430         case bbt_show_ticks_detail:
1431
1432                 beats = current_bbt_points->size();
1433                 bbt_nmarks = (beats + 2) * bbt_beat_subdivision;
1434
1435                 bbt_position_of_helper = lower + (30 * Editor::get_current_zoom ());
1436                 *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * bbt_nmarks);
1437
1438                 (*marks)[0].label = g_strdup(" ");
1439                 (*marks)[0].position = lower;
1440                 (*marks)[0].style = GtkCustomRulerMarkMicro;
1441
1442                 for (n = 1,   i = current_bbt_points->begin(); n < bbt_nmarks && i != current_bbt_points->end(); ++i) {
1443                         if  ((*i).type != TempoMap::Beat) {
1444                                 continue;
1445                         }
1446                         if ((*i).frame < lower && (bbt_bar_helper_on)) {
1447                                 snprintf (buf, sizeof(buf), "<%" PRIu32 "|%" PRIu32, (*i).bar, (*i).beat);
1448                                 (*marks)[0].label = g_strdup (buf);
1449                                 helper_active = true;
1450                         } else {
1451
1452                                 if ((*i).beat == 1) {
1453                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1454                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1455                                 } else {
1456                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1457                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).beat);
1458                                 }
1459                                 if (((*i).frame < bbt_position_of_helper) && helper_active) {
1460                                         snprintf (buf, sizeof(buf), " ");
1461                                 }
1462                                 (*marks)[n].label =  g_strdup (buf);
1463                                 (*marks)[n].position = (*i).frame;
1464                                 n++;
1465                         }
1466
1467                         /* Add the tick marks */
1468
1469                         /* Find the next beat */
1470
1471                         next_beat.beats = (*i).beat;
1472                         next_beat.bars = (*i).bar;
1473
1474                         if ((*i).meter->beats_per_bar() > (next_beat.beats + 1)) {
1475                                   next_beat.beats += 1;
1476                         } else {
1477                                   next_beat.bars += 1;
1478                                   next_beat.beats = 1;
1479                         }
1480
1481                         next_beat_pos = _session->tempo_map().frame_time(next_beat);
1482
1483                         frame_skip = (framepos_t) floor (frame_skip_error = (_session->frame_rate() *  60) / (bbt_beat_subdivision * (*i).tempo->beats_per_minute()));
1484                         frame_skip_error -= frame_skip;
1485                         skip = (uint32_t) (Timecode::BBT_Time::ticks_per_beat / bbt_beat_subdivision);
1486
1487                         pos = (*i).frame + frame_skip;
1488                         accumulated_error = frame_skip_error;
1489
1490                         tick = skip;
1491
1492                         for (t = 0; (tick < Timecode::BBT_Time::ticks_per_beat) && (n < bbt_nmarks) && (pos < next_beat_pos) ; pos += frame_skip, tick += skip, ++t) {
1493
1494                                 if (t % bbt_accent_modulo == (bbt_accent_modulo - 1)) {
1495                                         i_am_accented = true;
1496                                 }
1497
1498                                 if (i_am_accented && (pos > bbt_position_of_helper)){
1499                                         snprintf (buf, sizeof(buf), "%" PRIu32, tick);
1500                                 } else {
1501                                         snprintf (buf, sizeof(buf), " ");
1502                                 }
1503
1504                                 (*marks)[n].label = g_strdup (buf);
1505
1506                                 /* Error compensation for float to framepos_t*/
1507                                 accumulated_error += frame_skip_error;
1508                                 if (accumulated_error > 1) {
1509                                         pos += 1;
1510                                         accumulated_error -= 1.0f;
1511                                 }
1512
1513                                 (*marks)[n].position = pos;
1514
1515                                 if ((bbt_beat_subdivision > 4) && i_am_accented) {
1516                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1517                                 } else {
1518                                         (*marks)[n].style = GtkCustomRulerMarkMicro;
1519                                 }
1520                                 i_am_accented = false;
1521                                 n++;
1522                         }
1523                 }
1524
1525           break;
1526
1527         case bbt_show_ticks_super_detail:
1528
1529                 beats = current_bbt_points->size();
1530                 bbt_nmarks = (beats + 2) * bbt_beat_subdivision;
1531
1532                 bbt_position_of_helper = lower + (30 * Editor::get_current_zoom ());
1533                 *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * bbt_nmarks);
1534
1535                 (*marks)[0].label = g_strdup(" ");
1536                 (*marks)[0].position = lower;
1537                 (*marks)[0].style = GtkCustomRulerMarkMicro;
1538
1539                 for (n = 1,   i = current_bbt_points->begin(); n < bbt_nmarks && i != current_bbt_points->end(); ++i) {
1540                         if  ((*i).type != TempoMap::Beat) {
1541                                   continue;
1542                         }
1543                         if ((*i).frame < lower && (bbt_bar_helper_on)) {
1544                                   snprintf (buf, sizeof(buf), "<%" PRIu32 "|%" PRIu32, (*i).bar, (*i).beat);
1545                                   (*marks)[0].label = g_strdup (buf);
1546                                   helper_active = true;
1547                         } else {
1548
1549                                   if ((*i).beat == 1) {
1550                                           (*marks)[n].style = GtkCustomRulerMarkMajor;
1551                                           snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1552                                   } else {
1553                                           (*marks)[n].style = GtkCustomRulerMarkMinor;
1554                                           snprintf (buf, sizeof(buf), "%" PRIu32, (*i).beat);
1555                                   }
1556                                   if (((*i).frame < bbt_position_of_helper) && helper_active) {
1557                                           snprintf (buf, sizeof(buf), " ");
1558                                   }
1559                                   (*marks)[n].label =  g_strdup (buf);
1560                                   (*marks)[n].position = (*i).frame;
1561                                   n++;
1562                         }
1563
1564                         /* Add the tick marks */
1565
1566                         /* Find the next beat */
1567
1568                         next_beat.beats = (*i).beat;
1569                         next_beat.bars = (*i).bar;
1570
1571                         if ((*i).meter->beats_per_bar() > (next_beat.beats + 1)) {
1572                                   next_beat.beats += 1;
1573                         } else {
1574                                   next_beat.bars += 1;
1575                                   next_beat.beats = 1;
1576                         }
1577
1578                         next_beat_pos = _session->tempo_map().frame_time(next_beat);
1579
1580                         frame_skip = (framepos_t) floor (frame_skip_error = (_session->frame_rate() *  60) / (bbt_beat_subdivision * (*i).tempo->beats_per_minute()));
1581                         frame_skip_error -= frame_skip;
1582                         skip = (uint32_t) (Timecode::BBT_Time::ticks_per_beat / bbt_beat_subdivision);
1583
1584                         pos = (*i).frame + frame_skip;
1585                         accumulated_error = frame_skip_error;
1586
1587                         tick = skip;
1588
1589                         for (t = 0; (tick < Timecode::BBT_Time::ticks_per_beat) && (n < bbt_nmarks) && (pos < next_beat_pos) ; pos += frame_skip, tick += skip, ++t) {
1590
1591                                   if (t % bbt_accent_modulo == (bbt_accent_modulo - 1)) {
1592                                           i_am_accented = true;
1593                                   }
1594
1595                                   if (pos > bbt_position_of_helper) {
1596                                           snprintf (buf, sizeof(buf), "%" PRIu32, tick);
1597                                   } else {
1598                                           snprintf (buf, sizeof(buf), " ");
1599                                   }
1600
1601                                   (*marks)[n].label = g_strdup (buf);
1602
1603                                   /* Error compensation for float to framepos_t*/
1604                                   accumulated_error += frame_skip_error;
1605                                   if (accumulated_error > 1) {
1606                                           pos += 1;
1607                                           accumulated_error -= 1.0f;
1608                                   }
1609
1610                                   (*marks)[n].position = pos;
1611
1612                                   if ((bbt_beat_subdivision > 4) && i_am_accented) {
1613                                           (*marks)[n].style = GtkCustomRulerMarkMinor;
1614                                   } else {
1615                                           (*marks)[n].style = GtkCustomRulerMarkMicro;
1616                                   }
1617                                   i_am_accented = false;
1618                                   n++;
1619                         }
1620                 }
1621
1622           break;
1623
1624         case bbt_over:
1625                         bbt_nmarks = 1;
1626                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * bbt_nmarks);
1627                         snprintf (buf, sizeof(buf), "cannot handle %" PRIu32 " bars", bbt_bars );
1628                         (*marks)[0].style = GtkCustomRulerMarkMajor;
1629                         (*marks)[0].label = g_strdup (buf);
1630                         (*marks)[0].position = lower;
1631                         n = 1;
1632
1633           break;
1634
1635         case bbt_show_64:
1636                         bbt_nmarks = (gint) (bbt_bars / 64) + 1;
1637                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * bbt_nmarks);
1638                         for (n = 0,   i = current_bbt_points->begin(); i != current_bbt_points->end() && n < bbt_nmarks; i++) {
1639                                 if ((*i).type == TempoMap::Bar)  {
1640                                         if ((*i).bar % 64 == 1) {
1641                                                 if ((*i).bar % 256 == 1) {
1642                                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1643                                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1644                                                 } else {
1645                                                         snprintf (buf, sizeof(buf), " ");
1646                                                         if ((*i).bar % 256 == 129)  {
1647                                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1648                                                         } else {
1649                                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1650                                                         }
1651                                                 }
1652                                                 (*marks)[n].label = g_strdup (buf);
1653                                                 (*marks)[n].position = (*i).frame;
1654                                                 n++;
1655                                         }
1656                                 }
1657                         }
1658                         break;
1659
1660         case bbt_show_16:
1661                 bbt_nmarks = (bbt_bars / 16) + 1;
1662                 *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * bbt_nmarks);
1663                 for (n = 0,  i = current_bbt_points->begin(); i != current_bbt_points->end() && n < bbt_nmarks; i++) {
1664                         if ((*i).type == TempoMap::Bar)  {
1665                           if ((*i).bar % 16 == 1) {
1666                                 if ((*i).bar % 64 == 1) {
1667                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1668                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1669                                 } else {
1670                                         snprintf (buf, sizeof(buf), " ");
1671                                         if ((*i).bar % 64 == 33)  {
1672                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1673                                         } else {
1674                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1675                                         }
1676                                 }
1677                                 (*marks)[n].label = g_strdup (buf);
1678                                 (*marks)[n].position = (*i).frame;
1679                                 n++;
1680                           }
1681                         }
1682                 }
1683           break;
1684
1685         case bbt_show_4:
1686                 bbt_nmarks = (bbt_bars / 4) + 1;
1687                 *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * bbt_nmarks);
1688                 for (n = 0,   i = current_bbt_points->begin(); i != current_bbt_points->end() && n < bbt_nmarks; ++i) {
1689                         if ((*i).type == TempoMap::Bar)  {
1690                           if ((*i).bar % 4 == 1) {
1691                                 if ((*i).bar % 16 == 1) {
1692                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1693                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1694                                 } else {
1695                                         snprintf (buf, sizeof(buf), " ");
1696                                         if ((*i).bar % 16 == 9)  {
1697                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1698                                         } else {
1699                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1700                                         }
1701                                 }
1702                                 (*marks)[n].label = g_strdup (buf);
1703                                 (*marks)[n].position = (*i).frame;
1704                                 n++;
1705                           }
1706                         }
1707                 }
1708           break;
1709
1710         case bbt_show_1:
1711   //    default:
1712                 bbt_nmarks = bbt_bars + 2;
1713                 *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * bbt_nmarks );
1714                 for (n = 0,  i = current_bbt_points->begin(); i != current_bbt_points->end() && n < bbt_nmarks; i++) {
1715                         if ((*i).type == TempoMap::Bar)  {
1716                           if ((*i).bar % 4 == 1) {
1717                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1718                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1719                           } else {
1720                                 snprintf (buf, sizeof(buf), " ");
1721                                 if ((*i).bar % 4 == 3)  {
1722                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1723                                 } else {
1724                                         (*marks)[n].style = GtkCustomRulerMarkMicro;
1725                                 }
1726                           }
1727                         (*marks)[n].label = g_strdup (buf);
1728                         (*marks)[n].position = (*i).frame;
1729                         n++;
1730                         }
1731                 }
1732
1733         break;
1734
1735         }
1736
1737         return n; //return the actual number of marks made, since we might have skipped some from fractional time signatures
1738
1739 }
1740
1741 gint
1742 Editor::metric_get_frames (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint /*maxchars*/)
1743 {
1744         framepos_t mark_interval;
1745         framepos_t pos;
1746         framepos_t ilower = (framepos_t) floor (lower);
1747         framepos_t iupper = (framepos_t) floor (upper);
1748         gchar buf[16];
1749         gint nmarks;
1750         gint n;
1751
1752         if (_session == 0) {
1753                 return 0;
1754         }
1755
1756         mark_interval = (iupper - ilower) / 5;
1757         if (mark_interval > _session->frame_rate()) {
1758                 mark_interval -= mark_interval % _session->frame_rate();
1759         } else {
1760                 mark_interval = _session->frame_rate() / (_session->frame_rate() / mark_interval ) ;
1761         }
1762         nmarks = 5;
1763         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1764         for (n = 0, pos = ilower; n < nmarks; pos += mark_interval, ++n) {
1765                 snprintf (buf, sizeof(buf), "%" PRIi64, pos);
1766                 (*marks)[n].label = g_strdup (buf);
1767                 (*marks)[n].position = pos;
1768                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1769         }
1770
1771         return nmarks;
1772 }
1773
1774 static void
1775 sample_to_clock_parts ( framepos_t sample,
1776                         framepos_t sample_rate,
1777                         long *hrs_p,
1778                         long *mins_p,
1779                         long *secs_p,
1780                         long *millisecs_p)
1781
1782 {
1783         framepos_t left;
1784         long hrs;
1785         long mins;
1786         long secs;
1787         long millisecs;
1788
1789         left = sample;
1790         hrs = left / (sample_rate * 60 * 60);
1791         left -= hrs * sample_rate * 60 * 60;
1792         mins = left / (sample_rate * 60);
1793         left -= mins * sample_rate * 60;
1794         secs = left / sample_rate;
1795         left -= secs * sample_rate;
1796         millisecs = left * 1000 / sample_rate;
1797
1798         *millisecs_p = millisecs;
1799         *secs_p = secs;
1800         *mins_p = mins;
1801         *hrs_p = hrs;
1802
1803         return;
1804 }
1805
1806 void
1807 Editor::set_minsec_ruler_scale (gdouble lower, gdouble upper)
1808 {
1809         framepos_t range;
1810         framepos_t fr;
1811         framepos_t spacer;
1812
1813         if (_session == 0) {
1814                 return;
1815         }
1816
1817         fr = _session->frame_rate();
1818
1819         /* to prevent 'flashing' */
1820         if (lower > (spacer = (framepos_t)(128 * Editor::get_current_zoom ()))) {
1821                 lower -= spacer;
1822         } else {
1823                 lower = 0;
1824         }
1825         upper += spacer;
1826         range = (framepos_t) (upper - lower);
1827
1828         if (range <  (fr / 50)) {
1829                 minsec_mark_interval =  fr / 1000; /* show 1/1000 seconds */
1830                 minsec_ruler_scale = minsec_show_frames;
1831                 minsec_mark_modulo = 10;
1832         } else if (range <= (fr / 10)) { /* 0-0.1 second */
1833                 minsec_mark_interval = fr / 1000; /* show 1/1000 seconds */
1834                 minsec_ruler_scale = minsec_show_frames;
1835                 minsec_mark_modulo = 10;
1836         } else if (range <= (fr / 2)) { /* 0-0.5 second */
1837                 minsec_mark_interval = fr / 100;  /* show 1/100 seconds */
1838                 minsec_ruler_scale = minsec_show_frames;
1839                 minsec_mark_modulo = 100;
1840         } else if (range <= fr) { /* 0-1 second */
1841                 minsec_mark_interval = fr / 10;  /* show 1/10 seconds */
1842                 minsec_ruler_scale = minsec_show_frames;
1843                 minsec_mark_modulo = 200;
1844         } else if (range <= 2 * fr) { /* 1-2 seconds */
1845                 minsec_mark_interval = fr / 10; /* show 1/10 seconds */
1846                 minsec_ruler_scale = minsec_show_frames;
1847                 minsec_mark_modulo = 500;
1848         } else if (range <= 8 * fr) { /* 2-5 seconds */
1849                 minsec_mark_interval =  fr / 5; /* show 2 seconds */
1850                 minsec_ruler_scale = minsec_show_frames;
1851                 minsec_mark_modulo = 1000;
1852         } else if (range <= 16 * fr) { /* 8-16 seconds */
1853                 minsec_mark_interval =  fr; /* show 1 seconds */
1854                 minsec_ruler_scale = minsec_show_seconds;
1855                 minsec_mark_modulo = 2;
1856         } else if (range <= 30 * fr) { /* 10-30 seconds */
1857                 minsec_mark_interval =  fr; /* show 1 seconds */
1858                 minsec_ruler_scale = minsec_show_seconds;
1859                 minsec_mark_modulo = 5;
1860         } else if (range <= 60 * fr) { /* 30-60 seconds */
1861                 minsec_mark_interval = fr; /* show 1 seconds */
1862                 minsec_ruler_scale = minsec_show_seconds;
1863                 minsec_mark_modulo = 5;
1864         } else if (range <= 2 * 60 * fr) { /* 1-2 minutes */
1865                 minsec_mark_interval = 5 * fr; /* show 5 seconds */
1866                 minsec_ruler_scale = minsec_show_seconds;
1867                 minsec_mark_modulo = 3;
1868         } else if (range <= 4 * 60 * fr) { /* 4 minutes */
1869                 minsec_mark_interval = 5 * fr; /* show 10 seconds */
1870                 minsec_ruler_scale = minsec_show_seconds;
1871                 minsec_mark_modulo = 30;
1872         } else if (range <= 10 * 60 * fr) { /* 10 minutes */
1873                 minsec_mark_interval = 30 * fr; /* show 30 seconds */
1874                 minsec_ruler_scale = minsec_show_seconds;
1875                 minsec_mark_modulo = 120;
1876         } else if (range <= 30 * 60 * fr) { /* 10-30 minutes */
1877                 minsec_mark_interval =  60 * fr; /* show 1 minute */
1878                 minsec_ruler_scale = minsec_show_minutes;
1879                 minsec_mark_modulo = 5;
1880         } else if (range <= 60 * 60 * fr) { /* 30 minutes - 1hr */
1881                 minsec_mark_interval = 2 * 60 * fr; /* show 2 minutes */
1882                 minsec_ruler_scale = minsec_show_minutes;
1883                 minsec_mark_modulo = 10;
1884         } else if (range <= 4 * 60 * 60 * fr) { /* 1 - 4 hrs*/
1885                 minsec_mark_interval = 5 * 60 * fr; /* show 10 minutes */
1886                 minsec_ruler_scale = minsec_show_minutes;
1887                 minsec_mark_modulo = 30;
1888         } else if (range <= 8 * 60 * 60 * fr) { /* 4 - 8 hrs*/
1889                 minsec_mark_interval = 20 * 60 * fr; /* show 20 minutes */
1890                 minsec_ruler_scale = minsec_show_minutes;
1891                 minsec_mark_modulo = 60;
1892         } else if (range <= 16 * 60 * 60 * fr) { /* 16-24 hrs*/
1893                 minsec_mark_interval =  60 * 60 * fr; /* show 60 minutes */
1894                 minsec_ruler_scale = minsec_show_hours;
1895                 minsec_mark_modulo = 2;
1896         } else {
1897
1898                 /* not possible if framepos_t is a 32 bit quantity */
1899
1900                 minsec_mark_interval = 4 * 60 * 60 * fr; /* show 4 hrs */
1901         }
1902         minsec_nmarks = 2 + (range / minsec_mark_interval);
1903 }
1904
1905 gint
1906 Editor::metric_get_minsec (GtkCustomRulerMark **marks, gdouble lower, gdouble /*upper*/, gint /*maxchars*/)
1907 {
1908         framepos_t pos;
1909         framepos_t spacer;
1910         long hrs, mins, secs, millisecs;
1911         gchar buf[16];
1912         gint n;
1913
1914         if (_session == 0) {
1915                 return 0;
1916         }
1917
1918         /* to prevent 'flashing' */
1919         if (lower > (spacer = (framepos_t)(128 * Editor::get_current_zoom ()))) {
1920                 lower = lower - spacer;
1921         } else {
1922                 lower = 0;
1923         }
1924
1925         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * minsec_nmarks);
1926         pos = ((((framepos_t) floor(lower)) + (minsec_mark_interval/2))/minsec_mark_interval) * minsec_mark_interval;
1927         switch (minsec_ruler_scale) {
1928         case minsec_show_seconds:
1929                 for (n = 0; n < minsec_nmarks; pos += minsec_mark_interval, ++n) {
1930                         sample_to_clock_parts (pos, _session->frame_rate(), &hrs, &mins, &secs, &millisecs);
1931                         if (secs % minsec_mark_modulo == 0) {
1932                                 if (secs == 0) {
1933                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1934                                 } else {
1935                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1936                                 }
1937                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1938                         } else {
1939                                 snprintf (buf, sizeof(buf), " ");
1940                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1941                         }
1942                         (*marks)[n].label = g_strdup (buf);
1943                         (*marks)[n].position = pos;
1944                 }
1945           break;
1946         case minsec_show_minutes:
1947                 for (n = 0; n < minsec_nmarks; pos += minsec_mark_interval, ++n) {
1948                         sample_to_clock_parts (pos, _session->frame_rate(), &hrs, &mins, &secs, &millisecs);
1949                         if (mins % minsec_mark_modulo == 0) {
1950                                 if (mins == 0) {
1951                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1952                                 } else {
1953                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1954                                 }
1955                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1956                         } else {
1957                                 snprintf (buf, sizeof(buf), " ");
1958                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1959                         }
1960                         (*marks)[n].label = g_strdup (buf);
1961                         (*marks)[n].position = pos;
1962                 }
1963           break;
1964         case minsec_show_hours:
1965                  for (n = 0; n < minsec_nmarks; pos += minsec_mark_interval, ++n) {
1966                         sample_to_clock_parts (pos, _session->frame_rate(), &hrs, &mins, &secs, &millisecs);
1967                         if (hrs % minsec_mark_modulo == 0) {
1968                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1969                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1970                         } else {
1971                                 snprintf (buf, sizeof(buf), " ");
1972                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1973                         }
1974                         (*marks)[n].label = g_strdup (buf);
1975                         (*marks)[n].position = pos;
1976                 }
1977               break;
1978         case minsec_show_frames:
1979                 for (n = 0; n < minsec_nmarks; pos += minsec_mark_interval, ++n) {
1980                         sample_to_clock_parts (pos, _session->frame_rate(), &hrs, &mins, &secs, &millisecs);
1981                         if (millisecs % minsec_mark_modulo == 0) {
1982                                 if (secs == 0) {
1983                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1984                                 } else {
1985                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1986                                 }
1987                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1988                         } else {
1989                                 snprintf (buf, sizeof(buf), " ");
1990                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1991                         }
1992                         (*marks)[n].label = g_strdup (buf);
1993                         (*marks)[n].position = pos;
1994                 }
1995           break;
1996         }
1997
1998         return minsec_nmarks;
1999 }