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.

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