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