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.

376 lines
11KB

  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. /* Base class for virtual widget on a track */
  93. class Sequence_Widget : public Loggable
  94. {
  95. static std::list <Sequence_Widget *> _selection; /* all the widgets making up the selection */
  96. /* FIXME: is this not the same as /pushed/? */
  97. static Sequence_Widget * _current; /* the widget initiating events that affect the selection */
  98. /* these are actually managed in the Sequence classes */
  99. static Sequence_Widget * _pushed; /* the widget receiving drag events (a copy) */
  100. static Sequence_Widget * _original; /* the original of the /pushed/ widget */
  101. static Sequence_Widget * _belowmouse; /* the widget below the mouse cursor */
  102. static Fl_Color _selection_color;
  103. protected:
  104. Sequence *_sequence; /* track this region belongs to */
  105. Range _range; /* range for playback */
  106. Range *_r; /* range for editing / display (points to the same thing as above, except for when dragging etc) */
  107. Fl_Color _color; /* color of waveform */
  108. Fl_Color _box_color; /* color of background (box) */
  109. Drag *_drag;
  110. virtual void get ( Log_Entry &e ) const;
  111. virtual void set ( Log_Entry &e );
  112. /* careful with this, it doesn't journal */
  113. Sequence_Widget ( const Sequence_Widget &rhs ) : Loggable( rhs )
  114. {
  115. _drag = NULL;
  116. _sequence = rhs._sequence;
  117. _range = rhs._range;
  118. _r = &_range;
  119. _color = rhs._color;
  120. _box_color = rhs._box_color;
  121. };
  122. public:
  123. Sequence_Widget ( )
  124. {
  125. _sequence = NULL;
  126. _r = &_range;
  127. _r->start = _r->offset = _r->length = 0;
  128. _drag = NULL;
  129. _box_color = FL_BACKGROUND_COLOR;
  130. _color = FL_FOREGROUND_COLOR;
  131. }
  132. virtual ~Sequence_Widget ( )
  133. {
  134. redraw();
  135. _sequence->remove( this );
  136. _selection.remove( this );
  137. }
  138. const Sequence_Widget &
  139. operator= ( const Sequence_Widget &rhs )
  140. {
  141. if ( this == &rhs )
  142. return *this;
  143. _r = &_range;
  144. _range = rhs._range;
  145. _sequence = rhs._sequence;
  146. _box_color = rhs._box_color;
  147. _color = rhs._color;
  148. return *this;
  149. }
  150. /* Sequence_Widget ( const Sequence_Widget &rhs ) */
  151. /* { */
  152. /* *this = rhs; */
  153. /* } */
  154. #define SEQUENCE_WIDGET_CLONE_FUNC(class) \
  155. virtual Sequence_Widget *clone ( void ) const \
  156. { \
  157. return new class ( *this ); \
  158. } \
  159. \
  160. // virtual Sequence_Widget *clone ( const Sequence_Widget *r ) = 0;
  161. virtual Sequence_Widget *clone ( void ) const = 0;
  162. bool selected ( void ) const
  163. {
  164. return std::find( _selection.begin(), _selection.end(), this ) != _selection.end();
  165. }
  166. void select ( void )
  167. {
  168. if ( selected() )
  169. return;
  170. _selection.push_back( this );
  171. _selection.sort( sort_func );
  172. redraw();
  173. }
  174. void deselect ( void )
  175. {
  176. _selection.remove( this );
  177. redraw();
  178. }
  179. static void
  180. delete_selected ( void )
  181. {
  182. while ( _selection.size() )
  183. delete _selection.front();
  184. }
  185. static void
  186. select_none ( void )
  187. {
  188. while ( _selection.size() )
  189. {
  190. _selection.front()->redraw();
  191. _selection.pop_front();
  192. }
  193. }
  194. static Sequence_Widget *current ( void ) { return Sequence_Widget::_current; }
  195. static Sequence_Widget *pushed ( void ) { return Sequence_Widget::_pushed; }
  196. static Sequence_Widget *belowmouse ( void ) { return Sequence_Widget::_belowmouse; }
  197. static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
  198. static void belowmouse ( Sequence_Widget *w ) { Sequence_Widget::_belowmouse = w; }
  199. // static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
  200. void begin_drag ( const Drag &d );
  201. void end_drag ( void );
  202. int dispatch ( int m );
  203. Fl_Widget * parent ( void ) const { return _sequence; }
  204. int scroll_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
  205. nframes_t scroll_ts ( void ) const { return timeline->xoffset; }
  206. virtual int y ( void ) const { return _sequence->y(); }
  207. virtual int h ( void ) const { return _sequence->h(); }
  208. /* used by regions */
  209. virtual int x ( void ) const
  210. {
  211. return _r->start < timeline->xoffset ? _sequence->x() : min( _sequence->x() + _sequence->w(), _sequence->x() + timeline->ts_to_x( _r->start - timeline->xoffset ) );
  212. }
  213. /* use this as x() when you need to draw lines between widgets */
  214. int line_x ( void ) const
  215. {
  216. 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 ) );
  217. }
  218. virtual int w ( void ) const
  219. {
  220. int tx = timeline->ts_to_x( _r->start );
  221. int rw;
  222. if ( tx < scroll_x() )
  223. rw = abs_w() - (scroll_x() - tx);
  224. else
  225. rw = abs_w();
  226. return min( rw, _sequence->w() );
  227. }
  228. int abs_x ( void ) const { return timeline->ts_to_x( _r->start ); }
  229. virtual int abs_w ( void ) const { return timeline->ts_to_x( _r->length ); }
  230. Fl_Color color ( void ) const { return _color; }
  231. void color ( Fl_Color v ) { _color = v; }
  232. Fl_Color box_color ( void ) const { return _box_color; }
  233. void box_color ( Fl_Color v ) { _box_color = v; }
  234. virtual Fl_Color selection_color ( void ) const { return _selection_color; }
  235. virtual void selection_color ( Fl_Color v ) { _selection_color = v; }
  236. Sequence * sequence ( void ) const { return _sequence; }
  237. void sequence ( Sequence *t ) { _sequence = t; }
  238. nframes_t start ( void ) const { return _r->start; }
  239. /* void start ( nframes_t o ) { _r->start = o; } */
  240. void start ( nframes_t where );
  241. void length ( nframes_t v ) { _r->length = v; }
  242. virtual nframes_t length ( void ) const { return _r->length; }
  243. void offset ( nframes_t v ) { _r->offset = v; }
  244. nframes_t offset ( void ) const { return _r->offset; }
  245. /** convert a screen x coord into an start into the region */
  246. nframes_t x_to_offset ( int X )
  247. {
  248. return timeline->x_to_ts( scroll_x() + ( X - _sequence->x() ) ) - _r->start;
  249. }
  250. int active_r ( void ) const { return _sequence->active_r(); }
  251. virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
  252. virtual Fl_Align align ( void ) const { return (Fl_Align)0; }
  253. virtual void
  254. redraw ( void )
  255. {
  256. if ( ! (align() & FL_ALIGN_INSIDE) )
  257. {
  258. // FIXME: to better..
  259. _sequence->redraw();
  260. }
  261. else
  262. _sequence->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
  263. }
  264. virtual void draw_box ( void );
  265. virtual void draw ( void );
  266. bool
  267. operator< ( const Sequence_Widget & rhs ) const
  268. {
  269. return _r->start < rhs._r->start;
  270. }
  271. bool
  272. operator<=( const Sequence_Widget & rhs ) const
  273. {
  274. return _r->start <= rhs._r->start;
  275. }
  276. virtual void draw_label ( const char *label, Fl_Align align, Fl_Color color=(Fl_Color)0 );
  277. virtual int handle ( int m );
  278. static bool
  279. sort_func ( const Sequence_Widget *lhs, const Sequence_Widget *rhs )
  280. {
  281. return *lhs < *rhs;
  282. }
  283. };