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.

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