Added convenience method update_menubutton(),

which tries to keep the menu synchronized with the Fl_Input field,
assuming there's a match.



git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12908 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Greg Ercolano 2018-05-07 21:24:06 +00:00
parent 4ede9cec29
commit 28807a3fab
2 changed files with 61 additions and 4 deletions

View File

@ -85,8 +85,11 @@ public:
void resize(int X, int Y, int W, int H); void resize(int X, int Y, int W, int H);
/** Adds an item to the menu. /** Adds an item to the menu.
When any item is selected, the Fl_Input_Choice callback() is invoked,
which can do something with the selected item.
You can access the more complex Fl_Menu_Button::add() methods You can access the more complex Fl_Menu_Button::add() methods
(setting callbacks, userdata, etc), via menubutton(). Example: (setting item-specific callbacks, userdata, etc), via menubutton(). Example:
\code \code
Fl_Input_Choice *choice = new Fl_Input_Choice(100,10,120,25,"Fonts"); 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 Fl_Menu_Button *mb = choice->menubutton(); // use Fl_Input_Choice's Fl_Menu_Button
@ -143,8 +146,14 @@ public:
const char* value() const { return (inp_->value()); } const char* value() const { return (inp_->value()); }
/** Sets the Fl_Input text field's contents to \p val. /** Sets the Fl_Input text field's contents to \p val.
Does not affect the menu selection.
\see void value(int val) Note it is possible to set the value() to one that is not
in the menubutton's list of choices.
Setting the value() does NOT affect the menubutton's selection.
If that's needed, call update_menubutton() after setting value().
\see void value(int val), update_menubutton()
*/ */
void value(const char *val) { inp_->value(val); } void value(const char *val) { inp_->value(val); }
@ -152,6 +161,8 @@ public:
to that value. Any previous text is cleared. */ to that value. Any previous text is cleared. */
void value(int val); void value(int val);
int update_menubutton();
/** Returns a pointer to the internal Fl_Menu_Button widget. /** 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. This can be used to access any of the methods of the menu button, e.g.
\code \code

View File

@ -63,10 +63,14 @@
#include <FL/Fl_Double_Window.H> #include <FL/Fl_Double_Window.H>
#include <FL/Fl_Input_Choice.H> #include <FL/Fl_Input_Choice.H>
void choice_cb(Fl_Widget *w, void *userdata) { void choice_cb(Fl_Widget *w, void *userdata) {
// Show info about the picked item // Show info about the picked item
Fl_Input_Choice *choice = (Fl_Input_Choice*)w; Fl_Input_Choice *choice = (Fl_Input_Choice*)w;
const Fl_Menu_Item *item = choice->menubutton()->mvalue();
printf("*** Choice Callback:\n"); printf("*** Choice Callback:\n");
printf(" widget's text value='%s'\n", choice->value()); // normally all you need
// Access the menu via menubutton()..
const Fl_Menu_Item *item = choice->menubutton()->mvalue();
printf(" item label()='%s'\n", item ? item->label() : "(No item)"); printf(" item label()='%s'\n", item ? item->label() : "(No item)");
printf(" item value()=%d\n", choice->menubutton()->value()); printf(" item value()=%d\n", choice->menubutton()->value());
printf(" input value()='%s'\n", choice->input()->value()); printf(" input value()='%s'\n", choice->input()->value());
@ -214,6 +218,48 @@ void Fl_Input_Choice::clear_changed() {
Fl_Widget::clear_changed(); Fl_Widget::clear_changed();
} }
/** Updates the menubutton with the string value in Fl_Input.
If the string value currently in Fl_Input matches one of the
menu items in menubutton(), that menu item will become the
current item selected.
Call this method after setting value(const char*) if you need
the menubutton() to be synchronized with the Fl_Input field.
\code
// Add items
choice->add(".25");
choice->add(".50");
choice->add("1.0");
choice->add("2.0");
choice->add("4.0");
choice->value("1.0"); // sets Fl_Input to "1.0"
choice->update_menubutton(); // cause menubutton to reflect this value too
// (returns 1 if match was found, 0 if not)
// Verify menubutton()'s value.
printf("menu button choice index=%d, value=%s\n",
choice->menubutton()->value(), // would be -1 if update not done
choice->menubutton()->text()); // would be NULL if update not done
\endcode
\returns 1 if a matching menuitem was found and value set, 0 if not.
\version 1.4.0
*/
int Fl_Input_Choice::update_menubutton() {
// Find item in menu
for ( int i=0; i<menu_->size(); i++ ) {
const Fl_Menu_Item &item = menu_->menu()[i];
if (item.flags & (FL_SUBMENU|FL_SUBMENU_POINTER)) continue; // ignore submenus
const char *name = menu_->text(i);
if ( name && strcmp(name, inp_->value()) == 0) {
menu_->value(i);
return 1;
}
}
return 0; // not found
}
// //
// End of "$Id$". // End of "$Id$".