From 9fe564753c5a2677b25dd671441187f9463ffcf7 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 12 Dec 2015 19:26:02 -0500 Subject: [PATCH] Added Spinbox and Slider. That *should* leave just Group, Tab, and Box... (and Area of course, but I want to finalize the interface more first) --- slider.go | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++ spinbox.go | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++ zz_test.go | 5 +-- 3 files changed, 220 insertions(+), 4 deletions(-) create mode 100644 slider.go create mode 100644 spinbox.go diff --git a/slider.go b/slider.go new file mode 100644 index 0000000..74e07fb --- /dev/null +++ b/slider.go @@ -0,0 +1,108 @@ +// 12 december 2015 + +package ui + +import ( + "unsafe" +) + +// #include "ui.h" +// extern void doSliderOnChanged(uiSlider *, void *); +// static inline void realuiSliderOnChanged(uiSlider *b) +// { +// uiSliderOnChanged(b, doSliderOnChanged, NULL); +// } +import "C" + +// no need to lock this; only the GUI thread can access it +var sliders = make(map[*C.uiSlider]*Slider) + +// Slider is a Control that represents a horizontal bar that represents +// a range of integers. The user can drag a pointer on the bar to +// select an integer. +type Slider struct { + c *C.uiControl + s *C.uiSlider + + onChanged func(*Slider) +} + +// NewSlider creates a new Slider. TODO limits +func NewSlider(min int, max int) *Slider { + s := new(Slider) + + s.s = C.uiNewSlider(C.intmax_t(min), C.intmax_t(max)) + s.c = (*C.uiControl)(unsafe.Pointer(s.s)) + + C.realuiSliderOnChanged(s.s) + sliders[s.s] = s + + return s +} + +// Destroy destroys the Slider. +func (s *Slider) Destroy() { + delete(sliders, s.s) + C.uiControlDestroy(s.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 (s *Slider) LibuiControl() uintptr { + return uintptr(unsafe.Pointer(s.c)) +} + +// Handle returns the OS-level handle associated with this Slider. +// On Windows this is an HWND of a standard Windows API +// TRACKBAR_CLASS class (as provided by Common Controls +// version 6). +// On GTK+ this is a pointer to a GtkScale. +// On OS X this is a pointer to a NSSlider. +func (s *Slider) Handle() uintptr { + return uintptr(C.uiControlHandle(s.c)) +} + +// Show shows the Slider. +func (s *Slider) Show() { + C.uiControlShow(s.c) +} + +// Hide hides the Slider. +func (s *Slider) Hide() { + C.uiControlHide(s.c) +} + +// Enable enables the Slider. +func (s *Slider) Enable() { + C.uiControlEnable(s.c) +} + +// Disable disables the Slider. +func (s *Slider) Disable() { + C.uiControlDisable(s.c) +} + +// Value returns the Slider's current value. +func (s *Slider) Value() int { + return int(C.uiSliderValue(s.s)) +} + +// SetText sets the Slider's current value to value. +func (s *Slider) SetValue(value int) { + C.uiSliderSetValue(s.s, C.intmax_t(value)) +} + +// OnChanged registers f to be run when the user changes the value +// of the Slider. Only one function can be registered at a time. +func (s *Slider) OnChanged(f func(*Slider)) { + s.onChanged = f +} + +//export doSliderOnChanged +func doSliderOnChanged(ss *C.uiSlider, data unsafe.Pointer) { + s := sliders[ss] + if s.onChanged != nil { + s.onChanged(s) + } +} diff --git a/spinbox.go b/spinbox.go new file mode 100644 index 0000000..77cedf5 --- /dev/null +++ b/spinbox.go @@ -0,0 +1,111 @@ +// 12 december 2015 + +package ui + +import ( + "unsafe" +) + +// #include "ui.h" +// extern void doSpinboxOnChanged(uiSpinbox *, void *); +// static inline void realuiSpinboxOnChanged(uiSpinbox *b) +// { +// uiSpinboxOnChanged(b, doSpinboxOnChanged, NULL); +// } +import "C" + +// no need to lock this; only the GUI thread can access it +var spinboxes = make(map[*C.uiSpinbox]*Spinbox) + +// Spinbox is a Control that represents a space where the user can +// enter integers. The space also comes with buttons to add or +// subtract 1 from the integer. +type Spinbox struct { + c *C.uiControl + s *C.uiSpinbox + + onChanged func(*Spinbox) +} + +// NewSpinbox creates a new Spinbox. TODO limits +func NewSpinbox(min int, max int) *Spinbox { + s := new(Spinbox) + + s.s = C.uiNewSpinbox(C.intmax_t(min), C.intmax_t(max)) + s.c = (*C.uiControl)(unsafe.Pointer(s.s)) + + C.realuiSpinboxOnChanged(s.s) + spinboxes[s.s] = s + + return s +} + +// Destroy destroys the Spinbox. +func (s *Spinbox) Destroy() { + delete(spinboxes, s.s) + C.uiControlDestroy(s.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 (s *Spinbox) LibuiControl() uintptr { + return uintptr(unsafe.Pointer(s.c)) +} + +// Handle returns the OS-level handle associated with this Spinbox. +// On Windows this is an HWND of a standard Windows API EDIT +// class (as provided by Common Controls version 6). Due to +// various limitations which affect the lifetime of the associated +// Common Controls version 6 UPDOWN_CLASS window that +// provides the buttons, there is no way to access it. +// On GTK+ this is a pointer to a GtkSpinButton. +// On OS X this is a pointer to a NSView that contains a NSTextField +// and a NSStepper as subviews. +func (s *Spinbox) Handle() uintptr { + return uintptr(C.uiControlHandle(s.c)) +} + +// Show shows the Spinbox. +func (s *Spinbox) Show() { + C.uiControlShow(s.c) +} + +// Hide hides the Spinbox. +func (s *Spinbox) Hide() { + C.uiControlHide(s.c) +} + +// Enable enables the Spinbox. +func (s *Spinbox) Enable() { + C.uiControlEnable(s.c) +} + +// Disable disables the Spinbox. +func (s *Spinbox) Disable() { + C.uiControlDisable(s.c) +} + +// Value returns the Spinbox's current value. +func (s *Spinbox) Value() int { + return int(C.uiSpinboxValue(s.s)) +} + +// SetText sets the Spinbox's current value to value. +func (s *Spinbox) SetValue(value int) { + C.uiSpinboxSetValue(s.s, C.intmax_t(value)) +} + +// OnChanged registers f to be run when the user changes the value +// of the Spinbox. Only one function can be registered at a time. +func (s *Spinbox) OnChanged(f func(*Spinbox)) { + s.onChanged = f +} + +//export doSpinboxOnChanged +func doSpinboxOnChanged(ss *C.uiSpinbox, data unsafe.Pointer) { + s := spinboxes[ss] + if s.onChanged != nil { + s.onChanged(s) + } +} diff --git a/zz_test.go b/zz_test.go index 491dbb5..b642210 100644 --- a/zz_test.go +++ b/zz_test.go @@ -11,10 +11,7 @@ func TestIt(t *testing.T) { Quit() return true }) - s := NewRadioButtons() - s.Append("Item 1") - s.Append("Item 2") - s.Append("Item 3") + s := NewSlider(0, 100) w.SetChild(s) w.SetMargined(true) w.Show()