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.

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