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.

546 lines
13KB

  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 <FL/fl_draw.H>
  19. #include "Sequence_Widget.H"
  20. #include "Track.H"
  21. using namespace std;
  22. list <Sequence_Widget *> Sequence_Widget::_selection;
  23. Sequence_Widget * Sequence_Widget::_current = NULL;
  24. Sequence_Widget * Sequence_Widget::_pushed = NULL;
  25. Sequence_Widget * Sequence_Widget::_belowmouse = NULL;
  26. Fl_Color Sequence_Widget::_selection_color = FL_MAGENTA;
  27. Sequence_Widget::Sequence_Widget ( )
  28. {
  29. _sequence = NULL;
  30. _r = &_range;
  31. _r->start = _r->offset = _r->length = 0;
  32. _drag = NULL;
  33. _box_color = FL_BACKGROUND_COLOR;
  34. _color = FL_FOREGROUND_COLOR;
  35. }
  36. /* careful with this, it doesn't journal */
  37. Sequence_Widget::Sequence_Widget ( const Sequence_Widget &rhs ) : Loggable( rhs )
  38. {
  39. _drag = NULL;
  40. _sequence = rhs._sequence;
  41. _range = rhs._range;
  42. _r = &_range;
  43. _color = rhs._color;
  44. _box_color = rhs._box_color;
  45. };
  46. const Sequence_Widget &
  47. Sequence_Widget::operator= ( const Sequence_Widget &rhs )
  48. {
  49. if ( this == &rhs )
  50. return *this;
  51. _r = &_range;
  52. _range = rhs._range;
  53. _sequence = rhs._sequence;
  54. _box_color = rhs._box_color;
  55. _color = rhs._color;
  56. return *this;
  57. }
  58. Sequence_Widget::~Sequence_Widget ( )
  59. {
  60. redraw();
  61. if ( this == _pushed )
  62. _pushed = NULL;
  63. if ( this == _belowmouse )
  64. _belowmouse = NULL;
  65. _sequence->remove( this );
  66. _selection.remove( this );
  67. }
  68. void
  69. Sequence_Widget::get ( Log_Entry &e ) const
  70. {
  71. e.add( ":start", _r->start );
  72. // e.add( ":offset", _r->offset );
  73. // e.add( ":length", _r->length );
  74. e.add( ":sequence", _sequence );
  75. e.add( ":selected", selected() );
  76. }
  77. void
  78. Sequence_Widget::set ( Log_Entry &e )
  79. {
  80. for ( int i = 0; i < e.size(); ++i )
  81. {
  82. const char *s, *v;
  83. e.get( i, &s, &v );
  84. if ( ! strcmp( s, ":start" ) )
  85. _r->start = atoll( v );
  86. // else if ( ! strcmp( s, ":offset" ) )
  87. // _r->offset = atoll( v );
  88. // else if ( ! strcmp( s, ":length" ) )
  89. // _r->length = atoll( v );
  90. else if ( ! strcmp( s, ":selected" ) )
  91. {
  92. if ( atoi( v ) )
  93. select();
  94. else
  95. deselect();
  96. }
  97. else if ( ! strcmp( s, ":sequence" ) )
  98. {
  99. int i;
  100. sscanf( v, "%X", &i );
  101. Sequence *t = (Sequence*)Loggable::find( i );
  102. assert( t );
  103. t->add( this );
  104. }
  105. // else
  106. // e.erase( i );
  107. }
  108. if ( _sequence )
  109. _sequence->redraw();
  110. }
  111. void
  112. Sequence_Widget::begin_drag ( const Drag &d )
  113. {
  114. _drag = new Drag( d );
  115. timeline->rdlock();
  116. _r = new Range( _range );
  117. timeline->unlock();
  118. }
  119. void
  120. Sequence_Widget::end_drag ( void )
  121. {
  122. timeline->wrlock();
  123. /* swap in the new value */
  124. _range = *_r;
  125. timeline->unlock();
  126. delete _r;
  127. _r = &_range;
  128. delete _drag;
  129. _drag = NULL;
  130. sequence()->handle_widget_change( _r->start, _r->length );
  131. }
  132. /** set position of widget on the timeline. */
  133. void
  134. Sequence_Widget::start ( nframes_t where )
  135. {
  136. /* this is pretty complicated because of selection and snapping */
  137. if ( ! selected() )
  138. {
  139. redraw();
  140. _r->start = where;
  141. }
  142. else
  143. {
  144. if ( this != Sequence_Widget::_current )
  145. return;
  146. long d = where - _r->start;
  147. if ( d < 0 )
  148. {
  149. /* first, make sure we stop at 0 */
  150. nframes_t m = (nframes_t)-1;
  151. for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); ++i )
  152. m = min( m, (*i)->_r->start );
  153. d = 0 - d;
  154. if ( m <= (nframes_t)d )
  155. d = m;
  156. for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); ++i )
  157. {
  158. (*i)->redraw();
  159. (*i)->_r->start -= d;
  160. }
  161. }
  162. else
  163. {
  164. /* TODO: do like the above and disallow wrapping */
  165. for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); ++i )
  166. {
  167. (*i)->redraw();
  168. (*i)->_r->start += d;
  169. }
  170. }
  171. }
  172. }
  173. void
  174. Sequence_Widget::draw_label ( const char *label, Fl_Align align, Fl_Color color )
  175. {
  176. int X, Y;
  177. X = x();
  178. Y = y();
  179. /* FIXME: why do we have to do this here? why doesn't Fl_Label::draw take care of this stuff? */
  180. if ( ! (align & FL_ALIGN_INSIDE) )
  181. {
  182. if ( align & FL_ALIGN_RIGHT )
  183. {
  184. X += abs_w();
  185. align = (Fl_Align)((align & ~FL_ALIGN_RIGHT) | FL_ALIGN_LEFT);
  186. }
  187. if ( align & FL_ALIGN_BOTTOM )
  188. {
  189. Y += h();
  190. align = (Fl_Align)((align & ~FL_ALIGN_BOTTOM) | FL_ALIGN_TOP);
  191. }
  192. }
  193. Fl_Label lab;
  194. lab.color = color;
  195. // lab.type = FL_SHADOW_LABEL;
  196. lab.type = FL_NORMAL_LABEL;
  197. lab.value = label;
  198. lab.font = FL_HELVETICA;
  199. lab.size = 14;
  200. int lw = 0, lh = 0;
  201. fl_font( lab.font, lab.size );
  202. fl_measure( lab.value, lw, lh );
  203. int W = w();
  204. int H = h();
  205. if ( align & FL_ALIGN_INSIDE )
  206. {
  207. X += Fl::box_dx( box() );
  208. Y += Fl::box_dy( box() );
  209. W -= Fl::box_dw( box() );
  210. H -= Fl::box_dh( box() );
  211. }
  212. if ( align & FL_ALIGN_CLIP ) fl_push_clip( X, Y, W, H );
  213. int dx = 0;
  214. /* adjust for scrolling */
  215. if ( abs_x() < scroll_x() )
  216. dx = min( 32767, scroll_x() - abs_x() );
  217. // const Fl_Boxtype b = FL_ROUND_UP_BOX;
  218. const Fl_Boxtype b = FL_ROUNDED_BOX;
  219. const int bx = Fl::box_dx( b ) + 1;
  220. const int bw = Fl::box_dw( b ) + 1;
  221. if ( align & FL_ALIGN_BOTTOM )
  222. fl_draw_box( b, X - dx - bx, Y + H - lh, lw + bw, lh, FL_GRAY );
  223. else if ( align & FL_ALIGN_LEFT )
  224. fl_draw_box( b, X - dx, Y + ((H >> 1) - (lh >> 1)), lw + bw, lh, FL_GRAY );
  225. else if ( align & FL_ALIGN_TOP )
  226. fl_draw_box( b, X - dx - bx + ((W >> 1) - (lw >> 1)), Y + ((H >> 1) - (lh >> 1)), lw + bw, lh, FL_GRAY );
  227. // lab.draw( X - dx, Y, W, H, align );
  228. fl_color( color );
  229. fl_draw( label, ( X - dx ) + bx, Y, W, H, align );
  230. if ( align & FL_ALIGN_CLIP ) fl_pop_clip();
  231. }
  232. int
  233. Sequence_Widget::dispatch ( int m )
  234. {
  235. Sequence_Widget::_current = this;
  236. if ( selected() )
  237. {
  238. Loggable::block_start();
  239. int r = 0;
  240. for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); i++ )
  241. if ( *i != this )
  242. r |= (*i)->handle( m );
  243. r |= handle( m );
  244. Loggable::block_end();
  245. return r;
  246. }
  247. else
  248. return handle( m );
  249. }
  250. void
  251. Sequence_Widget::draw ( void )
  252. {
  253. draw_box();
  254. }
  255. void
  256. Sequence_Widget::draw_box ( void )
  257. {
  258. fl_draw_box( box(), x(), y(), w(), h(), selected() ? FL_MAGENTA : _box_color );
  259. }
  260. #include "FL/test_press.H"
  261. /* base hanlde just does basic dragging */
  262. int
  263. Sequence_Widget::handle ( int m )
  264. {
  265. int X = Fl::event_x();
  266. int Y = Fl::event_y();
  267. Logger _log( this );
  268. switch ( m )
  269. {
  270. case FL_ENTER:
  271. fl_cursor( FL_CURSOR_HAND );
  272. return 1;
  273. case FL_LEAVE:
  274. // DMESSAGE( "leave" );
  275. fl_cursor( sequence()->cursor() );
  276. return 1;
  277. case FL_PUSH:
  278. {
  279. /* deletion */
  280. if ( test_press( FL_BUTTON3 + FL_CTRL ) )
  281. {
  282. remove();
  283. return 1;
  284. }
  285. else if ( test_press( FL_BUTTON1 ) || test_press( FL_BUTTON1 + FL_CTRL ) )
  286. {
  287. fl_cursor( FL_CURSOR_MOVE );
  288. /* movement drag */
  289. return 1;
  290. }
  291. return 0;
  292. }
  293. case FL_RELEASE:
  294. if ( _drag )
  295. {
  296. end_drag();
  297. _log.release();
  298. }
  299. fl_cursor( FL_CURSOR_HAND );
  300. return 1;
  301. case FL_DRAG:
  302. {
  303. Fl::event_key( 0 );
  304. if ( ! _drag )
  305. {
  306. begin_drag ( Drag( x() - X, y() - Y, x_to_offset( X ) ) );
  307. _log.hold();
  308. }
  309. if ( test_press( FL_BUTTON1 + FL_CTRL ) && ! _drag->state )
  310. {
  311. /* duplication */
  312. sequence()->add( this->clone() );
  313. _drag->state = 1;
  314. return 1;
  315. }
  316. else if ( test_press( FL_BUTTON1 ) || test_press( FL_BUTTON1 + FL_CTRL ) )
  317. {
  318. redraw();
  319. const nframes_t of = timeline->x_to_offset( X );
  320. if ( of >= _drag->start )
  321. start( of - _drag->start );
  322. else
  323. start( 0 );
  324. if ( Sequence_Widget::_current == this )
  325. sequence()->snap( this );
  326. if ( X >= sequence()->x() + sequence()->w() ||
  327. X <= sequence()->x() )
  328. {
  329. /* this drag needs to scroll */
  330. nframes_t pos = timeline->xoffset;
  331. nframes_t d = timeline->x_to_ts( 100 );
  332. if ( X <= sequence()->x() )
  333. {
  334. if ( pos > d )
  335. pos -= d;
  336. else
  337. pos = 0;
  338. }
  339. else
  340. pos += d;
  341. timeline->xposition( timeline->ts_to_x( pos ) );
  342. // timeline->update_length( start() + length() );
  343. /* FIXME: why isn't this enough? */
  344. // sequence()->redraw();
  345. timeline->redraw();
  346. }
  347. if ( ! selected() )
  348. {
  349. /* track jumping */
  350. if ( Y > y() + h() || Y < y() )
  351. {
  352. Track *t = timeline->track_under( Y );
  353. fl_cursor( (Fl_Cursor)1 );
  354. if ( t )
  355. t->handle( FL_ENTER );
  356. return 0;
  357. }
  358. }
  359. return 1;
  360. }
  361. else
  362. {
  363. DMESSAGE( "unknown" );
  364. return 0;
  365. }
  366. }
  367. default:
  368. return 0;
  369. }
  370. }
  371. /**********/
  372. /* Public */
  373. /**********/
  374. /** add this widget to the selection */
  375. void
  376. Sequence_Widget::select ( void )
  377. {
  378. if ( selected() )
  379. return;
  380. _selection.push_back( this );
  381. _selection.sort( sort_func );
  382. redraw();
  383. }
  384. /** remove this widget from the selection */
  385. void
  386. Sequence_Widget::deselect ( void )
  387. {
  388. _selection.remove( this );
  389. redraw();
  390. }
  391. bool
  392. Sequence_Widget::selected ( void ) const
  393. {
  394. return std::find( _selection.begin(), _selection.end(), this ) != _selection.end();
  395. }
  396. /** remove this widget from its sequence */
  397. void
  398. Sequence_Widget::remove ( void )
  399. {
  400. redraw();
  401. sequence()->queue_delete( this );
  402. }
  403. void
  404. Sequence_Widget::delete_selected ( void )
  405. {
  406. Loggable::block_start();
  407. while ( _selection.size() )
  408. delete _selection.front();
  409. Loggable::block_end();
  410. }
  411. void
  412. Sequence_Widget::select_none ( void )
  413. {
  414. Loggable::block_start();
  415. while ( _selection.size() )
  416. {
  417. _selection.front()->redraw();
  418. _selection.pop_front();
  419. }
  420. Loggable::block_end();
  421. }