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.

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