fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_Light_Button.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_Light_Button.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Lighted button widget for the Fast Light Tool Kit (FLTK).
00005 //
00006 // Copyright 1998-2010 by Bill Spitzak and others.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 
00028 // Subclass of Fl_Button where the "box" indicates whether it is
00029 // pushed or not, and the "down box" is drawn small and square on
00030 // the left to indicate the current state.
00031 
00032 // The default down_box of zero draws a rectangle designed to look
00033 // just like Flame's buttons.
00034 
00035 #include <FL/Fl.H>
00036 #include <FL/Fl_Light_Button.H>
00037 #include <FL/fl_draw.H>
00038 #include "flstring.h"
00039 
00040 void Fl_Light_Button::draw() {
00041   if (box()) draw_box(this==Fl::pushed() ? fl_down(box()) : box(), color());
00042   Fl_Color col = value() ? (active_r() ? selection_color() :
00043                             fl_inactive(selection_color())) : color();
00044   int W;
00045   int dx, dy;
00046 
00047   W  = labelsize();
00048   dx = Fl::box_dx(box()) + 2;
00049   dy = (h() - W) / 2;
00050   // if (dy < 0) dy = 0;         // neg. offset o.k. for vertical centering
00051 
00052   if (down_box()) {
00053     // draw other down_box() styles:
00054     switch (down_box()) {
00055       case FL_DOWN_BOX :
00056       case FL_UP_BOX :
00057       case _FL_PLASTIC_DOWN_BOX :
00058       case _FL_PLASTIC_UP_BOX :
00059         // Check box...
00060         draw_box(down_box(), x()+dx, y()+dy, W, W, FL_BACKGROUND2_COLOR);
00061         if (value()) {
00062           if (Fl::scheme() && !strcmp(Fl::scheme(), "gtk+")) {
00063             fl_color(FL_SELECTION_COLOR);
00064           } else {
00065             fl_color(col);
00066           }
00067           int tx = x() + dx + 3;
00068           int tw = W - 6;
00069           int d1 = tw/3;
00070           int d2 = tw-d1;
00071           int ty = y() + dy + (W+d2)/2-d1-2;
00072           for (int n = 0; n < 3; n++, ty++) {
00073             fl_line(tx, ty, tx+d1, ty+d1);
00074             fl_line(tx+d1, ty+d1, tx+tw-1, ty+d1-d2+1);
00075           }
00076         }
00077         break;
00078       case _FL_ROUND_DOWN_BOX :
00079       case _FL_ROUND_UP_BOX :
00080         // Radio button...
00081         draw_box(down_box(), x()+dx, y()+dy, W, W, FL_BACKGROUND2_COLOR);
00082         if (value()) {
00083           int tW = (W - Fl::box_dw(down_box())) / 2 + 1;
00084           if ((W - tW) & 1) tW++; // Make sure difference is even to center
00085           int tdx = dx + (W - tW) / 2;
00086           int tdy = dy + (W - tW) / 2;
00087 
00088           if (Fl::scheme() && !strcmp(Fl::scheme(), "gtk+")) {
00089             fl_color(FL_SELECTION_COLOR);
00090             tW --;
00091             fl_pie(x() + tdx - 1, y() + tdy - 1, tW + 3, tW + 3, 0.0, 360.0);
00092             fl_arc(x() + tdx - 1, y() + tdy - 1, tW + 3, tW + 3, 0.0, 360.0);
00093             fl_color(fl_color_average(FL_WHITE, FL_SELECTION_COLOR, 0.2f));
00094           } else fl_color(col);
00095 
00096           switch (tW) {
00097             // Larger circles draw fine...
00098             default :
00099               fl_pie(x() + tdx, y() + tdy, tW, tW, 0.0, 360.0);
00100               break;
00101 
00102             // Small circles don't draw well on many systems...
00103             case 6 :
00104               fl_rectf(x() + tdx + 2, y() + tdy, tW - 4, tW);
00105               fl_rectf(x() + tdx + 1, y() + tdy + 1, tW - 2, tW - 2);
00106               fl_rectf(x() + tdx, y() + tdy + 2, tW, tW - 4);
00107               break;
00108 
00109             case 5 :
00110             case 4 :
00111             case 3 :
00112               fl_rectf(x() + tdx + 1, y() + tdy, tW - 2, tW);
00113               fl_rectf(x() + tdx, y() + tdy + 1, tW, tW - 2);
00114               break;
00115 
00116             case 2 :
00117             case 1 :
00118               fl_rectf(x() + tdx, y() + tdy, tW, tW);
00119               break;
00120           }
00121 
00122           if (Fl::scheme() && !strcmp(Fl::scheme(), "gtk+")) {
00123             fl_color(fl_color_average(FL_WHITE, FL_SELECTION_COLOR, 0.5));
00124             fl_arc(x() + tdx, y() + tdy, tW + 1, tW + 1, 60.0, 180.0);
00125           }
00126         }
00127         break;
00128       default :
00129         draw_box(down_box(), x()+dx, y()+dy, W, W, col);
00130         break;
00131     }
00132   } else {
00133     // if down_box() is zero, draw light button style:
00134     int hh = h()-2*dy - 2;
00135     int ww = W/2+1;
00136     int xx = dx;
00137     if (w()<ww+2*xx) xx = (w()-ww)/2;
00138     if (Fl::scheme() && !strcmp(Fl::scheme(), "plastic")) {
00139       col = active_r() ? selection_color() : fl_inactive(selection_color());
00140       fl_color(value() ? col : fl_color_average(col, FL_BLACK, 0.5f));
00141       fl_pie(x()+xx, y()+dy+1, ww, hh, 0, 360);
00142     } else {
00143       draw_box(FL_THIN_DOWN_BOX, x()+xx, y()+dy+1, ww, hh, col);
00144     }
00145     dx = (ww + 2 * dx - W) / 2;
00146   }
00147   draw_label(x()+W+2*dx, y(), w()-W-2*dx, h());
00148   if (Fl::focus() == this) draw_focus();
00149 }
00150 
00151 int Fl_Light_Button::handle(int event) {
00152   switch (event) {
00153   case FL_RELEASE:
00154     if (box()) redraw();
00155   default:
00156     return Fl_Button::handle(event);
00157   }
00158 }
00159 
00165 Fl_Light_Button::Fl_Light_Button(int X, int Y, int W, int H, const char* l)
00166 : Fl_Button(X, Y, W, H, l) {
00167   type(FL_TOGGLE_BUTTON);
00168   selection_color(FL_YELLOW);
00169   align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
00170 }
00171 
00172 //
00173 // End of "$Id: Fl_Light_Button.cxx 7903 2010-11-28 21:06:39Z matt $".
00174 //