-
Notifications
You must be signed in to change notification settings - Fork 45
/
controlbase.go
277 lines (218 loc) · 5.49 KB
/
controlbase.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
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
package gform
import (
"github.com/AllenDang/w32"
)
type ControlBase struct {
hwnd w32.HWND
font *Font
parent Controller
evtHandlerMap map[uint]EventHandler
isForm bool
// General events
onCreate EventManager
onClose EventManager
// Focus events
onKillFocus EventManager
onSetFocus EventManager
// Drag and drop events
onDropFiles EventManager
// Mouse events
onLBDown EventManager
onLBUp EventManager
onMBDown EventManager
onMBUp EventManager
onRBDown EventManager
onRBUp EventManager
onMouseHover EventManager
onMouseLeave EventManager
// Keyboard events
onKeyUp EventManager
// Paint events
onPaint EventManager
onSize EventManager
}
func (this *ControlBase) init(parent Controller) {
if this.hwnd == 0 {
panic("hwnd cannot be nil")
}
if parent != nil {
this.parent = parent
}
}
func (this *ControlBase) Handle() w32.HWND {
return this.hwnd
}
func (this *ControlBase) SetCaption(caption string) {
w32.SetWindowText(this.hwnd, caption)
}
func (this *ControlBase) Caption() string {
return w32.GetWindowText(this.hwnd)
}
func (this *ControlBase) Close() {
UnRegMsgHandler(this.hwnd)
w32.DestroyWindow(this.hwnd)
}
func (this *ControlBase) SetSize(width, height int) {
x, y := this.Pos()
w32.MoveWindow(this.hwnd, x, y, width, height, true)
}
func (this *ControlBase) Size() (width, height int) {
rect := w32.GetWindowRect(this.hwnd)
width = int(rect.Right - rect.Left)
height = int(rect.Bottom - rect.Top)
return
}
func (this *ControlBase) Width() int {
rect := w32.GetWindowRect(this.hwnd)
return int(rect.Right - rect.Left)
}
func (this *ControlBase) Height() int {
rect := w32.GetWindowRect(this.hwnd)
return int(rect.Bottom - rect.Top)
}
func (this *ControlBase) SetPos(x, y int) {
w, h := this.Size()
if w == 0 {
w = 100
}
if h == 0 {
h = 25
}
w32.MoveWindow(this.hwnd, x, y, w, h, true)
}
func (this *ControlBase) Pos() (x, y int) {
rect := w32.GetWindowRect(this.hwnd)
x = int(rect.Left)
y = int(rect.Top)
if !this.isForm && this.parent != nil {
x, y, _ = w32.ScreenToClient(this.parent.Handle(), x, y)
}
return
}
func (this *ControlBase) Visible() bool {
return w32.IsWindowVisible(this.hwnd)
}
func (this *ControlBase) Bounds() *Rect {
rect := w32.GetWindowRect(this.hwnd)
if this.isForm {
return &Rect{*rect}
}
return ScreenToClientRect(this.hwnd, rect)
}
func (this *ControlBase) ClientRect() *Rect {
rect := w32.GetClientRect(this.hwnd)
return ScreenToClientRect(this.hwnd, rect)
}
func (this *ControlBase) Show() {
w32.ShowWindow(this.hwnd, w32.SW_SHOWDEFAULT)
}
func (this *ControlBase) Hide() {
w32.ShowWindow(this.hwnd, w32.SW_HIDE)
}
func (this *ControlBase) Enabled() bool {
return w32.IsWindowEnabled(this.hwnd)
}
func (this *ControlBase) SetEnabled(b bool) {
w32.EnableWindow(this.hwnd, b)
}
func (this *ControlBase) Focus() {
w32.SetFocus(this.hwnd)
}
func (this *ControlBase) Invalidate(erase bool) {
pRect := w32.GetClientRect(this.hwnd)
if this.isForm {
w32.InvalidateRect(this.hwnd, pRect, erase)
} else {
rc := ScreenToClientRect(this.hwnd, pRect)
w32.InvalidateRect(this.hwnd, rc.GetW32Rect(), erase)
}
}
func (this *ControlBase) Parent() Controller {
return this.parent
}
func (this *ControlBase) Font() *Font {
return this.font
}
func (this *ControlBase) SetFont(font *Font) {
w32.SendMessage(this.hwnd, w32.WM_SETFONT, uintptr(font.hfont), 1)
this.font = font
}
func (this *ControlBase) EnableDragAcceptFiles(b bool) {
w32.DragAcceptFiles(this.hwnd, b)
}
func (this *ControlBase) InvokeRequired() bool {
if this.hwnd == 0 {
return false
}
windowThreadId, _ := w32.GetWindowThreadProcessId(this.hwnd)
currentThreadId := w32.GetCurrentThread()
return windowThreadId != currentThreadId
}
func (this *ControlBase) PreTranslateMessage(msg *w32.MSG) bool {
return false
}
func (this *ControlBase) Bind(msg uint, handler EventHandler) {
//Check whether map is already created
if this.evtHandlerMap == nil {
this.evtHandlerMap = make(map[uint]EventHandler)
}
if handler == nil {
delete(this.evtHandlerMap, msg)
} else {
this.evtHandlerMap[msg] = handler
}
}
// Get binded handlers for specifed message.
func (this *ControlBase) BindedHandler(msg uint) (EventHandler, bool) {
handler, ok := this.evtHandlerMap[msg]
return handler, ok
}
//Events
func (this *ControlBase) OnCreate() *EventManager {
return &this.onCreate
}
func (this *ControlBase) OnClose() *EventManager {
return &this.onClose
}
func (this *ControlBase) OnKillFocus() *EventManager {
return &this.onKillFocus
}
func (this *ControlBase) OnSetFocus() *EventManager {
return &this.onSetFocus
}
func (this *ControlBase) OnDropFiles() *EventManager {
return &this.onDropFiles
}
func (this *ControlBase) OnLBDown() *EventManager {
return &this.onLBDown
}
func (this *ControlBase) OnLBUp() *EventManager {
return &this.onLBUp
}
func (this *ControlBase) OnMBDown() *EventManager {
return &this.onMBDown
}
func (this *ControlBase) OnMBUp() *EventManager {
return &this.onMBUp
}
func (this *ControlBase) OnRBDown() *EventManager {
return &this.onRBDown
}
func (this *ControlBase) OnRBUp() *EventManager {
return &this.onRBUp
}
func (this *ControlBase) OnMouseHover() *EventManager {
return &this.onMouseHover
}
func (this *ControlBase) OnMouseLeave() *EventManager {
return &this.onMouseLeave
}
func (this *ControlBase) OnPaint() *EventManager {
return &this.onPaint
}
func (this *ControlBase) OnSize() *EventManager {
return &this.onSize
}
func (this *ControlBase) OnKeyUp() *EventManager {
return &this.onKeyUp
}