summaryrefslogtreecommitdiffstatshomepage
path: root/util.go
blob: 566d8dad481ca0633c0ff81b3d2577406705ef66 (plain) (blame)
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// 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 (
	"bytes"
	"math/big"
	"strconv"
	"strings"
	"syscall"
	"time"

	"github.com/lxn/win"
)

var (
	decimalSepB      byte
	decimalSepUint16 uint16
	decimalSepS      string
	groupSepB        byte
	groupSepUint16   uint16
	groupSepS        string
)

func init() {
	var buf [4]uint16

	win.GetLocaleInfo(win.LOCALE_USER_DEFAULT, win.LOCALE_SDECIMAL, &buf[0], int32(len(buf)))
	decimalSepB = byte(buf[0])
	decimalSepS = syscall.UTF16ToString(buf[0:1])
	decimalSepUint16 = buf[0]

	win.GetLocaleInfo(win.LOCALE_USER_DEFAULT, win.LOCALE_STHOUSAND, &buf[0], int32(len(buf)))
	groupSepB = byte(buf[0])
	groupSepS = syscall.UTF16ToString(buf[0:1])
	groupSepUint16 = buf[0]
}

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 uint16IndexUint16(s []uint16, v uint16) int {
	for i, u := range s {
		if u == v {
			return i
		}
	}

	return -1
}

func uint16ContainsUint16(s []uint16, v uint16) bool {
	return uint16IndexUint16(s, v) != -1
}

func uint16CountUint16(s []uint16, v uint16) int {
	var count int

	for _, u := range s {
		if u == v {
			count++
		}
	}

	return count
}

func uint16RemoveUint16(s []uint16, v uint16) []uint16 {
	count := uint16CountUint16(s, v)
	if count == 0 {
		return s
	}

	ret := make([]uint16, 0, len(s)-count)

	for _, u := range s {
		if u != v {
			ret = append(ret, u)
		}
	}

	return ret
}

func assertFloat64Or(value interface{}, defaultValue float64) float64 {
	if f, ok := value.(float64); ok {
		return f
	}

	return defaultValue
}

func assertIntOr(value interface{}, defaultValue int) int {
	if n, ok := value.(int); ok {
		return n
	}

	return defaultValue
}

func assertStringOr(value interface{}, defaultValue string) string {
	if s, ok := value.(string); ok {
		return s
	}

	return defaultValue
}

func assertTimeOr(value interface{}, defaultValue time.Time) time.Time {
	if t, ok := value.(time.Time); ok {
		return t
	}

	return defaultValue
}

func ParseFloat(s string) (float64, error) {
	s = strings.TrimSpace(s)

	t := FormatFloatGrouped(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 {
	return formatFloatString(strconv.FormatFloat(f, 'f', prec, 64), prec, false)
}

func FormatFloatGrouped(f float64, prec int) string {
	return formatFloatString(strconv.FormatFloat(f, 'f', prec, 64), prec, true)
}

func formatBigRat(r *big.Rat, prec int) string {
	return formatFloatString(r.FloatString(prec), prec, false)
}

func formatBigRatGrouped(r *big.Rat, prec int) string {
	return formatFloatString(r.FloatString(prec), prec, true)
}

func formatFloatString(s string, prec int, grouped bool) string {
	switch s {
	case "NaN", "-Inf", "+Inf":
		return s
	}

	s = strings.Replace(s, ".", decimalSepS, 1)
	if !grouped {
		return s
	}

	b := new(bytes.Buffer)

	var firstDigit int
	if len(s) > 0 && s[0] == '-' {
		firstDigit = 1
		b.WriteByte('-')
		s = s[1:]
	}

	intLen := len(s) - prec - 1

	n := intLen % 3
	if n != 0 {
		b.WriteString(s[:n])
	}
	for i := n; i < intLen; i += 3 {
		if b.Len() > firstDigit {
			b.WriteByte(groupSepB)
		}
		b.WriteString(s[i : i+3])
	}

	b.WriteString(s[intLen:])

	return b.String()
}

func applyEnabledToDescendants(window Window, enabled bool) {
	wb := window.AsWindowBase()
	wb.applyEnabled(enabled)

	walkDescendants(window, func(w Window) bool {
		if w.Handle() == wb.hWnd {
			return true
		}

		if enabled && !w.AsWindowBase().enabled {
			return false
		}

		w.(applyEnableder).applyEnabled(enabled)

		return true
	})
}

func applyFontToDescendants(window Window, font *Font) {
	wb := window.AsWindowBase()
	wb.applyFont(font)

	walkDescendants(window, func(w Window) bool {
		if w.Handle() == wb.hWnd {
			return true
		}

		if w.AsWindowBase().font != nil {
			return false
		}

		w.(applyFonter).applyFont(font)

		return true
	})
}

func applyDPIToDescendants(window Window, dpi int) {
	wb := window.AsWindowBase()
	wb.ApplyDPI(dpi)

	walkDescendants(window, func(w Window) bool {
		if w.Handle() == wb.hWnd {
			return true
		}

		w.(ApplyDPIer).ApplyDPI(dpi)

		return true
	})
}

func walkDescendants(window Window, f func(w Window) bool) {
	window = window.AsWindowBase().window

	if window == nil || !f(window) {
		return
	}

	var children []*WidgetBase

	switch w := window.(type) {
	case *NumberEdit:
		if w.edit != nil {
			children = append(children, w.edit.AsWidgetBase())
		}

	case *TabWidget:
		for _, p := range w.Pages().items {
			children = append(children, p.AsWidgetBase())
		}

	case Container:
		if c := w.Children(); c != nil {
			children = c.items
		} else {
			children = nil
		}
	}

	for _, wb := range children {
		walkDescendants(wb.window.(Widget), f)
	}
}

func less(a, b interface{}, order SortOrder) bool {
	if _, ok := a.(error); ok {
		_, bIsErr := b.(error)

		return order == SortAscending == !bIsErr
	}
	if _, ok := b.(error); ok {
		return order == SortDescending
	}

	if a == nil {
		return order == SortAscending == (b != nil)
	}
	if b == nil {
		return order == SortDescending
	}

	switch av := a.(type) {
	case string:
		if bv, ok := b.(string); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case int:
		if bv, ok := b.(int); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case float64:
		if bv, ok := b.(float64); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case float32:
		if bv, ok := b.(float32); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case int64:
		if bv, ok := b.(int64); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case int32:
		if bv, ok := b.(int32); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case int16:
		if bv, ok := b.(int16); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case int8:
		if bv, ok := b.(int8); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case uint:
		if bv, ok := b.(uint); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case uint64:
		if bv, ok := b.(uint64); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case uint32:
		if bv, ok := b.(uint32); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case uint16:
		if bv, ok := b.(uint16); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case uint8:
		if bv, ok := b.(uint8); ok {
			if order == SortAscending {
				return av < bv
			} else {
				return bv < av
			}
		}

	case time.Time:
		if bv, ok := b.(time.Time); ok {
			if order == SortAscending {
				return av.Before(bv)
			} else {
				return bv.Before(av)
			}
		}

	case bool:
		if bv, ok := b.(bool); ok {
			if order == SortAscending {
				return !av && bv
			} else {
				return !bv && av
			}
		}
	}

	return false
}