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.

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