blob: 6f3b06ffeaa2563fd91e99f53073a2c90cdae3a6 [file] [log] [blame]
DRC2ff39b82011-07-28 08:38:59 +00001//
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.
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#include <FL/Fl.H>
29#include <FL/Fl_Choice.H>
30#include <FL/fl_draw.H>
31#include "flstring.h"
32
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.
36
37void Fl_Choice::draw() {
38 int dx = Fl::box_dx(FL_DOWN_BOX);
39 int dy = Fl::box_dy(FL_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;
47
48 if (Fl::scheme()) {
49 draw_box(FL_UP_BOX, color());
50
51 fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
52 if (!strcmp(Fl::scheme(), "plastic")) {
53 // Show larger up/down arrows...
54 fl_polygon(x1, y1 + 3, x1 + w1, y1 + w1 + 3, x1 + 2 * w1, y1 + 3);
55 fl_polygon(x1, y1 + 1, x1 + w1, y1 - w1 + 1, x1 + 2 * w1, y1 + 1);
56 } else {
57 // Show smaller up/down arrows with a divider...
58 x1 = x() + w() - 13 - dx;
59 y1 = y() + h() / 2;
60 fl_polygon(x1, y1 - 2, x1 + 3, y1 - 5, x1 + 6, y1 - 2);
61 fl_polygon(x1, y1 + 2, x1 + 3, y1 + 5, x1 + 6, y1 + 2);
62
63 fl_color(fl_darker(color()));
64 fl_yxline(x1 - 7, y1 - 8, y1 + 8);
65
66 fl_color(fl_lighter(color()));
67 fl_yxline(x1 - 6, y1 - 8, y1 + 8);
68 }
69 } else {
70 if (fl_contrast(textcolor(), FL_BACKGROUND2_COLOR) == textcolor()) {
71 draw_box(FL_DOWN_BOX, FL_BACKGROUND2_COLOR);
72 } else {
73 draw_box(FL_DOWN_BOX, fl_lighter(color()));
74 }
75 draw_box(FL_UP_BOX,X,Y,W,H,color());
76
77 fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
78 fl_polygon(x1, y1, x1 + w1, y1 + w1, x1 + 2 * w1, y1);
79 }
80
81 W += 2 * dx;
82
83 if (mvalue()) {
84 Fl_Menu_Item m = *mvalue();
85 if (active_r()) m.activate(); else m.deactivate();
86
87 // ERCO
88 int xx = x() + dx, yy = y() + dy + 1, ww = w() - W, hh = H - 2;
89
90 fl_push_clip(xx, yy, ww, hh);
91
92 if ( Fl::scheme()) {
93 Fl_Label l;
94 l.value = m.text;
95 l.image = 0;
96 l.deimage = 0;
97 l.type = m.labeltype_;
98 l.font = m.labelsize_ || m.labelfont_ ? m.labelfont_ : textfont();
99 l.size = m.labelsize_ ? m.labelsize_ : textsize();
100 l.color= m.labelcolor_ ? m.labelcolor_ : textcolor();
101 if (!m.active()) l.color = fl_inactive((Fl_Color)l.color);
102 fl_draw_shortcut = 2; // hack value to make '&' disappear
103 l.draw(xx+3, yy, ww>6 ? ww-6 : 0, hh, FL_ALIGN_LEFT);
104 fl_draw_shortcut = 0;
105 if ( Fl::focus() == this ) draw_focus(box(), xx, yy, ww, hh);
106 }
107 else {
108 fl_draw_shortcut = 2; // hack value to make '&' disappear
109 m.draw(xx, yy, ww, hh, this, Fl::focus() == this);
110 fl_draw_shortcut = 0;
111 }
112
113 fl_pop_clip();
114 }
115
116 draw_label();
117}
118
119/**
120 Create a new Fl_Choice widget using the given position, size and label string.
121 The default boxtype is \c FL_UP_BOX.
122
123 The constructor sets menu() to NULL.
124 See Fl_Menu_ for the methods to set or change the menu.
125
126 \param[in] X, Y, W, H position and size of the widget
127 \param[in] L widget label, default is no label
128 */
129Fl_Choice::Fl_Choice(int X, int Y, int W, int H, const char *L)
130: Fl_Menu_(X,Y,W,H,L) {
131 align(FL_ALIGN_LEFT);
132 when(FL_WHEN_RELEASE);
133 textfont(FL_HELVETICA);
134 box(FL_FLAT_BOX);
135 down_box(FL_BORDER_BOX);
136}
137
138/**
139 Sets the currently selected value using a pointer to menu item.
140 Changing the selected value causes a redraw().
141 \param[in] v pointer to menu item in the menu item array.
142 \returns non-zero if the new value is different to the old one.
143 */
144int Fl_Choice::value(const Fl_Menu_Item *v) {
145 if (!Fl_Menu_::value(v)) return 0;
146 redraw();
147 return 1;
148}
149
150/**
151 Sets the currently selected value using the index into the menu item array.
152 Changing the selected value causes a redraw().
153 \param[in] v index of value in the menu item array.
154 \returns non-zero if the new value is different to the old one.
155 */
156int Fl_Choice::value(int v) {
157 if (v == -1) return value((const Fl_Menu_Item *)0);
158 if (v < 0 || v >= (size() - 1)) return 0;
159 if (!Fl_Menu_::value(v)) return 0;
160 redraw();
161 return 1;
162}
163
164int Fl_Choice::handle(int e) {
165 if (!menu() || !menu()->text) return 0;
166 const Fl_Menu_Item* v;
167 switch (e) {
168 case FL_ENTER:
169 case FL_LEAVE:
170 return 1;
171
172 case FL_KEYBOARD:
173 if (Fl::event_key() != ' ' ||
174 (Fl::event_state() & (FL_SHIFT | FL_CTRL | FL_ALT | FL_META))) return 0;
175 case FL_PUSH:
176 if (Fl::visible_focus()) Fl::focus(this);
177 J1:
178 if (Fl::scheme()
179 || fl_contrast(textcolor(), FL_BACKGROUND2_COLOR) != textcolor()) {
180 v = menu()->pulldown(x(), y(), w(), h(), mvalue(), this);
181 } else {
182 // In order to preserve the old look-n-feel of "white" menus,
183 // temporarily override the color() of this widget...
184 Fl_Color c = color();
185 color(FL_BACKGROUND2_COLOR);
186 v = menu()->pulldown(x(), y(), w(), h(), mvalue(), this);
187 color(c);
188 }
189 if (!v || v->submenu()) return 1;
190 if (v != mvalue()) redraw();
191 picked(v);
192 return 1;
193 case FL_SHORTCUT:
194 if (Fl_Widget::test_shortcut()) goto J1;
195 v = menu()->test_shortcut();
196 if (!v) return 0;
197 if (v != mvalue()) redraw();
198 picked(v);
199 return 1;
200 case FL_FOCUS:
201 case FL_UNFOCUS:
202 if (Fl::visible_focus()) {
203 redraw();
204 return 1;
205 } else return 0;
206 default:
207 return 0;
208 }
209}
210
211//
212// End of "$Id: Fl_Choice.cxx 7903 2010-11-28 21:06:39Z matt $".
213//