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.

514 lines
13KB

  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. // clear_visible_focus();
  45. }
  46. void
  47. Sequence::clear ( void )
  48. {
  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. }
  58. Sequence::~Sequence ( )
  59. {
  60. DMESSAGE( "destroying sequence" );
  61. if ( _name )
  62. free( _name );
  63. if ( _widgets.size() )
  64. FATAL( "programming error: leaf destructor must call Sequence::clear()!" );
  65. }
  66. nframes_t
  67. Sequence::x_to_offset ( int X )
  68. {
  69. return timeline->xoffset + timeline->x_to_ts( X - x() );
  70. }
  71. void
  72. Sequence::sort ( void )
  73. {
  74. _widgets.sort( Sequence_Widget::sort_func );
  75. }
  76. /** return a pointer to the widget that /r/ overlaps, or NULL if none. */
  77. Sequence_Widget *
  78. Sequence::overlaps ( Sequence_Widget *r )
  79. {
  80. for ( list <Sequence_Widget *>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  81. {
  82. if ( *i == r ) continue;
  83. if ( (*i)->overlaps( r ) )
  84. return *i;
  85. }
  86. return NULL;
  87. }
  88. void
  89. Sequence::draw ( void )
  90. {
  91. if ( ! fl_not_clipped( x(), y(), w(), h() ) )
  92. return;
  93. fl_push_clip( x(), y(), w(), h() );
  94. /* draw the box with the ends cut off. */
  95. draw_box( box(), x() - Fl::box_dx( box() ) - 1, y(), w() + Fl::box_dw( box() ) + 2, h(), color() );
  96. int X, Y, W, H;
  97. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  98. /* if ( Sequence_Widget::pushed() && Sequence_Widget::pushed()->sequence() == this ) */
  99. /* { */
  100. /* /\* make sure the Sequence_Widget::pushed widget is above all others *\/ */
  101. /* remove( Sequence_Widget::pushed() ); */
  102. /* add( Sequence_Widget::pushed() ); */
  103. /* } */
  104. // printf( "track::draw %d,%d %dx%d\n", X,Y,W,H );
  105. timeline->draw_measure_lines( X, Y, W, H, color() );
  106. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
  107. (*r)->draw_box();
  108. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
  109. (*r)->draw();
  110. fl_pop_clip();
  111. }
  112. void
  113. Sequence::remove ( Sequence_Widget *r )
  114. {
  115. timeline->wrlock();
  116. _widgets.remove( r );
  117. timeline->unlock();
  118. handle_widget_change( r->start(), r->length() );
  119. }
  120. void
  121. Sequence::remove_selected ( void )
  122. {
  123. Loggable::block_start();
  124. for ( list <Sequence_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
  125. if ( (*r)->selected() )
  126. {
  127. Sequence_Widget *t = *r;
  128. _widgets.erase( r++ );
  129. delete t;
  130. }
  131. else
  132. ++r;
  133. Loggable::block_end();
  134. }
  135. void
  136. Sequence::handle_widget_change ( nframes_t start, nframes_t length )
  137. {
  138. // timeline->update_length( start + length );
  139. }
  140. /** calculate the length of this sequence by looking at the end of the
  141. * least widget it contains */
  142. nframes_t
  143. Sequence::length ( void ) const
  144. {
  145. nframes_t l = 0;
  146. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
  147. l = max( l, (*r)->start() + (*r)->length() );
  148. return l;
  149. }
  150. Sequence_Widget *
  151. Sequence::widget_at ( nframes_t ts, int Y )
  152. {
  153. for ( list <Sequence_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); ++r )
  154. if ( ts >= (*r)->start() && ts <= (*r)->start() + (*r)->length()
  155. && Y >= (*r)->y() && Y <= (*r)->y() + (*r)->h() )
  156. return (*r);
  157. return NULL;
  158. }
  159. Sequence_Widget *
  160. Sequence::event_widget ( void )
  161. {
  162. nframes_t ets = timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() );
  163. return widget_at( ets, Fl::event_y() );
  164. }
  165. void
  166. Sequence::select_range ( int X, int W )
  167. {
  168. nframes_t sts = x_to_offset( X );
  169. nframes_t ets = sts + timeline->x_to_ts( W );
  170. for ( list <Sequence_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); ++r )
  171. if ( ! ( (*r)->start() > ets || (*r)->start() + (*r)->length() < sts ) )
  172. (*r)->select();
  173. }
  174. void
  175. Sequence::add ( Sequence_Widget *r )
  176. {
  177. // Logger _log( this );
  178. if ( r->sequence() )
  179. {
  180. r->redraw();
  181. r->sequence()->remove( r );
  182. // r->track()->redraw();
  183. }
  184. timeline->wrlock();
  185. r->sequence( this );
  186. _widgets.push_back( r );
  187. sort();
  188. timeline->unlock();
  189. handle_widget_change( r->start(), r->length() );
  190. }
  191. static nframes_t
  192. abs_diff ( nframes_t n1, nframes_t n2 )
  193. {
  194. return n1 > n2 ? n1 - n2 : n2 - n1;
  195. }
  196. /* snap /r/ to nearest edge */
  197. void
  198. Sequence::snap ( Sequence_Widget *r )
  199. {
  200. const int snap_pixels = 10;
  201. const nframes_t snap_frames = timeline->x_to_ts( snap_pixels );
  202. /* snap to other widgets */
  203. if ( Timeline::snap_magnetic )
  204. {
  205. const int rx1 = r->start();
  206. const int rx2 = r->start() + r->length();
  207. for ( list <Sequence_Widget*>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  208. {
  209. const Sequence_Widget *w = (*i);
  210. if ( w == r )
  211. continue;
  212. const int wx1 = w->start();
  213. const int wx2 = w->start() + w->length();
  214. if ( abs_diff( rx1, wx2 ) < snap_frames )
  215. {
  216. r->start( w->start() + w->length() + 1 );
  217. return;
  218. }
  219. if ( abs_diff( rx2, wx1 ) < snap_frames )
  220. {
  221. r->start( ( w->start() - r->length() ) - 1 );
  222. return;
  223. }
  224. }
  225. }
  226. nframes_t f = r->start();
  227. /* snap to beat/bar lines */
  228. if ( timeline->nearest_line( &f ) )
  229. r->start( f );
  230. }
  231. /** return the location of the next widget from frame /from/ */
  232. nframes_t
  233. Sequence::next ( nframes_t from ) const
  234. {
  235. for ( list <Sequence_Widget*>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  236. if ( (*i)->start() > from )
  237. return (*i)->start();
  238. if ( _widgets.size() )
  239. return _widgets.back()->start();
  240. else
  241. return 0;
  242. }
  243. /** return the location of the next widget from frame /from/ */
  244. nframes_t
  245. Sequence::prev ( nframes_t from ) const
  246. {
  247. for ( list <Sequence_Widget*>::const_reverse_iterator i = _widgets.rbegin(); i != _widgets.rend(); i++ )
  248. if ( (*i)->start() < from )
  249. return (*i)->start();
  250. if ( _widgets.size() )
  251. return _widgets.front()->start();
  252. else
  253. return 0;
  254. }
  255. #include "FL/event_name.H"
  256. #include "Transport.H" // for locate()
  257. int
  258. Sequence::handle ( int m )
  259. {
  260. /* if ( m != FL_NO_EVENT ) */
  261. /* DMESSAGE( "%s", event_name( m ) ); */
  262. switch ( m )
  263. {
  264. case FL_KEYBOARD:
  265. case FL_SHORTCUT:
  266. if ( Fl::test_shortcut( FL_CTRL + FL_Right ) )
  267. {
  268. transport->locate( next( transport->frame ) );
  269. return 1;
  270. }
  271. else if ( Fl::test_shortcut( FL_CTRL + FL_Left ) )
  272. {
  273. transport->locate( prev( transport->frame ) );
  274. return 1;
  275. }
  276. else if ( Fl::test_shortcut( FL_CTRL + ' ' ) )
  277. {
  278. Sequence_Widget *r = widget_at( transport->frame, y() );
  279. if ( r )
  280. {
  281. if ( r->selected() )
  282. r->deselect();
  283. else
  284. r->select();
  285. }
  286. }
  287. else
  288. {
  289. switch ( Fl::event_key() )
  290. {
  291. case FL_Left:
  292. case FL_Right:
  293. case FL_Up:
  294. case FL_Down:
  295. /* this is a hack to override FLTK's use of arrow keys for
  296. * focus navigation */
  297. return timeline->handle_scroll( m );
  298. default:
  299. break;
  300. }
  301. }
  302. if ( Sequence_Widget::belowmouse() )
  303. return Sequence_Widget::belowmouse()->dispatch( m );
  304. case FL_NO_EVENT:
  305. /* garbage from overlay window */
  306. return 0;
  307. case FL_FOCUS:
  308. case FL_UNFOCUS:
  309. Fl_Widget::handle( m );
  310. redraw();
  311. return 1;
  312. case FL_LEAVE:
  313. // DMESSAGE( "leave" );
  314. fl_cursor( FL_CURSOR_DEFAULT );
  315. Fl_Widget::handle( m );
  316. return 1;
  317. case FL_DND_DRAG:
  318. return 1;
  319. case FL_ENTER:
  320. // DMESSAGE( "enter" );
  321. if ( Sequence_Widget::pushed() )
  322. {
  323. if ( Sequence_Widget::pushed()->sequence()->class_name() == class_name() )
  324. {
  325. /* accept objects dragged from other sequences of this type */
  326. add( Sequence_Widget::pushed() );
  327. redraw();
  328. fl_cursor( FL_CURSOR_MOVE );
  329. }
  330. else
  331. fl_cursor( (Fl_Cursor)1 );
  332. }
  333. else
  334. if ( ! event_widget() )
  335. fl_cursor( cursor() );
  336. Fl_Widget::handle( m );
  337. return 1;
  338. case FL_DND_ENTER:
  339. case FL_DND_LEAVE:
  340. case FL_DND_RELEASE:
  341. return 1;
  342. case FL_MOVE:
  343. {
  344. Sequence_Widget *r = event_widget();
  345. if ( r != Sequence_Widget::belowmouse() )
  346. {
  347. if ( Sequence_Widget::belowmouse() )
  348. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  349. Sequence_Widget::belowmouse( r );
  350. if ( r )
  351. r->handle( FL_ENTER );
  352. }
  353. return 1;
  354. }
  355. default:
  356. {
  357. Sequence_Widget *r = Sequence_Widget::pushed() ? Sequence_Widget::pushed() : event_widget();
  358. /* if ( this == Fl::focus() ) */
  359. /* DMESSAGE( "Sequence widget = %p", r ); */
  360. if ( r )
  361. {
  362. int retval = r->dispatch( m );
  363. /* DMESSAGE( "retval = %d", retval ); */
  364. if ( m == FL_PUSH )
  365. take_focus();
  366. if ( retval )
  367. {
  368. if ( m == FL_PUSH )
  369. {
  370. if ( Sequence_Widget::pushed() )
  371. Sequence_Widget::pushed()->handle( FL_UNFOCUS );
  372. Sequence_Widget::pushed( r );
  373. r->handle( FL_FOCUS );
  374. }
  375. else if ( m == FL_RELEASE )
  376. Sequence_Widget::pushed( NULL );
  377. }
  378. Loggable::block_start();
  379. while ( _delete_queue.size() )
  380. {
  381. Sequence_Widget *t = _delete_queue.front();
  382. _delete_queue.pop();
  383. if ( Sequence_Widget::pushed() == t )
  384. Sequence_Widget::pushed( NULL );
  385. if ( Sequence_Widget::belowmouse() == t )
  386. {
  387. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  388. Sequence_Widget::belowmouse( NULL );
  389. }
  390. delete t;
  391. }
  392. Loggable::block_end();
  393. if ( m == FL_PUSH )
  394. return 1;
  395. else
  396. return retval;
  397. }
  398. else
  399. return Fl_Widget::handle( m );
  400. }
  401. }
  402. }