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.

372 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. Sequence_Widget ( );
  123. public:
  124. virtual ~Sequence_Widget ( );
  125. const Sequence_Widget &
  126. operator= ( const Sequence_Widget &rhs )
  127. {
  128. if ( this == &rhs )
  129. return *this;
  130. _r = &_range;
  131. _range = rhs._range;
  132. _sequence = rhs._sequence;
  133. _box_color = rhs._box_color;
  134. _color = rhs._color;
  135. return *this;
  136. }
  137. /* Sequence_Widget ( const Sequence_Widget &rhs ) */
  138. /* { */
  139. /* *this = rhs; */
  140. /* } */
  141. #define SEQUENCE_WIDGET_CLONE_FUNC(class) \
  142. virtual Sequence_Widget *clone ( void ) const \
  143. { \
  144. return new class ( *this ); \
  145. } \
  146. \
  147. // virtual Sequence_Widget *clone ( const Sequence_Widget *r ) = 0;
  148. virtual Sequence_Widget *clone ( void ) const = 0;
  149. bool selected ( void ) const
  150. {
  151. return std::find( _selection.begin(), _selection.end(), this ) != _selection.end();
  152. }
  153. void select ( void )
  154. {
  155. if ( selected() )
  156. return;
  157. _selection.push_back( this );
  158. _selection.sort( sort_func );
  159. redraw();
  160. }
  161. void deselect ( void )
  162. {
  163. _selection.remove( this );
  164. redraw();
  165. }
  166. static void
  167. delete_selected ( void )
  168. {
  169. while ( _selection.size() )
  170. delete _selection.front();
  171. }
  172. static void
  173. select_none ( void )
  174. {
  175. while ( _selection.size() )
  176. {
  177. _selection.front()->redraw();
  178. _selection.pop_front();
  179. }
  180. }
  181. static Sequence_Widget *current ( void ) { return Sequence_Widget::_current; }
  182. static Sequence_Widget *pushed ( void ) { return Sequence_Widget::_pushed; }
  183. static Sequence_Widget *belowmouse ( void ) { return Sequence_Widget::_belowmouse; }
  184. static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
  185. static void belowmouse ( Sequence_Widget *w ) { Sequence_Widget::_belowmouse = w; }
  186. // static void pushed ( Sequence_Widget *w ) { Sequence_Widget::_pushed = w; }
  187. void begin_drag ( const Drag &d );
  188. void end_drag ( void );
  189. int dispatch ( int m );
  190. Fl_Widget * parent ( void ) const { return _sequence; }
  191. int scroll_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
  192. nframes_t scroll_ts ( void ) const { return timeline->xoffset; }
  193. virtual int y ( void ) const { return _sequence->y(); }
  194. virtual int h ( void ) const { return _sequence->h(); }
  195. /* used by regions */
  196. virtual int x ( void ) const
  197. {
  198. return _r->start < timeline->xoffset ? _sequence->x() : min( _sequence->x() + _sequence->w(), _sequence->x() + timeline->ts_to_x( _r->start - timeline->xoffset ) );
  199. }
  200. /* use this as x() when you need to draw lines between widgets */
  201. int line_x ( void ) const
  202. {
  203. 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 ) );
  204. }
  205. virtual int w ( void ) const
  206. {
  207. int tx = timeline->ts_to_x( _r->start );
  208. int rw;
  209. if ( tx < scroll_x() )
  210. rw = abs_w() - (scroll_x() - tx);
  211. else
  212. rw = abs_w();
  213. return min( rw, _sequence->w() );
  214. }
  215. int abs_x ( void ) const { return timeline->ts_to_x( _r->start ); }
  216. virtual int abs_w ( void ) const { return timeline->ts_to_x( _r->length ); }
  217. Fl_Color color ( void ) const { return _color; }
  218. void color ( Fl_Color v ) { _color = v; }
  219. Fl_Color box_color ( void ) const { return _box_color; }
  220. void box_color ( Fl_Color v ) { _box_color = v; }
  221. virtual Fl_Color selection_color ( void ) const { return _selection_color; }
  222. virtual void selection_color ( Fl_Color v ) { _selection_color = v; }
  223. Sequence * sequence ( void ) const { return _sequence; }
  224. void sequence ( Sequence *t ) { _sequence = t; }
  225. nframes_t start ( void ) const { return _r->start; }
  226. /* void start ( nframes_t o ) { _r->start = o; } */
  227. void start ( nframes_t where );
  228. void length ( nframes_t v ) { _r->length = v; }
  229. virtual nframes_t length ( void ) const { return _r->length; }
  230. void offset ( nframes_t v ) { _r->offset = v; }
  231. nframes_t offset ( void ) const { return _r->offset; }
  232. /** convert a screen x coord into an start into the region */
  233. nframes_t x_to_offset ( int X )
  234. {
  235. return timeline->x_to_ts( scroll_x() + ( X - _sequence->x() ) ) - _r->start;
  236. }
  237. int active_r ( void ) const { return _sequence->active_r(); }
  238. /** returns true if widget /w/ begins and ends completely within the range of this widget */
  239. bool contains ( const Sequence_Widget *w ) const
  240. {
  241. return w->start() >= start() && w->start() + w->length() <= start() + length();
  242. }
  243. /** returns true of widget /w/ overlaps this widget in any place */
  244. bool overlaps ( const Sequence_Widget *w ) const
  245. {
  246. return ! ( w->start() > start() + length() || w->start() + w->length() < start() );
  247. }
  248. virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
  249. virtual Fl_Align align ( void ) const { return (Fl_Align)0; }
  250. virtual void
  251. redraw ( void )
  252. {
  253. if ( ! (align() & FL_ALIGN_INSIDE) )
  254. {
  255. // FIXME: to better..
  256. _sequence->redraw();
  257. }
  258. else
  259. _sequence->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
  260. }
  261. virtual void draw_box ( void );
  262. virtual void draw ( void );
  263. bool
  264. operator< ( const Sequence_Widget & rhs ) const
  265. {
  266. return _r->start < rhs._r->start;
  267. }
  268. bool
  269. operator<=( const Sequence_Widget & rhs ) const
  270. {
  271. return _r->start <= rhs._r->start;
  272. }
  273. virtual void draw_label ( const char *label, Fl_Align align, Fl_Color color=(Fl_Color)0 );
  274. virtual int handle ( int m );
  275. static bool
  276. sort_func ( const Sequence_Widget *lhs, const Sequence_Widget *rhs )
  277. {
  278. return *lhs < *rhs;
  279. }
  280. };