Update NTK.
[nondaw.git] / timeline / src / Sequence_Widget.C
blobb8b3722074c9c4699c2ed94150df6784566ce71e
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 <FL/fl_draw.H>
22 #include "Sequence_Widget.H"
23 #include "Track.H"
25 #include "const.h"
26 #include "debug.h"
28 using namespace std;
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 ( )
42     _sequence = NULL;
44     _r = &_range;
46     _r->start = _r->offset = _r->length = 0;
48     _drag = NULL;
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 )
57     _drag = NULL;
59     _sequence = rhs._sequence;
61     _range = rhs._range;
62     _r = &_range;
64     _color = rhs._color;
65     _box_color = rhs._box_color;
68 const Sequence_Widget &
69 Sequence_Widget::operator= ( const Sequence_Widget &rhs )
71     if ( this == &rhs )
72         return *this;
74     _r         = &_range;
75     _range     = rhs._range;
76     _sequence     = rhs._sequence;
77     _box_color = rhs._box_color;
78     _color     = rhs._color;
80     return *this;
83 Sequence_Widget::~Sequence_Widget ( )
85     redraw();
87     if ( this == _pushed )
88         _pushed = NULL;
90     if ( this == _belowmouse )
91         _belowmouse = NULL;
93     _sequence->remove( this );
95     _selection.remove( this );
100 void
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() );
110 void
111 Sequence_Widget::set ( Log_Entry &e )
113     for ( int i = 0; i < e.size(); ++i )
114     {
115         const char *s, *v;
117         e.get( i, &s, &v );
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" ) )
126         {
127             if ( atoi( v ) )
128                 select();
129             else
130                 deselect();
131         }
132         else if ( ! strcmp( s, ":sequence" ) )
133         {
134             int i;
135             sscanf( v, "%X", &i );
136             Sequence *t = (Sequence*)Loggable::find( i );
138             ASSERT( t, "No such object ID (%s)", v );
140             t->add( this );
141         }
142 //                else
143 //                    e.erase( i );
144     }
146     if ( _sequence )
147     {
148         _sequence->handle_widget_change( _r->start, _r->length );
149         _sequence->redraw();
150     }
153 void
154 Sequence_Widget::begin_drag ( const Drag &d )
156     _drag = new Drag( d );
158     timeline->rdlock();
160     _r = new Range( _range );
162     timeline->unlock();
165 void
166 Sequence_Widget::end_drag ( void )
168     timeline->wrlock();
170     /* swap in the new value */
171     _range = *_r;
173     timeline->unlock();
175     delete _r;
176     _r = &_range;
178     delete _drag;
179     _drag = NULL;
181     sequence()->handle_widget_change( _r->start, _r->length );
184 /** set position of widget on the timeline. */
185 void
186 Sequence_Widget::start ( nframes_t where )
188     /* this is pretty complicated because of selection and snapping */
190     if  ( ! selected() )
191     {
192         redraw();
193         _r->start = where;
194     }
195     else
196     {
197         if ( this != Sequence_Widget::_current )
198             return;
200         long d = where - _r->start;
202         if ( d < 0 )
203         {
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 );
210             d = 0 - d;
212             if ( m <= (nframes_t)d )
213                 d = m;
215             for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); ++i )
216             {
217                 (*i)->redraw();
218                 (*i)->_r->start -= d;
219             }
220         }
221         else
222         {
223             /* TODO: do like the above and disallow wrapping */
224             for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); ++i )
225             {
226                 (*i)->redraw();
227                 (*i)->_r->start += d;
228             }
229         }
230     }
233 void
234 Sequence_Widget::draw_label ( void )
238 void
239 Sequence_Widget::draw_label ( const char *label, Fl_Align align, Fl_Color color, int xo, int yo )
241     int X = x();
242     int Y = y();
243     int W = w();
244     int H = h();
246     if ( align & FL_ALIGN_CLIP ) fl_push_clip( X, Y, W, H );
248     X += xo;
249     Y += yo;
251     Fl_Label lab;
253     lab.color = color;
254 //    lab.type = FL_SHADOW_LABEL;
255     lab.type = FL_NORMAL_LABEL;
256     lab.value = label;
257     lab.font = FL_HELVETICA_ITALIC;
258     lab.size = 9;
260     int lw = 0, lh = 0;
262     fl_font( lab.font, lab.size );
263     fl_measure( lab.value, lw, lh );
265     int dx = 0;
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 )
279     {
280         if ( align & FL_ALIGN_BOTTOM  )
281             Y += h() - ( lh + bh );
282         else if ( align & FL_ALIGN_TOP )
283             Y += by;
284         else
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 )
290             X += bx;
291         else
292             X += ( abs_w() / 2 ) - ( ( lw + bw ) / 2 );
294     }
295     else
296     {
297         if ( align & FL_ALIGN_RIGHT )
298             X += abs_w();
299         else if ( align & FL_ALIGN_LEFT )
300             X -= lw + bw;
301         else
302             X += ( abs_w() / 2 ) - ( ( lw + bw ) / 2 );
304         if ( align & FL_ALIGN_BOTTOM  )
305             Y += h();
306         else if ( align & FL_ALIGN_TOP )
307             Y -= lh + bh;
308         else
309             Y += ( h() / 2 ) - ( ( lh + bh ) / 2 );
310     }
312     fl_draw_box( b, ( X - dx ), Y - by, lw + bw, lh + bh, fl_color_add_alpha( FL_DARK1, 150 )  );
314     fl_color( color );
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;
326     if ( selected() )
327     {
328         Loggable::block_start();
330         int r = 0;
332         for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); i++ )
333             if ( *i != this )
334                 r |= (*i)->handle( m );
336         r |= handle( m );
338         Loggable::block_end();
340         return r;
341     }
342     else
343         return handle( m );
346 void
347 Sequence_Widget::draw ( void )
349     draw_box();
352 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();
368     Logger _log( this );
370     switch ( m )
371     {
372         case FL_ENTER:
373             fl_cursor( FL_CURSOR_HAND );
374             return 1;
375         case FL_LEAVE:
376 //            DMESSAGE( "leave" );
377             fl_cursor( sequence()->cursor() );
378             return 1;
379         case FL_PUSH:
380         {
381             /* deletion */
382             if ( test_press( FL_BUTTON3 + FL_CTRL ) )
383             {
384                 remove();
385                 return 1;
386             }
387             else if ( test_press( FL_BUTTON1 ) || test_press( FL_BUTTON1 + FL_CTRL ) )
388             {
389                 /* traditional selection model */
390                 if ( Fl::event_ctrl() )
391                     select();
393                 fl_cursor( FL_CURSOR_MOVE );
395                 /* movement drag */
396                 return 1;
397             }
399             return 0;
400         }
401         case FL_RELEASE:
403             if ( _drag )
404             {
405                 end_drag();
406                 _log.release();
407             }
409             fl_cursor( FL_CURSOR_HAND );
411             return 1;
412         case FL_DRAG:
413         {
414             Fl::event_key( 0 );
416             if ( ! _drag )
417             {
418                 begin_drag ( Drag( x() - X, y() - Y, x_to_offset( X ) ) );
419                 _log.hold();
420             }
422             if ( test_press( FL_BUTTON1 + FL_CTRL ) && ! _drag->state )
423             {
424                 /* duplication */
425                 sequence()->add( this->clone() );
427                 _drag->state = 1;
428                 return 1;
429             }
430             else if ( test_press( FL_BUTTON1 ) || test_press( FL_BUTTON1 + FL_CTRL ) )
431             {
432                 redraw();
434                 const nframes_t of = timeline->x_to_offset( X );
436                 if ( of >= _drag->start )
437                     start( of - _drag->start );
438                 else
439                     start( 0 );
441                 if ( Sequence_Widget::_current == this )
442                     sequence()->snap( this );
444                 if ( X >= sequence()->x() + sequence()->w() ||
445                      X <= sequence()->x() )
446                 {
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() )
454                     {
456                         if ( pos > d )
457                             pos -= d;
458                         else
459                             pos = 0;
460                     }
461                     else
462                         pos += d;
464                     timeline->xposition( timeline->ts_to_x(  pos ) );
466 //                    timeline->update_length( start() + length() );
468                     /* FIXME: why isn't this enough? */
469                     sequence()->redraw();
470                 }
472                 if ( ! selected() || _selection.size() == 1 )
473                 {
474                     /* track jumping */
475                     if ( Y > y() + h() || Y < y() )
476                     {
477                         Track *t = timeline->track_under( Y );
479                         fl_cursor( (Fl_Cursor)1 );
481                         if ( t )
482                             t->handle( FL_ENTER );
484                         return 0;
485                     }
486                 }
488                 return 1;
489             }
490             else
491             {
492                 DMESSAGE( "unknown" );
493                 return 0;
494             }
495         }
496         default:
497             return 0;
498     }
502 /**********/
503 /* Public */
504 /**********/
506 /** add this widget to the selection */
507 void
508 Sequence_Widget::select ( void )
510     if ( selected() )
511         return;
513     _selection.push_back( this );
514     _selection.sort( sort_func );
516     redraw();
519 /** remove this widget from the selection */
520 void
521 Sequence_Widget::deselect ( void )
523     _selection.remove( this );
524     redraw();
527 bool
528 Sequence_Widget::selected ( void ) const
530     return std::find( _selection.begin(), _selection.end(), this ) != _selection.end();
533 /** remove this widget from its sequence */
534 void
535 Sequence_Widget::remove ( void )
537     redraw();
538     sequence()->queue_delete( this );
541 void
542 Sequence_Widget::delete_selected ( void )
544     Loggable::block_start();
546     while ( _selection.size() )
547         delete _selection.front();
549     Loggable::block_end();
552 void
553 Sequence_Widget::select_none ( void )
555     Loggable::block_start();
557     while ( _selection.size() )
558     {
559         Sequence_Widget *w = _selection.front();
561         w->log_start();
563         _selection.front()->redraw();
564         _selection.pop_front();
566         w->log_end();
567     }
569     Loggable::block_end();