summaryrefslogtreecommitdiff
path: root/newctrl/textfield_windows.go
blob: 76ec8825acc3e1c0b119f94549403b079d0800e2 (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
// 15 july 2014

package ui

import (
	"unsafe"
)

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

type textfield struct {
	*controlSingleHWNDWithText
	changed  *event
}

var editclass = toUTF16("EDIT")

func startNewTextField(style C.DWORD) *textfield {
	hwnd := C.newControl(editclass,
		style|C.textfieldStyle,
		C.textfieldExtStyle) // WS_EX_CLIENTEDGE without WS_BORDER will show the canonical visual styles border (thanks to MindChild in irc.efnet.net/#winprog)
	t := &textfield{
		controlSingleHWNDWithText:		newControlSingleHWNDWithText(hwnd),
		changed: newEvent(),
	}
	t.fpreferredSize = t.xpreferredSize
	C.controlSetControlFont(t.hwnd)
	C.setTextFieldSubclass(t.hwnd, unsafe.Pointer(t))
	return t
}

func newTextField() *textfield {
	return startNewTextField(0)
}

func newPasswordField() *textfield {
	return startNewTextField(C.ES_PASSWORD)
}

func (t *textfield) Text() string {
	return t.text()
}

func (t *textfield) SetText(text string) {
	t.setText(text)
}

func (t *textfield) OnChanged(f func()) {
	t.changed.set(f)
}

func (t *textfield) Invalid(reason string) {
	if reason == "" {
		C.textfieldHideInvalidBalloonTip(t.hwnd)
		return
	}
	C.textfieldSetAndShowInvalidBalloonTip(t.hwnd, toUTF16(reason))
}

//export textfieldChanged
func textfieldChanged(data unsafe.Pointer) {
	t := (*textfield)(data)
	t.changed.fire()
}

const (
	// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
	textfieldWidth  = 107 // this is actually the shorter progress bar width, but Microsoft only indicates as wide as necessary
	textfieldHeight = 14
)

func (t *textfield) xpreferredSize(d *sizing) (width, height int) {
	return fromdlgunitsX(textfieldWidth, d), fromdlgunitsY(textfieldHeight, d)
}