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.

622 lines
17KB

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