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.

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