Mixer: Changes to support project saving/loading.
[nondaw.git] / Timeline / Sequence.C
blob3438333d972960e7b2401c75e58323498d07447d
2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles                                     */
4 /*                                                                             */
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.                                                  */
9 /*                                                                             */
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   */
13 /* more details.                                                               */
14 /*                                                                             */
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 "Sequence.H"
21 #include "Timeline.H"
23 #include <FL/fl_draw.H>
25 #include "Track.H"
27 #include "FL/event_name.H"
29 #include "Transport.H" // for locate()
31 #include "../FL/Boxtypes.H"
33 #include "const.h"
34 #include "util/debug.h"
36 using namespace std;
40 queue <Sequence_Widget *> Sequence::_delete_queue;
44 Sequence::Sequence ( Track *track, const char *name ) : Fl_Widget( 0, 0, 0, 0 ), Loggable( true  )
46     init();
48     _track = track;
50     if ( name )
51         _name = strdup( name );
53 //    log_create();
56 Sequence::Sequence ( int X, int Y, int W, int H ) : Fl_Widget( X, Y, W, H ), Loggable( false )
58     init();
61 void
62 Sequence::init ( void )
64     _track = NULL;
66     _name = NULL;
68     box( FL_DOWN_BOX );
69     color(  FL_BACKGROUND_COLOR );
70     align( FL_ALIGN_LEFT );
72 //    clear_visible_focus();
75 Sequence::~Sequence (  )
77     DMESSAGE( "destroying sequence" );
79     if ( _name )
80         free( _name );
82     if ( _widgets.size() )
83         FATAL( "programming error: leaf destructor must call Sequence::clear()!" );
85     if ( parent() )
86         parent()->remove( this );
91 void
92 Sequence::log_children ( void ) const
94     if ( id() > 0 )
95         log_create();
97     for ( std::list <Sequence_Widget*>::const_iterator i = _widgets.begin();
98           i != _widgets.end(); ++i )
99         (*i)->log_create();
102 /** remove all widgets from this sequence */
103 void
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 */
115 nframes_t
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 */
122 void
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. */
129 Sequence_Widget *
130 Sequence::overlaps ( Sequence_Widget *r )
132     for ( list <Sequence_Widget *>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
133     {
134         if ( *i == r ) continue;
135         if ( (*i)->overlaps( r ) )
136             return *i;
137     }
139     return NULL;
142 void
143 Sequence::handle_widget_change ( nframes_t start, nframes_t length )
145     timeline->wrlock();
147     sort();
149     timeline->unlock();
150 //    timeline->update_length( start + length );
153 Sequence_Widget *
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() )
159             return (*r);
161     return NULL;
164 /** return a pointer to the widget under the current mouse event, or
165  * NULL if no widget intersects the event coordinates */
166 Sequence_Widget *
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() );
173 void
174 Sequence::add ( Sequence_Widget *r )
176 //    Logger _log( this );
178     if ( r->sequence() )
179     {
180         r->redraw();
181         r->sequence()->remove( r );
182 //        r->track()->redraw();
183     }
185     timeline->wrlock();
187     r->sequence( this );
188     _widgets.push_back( r );
190     timeline->unlock();
192     handle_widget_change( r->start(), r->length() );
195 void
196 Sequence::remove ( Sequence_Widget *r )
198     timeline->wrlock();
200     _widgets.remove( r );
202     timeline->unlock();
204     handle_widget_change( r->start(), r->length() );
207 static nframes_t
208 abs_diff ( nframes_t n1, nframes_t n2 )
210     return n1 > n2 ? n1 - n2 : n2 - n1;
213 /** snap widget /r/ to nearest edge */
214 void
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 )
222     {
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++ )
227         {
228             const Sequence_Widget *w = (*i);
230             if ( w == r )
231                 continue;
233             const int wx1 = w->start();
234             const int wx2 = w->start() + w->length();
236             if ( abs_diff( rx1, wx2 ) < snap_frames )
237             {
238                 r->start( w->start() + w->length() + 1 );
240                 return;
241             }
243             if ( abs_diff( rx2, wx1 ) < snap_frames )
244             {
245                 r->start( ( w->start() - r->length() ) - 1 );
247                 return;
248             }
249         }
250     }
252     nframes_t f = r->start();
254     /* snap to beat/bar lines */
255     if ( timeline->nearest_line( &f ) )
256         r->start( f );
260 void
261 Sequence::draw ( void )
264     if ( ! fl_not_clipped( x(), y(), w(), h() ) )
265         return;
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() );
272     int X, Y, W, H;
274     fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
276 /*     if ( Sequence_Widget::pushed() && Sequence_Widget::pushed()->sequence() == this ) */
277 /*     { */
278 /*         /\* make sure the Sequence_Widget::pushed widget is above all others *\/ */
279 /*         remove( Sequence_Widget::pushed() ); */
280 /*         add( Sequence_Widget::pushed() ); */
281 /*     } */
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 )
288         (*r)->draw_box();
291     for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin();  r != _widgets.end(); ++r )
292         (*r)->draw();
294     fl_pop_clip();
298 #include "FL/test_press.H"
301 Sequence::handle ( int m )
304 /*     if ( m != FL_NO_EVENT ) */
305 /*         DMESSAGE( "%s", event_name( m ) ); */
307     switch ( m )
308     {
309         case FL_KEYBOARD:
310         case FL_SHORTCUT:
311             if ( Fl::test_shortcut( FL_CTRL + FL_Right ) )
312             {
313                 transport->locate( next( transport->frame ) );
314                 return 1;
315             }
316             else if ( Fl::test_shortcut( FL_CTRL + FL_Left ) )
317             {
318                 transport->locate( prev( transport->frame ) );
319                 return 1;
320             }
321             else if ( Fl::test_shortcut( FL_CTRL + ' ' ) )
322             {
323                 Sequence_Widget *r = widget_at( transport->frame, y() );
325                 if ( r )
326                 {
327                     if ( r->selected() )
328                         r->deselect();
329                     else
330                         r->select();
331                 }
332             }
333             else
334             {
335                 switch ( Fl::event_key() )
336                 {
337                     case FL_Left:
338                     case FL_Right:
339                     case FL_Up:
340                     case FL_Down:
341                         /* this is a hack to override FLTK's use of arrow keys for
342                          * focus navigation */
343                         return timeline->handle_scroll( m );
344                     default:
345                         break;
346                 }
347             }
349             if ( Sequence_Widget::belowmouse() )
350                 return Sequence_Widget::belowmouse()->dispatch( m );
351         case FL_NO_EVENT:
352             /* garbage from overlay window */
353             return 0;
354         case FL_FOCUS:
355             Fl_Widget::handle( m );
356             redraw();
357             return 1;
358         case FL_UNFOCUS:
359             Fl_Widget::handle( m );
360             redraw();
361             return 1;
362         case FL_LEAVE:
363 //            DMESSAGE( "leave" );
364             fl_cursor( FL_CURSOR_DEFAULT );
365             Fl_Widget::handle( m );
366             return 1;
367         case FL_DND_DRAG:
368             return 1;
369         case FL_ENTER:
370 //            DMESSAGE( "enter" );
371             if ( Sequence_Widget::pushed() )
372             {
373                 if ( Sequence_Widget::pushed()->sequence()->class_name() == class_name() )
374                 {
375                     /* accept objects dragged from other sequences of this type */
376                     add( Sequence_Widget::pushed() );
377                     redraw();
379                     fl_cursor( FL_CURSOR_MOVE );
380                 }
381                 else
382                     fl_cursor( (Fl_Cursor)1 );
383             }
384             else
385                 if ( ! event_widget() )
386                     fl_cursor( cursor() );
388             Fl_Widget::handle( m );
390             return 1;
391         case FL_DND_ENTER:
392         case FL_DND_LEAVE:
393         case FL_DND_RELEASE:
394             return 1;
395         case FL_MOVE:
396         {
397             Sequence_Widget *r = event_widget();
399             if ( r != Sequence_Widget::belowmouse() )
400             {
401                 if ( Sequence_Widget::belowmouse() )
402                     Sequence_Widget::belowmouse()->handle( FL_LEAVE );
404                 Sequence_Widget::belowmouse( r );
406                 if ( r )
407                     r->handle( FL_ENTER );
408             }
410             return 1;
411         }
412         default:
413         {
414             Sequence_Widget *r = Sequence_Widget::pushed() ? Sequence_Widget::pushed() : event_widget();
416 /*             if ( this == Fl::focus() ) */
417 /*                 DMESSAGE( "Sequence widget = %p", r ); */
419             if ( r )
420             {
421                 int retval = r->dispatch( m );
423 /*                 DMESSAGE( "retval = %d", retval ); */
425                 if ( m == FL_PUSH )
426                     take_focus();
428                 if ( retval )
429                 {
430                     if ( m == FL_PUSH )
431                     {
432                         if ( Sequence_Widget::pushed() )
433                             Sequence_Widget::pushed()->handle( FL_UNFOCUS );
435                         Sequence_Widget::pushed( r );
437                         r->handle( FL_FOCUS );
438                     }
439                     else if ( m == FL_RELEASE )
440                         Sequence_Widget::pushed( NULL );
441                 }
443                 Loggable::block_start();
445                 while ( _delete_queue.size() )
446                 {
448                     Sequence_Widget *t = _delete_queue.front();
449                     _delete_queue.pop();
451                     if ( Sequence_Widget::pushed() == t )
452                         Sequence_Widget::pushed( NULL );
453                     if ( Sequence_Widget::belowmouse() == t )
454                     {
455                         Sequence_Widget::belowmouse()->handle( FL_LEAVE );
456                         Sequence_Widget::belowmouse( NULL );
457                     }
459                     delete t;
460                 }
462                 Loggable::block_end();
464                 if ( m == FL_PUSH )
465                     return 1;
466                 else
467                     return retval;
468             }
469             else
470             {
471                 if ( test_press( FL_BUTTON1 ) )
472                 {
473                     /* traditional selection model */
474                     Sequence_Widget::select_none();
475                 }
477                 return Fl_Widget::handle( m );
478             }
479         }
480     }
485 /**********/
486 /* Public */
487 /**********/
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 */
494     nframes_t
495         Sequence::length ( void ) const
496     {
497         nframes_t l = 0;
499         for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
500             l = max( l, (*r)->start() + (*r)->length() );
502         return l;
503     }
505 /** return the location of the next widget from frame /from/ */
506     nframes_t
507         Sequence::next ( nframes_t from ) const
508     {
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();
515         else
516             return 0;
517     }
519 /** return the location of the next widget from frame /from/ */
520     nframes_t
521         Sequence::prev ( nframes_t from ) const
522     {
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();
529         else
530             return 0;
531     }
533 /** delete all selected widgets in this sequence */
534     void
535         Sequence::remove_selected ( void )
536     {
537         Loggable::block_start();
539         for ( list <Sequence_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
540             if ( (*r)->selected() )
541             {
542                 Sequence_Widget *t = *r;
543                 _widgets.erase( r++ );
544                 delete t;
545             }
546             else
547                 ++r;
549         Loggable::block_end();
550     }
552 /** select all widgets intersecting with the range defined by the
553  * pixel coordinates X through W */
554     void
555         Sequence::select_range ( int X, int W )
556     {
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 ) )
562                 (*r)->select();
563     }