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.

295 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. #pragma once
  19. #include "Track.H"
  20. #include "Loggable.H"
  21. #include <algorithm>
  22. using namespace std;
  23. /* Base class for virtual widget on a track */
  24. class Track_Widget : public Loggable
  25. {
  26. protected:
  27. Track *_track; /* track this region belongs to */
  28. nframes_t _offset; /* where on the timeline */
  29. nframes_t _start; /* first sample from clip */
  30. nframes_t _end; /* last sample from clip */
  31. bool _selected;
  32. Fl_Color _color; /* color of waveform */
  33. Fl_Color _box_color; /* color of background (box) */
  34. public:
  35. Track_Widget ( )
  36. {
  37. _track = NULL;
  38. _offset = _start = _end = 0;
  39. _selected = false;
  40. }
  41. Fl_Group * parent ( void ) const { return _track; }
  42. int scroll_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
  43. nframes_t scroll_ts ( void ) const { return timeline->xoffset; }
  44. int y ( void ) const { return _track->y(); }
  45. int h ( void ) const { return _track->h(); }
  46. int x ( void ) const { return _offset < timeline->xoffset ? -1 : min( 32767, _track->x() + timeline->ts_to_x( _offset - timeline->xoffset ) ); }
  47. virtual int w ( void ) const
  48. {
  49. int tx = timeline->ts_to_x( _offset );
  50. int rw;
  51. if ( tx < scroll_x() )
  52. rw = abs_w() - (scroll_x() - tx);
  53. else
  54. rw = abs_w();
  55. return min( rw, _track->w() );
  56. }
  57. int abs_x ( void ) const { return timeline->ts_to_x( timeline->xoffset ); }
  58. virtual int abs_w ( void ) const { return timeline->ts_to_x( _end - _start ); }
  59. Fl_Color color ( void ) { return _color; }
  60. Fl_Color box_color ( void ) { return _box_color; }
  61. Track * track ( void ) const { return _track; }
  62. void track ( Track *t ) { _track = t; }
  63. nframes_t offset ( void ) const { return _offset; }
  64. void offset ( nframes_t o ) { _offset = o; }
  65. void end ( nframes_t v ) { _end = v; }
  66. nframes_t end ( void ) const { return _end; }
  67. void start ( nframes_t v ) { _start = v; }
  68. nframes_t start ( void ) const { return _start; }
  69. virtual nframes_t length ( void ) const { return _end - _start; }
  70. virtual Fl_Boxtype box ( void ) const { return FL_UP_BOX; }
  71. virtual Fl_Align align ( void ) const { return (Fl_Align)0; }
  72. virtual void
  73. redraw ( void )
  74. {
  75. if ( ! (align() & FL_ALIGN_INSIDE) )
  76. {
  77. // FIXME: to better..
  78. _track->redraw();
  79. }
  80. else
  81. _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
  82. }
  83. /* just draw a simple box */
  84. virtual void
  85. draw_box ( int X, int Y, int W, int H )
  86. {
  87. fl_draw_box( box(), x(), y(), w(), h(), _box_color );
  88. }
  89. virtual void
  90. draw ( int X, int Y, int W, int H )
  91. {
  92. draw_box( X, Y, W, H );
  93. }
  94. virtual void
  95. dump ( void )
  96. {
  97. printf( "Unknown %p %lu %lu %lu\n", this, _offset, _start, _end );
  98. }
  99. virtual void
  100. draw_label ( const char *label, Fl_Align align )
  101. {
  102. int X, Y;
  103. X = x();
  104. Y = y();
  105. /* FIXME: why do we have to to this here? why doesn't Fl_Lable::draw take care of this stuff? */
  106. if ( ! (align & FL_ALIGN_INSIDE) )
  107. {
  108. if ( align & FL_ALIGN_RIGHT )
  109. {
  110. X += w();
  111. align = (Fl_Align)((align & ~FL_ALIGN_RIGHT) | FL_ALIGN_LEFT);
  112. }
  113. if ( align & FL_ALIGN_BOTTOM )
  114. {
  115. Y += h();
  116. align = (Fl_Align)((align & ~FL_ALIGN_BOTTOM) | FL_ALIGN_TOP);
  117. }
  118. }
  119. /* fl_font( FL_HELVETICA, 14 ); */
  120. /* fl_color( FL_BLACK ); */
  121. /* fl_draw( label, X + 2, Y + 2, w(), h(), align ); */
  122. /* fl_color( FL_WHITE ); */
  123. /* fl_draw( label, X, Y, w(), h(), align ); */
  124. /* for some reasone FL_SHADOW_LABLE doesn't always use
  125. * black for the shadow color...so we can't rely on it. */
  126. Fl_Label lab;
  127. lab.color = FL_WHITE;
  128. lab.type = FL_SHADOW_LABEL;
  129. lab.value = label;
  130. lab.font = FL_HELVETICA;
  131. lab.size = 14;
  132. int W = w();
  133. int H = h();
  134. if ( align & FL_ALIGN_INSIDE )
  135. {
  136. X += Fl::box_dx( box() );
  137. Y += Fl::box_dy( box() );
  138. W -= Fl::box_dw( box() );
  139. H -= Fl::box_dh( box() );
  140. }
  141. if ( align & FL_ALIGN_CLIP ) fl_push_clip( X, Y, W, H );
  142. int dx = 0;
  143. if ( abs_x() < scroll_x() )
  144. dx = min( 32767, scroll_x() - abs_x() );
  145. lab.draw( X - dx, Y, W, H, align );
  146. if ( align & FL_ALIGN_CLIP ) fl_pop_clip();
  147. }
  148. /* base hanlde just does basic dragging */
  149. virtual int
  150. handle ( int m )
  151. {
  152. static int ox, oy;
  153. static bool moved = false;
  154. int X = Fl::event_x();
  155. int Y = Fl::event_y();
  156. switch ( m )
  157. {
  158. case FL_PUSH:
  159. {
  160. ox = x() - X;
  161. oy = y() - Y;
  162. if ( Fl::event_state() & FL_CTRL &&
  163. Fl::event_button() == 3 )
  164. {
  165. log_destroy();
  166. redraw();
  167. _track->queue_delete( this );
  168. return 0;
  169. }
  170. return 1;
  171. }
  172. case FL_RELEASE:
  173. if ( moved )
  174. {
  175. log_move();
  176. moved = false;
  177. }
  178. // dump();
  179. fl_cursor( FL_CURSOR_DEFAULT );
  180. return 1;
  181. case FL_DRAG:
  182. {
  183. redraw();
  184. if ( ox + X >= _track->x() )
  185. {
  186. int nx = ox + X;
  187. _offset = timeline->x_to_ts( nx ) + timeline->xoffset;
  188. _track->snap( this );
  189. moved = true;
  190. }
  191. // _track->redraw();
  192. fl_cursor( FL_CURSOR_MOVE );
  193. if ( X >= _track->x() + _track->w() ||
  194. X <= _track->x() )
  195. {
  196. /* this drag needs to scroll */
  197. nframes_t pos = timeline->xoffset;
  198. nframes_t d = timeline->x_to_ts( 100 );
  199. if ( X <= _track->x() )
  200. {
  201. if ( pos > d )
  202. pos -= d;
  203. else
  204. pos = 0;
  205. }
  206. else
  207. pos += d;
  208. timeline->position( timeline->ts_to_x( pos ) );
  209. // timeline->tracks->redraw();
  210. }
  211. return 1;
  212. }
  213. default:
  214. return 0;
  215. }
  216. }
  217. bool
  218. operator< ( const Track_Widget & rhs )
  219. {
  220. return _offset < rhs._offset;
  221. }
  222. };