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.

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