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.

547 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. #include "FL/test_press.H"
  206. int
  207. Sequence::handle ( int m )
  208. {
  209. /* if ( m != FL_NO_EVENT ) */
  210. /* DMESSAGE( "%s", event_name( m ) ); */
  211. switch ( m )
  212. {
  213. case FL_KEYBOARD:
  214. case FL_SHORTCUT:
  215. if ( Fl::test_shortcut( FL_CTRL + FL_Right ) )
  216. {
  217. transport->locate( next( transport->frame ) );
  218. return 1;
  219. }
  220. else if ( Fl::test_shortcut( FL_CTRL + FL_Left ) )
  221. {
  222. transport->locate( prev( transport->frame ) );
  223. return 1;
  224. }
  225. else if ( Fl::test_shortcut( FL_CTRL + ' ' ) )
  226. {
  227. Sequence_Widget *r = widget_at( transport->frame, y() );
  228. if ( r )
  229. {
  230. if ( r->selected() )
  231. r->deselect();
  232. else
  233. r->select();
  234. }
  235. }
  236. else
  237. {
  238. switch ( Fl::event_key() )
  239. {
  240. case FL_Left:
  241. case FL_Right:
  242. case FL_Up:
  243. case FL_Down:
  244. /* this is a hack to override FLTK's use of arrow keys for
  245. * focus navigation */
  246. return timeline->handle_scroll( m );
  247. default:
  248. break;
  249. }
  250. }
  251. if ( Sequence_Widget::belowmouse() )
  252. return Sequence_Widget::belowmouse()->dispatch( m );
  253. case FL_NO_EVENT:
  254. /* garbage from overlay window */
  255. return 0;
  256. case FL_FOCUS:
  257. case FL_UNFOCUS:
  258. Fl_Widget::handle( m );
  259. redraw();
  260. return 1;
  261. case FL_LEAVE:
  262. // DMESSAGE( "leave" );
  263. fl_cursor( FL_CURSOR_DEFAULT );
  264. Fl_Widget::handle( m );
  265. return 1;
  266. case FL_DND_DRAG:
  267. return 1;
  268. case FL_ENTER:
  269. // DMESSAGE( "enter" );
  270. if ( Sequence_Widget::pushed() )
  271. {
  272. if ( Sequence_Widget::pushed()->sequence()->class_name() == class_name() )
  273. {
  274. /* accept objects dragged from other sequences of this type */
  275. add( Sequence_Widget::pushed() );
  276. redraw();
  277. fl_cursor( FL_CURSOR_MOVE );
  278. }
  279. else
  280. fl_cursor( (Fl_Cursor)1 );
  281. }
  282. else
  283. if ( ! event_widget() )
  284. fl_cursor( cursor() );
  285. Fl_Widget::handle( m );
  286. return 1;
  287. case FL_DND_ENTER:
  288. case FL_DND_LEAVE:
  289. case FL_DND_RELEASE:
  290. return 1;
  291. case FL_MOVE:
  292. {
  293. Sequence_Widget *r = event_widget();
  294. if ( r != Sequence_Widget::belowmouse() )
  295. {
  296. if ( Sequence_Widget::belowmouse() )
  297. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  298. Sequence_Widget::belowmouse( r );
  299. if ( r )
  300. r->handle( FL_ENTER );
  301. }
  302. return 1;
  303. }
  304. default:
  305. {
  306. Sequence_Widget *r = Sequence_Widget::pushed() ? Sequence_Widget::pushed() : event_widget();
  307. /* if ( this == Fl::focus() ) */
  308. /* DMESSAGE( "Sequence widget = %p", r ); */
  309. if ( r )
  310. {
  311. int retval = r->dispatch( m );
  312. /* DMESSAGE( "retval = %d", retval ); */
  313. if ( m == FL_PUSH )
  314. take_focus();
  315. if ( retval )
  316. {
  317. if ( m == FL_PUSH )
  318. {
  319. if ( Sequence_Widget::pushed() )
  320. Sequence_Widget::pushed()->handle( FL_UNFOCUS );
  321. Sequence_Widget::pushed( r );
  322. r->handle( FL_FOCUS );
  323. }
  324. else if ( m == FL_RELEASE )
  325. Sequence_Widget::pushed( NULL );
  326. }
  327. Loggable::block_start();
  328. while ( _delete_queue.size() )
  329. {
  330. Sequence_Widget *t = _delete_queue.front();
  331. _delete_queue.pop();
  332. if ( Sequence_Widget::pushed() == t )
  333. Sequence_Widget::pushed( NULL );
  334. if ( Sequence_Widget::belowmouse() == t )
  335. {
  336. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  337. Sequence_Widget::belowmouse( NULL );
  338. }
  339. delete t;
  340. }
  341. Loggable::block_end();
  342. if ( m == FL_PUSH )
  343. return 1;
  344. else
  345. return retval;
  346. }
  347. else
  348. {
  349. if ( test_press( FL_BUTTON1 ) )
  350. {
  351. /* traditional selection model */
  352. Sequence_Widget::select_none();
  353. }
  354. return Fl_Widget::handle( m );
  355. }
  356. }
  357. }
  358. }
  359. /**********/
  360. /* Public */
  361. /**********/
  362. /** calculate the length of this sequence by looking at the end of the
  363. * least widget it contains */
  364. /** return the length in frames of this sequence calculated from the
  365. * right edge of the rightmost widget */
  366. nframes_t
  367. Sequence::length ( void ) const
  368. {
  369. nframes_t l = 0;
  370. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
  371. l = max( l, (*r)->start() + (*r)->length() );
  372. return l;
  373. }
  374. /** return the location of the next widget from frame /from/ */
  375. nframes_t
  376. Sequence::next ( nframes_t from ) const
  377. {
  378. for ( list <Sequence_Widget*>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  379. if ( (*i)->start() > from )
  380. return (*i)->start();
  381. if ( _widgets.size() )
  382. return _widgets.back()->start();
  383. else
  384. return 0;
  385. }
  386. /** return the location of the next widget from frame /from/ */
  387. nframes_t
  388. Sequence::prev ( nframes_t from ) const
  389. {
  390. for ( list <Sequence_Widget*>::const_reverse_iterator i = _widgets.rbegin(); i != _widgets.rend(); i++ )
  391. if ( (*i)->start() < from )
  392. return (*i)->start();
  393. if ( _widgets.size() )
  394. return _widgets.front()->start();
  395. else
  396. return 0;
  397. }
  398. /** delete all selected widgets in this sequence */
  399. void
  400. Sequence::remove_selected ( void )
  401. {
  402. Loggable::block_start();
  403. for ( list <Sequence_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
  404. if ( (*r)->selected() )
  405. {
  406. Sequence_Widget *t = *r;
  407. _widgets.erase( r++ );
  408. delete t;
  409. }
  410. else
  411. ++r;
  412. Loggable::block_end();
  413. }
  414. /** select all widgets intersecting with the range defined by the
  415. * pixel coordinates X through W */
  416. void
  417. Sequence::select_range ( int X, int W )
  418. {
  419. nframes_t sts = x_to_offset( X );
  420. nframes_t ets = sts + timeline->x_to_ts( W );
  421. for ( list <Sequence_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); ++r )
  422. if ( ! ( (*r)->start() > ets || (*r)->start() + (*r)->length() < sts ) )
  423. (*r)->select();
  424. }