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.

358 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 offset of drag */
  29. int x;
  30. int y;
  31. int state;
  32. nframes_t start;
  33. Drag( int X, int Y, nframes_t start=0 ) : x( X ), y( Y ), start( start ) { state = 0; }
  34. };
  35. struct Range
  36. {
  37. nframes_t start; /* where on the timeline */
  38. nframes_t offset; /* first sample from clip */
  39. nframes_t length; /* total number of samples */
  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. static Fl_Color _selection_color;
  52. protected:
  53. Sequence *_sequence; /* track this region belongs to */
  54. Range _range; /* range for playback */
  55. Range *_r; /* range for editing / display (points to the same thing as above, except for when dragging etc) */
  56. Fl_Color _color; /* color of waveform */
  57. Fl_Color _box_color; /* color of background (box) */
  58. Drag *_drag;
  59. virtual void get ( Log_Entry &e ) const;
  60. virtual void set ( Log_Entry &e );
  61. /* careful with this, it doesn't journal */
  62. Sequence_Widget ( const Sequence_Widget &rhs ) : Loggable( rhs )
  63. {
  64. _drag = NULL;
  65. _sequence = rhs._sequence;
  66. _range = rhs._range;
  67. _r = &_range;
  68. _color = rhs._color;
  69. _box_color = rhs._box_color;
  70. };
  71. public:
  72. Sequence_Widget ( )
  73. {
  74. _sequence = NULL;
  75. _r = &_range;
  76. _r->start = _r->offset = _r->length = 0;
  77. _drag = NULL;
  78. _box_color = FL_BACKGROUND_COLOR;
  79. _color = FL_FOREGROUND_COLOR;
  80. }
  81. virtual ~Sequence_Widget ( )
  82. {
  83. redraw();
  84. _sequence->remove( this );
  85. _selection.remove( this );
  86. }
  87. const Sequence_Widget &
  88. operator= ( const Sequence_Widget &rhs )
  89. {
  90. if ( this == &rhs )
  91. return *this;
  92. _r = &_range;
  93. _range = rhs._range;
  94. _sequence = rhs._sequence;
  95. _box_color = rhs._box_color;
  96. _color = rhs._color;
  97. return *this;
  98. }
  99. /* Sequence_Widget ( const Sequence_Widget &rhs ) */
  100. /* { */
  101. /* *this = rhs; */
  102. /* } */
  103. #define SEQUENCE_WIDGET_CLONE_FUNC(class) \
  104. virtual Sequence_Widget *clone ( void ) const \
  105. { \
  106. return new class ( *this ); \
  107. } \
  108. \
  109. // virtual Sequence_Widget *clone ( const Sequence_Widget *r ) = 0;
  110. virtual Sequence_Widget *clone ( void ) const = 0;
  111. bool selected ( void ) const
  112. {
  113. return ::find( _selection.begin(), _selection.end(), this ) != _selection.end();
  114. }
  115. void select ( void )
  116. {
  117. if ( selected() )
  118. return;
  119. _selection.push_back( this );
  120. _selection.sort( sort_func );
  121. redraw();
  122. }
  123. void deselect ( void )
  124. {
  125. _selection.remove( this );
  126. redraw();
  127. }
  128. static void
  129. delete_selected ( void )
  130. {
  131. while ( _selection.size() )
  132. delete _selection.front();
  133. }
  134. static void
  135. select_none ( void )
  136. {
  137. while ( _selection.size() )
  138. {
  139. _selection.front()->redraw();
  140. _selection.pop_front();
  141. }
  142. }
  143. static Sequence_Widget *current ( void ) { return Sequence_Widget::_current; }
  144. static Sequence_Widget *pushed ( void ) { return Sequence_Widget::_pushed; }
  145. static Sequence_Widget *belowmouse ( void ) { return Sequence_Widget::_belowmouse; }
  146. static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
  147. static void belowmouse ( Sequence_Widget *w ) { Sequence_Widget::_belowmouse = w; }
  148. // static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
  149. void begin_drag ( const Drag &d )
  150. {
  151. _drag = new Drag( d );
  152. _r = new Range( _range );
  153. }
  154. void length_drag ( void )
  155. {
  156. _range = *_r;
  157. delete _r;
  158. _r = &_range;
  159. delete _drag;
  160. _drag = NULL;
  161. }
  162. void
  163. start ( nframes_t where )
  164. {
  165. if ( ! selected() )
  166. {
  167. redraw();
  168. _r->start = where;
  169. }
  170. else
  171. {
  172. long d = where - _r->start;
  173. for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); i++ )
  174. {
  175. (*i)->redraw();
  176. if ( d < 0 )
  177. (*i)->_r->start -= 0 - d;
  178. else
  179. (*i)->_r->start += d;
  180. }
  181. }
  182. }
  183. int dispatch ( int m );
  184. Fl_Widget * parent ( void ) const { return _sequence; }
  185. int scroll_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
  186. nframes_t scroll_ts ( void ) const { return timeline->xoffset; }
  187. virtual int y ( void ) const { return _sequence->y(); }
  188. virtual int h ( void ) const { return _sequence->h(); }
  189. /* used by regions */
  190. virtual int x ( void ) const
  191. {
  192. return _r->start < timeline->xoffset ? _sequence->x() : min( _sequence->x() + _sequence->w(), _sequence->x() + timeline->ts_to_x( _r->start - timeline->xoffset ) );
  193. }
  194. /* use this as x() when you need to draw lines between widgets */
  195. int line_x ( void ) const
  196. {
  197. return _r->start < timeline->xoffset ? max( -32768, _sequence->x() - timeline->ts_to_x( timeline->xoffset - _r->start )) : min( 32767, _sequence->x() + timeline->ts_to_x( _r->start - timeline->xoffset ) );
  198. }
  199. virtual int w ( void ) const
  200. {
  201. int tx = timeline->ts_to_x( _r->start );
  202. int rw;
  203. if ( tx < scroll_x() )
  204. rw = abs_w() - (scroll_x() - tx);
  205. else
  206. rw = abs_w();
  207. return min( rw, _sequence->w() );
  208. }
  209. int abs_x ( void ) const { return timeline->ts_to_x( _r->start ); }
  210. virtual int abs_w ( void ) const { return timeline->ts_to_x( _r->length ); }
  211. Fl_Color color ( void ) const { return _color; }
  212. void color ( Fl_Color v ) { _color = v; }
  213. Fl_Color box_color ( void ) const { return _box_color; }
  214. virtual Fl_Color selection_color ( void ) const { return _selection_color; }
  215. virtual void selection_color ( Fl_Color v ) { _selection_color = v; }
  216. Sequence * sequence ( void ) const { return _sequence; }
  217. void sequence ( Sequence *t ) { _sequence = t; }
  218. nframes_t start ( void ) const { return _r->start; }
  219. // void start ( nframes_t o ) { _r->start = o; }
  220. void length ( nframes_t v ) { _r->length = v; }
  221. virtual nframes_t length ( void ) const { return _r->length; }
  222. void offset ( nframes_t v ) { _r->offset = v; }
  223. nframes_t offset ( void ) const { return _r->offset; }
  224. /** convert a screen x coord into an start into the region */
  225. nframes_t x_to_offset ( int X )
  226. {
  227. return timeline->x_to_ts( scroll_x() + ( X - _sequence->x() ) ) - _r->start;
  228. }
  229. int active_r ( void ) const { return _sequence->active_r(); }
  230. virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
  231. virtual Fl_Align align ( void ) const { return (Fl_Align)0; }
  232. virtual void
  233. redraw ( void )
  234. {
  235. if ( ! (align() & FL_ALIGN_INSIDE) )
  236. {
  237. // FIXME: to better..
  238. _sequence->redraw();
  239. }
  240. else
  241. _sequence->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
  242. }
  243. /* just draw a simple box */
  244. virtual void
  245. draw_box ( void )
  246. {
  247. fl_draw_box( box(), x(), y(), w(), h(), selected() ? FL_MAGENTA : _box_color );
  248. }
  249. virtual void
  250. draw ( void )
  251. {
  252. draw_box();
  253. }
  254. bool
  255. operator< ( const Sequence_Widget & rhs )
  256. {
  257. return _r->start < rhs._r->start;
  258. }
  259. bool
  260. operator<=( const Sequence_Widget & rhs )
  261. {
  262. return _r->start <= rhs._r->start;
  263. }
  264. virtual void draw_label ( const char *label, Fl_Align align, Fl_Color color=(Fl_Color)0 );
  265. virtual int handle ( int m );
  266. static bool
  267. sort_func ( Sequence_Widget *lhs, Sequence_Widget *rhs )
  268. {
  269. return *lhs < *rhs;
  270. }
  271. };