Fixes issue #361.

This commit is contained in:
Greg Ercolano 2022-01-13 07:28:00 -08:00
parent 879da686b6
commit 47cd9a11a0
2 changed files with 61 additions and 11 deletions

View File

@ -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:

View File

@ -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. */