diff options
| author | Greg Ercolano <erco@seriss.com> | 2022-01-13 07:28:00 -0800 |
|---|---|---|
| committer | Greg Ercolano <erco@seriss.com> | 2022-01-13 07:28:00 -0800 |
| commit | 47cd9a11a0d68fa511cb6c593b62b25af1550e19 (patch) | |
| tree | 5cd037a2849aa96bd4c8b435f2dddce4b31cfcb9 | |
| parent | 879da686b6aeafe227398e1a1cd2ce546d8dd659 (diff) | |
Fixes issue #361.
| -rw-r--r-- | FL/Fl_Input_Choice.H | 36 | ||||
| -rw-r--r-- | src/Fl_Input_Choice.cxx | 38 |
2 files changed, 62 insertions, 12 deletions
diff --git a/FL/Fl_Input_Choice.H b/FL/Fl_Input_Choice.H index 71bc76ee0..56fdd103a 100644 --- a/FL/Fl_Input_Choice.H +++ b/FL/Fl_Input_Choice.H @@ -58,16 +58,34 @@ class FL_EXPORT Fl_Input_Choice : public Fl_Group { // note: this is used by the Fl_Input_Choice ctor. static void inp_cb(Fl_Widget*, void *data); +protected: // 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())); } - inline int menu_y() { return(y() + Fl::box_dy(box())); } - inline int menu_w() { return(20); } - inline int menu_h() { return(h() - Fl::box_dh(box())); } + + /** The methods inp_x(), inp_y(), inp_w() and inp_h() return the + desired position and size of the internal Fl_Input widget. + These can be overridden by a subclass to redefine positioning. + See code example in the Description for subclassing details. + */ + virtual int inp_x() const { return(x() + Fl::box_dx(box())); } + /** See inp_x() for info. */ + virtual int inp_y() const { return(y() + Fl::box_dy(box())); } + /** See inp_x() for info. */ + virtual int inp_w() const { return(w() - Fl::box_dw(box()) - 20); } + /** See inp_x() for info. */ + virtual int inp_h() const { return(h() - Fl::box_dh(box())); } + + /** The methods menu_x(), menu_y(), menu_w() and menu_h() return the + desired position and size of the internal Fl_Menu_Button widget. + These can be overridden by a subclass to redefine positioning. + See code example in the Description for subclassing details. + */ + virtual int menu_x() const { return(x() + w() - 20 - Fl::box_dx(box())); } + /** See menu_x() for info. */ + virtual int menu_y() const { return(y() + Fl::box_dy(box())); } + /** See menu_x() for info. */ + virtual int menu_w() const { return(20); } + /** See menu_x() for info. */ + virtual int menu_h() const { return(h() - Fl::box_dh(box())); } public: diff --git a/src/Fl_Input_Choice.cxx b/src/Fl_Input_Choice.cxx index 765b9656b..bf52360f7 100644 --- a/src/Fl_Input_Choice.cxx +++ b/src/Fl_Input_Choice.cxx @@ -54,19 +54,18 @@ - 1: the user picked a different item in the choice menu - 0: the user typed or pasted directly into the input field - Example use: + \par Example Use of Fl_Input_Choice \code #include <stdio.h> #include <FL/Fl.H> #include <FL/Fl_Double_Window.H> #include <FL/Fl_Input_Choice.H> + // Fl_Input_Choice callback() void choice_cb(Fl_Widget *w, void *userdata) { - // Show info about the picked item Fl_Input_Choice *choice = (Fl_Input_Choice*)w; 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)"); @@ -90,6 +89,39 @@ return Fl::run(); } \endcode + + \par Subclassing Example + One can subclass Fl_Input_Choice to override the virtual methods inp_x/y/w/h() + and menu_x/y/w/h() to take control of the internal Fl_Input and Fl_Menu_Button widget + positioning. In this example, input and menubutton's positions are swapped: + \code + #include <FL/Fl.H> + #include <FL/Fl_Window.H> + #include <FL/Fl_Input_Choice.H> + + class MyInputChoice : public Fl_Input_Choice { + protected: + virtual int inp_x() const { return x() + Fl::box_dx(box()) + menu_w(); } // override to reposition + virtual int menu_x() const { return x() + Fl::box_dx(box()); } // override to reposition + public: + MyInputChoice(int X,int Y,int W,int H,const char*L=0) : Fl_Input_Choice(X,Y,W,H,L) { + resize(X,Y,W,H); // necessary for ctor to trigger our overrides + } + }; + + int main(int argc, char **argv) { + Fl_Window *win = new Fl_Window(400,300); + MyInputChoice *mychoice = new MyInputChoice(150,40,150,25,"Right Align Input"); + mychoice->add("Aaa"); + mychoice->add("Bbb"); + mychoice->add("Ccc"); + win->end(); + win->resizable(win); + win->show(); + return Fl::run(); + } + \endcode + */ /** Constructor for private menu button. */ |
