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