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.

341 lines
8.7KB

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