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.

366 lines
9.1KB

  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. #include "Sequence.H"
  19. #include "Timeline.H"
  20. #include "Audio_Region.H"
  21. #include <FL/fl_draw.H>
  22. #include "Track.H"
  23. #include "../FL/Boxtypes.H"
  24. queue <Sequence_Widget *> Sequence::_delete_queue;
  25. Sequence::Sequence ( Track *track ) : Fl_Widget( 0, 0, 0, 0 ), Loggable( true )
  26. {
  27. init();
  28. _track = track;
  29. // log_create();
  30. }
  31. Sequence::Sequence ( int X, int Y, int W, int H ) : Fl_Widget( X, Y, W, H ), Loggable( false )
  32. {
  33. init();
  34. }
  35. void
  36. Sequence::init ( void )
  37. {
  38. _track = NULL;
  39. _name = NULL;
  40. box( FL_DOWN_BOX );
  41. color( FL_BACKGROUND_COLOR );
  42. align( FL_ALIGN_LEFT );
  43. }
  44. Sequence::~Sequence ( )
  45. {
  46. if ( _name )
  47. free( _name );
  48. for ( std::list <Sequence_Widget*>::iterator i = _widgets.begin();
  49. i != _widgets.end(); ++i )
  50. {
  51. Sequence_Widget *w = *i;
  52. *i = NULL;
  53. delete w;
  54. }
  55. _widgets.clear();
  56. parent()->redraw();
  57. parent()->remove( this );
  58. // log_destroy();
  59. }
  60. nframes_t
  61. Sequence::x_to_offset ( int X )
  62. {
  63. return timeline->xoffset + timeline->x_to_ts( X - x() );
  64. }
  65. void
  66. Sequence::sort ( void )
  67. {
  68. _widgets.sort( Sequence_Widget::sort_func );
  69. }
  70. /** return a pointer to the widget that /r/ overlaps, or NULL if none. */
  71. Sequence_Widget *
  72. Sequence::overlaps ( Sequence_Widget *r )
  73. {
  74. for ( list <Sequence_Widget *>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  75. {
  76. if ( *i == r ) continue;
  77. if ( ! ( (*i)->start() > r->start() + r->length() || (*i)->start() + (*i)->length() < r->start() ) )
  78. return *i;
  79. }
  80. return NULL;
  81. }
  82. void
  83. Sequence::draw ( void )
  84. {
  85. if ( ! fl_not_clipped( x(), y(), w(), h() ) )
  86. return;
  87. fl_push_clip( x(), y(), w(), h() );
  88. /* draw the box with the ends cut off. */
  89. draw_box( box(), x() - Fl::box_dx( box() ) - 1, y(), w() + Fl::box_dw( box() ) + 2, h(), color() );
  90. int X, Y, W, H;
  91. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  92. if ( Sequence_Widget::pushed() && Sequence_Widget::pushed()->sequence() == this )
  93. {
  94. /* make sure the Sequence_Widget::pushed widget is above all others */
  95. remove( Sequence_Widget::pushed() );
  96. add( Sequence_Widget::pushed() );
  97. }
  98. // printf( "track::draw %d,%d %dx%d\n", X,Y,W,H );
  99. timeline->draw_measure_lines( X, Y, W, H, color() );
  100. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
  101. (*r)->draw_box();
  102. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
  103. (*r)->draw();
  104. fl_pop_clip();
  105. }
  106. void
  107. Sequence::remove ( Sequence_Widget *r )
  108. {
  109. // Logger _log( this );
  110. _widgets.remove( r );
  111. }
  112. void
  113. Sequence::remove_selected ( void )
  114. {
  115. Loggable::block_start();
  116. for ( list <Sequence_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
  117. if ( (*r)->selected() )
  118. {
  119. Sequence_Widget *t = *r;
  120. _widgets.erase( r++ );
  121. delete t;
  122. }
  123. else
  124. ++r;
  125. Loggable::block_end();
  126. }
  127. Sequence_Widget *
  128. Sequence::event_widget ( void )
  129. {
  130. nframes_t ets = timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() );
  131. for ( list <Sequence_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  132. if ( ets > (*r)->start() && ets < (*r)->start() + (*r)->length()
  133. && Fl::event_y() >= (*r)->y() && Fl::event_y() <= (*r)->y() + (*r)->h() )
  134. return (*r);
  135. return NULL;
  136. }
  137. void
  138. Sequence::select_range ( int X, int W )
  139. {
  140. nframes_t sts = timeline->xoffset + timeline->x_to_ts( X - x() );
  141. nframes_t ets = sts + timeline->x_to_ts( W );
  142. for ( list <Sequence_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  143. if ( ! ( (*r)->start() > ets || (*r)->start() + (*r)->length() < sts ) )
  144. (*r)->select();
  145. }
  146. void
  147. Sequence::add ( Sequence_Widget *r )
  148. {
  149. // Logger _log( this );
  150. if ( r->sequence() )
  151. {
  152. r->redraw();
  153. r->sequence()->remove( r );
  154. // r->track()->redraw();
  155. }
  156. r->sequence( this );
  157. _widgets.push_back( r );
  158. sort();
  159. }
  160. /* snap /r/ to nearest edge */
  161. void
  162. Sequence::snap ( Sequence_Widget *r )
  163. {
  164. const int snap_pixels = 10;
  165. if ( Timeline::snap_magnetic )
  166. {
  167. const int rx1 = r->x();
  168. const int rx2 = r->x() + r->w();
  169. for ( list <Sequence_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  170. {
  171. const Sequence_Widget *w = (*i);
  172. if ( w == r )
  173. continue;
  174. const int wx1 = w->x();
  175. const int wx2 = w->x() + w->w();
  176. if ( abs( rx1 - wx2 ) < snap_pixels )
  177. {
  178. r->start( w->start() + w->length() + 1 );
  179. return;
  180. }
  181. if ( abs( rx2 - wx1 ) < snap_pixels )
  182. {
  183. r->start( ( w->start() - r->length() ) - 1 );
  184. return;
  185. }
  186. }
  187. }
  188. nframes_t f;
  189. if ( timeline->nearest_line( r->start(), &f ) )
  190. {
  191. // printf( "snap frame is %lu\n", f );
  192. r->start( f );
  193. }
  194. }
  195. int
  196. Sequence::handle ( int m )
  197. {
  198. switch ( m )
  199. {
  200. case FL_FOCUS:
  201. case FL_UNFOCUS:
  202. case FL_LEAVE:
  203. fl_cursor( FL_CURSOR_DEFAULT );
  204. return 1;
  205. case FL_DND_DRAG:
  206. return 1;
  207. case FL_ENTER:
  208. if ( Sequence_Widget::pushed() )
  209. {
  210. if ( Sequence_Widget::pushed()->sequence()->class_name() == class_name() )
  211. {
  212. /* accept objects dragged from other sequences of this type */
  213. add( Sequence_Widget::pushed() );
  214. redraw();
  215. fl_cursor( FL_CURSOR_MOVE );
  216. }
  217. else
  218. fl_cursor( (Fl_Cursor)1 );
  219. }
  220. else
  221. fl_cursor( cursor() );
  222. return 1;
  223. case FL_DND_ENTER:
  224. case FL_DND_LEAVE:
  225. case FL_DND_RELEASE:
  226. return 1;
  227. case FL_MOVE:
  228. {
  229. Sequence_Widget *r = event_widget();
  230. if ( r != Sequence_Widget::belowmouse() )
  231. {
  232. if ( Sequence_Widget::belowmouse() )
  233. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  234. Sequence_Widget::belowmouse( r );
  235. if ( r )
  236. r->handle( FL_ENTER );
  237. }
  238. return 0;
  239. }
  240. default:
  241. {
  242. Sequence_Widget *r = Sequence_Widget::pushed() ? Sequence_Widget::pushed() : event_widget();
  243. if ( r )
  244. {
  245. int retval = r->dispatch( m );
  246. if ( retval && m == FL_PUSH )
  247. {
  248. take_focus();
  249. Sequence_Widget::pushed( r );
  250. }
  251. if ( retval && m == FL_RELEASE )
  252. Sequence_Widget::pushed( NULL );
  253. Loggable::block_start();
  254. while ( _delete_queue.size() )
  255. {
  256. Sequence_Widget *t = _delete_queue.front();
  257. _delete_queue.pop();
  258. if ( Sequence_Widget::pushed() == t )
  259. Sequence_Widget::pushed( NULL );
  260. if ( Sequence_Widget::belowmouse() == t )
  261. {
  262. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  263. Sequence_Widget::belowmouse( NULL );
  264. }
  265. timeline->wrlock();
  266. delete t;
  267. timeline->unlock();
  268. }
  269. Loggable::block_end();
  270. return retval;
  271. }
  272. else
  273. return Fl_Widget::handle( m );
  274. }
  275. }
  276. }