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.

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