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.

1264 lines
29KB

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