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.

378 lines
9.6KB

  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 "Audio_Region.H"
  21. #include <FL/fl_draw.H>
  22. #include "Track.H"
  23. #include "../FL/Boxtypes.H"
  24. using namespace std;
  25. queue <Sequence_Widget *> Sequence::_delete_queue;
  26. Sequence::Sequence ( Track *track ) : Fl_Widget( 0, 0, 0, 0 ), Loggable( true )
  27. {
  28. init();
  29. _track = track;
  30. // log_create();
  31. }
  32. Sequence::Sequence ( int X, int Y, int W, int H ) : Fl_Widget( X, Y, W, H ), Loggable( false )
  33. {
  34. init();
  35. }
  36. void
  37. Sequence::init ( void )
  38. {
  39. _track = NULL;
  40. _name = NULL;
  41. box( FL_DOWN_BOX );
  42. color( FL_BACKGROUND_COLOR );
  43. align( FL_ALIGN_LEFT );
  44. }
  45. Sequence::~Sequence ( )
  46. {
  47. if ( _name )
  48. free( _name );
  49. for ( std::list <Sequence_Widget*>::iterator i = _widgets.begin();
  50. i != _widgets.end(); ++i )
  51. {
  52. Sequence_Widget *w = *i;
  53. *i = NULL;
  54. delete w;
  55. }
  56. _widgets.clear();
  57. }
  58. nframes_t
  59. Sequence::x_to_offset ( int X )
  60. {
  61. return timeline->xoffset + timeline->x_to_ts( X - x() );
  62. }
  63. void
  64. Sequence::sort ( void )
  65. {
  66. _widgets.sort( Sequence_Widget::sort_func );
  67. }
  68. /** return a pointer to the widget that /r/ overlaps, or NULL if none. */
  69. Sequence_Widget *
  70. Sequence::overlaps ( Sequence_Widget *r )
  71. {
  72. for ( list <Sequence_Widget *>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  73. {
  74. if ( *i == r ) continue;
  75. if ( ! ( (*i)->start() > r->start() + r->length() || (*i)->start() + (*i)->length() < r->start() ) )
  76. return *i;
  77. }
  78. return NULL;
  79. }
  80. void
  81. Sequence::draw ( void )
  82. {
  83. if ( ! fl_not_clipped( x(), y(), w(), h() ) )
  84. return;
  85. fl_push_clip( x(), y(), w(), h() );
  86. /* draw the box with the ends cut off. */
  87. draw_box( box(), x() - Fl::box_dx( box() ) - 1, y(), w() + Fl::box_dw( box() ) + 2, h(), color() );
  88. int X, Y, W, H;
  89. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  90. /* if ( Sequence_Widget::pushed() && Sequence_Widget::pushed()->sequence() == this ) */
  91. /* { */
  92. /* /\* make sure the Sequence_Widget::pushed widget is above all others *\/ */
  93. /* remove( Sequence_Widget::pushed() ); */
  94. /* add( Sequence_Widget::pushed() ); */
  95. /* } */
  96. // printf( "track::draw %d,%d %dx%d\n", X,Y,W,H );
  97. timeline->draw_measure_lines( X, Y, W, H, color() );
  98. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
  99. (*r)->draw_box();
  100. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
  101. (*r)->draw();
  102. fl_pop_clip();
  103. }
  104. void
  105. Sequence::remove ( Sequence_Widget *r )
  106. {
  107. timeline->wrlock();
  108. _widgets.remove( r );
  109. timeline->unlock();
  110. handle_widget_change( r->start(), r->length() );
  111. }
  112. void
  113. Sequence::remove_selected ( void )
  114. {
  115. Loggable::block_start();
  116. for ( list <Sequence_Widget *>::iterator r = _widgets.begin(); r != _widgets.end(); )
  117. if ( (*r)->selected() )
  118. {
  119. Sequence_Widget *t = *r;
  120. _widgets.erase( r++ );
  121. delete t;
  122. }
  123. else
  124. ++r;
  125. Loggable::block_end();
  126. }
  127. Sequence_Widget *
  128. Sequence::event_widget ( void )
  129. {
  130. nframes_t ets = timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() );
  131. for ( list <Sequence_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  132. if ( ets > (*r)->start() && ets < (*r)->start() + (*r)->length()
  133. && Fl::event_y() >= (*r)->y() && Fl::event_y() <= (*r)->y() + (*r)->h() )
  134. return (*r);
  135. return NULL;
  136. }
  137. void
  138. Sequence::select_range ( int X, int W )
  139. {
  140. nframes_t sts = x_to_offset( X );
  141. nframes_t ets = sts + timeline->x_to_ts( W );
  142. for ( list <Sequence_Widget *>::const_reverse_iterator r = _widgets.rbegin(); r != _widgets.rend(); r++ )
  143. if ( ! ( (*r)->start() > ets || (*r)->start() + (*r)->length() < sts ) )
  144. (*r)->select();
  145. }
  146. void
  147. Sequence::add ( Sequence_Widget *r )
  148. {
  149. // Logger _log( this );
  150. if ( r->sequence() )
  151. {
  152. r->redraw();
  153. r->sequence()->remove( r );
  154. // r->track()->redraw();
  155. }
  156. timeline->wrlock();
  157. r->sequence( this );
  158. _widgets.push_back( r );
  159. sort();
  160. timeline->unlock();
  161. handle_widget_change( r->start(), r->length() );
  162. }
  163. static nframes_t
  164. abs_diff ( nframes_t n1, nframes_t n2 )
  165. {
  166. return n1 > n2 ? n1 - n2 : n2 - n1;
  167. }
  168. /* snap /r/ to nearest edge */
  169. void
  170. Sequence::snap ( Sequence_Widget *r )
  171. {
  172. const int snap_pixels = 10;
  173. const nframes_t snap_frames = timeline->x_to_ts( snap_pixels );
  174. /* snap to other widgets */
  175. if ( Timeline::snap_magnetic )
  176. {
  177. const int rx1 = r->start();
  178. const int rx2 = r->start() + r->length();
  179. for ( list <Sequence_Widget*>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  180. {
  181. const Sequence_Widget *w = (*i);
  182. if ( w == r )
  183. continue;
  184. const int wx1 = w->start();
  185. const int wx2 = w->start() + w->length();
  186. if ( abs_diff( rx1, wx2 ) < snap_frames )
  187. {
  188. r->start( w->start() + w->length() + 1 );
  189. return;
  190. }
  191. if ( abs_diff( rx2, wx1 ) < snap_frames )
  192. {
  193. r->start( ( w->start() - r->length() ) - 1 );
  194. return;
  195. }
  196. }
  197. }
  198. nframes_t f;
  199. /* snap to beat/bar lines */
  200. if ( timeline->nearest_line( r->start(), &f ) )
  201. r->start( f );
  202. }
  203. int
  204. Sequence::handle ( int m )
  205. {
  206. switch ( m )
  207. {
  208. case FL_FOCUS:
  209. case FL_UNFOCUS:
  210. return 1;
  211. case FL_LEAVE:
  212. // DMESSAGE( "leave" );
  213. fl_cursor( FL_CURSOR_DEFAULT );
  214. return 1;
  215. case FL_DND_DRAG:
  216. return 1;
  217. case FL_ENTER:
  218. // DMESSAGE( "enter" );
  219. if ( Sequence_Widget::pushed() )
  220. {
  221. if ( Sequence_Widget::pushed()->sequence()->class_name() == class_name() )
  222. {
  223. /* accept objects dragged from other sequences of this type */
  224. add( Sequence_Widget::pushed() );
  225. redraw();
  226. fl_cursor( FL_CURSOR_MOVE );
  227. }
  228. else
  229. fl_cursor( (Fl_Cursor)1 );
  230. }
  231. else
  232. if ( ! event_widget() )
  233. fl_cursor( cursor() );
  234. return 1;
  235. case FL_DND_ENTER:
  236. case FL_DND_LEAVE:
  237. case FL_DND_RELEASE:
  238. return 1;
  239. case FL_MOVE:
  240. {
  241. Sequence_Widget *r = event_widget();
  242. if ( r != Sequence_Widget::belowmouse() )
  243. {
  244. if ( Sequence_Widget::belowmouse() )
  245. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  246. Sequence_Widget::belowmouse( r );
  247. if ( r )
  248. r->handle( FL_ENTER );
  249. }
  250. return 0;
  251. }
  252. default:
  253. {
  254. Sequence_Widget *r = Sequence_Widget::pushed() ? Sequence_Widget::pushed() : event_widget();
  255. if ( r )
  256. {
  257. int retval = r->dispatch( m );
  258. if ( retval )
  259. {
  260. if ( m == FL_PUSH )
  261. {
  262. take_focus();
  263. Sequence_Widget::pushed( r );
  264. }
  265. else if ( m == FL_RELEASE )
  266. Sequence_Widget::pushed( NULL );
  267. }
  268. Loggable::block_start();
  269. while ( _delete_queue.size() )
  270. {
  271. Sequence_Widget *t = _delete_queue.front();
  272. _delete_queue.pop();
  273. if ( Sequence_Widget::pushed() == t )
  274. Sequence_Widget::pushed( NULL );
  275. if ( Sequence_Widget::belowmouse() == t )
  276. {
  277. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  278. Sequence_Widget::belowmouse( NULL );
  279. }
  280. delete t;
  281. }
  282. Loggable::block_end();
  283. return retval;
  284. }
  285. else
  286. return Fl_Widget::handle( m );
  287. }
  288. }
  289. }