Skip to content

Commit 25d713d

Browse files
author
rj
committed
Factored call to CreateWindowEx, with the special error handling, into a common function.
1 parent 2b62a24 commit 25d713d

12 files changed

+97
-199
lines changed

button_windows.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,17 @@ func buttonStyle(isDefault bool) uint32 {
2727
}
2828

2929
func (w *Button) mount(parent base.Control) (base.Element, error) {
30-
text, err := syscall.UTF16FromString(w.Text)
30+
// Create the control.
31+
hwnd, text, err := createControlWindow(0, &button.className[0], w.Text, buttonStyle(w.Default), parent.HWnd)
3132
if err != nil {
3233
return nil, err
3334
}
34-
35-
hwnd := win.CreateWindowEx(0, &button.className[0], &text[0], buttonStyle(w.Default),
36-
10, 10, 100, 100,
37-
parent.HWnd, win.HMENU(nextControlID()), 0, nil)
38-
if hwnd == 0 {
39-
err := syscall.GetLastError()
40-
if err == nil {
41-
return nil, syscall.EINVAL
42-
}
43-
return nil, err
44-
}
45-
46-
// Set the font for the window
47-
if hMessageFont != 0 {
48-
win.SendMessage(hwnd, win.WM_SETFONT, uintptr(hMessageFont), 0)
49-
}
50-
5135
if w.Disabled {
5236
win.EnableWindow(hwnd, false)
5337
}
5438

5539
// Subclass the window procedure
56-
subclassWindowProcedure(hwnd, &button.oldWindowProc, syscall.NewCallback(buttonWindowProc))
40+
subclassWindowProcedure(hwnd, &button.oldWindowProc, buttonWindowProc)
5741

5842
retval := &buttonElement{
5943
Control: Control{hwnd},

checkbox_windows.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,28 @@
11
package goey
22

33
import (
4-
"syscall"
54
"unsafe"
65

76
"bitbucket.org/rj/goey/base"
87
"github.com/lxn/win"
98
)
109

1110
func (w *Checkbox) mount(parent base.Control) (base.Element, error) {
12-
text, err := syscall.UTF16FromString(w.Text)
11+
// Create the control.
12+
const STYLE = win.WS_CHILD | win.WS_VISIBLE | win.WS_TABSTOP | win.BS_CHECKBOX | win.BS_TEXT | win.BS_NOTIFY
13+
hwnd, text, err := createControlWindow(0, &button.className[0], w.Text, STYLE, parent.HWnd)
1314
if err != nil {
1415
return nil, err
1516
}
16-
17-
hwnd := win.CreateWindowEx(0, &button.className[0], &text[0],
18-
win.WS_CHILD|win.WS_VISIBLE|win.WS_TABSTOP|win.BS_CHECKBOX|win.BS_TEXT|win.BS_NOTIFY,
19-
10, 10, 100, 100,
20-
parent.HWnd, win.HMENU(nextControlID()), 0, nil)
21-
if hwnd == 0 {
22-
err := syscall.GetLastError()
23-
if err == nil {
24-
return nil, syscall.EINVAL
25-
}
26-
return nil, err
27-
}
2817
if w.Value {
2918
win.SendMessage(hwnd, win.BM_SETCHECK, win.BST_CHECKED, 0)
3019
}
31-
32-
// Set the font for the window
33-
if hMessageFont != 0 {
34-
win.SendMessage(hwnd, win.WM_SETFONT, uintptr(hMessageFont), 0)
35-
}
36-
3720
if w.Disabled {
3821
win.EnableWindow(hwnd, false)
3922
}
4023

4124
// Subclass the window procedure
42-
subclassWindowProcedure(hwnd, &button.oldWindowProc, syscall.NewCallback(checkboxWindowProc))
25+
subclassWindowProcedure(hwnd, &button.oldWindowProc, checkboxWindowProc)
4326

4427
retval := &checkboxElement{
4528
Control: Control{hwnd},

dateinput_windows.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package goey
22

33
import (
4-
"syscall"
54
"time"
65
"unsafe"
76

@@ -31,24 +30,12 @@ func (w *DateInput) systemTime() win.SYSTEMTIME {
3130
}
3231

3332
func (w *DateInput) mount(parent base.Control) (base.Element, error) {
34-
style := uint32(win.WS_CHILD | win.WS_VISIBLE | win.WS_TABSTOP)
35-
hwnd := win.CreateWindowEx(0, &datetimepickClassName[0], nil,
36-
style,
37-
10, 10, 100, 100,
38-
parent.HWnd, 0, 0, nil)
39-
if hwnd == 0 {
40-
err := syscall.GetLastError()
41-
if err == nil {
42-
return nil, syscall.EINVAL
43-
}
33+
const STYLE = win.WS_CHILD | win.WS_VISIBLE | win.WS_TABSTOP
34+
hwnd, _, err := createControlWindow(0, &datetimepickClassName[0], "", STYLE, parent.HWnd)
35+
if err != nil {
4436
return nil, err
4537
}
4638

47-
// Set the font for the window
48-
if hMessageFont != 0 {
49-
win.SendMessage(hwnd, win.WM_SETFONT, uintptr(hMessageFont), 0)
50-
}
51-
5239
// Set the properties for the control
5340
st := w.systemTime()
5441
win.SendMessage(hwnd, win.DTM_SETSYSTEMTIME, win.GDT_VALID, uintptr(unsafe.Pointer(&st)))
@@ -57,7 +44,7 @@ func (w *DateInput) mount(parent base.Control) (base.Element, error) {
5744
}
5845

5946
// Subclass the window procedure
60-
subclassWindowProcedure(hwnd, &oldDateTimePickWindowProc, syscall.NewCallback(dateinputWindowProc))
47+
subclassWindowProcedure(hwnd, &oldDateTimePickWindowProc, dateinputWindowProc)
6148

6249
retval := &dateinputElement{
6350
Control: Control{hwnd},

intinput_windows.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,21 @@ import (
1010
)
1111

1212
func (w *IntInput) mount(parent base.Control) (base.Element, error) {
13-
text, err := syscall.UTF16PtrFromString(strconv.FormatInt(w.Value, 10))
14-
if err != nil {
15-
return nil, err
16-
}
17-
13+
// Create the control
1814
style := uint32(win.WS_CHILD | win.WS_VISIBLE | win.WS_TABSTOP | win.ES_LEFT | win.ES_AUTOHSCROLL | win.ES_NUMBER)
1915
if w.OnEnterKey != nil {
2016
style = style | win.ES_MULTILINE
2117
}
22-
hwnd := win.CreateWindowEx(win.WS_EX_CLIENTEDGE, &edit.className[0], text,
23-
style,
24-
10, 10, 100, 100,
25-
parent.HWnd, win.HMENU(nextControlID()), 0, nil)
26-
if hwnd == 0 {
27-
err := syscall.GetLastError()
28-
if err == nil {
29-
return nil, syscall.EINVAL
30-
}
18+
hwnd, _, err := createControlWindow(win.WS_EX_CLIENTEDGE, &edit.className[0], strconv.FormatInt(w.Value, 10), style, parent.HWnd)
19+
if err != nil {
3120
return nil, err
3221
}
33-
34-
// Set the font for the window
35-
if hMessageFont != 0 {
36-
win.SendMessage(hwnd, win.WM_SETFONT, uintptr(hMessageFont), 0)
37-
}
38-
3922
if w.Disabled {
4023
win.EnableWindow(hwnd, false)
4124
}
4225

4326
// Subclass the window procedure
44-
subclassWindowProcedure(hwnd, &edit.oldWindowProc, syscall.NewCallback(textinputWindowProc))
27+
subclassWindowProcedure(hwnd, &edit.oldWindowProc, textinputWindowProc)
4528

4629
// Create placeholder, if required.
4730
if w.Placeholder != "" {

progress_windows.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package goey
22

33
import (
4-
"syscall"
54
"unsafe"
65

76
"bitbucket.org/rj/goey/base"
@@ -20,25 +19,15 @@ func init() {
2019
}
2120

2221
func (w *Progress) mount(parent base.Control) (base.Element, error) {
22+
// Create the control.
2323
style := uint32(win.WS_CHILD | win.WS_VISIBLE)
24-
hwnd := win.CreateWindowEx(0, &progress.className[0], nil, style,
25-
10, 10, 100, 100,
26-
parent.HWnd, win.HMENU(nextControlID()), 0, nil)
27-
if hwnd == 0 {
28-
err := syscall.GetLastError()
29-
if err == nil {
30-
return nil, syscall.EINVAL
31-
}
24+
hwnd, _, err := createControlWindow(0, &progress.className[0], "", style, parent.HWnd)
25+
if err != nil {
3226
return nil, err
3327
}
3428
win.SendMessage(hwnd, win.PBM_SETRANGE32, uintptr(w.Min), uintptr(w.Max))
3529
win.SendMessage(hwnd, win.PBM_SETPOS, uintptr(w.Value), 0)
3630

37-
// Set the font for the window
38-
if hMessageFont != 0 {
39-
win.SendMessage(hwnd, win.WM_SETFONT, uintptr(hMessageFont), 0)
40-
}
41-
4231
retval := &progressElement{
4332
Control: Control{hwnd},
4433
}

selectinput_windows.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,9 @@ var (
1313
)
1414

1515
func (w *SelectInput) mount(parent base.Control) (base.Element, error) {
16-
if w.Value >= len(w.Items) {
17-
w.Value = len(w.Items) - 1
18-
}
19-
hwnd := win.CreateWindowEx(win.WS_EX_CLIENTEDGE, &comboboxClassName[0], nil,
20-
win.WS_CHILD|win.WS_VISIBLE|win.WS_TABSTOP|win.CBS_DROPDOWNLIST,
21-
10, 10, 100, 100,
22-
parent.HWnd, win.HMENU(nextControlID()), 0, nil)
23-
if hwnd == 0 {
24-
err := syscall.GetLastError()
25-
if err == nil {
26-
return nil, syscall.EINVAL
27-
}
16+
const STYLE = win.WS_CHILD | win.WS_VISIBLE | win.WS_TABSTOP | win.CBS_DROPDOWNLIST
17+
hwnd, _, err := createControlWindow(win.WS_EX_CLIENTEDGE, &comboboxClassName[0], "", STYLE, parent.HWnd)
18+
if err != nil {
2819
return nil, err
2920
}
3021

@@ -56,7 +47,7 @@ func (w *SelectInput) mount(parent base.Control) (base.Element, error) {
5647
}
5748

5849
// Subclass the window procedure
59-
subclassWindowProcedure(hwnd, &oldComboboxWindowProc, syscall.NewCallback(comboboxWindowProc))
50+
subclassWindowProcedure(hwnd, &oldComboboxWindowProc, comboboxWindowProc)
6051

6152
retval := &selectinputElement{
6253
Control: Control{hwnd},

slider_windows.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package goey
22

33
import (
4-
"syscall"
54
"unsafe"
65

76
"bitbucket.org/rj/goey/base"
@@ -26,23 +25,12 @@ func (w *Slider) mount(parent base.Control) (base.Element, error) {
2625
const TBM_SETPAGESIZE = win.WM_USER + 21
2726
const TBM_SETLINESIZE = win.WM_USER + 23
2827

29-
hwnd := win.CreateWindowEx(0, &slider.className[0], nil,
30-
win.WS_CHILD|win.WS_VISIBLE|win.WS_TABSTOP|TBS_HORZ|TBS_AUTOTICKS,
31-
10, 10, 100, 100,
32-
parent.HWnd, 0, 0, nil)
33-
if hwnd == 0 {
34-
err := syscall.GetLastError()
35-
if err == nil {
36-
return nil, syscall.EINVAL
37-
}
28+
// Create the control
29+
const STYLE = win.WS_CHILD | win.WS_VISIBLE | win.WS_TABSTOP | TBS_HORZ | TBS_AUTOTICKS
30+
hwnd, _, err := createControlWindow(0, &slider.className[0], "", STYLE, parent.HWnd)
31+
if err != nil {
3832
return nil, err
3933
}
40-
41-
// Set the font for the window
42-
if hMessageFont != 0 {
43-
win.SendMessage(hwnd, win.WM_SETFONT, uintptr(hMessageFont), 0)
44-
}
45-
4634
if w.Disabled {
4735
win.EnableWindow(hwnd, false)
4836
}
@@ -54,7 +42,7 @@ func (w *Slider) mount(parent base.Control) (base.Element, error) {
5442
win.SendMessage(hwnd, win.TBM_SETPOS, win.TRUE, currentValue)
5543

5644
// Subclass the window procedure
57-
subclassWindowProcedure(hwnd, &slider.oldWindowProc, syscall.NewCallback(sliderWindowProc))
45+
subclassWindowProcedure(hwnd, &slider.oldWindowProc, sliderWindowProc)
5846

5947
retval := &sliderElement{
6048
Control: Control{hwnd},

tabs_windows.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,13 @@ func init() {
2323
}
2424

2525
func (w *Tabs) mount(parent base.Control) (base.Element, error) {
26-
style := uint32(win.WS_CHILD | win.WS_VISIBLE)
27-
hwnd := win.CreateWindowEx(win.WS_EX_CONTROLPARENT, &tabs.className[0], nil, style,
28-
10, 10, 100, 100,
29-
parent.HWnd, win.HMENU(nextControlID()), 0, nil)
30-
if hwnd == 0 {
31-
err := syscall.GetLastError()
32-
if err == nil {
33-
return nil, syscall.EINVAL
34-
}
26+
// Create the control
27+
const STYLE = win.WS_CHILD | win.WS_VISIBLE
28+
hwnd, _, err := createControlWindow(win.WS_EX_CONTROLPARENT, &tabs.className[0], "", STYLE, parent.HWnd)
29+
if err != nil {
3530
return nil, err
3631
}
3732

38-
// Set the font for the window
39-
if hMessageFont != 0 {
40-
win.SendMessage(hwnd, win.WM_SETFONT, uintptr(hMessageFont), 0)
41-
}
42-
4333
for i, v := range w.Children {
4434
text, err := syscall.UTF16PtrFromString(v.Caption)
4535
if err != nil {
@@ -84,7 +74,7 @@ func (w *Tabs) mount(parent base.Control) (base.Element, error) {
8474

8575
// Subclass the window procedure
8676
win.SetWindowLongPtr(hwnd, win.GWLP_USERDATA, uintptr(unsafe.Pointer(retval)))
87-
subclassWindowProcedure(hwnd, &tabs.oldWindowProc, syscall.NewCallback(tabsWindowProc))
77+
subclassWindowProcedure(hwnd, &tabs.oldWindowProc, tabsWindowProc)
8878

8979
return retval, nil
9080
}

textarea_windows.go

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,17 @@ import (
99
)
1010

1111
func (w *TextArea) mount(parent base.Control) (base.Element, error) {
12-
text, err := syscall.UTF16PtrFromString(w.Value)
12+
// Create the control
13+
hwnd, _, err := createControlWindow(win.WS_EX_CLIENTEDGE, &edit.className[0], w.Value, w.style(), parent.HWnd)
1314
if err != nil {
1415
return nil, err
1516
}
16-
17-
style := uint32(win.WS_CHILD | win.WS_VISIBLE | win.WS_TABSTOP | win.ES_LEFT | win.ES_MULTILINE | win.ES_WANTRETURN | win.ES_AUTOVSCROLL)
18-
if w.ReadOnly {
19-
style = style | win.ES_READONLY
20-
}
21-
hwnd := win.CreateWindowEx(win.WS_EX_CLIENTEDGE, &edit.className[0], text,
22-
style,
23-
10, 10, 100, 100,
24-
parent.HWnd, win.HMENU(nextControlID()), 0, nil)
25-
if hwnd == 0 {
26-
err := syscall.GetLastError()
27-
if err == nil {
28-
return nil, syscall.EINVAL
29-
}
30-
return nil, err
31-
}
32-
33-
// Set the font for the window
34-
if hMessageFont != 0 {
35-
win.SendMessage(hwnd, win.WM_SETFONT, uintptr(hMessageFont), 0)
36-
}
37-
3817
if w.Disabled {
3918
win.EnableWindow(hwnd, false)
4019
}
4120

4221
// Subclass the window procedure
43-
subclassWindowProcedure(hwnd, &edit.oldWindowProc, syscall.NewCallback(textinputWindowProc))
22+
subclassWindowProcedure(hwnd, &edit.oldWindowProc, textinputWindowProc)
4423

4524
// Create placeholder, if required.
4625
if w.Value == "" && w.Placeholder != "" {
@@ -66,6 +45,14 @@ func (w *TextArea) mount(parent base.Control) (base.Element, error) {
6645
return retval, nil
6746
}
6847

48+
func (w *TextArea) style() uint32 {
49+
style := uint32(win.WS_CHILD | win.WS_VISIBLE | win.WS_TABSTOP | win.ES_LEFT | win.ES_MULTILINE | win.ES_WANTRETURN | win.ES_AUTOVSCROLL)
50+
if w.ReadOnly {
51+
style = style | win.ES_READONLY
52+
}
53+
return style
54+
}
55+
6956
type textareaElement struct {
7057
textinputElementBase
7158
minLines int

0 commit comments

Comments
 (0)