forked from wailsapp/wails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog.go
136 lines (111 loc) · 3.1 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
//go:build windows
/*
* Copyright (C) 2019 The Winc Authors. All Rights Reserved.
* Copyright (C) 2010-2013 Allen Dang. All Rights Reserved.
*/
package winc
import "github.com/rribou/wails/v2/internal/frontend/desktop/windows/winc/w32"
// Dialog displayed as z-order top window until closed.
// It also disables parent window so it can not be clicked.
type Dialog struct {
Form
isModal bool
btnOk *PushButton
btnCancel *PushButton
onLoad EventManager
onOk EventManager
onCancel EventManager
}
func NewDialog(parent Controller) *Dialog {
dlg := new(Dialog)
dlg.isForm = true
dlg.isModal = true
RegClassOnlyOnce("winc_Dialog")
dlg.hwnd = CreateWindow("winc_Dialog", parent, w32.WS_EX_CONTROLPARENT, /* IMPORTANT */
w32.WS_SYSMENU|w32.WS_CAPTION|w32.WS_THICKFRAME /*|w32.WS_BORDER|w32.WS_POPUP*/)
dlg.parent = parent
// dlg might fail if icon resource is not embedded in the binary
if ico, err := NewIconFromResource(GetAppInstance(), uint16(AppIconID)); err == nil {
dlg.SetIcon(0, ico)
}
// Dlg forces display of focus rectangles, as soon as the user starts to type.
w32.SendMessage(dlg.hwnd, w32.WM_CHANGEUISTATE, w32.UIS_INITIALIZE, 0)
RegMsgHandler(dlg)
dlg.SetFont(DefaultFont)
dlg.SetText("Form")
dlg.SetSize(200, 100)
return dlg
}
func (dlg *Dialog) SetModal(modal bool) {
dlg.isModal = modal
}
// SetButtons wires up dialog events to buttons. btnCancel can be nil.
func (dlg *Dialog) SetButtons(btnOk *PushButton, btnCancel *PushButton) {
dlg.btnOk = btnOk
dlg.btnOk.SetDefault()
dlg.btnCancel = btnCancel
}
// Events
func (dlg *Dialog) OnLoad() *EventManager {
return &dlg.onLoad
}
func (dlg *Dialog) OnOk() *EventManager {
return &dlg.onOk
}
func (dlg *Dialog) OnCancel() *EventManager {
return &dlg.onCancel
}
// PreTranslateMessage handles dialog specific messages. IMPORTANT.
func (dlg *Dialog) PreTranslateMessage(msg *w32.MSG) bool {
if msg.Message >= w32.WM_KEYFIRST && msg.Message <= w32.WM_KEYLAST {
if w32.IsDialogMessage(dlg.hwnd, msg) {
return true
}
}
return false
}
// Show dialog performs special setup for dialog windows.
func (dlg *Dialog) Show() {
if dlg.isModal {
dlg.Parent().SetEnabled(false)
}
dlg.onLoad.Fire(NewEvent(dlg, nil))
dlg.Form.Show()
}
// Close dialog when you done with it.
func (dlg *Dialog) Close() {
if dlg.isModal {
dlg.Parent().SetEnabled(true)
}
dlg.ControlBase.Close()
}
func (dlg *Dialog) cancel() {
if dlg.btnCancel != nil {
dlg.btnCancel.onClick.Fire(NewEvent(dlg.btnCancel, nil))
}
dlg.onCancel.Fire(NewEvent(dlg, nil))
}
func (dlg *Dialog) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
switch msg {
case w32.WM_COMMAND:
switch w32.LOWORD(uint32(wparam)) {
case w32.IDOK:
if dlg.btnOk != nil {
dlg.btnOk.onClick.Fire(NewEvent(dlg.btnOk, nil))
}
dlg.onOk.Fire(NewEvent(dlg, nil))
return w32.TRUE
case w32.IDCANCEL:
dlg.cancel()
return w32.TRUE
}
case w32.WM_CLOSE:
dlg.cancel() // use onCancel or dlg.btnCancel.OnClick to close
return 0
case w32.WM_DESTROY:
if dlg.isModal {
dlg.Parent().SetEnabled(true)
}
}
return w32.DefWindowProc(dlg.hwnd, msg, wparam, lparam)
}