-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogview.go
More file actions
73 lines (60 loc) · 1.63 KB
/
logview.go
File metadata and controls
73 lines (60 loc) · 1.63 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
package main
import (
"errors"
"syscall"
"unsafe"
)
import (
"github.com/lxn/walk"
"github.com/lxn/win"
)
type LogView struct {
textEdit *walk.TextEdit
logChan chan string
}
const (
TEM_APPENDTEXT = win.WM_USER + 6
)
func NewLogView(textEdit *walk.TextEdit) (*LogView, error) {
lc := make(chan string, 1024)
this := &LogView{textEdit: textEdit, logChan: lc}
this.setReadOnly(true)
this.textEdit.SendMessage(win.EM_SETLIMITTEXT, 4294967295, 0)
return this, nil
}
func (this *LogView) setTextSelection(start, end int) {
this.textEdit.SendMessage(win.EM_SETSEL, uintptr(start), uintptr(end))
}
func (this *LogView) textLength() int {
return int(this.textEdit.SendMessage(0x000E, uintptr(0), uintptr(0)))
}
func (this *LogView) AppendText(value string) {
textLength := this.textLength()
this.setTextSelection(textLength, textLength)
this.textEdit.SendMessage(win.EM_REPLACESEL, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))))
}
func (this *LogView) setReadOnly(readOnly bool) error {
if 0 == this.textEdit.SendMessage(win.EM_SETREADONLY, uintptr(win.BoolToBOOL(readOnly)), 0) {
return errors.New("fail to call EM_SETREADONLY")
}
return nil
}
func (this *LogView) GoAppendText(value string) {
this.logChan <- value
go this.MsgThread(TEM_APPENDTEXT)
}
func (this *LogView) Write(p []byte) (int, error) {
this.GoAppendText(string(p) + "\r\n")
return len(p), nil
}
func (this *LogView) MsgThread(msg uint32) {
switch msg {
case TEM_APPENDTEXT:
select {
case value := <-this.logChan:
this.AppendText(value)
default:
return
}
}
}