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.

1394 lines
32KB

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