FLTK 1.3.8

Fl_Input_Choice.H
1//
2// "$Id$"
3//
4// An input/chooser widget.
5// ______________ ____
6// | || __ |
7// | input area || \/ |
8// |______________||____|
9//
10// Copyright 1998-2010 by Bill Spitzak and others.
11// Copyright 2004 by Greg Ercolano.
12//
13// This library is free software. Distribution and use rights are outlined in
14// the file "COPYING" which should have been included with this file. If this
15// file is missing or damaged, see the license at:
16//
17// http://www.fltk.org/COPYING.php
18//
19// Please report all bugs and problems on the following page:
20//
21// http://www.fltk.org/str.php
22//
23
24/* \file
25 Fl_Input_Choice widget . */
26
27#ifndef Fl_Input_Choice_H
28#define Fl_Input_Choice_H
29
30#include <FL/Fl.H>
31#include <FL/Fl_Group.H>
32#include <FL/Fl_Input.H>
33#include <FL/Fl_Menu_Button.H>
34#include <FL/fl_draw.H>
35#include <string.h>
36
95 class FL_EXPORT Fl_Input_Choice : public Fl_Group {
96 // Private class to handle slightly 'special' behavior of menu button
97 class InputMenuButton : public Fl_Menu_Button {
98 void draw() {
99 draw_box(FL_UP_BOX, color());
100 fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
101 int xc = x()+w()/2, yc=y()+h()/2;
102 fl_polygon(xc-5,yc-3,xc+5,yc-3,xc,yc+3);
103 if (Fl::focus() == this) draw_focus();
104 }
105 public:
106 InputMenuButton(int X,int Y,int W,int H,const char*L=0) :
107 Fl_Menu_Button(X, Y, W, H, L) { box(FL_UP_BOX); }
108 };
109
110 Fl_Input *inp_;
111 InputMenuButton *menu_;
112
113 // note: this is used by the Fl_Input_Choice ctor defined in Fl_Group.
114 static void menu_cb(Fl_Widget*, void *data) {
115 Fl_Input_Choice *o=(Fl_Input_Choice *)data;
116 Fl_Widget_Tracker wp(o);
117 const Fl_Menu_Item *item = o->menubutton()->mvalue();
118 if (item && item->flags & (FL_SUBMENU|FL_SUBMENU_POINTER)) return; // ignore submenus
119 if (!strcmp(o->inp_->value(), o->menu_->text()))
120 {
121 o->Fl_Widget::clear_changed();
122 if (o->when() & FL_WHEN_NOT_CHANGED)
123 o->do_callback();
124 }
125 else
126 {
127 o->inp_->value(o->menu_->text());
128 o->inp_->set_changed();
129 o->Fl_Widget::set_changed();
130 if (o->when() & (FL_WHEN_CHANGED|FL_WHEN_RELEASE))
131 o->do_callback();
132 }
133
134 if (wp.deleted()) return;
135
136 if (o->callback() != default_callback)
137 {
138 o->Fl_Widget::clear_changed();
139 o->inp_->clear_changed();
140 }
141 }
142
143 // note: this is used by the Fl_Input_Choice ctor defined in Fl_Group.
144 static void inp_cb(Fl_Widget*, void *data) {
145 Fl_Input_Choice *o=(Fl_Input_Choice *)data;
146 Fl_Widget_Tracker wp(o);
147 if (o->inp_->changed()) {
148 o->Fl_Widget::set_changed();
149 if (o->when() & (FL_WHEN_CHANGED|FL_WHEN_RELEASE))
150 o->do_callback();
151 } else {
152 o->Fl_Widget::clear_changed();
153 if (o->when() & FL_WHEN_NOT_CHANGED)
154 o->do_callback();
155 }
156
157 if (wp.deleted()) return;
158
159 if (o->callback() != default_callback)
160 o->Fl_Widget::clear_changed();
161 }
162
163 // Custom resize behavior -- input stretches, menu button doesn't
164 inline int inp_x() { return(x() + Fl::box_dx(box())); }
165 inline int inp_y() { return(y() + Fl::box_dy(box())); }
166 inline int inp_w() { return(w() - Fl::box_dw(box()) - 20); }
167 inline int inp_h() { return(h() - Fl::box_dh(box())); }
168
169 inline int menu_x() { return(x() + w() - 20 - Fl::box_dx(box())); }
170 inline int menu_y() { return(y() + Fl::box_dy(box())); }
171 inline int menu_w() { return(20); }
172 inline int menu_h() { return(h() - Fl::box_dh(box())); }
173
174 public:
180 Fl_Input_Choice(int X,int Y,int W,int H,const char*L=0);
181
193 void add(const char *s) { menu_->add(s); }
195 int changed() const { return inp_->changed() | Fl_Widget::changed(); }
197 void clear_changed() {
198 inp_->clear_changed();
199 Fl_Widget::clear_changed();
200 }
203 void set_changed() {
204 inp_->set_changed();
205 // no need to call Fl_Widget::set_changed()
206 }
208 void clear() { menu_->clear(); }
210 Fl_Boxtype down_box() const { return (menu_->down_box()); }
212 void down_box(Fl_Boxtype b) { menu_->down_box(b); }
214 const Fl_Menu_Item *menu() { return (menu_->menu()); }
216 void menu(const Fl_Menu_Item *m) { menu_->menu(m); }
217 void resize(int X, int Y, int W, int H) {
218 Fl_Group::resize(X,Y,W,H);
219 inp_->resize(inp_x(), inp_y(), inp_w(), inp_h());
220 menu_->resize(menu_x(), menu_y(), menu_w(), menu_h());
221 }
223 Fl_Color textcolor() const { return (inp_->textcolor());}
225 void textcolor(Fl_Color c) { inp_->textcolor(c);}
227 Fl_Font textfont() const { return (inp_->textfont());}
229 void textfont(Fl_Font f) { inp_->textfont(f);}
231 Fl_Fontsize textsize() const { return (inp_->textsize()); }
233 void textsize(Fl_Fontsize s) { inp_->textsize(s); }
235 const char* value() const { return (inp_->value()); }
238 void value(const char *val) { inp_->value(val); }
241 void value(int val) {
242 menu_->value(val);
243 inp_->value(menu_->text(val));
244 }
257 Fl_Menu_Button *menubutton() { return menu_; }
261 Fl_Input *input() { return inp_; }
262};
263
264#endif // !Fl_Input_Choice_H
265
266//
267// End of "$Id$".
268//
Fl_Font
int Fl_Font
A font number is an index into the internal font table.
Definition: Enumerations.H:875
Fl_Color
unsigned int Fl_Color
An FLTK color value; see also Colors
Definition: Enumerations.H:932
Fl_Fontsize
int Fl_Fontsize
Size of a font in pixels.
Definition: Enumerations.H:904
FL_WHEN_NOT_CHANGED
@ FL_WHEN_NOT_CHANGED
Do the callback whenever the user interacts with the widget.
Definition: Enumerations.H:440
FL_WHEN_CHANGED
@ FL_WHEN_CHANGED
Do the callback only when the widget value changes.
Definition: Enumerations.H:439
FL_WHEN_RELEASE
@ FL_WHEN_RELEASE
Do the callback when the button or key is released and the value changes.
Definition: Enumerations.H:441
Fl_Boxtype
Fl_Boxtype
Definition: Enumerations.H:601
FL_UP_BOX
@ FL_UP_BOX
see figure 1
Definition: Enumerations.H:605
Fl.H
Fl static class.
Fl_Group
The Fl_Group class is the FLTK container widget.
Definition: Fl_Group.H:41
Fl_Group::resize
void resize(int, int, int, int)
Resizes the Fl_Group widget and all of its children.
Definition: Fl_Group.cxx:634
Fl_Input_Choice
A combination of the input widget and a menu button.
Definition: Fl_Input_Choice.H:95
Fl_Input_Choice::resize
void resize(int X, int Y, int W, int H)
Resizes the Fl_Group widget and all of its children.
Definition: Fl_Input_Choice.H:217
Fl_Input_Choice::menu
void menu(const Fl_Menu_Item *m)
Sets the Fl_Menu_Item array used for the menu.
Definition: Fl_Input_Choice.H:216
Fl_Input_Choice::set_changed
void set_changed()
Sets the changed() state of both input and menu button widgets to the specfied value.
Definition: Fl_Input_Choice.H:203
Fl_Input_Choice::textsize
void textsize(Fl_Fontsize s)
Sets the Fl_Input text field's font size to s.
Definition: Fl_Input_Choice.H:233
Fl_Input_Choice::menubutton
Fl_Menu_Button * menubutton()
Returns a pointer to the internal Fl_Menu_Button widget.
Definition: Fl_Input_Choice.H:257
Fl_Input_Choice::textcolor
void textcolor(Fl_Color c)
Sets the Fl_Input text field's text color to c.
Definition: Fl_Input_Choice.H:225
Fl_Input_Choice::textsize
Fl_Fontsize textsize() const
Gets the Fl_Input text field's font size.
Definition: Fl_Input_Choice.H:231
Fl_Input_Choice::add
void add(const char *s)
Adds an item to the menu.
Definition: Fl_Input_Choice.H:193
Fl_Input_Choice::clear_changed
void clear_changed()
Clears the changed() state of both input and menu button widgets.
Definition: Fl_Input_Choice.H:197
Fl_Input_Choice::value
void value(int val)
Chooses item# val in the menu, and sets the Fl_Input text field to that value.
Definition: Fl_Input_Choice.H:241
Fl_Input_Choice::textfont
Fl_Font textfont() const
Gets the Fl_Input text field's font style.
Definition: Fl_Input_Choice.H:227
Fl_Input_Choice::menu
const Fl_Menu_Item * menu()
Gets the Fl_Menu_Item array used for the menu.
Definition: Fl_Input_Choice.H:214
Fl_Input_Choice::textcolor
Fl_Color textcolor() const
Gets the Fl_Input text field's text color.
Definition: Fl_Input_Choice.H:223
Fl_Input_Choice::value
const char * value() const
Returns the Fl_Input text field's current contents.
Definition: Fl_Input_Choice.H:235
Fl_Input_Choice::down_box
void down_box(Fl_Boxtype b)
Sets the box type of the menu button.
Definition: Fl_Input_Choice.H:212
Fl_Input_Choice::changed
int changed() const
Returns the combined changed() state of the input and menu button widget.
Definition: Fl_Input_Choice.H:195
Fl_Input_Choice::value
void value(const char *val)
Sets the Fl_Input text field's contents to val.
Definition: Fl_Input_Choice.H:238
Fl_Input_Choice::down_box
Fl_Boxtype down_box() const
Gets the box type of the menu button.
Definition: Fl_Input_Choice.H:210
Fl_Input_Choice::textfont
void textfont(Fl_Font f)
Sets the Fl_Input text field's font style to f.
Definition: Fl_Input_Choice.H:229
Fl_Input_Choice::input
Fl_Input * input()
Returns a pointer to the internal Fl_Input widget.
Definition: Fl_Input_Choice.H:261
Fl_Input_Choice::clear
void clear()
Removes all items from the menu.
Definition: Fl_Input_Choice.H:208
Fl_Input_::resize
void resize(int, int, int, int)
Changes the size of the widget.
Definition: Fl_Input_.cxx:1294
Fl_Input_::textfont
Fl_Font textfont() const
Gets the font of the text in the input field.
Definition: Fl_Input_.H:386
Fl_Input_::textcolor
Fl_Color textcolor() const
Gets the color of the text in the input field.
Definition: Fl_Input_.H:405
Fl_Input_::value
int value(const char *)
Changes the widget text.
Definition: Fl_Input_.cxx:1284
Fl_Input_::textsize
Fl_Fontsize textsize() const
Gets the size of the text in the input field.
Definition: Fl_Input_.H:395
Fl_Input
This is the FLTK text input widget.
Definition: Fl_Input.H:222
Fl_Widget_Tracker
This class should be used to control safe widget deletion.
Definition: Fl.H:1371
Fl_Widget_Tracker::deleted
int deleted()
Returns 1, if the watched widget has been deleted.
Definition: Fl.H:1396
Fl_Widget
Fl_Widget is the base class for all widgets in FLTK.
Definition: Fl_Widget.H:101
Fl_Widget::do_callback
void do_callback()
Calls the widget callback.
Definition: Fl_Widget.H:861
Fl_Widget::y
int y() const
Gets the widget position in its window.
Definition: Fl_Widget.H:289
Fl_Widget::h
int h() const
Gets the widget height.
Definition: Fl_Widget.H:299
Fl_Widget::set_changed
void set_changed()
Marks the value of the widget as changed.
Definition: Fl_Widget.H:786
Fl_Widget::w
int w() const
Gets the widget width.
Definition: Fl_Widget.H:294
Fl_Widget::box
Fl_Boxtype box() const
Gets the box type of the widget.
Definition: Fl_Widget.H:363
Fl_Widget::clear_changed
void clear_changed()
Marks the value of the widget as unchanged.
Definition: Fl_Widget.H:791
Fl_Widget::callback
Fl_Callback_p callback() const
Gets the current callback function for the widget.
Definition: Fl_Widget.H:561
Fl_Widget::when
Fl_When when() const
Returns the conditions under which the callback is called.
Definition: Fl_Widget.H:621
Fl_Widget::x
int x() const
Gets the widget position in its window.
Definition: Fl_Widget.H:284
Fl_Widget::changed
unsigned int changed() const
Checks if the widget value changed since the last callback.
Definition: Fl_Widget.H:781
Fl::box_dx
static int box_dx(Fl_Boxtype)
Returns the X offset for the given boxtype.
Definition: fl_boxtype.cxx:360
Fl::box_dw
static int box_dw(Fl_Boxtype)
Returns the width offset for the given boxtype.
Definition: fl_boxtype.cxx:391
Fl::box_dy
static int box_dy(Fl_Boxtype)
Returns the Y offset for the given boxtype.
Definition: fl_boxtype.cxx:385
Fl::box_dh
static int box_dh(Fl_Boxtype)
Returns the height offset for the given boxtype.
Definition: fl_boxtype.cxx:397
fl_draw.H
utility header to pull drawing functions together
fl_inactive
FL_EXPORT Fl_Color fl_inactive(Fl_Color c)
Returns the inactive, dimmed version of the given color.
Definition: fl_color.cxx:423
fl_color
Fl_Color fl_color()
Returns the last fl_color() that was set.
Definition: fl_draw.H:70
fl_polygon
void fl_polygon(int x, int y, int x1, int y1, int x2, int y2)
Fills a 3-sided polygon.
Definition: fl_draw.H:244
Fl::focus
static Fl_Widget * focus()
Gets the current Fl::focus() widget.
Definition: Fl.H:840
- Copyright © 1998-2021 by Bill Spitzak and others.