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.

365 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)->offset() > r->offset() + r->length() || (*i)->offset() + (*i)->length() < r->offset() ) )
  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)->offset() && ets < (*r)->offset() + (*r)->length() )
  133. return (*r);
  134. return NULL;
  135. }
  136. void
  137. Sequence::select_range ( int X, int W )
  138. {
  139. nframes_t sts = timeline->xoffset + timeline->x_to_ts( X - x() );
  140. nframes_t ets = sts + timeline->x_to_ts( W );
  141. for ( list <Sequence_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  142. if ( ! ( (*r)->offset() > ets || (*r)->offset() + (*r)->length() < sts ) )
  143. (*r)->select();
  144. }
  145. void
  146. Sequence::add ( Sequence_Widget *r )
  147. {
  148. // Logger _log( this );
  149. if ( r->sequence() )
  150. {
  151. r->redraw();
  152. r->sequence()->remove( r );
  153. // r->track()->redraw();
  154. }
  155. r->sequence( this );
  156. _widgets.push_back( r );
  157. sort();
  158. }
  159. /* snap /r/ to nearest edge */
  160. void
  161. Sequence::snap ( Sequence_Widget *r )
  162. {
  163. const int snap_pixels = 10;
  164. if ( Timeline::snap_magnetic )
  165. {
  166. const int rx1 = r->x();
  167. const int rx2 = r->x() + r->w();
  168. for ( list <Sequence_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  169. {
  170. const Sequence_Widget *w = (*i);
  171. if ( w == r )
  172. continue;
  173. const int wx1 = w->x();
  174. const int wx2 = w->x() + w->w();
  175. if ( abs( rx1 - wx2 ) < snap_pixels )
  176. {
  177. r->offset( w->offset() + w->length() + 1 );
  178. return;
  179. }
  180. if ( abs( rx2 - wx1 ) < snap_pixels )
  181. {
  182. r->offset( ( w->offset() - r->length() ) - 1 );
  183. return;
  184. }
  185. }
  186. }
  187. nframes_t f;
  188. if ( timeline->nearest_line( r->offset(), &f ) )
  189. {
  190. // printf( "snap frame is %lu\n", f );
  191. r->offset( f );
  192. }
  193. }
  194. int
  195. Sequence::handle ( int m )
  196. {
  197. switch ( m )
  198. {
  199. case FL_FOCUS:
  200. case FL_UNFOCUS:
  201. case FL_LEAVE:
  202. fl_cursor( FL_CURSOR_DEFAULT );
  203. return 1;
  204. case FL_DND_DRAG:
  205. return 1;
  206. case FL_ENTER:
  207. if ( Sequence_Widget::pushed() )
  208. {
  209. if ( Sequence_Widget::pushed()->sequence()->class_name() == class_name() )
  210. {
  211. /* accept objects dragged from other sequences of this type */
  212. add( Sequence_Widget::pushed() );
  213. redraw();
  214. fl_cursor( FL_CURSOR_MOVE );
  215. }
  216. else
  217. fl_cursor( (Fl_Cursor)1 );
  218. }
  219. else
  220. fl_cursor( cursor() );
  221. return 1;
  222. case FL_DND_ENTER:
  223. case FL_DND_LEAVE:
  224. case FL_DND_RELEASE:
  225. return 1;
  226. case FL_MOVE:
  227. {
  228. Sequence_Widget *r = event_widget();
  229. if ( r != Sequence_Widget::belowmouse() )
  230. {
  231. if ( Sequence_Widget::belowmouse() )
  232. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  233. Sequence_Widget::belowmouse( r );
  234. if ( r )
  235. r->handle( FL_ENTER );
  236. }
  237. return 0;
  238. }
  239. default:
  240. {
  241. Sequence_Widget *r = Sequence_Widget::pushed() ? Sequence_Widget::pushed() : event_widget();
  242. if ( r )
  243. {
  244. int retval = r->dispatch( m );
  245. if ( retval && m == FL_PUSH )
  246. {
  247. take_focus();
  248. Sequence_Widget::pushed( r );
  249. }
  250. if ( retval && m == FL_RELEASE )
  251. Sequence_Widget::pushed( NULL );
  252. Loggable::block_start();
  253. while ( _delete_queue.size() )
  254. {
  255. Sequence_Widget *t = _delete_queue.front();
  256. _delete_queue.pop();
  257. if ( Sequence_Widget::pushed() == t )
  258. Sequence_Widget::pushed( NULL );
  259. if ( Sequence_Widget::belowmouse() == t )
  260. {
  261. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  262. Sequence_Widget::belowmouse( NULL );
  263. }
  264. timeline->wrlock();
  265. delete t;
  266. timeline->unlock();
  267. }
  268. Loggable::block_end();
  269. return retval;
  270. }
  271. else
  272. return Fl_Widget::handle( m );
  273. }
  274. }
  275. }