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.

383 lines
9.5KB

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