Skip to content

Commit

Permalink
Add event handling capability to all the widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
pwiecz committed Apr 3, 2022
1 parent 709ff26 commit 112a585
Show file tree
Hide file tree
Showing 50 changed files with 448 additions and 296 deletions.
24 changes: 3 additions & 21 deletions box.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,13 @@

#include <FL/Fl_Box.H>

#include "_cgo_export.h"
#include "event_handler.h"


class GBox : public Fl_Box {
class GBox : public EventHandler<Fl_Box> {
public:
GBox(int boxType, int x, int y, int w, int h, const char* label)
: Fl_Box((Fl_Boxtype)boxType, x, y, w, h, label) {
}

int handle(int event) final {
if (m_eventHandlerId >= 0) {
const int ret = _go_eventHandler(m_eventHandlerId, event);
if (ret != 0) {
return ret;
}
}
return Fl_Box::handle(event);
}

void set_event_handler(int handlerId) {
m_eventHandlerId = handlerId;
}

private:
int m_eventHandlerId = -1;
: EventHandler<Fl_Box>((Fl_Boxtype)boxType, x, y, w, h, label) {}
};

GBox *go_fltk_new_Box(int boxType, int x, int y, int w, int h, const char* label) {
Expand Down
8 changes: 0 additions & 8 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,10 @@ import "unsafe"

type Box struct {
widget
eventHandler int
}

func NewBox(boxType BoxType, x, y, w, h int, text ...string) *Box {
b := &Box{}
initWidget(b, unsafe.Pointer(C.go_fltk_new_Box(C.int(boxType), C.int(x), C.int(y), C.int(w), C.int(h), cStringOpt(text))))
return b
}
func (b *Box) SetEventHandler(handler func(Event) bool) {
if b.eventHandler > 0 {
globalEventHandlerMap.unregister(b.eventHandler)
}
b.eventHandler = globalEventHandlerMap.register(handler)
C.go_fltk_Box_set_event_handler((*C.GBox)(b.ptr), C.int(b.eventHandler))
}
69 changes: 54 additions & 15 deletions button.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,75 @@
#include <FL/Fl_Return_Button.H>
#include <FL/Fl_Radio_Round_Button.H>

Fl_Button *go_fltk_new_Button(int x, int y, int w, int h, const char *label) {
return new Fl_Button(x, y, w, h, label);
#include "event_handler.h"


class GButton : public EventHandler<Fl_Button> {
public:
GButton(int x, int y, int w, int h, const char *label)
: EventHandler<Fl_Button>(x, y, w, h, label) {}
};

GButton *go_fltk_new_Button(int x, int y, int w, int h, const char *label) {
return new GButton(x, y, w, h, label);
}

char go_fltk_Button_value(Fl_Button *b) {
char go_fltk_Button_value(GButton *b) {
return b->value();
}
void go_fltk_Button_set_value(Fl_Button *b, int val) {
void go_fltk_Button_set_value(GButton *b, int val) {
b->value(val);
}
void go_fltk_Button_set_down_box(Fl_Button *b, int val) {
void go_fltk_Button_set_down_box(GButton *b, int val) {
b->down_box((Fl_Boxtype)val);
}

Fl_Check_Button *go_fltk_new_Check_Button(int x, int y, int w, int h, const char *label) {
return new Fl_Check_Button(x, y, w, h, label);
class GCheck_Button : public EventHandler<Fl_Check_Button> {
public:
GCheck_Button(int x, int y, int w, int h, const char *label)
: EventHandler<Fl_Check_Button>(x, y, w, h, label) {}
};

GCheck_Button *go_fltk_new_Check_Button(int x, int y, int w, int h, const char *label) {
return new GCheck_Button(x, y, w, h, label);
}

Fl_Toggle_Button *go_fltk_new_Toggle_Button(int x, int y, int w, int h, const char *label) {
return new Fl_Toggle_Button(x, y, w, h, label);
class GToggle_Button : public EventHandler<Fl_Toggle_Button> {
public:
GToggle_Button(int x, int y, int w, int h, const char *label)
: EventHandler<Fl_Toggle_Button>(x, y, w, h, label) {}
};

GToggle_Button *go_fltk_new_Toggle_Button(int x, int y, int w, int h, const char *label) {
return new GToggle_Button(x, y, w, h, label);
}

Fl_Radio_Button *go_fltk_new_Radio_Button(int x, int y, int w, int h, const char *label) {
return new Fl_Radio_Button(x, y, w, h, label);
class GRadio_Button : public EventHandler<Fl_Radio_Button> {
public:
GRadio_Button(int x, int y, int w, int h, const char *label)
: EventHandler<Fl_Radio_Button>(x, y, w, h, label) {}
};

GRadio_Button *go_fltk_new_Radio_Button(int x, int y, int w, int h, const char *label) {
return new GRadio_Button(x, y, w, h, label);
}

Fl_Return_Button *go_fltk_new_Return_Button(int x, int y, int w, int h, const char *label) {
return new Fl_Return_Button(x, y, w, h, label);
class GReturn_Button : public EventHandler<Fl_Return_Button> {
public:
GReturn_Button(int x, int y, int w, int h, const char *label)
: EventHandler<Fl_Return_Button>(x, y, w, h, label) {}
};

GReturn_Button *go_fltk_new_Return_Button(int x, int y, int w, int h, const char *label) {
return new GReturn_Button(x, y, w, h, label);
}

Fl_Radio_Round_Button *go_fltk_new_Radio_Round_Button(int x, int y, int w, int h, const char *label) {
return new Fl_Radio_Round_Button(x, y, w, h, label);
class GRadio_Round_Button : public EventHandler<Fl_Radio_Round_Button> {
public:
GRadio_Round_Button(int x, int y, int w, int h, const char *label)
: EventHandler<Fl_Radio_Round_Button>(x, y, w, h, label) {}
};

GRadio_Round_Button *go_fltk_new_Radio_Round_Button(int x, int y, int w, int h, const char *label) {
return new GRadio_Round_Button(x, y, w, h, label);
}
8 changes: 4 additions & 4 deletions button.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ func NewButton(x, y, w, h int, text ...string) *Button {
}

func (b *Button) Value() bool {
return C.go_fltk_Button_value((*C.Fl_Button)(b.ptr)) != C.char(0)
return C.go_fltk_Button_value((*C.GButton)(b.ptr)) != C.char(0)
}
func (b *Button) SetValue(val bool) {
if val {
C.go_fltk_Button_set_value((*C.Fl_Button)(b.ptr), 1)
C.go_fltk_Button_set_value((*C.GButton)(b.ptr), 1)
} else {
C.go_fltk_Button_set_value((*C.Fl_Button)(b.ptr), 0)
C.go_fltk_Button_set_value((*C.GButton)(b.ptr), 0)
}
}
func (b *Button) SetDownBox(box BoxType) {
C.go_fltk_Button_set_down_box((*C.Fl_Button)(b.ptr), C.int(box))
C.go_fltk_Button_set_down_box((*C.GButton)(b.ptr), C.int(box))
}

type CheckButton struct {
Expand Down
30 changes: 15 additions & 15 deletions button.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
extern "C" {
#endif

typedef struct Fl_Button Fl_Button;
typedef struct Fl_Check_Button Fl_Check_Button;
typedef struct Fl_Radio_Button Fl_Radio_Button;
typedef struct Fl_Toggle_Button Fl_Toggle_Button;
typedef struct Fl_Return_Button Fl_Return_Button;
typedef struct Fl_Radio_Round_Button Fl_Radio_Round_Button;
typedef struct GButton GButton;
typedef struct GCheck_Button GCheck_Button;
typedef struct GRadio_Button GRadio_Button;
typedef struct GToggle_Button GToggle_Button;
typedef struct GReturn_Button GReturn_Button;
typedef struct GRadio_Round_Button GRadio_Round_Button;

extern Fl_Button* go_fltk_new_Button(int x, int y, int w, int h, const char* label);
extern char go_fltk_Button_value(Fl_Button* b);
extern void go_fltk_Button_set_value(Fl_Button* b, int value);
extern void go_fltk_Button_set_down_box(Fl_Button* b, int value);
extern Fl_Check_Button *go_fltk_new_Check_Button(int x, int y, int w, int h, const char *label);
extern Fl_Radio_Button *go_fltk_new_Radio_Button(int x, int y, int w, int h, const char *label);
extern Fl_Toggle_Button *go_fltk_new_Toggle_Button(int x, int y, int w, int h, const char *label);
extern Fl_Return_Button *go_fltk_new_Return_Button(int x, int y, int w, int h, const char *label);
extern Fl_Radio_Round_Button *go_fltk_new_Radio_Round_Button(int x, int y, int w, int h, const char *label);
extern GButton* go_fltk_new_Button(int x, int y, int w, int h, const char* label);
extern char go_fltk_Button_value(GButton* b);
extern void go_fltk_Button_set_value(GButton* b, int value);
extern void go_fltk_Button_set_down_box(GButton* b, int value);
extern GCheck_Button *go_fltk_new_Check_Button(int x, int y, int w, int h, const char *label);
extern GRadio_Button *go_fltk_new_Radio_Button(int x, int y, int w, int h, const char *label);
extern GToggle_Button *go_fltk_new_Toggle_Button(int x, int y, int w, int h, const char *label);
extern GReturn_Button *go_fltk_new_Return_Button(int x, int y, int w, int h, const char *label);
extern GRadio_Round_Button *go_fltk_new_Radio_Round_Button(int x, int y, int w, int h, const char *label);

#ifdef __cplusplus
}
Expand Down
11 changes: 9 additions & 2 deletions choice.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

#include <FL/Fl_Choice.H>

#include "event_handler.h"

Fl_Choice *go_fltk_new_Choice(int x, int y, int w, int h, const char *label) {
return new Fl_Choice(x, y, w, h, label);
class GChoice : public EventHandler<Fl_Choice> {
public:
GChoice(int x, int y, int w, int h, const char *label)
: EventHandler<Fl_Choice>(x, y, w, h, label) {}
};

GChoice *go_fltk_new_Choice(int x, int y, int w, int h, const char *label) {
return new GChoice(x, y, w, h, label);
}

4 changes: 2 additions & 2 deletions choice.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
extern "C" {
#endif

typedef struct Fl_Choice Fl_Choice;
typedef struct GChoice GChoice;

extern Fl_Choice *go_fltk_new_Choice(int x, int y, int w, int h, const char *label);
extern GChoice *go_fltk_new_Choice(int x, int y, int w, int h, const char *label);

#ifdef __cplusplus
}
Expand Down
35 changes: 35 additions & 0 deletions event_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include "_cgo_export.h"


class WidgetWithEventHandler {
public:
virtual void set_event_handler(int handlerId) = 0;
};

template<class BaseWidget>
class EventHandler : public BaseWidget, public WidgetWithEventHandler {
public:
template<class... Arg>
EventHandler(Arg... args)
: BaseWidget(args...) {}

int handle(int event) final {
if (m_eventHandlerId >= 0) {
const int ret = _go_eventHandler(m_eventHandlerId, event);
if (ret != 0) {
return ret;
}
}
return BaseWidget::handle(event);
}

void set_event_handler(int handlerId) final {
m_eventHandlerId = handlerId;
}

protected:
int m_eventHandlerId = -1;

};
21 changes: 4 additions & 17 deletions gl_window.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,35 @@

#include <FL/Fl_Gl_Window.H>

#include "event_handler.h"

#include "_cgo_export.h"


class GGlWindow : public Fl_Gl_Window {
class GGlWindow : public EventHandler<Fl_Gl_Window> {
public:
GGlWindow(int x, int y, int w, int h, uintptr_t drawFunId)
: Fl_Gl_Window(x, y, w, h)
: EventHandler<Fl_Gl_Window>(x, y, w, h)
, m_drawFunId(drawFunId) {
mode(FL_OPENGL3 | FL_MULTISAMPLE);
}
void draw() final {
_go_callbackHandler(m_drawFunId);
}

int handle(int event) final {
if (m_eventHandlerId >= 0) {
const int ret = _go_eventHandler(m_eventHandlerId, event);
if (ret != 0) {
return ret;
}
}
return Fl_Gl_Window::handle(event);
}

void resize(int x, int y, int w, int h) final {
Fl_Gl_Window::resize(x, y, w, h);
if (m_resizeHandlerId != 0) {
_go_callbackHandler(m_resizeHandlerId);
}
}

void set_event_handler(int handlerId) {
m_eventHandlerId = handlerId;
}

void set_resize_handler(uintptr_t resizeHandlerId) {
m_resizeHandlerId = resizeHandlerId;
}

private:
uintptr_t const m_drawFunId;
int m_eventHandlerId = -1;
uintptr_t m_resizeHandlerId = 0;
};

Expand Down
18 changes: 4 additions & 14 deletions gl_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ package fltk
#include "gl_window.h"
*/
import "C"
import "unsafe"
import (
"unsafe"
)

type GlWindow struct {
Window
drawFunId uintptr
resizeHandlerId uintptr
eventHandler int
}

func NewGlWindow(x, y, w, h int, drawFun func()) *GlWindow {
win := &GlWindow{eventHandler: -1}
win := &GlWindow{}
win.drawFunId = globalCallbackMap.register(drawFun)
initWidget(win, unsafe.Pointer(C.go_fltk_new_GlWindow(C.int(x), C.int(y), C.int(w), C.int(h), C.uintptr_t(win.drawFunId))))
return win
Expand All @@ -32,10 +33,6 @@ func (w *GlWindow) Destroy() {
globalCallbackMap.unregister(w.resizeHandlerId)
}
w.resizeHandlerId = 0
if w.eventHandler > 0 {
globalEventHandlerMap.unregister(w.eventHandler)
}
w.eventHandler = 0
w.Window.Destroy()
}
func (w *GlWindow) ContextValid() bool {
Expand All @@ -47,13 +44,6 @@ func (w *GlWindow) Valid() bool {
func (w *GlWindow) CanDo() bool {
return C.go_fltk_Gl_Window_can_do((*C.GGlWindow)(w.ptr)) != 0
}
func (w *GlWindow) SetEventHandler(handler func(Event) bool) {
if w.eventHandler > 0 {
globalEventHandlerMap.unregister(w.eventHandler)
}
w.eventHandler = globalEventHandlerMap.register(handler)
C.go_fltk_Gl_Window_set_event_handler((*C.GGlWindow)(w.ptr), C.int(w.eventHandler))
}

func (w *GlWindow) SetResizeHandler(handler func()) {
if w.resizeHandlerId > 0 {
Expand Down
Loading

0 comments on commit 112a585

Please sign in to comment.