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.

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