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.

361 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
  61. get ( Log_Entry &e )
  62. {
  63. e.add( ":x", _r->offset );
  64. e.add( ":l", _r->start );
  65. e.add( ":r", _r->end );
  66. e.add( ":t", _track );
  67. e.add( ":s", selected() );
  68. }
  69. virtual void
  70. set ( Log_Entry &e )
  71. {
  72. for ( int i = 0; i < e.size(); ++i )
  73. {
  74. const char *s, *v;
  75. e.get( i, &s, &v );
  76. if ( ! strcmp( s, ":x" ) )
  77. _r->offset = atol( v );
  78. else if ( ! strcmp( s, ":l" ) )
  79. _r->start = atol( v );
  80. else if ( ! strcmp( s, ":r" ) )
  81. _r->end = atol( v );
  82. else if ( ! strcmp( s, ":s" ) )
  83. {
  84. if ( atoi( v ) )
  85. select();
  86. else
  87. deselect();
  88. }
  89. else if ( ! strcmp( s, ":t" ) )
  90. {
  91. int i;
  92. sscanf( v, "%X", &i );
  93. Sequence *t = (Sequence*)Loggable::find( i );
  94. assert( t );
  95. t->add( this );
  96. }
  97. // else
  98. // e.erase( i );
  99. }
  100. if ( _track )
  101. _track->redraw();
  102. }
  103. public:
  104. Sequence_Widget ( )
  105. {
  106. _track = NULL;
  107. _r = &_range;
  108. _r->offset = _r->start = _r->end = 0;
  109. _drag = NULL;
  110. }
  111. virtual ~Sequence_Widget ( )
  112. {
  113. redraw();
  114. _track->remove( this );
  115. _selection.remove( this );
  116. }
  117. const Sequence_Widget &
  118. operator= ( const Sequence_Widget &rhs )
  119. {
  120. if ( this == &rhs )
  121. return *this;
  122. _r = &_range;
  123. _range = rhs._range;
  124. _track = rhs._track;
  125. _box_color = rhs._box_color;
  126. _color = rhs._color;
  127. return *this;
  128. }
  129. /* Sequence_Widget ( const Sequence_Widget &rhs ) */
  130. /* { */
  131. /* *this = rhs; */
  132. /* } */
  133. virtual Sequence_Widget *clone ( const Sequence_Widget *r ) = 0;
  134. bool selected ( void )
  135. {
  136. return ::find( _selection.begin(), _selection.end(), this ) != _selection.end();
  137. }
  138. void select ( void )
  139. {
  140. if ( selected() )
  141. return;
  142. _selection.push_back( this );
  143. _selection.sort( sort_func );
  144. redraw();
  145. }
  146. void deselect ( void )
  147. {
  148. _selection.remove( this );
  149. redraw();
  150. }
  151. static void
  152. delete_selected ( void )
  153. {
  154. while ( _selection.size() )
  155. delete _selection.front();
  156. }
  157. static Sequence_Widget *current ( void ) { return Sequence_Widget::_current; }
  158. static Sequence_Widget *pushed ( void ) { return Sequence_Widget::_pushed; }
  159. static Sequence_Widget *belowmouse ( void ) { return Sequence_Widget::_belowmouse; }
  160. static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
  161. static void belowmouse ( Sequence_Widget *w ) { Sequence_Widget::_belowmouse = w; }
  162. // static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
  163. void begin_drag ( const Drag &d )
  164. {
  165. _drag = new Drag( d );
  166. _r = new Range( _range );
  167. }
  168. void end_drag ( void )
  169. {
  170. _range = *_r;
  171. delete _r;
  172. _r = &_range;
  173. delete _drag;
  174. _drag = NULL;
  175. }
  176. void
  177. offset ( nframes_t where )
  178. {
  179. if ( ! selected() )
  180. {
  181. redraw();
  182. _r->offset = where;
  183. }
  184. else
  185. {
  186. long d = where - _r->offset;
  187. for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); i++ )
  188. {
  189. (*i)->redraw();
  190. if ( d < 0 )
  191. (*i)->_r->offset -= 0 - d;
  192. else
  193. (*i)->_r->offset += d;
  194. }
  195. }
  196. }
  197. int dispatch ( int m );
  198. Fl_Widget * parent ( void ) const { return _track; }
  199. int scroll_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
  200. nframes_t scroll_ts ( void ) const { return timeline->xoffset; }
  201. virtual int y ( void ) const { return _track->y(); }
  202. virtual int h ( void ) const { return _track->h(); }
  203. /* used by regions */
  204. 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 ) ); }
  205. /* use this as x() when you need to draw lines between widgets */
  206. int line_x ( void ) const
  207. {
  208. 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 ) );
  209. }
  210. virtual int w ( void ) const
  211. {
  212. int tx = timeline->ts_to_x( _r->offset );
  213. int rw;
  214. if ( tx < scroll_x() )
  215. rw = abs_w() - (scroll_x() - tx);
  216. else
  217. rw = abs_w();
  218. return min( rw, _track->w() );
  219. }
  220. int abs_x ( void ) const { return timeline->ts_to_x( _r->offset ); }
  221. virtual int abs_w ( void ) const { return timeline->ts_to_x( _r->end - _r->start ); }
  222. Fl_Color color ( void ) { return _color; }
  223. void color ( Fl_Color v ) { _color = v; }
  224. Fl_Color box_color ( void ) { return _box_color; }
  225. Sequence * track ( void ) const { return _track; }
  226. void track ( Sequence *t ) { _track = t; }
  227. nframes_t offset ( void ) const { return _r->offset; }
  228. // void offset ( nframes_t o ) { _r->offset = o; }
  229. void end ( nframes_t v ) { _r->end = v; }
  230. nframes_t end ( void ) const { return _r->end; }
  231. void start ( nframes_t v ) { _r->start = v; }
  232. nframes_t start ( void ) const { return _r->start; }
  233. virtual nframes_t length ( void ) const { return _r->end - _r->start; }
  234. virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
  235. virtual Fl_Align align ( void ) const { return (Fl_Align)0; }
  236. virtual void
  237. redraw ( void )
  238. {
  239. if ( ! (align() & FL_ALIGN_INSIDE) )
  240. {
  241. // FIXME: to better..
  242. _track->redraw();
  243. }
  244. else
  245. _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
  246. }
  247. /* just draw a simple box */
  248. virtual void
  249. draw_box ( void )
  250. {
  251. fl_draw_box( box(), x(), y(), w(), h(), _box_color );
  252. }
  253. virtual void
  254. draw ( void )
  255. {
  256. draw_box();
  257. }
  258. bool
  259. operator< ( const Sequence_Widget & rhs )
  260. {
  261. return _r->offset < rhs._r->offset;
  262. }
  263. bool
  264. operator<=( const Sequence_Widget & rhs )
  265. {
  266. return _r->offset <= rhs._r->offset;
  267. }
  268. virtual void draw_label ( const char *label, Fl_Align align, Fl_Color color=(Fl_Color)0 );
  269. virtual int handle ( int m );
  270. static bool
  271. sort_func ( Sequence_Widget *lhs, Sequence_Widget *rhs )
  272. {
  273. return *lhs < *rhs;
  274. }
  275. };