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.

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