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.

419 lines
11KB

  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::Track ( int X, int Y, int W, int H ) : Fl_Widget( X, Y, W, H )
  24. {
  25. _name = NULL;
  26. box( FL_DOWN_BOX );
  27. color( fl_darker( FL_GRAY ) );
  28. align( FL_ALIGN_LEFT );
  29. // log_create();
  30. }
  31. Track::~Track ( )
  32. {
  33. /* FIXME: what to do with regions? */
  34. parent()->redraw();
  35. parent()->remove( this );
  36. // log_destroy();
  37. }
  38. void
  39. Track::sort ( void )
  40. {
  41. _widgets.sort( Track_Widget::sort_func );
  42. }
  43. /** return a pointer to the widget that /r/ overlaps, or NULL if none. */
  44. Track_Widget *
  45. Track::overlaps ( Track_Widget *r )
  46. {
  47. for ( list <Track_Widget *>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  48. {
  49. if ( *i == r ) continue;
  50. if ( ! ( (*i)->offset() > r->offset() + r->length() || (*i)->offset() + (*i)->length() < r->offset() ) )
  51. return *i;
  52. }
  53. return NULL;
  54. }
  55. #include "Waveform.H"
  56. void
  57. Track::draw ( void )
  58. {
  59. if ( ! fl_not_clipped( x(), y(), w(), h() ) )
  60. return;
  61. fl_push_clip( x(), y(), w(), h() );
  62. draw_box();
  63. int X, Y, W, H;
  64. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  65. if ( Track_Widget::pushed() && Track_Widget::pushed()->track() == this )
  66. {
  67. /* make sure the Track_Widget::pushed widget is above all others */
  68. remove( Track_Widget::pushed() );
  69. add( Track_Widget::pushed() );
  70. }
  71. int xfades = 0;
  72. // printf( "track::draw %d,%d %dx%d\n", X,Y,W,H );
  73. timeline->draw_measure_lines( x(), y(), w(), h(), color() );
  74. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  75. (*r)->draw_box( X, Y, W, H );
  76. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  77. (*r)->draw( X, Y, W, H );
  78. /* draw crossfades */
  79. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  80. {
  81. if ( ! (*r)->shown() )
  82. continue;
  83. Track_Widget *o = overlaps( *r );
  84. if ( o && o->shown() )
  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. if ( ! (*r)->shown() )
  111. continue;
  112. Track_Widget *o = overlaps( *r );
  113. if ( o && o->shown() )
  114. {
  115. if ( *o <= **r )
  116. {
  117. if ( (*r)->x() >= o->x() && (*r)->x() + (*r)->w() <= o->x() + o->w() )
  118. /* completely inside */
  119. continue;
  120. Rectangle b( (*r)->x(), o->y(), (o->x() + o->w()) - (*r)->x(), o->h() );
  121. /* draw overlapping waveforms in X-ray style. */
  122. Waveform::fill = false;
  123. /* Fl_Color oc = o->color(); */
  124. /* Fl_Color rc = (*r)->color(); */
  125. /* /\* give each region a different color *\/ */
  126. /* o->color( FL_RED ); */
  127. /* (*r)->color( FL_GREEN ); */
  128. o->draw( b.x, b.y, b.w, b.h );
  129. (*r)->draw( b.x, b.y, b.w, b.h );
  130. Waveform::fill = true;
  131. /* o->color( oc ); */
  132. /* (*r)->color( rc ); */
  133. /* fl_color( FL_BLACK ); */
  134. /* fl_line_style( FL_DOT, 4 ); */
  135. /* b.x = (*r)->line_x(); */
  136. /* b.w = min( 32767, (*r)->abs_w() ); */
  137. /* fl_line( b.x, b.y, b.x + b.w, b.y + b.h ); */
  138. /* fl_line( b.x, b.y + b.h, b.x + b.w, b.y ); */
  139. /* fl_line_style( FL_SOLID, 0 ); */
  140. // fl_pop_clip();
  141. }
  142. }
  143. }
  144. fl_pop_clip();
  145. }
  146. void
  147. Track::remove ( Track_Widget *r )
  148. {
  149. // Logger _log( this );
  150. _widgets.remove( r );
  151. }
  152. void
  153. Track::remove_selected ( void )
  154. {
  155. Loggable::block_start();
  156. for ( list <Track_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
  157. if ( (*r)->selected() )
  158. {
  159. Track_Widget *t = *r;
  160. _widgets.erase( r++ );
  161. delete t;
  162. }
  163. else
  164. ++r;
  165. Loggable::block_end();
  166. }
  167. Track_Widget *
  168. Track::event_widget ( void )
  169. {
  170. nframes_t ets = timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() );
  171. for ( list <Track_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  172. if ( ets > (*r)->offset() && ets < (*r)->offset() + (*r)->length() )
  173. return (*r);
  174. return NULL;
  175. }
  176. void
  177. Track::select_range ( int X, int W )
  178. {
  179. nframes_t sts = timeline->xoffset + timeline->x_to_ts( X - x() );
  180. nframes_t ets = sts + timeline->x_to_ts( W );
  181. for ( list <Track_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  182. if ( ! ( (*r)->offset() > ets || (*r)->offset() + (*r)->length() < sts ) )
  183. (*r)->select();
  184. }
  185. void
  186. Track::add ( Track_Widget *r )
  187. {
  188. // Logger _log( this );
  189. if ( r->track() )
  190. {
  191. r->redraw();
  192. r->track()->remove( r );
  193. // r->track()->redraw();
  194. }
  195. r->track( this );
  196. _widgets.push_back( r );
  197. sort();
  198. }
  199. /* snap /r/ to nearest edge */
  200. void
  201. Track::snap ( Track_Widget *r )
  202. {
  203. const int snap_pixels = 10;
  204. const int rx1 = r->x();
  205. const int rx2 = r->x() + r->w();
  206. for ( list <Track_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  207. {
  208. const Track_Widget *w = (*i);
  209. if ( w == r )
  210. continue;
  211. const int wx1 = w->x();
  212. const int wx2 = w->x() + w->w();
  213. if ( abs( rx1 - wx2 ) < snap_pixels )
  214. {
  215. r->offset( w->offset() + w->length() + 1 );
  216. // printf( "snap: %lu | %lu\n", w->offset() + w->length(), r->offset() );
  217. goto done;
  218. }
  219. if ( abs( rx2 - wx1 ) < snap_pixels )
  220. {
  221. r->offset( ( w->offset() - r->length() ) - 1 );
  222. // printf( "snap: %lu | %lu\n", r->offset() + r->length(), w->offset() );
  223. goto done;
  224. }
  225. }
  226. {
  227. int nx = timeline->nearest_line( r->abs_x() );
  228. if ( nx >= 0 )
  229. {
  230. r->offset( timeline->x_to_ts( nx ) );
  231. return;
  232. }
  233. }
  234. // r->offset( timeline->x_to_ts( r->x() ) );
  235. done:
  236. return;
  237. // r->resize();
  238. // r->position( rx1, y() );
  239. }
  240. int
  241. Track::handle ( int m )
  242. {
  243. switch ( m )
  244. {
  245. case FL_FOCUS:
  246. return 1;
  247. case FL_UNFOCUS:
  248. return 1;
  249. case FL_DND_ENTER:
  250. printf( "enter\n" );
  251. if ( Track_Widget::pushed() && Track_Widget::pushed()->track()->class_name() == class_name() )
  252. {
  253. add( Track_Widget::pushed() );
  254. redraw();
  255. }
  256. case FL_DND_LEAVE:
  257. return 1;
  258. case FL_MOVE:
  259. {
  260. Track_Widget *r = event_widget();
  261. if ( r != Track_Widget::belowmouse() )
  262. {
  263. if ( Track_Widget::belowmouse() )
  264. Track_Widget::belowmouse()->handle( FL_LEAVE );
  265. Track_Widget::belowmouse( r );
  266. if ( r )
  267. r->handle( FL_ENTER );
  268. }
  269. return 0;
  270. }
  271. default:
  272. {
  273. Track_Widget *r = Track_Widget::pushed() ? Track_Widget::pushed() : event_widget();
  274. if ( r )
  275. {
  276. int retval = r->dispatch( m );
  277. if ( retval && m == FL_PUSH )
  278. {
  279. take_focus();
  280. Track_Widget::pushed( r );
  281. }
  282. if ( retval && m == FL_RELEASE )
  283. Track_Widget::pushed( NULL );
  284. Loggable::block_start();
  285. while ( _delete_queue.size() )
  286. {
  287. Track_Widget *t = _delete_queue.front();
  288. _delete_queue.pop();
  289. if ( Track_Widget::pushed() == t )
  290. Track_Widget::pushed( NULL );
  291. if ( Track_Widget::belowmouse() == t )
  292. {
  293. Track_Widget::belowmouse()->handle( FL_LEAVE );
  294. Track_Widget::belowmouse( NULL );
  295. }
  296. delete t;
  297. }
  298. Loggable::block_end();
  299. return retval;
  300. }
  301. else
  302. return Fl_Widget::handle( m );
  303. }
  304. }
  305. }