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.

173 lines
5.3KB

  1. //
  2. // "$Id: Fl_Light_Button.cxx 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // Lighted button widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. // Subclass of Fl_Button where the "box" indicates whether it is
  28. // pushed or not, and the "down box" is drawn small and square on
  29. // the left to indicate the current state.
  30. // The default down_box of zero draws a rectangle designed to look
  31. // just like Flame's buttons.
  32. #include <FL/Fl.H>
  33. #include <FL/Fl_Light_Button.H>
  34. #include <FL/fl_draw.H>
  35. #include "flstring.h"
  36. void Fl_Light_Button::draw() {
  37. if (box()) draw_box(this==Fl::pushed() ? fl_down(box()) : box(), color());
  38. Fl_Color col = value() ? (active_r() ? selection_color() :
  39. fl_inactive(selection_color())) : color();
  40. int W;
  41. int dx, dy;
  42. W = labelsize();
  43. dx = Fl::box_dx(box()) + 2;
  44. dy = (h() - W) / 2;
  45. // if (dy < 0) dy = 0; // neg. offset o.k. for vertical centering
  46. if (down_box()) {
  47. // draw other down_box() styles:
  48. switch (down_box()) {
  49. case FL_DOWN_BOX :
  50. case FL_UP_BOX :
  51. // Check box...
  52. draw_box(down_box(), x()+dx, y()+dy, W, W, FL_BACKGROUND2_COLOR);
  53. if (value()) {
  54. if (Fl::scheme() && !strcmp(Fl::scheme(), "gtk+")) {
  55. fl_color(FL_SELECTION_COLOR);
  56. } else {
  57. fl_color(col);
  58. }
  59. int tx = x() + dx + 3;
  60. int tw = W - 6;
  61. int d1 = tw/3;
  62. int d2 = tw-d1;
  63. int ty = y() + dy + (W+d2)/2-d1-2;
  64. for (int n = 0; n < 3; n++, ty++) {
  65. fl_line(tx, ty, tx+d1, ty+d1);
  66. fl_line(tx+d1, ty+d1, tx+tw-1, ty+d1-d2+1);
  67. }
  68. }
  69. break;
  70. case _FL_ROUND_DOWN_BOX :
  71. case _FL_ROUND_UP_BOX :
  72. // Radio button...
  73. draw_box(down_box(), x()+dx, y()+dy, W, W, FL_BACKGROUND2_COLOR);
  74. if (value()) {
  75. int tW = (W - Fl::box_dw(down_box())) / 2 + 1;
  76. if ((W - tW) & 1) tW++; // Make sure difference is even to center
  77. int tdx = dx + (W - tW) / 2;
  78. int tdy = dy + (W - tW) / 2;
  79. if (Fl::scheme() && !strcmp(Fl::scheme(), "gtk+")) {
  80. fl_color(FL_SELECTION_COLOR);
  81. tW --;
  82. fl_pie(x() + tdx - 1, y() + tdy - 1, tW + 3, tW + 3, 0.0, 360.0);
  83. fl_arc(x() + tdx - 1, y() + tdy - 1, tW + 3, tW + 3, 0.0, 360.0);
  84. fl_color(fl_color_average(FL_WHITE, FL_SELECTION_COLOR, 0.2f));
  85. } else fl_color(col);
  86. switch (tW) {
  87. // Larger circles draw fine...
  88. default :
  89. fl_pie(x() + tdx, y() + tdy, tW, tW, 0.0, 360.0);
  90. break;
  91. // Small circles don't draw well on many systems...
  92. case 6 :
  93. fl_rectf(x() + tdx + 2, y() + tdy, tW - 4, tW);
  94. fl_rectf(x() + tdx + 1, y() + tdy + 1, tW - 2, tW - 2);
  95. fl_rectf(x() + tdx, y() + tdy + 2, tW, tW - 4);
  96. break;
  97. case 5 :
  98. case 4 :
  99. case 3 :
  100. fl_rectf(x() + tdx + 1, y() + tdy, tW - 2, tW);
  101. fl_rectf(x() + tdx, y() + tdy + 1, tW, tW - 2);
  102. break;
  103. case 2 :
  104. case 1 :
  105. fl_rectf(x() + tdx, y() + tdy, tW, tW);
  106. break;
  107. }
  108. if (Fl::scheme() && !strcmp(Fl::scheme(), "gtk+")) {
  109. fl_color(fl_color_average(FL_WHITE, FL_SELECTION_COLOR, 0.5));
  110. fl_arc(x() + tdx, y() + tdy, tW + 1, tW + 1, 60.0, 180.0);
  111. }
  112. }
  113. break;
  114. default :
  115. draw_box(down_box(), x()+dx, y()+dy, W, W, col);
  116. break;
  117. }
  118. } else {
  119. // if down_box() is zero, draw light button style:
  120. int hh = h()-2*dy - 2;
  121. int ww = W/2+1;
  122. int xx = dx;
  123. if (w()<ww+2*xx) xx = (w()-ww)/2;
  124. /* if (Fl::scheme() && !strcmp(Fl::scheme(), "plastic")) { */
  125. /* col = active_r() ? selection_color() : fl_inactive(selection_color()); */
  126. /* fl_color(value() ? col : fl_color_average(col, FL_BLACK, 0.5f)); */
  127. /* fl_pie(x()+xx, y()+dy+1, ww, hh, 0, 360); */
  128. /* } else { */
  129. draw_box(FL_THIN_DOWN_BOX, x()+xx, y()+dy+1, ww, hh, col);
  130. /* } */
  131. dx = (ww + 2 * dx - W) / 2;
  132. }
  133. draw_label(x()+W+2*dx, y(), w()-W-2*dx, h());
  134. if (Fl::focus() == this) draw_focus();
  135. }
  136. int Fl_Light_Button::handle(int event) {
  137. switch (event) {
  138. case FL_RELEASE:
  139. if (box()) redraw();
  140. default:
  141. return Fl_Button::handle(event);
  142. }
  143. }
  144. /**
  145. Creates a new Fl_Light_Button widget using the given
  146. position, size, and label string.
  147. <P>The destructor deletes the check button.
  148. */
  149. Fl_Light_Button::Fl_Light_Button(int X, int Y, int W, int H, const char* l)
  150. : Fl_Button(X, Y, W, H, l) {
  151. type(FL_TOGGLE_BUTTON);
  152. selection_color(FL_YELLOW);
  153. align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
  154. }
  155. //
  156. // End of "$Id: Fl_Light_Button.cxx 7903 2010-11-28 21:06:39Z matt $".
  157. //