From 5ab5777d4cbfe6490760ef4e618bd5fe80a20bea Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 11 Aug 2018 19:52:29 -0400 Subject: [PATCH] More control conversion and syncing. --- AAA_GOFILES/editablecombobox.go | 120 ------------------ AAA_GOFILES/combobox.go => combobox.go | 59 +-------- control.go | 4 +- .../datetimepicker.go => datetimepicker.go | 98 +++----------- editablecombobox.go | 70 ++++++++++ AAA_GOFILES/entry.go => entry.go | 80 ++++-------- 6 files changed, 120 insertions(+), 311 deletions(-) delete mode 100644 AAA_GOFILES/editablecombobox.go rename AAA_GOFILES/combobox.go => combobox.go (50%) rename AAA_GOFILES/datetimepicker.go => datetimepicker.go (52%) create mode 100644 editablecombobox.go rename AAA_GOFILES/entry.go => entry.go (51%) diff --git a/AAA_GOFILES/editablecombobox.go b/AAA_GOFILES/editablecombobox.go deleted file mode 100644 index 8f6a825..0000000 --- a/AAA_GOFILES/editablecombobox.go +++ /dev/null @@ -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) - } -} diff --git a/AAA_GOFILES/combobox.go b/combobox.go similarity index 50% rename from AAA_GOFILES/combobox.go rename to combobox.go index 1224e1e..5da5f35 100644 --- a/AAA_GOFILES/combobox.go +++ b/combobox.go @@ -8,22 +8,14 @@ import ( // #include "ui.h" // extern void doComboboxOnSelected(uiCombobox *, void *); -// static inline void realuiComboboxOnSelected(uiCombobox *c) -// { -// uiComboboxOnSelected(c, doComboboxOnSelected, NULL); -// } 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 // that the user can choose one of at any time. For a Combobox that // users can type values into, see EditableCombobox. type Combobox struct { - co *C.uiControl + ControlBase c *C.uiCombobox - onSelected func(*Combobox) } @@ -32,56 +24,13 @@ func NewCombobox() *Combobox { c := new(Combobox) c.c = C.uiNewCombobox() - c.co = (*C.uiControl)(unsafe.Pointer(c.c)) - C.realuiComboboxOnSelected(c.c) - comboboxes[c.c] = c + C.uiComboboxOnSelected(c.c, C.doComboboxOnSelected, nil) + c.ControlBase = NewControlBase(c, uintptr(unsafe.Pointer(c.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. func (c *Combobox) Append(text string) { ctext := C.CString(text) @@ -109,7 +58,7 @@ func (c *Combobox) OnSelected(f func(*Combobox)) { //export doComboboxOnSelected func doComboboxOnSelected(cc *C.uiCombobox, data unsafe.Pointer) { - c := comboboxes[cc] + c := ControlFromLibui(uintptr(unsafe.Pointer(cc))).(*Combobox) if c.onSelected != nil { c.onSelected(c) } diff --git a/control.go b/control.go index 3bcdfbe..0762542 100644 --- a/control.go +++ b/control.go @@ -92,7 +92,7 @@ func (c *ControlBase) Handle() uintptr { } func (c *ControlBase) Visible() bool { - return frombool(C.uiControlVisible(c.c)) + return tobool(C.uiControlVisible(c.c)) } func (c *ControlBase) Show() { @@ -104,7 +104,7 @@ func (c *ControlBase) Hide() { } func (c *ControlBase) Enabled() bool { - return frombool(C.uiControlEnabled(c.c)) + return tobool(C.uiControlEnabled(c.c)) } func (c *ControlBase) Enable() { diff --git a/AAA_GOFILES/datetimepicker.go b/datetimepicker.go similarity index 52% rename from AAA_GOFILES/datetimepicker.go rename to datetimepicker.go index ff98ced..a69b737 100644 --- a/AAA_GOFILES/datetimepicker.go +++ b/datetimepicker.go @@ -15,100 +15,43 @@ import ( // return (struct tm *) malloc(sizeof (struct tm)); // } // extern void doDateTimePickerChanged(uiDateTimePicker *, void *); -// static inline void realuiDateTimePickerOnChanged(uiDateTimePicker *d) -// { -// uiDateTimePickerOnChanged(d, doDateTimePickerOnChanged, NULL); -// } import "C" // DateTimePicker is a Control that represents a field where the user // can enter a date and/or a time. type DateTimePicker struct { - c *C.uiControl + ControlBase d *C.uiDateTimePicker - 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 // both a date and a time. func NewDateTimePicker() *DateTimePicker { - d := new(DateTimePicker) - - d.d = C.uiNewDateTimePicker() - d.c = (*C.uiControl)(unsafe.Pointer(d.d)) - - C.realuiDateTimePickerOnChanged(d.d) - - return d + return finishNewDateTImePicker(C.uiNewDateTimePicker()) } // NewDatePicker creates a new DateTimePicker that shows // only a date. func NewDatePicker() *DateTimePicker { - d := new(DateTimePicker) - - d.d = C.uiNewDatePicker() - d.c = (*C.uiControl)(unsafe.Pointer(d.d)) - - C.realuiDateTimePickerOnChanged(d.d) - - return d + return finishNewDateTimePicker(C.uiNewDatePicker()) } // NewTimePicker creates a new DateTimePicker that shows // only a time. func NewTimePicker() *DateTimePicker { - d := new(DateTimePicker) - - 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) + return finishNewDateTimePicker(C.uiNewTimePicker()) } // 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. -// t's components are read as-is; no time zone manipulations -// are done. +// t's components are read as-is via t.Date() and t.Time(); +// no time zone manipulations are done. func (d *DateTimePicker) SetTime(t time.Time) { tm := C.allocTimeStruct() defer C.free(unsafe.Pointer(tm)) @@ -145,15 +88,16 @@ func (d *DateTimePicker) SetTime(t time.Time) { C.uiDateTimePickerSetTime(d.d, tm) } -// OnChanged registers f to be run when the user changes the time in the DateTimePicker. -// Only one function can be registered at a time. +// OnChanged registers f to be run when the user changes the time +// in the DateTimePicker. Only one function can be registered at a +// time. func (d *DateTimePicker) OnChanged(f func(*DateTimePicker)) { d.onChanged = f } //export doDateTimePickerOnChanged func doDateTimePickerOnChanged(dd *C.uiDateTimePicker, data unsafe.Pointer) { - d := dateTimePickers[dd] + d := ControlFromLibui(uintptr(unsafe.Pointer(dd)).(*DateTimePicker) if d.onChanged != nil { d.onChanged(d) } diff --git a/editablecombobox.go b/editablecombobox.go new file mode 100644 index 0000000..53ea839 --- /dev/null +++ b/editablecombobox.go @@ -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) + } +} diff --git a/AAA_GOFILES/entry.go b/entry.go similarity index 51% rename from AAA_GOFILES/entry.go rename to entry.go index ee14fc8..2b6cc11 100644 --- a/AAA_GOFILES/entry.go +++ b/entry.go @@ -12,77 +12,43 @@ import ( // #include "ui.h" // extern void doEntryOnChanged(uiEntry *, void *); -// static inline void realuiEntryOnChanged(uiEntry *b) -// { -// uiEntryOnChanged(b, doEntryOnChanged, NULL); -// } 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 // type a single line of text into. type Entry struct { - c *C.uiControl + ControlBase e *C.uiEntry - 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. func NewEntry() *Entry { - e := new(Entry) - - e.e = C.uiNewEntry() - e.c = (*C.uiControl)(unsafe.Pointer(e.e)) - - C.realuiEntryOnChanged(e.e) - entries[e.e] = e - - return e + return finishNewEntry(C.uiNewEntry()) } -// Destroy destroys the Entry. -func (e *Entry) Destroy() { - delete(entries, e.e) - C.uiControlDestroy(e.c) +// NewPasswordEntry creates a new Entry whose contents are +// visibly obfuscated, suitable for passwords. +func NewPasswordEntry() *Entry { + return finishNewEntry(C.uiNewPasswordEntry()) } -// 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 (e *Entry) LibuiControl() uintptr { - return uintptr(unsafe.Pointer(e.c)) -} - -// 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) +// NewSearchEntry creates a new Entry suitable for searching with. +// Changed events may, depending on the system, be delayed +// with a search Entry, to produce a smoother user experience. +func NewSearchEntry() *Entry { + return finishNewEntry(C.uiNewSearchEntry()) } // Text returns the Entry's text. @@ -108,7 +74,7 @@ func (e *Entry) OnChanged(f func(*Entry)) { //export doEntryOnChanged func doEntryOnChanged(ee *C.uiEntry, data unsafe.Pointer) { - e := entries[ee] + e := ControlFromLibui(uintptr(unsafe.Pointer(ee))).(*Entry) if e.onChanged != nil { e.onChanged(e) }