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