2023-10-22 22:00:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
#cgo pkg-config: Qt5Widgets
|
|
|
|
#include "binding.h"
|
|
|
|
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2024-08-18 04:42:11 +00:00
|
|
|
"runtime/cgo"
|
2023-10-22 22:00:00 +00:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CArray(data []string) (C.int, **C.char) {
|
|
|
|
|
2024-09-01 05:51:17 +00:00
|
|
|
c_argv := (*[0xfff]*C.char)(C.malloc(C.size_t(8 /* sizeof pointer */ * len(data))))
|
2023-10-22 22:00:00 +00:00
|
|
|
|
|
|
|
for i, arg := range data {
|
|
|
|
c_argv[i] = C.CString(arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.int(len(data)), (**C.char)(unsafe.Pointer(c_argv))
|
|
|
|
}
|
|
|
|
|
2024-08-18 04:42:11 +00:00
|
|
|
type CallbackFunc func(argc C.int, args *C.void)
|
|
|
|
|
|
|
|
//export miqt_exec_callback
|
2024-10-13 05:31:18 +00:00
|
|
|
func miqt_exec_callback(cb C.intptr_t, argc C.int, args *C.void) {
|
2024-08-18 04:42:11 +00:00
|
|
|
// Our CABI for all callbacks is void(int, void*).
|
|
|
|
// Our Go ABI is CallbackFunc
|
|
|
|
// Then the Go bindings can unmarshal the arguments and C.free() them as necessary
|
2024-10-13 05:31:18 +00:00
|
|
|
cfunc, ok := (cgo.Handle(cb)).Value().(CallbackFunc)
|
2024-08-18 04:42:11 +00:00
|
|
|
if !ok {
|
|
|
|
panic("miqt: callback of non-callback type (heap corruption?)")
|
|
|
|
}
|
|
|
|
|
|
|
|
cfunc(argc, args)
|
|
|
|
}
|
|
|
|
|
2023-10-22 22:00:00 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
type QApplication struct {
|
|
|
|
h C.PQApplication
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewQApplication(args []string) *QApplication {
|
2024-08-05 22:22:29 +00:00
|
|
|
argc, argv := CArray(args)
|
|
|
|
h := C.QApplication_new(&argc, argv)
|
2023-10-22 22:00:00 +00:00
|
|
|
return &QApplication{h: h}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *QApplication) Exec() int {
|
|
|
|
ret := C.QApplication_exec(this.h)
|
|
|
|
return int(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
type QWidget struct {
|
|
|
|
h C.PQWidget
|
|
|
|
}
|
|
|
|
|
2024-08-05 22:23:42 +00:00
|
|
|
func (this *QWidget) CPointer() C.PQWidget {
|
|
|
|
if this == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return this.h
|
|
|
|
}
|
|
|
|
|
2023-10-22 22:00:00 +00:00
|
|
|
func NewQWidget() *QWidget {
|
|
|
|
ret := C.QWidget_new()
|
|
|
|
return &QWidget{h: ret}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *QWidget) Show() {
|
|
|
|
C.QWidget_show(this.h)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
type QPushButton struct {
|
|
|
|
h C.PQPushButton
|
|
|
|
}
|
|
|
|
|
2024-08-05 22:23:42 +00:00
|
|
|
func NewQPushButton(label string, parent *QWidget) *QPushButton {
|
|
|
|
h := C.QPushButton_new(C.CString(label), parent.CPointer())
|
2023-10-22 22:00:00 +00:00
|
|
|
return &QPushButton{h: h}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *QPushButton) Show() {
|
|
|
|
C.QPushButton_show(this.h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *QPushButton) AsQWidget() *QWidget {
|
|
|
|
return &QWidget{h: C.PQWidget(this.h)} // Type cast
|
|
|
|
}
|
2024-08-18 04:42:11 +00:00
|
|
|
|
|
|
|
func (this *QPushButton) OnPressed(cb func()) {
|
|
|
|
var cbWrapper CallbackFunc = func(argc C.int, args *C.void) {
|
|
|
|
// Unmarshal arguments
|
|
|
|
// Pressed() doesn't take any, though
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
|
2024-10-13 05:31:18 +00:00
|
|
|
C.QPushButton_connect_pressed(this.h, C.intptr_t(cgo.NewHandle(cbWrapper)))
|
2024-08-18 04:42:11 +00:00
|
|
|
// TODO allow disconnect'ing, or tie lifespan for handle.Delete(), ...
|
|
|
|
}
|