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.

1405 lines
33KB

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