2 // "$Id: Fl_Choice.cxx 7903 2010-11-28 21:06:39Z matt $"
4 // Choice widget for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2010 by Bill Spitzak and others.
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
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
29 #include <FL/Fl_Choice.H>
30 #include <FL/fl_draw.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());
41 int W
= (H
> 20) ? 20 : H
;
42 int X
= x() + w() - W
- dx
;
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);
59 Fl_Menu_Item m
= *mvalue();
60 if (active_r()) m
.activate(); else m
.deactivate();
63 int xx
= x() + dx
, yy
= y() + dy
+ 1, ww
= w() - W
, hh
= H
- 2;
65 fl_push_clip(xx
, yy
, ww
, hh
);
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
);
80 if ( Fl::focus() == this ) draw_focus(box(), xx
, yy
, ww
, hh
);
83 fl_draw_shortcut
= 2; // hack value to make '&' disappear
84 m
.draw(xx
, yy
, ww
, hh
, this, Fl::focus() == this);
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
);
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;
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;
140 int Fl_Choice::handle(int e
) {
141 if (!menu() || !menu()->text
) return 0;
142 const Fl_Menu_Item
* v
;
149 if (Fl::event_key() != ' ' ||
150 (Fl::event_state() & (FL_SHIFT
| FL_CTRL
| FL_ALT
| FL_META
))) return 0;
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();
160 if (Fl_Widget::test_shortcut()) goto J1
;
161 v
= menu()->test_shortcut();
163 if (v
!= mvalue()) redraw();
168 if (Fl::visible_focus()) {
178 // End of "$Id: Fl_Choice.cxx 7903 2010-11-28 21:06:39Z matt $".