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.

537 lines
14KB

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