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.

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