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.

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