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.

301 lines
7.8KB

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