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