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.

375 lines
9.4KB

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