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.

542 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->add( o );
  126. t->add( new Control_Track( 0, 0, 1, 100 ) );
  127. t->show_all_takes();
  128. t->color( (Fl_Color)rand() );
  129. }
  130. {
  131. Track_Header *t = new Track_Header( 0, 0, W, 30 );
  132. Track *o = new Control_Track( 0, 0, 1, 100 );
  133. o->color( FL_BLUE );
  134. t->color( FL_RED );
  135. t->track( o );
  136. }
  137. tracks = o;
  138. o->end();
  139. }
  140. /* scroll = o; */
  141. /* o->end(); */
  142. }
  143. /* make sure scrollbars are on top */
  144. add( vscroll );
  145. add( hscroll );
  146. vscroll->range( 0, tracks->h() );
  147. redraw();
  148. end();
  149. }
  150. float
  151. Timeline::beats_per_minute ( nframes_t when ) const
  152. {
  153. return tempo_track->beats_per_minute( when );
  154. }
  155. int
  156. Timeline::beats_per_bar ( nframes_t when ) const
  157. {
  158. time_sig t = time_track->time( when );
  159. return t.beats_per_bar;
  160. }
  161. void
  162. Timeline::beats_per_minute ( nframes_t when, float bpm )
  163. {
  164. tempo_track->add( new Tempo_Point( when, bpm ) );
  165. }
  166. /** return the absolute pixel of the nearest measure line to /x/ */
  167. int
  168. Timeline::nearest_line ( int ix )
  169. {
  170. for ( int x = ix - 10; x < ix + 10; ++x )
  171. {
  172. const int measure = ts_to_x( (double)(_sample_rate * 60) / beats_per_minute( x_to_ts( x - Track_Header::width() ) + xoffset ));
  173. // const int abs_x = ts_to_x( xoffset ) + x;
  174. if ( 0 == x % measure )
  175. return x;
  176. }
  177. return -1;
  178. }
  179. /** draw appropriate measure lines inside the given bounding box */
  180. /* FIXME: this function *really* needs to be optimized. Currently it
  181. searched both the time and tempo lists once for every horiontal
  182. pixel and performs a number of calculations--this is slow. */
  183. void
  184. Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
  185. {
  186. if ( ! _enable_measure_lines )
  187. return;
  188. // fl_line_style( FL_DASH, 2 );
  189. fl_line_style( FL_DASH, 0 );
  190. Fl_Color beat = fl_color_average( FL_BLACK, color, 0.65f );
  191. Fl_Color bar = fl_color_average( FL_RED, color, 0.65f );
  192. int measure;
  193. for ( int x = X; x < X + W; ++x )
  194. {
  195. measure = ts_to_x( (double)(_sample_rate * 60) / beats_per_minute( x_to_ts( x - Track_Header::width() ) + xoffset ));
  196. const int abs_x = ts_to_x( xoffset ) + x - Track_Header::width();
  197. if ( 0 == abs_x % measure )
  198. {
  199. int bpb = beats_per_bar( x_to_ts( x -Track_Header::width() ) + xoffset );
  200. if ( 0 == (abs_x / measure) % bpb )
  201. {
  202. if ( measure * bpb < 8 )
  203. break;
  204. fl_color( bar );
  205. }
  206. else
  207. {
  208. if ( measure < 8 )
  209. continue;
  210. fl_color( beat );
  211. }
  212. fl_line( x, Y, x, Y + H );
  213. }
  214. }
  215. fl_line_style( FL_SOLID, 0 );
  216. }
  217. void
  218. Timeline::xposition ( int X )
  219. {
  220. // _old_xposition = xoffset;
  221. xoffset = x_to_ts( X );
  222. damage( FL_DAMAGE_SCROLL );
  223. }
  224. void
  225. Timeline::yposition ( int Y )
  226. {
  227. // _old_yposition = _yposition;
  228. _yposition = Y;
  229. damage( FL_DAMAGE_SCROLL );
  230. }
  231. void
  232. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  233. {
  234. Timeline *tl = (Timeline *)v;
  235. // printf( "draw_clip: %d,%d %dx%d\n", X, Y, W, H );
  236. fl_push_clip( X, Y, W, H );
  237. fl_color( rand() );
  238. fl_rectf( X, Y, X + W, Y + H );
  239. tl->draw_child( *tl->rulers );
  240. /* headers */
  241. fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), Track_Header::width(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  242. tl->draw_child( *tl->tracks );
  243. fl_pop_clip();
  244. /* track bodies */
  245. 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() );
  246. tl->draw_child( *tl->tracks );
  247. fl_pop_clip();
  248. // tl->draw_child( *tl->tracks );
  249. fl_pop_clip();
  250. }
  251. void
  252. Timeline::draw ( void )
  253. {
  254. int X, Y, W, H;
  255. X = tracks->x() + Fl::box_dx( tracks->child( 0 )->box() ) + 1;
  256. Y = tracks->y();
  257. W = tracks->w() - Fl::box_dw( tracks->child( 0 )->box() ) - 1;
  258. H = tracks->h();
  259. if ( (damage() & FL_DAMAGE_ALL)
  260. ||
  261. damage() & FL_DAMAGE_EXPOSE )
  262. {
  263. draw_box( box(), 0, 0, w(), h(), color() );
  264. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  265. draw_child( *rulers );
  266. fl_pop_clip();
  267. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  268. draw_child( *tracks );
  269. fl_pop_clip();
  270. draw_child( *hscroll );
  271. draw_child( *vscroll );
  272. redraw_overlay();
  273. return;
  274. }
  275. if ( damage() & FL_DAMAGE_CHILD )
  276. {
  277. // draw_box( box(), 0, 0, w(), h(), color() );
  278. fl_push_clip( rulers->x(), rulers->y(), rulers->w() - vscroll->w(), rulers->h() );
  279. update_child( *rulers );
  280. fl_pop_clip();
  281. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  282. update_child( *tracks );
  283. fl_pop_clip();
  284. update_child( *hscroll );
  285. update_child( *vscroll );
  286. }
  287. if ( damage() & FL_DAMAGE_SCROLL )
  288. {
  289. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  290. int dy = _old_yposition - _yposition;
  291. if ( ! dy )
  292. fl_scroll( X + Track_Header::width(), rulers->y(), rulers->w() - Fl::box_dw( rulers->child(0)->box() ), rulers->h(), dx, 0, draw_clip, this );
  293. Y = rulers->y() + rulers->h();
  294. H = h() - rulers->h() - hscroll->h();
  295. if ( dy == 0 )
  296. fl_scroll( X + Track_Header::width(), Y, W - Track_Header::width(), H, dx, dy, draw_clip, this );
  297. else
  298. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  299. _old_xposition = xoffset;
  300. _old_yposition = _yposition;
  301. }
  302. }
  303. void
  304. Timeline::draw_overlay ( void )
  305. {
  306. fl_push_clip( tracks->x() + Track_Header::width(), rulers->y() + rulers->h(), tracks->w() - Track_Header::width(), h() - rulers->h() - hscroll->h() );
  307. const Rectangle &r = _selection;
  308. fl_color( FL_BLACK );
  309. fl_line_style( FL_SOLID, 2 );
  310. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  311. fl_color( FL_MAGENTA );
  312. fl_line_style( FL_DASH, 2 );
  313. fl_rect( r.x, r.y, r.w, r.h );
  314. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  315. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  316. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  317. fl_line_style( FL_SOLID, 0 );
  318. fl_pop_clip();
  319. }
  320. // #include "Track_Widget.H"
  321. /** select all widgets in inside rectangle /r/ */
  322. void
  323. Timeline::select( const Rectangle &r )
  324. {
  325. const int Y = r.y;
  326. for ( int i = tracks->children(); i-- ; )
  327. {
  328. Track_Header *t = (Track_Header*)tracks->child( i );
  329. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  330. t->track()->select_range( r.x, r.w );
  331. }
  332. }
  333. int
  334. Timeline::handle ( int m )
  335. {
  336. static Drag *drag = NULL;
  337. switch ( m )
  338. {
  339. case FL_KEYBOARD:
  340. {
  341. switch ( Fl::event_key() )
  342. {
  343. case FL_Delete:
  344. {
  345. Track_Widget::delete_selected();
  346. return 1;
  347. }
  348. }
  349. return 0;
  350. }
  351. default:
  352. {
  353. int r = Fl_Overlay_Window::handle( m );
  354. if ( m != FL_RELEASE && r )
  355. return r;
  356. const int X = Fl::event_x();
  357. const int Y = Fl::event_y();
  358. switch ( m )
  359. {
  360. case FL_PUSH:
  361. {
  362. if ( ! Fl::event_button1() )
  363. return 0;
  364. assert( ! drag );
  365. drag = new Drag( X - x(), Y - y() );
  366. _selection.x = drag->x;
  367. _selection.y = drag->y;
  368. break;
  369. }
  370. case FL_DRAG:
  371. {
  372. int ox = X - drag->x;
  373. int oy = Y - drag->y;
  374. if ( ox < 0 )
  375. _selection.x = X;
  376. if ( oy < 0 )
  377. _selection.y = Y;
  378. _selection.w = abs( ox );
  379. _selection.h = abs( oy );
  380. break;
  381. }
  382. case FL_RELEASE:
  383. {
  384. delete drag;
  385. drag = NULL;
  386. select( _selection );
  387. _selection.w = _selection.h = 0;
  388. break;
  389. }
  390. default:
  391. return 0;
  392. break;
  393. }
  394. redraw_overlay();
  395. return 1;
  396. }
  397. }
  398. }