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.

519 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. damage( FL_DAMAGE_SCROLL );
  38. }
  39. else
  40. {
  41. if ( hscroll->zoom_changed() )
  42. {
  43. _fpp = hscroll->zoom() * 1;
  44. int maxx = ts_to_x( _length );
  45. hscroll->range( 0, maxx );
  46. redraw();
  47. }
  48. else
  49. {
  50. position( hscroll->value() );
  51. }
  52. }
  53. }
  54. Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Window( X, Y, W, H, L )
  55. {
  56. box( FL_FLAT_BOX );
  57. xoffset = 0;
  58. _enable_measure_lines = true;
  59. X = Y = 0;
  60. {
  61. Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
  62. o->range( 0, 48000 * 300 );
  63. o->zoom_range( 2, 8192 );
  64. o->zoom( 256 );
  65. o->type( FL_HORIZONTAL );
  66. o->callback( cb_scroll, this );
  67. hscroll = o;
  68. }
  69. {
  70. Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
  71. o->type( FL_VERTICAL );
  72. // o->step( 10 );
  73. o->callback( cb_scroll, this );
  74. vscroll = o;
  75. }
  76. {
  77. Fl_Pack *o = new Fl_Pack( X + Track_Header::width(), Y, (W - Track_Header::width()) - vscroll->w(), H - hscroll->h(), "rulers" );
  78. o->type( Fl_Pack::VERTICAL );
  79. {
  80. Tempo_Track *o = new Tempo_Track( 0, 0, 800, 24 );
  81. o->color( FL_RED );
  82. o->add( new Tempo_Point( 0, 120 ) );
  83. o->add( new Tempo_Point( 56000, 250 ) );
  84. o->label( "Tempo" );
  85. o->align( FL_ALIGN_LEFT );
  86. tempo_track = o;
  87. o->end();
  88. }
  89. {
  90. Time_Track *o = new Time_Track( 0, 24, 800, 24 );
  91. o->color( fl_color_average( FL_RED, FL_WHITE, 0.50f ) );
  92. o->add( new Time_Point( 0, 4, 4 ) );
  93. o->add( new Time_Point( 345344, 6, 8 ) );
  94. o->label( "Time" );
  95. o->align( FL_ALIGN_LEFT );
  96. time_track = o;
  97. o->end();
  98. }
  99. o->size( o->w(), o->child( 0 )->h() * o->children() );
  100. rulers = o;
  101. o->end();
  102. }
  103. {
  104. /* Fl_Scroll *o = new Fl_Scroll( 0, 24 * 2, 800, 600 - (24 * 3) ); */
  105. /* o->type( Fl_Scroll::VERTICAL_ALWAYS ); */
  106. _sample_rate = 44100;
  107. _fpp = 256;
  108. _length = _sample_rate * 60 * 2;
  109. {
  110. Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 5000 );
  111. o->type( Fl_Pack::VERTICAL );
  112. o->spacing( 0 );
  113. Track *l = NULL;
  114. for ( int i = 16; i--; )
  115. {
  116. // Track_Header *t = new Track_Header( 0, 0, W, 75 );
  117. Track_Header *t = new Track_Header( 0, 0, W, 30 );
  118. Track *o = new Audio_Track( 0, 0, 1, 100 );
  119. o->prev( l );
  120. if ( l )
  121. l->next( o );
  122. l = o;
  123. // o->end();
  124. t->track( o );
  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::position ( int X )
  209. {
  210. _old_xposition = xoffset;
  211. xoffset = x_to_ts( X );
  212. damage( FL_DAMAGE_SCROLL );
  213. }
  214. void
  215. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  216. {
  217. Timeline *tl = (Timeline *)v;
  218. // printf( "draw_clip: %d,%d %dx%d\n", X, Y, W, H );
  219. fl_push_clip( X, Y, W, H );
  220. fl_color( rand() );
  221. fl_rectf( X, Y, X + W, Y + H );
  222. tl->draw_child( *tl->rulers );
  223. /* headers */
  224. fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), Track_Header::width(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  225. tl->draw_child( *tl->tracks );
  226. fl_pop_clip();
  227. /* track bodies */
  228. 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() );
  229. tl->draw_child( *tl->tracks );
  230. fl_pop_clip();
  231. // tl->draw_child( *tl->tracks );
  232. fl_pop_clip();
  233. }
  234. void
  235. Timeline::draw ( void )
  236. {
  237. int X, Y, W, H;
  238. X = tracks->x() + Fl::box_dx( tracks->child( 0 )->box() ) + 1;
  239. Y = tracks->y();
  240. W = tracks->w() - Fl::box_dw( tracks->child( 0 )->box() ) - 1;
  241. H = tracks->h();
  242. if ( (damage() & FL_DAMAGE_ALL)
  243. ||
  244. damage() & FL_DAMAGE_EXPOSE )
  245. {
  246. draw_box( box(), 0, 0, w(), h(), color() );
  247. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  248. draw_child( *rulers );
  249. fl_pop_clip();
  250. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  251. draw_child( *tracks );
  252. fl_pop_clip();
  253. draw_child( *hscroll );
  254. draw_child( *vscroll );
  255. redraw_overlay();
  256. return;
  257. }
  258. if ( damage() & FL_DAMAGE_CHILD )
  259. {
  260. // draw_box( box(), 0, 0, w(), h(), color() );
  261. fl_push_clip( rulers->x(), rulers->y(), rulers->w() - vscroll->w(), rulers->h() );
  262. update_child( *rulers );
  263. fl_pop_clip();
  264. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  265. update_child( *tracks );
  266. fl_pop_clip();
  267. update_child( *hscroll );
  268. update_child( *vscroll );
  269. }
  270. if ( damage() & FL_DAMAGE_SCROLL )
  271. {
  272. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  273. int dy = _old_yposition - yposition;
  274. if ( ! dy )
  275. fl_scroll( X + Track_Header::width(), rulers->y(), rulers->w() - Fl::box_dw( rulers->child(0)->box() ), rulers->h(), dx, 0, draw_clip, this );
  276. Y = rulers->y() + rulers->h();
  277. H = h() - rulers->h() - hscroll->h();
  278. if ( dy == 0 )
  279. fl_scroll( X + Track_Header::width(), Y, W - Track_Header::width(), H, dx, dy, draw_clip, this );
  280. else
  281. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  282. _old_xposition = xoffset;
  283. _old_yposition = yposition;
  284. }
  285. }
  286. void
  287. Timeline::draw_overlay ( void )
  288. {
  289. fl_push_clip( tracks->x() + Track_Header::width(), rulers->y() + rulers->h(), tracks->w() - Track_Header::width(), h() - rulers->h() - hscroll->h() );
  290. const Rectangle &r = _selection;
  291. fl_color( FL_BLACK );
  292. fl_line_style( FL_SOLID, 2 );
  293. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  294. fl_color( FL_MAGENTA );
  295. fl_line_style( FL_DASH, 2 );
  296. fl_rect( r.x, r.y, r.w, r.h );
  297. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  298. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  299. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  300. fl_line_style( FL_SOLID, 0 );
  301. fl_pop_clip();
  302. }
  303. // #include "Track_Widget.H"
  304. /** select all widgets in inside rectangle /r/ */
  305. void
  306. Timeline::select( const Rectangle &r )
  307. {
  308. const int Y = r.y;
  309. for ( int i = tracks->children(); i-- ; )
  310. {
  311. Track_Header *t = (Track_Header*)tracks->child( i );
  312. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  313. t->track()->select_range( r.x, r.w );
  314. }
  315. }
  316. int
  317. Timeline::handle ( int m )
  318. {
  319. static Drag *drag = NULL;
  320. switch ( m )
  321. {
  322. case FL_KEYBOARD:
  323. {
  324. switch ( Fl::event_key() )
  325. {
  326. case FL_Delete:
  327. {
  328. Track_Widget::delete_selected();
  329. return 1;
  330. }
  331. }
  332. return 0;
  333. }
  334. default:
  335. {
  336. int r = Fl_Overlay_Window::handle( m );
  337. if ( m != FL_RELEASE && r )
  338. return r;
  339. const int X = Fl::event_x();
  340. const int Y = Fl::event_y();
  341. switch ( m )
  342. {
  343. case FL_PUSH:
  344. {
  345. if ( ! Fl::event_button1() )
  346. return 0;
  347. assert( ! drag );
  348. drag = new Drag( X - x(), Y - y() );
  349. _selection.x = drag->x;
  350. _selection.y = drag->y;
  351. break;
  352. }
  353. case FL_DRAG:
  354. {
  355. int ox = X - drag->x;
  356. int oy = Y - drag->y;
  357. if ( ox < 0 )
  358. _selection.x = X;
  359. if ( oy < 0 )
  360. _selection.y = Y;
  361. _selection.w = abs( ox );
  362. _selection.h = abs( oy );
  363. break;
  364. }
  365. case FL_RELEASE:
  366. {
  367. delete drag;
  368. drag = NULL;
  369. select( _selection );
  370. _selection.w = _selection.h = 0;
  371. break;
  372. }
  373. default:
  374. return 0;
  375. break;
  376. }
  377. redraw_overlay();
  378. return 1;
  379. }
  380. }
  381. }