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.

384 lines
9.4KB

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