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.

370 lines
9.3KB

  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. static nframes_t
  161. abs_diff ( nframes_t n1, nframes_t n2 )
  162. {
  163. return n1 > n2 ? n1 - n2 : n2 - n1;
  164. }
  165. /* snap /r/ to nearest edge */
  166. void
  167. Sequence::snap ( Sequence_Widget *r )
  168. {
  169. const int snap_pixels = 10;
  170. const int snap_frames = timeline->x_to_ts( snap_pixels );
  171. /* snap to other widgets */
  172. if ( Timeline::snap_magnetic )
  173. {
  174. const int rx1 = r->start();
  175. const int rx2 = r->start() + r->length();
  176. for ( list <Sequence_Widget*>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  177. {
  178. const Sequence_Widget *w = (*i);
  179. if ( w == r )
  180. continue;
  181. const int wx1 = w->start();
  182. const int wx2 = w->start() + w->length();
  183. if ( abs_diff( rx1, wx2 ) < snap_frames )
  184. {
  185. r->start( w->start() + w->length() + 1 );
  186. return;
  187. }
  188. if ( abs_diff( rx2, wx1 ) < snap_frames )
  189. {
  190. r->start( ( w->start() - r->length() ) - 1 );
  191. return;
  192. }
  193. }
  194. }
  195. nframes_t f;
  196. /* snap to beat/bar lines */
  197. if ( timeline->nearest_line( r->start(), &f ) )
  198. r->start( f );
  199. }
  200. int
  201. Sequence::handle ( int m )
  202. {
  203. switch ( m )
  204. {
  205. case FL_FOCUS:
  206. case FL_UNFOCUS:
  207. case FL_LEAVE:
  208. fl_cursor( FL_CURSOR_DEFAULT );
  209. return 1;
  210. case FL_DND_DRAG:
  211. return 1;
  212. case FL_ENTER:
  213. if ( Sequence_Widget::pushed() )
  214. {
  215. if ( Sequence_Widget::pushed()->sequence()->class_name() == class_name() )
  216. {
  217. /* accept objects dragged from other sequences of this type */
  218. add( Sequence_Widget::pushed() );
  219. redraw();
  220. fl_cursor( FL_CURSOR_MOVE );
  221. }
  222. else
  223. fl_cursor( (Fl_Cursor)1 );
  224. }
  225. else
  226. fl_cursor( cursor() );
  227. return 1;
  228. case FL_DND_ENTER:
  229. case FL_DND_LEAVE:
  230. case FL_DND_RELEASE:
  231. return 1;
  232. case FL_MOVE:
  233. {
  234. Sequence_Widget *r = event_widget();
  235. if ( r != Sequence_Widget::belowmouse() )
  236. {
  237. if ( Sequence_Widget::belowmouse() )
  238. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  239. Sequence_Widget::belowmouse( r );
  240. if ( r )
  241. r->handle( FL_ENTER );
  242. }
  243. return 0;
  244. }
  245. default:
  246. {
  247. Sequence_Widget *r = Sequence_Widget::pushed() ? Sequence_Widget::pushed() : event_widget();
  248. if ( r )
  249. {
  250. int retval = r->dispatch( m );
  251. if ( retval && m == FL_PUSH )
  252. {
  253. take_focus();
  254. Sequence_Widget::pushed( r );
  255. }
  256. if ( retval && m == FL_RELEASE )
  257. Sequence_Widget::pushed( NULL );
  258. Loggable::block_start();
  259. while ( _delete_queue.size() )
  260. {
  261. Sequence_Widget *t = _delete_queue.front();
  262. _delete_queue.pop();
  263. if ( Sequence_Widget::pushed() == t )
  264. Sequence_Widget::pushed( NULL );
  265. if ( Sequence_Widget::belowmouse() == t )
  266. {
  267. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  268. Sequence_Widget::belowmouse( NULL );
  269. }
  270. timeline->wrlock();
  271. delete t;
  272. timeline->unlock();
  273. }
  274. Loggable::block_end();
  275. return retval;
  276. }
  277. else
  278. return Fl_Widget::handle( m );
  279. }
  280. }
  281. }