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.

1381 lines
31KB

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