-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
170 lines (130 loc) · 2.81 KB
/
util.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
// 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.
package walk
import (
"math/big"
"strconv"
"strings"
"syscall"
)
import (
. "github.com/lxn/go-winapi"
)
func maxi(a, b int) int {
if a > b {
return a
}
return b
}
func mini(a, b int) int {
if a < b {
return a
}
return b
}
func boolToInt(value bool) int {
if value {
return 1
}
return 0
}
func parseFloat(s string) (float64, error) {
s = strings.TrimSpace(s)
t, _ := formatFloat(1000, 2)
replaceSep := func(new string, index func(string, func(rune) bool) int) {
i := index(t, func(r rune) bool {
return r < '0' || r > '9'
})
var sep string
if i > -1 {
sep = string(t[i])
}
if sep != "" {
s = strings.Replace(s, string(sep), new, -1)
}
}
replaceSep("", strings.IndexFunc)
replaceSep(".", strings.LastIndexFunc)
return strconv.ParseFloat(s, 64)
}
func formatFloat(f float64, prec int) (string, error) {
return formatFloatString(strconv.FormatFloat(f, 'f', prec, 64), prec)
}
func formatRat(r *big.Rat, prec int) (string, error) {
return formatFloatString(r.FloatString(prec), prec)
}
func formatFloatString(s string, prec int) (string, error) {
// FIXME: Currently precision is ignored, because passing a *NUMBERFMT
// with only NumDigits initialized causes GetNumberFormat to fail.
sPtr := syscall.StringToUTF16Ptr(s)
bufSize := GetNumberFormat(
LOCALE_USER_DEFAULT,
0,
sPtr,
nil,
nil,
0)
if bufSize == 0 {
switch s {
case "NaN", "-Inf", "+Inf":
return s, nil
}
return "", lastError("GetNumberFormat")
}
buf := make([]uint16, bufSize)
if 0 == GetNumberFormat(
LOCALE_USER_DEFAULT,
0,
sPtr,
nil,
&buf[0],
bufSize) {
return "", lastError("GetNumberFormat")
}
return UTF16PtrToString(&buf[0]), nil
}
func setDescendantsEnabled(widget Widget, enabled bool) {
wb := widget.BaseWidget()
wb.SetEnabled(enabled)
walkDescendants(widget, func(w Widget) bool {
if w.Handle() == wb.hWnd {
return true
}
EnableWindow(w.Handle(), enabled && w.BaseWidget().enabled)
return true
})
}
func setDescendantsFont(widget Widget, f *Font) {
wb := widget.BaseWidget()
wb.SetFont(f)
walkDescendants(widget, func(w Widget) bool {
if w.Handle() == wb.hWnd {
return true
}
if w.BaseWidget().font != nil {
return false
}
setWidgetFont(w.Handle(), f)
return true
})
}
func walkDescendants(widget Widget, f func(w Widget) bool) {
if !f(widget) {
return
}
var children []Widget
switch w := widget.(type) {
case *NumberEdit:
children = append(children, w.edit)
case *TabWidget:
for _, p := range w.Pages().items {
children = append(children, p)
}
case Container:
children = w.Children().items
}
for _, w := range children {
walkDescendants(w, f)
}
}