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.

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