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.

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