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.

496 lines
13KB

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