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.

334 lines
8.5KB

  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. /* // Fl_Widget::resize( x() + d, y(), w() - d, h() ); */
  76. /* // _offset = timeline.x_to_ts( x() ); */
  77. /* break; */
  78. /* } */
  79. /* case RIGHT: */
  80. /* { */
  81. /* int d = (x() + w()) - X; */
  82. /* long td = timeline.x_to_ts( d ); */
  83. /* _end -= td; */
  84. /* resize(); */
  85. /* // _end = _start + timeline.x_to_ts( w() - d ); */
  86. /* // Fl_Widget::resize( x(), y(), w() - d, h() ); */
  87. /* break; */
  88. /* } */
  89. /* default: */
  90. /* return; */
  91. /* } */
  92. /* redraw(); */
  93. /* parent()->redraw(); */
  94. /* } */
  95. int
  96. Region::handle ( int m )
  97. {
  98. static int ox, oy;
  99. static enum trim_e trimming;
  100. static bool copied = false;
  101. static nframes_t os;
  102. int X = Fl::event_x();
  103. int Y = Fl::event_y();
  104. switch ( m )
  105. {
  106. case FL_PUSH:
  107. {
  108. if ( Fl::event_state() & FL_SHIFT &&
  109. ! ( Fl::event_state() & FL_CTRL ))
  110. {
  111. switch ( Fl::event_button() )
  112. {
  113. /* case 1: */
  114. /* trim( trimming = LEFT, X ); */
  115. /* break; */
  116. /* case 3: */
  117. /* trim( trimming = RIGHT, X ); */
  118. /* break; */
  119. default:
  120. return 0;
  121. }
  122. fl_cursor( FL_CURSOR_WE );
  123. return 1;
  124. }
  125. else
  126. {
  127. ox = x() - X;
  128. oy = y() - Y;
  129. if ( Fl::event_state() && FL_CTRL )
  130. {
  131. os = _start;
  132. // Fl::local_grab( this );
  133. }
  134. /* if ( Fl::event_button() == 2 ) */
  135. /* normalize(); */
  136. return 1;
  137. }
  138. return 0;
  139. break;
  140. }
  141. case FL_RELEASE:
  142. fl_cursor( FL_CURSOR_DEFAULT );
  143. copied = false;
  144. trimming = NO;
  145. // Fl::release();
  146. return 1;
  147. case FL_DRAG:
  148. if ( Fl::event_state() & FL_SHIFT &&
  149. Fl::event_state() & FL_CTRL )
  150. {
  151. int d = (ox + X) - x();
  152. long td = timeline.x_to_ts( d );
  153. if ( td > 0 && os < td )
  154. _start = 0;
  155. else
  156. _start = os - td;
  157. // redraw();
  158. return 1;
  159. }
  160. /* if ( Fl::event_state() & FL_SHIFT ) */
  161. /* if ( trimming ) */
  162. /* { */
  163. /* trim( trimming, X ); */
  164. /* return 1; */
  165. /* } */
  166. /* else */
  167. /* return 0; */
  168. if ( Fl::event_state() & FL_CTRL )
  169. {
  170. if ( ! copied )
  171. {
  172. _track->add( new Region( *this ) );
  173. copied = true;
  174. return 1;
  175. }
  176. }
  177. if ( ox + X >= _track->x() )
  178. {
  179. int nx = ox + X;
  180. // nx = _track->snap( this, nx );
  181. // _offset = timeline.x_to_ts( nx );
  182. // position( nx, y() );
  183. _offset = timeline.x_to_ts( nx ) + timeline.xoffset;
  184. // _track->snap( this );
  185. }
  186. if ( Y > y() + h() )
  187. {
  188. if ( _track->next() )
  189. _track->next()->add( this );
  190. }
  191. else
  192. if ( Y < y() )
  193. {
  194. if ( _track->prev() )
  195. _track->prev()->add( this );
  196. }
  197. _track->redraw();
  198. fl_cursor( FL_CURSOR_MOVE );
  199. /* if ( X >= timeline.scroll->x() + timeline.scroll->w() || */
  200. /* X <= timeline.scroll->x() ) */
  201. /* { */
  202. /* /\* this drag needs to scroll *\/ */
  203. /* long pos = timeline.scroll->xposition(); */
  204. /* if ( X <= timeline.scroll->x() ) */
  205. /* pos -= 100; */
  206. /* else */
  207. /* pos += 100; */
  208. /* if ( pos < 0 ) */
  209. /* pos = 0; */
  210. /* timeline.scroll->position( pos, timeline.scroll->yposition() ); */
  211. /* } */
  212. // _offset = timeline.x_to_ts( x() );
  213. return 1;
  214. default:
  215. return 0;
  216. break;
  217. }
  218. }
  219. /** must be called whenever zoom is adjusted */
  220. void
  221. Region::resize ( void )
  222. {
  223. int X = timeline.ts_to_x( _offset );
  224. assert( _end >= _start );
  225. int W = timeline.ts_to_x( _end - _start );
  226. printf( "%dx%d\n", X, W );
  227. // if ( W )
  228. // Fl_Widget::resize( X, y(), W, h() );
  229. }
  230. /* X is the timeline offset, W is the width of the track */
  231. void
  232. Region::draw ( int X, int Y, int W, int H )
  233. {
  234. int rx = timeline.ts_to_x( _offset ) - X;
  235. int rw = min( timeline.ts_to_x( _end - _start ), W );
  236. fl_draw_box( FL_PLASTIC_UP_BOX, rx, Y, rw, H, FL_CYAN );
  237. // fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
  238. draw_waveform( rx, Y, rw, H, _clip, _start, _end, _scale, FL_GREEN );
  239. // fl_pop_clip();
  240. fl_font( FL_HELVETICA, 14 );
  241. fl_color( FL_BLACK );
  242. int bx = Fl::box_dx( FL_PLASTIC_UP_BOX );
  243. int by = Fl::box_dy( FL_PLASTIC_UP_BOX );
  244. int bw = Fl::box_dw( FL_PLASTIC_UP_BOX );
  245. int bh = Fl::box_dh( FL_PLASTIC_UP_BOX );
  246. Fl_Align align = (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM | FL_ALIGN_CLIP);
  247. fl_draw( _clip->name(), bx + rx + 1, Y + 1 + by, rw - bw, H - bh, align );
  248. fl_color( FL_WHITE );
  249. fl_draw( _clip->name(), bx + rx, Y + by , rw - bw, H - bh, align );
  250. // fl_draw( _clip->name(), X, Y );
  251. //(Fl_Align)FL_ALIGN_LEFT | FL_ALIGN_BOTTOM );
  252. }