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.

247 lines
7.3KB

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