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.

1399 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_snap_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. nearest_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. static void
  405. prev_next_line_cb ( nframes_t frame, const BBT &bbt, void *arg )
  406. {
  407. nearest_line_arg *n = (nearest_line_arg *)arg;
  408. if ( n->bar && bbt.beat )
  409. return;
  410. if ( abs_diff( frame, n->original ) < abs_diff( n->original, n->closest ) )
  411. n->closest = frame;
  412. }
  413. /** Set the value pointed to by /frame/ to the frame number of the of
  414. the nearest measure line to /when/. Returns true if the new value of
  415. *frame is valid, false otherwise. */
  416. bool
  417. Timeline::nearest_line ( nframes_t *frame, bool snap ) const
  418. {
  419. if ( snap && None == Timeline::snap_to )
  420. return false;
  421. nframes_t when = *frame;
  422. nearest_line_arg n = { when, -1, snap && Timeline::Bars == Timeline::snap_to };
  423. render_tempomap( when > x_to_ts( w() >> 1 ) ? when - x_to_ts( w() >> 1 ) : 0,
  424. when + x_to_ts( w() >> 1 ), snap ? nearest_line_snap_cb : nearest_line_cb, &n );
  425. if ( n.closest == (nframes_t)-1 )
  426. return false;
  427. else
  428. {
  429. *frame = n.closest;
  430. return true;
  431. }
  432. }
  433. /** Set the value pointed to by /frame/ to the frame number of the of
  434. the nearest measure line to *greater than* /when/. Returns true if
  435. the new value of *frame is valid, false otherwise. */
  436. bool
  437. Timeline::next_line ( nframes_t *frame, bool bar ) const
  438. {
  439. nframes_t when = *frame + 1;
  440. nearest_line_arg n = { when, -1, bar };
  441. render_tempomap( when, x_to_ts( w() ), prev_next_line_cb, &n );
  442. if ( n.closest == (nframes_t)-1 )
  443. return false;
  444. else
  445. {
  446. *frame = n.closest;
  447. return true;
  448. }
  449. }
  450. /** Set the value pointed to by /frame/ to the frame number of the of
  451. the nearest measure line to *less than* /when/. Returns true if
  452. the new value of *frame is valid, false otherwise. */
  453. bool
  454. Timeline::prev_line ( nframes_t *frame, bool bar ) const
  455. {
  456. nframes_t when = *frame - 1;
  457. nearest_line_arg n = { when, -1, bar };
  458. render_tempomap( xoffset, when - xoffset, prev_next_line_cb, &n );
  459. if ( n.closest == (nframes_t)-1 )
  460. return false;
  461. else
  462. {
  463. *frame = n.closest;
  464. return true;
  465. }
  466. }
  467. /** given screen pixel coordinate /x/ return frame offset into
  468. * timeline, taking into account the current scroll position, widget
  469. * layout, etc. */
  470. nframes_t
  471. Timeline::x_to_offset ( int x ) const
  472. {
  473. return x_to_ts( max( 0, x - Track::width() ) ) + xoffset;
  474. }
  475. /** draws a single measure line */
  476. static void
  477. draw_measure_cb ( nframes_t frame, const BBT &bbt, void *arg )
  478. {
  479. Fl_Color *color = (Fl_Color*)arg;
  480. fl_color( FL_BLACK );
  481. fl_line_style( FL_DASH, 0 );
  482. if ( bbt.beat )
  483. ++color;
  484. fl_color( *color );
  485. const int x = timeline->ts_to_x( frame - timeline->xoffset ) + Track::width();
  486. fl_line( x, 0, x, 5000 );
  487. fl_line_style( FL_SOLID, 0 );
  488. }
  489. /* FIXME: wrong place for this */
  490. const float ticks_per_beat = 1920.0;
  491. /** re-render the unified tempomap based on the current contents of the Time and Tempo sequences */
  492. void
  493. Timeline::update_tempomap ( void )
  494. {
  495. /* FIXME: we need some type of locking! */
  496. _tempomap.clear();
  497. for ( list <Sequence_Widget *>::const_iterator i = time_track->_widgets.begin();
  498. i != time_track->_widgets.end(); ++i )
  499. _tempomap.push_back( *i );
  500. for ( list <Sequence_Widget *>::const_iterator i = tempo_track->_widgets.begin();
  501. i != tempo_track->_widgets.end(); ++i )
  502. _tempomap.push_back( *i );
  503. _tempomap.sort( Sequence_Widget::sort_func );
  504. }
  505. /** return a stucture containing the BBT info which applies at /frame/ */
  506. position_info
  507. Timeline::solve_tempomap ( nframes_t frame ) const
  508. {
  509. return render_tempomap( frame, 0, 0, 0 );
  510. }
  511. /* THREAD: UI and RT */
  512. /** draw appropriate measure lines inside the given bounding box */
  513. position_info
  514. Timeline::render_tempomap( nframes_t start, nframes_t length, measure_line_callback * cb, void *arg ) const
  515. {
  516. const nframes_t end = start + length;
  517. position_info pos;
  518. memset( &pos, 0, sizeof( pos ) );
  519. BBT &bbt = pos.bbt;
  520. /* default values */
  521. pos.beat_type = 4;
  522. pos.beats_per_bar = 4;
  523. pos.tempo = 120.0;
  524. const nframes_t samples_per_minute = sample_rate() * 60;
  525. float bpm = 120.0f;
  526. time_sig sig;
  527. sig.beats_per_bar = 4;
  528. sig.beat_type = 4;
  529. nframes_t f = 0;
  530. nframes_t next = 0;
  531. nframes_t frames_per_beat = samples_per_minute / bpm;
  532. if ( ! _tempomap.size() )
  533. return pos;
  534. for ( list <const Sequence_Widget *>::const_iterator i = _tempomap.begin();
  535. i != _tempomap.end(); ++i )
  536. {
  537. if ( ! strcmp( (*i)->class_name(), "Tempo_Point" ) )
  538. {
  539. const Tempo_Point *p = (Tempo_Point*)(*i);
  540. bpm = p->tempo();
  541. frames_per_beat = samples_per_minute / bpm;
  542. }
  543. else
  544. {
  545. const Time_Point *p = (Time_Point*)(*i);
  546. sig = p->time();
  547. /* Time point resets beat */
  548. bbt.beat = 0;
  549. }
  550. {
  551. list <const Sequence_Widget *>::const_iterator n = i;
  552. ++n;
  553. if ( n == _tempomap.end() )
  554. next = end;
  555. else
  556. // next = min( (*n)->start(), end );
  557. /* points may not always be aligned with beat boundaries, so we must align here */
  558. next = (*n)->start() - ( ( (*n)->start() - (*i)->start() ) % frames_per_beat );
  559. }
  560. for ( ; f < next; ++bbt.beat, f += frames_per_beat )
  561. {
  562. if ( bbt.beat == sig.beats_per_bar )
  563. {
  564. bbt.beat = 0;
  565. ++bbt.bar;
  566. }
  567. if ( f >= start )
  568. {
  569. /* in the zone */
  570. if ( cb )
  571. cb( f, bbt, arg );
  572. }
  573. /* ugliness to avoid failing out at -1 */
  574. if ( end >= frames_per_beat )
  575. {
  576. if ( f >= end - frames_per_beat )
  577. goto done;
  578. }
  579. else if ( f + frames_per_beat >= end )
  580. goto done;
  581. }
  582. }
  583. done:
  584. pos.frame = f;
  585. pos.tempo = bpm;
  586. pos.beats_per_bar = sig.beats_per_bar;
  587. pos.beat_type = sig.beat_type;
  588. assert( f <= end );
  589. assert( end - f <= frames_per_beat );
  590. /* FIXME: this this right? */
  591. const double frames_per_tick = frames_per_beat / ticks_per_beat;
  592. bbt.tick = ( end - f ) / frames_per_tick;
  593. return pos;
  594. }
  595. /** maybe draw appropriate measure lines in rectangle defined by X, Y, W, and H, using color /color/ as a base */
  596. void
  597. Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
  598. {
  599. if ( ! draw_with_measure_lines )
  600. return;
  601. Fl_Color colors[2];
  602. colors[1] = fl_color_average( FL_BLACK, color, 0.65f );
  603. colors[0] = fl_color_average( FL_RED, colors[1], 0.65f );
  604. const nframes_t start = x_to_offset( X );
  605. const nframes_t length = x_to_ts( W );
  606. fl_push_clip( X, Y, W, H );
  607. render_tempomap( start, length, draw_measure_cb, &colors );
  608. fl_pop_clip();
  609. }
  610. void
  611. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  612. {
  613. Timeline *tl = (Timeline *)v;
  614. fl_push_clip( X, Y, W, H );
  615. /* fl_color( rand() ); */
  616. /* fl_rectf( X, Y, X + W, Y + H ); */
  617. tl->draw_box();
  618. tl->draw_child( *tl->rulers );
  619. fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), tl->tracks->w(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  620. tl->draw_child( *tl->tracks );
  621. fl_pop_clip();
  622. fl_pop_clip();
  623. }
  624. /** handle resize event */
  625. void
  626. Timeline::resize ( int X, int Y, int W, int H )
  627. {
  628. Fl_Overlay_Window::resize( X, Y, W, H );
  629. /* why is this necessary? */
  630. rulers->resize( Track::width(), 0, W - Track::width() - vscroll->w(), rulers->h() );
  631. /* why is THIS necessary? */
  632. hscroll->resize( 0, H - 18, hscroll->w(), 18 );
  633. vscroll->size( vscroll->w(), H - 18 );
  634. }
  635. /** draw ancillary cursors (not necessarily in the overlay plane) */
  636. void
  637. Timeline::draw_cursors ( void ) const
  638. {
  639. if ( p1 != p2 )
  640. {
  641. draw_cursor( p1, FL_BLUE, draw_full_arrow_symbol );
  642. draw_cursor( p2, FL_GREEN, draw_full_arrow_symbol );
  643. }
  644. }
  645. void
  646. Timeline::draw ( void )
  647. {
  648. int X, Y, W, H;
  649. int bdx = 0;
  650. int bdw = 0;
  651. X = tracks->x() + bdx + 1;
  652. Y = tracks->y();
  653. W = tracks->w() - bdw - 1;
  654. H = tracks->h();
  655. adjust_vscroll();
  656. if ( ( damage() & FL_DAMAGE_ALL ) || ( damage() & FL_DAMAGE_EXPOSE ) )
  657. {
  658. DMESSAGE( "complete redraw" );
  659. draw_box( box(), 0, 0, w(), h(), color() );
  660. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  661. draw_child( *rulers );
  662. fl_pop_clip();
  663. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  664. draw_child( *tracks );
  665. fl_pop_clip();
  666. draw_child( *hscroll );
  667. draw_child( *vscroll );
  668. draw_cursors();
  669. redraw_overlay();
  670. goto done;
  671. }
  672. if ( damage() & FL_DAMAGE_SCROLL )
  673. {
  674. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  675. int dy = _old_yposition - _yposition;
  676. /* draw_child( *rulers ); */
  677. if ( ! dy )
  678. fl_scroll( rulers->x(), rulers->y(), rulers->w(), rulers->h(), dx, 0, draw_clip, this );
  679. Y = rulers->y() + rulers->h();
  680. H = h() - rulers->h() - hscroll->h();
  681. if ( dy == 0 )
  682. fl_scroll( X + Track::width(), Y, W - Track::width(), H, dx, dy, draw_clip, this );
  683. else
  684. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  685. }
  686. if ( damage() & FL_DAMAGE_CHILD )
  687. {
  688. fl_push_clip( rulers->x(), rulers->y(), rulers->w(), rulers->h() );
  689. update_child( *rulers );
  690. fl_pop_clip();
  691. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - rulers->h() - hscroll->h() );
  692. update_child( *tracks );
  693. fl_pop_clip();
  694. update_child( *hscroll );
  695. update_child( *vscroll );
  696. draw_cursors();
  697. }
  698. done:
  699. _old_xposition = xoffset;
  700. _old_yposition = _yposition;
  701. }
  702. /** draw a single cursor line at /frame/ with color /color/ using symbol routine /symbol/ for the cap */
  703. void
  704. Timeline::draw_cursor ( nframes_t frame, Fl_Color color, void (*symbol)(Fl_Color) ) const
  705. {
  706. // int x = ( ts_to_x( frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track::width();
  707. if ( frame < xoffset )
  708. return;
  709. const int x = ts_to_x( frame - xoffset ) + tracks->x() + Track::width();
  710. if ( x > tracks->x() + tracks->w() )
  711. return;
  712. fl_color( color );
  713. const int y = rulers->y() + rulers->h();
  714. const int h = this->h() - hscroll->h() - 1;
  715. fl_push_clip( tracks->x() + Track::width(), y, tracks->w(), h );
  716. fl_line( x, y, x, h );
  717. fl_color( fl_darker( color ) );
  718. fl_line( x - 1, y, x - 1, h );
  719. fl_color( FL_BLACK );
  720. fl_line( x + 1, y, x + 1, h );
  721. fl_push_matrix();
  722. fl_translate( x, y );
  723. fl_scale( 16, 8 );
  724. symbol( color );
  725. fl_pop_matrix();
  726. fl_pop_clip();
  727. }
  728. void
  729. Timeline::draw_playhead ( void )
  730. {
  731. draw_cursor( transport->frame, FL_RED, draw_full_arrow_symbol );
  732. // draw_cursor( length(), FL_BLACK, draw_full_arrow_symbol );
  733. }
  734. void
  735. Timeline::redraw_playhead ( void )
  736. {
  737. static nframes_t last_playhead = -1;
  738. if ( last_playhead != transport->frame )
  739. {
  740. redraw_overlay();
  741. last_playhead = transport->frame;
  742. if ( follow_playhead )
  743. {
  744. if ( center_playhead && active() )
  745. xposition( max( 0, ts_to_x( transport->frame ) - ( ( tracks->w() - Track::width() ) >> 1 ) ) );
  746. else if ( ts_to_x( transport->frame ) > ts_to_x( xoffset ) + ( tracks->w() - Track::width() ) )
  747. xposition( ts_to_x( transport->frame ) );
  748. }
  749. }
  750. }
  751. /** called so many times a second to redraw the playhead etc. */
  752. void
  753. Timeline::update_cb ( void *arg )
  754. {
  755. Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
  756. Timeline *tl = (Timeline *)arg;
  757. tl->redraw_playhead();
  758. }
  759. /** draw cursors in overlay plane */
  760. void
  761. Timeline::draw_overlay ( void )
  762. {
  763. draw_playhead();
  764. if ( ! ( _selection.w && _selection.h ) )
  765. return;
  766. fl_push_clip( tracks->x() + Track::width(), rulers->y() + rulers->h(), tracks->w() - Track::width(), h() - rulers->h() - hscroll->h() );
  767. const Rectangle &r = _selection;
  768. fl_color( FL_BLACK );
  769. fl_line_style( FL_SOLID, 2 );
  770. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  771. fl_color( FL_MAGENTA );
  772. fl_line_style( FL_DASH, 2 );
  773. fl_rect( r.x, r.y, r.w, r.h );
  774. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  775. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  776. fl_line_style( FL_SOLID, 0 );
  777. fl_pop_clip();
  778. }
  779. /** select sequence widgets within rectangle /r/ */
  780. void
  781. Timeline::select ( const Rectangle &r )
  782. {
  783. const int Y = r.y;
  784. for ( int i = tracks->children(); i-- ; )
  785. {
  786. Track *t = (Track*)tracks->child( i );
  787. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  788. t->select( r.x, r.y, r.w, r.h, true, true );
  789. }
  790. }
  791. /** delete all selected sequence widgets */
  792. void
  793. Timeline::delete_selected ( void )
  794. {
  795. Sequence_Widget::delete_selected();
  796. }
  797. /** clear the selection of seqeunce widgets */
  798. void
  799. Timeline::select_none ( void )
  800. {
  801. Sequence_Widget::select_none();
  802. }
  803. /** An unfortunate necessity for implementing our own DND aside from
  804. * the (bogus) native FLTK system */
  805. Track *
  806. Timeline::track_under ( int Y )
  807. {
  808. for ( int i = tracks->children(); i-- ; )
  809. {
  810. Track *t = (Track*)tracks->child( i );
  811. if ( ! ( t->y() > Y || t->y() + t->h() < Y ) )
  812. return t;
  813. }
  814. return NULL;
  815. }
  816. #include "FL/event_name.H"
  817. #include "FL/test_press.H"
  818. /** a bit of a hack to keep FLTK's focus navigation stuff from
  819. * stealing the arrow keys from us */
  820. int
  821. Timeline::handle_scroll ( int m )
  822. {
  823. if ( m == FL_KEYBOARD &&
  824. Fl::event_key() != FL_Home &&
  825. Fl::event_key() != FL_End )
  826. return menu->test_shortcut() || hscroll->handle( m ) || vscroll->handle( m );
  827. else
  828. return 0;
  829. }
  830. int
  831. Timeline::handle ( int m )
  832. {
  833. static Drag *drag = NULL;
  834. static bool range = false;
  835. /* if ( m != FL_NO_EVENT ) */
  836. /* DMESSAGE( "%s", event_name( m ) ); */
  837. /* int r = Fl_Overlay_Window::handle( m ); */
  838. switch ( m )
  839. {
  840. case FL_FOCUS:
  841. case FL_UNFOCUS:
  842. // redraw();
  843. return 1;
  844. case FL_ENTER:
  845. return 1;
  846. case FL_LEAVE:
  847. return 1;
  848. case FL_KEYDOWN:
  849. if ( Fl::event_key() == 'r' )
  850. {
  851. range = true;
  852. return 1;
  853. }
  854. return 0;
  855. case FL_KEYUP:
  856. if ( Fl::event_key() == 'r' )
  857. {
  858. range = false;
  859. return 1;
  860. }
  861. return 0;
  862. // case FL_KEYBOARD:
  863. case FL_SHORTCUT:
  864. {
  865. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  866. /* we don't want any keys with modifiers... */
  867. return 0;
  868. switch ( Fl::event_key() )
  869. {
  870. case FL_Delete:
  871. case FL_Home:
  872. case FL_End:
  873. /* keep scrollbar from eating these. */
  874. return 0;
  875. default:
  876. return Fl_Overlay_Window::handle( m );
  877. }
  878. return 0;
  879. }
  880. default:
  881. {
  882. int r = Fl_Overlay_Window::handle( m );
  883. if ( m != FL_RELEASE && r )
  884. return r;
  885. const int X = Fl::event_x();
  886. const int Y = Fl::event_y();
  887. switch ( m )
  888. {
  889. case FL_PUSH:
  890. {
  891. if ( test_press( FL_BUTTON1 ) || test_press( FL_BUTTON1 + FL_CTRL ) )
  892. {
  893. assert( ! drag );
  894. drag = new Drag( X, Y );
  895. _selection.x = X;
  896. _selection.y = Y;
  897. if ( ! Fl::event_ctrl() )
  898. select_none();
  899. return 1;
  900. }
  901. else if ( test_press( FL_BUTTON3 ) )
  902. {
  903. menu_popup( menu );
  904. return 1;
  905. }
  906. return 0;
  907. }
  908. case FL_DRAG:
  909. {
  910. int ox = X - drag->x;
  911. int oy = Y - drag->y;
  912. if ( ox < 0 )
  913. _selection.x = X;
  914. if ( oy < 0 )
  915. _selection.y = Y;
  916. _selection.w = abs( ox );
  917. _selection.h = abs( oy );
  918. if ( range )
  919. {
  920. p1 = x_to_offset( _selection.x );
  921. p2 = x_to_offset( _selection.x + _selection.w );
  922. redraw();
  923. }
  924. redraw_overlay();
  925. return 1;
  926. break;
  927. }
  928. case FL_RELEASE:
  929. {
  930. delete drag;
  931. drag = NULL;
  932. if ( range )
  933. {
  934. p1 = x_to_offset( _selection.x );
  935. p2 = x_to_offset( _selection.x + _selection.w );
  936. redraw();
  937. }
  938. else
  939. select( _selection );
  940. _selection.x = _selection.y =_selection.w = _selection.h = 0;
  941. redraw_overlay();
  942. return 1;
  943. }
  944. default:
  945. return 0;
  946. break;
  947. }
  948. return 0;
  949. }
  950. }
  951. }
  952. /** retrun a pointer to the track named /name/, or NULL if no track is named /name/ */
  953. Track *
  954. Timeline::track_by_name ( const char *name )
  955. {
  956. for ( int i = tracks->children(); i-- ; )
  957. {
  958. Track *t = (Track*)tracks->child( i );
  959. if ( ! strcmp( name, t->name() ) )
  960. return t;
  961. }
  962. return NULL;
  963. }
  964. /** return a malloc'd string representing a unique name for a new track */
  965. char *
  966. Timeline::get_unique_track_name ( const char *name )
  967. {
  968. char pat[256];
  969. strcpy( pat, name );
  970. for ( int i = 1; track_by_name( pat ); ++i )
  971. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  972. return strdup( pat );
  973. }
  974. /**********/
  975. /* Public */
  976. /**********/
  977. /** return the current length of the timeline, which is arrived at by
  978. * calculating the end frame of the rightmost audio region on an
  979. * active audio sequence. Control_Points, etc. do not factor into this
  980. * calcaulation. */
  981. nframes_t
  982. Timeline::length ( void ) const
  983. {
  984. nframes_t l = 0;
  985. for ( int i = tracks->children(); i--; )
  986. l = max( l, ((Track*)tracks->child( i ))->sequence()->length() );
  987. // adjust_hscroll();
  988. return l;
  989. }
  990. /** set horizontal scroll postion to absolute pixel coordinate /X/ */
  991. void
  992. Timeline::xposition ( int X )
  993. {
  994. xoffset = x_to_ts( X );
  995. damage( FL_DAMAGE_SCROLL );
  996. }
  997. /** set vertical scroll position to absolute pixel coordinate /Y/ */
  998. void
  999. Timeline::yposition ( int Y )
  1000. {
  1001. _yposition = Y;
  1002. damage( FL_DAMAGE_SCROLL );
  1003. }
  1004. /** zoom in by one zoom step */
  1005. void
  1006. Timeline::zoom_in ( void )
  1007. {
  1008. hscroll->zoom_in();
  1009. }
  1010. /** zoom out by one zoom step */
  1011. void
  1012. Timeline::zoom_out ( void )
  1013. {
  1014. hscroll->zoom_out();
  1015. }
  1016. /** zoom the display to show /secs/ seconds per screen */
  1017. void
  1018. Timeline::zoom ( float secs )
  1019. {
  1020. const int sw = tracks->w() - Track::width();
  1021. int fpp = (int)((secs * sample_rate()) / sw);
  1022. int p = 0;
  1023. while ( 1 << p < fpp ) p++;
  1024. hscroll->zoom( p );
  1025. redraw();
  1026. }
  1027. /** fit the zoom to the current length of the timeline (subject to nearest power of two) */
  1028. void
  1029. Timeline::zoom_fit ( void )
  1030. {
  1031. xposition( 0 );
  1032. if ( length() )
  1033. zoom( length() / (float)sample_rate() );
  1034. else
  1035. zoom( 60 );
  1036. }
  1037. /** add /track/ to the timeline */
  1038. void
  1039. Timeline::add_track ( Track *track )
  1040. {
  1041. DMESSAGE( "added new track to the timeline" );
  1042. engine->lock();
  1043. tracks->add( track );
  1044. engine->unlock();
  1045. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  1046. redraw();
  1047. }
  1048. /** remove /track/ from the timeline */
  1049. void
  1050. Timeline::remove_track ( Track *track )
  1051. {
  1052. DMESSAGE( "removed track from the timeline" );
  1053. engine->lock();
  1054. /* FIXME: what to do about track contents? */
  1055. tracks->remove( track );
  1056. engine->unlock();
  1057. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  1058. redraw();
  1059. }