Ian MacArthur wrote:

> Following Ivano's query, I thought I'd put together a simple demo of changing 
> the slider widget colour in response to focus changing.
> 
> Using 1.1.9, this turned out to be trickier than I had initially expected: my 
> sub-classed Fl_Slider widget doesn't receive FL_FOCUS events for mouse 
> clicks, so I had to handle that event via the callback method, but with the 
> callback when() set to FL_WHEN_RELEASE_ALWAYS - which I actually knew, in 
> retrospect, as I've done this before... Oh well, worked example below, in 
> case anyone finds it useful!

The solution without using your special callback is to use take_focus() 
in the FL_PUSH event handling. Fl_Slider doesn't do this, and I'm not 
sure if this is a bug or not ....

See attached modified version. This one also has 2 horizontal sliders 
added. Note that they use left/right arrows for keyboard modification.

Albrecht

// demo of handling enter/leave events for sliders
// fltk-config --compile slider-focus.cxx
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Slider.H>
#include <FL/Fl_Button.H>

static Fl_Double_Window *main_win=(Fl_Double_Window *)0;
static Fl_Button *exit_bt=(Fl_Button *)0;

class mod_slider : public Fl_Slider {
public:
        mod_slider(int X, int Y, int W, int H, const char *L) :
         Fl_Slider(X,Y,W,H,L){};

        int handle(int ev);
};

int mod_slider::handle(int ev) {
        int res = Fl_Slider::handle(ev);
        switch(ev){
        case FL_FOCUS:
                selection_color(FL_YELLOW);
                break;

        case FL_UNFOCUS:
                selection_color(FL_BLUE);
                break;

        case FL_PUSH:           // <<<--- added !
                take_focus();
                break;

        default:
                break;
        }

        return res;
} // handle

static mod_slider *slider1=(mod_slider *)0;
static mod_slider *slider2=(mod_slider *)0;
static mod_slider *slider3=(mod_slider *)0;
static mod_slider *slider4=(mod_slider *)0;
static mod_slider *slider5=(mod_slider *)0;
static mod_slider *slider6=(mod_slider *)0;

/* we don't need this, use take_focus() instead (see above)
static void cb_slider(mod_slider *w, void*) {
        w->selection_color(FL_YELLOW);
        w->redraw();
}
*/

static void cb_exit_bt(Fl_Button*, void*) {
        main_win->hide();
}

int main(int argc, char **argv) {
        main_win = new Fl_Double_Window(600, 440, "Slider Test Window");
        main_win->begin();

        slider1 = new mod_slider(25, 24, 48, 251, "Chan 1");
        slider1->type(4);
        slider1->selection_color(FL_BLUE);
        // slider1->callback((Fl_Callback*)cb_slider);
        // slider1->when(FL_WHEN_RELEASE_ALWAYS);

        slider2 = new mod_slider(151, 24, 48, 251, "Chan 2");
        slider2->type(4);
        slider2->selection_color(FL_BLUE);
        // slider2->callback((Fl_Callback*)cb_slider);
        // slider2->when(FL_WHEN_RELEASE_ALWAYS);

        slider3 = new mod_slider(278, 24, 48, 251, "Chan 3");
        slider3->type(4);
        slider3->selection_color(FL_BLUE);
        // slider3->callback((Fl_Callback*)cb_slider);
        // slider3->when(FL_WHEN_RELEASE_ALWAYS);

        slider4 = new mod_slider(405, 24, 48, 251, "Chan 4");
        slider4->type(4);
        slider4->selection_color(FL_BLUE);
        // slider4->callback((Fl_Callback*)cb_slider);
        // slider4->when(FL_WHEN_RELEASE_ALWAYS);

        slider5 = new mod_slider(75,305,251, 48, "Chan 5 ");
        slider5->align(FL_ALIGN_LEFT);
        slider5->type(FL_HOR_NICE_SLIDER);
        slider5->selection_color(FL_BLUE);

        slider6 = new mod_slider(75,365,251, 48, "Chan 6 ");
        slider6->align(FL_ALIGN_LEFT);
        slider6->type(FL_HOR_NICE_SLIDER);
        slider6->selection_color(FL_BLUE);

        exit_bt = new Fl_Button(500, 256, 70, 29, "Exit");
        exit_bt->box(FL_THIN_UP_BOX);
        exit_bt->callback((Fl_Callback*)cb_exit_bt);

        main_win->end();
        main_win->show(argc, argv);
        return Fl::run();
} // main
/* end of file */
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to