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.

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