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