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.

304 lines
9.8KB

  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 "Sequence.H"
  20. #include "Loggable.H"
  21. #include "Timeline.H"
  22. #include <list>
  23. #include <algorithm>
  24. using std::min;
  25. using std::max;
  26. class Sequence_Widget;
  27. struct Drag
  28. {
  29. /* mouse coords at offset of drag */
  30. int x;
  31. int y;
  32. int state;
  33. nframes_t start;
  34. Drag( int X, int Y, nframes_t start=0 ) : x( X ), y( Y ), start( start ) { state = 0; }
  35. };
  36. /* most common position description. /offset/ is only used by Regions,
  37. but it's more convenient to have it here */
  38. struct Range
  39. {
  40. nframes_t start; /* where on the timeline */
  41. nframes_t offset; /* first sample from clip */
  42. nframes_t length; /* total number of samples */
  43. void
  44. trim_left ( long n )
  45. {
  46. start -= n;
  47. offset -= n;
  48. length += n;
  49. }
  50. void
  51. trim_right ( long n )
  52. {
  53. length += n;
  54. }
  55. void
  56. set_left ( nframes_t f )
  57. {
  58. offset += f - start;
  59. length -= f - start;
  60. start = f;
  61. }
  62. void
  63. set_right ( nframes_t f )
  64. {
  65. length = f - start;
  66. }
  67. Range ( ) : start( 0 ), offset( 0 ), length( 0 )
  68. {
  69. }
  70. };
  71. /* Used by time/tempo points or any other child of Sequence_Widget
  72. which must be locked to a point in musical time rather than wallclock
  73. time. Bar and beat start at 1. */
  74. struct BBT
  75. {
  76. unsigned short bar;
  77. unsigned char beat;
  78. unsigned short tick;
  79. BBT ( ) : bar( 0 ), beat( 0 ), tick( 0 )
  80. {
  81. }
  82. };
  83. /* FIXME: wrong place for this */
  84. struct position_info
  85. {
  86. nframes_t frame;
  87. float tempo;
  88. int beats_per_bar;
  89. int beat_type;
  90. BBT bbt;
  91. };
  92. #define SEQUENCE_WIDGET_CLONE_FUNC(class) \
  93. virtual Sequence_Widget *clone ( void ) const \
  94. { \
  95. return new class ( *this ); \
  96. }
  97. /* Base class for virtual widget on a track */
  98. class Sequence_Widget : public Loggable
  99. {
  100. static std::list <Sequence_Widget *> _selection; /* all the widgets making up the selection */
  101. /* FIXME: is this not the same as /pushed/? */
  102. static Sequence_Widget * _current; /* the widget initiating events that affect the selection */
  103. /* these are actually managed in the Sequence classes */
  104. static Sequence_Widget * _pushed; /* the widget receiving drag events (a copy) */
  105. static Sequence_Widget * _original; /* the original of the /pushed/ widget */
  106. static Sequence_Widget * _belowmouse; /* the widget below the mouse cursor */
  107. static Fl_Color _selection_color;
  108. protected:
  109. Sequence *_sequence; /* track this region belongs to */
  110. Range _range; /* range for playback */
  111. Range *_r; /* range for editing / display (points to the same thing as above, except for when dragging etc) */
  112. Fl_Color _color; /* color of waveform */
  113. Fl_Color _box_color; /* color of background (box) */
  114. Drag *_drag;
  115. virtual void get ( Log_Entry &e ) const;
  116. virtual void set ( Log_Entry &e );
  117. Sequence_Widget ( const Sequence_Widget &rhs );
  118. Sequence_Widget ( );
  119. const Sequence_Widget &
  120. operator= ( const Sequence_Widget &rhs );
  121. public:
  122. virtual ~Sequence_Widget ( );
  123. virtual Sequence_Widget *clone ( void ) const = 0;
  124. bool selected ( void ) const;
  125. static int nselected ( void )
  126. { return _selection.size(); }
  127. void select ( void );
  128. void deselect ( void );
  129. void remove ( void );
  130. static void delete_selected ( void );
  131. static void select_none ( void );
  132. static Sequence_Widget *current ( void ) { return Sequence_Widget::_current; }
  133. static Sequence_Widget *pushed ( void ) { return Sequence_Widget::_pushed; }
  134. static Sequence_Widget *belowmouse ( void ) { return Sequence_Widget::_belowmouse; }
  135. static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
  136. static void belowmouse ( Sequence_Widget *w ) { Sequence_Widget::_belowmouse = w; }
  137. void begin_drag ( const Drag &d );
  138. void end_drag ( void );
  139. int dispatch ( int m );
  140. Fl_Widget * parent ( void ) const { return _sequence; }
  141. int scroll_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
  142. nframes_t scroll_ts ( void ) const { return timeline->xoffset; }
  143. virtual int y ( void ) const { return _sequence->y(); }
  144. virtual int h ( void ) const { return _sequence->h(); }
  145. /* used by regions */
  146. virtual int x ( void ) const
  147. {
  148. return _r->start < timeline->xoffset ? _sequence->x() : min( _sequence->x() + _sequence->w(), _sequence->x() + timeline->ts_to_x( _r->start - timeline->xoffset ) );
  149. }
  150. /* use this as x() when you need to draw lines between widgets */
  151. int line_x ( void ) const
  152. {
  153. return _r->start < timeline->xoffset ? max( -32768, _sequence->x() - timeline->ts_to_x( timeline->xoffset - _r->start )) : min( 32767, _sequence->x() + timeline->ts_to_x( _r->start - timeline->xoffset ) );
  154. }
  155. virtual int w ( void ) const
  156. {
  157. int tx = timeline->ts_to_x( _r->start );
  158. int rw;
  159. if ( tx < scroll_x() )
  160. rw = abs_w() - (scroll_x() - tx);
  161. else
  162. rw = abs_w();
  163. return min( rw, _sequence->w() );
  164. }
  165. int abs_x ( void ) const { return timeline->ts_to_x( _r->start ); }
  166. virtual int abs_w ( void ) const { return timeline->ts_to_x( _r->length ); }
  167. Fl_Color color ( void ) const { return _color; }
  168. void color ( Fl_Color v ) { _color = v; }
  169. Fl_Color box_color ( void ) const { return _box_color; }
  170. void box_color ( Fl_Color v ) { _box_color = v; }
  171. virtual Fl_Color selection_color ( void ) const { return _selection_color; }
  172. virtual void selection_color ( Fl_Color v ) { _selection_color = v; }
  173. Sequence * sequence ( void ) const { return _sequence; }
  174. void sequence ( Sequence *t ) { _sequence = t; }
  175. nframes_t start ( void ) const { return _r->start; }
  176. /* void start ( nframes_t o ) { _r->start = o; } */
  177. void start ( nframes_t where );
  178. void length ( nframes_t v ) { _r->length = v; }
  179. virtual nframes_t length ( void ) const { return _r->length; }
  180. void offset ( nframes_t v ) { _r->offset = v; }
  181. nframes_t offset ( void ) const { return _r->offset; }
  182. /** convert a screen x coord into an start into the region */
  183. nframes_t x_to_offset ( int X )
  184. {
  185. return timeline->x_to_ts( scroll_x() + ( X - _sequence->x() ) ) - _r->start;
  186. }
  187. int active_r ( void ) const { return _sequence->active_r(); }
  188. /** returns true if widget /w/ begins and ends completely within the range of this widget */
  189. bool contains ( const Sequence_Widget *w ) const
  190. {
  191. return w->start() >= start() && w->start() + w->length() <= start() + length();
  192. }
  193. /** returns true of widget /w/ overlaps this widget in any place */
  194. bool overlaps ( const Sequence_Widget *w ) const
  195. {
  196. return ! ( w->start() > start() + length() || w->start() + w->length() < start() );
  197. }
  198. virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
  199. virtual Fl_Align align ( void ) const { return (Fl_Align)0; }
  200. virtual void
  201. redraw ( void )
  202. {
  203. if ( ! (align() & FL_ALIGN_INSIDE) )
  204. {
  205. // FIXME: to better..
  206. _sequence->redraw();
  207. }
  208. else
  209. _sequence->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
  210. }
  211. virtual void draw_box ( void );
  212. virtual void draw ( void );
  213. bool
  214. operator< ( const Sequence_Widget & rhs ) const
  215. {
  216. return _r->start < rhs._r->start;
  217. }
  218. bool
  219. operator<=( const Sequence_Widget & rhs ) const
  220. {
  221. return _r->start <= rhs._r->start;
  222. }
  223. virtual void draw_label ( const char *label, Fl_Align align, Fl_Color color=(Fl_Color)0, int xo=0, int yo=0 );
  224. virtual int handle ( int m );
  225. static bool
  226. sort_func ( const Sequence_Widget *lhs, const Sequence_Widget *rhs )
  227. {
  228. return *lhs < *rhs;
  229. }
  230. };