众所周知,FLTK的回调只能有一个,其他的需要重写handle(),所以我打算直接重写handle(),制作多个回调
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
void no_cb(Fl_Widget*,void*){}
class Button : public Fl_Button
{
private:
void (*onPush)(Fl_Widget*,void*);
void (*onRelease)(Fl_Widget*,void*);
void (*onMove)(Fl_Widget*,void*);
void (*onHide)(Fl_Widget*,void*);
void (*onShow)(Fl_Widget*,void*);
void* _push_data;
void* _release_data;
void* _move_data;
void* _hide_data;
void* _show_data;
public:
Button(int x,int y,int w,int h,char* l = 0)
:Fl_Button(x,y,w,h,l)
{
onPush = no_cb;
_push_data = 0;
onRelease = no_cb;
_release_data = 0;
onMove = no_cb;
_move_data = 0;
onHide = no_cb;
_hide_data = 0;
onShow = no_cb;
_show_data = 0;
}
int handle(int event)
{
switch(event)
{
case FL_PUSH:
(*onPush)(this,_push_data);
return 1;
case FL_RELEASE:
(*onRelease)(this,_release_data);
return 1;
case FL_MOVE:
(*onMove)(this,_move_data);
return 1;
case FL_HIDE:
(*onHide)(this,_hide_data);
return 1;
case FL_SHOW:
(*onShow)(this,_show_data);
return 1;
}
return 0;
}
void callback(int event,void (*cb)(Fl_Widget*,void*),void* data = 0)
{
switch(event)
{
case FL_PUSH:
onPush = cb;
_push_data = data;
break;
case FL_RELEASE:
onRelease = cb;
_release_data = data;
break;
case FL_MOVE:
onMove = cb;
_move_data = data;
break;
case FL_HIDE:
onHide = cb;
_hide_data = data;
break;
case FL_SHOW:
onShow = cb;
_show_data = data;
break;
}
}
};
int main()
{
Fl_Window window(600,800,"");
Button btn(0,0,600,100);
btn.callback(FL_PUSH,[](Fl_Widget* w,void*)
{
((Button*)(w))->label("push");
});
btn.callback(FL_RELEASE,[](Fl_Widget* w,void*)
{
((Button*)(w))->label("release");
});
btn.callback(FL_MOVE,[](Fl_Widget* w,void*)
{
((Button*)(w))->label("move");
});
btn.callback(FL_HIDE,[](Fl_Widget* w,void*)
{
((Button*)(w))->label("hide");
});
btn.callback(FL_SHOW,[](Fl_Widget* w,void*)
{
((Button*)(w))->label("show");
});
window.show();
return Fl::run();
}