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.

1424 lines
34KB

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