2005-02-25 00:14:22 +03:00
|
|
|
//
|
|
|
|
// "$Id$"
|
|
|
|
//
|
|
|
|
// An input/chooser widget.
|
|
|
|
// ______________ ____
|
|
|
|
// | || __ |
|
|
|
|
// | input area || \/ |
|
|
|
|
// |______________||____|
|
|
|
|
//
|
2010-11-29 00:06:39 +03:00
|
|
|
// Copyright 1998-2010 by Bill Spitzak and others.
|
2005-02-25 00:14:22 +03:00
|
|
|
// Copyright 2004 by Greg Ercolano.
|
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
2005-02-25 00:14:22 +03:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// http://www.fltk.org/COPYING.php
|
2005-02-25 00:14:22 +03:00
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
2005-02-25 00:14:22 +03:00
|
|
|
//
|
|
|
|
|
2008-10-15 17:46:06 +04:00
|
|
|
/* \file
|
2008-09-16 11:26:22 +04:00
|
|
|
Fl_Input_Choice widget . */
|
|
|
|
|
2005-02-25 00:14:22 +03:00
|
|
|
#ifndef Fl_Input_Choice_H
|
|
|
|
#define Fl_Input_Choice_H
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
2007-05-06 19:57:40 +04:00
|
|
|
#include <FL/Fl_Group.H>
|
2005-02-25 00:14:22 +03:00
|
|
|
#include <FL/Fl_Input.H>
|
|
|
|
#include <FL/Fl_Menu_Button.H>
|
|
|
|
#include <FL/fl_draw.H>
|
2008-02-20 20:59:13 +03:00
|
|
|
#include <string.h>
|
2005-02-25 00:14:22 +03:00
|
|
|
|
2008-09-15 15:10:51 +04:00
|
|
|
/**
|
|
|
|
A combination of the input widget and a menu button.
|
2011-12-08 22:18:51 +04:00
|
|
|
|
|
|
|
\image html input_choice.jpg
|
|
|
|
\image latex input_choice.jpg "Fl_Input_Choice widget" width=6cm
|
|
|
|
|
2008-09-15 15:10:51 +04:00
|
|
|
The user can either type into the input area, or use the
|
2011-12-08 22:18:51 +04:00
|
|
|
menu button chooser on the right to choose an item which loads
|
|
|
|
the input area with the selected text.
|
2015-01-11 01:05:15 +03:00
|
|
|
|
2011-12-08 22:18:51 +04:00
|
|
|
The application can directly access both the internal Fl_Input
|
|
|
|
and Fl_Menu_Button widgets respectively using the input() and menubutton()
|
|
|
|
accessor methods.
|
2015-01-11 01:05:15 +03:00
|
|
|
|
|
|
|
The default behavior is to invoke the Fl_Input_Choice::callback()
|
|
|
|
if the user changes the input field's contents, either by typing,
|
|
|
|
pasting, or clicking a different item in the choice menu.
|
|
|
|
|
|
|
|
The callback can determine if an item was picked vs. typing
|
|
|
|
into the input field by checking the value of menubutton()->changed(),
|
|
|
|
which will be:
|
|
|
|
|
|
|
|
- 1: the user picked a different item in the choice menu
|
|
|
|
- 0: the user typed or pasted directly into the input field
|
|
|
|
|
|
|
|
Example use:
|
|
|
|
\code
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Double_Window.H>
|
|
|
|
#include <FL/Fl_Input_Choice.H>
|
|
|
|
void choice_cb(Fl_Widget *w, void *userdata) {
|
|
|
|
// Show info about the picked item
|
|
|
|
Fl_Input_Choice *choice = (Fl_Input_Choice*)w;
|
|
|
|
const Fl_Menu_Item *item = choice->menubutton()->mvalue();
|
|
|
|
printf("*** Choice Callback:\n");
|
|
|
|
printf(" item label()='%s'\n", item ? item->label() : "(No item)");
|
|
|
|
printf(" item value()=%d\n", choice->menubutton()->value());
|
|
|
|
printf(" input value()='%s'\n", choice->input()->value());
|
|
|
|
printf(" The user %s\n", choice->menubutton()->changed()
|
|
|
|
? "picked a menu item"
|
|
|
|
: "typed text");
|
|
|
|
}
|
|
|
|
int main() {
|
|
|
|
Fl_Double_Window win(200,100,"Input Choice");
|
|
|
|
win.begin();
|
|
|
|
Fl_Input_Choice choice(10,10,100,30);
|
|
|
|
choice.callback(choice_cb, 0);
|
|
|
|
choice.add("Red");
|
|
|
|
choice.add("Orange");
|
|
|
|
choice.add("Yellow");
|
|
|
|
//choice.value("Red"); // uncomment to make "Red" default
|
|
|
|
win.end();
|
|
|
|
win.show();
|
|
|
|
return Fl::run();
|
|
|
|
}
|
|
|
|
\endcode
|
2008-09-15 15:10:51 +04:00
|
|
|
*/
|
2010-12-13 02:21:03 +03:00
|
|
|
class FL_EXPORT Fl_Input_Choice : public Fl_Group {
|
2005-02-25 00:14:22 +03:00
|
|
|
// Private class to handle slightly 'special' behavior of menu button
|
|
|
|
class InputMenuButton : public Fl_Menu_Button {
|
|
|
|
void draw() {
|
|
|
|
draw_box(FL_UP_BOX, color());
|
|
|
|
fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
|
|
|
|
int xc = x()+w()/2, yc=y()+h()/2;
|
|
|
|
fl_polygon(xc-5,yc-3,xc+5,yc-3,xc,yc+3);
|
|
|
|
if (Fl::focus() == this) draw_focus();
|
|
|
|
}
|
|
|
|
public:
|
2011-10-02 10:25:13 +04:00
|
|
|
InputMenuButton(int X,int Y,int W,int H,const char*L=0) :
|
|
|
|
Fl_Menu_Button(X, Y, W, H, L) { box(FL_UP_BOX); }
|
2005-02-25 00:14:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Fl_Input *inp_;
|
|
|
|
InputMenuButton *menu_;
|
|
|
|
|
2015-01-11 01:05:15 +03:00
|
|
|
// note: this is used by the Fl_Input_Choice ctor defined in Fl_Group.
|
2005-02-25 00:14:22 +03:00
|
|
|
static void menu_cb(Fl_Widget*, void *data) {
|
|
|
|
Fl_Input_Choice *o=(Fl_Input_Choice *)data;
|
2010-02-20 20:40:07 +03:00
|
|
|
Fl_Widget_Tracker wp(o);
|
2007-05-15 11:42:53 +04:00
|
|
|
const Fl_Menu_Item *item = o->menubutton()->mvalue();
|
2008-02-20 20:59:13 +03:00
|
|
|
if (item && item->flags & (FL_SUBMENU|FL_SUBMENU_POINTER)) return; // ignore submenus
|
|
|
|
if (!strcmp(o->inp_->value(), o->menu_->text()))
|
|
|
|
{
|
|
|
|
o->Fl_Widget::clear_changed();
|
|
|
|
if (o->when() & FL_WHEN_NOT_CHANGED)
|
|
|
|
o->do_callback();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
o->inp_->value(o->menu_->text());
|
|
|
|
o->inp_->set_changed();
|
|
|
|
o->Fl_Widget::set_changed();
|
|
|
|
if (o->when() & (FL_WHEN_CHANGED|FL_WHEN_RELEASE))
|
|
|
|
o->do_callback();
|
|
|
|
}
|
2010-02-20 20:40:07 +03:00
|
|
|
|
|
|
|
if (wp.deleted()) return;
|
2008-02-20 20:59:13 +03:00
|
|
|
|
|
|
|
if (o->callback() != default_callback)
|
|
|
|
{
|
|
|
|
o->Fl_Widget::clear_changed();
|
|
|
|
o->inp_->clear_changed();
|
|
|
|
}
|
2005-02-25 00:14:22 +03:00
|
|
|
}
|
|
|
|
|
2015-01-11 01:05:15 +03:00
|
|
|
// note: this is used by the Fl_Input_Choice ctor defined in Fl_Group.
|
2005-02-25 00:14:22 +03:00
|
|
|
static void inp_cb(Fl_Widget*, void *data) {
|
|
|
|
Fl_Input_Choice *o=(Fl_Input_Choice *)data;
|
2010-02-20 20:40:07 +03:00
|
|
|
Fl_Widget_Tracker wp(o);
|
2008-02-20 20:59:13 +03:00
|
|
|
if (o->inp_->changed()) {
|
2007-06-08 01:40:36 +04:00
|
|
|
o->Fl_Widget::set_changed();
|
2008-02-20 20:59:13 +03:00
|
|
|
if (o->when() & (FL_WHEN_CHANGED|FL_WHEN_RELEASE))
|
|
|
|
o->do_callback();
|
|
|
|
} else {
|
2007-06-08 01:40:36 +04:00
|
|
|
o->Fl_Widget::clear_changed();
|
2008-02-20 20:59:13 +03:00
|
|
|
if (o->when() & FL_WHEN_NOT_CHANGED)
|
|
|
|
o->do_callback();
|
|
|
|
}
|
2010-02-20 20:40:07 +03:00
|
|
|
|
|
|
|
if (wp.deleted()) return;
|
2008-02-20 20:59:13 +03:00
|
|
|
|
2007-06-08 01:40:36 +04:00
|
|
|
if (o->callback() != default_callback)
|
|
|
|
o->Fl_Widget::clear_changed();
|
2005-02-25 00:14:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Custom resize behavior -- input stretches, menu button doesn't
|
|
|
|
inline int inp_x() { return(x() + Fl::box_dx(box())); }
|
|
|
|
inline int inp_y() { return(y() + Fl::box_dy(box())); }
|
|
|
|
inline int inp_w() { return(w() - Fl::box_dw(box()) - 20); }
|
|
|
|
inline int inp_h() { return(h() - Fl::box_dh(box())); }
|
|
|
|
|
|
|
|
inline int menu_x() { return(x() + w() - 20 - Fl::box_dx(box())); }
|
2005-08-08 06:45:06 +04:00
|
|
|
inline int menu_y() { return(y() + Fl::box_dy(box())); }
|
2005-02-25 00:14:22 +03:00
|
|
|
inline int menu_w() { return(20); }
|
2005-08-08 06:45:06 +04:00
|
|
|
inline int menu_h() { return(h() - Fl::box_dh(box())); }
|
2005-02-25 00:14:22 +03:00
|
|
|
|
|
|
|
public:
|
2008-09-15 15:10:51 +04:00
|
|
|
/**
|
|
|
|
Creates a new Fl_Input_Choice widget using the given position, size,
|
|
|
|
and label string.
|
2011-12-08 22:18:51 +04:00
|
|
|
Inherited destructor destroys the widget and any values associated with it.
|
2008-09-15 15:10:51 +04:00
|
|
|
*/
|
2012-07-24 08:37:22 +04:00
|
|
|
Fl_Input_Choice(int X,int Y,int W,int H,const char*L=0);
|
2008-09-15 15:10:51 +04:00
|
|
|
|
2012-12-09 21:45:24 +04:00
|
|
|
/** Adds an item to the menu.
|
|
|
|
You can access the more complex Fl_Menu_Button::add() methods
|
|
|
|
(setting callbacks, userdata, etc), via menubutton(). Example:
|
|
|
|
\code
|
|
|
|
Fl_Input_Choice *choice = new Fl_Input_Choice(100,10,120,25,"Fonts");
|
|
|
|
Fl_Menu_Button *mb = choice->menubutton(); // use Fl_Input_Choice's Fl_Menu_Button
|
|
|
|
mb->add("Helvetica", 0, MyFont_CB, (void*)mydata); // use Fl_Menu_Button's add() methods
|
|
|
|
mb->add("Courier", 0, MyFont_CB, (void*)mydata);
|
|
|
|
mb->add("More..", 0, FontDialog_CB, (void*)mydata);
|
|
|
|
\endcode
|
|
|
|
*/
|
2008-09-15 15:10:51 +04:00
|
|
|
void add(const char *s) { menu_->add(s); }
|
2012-12-09 21:45:24 +04:00
|
|
|
/** Returns the combined changed() state of the input and menu button widget. */
|
2011-12-08 22:18:51 +04:00
|
|
|
int changed() const { return inp_->changed() | Fl_Widget::changed(); }
|
2012-12-09 21:45:24 +04:00
|
|
|
/** Clears the changed() state of both input and menu button widgets. */
|
2007-06-07 21:38:49 +04:00
|
|
|
void clear_changed() {
|
2007-06-08 01:40:36 +04:00
|
|
|
inp_->clear_changed();
|
|
|
|
Fl_Widget::clear_changed();
|
2007-06-07 21:38:49 +04:00
|
|
|
}
|
2012-12-09 21:45:24 +04:00
|
|
|
/** Sets the changed() state of both input and menu button widgets
|
|
|
|
to the specfied value.*/
|
2007-06-07 21:38:49 +04:00
|
|
|
void set_changed() {
|
2007-06-08 01:40:36 +04:00
|
|
|
inp_->set_changed();
|
|
|
|
// no need to call Fl_Widget::set_changed()
|
2007-06-07 21:38:49 +04:00
|
|
|
}
|
2008-09-15 15:10:51 +04:00
|
|
|
/** Removes all items from the menu. */
|
|
|
|
void clear() { menu_->clear(); }
|
|
|
|
/** Gets the box type of the menu button */
|
|
|
|
Fl_Boxtype down_box() const { return (menu_->down_box()); }
|
|
|
|
/** Sets the box type of the menu button */
|
|
|
|
void down_box(Fl_Boxtype b) { menu_->down_box(b); }
|
|
|
|
/** Gets the Fl_Menu_Item array used for the menu. */
|
|
|
|
const Fl_Menu_Item *menu() { return (menu_->menu()); }
|
|
|
|
/** Sets the Fl_Menu_Item array used for the menu. */
|
|
|
|
void menu(const Fl_Menu_Item *m) { menu_->menu(m); }
|
2005-02-25 00:14:22 +03:00
|
|
|
void resize(int X, int Y, int W, int H) {
|
|
|
|
Fl_Group::resize(X,Y,W,H);
|
|
|
|
inp_->resize(inp_x(), inp_y(), inp_w(), inp_h());
|
|
|
|
menu_->resize(menu_x(), menu_y(), menu_w(), menu_h());
|
|
|
|
}
|
2011-12-08 22:18:51 +04:00
|
|
|
/// Gets the Fl_Input text field's text color.
|
2008-09-15 15:10:51 +04:00
|
|
|
Fl_Color textcolor() const { return (inp_->textcolor());}
|
2011-12-08 22:18:51 +04:00
|
|
|
/// Sets the Fl_Input text field's text color to \p c.
|
2008-09-15 15:10:51 +04:00
|
|
|
void textcolor(Fl_Color c) { inp_->textcolor(c);}
|
2011-12-08 22:18:51 +04:00
|
|
|
/// Gets the Fl_Input text field's font style.
|
2008-09-15 15:10:51 +04:00
|
|
|
Fl_Font textfont() const { return (inp_->textfont());}
|
2011-12-08 22:18:51 +04:00
|
|
|
/// Sets the Fl_Input text field's font style to \p f.
|
2008-09-15 15:10:51 +04:00
|
|
|
void textfont(Fl_Font f) { inp_->textfont(f);}
|
2011-12-08 22:18:51 +04:00
|
|
|
/// Gets the Fl_Input text field's font size
|
2008-09-15 15:10:51 +04:00
|
|
|
Fl_Fontsize textsize() const { return (inp_->textsize()); }
|
2011-12-08 22:18:51 +04:00
|
|
|
/// Sets the Fl_Input text field's font size to \p s.
|
2008-09-15 15:10:51 +04:00
|
|
|
void textsize(Fl_Fontsize s) { inp_->textsize(s); }
|
2011-12-08 22:18:51 +04:00
|
|
|
/// Returns the Fl_Input text field's current contents.
|
2008-09-15 15:10:51 +04:00
|
|
|
const char* value() const { return (inp_->value()); }
|
2012-12-09 21:45:24 +04:00
|
|
|
/** Sets the Fl_Input text field's contents to \p val.
|
|
|
|
Does not affect the menu selection.*/
|
2008-09-15 15:10:51 +04:00
|
|
|
void value(const char *val) { inp_->value(val); }
|
2012-12-09 21:45:24 +04:00
|
|
|
/** Chooses item# \p val in the menu, and sets the Fl_Input text field
|
|
|
|
to that value. Any previous text is cleared.*/
|
2005-02-25 00:14:22 +03:00
|
|
|
void value(int val) {
|
|
|
|
menu_->value(val);
|
|
|
|
inp_->value(menu_->text(val));
|
|
|
|
}
|
2012-12-09 21:45:24 +04:00
|
|
|
/** Returns a pointer to the internal Fl_Menu_Button widget.
|
|
|
|
This can be used to access any of the methods of the menu button, e.g.
|
|
|
|
\code
|
|
|
|
Fl_Input_Choice *choice = new Fl_Input_Choice(100,10,120,25,"Choice:");
|
|
|
|
[..]
|
|
|
|
// Print all the items in the choice menu
|
|
|
|
for ( int t=0; t<choice->menubutton()->size(); t++ ) {
|
|
|
|
const Fl_Menu_Item &item = choice->menubutton()->menu()[t];
|
|
|
|
printf("item %d -- label=%s\n", t, item.label() ? item.label() : "(Null)");
|
|
|
|
}
|
|
|
|
\endcode
|
|
|
|
*/
|
2005-02-25 00:14:22 +03:00
|
|
|
Fl_Menu_Button *menubutton() { return menu_; }
|
2012-12-09 21:45:24 +04:00
|
|
|
/** Returns a pointer to the internal Fl_Input widget.
|
|
|
|
This can be used to directly access all of the Fl_Input widget's
|
|
|
|
methods.*/
|
2005-02-25 00:14:22 +03:00
|
|
|
Fl_Input *input() { return inp_; }
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // !Fl_Input_Choice_H
|
|
|
|
|
|
|
|
//
|
|
|
|
// End of "$Id$".
|
|
|
|
//
|