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.

1238 lines
28KB

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