More control conversion and syncing.

This commit is contained in:
Pietro Gagliardi 2018-08-11 19:52:29 -04:00
parent 97c3d186f1
commit 5ab5777d4c
6 changed files with 120 additions and 311 deletions

View File

@ -1,120 +0,0 @@
// 12 december 2015
package ui
import (
"unsafe"
)
// #include "ui.h"
// extern void doEditableComboboxOnChanged(uiCombobox *, void *);
// static inline void realuiEditableComboboxOnChanged(uiCombobox *c)
// {
// uiEditableComboboxOnChanged(c, doEditableComboboxOnChanged, NULL);
// }
import "C"
// no need to lock this; only the GUI thread can access it
var editableComboboxes = make(map[*C.uiEditableCombobox]*Combobox)
// EditableCombobox is a Control that represents a drop-down list
// of strings that the user can choose one of at any time. It also has
// an entry field that the user can type an alternate choice into.
type EditableCombobox struct {
co *C.uiControl
c *C.uiEditableCombobox
onChanged func(*EditableCombobox)
}
// NewEditableCombobox creates a new EditableCombobox.
func NewEditableCombobox() *EditableCombobox {
c := new(EditableCombobox)
c.c = C.uiNewEditableCombobox()
c.co = (*C.uiControl)(unsafe.Pointer(c.c))
C.realuiEditableComboboxOnChanged(c.c)
editableComboboxes[c.c] = c
return c
}
// Destroy destroys the EditableCombobox.
func (c *Combobox) Destroy() {
delete(editableComboboxes, c.c)
C.uiControlDestroy(c.co)
}
// LibuiControl returns the libui uiControl pointer that backs
// the EditableCombobox. This is only used by package ui itself and
// should not be called by programs.
func (c *Combobox) LibuiControl() uintptr {
return uintptr(unsafe.Pointer(c.co))
}
// Handle returns the OS-level handle associated with this EditableCombobox.
// On Windows this is an HWND of a standard Windows API COMBOBOX
// class (as provided by Common Controls version 6).
// On GTK+ this is a pointer to a GtkComboBoxText.
// On OS X this is a pointer to a NSComboBox.
func (c *Combobox) Handle() uintptr {
return uintptr(C.uiControlHandle(c.co))
}
// Show shows the EditableCombobox.
func (c *Combobox) Show() {
C.uiControlShow(c.co)
}
// Hide hides the EditableCombobox.
func (c *Combobox) Hide() {
C.uiControlHide(c.co)
}
// Enable enables the EditableCombobox.
func (c *Combobox) Enable() {
C.uiControlEnable(c.co)
}
// Disable disables the EditableCombobox.
func (c *Combobox) Disable() {
C.uiControlDisable(c.co)
}
// Append adds the named item to the end of the EditableCombobox.
func (c *Combobox) Append(text string) {
ctext := C.CString(text)
C.uiComboboxAppend(c.c, ctext)
freestr(ctext)
}
// Text returns the text in the entry of the EditableCombobox, which
// could be one of the choices in the list if the user has selected one.
func (c *Combobox) Text() string {
ctext := C.uiEditableComboboxText(c.c)
text := C.GoString(ctext)
C.uiFreeText(ctext)
return text
}
// SetText sets the text in the entry of the EditableCombobox.
func (c *Combobox) SetText(index int) {
ctext := C.CString(text)
C.uiEditableComboboxSetText(c.c, ctext)
freestr(ctext)
}
// OnChanged registers f to be run when the user selects an item in
// the Combobox. Only one function can be registered at a time.
func (c *Combobox) OnChanged(f func(*Combobox)) {
c.onChanged = f
}
//export doComboboxOnChanged
func doComboboxOnChanged(cc *C.uiCombobox, data unsafe.Pointer) {
c := editableComboboxes[cc]
if c.onChanged != nil {
c.onChanged(c)
}
}

View File

@ -8,22 +8,14 @@ import (
// #include "ui.h" // #include "ui.h"
// extern void doComboboxOnSelected(uiCombobox *, void *); // extern void doComboboxOnSelected(uiCombobox *, void *);
// static inline void realuiComboboxOnSelected(uiCombobox *c)
// {
// uiComboboxOnSelected(c, doComboboxOnSelected, NULL);
// }
import "C" import "C"
// no need to lock this; only the GUI thread can access it
var comboboxes = make(map[*C.uiCombobox]*Combobox)
// Combobox is a Control that represents a drop-down list of strings // Combobox is a Control that represents a drop-down list of strings
// that the user can choose one of at any time. For a Combobox that // that the user can choose one of at any time. For a Combobox that
// users can type values into, see EditableCombobox. // users can type values into, see EditableCombobox.
type Combobox struct { type Combobox struct {
co *C.uiControl ControlBase
c *C.uiCombobox c *C.uiCombobox
onSelected func(*Combobox) onSelected func(*Combobox)
} }
@ -32,56 +24,13 @@ func NewCombobox() *Combobox {
c := new(Combobox) c := new(Combobox)
c.c = C.uiNewCombobox() c.c = C.uiNewCombobox()
c.co = (*C.uiControl)(unsafe.Pointer(c.c))
C.realuiComboboxOnSelected(c.c) C.uiComboboxOnSelected(c.c, C.doComboboxOnSelected, nil)
comboboxes[c.c] = c
c.ControlBase = NewControlBase(c, uintptr(unsafe.Pointer(c.c)))
return c return c
} }
// Destroy destroys the Combobox.
func (c *Combobox) Destroy() {
delete(comboboxes, c.c)
C.uiControlDestroy(c.co)
}
// LibuiControl returns the libui uiControl pointer that backs
// the Window. This is only used by package ui itself and should
// not be called by programs.
func (c *Combobox) LibuiControl() uintptr {
return uintptr(unsafe.Pointer(c.co))
}
// Handle returns the OS-level handle associated with this Combobox.
// On Windows this is an HWND of a standard Windows API COMBOBOX
// class (as provided by Common Controls version 6).
// On GTK+ this is a pointer to a GtkComboBoxText.
// On OS X this is a pointer to a NSPopUpButton.
func (c *Combobox) Handle() uintptr {
return uintptr(C.uiControlHandle(c.co))
}
// Show shows the Combobox.
func (c *Combobox) Show() {
C.uiControlShow(c.co)
}
// Hide hides the Combobox.
func (c *Combobox) Hide() {
C.uiControlHide(c.co)
}
// Enable enables the Combobox.
func (c *Combobox) Enable() {
C.uiControlEnable(c.co)
}
// Disable disables the Combobox.
func (c *Combobox) Disable() {
C.uiControlDisable(c.co)
}
// Append adds the named item to the end of the Combobox. // Append adds the named item to the end of the Combobox.
func (c *Combobox) Append(text string) { func (c *Combobox) Append(text string) {
ctext := C.CString(text) ctext := C.CString(text)
@ -109,7 +58,7 @@ func (c *Combobox) OnSelected(f func(*Combobox)) {
//export doComboboxOnSelected //export doComboboxOnSelected
func doComboboxOnSelected(cc *C.uiCombobox, data unsafe.Pointer) { func doComboboxOnSelected(cc *C.uiCombobox, data unsafe.Pointer) {
c := comboboxes[cc] c := ControlFromLibui(uintptr(unsafe.Pointer(cc))).(*Combobox)
if c.onSelected != nil { if c.onSelected != nil {
c.onSelected(c) c.onSelected(c)
} }

View File

@ -92,7 +92,7 @@ func (c *ControlBase) Handle() uintptr {
} }
func (c *ControlBase) Visible() bool { func (c *ControlBase) Visible() bool {
return frombool(C.uiControlVisible(c.c)) return tobool(C.uiControlVisible(c.c))
} }
func (c *ControlBase) Show() { func (c *ControlBase) Show() {
@ -104,7 +104,7 @@ func (c *ControlBase) Hide() {
} }
func (c *ControlBase) Enabled() bool { func (c *ControlBase) Enabled() bool {
return frombool(C.uiControlEnabled(c.c)) return tobool(C.uiControlEnabled(c.c))
} }
func (c *ControlBase) Enable() { func (c *ControlBase) Enable() {

View File

@ -15,100 +15,43 @@ import (
// return (struct tm *) malloc(sizeof (struct tm)); // return (struct tm *) malloc(sizeof (struct tm));
// } // }
// extern void doDateTimePickerChanged(uiDateTimePicker *, void *); // extern void doDateTimePickerChanged(uiDateTimePicker *, void *);
// static inline void realuiDateTimePickerOnChanged(uiDateTimePicker *d)
// {
// uiDateTimePickerOnChanged(d, doDateTimePickerOnChanged, NULL);
// }
import "C" import "C"
// DateTimePicker is a Control that represents a field where the user // DateTimePicker is a Control that represents a field where the user
// can enter a date and/or a time. // can enter a date and/or a time.
type DateTimePicker struct { type DateTimePicker struct {
c *C.uiControl ControlBase
d *C.uiDateTimePicker d *C.uiDateTimePicker
onChanged func(*DateTimePicker) onChanged func(*DateTimePicker)
} }
func finishNewDateTimePicker(dd *C.uiDateTimePicker) *DateTimePicker {
d := new(DateTimePicker)
d.d = dd
C.uiDateTimePickerOnChanged(d.d, C.doDateTimePickerOnChanged, nil)
d.ControlBase = NewControlBase(d, uintptr(unsafe.Pointer(d.d)))
return d
}
// NewDateTimePicker creates a new DateTimePicker that shows // NewDateTimePicker creates a new DateTimePicker that shows
// both a date and a time. // both a date and a time.
func NewDateTimePicker() *DateTimePicker { func NewDateTimePicker() *DateTimePicker {
d := new(DateTimePicker) return finishNewDateTImePicker(C.uiNewDateTimePicker())
d.d = C.uiNewDateTimePicker()
d.c = (*C.uiControl)(unsafe.Pointer(d.d))
C.realuiDateTimePickerOnChanged(d.d)
return d
} }
// NewDatePicker creates a new DateTimePicker that shows // NewDatePicker creates a new DateTimePicker that shows
// only a date. // only a date.
func NewDatePicker() *DateTimePicker { func NewDatePicker() *DateTimePicker {
d := new(DateTimePicker) return finishNewDateTimePicker(C.uiNewDatePicker())
d.d = C.uiNewDatePicker()
d.c = (*C.uiControl)(unsafe.Pointer(d.d))
C.realuiDateTimePickerOnChanged(d.d)
return d
} }
// NewTimePicker creates a new DateTimePicker that shows // NewTimePicker creates a new DateTimePicker that shows
// only a time. // only a time.
func NewTimePicker() *DateTimePicker { func NewTimePicker() *DateTimePicker {
d := new(DateTimePicker) return finishNewDateTimePicker(C.uiNewTimePicker())
d.d = C.uiNewTimePicker()
d.c = (*C.uiControl)(unsafe.Pointer(d.d))
C.realuiDateTimePickerOnChanged(d.d)
return d
}
// Destroy destroys the DateTimePicker.
func (d *DateTimePicker) Destroy() {
C.uiControlDestroy(d.c)
}
// LibuiControl returns the libui uiControl pointer that backs
// the Window. This is only used by package ui itself and should
// not be called by programs.
func (d *DateTimePicker) LibuiControl() uintptr {
return uintptr(unsafe.Pointer(d.c))
}
// Handle returns the OS-level handle associated with this DateTimePicker.
// On Windows this is an HWND of a standard Windows API
// DATETIMEPICK_CLASS class (as provided by Common Controls
// version 6).
// On GTK+ this is a pointer to a libui-internal class.
// On OS X this is a pointer to a NSDatePicker.
func (d *DateTimePicker) Handle() uintptr {
return uintptr(C.uiControlHandle(d.c))
}
// Show shows the DateTimePicker.
func (d *DateTimePicker) Show() {
C.uiControlShow(d.c)
}
// Hide hides the DateTimePicker.
func (d *DateTimePicker) Hide() {
C.uiControlHide(d.c)
}
// Enable enables the DateTimePicker.
func (d *DateTimePicker) Enable() {
C.uiControlEnable(d.c)
}
// Disable disables the DateTimePicker.
func (d *DateTimePicker) Disable() {
C.uiControlDisable(d.c)
} }
// Time returns the time stored in the uiDateTimePicker. // Time returns the time stored in the uiDateTimePicker.
@ -128,8 +71,8 @@ func (d *DateTimePicker) Time() time.Time {
} }
// SetTime sets the time in the DateTimePicker to t. // SetTime sets the time in the DateTimePicker to t.
// t's components are read as-is; no time zone manipulations // t's components are read as-is via t.Date() and t.Time();
// are done. // no time zone manipulations are done.
func (d *DateTimePicker) SetTime(t time.Time) { func (d *DateTimePicker) SetTime(t time.Time) {
tm := C.allocTimeStruct() tm := C.allocTimeStruct()
defer C.free(unsafe.Pointer(tm)) defer C.free(unsafe.Pointer(tm))
@ -145,15 +88,16 @@ func (d *DateTimePicker) SetTime(t time.Time) {
C.uiDateTimePickerSetTime(d.d, tm) C.uiDateTimePickerSetTime(d.d, tm)
} }
// OnChanged registers f to be run when the user changes the time in the DateTimePicker. // OnChanged registers f to be run when the user changes the time
// Only one function can be registered at a time. // in the DateTimePicker. Only one function can be registered at a
// time.
func (d *DateTimePicker) OnChanged(f func(*DateTimePicker)) { func (d *DateTimePicker) OnChanged(f func(*DateTimePicker)) {
d.onChanged = f d.onChanged = f
} }
//export doDateTimePickerOnChanged //export doDateTimePickerOnChanged
func doDateTimePickerOnChanged(dd *C.uiDateTimePicker, data unsafe.Pointer) { func doDateTimePickerOnChanged(dd *C.uiDateTimePicker, data unsafe.Pointer) {
d := dateTimePickers[dd] d := ControlFromLibui(uintptr(unsafe.Pointer(dd)).(*DateTimePicker)
if d.onChanged != nil { if d.onChanged != nil {
d.onChanged(d) d.onChanged(d)
} }

70
editablecombobox.go Normal file
View File

@ -0,0 +1,70 @@
// 12 december 2015
package ui
import (
"unsafe"
)
// #include "ui.h"
// extern void doEditableComboboxOnChanged(uiCombobox *, void *);
import "C"
// EditableCombobox is a Control that represents a drop-down list
// of strings that the user can choose one of at any time. It also has
// an entry field that the user can type an alternate choice into.
type EditableCombobox struct {
ControlBase
c *C.uiEditableCombobox
onChanged func(*EditableCombobox)
}
// NewEditableCombobox creates a new EditableCombobox.
func NewEditableCombobox() *EditableCombobox {
c := new(EditableCombobox)
c.c = C.uiNewEditableCombobox()
C.uiEditableComboboxOnChanged(c.c, C.doEditableComboboxOnChanged, nil)
c.ControlBase = NewControlBase(c, uintptr(unsafe.Pointer(c.c)))
return c
}
// Append adds the named item to the end of the EditableCombobox.
func (e *EditableCombobox) Append(text string) {
ctext := C.CString(text)
C.uiEditableComboboxAppend(e.c, ctext)
freestr(ctext)
}
// Text returns the text in the entry of the EditableCombobox, which
// could be one of the choices in the list if the user has selected one.
func (e *EditableCombobox) Text() string {
ctext := C.uiEditableComboboxText(e.c)
text := C.GoString(ctext)
C.uiFreeText(ctext)
return text
}
// SetText sets the text in the entry of the EditableCombobox.
func (e *EditableCombobox) SetText(index int) {
ctext := C.CString(text)
C.uiEditableComboboxSetText(e.c, ctext)
freestr(ctext)
}
// OnChanged registers f to be run when the user either selects an
// item or changes the text in the EditableCombobox. Only one
// function can be registered at a time.
func (e *EditableCombobox) OnChanged(f func(*EditableCombobox)) {
c.onChanged = f
}
//export doEditableComboboxOnChanged
func doEditableComboboxOnChanged(cc *C.uiEditableCombobox, data unsafe.Pointer) {
e := ControlFromLibui(uintptr(unsafe.Pointer(cc))).(*EditableCombobox)
if e.onChanged != nil {
e.onChanged(e)
}
}

View File

@ -12,77 +12,43 @@ import (
// #include "ui.h" // #include "ui.h"
// extern void doEntryOnChanged(uiEntry *, void *); // extern void doEntryOnChanged(uiEntry *, void *);
// static inline void realuiEntryOnChanged(uiEntry *b)
// {
// uiEntryOnChanged(b, doEntryOnChanged, NULL);
// }
import "C" import "C"
// no need to lock this; only the GUI thread can access it
var entries = make(map[*C.uiEntry]*Entry)
// Entry is a Control that represents a space that the user can // Entry is a Control that represents a space that the user can
// type a single line of text into. // type a single line of text into.
type Entry struct { type Entry struct {
c *C.uiControl ControlBase
e *C.uiEntry e *C.uiEntry
onChanged func(*Entry) onChanged func(*Entry)
} }
func finishNewEntry(ee *C.uiEntry) *Entry {
e := new(Entry)
e.e = ee
C.uiEntryOnChanged(e.e, C.doEntryOnChanged, nil)
e.ControlBase = NewControlBase(e, uintptr(unsafe.Pointer(e.e)))
return e
}
// NewEntry creates a new Entry. // NewEntry creates a new Entry.
func NewEntry() *Entry { func NewEntry() *Entry {
e := new(Entry) return finishNewEntry(C.uiNewEntry())
e.e = C.uiNewEntry()
e.c = (*C.uiControl)(unsafe.Pointer(e.e))
C.realuiEntryOnChanged(e.e)
entries[e.e] = e
return e
} }
// Destroy destroys the Entry. // NewPasswordEntry creates a new Entry whose contents are
func (e *Entry) Destroy() { // visibly obfuscated, suitable for passwords.
delete(entries, e.e) func NewPasswordEntry() *Entry {
C.uiControlDestroy(e.c) return finishNewEntry(C.uiNewPasswordEntry())
} }
// LibuiControl returns the libui uiControl pointer that backs // NewSearchEntry creates a new Entry suitable for searching with.
// the Window. This is only used by package ui itself and should // Changed events may, depending on the system, be delayed
// not be called by programs. // with a search Entry, to produce a smoother user experience.
func (e *Entry) LibuiControl() uintptr { func NewSearchEntry() *Entry {
return uintptr(unsafe.Pointer(e.c)) return finishNewEntry(C.uiNewSearchEntry())
}
// Handle returns the OS-level handle associated with this Entry.
// On Windows this is an HWND of a standard Windows API EDIT
// class (as provided by Common Controls version 6).
// On GTK+ this is a pointer to a GtkEntry.
// On OS X this is a pointer to a NSTextField.
func (e *Entry) Handle() uintptr {
return uintptr(C.uiControlHandle(e.c))
}
// Show shows the Entry.
func (e *Entry) Show() {
C.uiControlShow(e.c)
}
// Hide hides the Entry.
func (e *Entry) Hide() {
C.uiControlHide(e.c)
}
// Enable enables the Entry.
func (e *Entry) Enable() {
C.uiControlEnable(e.c)
}
// Disable disables the Entry.
func (e *Entry) Disable() {
C.uiControlDisable(e.c)
} }
// Text returns the Entry's text. // Text returns the Entry's text.
@ -108,7 +74,7 @@ func (e *Entry) OnChanged(f func(*Entry)) {
//export doEntryOnChanged //export doEntryOnChanged
func doEntryOnChanged(ee *C.uiEntry, data unsafe.Pointer) { func doEntryOnChanged(ee *C.uiEntry, data unsafe.Pointer) {
e := entries[ee] e := ControlFromLibui(uintptr(unsafe.Pointer(ee))).(*Entry)
if e.onChanged != nil { if e.onChanged != nil {
e.onChanged(e) e.onChanged(e)
} }