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.

582 lines
14KB

  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. void
  242. Timeline::draw ( void )
  243. {
  244. int X, Y, W, H;
  245. X = tracks->x() + Fl::box_dx( tracks->child( 0 )->box() ) + 1;
  246. Y = tracks->y();
  247. W = tracks->w() - Fl::box_dw( tracks->child( 0 )->box() ) - 1;
  248. H = tracks->h();
  249. if ( (damage() & FL_DAMAGE_ALL)
  250. ||
  251. damage() & FL_DAMAGE_EXPOSE )
  252. {
  253. draw_box( box(), 0, 0, w(), h(), color() );
  254. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  255. draw_child( *rulers );
  256. fl_pop_clip();
  257. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  258. draw_child( *tracks );
  259. fl_pop_clip();
  260. draw_child( *hscroll );
  261. draw_child( *vscroll );
  262. redraw_overlay();
  263. Rectangle &r = _selection;
  264. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  265. /* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
  266. /* bi.color_average( FL_BLACK, 0.50f ); */
  267. /* bi.draw( r.x, r.y ); */
  268. /* delete[] data; */
  269. if ( r.w && r.h )
  270. {
  271. const unsigned char data[] = { 0, 127, 0, 96,
  272. 0, 96, 0, 127 };
  273. Fl_RGB_Image bi( data, 2, 2, 2 );
  274. Fl_Image *bi2 = bi.copy( r.w, r.h );
  275. bi2->draw( r.x, r.y );
  276. delete bi2;
  277. }
  278. return;
  279. }
  280. if ( damage() & FL_DAMAGE_CHILD )
  281. {
  282. // draw_box( box(), 0, 0, w(), h(), color() );
  283. fl_push_clip( rulers->x(), rulers->y(), rulers->w() - vscroll->w(), rulers->h() );
  284. update_child( *rulers );
  285. fl_pop_clip();
  286. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  287. update_child( *tracks );
  288. fl_pop_clip();
  289. update_child( *hscroll );
  290. update_child( *vscroll );
  291. }
  292. if ( damage() & FL_DAMAGE_SCROLL )
  293. {
  294. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  295. int dy = _old_yposition - _yposition;
  296. if ( ! dy )
  297. fl_scroll( X + Track_Header::width(), rulers->y(), rulers->w() - Fl::box_dw( rulers->child(0)->box() ), rulers->h(), dx, 0, draw_clip, this );
  298. Y = rulers->y() + rulers->h();
  299. H = h() - rulers->h() - hscroll->h();
  300. if ( dy == 0 )
  301. fl_scroll( X + Track_Header::width(), Y, W - Track_Header::width(), H, dx, dy, draw_clip, this );
  302. else
  303. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  304. _old_xposition = xoffset;
  305. _old_yposition = _yposition;
  306. }
  307. }
  308. void
  309. Timeline::draw_overlay ( void )
  310. {
  311. if ( ! ( _selection.w && _selection.h ) )
  312. return;
  313. fl_push_clip( tracks->x() + Track_Header::width(), rulers->y() + rulers->h(), tracks->w() - Track_Header::width(), h() - rulers->h() - hscroll->h() );
  314. const Rectangle &r = _selection;
  315. fl_color( FL_BLACK );
  316. fl_line_style( FL_SOLID, 2 );
  317. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  318. fl_color( FL_MAGENTA );
  319. fl_line_style( FL_DASH, 2 );
  320. fl_rect( r.x, r.y, r.w, r.h );
  321. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  322. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  323. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  324. fl_line_style( FL_SOLID, 0 );
  325. /* const unsigned char data[] = { 127, 127, 127, 96, */
  326. /* 127, 96, 127, 40 }; */
  327. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  328. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  329. /* bi2->draw( r.x, r.y ); */
  330. /* delete bi2; */
  331. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  332. /* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
  333. /* bi.color_average( FL_BLACK, 0.50f ); */
  334. /* bi.draw( r.x, r.y ); */
  335. /* delete[] data; */
  336. fl_pop_clip();
  337. }
  338. // #include "Track_Widget.H"
  339. /** select all widgets in inside rectangle /r/ */
  340. void
  341. Timeline::select( const Rectangle &r )
  342. {
  343. const int Y = r.y;
  344. for ( int i = tracks->children(); i-- ; )
  345. {
  346. Track_Header *t = (Track_Header*)tracks->child( i );
  347. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  348. t->track()->select_range( r.x, r.w );
  349. }
  350. }
  351. int
  352. Timeline::handle ( int m )
  353. {
  354. static Drag *drag = NULL;
  355. switch ( m )
  356. {
  357. case FL_KEYBOARD:
  358. {
  359. switch ( Fl::event_key() )
  360. {
  361. case FL_Delete:
  362. {
  363. Track_Widget::delete_selected();
  364. return 1;
  365. }
  366. }
  367. return 0;
  368. }
  369. default:
  370. {
  371. int r = Fl_Overlay_Window::handle( m );
  372. if ( m != FL_RELEASE && r )
  373. return r;
  374. const int X = Fl::event_x();
  375. const int Y = Fl::event_y();
  376. switch ( m )
  377. {
  378. case FL_PUSH:
  379. {
  380. if ( ! Fl::event_button1() )
  381. return 0;
  382. assert( ! drag );
  383. drag = new Drag( X - x(), Y - y() );
  384. _selection.x = drag->x;
  385. _selection.y = drag->y;
  386. break;
  387. }
  388. case FL_DRAG:
  389. {
  390. int ox = X - drag->x;
  391. int oy = Y - drag->y;
  392. if ( ox < 0 )
  393. _selection.x = X;
  394. if ( oy < 0 )
  395. _selection.y = Y;
  396. _selection.w = abs( ox );
  397. _selection.h = abs( oy );
  398. redraw();
  399. break;
  400. }
  401. case FL_RELEASE:
  402. {
  403. delete drag;
  404. drag = NULL;
  405. select( _selection );
  406. _selection.w = _selection.h = 0;
  407. break;
  408. }
  409. default:
  410. return 0;
  411. break;
  412. }
  413. redraw_overlay();
  414. return 1;
  415. }
  416. }
  417. }