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.

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