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.

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