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.

339 lines
8.7KB

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