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