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.

306 lines
9.2KB

  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. protected:
  52. Track *_track; /* track this region belongs to */
  53. Range _range; /* range for playback */
  54. Range *_r; /* range for editing / display (points to the same thing as above, except for when dragging etc) */
  55. Fl_Color _color; /* color of waveform */
  56. Fl_Color _box_color; /* color of background (box) */
  57. bool _shown;
  58. Drag *_drag;
  59. public:
  60. Track_Widget ( )
  61. {
  62. _track = NULL;
  63. _r = &_range;
  64. _r->offset = _r->start = _r->end = 0;
  65. _shown = true;
  66. _drag = NULL;
  67. }
  68. virtual ~Track_Widget ( )
  69. {
  70. redraw();
  71. _track->remove( this );
  72. _selection.remove( this );
  73. }
  74. Track_Widget ( const Track_Widget &rhs )
  75. {
  76. *_r = *rhs._r;
  77. _track = rhs._track;
  78. }
  79. virtual Track_Widget *clone ( const Track_Widget *r ) = 0;
  80. bool selected ( void )
  81. {
  82. return ::find( _selection.begin(), _selection.end(), this ) != _selection.end();
  83. }
  84. void select ( void )
  85. {
  86. if ( selected() )
  87. return;
  88. _selection.push_back( this );
  89. _selection.sort( sort_func );
  90. redraw();
  91. }
  92. void deselect ( void )
  93. {
  94. _selection.remove( this );
  95. redraw();
  96. }
  97. static void
  98. delete_selected ( void )
  99. {
  100. while ( _selection.size() )
  101. delete _selection.front();
  102. }
  103. static Track_Widget *current ( void ) { return Track_Widget::_current; }
  104. static Track_Widget *pushed ( void ) { return Track_Widget::_pushed; }
  105. static Track_Widget *belowmouse ( void ) { return Track_Widget::_belowmouse; }
  106. static void pushed ( Track_Widget *w ) { Track_Widget::_pushed = w; }
  107. static void belowmouse ( Track_Widget *w ) { Track_Widget::_belowmouse = w; }
  108. // static void pushed ( Track_Widget *w ) { Track_Widget::_pushed = w; }
  109. bool shown ( void ) const { return _shown; }
  110. void show ( void ) { _shown = true; }
  111. void hide ( void ) { _shown = false; }
  112. void begin_drag ( const Drag &d )
  113. {
  114. _drag = new Drag( d );
  115. _r = new Range( _range );
  116. }
  117. void end_drag ( void )
  118. {
  119. _range = *_r;
  120. delete _r;
  121. _r = &_range;
  122. delete _drag;
  123. _drag = NULL;
  124. }
  125. void
  126. offset ( nframes_t where )
  127. {
  128. if ( ! selected() )
  129. {
  130. redraw();
  131. _r->offset = where;
  132. }
  133. else
  134. {
  135. long d = where - _r->offset;
  136. for ( list <Track_Widget *>::iterator i = _selection.begin(); i != _selection.end(); i++ )
  137. {
  138. (*i)->redraw();
  139. if ( d < 0 )
  140. (*i)->_r->offset -= 0 - d;
  141. else
  142. (*i)->_r->offset += d;
  143. }
  144. }
  145. }
  146. int dispatch ( int m );
  147. Fl_Widget * parent ( void ) const { return _track; }
  148. int scroll_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
  149. nframes_t scroll_ts ( void ) const { return timeline->xoffset; }
  150. virtual int y ( void ) const { return _track->y(); }
  151. virtual int h ( void ) const { return _track->h(); }
  152. /* used by regions */
  153. 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 ) ); }
  154. /* use this as x() when you need to draw lines between widgets */
  155. int line_x ( void ) const
  156. {
  157. 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 ) );
  158. }
  159. virtual int w ( void ) const
  160. {
  161. int tx = timeline->ts_to_x( _r->offset );
  162. int rw;
  163. if ( tx < scroll_x() )
  164. rw = abs_w() - (scroll_x() - tx);
  165. else
  166. rw = abs_w();
  167. return min( rw, _track->w() );
  168. }
  169. int abs_x ( void ) const { return timeline->ts_to_x( _r->offset ); }
  170. virtual int abs_w ( void ) const { return timeline->ts_to_x( _r->end - _r->start ); }
  171. Fl_Color color ( void ) { return _color; }
  172. void color ( Fl_Color v ) { _color = v; }
  173. Fl_Color box_color ( void ) { return _box_color; }
  174. Track * track ( void ) const { return _track; }
  175. void track ( Track *t ) { _track = t; }
  176. nframes_t offset ( void ) const { return _r->offset; }
  177. // void offset ( nframes_t o ) { _r->offset = o; }
  178. void end ( nframes_t v ) { _r->end = v; }
  179. nframes_t end ( void ) const { return _r->end; }
  180. void start ( nframes_t v ) { _r->start = v; }
  181. nframes_t start ( void ) const { return _r->start; }
  182. virtual nframes_t length ( void ) const { return _r->end - _r->start; }
  183. virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
  184. virtual Fl_Align align ( void ) const { return (Fl_Align)0; }
  185. virtual void
  186. redraw ( void )
  187. {
  188. if ( ! (align() & FL_ALIGN_INSIDE) )
  189. {
  190. // FIXME: to better..
  191. _track->redraw();
  192. }
  193. else
  194. _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
  195. }
  196. /* just draw a simple box */
  197. virtual void
  198. draw_box ( int X, int Y, int W, int H )
  199. {
  200. if ( x() > X + W || x() + w() < X )
  201. return;
  202. fl_draw_box( box(), x(), y(), w(), h(), _box_color );
  203. }
  204. virtual void
  205. draw ( int X, int Y, int W, int H )
  206. {
  207. if ( x() > X + W || x() + w() < X )
  208. return;
  209. draw_box( X, Y, W, H );
  210. }
  211. bool
  212. operator< ( const Track_Widget & rhs )
  213. {
  214. return _r->offset < rhs._r->offset;
  215. }
  216. bool
  217. operator<=( const Track_Widget & rhs )
  218. {
  219. return _r->offset <= rhs._r->offset;
  220. }
  221. virtual void draw_label ( const char *label, Fl_Align align, Fl_Color color=(Fl_Color)0 );
  222. virtual int handle ( int m );
  223. static bool
  224. sort_func ( Track_Widget *lhs, Track_Widget *rhs )
  225. {
  226. return *lhs < *rhs;
  227. }
  228. };