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.

1366 lines
31KB

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