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.

371 lines
9.1KB

  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. nframes_t W = _end - _start;
  153. if ( td > 0 && os < td )
  154. _start = 0;
  155. else
  156. _start = os - td;
  157. _end = _start + W;
  158. _track->redraw();
  159. return 1;
  160. }
  161. if ( Fl::event_state() & FL_SHIFT )
  162. if ( trimming )
  163. {
  164. trim( trimming, X );
  165. return 1;
  166. }
  167. else
  168. return 0;
  169. if ( Fl::event_state() & FL_CTRL )
  170. {
  171. if ( ! copied )
  172. {
  173. _track->add( new Region( *this ) );
  174. copied = true;
  175. return 1;
  176. }
  177. }
  178. if ( ox + X >= _track->x() )
  179. {
  180. int nx = ox + X;
  181. // nx = _track->snap( this, nx );
  182. // _offset = timeline.x_to_ts( nx );
  183. // position( nx, y() );
  184. _offset = timeline.x_to_ts( nx ) + timeline.xoffset;
  185. _track->snap( this );
  186. }
  187. if ( Y > y() + h() )
  188. {
  189. if ( _track->next() )
  190. _track->next()->add( this );
  191. }
  192. else
  193. if ( Y < y() )
  194. {
  195. if ( _track->prev() )
  196. _track->prev()->add( this );
  197. }
  198. _track->redraw();
  199. fl_cursor( FL_CURSOR_MOVE );
  200. /* if ( X >= timeline.scroll->x() + timeline.scroll->w() || */
  201. /* X <= timeline.scroll->x() ) */
  202. /* { */
  203. /* /\* this drag needs to scroll *\/ */
  204. /* long pos = timeline.scroll->xposition(); */
  205. /* if ( X <= timeline.scroll->x() ) */
  206. /* pos -= 100; */
  207. /* else */
  208. /* pos += 100; */
  209. /* if ( pos < 0 ) */
  210. /* pos = 0; */
  211. /* timeline.scroll->position( pos, timeline.scroll->yposition() ); */
  212. /* } */
  213. // _offset = timeline.x_to_ts( x() );
  214. return 1;
  215. default:
  216. return 0;
  217. break;
  218. }
  219. }
  220. /** must be called whenever zoom is adjusted */
  221. void
  222. Region::resize ( void )
  223. {
  224. int X = timeline.ts_to_x( _offset );
  225. assert( _end >= _start );
  226. int W = timeline.ts_to_x( _end - _start );
  227. printf( "%dx%d\n", X, W );
  228. // if ( W )
  229. // Fl_Widget::resize( X, y(), W, h() );
  230. }
  231. /* X is the timeline offset, W is the width of the track */
  232. void
  233. Region::draw ( int X, int Y, int W, int H )
  234. {
  235. if ( ! ( W > 0 && H > 0 ) )
  236. return;
  237. if ( _offset > timeline.xoffset + timeline.x_to_ts( _track->w() ) ||
  238. ( _offset < timeline.xoffset &&
  239. _offset + (_end - _start) < timeline.xoffset ) )
  240. return;
  241. int rw = timeline.ts_to_x( _end - _start );
  242. nframes_t end = _offset + ( _end - _start );
  243. /* calculate waveform offset due to scrolling */
  244. nframes_t offset = 0;
  245. if ( _offset < timeline.xoffset )
  246. {
  247. offset = timeline.xoffset - _offset;
  248. rw = timeline.ts_to_x( (_end - _start) - offset );
  249. }
  250. rw = min( rw, _track->w() );
  251. int rx = x();
  252. printf( "rx %d, rw %d\n", rx, rw );
  253. fl_push_clip( rx, Y, rw, H );
  254. /* dirty hack to keep the box from flipping to vertical at small sizes */
  255. fl_draw_box( FL_PLASTIC_UP_BOX, rx - 10, Y, rw + 50, H, _box_color );
  256. // fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
  257. draw_waveform( rx, Y, rw, H, _clip, _start + offset, min( (_end - _start) - offset, _end), _scale, _color );
  258. fl_color( FL_BLACK );
  259. fl_line( rx, Y, rx, Y + H );
  260. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  261. fl_pop_clip();
  262. fl_font( FL_HELVETICA, 14 );
  263. fl_color( FL_BLACK );
  264. int bx = Fl::box_dx( FL_PLASTIC_UP_BOX );
  265. int by = Fl::box_dy( FL_PLASTIC_UP_BOX );
  266. int bw = Fl::box_dw( FL_PLASTIC_UP_BOX );
  267. int bh = Fl::box_dh( FL_PLASTIC_UP_BOX );
  268. int dx = min( 32767, timeline.ts_to_x( offset ) );
  269. Fl_Align align = (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM | FL_ALIGN_CLIP);
  270. fl_draw( _clip->name(), (bx + rx + 1) - dx, Y + 1 + by, rw - bw, H - bh, align );
  271. fl_color( FL_WHITE );
  272. fl_draw( _clip->name(), (bx + rx) - dx, Y + by , rw - bw, H - bh, align );
  273. // fl_draw( _clip->name(), X, Y );
  274. //(Fl_Align)FL_ALIGN_LEFT | FL_ALIGN_BOTTOM );
  275. fl_color( FL_RED );
  276. fl_line( x(), y(), x(), y() + h() );
  277. }