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.

1185 lines
27KB

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