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.

393 lines
12KB

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