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.

1259 lines
29KB

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