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.

358 lines
10KB

  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. #pragma once
  19. // class Track;
  20. // #include "Waveform.H"
  21. #include "Clip.H"
  22. #include "Track.H"
  23. #include "Timeline.H"
  24. #include <algorithm>
  25. using namespace std;
  26. /* Regions are "virtual" FLTK widgets; this is necessary because the
  27. * dimensions of real FLTK widgets are limited to 16-bits, which is
  28. * far too little for our purposes */
  29. /* Base class for virtual widget on a track */
  30. class Track_Widget
  31. {
  32. protected:
  33. Track *_track; /* track this region belongs to */
  34. nframes_t _offset; /* where on the timeline */
  35. nframes_t _start; /* first sample from clip */
  36. nframes_t _end; /* last sample from clip */
  37. bool _selected;
  38. Fl_Color _color; /* color of waveform */
  39. Fl_Color _box_color; /* color of background (box) */
  40. public:
  41. Track_Widget ( )
  42. {
  43. _track = NULL;
  44. _offset = _start = _end = 0;
  45. _selected = false;
  46. }
  47. Fl_Group * parent ( void ) const { return _track; }
  48. int scroll_x ( void ) const { return timeline.ts_to_x( timeline.xoffset ); }
  49. nframes_t scroll_ts ( void ) const { return timeline.xoffset; }
  50. int y ( void ) const { return _track->y(); }
  51. int h ( void ) const { return _track->h(); }
  52. int x ( void ) const { return _offset < timeline.xoffset ? -1 : min( 32767, _track->x() + timeline.ts_to_x( _offset - timeline.xoffset ) ); }
  53. virtual int w ( void ) const
  54. {
  55. int tx = timeline.ts_to_x( _offset );
  56. int rw;
  57. if ( tx < scroll_x() )
  58. rw = abs_w() - (scroll_x() - tx);
  59. else
  60. rw = abs_w();
  61. return min( rw, _track->w() );
  62. }
  63. int abs_x ( void ) const { return timeline.ts_to_x( timeline.xoffset ); }
  64. virtual int abs_w ( void ) const { return timeline.ts_to_x( _end - _start ); }
  65. Fl_Color color ( void ) { return _color; }
  66. Fl_Color box_color ( void ) { return _box_color; }
  67. Track * track ( void ) const { return _track; }
  68. void track ( Track *t ) { _track = t; }
  69. nframes_t offset ( void ) const { return _offset; }
  70. void offset ( nframes_t o ) { _offset = o; }
  71. void end ( nframes_t v ) { _end = v; }
  72. nframes_t end ( void ) const { return _end; }
  73. void start ( nframes_t v ) { _start = v; }
  74. nframes_t start ( void ) const { return _start; }
  75. virtual nframes_t length ( void ) const { return _end - _start; }
  76. virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
  77. /* just draw a simple box */
  78. virtual void
  79. draw_box ( int X, int Y, int W, int H )
  80. {
  81. fl_draw_box( box(), x(), y(), w(), y(), _box_color );
  82. }
  83. virtual void
  84. draw ( int X, int Y, int W, int H )
  85. {
  86. draw_box( X, Y, W, H );
  87. }
  88. virtual void
  89. draw_label ( const char *label, Fl_Align align )
  90. {
  91. int X, Y;
  92. X = x();
  93. Y = y();
  94. /* FIXME: why do we have to to this here? why doesn't Fl_Lable::draw take care of this stuff? */
  95. if ( ! (align & FL_ALIGN_INSIDE) )
  96. {
  97. if ( align & FL_ALIGN_RIGHT )
  98. {
  99. X += w();
  100. align = (Fl_Align)((align & ~FL_ALIGN_RIGHT) | FL_ALIGN_LEFT);
  101. }
  102. if ( align & FL_ALIGN_BOTTOM )
  103. {
  104. Y += h();
  105. align = (Fl_Align)((align & ~FL_ALIGN_BOTTOM) | FL_ALIGN_TOP);
  106. }
  107. }
  108. /* fl_font( FL_HELVETICA, 14 ); */
  109. /* fl_color( FL_BLACK ); */
  110. /* fl_draw( label, X + 2, Y + 2, w(), h(), align ); */
  111. /* fl_color( FL_WHITE ); */
  112. /* fl_draw( label, X, Y, w(), h(), align ); */
  113. /* for some reasone FL_SHADOW_LABLE doesn't always use
  114. * black for the shadow color...so we can't rely on it. */
  115. Fl_Label lab;
  116. lab.color = FL_WHITE;
  117. lab.type = FL_SHADOW_LABEL;
  118. lab.value = label;
  119. lab.font = FL_HELVETICA;
  120. lab.size = 14;
  121. int W = w();
  122. int H = h();
  123. if ( align & FL_ALIGN_INSIDE )
  124. {
  125. X += Fl::box_dx( box() );
  126. Y += Fl::box_dy( box() );
  127. W -= Fl::box_dw( box() );
  128. H -= Fl::box_dh( box() );
  129. }
  130. if ( align & FL_ALIGN_CLIP ) fl_push_clip( X, Y, W, H );
  131. int dx = 0;
  132. int tx = timeline.ts_to_x( _offset );
  133. if ( tx < scroll_x() )
  134. dx = min( 32767, scroll_x() - tx );
  135. lab.draw( X - dx, Y, W, H, align );
  136. if ( align & FL_ALIGN_CLIP ) fl_pop_clip();
  137. }
  138. /* base hanlde just does basic dragging */
  139. virtual int
  140. handle ( int m )
  141. {
  142. static int ox, oy;
  143. int X = Fl::event_x();
  144. int Y = Fl::event_y();
  145. switch ( m )
  146. {
  147. case FL_PUSH:
  148. {
  149. ox = x() - X;
  150. oy = y() - Y;
  151. if ( Fl::event_state() & FL_CTRL &&
  152. Fl::event_button() == 3 )
  153. {
  154. _track->queue_delete( this );
  155. return 0;
  156. }
  157. return 1;
  158. }
  159. case FL_RELEASE:
  160. fl_cursor( FL_CURSOR_DEFAULT );
  161. return 1;
  162. case FL_DRAG:
  163. {
  164. if ( ox + X >= _track->x() )
  165. {
  166. int nx = ox + X;
  167. _offset = timeline.x_to_ts( nx ) + timeline.xoffset;
  168. _track->snap( this );
  169. }
  170. _track->redraw();
  171. fl_cursor( FL_CURSOR_MOVE );
  172. if ( X >= _track->x() + _track->w() ||
  173. X <= _track->x() )
  174. {
  175. /* this drag needs to scroll */
  176. nframes_t pos = timeline.xoffset;
  177. nframes_t d = timeline.x_to_ts( 100 );
  178. if ( X <= _track->x() )
  179. {
  180. if ( pos > d )
  181. pos -= d;
  182. else
  183. pos = 0;
  184. }
  185. else
  186. pos += d;
  187. timeline.xoffset = pos;
  188. timeline.tracks->redraw();
  189. }
  190. return 1;
  191. }
  192. default:
  193. return 0;
  194. }
  195. }
  196. bool
  197. operator< ( const Track_Widget & rhs )
  198. {
  199. return _offset < rhs._offset;
  200. }
  201. };
  202. class Track_Point : public Track_Widget
  203. {
  204. protected:
  205. char *_label;
  206. public:
  207. int abs_w ( void ) const { return 10; }
  208. nframes_t length ( void ) const { return timeline.x_to_ts( abs_w() ); }
  209. Track_Point ( )
  210. {
  211. _label = NULL;
  212. }
  213. void
  214. draw ( int X, int Y, int W, int H )
  215. {
  216. Track_Widget::draw( x(), Y, w(), H );
  217. draw_label( _label, FL_ALIGN_RIGHT );
  218. }
  219. };
  220. class Tempo_Point : public Track_Point
  221. {
  222. float _tempo;
  223. void
  224. _make_label ( void )
  225. {
  226. if ( ! _label )
  227. _label = new char[40];
  228. snprintf( _label, 40, "%.1f", _tempo );
  229. }
  230. public:
  231. Tempo_Point ( nframes_t when, float bpm )
  232. {
  233. _tempo = bpm;
  234. _offset = when;
  235. _make_label();
  236. }
  237. ~Tempo_Point ( )
  238. { if ( _label ) delete[] _label; }
  239. };
  240. class Region : public Track_Widget
  241. {
  242. Clip *_clip; /* clip this region represents */
  243. float _scale; /* amplitude adjustment */
  244. static Fl_Boxtype _box;
  245. static Fl_Color _selection_color;
  246. static Fl_Color selection_color ( void ) { return _selection_color; }
  247. static void selection_color ( Fl_Color v ) { _selection_color = v; }
  248. enum trim_e { NO, LEFT, RIGHT };
  249. void trim ( enum trim_e t, int X );
  250. void init ( void );
  251. public:
  252. Fl_Boxtype box ( void ) const { return Region::_box; }
  253. Region ( const Region & rhs );
  254. Region ( Clip *c );
  255. int handle ( int m );
  256. void draw_box( int X, int Y, int W, int H );
  257. void draw ( int X, int Y, int W, int H );
  258. void resize ( void );
  259. void normalize ( void );
  260. };