-
Notifications
You must be signed in to change notification settings - Fork 0
/
lineerrorpresenter.go
223 lines (170 loc) · 4.33 KB
/
lineerrorpresenter.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
// Copyright 2012 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 (
"bytes"
"strings"
)
import (
"github.com/lxn/win"
)
const lineErrorPresenterWindowClass = `\o/ Walk_LineErrorPresenter_Class \o/`
func init() {
MustRegisterWindowClass(lineErrorPresenterWindowClass)
lineErrorPresenterBackground, _ = NewSolidColorBrush(RGB(255, 128, 128))
}
var lineErrorPresenterBackground Brush
type LineErrorPresenter struct {
WidgetBase
composite *Composite
label *Label
curWidget Widget
widget2error map[Widget]error
}
func NewLineErrorPresenter(parent Container) (*LineErrorPresenter, error) {
lep := &LineErrorPresenter{widget2error: make(map[Widget]error)}
if err := InitWidget(
lep,
parent,
lineErrorPresenterWindowClass,
win.WS_VISIBLE,
win.WS_EX_CONTROLPARENT); err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
lep.Dispose()
}
}()
var err error
if lep.composite, err = NewCompositeWithStyle(lep, 0); err != nil {
return nil, err
}
l := NewGridLayout()
l.SetMargins(Margins{2, 2, 2, 2})
if err = lep.composite.SetLayout(l); err != nil {
return nil, err
}
if lep.label, err = NewLabel(lep.composite); err != nil {
return nil, err
}
l.SetRange(lep.label, Rectangle{0, 0, 1, 1})
focusCurWidget := func(x, y int, button MouseButton) {
widget := lep.curWidget
if button == LeftButton && widget != nil {
widget.SetFocus()
if textSel, ok := widget.(textSelectable); ok {
textSel.SetTextSelection(0, -1)
}
}
}
lep.MouseDown().Attach(focusCurWidget)
lep.composite.MouseDown().Attach(focusCurWidget)
lep.label.MouseDown().Attach(focusCurWidget)
lep.PresentError(nil, nil)
succeeded = true
return lep, nil
}
func (*LineErrorPresenter) LayoutFlags() LayoutFlags {
return GrowableHorz | GreedyHorz
}
func (lep *LineErrorPresenter) MinSizeHint() Size {
if lep.label == nil {
return Size{}
}
text := lep.label.Text()
if text == "" {
text = "gM"
}
s := lep.label.calculateTextSizeImpl(text)
return Size{s.Width + 8, s.Height + 8}
}
func (lep *LineErrorPresenter) SizeHint() Size {
return lep.MinSizeHint()
}
func (lep *LineErrorPresenter) applyEnabled(enabled bool) {
lep.WidgetBase.applyEnabled(enabled)
lep.composite.applyEnabled(enabled)
}
func (lep *LineErrorPresenter) applyFont(font *Font) {
lep.WidgetBase.applyFont(font)
if lep.composite == nil {
return
}
// We have to call SetFont instead of applyFont here, because
// LineErrorPresenter does not implement Container.
lep.composite.SetFont(font)
}
func (lep *LineErrorPresenter) PresentError(err error, widget Widget) {
if err == nil {
delete(lep.widget2error, widget)
} else {
lep.widget2error[widget] = err
}
var found bool
if widget != nil {
walkDescendants(ancestor(widget).AsFormBase().clientComposite, func(w Window) bool {
if found {
return false
}
wt := w.(Widget)
if e, ok := lep.widget2error[wt]; ok {
err, widget, found = e, wt, true
}
return !found
})
}
if err != nil {
lep.curWidget = widget
} else {
lep.curWidget = nil
}
var msg string
var background Brush
if err != nil {
background = lineErrorPresenterBackground
var labelText string
if widget != nil {
parent := widget.Parent()
if parent != nil {
children := parent.Children()
i := children.Index(widget)
if i > 0 {
prev := children.At(i - 1)
if label, ok := prev.(*Label); ok {
labelText = label.Text()
}
}
}
}
buf := new(bytes.Buffer)
buf.WriteString(labelText)
if labelText != "" && !strings.HasSuffix(labelText, ":") {
buf.WriteString(":")
}
if labelText != "" {
buf.WriteString(" ")
}
buf.WriteString(err.Error())
msg = buf.String()
} else {
background = nullBrushSingleton
}
lep.SetBackground(background)
lep.composite.SetVisible(err != nil)
lep.label.SetText(msg)
lep.updateParentLayout()
}
func (lep *LineErrorPresenter) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_SIZE, win.WM_SIZING:
if lep.composite != nil {
b := lep.ClientBoundsPixels()
lep.composite.SetBoundsPixels(Rectangle{b.X + 2, b.Y + 2, b.Width - 4, b.Height - 4})
}
}
return lep.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}