-
Notifications
You must be signed in to change notification settings - Fork 24
/
dialog.go
223 lines (207 loc) · 5.72 KB
/
dialog.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
package wingui
import (
"errors"
"log"
"strconv"
"syscall"
"unsafe"
"github.com/lxn/win"
)
// Widget inspect dialog item.
type Widget interface {
WndProc(msg uint32, wParam, lParam uintptr) uintptr
AsWindowBase() *WindowBase
}
// DialogConfig TODO.
type DialogConfig struct {
Style uint32
//Widgets will be bind when dialog init.
Widgets []Widget
}
// ModalDialogCallBack is modal dialog callback
type ModalDialogCallBack func(dlg *Dialog)
var dlgCount = 0
// Dialog is main windows struct.
type Dialog struct {
WindowBase
items map[win.HWND]Widget
iddMap map[uintptr]Widget
config *DialogConfig
// Indicates whether it is a modal dialog
cb ModalDialogCallBack
wndCallBack uintptr
// if return true,will eat message.
OnClose func() bool
OnDestroy func()
// TODO Support optionsl sub wnd class.
}
//func NewDialogIndirect(idd uintptr, parent win.HWND, dialogConfig *DialogConfig) (dlg *Dialog, err error) {
// dlg, err = NewDialog(idd, parent, dialogConfig)
// if err != nil {
// return
// }
// var rect = dlg.GetWindowRect()
// dlg.SetBounds(Rectangle{
// X: 0,
// Y: 0,
// Width: int(rect.Right - rect.Left),
// Height: int(rect.Bottom - rect.Top),
// })
// return
//}
// NewDialog create a new Dialog.
func NewDialog(idd uintptr, parent win.HWND, dialogConfig *DialogConfig) (dlg *Dialog, err error) {
if dialogConfig == nil {
dialogConfig = &DialogConfig{}
}
dlg = &Dialog{
items: make(map[win.HWND]Widget),
iddMap: make(map[uintptr]Widget),
config: dialogConfig,
}
dlg.idd = idd
dlg.wndCallBack = syscall.NewCallback(dlg.dialogWndProc)
h := win.CreateDialogParam(hInstance, win.MAKEINTRESOURCE(idd), parent, dlg.wndCallBack, 0)
if h == 0 {
err = errors.New("Create Dialog error:" + strconv.Itoa(int(idd)))
return
}
dlgCount++
return
}
// NewModalDialog create a new modal Dialog.
func NewModalDialog(idd uintptr, parent win.HWND, dialogConfig *DialogConfig, cb ModalDialogCallBack) int {
if dialogConfig == nil {
dialogConfig = &DialogConfig{}
}
dlg := &Dialog{
items: make(map[win.HWND]Widget),
iddMap: make(map[uintptr]Widget),
config: dialogConfig,
cb: cb,
}
dlg.idd = idd
return win.DialogBoxParam(hInstance, win.MAKEINTRESOURCE(idd), parent, syscall.NewCallback(dlg.dialogWndProc), 0)
}
func (dlg *Dialog) dialogWndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
//log.Println("NewDialog.WndProc", hwnd, "msg:", msg, "wparam:", strconv.FormatInt(int64(win.HIWORD(uint32(wParam))), 16), win.LOWORD(uint32(wParam)), "lparam:", lParam)
// process subclassing items
if dlg.hwnd != hwnd {
if item, ok := dlg.items[hwnd]; ok {
return item.WndProc(msg, wParam, lParam)
}
}
if lParam != 0 && dlg.hwnd == hwnd {
if item, ok := dlg.items[win.HWND(lParam)]; ok {
return item.WndProc(msg, wParam, lParam)
}
}
switch msg {
//case win.WM_ACTIVATEAPP:
//
// return 1
case win.WM_INITDIALOG:
log.Println("wm init Dialog", hwnd, dlg.idd)
dlg.hwnd = hwnd
if dlg.config.Widgets != nil {
dlg.BindWidgets(dlg.config.Widgets...)
}
if dlg.cb != nil {
dlgCount++
dlg.cb(dlg)
return 0
}
return 1
//case win.WM_COMMAND:
// // process WM_COMMAND for dlg items.
// return 0
case win.WM_CLOSE:
log.Println("WM_CLOSE", hwnd)
if dlg.OnClose != nil {
if dlg.OnClose() {
return 0
}
}
if dlg.cb != nil {
win.EndDialog(hwnd, 0)
} else {
win.DestroyWindow(hwnd)
}
return 0
case win.WM_DESTROY:
log.Println("WM_DESTROY", hwnd, dlgCount)
if dlg.OnDestroy != nil {
dlg.OnDestroy()
}
dlgCount--
if dlgCount == 0 {
win.PostQuitMessage(0)
}
return 0
case win.WM_DELETEITEM:
// TODO
// Sent to the owner of a list box or combo box when the list box or combo box is destroyed or
// when items are removed by the LB_DELETESTRING, LB_RESETCONTENT, CB_DELETESTRING, or CB_RESETCONTENT message.
// The system sends a WM_DELETEITEM message for each deleted item. The system sends the WM_DELETEITEM message
// for any deleted list box or combo box item with nonzero item data.
// Parameters
//wParam
//Specifies the identifier of the control that sent the WM_DELETEITEM message.
//
//lParam
//Pointer to a DELETEITEMSTRUCT structure that contains information about the item deleted from a list box.
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-deleteitemstruct
//
//Return value
//An application should return TRUE if it processes this message.
case win.WM_NOTIFY:
// trans message to child.
nmhdr := (*win.NMHDR)(unsafe.Pointer(lParam))
if item, ok := dlg.items[nmhdr.HwndFrom]; ok {
return item.WndProc(msg, wParam, lParam)
}
}
return uintptr(0)
}
func (dlg *Dialog) getDlgItem(id uintptr) (h win.HWND, err error) {
h = win.GetDlgItem(dlg.hwnd, int32(id))
if h == 0 {
err = errors.New("GetDlgItem Error:" + strconv.Itoa(int(dlg.hwnd)) + " id:" + strconv.Itoa(int(id)))
return
}
return
}
// SetIcon set Window Icon.
func (dlg *Dialog) SetIcon(id uintptr) {
h := win.LoadIcon(hInstance, win.MAKEINTRESOURCE(id))
dlg.AsWindowBase().SetIcon(0, uintptr(h), false)
dlg.AsWindowBase().SetIcon(1, uintptr(h), false)
}
// BindWidgets bind dialog items.
func (dlg *Dialog) BindWidgets(widgets ...Widget) error {
var h win.HWND
var err error
for _, w := range widgets {
base := w.AsWindowBase()
// support custom widgets.
h = base.Handle()
if h == 0 {
h, err = dlg.getDlgItem(base.idd)
}
if err != nil {
log.Println("BindWidgets error", err, widgets)
return err
}
if base.Subclassing {
log.Println("sub classing idd", base.idd, h)
base.lpPrevWndFunc = win.SetWindowLongPtr(h, win.GWL_WNDPROC, dlg.wndCallBack)
}
base.hwnd = h
dlg.items[h] = w
dlg.iddMap[base.idd] = w
}
return err
}
func (dlg *Dialog) GetWidget(idd uintptr) Widget {
return dlg.iddMap[idd]
}