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_Choice.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_Choice.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Choice 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 #include <FL/Fl.H>
00029 #include <FL/Fl_Choice.H>
00030 #include <FL/fl_draw.H>
00031 #include "flstring.h"
00032 
00033 // Emulates the Forms choice widget.  This is almost exactly the same
00034 // as an Fl_Menu_Button.  The only difference is the appearance of the
00035 // button: it draws the text of the current pick and a down-arrow.
00036 
00037 void Fl_Choice::draw() {
00038   int dx = Fl::box_dx(FL_DOWN_BOX);
00039   int dy = Fl::box_dy(FL_DOWN_BOX);
00040   int H = h() - 2 * dy;
00041   int W = (H > 20) ? 20 : H;
00042   int X = x() + w() - W - dx;
00043   int Y = y() + dy;
00044   int w1 = (W - 4) / 3; if (w1 < 1) w1 = 1;
00045   int x1 = X + (W - 2 * w1 - 1) / 2;
00046   int y1 = Y + (H - w1 - 1) / 2;
00047 
00048   if (Fl::scheme()) {
00049     draw_box(FL_UP_BOX, color());
00050 
00051     fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
00052     if (!strcmp(Fl::scheme(), "plastic")) {
00053       // Show larger up/down arrows...
00054       fl_polygon(x1, y1 + 3, x1 + w1, y1 + w1 + 3, x1 + 2 * w1, y1 + 3);
00055       fl_polygon(x1, y1 + 1, x1 + w1, y1 - w1 + 1, x1 + 2 * w1, y1 + 1);
00056     } else {
00057       // Show smaller up/down arrows with a divider...
00058       x1 = x() + w() - 13 - dx;
00059       y1 = y() + h() / 2;
00060       fl_polygon(x1, y1 - 2, x1 + 3, y1 - 5, x1 + 6, y1 - 2);
00061       fl_polygon(x1, y1 + 2, x1 + 3, y1 + 5, x1 + 6, y1 + 2);
00062 
00063       fl_color(fl_darker(color()));
00064       fl_yxline(x1 - 7, y1 - 8, y1 + 8);
00065 
00066       fl_color(fl_lighter(color()));
00067       fl_yxline(x1 - 6, y1 - 8, y1 + 8);
00068     }
00069   } else {
00070     if (fl_contrast(textcolor(), FL_BACKGROUND2_COLOR) == textcolor()) {
00071       draw_box(FL_DOWN_BOX, FL_BACKGROUND2_COLOR);
00072     } else {
00073       draw_box(FL_DOWN_BOX, fl_lighter(color()));
00074     }
00075     draw_box(FL_UP_BOX,X,Y,W,H,color());
00076 
00077     fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
00078     fl_polygon(x1, y1, x1 + w1, y1 + w1, x1 + 2 * w1, y1);
00079   }
00080 
00081   W += 2 * dx;
00082 
00083   if (mvalue()) {
00084     Fl_Menu_Item m = *mvalue();
00085     if (active_r()) m.activate(); else m.deactivate();
00086 
00087     // ERCO
00088     int xx = x() + dx, yy = y() + dy + 1, ww = w() - W, hh = H - 2;
00089 
00090     fl_push_clip(xx, yy, ww, hh);
00091 
00092     if ( Fl::scheme()) {
00093       Fl_Label l;
00094       l.value = m.text;
00095       l.image = 0;
00096       l.deimage = 0;
00097       l.type = m.labeltype_;
00098       l.font = m.labelsize_ || m.labelfont_ ? m.labelfont_ : textfont();
00099       l.size = m.labelsize_ ? m.labelsize_ : textsize();
00100       l.color= m.labelcolor_ ? m.labelcolor_ : textcolor();
00101       if (!m.active()) l.color = fl_inactive((Fl_Color)l.color);
00102       fl_draw_shortcut = 2; // hack value to make '&' disappear
00103       l.draw(xx+3, yy, ww>6 ? ww-6 : 0, hh, FL_ALIGN_LEFT);
00104       fl_draw_shortcut = 0;
00105       if ( Fl::focus() == this ) draw_focus(box(), xx, yy, ww, hh);
00106     }
00107     else {
00108       fl_draw_shortcut = 2; // hack value to make '&' disappear
00109       m.draw(xx, yy, ww, hh, this, Fl::focus() == this);
00110       fl_draw_shortcut = 0;
00111     }
00112 
00113     fl_pop_clip();
00114   }
00115 
00116   draw_label();
00117 }
00118 
00129 Fl_Choice::Fl_Choice(int X, int Y, int W, int H, const char *L)
00130 : Fl_Menu_(X,Y,W,H,L) {
00131   align(FL_ALIGN_LEFT);
00132   when(FL_WHEN_RELEASE);
00133   textfont(FL_HELVETICA);
00134   box(FL_FLAT_BOX);
00135   down_box(FL_BORDER_BOX);
00136 }
00137 
00144 int Fl_Choice::value(const Fl_Menu_Item *v) {
00145   if (!Fl_Menu_::value(v)) return 0;
00146   redraw();
00147   return 1;
00148 }
00149 
00156 int Fl_Choice::value(int v) {
00157   if (v == -1) return value((const Fl_Menu_Item *)0);
00158   if (v < 0 || v >= (size() - 1)) return 0;
00159   if (!Fl_Menu_::value(v)) return 0;
00160   redraw();
00161   return 1;
00162 }
00163 
00164 int Fl_Choice::handle(int e) {
00165   if (!menu() || !menu()->text) return 0;
00166   const Fl_Menu_Item* v;
00167   switch (e) {
00168   case FL_ENTER:
00169   case FL_LEAVE:
00170     return 1;
00171 
00172   case FL_KEYBOARD:
00173     if (Fl::event_key() != ' ' ||
00174         (Fl::event_state() & (FL_SHIFT | FL_CTRL | FL_ALT | FL_META))) return 0;
00175   case FL_PUSH:
00176     if (Fl::visible_focus()) Fl::focus(this);
00177   J1:
00178     if (Fl::scheme()
00179         || fl_contrast(textcolor(), FL_BACKGROUND2_COLOR) != textcolor()) {
00180       v = menu()->pulldown(x(), y(), w(), h(), mvalue(), this);
00181     } else {
00182       // In order to preserve the old look-n-feel of "white" menus,
00183       // temporarily override the color() of this widget...
00184       Fl_Color c = color();
00185       color(FL_BACKGROUND2_COLOR);
00186       v = menu()->pulldown(x(), y(), w(), h(), mvalue(), this);
00187       color(c);
00188     }
00189     if (!v || v->submenu()) return 1;
00190     if (v != mvalue()) redraw();
00191     picked(v);
00192     return 1;
00193   case FL_SHORTCUT:
00194     if (Fl_Widget::test_shortcut()) goto J1;
00195     v = menu()->test_shortcut();
00196     if (!v) return 0;
00197     if (v != mvalue()) redraw();
00198     picked(v);
00199     return 1;
00200   case FL_FOCUS:
00201   case FL_UNFOCUS:
00202     if (Fl::visible_focus()) {
00203       redraw();
00204       return 1;
00205     } else return 0;
00206   default:
00207     return 0;
00208   }
00209 }
00210 
00211 //
00212 // End of "$Id: Fl_Choice.cxx 7903 2010-11-28 21:06:39Z matt $".
00213 //