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.

336 lines
8.3KB

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