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.

343 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. Fl_Boxtype Region::_box = FL_PLASTIC_UP_BOX;
  33. Fl_Color Region::_selection_color = FL_MAGENTA;
  34. void
  35. Region::init ( void )
  36. {
  37. /* align( FL_ALIGN_INSIDE | FL_ALIGN_LEFT | FL_ALIGN_BOTTOM | FL_ALIGN_CLIP ); */
  38. /* labeltype( FL_SHADOW_LABEL ); */
  39. /* labelcolor( FL_WHITE ); */
  40. /* box( FL_PLASTIC_UP_BOX ); */
  41. _track = NULL;
  42. _offset = 0;
  43. _start = 0;
  44. _end = 0;
  45. _scale = 1.0f;
  46. _clip = NULL;
  47. _box_color = FL_CYAN;
  48. _color = FL_BLUE;
  49. }
  50. Region::Region ( const Region & rhs )
  51. {
  52. _offset = rhs._offset;
  53. _track = rhs._track;
  54. _clip = rhs._clip;
  55. _start = rhs._start;
  56. _end = rhs._end;
  57. _scale = rhs._scale;
  58. }
  59. Region::Region ( Clip *c )
  60. {
  61. init();
  62. _clip = c;
  63. _end = _clip->length();
  64. }
  65. void
  66. Region::trim ( enum trim_e t, int X )
  67. {
  68. switch ( t )
  69. {
  70. case LEFT:
  71. {
  72. int d = X - x();
  73. long td = timeline.x_to_ts( d );
  74. if ( td < 0 && _start < 0 - td )
  75. td = 0 - _start;
  76. _start += td;
  77. _offset += td;
  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. break;
  86. }
  87. default:
  88. return;
  89. }
  90. _track->redraw();
  91. }
  92. int
  93. Region::handle ( int m )
  94. {
  95. static int ox, oy;
  96. static enum trim_e trimming;
  97. static bool copied = false;
  98. static nframes_t os;
  99. int X = Fl::event_x();
  100. int Y = Fl::event_y();
  101. int ret;
  102. switch ( m )
  103. {
  104. case FL_PUSH:
  105. {
  106. if ( Fl::event_state() & FL_SHIFT &&
  107. ! ( Fl::event_state() & FL_CTRL ))
  108. {
  109. switch ( Fl::event_button() )
  110. {
  111. case 1:
  112. trim( trimming = LEFT, X );
  113. break;
  114. case 3:
  115. trim( trimming = RIGHT, X );
  116. break;
  117. default:
  118. return 0;
  119. }
  120. fl_cursor( FL_CURSOR_WE );
  121. return 1;
  122. }
  123. else
  124. {
  125. ox = x() - X;
  126. oy = y() - Y;
  127. if ( Fl::event_state() && FL_CTRL )
  128. {
  129. os = _start;
  130. // Fl::local_grab( this );
  131. }
  132. if ( Fl::event_button() == 2 )
  133. {
  134. // normalize();
  135. _selected = ! _selected;
  136. _track->redraw();
  137. }
  138. ret = Track_Widget::handle( m );
  139. return ret | 1;
  140. }
  141. break;
  142. }
  143. case FL_RELEASE:
  144. fl_cursor( FL_CURSOR_DEFAULT );
  145. copied = false;
  146. trimming = NO;
  147. // Fl::release();
  148. return 1;
  149. case FL_DRAG:
  150. if ( Fl::event_state() & FL_SHIFT &&
  151. Fl::event_state() & FL_CTRL )
  152. {
  153. int d = (ox + X) - x();
  154. long td = timeline.x_to_ts( d );
  155. nframes_t W = _end - _start;
  156. if ( td > 0 && os < td )
  157. _start = 0;
  158. else
  159. _start = os - td;
  160. _end = _start + W;
  161. _track->redraw();
  162. return 1;
  163. }
  164. if ( Fl::event_state() & FL_SHIFT )
  165. if ( trimming )
  166. {
  167. trim( trimming, X );
  168. return 1;
  169. }
  170. else
  171. return 0;
  172. if ( Fl::event_state() & FL_CTRL )
  173. {
  174. if ( ! copied )
  175. {
  176. _track->add( new Region( *this ) );
  177. copied = true;
  178. return 1;
  179. }
  180. }
  181. if ( Y > y() + h() )
  182. {
  183. if ( _track->next() )
  184. _track->next()->add( this );
  185. }
  186. else
  187. if ( Y < y() )
  188. {
  189. if ( _track->prev() )
  190. _track->prev()->add( this );
  191. }
  192. _track->redraw();
  193. ret = Track_Widget::handle( m );
  194. return ret | 1;
  195. // _offset = timeline.x_to_ts( x() );
  196. default:
  197. return Track_Widget::handle( m );
  198. break;
  199. }
  200. }
  201. void
  202. Region::draw_box( int X, int Y, int W, int H )
  203. {
  204. /* dirty hack to keep the box from flipping to vertical at small sizes */
  205. fl_push_clip( x(), Y, w(), H );
  206. if ( _selected )
  207. fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, _selection_color );
  208. else
  209. fl_draw_box( box(), x() - 10, Y, w() + 50, H, _box_color );
  210. fl_pop_clip();
  211. }
  212. /* Draw (part of) region. OX is pixel offset from start of timeline, X
  213. Y W and H are the portion of the widget to draw (arrived at by
  214. intersection of the clip and relative to OX) */
  215. void
  216. Region::draw ( int X, int Y, int W, int H )
  217. {
  218. if ( ! ( W > 0 && H > 0 ) )
  219. return;
  220. int OX = scroll_x();
  221. int ox = timeline.ts_to_x( _offset );
  222. if ( ox > OX + _track->w() ||
  223. ox < OX && ox + abs_w() < OX )
  224. return;
  225. int rw = timeline.ts_to_x( _end - _start );
  226. nframes_t end = _offset + ( _end - _start );
  227. /* calculate waveform offset due to scrolling */
  228. nframes_t offset = 0;
  229. if ( ox < OX )
  230. {
  231. offset = timeline.x_to_ts( OX - ox );
  232. rw = timeline.ts_to_x( (_end - _start) - offset );
  233. }
  234. rw = min( rw, _track->w() );
  235. int rx = x();
  236. fl_push_clip( rx, Y, rw, H );
  237. /* dirty hack to keep the box from flipping to vertical at small sizes */
  238. // fl_draw_box( box(), rx - 10, Y, rw + 50, H, _box_color );
  239. // fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
  240. draw_waveform( rx, Y, rw, H, _clip, _start + offset, min( (_end - _start) - offset, _end), _scale, _color );
  241. timeline.draw_measure_lines( rx, Y, rw, H, _box_color );
  242. fl_color( FL_BLACK );
  243. fl_line( rx, Y, rx, Y + H );
  244. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  245. draw_label( _clip->name(), (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM /*| FL_ALIGN_CLIP*/ | FL_ALIGN_INSIDE) );
  246. /* if ( _selected ) */
  247. /* { */
  248. /* fl_color( selection_color() ); */
  249. /* fl_line_style( FL_SOLID, 4 ); */
  250. /* fl_rect( x(), y(), w(), h() ); */
  251. /* fl_line_style( FL_SOLID, 0 ); */
  252. /* } */
  253. fl_pop_clip();
  254. }
  255. void
  256. Region::normalize ( void )
  257. {
  258. printf( "normalize: start=%lu end=%lu\n", _start, _end );
  259. _scale = _clip->peaks()->normalization_factor( _start, _end );
  260. }