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.

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