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.

458 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. int steps = 16;
  89. if ( Fl::event_ctrl() )
  90. steps = 128;
  91. float step = fabs( maximum() - minimum() ) / (float)steps;
  92. float d = ((float)Fl::event_dy()) * step;
  93. double v = value() + d;
  94. if ( maximum() > minimum() )
  95. {
  96. if ( v < minimum() )
  97. v = minimum();
  98. else if ( v > maximum() )
  99. v = maximum();
  100. }
  101. else
  102. {
  103. if ( v > minimum() )
  104. v = minimum();
  105. else if ( v < maximum() )
  106. v = maximum();
  107. }
  108. value( v );
  109. do_callback();
  110. return 1;
  111. }
  112. }
  113. int X, Y, S;
  114. get_knob_dimensions ( &X, &Y, &S );
  115. return Fl_Dial_Base::handle( m, X, Y, S, S );
  116. }
  117. void
  118. Fl_Dial::draw ( void )
  119. {
  120. int X, Y, S;
  121. get_knob_dimensions ( &X, &Y, &S);
  122. draw_box();
  123. draw_label();
  124. double angle = ( angle2() - angle1() ) * ( value() - minimum()) / ( maximum() - minimum() ) + angle1();
  125. int t = type();
  126. if ( t == PIXMAP_DIAL )
  127. {
  128. Fl_Image *im = pixmap();
  129. if ( !im )
  130. im = Fl_Dial::_default_image;
  131. if ( im )
  132. {
  133. fl_push_clip( x(), y(), w(), h() );
  134. int knob_width = im->h();
  135. const int frames = im->w() / im->h();
  136. const int index = (int)( ( frames - 1 ) * ( value() - minimum()) / ( maximum() - minimum() ));
  137. /* if ( ( damage() == FL_DAMAGE_ALL ) || */
  138. /* ( ( damage() & FL_DAMAGE_EXPOSE ) && */
  139. /* index != _last_pixmap_index ) ) */
  140. {
  141. /* FIXME: Why doesn't this work? */
  142. /* if ( ! active_r() ) */
  143. /* { */
  144. /* im->inactive(); */
  145. /* } */
  146. if ( w() >= knob_width )
  147. {
  148. im->draw( x() + ( w() / 2 ) - ( knob_width / 2 ),
  149. y() + ( h() / 2 ) - ( knob_width / 2 ),
  150. knob_width,
  151. knob_width,
  152. knob_width * index,
  153. 0 );
  154. }
  155. else
  156. {
  157. // while ( knob_width > w() )
  158. knob_width = w();
  159. image_node *i;
  160. Fl_Image *scaled = 0;
  161. for ( i = _first; i; i = i->next )
  162. {
  163. if ( i->original == im &&
  164. i->scaled && i->scaled->h() == knob_width )
  165. {
  166. scaled = i->scaled;
  167. break;
  168. }
  169. }
  170. if ( ! scaled )
  171. {
  172. scaled = im->copy( knob_width * frames, knob_width );
  173. i = new image_node();
  174. i->original = im;
  175. i->scaled = scaled;
  176. i->next = _first;
  177. _first = i;
  178. }
  179. scaled->draw( x() + ( w() / 2 ) - ( knob_width / 2 ),
  180. y() + ( h() / 2 ) - ( knob_width / 2 ),
  181. knob_width,
  182. knob_width,
  183. knob_width * index,
  184. 0 );
  185. }
  186. _last_pixmap_index = index;
  187. }
  188. fl_pop_clip();
  189. goto done;
  190. }
  191. else
  192. /* draw as plastic dial instead when image is missing */
  193. t = PLASTIC_DIAL;
  194. }
  195. if ( t == ARC_DIAL )
  196. {
  197. /* fl_line_style( FL_SOLID, 0 ); */
  198. if ( type() == ARC_DIAL )
  199. fl_draw_box( box(), X, Y, S, S, color() );
  200. /* shrink a bit */
  201. X += S / 16.0;
  202. Y += S / 16.0;
  203. S -= S / 8;
  204. fl_line_style( FL_SOLID, S / 6 );
  205. /* background arc */
  206. fl_color( fl_darker( color() ) );
  207. fl_arc( X, Y, S, S, 270 - angle1(), 270 - angle2() );
  208. /* foreground arc */
  209. fl_color( selection_color() );
  210. fl_arc( X, Y, S, S, 270 - angle1(), 270 - angle );
  211. fl_line_style( FL_SOLID, 0 );
  212. fl_color( fl_contrast( labelcolor(), color() ) );
  213. }
  214. else if ( t == PLASTIC_DIAL || t == BURNISHED_DIAL )
  215. {
  216. draw_knob(t);
  217. draw_cursor( X, Y, S);
  218. }
  219. done:
  220. if ( _mouse_inside == this )
  221. {
  222. /* TODO: Make this optional */
  223. char s[10];
  224. fl_font( FL_HELVETICA, 10 );
  225. snprintf( s, sizeof( s ), "%.1f", value() );
  226. fl_color( FL_FOREGROUND_COLOR );
  227. fl_draw( s, X, Y, S, S, FL_ALIGN_CENTER );
  228. }
  229. }
  230. void
  231. Fl_Dial::get_knob_dimensions ( int *X, int *Y, int *S )
  232. {
  233. int ox, oy, ww, hh, side;
  234. ox = x();
  235. oy = y();
  236. ww = w();
  237. hh = h();
  238. if (ww > hh)
  239. {
  240. side = hh;
  241. ox = ox + (ww - side) / 2;
  242. }
  243. else
  244. {
  245. side = ww;
  246. oy = oy + (hh - side) / 2;
  247. }
  248. side = w() > h() ? hh : ww;
  249. *X = ox;
  250. *Y = oy;
  251. *S = side;
  252. }
  253. void
  254. Fl_Dial::draw_cursor ( int ox, int oy, int side )
  255. {
  256. double angle;
  257. // fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_BLACK, .7f));
  258. angle = ( angle2() - angle1() ) * ( value() - minimum()) / ( maximum() - minimum() ) + angle1();
  259. fl_color( fl_contrast( selection_color(), FL_BACKGROUND_COLOR ) );
  260. fl_line_style( FL_SOLID, side / 8 );
  261. const int d = 6;
  262. /* account for edge conditions */
  263. angle = angle < angle1() + d ? angle1() + d : angle;
  264. angle = angle > angle2() - d ? angle2() - d : angle;
  265. ox += side * 0.15;
  266. oy += side * 0.15;
  267. side -= side * 0.15 * 2;
  268. fl_arc( ox, oy, side, side, 270 - (angle - d), 270 - (angle + d) );
  269. // fl_arc( ox, oy, side, side, 270 - (angle + d), 270 - (angle - d) );
  270. fl_line_style( FL_SOLID, 0 );
  271. }
  272. void
  273. Fl_Dial::draw_knob ( int type )
  274. {
  275. int ox, oy, ww, hh, side;
  276. get_knob_dimensions ( &ox, &oy, &side );
  277. ww = w();
  278. hh = h();
  279. draw_label();
  280. fl_clip(ox, oy, ww, hh);
  281. int o = side * 0.15;
  282. // background
  283. /* fl_color(FL_BACKGROUND_COLOR); */
  284. /* fl_rectf(ox, oy, side, side); */
  285. /* scale color */
  286. if ( damage() & FL_DAMAGE_ALL )
  287. {
  288. fl_color(fl_color_average(color(), FL_BACKGROUND2_COLOR, .6));
  289. fl_pie(ox + 1, oy + 3, side - 2, side - 12, 0, 360);
  290. // scale
  291. draw_scale(ox, oy, side);
  292. }
  293. Fl_Color c = active_r() ? fl_color_average(FL_BACKGROUND_COLOR, FL_WHITE, .7) : FL_INACTIVE_COLOR;
  294. if ( type == BURNISHED_DIAL )
  295. {
  296. burnished_oval_box( ox + o, oy + o, side - (o*2), side - (o*2), c );
  297. }
  298. else
  299. {
  300. fl_color(FL_BACKGROUND_COLOR);
  301. fl_pie(ox + o, oy + o, side - (o*2), side - (o*2), 0, 360);
  302. // shadow
  303. fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_BLACK, .8f));
  304. fl_pie(ox + o + 2, oy + o + 3, side - o*2, side - o*2, 0, 360);
  305. /* fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_BLACK, .2f)); */
  306. /* fl_pie(ox + o + 4, oy + o + 5, side - o*2, side - o*2, 0, 360); */
  307. // knob edge
  308. fl_color( c);
  309. fl_arc(ox + o, oy + o, side - o*2, side - o*2, 0, 360);
  310. fl_color(fl_color_average(FL_BACKGROUND_COLOR, FL_WHITE, .6));
  311. fl_pie(ox + o, oy + o, side - o*2, side - o*2, 0, 360);
  312. }
  313. fl_pop_clip();
  314. }
  315. void
  316. Fl_Dial::draw_scale ( int ox, int oy, int side )
  317. {
  318. float x1, y1, x2, y2, rds, cx, cy, ca, sa;
  319. rds = side / 2;
  320. cx = ox + side / 2;
  321. cy = oy + side / 2;
  322. if (_scaleticks == 0)
  323. return;
  324. double a_step = (10.0 * 3.14159 / 6.0) / _scaleticks;
  325. double a_orig = -(3.14159 / 3.0);
  326. for (int a = 0; a <= _scaleticks; a++)
  327. {
  328. double na = a_orig + a * a_step;
  329. ca = cos(na);
  330. sa = sin(na);
  331. x1 = cx + (rds) * ca;
  332. y1 = cy - (rds) * sa;
  333. x2 = cx + (rds - 6) * ca;
  334. y2 = cy - (rds - 6) * sa;
  335. fl_color(FL_BACKGROUND_COLOR);
  336. fl_line(x1, y1, x2, y2);
  337. }
  338. }
  339. void
  340. Fl_Dial::scaleticks ( int tck )
  341. {
  342. _scaleticks = tck;
  343. if (_scaleticks < 0)
  344. _scaleticks = 0;
  345. if (_scaleticks > 31)
  346. _scaleticks = 31;
  347. if (visible())
  348. damage(FL_DAMAGE_ALL);
  349. }