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.

348 lines
8.9KB

  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. // printf( "snap: %lu | %lu\n", w->offset() + w->length(), r->offset() );
  163. goto done;
  164. }
  165. if ( abs( rx2 - wx1 ) < snap_pixels )
  166. {
  167. r->offset( ( w->offset() - r->length() ) - 1 );
  168. // printf( "snap: %lu | %lu\n", r->offset() + r->length(), w->offset() );
  169. goto done;
  170. }
  171. }
  172. }
  173. {
  174. int nx = timeline->nearest_line( r->abs_x() );
  175. if ( nx >= 0 )
  176. {
  177. r->offset( timeline->x_to_ts( nx ) );
  178. return;
  179. }
  180. }
  181. // r->offset( timeline->x_to_ts( r->x() ) );
  182. done:
  183. return;
  184. // r->resize();
  185. // r->position( rx1, y() );
  186. }
  187. int
  188. Sequence::handle ( int m )
  189. {
  190. switch ( m )
  191. {
  192. case FL_FOCUS:
  193. case FL_UNFOCUS:
  194. case FL_LEAVE:
  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. case FL_DND_ENTER:
  211. case FL_DND_LEAVE:
  212. case FL_DND_RELEASE:
  213. return 1;
  214. case FL_MOVE:
  215. {
  216. Sequence_Widget *r = event_widget();
  217. if ( r != Sequence_Widget::belowmouse() )
  218. {
  219. if ( Sequence_Widget::belowmouse() )
  220. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  221. Sequence_Widget::belowmouse( r );
  222. if ( r )
  223. r->handle( FL_ENTER );
  224. }
  225. return 0;
  226. }
  227. default:
  228. {
  229. Sequence_Widget *r = Sequence_Widget::pushed() ? Sequence_Widget::pushed() : event_widget();
  230. if ( r )
  231. {
  232. int retval = r->dispatch( m );
  233. if ( retval && m == FL_PUSH )
  234. {
  235. take_focus();
  236. Sequence_Widget::pushed( r );
  237. }
  238. if ( retval && m == FL_RELEASE )
  239. Sequence_Widget::pushed( NULL );
  240. Loggable::block_start();
  241. while ( _delete_queue.size() )
  242. {
  243. Sequence_Widget *t = _delete_queue.front();
  244. _delete_queue.pop();
  245. if ( Sequence_Widget::pushed() == t )
  246. Sequence_Widget::pushed( NULL );
  247. if ( Sequence_Widget::belowmouse() == t )
  248. {
  249. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  250. Sequence_Widget::belowmouse( NULL );
  251. }
  252. timeline->wrlock();
  253. delete t;
  254. timeline->unlock();
  255. }
  256. Loggable::block_end();
  257. return retval;
  258. }
  259. else
  260. return Fl_Widget::handle( m );
  261. }
  262. }
  263. }