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.

1257 lines
28KB

  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 "Timeline.H"
  19. #include "Tempo_Sequence.H"
  20. #include "Time_Sequence.H"
  21. #include "Audio_Sequence.H"
  22. #include "Control_Sequence.H"
  23. #include <FL/Fl_Scrollbar.H>
  24. #include "Annotation_Sequence.H"
  25. // #include <FL/Fl_Image.H>
  26. // #include <FL/Fl_RGB_Image.H> // needed for alpha blending
  27. #include "Track.H"
  28. bool Timeline::draw_with_measure_lines = true;
  29. Timeline::snap_e Timeline::snap_to = Bars;
  30. bool Timeline::snap_magnetic = true;
  31. bool Timeline::follow_playhead = true;
  32. bool Timeline::center_playhead = true;
  33. const float UPDATE_FREQ = 0.02f;
  34. #include "Playback_DS.H"
  35. #include "Record_DS.H"
  36. #include "Transport.H"
  37. /** return the combined height of all visible children of (veritcal)
  38. pack, /p/. This is necessary because pack sizes are adjusted only
  39. when the relevant areas are exposes. */
  40. static int
  41. pack_visible_height ( const Fl_Pack *p )
  42. {
  43. int th = 0;
  44. const Fl_Widget* const *w = p->array();
  45. for ( int i = p->children(); i--; ++w )
  46. if ( (*w)->visible() )
  47. th += (*w)->h() + p->spacing();
  48. return th;
  49. }
  50. /** recalculate the size of vertical scrolling area and inform scrollbar */
  51. void
  52. Timeline::adjust_vscroll ( void )
  53. {
  54. vscroll->value( _yposition, h() - rulers->h() - hscroll->h(), 0, pack_visible_height( tracks ) );
  55. }
  56. void
  57. Timeline::cb_scroll ( Fl_Widget *w, void *v )
  58. {
  59. ((Timeline*)v)->cb_scroll( w );
  60. }
  61. void
  62. Timeline::cb_scroll ( Fl_Widget *w )
  63. {
  64. if ( w == vscroll )
  65. {
  66. tracks->position( tracks->x(), (rulers->y() + rulers->h()) - vscroll->value() );
  67. yposition( vscroll->value() );
  68. adjust_vscroll();
  69. }
  70. else
  71. {
  72. if ( hscroll->zoom_changed() )
  73. {
  74. nframes_t under_mouse = x_to_offset( Fl::event_x() );
  75. _fpp = hscroll->zoom();
  76. const int tw = tracks->w() - Track::width();
  77. // hscroll->value( ts_to_x( xoffset ), tw, 0, ts_to_x( _length ) );
  78. hscroll->value( max( 0, ts_to_x( under_mouse ) - ( Fl::event_x() - tracks->x() - Track::width() ) ),
  79. tw, 0, ts_to_x( _length ) );
  80. redraw();
  81. }
  82. xposition( hscroll->value() );
  83. }
  84. }
  85. Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Window( X, Y, W, H, L )
  86. {
  87. box( FL_FLAT_BOX );
  88. xoffset = 0;
  89. _yposition = 0;
  90. _old_yposition = 0;
  91. _old_xposition = 0;
  92. X = Y = 0;
  93. p1 = p2 = 0;
  94. {
  95. Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
  96. o->range( 0, 48000 * 300 );
  97. // o->zoom_range( 1, 16384 );
  98. // o->zoom_range( 1, 65536 << 4 );
  99. o->zoom_range( 1, 20 );
  100. o->zoom( 8 );
  101. o->type( FL_HORIZONTAL );
  102. o->callback( cb_scroll, this );
  103. hscroll = o;
  104. }
  105. {
  106. Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
  107. o->type( FL_VERTICAL );
  108. o->callback( cb_scroll, this );
  109. vscroll = o;
  110. }
  111. {
  112. Fl_Pack *o = new Fl_Pack( X + Track::width(), Y, (W - Track::width()) - vscroll->w(), H - hscroll->h(), "rulers" );
  113. o->type( Fl_Pack::VERTICAL );
  114. {
  115. Tempo_Sequence *o = new Tempo_Sequence( 0, 0, 800, 24 );
  116. o->color( fl_gray_ramp( 18 ) );
  117. o->label( "Tempo" );
  118. o->align( FL_ALIGN_LEFT );
  119. tempo_track = o;
  120. }
  121. {
  122. Time_Sequence *o = new Time_Sequence( 0, 24, 800, 24 );
  123. o->color( fl_gray_ramp( 16 ) );
  124. o->label( "Time" );
  125. o->align( FL_ALIGN_LEFT );
  126. time_track = o;
  127. }
  128. /* { */
  129. /* Annotation_Sequence *o = new Annotation_Sequence( 0, 24, 800, 24 ); */
  130. /* o->color( fl_gray_ramp( 'F' ) ); */
  131. /* o->label( "Ruler" ); */
  132. /* o->align( FL_ALIGN_LEFT ); */
  133. /* ruler_track = o; */
  134. /* } */
  135. o->size( o->w(), o->child( 0 )->h() * o->children() );
  136. rulers = o;
  137. o->end();
  138. }
  139. {
  140. // sample_rate() = engine->sample_rate();
  141. _fpp = 8;
  142. // _length = sample_rate() * 60 * 2;
  143. /* FIXME: hack */
  144. _length = -1;
  145. {
  146. Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 1 );
  147. o->type( Fl_Pack::VERTICAL );
  148. o->spacing( 1 );
  149. tracks = o;
  150. o->end();
  151. resizable( o );
  152. }
  153. }
  154. /* rulers go above tracks... */
  155. add( rulers );
  156. /* make sure scrollbars are on top */
  157. add( vscroll );
  158. add( hscroll );
  159. vscroll->range( 0, tracks->h() );
  160. redraw();
  161. end();
  162. Fl::add_timeout( UPDATE_FREQ, update_cb, this );
  163. }
  164. float
  165. Timeline::beats_per_minute ( nframes_t when ) const
  166. {
  167. return tempo_track->beats_per_minute( when );
  168. }
  169. int
  170. Timeline::beats_per_bar ( nframes_t when ) const
  171. {
  172. time_sig t = time_track->time( when );
  173. return t.beats_per_bar;
  174. }
  175. void
  176. Timeline::beats_per_minute ( nframes_t when, float bpm )
  177. {
  178. tempo_track->add( new Tempo_Point( when, bpm ) );
  179. }
  180. void
  181. Timeline::time ( nframes_t when, int bpb, int note_type )
  182. {
  183. time_track->add( new Time_Point( when, bpb, note_type ) );
  184. }
  185. /* struct BBT */
  186. /* { */
  187. /* int bar, beat, tick; */
  188. /* BBT ( int bar, int beat, int tick ) : bar( bar ), beat( beat ), tick( tick ) */
  189. /* { */
  190. /* } */
  191. /* }; */
  192. /* /\** returns the BBT value for timestamp /when/ by examining the tempo/time maps *\/ */
  193. /* BBT */
  194. /* Timeline::bbt ( nframes_t when ) */
  195. /* { */
  196. /* Tempo_Sequence *tempo = (Tempo_Sequence*)rulers->child( 0 ); */
  197. /* BBT bbt; */
  198. /* for ( list <Sequence_Widget *>::const_iterator i = tempo.widgets.begin(); */
  199. /* i != tempo.widgets.end(); ++i ) */
  200. /* { */
  201. /* Tempo_Point *p = *i; */
  202. /* } */
  203. /* } */
  204. struct nearest_line_arg
  205. {
  206. nframes_t original;
  207. nframes_t closest;
  208. };
  209. const int snap_pixel = 10;
  210. static nframes_t
  211. abs_diff ( nframes_t n1, nframes_t n2 )
  212. {
  213. return n1 > n2 ? n1 - n2 : n2 - n1;
  214. }
  215. void
  216. nearest_line_cb ( nframes_t frame, int X, int Y, int H, void *arg )
  217. {
  218. nearest_line_arg *n = (nearest_line_arg *)arg;
  219. if ( Timeline::snap_magnetic &&
  220. abs_diff( frame, n->original ) > timeline->x_to_ts( snap_pixel ) )
  221. return;
  222. if ( abs_diff( frame, n->original ) < abs_diff( n->original, n->closest ) )
  223. n->closest = frame;
  224. }
  225. /** return the absolute pixel of the nearest measure line to /x/ */
  226. bool
  227. Timeline::nearest_line ( nframes_t when, nframes_t *frame ) const
  228. {
  229. if ( snap_to == None )
  230. return false;
  231. nearest_line_arg n = { when, -1 };
  232. /* FIXME: handle snap to bar */
  233. draw_measure( when - x_to_ts( 10 ), 0, 20, 0, (Fl_Color)0, nearest_line_cb, &n, false );
  234. *frame = n.closest;
  235. return *frame != (nframes_t)-1;
  236. }
  237. nframes_t
  238. Timeline::x_to_offset ( int x ) const
  239. {
  240. return x_to_ts( max( 0, x - Track::width() ) ) + xoffset;
  241. }
  242. void
  243. static
  244. draw_measure_cb ( nframes_t frame, int X, int Y, int H, void *arg )
  245. {
  246. fl_color( FL_BLACK );
  247. fl_line( X, Y, X, Y + H );
  248. }
  249. /** draw appropriate measure lines inside the given bounding box */
  250. void
  251. Timeline::draw_measure ( nframes_t when, int Y, int W, int H, Fl_Color color, measure_line_callback * cb, void *arg, bool BBT ) const
  252. {
  253. if ( ! draw_with_measure_lines )
  254. return;
  255. if ( ! tempo_track->_widgets.size() || ! time_track->_widgets.size() )
  256. {
  257. DWARNING( "No tempo/time points" );
  258. return;
  259. }
  260. fl_line_style( FL_DASH, 0 );
  261. const Fl_Color beat = fl_color_average( FL_BLACK, color, 0.65f );
  262. const Fl_Color bar = fl_color_average( FL_RED, beat, 0.65f );
  263. const nframes_t samples_per_minute = sample_rate() * 60;
  264. /* we need to back up a bit in order to catch all the numbers */
  265. /* if ( BBT ) */
  266. /* { */
  267. /* X -= 40; */
  268. /* W += 40; */
  269. /* } */
  270. list <Sequence_Widget*>::const_iterator tpi;
  271. list <Sequence_Widget*>::const_iterator mpi;
  272. /* find the first points before our range */
  273. for ( list <Sequence_Widget *>::const_reverse_iterator i = tempo_track->_widgets.rbegin();
  274. i != tempo_track->_widgets.rend(); i++ )
  275. if ( (*i)->start() <= when )
  276. {
  277. tpi = i.base();
  278. break;
  279. }
  280. for ( list <Sequence_Widget *>::const_reverse_iterator i = time_track->_widgets.rbegin();
  281. i != time_track->_widgets.rend(); i++ )
  282. if ( (*i)->start() <= when )
  283. {
  284. mpi = i.base();
  285. break;
  286. }
  287. --tpi;
  288. /* start on the next beat */
  289. const Tempo_Point *tp = (Tempo_Point*)(*tpi);
  290. nframes_t beat_inc = samples_per_minute / tp->tempo();
  291. nframes_t f = when - ( ( when - tp->start() ) % beat_inc );
  292. for ( ; tpi != tempo_track->_widgets.end(); ++tpi )
  293. {
  294. list <Sequence_Widget*>::const_iterator ntpi = tpi;
  295. ++ntpi;
  296. const Tempo_Point *ntp = ntpi == tempo_track->_widgets.end() ? NULL : (Tempo_Point*)(*ntpi);
  297. tp = (Tempo_Point*)(*tpi);
  298. nframes_t ntpo = ntp ? ntp->start() : when + x_to_ts( W + 1 );
  299. beat_inc = samples_per_minute / tp->tempo();
  300. const int incx = ts_to_x( beat_inc );
  301. /* lock to beat lines */
  302. ntpo += (ntpo % beat_inc);
  303. if ( incx < 8 )
  304. continue;
  305. for ( ; f < ntpo; f += beat_inc )
  306. {
  307. const int x = ts_to_x( f - xoffset ) + Track::width();
  308. cb( f, x, Y, H, arg );
  309. }
  310. }
  311. fl_line_style( FL_SOLID, 0 );
  312. }
  313. void
  314. Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
  315. {
  316. draw_measure( x_to_offset( X ), Y, W, H, color, draw_measure_cb, 0, false );
  317. }
  318. /** just like draw mesure lines except that it also draws the BBT values. */
  319. void
  320. Timeline::draw_measure_BBT ( int X, int Y, int W, int H, Fl_Color color )
  321. {
  322. // draw_measure( X, Y, W, H, color, true );
  323. }
  324. void
  325. Timeline::xposition ( int X )
  326. {
  327. // _old_xposition = xoffset;
  328. /* FIXME: shouldn't have to do this... */
  329. X = min( X, ts_to_x( _length ) - tracks->w() - Track::width() );
  330. xoffset = x_to_ts( X );
  331. damage( FL_DAMAGE_SCROLL );
  332. }
  333. void
  334. Timeline::yposition ( int Y )
  335. {
  336. // _old_yposition = _yposition;
  337. _yposition = Y;
  338. damage( FL_DAMAGE_SCROLL );
  339. }
  340. void
  341. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  342. {
  343. Timeline *tl = (Timeline *)v;
  344. fl_push_clip( X, Y, W, H );
  345. /* fl_color( rand() ); */
  346. /* fl_rectf( X, Y, X + W, Y + H ); */
  347. tl->draw_box();
  348. tl->draw_child( *tl->rulers );
  349. fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), tl->tracks->w(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  350. tl->draw_child( *tl->tracks );
  351. fl_pop_clip();
  352. fl_pop_clip();
  353. }
  354. // static unsigned char *rect_image;
  355. void
  356. Timeline::resize ( int X, int Y, int W, int H )
  357. {
  358. Fl_Overlay_Window::resize( X, Y, W, H );
  359. /* why is this necessary? */
  360. rulers->resize( Track::width(), 0, W - Track::width() - vscroll->w(), rulers->h() );
  361. /* why is THIS necessary? */
  362. hscroll->resize( 0, H - 18, hscroll->w(), 18 );
  363. vscroll->size( vscroll->w(), H - 18 );
  364. }
  365. void
  366. Timeline::draw ( void )
  367. {
  368. int X, Y, W, H;
  369. int bdx = 0;
  370. int bdw = 0;
  371. X = tracks->x() + bdx + 1;
  372. Y = tracks->y();
  373. W = tracks->w() - bdw - 1;
  374. H = tracks->h();
  375. adjust_vscroll();
  376. /* if ( damage() & FL_DAMAGE_USER1 ) */
  377. /* { */
  378. /* /\* save the rectangle so we can draw it (darkened) in the overlay *\/ */
  379. /* Rectangle &r = _selection; */
  380. /* make_current(); */
  381. /* rect_image = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  382. /* return; */
  383. /* } */
  384. if ( ( damage() & FL_DAMAGE_ALL ) || ( damage() & FL_DAMAGE_EXPOSE ) )
  385. {
  386. DMESSAGE( "complete redraw" );
  387. draw_box( box(), 0, 0, w(), h(), color() );
  388. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  389. draw_child( *rulers );
  390. fl_pop_clip();
  391. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  392. draw_child( *tracks );
  393. fl_pop_clip();
  394. draw_child( *hscroll );
  395. draw_child( *vscroll );
  396. redraw_overlay();
  397. /* Rectangle &r = _selection; */
  398. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  399. /* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
  400. /* bi.color_average( FL_BLACK, 0.50f ); */
  401. /* bi.draw( r.x, r.y ); */
  402. /* delete[] data; */
  403. /* if ( r.w && r.h ) */
  404. /* { */
  405. /* const unsigned char data[] = { 0, 127, 0, 96, */
  406. /* 0, 96, 0, 127 }; */
  407. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  408. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  409. /* bi2->draw( r.x, r.y ); */
  410. /* delete bi2; */
  411. /* } */
  412. goto done;
  413. }
  414. if ( damage() & FL_DAMAGE_SCROLL )
  415. {
  416. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  417. int dy = _old_yposition - _yposition;
  418. /* draw_child( *rulers ); */
  419. if ( ! dy )
  420. fl_scroll( rulers->x(), rulers->y(), rulers->w(), rulers->h(), dx, 0, draw_clip, this );
  421. Y = rulers->y() + rulers->h();
  422. H = h() - rulers->h() - hscroll->h();
  423. if ( dy == 0 )
  424. fl_scroll( X + Track::width(), Y, W - Track::width(), H, dx, dy, draw_clip, this );
  425. else
  426. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  427. }
  428. if ( damage() & FL_DAMAGE_CHILD )
  429. {
  430. fl_push_clip( rulers->x(), rulers->y(), rulers->w(), rulers->h() );
  431. update_child( *rulers );
  432. fl_pop_clip();
  433. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - rulers->h() - hscroll->h() );
  434. update_child( *tracks );
  435. fl_pop_clip();
  436. update_child( *hscroll );
  437. update_child( *vscroll );
  438. }
  439. done:
  440. _old_xposition = xoffset;
  441. _old_yposition = _yposition;
  442. }
  443. void
  444. Timeline::draw_cursor ( nframes_t frame, Fl_Color color )
  445. {
  446. // int x = ( ts_to_x( frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track::width();
  447. if ( frame < xoffset )
  448. return;
  449. const int x = ts_to_x( frame - xoffset ) + tracks->x() + Track::width();
  450. if ( x > tracks->x() + tracks->w() )
  451. return;
  452. fl_color( color );
  453. const int y = rulers->y() + rulers->h();
  454. const int h = this->h() - hscroll->h();
  455. fl_push_clip( Track::width(), y, tracks->w(), h );
  456. fl_line( x, y, x, h );
  457. fl_color( fl_darker( color ) );
  458. fl_line( x - 1, y, x - 1, h );
  459. fl_color( FL_BLACK );
  460. fl_line( x + 1, y, x + 1, h );
  461. /* draw cap */
  462. fl_color( color );
  463. fl_begin_polygon();
  464. fl_vertex( x - 8, y );
  465. fl_vertex( x, y + 8 );
  466. fl_vertex( x + 8, y );
  467. fl_end_polygon();
  468. /* draw cap outline */
  469. fl_color( FL_BLACK );
  470. fl_begin_line();
  471. fl_vertex( x - 8, y );
  472. fl_vertex( x, y + 8 );
  473. fl_vertex( x + 8, y );
  474. fl_end_line();
  475. fl_pop_clip();
  476. }
  477. void
  478. Timeline::draw_playhead ( void )
  479. {
  480. if ( p1 != p2 )
  481. {
  482. draw_cursor( p1, FL_BLUE );
  483. draw_cursor( p2, FL_GREEN );
  484. }
  485. draw_cursor( transport->frame, FL_RED );
  486. }
  487. void
  488. Timeline::redraw_playhead ( void )
  489. {
  490. static nframes_t last_playhead = -1;
  491. if ( last_playhead != transport->frame )
  492. {
  493. redraw_overlay();
  494. last_playhead = transport->frame;
  495. if ( follow_playhead )
  496. {
  497. if ( center_playhead && active() )
  498. xposition( max( 0, ts_to_x( transport->frame ) - ( ( tracks->w() - Track::width() ) >> 1 ) ) );
  499. else if ( ts_to_x( transport->frame ) > ts_to_x( xoffset ) + ( tracks->w() - Track::width() ) )
  500. xposition( ts_to_x( transport->frame ) );
  501. }
  502. }
  503. }
  504. /** called so many times a second to redraw the playhead etc. */
  505. void
  506. Timeline::update_cb ( void *arg )
  507. {
  508. Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
  509. Timeline *tl = (Timeline *)arg;
  510. tl->redraw_playhead();
  511. }
  512. void
  513. Timeline::draw_overlay ( void )
  514. {
  515. draw_playhead();
  516. if ( ! ( _selection.w && _selection.h ) )
  517. return;
  518. fl_push_clip( tracks->x() + Track::width(), rulers->y() + rulers->h(), tracks->w() - Track::width(), h() - rulers->h() - hscroll->h() );
  519. const Rectangle &r = _selection;
  520. fl_color( FL_BLACK );
  521. fl_line_style( FL_SOLID, 2 );
  522. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  523. fl_color( FL_MAGENTA );
  524. fl_line_style( FL_DASH, 2 );
  525. fl_rect( r.x, r.y, r.w, r.h );
  526. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  527. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  528. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  529. fl_line_style( FL_SOLID, 0 );
  530. /* const unsigned char data[] = { 127, 127, 127, 96, */
  531. /* 127, 96, 127, 40 }; */
  532. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  533. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  534. /* bi2->draw( r.x, r.y ); */
  535. /* delete bi2; */
  536. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  537. /* Fl_RGB_Image bi( rect_image, r.w, r.h, 3 ); */
  538. /* bi.color_average( FL_BLACK, 0.50f ); */
  539. /* bi.draw( r.x, r.y ); */
  540. /* delete[] rect_image; */
  541. /* rect_image = NULL; */
  542. fl_pop_clip();
  543. }
  544. // #include "Sequence_Widget.H"
  545. /** select all widgets in inside rectangle /r/ */
  546. void
  547. Timeline::select ( const Rectangle &r )
  548. {
  549. const int Y = r.y;
  550. for ( int i = tracks->children(); i-- ; )
  551. {
  552. Track *t = (Track*)tracks->child( i );
  553. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  554. t->select( r.x, r.y, r.w, r.h, true, true );
  555. }
  556. }
  557. void
  558. Timeline::delete_selected ( void )
  559. {
  560. Sequence_Widget::delete_selected();
  561. }
  562. void
  563. Timeline::select_none ( void )
  564. {
  565. Sequence_Widget::select_none();
  566. }
  567. /** An unfortunate necessity for implementing our own DND aside from
  568. * the (bogus) native FLTK system */
  569. Track *
  570. Timeline::track_under ( int Y )
  571. {
  572. for ( int i = tracks->children(); i-- ; )
  573. {
  574. Track *t = (Track*)tracks->child( i );
  575. if ( ! ( t->y() > Y || t->y() + t->h() < Y ) )
  576. return t;
  577. }
  578. return NULL;
  579. }
  580. int
  581. Timeline::handle ( int m )
  582. {
  583. static Drag *drag = NULL;
  584. switch ( m )
  585. {
  586. case FL_FOCUS:
  587. case FL_UNFOCUS:
  588. // redraw();
  589. return 1;
  590. case FL_KEYBOARD:
  591. case FL_SHORTCUT:
  592. {
  593. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  594. /* we don't want any keys with modifiers... */
  595. return 0;
  596. switch ( Fl::event_key() )
  597. {
  598. case FL_Delete:
  599. case FL_Home:
  600. case FL_End:
  601. /* keep scrollbar from eating these. */
  602. return 0;
  603. case 'p':
  604. {
  605. int X = Fl::event_x() - Track::width();
  606. if ( X > 0 )
  607. {
  608. transport->locate( xoffset + x_to_ts( X ) );
  609. }
  610. return 1;
  611. }
  612. case '[':
  613. {
  614. int X = Fl::event_x() - Track::width();
  615. if ( X > 0 )
  616. {
  617. p1 = xoffset + x_to_ts( X );
  618. }
  619. return 1;
  620. }
  621. case ']':
  622. {
  623. int X = Fl::event_x() - Track::width();
  624. if ( X > 0 )
  625. {
  626. p2 = xoffset + x_to_ts( X );
  627. }
  628. return 1;
  629. }
  630. default:
  631. return Fl_Overlay_Window::handle( m );
  632. }
  633. return 0;
  634. }
  635. default:
  636. {
  637. if ( m == FL_PUSH )
  638. Fl::focus( this );
  639. int r = Fl_Overlay_Window::handle( m );
  640. if ( m != FL_RELEASE && r )
  641. return r;
  642. const int X = Fl::event_x();
  643. const int Y = Fl::event_y();
  644. switch ( m )
  645. {
  646. case FL_PUSH:
  647. {
  648. // take_focus();
  649. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  650. return 0;
  651. if ( Fl::event_button1() )
  652. {
  653. assert( ! drag );
  654. drag = new Drag( X - x(), Y - y() );
  655. _selection.x = drag->x;
  656. _selection.y = drag->y;
  657. }
  658. else if ( Fl::test_shortcut( FL_BUTTON3 ) && ! Fl::event_shift() )
  659. {
  660. Fl_Menu_Item menu[] =
  661. {
  662. { "Add Track", 0, 0, 0, FL_SUBMENU },
  663. { "Audio", 0, 0, 0 },
  664. { 0 },
  665. { 0 },
  666. };
  667. const Fl_Menu_Item *r = menu->popup( X, Y, "Timeline" );
  668. if ( r == &menu[1] )
  669. {
  670. /* FIXME: prompt for I/O config? */
  671. Loggable::block_start();
  672. /* add audio track */
  673. char *name = get_unique_track_name( "Audio" );
  674. Track *t = new Track( name );
  675. Audio_Sequence *o = new Audio_Sequence( t );
  676. // new Control_Sequence( t );
  677. add_track( t );
  678. t->sequence( o );
  679. Loggable::block_end();
  680. }
  681. }
  682. else
  683. return 0;
  684. break;
  685. }
  686. case FL_DRAG:
  687. {
  688. int ox = X - drag->x;
  689. int oy = Y - drag->y;
  690. if ( ox < 0 )
  691. _selection.x = X;
  692. if ( oy < 0 )
  693. _selection.y = Y;
  694. _selection.w = abs( ox );
  695. _selection.h = abs( oy );
  696. break;
  697. }
  698. case FL_RELEASE:
  699. {
  700. delete drag;
  701. drag = NULL;
  702. select( _selection );
  703. _selection.w = _selection.h = 0;
  704. break;
  705. }
  706. default:
  707. return 0;
  708. break;
  709. }
  710. redraw_overlay();
  711. return 1;
  712. }
  713. }
  714. }
  715. void
  716. Timeline::zoom_in ( void )
  717. {
  718. hscroll->zoom_in();
  719. }
  720. void
  721. Timeline::zoom_out ( void )
  722. {
  723. hscroll->zoom_out();
  724. }
  725. /** zoom the display to show /secs/ seconds per screen */
  726. void
  727. Timeline::zoom ( float secs )
  728. {
  729. const int sw = tracks->w() - Track::width();
  730. int fpp = (int)((secs * sample_rate()) / sw);
  731. int p = 0;
  732. while ( 1 << p < fpp ) p++;
  733. hscroll->zoom( p );
  734. redraw();
  735. }
  736. void
  737. Timeline::zoom_fit ( void )
  738. {
  739. zoom( _length / (float)sample_rate() );
  740. }
  741. Track *
  742. Timeline::track_by_name ( const char *name )
  743. {
  744. for ( int i = tracks->children(); i-- ; )
  745. {
  746. Track *t = (Track*)tracks->child( i );
  747. if ( ! strcmp( name, t->name() ) )
  748. return t;
  749. }
  750. return NULL;
  751. }
  752. char *
  753. Timeline::get_unique_track_name ( const char *name )
  754. {
  755. char pat[256];
  756. strcpy( pat, name );
  757. for ( int i = 1; track_by_name( pat ); ++i )
  758. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  759. return strdup( pat );
  760. }
  761. void
  762. Timeline::add_track ( Track *track )
  763. {
  764. DMESSAGE( "added new track to the timeline" );
  765. /* FIXME: do locking */
  766. tracks->add( track );
  767. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  768. redraw();
  769. }
  770. void
  771. Timeline::remove_track ( Track *track )
  772. {
  773. DMESSAGE( "removed track from the timeline" );
  774. /* FIXME: do locking */
  775. /* FIXME: what to do about track contents? */
  776. tracks->remove( track );
  777. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  778. redraw();
  779. }
  780. /** Initiate recording for all armed tracks */
  781. bool
  782. Timeline::record ( void )
  783. {
  784. /* FIXME: right place for this? */
  785. transport->recording = true;
  786. Loggable::block_start();
  787. for ( int i = tracks->children(); i-- ; )
  788. {
  789. Track *t = (Track*)tracks->child( i );
  790. if ( t->armed() && t->record_ds )
  791. t->record_ds->start( transport->frame );
  792. }
  793. deactivate();
  794. return true;
  795. }
  796. /** stop recording for all armed tracks */
  797. void
  798. Timeline::stop ( void )
  799. {
  800. for ( int i = tracks->children(); i-- ; )
  801. {
  802. Track *t = (Track*)tracks->child( i );
  803. if ( t->armed() && t->record_ds )
  804. t->record_ds->stop( transport->frame );
  805. }
  806. Loggable::block_end();
  807. activate();
  808. transport->recording = false;
  809. }
  810. /**********/
  811. /* Engine */
  812. /**********/
  813. /** call process() on each track header */
  814. nframes_t
  815. Timeline::process ( nframes_t nframes )
  816. {
  817. for ( int i = tracks->children(); i-- ; )
  818. {
  819. Track *t = (Track*)tracks->child( i );
  820. t->process( nframes );
  821. }
  822. /* FIXME: BOGUS */
  823. return nframes;
  824. }
  825. /* THREAD: RT */
  826. void
  827. Timeline::seek ( nframes_t frame )
  828. {
  829. for ( int i = tracks->children(); i-- ; )
  830. {
  831. Track *t = (Track*)tracks->child( i );
  832. t->seek( frame );
  833. }
  834. }
  835. /* THREAD: RT */
  836. int
  837. Timeline::seek_pending ( void )
  838. {
  839. int r = 0;
  840. for ( int i = tracks->children(); i-- ; )
  841. {
  842. Track *t = (Track*)tracks->child( i );
  843. if ( t->playback_ds )
  844. r += t->playback_ds->buffer_percent() < 50;
  845. }
  846. return r;
  847. }
  848. /* FIXME: shouldn't these belong to the engine? */
  849. int
  850. Timeline::total_input_buffer_percent ( void )
  851. {
  852. int r = 0;
  853. int cnt = 0;
  854. for ( int i = tracks->children(); i-- ; )
  855. {
  856. Track *t = (Track*)tracks->child( i );
  857. if ( t->record_ds )
  858. {
  859. ++cnt;
  860. r += t->record_ds->buffer_percent();
  861. }
  862. }
  863. if ( ! cnt )
  864. return 0;
  865. return r / cnt;
  866. }
  867. int
  868. Timeline::total_output_buffer_percent ( void )
  869. {
  870. int r = 0;
  871. int cnt = 0;
  872. for ( int i = tracks->children(); i-- ; )
  873. {
  874. Track *t = (Track*)tracks->child( i );
  875. if ( t->playback_ds )
  876. {
  877. ++cnt;
  878. r += t->playback_ds->buffer_percent();
  879. }
  880. }
  881. if ( ! cnt )
  882. return 0;
  883. return r / cnt;
  884. }
  885. int
  886. Timeline::total_playback_xruns ( void )
  887. {
  888. int r = 0;
  889. for ( int i = tracks->children(); i-- ; )
  890. {
  891. Track *t = (Track*)tracks->child( i );
  892. if ( t->playback_ds )
  893. r += t->playback_ds->xruns();
  894. }
  895. return r;
  896. }
  897. int
  898. Timeline::total_capture_xruns ( void )
  899. {
  900. int r = 0;
  901. for ( int i = tracks->children(); i-- ; )
  902. {
  903. Track *t = (Track*)tracks->child( i );
  904. if ( t->record_ds )
  905. r += t->record_ds->xruns();
  906. }
  907. return r;
  908. }