2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles */
5 /* This program is free software; you can redistribute it and/or modify it */
6 /* under the terms of the GNU General Public License as published by the */
7 /* Free Software Foundation; either version 2 of the License, or (at your */
8 /* option) any later version. */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
12 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
15 /* You should have received a copy of the GNU General Public License along */
16 /* with This program; see the file COPYING. If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /*******************************************************************************/
20 #include <FL/fl_draw.H>
22 #include "Sequence_Widget.H"
32 list <Sequence_Widget *> Sequence_Widget::_selection;
33 Sequence_Widget * Sequence_Widget::_current = NULL;
34 Sequence_Widget * Sequence_Widget::_pushed = NULL;
35 Sequence_Widget * Sequence_Widget::_belowmouse = NULL;
36 Fl_Color Sequence_Widget::_selection_color = FL_MAGENTA;
40 Sequence_Widget::Sequence_Widget ( )
46 _r->start = _r->offset = _r->length = 0;
50 _box_color = FL_BACKGROUND_COLOR;
51 _color = FL_FOREGROUND_COLOR;
54 /* careful with this, it doesn't journal */
55 Sequence_Widget::Sequence_Widget ( const Sequence_Widget &rhs ) : Loggable( rhs )
59 _sequence = rhs._sequence;
65 _box_color = rhs._box_color;
68 const Sequence_Widget &
69 Sequence_Widget::operator= ( const Sequence_Widget &rhs )
76 _sequence = rhs._sequence;
77 _box_color = rhs._box_color;
83 Sequence_Widget::~Sequence_Widget ( )
87 if ( this == _pushed )
90 if ( this == _belowmouse )
93 _sequence->remove( this );
95 _selection.remove( this );
101 Sequence_Widget::get ( Log_Entry &e ) const
103 e.add( ":start", _r->start );
104 // e.add( ":offset", _r->offset );
105 // e.add( ":length", _r->length );
106 e.add( ":sequence", _sequence );
107 e.add( ":selected", selected() );
111 Sequence_Widget::set ( Log_Entry &e )
113 for ( int i = 0; i < e.size(); ++i )
119 if ( ! strcmp( s, ":start" ) )
120 _r->start = atoll( v );
121 // else if ( ! strcmp( s, ":offset" ) )
122 // _r->offset = atoll( v );
123 // else if ( ! strcmp( s, ":length" ) )
124 // _r->length = atoll( v );
125 else if ( ! strcmp( s, ":selected" ) )
132 else if ( ! strcmp( s, ":sequence" ) )
135 sscanf( v, "%X", &i );
136 Sequence *t = (Sequence*)Loggable::find( i );
138 ASSERT( t, "No such object ID (%s)", v );
148 _sequence->handle_widget_change( _r->start, _r->length );
154 Sequence_Widget::begin_drag ( const Drag &d )
156 _drag = new Drag( d );
160 _r = new Range( _range );
166 Sequence_Widget::end_drag ( void )
170 /* swap in the new value */
181 sequence()->handle_widget_change( _r->start, _r->length );
184 /** set position of widget on the timeline. */
186 Sequence_Widget::start ( nframes_t where )
188 /* this is pretty complicated because of selection and snapping */
197 if ( this != Sequence_Widget::_current )
200 long d = where - _r->start;
204 /* first, make sure we stop at 0 */
205 nframes_t m = (nframes_t)-1;
207 for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); ++i )
208 m = min( m, (*i)->_r->start );
212 if ( m <= (nframes_t)d )
215 for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); ++i )
218 (*i)->_r->start -= d;
223 /* TODO: do like the above and disallow wrapping */
224 for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); ++i )
227 (*i)->_r->start += d;
234 Sequence_Widget::draw_label ( void )
239 Sequence_Widget::draw_label ( const char *label, Fl_Align align, Fl_Color color, int xo, int yo )
246 if ( align & FL_ALIGN_CLIP ) fl_push_clip( X, Y, W, H );
254 // lab.type = FL_SHADOW_LABEL;
255 lab.type = FL_NORMAL_LABEL;
257 lab.font = FL_HELVETICA_ITALIC;
262 fl_font( lab.font, lab.size );
263 fl_measure( lab.value, lw, lh );
267 /* adjust for scrolling */
268 if ( abs_x() < scroll_x() )
269 dx = min( 32767, scroll_x() - abs_x() );
271 const Fl_Boxtype b = FL_BORDER_BOX;
272 const int bx = Fl::box_dx( b );
273 const int bw = Fl::box_dw( b );
274 const int by = Fl::box_dy( b );
275 const int bh = Fl::box_dh( b );
277 /* FIXME: why do we have to do this here? why doesn't Fl_Label::draw take care of this stuff? */
278 if ( align & FL_ALIGN_INSIDE )
280 if ( align & FL_ALIGN_BOTTOM )
281 Y += h() - ( lh + bh );
282 else if ( align & FL_ALIGN_TOP )
285 Y += ( h() / 2 ) - ( lh + bh );
287 if ( align & FL_ALIGN_RIGHT )
288 X += abs_w() - ( lw + bw );
289 else if ( align & FL_ALIGN_LEFT )
292 X += ( abs_w() / 2 ) - ( ( lw + bw ) / 2 );
297 if ( align & FL_ALIGN_RIGHT )
299 else if ( align & FL_ALIGN_LEFT )
302 X += ( abs_w() / 2 ) - ( ( lw + bw ) / 2 );
304 if ( align & FL_ALIGN_BOTTOM )
306 else if ( align & FL_ALIGN_TOP )
309 Y += ( h() / 2 ) - ( ( lh + bh ) / 2 );
312 fl_draw_box( b, ( X - dx ), Y - by, lw + bw, lh + bh, fl_color_add_alpha( FL_DARK1, 150 ) );
316 fl_draw( label, ( X - dx ), Y, lw + bw, lh, (Fl_Align)(FL_ALIGN_CENTER) );
318 if ( align & FL_ALIGN_CLIP ) fl_pop_clip();
322 Sequence_Widget::dispatch ( int m )
324 Sequence_Widget::_current = this;
328 Loggable::block_start();
332 for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); i++ )
334 r |= (*i)->handle( m );
338 Loggable::block_end();
347 Sequence_Widget::draw ( void )
353 Sequence_Widget::draw_box ( void )
355 fl_draw_box( box(), x(), y(), w(), h(), selected() ? FL_MAGENTA : _box_color );
359 #include "FL/test_press.H"
361 /* base hanlde just does basic dragging */
363 Sequence_Widget::handle ( int m )
365 int X = Fl::event_x();
366 int Y = Fl::event_y();
373 fl_cursor( FL_CURSOR_HAND );
376 // DMESSAGE( "leave" );
377 fl_cursor( sequence()->cursor() );
382 if ( test_press( FL_BUTTON3 + FL_CTRL ) )
387 else if ( test_press( FL_BUTTON1 ) || test_press( FL_BUTTON1 + FL_CTRL ) )
389 /* traditional selection model */
390 if ( Fl::event_ctrl() )
393 fl_cursor( FL_CURSOR_MOVE );
409 fl_cursor( FL_CURSOR_HAND );
418 begin_drag ( Drag( x() - X, y() - Y, x_to_offset( X ) ) );
422 if ( test_press( FL_BUTTON1 + FL_CTRL ) && ! _drag->state )
425 sequence()->add( this->clone() );
430 else if ( test_press( FL_BUTTON1 ) || test_press( FL_BUTTON1 + FL_CTRL ) )
434 const nframes_t of = timeline->x_to_offset( X );
436 if ( of >= _drag->start )
437 start( of - _drag->start );
441 if ( Sequence_Widget::_current == this )
442 sequence()->snap( this );
444 if ( X >= sequence()->x() + sequence()->w() ||
445 X <= sequence()->x() )
447 /* this drag needs to scroll */
449 nframes_t pos = timeline->xoffset;
451 nframes_t d = timeline->x_to_ts( 100 );
453 if ( X <= sequence()->x() )
464 timeline->xposition( timeline->ts_to_x( pos ) );
466 // timeline->update_length( start() + length() );
468 /* FIXME: why isn't this enough? */
469 sequence()->redraw();
472 if ( ! selected() || _selection.size() == 1 )
475 if ( Y > y() + h() || Y < y() )
477 Track *t = timeline->track_under( Y );
479 fl_cursor( (Fl_Cursor)1 );
482 t->handle( FL_ENTER );
492 DMESSAGE( "unknown" );
506 /** add this widget to the selection */
508 Sequence_Widget::select ( void )
513 _selection.push_back( this );
514 _selection.sort( sort_func );
519 /** remove this widget from the selection */
521 Sequence_Widget::deselect ( void )
523 _selection.remove( this );
528 Sequence_Widget::selected ( void ) const
530 return std::find( _selection.begin(), _selection.end(), this ) != _selection.end();
533 /** remove this widget from its sequence */
535 Sequence_Widget::remove ( void )
538 sequence()->queue_delete( this );
542 Sequence_Widget::delete_selected ( void )
544 Loggable::block_start();
546 while ( _selection.size() )
547 delete _selection.front();
549 Loggable::block_end();
553 Sequence_Widget::select_none ( void )
555 Loggable::block_start();
557 while ( _selection.size() )
559 Sequence_Widget *w = _selection.front();
563 _selection.front()->redraw();
564 _selection.pop_front();
569 Loggable::block_end();