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