andlabs-ui/sysdata_darwin.go

381 lines
8.6 KiB
Go
Raw Normal View History

// 1 march 2014
package ui
import (
"fmt"
"sync"
)
// #include "objc_darwin.h"
import "C"
type sysData struct {
cSysData
2014-06-10 09:22:30 -05:00
id C.id
trackingArea C.id // for Area
}
type classData struct {
2014-06-10 09:22:30 -05:00
make func(parentWindow C.id, alternate bool, s *sysData) C.id
getinside func(scrollview C.id) C.id
show func(what C.id)
hide func(what C.id)
settext func(what C.id, text C.id)
text func(what C.id, alternate bool) C.id
append func(id C.id, what string, alternate bool)
insertBefore func(id C.id, what string, before int, alternate bool)
selIndex func(id C.id) int
selIndices func(id C.id) []int
selTexts func(id C.id) []string
delete func(id C.id, index int)
len func(id C.id) int
}
func addControl(parentWindow C.id, control C.id) {
C.addControl(parentWindow, control)
}
func controlShow(what C.id) {
C.controlShow(what)
}
func controlHide(what C.id) {
C.controlHide(what)
}
// By default some controls do not use the correct font.
// These functions set the appropriate control font.
// Which one is used on each control was determined by comparing https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AppleHIGuidelines/Characteristics/Characteristics.html#//apple_ref/doc/uid/TP40002721-SW10 to what Interface Builder says for each control.
// (not applicable to ProgressBar, Area)
// Button, Checkbox, Combobox, LineEdit, Label, Listbox
func applyStandardControlFont(id C.id) {
C.applyStandardControlFont(id)
}
var classTypes = [nctypes]*classData{
2014-06-10 09:22:30 -05:00
c_window: &classData{
make: func(parentWindow C.id, alternate bool, s *sysData) C.id {
return C.makeWindow(appDelegate)
},
2014-06-10 09:22:30 -05:00
show: func(what C.id) {
C.windowShow(what)
},
2014-06-10 09:22:30 -05:00
hide: func(what C.id) {
C.windowHide(what)
},
2014-06-10 09:22:30 -05:00
settext: func(what C.id, text C.id) {
C.windowSetTitle(what, text)
},
2014-06-10 09:22:30 -05:00
text: func(what C.id, alternate bool) C.id {
return C.windowTitle(what)
},
},
2014-06-10 09:22:30 -05:00
c_button: &classData{
make: func(parentWindow C.id, alternate bool, s *sysData) C.id {
button := C.makeButton()
C.buttonSetTargetAction(button, appDelegate)
applyStandardControlFont(button)
addControl(parentWindow, button)
return button
},
2014-06-10 09:22:30 -05:00
show: controlShow,
hide: controlHide,
settext: func(what C.id, text C.id) {
C.buttonSetText(what, text)
},
2014-06-10 09:22:30 -05:00
text: func(what C.id, alternate bool) C.id {
return C.buttonText(what)
},
},
2014-06-10 09:22:30 -05:00
c_checkbox: &classData{
make: func(parentWindow C.id, alternate bool, s *sysData) C.id {
checkbox := C.makeCheckbox()
applyStandardControlFont(checkbox)
addControl(parentWindow, checkbox)
2014-03-02 08:17:42 -06:00
return checkbox
},
2014-06-10 09:22:30 -05:00
show: controlShow,
hide: controlHide,
settext: func(what C.id, text C.id) {
C.buttonSetText(what, text)
},
2014-06-10 09:22:30 -05:00
text: func(what C.id, alternate bool) C.id {
return C.buttonText(what)
},
},
2014-06-10 09:22:30 -05:00
c_combobox: &classData{
make: func(parentWindow C.id, alternate bool, s *sysData) C.id {
combobox := C.makeCombobox(toBOOL(alternate))
applyStandardControlFont(combobox)
addControl(parentWindow, combobox)
return combobox
},
2014-06-10 09:22:30 -05:00
show: controlShow,
hide: controlHide,
text: func(what C.id, alternate bool) C.id {
return C.comboboxText(what, toBOOL(alternate))
},
2014-06-10 09:22:30 -05:00
append: func(id C.id, what string, alternate bool) {
C.comboboxAppend(id, toBOOL(alternate), toNSString(what))
},
2014-06-10 09:22:30 -05:00
insertBefore: func(id C.id, what string, before int, alternate bool) {
C.comboboxInsertBefore(id, toBOOL(alternate),
toNSString(what), C.intptr_t(before))
},
2014-06-10 09:22:30 -05:00
selIndex: func(id C.id) int {
return int(C.comboboxSelectedIndex(id))
},
2014-06-10 09:22:30 -05:00
delete: func(id C.id, index int) {
C.comboboxDelete(id, C.intptr_t(index))
},
2014-06-10 09:22:30 -05:00
len: func(id C.id) int {
return int(C.comboboxLen(id))
},
},
2014-06-10 09:22:30 -05:00
c_lineedit: &classData{
make: func(parentWindow C.id, alternate bool, s *sysData) C.id {
lineedit := C.makeLineEdit(toBOOL(alternate))
applyStandardControlFont(lineedit)
addControl(parentWindow, lineedit)
return lineedit
},
2014-06-10 09:22:30 -05:00
show: controlShow,
hide: controlHide,
settext: func(what C.id, text C.id) {
C.lineeditSetText(what, text)
},
2014-06-10 09:22:30 -05:00
text: func(what C.id, alternate bool) C.id {
return C.lineeditText(what)
},
},
2014-06-10 09:22:30 -05:00
c_label: &classData{
make: func(parentWindow C.id, alternate bool, s *sysData) C.id {
label := C.makeLabel()
applyStandardControlFont(label)
addControl(parentWindow, label)
return label
},
2014-06-10 09:22:30 -05:00
show: controlShow,
hide: controlHide,
settext: func(what C.id, text C.id) {
C.lineeditSetText(what, text)
},
2014-06-10 09:22:30 -05:00
text: func(what C.id, alternate bool) C.id {
return C.lineeditText(what)
},
},
2014-06-10 09:22:30 -05:00
c_listbox: &classData{
make: makeListbox,
show: controlShow,
hide: controlHide,
append: listboxAppend,
insertBefore: listboxInsertBefore,
selIndices: listboxSelectedIndices,
selTexts: listboxSelectedTexts,
delete: listboxDelete,
len: listboxLen,
},
2014-06-10 09:22:30 -05:00
c_progressbar: &classData{
make: func(parentWindow C.id, alternate bool, s *sysData) C.id {
pbar := C.makeProgressBar()
addControl(parentWindow, pbar)
return pbar
},
2014-06-10 09:22:30 -05:00
show: controlShow,
hide: controlHide,
},
2014-06-10 09:22:30 -05:00
c_area: &classData{
make: makeArea,
getinside: areaInScrollView,
show: controlShow,
hide: controlHide,
},
}
// I need to access sysData from appDelegate, but appDelegate doesn't store any data. So, this.
var (
2014-06-10 09:22:30 -05:00
sysdatas = make(map[C.id]*sysData)
sysdatalock sync.Mutex
)
func addSysData(key C.id, value *sysData) {
sysdatalock.Lock()
sysdatas[key] = value
sysdatalock.Unlock()
}
func getSysData(key C.id) *sysData {
sysdatalock.Lock()
defer sysdatalock.Unlock()
v, ok := sysdatas[key]
if !ok {
panic(fmt.Errorf("internal error: getSysData(%v) unknown", key))
}
return v
}
func (s *sysData) make(window *sysData) error {
var parentWindow C.id
ct := classTypes[s.ctype]
if window != nil {
parentWindow = window.id
}
uitask(func() {
s.id = ct.make(parentWindow, s.alternate, s)
if ct.getinside != nil {
addSysData(ct.getinside(s.id), s)
} else {
addSysData(s.id, s)
}
})
return nil
}
// used for Windows; nothing special needed elsewhere
func (s *sysData) firstShow() error {
s.show()
return nil
}
func (s *sysData) show() {
uitask(func() {
classTypes[s.ctype].show(s.id)
})
}
func (s *sysData) hide() {
uitask(func() {
classTypes[s.ctype].hide(s.id)
})
}
func (s *sysData) setText(text string) {
uitask(func() {
classTypes[s.ctype].settext(s.id, toNSString(text))
})
}
func (s *sysData) setRect(x int, y int, width int, height int, winheight int) error {
// winheight - y because (0,0) is the bottom-left corner of the window and not the top-left corner
// (winheight - y) - height because (x, y) is the bottom-left corner of the control and not the top-left
C.setRect(s.id,
2014-06-10 09:22:30 -05:00
C.intptr_t(x), C.intptr_t((winheight-y)-height),
C.intptr_t(width), C.intptr_t(height))
return nil
}
func (s *sysData) isChecked() bool {
var b bool
uitask(func() {
b = C.isCheckboxChecked(s.id) != C.NO
})
return b
}
func (s *sysData) text() string {
var text string
uitask(func() {
str := classTypes[s.ctype].text(s.id, s.alternate)
text = fromNSString(str)
})
return text
}
func (s *sysData) append(what string) {
uitask(func() {
classTypes[s.ctype].append(s.id, what, s.alternate)
})
}
func (s *sysData) insertBefore(what string, before int) {
uitask(func() {
classTypes[s.ctype].insertBefore(s.id, what, before, s.alternate)
})
}
func (s *sysData) selectedIndex() int {
var i int
uitask(func() {
i = classTypes[s.ctype].selIndex(s.id)
})
return i
}
func (s *sysData) selectedIndices() []int {
var i []int
uitask(func() {
i = classTypes[s.ctype].selIndices(s.id)
})
return i
}
func (s *sysData) selectedTexts() []string {
var texts []string
uitask(func() {
texts = classTypes[s.ctype].selTexts(s.id)
})
return texts
}
func (s *sysData) setWindowSize(width int, height int) error {
uitask(func() {
C.windowSetContentSize(s.id, C.intptr_t(width), C.intptr_t(height))
})
return nil
}
func (s *sysData) delete(index int) {
uitask(func() {
classTypes[s.ctype].delete(s.id, index)
})
}
func (s *sysData) setProgress(percent int) {
uitask(func() {
C.setProgress(s.id, C.intptr_t(percent))
})
}
func (s *sysData) len() int {
var i int
uitask(func() {
i = classTypes[s.ctype].len(s.id)
})
return i
}
func (s *sysData) setAreaSize(width int, height int) {
uitask(func() {
C.setAreaSize(s.id, C.intptr_t(width), C.intptr_t(height))
})
}
func (s *sysData) repaintAll() {
uitask(func() {
C.display(s.id)
})
}
func (s *sysData) center() {
uitask(func() {
C.center(s.id)
})
}
func (s *sysData) setChecked(checked bool) {
uitask(func() {
C.setCheckboxChecked(s.id, toBOOL(checked))
})
}