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.

1475 lines
35KB

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