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.

1238 lines
27KB

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