Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
11KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #pragma once
  19. #include "Sequence.H"
  20. #include "Loggable.H"
  21. #include "Timeline.H"
  22. #include <list>
  23. #include <algorithm>
  24. using namespace std;
  25. class Sequence_Widget;
  26. struct Drag
  27. {
  28. /* mouse coords at start of drag */
  29. int x;
  30. int y;
  31. int state;
  32. Sequence_Widget *original;
  33. Drag( int X, int Y ) : x( X ), y( Y ) { state = 0; }
  34. };
  35. struct Range
  36. {
  37. nframes_t offset; /* where on the timeline */
  38. nframes_t start; /* first sample from clip */
  39. nframes_t end; /* last sample from clip */
  40. };
  41. /* Base class for virtual widget on a track */
  42. class Sequence_Widget : public Loggable
  43. {
  44. static list <Sequence_Widget *> _selection; /* all the widgets making up the selection */
  45. /* FIXME: is this not the same as /pushed/? */
  46. static Sequence_Widget * _current; /* the widget initiating events that affect the selection */
  47. /* these are actually managed in the Sequence classes */
  48. static Sequence_Widget * _pushed; /* the widget receiving drag events (a copy) */
  49. static Sequence_Widget * _original; /* the original of the /pushed/ widget */
  50. static Sequence_Widget * _belowmouse; /* the widget below the mouse cursor */
  51. /* can't have this */
  52. Sequence_Widget ( const Sequence_Widget &rhs );
  53. protected:
  54. Sequence *_track; /* track this region belongs to */
  55. Range _range; /* range for playback */
  56. Range *_r; /* range for editing / display (points to the same thing as above, except for when dragging etc) */
  57. Fl_Color _color; /* color of waveform */
  58. Fl_Color _box_color; /* color of background (box) */
  59. Drag *_drag;
  60. virtual void get ( Log_Entry &e ) const
  61. {
  62. e.add( ":x", _r->offset );
  63. e.add( ":l", _r->start );
  64. e.add( ":r", _r->end );
  65. e.add( ":t", _track );
  66. e.add( ":s", selected() );
  67. }
  68. virtual void
  69. set ( Log_Entry &e )
  70. {
  71. for ( int i = 0; i < e.size(); ++i )
  72. {
  73. const char *s, *v;
  74. e.get( i, &s, &v );
  75. if ( ! strcmp( s, ":x" ) )
  76. _r->offset = atol( v );
  77. else if ( ! strcmp( s, ":l" ) )
  78. _r->start = atol( v );
  79. else if ( ! strcmp( s, ":r" ) )
  80. _r->end = atol( v );
  81. else if ( ! strcmp( s, ":s" ) )
  82. {
  83. if ( atoi( v ) )
  84. select();
  85. else
  86. deselect();
  87. }
  88. else if ( ! strcmp( s, ":t" ) )
  89. {
  90. int i;
  91. sscanf( v, "%X", &i );
  92. Sequence *t = (Sequence*)Loggable::find( i );
  93. assert( t );
  94. t->add( this );
  95. }
  96. // else
  97. // e.erase( i );
  98. }
  99. if ( _track )
  100. _track->redraw();
  101. }
  102. public:
  103. Sequence_Widget ( )
  104. {
  105. _track = NULL;
  106. _r = &_range;
  107. _r->offset = _r->start = _r->end = 0;
  108. _drag = NULL;
  109. _box_color = FL_BACKGROUND_COLOR;
  110. _color = FL_FOREGROUND_COLOR;
  111. }
  112. virtual ~Sequence_Widget ( )
  113. {
  114. redraw();
  115. _track->remove( this );
  116. _selection.remove( this );
  117. }
  118. const Sequence_Widget &
  119. operator= ( const Sequence_Widget &rhs )
  120. {
  121. if ( this == &rhs )
  122. return *this;
  123. _r = &_range;
  124. _range = rhs._range;
  125. _track = rhs._track;
  126. _box_color = rhs._box_color;
  127. _color = rhs._color;
  128. return *this;
  129. }
  130. /* Sequence_Widget ( const Sequence_Widget &rhs ) */
  131. /* { */
  132. /* *this = rhs; */
  133. /* } */
  134. virtual Sequence_Widget *clone ( const Sequence_Widget *r ) = 0;
  135. bool selected ( void ) const
  136. {
  137. return ::find( _selection.begin(), _selection.end(), this ) != _selection.end();
  138. }
  139. void select ( void )
  140. {
  141. if ( selected() )
  142. return;
  143. _selection.push_back( this );
  144. _selection.sort( sort_func );
  145. redraw();
  146. }
  147. void deselect ( void )
  148. {
  149. _selection.remove( this );
  150. redraw();
  151. }
  152. static void
  153. delete_selected ( void )
  154. {
  155. while ( _selection.size() )
  156. delete _selection.front();
  157. }
  158. static Sequence_Widget *current ( void ) { return Sequence_Widget::_current; }
  159. static Sequence_Widget *pushed ( void ) { return Sequence_Widget::_pushed; }
  160. static Sequence_Widget *belowmouse ( void ) { return Sequence_Widget::_belowmouse; }
  161. static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
  162. static void belowmouse ( Sequence_Widget *w ) { Sequence_Widget::_belowmouse = w; }
  163. // static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
  164. void begin_drag ( const Drag &d )
  165. {
  166. _drag = new Drag( d );
  167. _r = new Range( _range );
  168. }
  169. void end_drag ( void )
  170. {
  171. _range = *_r;
  172. delete _r;
  173. _r = &_range;
  174. delete _drag;
  175. _drag = NULL;
  176. }
  177. void
  178. offset ( nframes_t where )
  179. {
  180. if ( ! selected() )
  181. {
  182. redraw();
  183. _r->offset = where;
  184. }
  185. else
  186. {
  187. long d = where - _r->offset;
  188. for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); i++ )
  189. {
  190. (*i)->redraw();
  191. if ( d < 0 )
  192. (*i)->_r->offset -= 0 - d;
  193. else
  194. (*i)->_r->offset += d;
  195. }
  196. }
  197. }
  198. int dispatch ( int m );
  199. Fl_Widget * parent ( void ) const { return _track; }
  200. int scroll_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
  201. nframes_t scroll_ts ( void ) const { return timeline->xoffset; }
  202. virtual int y ( void ) const { return _track->y(); }
  203. virtual int h ( void ) const { return _track->h(); }
  204. /* used by regions */
  205. virtual int x ( void ) const { return _r->offset < timeline->xoffset ? _track->x() - 1 : min( 32767, _track->x() + timeline->ts_to_x( _r->offset - timeline->xoffset ) ); }
  206. /* use this as x() when you need to draw lines between widgets */
  207. int line_x ( void ) const
  208. {
  209. return _r->offset < timeline->xoffset ? max( -32768, _track->x() - timeline->ts_to_x( timeline->xoffset - _r->offset )) : min( 32767, _track->x() + timeline->ts_to_x( _r->offset - timeline->xoffset ) );
  210. }
  211. virtual int w ( void ) const
  212. {
  213. int tx = timeline->ts_to_x( _r->offset );
  214. int rw;
  215. if ( tx < scroll_x() )
  216. rw = abs_w() - (scroll_x() - tx);
  217. else
  218. rw = abs_w();
  219. return min( rw, _track->w() );
  220. }
  221. int abs_x ( void ) const { return timeline->ts_to_x( _r->offset ); }
  222. virtual int abs_w ( void ) const { return timeline->ts_to_x( _r->end - _r->start ); }
  223. Fl_Color color ( void ) { return _color; }
  224. void color ( Fl_Color v ) { _color = v; }
  225. Fl_Color box_color ( void ) { return _box_color; }
  226. Sequence * track ( void ) const { return _track; }
  227. void track ( Sequence *t ) { _track = t; }
  228. nframes_t offset ( void ) const { return _r->offset; }
  229. // void offset ( nframes_t o ) { _r->offset = o; }
  230. void end ( nframes_t v ) { _r->end = v; }
  231. nframes_t end ( void ) const { return _r->end; }
  232. void start ( nframes_t v ) { _r->start = v; }
  233. nframes_t start ( void ) const { return _r->start; }
  234. virtual nframes_t length ( void ) const { return _r->end - _r->start; }
  235. virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
  236. virtual Fl_Align align ( void ) const { return (Fl_Align)0; }
  237. virtual void
  238. redraw ( void )
  239. {
  240. if ( ! (align() & FL_ALIGN_INSIDE) )
  241. {
  242. // FIXME: to better..
  243. _track->redraw();
  244. }
  245. else
  246. _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
  247. }
  248. /* just draw a simple box */
  249. virtual void
  250. draw_box ( void )
  251. {
  252. fl_draw_box( box(), x(), y(), w(), h(), _box_color );
  253. }
  254. virtual void
  255. draw ( void )
  256. {
  257. draw_box();
  258. }
  259. bool
  260. operator< ( const Sequence_Widget & rhs )
  261. {
  262. return _r->offset < rhs._r->offset;
  263. }
  264. bool
  265. operator<=( const Sequence_Widget & rhs )
  266. {
  267. return _r->offset <= rhs._r->offset;
  268. }
  269. virtual void draw_label ( const char *label, Fl_Align align, Fl_Color color=(Fl_Color)0 );
  270. virtual int handle ( int m );
  271. static bool
  272. sort_func ( Sequence_Widget *lhs, Sequence_Widget *rhs )
  273. {
  274. return *lhs < *rhs;
  275. }
  276. };