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.

323 lines
9.6KB

  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 "Track.H"
  20. #include "Loggable.H"
  21. #include "Timeline.H"
  22. #include <list>
  23. #include <algorithm>
  24. using namespace std;
  25. class Track_Widget;
  26. struct Drag
  27. {
  28. /* mouse coords at start of drag */
  29. int x;
  30. int y;
  31. int state;
  32. Track_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 Track_Widget : public Loggable
  43. {
  44. static list <Track_Widget *> _selection; /* all the widgets making up the selection */
  45. /* FIXME: is this not the same as /pushed/? */
  46. static Track_Widget * _current; /* the widget initiating events that affect the selection */
  47. /* these are actually managed in the Track classes */
  48. static Track_Widget * _pushed; /* the widget receiving drag events (a copy) */
  49. static Track_Widget * _original; /* the original of the /pushed/ widget */
  50. static Track_Widget * _belowmouse; /* the widget below the mouse cursor */
  51. /* can't have this */
  52. Track_Widget ( const Track_Widget &rhs );
  53. protected:
  54. Track *_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. bool _shown;
  60. Drag *_drag;
  61. public:
  62. Track_Widget ( )
  63. {
  64. _track = NULL;
  65. _r = &_range;
  66. _r->offset = _r->start = _r->end = 0;
  67. _shown = true;
  68. _drag = NULL;
  69. }
  70. virtual ~Track_Widget ( )
  71. {
  72. redraw();
  73. _track->remove( this );
  74. _selection.remove( this );
  75. }
  76. const Track_Widget &
  77. operator= ( const Track_Widget &rhs )
  78. {
  79. if ( this == &rhs )
  80. return *this;
  81. _r = &_range;
  82. _range = rhs._range;
  83. _track = rhs._track;
  84. _box_color = rhs._box_color;
  85. _color = rhs._color;
  86. return *this;
  87. }
  88. /* Track_Widget ( const Track_Widget &rhs ) */
  89. /* { */
  90. /* *this = rhs; */
  91. /* } */
  92. virtual Track_Widget *clone ( const Track_Widget *r ) = 0;
  93. bool selected ( void )
  94. {
  95. return ::find( _selection.begin(), _selection.end(), this ) != _selection.end();
  96. }
  97. void select ( void )
  98. {
  99. if ( selected() )
  100. return;
  101. _selection.push_back( this );
  102. _selection.sort( sort_func );
  103. redraw();
  104. }
  105. void deselect ( void )
  106. {
  107. _selection.remove( this );
  108. redraw();
  109. }
  110. static void
  111. delete_selected ( void )
  112. {
  113. while ( _selection.size() )
  114. delete _selection.front();
  115. }
  116. static Track_Widget *current ( void ) { return Track_Widget::_current; }
  117. static Track_Widget *pushed ( void ) { return Track_Widget::_pushed; }
  118. static Track_Widget *belowmouse ( void ) { return Track_Widget::_belowmouse; }
  119. static void pushed ( Track_Widget *w ) { Track_Widget::_pushed = w; }
  120. static void belowmouse ( Track_Widget *w ) { Track_Widget::_belowmouse = w; }
  121. // static void pushed ( Track_Widget *w ) { Track_Widget::_pushed = w; }
  122. bool shown ( void ) const { return _shown; }
  123. void show ( void ) { _shown = true; }
  124. void hide ( void ) { _shown = false; }
  125. void begin_drag ( const Drag &d )
  126. {
  127. _drag = new Drag( d );
  128. _r = new Range( _range );
  129. }
  130. void end_drag ( void )
  131. {
  132. _range = *_r;
  133. delete _r;
  134. _r = &_range;
  135. delete _drag;
  136. _drag = NULL;
  137. }
  138. void
  139. offset ( nframes_t where )
  140. {
  141. if ( ! selected() )
  142. {
  143. redraw();
  144. _r->offset = where;
  145. }
  146. else
  147. {
  148. long d = where - _r->offset;
  149. for ( list <Track_Widget *>::iterator i = _selection.begin(); i != _selection.end(); i++ )
  150. {
  151. (*i)->redraw();
  152. if ( d < 0 )
  153. (*i)->_r->offset -= 0 - d;
  154. else
  155. (*i)->_r->offset += d;
  156. }
  157. }
  158. }
  159. int dispatch ( int m );
  160. Fl_Widget * parent ( void ) const { return _track; }
  161. int scroll_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
  162. nframes_t scroll_ts ( void ) const { return timeline->xoffset; }
  163. virtual int y ( void ) const { return _track->y(); }
  164. virtual int h ( void ) const { return _track->h(); }
  165. /* used by regions */
  166. 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 ) ); }
  167. /* use this as x() when you need to draw lines between widgets */
  168. int line_x ( void ) const
  169. {
  170. 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 ) );
  171. }
  172. virtual int w ( void ) const
  173. {
  174. int tx = timeline->ts_to_x( _r->offset );
  175. int rw;
  176. if ( tx < scroll_x() )
  177. rw = abs_w() - (scroll_x() - tx);
  178. else
  179. rw = abs_w();
  180. return min( rw, _track->w() );
  181. }
  182. int abs_x ( void ) const { return timeline->ts_to_x( _r->offset ); }
  183. virtual int abs_w ( void ) const { return timeline->ts_to_x( _r->end - _r->start ); }
  184. Fl_Color color ( void ) { return _color; }
  185. void color ( Fl_Color v ) { _color = v; }
  186. Fl_Color box_color ( void ) { return _box_color; }
  187. Track * track ( void ) const { return _track; }
  188. void track ( Track *t ) { _track = t; }
  189. nframes_t offset ( void ) const { return _r->offset; }
  190. // void offset ( nframes_t o ) { _r->offset = o; }
  191. void end ( nframes_t v ) { _r->end = v; }
  192. nframes_t end ( void ) const { return _r->end; }
  193. void start ( nframes_t v ) { _r->start = v; }
  194. nframes_t start ( void ) const { return _r->start; }
  195. virtual nframes_t length ( void ) const { return _r->end - _r->start; }
  196. virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
  197. virtual Fl_Align align ( void ) const { return (Fl_Align)0; }
  198. virtual void
  199. redraw ( void )
  200. {
  201. if ( ! (align() & FL_ALIGN_INSIDE) )
  202. {
  203. // FIXME: to better..
  204. _track->redraw();
  205. }
  206. else
  207. _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
  208. }
  209. /* just draw a simple box */
  210. virtual void
  211. draw_box ( int X, int Y, int W, int H )
  212. {
  213. if ( x() > X + W || x() + w() < X )
  214. return;
  215. fl_draw_box( box(), x(), y(), w(), h(), _box_color );
  216. }
  217. virtual void
  218. draw ( int X, int Y, int W, int H )
  219. {
  220. if ( x() > X + W || x() + w() < X )
  221. return;
  222. draw_box( X, Y, W, H );
  223. }
  224. bool
  225. operator< ( const Track_Widget & rhs )
  226. {
  227. return _r->offset < rhs._r->offset;
  228. }
  229. bool
  230. operator<=( const Track_Widget & rhs )
  231. {
  232. return _r->offset <= rhs._r->offset;
  233. }
  234. virtual void draw_label ( const char *label, Fl_Align align, Fl_Color color=(Fl_Color)0 );
  235. virtual int handle ( int m );
  236. static bool
  237. sort_func ( Track_Widget *lhs, Track_Widget *rhs )
  238. {
  239. return *lhs < *rhs;
  240. }
  241. };