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.

313 lines
7.6KB

  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 "Track.H"
  19. #include "Region.H"
  20. #include "Timeline.H"
  21. #include <FL/fl_draw.H>
  22. #include <FL/Fl.H>
  23. #include <FL/Fl_Group.H>
  24. #include <FL/Fl_Widget.H>
  25. #include <stdio.h>
  26. extern Timeline timeline;
  27. Region::Region ( int X, int Y, int W, int H, const char *L ) : Waveform( X, Y, W, H, L )
  28. {
  29. init();
  30. }
  31. void
  32. Region::init ( void )
  33. {
  34. align( FL_ALIGN_INSIDE | FL_ALIGN_LEFT | FL_ALIGN_BOTTOM | FL_ALIGN_CLIP );
  35. labeltype( FL_SHADOW_LABEL );
  36. labelcolor( FL_WHITE );
  37. box( FL_PLASTIC_UP_BOX );
  38. _track = NULL;
  39. // _offset = 0;
  40. _offset = timeline.x_to_ts( x() );
  41. }
  42. Region::Region ( const Region & rhs ) : Waveform( rhs )
  43. {
  44. box( rhs.box() );
  45. align( rhs.align() );
  46. color( rhs.color() );
  47. selection_color( rhs.selection_color() );
  48. labelcolor( rhs.labelcolor() );
  49. labeltype( rhs.labeltype() );
  50. _offset = rhs._offset;
  51. _track = rhs._track;
  52. }
  53. Region::Region ( Clip *c ) : Waveform( c )
  54. {
  55. init();
  56. }
  57. void
  58. Region::trim ( enum trim_e t, int X )
  59. {
  60. switch ( t )
  61. {
  62. case LEFT:
  63. {
  64. int d = X - x();
  65. long td = timeline.x_to_ts( d );
  66. if ( td < 0 && _start < 0 - td )
  67. td = 0 - _start;
  68. _start += td;
  69. _offset += td;
  70. resize();
  71. // Fl_Widget::resize( x() + d, y(), w() - d, h() );
  72. // _offset = timeline.x_to_ts( x() );
  73. break;
  74. }
  75. case RIGHT:
  76. {
  77. int d = (x() + w()) - X;
  78. long td = timeline.x_to_ts( d );
  79. _end -= td;
  80. resize();
  81. // _end = _start + timeline.x_to_ts( w() - d );
  82. // Fl_Widget::resize( x(), y(), w() - d, h() );
  83. break;
  84. }
  85. default:
  86. return;
  87. }
  88. redraw();
  89. parent()->redraw();
  90. }
  91. int
  92. Region::handle ( int m )
  93. {
  94. if ( Fl_Widget::handle( m ) )
  95. return 1;
  96. static int ox, oy;
  97. static enum trim_e trimming;
  98. static bool copied = false;
  99. static nframes_t os;
  100. int X = Fl::event_x();
  101. int Y = Fl::event_y();
  102. switch ( m )
  103. {
  104. case FL_PUSH:
  105. {
  106. if ( Fl::event_state() & FL_SHIFT &&
  107. ! ( Fl::event_state() & FL_CTRL ))
  108. {
  109. switch ( Fl::event_button() )
  110. {
  111. case 1:
  112. trim( trimming = LEFT, X );
  113. break;
  114. case 3:
  115. trim( trimming = RIGHT, X );
  116. break;
  117. default:
  118. return 0;
  119. }
  120. fl_cursor( FL_CURSOR_WE );
  121. return 1;
  122. }
  123. else
  124. {
  125. ox = x() - X;
  126. oy = y() - Y;
  127. if ( Fl::event_state() && FL_CTRL )
  128. {
  129. os = _start;
  130. // Fl::local_grab( this );
  131. }
  132. if ( Fl::event_button() == 2 )
  133. normalize();
  134. return 1;
  135. }
  136. return 0;
  137. break;
  138. }
  139. case FL_RELEASE:
  140. fl_cursor( FL_CURSOR_DEFAULT );
  141. copied = false;
  142. trimming = NO;
  143. // Fl::release();
  144. return 1;
  145. case FL_DRAG:
  146. if ( Fl::event_state() & FL_SHIFT &&
  147. Fl::event_state() & FL_CTRL )
  148. {
  149. int d = (ox + X) - x();
  150. long td = timeline.x_to_ts( d );
  151. if ( td > 0 && os < td )
  152. _start = 0;
  153. else
  154. _start = os - td;
  155. redraw();
  156. return 1;
  157. }
  158. if ( Fl::event_state() & FL_SHIFT )
  159. if ( trimming )
  160. {
  161. trim( trimming, X );
  162. return 1;
  163. }
  164. else
  165. return 0;
  166. if ( Fl::event_state() & FL_CTRL )
  167. {
  168. if ( ! copied )
  169. {
  170. _track->add( new Region( *this ) );
  171. copied = true;
  172. return 1;
  173. }
  174. }
  175. if ( ox + X >= _track->x() )
  176. {
  177. int nx = ox + X;
  178. // nx = _track->snap( this, nx );
  179. position( nx, y() );
  180. _track->snap( this );
  181. }
  182. if ( Y > y() + h() )
  183. {
  184. if ( _track->next() )
  185. _track->next()->add( this );
  186. }
  187. else
  188. if ( Y < y() )
  189. {
  190. if ( _track->prev() )
  191. _track->prev()->add( this );
  192. }
  193. parent()->redraw();
  194. fl_cursor( FL_CURSOR_MOVE );
  195. if ( X >= timeline.scroll->x() + timeline.scroll->w() ||
  196. X <= timeline.scroll->x() )
  197. {
  198. /* this drag needs to scroll */
  199. long pos = timeline.scroll->xposition();
  200. if ( X <= timeline.scroll->x() )
  201. pos -= 100;
  202. else
  203. pos += 100;
  204. if ( pos < 0 )
  205. pos = 0;
  206. timeline.scroll->position( pos, timeline.scroll->yposition() );
  207. }
  208. _offset = timeline.x_to_ts( x() );
  209. return 1;
  210. default:
  211. return 0;
  212. break;
  213. }
  214. }
  215. /** must be called whenever zoom is adjusted */
  216. void
  217. Region::resize ( void )
  218. {
  219. int X = timeline.ts_to_x( _offset );
  220. int W = timeline.ts_to_x( _end - _start );
  221. if ( W )
  222. Fl_Widget::resize( X, y(), W, h() );
  223. }
  224. void
  225. Region::draw ( void )
  226. {
  227. draw_box();
  228. // fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
  229. Waveform::draw();
  230. // fl_pop_clip();
  231. /* fl_color( FL_RED ); */
  232. /* int sx = x() - timeline.ts_to_x( _start ); */
  233. /* fl_line( sx, y(), sx, y() + h() ); */
  234. /* int ex = x() + timeline.ts_to_x( _end - _start ); */
  235. /* fl_line( ex, y(), ex, y() + h() ); */
  236. draw_label();
  237. /* static char pat[200]; */
  238. /* sprintf( pat, "start %lu, end %lu", _start, _end ); */
  239. /* fl_draw( pat, x(), y() + h() / 2 ); */
  240. }