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.

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