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.

1408 lines
33KB

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