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.

280 lines
8.7KB

  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. struct Drag
  26. {
  27. /* mouse coords at start of drag */
  28. int x;
  29. int y;
  30. int state;
  31. Drag( int X, int Y ) : x( X ), y( Y ) { state = 0; }
  32. };
  33. /* Base class for virtual widget on a track */
  34. class Track_Widget : public Loggable
  35. {
  36. static list <Track_Widget *> _selection; /* all the widgets making up the selection */
  37. /* FIXME: is this not the same as /pushed/? */
  38. static Track_Widget * _current; /* the widget initiating events that affect the selection */
  39. /* these are actually managed in the Track classes */
  40. static Track_Widget * _pushed; /* the widget receiving drag events (a copy) */
  41. static Track_Widget * _original; /* the original of the /pushed/ widget */
  42. static Track_Widget * _belowmouse; /* the widget below the mouse cursor */
  43. protected:
  44. Track *_track; /* track this region belongs to */
  45. nframes_t _offset; /* where on the timeline */
  46. nframes_t _start; /* first sample from clip */
  47. nframes_t _end; /* last sample from clip */
  48. Fl_Color _color; /* color of waveform */
  49. Fl_Color _box_color; /* color of background (box) */
  50. bool _shown;
  51. Drag *_drag;
  52. public:
  53. Track_Widget ( )
  54. {
  55. _track = NULL;
  56. _offset = _start = _end = 0;
  57. _shown = true;
  58. _drag = NULL;
  59. }
  60. virtual ~Track_Widget ( )
  61. {
  62. redraw();
  63. _track->remove( this );
  64. _selection.remove( this );
  65. }
  66. Track_Widget ( const Track_Widget &rhs )
  67. {
  68. _offset = rhs._offset;
  69. _track = rhs._track;
  70. }
  71. virtual Track_Widget *clone ( const Track_Widget *r ) = 0;
  72. bool selected ( void )
  73. {
  74. return ::find( _selection.begin(), _selection.end(), this ) != _selection.end();
  75. }
  76. void select ( void )
  77. {
  78. if ( selected() )
  79. return;
  80. _selection.push_back( this );
  81. _selection.sort( sort_func );
  82. redraw();
  83. }
  84. void deselect ( void )
  85. {
  86. _selection.remove( this );
  87. redraw();
  88. }
  89. static void
  90. delete_selected ( void )
  91. {
  92. while ( _selection.size() )
  93. delete _selection.front();
  94. }
  95. static Track_Widget *current ( void ) { return Track_Widget::_current; }
  96. static Track_Widget *pushed ( void ) { return Track_Widget::_pushed; }
  97. static Track_Widget *original ( void ) { return Track_Widget::_original; }
  98. static Track_Widget *belowmouse ( void ) { return Track_Widget::_belowmouse; }
  99. static void pushed ( Track_Widget *w ) { Track_Widget::_pushed = w; }
  100. static void original ( Track_Widget *w ) { Track_Widget::_original = w; }
  101. static void belowmouse ( Track_Widget *w ) { Track_Widget::_belowmouse = w; }
  102. // static void pushed ( Track_Widget *w ) { Track_Widget::_pushed = w; }
  103. bool shown ( void ) const { return _shown; }
  104. void show ( void ) { _shown = true; }
  105. void hide ( void ) { _shown = false; }
  106. void
  107. offset ( nframes_t where )
  108. {
  109. if ( ! selected() )
  110. {
  111. redraw();
  112. _offset = where;
  113. }
  114. else
  115. {
  116. long d = where - _offset;
  117. for ( list <Track_Widget *>::iterator i = _selection.begin(); i != _selection.end(); i++ )
  118. {
  119. (*i)->redraw();
  120. if ( d < 0 )
  121. (*i)->_offset -= 0 - d;
  122. else
  123. (*i)->_offset += d;
  124. }
  125. }
  126. }
  127. int dispatch ( int m );
  128. Fl_Widget * parent ( void ) const { return _track; }
  129. int scroll_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
  130. nframes_t scroll_ts ( void ) const { return timeline->xoffset; }
  131. virtual int y ( void ) const { return _track->y(); }
  132. virtual int h ( void ) const { return _track->h(); }
  133. /* used by regions */
  134. virtual int x ( void ) const { return _offset < timeline->xoffset ? _track->x() - 1 : min( 32767, _track->x() + timeline->ts_to_x( _offset - timeline->xoffset ) ); }
  135. /* use this as x() when you need to draw lines between widgets */
  136. int line_x ( void ) const
  137. {
  138. return _offset < timeline->xoffset ? max( -32768, _track->x() - timeline->ts_to_x( timeline->xoffset - _offset )) : min( 32767, _track->x() + timeline->ts_to_x( _offset - timeline->xoffset ) );
  139. }
  140. virtual int w ( void ) const
  141. {
  142. int tx = timeline->ts_to_x( _offset );
  143. int rw;
  144. if ( tx < scroll_x() )
  145. rw = abs_w() - (scroll_x() - tx);
  146. else
  147. rw = abs_w();
  148. return min( rw, _track->w() );
  149. }
  150. int abs_x ( void ) const { return timeline->ts_to_x( _offset ); }
  151. virtual int abs_w ( void ) const { return timeline->ts_to_x( _end - _start ); }
  152. Fl_Color color ( void ) { return _color; }
  153. void color ( Fl_Color v ) { _color = v; }
  154. Fl_Color box_color ( void ) { return _box_color; }
  155. Track * track ( void ) const { return _track; }
  156. void track ( Track *t ) { _track = t; }
  157. nframes_t offset ( void ) const { return _offset; }
  158. // void offset ( nframes_t o ) { _offset = o; }
  159. void end ( nframes_t v ) { _end = v; }
  160. nframes_t end ( void ) const { return _end; }
  161. void start ( nframes_t v ) { _start = v; }
  162. nframes_t start ( void ) const { return _start; }
  163. virtual nframes_t length ( void ) const { return _end - _start; }
  164. virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
  165. virtual Fl_Align align ( void ) const { return (Fl_Align)0; }
  166. virtual void
  167. redraw ( void )
  168. {
  169. if ( ! (align() & FL_ALIGN_INSIDE) )
  170. {
  171. // FIXME: to better..
  172. _track->redraw();
  173. }
  174. else
  175. _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
  176. }
  177. /* just draw a simple box */
  178. virtual void
  179. draw_box ( int X, int Y, int W, int H )
  180. {
  181. if ( x() > X + W || x() + w() < X )
  182. return;
  183. fl_draw_box( box(), x(), y(), w(), h(), _box_color );
  184. }
  185. virtual void
  186. draw ( int X, int Y, int W, int H )
  187. {
  188. if ( x() > X + W || x() + w() < X )
  189. return;
  190. draw_box( X, Y, W, H );
  191. }
  192. bool
  193. operator< ( const Track_Widget & rhs )
  194. {
  195. return _offset < rhs._offset;
  196. }
  197. bool
  198. operator<=( const Track_Widget & rhs )
  199. {
  200. return _offset <= rhs._offset;
  201. }
  202. virtual void draw_label ( const char *label, Fl_Align align, Fl_Color color=(Fl_Color)0 );
  203. virtual int handle ( int m );
  204. static bool
  205. sort_func ( Track_Widget *lhs, Track_Widget *rhs )
  206. {
  207. return *lhs < *rhs;
  208. }
  209. };