From 809662459dcd2cbe0b42f338413b88fea0483086 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 26 Aug 2018 13:18:53 -0400 Subject: Migrate the link_darwin_amd64.go and control.go files back; fixed up errors in the C files and util.go. --- BBB_GOFILES/control.go | 145 --------------------------------------- BBB_GOFILES/link_darwin_amd64.go | 7 -- control.go | 145 +++++++++++++++++++++++++++++++++++++++ link_darwin_amd64.go | 7 ++ pkgui.c | 2 +- pkgui.h | 2 +- util.go | 1 + 7 files changed, 155 insertions(+), 154 deletions(-) delete mode 100644 BBB_GOFILES/control.go delete mode 100644 BBB_GOFILES/link_darwin_amd64.go create mode 100644 control.go create mode 100644 link_darwin_amd64.go diff --git a/BBB_GOFILES/control.go b/BBB_GOFILES/control.go deleted file mode 100644 index 23a8ddf..0000000 --- a/BBB_GOFILES/control.go +++ /dev/null @@ -1,145 +0,0 @@ -// 12 december 2015 - -package ui - -import ( - "unsafe" -) - -// #include "ui.h" -import "C" - -// no need to lock this; only the GUI thread can access it -var controls = make(map[*C.uiControl]Control) - -// Control represents a GUI control. It provdes methods -// common to all Controls. -// -// The preferred way to create new Controls is to use -// ControlBase; see ControlBase below. -type Control interface { - // LibuiControl returns the uiControl pointer for the Control. - // This is intended for use when adding a control to a - // container. - LibuiControl() uintptr - - // Destroy destroys the Control. - Destroy() - - // Handle returns the OS-level handle that backs the - // Control. On OSs that use reference counting for - // controls, Handle does not increment the reference - // count; you are sharing package ui's reference. - Handle() uintptr - - // Visible returns whether the Control is visible. - Visible() bool - - // Show shows the Control. - Show() - - // Hide shows the Control. Hidden controls do not participate - // in layout (that is, Box, Grid, etc. does not reserve space for - // hidden controls). - Hide() - - // Enabled returns whether the Control is enabled. - Enabled() bool - - // Enable enables the Control. - Enable() - - // Disable disables the Control. - Disable() -} - -// ControlBase is an implementation of Control that provides -// all the methods that Control requires. To use it, embed a -// ControlBase (not a *ControlBase) into your structure, then -// assign the result of NewControlBase to that field: -// -// type MyControl struct { -// ui.ControlBase -// c *C.MyControl -// } -// -// func NewMyControl() *MyControl { -// m := &NewMyControl{ -// c: C.newMyControl(), -// } -// m.ControlBase = ui.NewControlBase(m, uintptr(unsafe.Pointer(c))) -// return m -// } -type ControlBase struct { - iface Control - c *C.uiControl -} - -// NewControlBase creates a new ControlBase. See the -// documentation of ControlBase for an example. -// NewControl should only be called once per instance of Control. -func NewControlBase(iface Control, c uintptr) ControlBase { - b := ControlBase{ - iface: iface, - c: (*C.uiControl)(unsafe.Pointer(c)), - } - controls[b.c] = b.iface - return b -} - -func (c *ControlBase) LibuiControl() uintptr { - return uintptr(unsafe.Pointer(c.c)) -} - -func (c *ControlBase) Destroy() { - delete(controls, c.c) - C.uiControlDestroy(c.c) -} - -func (c *ControlBase) Handle() uintptr { - return uintptr(C.uiControlHandle(c.c)) -} - -func (c *ControlBase) Visible() bool { - return tobool(C.uiControlVisible(c.c)) -} - -func (c *ControlBase) Show() { - C.uiControlShow(c.c) -} - -func (c *ControlBase) Hide() { - C.uiControlHide(c.c) -} - -func (c *ControlBase) Enabled() bool { - return tobool(C.uiControlEnabled(c.c)) -} - -func (c *ControlBase) Enable() { - C.uiControlEnable(c.c) -} - -func (c *ControlBase) Disable() { - C.uiControlDisable(c.c) -} - -// ControlFromLibui returns the Control associated with a libui -// uiControl. This is intended for implementing event handlers -// on the Go side, to prevent sharing Go pointers with C. -// This function only works on Controls that use ControlBase. -func ControlFromLibui(c uintptr) Control { - // comma-ok form to avoid creating nil entries - cc, _ := controls[(*C.uiControl)(unsafe.Pointer(c))] - return cc -} - -func touiControl(c uintptr) *C.uiControl { - return (*C.uiControl)(unsafe.Pointer(c)) -} - -// LibuiFreeText allows implementations of Control -// to call the libui function uiFreeText. -func LibuiFreeText(c uintptr) { - C.uiFreeText((*C.char)(unsafe.Pointer(c))) -} diff --git a/BBB_GOFILES/link_darwin_amd64.go b/BBB_GOFILES/link_darwin_amd64.go deleted file mode 100644 index ef9c2e5..0000000 --- a/BBB_GOFILES/link_darwin_amd64.go +++ /dev/null @@ -1,7 +0,0 @@ -// 13 december 2015 - -package ui - -// #cgo CFLAGS: -mmacosx-version-min=10.8 -// #cgo LDFLAGS: ${SRCDIR}/libui_darwin_amd64.a -framework Foundation -framework AppKit -mmacosx-version-min=10.8 -import "C" diff --git a/control.go b/control.go new file mode 100644 index 0000000..ab8673c --- /dev/null +++ b/control.go @@ -0,0 +1,145 @@ +// 12 december 2015 + +package ui + +import ( + "unsafe" +) + +// #include "pkgui.h" +import "C" + +// no need to lock this; only the GUI thread can access it +var controls = make(map[*C.uiControl]Control) + +// Control represents a GUI control. It provdes methods +// common to all Controls. +// +// The preferred way to create new Controls is to use +// ControlBase; see ControlBase below. +type Control interface { + // LibuiControl returns the uiControl pointer for the Control. + // This is intended for use when adding a control to a + // container. + LibuiControl() uintptr + + // Destroy destroys the Control. + Destroy() + + // Handle returns the OS-level handle that backs the + // Control. On OSs that use reference counting for + // controls, Handle does not increment the reference + // count; you are sharing package ui's reference. + Handle() uintptr + + // Visible returns whether the Control is visible. + Visible() bool + + // Show shows the Control. + Show() + + // Hide shows the Control. Hidden controls do not participate + // in layout (that is, Box, Grid, etc. does not reserve space for + // hidden controls). + Hide() + + // Enabled returns whether the Control is enabled. + Enabled() bool + + // Enable enables the Control. + Enable() + + // Disable disables the Control. + Disable() +} + +// ControlBase is an implementation of Control that provides +// all the methods that Control requires. To use it, embed a +// ControlBase (not a *ControlBase) into your structure, then +// assign the result of NewControlBase to that field: +// +// type MyControl struct { +// ui.ControlBase +// c *C.MyControl +// } +// +// func NewMyControl() *MyControl { +// m := &NewMyControl{ +// c: C.newMyControl(), +// } +// m.ControlBase = ui.NewControlBase(m, uintptr(unsafe.Pointer(c))) +// return m +// } +type ControlBase struct { + iface Control + c *C.uiControl +} + +// NewControlBase creates a new ControlBase. See the +// documentation of ControlBase for an example. +// NewControl should only be called once per instance of Control. +func NewControlBase(iface Control, c uintptr) ControlBase { + b := ControlBase{ + iface: iface, + c: (*C.uiControl)(unsafe.Pointer(c)), + } + controls[b.c] = b.iface + return b +} + +func (c *ControlBase) LibuiControl() uintptr { + return uintptr(unsafe.Pointer(c.c)) +} + +func (c *ControlBase) Destroy() { + delete(controls, c.c) + C.uiControlDestroy(c.c) +} + +func (c *ControlBase) Handle() uintptr { + return uintptr(C.uiControlHandle(c.c)) +} + +func (c *ControlBase) Visible() bool { + return tobool(C.uiControlVisible(c.c)) +} + +func (c *ControlBase) Show() { + C.uiControlShow(c.c) +} + +func (c *ControlBase) Hide() { + C.uiControlHide(c.c) +} + +func (c *ControlBase) Enabled() bool { + return tobool(C.uiControlEnabled(c.c)) +} + +func (c *ControlBase) Enable() { + C.uiControlEnable(c.c) +} + +func (c *ControlBase) Disable() { + C.uiControlDisable(c.c) +} + +// ControlFromLibui returns the Control associated with a libui +// uiControl. This is intended for implementing event handlers +// on the Go side, to prevent sharing Go pointers with C. +// This function only works on Controls that use ControlBase. +func ControlFromLibui(c uintptr) Control { + // comma-ok form to avoid creating nil entries + cc, _ := controls[(*C.uiControl)(unsafe.Pointer(c))] + return cc +} + +func touiControl(c uintptr) *C.uiControl { + return (*C.uiControl)(unsafe.Pointer(c)) +} + +// LibuiFreeText allows implementations of Control +// to call the libui function uiFreeText. +func LibuiFreeText(c uintptr) { + C.uiFreeText((*C.char)(unsafe.Pointer(c))) +} diff --git a/link_darwin_amd64.go b/link_darwin_amd64.go new file mode 100644 index 0000000..ef9c2e5 --- /dev/null +++ b/link_darwin_amd64.go @@ -0,0 +1,7 @@ +// 13 december 2015 + +package ui + +// #cgo CFLAGS: -mmacosx-version-min=10.8 +// #cgo LDFLAGS: ${SRCDIR}/libui_darwin_amd64.a -framework Foundation -framework AppKit -mmacosx-version-min=10.8 +import "C" diff --git a/pkgui.c b/pkgui.c index 7caab2c..586f5e9 100644 --- a/pkgui.c +++ b/pkgui.c @@ -1,6 +1,6 @@ // 26 august 2018 #include "pkgui.h" -#include "xxxxx" +#include "_cgo_export.h" uiInitOptions *pkguiAllocInitOptions(void) { diff --git a/pkgui.h b/pkgui.h index 2af5f2b..9aaa394 100644 --- a/pkgui.h +++ b/pkgui.h @@ -1,5 +1,5 @@ // 12 august 2018 -#include +#include #include "ui.h" // main.go diff --git a/util.go b/util.go index 667f679..e7fb937 100644 --- a/util.go +++ b/util.go @@ -12,6 +12,7 @@ import "C" //export pkguiAlloc func pkguiAlloc(n C.size_t) unsafe.Pointer { // cgo turns C.malloc() into a panic-on-OOM version; use it + // TODO make sure it zero-initializes too return C.malloc(n) } -- cgit v1.2.3