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.

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