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.

562 lines
15KB

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