diff options
| author | Greg Ercolano <erco@seriss.com> | 2018-05-07 21:24:06 +0000 |
|---|---|---|
| committer | Greg Ercolano <erco@seriss.com> | 2018-05-07 21:24:06 +0000 |
| commit | 28807a3fab6e94d5c1907fdf5417899a6957d8d3 (patch) | |
| tree | 4d59cbc413ceebf26f120d49f6a66d2b98fae4c9 | |
| parent | 4ede9cec29698d2ca3b195e764f228c0da44e02c (diff) | |
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
| -rw-r--r-- | FL/Fl_Input_Choice.H | 17 | ||||
| -rw-r--r-- | src/Fl_Input_Choice.cxx | 48 |
2 files changed, 61 insertions, 4 deletions
diff --git a/FL/Fl_Input_Choice.H b/FL/Fl_Input_Choice.H index ab9c63559..80cee90b4 100644 --- a/FL/Fl_Input_Choice.H +++ b/FL/Fl_Input_Choice.H @@ -85,8 +85,11 @@ public: void resize(int X, int Y, int W, int H); /** 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 - (setting callbacks, userdata, etc), via menubutton(). Example: + (setting item-specific 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 @@ -143,8 +146,14 @@ public: const char* value() const { return (inp_->value()); } /** 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); } @@ -152,6 +161,8 @@ public: to that value. Any previous text is cleared. */ void value(int val); + int update_menubutton(); + /** 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 diff --git a/src/Fl_Input_Choice.cxx b/src/Fl_Input_Choice.cxx index a3fcedcbb..dd3c9b1cc 100644 --- a/src/Fl_Input_Choice.cxx +++ b/src/Fl_Input_Choice.cxx @@ -63,10 +63,14 @@ #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(" 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 value()=%d\n", choice->menubutton()->value()); printf(" input value()='%s'\n", choice->input()->value()); @@ -214,6 +218,48 @@ void Fl_Input_Choice::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$". |
