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.

407 lines
10KB

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