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.

371 lines
9.3KB

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