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.

347 lines
9.0KB

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