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.

379 lines
9.4KB

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