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.

527 lines
13KB

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