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.

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