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.

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