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.

442 lines
12KB

  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 <FL/Fl_Dial.H>
  19. #include <FL/fl_draw.H>
  20. #include <FL/Fl.H>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <algorithm>
  25. #include <FL/Fl_Shared_Image.H>
  26. class image_node {
  27. public:
  28. Fl_Image *original; /* originl image */
  29. Fl_Image *scaled; /* downscaled image */
  30. class image_node *next;
  31. };
  32. static image_node *_first = 0;
  33. int Fl_Dial::_default_style = Fl_Dial::PLASTIC_DIAL;
  34. Fl_Image *Fl_Dial::_default_image = 0;
  35. /** This simple box is suitable for use with knob-type widgets. It
  36. * comprises a border with shadow, and a cap with glare-lines akin
  37. * to those seen on burnished aluminum knobs. */
  38. static void
  39. burnished_oval_box ( int x, int y, int w, int h, Fl_Color c )
  40. {
  41. /* draw background */
  42. fl_color( fl_darker( c ) );
  43. fl_pie( x, y, w, h, 0, 360 );
  44. fl_color( fl_darker( fl_darker( c ) ) );
  45. fl_pie( x, y, w, h, 180 + 215, 180 + 45 );
  46. /* shrink */
  47. x += 4;
  48. y += 4;
  49. w -= 7;
  50. h -= 7;
  51. /* draw cap */
  52. fl_color( c );
  53. fl_pie( x, y, w, h, 0, 360 );
  54. /* draw glare */
  55. const int a1 = 10;
  56. const int a2 = 90;
  57. fl_color( fl_color_average( FL_WHITE, c, 0.15f ) );
  58. fl_pie( x, y, w, h, a1, a2 );
  59. fl_pie( x, y, w, h, 180 + a1, 180 + a2 );
  60. fl_color( fl_color_average( FL_WHITE, c, 0.25f ) );
  61. const int d = (a2 - a1) / 2;
  62. fl_pie( x, y, w, h, a1 + (d / 2), a2 - (d / 2) );
  63. fl_pie( x, y, w, h, 180 + a1 + (d / 2), 180 + a2 - (d / 2) );
  64. }
  65. void
  66. Fl_Dial::draw_box ( void )
  67. {
  68. }
  69. static Fl_Widget *_mouse_inside = NULL;
  70. int
  71. Fl_Dial::handle ( int m )
  72. {
  73. /* Fl_Dial and friends should really handle mousewheel, but they don't in FTLK1 */
  74. switch ( m )
  75. {
  76. case FL_ENTER:
  77. _mouse_inside = this;
  78. redraw();
  79. return Fl_Dial_Base::handle(m) || 1;
  80. case FL_LEAVE:
  81. _mouse_inside = NULL;
  82. redraw();
  83. return Fl_Dial_Base::handle(m) || 1;
  84. case FL_MOUSEWHEEL:
  85. {
  86. if ( this != Fl::belowmouse() )
  87. return 0;
  88. if (Fl::e_dy==0)
  89. return 0;
  90. const int steps = Fl::event_ctrl() ? 128 : 16;
  91. const float step = fabs( maximum() - minimum() ) / (float)steps;
  92. int dy = Fl::e_dy;
  93. /* slider is in 'upside down' configuration, invert meaning of mousewheel */
  94. if ( maximum() > minimum() )
  95. dy = 0 - dy;
  96. handle_drag(clamp(value() + step * dy));
  97. return 1;
  98. }
  99. }
  100. int X, Y, S;
  101. get_knob_dimensions ( &X, &Y, &S );
  102. return Fl_Dial_Base::handle( m, X, Y, S, S );
  103. }
  104. void
  105. Fl_Dial::draw ( void )
  106. {
  107. int X, Y, S;
  108. get_knob_dimensions ( &X, &Y, &S);
  109. draw_box();
  110. draw_label();
  111. double angle = ( angle2() - angle1() ) * ( value() - minimum()) / ( maximum() - minimum() ) + angle1();
  112. int t = type();
  113. if ( t == PIXMAP_DIAL )
  114. {
  115. Fl_Image *im = pixmap();
  116. if ( !im )
  117. im = Fl_Dial::_default_image;
  118. if ( im )
  119. {
  120. fl_push_clip( x(), y(), w(), h() );
  121. int knob_width = im->h();
  122. const int frames = im->w() / im->h();
  123. const int index = (int)( ( frames - 1 ) * ( value() - minimum()) / ( maximum() - minimum() ));
  124. /* if ( ( damage() == FL_DAMAGE_ALL ) || */
  125. /* ( ( damage() & FL_DAMAGE_EXPOSE ) && */
  126. /* index != _last_pixmap_index ) ) */
  127. {
  128. /* FIXME: Why doesn't this work? */
  129. /* if ( ! active_r() ) */
  130. /* { */
  131. /* im->inactive(); */
  132. /* } */
  133. if ( w() >= knob_width )
  134. {
  135. im->draw( x() + ( w() / 2 ) - ( knob_width / 2 ),
  136. y() + ( h() / 2 ) - ( knob_width / 2 ),
  137. knob_width,
  138. knob_width,
  139. knob_width * index,
  140. 0 );
  141. }
  142. else
  143. {
  144. // while ( knob_width > w() )
  145. knob_width = w();
  146. image_node *i;
  147. Fl_Image *scaled = 0;
  148. for ( i = _first; i; i = i->next )
  149. {
  150. if ( i->original == im &&
  151. i->scaled && i->scaled->h() == knob_width )
  152. {
  153. scaled = i->scaled;
  154. break;
  155. }
  156. }
  157. if ( ! scaled )
  158. {
  159. scaled = im->copy( knob_width * frames, knob_width );
  160. i = new image_node();
  161. i->original = im;
  162. i->scaled = scaled;
  163. i->next = _first;
  164. _first = i;
  165. }
  166. scaled->draw( x() + ( w() / 2 ) - ( knob_width / 2 ),
  167. y() + ( h() / 2 ) - ( knob_width / 2 ),
  168. knob_width,
  169. knob_width,
  170. knob_width * index,
  171. 0 );
  172. }
  173. _last_pixmap_index = index;
  174. }
  175. fl_pop_clip();
  176. goto done;
  177. }
  178. else
  179. /* draw as plastic dial instead when image is missing */
  180. t = PLASTIC_DIAL;
  181. }
  182. if ( t == ARC_DIAL )
  183. {
  184. /* fl_line_style( FL_SOLID, 0 ); */
  185. if ( type() == ARC_DIAL )
  186. fl_draw_box( box(), X, Y, S, S, color() );
  187. /* shrink a bit */
  188. X += S / 16.0;
  189. Y += S / 16.0;
  190. S -= S / 8;
  191. fl_line_style( FL_SOLID, S / 6 );
  192. /* background arc */
  193. fl_color( fl_darker( color() ) );
  194. fl_arc( X, Y, S, S, 270 - angle1(), 270 - angle2() );
  195. /* foreground arc */
  196. fl_color( selection_color() );
  197. fl_arc( X, Y, S, S, 270 - angle1(), 270 - angle );
  198. fl_line_style( FL_SOLID, 0 );
  199. fl_color( fl_contrast( labelcolor(), color() ) );
  200. }
  201. else if ( t == PLASTIC_DIAL || t == BURNISHED_DIAL )
  202. {
  203. draw_knob(t);
  204. draw_cursor( X, Y, S);
  205. }
  206. done:
  207. if ( _mouse_inside == this )
  208. {
  209. /* TODO: Make this optional */
  210. char s[10];
  211. fl_font( FL_HELVETICA, 10 );
  212. snprintf( s, sizeof( s ), "%.1f", value() );
  213. fl_color( FL_FOREGROUND_COLOR );
  214. fl_draw( s, X, Y, S, S, FL_ALIGN_CENTER );
  215. }
  216. }
  217. void
  218. Fl_Dial::get_knob_dimensions ( int *X, int *Y, int *S )
  219. {
  220. int ox, oy, ww, hh, side;
  221. ox = x();
  222. oy = y();
  223. ww = w();
  224. hh = h();
  225. if (ww > hh)
  226. {
  227. side = hh;
  228. ox = ox + (ww - side) / 2;
  229. }
  230. else
  231. {
  232. side = ww;
  233. oy = oy + (hh - side) / 2;
  234. }
  235. side = w() > h() ? hh : ww;
  236. *X = ox;
  237. *Y = oy;
  238. *S = side;
  239. }
  240. void
  241. Fl_Dial::draw_cursor ( int ox, int oy, int side )
  242. {
  243. double angle;
  244. // fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_BLACK, .7f));
  245. angle = ( angle2() - angle1() ) * ( value() - minimum()) / ( maximum() - minimum() ) + angle1();
  246. fl_color( fl_contrast( selection_color(), FL_BACKGROUND_COLOR ) );
  247. fl_line_style( FL_SOLID, side / 8 );
  248. const int d = 6;
  249. /* account for edge conditions */
  250. angle = angle < angle1() + d ? angle1() + d : angle;
  251. angle = angle > angle2() - d ? angle2() - d : angle;
  252. ox += side * 0.15;
  253. oy += side * 0.15;
  254. side -= side * 0.15 * 2;
  255. fl_arc( ox, oy, side, side, 270 - (angle - d), 270 - (angle + d) );
  256. // fl_arc( ox, oy, side, side, 270 - (angle + d), 270 - (angle - d) );
  257. fl_line_style( FL_SOLID, 0 );
  258. }
  259. void
  260. Fl_Dial::draw_knob ( int type )
  261. {
  262. int ox, oy, ww, hh, side;
  263. get_knob_dimensions ( &ox, &oy, &side );
  264. ww = w();
  265. hh = h();
  266. draw_label();
  267. fl_clip(ox, oy, ww, hh);
  268. int o = side * 0.15;
  269. // background
  270. /* fl_color(FL_BACKGROUND_COLOR); */
  271. /* fl_rectf(ox, oy, side, side); */
  272. /* scale color */
  273. if ( damage() & FL_DAMAGE_ALL )
  274. {
  275. fl_color(fl_color_average(color(), FL_BACKGROUND2_COLOR, .6));
  276. fl_pie(ox + 1, oy + 3, side - 2, side - 12, 0, 360);
  277. // scale
  278. draw_scale(ox, oy, side);
  279. }
  280. Fl_Color c = active_r() ? fl_color_average(FL_BACKGROUND_COLOR, FL_WHITE, .7) : FL_INACTIVE_COLOR;
  281. if ( type == BURNISHED_DIAL )
  282. {
  283. burnished_oval_box( ox + o, oy + o, side - (o*2), side - (o*2), c );
  284. }
  285. else
  286. {
  287. fl_color(FL_BACKGROUND_COLOR);
  288. fl_pie(ox + o, oy + o, side - (o*2), side - (o*2), 0, 360);
  289. // shadow
  290. fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_BLACK, .8f));
  291. fl_pie(ox + o + 2, oy + o + 3, side - o*2, side - o*2, 0, 360);
  292. /* fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_BLACK, .2f)); */
  293. /* fl_pie(ox + o + 4, oy + o + 5, side - o*2, side - o*2, 0, 360); */
  294. // knob edge
  295. fl_color( c);
  296. fl_arc(ox + o, oy + o, side - o*2, side - o*2, 0, 360);
  297. fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_WHITE, .6));
  298. fl_pie(ox + o, oy + o, side - o*2, side - o*2, 0, 360);
  299. }
  300. fl_pop_clip();
  301. }
  302. void
  303. Fl_Dial::draw_scale ( int ox, int oy, int side )
  304. {
  305. float x1, y1, x2, y2, rds, cx, cy, ca, sa;
  306. rds = side / 2;
  307. cx = ox + side / 2;
  308. cy = oy + side / 2;
  309. if (_scaleticks == 0)
  310. return;
  311. double a_step = (10.0 * 3.14159 / 6.0) / _scaleticks;
  312. double a_orig = -(3.14159 / 3.0);
  313. for (int a = 0; a <= _scaleticks; a++)
  314. {
  315. double na = a_orig + a * a_step;
  316. ca = cos(na);
  317. sa = sin(na);
  318. x1 = cx + (rds) * ca;
  319. y1 = cy - (rds) * sa;
  320. x2 = cx + (rds - 6) * ca;
  321. y2 = cy - (rds - 6) * sa;
  322. fl_color(FL_BACKGROUND_COLOR);
  323. fl_line(x1, y1, x2, y2);
  324. }
  325. }
  326. void
  327. Fl_Dial::scaleticks ( int tck )
  328. {
  329. _scaleticks = tck;
  330. if (_scaleticks < 0)
  331. _scaleticks = 0;
  332. if (_scaleticks > 31)
  333. _scaleticks = 31;
  334. if (visible())
  335. damage(FL_DAMAGE_ALL);
  336. }