DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 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 | |
| 28 | // Subclass of Fl_Button where the "box" indicates whether it is |
| 29 | // pushed or not, and the "down box" is drawn small and square on |
| 30 | // the left to indicate the current state. |
| 31 | |
| 32 | // The default down_box of zero draws a rectangle designed to look |
| 33 | // just like Flame's buttons. |
| 34 | |
| 35 | #include <FL/Fl.H> |
| 36 | #include <FL/Fl_Light_Button.H> |
| 37 | #include <FL/fl_draw.H> |
| 38 | #include "flstring.h" |
| 39 | |
| 40 | void Fl_Light_Button::draw() { |
| 41 | if (box()) draw_box(this==Fl::pushed() ? fl_down(box()) : box(), color()); |
| 42 | Fl_Color col = value() ? (active_r() ? selection_color() : |
| 43 | fl_inactive(selection_color())) : color(); |
| 44 | int W; |
| 45 | int dx, dy; |
| 46 | |
| 47 | W = labelsize(); |
| 48 | dx = Fl::box_dx(box()) + 2; |
| 49 | dy = (h() - W) / 2; |
| 50 | // if (dy < 0) dy = 0; // neg. offset o.k. for vertical centering |
| 51 | |
| 52 | if (down_box()) { |
| 53 | // draw other down_box() styles: |
| 54 | switch (down_box()) { |
| 55 | case FL_DOWN_BOX : |
| 56 | case FL_UP_BOX : |
| 57 | case _FL_PLASTIC_DOWN_BOX : |
| 58 | case _FL_PLASTIC_UP_BOX : |
| 59 | // Check box... |
| 60 | draw_box(down_box(), x()+dx, y()+dy, W, W, FL_BACKGROUND2_COLOR); |
| 61 | if (value()) { |
| 62 | if (Fl::scheme() && !strcmp(Fl::scheme(), "gtk+")) { |
| 63 | fl_color(FL_SELECTION_COLOR); |
| 64 | } else { |
| 65 | fl_color(col); |
| 66 | } |
| 67 | int tx = x() + dx + 3; |
| 68 | int tw = W - 6; |
| 69 | int d1 = tw/3; |
| 70 | int d2 = tw-d1; |
| 71 | int ty = y() + dy + (W+d2)/2-d1-2; |
| 72 | for (int n = 0; n < 3; n++, ty++) { |
| 73 | fl_line(tx, ty, tx+d1, ty+d1); |
| 74 | fl_line(tx+d1, ty+d1, tx+tw-1, ty+d1-d2+1); |
| 75 | } |
| 76 | } |
| 77 | break; |
| 78 | case _FL_ROUND_DOWN_BOX : |
| 79 | case _FL_ROUND_UP_BOX : |
| 80 | // Radio button... |
| 81 | draw_box(down_box(), x()+dx, y()+dy, W, W, FL_BACKGROUND2_COLOR); |
| 82 | if (value()) { |
| 83 | int tW = (W - Fl::box_dw(down_box())) / 2 + 1; |
| 84 | if ((W - tW) & 1) tW++; // Make sure difference is even to center |
| 85 | int tdx = dx + (W - tW) / 2; |
| 86 | int tdy = dy + (W - tW) / 2; |
| 87 | |
| 88 | if (Fl::scheme() && !strcmp(Fl::scheme(), "gtk+")) { |
| 89 | fl_color(FL_SELECTION_COLOR); |
| 90 | tW --; |
| 91 | fl_pie(x() + tdx - 1, y() + tdy - 1, tW + 3, tW + 3, 0.0, 360.0); |
| 92 | fl_arc(x() + tdx - 1, y() + tdy - 1, tW + 3, tW + 3, 0.0, 360.0); |
| 93 | fl_color(fl_color_average(FL_WHITE, FL_SELECTION_COLOR, 0.2f)); |
| 94 | } else fl_color(col); |
| 95 | |
| 96 | switch (tW) { |
| 97 | // Larger circles draw fine... |
| 98 | default : |
| 99 | fl_pie(x() + tdx, y() + tdy, tW, tW, 0.0, 360.0); |
| 100 | break; |
| 101 | |
| 102 | // Small circles don't draw well on many systems... |
| 103 | case 6 : |
| 104 | fl_rectf(x() + tdx + 2, y() + tdy, tW - 4, tW); |
| 105 | fl_rectf(x() + tdx + 1, y() + tdy + 1, tW - 2, tW - 2); |
| 106 | fl_rectf(x() + tdx, y() + tdy + 2, tW, tW - 4); |
| 107 | break; |
| 108 | |
| 109 | case 5 : |
| 110 | case 4 : |
| 111 | case 3 : |
| 112 | fl_rectf(x() + tdx + 1, y() + tdy, tW - 2, tW); |
| 113 | fl_rectf(x() + tdx, y() + tdy + 1, tW, tW - 2); |
| 114 | break; |
| 115 | |
| 116 | case 2 : |
| 117 | case 1 : |
| 118 | fl_rectf(x() + tdx, y() + tdy, tW, tW); |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | if (Fl::scheme() && !strcmp(Fl::scheme(), "gtk+")) { |
| 123 | fl_color(fl_color_average(FL_WHITE, FL_SELECTION_COLOR, 0.5)); |
| 124 | fl_arc(x() + tdx, y() + tdy, tW + 1, tW + 1, 60.0, 180.0); |
| 125 | } |
| 126 | } |
| 127 | break; |
| 128 | default : |
| 129 | draw_box(down_box(), x()+dx, y()+dy, W, W, col); |
| 130 | break; |
| 131 | } |
| 132 | } else { |
| 133 | // if down_box() is zero, draw light button style: |
| 134 | int hh = h()-2*dy - 2; |
| 135 | int ww = W/2+1; |
| 136 | int xx = dx; |
| 137 | if (w()<ww+2*xx) xx = (w()-ww)/2; |
| 138 | if (Fl::scheme() && !strcmp(Fl::scheme(), "plastic")) { |
| 139 | col = active_r() ? selection_color() : fl_inactive(selection_color()); |
| 140 | fl_color(value() ? col : fl_color_average(col, FL_BLACK, 0.5f)); |
| 141 | fl_pie(x()+xx, y()+dy+1, ww, hh, 0, 360); |
| 142 | } else { |
| 143 | draw_box(FL_THIN_DOWN_BOX, x()+xx, y()+dy+1, ww, hh, col); |
| 144 | } |
| 145 | dx = (ww + 2 * dx - W) / 2; |
| 146 | } |
| 147 | draw_label(x()+W+2*dx, y(), w()-W-2*dx, h()); |
| 148 | if (Fl::focus() == this) draw_focus(); |
| 149 | } |
| 150 | |
| 151 | int Fl_Light_Button::handle(int event) { |
| 152 | switch (event) { |
| 153 | case FL_RELEASE: |
| 154 | if (box()) redraw(); |
| 155 | default: |
| 156 | return Fl_Button::handle(event); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | Creates a new Fl_Light_Button widget using the given |
| 162 | position, size, and label string. |
| 163 | <P>The destructor deletes the check button. |
| 164 | */ |
| 165 | Fl_Light_Button::Fl_Light_Button(int X, int Y, int W, int H, const char* l) |
| 166 | : Fl_Button(X, Y, W, H, l) { |
| 167 | type(FL_TOGGLE_BUTTON); |
| 168 | selection_color(FL_YELLOW); |
| 169 | align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE); |
| 170 | } |
| 171 | |
| 172 | // |
| 173 | // End of "$Id: Fl_Light_Button.cxx 7903 2010-11-28 21:06:39Z matt $". |
| 174 | // |