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.

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