summaryrefslogtreecommitdiff
path: root/redo/controls_windows.go
blob: ec2f827b9105d68fe0047e8a4b7f5f6c5d79b92d (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 15 july 2014

package ui

import (
	"unsafe"
)

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

type widgetbase struct {
	hwnd	C.HWND
}

func newWidget(class C.LPCWSTR, style C.DWORD, extstyle C.DWORD) *widgetbase {
	return &widgetbase{
		hwnd:	C.newWidget(class, style, extstyle),
	}
}

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

func (w *widgetbase) unparent() {
	C.controlSetParent(w.hwnd, C.msgwin)
}

func (w *widgetbase) parent(win *window) {
	C.controlSetParent(w.hwnd, win.hwnd)
}

// don't embed these as exported; let each Control decide if it should

func (w *widgetbase) text() *Request {
	c := make(chan interface{})
	return &Request{
		op:		func() {
			c <- getWindowText(w.hwnd)
		},
		resp:		c,
	}
}

func (w *widgetbase) settext(text string) *Request {
	c := make(chan interface{})
	return &Request{
		op:		func() {
			C.setWindowText(w.hwnd, toUTF16(text))
			c <- struct{}{}
		},
		resp:		c,
	}
}

type button struct {
	*widgetbase
	clicked		*event
}

var buttonclass = toUTF16("BUTTON")

func newButton(text string) *Request {
	c := make(chan interface{})
	return &Request{
		op:		func() {
			w := newWidget(buttonclass,
				C.BS_PUSHBUTTON | C.WS_TABSTOP,
				0)
			C.setWindowText(w.hwnd, toUTF16(text))
			b := &button{
				widgetbase:	w,
				clicked:		newEvent(),
			}
			C.setButtonSubclass(w.hwnd, unsafe.Pointer(b))
			c <- b
		},
		resp:		c,
	}
}

func (b *button) OnClicked(e func(c Doer)) *Request {
	c := make(chan interface{})
	return &Request{
		op:		func() {
			b.clicked.set(e)
			c <- struct{}{}
		},
		resp:		c,
	}
}

func (b *button) Text() *Request {
	return b.text()
}

func (b *button) SetText(text string) *Request {
	return b.settext(text)
}

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