diff --git a/Timeline.C b/Timeline.C index d5dd482..044353e 100644 --- a/Timeline.C +++ b/Timeline.C @@ -190,6 +190,23 @@ Timeline::beats_per_minute ( nframes_t when, float bpm ) tempo_track->add( new Tempo_Point( when, bpm ) ); } +/** return the absolute pixel of the nearest measure line to /x/ */ +int +Timeline::nearest_line ( int ix ) +{ + for ( int x = ix - 10; x < ix + 10; ++x ) + { + const int measure = ts_to_x( (double)(sample_rate * 60) / beats_per_minute( x_to_ts( x ) + xoffset )); + +// const int abs_x = ts_to_x( xoffset ) + x; + + if ( 0 == x % measure ) + return x; + } + + return -1; +} + /* draw appropriate measure lines inside the given bounding box */ void Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color ) diff --git a/Timeline.H b/Timeline.H index 45261c5..e8297cf 100644 --- a/Timeline.H +++ b/Timeline.H @@ -106,6 +106,8 @@ struct Timeline : public Fl_Group float beats_per_minute ( nframes_t when ) const; int beats_per_bar ( nframes_t when ) const; void beats_per_minute ( nframes_t when, float bpm ); + int nearest_line ( int ix ); + void draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color ); void position ( int X ); void draw ( void ); diff --git a/Track.C b/Track.C index 74a4098..a6193db 100644 --- a/Track.C +++ b/Track.C @@ -119,6 +119,14 @@ Track::snap ( Track_Widget *r ) int rx1 = r->x(); int rx2 = r->x() + r->w(); + int nx = timeline->nearest_line( r->abs_x() ); + + if ( nx >= 0 ) + { + r->offset( timeline->x_to_ts( nx ) ); + return; + } + for ( list ::iterator i = _widgets.begin(); i != _widgets.end(); i++ ) { const Track_Widget *w = (*i);