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.

310 lines
7.8KB

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