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.

446 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. color( FL_BACKGROUND_COLOR );
  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() ) - 1, y(), w() + Fl::box_dw( box() ) + 2, 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. if ( Timeline::snap_magnetic )
  216. {
  217. const int rx1 = r->x();
  218. const int rx2 = r->x() + r->w();
  219. for ( list <Sequence_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  220. {
  221. const Sequence_Widget *w = (*i);
  222. if ( w == r )
  223. continue;
  224. const int wx1 = w->x();
  225. const int wx2 = w->x() + w->w();
  226. if ( abs( rx1 - wx2 ) < snap_pixels )
  227. {
  228. r->offset( w->offset() + w->length() + 1 );
  229. // printf( "snap: %lu | %lu\n", w->offset() + w->length(), r->offset() );
  230. goto done;
  231. }
  232. if ( abs( rx2 - wx1 ) < snap_pixels )
  233. {
  234. r->offset( ( w->offset() - r->length() ) - 1 );
  235. // printf( "snap: %lu | %lu\n", r->offset() + r->length(), w->offset() );
  236. goto done;
  237. }
  238. }
  239. }
  240. {
  241. int nx = timeline->nearest_line( r->abs_x() );
  242. if ( nx >= 0 )
  243. {
  244. r->offset( timeline->x_to_ts( nx ) );
  245. return;
  246. }
  247. }
  248. // r->offset( timeline->x_to_ts( r->x() ) );
  249. done:
  250. return;
  251. // r->resize();
  252. // r->position( rx1, y() );
  253. }
  254. int
  255. Sequence::handle ( int m )
  256. {
  257. switch ( m )
  258. {
  259. case FL_FOCUS:
  260. return 1;
  261. case FL_UNFOCUS:
  262. return 1;
  263. case FL_ENTER:
  264. case FL_LEAVE:
  265. return 1;
  266. case FL_DND_ENTER:
  267. printf( "enter\n" );
  268. if ( Sequence_Widget::pushed() && Sequence_Widget::pushed()->track()->class_name() == class_name() )
  269. {
  270. add( Sequence_Widget::pushed() );
  271. redraw();
  272. }
  273. case FL_DND_LEAVE:
  274. return 1;
  275. case FL_MOVE:
  276. {
  277. Sequence_Widget *r = event_widget();
  278. if ( r != Sequence_Widget::belowmouse() )
  279. {
  280. if ( Sequence_Widget::belowmouse() )
  281. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  282. Sequence_Widget::belowmouse( r );
  283. if ( r )
  284. r->handle( FL_ENTER );
  285. }
  286. return 0;
  287. }
  288. default:
  289. {
  290. Sequence_Widget *r = Sequence_Widget::pushed() ? Sequence_Widget::pushed() : event_widget();
  291. if ( r )
  292. {
  293. int retval = r->dispatch( m );
  294. if ( retval && m == FL_PUSH )
  295. {
  296. take_focus();
  297. Sequence_Widget::pushed( r );
  298. }
  299. if ( retval && m == FL_RELEASE )
  300. Sequence_Widget::pushed( NULL );
  301. Loggable::block_start();
  302. while ( _delete_queue.size() )
  303. {
  304. Sequence_Widget *t = _delete_queue.front();
  305. _delete_queue.pop();
  306. if ( Sequence_Widget::pushed() == t )
  307. Sequence_Widget::pushed( NULL );
  308. if ( Sequence_Widget::belowmouse() == t )
  309. {
  310. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  311. Sequence_Widget::belowmouse( NULL );
  312. }
  313. timeline->wrlock();
  314. delete t;
  315. timeline->unlock();
  316. }
  317. Loggable::block_end();
  318. return retval;
  319. }
  320. else
  321. return Fl_Widget::handle( m );
  322. }
  323. }
  324. }