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.

345 lines
8.5KB

  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. if ( Y > _track->next()->y() )
  185. _track->next()->add( this );
  186. }
  187. else
  188. if ( Y < y() )
  189. {
  190. if ( _track->prev() )
  191. if ( Y < _track->prev()->y() + _track->prev()->h() )
  192. _track->prev()->add( this );
  193. }
  194. _track->redraw();
  195. ret = Track_Widget::handle( m );
  196. return ret | 1;
  197. // _offset = timeline.x_to_ts( x() );
  198. default:
  199. return Track_Widget::handle( m );
  200. break;
  201. }
  202. }
  203. void
  204. Region::draw_box( int X, int Y, int W, int H )
  205. {
  206. /* dirty hack to keep the box from flipping to vertical at small sizes */
  207. fl_push_clip( x(), Y, w(), H );
  208. if ( _selected )
  209. fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, _selection_color );
  210. else
  211. fl_draw_box( box(), x() - 10, Y, w() + 50, H, _box_color );
  212. fl_pop_clip();
  213. }
  214. /* Draw (part of) region. OX is pixel offset from start of timeline, X
  215. Y W and H are the portion of the widget to draw (arrived at by
  216. intersection of the clip and relative to OX) */
  217. void
  218. Region::draw ( int X, int Y, int W, int H )
  219. {
  220. if ( ! ( W > 0 && H > 0 ) )
  221. return;
  222. int OX = scroll_x();
  223. int ox = timeline.ts_to_x( _offset );
  224. if ( ox > OX + _track->w() ||
  225. ox < OX && ox + abs_w() < OX )
  226. return;
  227. int rw = timeline.ts_to_x( _end - _start );
  228. nframes_t end = _offset + ( _end - _start );
  229. /* calculate waveform offset due to scrolling */
  230. nframes_t offset = 0;
  231. if ( ox < OX )
  232. {
  233. offset = timeline.x_to_ts( OX - ox );
  234. rw = timeline.ts_to_x( (_end - _start) - offset );
  235. }
  236. rw = min( rw, _track->w() );
  237. int rx = x();
  238. fl_push_clip( rx, Y, rw, H );
  239. /* dirty hack to keep the box from flipping to vertical at small sizes */
  240. // fl_draw_box( box(), rx - 10, Y, rw + 50, H, _box_color );
  241. // fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
  242. draw_waveform( rx, Y, rw, H, _clip, _start + offset, min( (_end - _start) - offset, _end), _scale, _color );
  243. timeline.draw_measure_lines( rx, Y, rw, H, _box_color );
  244. fl_color( FL_BLACK );
  245. fl_line( rx, Y, rx, Y + H );
  246. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  247. draw_label( _clip->name(), (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM /*| FL_ALIGN_CLIP*/ | FL_ALIGN_INSIDE) );
  248. /* if ( _selected ) */
  249. /* { */
  250. /* fl_color( selection_color() ); */
  251. /* fl_line_style( FL_SOLID, 4 ); */
  252. /* fl_rect( x(), y(), w(), h() ); */
  253. /* fl_line_style( FL_SOLID, 0 ); */
  254. /* } */
  255. fl_pop_clip();
  256. }
  257. void
  258. Region::normalize ( void )
  259. {
  260. printf( "normalize: start=%lu end=%lu\n", _start, _end );
  261. _scale = _clip->peaks()->normalization_factor( _start, _end );
  262. }