Repository navigation
Expand file tree
/
Copy pathrichedit.go
More file actions
152 lines (133 loc) · 3.82 KB
/
Copy pathrichedit.go
File metadata and controls
152 lines (133 loc) · 3.82 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package wingui
import (
"syscall"
"unsafe"
"github.com/lxn/win"
)
const (
emSetCharFormat = win.WM_USER + 68
scfSelection = 0x0001
cfmColor = 0x40000000
)
type charformat2W struct {
cbSize uint32
dwMask uint32
dwEffects uint32
yHeight int32
yOffset int32
crTextColor win.COLORREF
bCharSet byte
bPitchAndFam byte
szFaceName [32]uint16
wWeight uint16
sSpacing int16
crBackColor win.COLORREF
lcid uint32
dwReserved uint32
sStyle int16
wKerning uint16
bUnderlineTyp byte
bAnimation byte
bRevAuthor byte
bReserved1 byte
}
// RichEdit is a wrapper for the Win32 Rich Edit control.
// The class is provided by Msftedit.dll (RICHEDIT50W) or Riched20.dll (RichEdit20W).
type RichEdit struct {
WindowBase
OnChanged func()
lastLen int
}
func (re *RichEdit) WndProc(msg uint32, wParam, lParam uintptr) uintptr {
maybeEmitChange := func() {
if re.OnChanged == nil || re.hwnd == 0 {
return
}
cur := re.TextLength()
if re.lastLen != cur {
re.lastLen = cur
re.OnChanged()
}
}
switch msg {
case win.WM_COMMAND:
switch win.HIWORD(uint32(wParam)) {
case win.EN_CHANGE:
if lParam == uintptr(re.hwnd) {
maybeEmitChange()
}
case win.EN_UPDATE:
if lParam == uintptr(re.hwnd) {
maybeEmitChange()
}
}
case win.WM_CHAR, win.WM_PASTE, win.WM_CUT, win.WM_CLEAR, win.WM_UNDO:
ret := re.AsWindowBase().WndProc(msg, wParam, lParam)
maybeEmitChange()
return ret
}
return re.AsWindowBase().WndProc(msg, wParam, lParam)
}
// TextSelection returns the current selection range (character index).
func (re *RichEdit) TextSelection() (start, end int) {
re.SendMessage(win.EM_GETSEL, uintptr(unsafe.Pointer(&start)), uintptr(unsafe.Pointer(&end)))
return
}
// SetTextSelection sets the current selection range (character index).
func (re *RichEdit) SetTextSelection(start, end int) {
re.SendMessage(win.EM_SETSEL, uintptr(start), uintptr(end))
}
// ReplaceSelectedText replaces the current selection with text.
func (re *RichEdit) ReplaceSelectedText(text string, canUndo bool) {
re.SendMessage(win.EM_REPLACESEL,
uintptr(win.BoolToBOOL(canUndo)),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
)
}
// AppendText appends text to the end of the control.
func (re *RichEdit) AppendText(value string) {
s, e := re.TextSelection()
l := re.TextLength()
re.SetTextSelection(l, l)
re.ReplaceSelectedText(value, false)
re.SetTextSelection(s, e)
}
// SetSelectionTextColor sets the selected text (and typing attributes) color.
func (re *RichEdit) SetSelectionTextColor(color win.COLORREF) bool {
cf := charformat2W{
cbSize: uint32(unsafe.Sizeof(charformat2W{})),
dwMask: cfmColor,
crTextColor: color,
}
ret := re.SendMessage(emSetCharFormat, scfSelection, uintptr(unsafe.Pointer(&cf)))
return ret != 0
}
// AppendTextColor appends text with a specific color.
func (re *RichEdit) AppendTextColor(value string, color win.COLORREF) {
l := re.TextLength()
re.SetTextSelection(l, l)
_ = re.SetSelectionTextColor(color)
re.ReplaceSelectedText(value, false)
_ = re.SetSelectionTextColor(win.COLORREF(0))
}
// TextLength returns current text length.
func (re *RichEdit) TextLength() int {
return int(re.SendMessage(win.WM_GETTEXTLENGTH, 0, 0))
}
// SetReadOnly toggles read-only mode.
func (re *RichEdit) SetReadOnly(readOnly bool) {
re.SendMessage(win.EM_SETREADONLY, uintptr(win.BoolToBOOL(readOnly)), 0)
}
// NewRichEdit creates a new RichEdit, need bind to Dialog before use.
func NewRichEdit(idd uintptr) *RichEdit {
return &RichEdit{
WindowBase: WindowBase{idd: idd, Subclassing: true},
lastLen: -1,
}
}
// BindNewRichEdit creates a new RichEdit and bind to target dlg.
func BindNewRichEdit(idd uintptr, dlg *Dialog) (*RichEdit, error) {
re := NewRichEdit(idd)
err := dlg.BindWidgets(re)
return re, err
}