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.

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