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.

307 lines
9.5KB

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