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.

413 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 "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. // clear_visible_focus();
  45. }
  46. Sequence::~Sequence ( )
  47. {
  48. if ( _name )
  49. free( _name );
  50. for ( std::list <Sequence_Widget*>::iterator i = _widgets.begin();
  51. i != _widgets.end(); ++i )
  52. {
  53. Sequence_Widget *w = *i;
  54. *i = NULL;
  55. delete w;
  56. }
  57. _widgets.clear();
  58. }
  59. nframes_t
  60. Sequence::x_to_offset ( int X )
  61. {
  62. return timeline->xoffset + timeline->x_to_ts( X - x() );
  63. }
  64. void
  65. Sequence::sort ( void )
  66. {
  67. _widgets.sort( Sequence_Widget::sort_func );
  68. }
  69. /** return a pointer to the widget that /r/ overlaps, or NULL if none. */
  70. Sequence_Widget *
  71. Sequence::overlaps ( Sequence_Widget *r )
  72. {
  73. for ( list <Sequence_Widget *>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  74. {
  75. if ( *i == r ) continue;
  76. if ( ! ( (*i)->start() > r->start() + r->length() || (*i)->start() + (*i)->length() < r->start() ) )
  77. return *i;
  78. }
  79. return NULL;
  80. }
  81. void
  82. Sequence::draw ( void )
  83. {
  84. if ( ! fl_not_clipped( x(), y(), w(), h() ) )
  85. return;
  86. fl_push_clip( x(), y(), w(), h() );
  87. /* draw the box with the ends cut off. */
  88. draw_box( box(), x() - Fl::box_dx( box() ) - 1, y(), w() + Fl::box_dw( box() ) + 2, h(), color() );
  89. int X, Y, W, H;
  90. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  91. /* if ( Sequence_Widget::pushed() && Sequence_Widget::pushed()->sequence() == this ) */
  92. /* { */
  93. /* /\* make sure the Sequence_Widget::pushed widget is above all others *\/ */
  94. /* remove( Sequence_Widget::pushed() ); */
  95. /* add( Sequence_Widget::pushed() ); */
  96. /* } */
  97. // printf( "track::draw %d,%d %dx%d\n", X,Y,W,H );
  98. timeline->draw_measure_lines( X, Y, W, H, color() );
  99. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
  100. (*r)->draw_box();
  101. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); ++r )
  102. (*r)->draw();
  103. fl_pop_clip();
  104. }
  105. void
  106. Sequence::remove ( Sequence_Widget *r )
  107. {
  108. timeline->wrlock();
  109. _widgets.remove( r );
  110. timeline->unlock();
  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. timeline->wrlock();
  158. r->sequence( this );
  159. _widgets.push_back( r );
  160. sort();
  161. timeline->unlock();
  162. handle_widget_change( r->start(), r->length() );
  163. }
  164. static nframes_t
  165. abs_diff ( nframes_t n1, nframes_t n2 )
  166. {
  167. return n1 > n2 ? n1 - n2 : n2 - n1;
  168. }
  169. /* snap /r/ to nearest edge */
  170. void
  171. Sequence::snap ( Sequence_Widget *r )
  172. {
  173. const int snap_pixels = 10;
  174. const nframes_t snap_frames = timeline->x_to_ts( snap_pixels );
  175. /* snap to other widgets */
  176. if ( Timeline::snap_magnetic )
  177. {
  178. const int rx1 = r->start();
  179. const int rx2 = r->start() + r->length();
  180. for ( list <Sequence_Widget*>::const_iterator i = _widgets.begin(); i != _widgets.end(); i++ )
  181. {
  182. const Sequence_Widget *w = (*i);
  183. if ( w == r )
  184. continue;
  185. const int wx1 = w->start();
  186. const int wx2 = w->start() + w->length();
  187. if ( abs_diff( rx1, wx2 ) < snap_frames )
  188. {
  189. r->start( w->start() + w->length() + 1 );
  190. return;
  191. }
  192. if ( abs_diff( rx2, wx1 ) < snap_frames )
  193. {
  194. r->start( ( w->start() - r->length() ) - 1 );
  195. return;
  196. }
  197. }
  198. }
  199. nframes_t f;
  200. /* snap to beat/bar lines */
  201. if ( timeline->nearest_line( r->start(), &f ) )
  202. r->start( f );
  203. }
  204. #include "FL/event_name.H"
  205. int
  206. Sequence::handle ( int m )
  207. {
  208. /* if ( m != FL_NO_EVENT ) */
  209. /* DMESSAGE( "%s", event_name( m ) ); */
  210. switch ( m )
  211. {
  212. case FL_KEYBOARD:
  213. /* this is a hack to override FLTK's use of arrow keys for
  214. * focus navigation */
  215. return timeline->handle_scroll( m );
  216. case FL_NO_EVENT:
  217. /* garbage from overlay window */
  218. return 0;
  219. case FL_FOCUS:
  220. case FL_UNFOCUS:
  221. Fl_Widget::handle( m );
  222. redraw();
  223. return 1;
  224. case FL_LEAVE:
  225. // DMESSAGE( "leave" );
  226. fl_cursor( FL_CURSOR_DEFAULT );
  227. Fl_Widget::handle( m );
  228. return 1;
  229. case FL_DND_DRAG:
  230. return 1;
  231. case FL_ENTER:
  232. // DMESSAGE( "enter" );
  233. if ( Sequence_Widget::pushed() )
  234. {
  235. if ( Sequence_Widget::pushed()->sequence()->class_name() == class_name() )
  236. {
  237. /* accept objects dragged from other sequences of this type */
  238. add( Sequence_Widget::pushed() );
  239. redraw();
  240. fl_cursor( FL_CURSOR_MOVE );
  241. }
  242. else
  243. fl_cursor( (Fl_Cursor)1 );
  244. }
  245. else
  246. if ( ! event_widget() )
  247. fl_cursor( cursor() );
  248. Fl_Widget::handle( m );
  249. return 1;
  250. case FL_DND_ENTER:
  251. case FL_DND_LEAVE:
  252. case FL_DND_RELEASE:
  253. return 1;
  254. case FL_MOVE:
  255. {
  256. Sequence_Widget *r = event_widget();
  257. if ( r != Sequence_Widget::belowmouse() )
  258. {
  259. if ( Sequence_Widget::belowmouse() )
  260. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  261. Sequence_Widget::belowmouse( r );
  262. if ( r )
  263. r->handle( FL_ENTER );
  264. }
  265. return 1;
  266. }
  267. default:
  268. {
  269. Sequence_Widget *r = Sequence_Widget::pushed() ? Sequence_Widget::pushed() : event_widget();
  270. /* if ( this == Fl::focus() ) */
  271. /* DMESSAGE( "Sequence widget = %p", r ); */
  272. if ( r )
  273. {
  274. int retval = r->dispatch( m );
  275. /* DMESSAGE( "retval = %d", retval ); */
  276. if ( m == FL_PUSH )
  277. take_focus();
  278. if ( retval )
  279. {
  280. if ( m == FL_PUSH )
  281. {
  282. if ( Sequence_Widget::pushed() )
  283. Sequence_Widget::pushed()->handle( FL_UNFOCUS );
  284. Sequence_Widget::pushed( r );
  285. r->handle( FL_FOCUS );
  286. }
  287. else if ( m == FL_RELEASE )
  288. Sequence_Widget::pushed( NULL );
  289. }
  290. Loggable::block_start();
  291. while ( _delete_queue.size() )
  292. {
  293. Sequence_Widget *t = _delete_queue.front();
  294. _delete_queue.pop();
  295. if ( Sequence_Widget::pushed() == t )
  296. Sequence_Widget::pushed( NULL );
  297. if ( Sequence_Widget::belowmouse() == t )
  298. {
  299. Sequence_Widget::belowmouse()->handle( FL_LEAVE );
  300. Sequence_Widget::belowmouse( NULL );
  301. }
  302. delete t;
  303. }
  304. Loggable::block_end();
  305. if ( m == FL_PUSH )
  306. return 1;
  307. else
  308. return retval;
  309. }
  310. else
  311. return Fl_Widget::handle( m );
  312. }
  313. }
  314. }