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.

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