Button Hover (mouse-over) visual state?

22 views
Skip to first unread message

Tom Shirley

unread,
Sep 20, 2021, 1:42:57 PM9/20/21
to fltk.general
Hello,

I am trying out FLTK for the first time, and it looks like buttons do not seem to change visually when the mouse cursor is over them. Is there a way to make buttons change visually on mouse-over/mouse-out? (I'm on Windows 10 if helpful.) 

imm

unread,
Sep 20, 2021, 1:52:04 PM9/20/21
to General FLTK
On Mon, 20 Sep 2021, 18:43 Tom Shirley wrote:
Hello,

I am trying out FLTK for the first time, and it looks like buttons do not seem to change visually when the mouse cursor is over them. Is there a way to make buttons change visually on mouse-over/mouse-out?

Sure, you can do that, but it isn't done by default because that sort of visual feedback tends to be neither "fast" nor "light" so fltk widgets don't have that as a stock feature.

That said, some of the test folder demos do have that (not sure which ones, off the top of my head, but maybe the subwindow for one) and I'm pretty sure Greg has demos for this on his "Cheat sheet".

--
Ian
From my Fairphone FP3
 

Mo_Al_

unread,
Sep 20, 2021, 3:08:48 PM9/20/21
to fltk.general
You would have to subclass the widget for which you need hover support:

<code>
#include <FL/Enumerations.H>
#include <FL/Fl.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Double_Window.H>

class MyButton : public Fl_Button {
  Fl_Color default_color;
  public:
    MyButton(int x, int y, int w, int h, const char* label = NULL)
        : Fl_Button(xywhlabel) {}

    int handle(int e) {
        int ret = Fl_Button::handle(e);
        switch (e) {
        case FL_ENTER:
            default_color = color();
            color(fl_lighter(default_color));
            redraw();
            return 1;
        case FL_LEAVE:
            color(default_color);
            redraw();
            return 1;
        }
        return ret;
    }
};

int main() {
    auto win = new Fl_Double_Window(400, 300);
    auto btn = new MyButton(160, 200, 80, 30, "Click");
    win->end();
    win->show();
    return Fl::run();
}
</code>

I needed to see if the code blocks work here!
Reply all
Reply to author
Forward
0 new messages