non/timeline/src/Sequence_Widget.C

597 lines
14 KiB
C++
Raw Normal View History

2008-03-03 22:00:38 +01:00
/*******************************************************************************/
/* 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-05-22 22:58:36 +02:00
#include <FL/fl_draw.H>
2008-04-19 06:16:21 +02:00
#include "Sequence_Widget.H"
#include "Track.H"
2008-03-03 22:00:38 +01:00
#include "const.h"
2010-02-01 07:25:16 +01:00
#include "debug.h"
2008-05-22 22:58:36 +02:00
using namespace std;
2008-05-18 07:09:18 +02:00
2008-04-19 06:16:21 +02:00
list <Sequence_Widget *> Sequence_Widget::_selection;
Sequence_Widget * Sequence_Widget::_current = NULL;
Sequence_Widget * Sequence_Widget::_pushed = NULL;
Sequence_Widget * Sequence_Widget::_belowmouse = NULL;
Fl_Color Sequence_Widget::_selection_color = FL_MAGENTA;
2008-03-03 22:00:38 +01:00
2008-05-22 22:58:36 +02:00
Sequence_Widget::Sequence_Widget ( )
{
_label = 0;
_sequence = NULL;
_r = &_range;
_r->start = _r->offset = _r->length = 0;
_drag = NULL;
_box_color = FL_BACKGROUND_COLOR;
_color = FL_FOREGROUND_COLOR;
}
2008-06-23 03:01:09 +02:00
/* careful with this, it doesn't journal */
Sequence_Widget::Sequence_Widget ( const Sequence_Widget &rhs ) : Loggable( rhs )
{
_drag = NULL;
if ( rhs._label )
_label = strdup( rhs._label );
else
_label = 0;
2008-06-23 03:01:09 +02:00
_range = rhs._range;
_dragging_range = rhs._dragging_range;
2008-06-23 03:01:09 +02:00
_r = &_range;
_color = rhs._color;
_box_color = rhs._box_color;
_sequence = NULL;
if ( rhs._sequence )
rhs._sequence->add( this );
2008-06-23 03:01:09 +02:00
};
const Sequence_Widget &
Sequence_Widget::operator= ( const Sequence_Widget &rhs )
{
if ( this == &rhs )
return *this;
_r = &_range;
_range = rhs._range;
_dragging_range = rhs._dragging_range;
2008-06-23 03:01:09 +02:00
_sequence = rhs._sequence;
_box_color = rhs._box_color;
_color = rhs._color;
if ( rhs._label )
_label = strdup( rhs._label );
2008-06-23 03:01:09 +02:00
return *this;
}
Sequence_Widget::~Sequence_Widget ( )
{
redraw();
if ( this == _pushed )
_pushed = NULL;
if ( this == _belowmouse )
_belowmouse = NULL;
label( NULL );
_sequence->remove( this );
_selection.remove( this );
}
2008-06-19 06:14:14 +02:00
void
Sequence_Widget::get ( Log_Entry &e ) const
{
e.add( ":start", _r->start );
// e.add( ":offset", _r->offset );
// e.add( ":length", _r->length );
e.add( ":sequence", _sequence );
e.add( ":selected", selected() );
}
void
Sequence_Widget::set ( Log_Entry &e )
{
for ( int i = 0; i < e.size(); ++i )
{
const char *s, *v;
e.get( i, &s, &v );
if ( ! strcmp( s, ":start" ) )
_r->start = atoll( v );
// else if ( ! strcmp( s, ":offset" ) )
// _r->offset = atoll( v );
// else if ( ! strcmp( s, ":length" ) )
// _r->length = atoll( v );
else if ( ! strcmp( s, ":selected" ) )
{
if ( atoi( v ) )
select();
else
deselect();
}
else if ( ! strcmp( s, ":sequence" ) )
{
int i;
sscanf( v, "%X", &i );
Sequence *t = (Sequence*)Loggable::find( i );
ASSERT( t, "No such object ID (%s)", v );
t->add( this );
}
// else
// e.erase( i );
}
if ( _sequence )
{
_sequence->handle_widget_change( _r->start, _r->length );
_sequence->damage( FL_DAMAGE_USER1 );
}
}
2008-05-22 21:20:15 +02:00
void
Sequence_Widget::begin_drag ( const Drag &d )
{
_drag = new Drag( d );
timeline->sequence_lock.wrlock();
/* copy current values */
_dragging_range = _range;
/* tell display to use temporary */
_r = &_dragging_range;
timeline->sequence_lock.unlock();
2008-05-22 21:20:15 +02:00
}
void
Sequence_Widget::end_drag ( void )
{
/* swap in the new value */
/* go back to playback and display using same values */
timeline->sequence_lock.wrlock();
2008-05-22 21:20:15 +02:00
_range = _dragging_range;
2008-05-22 21:20:15 +02:00
_r = &_range;
delete _drag;
_drag = NULL;
/* this will result in a sort */
sequence()->handle_widget_change( _r->start, _r->length );
timeline->sequence_lock.unlock();
2008-05-22 21:20:15 +02:00
}
/** set position of widget on the timeline. */
void
Sequence_Widget::start ( nframes_t where )
{
/* this is pretty complicated because of selection and snapping */
if ( ! selected() )
{
redraw();
_r->start = where;
}
else
{
if ( this != Sequence_Widget::_current )
return;
/* difference between where we are current and desired position */
if ( where < _r->start )
{
nframes_t d = _r->start - where;
/* first, make sure we stop at 0 */
nframes_t m = JACK_MAX_FRAMES;
/* find the earliest region start point */
for ( list <Sequence_Widget *>::const_iterator i = _selection.begin(); i != _selection.end(); ++i )
m = min( m, (*i)->_r->start );
if ( d > m )
d = 0;
for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); ++i )
{
(*i)->redraw();
(*i)->_r->start -= d;
}
}
else
{
nframes_t d = where - _r->start;
/* TODO: do like the above and disallow wrapping */
for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); ++i )
{
(*i)->redraw();
(*i)->_r->start += d;
}
}
}
}
void
Sequence_Widget::draw_label ( void )
{
}
2008-03-03 22:00:38 +01:00
void
Sequence_Widget::draw_label ( const char *label, Fl_Align align, Fl_Color color, int xo, int yo )
2008-03-03 22:00:38 +01:00
{
int X = x();
int Y = y();
int W = w();
int H = h();
2008-03-03 22:00:38 +01:00
if ( align & FL_ALIGN_CLIP ) fl_push_clip( X, Y, W, H );
2008-03-03 22:00:38 +01:00
X += xo;
Y += yo;
2008-03-03 22:00:38 +01:00
Fl_Label lab;
lab.color = color;
// lab.type = FL_SHADOW_LABEL;
lab.type = FL_NORMAL_LABEL;
2008-03-03 22:00:38 +01:00
lab.value = label;
lab.font = FL_HELVETICA_ITALIC;
lab.size = 9;
2008-03-03 22:00:38 +01:00
int lw = 0, lh = 0;
fl_font( lab.font, lab.size );
fl_measure( lab.value, lw, lh );
int dx = 0;
2008-03-03 22:00:38 +01:00
/* adjust for scrolling */
if ( abs_x() < scroll_x() )
dx = min( 32767, scroll_x() - abs_x() );
const Fl_Boxtype b = FL_BORDER_BOX;
const int bx = Fl::box_dx( b );
const int bw = Fl::box_dw( b );
const int by = Fl::box_dy( b );
const int bh = Fl::box_dh( b );
/* FIXME: why do we have to do this here? why doesn't Fl_Label::draw take care of this stuff? */
2008-03-03 22:00:38 +01:00
if ( align & FL_ALIGN_INSIDE )
{
if ( align & FL_ALIGN_BOTTOM )
Y += h() - ( lh + bh );
else if ( align & FL_ALIGN_TOP )
Y += by;
else
Y += ( h() / 2 ) - ( lh + bh );
if ( align & FL_ALIGN_RIGHT )
X += abs_w() - ( lw + bw );
else if ( align & FL_ALIGN_LEFT )
X += bx;
else
X += ( abs_w() / 2 ) - ( ( lw + bw ) / 2 );
2008-03-03 22:00:38 +01:00
}
else
{
if ( align & FL_ALIGN_RIGHT )
X += abs_w();
else if ( align & FL_ALIGN_LEFT )
X -= lw + bw;
else
X += ( abs_w() / 2 ) - ( ( lw + bw ) / 2 );
2008-03-03 22:00:38 +01:00
if ( align & FL_ALIGN_BOTTOM )
Y += h();
else if ( align & FL_ALIGN_TOP )
Y -= lh + bh;
else
Y += ( h() / 2 ) - ( ( lh + bh ) / 2 );
}
fl_draw_box( b, ( X - dx ), Y - by, lw + bw, lh + bh, fl_color_add_alpha( FL_DARK1, 150 ) );
fl_color( color );
fl_draw( label, ( X - dx ), Y, lw + bw, lh, (Fl_Align)(FL_ALIGN_CENTER) );
2008-03-03 22:00:38 +01:00
if ( align & FL_ALIGN_CLIP ) fl_pop_clip();
}
2008-03-04 01:25:05 +01:00
int
2008-04-19 06:16:21 +02:00
Sequence_Widget::dispatch ( int m )
2008-03-04 01:25:05 +01:00
{
2008-04-19 06:16:21 +02:00
Sequence_Widget::_current = this;
2008-03-04 01:25:05 +01:00
if ( selected() )
{
Loggable::block_start();
int r = 0;
2008-04-19 06:16:21 +02:00
for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); i++ )
2008-03-04 01:25:05 +01:00
if ( *i != this )
r |= (*i)->handle( m );
r |= handle( m );
Loggable::block_end();
return r;
}
else
return handle( m );
}
2008-03-03 22:00:38 +01:00
2008-05-18 07:09:18 +02:00
void
Sequence_Widget::draw ( void )
{
draw_box();
}
void
Sequence_Widget::draw_box ( void )
{
fl_draw_box( box(), x(), y(), w(), h(), selected() ? FL_MAGENTA : _box_color );
}
#include "FL/test_press.H"
2008-03-03 22:00:38 +01:00
/* base hanlde just does basic dragging */
int
2008-04-19 06:16:21 +02:00
Sequence_Widget::handle ( int m )
2008-03-03 22:00:38 +01:00
{
int X = Fl::event_x();
int Y = Fl::event_y();
Logger _log( this );
switch ( m )
{
case FL_ENTER:
fl_cursor( FL_CURSOR_HAND );
return 1;
case FL_LEAVE:
2008-05-26 04:22:52 +02:00
// DMESSAGE( "leave" );
2008-05-07 18:42:31 +02:00
fl_cursor( sequence()->cursor() );
2008-03-03 22:00:38 +01:00
return 1;
case FL_PUSH:
{
/* deletion */
if ( test_press( FL_BUTTON3 + FL_CTRL ) )
2008-03-03 22:00:38 +01:00
{
2008-06-23 03:01:09 +02:00
remove();
return 1;
2008-03-03 22:00:38 +01:00
}
else if ( test_press( FL_BUTTON1 ) || test_press( FL_BUTTON1 + FL_CTRL ) )
2008-05-26 04:22:52 +02:00
{
/* traditional selection model */
if ( Fl::event_ctrl() )
select();
2008-05-26 04:22:52 +02:00
fl_cursor( FL_CURSOR_MOVE );
/* movement drag */
return 1;
}
2008-03-03 22:00:38 +01:00
2008-03-05 23:51:04 +01:00
return 0;
2008-03-03 22:00:38 +01:00
}
case FL_RELEASE:
2008-03-04 01:25:05 +01:00
if ( _drag )
{
2008-05-17 04:21:44 +02:00
end_drag();
2008-03-03 22:00:38 +01:00
_log.release();
2008-03-04 01:25:05 +01:00
}
fl_cursor( FL_CURSOR_HAND );
2008-03-03 22:00:38 +01:00
return 1;
case FL_DRAG:
{
Fl::event_key( 0 );
2008-03-04 01:25:05 +01:00
if ( ! _drag )
2008-03-03 22:00:38 +01:00
{
begin_drag ( Drag( X, Y, x_to_offset( X ), start() ) );
2008-03-03 22:00:38 +01:00
_log.hold();
}
if ( test_press( FL_BUTTON1 + FL_CTRL ) && ! _drag->state )
{
/* duplication */
timeline->sequence_lock.wrlock();
sequence()->add( this->clone() );
timeline->sequence_lock.unlock();
_drag->state = 1;
return 1;
}
else if ( test_press( FL_BUTTON1 ) || test_press( FL_BUTTON1 + FL_CTRL ) )
2008-05-26 04:22:52 +02:00
{
redraw();
2008-03-03 22:00:38 +01:00
2008-05-02 07:52:50 +02:00
const nframes_t of = timeline->x_to_offset( X );
2008-03-03 22:00:38 +01:00
int64_t s = (int64_t)of - _drag->offset;
if ( s < 0 )
s = 0;
start(s);
if ( Sequence_Widget::_current == this )
sequence()->snap( this );
2008-05-26 04:22:52 +02:00
if ( X >= sequence()->x() + sequence()->w() ||
X <= sequence()->drawable_x() )
2008-05-26 04:22:52 +02:00
{
/* this drag needs to scroll */
2008-03-03 22:00:38 +01:00
int64_t pos = s - ( _drag->mouse_offset - _drag->offset );
2008-03-03 22:00:38 +01:00
if ( X > sequence()->x() + sequence()->w() )
pos -= timeline->x_to_ts( sequence()->drawable_w() );
2008-03-03 22:00:38 +01:00
if ( s == 0 )
pos = 0;
2008-04-27 20:44:20 +02:00
if ( pos < 0 )
pos = 0;
2008-05-28 01:47:47 +02:00
timeline->xposition(timeline->ts_to_x(pos));
/* timeline->redraw(); */
sequence()->damage( FL_DAMAGE_USER1 );
2008-05-26 04:22:52 +02:00
}
2008-03-03 22:00:38 +01:00
if ( ! selected() || _selection.size() == 1 )
{
/* track jumping */
if ( Y > _sequence->y() + _sequence->h() || Y < _sequence->y() )
{
Track *t = timeline->track_under( Y );
fl_cursor( FL_CURSOR_HAND );
if ( t )
t->handle( FL_ENTER );
return 0;
}
}
2008-05-26 04:22:52 +02:00
return 1;
}
else
{
DMESSAGE( "unknown" );
2008-05-26 04:22:52 +02:00
return 0;
}
2008-03-03 22:00:38 +01:00
}
default:
return 0;
}
}
2008-06-23 03:01:09 +02:00
/**********/
/* Public */
/**********/
/** add this widget to the selection */
void
Sequence_Widget::select ( void )
{
if ( selected() )
return;
_selection.push_back( this );
_selection.sort( sort_func );
redraw();
}
/** remove this widget from the selection */
void
Sequence_Widget::deselect ( void )
{
_selection.remove( this );
redraw();
}
bool
Sequence_Widget::selected ( void ) const
{
return std::find( _selection.begin(), _selection.end(), this ) != _selection.end();
}
/** remove this widget from its sequence */
void
Sequence_Widget::remove ( void )
{
redraw();
sequence()->queue_delete( this );
}
void
Sequence_Widget::delete_selected ( void )
{
Loggable::block_start();
while ( _selection.size() )
delete _selection.front();
Loggable::block_end();
}
void
Sequence_Widget::select_none ( void )
{
Loggable::block_start();
while ( _selection.size() )
{
Sequence_Widget *w = _selection.front();
w->log_start();
2008-06-23 03:01:09 +02:00
_selection.front()->redraw();
_selection.pop_front();
w->log_end();
2008-06-23 03:01:09 +02:00
}
Loggable::block_end();
}