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.

366 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. 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. if ( Fl::event_state() & FL_CTRL )
  141. normalize();
  142. else
  143. _selected = ! _selected;
  144. _track->redraw();
  145. }
  146. ret = Track_Widget::handle( m );
  147. return ret | 1;
  148. }
  149. break;
  150. }
  151. case FL_RELEASE:
  152. fl_cursor( FL_CURSOR_DEFAULT );
  153. copied = false;
  154. trimming = NO;
  155. // Fl::release();
  156. return 1;
  157. case FL_DRAG:
  158. if ( Fl::event_state() & FL_SHIFT &&
  159. Fl::event_state() & FL_CTRL )
  160. {
  161. int d = (ox + X) - x();
  162. long td = timeline->x_to_ts( d );
  163. nframes_t W = _end - _start;
  164. if ( td > 0 && os < td )
  165. _start = 0;
  166. else
  167. _start = os - td;
  168. _end = _start + W;
  169. _track->redraw();
  170. return 1;
  171. }
  172. if ( Fl::event_state() & FL_SHIFT )
  173. if ( trimming )
  174. {
  175. trim( trimming, X );
  176. return 1;
  177. }
  178. else
  179. return 0;
  180. if ( Fl::event_state() & FL_CTRL )
  181. {
  182. if ( ! copied )
  183. {
  184. _track->add( new Region( *this ) );
  185. copied = true;
  186. return 1;
  187. }
  188. }
  189. if ( Y > y() + h() )
  190. {
  191. if ( _track->next() )
  192. if ( Y > _track->next()->y() )
  193. _track->next()->add( this );
  194. }
  195. else
  196. if ( Y < y() )
  197. {
  198. if ( _track->prev() )
  199. if ( Y < _track->prev()->y() + _track->prev()->h() )
  200. _track->prev()->add( this );
  201. }
  202. _track->redraw();
  203. ret = Track_Widget::handle( m );
  204. return ret | 1;
  205. // _offset = timeline->x_to_ts( x() );
  206. default:
  207. return Track_Widget::handle( m );
  208. break;
  209. }
  210. }
  211. void
  212. Region::draw_box( int X, int Y, int W, int H )
  213. {
  214. /* dirty hack to keep the box from flipping to vertical at small sizes */
  215. fl_push_clip( x(), Y, w(), H );
  216. if ( _selected )
  217. fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, _selection_color );
  218. // fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, fl_invert_color( _box_color ) );
  219. else
  220. fl_draw_box( box(), x() - 10, Y, w() + 50, H, _box_color );
  221. fl_pop_clip();
  222. }
  223. /* Draw (part of) region. OX is pixel offset from start of timeline, X
  224. Y W and H are the portion of the widget to draw (arrived at by
  225. intersection of the clip and relative to OX) */
  226. void
  227. Region::draw ( int X, int Y, int W, int H )
  228. {
  229. if ( ! ( W > 0 && H > 0 ) )
  230. return;
  231. int OX = scroll_x();
  232. int ox = timeline->ts_to_x( _offset );
  233. if ( ox > OX + _track->w() ||
  234. ox < OX && ox + abs_w() < OX )
  235. return;
  236. int rw = timeline->ts_to_x( _end - _start );
  237. nframes_t end = _offset + ( _end - _start );
  238. /* calculate waveform offset due to scrolling */
  239. nframes_t offset = 0;
  240. if ( ox < OX )
  241. {
  242. offset = timeline->x_to_ts( OX - ox );
  243. rw = timeline->ts_to_x( (_end - _start) - offset );
  244. }
  245. rw = min( rw, _track->w() );
  246. int rx = x();
  247. fl_push_clip( rx, Y, rw, H );
  248. /* dirty hack to keep the box from flipping to vertical at small sizes */
  249. // fl_draw_box( box(), rx - 10, Y, rw + 50, H, _box_color );
  250. // fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
  251. draw_waveform( rx, Y, rw, H, _clip, _start + offset, min( (_end - _start) - offset, _end), _scale, _selected ? _color : fl_invert_color( _color ) );
  252. timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
  253. fl_color( FL_BLACK );
  254. fl_line( rx, Y, rx, Y + H );
  255. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  256. draw_label( _clip->name(), (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM /*| FL_ALIGN_CLIP*/ | FL_ALIGN_INSIDE) );
  257. /* if ( _selected ) */
  258. /* { */
  259. /* fl_color( selection_color() ); */
  260. /* fl_line_style( FL_SOLID, 4 ); */
  261. /* fl_rect( x(), y(), w(), h() ); */
  262. /* fl_line_style( FL_SOLID, 0 ); */
  263. /* } */
  264. fl_pop_clip();
  265. }
  266. void
  267. Region::normalize ( void )
  268. {
  269. printf( "normalize: start=%lu end=%lu\n", _start, _end );
  270. _scale = _clip->peaks()->normalization_factor( _start, _end );
  271. }
  272. void
  273. Region::dump ( void )
  274. {
  275. printf( "%lu { \"%s\" %lu %lu }\n", _offset, _clip->name(), _start, _end );
  276. }