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.

1378 lines
32KB

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