From 5ab5777d4cbfe6490760ef4e618bd5fe80a20bea Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 11 Aug 2018 19:52:29 -0400 Subject: More control conversion and syncing. --- AAA_GOFILES/combobox.go | 116 ----------------------------- AAA_GOFILES/datetimepicker.go | 160 ---------------------------------------- AAA_GOFILES/editablecombobox.go | 120 ------------------------------ AAA_GOFILES/entry.go | 125 ------------------------------- combobox.go | 65 ++++++++++++++++ control.go | 4 +- datetimepicker.go | 104 ++++++++++++++++++++++++++ editablecombobox.go | 70 ++++++++++++++++++ entry.go | 91 +++++++++++++++++++++++ 9 files changed, 332 insertions(+), 523 deletions(-) delete mode 100644 AAA_GOFILES/combobox.go delete mode 100644 AAA_GOFILES/datetimepicker.go delete mode 100644 AAA_GOFILES/editablecombobox.go delete mode 100644 AAA_GOFILES/entry.go create mode 100644 combobox.go create mode 100644 datetimepicker.go create mode 100644 editablecombobox.go create mode 100644 entry.go diff --git a/AAA_GOFILES/combobox.go b/AAA_GOFILES/combobox.go deleted file mode 100644 index 1224e1e..0000000 --- a/AAA_GOFILES/combobox.go +++ /dev/null @@ -1,116 +0,0 @@ -// 12 december 2015 - -package ui - -import ( - "unsafe" -) - -// #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 - c *C.uiCombobox - - onSelected func(*Combobox) -} - -// NewCombobox creates a new Combobox. -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 - - 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) - C.uiComboboxAppend(c.c, ctext) - freestr(ctext) -} - -// Selected returns the index of the currently selected item in the -// Combobox, or -1 if nothing is selected. -func (c *Combobox) Selected() int { - return int(C.uiComboboxSelected(c.c)) -} - -// SetChecked sets the currently select item in the Combobox -// to index. If index is -1 no item will be selected. -func (c *Combobox) SetSelected(index int) { - C.uiComboboxSetSelected(c.c, C.intmax_t(index)) -} - -// OnSelected 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) OnSelected(f func(*Combobox)) { - c.onSelected = f -} - -//export doComboboxOnSelected -func doComboboxOnSelected(cc *C.uiCombobox, data unsafe.Pointer) { - c := comboboxes[cc] - if c.onSelected != nil { - c.onSelected(c) - } -} diff --git a/AAA_GOFILES/datetimepicker.go b/AAA_GOFILES/datetimepicker.go deleted file mode 100644 index ff98ced..0000000 --- a/AAA_GOFILES/datetimepicker.go +++ /dev/null @@ -1,160 +0,0 @@ -// 12 december 2015 - -package ui - -import ( - "time" - "unsafe" -) - -// #include -// #include "ui.h" -// static inline struct tm *allocTimeStruct(void) -// { -// /* TODO handle error */ -// 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 - d *C.uiDateTimePicker - - onChanged func(*DateTimePicker) -} - -// 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 -} - -// 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 -} - -// 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) -} - -// Time returns the time stored in the uiDateTimePicker. -// The time is assumed to be local time. -func (d *DateTimePicker) Time() time.Time { - tm := C.allocTimeStruct() - defer C.free(unsafe.Pointer(tm)) - C.uiDateTimePickerTime(d.d, tm) - return time.Date( - int(tm.tm_year + 1900), - time.Month(tm.tm_mon + 1), - int(tm.tm_mday), - int(tm.tm_hour), - int(tm.tm_min), - int(tm.tm_sec), - 0, time.Local) -} - -// SetTime sets the time in the DateTimePicker to t. -// t's components are read as-is; no time zone manipulations -// are done. -func (d *DateTimePicker) SetTime(t time.Time) { - tm := C.allocTimeStruct() - defer C.free(unsafe.Pointer(tm)) - year, mon, mday := t.Date() - tm.tm_year = C.int(year - 1900) - tm.tm_mon = C.int(mon - 1) - tm.tm_mday = C.int(mday) - hour, min, sec := t.Time() - tm.tm_hour = C.int(hour) - tm.tm_min = C.int(min) - tm.tm_sec = C.int(sec) - tm.tm_isdst = -1 - 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. -func (d *DateTimePicker) OnChanged(f func(*DateTimePicker)) { - d.onChanged = f -} - -//export doDateTimePickerOnChanged -func doDateTimePickerOnChanged(dd *C.uiDateTimePicker, data unsafe.Pointer) { - d := dateTimePickers[dd] - if d.onChanged != nil { - d.onChanged(d) - } -} 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/entry.go b/AAA_GOFILES/entry.go deleted file mode 100644 index ee14fc8..0000000 --- a/AAA_GOFILES/entry.go +++ /dev/null @@ -1,125 +0,0 @@ -// 12 december 2015 - -// TODO typing in entry in OS X crashes libui -// I've had similar issues with checkboxes on libui -// something's wrong with NSMapTable - -package ui - -import ( - "unsafe" -) - -// #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 - e *C.uiEntry - - onChanged func(*Entry) -} - -// 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 -} - -// Destroy destroys the Entry. -func (e *Entry) Destroy() { - delete(entries, e.e) - C.uiControlDestroy(e.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 (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) -} - -// Text returns the Entry's text. -func (e *Entry) Text() string { - ctext := C.uiEntryText(e.e) - text := C.GoString(ctext) - C.uiFreeText(ctext) - return text -} - -// SetText sets the Entry's text to text. -func (e *Entry) SetText(text string) { - ctext := C.CString(text) - C.uiEntrySetText(e.e, ctext) - freestr(ctext) -} - -// OnChanged registers f to be run when the user makes a change to -// the Entry. Only one function can be registered at a time. -func (e *Entry) OnChanged(f func(*Entry)) { - e.onChanged = f -} - -//export doEntryOnChanged -func doEntryOnChanged(ee *C.uiEntry, data unsafe.Pointer) { - e := entries[ee] - if e.onChanged != nil { - e.onChanged(e) - } -} - -// ReadOnly returns whether the Entry can be changed. -func (e *Entry) ReadOnly() bool { - return tobool(C.uiEntryReadOnly(e.e)) -} - -// SetReadOnly sets whether the Entry can be changed. -func (e *Entry) SetReadOnly(ro bool) { - C.uiEntrySetReadOnly(e.e, frombool(ro)) -} diff --git a/combobox.go b/combobox.go new file mode 100644 index 0000000..5da5f35 --- /dev/null +++ b/combobox.go @@ -0,0 +1,65 @@ +// 12 december 2015 + +package ui + +import ( + "unsafe" +) + +// #include "ui.h" +// extern void doComboboxOnSelected(uiCombobox *, void *); +import "C" + +// 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 { + ControlBase + c *C.uiCombobox + onSelected func(*Combobox) +} + +// NewCombobox creates a new Combobox. +func NewCombobox() *Combobox { + c := new(Combobox) + + c.c = C.uiNewCombobox() + + C.uiComboboxOnSelected(c.c, C.doComboboxOnSelected, nil) + + c.ControlBase = NewControlBase(c, uintptr(unsafe.Pointer(c.c))) + return c +} + +// Append adds the named item to the end of the Combobox. +func (c *Combobox) Append(text string) { + ctext := C.CString(text) + C.uiComboboxAppend(c.c, ctext) + freestr(ctext) +} + +// Selected returns the index of the currently selected item in the +// Combobox, or -1 if nothing is selected. +func (c *Combobox) Selected() int { + return int(C.uiComboboxSelected(c.c)) +} + +// SetChecked sets the currently select item in the Combobox +// to index. If index is -1 no item will be selected. +func (c *Combobox) SetSelected(index int) { + C.uiComboboxSetSelected(c.c, C.intmax_t(index)) +} + +// OnSelected 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) OnSelected(f func(*Combobox)) { + c.onSelected = f +} + +//export doComboboxOnSelected +func doComboboxOnSelected(cc *C.uiCombobox, data unsafe.Pointer) { + 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/datetimepicker.go b/datetimepicker.go new file mode 100644 index 0000000..a69b737 --- /dev/null +++ b/datetimepicker.go @@ -0,0 +1,104 @@ +// 12 december 2015 + +package ui + +import ( + "time" + "unsafe" +) + +// #include +// #include "ui.h" +// static inline struct tm *allocTimeStruct(void) +// { +// /* TODO handle error */ +// return (struct tm *) malloc(sizeof (struct tm)); +// } +// extern void doDateTimePickerChanged(uiDateTimePicker *, void *); +import "C" + +// DateTimePicker is a Control that represents a field where the user +// can enter a date and/or a time. +type DateTimePicker struct { + 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 { + return finishNewDateTImePicker(C.uiNewDateTimePicker()) +} + +// NewDatePicker creates a new DateTimePicker that shows +// only a date. +func NewDatePicker() *DateTimePicker { + return finishNewDateTimePicker(C.uiNewDatePicker()) +} + +// NewTimePicker creates a new DateTimePicker that shows +// only a time. +func NewTimePicker() *DateTimePicker { + return finishNewDateTimePicker(C.uiNewTimePicker()) +} + +// Time returns the time stored in the uiDateTimePicker. +// The time is assumed to be local time. +func (d *DateTimePicker) Time() time.Time { + tm := C.allocTimeStruct() + defer C.free(unsafe.Pointer(tm)) + C.uiDateTimePickerTime(d.d, tm) + return time.Date( + int(tm.tm_year + 1900), + time.Month(tm.tm_mon + 1), + int(tm.tm_mday), + int(tm.tm_hour), + int(tm.tm_min), + int(tm.tm_sec), + 0, time.Local) +} + +// SetTime sets the time in the DateTimePicker to t. +// 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)) + year, mon, mday := t.Date() + tm.tm_year = C.int(year - 1900) + tm.tm_mon = C.int(mon - 1) + tm.tm_mday = C.int(mday) + hour, min, sec := t.Time() + tm.tm_hour = C.int(hour) + tm.tm_min = C.int(min) + tm.tm_sec = C.int(sec) + tm.tm_isdst = -1 + 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. +func (d *DateTimePicker) OnChanged(f func(*DateTimePicker)) { + d.onChanged = f +} + +//export doDateTimePickerOnChanged +func doDateTimePickerOnChanged(dd *C.uiDateTimePicker, data unsafe.Pointer) { + 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/entry.go b/entry.go new file mode 100644 index 0000000..2b6cc11 --- /dev/null +++ b/entry.go @@ -0,0 +1,91 @@ +// 12 december 2015 + +// TODO typing in entry in OS X crashes libui +// I've had similar issues with checkboxes on libui +// something's wrong with NSMapTable + +package ui + +import ( + "unsafe" +) + +// #include "ui.h" +// extern void doEntryOnChanged(uiEntry *, void *); +import "C" + +// Entry is a Control that represents a space that the user can +// type a single line of text into. +type Entry struct { + 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 { + return finishNewEntry(C.uiNewEntry()) +} + +// NewPasswordEntry creates a new Entry whose contents are +// visibly obfuscated, suitable for passwords. +func NewPasswordEntry() *Entry { + return finishNewEntry(C.uiNewPasswordEntry()) +} + +// 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. +func (e *Entry) Text() string { + ctext := C.uiEntryText(e.e) + text := C.GoString(ctext) + C.uiFreeText(ctext) + return text +} + +// SetText sets the Entry's text to text. +func (e *Entry) SetText(text string) { + ctext := C.CString(text) + C.uiEntrySetText(e.e, ctext) + freestr(ctext) +} + +// OnChanged registers f to be run when the user makes a change to +// the Entry. Only one function can be registered at a time. +func (e *Entry) OnChanged(f func(*Entry)) { + e.onChanged = f +} + +//export doEntryOnChanged +func doEntryOnChanged(ee *C.uiEntry, data unsafe.Pointer) { + e := ControlFromLibui(uintptr(unsafe.Pointer(ee))).(*Entry) + if e.onChanged != nil { + e.onChanged(e) + } +} + +// ReadOnly returns whether the Entry can be changed. +func (e *Entry) ReadOnly() bool { + return tobool(C.uiEntryReadOnly(e.e)) +} + +// SetReadOnly sets whether the Entry can be changed. +func (e *Entry) SetReadOnly(ro bool) { + C.uiEntrySetReadOnly(e.e, frombool(ro)) +} -- cgit v1.2.3