non/Timeline/Timeline.C

1400 lines
33 KiB
C++
Raw Normal View History

/*******************************************************************************/
/* Copyright (C) 2008 Jonathan Moore Liles */
/* */
/* This program is free software; you can redistribute it and/or modify it */
/* under the terms of the GNU General Public License as published by the */
/* Free Software Foundation; either version 2 of the License, or (at your */
/* option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
/* more details. */
/* */
/* You should have received a copy of the GNU General Public License along */
/* with This program; see the file COPYING. If not,write to the Free Software */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/*******************************************************************************/
2008-06-21 06:38:29 +02:00
/* This is the Timeline widget, which contains all the tracks and
* provides cursor overlays, scrolling, zooming, measure lines, tempo
* map and just about everything else. */
2008-05-18 07:09:18 +02:00
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Pack.H>
#include <FL/Fl_Scrollbar.H>
#include <FL/Fl_Widget.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Menu_Button.H>
#include "Timeline.H"
2008-04-19 06:16:21 +02:00
#include "Tempo_Sequence.H"
#include "Time_Sequence.H"
#include "Audio_Sequence.H"
#include "Control_Sequence.H"
2008-05-18 02:15:39 +02:00
#include "Scalebar.H"
2008-05-18 07:09:18 +02:00
#include "Sequence.H"
2008-05-05 00:32:08 +02:00
#include "Annotation_Sequence.H"
2008-04-19 06:16:21 +02:00
#include "Track.H"
2008-05-22 05:05:10 +02:00
#include "Transport.H"
#include "Engine/Engine.H" // for lock()
#include "FL/menu_popup.H"
2008-05-22 05:05:10 +02:00
2008-04-25 04:48:44 +02:00
bool Timeline::draw_with_measure_lines = true;
2008-04-26 18:07:43 +02:00
Timeline::snap_e Timeline::snap_to = Bars;
2008-04-27 20:56:20 +02:00
bool Timeline::snap_magnetic = true;
2008-04-30 03:08:49 +02:00
bool Timeline::follow_playhead = true;
bool Timeline::center_playhead = true;
2008-04-25 04:48:44 +02:00
2008-04-12 21:50:36 +02:00
const float UPDATE_FREQ = 0.02f;
2008-05-22 05:05:10 +02:00
2008-04-12 21:50:36 +02:00
2008-04-28 07:11:47 +02:00
/** return the combined height of all visible children of (veritcal)
2008-05-13 06:38:05 +02:00
pack, /p/. This is necessary because pack sizes are adjusted only
when the relevant areas are exposes. */
2008-04-28 07:11:47 +02:00
static int
pack_visible_height ( const Fl_Pack *p )
{
int th = 0;
2008-04-28 18:48:35 +02:00
const Fl_Widget* const *w = p->array();
for ( int i = p->children(); i--; ++w )
2008-04-28 07:11:47 +02:00
if ( (*w)->visible() )
th += (*w)->h() + p->spacing();
return th;
}
2008-05-22 04:29:06 +02:00
#define BP fl_begin_polygon()
#define EP fl_end_polygon()
#define vv(x,y) fl_vertex( x, y )
#define BL fl_begin_line()
#define EL fl_end_line()
static void
draw_full_arrow_symbol ( Fl_Color color )
{
/* draw cap */
fl_color( color );
BP;
vv( -1, -1 );
vv( 0, 1 );
vv( 1, -1 );
EP;
/* draw cap outline */
fl_color( FL_BLACK );
BL;
vv( -1, -1 );
vv( 0, 1 );
vv( 1, -1 );
EL;
}
2008-05-22 05:05:10 +02:00
2008-04-28 07:11:47 +02:00
/** recalculate the size of vertical scrolling area and inform scrollbar */
void
Timeline::adjust_vscroll ( void )
{
vscroll->value( _yposition, h() - rulers->h() - hscroll->h(), 0, pack_visible_height( tracks ) );
}
2008-06-19 06:14:14 +02:00
/** recalculate the size of horizontal scrolling area and inform scrollbar */
2008-05-28 01:47:47 +02:00
void
Timeline::adjust_hscroll ( void )
{
hscroll->value( ts_to_x( xoffset ), tracks->w() - Track::width(), 0, ts_to_x( length() ) );
2008-05-28 01:47:47 +02:00
}
2008-02-26 05:58:15 +01:00
void
2008-03-06 23:45:23 +01:00
Timeline::cb_scroll ( Fl_Widget *w, void *v )
2008-02-26 05:58:15 +01:00
{
2008-03-06 23:45:23 +01:00
((Timeline*)v)->cb_scroll( w );
}
2008-02-26 05:58:15 +01:00
2008-03-06 23:45:23 +01:00
void
Timeline::cb_scroll ( Fl_Widget *w )
{
if ( w == vscroll )
2008-02-26 05:58:15 +01:00
{
2008-03-06 23:45:23 +01:00
tracks->position( tracks->x(), (rulers->y() + rulers->h()) - vscroll->value() );
2008-02-26 05:58:15 +01:00
2008-03-07 00:29:28 +01:00
yposition( vscroll->value() );
2008-02-26 05:58:15 +01:00
2008-04-28 07:11:47 +02:00
adjust_vscroll();
2008-02-26 05:58:15 +01:00
}
else
{
2008-03-06 23:45:23 +01:00
if ( hscroll->zoom_changed() )
{
2008-05-03 04:17:34 +02:00
nframes_t under_mouse = x_to_offset( Fl::event_x() );
2008-04-27 17:45:49 +02:00
_fpp = hscroll->zoom();
2008-02-27 21:04:17 +01:00
2008-05-03 04:17:34 +02:00
const int tw = tracks->w() - Track::width();
// hscroll->value( ts_to_x( xoffset ), tw, 0, ts_to_x( length() ) );
2008-05-28 01:47:47 +02:00
2008-05-03 04:17:34 +02:00
hscroll->value( max( 0, ts_to_x( under_mouse ) - ( Fl::event_x() - tracks->x() - Track::width() ) ),
tw, 0, ts_to_x( length() ) );
2008-02-26 05:58:15 +01:00
2008-03-06 23:45:23 +01:00
redraw();
}
2008-04-27 15:37:50 +02:00
xposition( hscroll->value() );
2008-03-06 23:45:23 +01:00
}
2008-02-26 05:58:15 +01:00
}
void
Timeline::menu_cb ( Fl_Widget *w, void *v )
{
((Timeline*)v)->menu_cb( (Fl_Menu_*)w );
}
2008-06-19 06:14:14 +02:00
/** ensure that p1 is less than p2 */
2008-06-01 03:54:01 +02:00
void
Timeline::fix_range ( void )
{
if ( p1 > p2 )
{
nframes_t t = p2;
p2 = p1;
p1 = t;
}
}
void
Timeline::menu_cb ( Fl_Menu_ *m )
{
if ( ! active_r() )
return;
const char *picked = m->mvalue()->label();
/* m->item_pathname( picked, sizeof( picked ) ); */
DMESSAGE( "%s", picked );
if ( ! strcmp( picked, "Add Audio Track" ) )
{
/* FIXME: prompt for I/O config? */
Loggable::block_start();
/* add audio track */
char *name = get_unique_track_name( "Audio" );
Track *t = new Track( name );
Audio_Sequence *o = new Audio_Sequence( t );
add_track( t );
t->sequence( o );
Loggable::block_end();
}
2008-06-01 03:54:01 +02:00
else if ( ! strcmp( picked, "Tempo from range (beat)" ) )
{
if ( p1 != p2 )
{
2008-06-01 03:54:01 +02:00
fix_range();
beats_per_minute( p1, sample_rate() * 60 / (float)( p2 - p1 ) );
p2 = p1;
}
}
2008-06-01 03:54:01 +02:00
else if ( ! strcmp( picked, "Tempo from range (bar)" ) )
{
if ( p1 != p2 )
{
fix_range();
position_info pi = solve_tempomap( p1 );
beats_per_minute( p1, sample_rate() * 60 / (float)( ( p2 - p1 ) / pi.beats_per_bar ) );
p2 = p1;
}
}
else if ( ! strcmp( picked, "Playhead to mouse" ) )
{
2008-05-29 03:31:06 +02:00
int X = Fl::event_x() - Track::width();
2008-05-29 03:31:06 +02:00
if ( X > 0 )
{
transport->locate( xoffset + x_to_ts( X ) );
}
}
else if ( ! strcmp( picked, "P1 to mouse" ) )
{
int X = Fl::event_x() - Track::width();
if ( X > 0 )
{
p1 = xoffset + x_to_ts( X );
2008-05-29 03:31:06 +02:00
}
/* FIXME: only needs to damage the location of the old cursor! */
redraw();
}
else if ( ! strcmp( picked, "P2 to mouse" ) )
{
int X = Fl::event_x() - Track::width();
if ( X > 0 )
{
p2 = xoffset + x_to_ts( X );
}
/* FIXME: only needs to damage the location of the old cursor! */
redraw();
}
2008-05-30 04:28:26 +02:00
else if ( ! strcmp( picked, "Playhead left beat" ) )
2008-05-29 03:31:06 +02:00
{
2008-05-30 00:45:04 +02:00
nframes_t f = transport->frame;
if ( prev_line( &f ) )
transport->locate( f );
2008-05-29 03:31:06 +02:00
}
2008-05-30 04:28:26 +02:00
else if ( ! strcmp( picked, "Playhead right beat" ) )
2008-05-29 03:31:06 +02:00
{
2008-05-30 00:45:04 +02:00
nframes_t f = transport->frame;
if ( next_line( &f ) )
transport->locate( f );
2008-05-29 03:31:06 +02:00
}
2008-05-30 04:28:26 +02:00
else if ( ! strcmp( picked, "Playhead left bar" ) )
{
nframes_t f = transport->frame;
if ( prev_line( &f, true ) )
transport->locate( f );
}
else if ( ! strcmp( picked, "Playhead right bar" ) )
{
nframes_t f = transport->frame;
if ( next_line( &f, true ) )
transport->locate( f );
}
2008-05-29 03:31:06 +02:00
else if ( ! strcmp( picked, "Swap P1 and playhead" ) )
{
nframes_t t = transport->frame;
transport->locate( p1 );
p1 = t;
redraw();
}
2008-05-30 04:28:26 +02:00
else if ( ! strcmp( picked, "Swap P2 and playhead" ) )
{
nframes_t t = transport->frame;
transport->locate( p2 );
p2 = t;
redraw();
}
else if ( ! strcmp( picked, "P1 to playhead" ) )
{
p1 = transport->frame;
redraw();
}
else if ( ! strcmp( picked, "P2 to playhead" ) )
{
p2 = transport->frame;
redraw();
}
else
WARNING( "programming error: Unknown menu item" );
}
2008-02-26 05:58:15 +01:00
int
Timeline::ntracks ( void ) const
{
return tracks->children();
}
2008-03-01 05:57:53 +01:00
Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Window( X, Y, W, H, L )
2008-02-21 17:20:36 +01:00
{
_sample_rate = 0;
2008-02-23 07:59:10 +01:00
box( FL_FLAT_BOX );
2008-02-23 07:59:10 +01:00
xoffset = 0;
2008-04-28 18:48:35 +02:00
_yposition = 0;
_old_yposition = 0;
_old_xposition = 0;
2008-02-23 07:59:10 +01:00
2008-03-01 05:57:53 +01:00
X = Y = 0;
2008-04-23 04:22:46 +02:00
p1 = p2 = 0;
menu = new Fl_Menu_Button( 0, 0, 0, 0, "Timeline" );
/* menu->add( "Add Track", 0, 0, 0 ); */
menu->add( "Add Audio Track", 'a', 0, 0 );
2008-06-01 03:54:01 +02:00
menu->add( "Tempo from range (beat)", 't', 0, 0 );
menu->add( "Tempo from range (bar)", FL_CTRL + 't', 0, 0 );
menu->add( "Playhead to mouse", 'p', 0, 0 );
menu->add( "P1 to mouse", '[', 0, 0 );
menu->add( "P2 to mouse", ']', 0, 0 );
menu->add( "Playhead left beat", FL_SHIFT + FL_Left, 0, 0 );
menu->add( "Playhead right beat", FL_SHIFT + FL_Right, 0, 0 );
menu->add( "Playhead left bar", FL_CTRL + FL_SHIFT + FL_Left, 0, 0 );
menu->add( "Playhead right bar", FL_CTRL + FL_SHIFT + FL_Right, 0, 0 );
menu->add( "Swap P1 and playhead", FL_CTRL + FL_SHIFT + '[', 0, 0 );
menu->add( "Swap P2 and playhead", FL_CTRL + FL_SHIFT + ']', 0, 0 );
menu->add( "P1 to playhead", FL_CTRL + '[', 0, 0 );
menu->add( "P2 to playhead", FL_CTRL + ']', 0, 0 );
menu_set_callback( const_cast<Fl_Menu_Item*>(menu->menu()), &Timeline::menu_cb, (void*)this );
2008-02-21 17:20:36 +01:00
{
2008-02-28 21:10:41 +01:00
Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
2008-02-26 05:58:15 +01:00
2008-03-05 03:12:54 +01:00
o->range( 0, 48000 * 300 );
2008-04-27 15:37:50 +02:00
// o->zoom_range( 1, 16384 );
2008-04-27 17:45:49 +02:00
// o->zoom_range( 1, 65536 << 4 );
o->zoom_range( 1, 20 );
o->zoom( 8 );
2008-02-26 05:58:15 +01:00
o->type( FL_HORIZONTAL );
2008-03-06 23:45:23 +01:00
o->callback( cb_scroll, this );
2008-02-26 05:58:15 +01:00
hscroll = o;
}
{
2008-02-28 21:10:41 +01:00
Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
2008-02-26 05:58:15 +01:00
o->type( FL_VERTICAL );
2008-03-06 23:45:23 +01:00
o->callback( cb_scroll, this );
2008-02-26 05:58:15 +01:00
vscroll = o;
}
{
2008-04-19 06:16:21 +02:00
Fl_Pack *o = new Fl_Pack( X + Track::width(), Y, (W - Track::width()) - vscroll->w(), H - hscroll->h(), "rulers" );
2008-02-21 17:20:36 +01:00
o->type( Fl_Pack::VERTICAL );
{
2008-04-19 06:16:21 +02:00
Tempo_Sequence *o = new Tempo_Sequence( 0, 0, 800, 24 );
2008-02-21 17:20:36 +01:00
2008-04-27 12:33:26 +02:00
o->color( fl_gray_ramp( 18 ) );
2008-02-21 17:20:36 +01:00
o->label( "Tempo" );
o->align( FL_ALIGN_LEFT );
2008-02-21 17:20:36 +01:00
tempo_track = o;
}
{
2008-04-19 06:16:21 +02:00
Time_Sequence *o = new Time_Sequence( 0, 24, 800, 24 );
2008-02-21 17:20:36 +01:00
2008-04-27 12:33:26 +02:00
o->color( fl_gray_ramp( 16 ) );
2008-02-21 17:20:36 +01:00
o->label( "Time" );
o->align( FL_ALIGN_LEFT );
2008-02-21 17:20:36 +01:00
time_track = o;
}
/* { */
/* Annotation_Sequence *o = new Annotation_Sequence( 0, 24, 800, 24 ); */
/* o->color( fl_gray_ramp( 'F' ) ); */
/* o->label( "Ruler" ); */
/* o->align( FL_ALIGN_LEFT ); */
/* ruler_track = o; */
/* } */
2008-02-26 05:58:15 +01:00
o->size( o->w(), o->child( 0 )->h() * o->children() );
2008-02-21 17:20:36 +01:00
rulers = o;
o->end();
}
2008-02-26 05:58:15 +01:00
2008-02-21 17:20:36 +01:00
{
2008-04-27 14:04:37 +02:00
// sample_rate() = engine->sample_rate();
2008-04-27 17:45:49 +02:00
_fpp = 8;
// length() = sample_rate() * 60 * 2;
2008-05-28 01:47:47 +02:00
2008-04-27 14:04:37 +02:00
/* FIXME: hack */
// length() = x_to_ts( W );
2008-02-21 17:20:36 +01:00
{
Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 1 );
2008-02-21 17:20:36 +01:00
o->type( Fl_Pack::VERTICAL );
o->spacing( 1 );
2008-02-21 17:20:36 +01:00
tracks = o;
o->end();
2008-04-23 04:22:46 +02:00
resizable( o );
2008-02-21 17:20:36 +01:00
}
}
2008-04-26 14:37:25 +02:00
/* rulers go above tracks... */
add( rulers );
2008-04-12 21:50:36 +02:00
/* make sure scrollbars are on top */
add( vscroll );
add( hscroll );
2008-02-26 05:58:15 +01:00
vscroll->range( 0, tracks->h() );
2008-02-23 07:59:10 +01:00
redraw();
2008-02-21 17:20:36 +01:00
end();
2008-04-12 21:50:36 +02:00
Fl::add_timeout( UPDATE_FREQ, update_cb, this );
2008-02-21 17:20:36 +01:00
}
void
Timeline::beats_per_minute ( nframes_t when, float bpm )
{
tempo_track->add( new Tempo_Point( when, bpm ) );
}
void
Timeline::time ( nframes_t when, int bpb, int note_type )
{
time_track->add( new Time_Point( when, bpb, note_type ) );
}
/************/
/* Snapping */
/************/
2008-05-13 06:38:05 +02:00
struct nearest_line_arg
{
nframes_t original;
nframes_t closest;
2008-05-30 04:28:26 +02:00
bool bar;
2008-05-13 06:38:05 +02:00
};
2008-05-13 06:38:05 +02:00
const int snap_pixel = 10;
2008-05-13 06:38:05 +02:00
static nframes_t
abs_diff ( nframes_t n1, nframes_t n2 )
{
return n1 > n2 ? n1 - n2 : n2 - n1;
}
2008-05-30 00:45:04 +02:00
static void
2008-07-02 05:19:31 +02:00
nearest_line_snap_cb ( nframes_t frame, const BBT &bbt, void *arg )
{
2008-05-13 06:38:05 +02:00
nearest_line_arg *n = (nearest_line_arg *)arg;
2008-05-30 04:28:26 +02:00
if ( n->bar && bbt.beat )
return;
2008-05-13 06:38:05 +02:00
if ( Timeline::snap_magnetic &&
abs_diff( frame, n->original ) > timeline->x_to_ts( snap_pixel ) )
return;
if ( abs_diff( frame, n->original ) < abs_diff( n->original, n->closest ) )
n->closest = frame;
}
2008-07-02 05:19:31 +02:00
static void
nearest_line_cb ( nframes_t frame, const BBT &bbt, void *arg )
{
nearest_line_arg *n = (nearest_line_arg *)arg;
if ( n->bar && bbt.beat )
return;
if ( abs_diff( frame, n->original ) < abs_diff( n->original, n->closest ) )
n->closest = frame;
}
2008-05-30 00:45:04 +02:00
static void
2008-05-30 04:28:26 +02:00
prev_next_line_cb ( nframes_t frame, const BBT &bbt, void *arg )
2008-05-30 00:45:04 +02:00
{
nearest_line_arg *n = (nearest_line_arg *)arg;
2008-05-30 04:28:26 +02:00
if ( n->bar && bbt.beat )
return;
2008-05-30 00:45:04 +02:00
if ( abs_diff( frame, n->original ) < abs_diff( n->original, n->closest ) )
n->closest = frame;
}
/** Set the value pointed to by /frame/ to the frame number of the of
the nearest measure line to /when/. Returns true if the new value of
*frame is valid, false otherwise. */
bool
2008-07-02 05:19:31 +02:00
Timeline::nearest_line ( nframes_t *frame, bool snap ) const
2008-02-28 21:36:46 +01:00
{
2008-07-02 05:19:31 +02:00
if ( snap && None == Timeline::snap_to )
return false;
2008-04-26 18:07:43 +02:00
2008-05-29 03:31:06 +02:00
nframes_t when = *frame;
2008-07-02 05:19:31 +02:00
nearest_line_arg n = { when, -1, snap && Timeline::Bars == Timeline::snap_to };
2008-04-27 18:01:55 +02:00
2008-06-01 03:13:18 +02:00
render_tempomap( when > x_to_ts( w() >> 1 ) ? when - x_to_ts( w() >> 1 ) : 0,
2008-07-02 05:19:31 +02:00
when + x_to_ts( w() >> 1 ), snap ? nearest_line_snap_cb : nearest_line_cb, &n );
2008-05-13 06:38:05 +02:00
2008-05-29 03:31:06 +02:00
if ( n.closest == (nframes_t)-1 )
return false;
else
{
*frame = n.closest;
return true;
}
}
2008-04-27 15:37:50 +02:00
2008-05-30 00:45:04 +02:00
/** Set the value pointed to by /frame/ to the frame number of the of
the nearest measure line to *greater than* /when/. Returns true if
the new value of *frame is valid, false otherwise. */
bool
2008-05-30 04:28:26 +02:00
Timeline::next_line ( nframes_t *frame, bool bar ) const
2008-05-30 00:45:04 +02:00
{
nframes_t when = *frame + 1;
2008-05-30 04:28:26 +02:00
nearest_line_arg n = { when, -1, bar };
2008-05-30 00:45:04 +02:00
render_tempomap( when, x_to_ts( w() ), prev_next_line_cb, &n );
if ( n.closest == (nframes_t)-1 )
return false;
else
{
*frame = n.closest;
return true;
}
}
/** Set the value pointed to by /frame/ to the frame number of the of
the nearest measure line to *less than* /when/. Returns true if
the new value of *frame is valid, false otherwise. */
bool
2008-05-30 04:28:26 +02:00
Timeline::prev_line ( nframes_t *frame, bool bar ) const
2008-05-30 00:45:04 +02:00
{
nframes_t when = *frame - 1;
2008-05-30 04:28:26 +02:00
nearest_line_arg n = { when, -1, bar };
2008-05-30 00:45:04 +02:00
render_tempomap( xoffset, when - xoffset, prev_next_line_cb, &n );
if ( n.closest == (nframes_t)-1 )
return false;
else
{
*frame = n.closest;
return true;
}
}
2008-06-19 06:14:14 +02:00
/** given screen pixel coordinate /x/ return frame offset into
* timeline, taking into account the current scroll position, widget
* layout, etc. */
nframes_t
Timeline::x_to_offset ( int x ) const
{
return x_to_ts( max( 0, x - Track::width() ) ) + xoffset;
}
2008-04-26 18:07:43 +02:00
/** draws a single measure line */
static void
draw_measure_cb ( nframes_t frame, const BBT &bbt, void *arg )
{
Fl_Color *color = (Fl_Color*)arg;
fl_color( FL_BLACK );
fl_line_style( FL_DASH, 0 );
if ( bbt.beat )
++color;
fl_color( *color );
const int x = timeline->ts_to_x( frame - timeline->xoffset ) + Track::width();
fl_line( x, 0, x, 5000 );
fl_line_style( FL_SOLID, 0 );
}
/* FIXME: wrong place for this */
2008-05-13 23:38:13 +02:00
const float ticks_per_beat = 1920.0;
2008-06-19 06:14:14 +02:00
/** re-render the unified tempomap based on the current contents of the Time and Tempo sequences */
void
Timeline::update_tempomap ( void )
{
2008-05-15 01:47:28 +02:00
/* FIXME: we need some type of locking! */
_tempomap.clear();
for ( list <Sequence_Widget *>::const_iterator i = time_track->_widgets.begin();
i != time_track->_widgets.end(); ++i )
_tempomap.push_back( *i );
for ( list <Sequence_Widget *>::const_iterator i = tempo_track->_widgets.begin();
i != tempo_track->_widgets.end(); ++i )
_tempomap.push_back( *i );
_tempomap.sort( Sequence_Widget::sort_func );
}
2008-06-19 06:14:14 +02:00
/** return a stucture containing the BBT info which applies at /frame/ */
position_info
Timeline::solve_tempomap ( nframes_t frame ) const
{
2008-05-14 22:20:04 +02:00
return render_tempomap( frame, 0, 0, 0 );
}
2008-05-15 01:47:28 +02:00
/* THREAD: UI and RT */
/** draw appropriate measure lines inside the given bounding box */
position_info
Timeline::render_tempomap( nframes_t start, nframes_t length, measure_line_callback * cb, void *arg ) const
{
const nframes_t end = start + length;
position_info pos;
memset( &pos, 0, sizeof( pos ) );
BBT &bbt = pos.bbt;
/* default values */
pos.beat_type = 4;
pos.beats_per_bar = 4;
pos.tempo = 120.0;
2008-04-27 18:01:55 +02:00
const nframes_t samples_per_minute = sample_rate() * 60;
float bpm = 120.0f;
time_sig sig;
sig.beats_per_bar = 4;
sig.beat_type = 4;
nframes_t f = 0;
2008-05-14 20:43:12 +02:00
nframes_t next = 0;
nframes_t frames_per_beat = samples_per_minute / bpm;
if ( ! _tempomap.size() )
return pos;
for ( list <const Sequence_Widget *>::const_iterator i = _tempomap.begin();
i != _tempomap.end(); ++i )
{
if ( ! strcmp( (*i)->class_name(), "Tempo_Point" ) )
{
const Tempo_Point *p = (Tempo_Point*)(*i);
bpm = p->tempo();
frames_per_beat = samples_per_minute / bpm;
}
else
{
const Time_Point *p = (Time_Point*)(*i);
sig = p->time();
2008-05-15 01:47:28 +02:00
/* Time point resets beat */
bbt.beat = 0;
}
{
list <const Sequence_Widget *>::const_iterator n = i;
++n;
if ( n == _tempomap.end() )
next = end;
else
2008-05-14 20:43:12 +02:00
// next = min( (*n)->start(), end );
/* points may not always be aligned with beat boundaries, so we must align here */
next = (*n)->start() - ( ( (*n)->start() - (*i)->start() ) % frames_per_beat );
}
2008-05-14 21:17:58 +02:00
for ( ; f < next; ++bbt.beat, f += frames_per_beat )
{
2008-05-14 21:17:58 +02:00
if ( bbt.beat == sig.beats_per_bar )
{
bbt.beat = 0;
++bbt.bar;
}
2008-05-14 21:20:11 +02:00
if ( f >= start )
{
/* in the zone */
if ( cb )
cb( f, bbt, arg );
}
2008-05-14 21:20:11 +02:00
/* ugliness to avoid failing out at -1 */
if ( end >= frames_per_beat )
{
if ( f >= end - frames_per_beat )
goto done;
}
else if ( f + frames_per_beat >= end )
2008-05-14 21:20:11 +02:00
goto done;
2008-02-28 19:34:10 +01:00
}
}
2008-04-27 15:37:50 +02:00
done:
pos.frame = f;
pos.tempo = bpm;
pos.beats_per_bar = sig.beats_per_bar;
pos.beat_type = sig.beat_type;
2008-05-14 22:20:04 +02:00
assert( f <= end );
assert( end - f <= frames_per_beat );
/* FIXME: this this right? */
2008-05-17 01:44:18 +02:00
const double frames_per_tick = frames_per_beat / ticks_per_beat;
2008-05-14 22:20:04 +02:00
bbt.tick = ( end - f ) / frames_per_tick;
return pos;
}
2008-02-28 19:34:10 +01:00
2008-06-19 06:14:14 +02:00
/** maybe draw appropriate measure lines in rectangle defined by X, Y, W, and H, using color /color/ as a base */
void
Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
{
if ( ! draw_with_measure_lines )
return;
2008-05-16 23:06:15 +02:00
Fl_Color colors[2];
colors[1] = fl_color_average( FL_BLACK, color, 0.65f );
colors[0] = fl_color_average( FL_RED, colors[1], 0.65f );
const nframes_t start = x_to_offset( X );
const nframes_t length = x_to_ts( W );
fl_push_clip( X, Y, W, H );
render_tempomap( start, length, draw_measure_cb, &colors );
fl_pop_clip();
}
2008-02-28 19:34:10 +01:00
void
Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
{
Timeline *tl = (Timeline *)v;
fl_push_clip( X, Y, W, H );
/* fl_color( rand() ); */
/* fl_rectf( X, Y, X + W, Y + H ); */
2008-02-28 19:34:10 +01:00
tl->draw_box();
2008-02-28 19:34:10 +01:00
tl->draw_child( *tl->rulers );
2008-04-28 07:11:47 +02:00
fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), tl->tracks->w(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
tl->draw_child( *tl->tracks );
2008-04-28 07:11:47 +02:00
fl_pop_clip();
2008-02-28 19:34:10 +01:00
fl_pop_clip();
}
2008-06-19 06:14:14 +02:00
/** handle resize event */
2008-04-23 04:22:46 +02:00
void
Timeline::resize ( int X, int Y, int W, int H )
{
Fl_Overlay_Window::resize( X, Y, W, H );
2008-04-23 04:22:46 +02:00
/* why is this necessary? */
2008-04-23 08:33:21 +02:00
rulers->resize( Track::width(), 0, W - Track::width() - vscroll->w(), rulers->h() );
/* why is THIS necessary? */
hscroll->resize( 0, H - 18, hscroll->w(), 18 );
vscroll->size( vscroll->w(), H - 18 );
2008-04-23 04:22:46 +02:00
}
2008-06-19 06:14:14 +02:00
/** draw ancillary cursors (not necessarily in the overlay plane) */
void
Timeline::draw_cursors ( void ) const
{
if ( p1 != p2 )
{
draw_cursor( p1, FL_BLUE, draw_full_arrow_symbol );
draw_cursor( p2, FL_GREEN, draw_full_arrow_symbol );
}
}
2008-02-28 19:34:10 +01:00
void
Timeline::draw ( void )
{
int X, Y, W, H;
int bdx = 0;
int bdw = 0;
2008-02-28 19:34:10 +01:00
X = tracks->x() + bdx + 1;
Y = tracks->y();
W = tracks->w() - bdw - 1;
H = tracks->h();
2008-04-28 07:11:47 +02:00
adjust_vscroll();
if ( ( damage() & FL_DAMAGE_ALL ) || ( damage() & FL_DAMAGE_EXPOSE ) )
2008-02-28 19:34:10 +01:00
{
2008-05-04 01:44:28 +02:00
DMESSAGE( "complete redraw" );
2008-04-27 20:44:20 +02:00
2008-04-27 14:34:33 +02:00
draw_box( box(), 0, 0, w(), h(), color() );
2008-02-28 19:34:10 +01:00
fl_push_clip( 0, rulers->y(), w(), rulers->h() );
2008-02-28 19:34:10 +01:00
draw_child( *rulers );
fl_pop_clip();
2008-03-01 16:48:15 +01:00
fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
2008-02-28 19:34:10 +01:00
draw_child( *tracks );
fl_pop_clip();
draw_child( *hscroll );
draw_child( *vscroll );
draw_cursors();
2008-03-05 20:06:41 +01:00
redraw_overlay();
goto done;
2008-02-28 19:34:10 +01:00
}
if ( damage() & FL_DAMAGE_SCROLL )
{
int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
2008-03-07 00:29:28 +01:00
int dy = _old_yposition - _yposition;
2008-02-28 19:34:10 +01:00
/* draw_child( *rulers ); */
2008-02-28 19:34:10 +01:00
if ( ! dy )
2008-04-23 08:33:21 +02:00
fl_scroll( rulers->x(), rulers->y(), rulers->w(), rulers->h(), dx, 0, draw_clip, this );
2008-02-28 19:34:10 +01:00
Y = rulers->y() + rulers->h();
H = h() - rulers->h() - hscroll->h();
if ( dy == 0 )
2008-04-19 06:16:21 +02:00
fl_scroll( X + Track::width(), Y, W - Track::width(), H, dx, dy, draw_clip, this );
else
fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
2008-02-28 19:34:10 +01:00
}
if ( damage() & FL_DAMAGE_CHILD )
{
fl_push_clip( rulers->x(), rulers->y(), rulers->w(), rulers->h() );
update_child( *rulers );
fl_pop_clip();
2008-04-28 07:11:47 +02:00
fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - rulers->h() - hscroll->h() );
update_child( *tracks );
fl_pop_clip();
update_child( *hscroll );
update_child( *vscroll );
draw_cursors();
}
done:
_old_xposition = xoffset;
_old_yposition = _yposition;
2008-02-28 19:34:10 +01:00
}
2008-06-19 06:14:14 +02:00
/** draw a single cursor line at /frame/ with color /color/ using symbol routine /symbol/ for the cap */
2008-04-12 21:50:36 +02:00
void
Timeline::draw_cursor ( nframes_t frame, Fl_Color color, void (*symbol)(Fl_Color) ) const
2008-04-12 21:50:36 +02:00
{
// int x = ( ts_to_x( frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track::width();
if ( frame < xoffset )
return;
const int x = ts_to_x( frame - xoffset ) + tracks->x() + Track::width();
2008-04-12 21:50:36 +02:00
if ( x > tracks->x() + tracks->w() )
2008-04-12 21:50:36 +02:00
return;
2008-04-24 04:23:29 +02:00
fl_color( color );
2008-04-12 21:50:36 +02:00
const int y = rulers->y() + rulers->h();
const int h = this->h() - hscroll->h() - 1;
2008-04-12 23:47:33 +02:00
fl_push_clip( tracks->x() + Track::width(), y, tracks->w(), h );
2008-04-23 08:01:32 +02:00
2008-04-12 23:47:33 +02:00
fl_line( x, y, x, h );
2008-04-19 07:30:41 +02:00
2008-04-24 04:23:29 +02:00
fl_color( fl_darker( color ) );
2008-04-19 07:30:41 +02:00
fl_line( x - 1, y, x - 1, h );
fl_color( FL_BLACK );
fl_line( x + 1, y, x + 1, h );
2008-05-22 04:29:06 +02:00
fl_push_matrix();
2008-04-23 08:01:32 +02:00
2008-05-22 04:29:06 +02:00
fl_translate( x, y );
fl_scale( 16, 8 );
2008-04-23 08:01:32 +02:00
2008-05-22 04:29:06 +02:00
symbol( color );
2008-04-23 08:01:32 +02:00
2008-05-22 04:29:06 +02:00
fl_pop_matrix();
2008-04-23 08:01:32 +02:00
fl_pop_clip();
2008-04-24 04:23:29 +02:00
}
void
Timeline::draw_playhead ( void )
{
2008-05-22 04:29:06 +02:00
draw_cursor( transport->frame, FL_RED, draw_full_arrow_symbol );
2008-06-13 04:21:24 +02:00
// draw_cursor( length(), FL_BLACK, draw_full_arrow_symbol );
2008-04-12 21:50:36 +02:00
}
void
Timeline::redraw_playhead ( void )
{
static nframes_t last_playhead = -1;
2008-04-22 04:47:29 +02:00
if ( last_playhead != transport->frame )
{
redraw_overlay();
2008-04-22 04:47:29 +02:00
last_playhead = transport->frame;
2008-04-30 03:08:49 +02:00
if ( follow_playhead )
{
if ( center_playhead && active() )
xposition( max( 0, ts_to_x( transport->frame ) - ( ( tracks->w() - Track::width() ) >> 1 ) ) );
else if ( ts_to_x( transport->frame ) > ts_to_x( xoffset ) + ( tracks->w() - Track::width() ) )
xposition( ts_to_x( transport->frame ) );
}
}
2008-04-12 21:50:36 +02:00
}
2008-06-19 06:14:14 +02:00
/** called so many times a second to redraw the playhead etc. */
2008-04-12 21:50:36 +02:00
void
Timeline::update_cb ( void *arg )
{
Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
Timeline *tl = (Timeline *)arg;
tl->redraw_playhead();
}
2008-06-19 06:14:14 +02:00
/** draw cursors in overlay plane */
2008-03-01 05:57:53 +01:00
void
Timeline::draw_overlay ( void )
{
2008-04-12 21:50:36 +02:00
draw_playhead();
if ( ! ( _selection.w && _selection.h ) )
return;
2008-03-01 05:57:53 +01:00
2008-04-19 06:16:21 +02:00
fl_push_clip( tracks->x() + Track::width(), rulers->y() + rulers->h(), tracks->w() - Track::width(), h() - rulers->h() - hscroll->h() );
const Rectangle &r = _selection;
fl_color( FL_BLACK );
2008-03-07 00:21:57 +01:00
fl_line_style( FL_SOLID, 2 );
2008-03-07 00:21:57 +01:00
fl_rect( r.x + 2, r.y + 2, r.w, r.h );
fl_color( FL_MAGENTA );
fl_line_style( FL_DASH, 2 );
fl_rect( r.x, r.y, r.w, r.h );
2008-03-07 00:21:57 +01:00
fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
2008-03-05 20:06:41 +01:00
fl_line_style( FL_SOLID, 0 );
2008-03-01 05:57:53 +01:00
fl_pop_clip();
2008-03-05 20:06:41 +01:00
}
2008-03-01 05:57:53 +01:00
2008-06-19 06:14:14 +02:00
/** select sequence widgets within rectangle /r/ */
2008-03-05 20:06:41 +01:00
void
2008-04-29 23:16:09 +02:00
Timeline::select ( const Rectangle &r )
2008-03-05 20:06:41 +01:00
{
const int Y = r.y;
2008-03-05 20:06:41 +01:00
for ( int i = tracks->children(); i-- ; )
{
2008-04-19 06:16:21 +02:00
Track *t = (Track*)tracks->child( i );
2008-03-01 05:57:53 +01:00
if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
t->select( r.x, r.y, r.w, r.h, true, true );
2008-03-05 20:06:41 +01:00
}
2008-03-01 05:57:53 +01:00
}
2008-02-28 19:34:10 +01:00
2008-06-19 06:14:14 +02:00
/** delete all selected sequence widgets */
2008-04-29 23:16:09 +02:00
void
Timeline::delete_selected ( void )
{
Sequence_Widget::delete_selected();
}
2008-06-19 06:14:14 +02:00
/** clear the selection of seqeunce widgets */
2008-04-29 23:16:09 +02:00
void
Timeline::select_none ( void )
{
Sequence_Widget::select_none();
}
/** An unfortunate necessity for implementing our own DND aside from
* the (bogus) native FLTK system */
Track *
Timeline::track_under ( int Y )
{
for ( int i = tracks->children(); i-- ; )
{
Track *t = (Track*)tracks->child( i );
if ( ! ( t->y() > Y || t->y() + t->h() < Y ) )
return t;
}
2008-05-04 01:44:28 +02:00
return NULL;
}
#include "FL/event_name.H"
#include "FL/test_press.H"
2008-05-30 00:45:04 +02:00
/** a bit of a hack to keep FLTK's focus navigation stuff from
* stealing the arrow keys from us */
2008-05-27 05:32:35 +02:00
int
Timeline::handle_scroll ( int m )
{
2008-05-30 00:45:04 +02:00
if ( m == FL_KEYBOARD &&
Fl::event_key() != FL_Home &&
Fl::event_key() != FL_End )
return menu->test_shortcut() || hscroll->handle( m ) || vscroll->handle( m );
else
return 0;
2008-05-27 05:32:35 +02:00
}
2008-02-28 19:34:10 +01:00
int
Timeline::handle ( int m )
{
2008-03-05 20:06:41 +01:00
static Drag *drag = NULL;
static bool range = false;
2008-03-01 15:23:59 +01:00
/* if ( m != FL_NO_EVENT ) */
/* DMESSAGE( "%s", event_name( m ) ); */
2008-05-27 05:32:35 +02:00
/* int r = Fl_Overlay_Window::handle( m ); */
2008-02-28 19:34:10 +01:00
switch ( m )
{
case FL_FOCUS:
case FL_UNFOCUS:
2008-04-27 20:44:20 +02:00
// redraw();
return 1;
2008-05-26 04:22:52 +02:00
case FL_ENTER:
return 1;
case FL_LEAVE:
return 1;
case FL_KEYDOWN:
if ( Fl::event_key() == 'r' )
{
range = true;
return 1;
}
return 0;
case FL_KEYUP:
if ( Fl::event_key() == 'r' )
{
range = false;
return 1;
}
return 0;
// case FL_KEYBOARD:
case FL_SHORTCUT:
{
if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
/* we don't want any keys with modifiers... */
return 0;
switch ( Fl::event_key() )
{
case FL_Delete:
case FL_Home:
case FL_End:
/* keep scrollbar from eating these. */
return 0;
default:
2008-05-27 05:32:35 +02:00
return Fl_Overlay_Window::handle( m );
}
2008-03-01 15:23:59 +01:00
2008-03-05 20:06:41 +01:00
return 0;
}
default:
{
2008-05-27 05:32:35 +02:00
int r = Fl_Overlay_Window::handle( m );
2008-03-01 15:23:59 +01:00
if ( m != FL_RELEASE && r )
2008-03-05 20:06:41 +01:00
return r;
2008-03-01 15:23:59 +01:00
2008-03-05 20:06:41 +01:00
const int X = Fl::event_x();
const int Y = Fl::event_y();
2008-03-01 15:23:59 +01:00
2008-03-05 20:06:41 +01:00
switch ( m )
{
case FL_PUSH:
{
if ( test_press( FL_BUTTON1 ) || test_press( FL_BUTTON1 + FL_CTRL ) )
2008-04-19 04:26:27 +02:00
{
assert( ! drag );
drag = new Drag( X - x(), Y - y() );
_selection.x = drag->x;
_selection.y = drag->y;
2008-05-26 04:22:52 +02:00
if ( ! Fl::event_ctrl() )
select_none();
2008-05-26 04:22:52 +02:00
return 1;
2008-04-19 04:26:27 +02:00
}
else if ( test_press( FL_BUTTON3 ) )
2008-04-19 04:26:27 +02:00
{
menu_popup( menu );
2008-04-19 04:26:27 +02:00
2008-05-26 04:22:52 +02:00
return 1;
2008-04-19 04:26:27 +02:00
}
2008-05-26 04:22:52 +02:00
return 0;
2008-03-05 20:06:41 +01:00
}
case FL_DRAG:
{
int ox = X - drag->x;
int oy = Y - drag->y;
if ( ox < 0 )
_selection.x = X;
if ( oy < 0 )
_selection.y = Y;
_selection.w = abs( ox );
_selection.h = abs( oy );
if ( range )
{
p1 = x_to_offset( _selection.x );
p2 = x_to_offset( _selection.x + _selection.w );
redraw();
}
2008-05-26 04:22:52 +02:00
redraw_overlay();
return 1;
2008-03-05 20:06:41 +01:00
break;
}
case FL_RELEASE:
{
delete drag;
drag = NULL;
2008-03-01 15:23:59 +01:00
if ( range )
{
p1 = x_to_offset( _selection.x );
p2 = x_to_offset( _selection.x + _selection.w );
redraw();
}
else
select( _selection );
2008-03-01 15:23:59 +01:00
2008-03-05 20:06:41 +01:00
_selection.w = _selection.h = 0;
2008-05-26 04:22:52 +02:00
redraw_overlay();
return 1;
2008-03-05 20:06:41 +01:00
}
default:
return 0;
break;
}
2008-03-01 15:23:59 +01:00
2008-05-26 04:22:52 +02:00
return 0;
2008-03-05 20:06:41 +01:00
}
2008-02-28 19:34:10 +01:00
}
}
2008-06-19 06:14:14 +02:00
/** retrun a pointer to the track named /name/, or NULL if no track is named /name/ */
Track *
Timeline::track_by_name ( const char *name )
{
for ( int i = tracks->children(); i-- ; )
{
Track *t = (Track*)tracks->child( i );
if ( ! strcmp( name, t->name() ) )
return t;
}
return NULL;
}
/** return a malloc'd string representing a unique name for a new track */
char *
Timeline::get_unique_track_name ( const char *name )
{
char pat[256];
strcpy( pat, name );
for ( int i = 1; track_by_name( pat ); ++i )
snprintf( pat, sizeof( pat ), "%s.%d", name, i );
return strdup( pat );
}
/**********/
/* Public */
/**********/
/** return the current length of the timeline, which is arrived at by
* calculating the end frame of the rightmost audio region on an
* active audio sequence. Control_Points, etc. do not factor into this
* calcaulation. */
nframes_t
Timeline::length ( void ) const
{
nframes_t l = 0;
for ( int i = tracks->children(); i--; )
l = max( l, ((Track*)tracks->child( i ))->sequence()->length() );
// adjust_hscroll();
return l;
}
/** set horizontal scroll postion to absolute pixel coordinate /X/ */
void
Timeline::xposition ( int X )
{
xoffset = x_to_ts( X );
damage( FL_DAMAGE_SCROLL );
}
/** set vertical scroll position to absolute pixel coordinate /Y/ */
void
Timeline::yposition ( int Y )
{
_yposition = Y;
damage( FL_DAMAGE_SCROLL );
}
2008-06-19 06:14:14 +02:00
/** zoom in by one zoom step */
void
Timeline::zoom_in ( void )
{
hscroll->zoom_in();
}
2008-06-19 06:14:14 +02:00
/** zoom out by one zoom step */
void
Timeline::zoom_out ( void )
{
hscroll->zoom_out();
}
/** zoom the display to show /secs/ seconds per screen */
void
Timeline::zoom ( float secs )
{
2008-05-03 00:34:43 +02:00
const int sw = tracks->w() - Track::width();
2008-05-03 00:34:43 +02:00
int fpp = (int)((secs * sample_rate()) / sw);
int p = 0;
while ( 1 << p < fpp ) p++;
hscroll->zoom( p );
redraw();
}
2008-04-19 07:09:42 +02:00
2008-06-19 06:14:14 +02:00
/** fit the zoom to the current length of the timeline (subject to nearest power of two) */
2008-05-03 01:03:44 +02:00
void
Timeline::zoom_fit ( void )
{
xposition( 0 );
2008-06-22 05:57:14 +02:00
if ( length() )
zoom( length() / (float)sample_rate() );
else
zoom( 60 );
2008-05-03 01:03:44 +02:00
}
2008-06-19 06:14:14 +02:00
/** add /track/ to the timeline */
2008-04-19 04:26:27 +02:00
void
2008-04-19 06:16:21 +02:00
Timeline::add_track ( Track *track )
2008-04-19 04:26:27 +02:00
{
2008-05-04 01:44:28 +02:00
DMESSAGE( "added new track to the timeline" );
engine->lock();
2008-04-19 04:26:27 +02:00
tracks->add( track );
engine->unlock();
2008-04-19 04:26:27 +02:00
/* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
redraw();
}
2008-06-19 06:14:14 +02:00
/** remove /track/ from the timeline */
2008-04-19 04:26:27 +02:00
void
2008-04-19 06:16:21 +02:00
Timeline::remove_track ( Track *track )
2008-04-19 04:26:27 +02:00
{
2008-05-04 01:44:28 +02:00
DMESSAGE( "removed track from the timeline" );
2008-04-19 04:26:27 +02:00
engine->lock();
2008-04-19 04:26:27 +02:00
/* FIXME: what to do about track contents? */
tracks->remove( track );
engine->unlock();
2008-04-19 04:26:27 +02:00
/* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
redraw();
}