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.

391 lines
10.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. /* if ( o->x() == (*r)->x() && o->w() == (*r)->w() ) */
  81. /* printf( "complete superposition\n" ); */
  82. if ( (*r)->x() >= o->x() && (*r)->x() + (*r)->w() <= o->x() + o->w() )
  83. /* completely inside */
  84. continue;
  85. Rectangle b( (*r)->x(),
  86. o->y(),
  87. (o->x() + o->w()) - (*r)->x(),
  88. o->h() );
  89. Fl_Color c = fl_color_average( o->box_color(), (*r)->box_color(), 0.50f );
  90. c = fl_color_average( c, FL_YELLOW, 0.30f );
  91. fl_push_clip( b.x, b.y, b.w, b.h );
  92. draw_box( FL_FLAT_BOX, b.x - 100, b.y, b.w + 200, b.h, c );
  93. draw_box( FL_UP_FRAME, b.x - 100, b.y, b.w + 200, b.h, c );
  94. fl_pop_clip();
  95. }
  96. }
  97. }
  98. for ( list <Track_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  99. {
  100. Track_Widget *o = overlaps( *r );
  101. if ( o )
  102. {
  103. if ( *o <= **r )
  104. {
  105. if ( (*r)->x() >= o->x() && (*r)->x() + (*r)->w() <= o->x() + o->w() )
  106. /* completely inside */
  107. continue;
  108. Rectangle b( (*r)->x(), o->y(), (o->x() + o->w()) - (*r)->x(), o->h() );
  109. /* draw overlapping waveforms in X-ray style. */
  110. Waveform::fill = false;
  111. /* Fl_Color oc = o->color(); */
  112. /* Fl_Color rc = (*r)->color(); */
  113. /* /\* give each region a different color *\/ */
  114. /* o->color( FL_RED ); */
  115. /* (*r)->color( FL_GREEN ); */
  116. o->draw( b.x, b.y, b.w, b.h );
  117. (*r)->draw( b.x, b.y, b.w, b.h );
  118. Waveform::fill = true;
  119. /* o->color( oc ); */
  120. /* (*r)->color( rc ); */
  121. /* fl_color( FL_BLACK ); */
  122. /* fl_line_style( FL_DOT, 4 ); */
  123. /* b.x = (*r)->line_x(); */
  124. /* b.w = min( 32767, (*r)->abs_w() ); */
  125. /* fl_line( b.x, b.y, b.x + b.w, b.y + b.h ); */
  126. /* fl_line( b.x, b.y + b.h, b.x + b.w, b.y ); */
  127. /* fl_line_style( FL_SOLID, 0 ); */
  128. // fl_pop_clip();
  129. }
  130. }
  131. }
  132. timeline->draw_measure_lines( x(), y(), w(), h(), color() );
  133. fl_pop_clip();
  134. }
  135. void
  136. Track::remove ( Track_Widget *r )
  137. {
  138. // Logger _log( this );
  139. _widgets.remove( r );
  140. }
  141. void
  142. Track::remove_selected ( void )
  143. {
  144. Loggable::block_start();
  145. for ( list <Track_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
  146. if ( (*r)->selected() )
  147. {
  148. Track_Widget *t = *r;
  149. _widgets.erase( r++ );
  150. delete t;
  151. }
  152. else
  153. ++r;
  154. Loggable::block_end();
  155. }
  156. Track_Widget *
  157. Track::event_widget ( void )
  158. {
  159. nframes_t ets = timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() );
  160. for ( list <Track_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  161. if ( ets > (*r)->offset() && ets < (*r)->offset() + (*r)->length() )
  162. return (*r);
  163. return NULL;
  164. }
  165. void
  166. Track::select_range ( int X, int W )
  167. {
  168. nframes_t sts = timeline->xoffset + timeline->x_to_ts( X - x() );
  169. nframes_t ets = sts + timeline->x_to_ts( W );
  170. for ( list <Track_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  171. if ( ! ( (*r)->offset() > ets || (*r)->offset() + (*r)->length() < sts ) )
  172. (*r)->select();
  173. }
  174. void
  175. Track::add ( Track_Widget *r )
  176. {
  177. // Logger _log( this );
  178. if ( r->track() )
  179. {
  180. r->redraw();
  181. r->track()->remove( r );
  182. // r->track()->redraw();
  183. }
  184. r->track( this );
  185. _widgets.push_back( r );
  186. sort();
  187. }
  188. /* snap /r/ to nearest edge */
  189. void
  190. Track::snap ( Track_Widget *r )
  191. {
  192. const int snap_pixels = 10;
  193. const int rx1 = r->x();
  194. const int rx2 = r->x() + r->w();
  195. for ( list <Track_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  196. {
  197. const Track_Widget *w = (*i);
  198. if ( w == r )
  199. continue;
  200. const int wx1 = w->x();
  201. const int wx2 = w->x() + w->w();
  202. if ( abs( rx1 - wx2 ) < snap_pixels )
  203. {
  204. r->offset( w->offset() + w->length() + 1 );
  205. // printf( "snap: %lu | %lu\n", w->offset() + w->length(), r->offset() );
  206. goto done;
  207. }
  208. if ( abs( rx2 - wx1 ) < snap_pixels )
  209. {
  210. r->offset( ( w->offset() - r->length() ) - 1 );
  211. // printf( "snap: %lu | %lu\n", r->offset() + r->length(), w->offset() );
  212. goto done;
  213. }
  214. }
  215. {
  216. int nx = timeline->nearest_line( r->abs_x() );
  217. if ( nx >= 0 )
  218. {
  219. r->offset( timeline->x_to_ts( nx ) );
  220. return;
  221. }
  222. }
  223. // r->offset( timeline->x_to_ts( r->x() ) );
  224. done:
  225. return;
  226. // r->resize();
  227. // r->position( rx1, y() );
  228. }
  229. int
  230. Track::handle ( int m )
  231. {
  232. switch ( m )
  233. {
  234. case FL_DND_ENTER:
  235. printf( "enter\n" );
  236. if ( pushed() && pushed()->track()->class_name() == class_name() )
  237. {
  238. add( pushed() );
  239. redraw();
  240. }
  241. case FL_DND_LEAVE:
  242. return 1;
  243. case FL_MOVE:
  244. {
  245. Track_Widget *r = event_widget();
  246. if ( r != belowmouse() )
  247. {
  248. if ( belowmouse() )
  249. belowmouse()->handle( FL_LEAVE );
  250. _belowmouse = r;
  251. if ( r )
  252. r->handle( FL_ENTER );
  253. }
  254. return 0;
  255. }
  256. default:
  257. {
  258. Track_Widget *r = pushed() ? pushed() : event_widget();
  259. if ( r )
  260. {
  261. int retval = r->dispatch( m );
  262. if ( retval && m == FL_PUSH )
  263. _pushed = r;
  264. if ( retval && m == FL_RELEASE )
  265. _pushed = NULL;
  266. Loggable::block_start();
  267. while ( _delete_queue.size() )
  268. {
  269. Track_Widget *t = _delete_queue.front();
  270. _delete_queue.pop();
  271. if ( pushed() == t )
  272. _pushed = NULL;
  273. if ( belowmouse() == t )
  274. {
  275. belowmouse()->handle( FL_LEAVE );
  276. _belowmouse = NULL;
  277. }
  278. delete t;
  279. }
  280. Loggable::block_end();
  281. return retval;
  282. }
  283. else
  284. return Fl_Widget::handle( m );
  285. }
  286. }
  287. }