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.

316 lines
8.1KB

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