summaryrefslogtreecommitdiff
path: root/redo/controls_darwin.go
blob: 3c3289ea4248a800bb13e6091a7d47edb97166bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 16 july 2014

package ui

import (
	"unsafe"
)

// #include "objc_darwin.h"
import "C"

// TODO move to common_darwin.go
func toBOOL(b bool) C.BOOL {
	if b == true {
		return C.YES
	}
	return C.NO
}

type widgetbase struct {
	id		C.id
	parentw	*window
	floating	bool
}

func newWidget(id C.id) *widgetbase {
	return &widgetbase{
		id:	id,
	}
}

// these few methods are embedded by all the various Controls since they all will do the same thing

func (w *widgetbase) unparent() {
	if w.parentw != nil {
		C.unparent(w.id)
		w.floating = true
		w.parentw = nil
	}
}

func (w *widgetbase) parent(win *window) {
	C.parent(w.id, win.id, toBOOL(w.floating))
	w.floating = false
	w.parentw = win
}

type button struct {
	*widgetbase
	clicked		*event
}

func newButton(text string) *button {
	ctext := C.CString(text)
	defer C.free(unsafe.Pointer(ctext))
	b := &button{
		widgetbase:	newWidget(C.newButton(ctext)),
		clicked:		newEvent(),
	}
	C.buttonSetDelegate(b.id, unsafe.Pointer(b))
	return b
}

func (b *button) OnClicked(e func()) {
	b.clicked.set(e)
}

//export buttonClicked
func buttonClicked(xb unsafe.Pointer) {
	b := (*button)(unsafe.Pointer(xb))
	b.clicked.fire()
	println("button clicked")
}

func (b *button) Text() string {
	return C.GoString(C.buttonText(b.id))
}

func (b *button) SetText(text string) {
	ctext := C.CString(text)
	defer C.free(unsafe.Pointer(ctext))
	C.buttonSetText(b.id, ctext)
}