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.

377 lines
9.2KB

  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. Fl_Boxtype Region::_box = FL_PLASTIC_UP_BOX;
  33. void
  34. Region::init ( void )
  35. {
  36. /* align( FL_ALIGN_INSIDE | FL_ALIGN_LEFT | FL_ALIGN_BOTTOM | FL_ALIGN_CLIP ); */
  37. /* labeltype( FL_SHADOW_LABEL ); */
  38. /* labelcolor( FL_WHITE ); */
  39. /* box( FL_PLASTIC_UP_BOX ); */
  40. _track = NULL;
  41. _offset = 0;
  42. _start = 0;
  43. _end = 0;
  44. _scale = 1.0f;
  45. _clip = NULL;
  46. _box_color = FL_CYAN;
  47. _color = FL_BLUE;
  48. }
  49. Region::Region ( const Region & rhs )
  50. {
  51. _offset = rhs._offset;
  52. _track = rhs._track;
  53. _clip = rhs._clip;
  54. _start = rhs._start;
  55. _end = rhs._end;
  56. _scale = rhs._scale;
  57. }
  58. Region::Region ( Clip *c )
  59. {
  60. init();
  61. _clip = c;
  62. _end = _clip->length();
  63. }
  64. void
  65. Region::trim ( enum trim_e t, int X )
  66. {
  67. switch ( t )
  68. {
  69. case LEFT:
  70. {
  71. int d = X - x();
  72. long td = timeline.x_to_ts( d );
  73. if ( td < 0 && _start < 0 - td )
  74. td = 0 - _start;
  75. _start += td;
  76. _offset += td;
  77. // resize();
  78. break;
  79. }
  80. case RIGHT:
  81. {
  82. int d = (x() + w()) - X;
  83. long td = timeline.x_to_ts( d );
  84. _end -= td;
  85. // resize();
  86. break;
  87. }
  88. default:
  89. return;
  90. }
  91. _track->redraw();
  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. nframes_t W = _end - _start;
  154. if ( td > 0 && os < td )
  155. _start = 0;
  156. else
  157. _start = os - td;
  158. _end = _start + W;
  159. _track->redraw();
  160. return 1;
  161. }
  162. if ( Fl::event_state() & FL_SHIFT )
  163. if ( trimming )
  164. {
  165. trim( trimming, X );
  166. return 1;
  167. }
  168. else
  169. return 0;
  170. if ( Fl::event_state() & FL_CTRL )
  171. {
  172. if ( ! copied )
  173. {
  174. _track->add( new Region( *this ) );
  175. copied = true;
  176. return 1;
  177. }
  178. }
  179. if ( ox + X >= _track->x() )
  180. {
  181. int nx = ox + X;
  182. // nx = _track->snap( this, nx );
  183. // _offset = timeline.x_to_ts( nx );
  184. // position( nx, y() );
  185. _offset = timeline.x_to_ts( nx ) + timeline.xoffset;
  186. _track->snap( this );
  187. }
  188. if ( Y > y() + h() )
  189. {
  190. if ( _track->next() )
  191. _track->next()->add( this );
  192. }
  193. else
  194. if ( Y < y() )
  195. {
  196. if ( _track->prev() )
  197. _track->prev()->add( this );
  198. }
  199. _track->redraw();
  200. fl_cursor( FL_CURSOR_MOVE );
  201. /* if ( X >= timeline.scroll->x() + timeline.scroll->w() || */
  202. /* X <= timeline.scroll->x() ) */
  203. /* { */
  204. /* /\* this drag needs to scroll *\/ */
  205. /* long pos = timeline.scroll->xposition(); */
  206. /* if ( X <= timeline.scroll->x() ) */
  207. /* pos -= 100; */
  208. /* else */
  209. /* pos += 100; */
  210. /* if ( pos < 0 ) */
  211. /* pos = 0; */
  212. /* timeline.scroll->position( pos, timeline.scroll->yposition() ); */
  213. /* } */
  214. // _offset = timeline.x_to_ts( x() );
  215. return 1;
  216. default:
  217. return 0;
  218. break;
  219. }
  220. }
  221. /** must be called whenever zoom is adjusted */
  222. void
  223. Region::resize ( void )
  224. {
  225. int X = timeline.ts_to_x( _offset );
  226. assert( _end >= _start );
  227. int W = timeline.ts_to_x( _end - _start );
  228. printf( "%dx%d\n", X, W );
  229. // if ( W )
  230. // Fl_Widget::resize( X, y(), W, h() );
  231. }
  232. int measure = 40;
  233. /* Draw (part of) region. Start is */
  234. void
  235. Region::draw ( int X, int Y, int W, int H )
  236. {
  237. if ( ! ( W > 0 && H > 0 ) )
  238. return;
  239. if ( _offset > timeline.xoffset + timeline.x_to_ts( _track->w() ) ||
  240. ( _offset < timeline.xoffset &&
  241. _offset + (_end - _start) < timeline.xoffset ) )
  242. return;
  243. int rw = timeline.ts_to_x( _end - _start );
  244. nframes_t end = _offset + ( _end - _start );
  245. /* calculate waveform offset due to scrolling */
  246. nframes_t offset = 0;
  247. if ( _offset < timeline.xoffset )
  248. {
  249. offset = timeline.xoffset - _offset;
  250. rw = timeline.ts_to_x( (_end - _start) - offset );
  251. }
  252. rw = min( rw, _track->w() );
  253. int rx = x();
  254. // printf( "rx %d, rw %d\n", rx, rw );
  255. fl_push_clip( rx, Y, rw, H );
  256. /* dirty hack to keep the box from flipping to vertical at small sizes */
  257. fl_draw_box( box(), rx - 10, Y, rw + 50, H, _box_color );
  258. // fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
  259. draw_waveform( rx, Y, rw, H, _clip, _start + offset, min( (_end - _start) - offset, _end), _scale, _color );
  260. timeline.draw_measure_lines( rx, Y, rw, H, _box_color );
  261. fl_color( FL_BLACK );
  262. fl_line( rx, Y, rx, Y + H );
  263. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  264. fl_pop_clip();
  265. fl_font( FL_HELVETICA, 14 );
  266. fl_color( FL_BLACK );
  267. int bx = Fl::box_dx( box() );
  268. int by = Fl::box_dy( box() );
  269. int bw = Fl::box_dw( box() );
  270. int bh = Fl::box_dh( box() );
  271. int dx = min( 32767, timeline.ts_to_x( offset ) );
  272. Fl_Align align = (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM | FL_ALIGN_CLIP);
  273. fl_draw( _clip->name(), (bx + rx + 1) - dx, Y + 1 + by, rw - bw, H - bh, align );
  274. fl_color( FL_WHITE );
  275. fl_draw( _clip->name(), (bx + rx) - dx, Y + by , rw - bw, H - bh, align );
  276. // fl_draw( _clip->name(), X, Y );
  277. //(Fl_Align)FL_ALIGN_LEFT | FL_ALIGN_BOTTOM );
  278. /* fl_color( FL_RED ); */
  279. /* fl_line( x(), y(), x(), y() + h() ); */
  280. }