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.

379 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 namespace std;
  25. class Sequence_Widget;
  26. struct Drag
  27. {
  28. /* mouse coords at offset of drag */
  29. int x;
  30. int y;
  31. int state;
  32. nframes_t start;
  33. Drag( int X, int Y, nframes_t start=0 ) : x( X ), y( Y ), start( start ) { state = 0; }
  34. };
  35. /* most common position description. /offset/ is only used by Regions,
  36. but it's more convenient to have it here */
  37. struct Range
  38. {
  39. nframes_t start; /* where on the timeline */
  40. nframes_t offset; /* first sample from clip */
  41. nframes_t length; /* total number of samples */
  42. void
  43. trim_left ( long n )
  44. {
  45. start -= n;
  46. offset -= n;
  47. length += n;
  48. }
  49. void
  50. trim_right ( long n )
  51. {
  52. length += n;
  53. }
  54. Range ( ) : start( 0 ), offset( 0 ), length( 0 )
  55. {
  56. }
  57. };
  58. /* Used by time/tempo points or any other child of Sequence_Widget
  59. which must be locked to a point in musical time rather than wallclock
  60. time. Bar and beat start at 1. */
  61. struct BBT
  62. {
  63. unsigned short bar;
  64. unsigned char beat;
  65. unsigned short tick;
  66. BBT ( ) : bar( 0 ), beat( 0 ), tick( 0 )
  67. {
  68. }
  69. };
  70. /* FIXME: wrong place for this */
  71. struct position_info
  72. {
  73. nframes_t frame;
  74. float tempo;
  75. int beats_per_bar;
  76. int beat_type;
  77. BBT bbt;
  78. };
  79. /* Base class for virtual widget on a track */
  80. class Sequence_Widget : public Loggable
  81. {
  82. static list <Sequence_Widget *> _selection; /* all the widgets making up the selection */
  83. /* FIXME: is this not the same as /pushed/? */
  84. static Sequence_Widget * _current; /* the widget initiating events that affect the selection */
  85. /* these are actually managed in the Sequence classes */
  86. static Sequence_Widget * _pushed; /* the widget receiving drag events (a copy) */
  87. static Sequence_Widget * _original; /* the original of the /pushed/ widget */
  88. static Sequence_Widget * _belowmouse; /* the widget below the mouse cursor */
  89. static Fl_Color _selection_color;
  90. protected:
  91. Sequence *_sequence; /* track this region belongs to */
  92. Range _range; /* range for playback */
  93. Range *_r; /* range for editing / display (points to the same thing as above, except for when dragging etc) */
  94. Fl_Color _color; /* color of waveform */
  95. Fl_Color _box_color; /* color of background (box) */
  96. Drag *_drag;
  97. virtual void get ( Log_Entry &e ) const;
  98. virtual void set ( Log_Entry &e );
  99. /* careful with this, it doesn't journal */
  100. Sequence_Widget ( const Sequence_Widget &rhs ) : Loggable( rhs )
  101. {
  102. _drag = NULL;
  103. _sequence = rhs._sequence;
  104. _range = rhs._range;
  105. _r = &_range;
  106. _color = rhs._color;
  107. _box_color = rhs._box_color;
  108. };
  109. public:
  110. Sequence_Widget ( )
  111. {
  112. _sequence = NULL;
  113. _r = &_range;
  114. _r->start = _r->offset = _r->length = 0;
  115. _drag = NULL;
  116. _box_color = FL_BACKGROUND_COLOR;
  117. _color = FL_FOREGROUND_COLOR;
  118. }
  119. virtual ~Sequence_Widget ( )
  120. {
  121. redraw();
  122. _sequence->remove( this );
  123. _selection.remove( this );
  124. }
  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 ::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. {
  189. _drag = new Drag( d );
  190. _r = new Range( _range );
  191. }
  192. void end_drag ( void )
  193. {
  194. _range = *_r;
  195. delete _r;
  196. _r = &_range;
  197. delete _drag;
  198. _drag = NULL;
  199. }
  200. int dispatch ( int m );
  201. Fl_Widget * parent ( void ) const { return _sequence; }
  202. int scroll_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
  203. nframes_t scroll_ts ( void ) const { return timeline->xoffset; }
  204. virtual int y ( void ) const { return _sequence->y(); }
  205. virtual int h ( void ) const { return _sequence->h(); }
  206. /* used by regions */
  207. virtual int x ( void ) const
  208. {
  209. return _r->start < timeline->xoffset ? _sequence->x() : min( _sequence->x() + _sequence->w(), _sequence->x() + timeline->ts_to_x( _r->start - timeline->xoffset ) );
  210. }
  211. /* use this as x() when you need to draw lines between widgets */
  212. int line_x ( void ) const
  213. {
  214. 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 ) );
  215. }
  216. virtual int w ( void ) const
  217. {
  218. int tx = timeline->ts_to_x( _r->start );
  219. int rw;
  220. if ( tx < scroll_x() )
  221. rw = abs_w() - (scroll_x() - tx);
  222. else
  223. rw = abs_w();
  224. return min( rw, _sequence->w() );
  225. }
  226. int abs_x ( void ) const { return timeline->ts_to_x( _r->start ); }
  227. virtual int abs_w ( void ) const { return timeline->ts_to_x( _r->length ); }
  228. Fl_Color color ( void ) const { return _color; }
  229. void color ( Fl_Color v ) { _color = v; }
  230. Fl_Color box_color ( void ) const { return _box_color; }
  231. virtual Fl_Color selection_color ( void ) const { return _selection_color; }
  232. virtual void selection_color ( Fl_Color v ) { _selection_color = v; }
  233. Sequence * sequence ( void ) const { return _sequence; }
  234. void sequence ( Sequence *t ) { _sequence = t; }
  235. nframes_t start ( void ) const { return _r->start; }
  236. void start ( nframes_t o ) { _r->start = o; }
  237. void length ( nframes_t v ) { _r->length = v; }
  238. virtual nframes_t length ( void ) const { return _r->length; }
  239. void offset ( nframes_t v ) { _r->offset = v; }
  240. nframes_t offset ( void ) const { return _r->offset; }
  241. /** convert a screen x coord into an start into the region */
  242. nframes_t x_to_offset ( int X )
  243. {
  244. return timeline->x_to_ts( scroll_x() + ( X - _sequence->x() ) ) - _r->start;
  245. }
  246. int active_r ( void ) const { return _sequence->active_r(); }
  247. virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
  248. virtual Fl_Align align ( void ) const { return (Fl_Align)0; }
  249. virtual void
  250. redraw ( void )
  251. {
  252. if ( ! (align() & FL_ALIGN_INSIDE) )
  253. {
  254. // FIXME: to better..
  255. _sequence->redraw();
  256. }
  257. else
  258. _sequence->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
  259. }
  260. /* just draw a simple box */
  261. virtual void
  262. draw_box ( void )
  263. {
  264. fl_draw_box( box(), x(), y(), w(), h(), selected() ? FL_MAGENTA : _box_color );
  265. }
  266. virtual void
  267. draw ( void )
  268. {
  269. draw_box();
  270. }
  271. bool
  272. operator< ( const Sequence_Widget & rhs ) const
  273. {
  274. return _r->start < rhs._r->start;
  275. }
  276. bool
  277. operator<=( const Sequence_Widget & rhs ) const
  278. {
  279. return _r->start <= rhs._r->start;
  280. }
  281. virtual void draw_label ( const char *label, Fl_Align align, Fl_Color color=(Fl_Color)0 );
  282. virtual int handle ( int m );
  283. static bool
  284. sort_func ( const Sequence_Widget *lhs, const Sequence_Widget *rhs )
  285. {
  286. return *lhs < *rhs;
  287. }
  288. };