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, "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, int xo, int yo )
  180. {
  181. int X = x();
  182. int Y = y();
  183. int W = w();
  184. int H = h();
  185. if ( align & FL_ALIGN_CLIP ) fl_push_clip( X, Y, W, H );
  186. X += xo;
  187. Y += yo;
  188. Fl_Label lab;
  189. lab.color = color;
  190. // lab.type = FL_SHADOW_LABEL;
  191. lab.type = FL_NORMAL_LABEL;
  192. lab.value = label;
  193. lab.font = FL_HELVETICA;
  194. lab.size = 14;
  195. int lw = 0, lh = 0;
  196. fl_font( lab.font, lab.size );
  197. fl_measure( lab.value, lw, lh );
  198. int dx = 0;
  199. /* adjust for scrolling */
  200. if ( abs_x() < scroll_x() )
  201. dx = min( 32767, scroll_x() - abs_x() );
  202. const Fl_Boxtype b = FL_ROUNDED_BOX;
  203. const int bx = Fl::box_dx( b ) + 1;
  204. const int bw = Fl::box_dw( b ) + 1;
  205. // const int by = Fl::box_dy( b ) + 1;
  206. const int bh = Fl::box_dh( b ) + 1;
  207. /* FIXME: why do we have to do this here? why doesn't Fl_Label::draw take care of this stuff? */
  208. if ( align & FL_ALIGN_INSIDE )
  209. {
  210. X += Fl::box_dx( box() );
  211. Y += Fl::box_dy( box() );
  212. W -= Fl::box_dw( box() );
  213. H -= Fl::box_dh( box() );
  214. if ( align & FL_ALIGN_RIGHT )
  215. X += abs_w() - (lw + bw);
  216. if ( align & FL_ALIGN_BOTTOM )
  217. Y += h() - (lh + (bh << 1));
  218. }
  219. else
  220. {
  221. if ( align & FL_ALIGN_RIGHT )
  222. X += abs_w();
  223. else if ( align & FL_ALIGN_LEFT )
  224. X -= lw + bw;
  225. if ( align & FL_ALIGN_BOTTOM )
  226. Y += h();
  227. else if ( align & FL_ALIGN_TOP )
  228. Y -= lh + bh;
  229. }
  230. fl_draw_box( b, ( X - dx ), Y, lw + bw, lh, FL_GRAY );
  231. fl_color( color );
  232. fl_draw( label, ( X - dx ) + bx, Y, lw, lh, (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_CENTER ) );
  233. if ( align & FL_ALIGN_CLIP ) fl_pop_clip();
  234. }
  235. int
  236. Sequence_Widget::dispatch ( int m )
  237. {
  238. Sequence_Widget::_current = this;
  239. if ( selected() )
  240. {
  241. Loggable::block_start();
  242. int r = 0;
  243. for ( list <Sequence_Widget *>::iterator i = _selection.begin(); i != _selection.end(); i++ )
  244. if ( *i != this )
  245. r |= (*i)->handle( m );
  246. r |= handle( m );
  247. Loggable::block_end();
  248. return r;
  249. }
  250. else
  251. return handle( m );
  252. }
  253. void
  254. Sequence_Widget::draw ( void )
  255. {
  256. draw_box();
  257. }
  258. void
  259. Sequence_Widget::draw_box ( void )
  260. {
  261. fl_draw_box( box(), x(), y(), w(), h(), selected() ? FL_MAGENTA : _box_color );
  262. }
  263. #include "FL/test_press.H"
  264. /* base hanlde just does basic dragging */
  265. int
  266. Sequence_Widget::handle ( int m )
  267. {
  268. int X = Fl::event_x();
  269. int Y = Fl::event_y();
  270. Logger _log( this );
  271. switch ( m )
  272. {
  273. case FL_ENTER:
  274. fl_cursor( FL_CURSOR_HAND );
  275. return 1;
  276. case FL_LEAVE:
  277. // DMESSAGE( "leave" );
  278. fl_cursor( sequence()->cursor() );
  279. return 1;
  280. case FL_PUSH:
  281. {
  282. /* deletion */
  283. if ( test_press( FL_BUTTON3 + FL_CTRL ) )
  284. {
  285. remove();
  286. return 1;
  287. }
  288. else if ( test_press( FL_BUTTON1 ) || test_press( FL_BUTTON1 + FL_CTRL ) )
  289. {
  290. /* traditional selection model */
  291. if ( Fl::event_ctrl() )
  292. select();
  293. else if ( ! selected() )
  294. {
  295. select_none();
  296. select();
  297. }
  298. fl_cursor( FL_CURSOR_MOVE );
  299. /* movement drag */
  300. return 1;
  301. }
  302. return 0;
  303. }
  304. case FL_RELEASE:
  305. if ( _drag )
  306. {
  307. end_drag();
  308. _log.release();
  309. }
  310. fl_cursor( FL_CURSOR_HAND );
  311. return 1;
  312. case FL_DRAG:
  313. {
  314. Fl::event_key( 0 );
  315. if ( ! _drag )
  316. {
  317. begin_drag ( Drag( x() - X, y() - Y, x_to_offset( X ) ) );
  318. _log.hold();
  319. }
  320. if ( test_press( FL_BUTTON1 + FL_CTRL ) && ! _drag->state )
  321. {
  322. /* duplication */
  323. sequence()->add( this->clone() );
  324. _drag->state = 1;
  325. return 1;
  326. }
  327. else if ( test_press( FL_BUTTON1 ) || test_press( FL_BUTTON1 + FL_CTRL ) )
  328. {
  329. redraw();
  330. const nframes_t of = timeline->x_to_offset( X );
  331. if ( of >= _drag->start )
  332. start( of - _drag->start );
  333. else
  334. start( 0 );
  335. if ( Sequence_Widget::_current == this )
  336. sequence()->snap( this );
  337. if ( X >= sequence()->x() + sequence()->w() ||
  338. X <= sequence()->x() )
  339. {
  340. /* this drag needs to scroll */
  341. nframes_t pos = timeline->xoffset;
  342. nframes_t d = timeline->x_to_ts( 100 );
  343. if ( X <= sequence()->x() )
  344. {
  345. if ( pos > d )
  346. pos -= d;
  347. else
  348. pos = 0;
  349. }
  350. else
  351. pos += d;
  352. timeline->xposition( timeline->ts_to_x( pos ) );
  353. // timeline->update_length( start() + length() );
  354. /* FIXME: why isn't this enough? */
  355. // sequence()->redraw();
  356. timeline->redraw();
  357. }
  358. if ( ! selected() || _selection.size() == 1 )
  359. {
  360. /* track jumping */
  361. if ( Y > y() + h() || Y < y() )
  362. {
  363. Track *t = timeline->track_under( Y );
  364. fl_cursor( (Fl_Cursor)1 );
  365. if ( t )
  366. t->handle( FL_ENTER );
  367. return 0;
  368. }
  369. }
  370. return 1;
  371. }
  372. else
  373. {
  374. DMESSAGE( "unknown" );
  375. return 0;
  376. }
  377. }
  378. default:
  379. return 0;
  380. }
  381. }
  382. /**********/
  383. /* Public */
  384. /**********/
  385. /** add this widget to the selection */
  386. void
  387. Sequence_Widget::select ( void )
  388. {
  389. if ( selected() )
  390. return;
  391. _selection.push_back( this );
  392. _selection.sort( sort_func );
  393. redraw();
  394. }
  395. /** remove this widget from the selection */
  396. void
  397. Sequence_Widget::deselect ( void )
  398. {
  399. _selection.remove( this );
  400. redraw();
  401. }
  402. bool
  403. Sequence_Widget::selected ( void ) const
  404. {
  405. return std::find( _selection.begin(), _selection.end(), this ) != _selection.end();
  406. }
  407. /** remove this widget from its sequence */
  408. void
  409. Sequence_Widget::remove ( void )
  410. {
  411. redraw();
  412. sequence()->queue_delete( this );
  413. }
  414. void
  415. Sequence_Widget::delete_selected ( void )
  416. {
  417. Loggable::block_start();
  418. while ( _selection.size() )
  419. delete _selection.front();
  420. Loggable::block_end();
  421. }
  422. void
  423. Sequence_Widget::select_none ( void )
  424. {
  425. Loggable::block_start();
  426. while ( _selection.size() )
  427. {
  428. Sequence_Widget *w = _selection.front();
  429. w->log_start();
  430. _selection.front()->redraw();
  431. _selection.pop_front();
  432. w->log_end();
  433. }
  434. Loggable::block_end();
  435. }