Unify the canvases by moving groups around rather than using set_scrolling_region...
[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 = 1; /*this will be changed below */
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_lab_children =  ruler_label_vbox.children();
560         BoxList & ruler_children =  time_canvas_vbox.children();
561         int visible_rulers = 0;
562
563         if (no_ruler_shown_update) {
564                 return;
565         }
566
567         visible_timebars = 0;
568
569         lab_children.clear();
570         ruler_lab_children.clear();
571
572         // leave the last one (the time_canvas) intact
573         while (ruler_children.size() > 0) {
574                 ruler_children.pop_front();
575         }
576
577         BoxList::iterator canvaspos = ruler_children.begin();
578         
579         _smpte_ruler = gtk_custom_hruler_new ();
580         smpte_ruler = Glib::wrap (_smpte_ruler);
581         smpte_ruler->set_name ("SMPTERuler");
582         smpte_ruler->set_size_request (-1, (int)timebar_height);
583         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_smpte_ruler), &ruler_metrics[ruler_metric_smpte]);
584         
585         _bbt_ruler = gtk_custom_hruler_new ();
586         bbt_ruler = Glib::wrap (_bbt_ruler);
587         bbt_ruler->set_name ("BBTRuler");
588         bbt_ruler->set_size_request (-1, (int)timebar_height);
589         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_bbt_ruler), &ruler_metrics[ruler_metric_bbt]);
590
591         _frames_ruler = gtk_custom_hruler_new ();
592         frames_ruler = Glib::wrap (_frames_ruler);
593         frames_ruler->set_name ("FramesRuler");
594         frames_ruler->set_size_request (-1, (int)timebar_height);
595         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_frames_ruler), &ruler_metrics[ruler_metric_frames]);
596
597         _minsec_ruler = gtk_custom_hruler_new ();
598         minsec_ruler = Glib::wrap (_minsec_ruler);
599         minsec_ruler->set_name ("MinSecRuler");
600         minsec_ruler->set_size_request (-1, (int)timebar_height);
601         gtk_custom_ruler_set_metric (GTK_CUSTOM_RULER(_minsec_ruler), &ruler_metrics[ruler_metric_minsec]);
602
603         smpte_ruler->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
604         bbt_ruler->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
605         frames_ruler->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
606         minsec_ruler->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::SCROLL_MASK);
607
608         smpte_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
609         bbt_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
610         frames_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
611         minsec_ruler->signal_button_release_event().connect (mem_fun(*this, &Editor::ruler_button_release));
612
613         smpte_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
614         bbt_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
615         frames_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
616         minsec_ruler->signal_button_press_event().connect (mem_fun(*this, &Editor::ruler_button_press));
617         
618         smpte_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
619         bbt_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
620         frames_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
621         minsec_ruler->signal_motion_notify_event().connect (mem_fun(*this, &Editor::ruler_mouse_motion));
622
623         smpte_ruler->signal_scroll_event().connect (mem_fun(*this, &Editor::ruler_scroll));
624         bbt_ruler->signal_scroll_event().connect (mem_fun(*this, &Editor::ruler_scroll));
625         frames_ruler->signal_scroll_event().connect (mem_fun(*this, &Editor::ruler_scroll));
626         minsec_ruler->signal_scroll_event().connect (mem_fun(*this, &Editor::ruler_scroll));
627
628         ruler_children.insert (canvaspos, Element(*_ruler_separator, PACK_SHRINK, PACK_START));
629         
630         if (ruler_minsec_action->get_active()) {
631                 ruler_lab_children.push_back (Element(minsec_label, PACK_SHRINK, PACK_START));
632                 ruler_children.insert (canvaspos, Element(*minsec_ruler, PACK_SHRINK, PACK_START));
633                 visible_rulers++;
634         }
635
636         if (ruler_timecode_action->get_active()) {
637                 ruler_lab_children.push_back (Element(smpte_label, PACK_SHRINK, PACK_START));
638                 ruler_children.insert (canvaspos, Element(*smpte_ruler, PACK_SHRINK, PACK_START));
639                 visible_rulers++;
640         }
641
642         if (ruler_samples_action->get_active()) {
643                 ruler_lab_children.push_back (Element(frame_label, PACK_SHRINK, PACK_START));
644                 ruler_children.insert (canvaspos, Element(*frames_ruler, PACK_SHRINK, PACK_START));
645                 visible_rulers++;
646         }
647
648         if (ruler_bbt_action->get_active()) {
649                 ruler_lab_children.push_back (Element(bbt_label, PACK_SHRINK, PACK_START));
650                 ruler_children.insert (canvaspos, Element(*bbt_ruler, PACK_SHRINK, PACK_START));
651                 visible_rulers++;
652         }
653
654         double tbpos = 1.0;
655         double tbgpos = 1.0;
656         double old_unit_pos;
657         
658         if (ruler_meter_action->get_active()) {
659                 lab_children.push_back (Element(meter_label, PACK_SHRINK, PACK_START));
660
661                 old_unit_pos = meter_group->property_y();
662                 if (tbpos != old_unit_pos) {
663                         meter_group->move ( 0.0, tbpos - old_unit_pos);
664                 }
665                 old_unit_pos = meter_bar_group->property_y();
666                 if (tbgpos != old_unit_pos) {
667                         meter_bar_group->move ( 0.0, tbgpos - old_unit_pos);
668                 }
669                 meter_bar_group->show();
670                 meter_group->show();
671                 tbpos += timebar_height;
672                 tbgpos += timebar_height;
673                 visible_timebars++;
674         } else {
675                 meter_bar_group->hide();
676                 meter_group->hide();
677         }
678         
679         if (ruler_tempo_action->get_active()) {
680                 lab_children.push_back (Element(tempo_label, PACK_SHRINK, PACK_START));
681                 old_unit_pos = tempo_group->property_y();
682                 if (tbpos != old_unit_pos) {
683                         tempo_group->move(0.0, tbpos - old_unit_pos);
684                 }
685                 old_unit_pos = tempo_bar_group->property_y();
686                 if (tbgpos != old_unit_pos) {
687                         tempo_bar_group->move ( 0.0, tbgpos - old_unit_pos);
688                 }
689                 tempo_bar_group->show();
690                 tempo_group->show();
691                 tbpos += timebar_height;
692                 tbgpos += timebar_height;
693                 visible_timebars++;
694         } else {
695                 tempo_bar_group->hide();
696                 tempo_group->hide();
697         }
698         
699         if (!Profile->get_sae() && ruler_range_action->get_active()) {
700                 lab_children.push_back (Element(range_mark_label, PACK_SHRINK, PACK_START));
701                 old_unit_pos = range_marker_group->property_y();
702                 if (tbpos != old_unit_pos) {
703                         range_marker_group->move (0.0, tbpos - old_unit_pos);
704                 }
705                 old_unit_pos = range_marker_bar_group->property_y();
706                 if (tbgpos != old_unit_pos) {
707                         range_marker_bar_group->move (0.0, tbgpos - old_unit_pos);
708                 }
709                 range_marker_bar_group->show();
710                 range_marker_group->show();
711                 tbpos += timebar_height;
712                 tbgpos += timebar_height;
713                 visible_timebars++;
714         } else {
715                 range_marker_bar_group->hide();
716                 range_marker_group->hide();
717         }
718
719         if (ruler_loop_punch_action->get_active()) {
720                 lab_children.push_back (Element(transport_mark_label, PACK_SHRINK, PACK_START));
721                 old_unit_pos = transport_marker_group->property_y();
722                 if (tbpos != old_unit_pos) {
723                         transport_marker_group->move ( 0.0, tbpos - old_unit_pos);
724                 }
725                 old_unit_pos = transport_marker_bar_group->property_y();
726                 if (tbgpos != old_unit_pos) {
727                         transport_marker_bar_group->move ( 0.0, tbgpos - old_unit_pos);
728                 }
729                 transport_marker_bar_group->show();
730                 transport_marker_group->show();
731                 tbpos += timebar_height;
732                 tbgpos += timebar_height;
733                 visible_timebars++;
734         } else {
735                 transport_marker_bar_group->hide();
736                 transport_marker_group->hide();
737         }
738
739         if (ruler_cd_marker_action->get_active()) {
740                 lab_children.push_back (Element(cd_mark_label, PACK_SHRINK, PACK_START));
741                 old_unit_pos = cd_marker_group->property_y();
742                 if (tbpos != old_unit_pos) {
743                         cd_marker_group->move (0.0, tbpos - old_unit_pos);
744                 }
745                 old_unit_pos = cd_marker_bar_group->property_y();
746                 if (tbgpos != old_unit_pos) {
747                         cd_marker_bar_group->move (0.0, tbgpos - old_unit_pos);
748                 }
749                 cd_marker_bar_group->show();
750                 cd_marker_group->show();
751                 tbpos += timebar_height;
752                 tbgpos += timebar_height;
753                 visible_timebars++;
754                 // make sure all cd markers show up in their respective places
755                 update_cd_marker_display();
756         } else {
757                 cd_marker_bar_group->hide();
758                 cd_marker_group->hide();
759                 // make sure all cd markers show up in their respective places
760                 update_cd_marker_display();
761         }
762         
763         if (ruler_marker_action->get_active()) {
764                 lab_children.push_back (Element(mark_label, PACK_SHRINK, PACK_START));
765                 old_unit_pos = marker_group->property_y();
766                 if (tbpos != old_unit_pos) {
767                         marker_group->move ( 0.0, tbpos - old_unit_pos);
768                 }
769                 old_unit_pos = marker_bar_group->property_y();
770                 if (tbgpos != old_unit_pos) {
771                         marker_bar_group->move ( 0.0, tbgpos - old_unit_pos);
772                 }
773                 marker_bar_group->show();
774                 marker_group->show();
775                 tbpos += timebar_height;
776                 tbgpos += timebar_height;
777                 visible_timebars++;
778         } else {
779                 marker_bar_group->hide();
780                 marker_group->hide();
781         }
782         
783         gdouble old_canvas_timebars_vsize = canvas_timebars_vsize;
784         canvas_timebars_vsize = (timebar_height * visible_timebars) + 2.0;
785         gdouble vertical_pos_delta = canvas_timebars_vsize - old_canvas_timebars_vsize;
786
787         if (vertical_pos_delta < 0 && (vertical_adjustment.get_value() + canvas_height) >= vertical_adjustment.get_upper()) {
788                 /*if we're at the bottom of the canvas, don't move the _trackview_grooup*/
789                 vertical_adjustment.set_upper(vertical_adjustment.get_upper() + vertical_pos_delta);
790         } else {
791                 vertical_adjustment.set_upper(vertical_adjustment.get_upper() + vertical_pos_delta);
792                 _trackview_group->move (0, vertical_pos_delta);
793         }
794         ruler_label_vbox.set_size_request (-1, (int)(timebar_height * visible_rulers));
795
796         time_canvas_vbox.set_size_request (-1,-1);
797         time_canvas_event_box.queue_resize();
798         
799         update_fixed_rulers();
800         redisplay_tempo (false);
801
802         time_canvas_event_box.show_all();
803         ruler_label_frame.show_all();
804         time_button_frame.show_all();
805 }
806
807 void
808 Editor::update_just_smpte ()
809 {
810         ENSURE_GUI_THREAD(mem_fun(*this, &Editor::update_just_smpte));
811         
812         if (session == 0) {
813                 return;
814         }
815
816         nframes64_t rightmost_frame = leftmost_frame + current_page_frames();
817
818         if (ruler_timecode_action->get_active()) {
819                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_smpte_ruler), leftmost_frame, rightmost_frame,
820                                             leftmost_frame, session->current_end_frame());
821         }
822 }
823
824 void
825 Editor::update_fixed_rulers ()
826 {
827         nframes64_t rightmost_frame;
828
829         if (session == 0) {
830                 return;
831         }
832
833         ruler_metrics[ruler_metric_smpte].units_per_pixel = frames_per_unit;
834         ruler_metrics[ruler_metric_frames].units_per_pixel = frames_per_unit;
835         ruler_metrics[ruler_metric_minsec].units_per_pixel = frames_per_unit;
836
837         rightmost_frame = leftmost_frame + current_page_frames ();
838
839         /* these force a redraw, which in turn will force execution of the metric callbacks
840            to compute the relevant ticks to display.
841         */
842
843         if (ruler_timecode_action->get_active()) {
844                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_smpte_ruler), leftmost_frame, rightmost_frame,
845                                             leftmost_frame, session->current_end_frame());
846         }
847         
848         if (ruler_samples_action->get_active()) {
849                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_frames_ruler), leftmost_frame, rightmost_frame,
850                                             leftmost_frame, session->current_end_frame());
851         }
852         
853         if (ruler_minsec_action->get_active()) {
854                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_minsec_ruler), leftmost_frame, rightmost_frame,
855                                             leftmost_frame, session->current_end_frame());
856         }
857 }               
858
859 void
860 Editor::update_tempo_based_rulers ()
861 {
862         if (session == 0) {
863                 return;
864         }
865
866         ruler_metrics[ruler_metric_bbt].units_per_pixel = frames_per_unit;
867
868         if (ruler_bbt_action->get_active()) {
869                 gtk_custom_ruler_set_range (GTK_CUSTOM_RULER(_bbt_ruler), leftmost_frame, leftmost_frame+current_page_frames(),
870                                             leftmost_frame, session->current_end_frame());
871         }
872 }
873
874 /* Mark generation */
875
876 gint
877 Editor::_metric_get_smpte (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
878 {
879         return ruler_editor->metric_get_smpte (marks, lower, upper, maxchars);
880 }
881
882 gint
883 Editor::_metric_get_bbt (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
884 {
885         return ruler_editor->metric_get_bbt (marks, lower, upper, maxchars);
886 }
887
888 gint
889 Editor::_metric_get_frames (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
890 {
891         return ruler_editor->metric_get_frames (marks, lower, upper, maxchars);
892 }
893
894 gint
895 Editor::_metric_get_minsec (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
896 {
897         return ruler_editor->metric_get_minsec (marks, lower, upper, maxchars);
898 }
899
900 gint
901 Editor::metric_get_smpte (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
902 {
903         nframes_t pos;
904         nframes64_t range;
905         nframes64_t spacer;
906         nframes64_t fr;
907         SMPTE::Time smpte;
908         gchar buf[16];
909         gint nmarks = 0;
910         gint n;
911         bool show_bits = false;
912         bool show_frames = false;
913         bool show_seconds = false;
914         bool show_minutes = false;
915         bool show_hours = false;
916         int mark_modulo;
917
918         if (session == 0) {
919                 return 0;
920         }
921
922         fr = session->frame_rate();
923
924         if (lower > (spacer = (nframes64_t)(128 * Editor::get_current_zoom ()))) {
925                 lower = lower - spacer;
926         } else {
927                 lower = 0;
928         }
929         upper = upper + spacer;
930         range = (nframes64_t) floor (upper - lower);
931
932         if (range < (2 * session->frames_per_smpte_frame())) { /* 0 - 2 frames */
933                 show_bits = true;
934                 mark_modulo = 20;
935                 nmarks = 1 + (2 * Config->get_subframes_per_frame());
936         } else if (range <= (fr / 4)) { /* 2 frames - 0.250 second */
937                 show_frames = true;
938                 mark_modulo = 1;
939                 nmarks = 1 + (range / (nframes64_t)session->frames_per_smpte_frame());
940         } else if (range <= (fr / 2)) { /* 0.25-0.5 second */
941                 show_frames = true;
942                 mark_modulo = 2;
943                 nmarks = 1 + (range / (nframes64_t)session->frames_per_smpte_frame());
944         } else if (range <= fr) { /* 0.5-1 second */
945                 show_frames = true;
946                 mark_modulo = 5;
947                 nmarks = 1 + (range / (nframes64_t)session->frames_per_smpte_frame());
948         } else if (range <= 2 * fr) { /* 1-2 seconds */
949                 show_frames = true;
950                 mark_modulo = 10;
951                 nmarks = 1 + (range / (nframes64_t)session->frames_per_smpte_frame());
952         } else if (range <= 8 * fr) { /* 2-8 seconds */
953                 show_seconds = true;
954                 mark_modulo = 1;
955                 nmarks = 1 + (range / fr);
956         } else if (range <= 16 * fr) { /* 8-16 seconds */
957                 show_seconds = true;
958                 mark_modulo = 2;
959                 nmarks = 1 + (range / fr);
960         } else if (range <= 30 * fr) { /* 16-30 seconds */
961                 show_seconds = true;
962                 mark_modulo = 5;
963                 nmarks = 1 + (range / fr);
964         } else if (range <= 60 * fr) { /* 30-60 seconds */
965                 show_seconds = true;
966                 mark_modulo = 5;
967                 nmarks = 1 + (range / fr);
968         } else if (range <= 2 * 60 * fr) { /* 1-2 minutes */
969                 show_seconds = true;
970                 mark_modulo = 20;
971                 nmarks = 1 + (range / fr);
972         } else if (range <= 4 * 60 * fr) { /* 2-4 minutes */
973                 show_seconds = true;
974                 mark_modulo = 30;
975                 nmarks = 1 + (range / fr);
976         } else if (range <= 10 * 60 * fr) { /* 4-10 minutes */
977                 show_minutes = true;
978                 mark_modulo = 2;
979                 nmarks = 1 + 10;
980         } else if (range <= 30 * 60 * fr) { /* 10-30 minutes */
981                 show_minutes = true;
982                 mark_modulo = 5;
983                 nmarks = 1 + 30;
984         } else if (range <= 60 * 60 * fr) { /* 30 minutes - 1hr */
985                 show_minutes = true;
986                 mark_modulo = 10;
987                 nmarks = 1 + 60;
988         } else if (range <= 4 * 60 * 60 * fr) { /* 1 - 4 hrs*/
989                 show_minutes = true;
990                 mark_modulo = 30;
991                 nmarks = 1 + (60 * 4);
992         } else if (range <= 8 * 60 * 60 * fr) { /* 4 - 8 hrs*/
993                 show_hours = true;
994                 mark_modulo = 1;
995                 nmarks = 1 + 8;
996         } else if (range <= 16 * 60 * 60 * fr) { /* 16-24 hrs*/
997                 show_hours = true;
998                 mark_modulo = 1;
999                 nmarks = 1 + 24;
1000         } else {
1001     
1002                 /* not possible if nframes64_t is a 32 bit quantity */
1003     
1004                 show_hours = true;
1005                 mark_modulo = 4;
1006                 nmarks = 1 + 24;
1007         }
1008   
1009         pos = (nframes_t) floor (lower);
1010         
1011         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);  
1012         
1013         if (show_bits) {
1014                 // Find smpte time of this sample (pos) with subframe accuracy
1015                 session->sample_to_smpte(pos, smpte, true /* use_offset */, true /* use_subframes */ );
1016     
1017                 for (n = 0; n < nmarks; n++) {
1018                         session->smpte_to_sample(smpte, pos, true /* use_offset */, true /* use_subframes */ );
1019                         if ((smpte.subframes % mark_modulo) == 0) {
1020                                 if (smpte.subframes == 0) {
1021                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1022                                         snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
1023                                 } else {
1024                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1025                                         snprintf (buf, sizeof(buf), ".%02u", smpte.subframes);
1026                                 }
1027                         } else {
1028                                 snprintf (buf, sizeof(buf)," ");
1029                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1030         
1031                         }
1032                         (*marks)[n].label = g_strdup (buf);
1033                         (*marks)[n].position = pos;
1034
1035                         // Increment subframes by one
1036                         SMPTE::increment_subframes( smpte );
1037                 }
1038         } else if (show_seconds) {
1039                 // Find smpte time of this sample (pos)
1040                 session->sample_to_smpte(pos, smpte, true /* use_offset */, false /* use_subframes */ );
1041                 // Go to next whole second down
1042                 SMPTE::seconds_floor( smpte );
1043
1044                 for (n = 0; n < nmarks; n++) {
1045                         session->smpte_to_sample(smpte, pos, true /* use_offset */, false /* use_subframes */ );
1046                         if ((smpte.seconds % mark_modulo) == 0) {
1047                                 if (smpte.seconds == 0) {
1048                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1049                                         (*marks)[n].position = pos;
1050                                 } else {
1051                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1052                                         (*marks)[n].position = pos;
1053                                 }
1054                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
1055                         } else {
1056                                 snprintf (buf, sizeof(buf)," ");
1057                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1058                                 (*marks)[n].position = pos;
1059         
1060                         }
1061                         (*marks)[n].label = g_strdup (buf);
1062                         SMPTE::increment_seconds( smpte );
1063                 }
1064         } else if (show_minutes) {
1065                 // Find smpte time of this sample (pos)
1066                 session->sample_to_smpte(pos, smpte, true /* use_offset */, false /* use_subframes */ );
1067                 // Go to next whole minute down
1068                 SMPTE::minutes_floor( smpte );
1069
1070                 for (n = 0; n < nmarks; n++) {
1071                         session->smpte_to_sample(smpte, pos, true /* use_offset */, false /* use_subframes */ );
1072                         if ((smpte.minutes % mark_modulo) == 0) {
1073                                 if (smpte.minutes == 0) {
1074                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1075                                 } else {
1076                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1077                                 }
1078                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
1079                         } else {
1080                                 snprintf (buf, sizeof(buf)," ");
1081                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1082         
1083                         }
1084                         (*marks)[n].label = g_strdup (buf);
1085                         (*marks)[n].position = pos;
1086                         SMPTE::increment_minutes( smpte );
1087                 }
1088         } else if (show_hours) {
1089                 // Find smpte time of this sample (pos)
1090                 session->sample_to_smpte(pos, smpte, true /* use_offset */, false /* use_subframes */ );
1091                 // Go to next whole hour down
1092                 SMPTE::hours_floor( smpte );
1093
1094                 for (n = 0; n < nmarks; n++) {
1095                         session->smpte_to_sample(smpte, pos, true /* use_offset */, false /* use_subframes */ );
1096                         if ((smpte.hours % mark_modulo) == 0) {
1097                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1098                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
1099                         } else {
1100                                 snprintf (buf, sizeof(buf)," ");
1101                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1102         
1103                         }
1104                         (*marks)[n].label = g_strdup (buf);
1105                         (*marks)[n].position = pos;
1106
1107                         SMPTE::increment_hours( smpte );
1108                 }
1109         } else { // show_frames
1110                 // Find smpte time of this sample (pos)
1111                 session->sample_to_smpte(pos, smpte, true /* use_offset */, false /* use_subframes */ );
1112                 // Go to next whole frame down
1113                 SMPTE::frames_floor( smpte );
1114
1115                 for (n = 0; n < nmarks; n++) {
1116                         session->smpte_to_sample(smpte, pos, true /* use_offset */, false /* use_subframes */ );
1117                         if ((smpte.frames % mark_modulo) == 0)  {
1118                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1119                                 (*marks)[n].position = pos;
1120                                 snprintf (buf, sizeof(buf), "%s%02u:%02u:%02u:%02u", smpte.negative ? "-" : "", smpte.hours, smpte.minutes, smpte.seconds, smpte.frames);
1121                         } else {
1122                                 snprintf (buf, sizeof(buf)," ");
1123                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1124                                 (*marks)[n].position = pos;
1125         
1126                         }
1127                         (*marks)[n].label = g_strdup (buf);
1128                         SMPTE::increment( smpte );
1129                 }
1130         }
1131   
1132         return nmarks;
1133 }
1134
1135
1136 gint
1137 Editor::metric_get_bbt (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
1138 {
1139         if (session == 0) {
1140                 return 0;
1141         }
1142
1143         TempoMap::BBTPointList::iterator i;
1144
1145         uint32_t beats = 0;
1146         uint32_t bars = 0;
1147         uint32_t desirable_marks;
1148         uint32_t magic_accent_number = 1;
1149         gint nmarks;
1150         char buf[64];
1151         gint  n = 0;
1152         nframes64_t pos;
1153         bool bar_helper_on = true;
1154        
1155         BBT_Time next_beat;
1156         nframes64_t next_beat_pos;
1157
1158         if ((desirable_marks = maxchars / 7) == 0) {
1159                return 0;
1160         }
1161
1162         /* align the tick marks to whatever we're snapping to... */
1163
1164         switch (snap_type) {
1165         case SnapToAThirdBeat:
1166                 bbt_beat_subdivision = 3;
1167                 break;
1168         case SnapToAQuarterBeat:
1169                 bbt_beat_subdivision = 4;
1170                 break;
1171         case SnapToAEighthBeat:
1172                 bbt_beat_subdivision = 8;
1173                 magic_accent_number = 2;
1174                 break;
1175         case SnapToASixteenthBeat:
1176                 bbt_beat_subdivision = 16;
1177                 magic_accent_number = 4;
1178                 break;
1179         case SnapToAThirtysecondBeat:
1180                 bbt_beat_subdivision = 32;
1181                 magic_accent_number = 8;
1182                 break;
1183         default:
1184                bbt_beat_subdivision = 4;
1185                 break;
1186         }
1187
1188         if (current_bbt_points == 0 || current_bbt_points->empty()) {
1189                 return 0;
1190         }
1191
1192         i = current_bbt_points->end();
1193         i--;
1194         bars = (*i).bar - (*current_bbt_points->begin()).bar;
1195         beats = current_bbt_points->size() - bars;
1196
1197         /*Only show the bar helper if there aren't many bars on the screen */
1198         if (bars > 1) {
1199                 bar_helper_on = false;
1200         }
1201
1202         if (desirable_marks > (beats / 4)) {
1203
1204                 /* we're in beat land...*/
1205
1206                 uint32_t tick = 0;
1207                 uint32_t skip;
1208                 uint32_t t;
1209                 nframes64_t frame_skip;
1210                 double frame_skip_error;
1211                 double accumulated_error;
1212                 double position_of_helper;
1213                 bool i_am_accented = false;
1214                 bool we_need_ticks = false;
1215                 bool helper_active = false;
1216         
1217                 position_of_helper = lower + (30 * Editor::get_current_zoom ());
1218
1219                 if (desirable_marks >= (beats)) {
1220                         nmarks = (beats * bbt_beat_subdivision) + 1;
1221                         we_need_ticks = true;
1222                 } else {
1223                         nmarks = beats + 1;
1224                 }
1225
1226                 *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1227
1228                 (*marks)[0].label = g_strdup(" ");
1229                 (*marks)[0].position = lower;
1230                 (*marks)[0].style = GtkCustomRulerMarkMicro;
1231                 
1232                 for (n = 1,   i = current_bbt_points->begin(); n < nmarks && i != current_bbt_points->end(); ++i) {
1233
1234                         if ((*i).frame < lower && (bar_helper_on)) {
1235                                         snprintf (buf, sizeof(buf), "<%" PRIu32 "|%" PRIu32, (*i).bar, (*i).beat);
1236                                         (*marks)[0].label = g_strdup (buf); 
1237                                         helper_active = true;
1238                         } else {
1239
1240                           if ((*i).type == TempoMap::Bar)  {
1241                             if (((*i).frame < position_of_helper) && helper_active) {
1242                               snprintf (buf, sizeof(buf), " ");
1243                             } else {
1244                               snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1245                             }
1246                             (*marks)[n].label = g_strdup (buf);
1247                             (*marks)[n].position = (*i).frame;
1248                             (*marks)[n].style = GtkCustomRulerMarkMajor;
1249                             n++;
1250                             
1251                           } else if (((*i).type == TempoMap::Beat) && ((*i).beat > 1)) {
1252                             ((((*i).frame < position_of_helper) && bar_helper_on) || !we_need_ticks) ?
1253                               snprintf (buf, sizeof(buf), " ") : snprintf (buf, sizeof(buf), "%" PRIu32, (*i).beat);
1254                             if (((*i).beat % 2 == 1) || we_need_ticks) {
1255                               (*marks)[n].style = GtkCustomRulerMarkMinor;
1256                             } else {
1257                               (*marks)[n].style = GtkCustomRulerMarkMicro;
1258                             }
1259                             (*marks)[n].label =  g_strdup (buf);
1260                             (*marks)[n].position = (*i).frame;
1261                             n++;
1262                           }
1263
1264                         }
1265
1266
1267                         /* Add the tick marks */
1268
1269                         if (we_need_ticks && (*i).type == TempoMap::Beat) {
1270
1271                                 /* Find the next beat */
1272
1273                                 next_beat.beats = (*i).beat;
1274                                 next_beat.bars = (*i).bar;
1275
1276                                 if ((*i).meter->beats_per_bar() > (next_beat.beats + 1)) {
1277                                   next_beat.beats += 1;
1278                                 } else {
1279                                   next_beat.bars += 1;
1280                                   next_beat.beats = 1;
1281                                 }
1282                                 
1283                                 next_beat_pos = session->tempo_map().frame_time(next_beat);
1284
1285                                 frame_skip = (nframes64_t) floor (frame_skip_error = (session->frame_rate() *  60) / (bbt_beat_subdivision * (*i).tempo->beats_per_minute()));
1286                                 frame_skip_error -= frame_skip;
1287                                 skip = (uint32_t) (Meter::ticks_per_beat / bbt_beat_subdivision);
1288
1289                                 pos = (*i).frame + frame_skip;
1290                                 accumulated_error = frame_skip_error;
1291
1292                                 tick = skip;
1293
1294                                 for (t = 0; (tick < Meter::ticks_per_beat) && (n < nmarks) && (pos < next_beat_pos) ; pos += frame_skip, tick += skip, ++t) {
1295
1296                                         if (t % magic_accent_number == (magic_accent_number - 1)) {
1297                                                 i_am_accented = true;
1298                                         }
1299                                         if (Editor::get_current_zoom () > 32) {
1300                                                 snprintf (buf, sizeof(buf), " ");
1301                                         } else if ((Editor::get_current_zoom () > 8) && !i_am_accented) {
1302                                                 snprintf (buf, sizeof(buf), " ");
1303                                         } else  if (bar_helper_on && (pos < position_of_helper)) {
1304                                                 snprintf (buf, sizeof(buf), " ");
1305                                         } else {
1306                                                 snprintf (buf, sizeof(buf), "%" PRIu32, tick);
1307                                         }
1308
1309                                         (*marks)[n].label = g_strdup (buf);
1310
1311                                         /* Error compensation for float to nframes64_t*/
1312                                         accumulated_error += frame_skip_error;
1313                                         if (accumulated_error > 1) {
1314                                                 pos += 1;
1315                                                 accumulated_error -= 1.0f;
1316                                         }
1317
1318                                         (*marks)[n].position = pos;
1319
1320                                         if ((bbt_beat_subdivision > 4) && i_am_accented) {
1321                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1322                                         } else {
1323                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1324                                         }
1325                                         i_am_accented = false;
1326                                         n++;
1327                                 
1328                                 }
1329                         }
1330                 }
1331                 return n; //return the actual number of marks made, since we might have skipped some fro fractional time signatures 
1332
1333        } else {
1334
1335                 /* we're in bar land */
1336
1337                 if (desirable_marks < (bars / 256)) {
1338                         nmarks = 1;
1339                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1340                         snprintf (buf, sizeof(buf), "too many bars... (currently %" PRIu32 ")", bars );
1341                         (*marks)[0].style = GtkCustomRulerMarkMajor;
1342                         (*marks)[0].label = g_strdup (buf);
1343                         (*marks)[0].position = lower;
1344                 } else if (desirable_marks < (uint32_t)(nmarks = (gint) (bars / 64) + 1)) {
1345                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1346                         for (n = 0,   i = current_bbt_points->begin(); i != current_bbt_points->end() && n < nmarks; i++) {
1347                                 if ((*i).type == TempoMap::Bar)  {
1348                                         if ((*i).bar % 64 == 1) {
1349                                                 if ((*i).bar % 256 == 1) {
1350                                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1351                                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1352                                                 } else {
1353                                                         snprintf (buf, sizeof(buf), " ");
1354                                                         if ((*i).bar % 256 == 129)  {
1355                                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1356                                                         } else {
1357                                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1358                                                         }
1359                                                 }
1360                                                 (*marks)[n].label = g_strdup (buf);
1361                                                 (*marks)[n].position = (*i).frame;
1362                                                 n++;
1363                                         }
1364                                 }
1365                         }
1366                 } else if (desirable_marks < (uint32_t)(nmarks = (bars / 16) + 1)) {
1367                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1368                         for (n = 0,  i = current_bbt_points->begin(); i != current_bbt_points->end() && n < nmarks; i++) {
1369                                 if ((*i).type == TempoMap::Bar)  {
1370                                         if ((*i).bar % 16 == 1) {
1371                                                 if ((*i).bar % 64 == 1) {
1372                                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1373                                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1374                                                 } else {
1375                                                         snprintf (buf, sizeof(buf), " ");
1376                                                         if ((*i).bar % 64 == 33)  {
1377                                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1378                                                         } else {
1379                                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1380                                                         }
1381                                                 }
1382                                                 (*marks)[n].label = g_strdup (buf);
1383                                                 (*marks)[n].position = (*i).frame;
1384                                                 n++;
1385                                         }
1386                                 }
1387                         }
1388                 } else if (desirable_marks < (uint32_t)(nmarks = (bars / 4) + 1)){
1389                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1390                         for (n = 0,   i = current_bbt_points->begin(); i != current_bbt_points->end() && n < nmarks; ++i) {
1391                                 if ((*i).type == TempoMap::Bar)  {
1392                                         if ((*i).bar % 4 == 1) {
1393                                                 if ((*i).bar % 16 == 1) {
1394                                                         snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1395                                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1396                                                 } else {
1397                                                         snprintf (buf, sizeof(buf), " ");
1398                                                         if ((*i).bar % 16 == 9)  {
1399                                                                 (*marks)[n].style = GtkCustomRulerMarkMinor;
1400                                                         } else {
1401                                                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1402                                                         }
1403                                                 }
1404                                                 (*marks)[n].label = g_strdup (buf);
1405                                                 (*marks)[n].position = (*i).frame;
1406                                                 n++;
1407                                         }
1408                                 }
1409                         }
1410                 } else {
1411                         nmarks = bars + 1;
1412                         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks );
1413                         for (n = 0,  i = current_bbt_points->begin(); i != current_bbt_points->end() && n < nmarks; i++) {
1414                                 if ((*i).type == TempoMap::Bar)  {
1415                                         if ((*i).bar % 4 == 1) {
1416                                                 snprintf (buf, sizeof(buf), "%" PRIu32, (*i).bar);
1417                                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1418                                         } else {
1419                                                 snprintf (buf, sizeof(buf), " ");
1420                                                 if ((*i).bar % 4 == 3)  {
1421                                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1422                                                 } else {
1423                                                         (*marks)[n].style = GtkCustomRulerMarkMicro;
1424                                                 }
1425                                         }
1426                                         (*marks)[n].label = g_strdup (buf);
1427                                         (*marks)[n].position = (*i).frame;
1428                                         n++;
1429                                 }
1430                         }
1431                 }
1432                 return n;
1433         }
1434 }
1435
1436 gint
1437 Editor::metric_get_frames (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
1438 {
1439         nframes64_t mark_interval;
1440         nframes64_t pos;
1441         nframes64_t ilower = (nframes64_t) floor (lower);
1442         nframes64_t iupper = (nframes64_t) floor (upper);
1443         gchar buf[16];
1444         gint nmarks;
1445         gint n;
1446
1447         if (session == 0) {
1448                 return 0;
1449         }
1450
1451         mark_interval = (iupper - ilower) / 5;
1452         if (mark_interval > session->frame_rate()) {
1453                 mark_interval -= mark_interval % session->frame_rate();
1454         } else {
1455                 mark_interval = session->frame_rate() / (session->frame_rate() / mark_interval ) ;
1456         }
1457         nmarks = 5;
1458         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1459         for (n = 0, pos = ilower; n < nmarks; pos += mark_interval, ++n) {
1460                 snprintf (buf, sizeof(buf), "%" PRIi64,  pos);
1461                 (*marks)[n].label = g_strdup (buf);
1462                 (*marks)[n].position = pos;
1463                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1464         }
1465         
1466         return nmarks;
1467 }
1468
1469 static void
1470 sample_to_clock_parts ( nframes64_t sample,
1471                         nframes64_t sample_rate, 
1472                         long *hrs_p,
1473                         long *mins_p,
1474                         long *secs_p,
1475                         long *millisecs_p)
1476
1477 {
1478         nframes64_t left;
1479         long hrs;
1480         long mins;
1481         long secs;
1482         long millisecs;
1483         
1484         left = sample;
1485         hrs = left / (sample_rate * 60 * 60);
1486         left -= hrs * sample_rate * 60 * 60;
1487         mins = left / (sample_rate * 60);
1488         left -= mins * sample_rate * 60;
1489         secs = left / sample_rate;
1490         left -= secs * sample_rate;
1491         millisecs = left * 1000 / sample_rate;
1492
1493         *millisecs_p = millisecs;
1494         *secs_p = secs;
1495         *mins_p = mins;
1496         *hrs_p = hrs;
1497
1498         return;
1499 }
1500
1501 gint
1502 Editor::metric_get_minsec (GtkCustomRulerMark **marks, gdouble lower, gdouble upper, gint maxchars)
1503 {
1504         nframes64_t range;
1505         nframes64_t fr;
1506         nframes64_t mark_interval;
1507         nframes64_t pos;
1508         nframes64_t spacer;
1509         long hrs, mins, secs, millisecs;
1510         gchar buf[16];
1511         gint nmarks;
1512         gint n;
1513         gint mark_modulo = 100;
1514         bool show_seconds = false;
1515         bool show_minutes = false;
1516         bool show_hours = false;
1517         nframes64_t ilower = (nframes64_t) floor (lower);
1518         nframes64_t iupper = (nframes64_t) floor (upper);
1519
1520         if (session == 0) {
1521                 return 0;
1522         }
1523
1524         fr = session->frame_rate();
1525
1526         /* to prevent 'flashing' */
1527         if (lower > (spacer = (nframes64_t)(128 * Editor::get_current_zoom ()))) {
1528                 lower = lower - spacer;
1529         } else {
1530                 lower = 0;
1531         }
1532         upper = upper + spacer;
1533         range = iupper - ilower;
1534
1535         if (range <  (fr / 50)) {
1536                 mark_interval =  fr / 100; /* show 1/100 seconds */
1537                 mark_modulo = 10;
1538         } else if (range <= (fr / 10)) { /* 0-0.1 second */
1539                 mark_interval = fr / 50; /* show 1/50 seconds */
1540                 mark_modulo = 20;
1541         } else if (range <= (fr / 2)) { /* 0-0.5 second */
1542                 mark_interval = fr / 20;  /* show 1/20 seconds */
1543                 mark_modulo = 100;
1544         } else if (range <= fr) { /* 0-1 second */
1545                 mark_interval = fr / 10;  /* show 1/10 seconds */
1546                 mark_modulo = 200;
1547         } else if (range <= 2 * fr) { /* 1-2 seconds */
1548                 mark_interval = fr / 2; /* show 1/2 seconds */
1549                 mark_modulo = 500;
1550         } else if (range <= 8 * fr) { /* 2-5 seconds */
1551                 mark_interval =  fr / 5; /* show 2 seconds */
1552                 mark_modulo = 1000;
1553         } else if (range <= 16 * fr) { /* 8-16 seconds */
1554                 mark_interval =  fr; /* show 1 seconds */
1555                 show_seconds = true;
1556                 mark_modulo = 5;
1557         } else if (range <= 30 * fr) { /* 10-30 seconds */
1558                 mark_interval =  fr; /* show 10 seconds */
1559                 show_seconds = true;
1560                 mark_modulo = 5;
1561         } else if (range <= 60 * fr) { /* 30-60 seconds */
1562                 mark_interval = 5 * fr; /* show 5 seconds */
1563                 show_seconds = true;
1564                 mark_modulo = 3;
1565         } else if (range <= 2 * 60 * fr) { /* 1-2 minutes */
1566                 mark_interval = 5 * fr; /* show 5 seconds */
1567                 show_seconds = true;
1568                 mark_modulo = 3;
1569         } else if (range <= 4 * 60 * fr) { /* 4 minutes */
1570                 mark_interval = 10 * fr; /* show 10 seconds */
1571                 show_seconds = true;
1572                 mark_modulo = 30;
1573         } else if (range <= 10 * 60 * fr) { /* 10 minutes */
1574                 mark_interval = 30 * fr; /* show 30 seconds */
1575                 show_seconds = true;
1576                 mark_modulo = 60;
1577         } else if (range <= 30 * 60 * fr) { /* 10-30 minutes */
1578                 mark_interval =  60 * fr; /* show 1 minute */
1579                 show_minutes = true;
1580                 mark_modulo = 5;
1581         } else if (range <= 60 * 60 * fr) { /* 30 minutes - 1hr */
1582                 mark_interval = 2 * 60 * fr; /* show 2 minutes */
1583                 show_minutes = true;
1584                 mark_modulo = 10;
1585         } else if (range <= 4 * 60 * 60 * fr) { /* 1 - 4 hrs*/
1586                 mark_interval = 5 * 60 * fr; /* show 10 minutes */
1587                 show_minutes = true;
1588                 mark_modulo = 30;
1589         } else if (range <= 8 * 60 * 60 * fr) { /* 4 - 8 hrs*/
1590                 mark_interval = 20 * 60 * fr; /* show 20 minutes */
1591                 show_minutes = true;
1592                 mark_modulo = 60;
1593         } else if (range <= 16 * 60 * 60 * fr) { /* 16-24 hrs*/
1594                 mark_interval =  60 * 60 * fr; /* show 60 minutes */
1595                 show_hours = true;
1596                 mark_modulo = 2;
1597         } else {
1598                                                                                                                    
1599                 /* not possible if nframes64_t is a 32 bit quantity */
1600                                                                                                                    
1601                 mark_interval = 4 * 60 * 60 * fr; /* show 4 hrs */
1602         }
1603
1604         nmarks = 1 + (range / mark_interval);
1605         *marks = (GtkCustomRulerMark *) g_malloc (sizeof(GtkCustomRulerMark) * nmarks);
1606         pos = ((ilower + (mark_interval/2))/mark_interval) * mark_interval;
1607         
1608         if (show_seconds) {
1609                 for (n = 0; n < nmarks; pos += mark_interval, ++n) {
1610                         sample_to_clock_parts (pos, fr, &hrs, &mins, &secs, &millisecs);
1611                         if (secs % mark_modulo == 0) {
1612                                 if (secs == 0) {
1613                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1614                                 } else {
1615                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1616                                 }
1617                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1618                         } else {
1619                                 snprintf (buf, sizeof(buf), " ");
1620                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1621                         }
1622                         (*marks)[n].label = g_strdup (buf);
1623                         (*marks)[n].position = pos;
1624                 }
1625         } else if (show_minutes) {
1626                 for (n = 0; n < nmarks; pos += mark_interval, ++n) {
1627                         sample_to_clock_parts (pos, fr, &hrs, &mins, &secs, &millisecs);
1628                         if (mins % mark_modulo == 0) {
1629                                 if (mins == 0) {
1630                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1631                                 } else {
1632                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1633                                 }
1634                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1635                         } else {
1636                                 snprintf (buf, sizeof(buf), " ");
1637                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1638                         }
1639                         (*marks)[n].label = g_strdup (buf);
1640                         (*marks)[n].position = pos;
1641                 }
1642         } else if (show_hours) {
1643                  for (n = 0; n < nmarks; pos += mark_interval, ++n) {
1644                         sample_to_clock_parts (pos, fr, &hrs, &mins, &secs, &millisecs);
1645                         if (hrs % mark_modulo == 0) {
1646                                 (*marks)[n].style = GtkCustomRulerMarkMajor;
1647                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1648                         } else {
1649                                 snprintf (buf, sizeof(buf), " ");
1650                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1651                         }
1652                         (*marks)[n].label = g_strdup (buf);
1653                         (*marks)[n].position = pos;
1654                 }
1655         } else {
1656                 for (n = 0; n < nmarks; pos += mark_interval, ++n) {
1657                         sample_to_clock_parts (pos, fr, &hrs, &mins, &secs, &millisecs);
1658                         if (millisecs % mark_modulo == 0) {
1659                                 if (millisecs == 0) {
1660                                         (*marks)[n].style = GtkCustomRulerMarkMajor;
1661                                 } else {
1662                                         (*marks)[n].style = GtkCustomRulerMarkMinor;
1663                                 }
1664                                 snprintf (buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", hrs, mins, secs, millisecs);
1665                         } else {
1666                                 snprintf (buf, sizeof(buf), " ");
1667                                 (*marks)[n].style = GtkCustomRulerMarkMicro;
1668                         }
1669                         (*marks)[n].label = g_strdup (buf);
1670                         (*marks)[n].position = pos;
1671                 }
1672         }
1673
1674         return nmarks;
1675 }