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.

358 lines
9.0KB

  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 "Track.H"
  19. #include "Timeline.H"
  20. #include "Region.H"
  21. #include <FL/fl_draw.H>
  22. queue <Track_Widget *> Track::_delete_queue;
  23. Track_Widget *Track::_pushed = NULL;
  24. Track_Widget *Track::_belowmouse = NULL;
  25. Track::Track ( int X, int Y, int W, int H ) : Fl_Widget( X, Y, W, H )
  26. {
  27. _name = NULL;
  28. box( FL_DOWN_BOX );
  29. color( fl_darker( FL_GRAY ) );
  30. align( FL_ALIGN_LEFT );
  31. log_create();
  32. }
  33. Track::~Track ( )
  34. {
  35. /* FIXME: what to do with regions? */
  36. parent()->redraw();
  37. parent()->remove( this );
  38. log_destroy();
  39. }
  40. void
  41. Track::sort ( void )
  42. {
  43. _widgets.sort( Track_Widget::sort_func );
  44. }
  45. /** return a pointer to the widget that /r/ overlaps, or NULL if none. */
  46. Track_Widget *
  47. Track::overlaps ( Track_Widget *r )
  48. {
  49. for ( list <Track_Widget *>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  50. {
  51. if ( *i == r ) continue;
  52. if ( ! ( (*i)->offset() > r->offset() + r->length() || (*i)->offset() + (*i)->length() < r->offset() ) )
  53. return *i;
  54. }
  55. return NULL;
  56. }
  57. #include "Waveform.H"
  58. void
  59. Track::draw ( void )
  60. {
  61. if ( ! fl_not_clipped( x(), y(), w(), h() ) )
  62. return;
  63. fl_push_clip( x(), y(), w(), h() );
  64. draw_box();
  65. int X, Y, W, H;
  66. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  67. // printf( "track::draw %d,%d %dx%d\n", X,Y,W,H );
  68. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  69. (*r)->draw_box( X, Y, W, H );
  70. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  71. (*r)->draw( X, Y, W, H );
  72. /* draw crossfades */
  73. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  74. {
  75. Track_Widget *o = overlaps( *r );
  76. if ( o )
  77. {
  78. if ( *o < **r )
  79. {
  80. Rectangle b( (*r)->x(), o->y(), (o->x() + o->w()) - (*r)->x(), o->h() );
  81. Fl_Color c = fl_color_average( o->box_color(), (*r)->box_color(), 0.50f );
  82. c = fl_color_average( c, FL_YELLOW, 0.30f );
  83. fl_push_clip( b.x, b.y, b.w, b.h );
  84. draw_box( FL_FLAT_BOX, b.x - 100, b.y, b.w + 200, b.h, c );
  85. draw_box( FL_UP_FRAME, b.x - 100, b.y, b.w + 200, b.h, c );
  86. /* draw overlapping waveforms in X-ray style. */
  87. Waveform::fill = false;
  88. /* Fl_Color oc = o->color(); */
  89. /* Fl_Color rc = (*r)->color(); */
  90. /* /\* give each region a different color *\/ */
  91. /* o->color( FL_RED ); */
  92. /* (*r)->color( FL_GREEN ); */
  93. o->draw( b.x, b.y, b.w, b.h );
  94. (*r)->draw( b.x, b.y, b.w, b.h );
  95. Waveform::fill = true;
  96. /* o->color( oc ); */
  97. /* (*r)->color( rc ); */
  98. /* fl_color( FL_BLACK ); */
  99. /* fl_line_style( FL_DOT, 4 ); */
  100. /* b.x = (*r)->line_x(); */
  101. /* b.w = min( 32767, (*r)->abs_w() ); */
  102. /* fl_line( b.x, b.y, b.x + b.w, b.y + b.h ); */
  103. /* fl_line( b.x, b.y + b.h, b.x + b.w, b.y ); */
  104. /* fl_line_style( FL_SOLID, 0 ); */
  105. fl_pop_clip();
  106. }
  107. }
  108. }
  109. timeline->draw_measure_lines( x(), y(), w(), h(), color() );
  110. fl_pop_clip();
  111. }
  112. void
  113. Track::remove ( Track_Widget *r )
  114. {
  115. // Logger _log( this );
  116. _widgets.remove( r );
  117. }
  118. void
  119. Track::remove_selected ( void )
  120. {
  121. Loggable::block_start();
  122. for ( list <Track_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
  123. if ( (*r)->selected() )
  124. {
  125. Track_Widget *t = *r;
  126. _widgets.erase( r++ );
  127. delete t;
  128. }
  129. else
  130. ++r;
  131. Loggable::block_end();
  132. }
  133. Track_Widget *
  134. Track::event_widget ( void )
  135. {
  136. nframes_t ets = timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() );
  137. for ( list <Track_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  138. if ( ets > (*r)->offset() && ets < (*r)->offset() + (*r)->length() )
  139. return (*r);
  140. return NULL;
  141. }
  142. void
  143. Track::select_range ( int X, int W )
  144. {
  145. nframes_t sts = timeline->xoffset + timeline->x_to_ts( X - x() );
  146. nframes_t ets = sts + timeline->x_to_ts( W );
  147. for ( list <Track_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  148. if ( ! ( (*r)->offset() > ets || (*r)->offset() + (*r)->length() < sts ) )
  149. (*r)->select();
  150. }
  151. void
  152. Track::add ( Track_Widget *r )
  153. {
  154. // Logger _log( this );
  155. if ( r->track() )
  156. {
  157. r->redraw();
  158. r->track()->remove( r );
  159. // r->track()->redraw();
  160. }
  161. r->track( this );
  162. _widgets.push_back( r );
  163. sort();
  164. }
  165. /* snap /r/ to nearest edge */
  166. void
  167. Track::snap ( Track_Widget *r )
  168. {
  169. const int snap_pixels = 10;
  170. const int rx1 = r->x();
  171. const int rx2 = r->x() + r->w();
  172. for ( list <Track_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  173. {
  174. const Track_Widget *w = (*i);
  175. if ( w == r )
  176. continue;
  177. const int wx1 = w->x();
  178. const int wx2 = w->x() + w->w();
  179. if ( abs( rx1 - wx2 ) < snap_pixels )
  180. {
  181. r->offset( w->offset() + w->length() + 1 );
  182. // printf( "snap: %lu | %lu\n", w->offset() + w->length(), r->offset() );
  183. goto done;
  184. }
  185. if ( abs( rx2 - wx1 ) < snap_pixels )
  186. {
  187. r->offset( ( w->offset() - r->length() ) - 1 );
  188. // printf( "snap: %lu | %lu\n", r->offset() + r->length(), w->offset() );
  189. goto done;
  190. }
  191. }
  192. {
  193. int nx = timeline->nearest_line( r->abs_x() );
  194. if ( nx >= 0 )
  195. {
  196. r->offset( timeline->x_to_ts( nx ) );
  197. return;
  198. }
  199. }
  200. // r->offset( timeline->x_to_ts( r->x() ) );
  201. done:
  202. return;
  203. // r->resize();
  204. // r->position( rx1, y() );
  205. }
  206. int
  207. Track::handle ( int m )
  208. {
  209. switch ( m )
  210. {
  211. case FL_DND_ENTER:
  212. printf( "enter\n" );
  213. if ( pushed() && pushed()->track()->class_name() == class_name() )
  214. {
  215. add( pushed() );
  216. redraw();
  217. }
  218. case FL_DND_LEAVE:
  219. return 1;
  220. case FL_MOVE:
  221. {
  222. Track_Widget *r = event_widget();
  223. if ( r != belowmouse() )
  224. {
  225. if ( belowmouse() )
  226. belowmouse()->handle( FL_LEAVE );
  227. _belowmouse = r;
  228. if ( r )
  229. r->handle( FL_ENTER );
  230. }
  231. return 0;
  232. }
  233. default:
  234. {
  235. Track_Widget *r = pushed() ? pushed() : event_widget();
  236. if ( r )
  237. {
  238. int retval = r->dispatch( m );
  239. if ( retval && m == FL_PUSH )
  240. _pushed = r;
  241. if ( retval && m == FL_RELEASE )
  242. _pushed = NULL;
  243. Loggable::block_start();
  244. while ( _delete_queue.size() )
  245. {
  246. Track_Widget *t = _delete_queue.front();
  247. _delete_queue.pop();
  248. if ( pushed() == t )
  249. _pushed = NULL;
  250. if ( belowmouse() == t )
  251. {
  252. belowmouse()->handle( FL_LEAVE );
  253. _belowmouse = NULL;
  254. }
  255. delete t;
  256. }
  257. Loggable::block_end();
  258. return retval;
  259. }
  260. else
  261. return Fl_Widget::handle( m );
  262. }
  263. }
  264. }