396f88515c6cc63402d63d57fd6bb05a863afe3a
[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 #include <cstdio> // for sprintf, grrr 
21 #include <cmath>
22
23 #include <string>
24
25 #include <gtk/gtkaction.h>
26
27 #include <ardour/tempo.h>
28 #include <ardour/profile.h>
29 #include <gtkmm2ext/gtk_ui.h>
30
31 #include "editor.h"
32 #include "editing.h"
33 #include "actions.h"
34 #include "gtk-custom-hruler.h"
35 #include "gui_thread.h"
36
37 #include "i18n.h"
38
39 using namespace sigc;
40 using namespace ARDOUR;
41 using namespace PBD;
42 using namespace Gtk;
43 using namespace Editing;
44
45 Editor *Editor::ruler_editor;
46
47 /* the order here must match the "metric" enums in editor.h */
48
49 GtkCustomMetric Editor::ruler_metrics[4] = {
50         {1, Editor::_metric_get_smpte },
51         {1, Editor::_metric_get_bbt },
52         {1, Editor::_metric_get_frames },
53         {1, Editor::_metric_get_minsec }
54 };
55
56 void
57 Editor::initialize_rulers ()
58 {
59         ruler_editor = this;
60         ruler_grabbed_widget = 0;
61         
62         _ruler_separator = new Gtk::HSeparator();
63         _ruler_separator->set_size_request(-1, 2);
64         _ruler_separator->show();
65
66         _smpte_ruler = gtk_custom_hruler_new ();
67         smpte_ruler = Glib::wrap (_smpte_ruler);
68         smpte_ruler->set_name ("SMPTERuler");
69         smpte_ruler->set_size_request (-1, (int)timebar_height);
70         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_smpte_ruler), &ruler_metrics[ruler_metric_smpte]);
71         
72         _bbt_ruler = gtk_custom_hruler_new ();
73         bbt_ruler = Glib::wrap (_bbt_ruler);
74         bbt_ruler->set_name ("BBTRuler");
75         bbt_ruler->set_size_request (-1, (int)timebar_height);
76         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_bbt_ruler), &ruler_metrics[ruler_metric_bbt]);
77
78         _frames_ruler = gtk_custom_hruler_new ();
79         frames_ruler = Glib::wrap (_frames_ruler);
80         frames_ruler->set_name ("FramesRuler");
81         frames_ruler->set_size_request (-1, (int)timebar_height);
82         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_frames_ruler), &ruler_metrics[ruler_metric_frames]);
83
84         _minsec_ruler = gtk_custom_hruler_new ();
85         minsec_ruler = Glib::wrap (_minsec_ruler);
86         minsec_ruler->set_name ("MinSecRuler");
87         minsec_ruler->set_size_request (-1, (int)timebar_height);
88         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_minsec_ruler), &ruler_metrics[ruler_metric_minsec]);
89
90         visible_timebars = 7; /* 4 here, 3 in time_canvas */
91         ruler_pressed_button = 0;
92 }
93
94 bool
95 Editor::ruler_scroll (GdkEventScroll* event)
96 {
97         nframes64_t xdelta;
98         int direction = event->direction;
99         bool handled = false;
100
101         switch (direction) {
102         case GDK_SCROLL_UP:
103                 temporal_zoom_step (true);
104                 handled = true;
105                 break;
106
107         case GDK_SCROLL_DOWN:
108                 temporal_zoom_step (false);
109                 handled = true;
110                 break;
111
112         case GDK_SCROLL_LEFT:
113                 xdelta = (current_page_frames() / 2);
114                 if (leftmost_frame > xdelta) {
115                         reset_x_origin (leftmost_frame - xdelta);
116                 } else {
117                         reset_x_origin (0);
118                 }
119                 handled = true;
120                 break;
121
122         case GDK_SCROLL_RIGHT:
123                 xdelta = (current_page_frames() / 2);
124                 if (max_frames - xdelta > leftmost_frame) {
125                         reset_x_origin (leftmost_frame + xdelta);
126                 } else {
127                         reset_x_origin (max_frames - current_page_frames());
128                 }
129                 handled = true;
130                 break;
131
132         default:
133                 /* what? */
134                 break;
135         }
136
137         return handled;
138 }
139
140
141 gint
142 Editor::ruler_button_press (GdkEventButton* ev)
143 {
144         if (session == 0) {
145                 return FALSE;
146         }
147
148         ruler_pressed_button = ev->button;
149
150         // jlc: grab ev->window ?
151         //Gtk::Main::grab_add (*minsec_ruler);
152         Widget * grab_widget = 0;
153
154         if (smpte_ruler->is_realized() && ev->window == smpte_ruler->get_window()->gobj()) grab_widget = smpte_ruler;
155         else if (bbt_ruler->is_realized() && ev->window == bbt_ruler->get_window()->gobj()) grab_widget = bbt_ruler;
156         else if (frames_ruler->is_realized() && ev->window == frames_ruler->get_window()->gobj()) grab_widget = frames_ruler;
157         else if (minsec_ruler->is_realized() && ev->window == minsec_ruler->get_window()->gobj()) grab_widget = minsec_ruler;
158
159         if (grab_widget) {
160                 grab_widget->add_modal_grab ();
161                 ruler_grabbed_widget = grab_widget;
162         }
163
164         gint x,y;
165         Gdk::ModifierType state;
166
167         /* need to use the correct x,y, the event lies */
168         time_canvas_event_box.get_window()->get_pointer (x, y, state);
169
170         nframes64_t where = leftmost_frame + pixel_to_frame (x);
171
172         switch (ev->button) {
173         case 1:
174                 // Since we will locate the playhead on button release, cancel any running
175                 // auditions.
176                 if (session->is_auditioning()) {
177                         session->cancel_audition ();
178                 }
179                 /* playhead cursor */
180                 snap_to (where);
181                 playhead_cursor->set_position (where);
182                 _dragging_playhead = true;
183                 break;
184
185         case 2:
186                 /* edit point */
187                 snap_to (where);
188                 break;
189
190         default:
191                 break;
192         }
193
194         return TRUE;
195 }
196
197 gint
198 Editor::ruler_button_release (GdkEventButton* ev)
199 {
200         gint x,y;
201         Gdk::ModifierType state;
202
203         /* need to use the correct x,y, the event lies */
204         time_canvas_event_box.get_window()->get_pointer (x, y, state);
205
206         ruler_pressed_button = 0;
207         
208         if (session == 0) {
209                 return FALSE;
210         }
211
212         stop_canvas_autoscroll();
213         
214         nframes64_t where = leftmost_frame + pixel_to_frame (x);
215
216         switch (ev->button) {
217         case 1:
218                 /* transport playhead */
219                 _dragging_playhead = false;
220                 snap_to (where);
221                 session->request_locate (where, session->transport_rolling());
222                 break;
223
224         case 2:
225                 /* edit point */
226                 snap_to (where);
227                 break;
228
229         case 3:
230                 /* popup menu */
231                 snap_to (where);
232                 popup_ruler_menu (where);
233                 
234                 break;
235         default:
236                 break;
237         }
238
239
240         if (ruler_grabbed_widget) {
241                 ruler_grabbed_widget->remove_modal_grab();
242                 ruler_grabbed_widget = 0;
243         }
244
245         return TRUE;
246 }
247
248 gint
249 Editor::ruler_label_button_release (GdkEventButton* ev)
250 {
251         if (ev->button == 3) {
252                 Gtk::Menu* m= dynamic_cast<Gtk::Menu*> (ActionManager::get_widget (X_("/RulerMenuPopup")));
253                 if (m) {
254                         m->popup (1, ev->time);
255                 }
256         }
257         
258         return TRUE;
259 }
260
261
262 gint
263 Editor::ruler_mouse_motion (GdkEventMotion* ev)
264 {
265         if (session == 0 || !ruler_pressed_button) {
266                 return FALSE;
267         }
268         
269         double wcx=0,wcy=0;
270         double cx=0,cy=0;
271
272         gint x,y;
273         Gdk::ModifierType state;
274
275         /* need to use the correct x,y, the event lies */
276         time_canvas_event_box.get_window()->get_pointer (x, y, state);
277
278
279         track_canvas->c2w (x, y, wcx, wcy);
280         track_canvas->w2c (wcx, wcy, cx, cy);
281         
282         nframes64_t where = leftmost_frame + pixel_to_frame (x);
283
284         /// ripped from maybe_autoscroll, and adapted to work here
285         nframes64_t rightmost_frame = leftmost_frame + current_page_frames ();
286
287         if (autoscroll_timeout_tag < 0) {
288                 if (where > rightmost_frame) {
289                         if (rightmost_frame < max_frames) {
290                                 start_canvas_autoscroll (1, 0);
291                         }
292                 } else if (where <= leftmost_frame) {
293                         if (leftmost_frame > 0) {
294                                 start_canvas_autoscroll (-1, 0);
295                         }
296                 } 
297         } else {
298                 if (where >= leftmost_frame && where < rightmost_frame) {
299                         stop_canvas_autoscroll ();
300                 }
301         }
302         //////  
303         
304         snap_to (where);
305
306         Cursor* cursor = 0;
307         
308         switch (ruler_pressed_button) {
309         case 1:
310                 /* transport playhead */
311                 cursor = playhead_cursor;
312                 break;
313
314         case 2:
315                 /* edit point */
316                 // EDIT CURSOR XXX do something useful
317                 break;
318
319         default:
320                 break;
321         }
322
323         if (cursor) {
324                 cursor->set_position (where);
325                 
326                 if (cursor == playhead_cursor) {
327                         UpdateAllTransportClocks (cursor->current_frame);
328                 }
329         }
330         
331         return TRUE;
332 }
333
334
335 void
336 Editor::popup_ruler_menu (nframes64_t where, ItemType t)
337 {
338         using namespace Menu_Helpers;
339
340         if (editor_ruler_menu == 0) {
341                 editor_ruler_menu = new Menu;
342                 editor_ruler_menu->set_name ("ArdourContextMenu");
343         }
344
345         // always build from scratch
346         MenuList& ruler_items = editor_ruler_menu->items();
347         editor_ruler_menu->set_name ("ArdourContextMenu");
348         ruler_items.clear();
349
350         switch (t) {
351         case MarkerBarItem:
352                 ruler_items.push_back (MenuElem (_("New location marker"), bind ( mem_fun(*this, &Editor::mouse_add_new_marker), where, false, false)));
353                 ruler_items.push_back (MenuElem (_("Clear all locations"), mem_fun(*this, &Editor::clear_markers)));
354                 ruler_items.push_back (MenuElem (_("Unhide locations"), mem_fun(*this, &Editor::unhide_markers)));
355                 ruler_items.push_back (SeparatorElem ());
356                 break;
357         case RangeMarkerBarItem:
358                 //ruler_items.push_back (MenuElem (_("New Range")));
359                 ruler_items.push_back (MenuElem (_("Clear all ranges"), mem_fun(*this, &Editor::clear_ranges)));
360                 ruler_items.push_back (MenuElem (_("Unhide ranges"), mem_fun(*this, &Editor::unhide_ranges)));
361                 ruler_items.push_back (SeparatorElem ());
362
363                 break;
364         case TransportMarkerBarItem:
365
366                 break;
367         
368         case CdMarkerBarItem:
369                 // TODO
370                 ruler_items.push_back (MenuElem (_("New CD track marker"), bind ( mem_fun(*this, &Editor::mouse_add_new_marker), where, true, false)));
371                 break;
372                 
373         
374         case TempoBarItem:
375                 ruler_items.push_back (MenuElem (_("New Tempo"), bind ( mem_fun(*this, &Editor::mouse_add_new_tempo_event), where)));
376                 ruler_items.push_back (MenuElem (_("Clear tempo")));
377                 ruler_items.push_back (SeparatorElem ());
378                 break;
379
380         case MeterBarItem:
381                 ruler_items.push_back (MenuElem (_("New Meter"), bind ( mem_fun(*this, &Editor::mouse_add_new_meter_event), where)));
382                 ruler_items.push_back (MenuElem (_("Clear meter")));
383                 ruler_items.push_back (SeparatorElem ());
384                 break;
385
386         default:
387                 break;
388         }
389
390         Glib::RefPtr<Action> action;
391
392         action = ActionManager::get_action ("Rulers", "toggle-minsec-ruler");
393         if (action) {
394                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
395         }
396         if (!Profile->get_sae()) {
397                 action = ActionManager::get_action ("Rulers", "toggle-timecode-ruler");
398                 if (action) {
399                         ruler_items.push_back (MenuElem (*action->create_menu_item()));
400                 }
401         }
402         action = ActionManager::get_action ("Rulers", "toggle-samples-ruler");
403         if (action) {
404                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
405         }
406         action = ActionManager::get_action ("Rulers", "toggle-bbt-ruler");
407         if (action) {
408                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
409         }
410         action = ActionManager::get_action ("Rulers", "toggle-meter-ruler");
411         if (action) {
412                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
413         }
414         action = ActionManager::get_action ("Rulers", "toggle-tempo-ruler");
415         if (action) {
416                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
417         }
418         if (!Profile->get_sae()) {
419                 action = ActionManager::get_action ("Rulers", "toggle-range-ruler");
420                 if (action) {
421                         ruler_items.push_back (MenuElem (*action->create_menu_item()));
422                 }
423         }
424         action = ActionManager::get_action ("Rulers", "toggle-loop-punch-ruler");
425         if (action) {
426                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
427         }
428         action = ActionManager::get_action ("Rulers", "toggle-cd-marker-ruler");
429         if (action) {
430                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
431         }
432         action = ActionManager::get_action ("Rulers", "toggle-marker-ruler");
433         if (action) {
434                 ruler_items.push_back (MenuElem (*action->create_menu_item()));
435         }
436
437         editor_ruler_menu->popup (1, gtk_get_current_event_time());
438
439         no_ruler_shown_update = false;
440 }
441
442 void
443 Editor::store_ruler_visibility ()
444 {
445         XMLNode* node = new XMLNode(X_("RulerVisibility"));
446
447         node->add_property (X_("smpte"), ruler_timecode_action->get_active() ? "yes": "no");
448         node->add_property (X_("bbt"), ruler_bbt_action->get_active() ? "yes": "no");
449         node->add_property (X_("frames"), ruler_samples_action->get_active() ? "yes": "no");
450         node->add_property (X_("minsec"), ruler_minsec_action->get_active() ? "yes": "no");
451         node->add_property (X_("tempo"), ruler_tempo_action->get_active() ? "yes": "no");
452         node->add_property (X_("meter"), ruler_meter_action->get_active() ? "yes": "no");
453         node->add_property (X_("marker"), ruler_marker_action->get_active() ? "yes": "no");
454         node->add_property (X_("rangemarker"), ruler_range_action->get_active() ? "yes": "no");
455         node->add_property (X_("transportmarker"), ruler_loop_punch_action->get_active() ? "yes": "no");
456         node->add_property (X_("cdmarker"), ruler_cd_marker_action->get_active() ? "yes": "no");
457
458         session->add_extra_xml (*node);
459         session->set_dirty ();
460 }
461
462 void 
463 Editor::restore_ruler_visibility ()
464 {
465         XMLProperty* prop;
466         XMLNode * node = session->extra_xml (X_("RulerVisibility"));
467
468         no_ruler_shown_update = true;
469
470         if (node) {
471                 if ((prop = node->property ("smpte")) != 0) {
472                         if (prop->value() == "yes") 
473                                 ruler_timecode_action->set_active (true);
474                         else 
475                                 ruler_timecode_action->set_active (false);
476                 }
477                 if ((prop = node->property ("bbt")) != 0) {
478                         if (prop->value() == "yes") 
479                                 ruler_bbt_action->set_active (true);
480                         else 
481                                 ruler_bbt_action->set_active (false);
482                 }
483                 if ((prop = node->property ("frames")) != 0) {
484                         if (prop->value() == "yes") 
485                                 ruler_samples_action->set_active (true);
486                         else 
487                                 ruler_samples_action->set_active (false);
488                 }
489                 if ((prop = node->property ("minsec")) != 0) {
490                         if (prop->value() == "yes") 
491                                 ruler_minsec_action->set_active (true);
492                         else 
493                                 ruler_minsec_action->set_active (false);
494                 }
495                 if ((prop = node->property ("tempo")) != 0) {
496                         if (prop->value() == "yes") 
497                                 ruler_tempo_action->set_active (true);
498                         else 
499                                 ruler_tempo_action->set_active (false);
500                 }
501                 if ((prop = node->property ("meter")) != 0) {
502                         if (prop->value() == "yes") 
503                                 ruler_meter_action->set_active (true);
504                         else 
505                                 ruler_meter_action->set_active (false);
506                 }
507                 if ((prop = node->property ("marker")) != 0) {
508                         if (prop->value() == "yes") 
509                                 ruler_marker_action->set_active (true);
510                         else 
511                                 ruler_marker_action->set_active (false);
512                 }
513                 if ((prop = node->property ("rangemarker")) != 0) {
514                         if (prop->value() == "yes") 
515                                 ruler_range_action->set_active (true);
516                         else 
517                                 ruler_range_action->set_active (false);
518                 }
519
520                 if ((prop = node->property ("transportmarker")) != 0) {
521                         if (prop->value() == "yes") 
522                                 ruler_loop_punch_action->set_active (true);
523                         else 
524                                 ruler_loop_punch_action->set_active (false);
525                 }
526
527                 if ((prop = node->property ("cdmarker")) != 0) {
528                         if (prop->value() == "yes") 
529                                 ruler_cd_marker_action->set_active (true);
530                         else 
531                                 ruler_cd_marker_action->set_active (false);
532
533                 } else {
534                         // this session doesn't yet know about the cdmarker ruler
535                         // as a benefit to the user who doesn't know the feature exists, show the ruler if 
536                         // any cd marks exist
537                         ruler_cd_marker_action->set_active (false);
538                         const Locations::LocationList & locs = session->locations()->list();
539                         for (Locations::LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
540                                 if ((*i)->is_cd_marker()) {
541                                         ruler_cd_marker_action->set_active (true);
542                                         break;
543                                 }
544                         }
545                 }
546
547         }
548
549         no_ruler_shown_update = false;
550
551         update_ruler_visibility ();
552 }
553
554 void
555 Editor::update_ruler_visibility ()
556 {
557         using namespace Box_Helpers;
558         BoxList & lab_children =  time_button_vbox.children();
559         BoxList & ruler_children =  time_canvas_vbox.children();
560
561         if (no_ruler_shown_update) {
562                 return;
563         }
564
565         visible_timebars = 0;
566
567         lab_children.clear();
568
569         // leave the last one (the time_canvas) intact
570         while (ruler_children.size() > 1) {
571                 ruler_children.pop_front();
572         }
573
574         BoxList::iterator canvaspos = ruler_children.begin();
575         
576         _smpte_ruler = gtk_custom_hruler_new ();
577         smpte_ruler = Glib::wrap (_smpte_ruler);
578         smpte_ruler->set_name ("SMPTERuler");
579         smpte_ruler->set_size_request (-1, (int)timebar_height);
580         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_smpte_ruler), &ruler_metrics[ruler_metric_smpte]);
581         
582         _bbt_ruler = gtk_custom_hruler_new ();
583         bbt_ruler = Glib::wrap (_bbt_ruler);
584         bbt_ruler->set_name ("BBTRuler");
585         bbt_ruler->set_size_request (-1, (int)timebar_height);
586         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_bbt_ruler), &ruler_metrics[ruler_metric_bbt]);
587
588         _frames_ruler = gtk_custom_hruler_new ();
589         frames_ruler = Glib::wrap (_frames_ruler);
590         frames_ruler->set_name ("FramesRuler");
591         frames_ruler->set_size_request (-1, (int)timebar_height);
592         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_frames_ruler), &ruler_metrics[ruler_metric_frames]);
593
594         _minsec_ruler = gtk_custom_hruler_new ();
595         minsec_ruler = Glib::wrap (_minsec_ruler);
596         minsec_ruler->set_name ("MinSecRuler");
597         minsec_ruler->set_size_request (-1, (int)timebar_height);
598         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_minsec_ruler), &ruler_metrics[ruler_metric_minsec]);
599
600         smpte_ruler->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
601         bbt_ruler->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
602         frames_ruler->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
603         minsec_ruler->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
604
605         smpte_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
606         bbt_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
607         frames_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
608         minsec_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
609
610         smpte_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
611         bbt_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
612         frames_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
613         minsec_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
614         
615         smpte_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
616         bbt_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
617         frames_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
618         minsec_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
619
620         smpte_ruler->signal_scroll_event().connect (mem_fun(*this, &Editor::ruler_scroll));
621         bbt_ruler->signal_scroll_event().connect (mem_fun(*this, &Editor::ruler_scroll));
622         frames_ruler->signal_scroll_event().connect (mem_fun(*this, &Editor::ruler_scroll));
623         minsec_ruler->signal_scroll_event().connect (mem_fun(*this, &Editor::ruler_scroll));
624
625         ruler_children.insert (canvaspos, Element(*_ruler_separator, PACK_SHRINK, PACK_START));
626         
627         if (ruler_minsec_action->get_active()) {
628                 lab_children.push_back (Element(minsec_label, PACK_SHRINK, PACK_START));
629                 ruler_children.insert (canvaspos, Element(*minsec_ruler, PACK_SHRINK, PACK_START));
630                 visible_timebars++;
631         }
632
633         if (ruler_timecode_action->get_active()) {
634                 lab_children.push_back (Element(smpte_label, PACK_SHRINK, PACK_START));
635                 ruler_children.insert (canvaspos, Element(*smpte_ruler, PACK_SHRINK, PACK_START));
636                 visible_timebars++;
637         }
638
639         if (ruler_samples_action->get_active()) {
640                 lab_children.push_back (Element(frame_label, PACK_SHRINK, PACK_START));
641                 ruler_children.insert (canvaspos, Element(*frames_ruler, PACK_SHRINK, PACK_START));
642                 visible_timebars++;
643         }
644
645         if (ruler_bbt_action->get_active()) {
646                 lab_children.push_back (Element(bbt_label, PACK_SHRINK, PACK_START));
647                 ruler_children.insert (canvaspos, Element(*bbt_ruler, PACK_SHRINK, PACK_START));
648                 visible_timebars++;
649         }
650
651         double tbpos = 1.0;
652         double old_unit_pos ;
653         
654         if (ruler_meter_action->get_active()) {
655                 lab_children.push_back (Element(meter_label, PACK_SHRINK, PACK_START));
656
657                 old_unit_pos = meter_group->property_y();
658                 if (tbpos != old_unit_pos) {
659                         meter_group->move ( 0.0, tbpos - old_unit_pos);
660                 }
661                 meter_group->show();
662                 tbpos += timebar_height;
663                 visible_timebars++;
664         }
665         else {
666                 meter_group->hide();
667         }
668         
669         if (ruler_tempo_action->get_active()) {
670                 lab_children.push_back (Element(tempo_label, PACK_SHRINK, PACK_START));
671                 old_unit_pos = tempo_group->property_y();
672                 if (tbpos != old_unit_pos) {
673                         tempo_group->move(0.0, tbpos - old_unit_pos);
674                 }
675                 tempo_group->show();
676                 tbpos += timebar_height;
677                 visible_timebars++;
678         }
679         else {
680                 tempo_group->hide();
681         }
682         
683         if (!Profile->get_sae() && ruler_range_action->get_active()) {
684                 lab_children.push_back (Element(range_mark_label, PACK_SHRINK, PACK_START));
685                 old_unit_pos = range_marker_group->property_y();
686                 if (tbpos != old_unit_pos) {
687                         range_marker_group->move (0.0, tbpos - old_unit_pos);
688                 }
689                 range_marker_group->show();
690                 tbpos += timebar_height;
691                 visible_timebars++;
692         } else {
693                 range_marker_group->hide();
694         }
695
696         if (ruler_loop_punch_action->get_active()) {
697                 lab_children.push_back (Element(transport_mark_label, PACK_SHRINK, PACK_START));
698                 old_unit_pos = transport_marker_group->property_y();
699                 if (tbpos != old_unit_pos) {
700                         transport_marker_group->move ( 0.0, tbpos - old_unit_pos);
701                 }
702                 transport_marker_group->show();
703                 tbpos += timebar_height;
704                 visible_timebars++;
705         }
706         else {
707                 transport_marker_group->hide();
708         }
709
710         if (ruler_cd_marker_action->get_active()) {
711                 lab_children.push_back (Element(cd_mark_label, PACK_SHRINK, PACK_START));
712                 old_unit_pos = cd_marker_group->property_y();
713                 if (tbpos != old_unit_pos) {
714                         cd_marker_group->move (0.0, tbpos - old_unit_pos);
715                 }
716                 cd_marker_group->show();
717                 tbpos += timebar_height;
718                 visible_timebars++;
719                 // make sure all cd markers show up in their respective places
720                 update_cd_marker_display();
721         }
722         else {
723                 cd_marker_group->hide();
724                 // make sure all cd markers show up in their respective places
725                 update_cd_marker_display();
726         }
727         
728         if (ruler_marker_action->get_active()) {
729                 lab_children.push_back (Element(mark_label, PACK_SHRINK, PACK_START));
730                 old_unit_pos = marker_group->property_y();
731                 if (tbpos != old_unit_pos) {
732                         marker_group->move ( 0.0, tbpos - old_unit_pos);
733                 }
734                 marker_group->show();
735                 tbpos += timebar_height;
736                 visible_timebars++;
737         }
738         else {
739                 marker_group->hide();
740         }
741         
742         time_canvas_vbox.set_size_request (-1, (int)(timebar_height * visible_timebars));
743         time_canvas_event_box.queue_resize();
744         
745         update_fixed_rulers();
746         //update_tempo_based_rulers();
747         redisplay_tempo (false);
748
749         time_canvas_event_box.show_all();
750         time_button_frame.show_all();
751 }
752
753 void
754 Editor::update_just_smpte ()
755 {
756         ENSURE_GUI_THREAD(mem_fun(*this, &Editor::update_just_smpte));
757         
758         if (session == 0) {
759                 return;
760         }
761
762         nframes64_t rightmost_frame = leftmost_frame + current_page_frames();
763
764         if (ruler_timecode_action->get_active()) {
765                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_smpte_ruler), leftmost_frame, rightmost_frame,
766                                             leftmost_frame, session->current_end_frame());
767         }
768 }
769
770 void
771 Editor::update_fixed_rulers ()
772 {
773         nframes64_t rightmost_frame;
774
775         if (session == 0) {
776                 return;
777         }
778
779         ruler_metrics[ruler_metric_smpte].units_per_pixel = frames_per_unit;
780         ruler_metrics[ruler_metric_frames].units_per_pixel = frames_per_unit;
781         ruler_metrics[ruler_metric_minsec].units_per_pixel = frames_per_unit;
782
783         rightmost_frame = leftmost_frame + current_page_frames ();
784
785         /* these force a redraw, which in turn will force execution of the metric callbacks
786            to compute the relevant ticks to display.
787         */
788
789         if (ruler_timecode_action->get_active()) {
790                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_smpte_ruler), leftmost_frame, rightmost_frame,
791                                             leftmost_frame, session->current_end_frame());
792         }
793         
794         if (ruler_samples_action->get_active()) {
795                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_frames_ruler), leftmost_frame, rightmost_frame,
796                                             leftmost_frame, session->current_end_frame());
797         }
798         
799         if (ruler_minsec_action->get_active()) {
800                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_minsec_ruler), leftmost_frame, rightmost_frame,
801                                             leftmost_frame, session->current_end_frame());
802         }
803 }               
804
805 void
806 Editor::update_tempo_based_rulers ()
807 {
808         if (session == 0) {
809                 return;
810         }
811
812         ruler_metrics[ruler_metric_bbt].units_per_pixel = frames_per_unit;
813
814         if (ruler_bbt_action->get_active()) {
815                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_bbt_ruler), leftmost_frame, leftmost_frame+current_page_frames(),
816                                             leftmost_frame, session->current_end_frame());
817         }
818 }
819
820 /* Mark generation */
821
822 gint
823 Editor::_metric_get_smpte (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
824 {
825         return ruler_editor->metric_get_smpte (marks, lower, upper, maxchars);
826 }
827
828 gint
829 Editor::_metric_get_bbt (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
830 {
831         return ruler_editor->metric_get_bbt (marks, lower, upper, maxchars);
832 }
833
834 gint
835 Editor::_metric_get_frames (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
836 {
837         return ruler_editor->metric_get_frames (marks, lower, upper, maxchars);
838 }
839
840 gint
841 Editor::_metric_get_minsec (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
842 {
843         return ruler_editor->metric_get_minsec (marks, lower, upper, maxchars);
844 }
845
846 gint
847 Editor::metric_get_smpte (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
848 {
849         nframes_t pos;
850         nframes64_t range;
851         nframes64_t spacer;
852         nframes64_t fr;
853         SMPTE::Time smpte;
854         gchar buf[16];
855         gint nmarks = 0;
856         gint n;
857         bool show_bits = false;
858         bool show_frames = false;
859         bool show_seconds = false;
860         bool show_minutes = false;
861         bool show_hours = false;
862         int mark_modulo;
863
864         if (session == 0) {
865                 return 0;
866         }
867
868         fr = session->frame_rate();
869
870         if (lower > (spacer = (nframes64_t)(128 * Editor::get_current_zoom ()))) {
871                 lower = lower - spacer;
872         } else {
873                 lower = 0;
874         }
875         upper = upper + spacer;
876         range = (nframes64_t) floor (upper - lower);
877
878         if (range < (2 * session->frames_per_smpte_frame())) { /* 0 - 2 frames */
879                 show_bits = true;
880                 mark_modulo = 20;
881                 nmarks = 1 + (2 * Config->get_subframes_per_frame());
882         } else if (range <= (fr / 4)) { /* 2 frames - 0.250 second */
883                 show_frames = true;
884                 mark_modulo = 1;
885                 nmarks = 1 + (range / (nframes64_t)session->frames_per_smpte_frame());
886         } else if (range <= (fr / 2)) { /* 0.25-0.5 second */
887                 show_frames = true;
888                 mark_modulo = 2;
889                 nmarks = 1 + (range / (nframes64_t)session->frames_per_smpte_frame());
890         } else if (range <= fr) { /* 0.5-1 second */
891                 show_frames = true;
892                 mark_modulo = 5;
893                 nmarks = 1 + (range / (nframes64_t)session->frames_per_smpte_frame());
894         } else if (range <= 2 * fr) { /* 1-2 seconds */
895                 show_frames = true;
896                 mark_modulo = 10;
897                 nmarks = 1 + (range / (nframes64_t)session->frames_per_smpte_frame());
898         } else if (range <= 8 * fr) { /* 2-8 seconds */
899                 show_seconds = true;
900                 mark_modulo = 1;
901                 nmarks = 1 + (range / fr);
902         } else if (range <= 16 * fr) { /* 8-16 seconds */
903                 show_seconds = true;
904                 mark_modulo = 2;
905                 nmarks = 1 + (range / fr);
906         } else if (range <= 30 * fr) { /* 16-30 seconds */
907                 show_seconds = true;
908                 mark_modulo = 5;
909                 nmarks = 1 + (range / fr);
910         } else if (range <= 60 * fr) { /* 30-60 seconds */
911                 show_seconds = true;
912                 mark_modulo = 5;
913                 nmarks = 1 + (range / fr);
914         } else if (range <= 2 * 60 * fr) { /* 1-2 minutes */
915                 show_seconds = true;
916                 mark_modulo = 20;
917                 nmarks = 1 + (range / fr);
918         } else if (range <= 4 * 60 * fr) { /* 2-4 minutes */
919                 show_seconds = true;
920                 mark_modulo = 30;
921                 nmarks = 1 + (range / fr);
922         } else if (range <= 10 * 60 * fr) { /* 4-10 minutes */
923                 show_minutes = true;
924                 mark_modulo = 2;
925                 nmarks = 1 + 10;
926         } else if (range <= 30 * 60 * fr) { /* 10-30 minutes */
927                 show_minutes = true;
928                 mark_modulo = 5;
929                 nmarks = 1 + 30;
930         } else if (range <= 60 * 60 * fr) { /* 30 minutes - 1hr */
931                 show_minutes = true;
932                 mark_modulo = 10;
933                 nmarks = 1 + 60;
934         } else if (range <= 4 * 60 * 60 * fr) { /* 1 - 4 hrs*/
935                 show_minutes = true;
936                 mark_modulo = 30;
937                 nmarks = 1 + (60 * 4);
938         } else if (range <= 8 * 60 * 60 * fr) { /* 4 - 8 hrs*/
939                 show_hours = true;
940                 mark_modulo = 1;
941                 nmarks = 1 + 8;
942         } else if (range <= 16 * 60 * 60 * fr) { /* 16-24 hrs*/
943                 show_hours = true;
944                 mark_modulo = 1;
945                 nmarks = 1 + 24;
946         } else {
947     
948                 /* not possible if nframes64_t is a 32 bit quantity */
949     
950                 show_hours = true;
951                 mark_modulo = 4;
952                 nmarks = 1 + 24;
953         }
954   
955         pos = (nframes_t) floor (lower);
956         
957         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);  
958         
959         if (show_bits) {
960                 // Find smpte time of this sample (pos) with subframe accuracy
961                 session->sample_to_smpte(pos, smpte, true /* use_offset */, true /* use_subframes */ );
962     
963                 for (n = 0; n < nmarks; n++) {
964                         session->smpte_to_sample(smpte, pos, true /* use_offset */, true /* use_subframes */ );
965                         if ((smpte.subframes % mark_modulo) == 0) {
966                                 if (smpte.subframes == 0) {
967                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
968                                         snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
969                                 } else {
970                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
971                                         snprintf (buf, sizeof(buf), ".%02u", smpte.subframes);
972                                 }
973                         } else {
974                                 snprintf (buf, sizeof(buf)," ");
975                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
976         
977                         }
978                         (*marks)[n].label = g_strdup (buf);
979                         (*marks)[n].position = pos;
980
981                         // Increment subframes by one
982                         SMPTE::increment_subframes( smpte );
983                 }
984         } else if (show_seconds) {
985                 // Find smpte time of this sample (pos)
986                 session->sample_to_smpte(pos, smpte, true /* use_offset */, false /* use_subframes */ );
987                 // Go to next whole second down
988                 SMPTE::seconds_floor( smpte );
989
990                 for (n = 0; n < nmarks; n++) {
991                         session->smpte_to_sample(smpte, pos, true /* use_offset */, false /* use_subframes */ );
992                         if ((smpte.seconds % mark_modulo) == 0) {
993                                 if (smpte.seconds == 0) {
994                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
995                                         (*marks)[n].position = pos;
996                                 } else {
997                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
998                                         (*marks)[n].position = pos;
999                                 }
1000                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
1001                         } else {
1002                                 snprintf (buf, sizeof(buf)," ");
1003                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1004                                 (*marks)[n].position = pos;
1005         
1006                         }
1007                         (*marks)[n].label = g_strdup (buf);
1008                         SMPTE::increment_seconds( smpte );
1009                 }
1010         } else if (show_minutes) {
1011                 // Find smpte time of this sample (pos)
1012                 session->sample_to_smpte(pos, smpte, true /* use_offset */, false /* use_subframes */ );
1013                 // Go to next whole minute down
1014                 SMPTE::minutes_floor( smpte );
1015
1016                 for (n = 0; n < nmarks; n++) {
1017                         session->smpte_to_sample(smpte, pos, true /* use_offset */, false /* use_subframes */ );
1018                         if ((smpte.minutes % mark_modulo) == 0) {
1019                                 if (smpte.minutes == 0) {
1020                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1021                                 } else {
1022                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1023                                 }
1024                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
1025                         } else {
1026                                 snprintf (buf, sizeof(buf)," ");
1027                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1028         
1029                         }
1030                         (*marks)[n].label = g_strdup (buf);
1031                         (*marks)[n].position = pos;
1032                         SMPTE::increment_minutes( smpte );
1033                 }
1034         } else if (show_hours) {
1035                 // Find smpte time of this sample (pos)
1036                 session->sample_to_smpte(pos, smpte, true /* use_offset */, false /* use_subframes */ );
1037                 // Go to next whole hour down
1038                 SMPTE::hours_floor( smpte );
1039
1040                 for (n = 0; n < nmarks; n++) {
1041                         session->smpte_to_sample(smpte, pos, true /* use_offset */, false /* use_subframes */ );
1042                         if ((smpte.hours % mark_modulo) == 0) {
1043                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1044                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
1045                         } else {
1046                                 snprintf (buf, sizeof(buf)," ");
1047                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1048         
1049                         }
1050                         (*marks)[n].label = g_strdup (buf);
1051                         (*marks)[n].position = pos;
1052
1053                         SMPTE::increment_hours( smpte );
1054                 }
1055         } else { // show_frames
1056                 // Find smpte time of this sample (pos)
1057                 session->sample_to_smpte(pos, smpte, true /* use_offset */, false /* use_subframes */ );
1058                 // Go to next whole frame down
1059                 SMPTE::frames_floor( smpte );
1060
1061                 for (n = 0; n < nmarks; n++) {
1062                         session->smpte_to_sample(smpte, pos, true /* use_offset */, false /* use_subframes */ );
1063                         if ((smpte.frames % mark_modulo) == 0)  {
1064                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1065                                 (*marks)[n].position = pos;
1066                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
1067                         } else {
1068                                 snprintf (buf, sizeof(buf)," ");
1069                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1070                                 (*marks)[n].position = pos;
1071         
1072                         }
1073                         (*marks)[n].label = g_strdup (buf);
1074                         SMPTE::increment( smpte );
1075                 }
1076         }
1077   
1078         return nmarks;
1079 }
1080
1081
1082 gint
1083 Editor::metric_get_bbt (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
1084 {
1085         if (session == 0) {
1086                 return 0;
1087         }
1088
1089         TempoMap::BBTPointList::iterator i;
1090
1091         uint32_t beats = 0;
1092         uint32_t bars = 0;
1093         uint32_t desirable_marks;
1094         uint32_t magic_accent_number = 1;
1095         gint nmarks;
1096         char buf[64];
1097         gint  n = 0;
1098         nframes64_t pos;
1099         bool bar_helper_on = true;
1100        
1101         BBT_Time next_beat;
1102         nframes64_t next_beat_pos;
1103
1104         if ((desirable_marks = maxchars / 7) == 0) {
1105                return 0;
1106         }
1107
1108         /* align the tick marks to whatever we're snapping to... */
1109
1110         switch (snap_type) {
1111         case SnapToAThirdBeat:
1112                 bbt_beat_subdivision = 3;
1113                 break;
1114         case SnapToAQuarterBeat:
1115                 bbt_beat_subdivision = 4;
1116                 break;
1117         case SnapToAEighthBeat:
1118                 bbt_beat_subdivision = 8;
1119                 magic_accent_number = 2;
1120                 break;
1121         case SnapToASixteenthBeat:
1122                 bbt_beat_subdivision = 16;
1123                 magic_accent_number = 4;
1124                 break;
1125         case SnapToAThirtysecondBeat:
1126                 bbt_beat_subdivision = 32;
1127                 magic_accent_number = 8;
1128                 break;
1129         default:
1130                bbt_beat_subdivision = 4;
1131                 break;
1132         }
1133
1134         if (current_bbt_points == 0 || current_bbt_points->empty()) {
1135                 return 0;
1136         }
1137
1138         i = current_bbt_points->end();
1139         i--;
1140         bars = (*i).bar - (*current_bbt_points->begin()).bar;
1141         beats = current_bbt_points->size() - bars;
1142
1143         /*Only show the bar helper if there aren't many bars on the screen */
1144         if (bars > 1) {
1145                 bar_helper_on = false;
1146         }
1147
1148         if (desirable_marks > (beats / 4)) {
1149
1150                 /* we're in beat land...*/
1151
1152                 uint32_t tick = 0;
1153                 uint32_t skip;
1154                 uint32_t t;
1155                 nframes64_t frame_skip;
1156                 double frame_skip_error;
1157                 double accumulated_error;
1158                 double position_of_helper;
1159                 bool i_am_accented = false;
1160                 bool we_need_ticks = false;
1161                 bool helper_active = false;
1162         
1163                 position_of_helper = lower + (30 * Editor::get_current_zoom ());
1164
1165                 if (desirable_marks >= (beats)) {
1166                         nmarks = (beats * bbt_beat_subdivision) + 1;
1167                         we_need_ticks = true;
1168                 } else {
1169                         nmarks = beats + 1;
1170                 }
1171
1172                 *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1173
1174                 (*marks)[0].label = g_strdup(" ");
1175                 (*marks)[0].position = lower;
1176                 (*marks)[0].style = GtkCustomRulerMarkMicro;
1177                 
1178                 for (n = 1,   i = current_bbt_points->begin(); n < nmarks && i != current_bbt_points->end(); ++i) {
1179
1180                         if ((*i).frame < lower && (bar_helper_on)) {
1181                                         snprintf (buf, sizeof(buf), "<%" PRIu32 "|%" PRIu32, (*i).bar, (*i).beat);
1182                                         (*marks)[0].label = g_strdup (buf); 
1183                                         helper_active = true;
1184                         } else {
1185
1186                           if ((*i).type == TempoMap::Bar)  {
1187                             if (((*i).frame < position_of_helper) && helper_active) {
1188                               snprintf (buf, sizeof(buf), " ");
1189                             } else {
1190                               snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1191                             }
1192                             (*marks)[n].label = g_strdup (buf);
1193                             (*marks)[n].position = (*i).frame;
1194                             (*marks)[n].style = GtkCustomRulerMarkMajor;
1195                             n++;
1196                             
1197                           } else if (((*i).type == TempoMap::Beat) && ((*i).beat > 1)) {
1198                             ((((*i).frame < position_of_helper) && bar_helper_on) || !we_need_ticks) ?
1199                               snprintf (buf, sizeof(buf), " ") : snprintf (buf, sizeof(buf), "%" PRIu32, (*i).beat);
1200                             if (((*i).beat % 2 == 1) || we_need_ticks) {
1201                               (*marks)[n].style = GtkCustomRulerMarkMinor;
1202                             } else {
1203                               (*marks)[n].style = GtkCustomRulerMarkMicro;
1204                             }
1205                             (*marks)[n].label =  g_strdup (buf);
1206                             (*marks)[n].position = (*i).frame;
1207                             n++;
1208                           }
1209
1210                         }
1211
1212
1213                         /* Add the tick marks */
1214
1215                         if (we_need_ticks && (*i).type == TempoMap::Beat) {
1216
1217                                 /* Find the next beat */
1218
1219                                 next_beat.beats = (*i).beat;
1220                                 next_beat.bars = (*i).bar;
1221
1222                                 if ((*i).meter->beats_per_bar() > (next_beat.beats + 1)) {
1223                                   next_beat.beats += 1;
1224                                 } else {
1225                                   next_beat.bars += 1;
1226                                   next_beat.beats = 1;
1227                                 }
1228                                 
1229                                 next_beat_pos = session->tempo_map().frame_time(next_beat);
1230
1231                                 frame_skip = (nframes64_t) floor (frame_skip_error = (session->frame_rate() *  60) / (bbt_beat_subdivision * (*i).tempo->beats_per_minute()));
1232                                 frame_skip_error -= frame_skip;
1233                                 skip = (uint32_t) (Meter::ticks_per_beat / bbt_beat_subdivision);
1234
1235                                 pos = (*i).frame + frame_skip;
1236                                 accumulated_error = frame_skip_error;
1237
1238                                 tick = skip;
1239
1240                                 for (t = 0; (tick < Meter::ticks_per_beat) && (n < nmarks) && (pos < next_beat_pos) ; pos += frame_skip, tick += skip, ++t) {
1241
1242                                         if (t % magic_accent_number == (magic_accent_number - 1)) {
1243                                                 i_am_accented = true;
1244                                         }
1245                                         if (Editor::get_current_zoom () > 32) {
1246                                                 snprintf (buf, sizeof(buf), " ");
1247                                         } else if ((Editor::get_current_zoom () > 8) && !i_am_accented) {
1248                                                 snprintf (buf, sizeof(buf), " ");
1249                                         } else  if (bar_helper_on && (pos < position_of_helper)) {
1250                                                 snprintf (buf, sizeof(buf), " ");
1251                                         } else {
1252                                                 snprintf (buf, sizeof(buf), "%" PRIu32, tick);
1253                                         }
1254
1255                                         (*marks)[n].label = g_strdup (buf);
1256
1257                                         /* Error compensation for float to nframes64_t*/
1258                                         accumulated_error += frame_skip_error;
1259                                         if (accumulated_error > 1) {
1260                                                 pos += 1;
1261                                                 accumulated_error -= 1.0f;
1262                                         }
1263
1264                                         (*marks)[n].position = pos;
1265
1266                                         if ((bbt_beat_subdivision > 4) && i_am_accented) {
1267                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1268                                         } else {
1269                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1270                                         }
1271                                         i_am_accented = false;
1272                                         n++;
1273                                 
1274                                 }
1275                         }
1276                 }
1277                 return n; //return the actual number of marks made, since we might have skipped some fro fractional time signatures 
1278
1279        } else {
1280
1281                 /* we're in bar land */
1282
1283                 if (desirable_marks < (bars / 256)) {
1284                         nmarks = 1;
1285                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1286                         snprintf (buf, sizeof(buf), "too many bars... (currently %" PRIu32 ")", bars );
1287                         (*marks)[0].style = GtkCustomRulerMarkMajor;
1288                         (*marks)[0].label = g_strdup (buf);
1289                         (*marks)[0].position = lower;
1290                 } else if (desirable_marks < (uint32_t)(nmarks = (gint) (bars / 64) + 1)) {
1291                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1292                         for (n = 0,   i = current_bbt_points->begin(); i != current_bbt_points->end() && n < nmarks; i++) {
1293                                 if ((*i).type == TempoMap::Bar)  {
1294                                         if ((*i).bar % 64 == 1) {
1295                                                 if ((*i).bar % 256 == 1) {
1296                                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1297                                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1298                                                 } else {
1299                                                         snprintf (buf, sizeof(buf), " ");
1300                                                         if ((*i).bar % 256 == 129)  {
1301                                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1302                                                         } else {
1303                                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1304                                                         }
1305                                                 }
1306                                                 (*marks)[n].label = g_strdup (buf);
1307                                                 (*marks)[n].position = (*i).frame;
1308                                                 n++;
1309                                         }
1310                                 }
1311                         }
1312                 } else if (desirable_marks < (uint32_t)(nmarks = (bars / 16) + 1)) {
1313                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1314                         for (n = 0,  i = current_bbt_points->begin(); i != current_bbt_points->end() && n < nmarks; i++) {
1315                                 if ((*i).type == TempoMap::Bar)  {
1316                                         if ((*i).bar % 16 == 1) {
1317                                                 if ((*i).bar % 64 == 1) {
1318                                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1319                                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1320                                                 } else {
1321                                                         snprintf (buf, sizeof(buf), " ");
1322                                                         if ((*i).bar % 64 == 33)  {
1323                                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1324                                                         } else {
1325                                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1326                                                         }
1327                                                 }
1328                                                 (*marks)[n].label = g_strdup (buf);
1329                                                 (*marks)[n].position = (*i).frame;
1330                                                 n++;
1331                                         }
1332                                 }
1333                         }
1334                 } else if (desirable_marks < (uint32_t)(nmarks = (bars / 4) + 1)){
1335                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1336                         for (n = 0,   i = current_bbt_points->begin(); i != current_bbt_points->end() && n < nmarks; ++i) {
1337                                 if ((*i).type == TempoMap::Bar)  {
1338                                         if ((*i).bar % 4 == 1) {
1339                                                 if ((*i).bar % 16 == 1) {
1340                                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1341                                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1342                                                 } else {
1343                                                         snprintf (buf, sizeof(buf), " ");
1344                                                         if ((*i).bar % 16 == 9)  {
1345                                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1346                                                         } else {
1347                                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1348                                                         }
1349                                                 }
1350                                                 (*marks)[n].label = g_strdup (buf);
1351                                                 (*marks)[n].position = (*i).frame;
1352                                                 n++;
1353                                         }
1354                                 }
1355                         }
1356                 } else {
1357                         nmarks = bars + 1;
1358                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks );
1359                         for (n = 0,  i = current_bbt_points->begin(); i != current_bbt_points->end() && n < nmarks; i++) {
1360                                 if ((*i).type == TempoMap::Bar)  {
1361                                         if ((*i).bar % 4 == 1) {
1362                                                 snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1363                                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1364                                         } else {
1365                                                 snprintf (buf, sizeof(buf), " ");
1366                                                 if ((*i).bar % 4 == 3)  {
1367                                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1368                                                 } else {
1369                                                         (*marks)[n].style = GtkCustomRulerMarkMicro;
1370                                                 }
1371                                         }
1372                                         (*marks)[n].label = g_strdup (buf);
1373                                         (*marks)[n].position = (*i).frame;
1374                                         n++;
1375                                 }
1376                         }
1377                 }
1378                 return n;
1379         }
1380 }
1381
1382 gint
1383 Editor::metric_get_frames (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
1384 {
1385         nframes64_t mark_interval;
1386         nframes64_t pos;
1387         nframes64_t ilower = (nframes64_t) floor (lower);
1388         nframes64_t iupper = (nframes64_t) floor (upper);
1389         gchar buf[16];
1390         gint nmarks;
1391         gint n;
1392
1393         if (session == 0) {
1394                 return 0;
1395         }
1396
1397         mark_interval = (iupper - ilower) / 5;
1398         if (mark_interval > session->frame_rate()) {
1399                 mark_interval -= mark_interval % session->frame_rate();
1400         } else {
1401                 mark_interval = session->frame_rate() / (session->frame_rate() / mark_interval ) ;
1402         }
1403         nmarks = 5;
1404         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1405         for (n = 0, pos = ilower; n < nmarks; pos += mark_interval, ++n) {
1406                 snprintf (buf, sizeof(buf), "%" PRIi64,  pos);
1407                 (*marks)[n].label = g_strdup (buf);
1408                 (*marks)[n].position = pos;
1409                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1410         }
1411         
1412         return nmarks;
1413 }
1414
1415 static void
1416 sample_to_clock_parts ( nframes64_t sample,
1417                         nframes64_t sample_rate, 
1418                         long *hrs_p,
1419                         long *mins_p,
1420                         long *secs_p,
1421                         long *millisecs_p)
1422
1423 {
1424         nframes64_t left;
1425         long hrs;
1426         long mins;
1427         long secs;
1428         long millisecs;
1429         
1430         left = sample;
1431         hrs = left / (sample_rate * 60 * 60);
1432         left -= hrs * sample_rate * 60 * 60;
1433         mins = left / (sample_rate * 60);
1434         left -= mins * sample_rate * 60;
1435         secs = left / sample_rate;
1436         left -= secs * sample_rate;
1437         millisecs = left * 1000 / sample_rate;
1438
1439         *millisecs_p = millisecs;
1440         *secs_p = secs;
1441         *mins_p = mins;
1442         *hrs_p = hrs;
1443
1444         return;
1445 }
1446
1447 gint
1448 Editor::metric_get_minsec (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
1449 {
1450         nframes64_t range;
1451         nframes64_t fr;
1452         nframes64_t mark_interval;
1453         nframes64_t pos;
1454         nframes64_t spacer;
1455         long hrs, mins, secs, millisecs;
1456         gchar buf[16];
1457         gint nmarks;
1458         gint n;
1459         gint mark_modulo = 100;
1460         bool show_seconds = false;
1461         bool show_minutes = false;
1462         bool show_hours = false;
1463         nframes64_t ilower = (nframes64_t) floor (lower);
1464         nframes64_t iupper = (nframes64_t) floor (upper);
1465
1466         if (session == 0) {
1467                 return 0;
1468         }
1469
1470         fr = session->frame_rate();
1471
1472         /* to prevent 'flashing' */
1473         if (lower > (spacer = (nframes64_t)(128 * Editor::get_current_zoom ()))) {
1474                 lower = lower - spacer;
1475         } else {
1476                 lower = 0;
1477         }
1478         upper = upper + spacer;
1479         range = iupper - ilower;
1480
1481         if (range <  (fr / 50)) {
1482                 mark_interval =  fr / 100; /* show 1/100 seconds */
1483                 mark_modulo = 10;
1484         } else if (range <= (fr / 10)) { /* 0-0.1 second */
1485                 mark_interval = fr / 50; /* show 1/50 seconds */
1486                 mark_modulo = 20;
1487         } else if (range <= (fr / 2)) { /* 0-0.5 second */
1488                 mark_interval = fr / 20;  /* show 1/20 seconds */
1489                 mark_modulo = 100;
1490         } else if (range <= fr) { /* 0-1 second */
1491                 mark_interval = fr / 10;  /* show 1/10 seconds */
1492                 mark_modulo = 200;
1493         } else if (range <= 2 * fr) { /* 1-2 seconds */
1494                 mark_interval = fr / 2; /* show 1/2 seconds */
1495                 mark_modulo = 500;
1496         } else if (range <= 8 * fr) { /* 2-5 seconds */
1497                 mark_interval =  fr / 5; /* show 2 seconds */
1498                 mark_modulo = 1000;
1499         } else if (range <= 16 * fr) { /* 8-16 seconds */
1500                 mark_interval =  fr; /* show 1 seconds */
1501                 show_seconds = true;
1502                 mark_modulo = 5;
1503         } else if (range <= 30 * fr) { /* 10-30 seconds */
1504                 mark_interval =  fr; /* show 10 seconds */
1505                 show_seconds = true;
1506                 mark_modulo = 5;
1507         } else if (range <= 60 * fr) { /* 30-60 seconds */
1508                 mark_interval = 5 * fr; /* show 5 seconds */
1509                 show_seconds = true;
1510                 mark_modulo = 3;
1511         } else if (range <= 2 * 60 * fr) { /* 1-2 minutes */
1512                 mark_interval = 5 * fr; /* show 5 seconds */
1513                 show_seconds = true;
1514                 mark_modulo = 3;
1515         } else if (range <= 4 * 60 * fr) { /* 4 minutes */
1516                 mark_interval = 10 * fr; /* show 10 seconds */
1517                 show_seconds = true;
1518                 mark_modulo = 30;
1519         } else if (range <= 10 * 60 * fr) { /* 10 minutes */
1520                 mark_interval = 30 * fr; /* show 30 seconds */
1521                 show_seconds = true;
1522                 mark_modulo = 60;
1523         } else if (range <= 30 * 60 * fr) { /* 10-30 minutes */
1524                 mark_interval =  60 * fr; /* show 1 minute */
1525                 show_minutes = true;
1526                 mark_modulo = 5;
1527         } else if (range <= 60 * 60 * fr) { /* 30 minutes - 1hr */
1528                 mark_interval = 2 * 60 * fr; /* show 2 minutes */
1529                 show_minutes = true;
1530                 mark_modulo = 10;
1531         } else if (range <= 4 * 60 * 60 * fr) { /* 1 - 4 hrs*/
1532                 mark_interval = 5 * 60 * fr; /* show 10 minutes */
1533                 show_minutes = true;
1534                 mark_modulo = 30;
1535         } else if (range <= 8 * 60 * 60 * fr) { /* 4 - 8 hrs*/
1536                 mark_interval = 20 * 60 * fr; /* show 20 minutes */
1537                 show_minutes = true;
1538                 mark_modulo = 60;
1539         } else if (range <= 16 * 60 * 60 * fr) { /* 16-24 hrs*/
1540                 mark_interval =  60 * 60 * fr; /* show 60 minutes */
1541                 show_hours = true;
1542                 mark_modulo = 2;
1543         } else {
1544                                                                                                                    
1545                 /* not possible if nframes64_t is a 32 bit quantity */
1546                                                                                                                    
1547                 mark_interval = 4 * 60 * 60 * fr; /* show 4 hrs */
1548         }
1549
1550         nmarks = 1 + (range / mark_interval);
1551         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1552         pos = ((ilower + (mark_interval/2))/mark_interval) * mark_interval;
1553         
1554         if (show_seconds) {
1555                 for (n = 0; n < nmarks; pos += mark_interval, ++n) {
1556                         sample_to_clock_parts (pos, fr, &hrs, &mins, &secs, &millisecs);
1557                         if (secs % mark_modulo == 0) {
1558                                 if (secs == 0) {
1559                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1560                                 } else {
1561                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1562                                 }
1563                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1564                         } else {
1565                                 snprintf (buf, sizeof(buf), " ");
1566                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1567                         }
1568                         (*marks)[n].label = g_strdup (buf);
1569                         (*marks)[n].position = pos;
1570                 }
1571         } else if (show_minutes) {
1572                 for (n = 0; n < nmarks; pos += mark_interval, ++n) {
1573                         sample_to_clock_parts (pos, fr, &hrs, &mins, &secs, &millisecs);
1574                         if (mins % mark_modulo == 0) {
1575                                 if (mins == 0) {
1576                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1577                                 } else {
1578                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1579                                 }
1580                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1581                         } else {
1582                                 snprintf (buf, sizeof(buf), " ");
1583                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1584                         }
1585                         (*marks)[n].label = g_strdup (buf);
1586                         (*marks)[n].position = pos;
1587                 }
1588         } else if (show_hours) {
1589                  for (n = 0; n < nmarks; pos += mark_interval, ++n) {
1590                         sample_to_clock_parts (pos, fr, &hrs, &mins, &secs, &millisecs);
1591                         if (hrs % mark_modulo == 0) {
1592                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1593                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1594                         } else {
1595                                 snprintf (buf, sizeof(buf), " ");
1596                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1597                         }
1598                         (*marks)[n].label = g_strdup (buf);
1599                         (*marks)[n].position = pos;
1600                 }
1601         } else {
1602                 for (n = 0; n < nmarks; pos += mark_interval, ++n) {
1603                         sample_to_clock_parts (pos, fr, &hrs, &mins, &secs, &millisecs);
1604                         if (millisecs % mark_modulo == 0) {
1605                                 if (millisecs == 0) {
1606                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1607                                 } else {
1608                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1609                                 }
1610                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1611                         } else {
1612                                 snprintf (buf, sizeof(buf), " ");
1613                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1614                         }
1615                         (*marks)[n].label = g_strdup (buf);
1616                         (*marks)[n].position = pos;
1617                 }
1618         }
1619
1620         return nmarks;
1621 }