non/Timeline/Sequence_Widget.H

365 lines
11 KiB
C++

/*******************************************************************************/
/* 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. */
/*******************************************************************************/
#pragma once
#include "Sequence.H"
#include "Loggable.H"
#include "Timeline.H"
#include <list>
#include <algorithm>
using namespace std;
class Sequence_Widget;
struct Drag
{
/* mouse coords at start of drag */
int x;
int y;
int state;
Sequence_Widget *original;
Drag( int X, int Y ) : x( X ), y( Y ) { state = 0; }
};
struct Range
{
nframes_t offset; /* where on the timeline */
nframes_t start; /* first sample from clip */
nframes_t end; /* last sample from clip */
};
/* Base class for virtual widget on a track */
class Sequence_Widget : public Loggable
{
static list <Sequence_Widget *> _selection; /* all the widgets making up the selection */
/* FIXME: is this not the same as /pushed/? */
static Sequence_Widget * _current; /* the widget initiating events that affect the selection */
/* these are actually managed in the Sequence classes */
static Sequence_Widget * _pushed; /* the widget receiving drag events (a copy) */
static Sequence_Widget * _original; /* the original of the /pushed/ widget */
static Sequence_Widget * _belowmouse; /* the widget below the mouse cursor */
/* can't have this */
Sequence_Widget ( const Sequence_Widget &rhs );
protected:
Sequence *_track; /* track this region belongs to */
Range _range; /* range for playback */
Range *_r; /* range for editing / display (points to the same thing as above, except for when dragging etc) */
Fl_Color _color; /* color of waveform */
Fl_Color _box_color; /* color of background (box) */
Drag *_drag;
virtual void get ( Log_Entry &e ) const
{
e.add( ":x", _r->offset );
e.add( ":l", _r->start );
e.add( ":r", _r->end );
e.add( ":t", _track );
e.add( ":s", selected() );
}
virtual void
set ( Log_Entry &e )
{
for ( int i = 0; i < e.size(); ++i )
{
const char *s, *v;
e.get( i, &s, &v );
if ( ! strcmp( s, ":x" ) )
_r->offset = atoll( v );
else if ( ! strcmp( s, ":l" ) )
_r->start = atoll( v );
else if ( ! strcmp( s, ":r" ) )
_r->end = atoll( v );
else if ( ! strcmp( s, ":s" ) )
{
if ( atoi( v ) )
select();
else
deselect();
}
else if ( ! strcmp( s, ":t" ) )
{
int i;
sscanf( v, "%X", &i );
Sequence *t = (Sequence*)Loggable::find( i );
assert( t );
t->add( this );
}
// else
// e.erase( i );
}
if ( _track )
_track->redraw();
}
public:
Sequence_Widget ( )
{
_track = NULL;
_r = &_range;
_r->offset = _r->start = _r->end = 0;
_drag = NULL;
_box_color = FL_BACKGROUND_COLOR;
_color = FL_FOREGROUND_COLOR;
}
virtual ~Sequence_Widget ( )
{
redraw();
_track->remove( this );
_selection.remove( this );
}
const Sequence_Widget &
operator= ( const Sequence_Widget &rhs )
{
if ( this == &rhs )
return *this;
_r = &_range;
_range = rhs._range;
_track = rhs._track;
_box_color = rhs._box_color;
_color = rhs._color;
return *this;
}
/* Sequence_Widget ( const Sequence_Widget &rhs ) */
/* { */
/* *this = rhs; */
/* } */
virtual Sequence_Widget *clone ( const Sequence_Widget *r ) = 0;
bool selected ( void ) const
{
return ::find( _selection.begin(), _selection.end(), this ) != _selection.end();
}
void select ( void )
{
if ( selected() )
return;
_selection.push_back( this );
_selection.sort( sort_func );
redraw();
}
void deselect ( void )
{
_selection.remove( this );
redraw();
}
static void
delete_selected ( void )
{
while ( _selection.size() )
delete _selection.front();
}
static Sequence_Widget *current ( void ) { return Sequence_Widget::_current; }
static Sequence_Widget *pushed ( void ) { return Sequence_Widget::_pushed; }
static Sequence_Widget *belowmouse ( void ) { return Sequence_Widget::_belowmouse; }
static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
static void belowmouse ( Sequence_Widget *w ) { Sequence_Widget::_belowmouse = w; }
// static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
void begin_drag ( const Drag &d )
{
_drag = new Drag( d );
_r = new Range( _range );
}
void end_drag ( void )
{
_range = *_r;
delete _r;
_r = &_range;
delete _drag;
_drag = NULL;
}
void
offset ( nframes_t where )
{
if ( ! selected() )
{
redraw();
_r->offset = where;
}
else
{
long d = where - _r->offset;
for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); i++ )
{
(*i)->redraw();
if ( d < 0 )
(*i)->_r->offset -= 0 - d;
else
(*i)->_r->offset += d;
}
}
}
int dispatch ( int m );
Fl_Widget * parent ( void ) const { return _track; }
int scroll_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
nframes_t scroll_ts ( void ) const { return timeline->xoffset; }
virtual int y ( void ) const { return _track->y(); }
virtual int h ( void ) const { return _track->h(); }
/* used by regions */
virtual int x ( void ) const { return _r->offset < timeline->xoffset ? _track->x() - 1 : min( 32767, _track->x() + timeline->ts_to_x( _r->offset - timeline->xoffset ) ); }
/* use this as x() when you need to draw lines between widgets */
int line_x ( void ) const
{
return _r->offset < timeline->xoffset ? max( -32768, _track->x() - timeline->ts_to_x( timeline->xoffset - _r->offset )) : min( 32767, _track->x() + timeline->ts_to_x( _r->offset - timeline->xoffset ) );
}
virtual int w ( void ) const
{
int tx = timeline->ts_to_x( _r->offset );
int rw;
if ( tx < scroll_x() )
rw = abs_w() - (scroll_x() - tx);
else
rw = abs_w();
return min( rw, _track->w() );
}
int abs_x ( void ) const { return timeline->ts_to_x( _r->offset ); }
virtual int abs_w ( void ) const { return timeline->ts_to_x( _r->end - _r->start ); }
Fl_Color color ( void ) { return _color; }
void color ( Fl_Color v ) { _color = v; }
Fl_Color box_color ( void ) { return _box_color; }
Sequence * track ( void ) const { return _track; }
void track ( Sequence *t ) { _track = t; }
nframes_t offset ( void ) const { return _r->offset; }
// void offset ( nframes_t o ) { _r->offset = o; }
void end ( nframes_t v ) { _r->end = v; }
nframes_t end ( void ) const { return _r->end; }
void start ( nframes_t v ) { _r->start = v; }
nframes_t start ( void ) const { return _r->start; }
int active_r ( void ) const { return _track->active_r(); }
virtual nframes_t length ( void ) const { return _r->end - _r->start; }
virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
virtual Fl_Align align ( void ) const { return (Fl_Align)0; }
virtual void
redraw ( void )
{
if ( ! (align() & FL_ALIGN_INSIDE) )
{
// FIXME: to better..
_track->redraw();
}
else
_track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
}
/* just draw a simple box */
virtual void
draw_box ( void )
{
fl_draw_box( box(), x(), y(), w(), h(), _box_color );
}
virtual void
draw ( void )
{
draw_box();
}
bool
operator< ( const Sequence_Widget & rhs )
{
return _r->offset < rhs._r->offset;
}
bool
operator<=( const Sequence_Widget & rhs )
{
return _r->offset <= rhs._r->offset;
}
virtual void draw_label ( const char *label, Fl_Align align, Fl_Color color=(Fl_Color)0 );
virtual int handle ( int m );
static bool
sort_func ( Sequence_Widget *lhs, Sequence_Widget *rhs )
{
return *lhs < *rhs;
}
};