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.

600 lines
15KB

  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 "Timeline.H"
  19. #include "Tempo_Track.H"
  20. #include "Time_Track.H"
  21. #include "Audio_Track.H"
  22. #include "Control_Track.H"
  23. #include <FL/Fl_Scrollbar.H>
  24. #include <FL/Fl_Image.H>
  25. #include <FL/Fl_RGB_Image.H> // needed for alpha blending
  26. #include "Track_Header.H"
  27. void
  28. Timeline::cb_scroll ( Fl_Widget *w, void *v )
  29. {
  30. ((Timeline*)v)->cb_scroll( w );
  31. }
  32. void
  33. Timeline::cb_scroll ( Fl_Widget *w )
  34. {
  35. if ( w == vscroll )
  36. {
  37. tracks->position( tracks->x(), (rulers->y() + rulers->h()) - vscroll->value() );
  38. yposition( vscroll->value() );
  39. int rh = h() - rulers->h();
  40. vscroll->value( vscroll->value(), 30, 0, max( tracks->h() - rh, rh) );
  41. }
  42. else
  43. {
  44. if ( hscroll->zoom_changed() )
  45. {
  46. _fpp = hscroll->zoom() * 1;
  47. int maxx = ts_to_x( _length );
  48. hscroll->range( 0, maxx );
  49. redraw();
  50. }
  51. else
  52. {
  53. xposition( hscroll->value() );
  54. }
  55. }
  56. }
  57. Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Window( X, Y, W, H, L )
  58. {
  59. box( FL_FLAT_BOX );
  60. xoffset = 0;
  61. _enable_measure_lines = true;
  62. X = Y = 0;
  63. {
  64. Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
  65. o->range( 0, 48000 * 300 );
  66. o->zoom_range( 2, 8192 );
  67. o->zoom( 256 );
  68. o->type( FL_HORIZONTAL );
  69. o->callback( cb_scroll, this );
  70. hscroll = o;
  71. }
  72. {
  73. Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
  74. o->type( FL_VERTICAL );
  75. // o->step( 10 );
  76. o->callback( cb_scroll, this );
  77. vscroll = o;
  78. }
  79. {
  80. Fl_Pack *o = new Fl_Pack( X + Track_Header::width(), Y, (W - Track_Header::width()) - vscroll->w(), H - hscroll->h(), "rulers" );
  81. o->type( Fl_Pack::VERTICAL );
  82. {
  83. Tempo_Track *o = new Tempo_Track( 0, 0, 800, 24 );
  84. o->color( FL_RED );
  85. o->add( new Tempo_Point( 0, 120 ) );
  86. o->add( new Tempo_Point( 56000, 250 ) );
  87. o->label( "Tempo" );
  88. o->align( FL_ALIGN_LEFT );
  89. tempo_track = o;
  90. // o->end();
  91. }
  92. {
  93. Time_Track *o = new Time_Track( 0, 24, 800, 24 );
  94. o->color( fl_color_average( FL_RED, FL_WHITE, 0.50f ) );
  95. o->add( new Time_Point( 0, 4, 4 ) );
  96. o->add( new Time_Point( 345344, 6, 8 ) );
  97. o->label( "Time" );
  98. o->align( FL_ALIGN_LEFT );
  99. time_track = o;
  100. // o->end();
  101. }
  102. o->size( o->w(), o->child( 0 )->h() * o->children() );
  103. rulers = o;
  104. o->end();
  105. }
  106. {
  107. /* Fl_Scroll *o = new Fl_Scroll( 0, 24 * 2, 800, 600 - (24 * 3) ); */
  108. /* o->type( Fl_Scroll::VERTICAL_ALWAYS ); */
  109. _sample_rate = 44100;
  110. _fpp = 256;
  111. _length = _sample_rate * 60 * 2;
  112. {
  113. Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 5000 );
  114. o->type( Fl_Pack::VERTICAL );
  115. o->spacing( 0 );
  116. for ( int i = 8; i--; )
  117. {
  118. // Track_Header *t = new Track_Header( 0, 0, W, 75 );
  119. Track_Header *t = new Track_Header( 0, 0, W, 30 );
  120. Track *o = new Audio_Track( 0, 0, 1, 100 );
  121. t->track( o );
  122. t->add( new Audio_Track( 0, 0, 1, 100 ) );
  123. t->add( new Audio_Track( 0, 0, 1, 100 ) );
  124. t->add_control( new Control_Track( 0, 0, 1, 100 ) );
  125. t->color( (Fl_Color)rand() );
  126. }
  127. tracks = o;
  128. o->end();
  129. }
  130. /* scroll = o; */
  131. /* o->end(); */
  132. }
  133. /* make sure scrollbars are on top */
  134. add( vscroll );
  135. add( hscroll );
  136. vscroll->range( 0, tracks->h() );
  137. redraw();
  138. end();
  139. }
  140. float
  141. Timeline::beats_per_minute ( nframes_t when ) const
  142. {
  143. return tempo_track->beats_per_minute( when );
  144. }
  145. int
  146. Timeline::beats_per_bar ( nframes_t when ) const
  147. {
  148. time_sig t = time_track->time( when );
  149. return t.beats_per_bar;
  150. }
  151. void
  152. Timeline::beats_per_minute ( nframes_t when, float bpm )
  153. {
  154. tempo_track->add( new Tempo_Point( when, bpm ) );
  155. }
  156. /** return the absolute pixel of the nearest measure line to /x/ */
  157. int
  158. Timeline::nearest_line ( int ix )
  159. {
  160. for ( int x = ix - 10; x < ix + 10; ++x )
  161. {
  162. const int measure = ts_to_x( (double)(_sample_rate * 60) / beats_per_minute( x_to_ts( x - Track_Header::width() ) + xoffset ));
  163. // const int abs_x = ts_to_x( xoffset ) + x;
  164. if ( 0 == x % measure )
  165. return x;
  166. }
  167. return -1;
  168. }
  169. /** draw appropriate measure lines inside the given bounding box */
  170. /* FIXME: this function *really* needs to be optimized. Currently it
  171. searched both the time and tempo lists once for every horiontal
  172. pixel and performs a number of calculations--this is slow. */
  173. void
  174. Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
  175. {
  176. if ( ! _enable_measure_lines )
  177. return;
  178. // fl_line_style( FL_DASH, 2 );
  179. fl_line_style( FL_DASH, 0 );
  180. Fl_Color beat = fl_color_average( FL_BLACK, color, 0.65f );
  181. Fl_Color bar = fl_color_average( FL_RED, color, 0.65f );
  182. int measure;
  183. for ( int x = X; x < X + W; ++x )
  184. {
  185. measure = ts_to_x( (double)(_sample_rate * 60) / beats_per_minute( x_to_ts( x - Track_Header::width() ) + xoffset ));
  186. const int abs_x = ts_to_x( xoffset ) + x - Track_Header::width();
  187. if ( 0 == abs_x % measure )
  188. {
  189. int bpb = beats_per_bar( x_to_ts( x -Track_Header::width() ) + xoffset );
  190. if ( 0 == (abs_x / measure) % bpb )
  191. {
  192. if ( measure * bpb < 8 )
  193. break;
  194. fl_color( bar );
  195. }
  196. else
  197. {
  198. if ( measure < 8 )
  199. continue;
  200. fl_color( beat );
  201. }
  202. fl_line( x, Y, x, Y + H );
  203. }
  204. }
  205. fl_line_style( FL_SOLID, 0 );
  206. }
  207. void
  208. Timeline::xposition ( int X )
  209. {
  210. // _old_xposition = xoffset;
  211. xoffset = x_to_ts( X );
  212. damage( FL_DAMAGE_SCROLL );
  213. }
  214. void
  215. Timeline::yposition ( int Y )
  216. {
  217. // _old_yposition = _yposition;
  218. _yposition = Y;
  219. damage( FL_DAMAGE_SCROLL );
  220. }
  221. void
  222. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  223. {
  224. Timeline *tl = (Timeline *)v;
  225. // printf( "draw_clip: %d,%d %dx%d\n", X, Y, W, H );
  226. fl_push_clip( X, Y, W, H );
  227. fl_color( rand() );
  228. fl_rectf( X, Y, X + W, Y + H );
  229. tl->draw_child( *tl->rulers );
  230. /* headers */
  231. fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), Track_Header::width(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  232. tl->draw_child( *tl->tracks );
  233. fl_pop_clip();
  234. /* track bodies */
  235. fl_push_clip( tl->tracks->x() + Track_Header::width(), tl->rulers->y() + tl->rulers->h(), tl->tracks->w() - Track_Header::width(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  236. tl->draw_child( *tl->tracks );
  237. fl_pop_clip();
  238. // tl->draw_child( *tl->tracks );
  239. fl_pop_clip();
  240. }
  241. static unsigned char *rect_image;
  242. void
  243. Timeline::draw ( void )
  244. {
  245. int X, Y, W, H;
  246. X = tracks->x() + Fl::box_dx( tracks->child( 0 )->box() ) + 1;
  247. Y = tracks->y();
  248. W = tracks->w() - Fl::box_dw( tracks->child( 0 )->box() ) - 1;
  249. H = tracks->h();
  250. /* if ( damage() & FL_DAMAGE_USER1 ) */
  251. /* { */
  252. /* /\* save the rectangle so we can draw it (darkened) in the overlay *\/ */
  253. /* Rectangle &r = _selection; */
  254. /* make_current(); */
  255. /* rect_image = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  256. /* return; */
  257. /* } */
  258. if ( (damage() & FL_DAMAGE_ALL)
  259. ||
  260. damage() & FL_DAMAGE_EXPOSE )
  261. {
  262. draw_box( box(), 0, 0, w(), h(), color() );
  263. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  264. draw_child( *rulers );
  265. fl_pop_clip();
  266. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  267. draw_child( *tracks );
  268. fl_pop_clip();
  269. draw_child( *hscroll );
  270. draw_child( *vscroll );
  271. redraw_overlay();
  272. /* Rectangle &r = _selection; */
  273. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  274. /* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
  275. /* bi.color_average( FL_BLACK, 0.50f ); */
  276. /* bi.draw( r.x, r.y ); */
  277. /* delete[] data; */
  278. /* if ( r.w && r.h ) */
  279. /* { */
  280. /* const unsigned char data[] = { 0, 127, 0, 96, */
  281. /* 0, 96, 0, 127 }; */
  282. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  283. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  284. /* bi2->draw( r.x, r.y ); */
  285. /* delete bi2; */
  286. /* } */
  287. return;
  288. }
  289. if ( damage() & FL_DAMAGE_CHILD )
  290. {
  291. // draw_box( box(), 0, 0, w(), h(), color() );
  292. fl_push_clip( rulers->x(), rulers->y(), rulers->w() - vscroll->w(), rulers->h() );
  293. update_child( *rulers );
  294. fl_pop_clip();
  295. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  296. update_child( *tracks );
  297. fl_pop_clip();
  298. update_child( *hscroll );
  299. update_child( *vscroll );
  300. }
  301. if ( damage() & FL_DAMAGE_SCROLL )
  302. {
  303. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  304. int dy = _old_yposition - _yposition;
  305. if ( ! dy )
  306. fl_scroll( X + Track_Header::width(), rulers->y(), rulers->w() - Fl::box_dw( rulers->child(0)->box() ), rulers->h(), dx, 0, draw_clip, this );
  307. Y = rulers->y() + rulers->h();
  308. H = h() - rulers->h() - hscroll->h();
  309. if ( dy == 0 )
  310. fl_scroll( X + Track_Header::width(), Y, W - Track_Header::width(), H, dx, dy, draw_clip, this );
  311. else
  312. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  313. _old_xposition = xoffset;
  314. _old_yposition = _yposition;
  315. }
  316. }
  317. void
  318. Timeline::draw_overlay ( void )
  319. {
  320. if ( ! ( _selection.w && _selection.h ) )
  321. return;
  322. fl_push_clip( tracks->x() + Track_Header::width(), rulers->y() + rulers->h(), tracks->w() - Track_Header::width(), h() - rulers->h() - hscroll->h() );
  323. const Rectangle &r = _selection;
  324. fl_color( FL_BLACK );
  325. fl_line_style( FL_SOLID, 2 );
  326. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  327. fl_color( FL_MAGENTA );
  328. fl_line_style( FL_DASH, 2 );
  329. fl_rect( r.x, r.y, r.w, r.h );
  330. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  331. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  332. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  333. fl_line_style( FL_SOLID, 0 );
  334. /* const unsigned char data[] = { 127, 127, 127, 96, */
  335. /* 127, 96, 127, 40 }; */
  336. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  337. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  338. /* bi2->draw( r.x, r.y ); */
  339. /* delete bi2; */
  340. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  341. /* Fl_RGB_Image bi( rect_image, r.w, r.h, 3 ); */
  342. /* bi.color_average( FL_BLACK, 0.50f ); */
  343. /* bi.draw( r.x, r.y ); */
  344. /* delete[] rect_image; */
  345. /* rect_image = NULL; */
  346. fl_pop_clip();
  347. }
  348. // #include "Track_Widget.H"
  349. /** select all widgets in inside rectangle /r/ */
  350. void
  351. Timeline::select( const Rectangle &r )
  352. {
  353. const int Y = r.y;
  354. for ( int i = tracks->children(); i-- ; )
  355. {
  356. Track_Header *t = (Track_Header*)tracks->child( i );
  357. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  358. t->track()->select_range( r.x, r.w );
  359. }
  360. }
  361. int
  362. Timeline::handle ( int m )
  363. {
  364. static Drag *drag = NULL;
  365. switch ( m )
  366. {
  367. case FL_KEYBOARD:
  368. {
  369. switch ( Fl::event_key() )
  370. {
  371. case FL_Delete:
  372. {
  373. Track_Widget::delete_selected();
  374. return 1;
  375. }
  376. }
  377. return 0;
  378. }
  379. default:
  380. {
  381. int r = Fl_Overlay_Window::handle( m );
  382. if ( m != FL_RELEASE && r )
  383. return r;
  384. const int X = Fl::event_x();
  385. const int Y = Fl::event_y();
  386. switch ( m )
  387. {
  388. case FL_PUSH:
  389. {
  390. if ( ! Fl::event_button1() )
  391. return 0;
  392. assert( ! drag );
  393. drag = new Drag( X - x(), Y - y() );
  394. _selection.x = drag->x;
  395. _selection.y = drag->y;
  396. break;
  397. }
  398. case FL_DRAG:
  399. {
  400. int ox = X - drag->x;
  401. int oy = Y - drag->y;
  402. if ( ox < 0 )
  403. _selection.x = X;
  404. if ( oy < 0 )
  405. _selection.y = Y;
  406. _selection.w = abs( ox );
  407. _selection.h = abs( oy );
  408. break;
  409. }
  410. case FL_RELEASE:
  411. {
  412. delete drag;
  413. drag = NULL;
  414. select( _selection );
  415. _selection.w = _selection.h = 0;
  416. break;
  417. }
  418. default:
  419. return 0;
  420. break;
  421. }
  422. redraw_overlay();
  423. return 1;
  424. }
  425. }
  426. }