forked from whtiehack/wingui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.go
73 lines (62 loc) · 1.76 KB
/
edit.go
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
package wingui
import (
"syscall"
"unsafe"
"github.com/lxn/win"
)
// Edit a widget for Dialog.
type Edit struct {
WindowBase
OnChanged func()
}
// WndProc Edit Window WndProc.
func (e *Edit) WndProc(msg uint32, wParam, lParam uintptr) uintptr {
// log.Println("btn wnd proc", b.hwnd, msg, wParam, lParam)
switch msg {
case win.WM_COMMAND:
switch win.HIWORD(uint32(wParam)) {
case win.EN_CHANGE:
if e.OnChanged != nil && lParam == uintptr(e.hwnd) {
e.OnChanged()
}
}
}
return e.AsWindowBase().WndProc(msg, wParam, lParam)
}
// TextSelection get Edit selection.
func (e *Edit) TextSelection() (start, end int) {
e.SendMessage(win.EM_GETSEL, uintptr(unsafe.Pointer(&start)), uintptr(unsafe.Pointer(&end)))
return
}
// SetTextSelection set Edit selection
func (e *Edit) SetTextSelection(start, end int) {
e.SendMessage(win.EM_SETSEL, uintptr(start), uintptr(end))
}
// ReplaceSelectedText replace Text.
func (e *Edit) ReplaceSelectedText(text string, canUndo bool) {
e.SendMessage(win.EM_REPLACESEL,
uintptr(win.BoolToBOOL(canUndo)),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))))
}
// AppendText append text to Edit
func (e *Edit) AppendText(value string) {
s, end := e.TextSelection()
l := e.TextLength()
e.SetTextSelection(l, l)
e.ReplaceSelectedText(value, false)
e.SetTextSelection(s, end)
}
// TextLength get Edit text length.
func (e *Edit) TextLength() int {
return int(e.SendMessage(win.WM_GETTEXTLENGTH, 0, 0))
}
// NewEdit create a new Edit ,need bind to Dialog before use.
func NewEdit(idd uintptr) *Edit {
return &Edit{WindowBase: WindowBase{idd: idd}}
}
//BindNewEdit create a new Edit and bind to dlg.
func BindNewEdit(idd uintptr, dlg *Dialog) (*Edit, error) {
edit := NewEdit(idd)
err := dlg.BindWidgets(edit)
return edit, err
}