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