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.

351 lines
8.8KB

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