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.

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