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.

356 lines
8.8KB

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