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.

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