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.

369 lines
9.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. 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. int ret;
  105. switch ( m )
  106. {
  107. case FL_PUSH:
  108. {
  109. if ( Fl::event_state() & FL_SHIFT &&
  110. ! ( Fl::event_state() & FL_CTRL ))
  111. {
  112. switch ( Fl::event_button() )
  113. {
  114. case 1:
  115. trim( trimming = LEFT, X );
  116. break;
  117. case 3:
  118. trim( trimming = RIGHT, X );
  119. break;
  120. default:
  121. return 0;
  122. }
  123. fl_cursor( FL_CURSOR_WE );
  124. return 1;
  125. }
  126. else
  127. {
  128. ox = x() - X;
  129. oy = y() - Y;
  130. if ( Fl::event_state() && FL_CTRL )
  131. {
  132. os = _start;
  133. // Fl::local_grab( this );
  134. }
  135. ret = Track_Widget::handle( m );
  136. return ret | 1;
  137. /* if ( Fl::event_button() == 2 ) */
  138. /* normalize(); */
  139. }
  140. break;
  141. }
  142. case FL_RELEASE:
  143. fl_cursor( FL_CURSOR_DEFAULT );
  144. copied = false;
  145. trimming = NO;
  146. // Fl::release();
  147. return 1;
  148. case FL_DRAG:
  149. if ( Fl::event_state() & FL_SHIFT &&
  150. Fl::event_state() & FL_CTRL )
  151. {
  152. int d = (ox + X) - x();
  153. long td = timeline.x_to_ts( d );
  154. nframes_t W = _end - _start;
  155. if ( td > 0 && os < td )
  156. _start = 0;
  157. else
  158. _start = os - td;
  159. _end = _start + W;
  160. _track->redraw();
  161. return 1;
  162. }
  163. if ( Fl::event_state() & FL_SHIFT )
  164. if ( trimming )
  165. {
  166. trim( trimming, X );
  167. return 1;
  168. }
  169. else
  170. return 0;
  171. if ( Fl::event_state() & FL_CTRL )
  172. {
  173. if ( ! copied )
  174. {
  175. _track->add( new Region( *this ) );
  176. copied = true;
  177. return 1;
  178. }
  179. }
  180. if ( Y > y() + h() )
  181. {
  182. if ( _track->next() )
  183. _track->next()->add( this );
  184. }
  185. else
  186. if ( Y < y() )
  187. {
  188. if ( _track->prev() )
  189. _track->prev()->add( this );
  190. }
  191. _track->redraw();
  192. ret = Track_Widget::handle( m );
  193. return ret | 1;
  194. /* if ( X >= timeline.scroll->x() + timeline.scroll->w() || */
  195. /* X <= timeline.scroll->x() ) */
  196. /* { */
  197. /* /\* this drag needs to scroll *\/ */
  198. /* long pos = timeline.scroll->xposition(); */
  199. /* if ( X <= timeline.scroll->x() ) */
  200. /* pos -= 100; */
  201. /* else */
  202. /* pos += 100; */
  203. /* if ( pos < 0 ) */
  204. /* pos = 0; */
  205. /* timeline.scroll->position( pos, timeline.scroll->yposition() ); */
  206. /* } */
  207. // _offset = timeline.x_to_ts( x() );
  208. default:
  209. return Track_Widget::handle( m );
  210. break;
  211. }
  212. }
  213. /** must be called whenever zoom is adjusted */
  214. void
  215. Region::resize ( void )
  216. {
  217. int X = timeline.ts_to_x( _offset );
  218. assert( _end >= _start );
  219. int W = timeline.ts_to_x( _end - _start );
  220. printf( "%dx%d\n", X, W );
  221. // if ( W )
  222. // Fl_Widget::resize( X, y(), W, h() );
  223. }
  224. int measure = 40;
  225. /* Draw (part of) region. OX is pixel offset from start of timeline, X
  226. Y W and H are the portion of the widget to draw (arrived at by
  227. intersection of the clip and relative to OX) */
  228. void
  229. Region::draw ( int X, int Y, int W, int H )
  230. {
  231. if ( ! ( W > 0 && H > 0 ) )
  232. return;
  233. int OX = scroll_x();
  234. int ox = timeline.ts_to_x( _offset );
  235. if ( ox > OX + _track->w() ||
  236. ox < OX && ox + w() < OX )
  237. return;
  238. int rw = timeline.ts_to_x( _end - _start );
  239. nframes_t end = _offset + ( _end - _start );
  240. /* calculate waveform offset due to scrolling */
  241. nframes_t offset = 0;
  242. if ( ox < OX )
  243. {
  244. offset = timeline.x_to_ts( OX - ox );
  245. rw = timeline.ts_to_x( (_end - _start) - offset );
  246. }
  247. rw = min( rw, _track->w() );
  248. int rx = x();
  249. // printf( "rx %d, rw %d\n", rx, rw );
  250. fl_push_clip( rx, Y, rw, H );
  251. /* dirty hack to keep the box from flipping to vertical at small sizes */
  252. fl_draw_box( box(), rx - 10, Y, rw + 50, H, _box_color );
  253. // fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
  254. draw_waveform( rx, Y, rw, H, _clip, _start + offset, min( (_end - _start) - offset, _end), _scale, _color );
  255. timeline.draw_measure_lines( rx, Y, rw, H, _box_color );
  256. fl_color( FL_BLACK );
  257. fl_line( rx, Y, rx, Y + H );
  258. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  259. fl_pop_clip();
  260. fl_font( FL_HELVETICA, 14 );
  261. fl_color( FL_BLACK );
  262. int bx = Fl::box_dx( box() );
  263. int by = Fl::box_dy( box() );
  264. int bw = Fl::box_dw( box() );
  265. int bh = Fl::box_dh( box() );
  266. int dx = min( 32767, timeline.ts_to_x( offset ) );
  267. Fl_Align align = (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM | FL_ALIGN_CLIP);
  268. fl_draw( _clip->name(), (bx + rx + 1) - dx, Y + 1 + by, rw - bw, H - bh, align );
  269. fl_color( FL_WHITE );
  270. fl_draw( _clip->name(), (bx + rx) - dx, Y + by , rw - bw, H - bh, align );
  271. // fl_draw( _clip->name(), X, Y );
  272. //(Fl_Align)FL_ALIGN_LEFT | FL_ALIGN_BOTTOM );
  273. /* fl_color( FL_RED ); */
  274. /* fl_line( x(), y(), x(), y() + h() ); */
  275. }