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.

418 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();
  76. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  77. (*r)->draw();
  78. /* draw crossfades */
  79. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  80. {
  81. Track_Widget *o = overlaps( *r );
  82. if ( o )
  83. {
  84. if ( *o <= **r )
  85. {
  86. /* if ( o->x() == (*r)->x() && o->w() == (*r)->w() ) */
  87. /* printf( "complete superposition\n" ); */
  88. if ( (*r)->x() >= o->x() && (*r)->x() + (*r)->w() <= o->x() + o->w() )
  89. /* completely inside */
  90. continue;
  91. ++xfades;
  92. Rectangle b( (*r)->x(),
  93. o->y(),
  94. (o->x() + o->w()) - (*r)->x(),
  95. o->h() );
  96. Fl_Color c = fl_color_average( o->box_color(), (*r)->box_color(), 0.50f );
  97. c = fl_color_average( c, FL_YELLOW, 0.30f );
  98. fl_push_clip( b.x, b.y, b.w, b.h );
  99. draw_box( FL_FLAT_BOX, b.x - 100, b.y, b.w + 200, b.h, c );
  100. draw_box( FL_UP_FRAME, b.x - 100, b.y, b.w + 200, b.h, c );
  101. fl_pop_clip();
  102. }
  103. }
  104. }
  105. // printf( "There are %d xfades\n", xfades );
  106. for ( list <Track_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  107. {
  108. Track_Widget *o = overlaps( *r );
  109. if ( o )
  110. {
  111. if ( *o <= **r )
  112. {
  113. if ( (*r)->x() >= o->x() && (*r)->x() + (*r)->w() <= o->x() + o->w() )
  114. /* completely inside */
  115. continue;
  116. Rectangle b( (*r)->x(), o->y(), (o->x() + o->w()) - (*r)->x(), o->h() );
  117. /* draw overlapping waveforms in X-ray style. */
  118. Waveform::fill = false;
  119. /* Fl_Color oc = o->color(); */
  120. /* Fl_Color rc = (*r)->color(); */
  121. /* /\* give each region a different color *\/ */
  122. /* o->color( FL_RED ); */
  123. /* (*r)->color( FL_GREEN ); */
  124. fl_push_clip( b.x, b.y, b.w, b.h );
  125. o->draw();
  126. (*r)->draw();
  127. fl_pop_clip();
  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_FOCUS:
  244. return 1;
  245. case FL_UNFOCUS:
  246. return 1;
  247. case FL_DND_ENTER:
  248. printf( "enter\n" );
  249. if ( Track_Widget::pushed() && Track_Widget::pushed()->track()->class_name() == class_name() )
  250. {
  251. add( Track_Widget::pushed() );
  252. redraw();
  253. }
  254. case FL_DND_LEAVE:
  255. return 1;
  256. case FL_MOVE:
  257. {
  258. Track_Widget *r = event_widget();
  259. if ( r != Track_Widget::belowmouse() )
  260. {
  261. if ( Track_Widget::belowmouse() )
  262. Track_Widget::belowmouse()->handle( FL_LEAVE );
  263. Track_Widget::belowmouse( r );
  264. if ( r )
  265. r->handle( FL_ENTER );
  266. }
  267. return 0;
  268. }
  269. default:
  270. {
  271. Track_Widget *r = Track_Widget::pushed() ? Track_Widget::pushed() : event_widget();
  272. if ( r )
  273. {
  274. int retval = r->dispatch( m );
  275. if ( retval && m == FL_PUSH )
  276. {
  277. take_focus();
  278. Track_Widget::pushed( r );
  279. }
  280. if ( retval && m == FL_RELEASE )
  281. Track_Widget::pushed( NULL );
  282. Loggable::block_start();
  283. while ( _delete_queue.size() )
  284. {
  285. Track_Widget *t = _delete_queue.front();
  286. _delete_queue.pop();
  287. if ( Track_Widget::pushed() == t )
  288. Track_Widget::pushed( NULL );
  289. if ( Track_Widget::belowmouse() == t )
  290. {
  291. Track_Widget::belowmouse()->handle( FL_LEAVE );
  292. Track_Widget::belowmouse( NULL );
  293. }
  294. delete t;
  295. }
  296. Loggable::block_end();
  297. return retval;
  298. }
  299. else
  300. return Fl_Widget::handle( m );
  301. }
  302. }
  303. }