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.

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