/
textedit.go
394 lines (317 loc) · 9.22 KB
/
textedit.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"sync"
"syscall"
"unsafe"
"github.com/lxn/win"
)
type TextEdit struct {
WidgetBase
readOnlyChangedPublisher EventPublisher
textChangedPublisher EventPublisher
textColor Color
compactHeight bool
margins Size // in native pixels
lastHeight int
origWordbreakProcPtr uintptr
}
func NewTextEdit(parent Container) (*TextEdit, error) {
return NewTextEditWithStyle(parent, 0)
}
func NewTextEditWithStyle(parent Container, style uint32) (*TextEdit, error) {
te := new(TextEdit)
if err := InitWidget(
te,
parent,
"EDIT",
win.WS_TABSTOP|win.WS_VISIBLE|win.ES_MULTILINE|win.ES_WANTRETURN|style,
win.WS_EX_CLIENTEDGE); err != nil {
return nil, err
}
te.origWordbreakProcPtr = te.SendMessage(win.EM_GETWORDBREAKPROC, 0, 0)
te.GraphicsEffects().Add(InteractionEffect)
te.GraphicsEffects().Add(FocusEffect)
te.MustRegisterProperty("ReadOnly", NewProperty(
func() interface{} {
return te.ReadOnly()
},
func(v interface{}) error {
return te.SetReadOnly(v.(bool))
},
te.readOnlyChangedPublisher.Event()))
te.MustRegisterProperty("Text", NewProperty(
func() interface{} {
return te.Text()
},
func(v interface{}) error {
return te.SetText(assertStringOr(v, ""))
},
te.textChangedPublisher.Event()))
return te, nil
}
func (te *TextEdit) applyFont(font *Font) {
te.WidgetBase.applyFont(font)
te.updateMargins()
}
func (te *TextEdit) updateMargins() {
// 56 works at least from 96 to 192 DPI, so until a better solution comes up, this is it.
defaultSize := te.dialogBaseUnitsToPixels(Size{56, 12})
var rc win.RECT
te.SendMessage(win.EM_GETRECT, 0, uintptr(unsafe.Pointer(&rc)))
if te.hasExtendedStyleBits(win.WS_EX_CLIENTEDGE) {
width := te.WidthPixels()
if width == 0 {
width = defaultSize.Width
}
te.margins.Width = width - int(rc.Right-rc.Left)
} else {
te.margins.Width = int(rc.Left) * 2
}
lineHeight := te.calculateTextSizeImpl("gM").Height
te.margins.Height = defaultSize.Height - lineHeight
}
var drawTextCompatibleEditWordbreakProcPtr uintptr
func init() {
AppendToWalkInit(func() {
drawTextCompatibleEditWordbreakProcPtr = syscall.NewCallback(drawTextCompatibleEditWordbreakProc)
})
}
func drawTextCompatibleEditWordbreakProc(lpch *uint16, ichCurrent, cch, code uintptr) uintptr {
switch code {
case win.WB_LEFT:
for i := int(ichCurrent); i >= 0; i-- {
if *(*uint16)(unsafe.Pointer(uintptr(unsafe.Pointer(lpch)) + uintptr(i)*2)) == 32 {
return uintptr(i)
}
}
case win.WB_RIGHT:
for i := int(ichCurrent); i < int(cch); i++ {
if *(*uint16)(unsafe.Pointer(uintptr(unsafe.Pointer(lpch)) + uintptr(i)*2)) == 32 {
return uintptr(i)
}
}
case win.WB_ISDELIMITER:
if *(*uint16)(unsafe.Pointer(uintptr(unsafe.Pointer(lpch)) + ichCurrent*2)) == 32 {
return 1
}
}
return 0
}
func (te *TextEdit) Text() string {
return te.text()
}
func (te *TextEdit) TextLength() int {
return int(te.SendMessage(win.WM_GETTEXTLENGTH, 0, 0))
}
func (te *TextEdit) SetText(text string) (err error) {
if text == te.Text() {
return nil
}
var oldLineCount int
if te.compactHeight {
oldLineCount = int(te.SendMessage(win.EM_GETLINECOUNT, 0, 0))
}
err = te.setText(text)
if te.compactHeight {
if newLineCount := int(te.SendMessage(win.EM_GETLINECOUNT, 0, 0)); newLineCount != oldLineCount {
te.RequestLayout()
}
}
te.textChangedPublisher.Publish()
return
}
func (te *TextEdit) CompactHeight() bool {
return te.compactHeight
}
func (te *TextEdit) SetCompactHeight(enabled bool) {
if enabled == te.compactHeight {
return
}
te.compactHeight = enabled
var ptr uintptr
if enabled {
te.updateMargins()
ptr = drawTextCompatibleEditWordbreakProcPtr
} else {
ptr = te.origWordbreakProcPtr
}
te.SendMessage(win.EM_SETWORDBREAKPROC, 0, ptr)
te.RequestLayout()
}
func (te *TextEdit) TextAlignment() Alignment1D {
switch win.GetWindowLong(te.hWnd, win.GWL_STYLE) & (win.ES_LEFT | win.ES_CENTER | win.ES_RIGHT) {
case win.ES_CENTER:
return AlignCenter
case win.ES_RIGHT:
return AlignFar
}
return AlignNear
}
func (te *TextEdit) SetTextAlignment(alignment Alignment1D) error {
if alignment == AlignDefault {
alignment = AlignNear
}
var bit uint32
switch alignment {
case AlignCenter:
bit = win.ES_CENTER
case AlignFar:
bit = win.ES_RIGHT
default:
bit = win.ES_LEFT
}
return te.setAndClearStyleBits(bit, win.ES_LEFT|win.ES_CENTER|win.ES_RIGHT)
}
func (te *TextEdit) MaxLength() int {
return int(te.SendMessage(win.EM_GETLIMITTEXT, 0, 0))
}
func (te *TextEdit) SetMaxLength(value int) {
te.SendMessage(win.EM_SETLIMITTEXT, uintptr(value), 0)
}
func (te *TextEdit) ScrollToCaret() {
te.SendMessage(win.EM_SCROLLCARET, 0, 0)
}
func (te *TextEdit) TextSelection() (start, end int) {
te.SendMessage(win.EM_GETSEL, uintptr(unsafe.Pointer(&start)), uintptr(unsafe.Pointer(&end)))
return
}
func (te *TextEdit) SetTextSelection(start, end int) {
te.SendMessage(win.EM_SETSEL, uintptr(start), uintptr(end))
}
func (te *TextEdit) ReplaceSelectedText(text string, canUndo bool) {
te.SendMessage(win.EM_REPLACESEL,
uintptr(win.BoolToBOOL(canUndo)),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))))
}
func (te *TextEdit) AppendText(value string) {
s, e := te.TextSelection()
l := te.TextLength()
te.SetTextSelection(l, l)
te.ReplaceSelectedText(value, false)
te.SetTextSelection(s, e)
}
func (te *TextEdit) ReadOnly() bool {
return te.hasStyleBits(win.ES_READONLY)
}
func (te *TextEdit) SetReadOnly(readOnly bool) error {
if 0 == te.SendMessage(win.EM_SETREADONLY, uintptr(win.BoolToBOOL(readOnly)), 0) {
return newError("SendMessage(EM_SETREADONLY)")
}
te.readOnlyChangedPublisher.Publish()
return nil
}
func (te *TextEdit) TextChanged() *Event {
return te.textChangedPublisher.Event()
}
func (te *TextEdit) TextColor() Color {
return te.textColor
}
func (te *TextEdit) SetTextColor(c Color) {
te.textColor = c
te.Invalidate()
}
// ContextMenuLocation returns carret position in screen coordinates in native pixels.
func (te *TextEdit) ContextMenuLocation() Point {
idx := int(te.SendMessage(win.EM_GETCARETINDEX, 0, 0))
if idx < 0 {
start, end := te.TextSelection()
idx = (start + end) / 2
}
res := uint32(te.SendMessage(win.EM_POSFROMCHAR, uintptr(idx), 0))
pt := win.POINT{int32(win.LOWORD(res)), int32(win.HIWORD(res))}
windowTrimToClientBounds(te.hWnd, &pt)
return pointPixelsFromPOINT(pt)
}
func (*TextEdit) NeedsWmSize() bool {
return true
}
func (te *TextEdit) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_COMMAND:
switch win.HIWORD(uint32(wParam)) {
case win.EN_CHANGE:
if te.compactHeight {
if createLayoutItemForWidget(te).(MinSizer).MinSize().Height != te.HeightPixels() {
te.RequestLayout()
}
}
te.textChangedPublisher.Publish()
}
case win.WM_GETDLGCODE:
if wParam == win.VK_RETURN {
return win.DLGC_WANTALLKEYS
}
return win.DLGC_HASSETSEL | win.DLGC_WANTARROWS | win.DLGC_WANTCHARS
case win.WM_KEYDOWN:
if Key(wParam) == KeyA && ControlDown() {
te.SetTextSelection(0, -1)
}
}
return te.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}
func (te *TextEdit) CreateLayoutItem(ctx *LayoutContext) LayoutItem {
if te.margins.Width <= 0 {
te.updateMargins()
}
return &textEditLayoutItem{
width2Height: make(map[int]int),
compactHeight: te.compactHeight,
margins: te.margins,
text: te.Text(),
font: te.Font(),
minWidth: te.calculateTextSizeImpl("W").Width,
nonCompactHeightMinSize: te.dialogBaseUnitsToPixels(Size{20, 12}),
}
}
type textEditLayoutItem struct {
LayoutItemBase
mutex sync.Mutex
width2Height map[int]int // in native pixels
nonCompactHeightMinSize Size // in native pixels
margins Size // in native pixels
text string
font *Font
minWidth int // in native pixels
compactHeight bool
}
func (li *textEditLayoutItem) LayoutFlags() LayoutFlags {
flags := ShrinkableHorz | GrowableHorz | GreedyHorz
if !li.compactHeight {
flags |= GreedyVert | GrowableVert | ShrinkableVert
}
return flags
}
func (li *textEditLayoutItem) IdealSize() Size {
if li.compactHeight {
return li.MinSize()
} else {
return SizeFrom96DPI(Size{100, 100}, li.ctx.dpi)
}
}
func (li *textEditLayoutItem) MinSize() Size {
if li.compactHeight {
width := IntFrom96DPI(100, li.ctx.dpi)
return Size{width, li.HeightForWidth(width)}
} else {
return li.nonCompactHeightMinSize
}
}
func (li *textEditLayoutItem) HasHeightForWidth() bool {
return li.compactHeight
}
func (li *textEditLayoutItem) HeightForWidth(width int) int {
li.mutex.Lock()
defer li.mutex.Unlock()
if height, ok := li.width2Height[width]; ok {
return height
}
size := calculateTextSize(li.text, li.font, li.ctx.dpi, width-li.margins.Width, li.handle)
size.Height += li.margins.Height
size.Height = maxi(size.Height, li.nonCompactHeightMinSize.Height)
li.width2Height[width] = size.Height
return size.Height
}