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.

615 lines
16KB

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