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.

311 lines
7.9KB

  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. void
  24. Track::sort ( void )
  25. {
  26. _widgets.sort( Track_Widget::sort_func );
  27. }
  28. /** return a pointer to the widget that /r/ overlaps, or NULL if none. */
  29. Track_Widget *
  30. Track::overlaps ( Track_Widget *r )
  31. {
  32. for ( list <Track_Widget *>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  33. {
  34. if ( *i == r ) continue;
  35. if ( ! ( (*i)->offset() > r->offset() + r->length() || (*i)->offset() + (*i)->length() < r->offset() ) )
  36. return *i;
  37. }
  38. return NULL;
  39. }
  40. void
  41. Track::draw ( void )
  42. {
  43. if ( ! fl_not_clipped( x(), y(), w(), h() ) )
  44. return;
  45. fl_push_clip( x(), y(), w(), h() );
  46. Fl_Group::draw();
  47. timeline->draw_measure_lines( x(), y(), w(), h(), color() );
  48. int X, Y, W, H;
  49. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  50. // printf( "track::draw %d,%d %dx%d\n", X,Y,W,H );
  51. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  52. (*r)->draw_box( X, Y, W, H );
  53. /* draw crossfades */
  54. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  55. {
  56. Track_Widget *o = overlaps( *r );
  57. if ( o )
  58. {
  59. if ( *o < **r )
  60. {
  61. Rectangle b( (*r)->x(), o->y(), (o->x() + o->w()) - (*r)->x(), o->h() );
  62. Fl_Color c = fl_color_average( o->box_color(), (*r)->box_color(), 0.50f );
  63. c = fl_color_average( c, FL_YELLOW, 0.20f );
  64. fl_push_clip( b.x, b.y, b.w, b.h );
  65. draw_box( FL_FLAT_BOX, b.x - 100, b.y, b.w + 200, b.h, c );
  66. draw_box( FL_UP_FRAME, b.x - 100, b.y, b.w + 200, b.h, c );
  67. /* fl_color( FL_BLACK ); */
  68. /* fl_line_style( FL_DOT, 4 ); */
  69. /* fl_line( b.x, b.y, b.x + b.w, b.y + b.h ); */
  70. /* fl_line( b.x, b.y + b.h, b.x + b.w, b.y ); */
  71. /* fl_line_style( FL_SOLID, 0 ); */
  72. fl_pop_clip();
  73. }
  74. }
  75. }
  76. /* TODO: detect overlap and draw with transparency/crossfade */
  77. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  78. (*r)->draw( X, Y, W, H );
  79. fl_pop_clip();
  80. }
  81. void
  82. Track::remove ( Track_Widget *r )
  83. {
  84. // Logger _log( this );
  85. _widgets.remove( r );
  86. }
  87. void
  88. Track::remove_selected ( void )
  89. {
  90. Loggable::block_start();
  91. for ( list <Track_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
  92. if ( (*r)->selected() )
  93. {
  94. Track_Widget *t = *r;
  95. _widgets.erase( r++ );
  96. delete t;
  97. }
  98. else
  99. ++r;
  100. Loggable::block_end();
  101. }
  102. Track_Widget *
  103. Track::event_widget ( void )
  104. {
  105. nframes_t ets = timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() );
  106. for ( list <Track_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  107. if ( ets > (*r)->offset() && ets < (*r)->offset() + (*r)->length() )
  108. return (*r);
  109. return NULL;
  110. }
  111. void
  112. Track::select_range ( int X, int W )
  113. {
  114. nframes_t sts = timeline->xoffset + timeline->x_to_ts( X - x() );
  115. nframes_t ets = sts + timeline->x_to_ts( W );
  116. for ( list <Track_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  117. if ( ! ( (*r)->offset() > ets || (*r)->offset() + (*r)->length() < sts ) )
  118. (*r)->select();
  119. }
  120. void
  121. Track::add ( Track_Widget *r )
  122. {
  123. // Logger _log( this );
  124. if ( r->track() )
  125. {
  126. r->redraw();
  127. r->track()->remove( r );
  128. // r->track()->redraw();
  129. }
  130. r->track( this );
  131. _widgets.push_back( r );
  132. sort();
  133. }
  134. /* snap /r/ to nearest edge */
  135. void
  136. Track::snap ( Track_Widget *r )
  137. {
  138. const int snap_pixels = 10;
  139. const int rx1 = r->x();
  140. const int rx2 = r->x() + r->w();
  141. for ( list <Track_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  142. {
  143. const Track_Widget *w = (*i);
  144. if ( w == r )
  145. continue;
  146. const int wx1 = w->x();
  147. const int wx2 = w->x() + w->w();
  148. if ( abs( rx1 - wx2 ) < snap_pixels )
  149. {
  150. r->offset( w->offset() + w->length() + 1 );
  151. // printf( "snap: %lu | %lu\n", w->offset() + w->length(), r->offset() );
  152. goto done;
  153. }
  154. if ( abs( rx2 - wx1 ) < snap_pixels )
  155. {
  156. r->offset( ( w->offset() - r->length() ) - 1 );
  157. // printf( "snap: %lu | %lu\n", r->offset() + r->length(), w->offset() );
  158. goto done;
  159. }
  160. }
  161. {
  162. int nx = timeline->nearest_line( r->abs_x() );
  163. if ( nx >= 0 )
  164. {
  165. r->offset( timeline->x_to_ts( nx ) );
  166. return;
  167. }
  168. }
  169. // r->offset( timeline->x_to_ts( r->x() ) );
  170. done:
  171. return;
  172. // r->resize();
  173. // r->position( rx1, y() );
  174. }
  175. int
  176. Track::handle ( int m )
  177. {
  178. static Track_Widget *pushed;
  179. static Track_Widget *belowmouse;
  180. switch ( m )
  181. {
  182. case FL_ENTER:
  183. case FL_LEAVE:
  184. return 1;
  185. case FL_MOVE:
  186. {
  187. /* these aren't used, so don't bother doing lookups for them */
  188. Track_Widget *r = event_widget();
  189. if ( r != belowmouse )
  190. {
  191. if ( belowmouse )
  192. belowmouse->handle( FL_LEAVE );
  193. belowmouse = r;
  194. if ( r )
  195. r->handle( FL_ENTER );
  196. }
  197. return 1;
  198. }
  199. default:
  200. {
  201. Track_Widget *r = pushed ? pushed : event_widget();
  202. if ( r )
  203. {
  204. int retval = r->dispatch( m );
  205. if ( retval && m == FL_PUSH )
  206. pushed = r;
  207. if ( retval && m == FL_RELEASE )
  208. pushed = NULL;
  209. Loggable::block_start();
  210. while ( _delete_queue.size() )
  211. {
  212. Track_Widget *t = _delete_queue.front();
  213. _delete_queue.pop();
  214. if ( pushed == t )
  215. pushed = NULL;
  216. if ( belowmouse == t )
  217. {
  218. belowmouse->handle( FL_LEAVE );
  219. belowmouse = NULL;
  220. }
  221. delete t;
  222. }
  223. Loggable::block_end();
  224. return retval;
  225. }
  226. else
  227. return Fl_Group::handle( m );
  228. }
  229. }
  230. }