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.

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