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.

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