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.

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