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.

424 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. Waveform::fill = false;
  124. /* Fl_Color oc = o->color(); */
  125. /* Fl_Color rc = (*r)->color(); */
  126. /* /\* give each region a different color *\/ */
  127. /* o->color( FL_RED ); */
  128. /* (*r)->color( FL_GREEN ); */
  129. fl_push_clip( b.x, b.y, b.w, b.h );
  130. o->draw();
  131. (*r)->draw();
  132. fl_pop_clip();
  133. Waveform::fill = true;
  134. /* o->color( oc ); */
  135. /* (*r)->color( rc ); */
  136. /* fl_color( FL_BLACK ); */
  137. /* fl_line_style( FL_DOT, 4 ); */
  138. /* b.x = (*r)->line_x(); */
  139. /* b.w = min( 32767, (*r)->abs_w() ); */
  140. /* fl_line( b.x, b.y, b.x + b.w, b.y + b.h ); */
  141. /* fl_line( b.x, b.y + b.h, b.x + b.w, b.y ); */
  142. /* fl_line_style( FL_SOLID, 0 ); */
  143. // fl_pop_clip();
  144. }
  145. }
  146. }
  147. fl_pop_clip();
  148. }
  149. void
  150. Sequence::remove ( Sequence_Widget *r )
  151. {
  152. // Logger _log( this );
  153. _widgets.remove( r );
  154. }
  155. void
  156. Sequence::remove_selected ( void )
  157. {
  158. Loggable::block_start();
  159. for ( list <Sequence_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
  160. if ( (*r)->selected() )
  161. {
  162. Sequence_Widget *t = *r;
  163. _widgets.erase( r++ );
  164. delete t;
  165. }
  166. else
  167. ++r;
  168. Loggable::block_end();
  169. }
  170. Sequence_Widget *
  171. Sequence::event_widget ( void )
  172. {
  173. nframes_t ets = timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() );
  174. for ( list <Sequence_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  175. if ( ets > (*r)->offset() && ets < (*r)->offset() + (*r)->length() )
  176. return (*r);
  177. return NULL;
  178. }
  179. void
  180. Sequence::select_range ( int X, int W )
  181. {
  182. nframes_t sts = timeline->xoffset + timeline->x_to_ts( X - x() );
  183. nframes_t ets = sts + timeline->x_to_ts( W );
  184. for ( list <Sequence_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  185. if ( ! ( (*r)->offset() > ets || (*r)->offset() + (*r)->length() < sts ) )
  186. (*r)->select();
  187. }
  188. void
  189. Sequence::add ( Sequence_Widget *r )
  190. {
  191. // Logger _log( this );
  192. if ( r->track() )
  193. {
  194. r->redraw();
  195. r->track()->remove( r );
  196. // r->track()->redraw();
  197. }
  198. r->track( this );
  199. _widgets.push_back( r );
  200. sort();
  201. }
  202. /* snap /r/ to nearest edge */
  203. void
  204. Sequence::snap ( Sequence_Widget *r )
  205. {
  206. const int snap_pixels = 10;
  207. const int rx1 = r->x();
  208. const int rx2 = r->x() + r->w();
  209. for ( list <Sequence_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  210. {
  211. const Sequence_Widget *w = (*i);
  212. if ( w == r )
  213. continue;
  214. const int wx1 = w->x();
  215. const int wx2 = w->x() + w->w();
  216. if ( abs( rx1 - wx2 ) < snap_pixels )
  217. {
  218. r->offset( w->offset() + w->length() + 1 );
  219. // printf( "snap: %lu | %lu\n", w->offset() + w->length(), r->offset() );
  220. goto done;
  221. }
  222. if ( abs( rx2 - wx1 ) < snap_pixels )
  223. {
  224. r->offset( ( w->offset() - r->length() ) - 1 );
  225. // printf( "snap: %lu | %lu\n", r->offset() + r->length(), w->offset() );
  226. goto done;
  227. }
  228. }
  229. {
  230. int nx = timeline->nearest_line( r->abs_x() );
  231. if ( nx >= 0 )
  232. {
  233. r->offset( timeline->x_to_ts( nx ) );
  234. return;
  235. }
  236. }
  237. // r->offset( timeline->x_to_ts( r->x() ) );
  238. done:
  239. return;
  240. // r->resize();
  241. // r->position( rx1, y() );
  242. }
  243. int
  244. Sequence::handle ( int m )
  245. {
  246. switch ( m )
  247. {
  248. case FL_FOCUS:
  249. return 1;
  250. case FL_UNFOCUS:
  251. return 1;
  252. case FL_DND_ENTER:
  253. printf( "enter\n" );
  254. if ( Sequence_Widget::pushed() && Sequence_Widget::pushed()->track()->class_name() == class_name() )
  255. {
  256. add( Sequence_Widget::pushed() );
  257. redraw();
  258. }
  259. case FL_DND_LEAVE:
  260. return 1;
  261. case FL_MOVE:
  262. {
  263. Sequence_Widget *r = event_widget();
  264. if ( r != Sequence_Widget::belowmouse() )
  265. {
  266. if ( Sequence_Widget::belowmouse() )
  267. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  268. Sequence_Widget::belowmouse( r );
  269. if ( r )
  270. r->handle( FL_ENTER );
  271. }
  272. return 0;
  273. }
  274. default:
  275. {
  276. Sequence_Widget *r = Sequence_Widget::pushed() ? Sequence_Widget::pushed() : event_widget();
  277. if ( r )
  278. {
  279. int retval = r->dispatch( m );
  280. if ( retval && m == FL_PUSH )
  281. {
  282. take_focus();
  283. Sequence_Widget::pushed( r );
  284. }
  285. if ( retval && m == FL_RELEASE )
  286. Sequence_Widget::pushed( NULL );
  287. Loggable::block_start();
  288. while ( _delete_queue.size() )
  289. {
  290. Sequence_Widget *t = _delete_queue.front();
  291. _delete_queue.pop();
  292. if ( Sequence_Widget::pushed() == t )
  293. Sequence_Widget::pushed( NULL );
  294. if ( Sequence_Widget::belowmouse() == t )
  295. {
  296. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  297. Sequence_Widget::belowmouse( NULL );
  298. }
  299. delete t;
  300. }
  301. Loggable::block_end();
  302. return retval;
  303. }
  304. else
  305. return Fl_Widget::handle( m );
  306. }
  307. }
  308. }