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 /*******************************************************************************/
23 #include <FL/fl_draw.H>
27 #include "FL/event_name.H"
29 #include "Transport.H" // for locate()
31 #include "../FL/Boxtypes.H"
34 #include "util/debug.h"
40 queue <Sequence_Widget *> Sequence::_delete_queue;
44 Sequence::Sequence ( Track *track, const char *name ) : Fl_Widget( 0, 0, 0, 0 ), Loggable( true )
51 _name = strdup( name );
56 Sequence::Sequence ( int X, int Y, int W, int H ) : Fl_Widget( X, Y, W, H ), Loggable( false )
62 Sequence::init ( void )
69 color( FL_BACKGROUND_COLOR );
70 align( FL_ALIGN_LEFT );
72 // clear_visible_focus();
75 Sequence::~Sequence ( )
77 DMESSAGE( "destroying sequence" );
82 if ( _widgets.size() )
83 FATAL( "programming error: leaf destructor must call Sequence::clear()!" );
86 parent()->remove( this );
92 Sequence::log_children ( void ) const
97 for ( std::list <Sequence_Widget*>::const_iterator i = _widgets.begin();
98 i != _widgets.end(); ++i )
102 /** remove all widgets from this sequence */
104 Sequence::clear ( void )
106 Loggable::block_start();
108 while ( _widgets.size() )
109 delete _widgets.front();
111 Loggable::block_end();
114 /** given screen pixel coordinate X, return an absolute frame offset into this sequence */
116 Sequence::x_to_offset ( int X )
118 return timeline->xoffset + timeline->x_to_ts( X - x() );
121 /** sort the widgets in this sequence by position */
123 Sequence::sort ( void )
125 _widgets.sort( Sequence_Widget::sort_func );
128 /** return a pointer to the widget that /r/ overlaps, or NULL if none. */
130 Sequence::overlaps ( Sequence_Widget *r )
132 for ( list <Sequence_Widget *>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
134 if ( *i == r ) continue;
135 if ( (*i)->overlaps( r ) )
143 Sequence::handle_widget_change ( nframes_t start, nframes_t length )
150 // timeline->update_length( start + length );
154 Sequence::widget_at ( nframes_t ts, int Y )
156 for ( list <Sequence_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); ++r )
157 if ( ts >= (*r)->start() && ts <= (*r)->start() + (*r)->length()
158 && Y >= (*r)->y() && Y <= (*r)->y() + (*r)->h() )
164 /** return a pointer to the widget under the current mouse event, or
165 * NULL if no widget intersects the event coordinates */
167 Sequence::event_widget ( void )
169 nframes_t ets = timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() );
170 return widget_at( ets, Fl::event_y() );
174 Sequence::add ( Sequence_Widget *r )
176 // Logger _log( this );
181 r->sequence()->remove( r );
182 // r->track()->redraw();
188 _widgets.push_back( r );
192 handle_widget_change( r->start(), r->length() );
196 Sequence::remove ( Sequence_Widget *r )
200 _widgets.remove( r );
204 handle_widget_change( r->start(), r->length() );
208 abs_diff ( nframes_t n1, nframes_t n2 )
210 return n1 > n2 ? n1 - n2 : n2 - n1;
213 /** snap widget /r/ to nearest edge */
215 Sequence::snap ( Sequence_Widget *r )
217 const int snap_pixels = 10;
218 const nframes_t snap_frames = timeline->x_to_ts( snap_pixels );
220 /* snap to other widgets */
221 if ( Timeline::snap_magnetic )
223 const int rx1 = r->start();
224 const int rx2 = r->start() + r->length();
226 for ( list <Sequence_Widget*>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
228 const Sequence_Widget *w = (*i);
233 const int wx1 = w->start();
234 const int wx2 = w->start() + w->length();
236 if ( abs_diff( rx1, wx2 ) < snap_frames )
238 r->start( w->start() + w->length() + 1 );
243 if ( abs_diff( rx2, wx1 ) < snap_frames )
245 r->start( ( w->start() - r->length() ) - 1 );
252 nframes_t f = r->start();
254 /* snap to beat/bar lines */
255 if ( timeline->nearest_line( &f ) )
261 Sequence::draw ( void )
264 if ( ! fl_not_clipped( x(), y(), w(), h() ) )
267 fl_push_clip( x(), y(), w(), h() );
269 /* draw the box with the ends cut off. */
270 draw_box( box(), x() - Fl::box_dx( box() ) - 1, y(), w() + Fl::box_dw( box() ) + 2, h(), color() );
274 fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
276 /* if ( Sequence_Widget::pushed() && Sequence_Widget::pushed()->sequence() == this ) */
278 /* /\* make sure the Sequence_Widget::pushed widget is above all others *\/ */
279 /* remove( Sequence_Widget::pushed() ); */
280 /* add( Sequence_Widget::pushed() ); */
283 // printf( "track::draw %d,%d %dx%d\n", X,Y,W,H );
285 timeline->draw_measure_lines( X, Y, W, H, color() );
287 for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
291 for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
298 #include "FL/test_press.H"
301 Sequence::handle ( int m )
304 /* if ( m != FL_NO_EVENT ) */
305 /* DMESSAGE( "%s", event_name( m ) ); */
311 if ( Fl::test_shortcut( FL_CTRL + FL_Right ) )
313 transport->locate( next( transport->frame ) );
316 else if ( Fl::test_shortcut( FL_CTRL + FL_Left ) )
318 transport->locate( prev( transport->frame ) );
321 else if ( Fl::test_shortcut( FL_CTRL + ' ' ) )
323 Sequence_Widget *r = widget_at( transport->frame, y() );
335 switch ( Fl::event_key() )
341 /* this is a hack to override FLTK's use of arrow keys for
342 * focus navigation */
343 return timeline->handle_scroll( m );
349 if ( Sequence_Widget::belowmouse() )
350 return Sequence_Widget::belowmouse()->dispatch( m );
352 /* garbage from overlay window */
355 Fl_Widget::handle( m );
359 Fl_Widget::handle( m );
363 // DMESSAGE( "leave" );
364 fl_cursor( FL_CURSOR_DEFAULT );
365 Fl_Widget::handle( m );
370 // DMESSAGE( "enter" );
371 if ( Sequence_Widget::pushed() )
373 if ( Sequence_Widget::pushed()->sequence()->class_name() == class_name() )
375 /* accept objects dragged from other sequences of this type */
376 add( Sequence_Widget::pushed() );
379 fl_cursor( FL_CURSOR_MOVE );
382 fl_cursor( (Fl_Cursor)1 );
385 if ( ! event_widget() )
386 fl_cursor( cursor() );
388 Fl_Widget::handle( m );
397 Sequence_Widget *r = event_widget();
399 if ( r != Sequence_Widget::belowmouse() )
401 if ( Sequence_Widget::belowmouse() )
402 Sequence_Widget::belowmouse()->handle( FL_LEAVE );
404 Sequence_Widget::belowmouse( r );
407 r->handle( FL_ENTER );
414 Sequence_Widget *r = Sequence_Widget::pushed() ? Sequence_Widget::pushed() : event_widget();
416 /* if ( this == Fl::focus() ) */
417 /* DMESSAGE( "Sequence widget = %p", r ); */
421 int retval = r->dispatch( m );
423 /* DMESSAGE( "retval = %d", retval ); */
432 if ( Sequence_Widget::pushed() )
433 Sequence_Widget::pushed()->handle( FL_UNFOCUS );
435 Sequence_Widget::pushed( r );
437 r->handle( FL_FOCUS );
439 else if ( m == FL_RELEASE )
440 Sequence_Widget::pushed( NULL );
443 Loggable::block_start();
445 while ( _delete_queue.size() )
448 Sequence_Widget *t = _delete_queue.front();
451 if ( Sequence_Widget::pushed() == t )
452 Sequence_Widget::pushed( NULL );
453 if ( Sequence_Widget::belowmouse() == t )
455 Sequence_Widget::belowmouse()->handle( FL_LEAVE );
456 Sequence_Widget::belowmouse( NULL );
462 Loggable::block_end();
471 if ( test_press( FL_BUTTON1 ) )
473 /* traditional selection model */
474 Sequence_Widget::select_none();
477 return Fl_Widget::handle( m );
489 /** calculate the length of this sequence by looking at the end of the
490 * least widget it contains */
492 /** return the length in frames of this sequence calculated from the
493 * right edge of the rightmost widget */
495 Sequence::length ( void ) const
499 for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
500 l = max( l, (*r)->start() + (*r)->length() );
505 /** return the location of the next widget from frame /from/ */
507 Sequence::next ( nframes_t from ) const
509 for ( list <Sequence_Widget*>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
510 if ( (*i)->start() > from )
511 return (*i)->start();
513 if ( _widgets.size() )
514 return _widgets.back()->start();
519 /** return the location of the next widget from frame /from/ */
521 Sequence::prev ( nframes_t from ) const
523 for ( list <Sequence_Widget*>::const_reverse_iterator i = _widgets.rbegin(); i != _widgets.rend(); i++ )
524 if ( (*i)->start() < from )
525 return (*i)->start();
527 if ( _widgets.size() )
528 return _widgets.front()->start();
533 /** delete all selected widgets in this sequence */
535 Sequence::remove_selected ( void )
537 Loggable::block_start();
539 for ( list <Sequence_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
540 if ( (*r)->selected() )
542 Sequence_Widget *t = *r;
543 _widgets.erase( r++ );
549 Loggable::block_end();
552 /** select all widgets intersecting with the range defined by the
553 * pixel coordinates X through W */
555 Sequence::select_range ( int X, int W )
557 nframes_t sts = x_to_offset( X );
558 nframes_t ets = sts + timeline->x_to_ts( W );
560 for ( list <Sequence_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); ++r )
561 if ( ! ( (*r)->start() > ets || (*r)->start() + (*r)->length() < sts ) )