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