themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / Fl_Choice.cxx
blob52d1031909648c26fb37fc91dc562585d47b2e5b
1 //
2 // "$Id: Fl_Choice.cxx 7903 2010-11-28 21:06:39Z matt $"
3 //
4 // Choice 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.
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.
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.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 #include <FL/Fl.H>
29 #include <FL/Fl_Choice.H>
30 #include <FL/fl_draw.H>
31 #include "flstring.h"
33 // Emulates the Forms choice widget. This is almost exactly the same
34 // as an Fl_Menu_Button. The only difference is the appearance of the
35 // button: it draws the text of the current pick and a down-arrow.
37 void Fl_Choice::draw() {
38 int dx = Fl::box_dx(down_box());
39 int dy = Fl::box_dy(down_box());
40 int H = h() - 2 * dy;
41 int W = (H > 20) ? 20 : H;
42 int X = x() + w() - W - dx;
43 int Y = y() + dy;
44 int w1 = (W - 4) / 3; if (w1 < 1) w1 = 1;
45 int x1 = X + (W - 2 * w1 - 1) / 2;
46 int y1 = Y + (H - w1 - 1) / 2;
48 /* if (Fl::scheme()) { */
49 draw_box(box(), color());
51 fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
52 // Show larger up/down arrows...
53 fl_polygon(x1, y1 + 3, x1 + w1, y1 + w1 + 3, x1 + 2 * w1, y1 + 3);
54 fl_polygon(x1, y1 + 1, x1 + w1, y1 - w1 + 1, x1 + 2 * w1, y1 + 1);
56 W += 2 * dx;
58 if (mvalue()) {
59 Fl_Menu_Item m = *mvalue();
60 if (active_r()) m.activate(); else m.deactivate();
62 // ERCO
63 int xx = x() + dx, yy = y() + dy + 1, ww = w() - W, hh = H - 2;
65 fl_push_clip(xx, yy, ww, hh);
67 if ( Fl::scheme()) {
68 Fl_Label l;
69 l.value = m.text;
70 l.image = 0;
71 l.deimage = 0;
72 l.type = m.labeltype_;
73 l.font = m.labelsize_ || m.labelfont_ ? m.labelfont_ : textfont();
74 l.size = m.labelsize_ ? m.labelsize_ : textsize();
75 l.color= m.labelcolor_ ? m.labelcolor_ : textcolor();
76 if (!m.active()) l.color = fl_inactive((Fl_Color)l.color);
77 fl_draw_shortcut = 2; // hack value to make '&' disappear
78 l.draw(xx+3, yy, ww>6 ? ww-6 : 0, hh, FL_ALIGN_LEFT);
79 fl_draw_shortcut = 0;
80 if ( Fl::focus() == this ) draw_focus(box(), xx, yy, ww, hh);
82 else {
83 fl_draw_shortcut = 2; // hack value to make '&' disappear
84 m.draw(xx, yy, ww, hh, this, Fl::focus() == this);
85 fl_draw_shortcut = 0;
88 fl_pop_clip();
91 draw_label();
94 /**
95 Create a new Fl_Choice widget using the given position, size and label string.
96 The default boxtype is \c FL_UP_BOX.
98 The constructor sets menu() to NULL.
99 See Fl_Menu_ for the methods to set or change the menu.
101 \param[in] X, Y, W, H position and size of the widget
102 \param[in] L widget label, default is no label
104 Fl_Choice::Fl_Choice(int X, int Y, int W, int H, const char *L)
105 : Fl_Menu_(X,Y,W,H,L) {
106 align(FL_ALIGN_LEFT);
107 when(FL_WHEN_RELEASE);
108 textfont(FL_HELVETICA);
109 box(FL_UP_BOX);
110 down_box(FL_BORDER_BOX);
111 color(FL_BACKGROUND_COLOR);
115 Sets the currently selected value using a pointer to menu item.
116 Changing the selected value causes a redraw().
117 \param[in] v pointer to menu item in the menu item array.
118 \returns non-zero if the new value is different to the old one.
120 int Fl_Choice::value(const Fl_Menu_Item *v) {
121 if (!Fl_Menu_::value(v)) return 0;
122 redraw();
123 return 1;
127 Sets the currently selected value using the index into the menu item array.
128 Changing the selected value causes a redraw().
129 \param[in] v index of value in the menu item array.
130 \returns non-zero if the new value is different to the old one.
132 int Fl_Choice::value(int v) {
133 if (v == -1) return value((const Fl_Menu_Item *)0);
134 if (v < 0 || v >= (size() - 1)) return 0;
135 if (!Fl_Menu_::value(v)) return 0;
136 redraw();
137 return 1;
140 int Fl_Choice::handle(int e) {
141 if (!menu() || !menu()->text) return 0;
142 const Fl_Menu_Item* v;
143 switch (e) {
144 case FL_ENTER:
145 case FL_LEAVE:
146 return 1;
148 case FL_KEYBOARD:
149 if (Fl::event_key() != ' ' ||
150 (Fl::event_state() & (FL_SHIFT | FL_CTRL | FL_ALT | FL_META))) return 0;
151 case FL_PUSH:
152 if (Fl::visible_focus()) Fl::focus(this);
154 v = menu()->pulldown(x(), y(), w(), h(), mvalue(), this);
155 if (!v || v->submenu()) return 1;
156 if (v != mvalue()) redraw();
157 picked(v);
158 return 1;
159 case FL_SHORTCUT:
160 if (Fl_Widget::test_shortcut()) goto J1;
161 v = menu()->test_shortcut();
162 if (!v) return 0;
163 if (v != mvalue()) redraw();
164 picked(v);
165 return 1;
166 case FL_FOCUS:
167 case FL_UNFOCUS:
168 if (Fl::visible_focus()) {
169 redraw();
170 return 1;
171 } else return 0;
172 default:
173 return 0;
178 // End of "$Id: Fl_Choice.cxx 7903 2010-11-28 21:06:39Z matt $".