/
associatedEvents.go
545 lines (455 loc) · 14.7 KB
/
associatedEvents.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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
这里主要是窗口资源用来反射关联事件,就可以简化相关代码。
感觉有点蹩脚,暂无好办法替代。
事件名命名的规则:
On + 组件名 + 事件,TForm特殊性,固定为名称为Form, 相关支持的事件见: _events中
例如窗口建完后:
func (f *TForm1) OnFormCreate(sender vcl.IObject)
又如按钮:
func (f *TForm1) OnButton1Click(sender vcl.IObject)
*/
package vcl
import (
"reflect"
"strings"
"fmt"
. "github.com/ying32/govcl/vcl/types"
)
type tComponentItem struct {
Name string
Component *TComponent
}
type tMethod struct {
M reflect.Method
This reflect.Value
}
var (
_eventsTable = make(map[string]tMethod, 0)
// 这里的事件还不全,以后继续补库,先满足基本需求
_events = map[string]struct{ call1, call2 interface{} }{
"Click": {onClick, nil},
"Update": {onUpdate, nil},
"Execute": {onExecute, nil},
"MouseDown": {onMouseDown, nil},
"MouseUp": {onMouseUp, nil},
"MouseMove": {onMouseMove, nil},
"MouseWheel": {onMouseWheel, nil},
"MouseEnter": {onMouseEnter, nil},
"MouseLeave": {onMouseLeave, nil},
"CloseQuery": {onCloseQuery, nil},
"Destroy": {onDestroy, nil},
"Show": {onShow, nil},
"Hide": {onHide, nil},
"Close": {onClose, onFormClose},
"Find": {onFind, nil},
"Replace": {onReplace, nil},
"LinkClick": {onLinkClick, nil},
"Change": {onChange, nil},
"KeyPress": {onKeyPress, nil},
"DblClick": {onDblClick, nil},
"Resize": {onResize, nil},
"KeyDown": {onKeyDown, nil},
"KeyUp": {onKeyUp, nil},
"DropFiles": {onDropFiles, nil},
"Activate": {onActivate, nil},
"Deactivate": {onDeactivate, nil},
"ConstrainedResize": {onConstrainedResize, nil},
"Paint": {onPaint, nil},
"ContextPopup": {onContextPopup, nil},
"DragOver": {onDragOver, nil},
"DragDrop": {onDragDrop, nil},
"StartDrag": {onStartDrag, nil},
"EndDrag": {onEndDrag, nil},
"DockDrop": {onDockDrop, nil},
"DockOver": {onDockOver, nil},
"UnDock": {onUnDock, nil},
"StartDock": {onStartDock, nil},
"GetSiteInfo": {onGetSiteInfo, nil},
"MouseWheelDown": {onMouseWheelDown, nil},
"MouseWheelUp": {onMouseWheelUp, nil},
"Timer": {onTimer, nil},
// Grid
"ColumnMoved": {onColumnMoved, nil},
"DrawCell": {onDrawCell, nil},
"FixedCellClick": {onFixedCellClick, nil}, // libvcl
"GetEditMask": {onGetEditMask, nil},
"GetEditText": {onGetEditText, nil},
"RowMoved": {onRowMoved, nil},
"SelectCell": {onSelectCell, nil},
"SetEditText": {onSetEditText, nil},
"TopLeftChanged": {onTopLeftChanged, nil},
//headercontrol
"DrawSection": {onDrawSection, nil}, // livcl
"SectionCheck": {onSectionCheck, nil}, // libvcl
"SectionClick": {onSectionClick, nil}, // libvcl
"SectionDrag": {onSectionDrag, nil},
"SectionEndDrag": {onSectionEndDrag, nil},
"SectionResize": {onSectionResize, nil},
"SectionTrack": {onSectionTrack, nil}}
// Application 独有事件
_appEvents = map[string]interface{}{
"Hint": onHint,
"Exception": onException,
"Minimize": onMinimize,
"Restore": onRestore,
"Message": onMessage,
"Help": onHelp,
"ShortCut": onShortCut}
)
func getComponents(f *TForm) (ret []tComponentItem) {
ret = make([]tComponentItem, f.ComponentCount()+1)
ret[0].Name = f.Name()
ret[0].Component = ComponentFromObj(f)
var i int32
for i = 0; i < f.ComponentCount(); i++ {
c := f.Components(i)
ret[i+1].Name = c.Name()
ret[i+1].Component = c
}
return
}
// addMethod 将相关方法添加到表中
func addMethod(M reflect.Method, obj IObject, eventTpy string, This reflect.Value) {
key := fmt.Sprintf("%d_%s", obj.Instance(), eventTpy)
var mm tMethod
mm.M = M
mm.This = This
_eventsTable[key] = mm
}
// getMethod 查表,找此对象事件对应的方法
func getMethod(sender IObject, eventTpy string) (tMethod, bool) {
key := fmt.Sprintf("%d_%s", sender.Instance(), eventTpy)
m, ok := _eventsTable[key]
return m, ok
}
func iifstr(b bool, atrue, afalse string) string {
if b {
return atrue
}
return afalse
}
// associatedEvents 关联事件,诶,感觉很蹩脚。。。。这方面感觉还是Delphi方便。
func associatedEvents(vv reflect.Value, comps []tComponentItem) {
defer func() {
if err := recover(); err != nil {
fmt.Println("associatedEvents ERROR: ", err)
}
}()
// --- test
// test test test
// 关联事件, 查找组件
//vv := reflect.ValueOf(inst)
vt := vv.Type()
eventMethods := make(map[string]reflect.Method, 0)
for i := 0; i < vt.NumMethod(); i++ {
m := vt.Method(i)
if strings.HasPrefix(m.Name, "On") {
eventMethods[m.Name] = m
}
}
// 简化查找
findMethod := func(name, event string) (reflect.Method, bool) {
v, ok := eventMethods[fmt.Sprintf("On%s%s", name, event)]
return v, ok
}
// 因为TForm的特殊性,所以需要重新命名下
realName := func(ii int, name string) string {
if ii > 0 {
return name
}
return "Form"
}
// 遍沥组件,找寻方法
for i, item := range comps {
if item.Name != "" {
for k, v := range _events {
if m, ok := findMethod(realName(i, item.Name), k); ok {
callProc := v.call1
if (i == 0) && k == "Close" {
callProc = v.call2
}
addNotifyEvent(vv, iifstr(i == 0, "TForm", item.Name), m, item.Component, k, callProc)
}
}
}
}
// 一些特殊的,比如Application
addApplicationNotifyEvent(vv, eventMethods)
// 最后调用OnCreate
callFormCreate(vv)
}
func callFormCreate(vv reflect.Value) {
defer func() {
if err := recover(); err != nil {
fmt.Println("callFormCreate ERROR: ", err)
}
}()
// 查找默认的Form创建,命名规则以 On+组件名+方法名
m := vv.MethodByName(fmt.Sprintf("OnFormCreate"))
if m.IsValid() {
m.Call([]reflect.Value{vv})
}
}
func mcall(eventType string, params ...interface{}) {
if len(params) < 1 {
return
}
// 这里要防止异常,不然全部挂了
defer func() {
if err := recover(); err != nil {
fmt.Println("mcall ERROR:", err)
}
}()
sender := params[0].(IObject)
if mmm, ok := getMethod(sender, eventType); ok {
ps := make([]reflect.Value, len(params)+1)
ps[0] = mmm.This
for i, p := range params {
ps[i+1] = reflect.ValueOf(p)
}
mmm.M.Func.Call(ps)
}
}
func onClick(sender IObject) {
mcall("Click", sender)
}
func onUpdate(sender IObject) {
mcall("Update", sender)
}
func onExecute(sender IObject) {
mcall("Execute", sender)
}
func onDestroy(sender IObject) {
mcall("Destroy", sender)
}
func onCloseQuery(sender IObject, canClose *bool) {
mcall("CloseQuery", sender, canClose)
}
func onMouseEnter(sender IObject) {
mcall("MouseEnter", sender)
}
func onMouseLeave(sender IObject) {
mcall("MouseLeave", sender)
}
func onShow(sender IObject) {
mcall("Show", sender)
}
func onHide(sender IObject) {
mcall("Hide", sender)
}
func onClose(sender IObject) {
mcall("Close", sender)
}
func onFormClose(sender IObject, action *TCloseAction) {
mcall("Close", sender, action)
}
func onFind(sender IObject) {
mcall("Find", sender)
}
func onReplace(sender IObject) {
mcall("Replace", sender)
}
func onMouseDown(sender IObject, button TMouseButton, shift TShiftState, x, y int32) {
mcall("MouseDown", sender, button, shift, x, y)
}
func onMouseUp(sender IObject, button TMouseButton, shift TShiftState, x, y int32) {
mcall("MouseUp", sender, button, shift, x, y)
}
func onMouseMove(sender IObject, shift TShiftState, x, y int32) {
mcall("MouseMove", sender, shift, x, y)
}
func onMouseWheel(sender IObject, shift TShiftState, wheelDelta, x, y int32, handled *bool) {
mcall("MouseWheel", sender, shift, wheelDelta, x, y, handled)
}
func onLinkClick(sender IObject, link string, linktype TSysLinkType) {
mcall("LinkClick", sender, link, linktype)
}
func onChange(sender IObject) {
mcall("Change", sender)
}
func onKeyPress(sender IObject, key *Char) {
mcall("KeyPress", sender, key)
}
func onDblClick(sender IObject) {
mcall("DblClick", sender)
}
func onResize(sender IObject) {
mcall("Resize", sender)
}
func onKeyDown(sender IObject, key *Char, shift TShiftState) {
mcall("KeyDown", sender, key, shift)
}
func onKeyUp(sender IObject, key *Char, shift TShiftState) {
mcall("KeyUp", sender, key, shift)
}
func onDropFiles(sender IObject, aFileNames []string) {
mcall("DropFiles", sender, aFileNames)
}
func onPaint(sender IObject) {
mcall("Paint", sender)
}
func onActivate(sender IObject) {
mcall("Activate", sender)
}
func onDeactivate(sender IObject) {
mcall("Deactivate", sender)
}
func onConstrainedResize(sender IObject, minWidth, minHeight, maxWidth, maxHeight *int32) {
mcall("ConstrainedResize", sender, minWidth, minHeight, maxWidth, maxHeight)
}
func onContextPopup(sender IObject, mousePos TPoint, handled *bool) {
mcall("ContextPopup", sender, mousePos, handled)
}
func onDragOver(sender, source IObject, x, y int32, state TDragState, accept *bool) {
mcall("DragOver", sender, source, x, y, state, accept)
}
func onDragDrop(sender, source IObject, x, y int32) {
mcall("DragDrop", sender, source, x, y)
}
func onStartDrag(sender IObject, dragObject *TDragObject) {
mcall("StartDrag", sender, dragObject)
}
func onEndDrag(sender, target IObject, x, y int32) {
mcall("EndDrag", sender, target, x, y)
}
func onDockDrop(sender IObject, source *TDragDockObject, x, y int32) {
mcall("DockDrop", sender, x, y)
}
func onDockOver(sender IObject, source *TDragDockObject, x, y int32, state TDragState, accept *bool) {
mcall("DockOver", sender, source, x, y, state, accept)
}
func onUnDock(sender IObject, client *TControl, newTarget *TControl, allow *bool) {
mcall("UnDock", sender, client, newTarget, allow)
}
func onStartDock(sender IObject, dragObject *TDragDockObject) {
mcall("StartDock", sender, dragObject)
}
func onGetSiteInfo(sender IObject, dockClient *TControl, influenceRect *TRect, mousePos TPoint, canDock *bool) {
mcall("GetSiteInfo", sender, dockClient, influenceRect, mousePos, canDock)
}
func onMouseWheelDown(sender IObject, shift TShiftState, mousePos TPoint, handled *bool) {
mcall("MouseWheelDown", sender, shift, mousePos, handled)
}
func onMouseWheelUp(sender IObject, shift TShiftState, mousePos TPoint, handled *bool) {
mcall("MouseWheelUp", sender, shift, mousePos, handled)
}
func onTimer(sender IObject) {
mcall("Timer", sender)
}
// Grid
func onColumnMoved(sender IObject, fromIndex, toIndex int32) {
mcall("ColumnMoved", sender, fromIndex, toIndex)
}
func onDrawCell(sender IObject, aCol, aRow int32, aRect TRect, state TGridDrawState) {
mcall("DrawCell", sender, aCol, aRow, aRect, state)
}
func onFixedCellClick(sender IObject, aCol, aRow int32) {
mcall("FixedCellClick", sender, aCol, aRow)
}
func onGetEditMask(sender IObject, aCol, aRow int32, value *string) {
mcall("GetEditMask", sender, aCol, aRow, value)
}
func onGetEditText(sender IObject, aCol, aRow int32, value *string) {
mcall("GetEditText", sender, aCol, aRow, value)
}
func onRowMoved(sender IObject, fromIndex, toIndex int32) {
mcall("RowMoved", sender, fromIndex, toIndex)
}
func onSelectCell(sender IObject, aCol, aRow int32, canSelect *bool) {
mcall("SelectCell", sender, aCol, aRow, canSelect)
}
func onSetEditText(sender IObject, aCol, aRow int32, value string) {
mcall("SetEditText", sender, aCol, aRow, value)
}
func onTopLeftChanged(sender IObject) {
mcall("TopLeftChanged", sender)
}
//headercontrol
func onDrawSection(headerControl *THeaderControl, section *THeaderSection, aRect TRect, pressed bool) {
mcall("DrawSection", headerControl, section, aRect, pressed)
}
func onSectionCheck(headerControl *THeaderControl, section *THeaderSection) {
mcall("SectionCheck", headerControl, section)
}
func onSectionClick(headerControl *THeaderControl, section *THeaderSection) {
mcall("SectionClick", headerControl, section)
}
func onSectionDrag(sender IObject, fromSection, toSection *THeaderSection, allowDrag *bool) {
mcall("SectionDrag", sender, fromSection, toSection, allowDrag)
}
func onSectionEndDrag(sender IObject) {
mcall("SectionEndDrag", sender)
}
func onSectionResize(headerControl *THeaderControl, section *THeaderSection) {
mcall("SectionResize", headerControl, section)
}
func onSectionTrack(headerControl *THeaderControl, section *THeaderSection, width int32, state TSectionTrackState) {
mcall("SectionTrack", headerControl, section, width, state)
}
// -- Application
func onException(sender IObject, e *Exception) {
mcall("Exception", sender, e)
}
func onHint(sender IObject) {
mcall("Hint", sender)
}
func onMinimize(sender IObject) {
mcall("Minimize", sender)
}
func onRestore(sender IObject) {
mcall("Restore", sender)
}
func onMessage(msg *TMsg, handled *bool) {
mcall("Message", msg, handled)
}
func onHelp(command uint16, data THelpEventData, callhelp, result *bool) {
mcall("Help", data, callhelp, result)
}
func onShortCut(msg *TWMKey, handled *bool) {
mcall("ShortCut", msg, handled)
}
// callSetEventMethod 公用的call SetOnXXXX方法
func callSetEventMethod(v reflect.Value, eventType string, component IObject, this reflect.Value, m reflect.Method, fn interface{}) {
defer func() {
if err := recover(); err != nil {
fmt.Println("callSetEventMethod ERROR: ", err, ", eventType:", eventType, ", Component:", component, ", this:", this)
}
}()
event := v.MethodByName("SetOn" + eventType)
if event.IsValid() {
addMethod(m, component, eventType, this)
event.Call([]reflect.Value{reflect.ValueOf(fn)})
}
}
// addNotifyEvent
func addNotifyEvent(vv reflect.Value, compName string, m reflect.Method, c IObject, eventType string, fn interface{}) {
vCtl := vv.Elem().FieldByName(compName)
if !vCtl.IsValid() {
return
}
callSetEventMethod(vCtl, eventType, c, vv, m, fn)
}
// addApplicationNotifyEvent
// 添加Application的关联事件,在一个程序内,application只的事件只有最后一次设置的才会生效。
// 因为Application是单例存在,推荐在主窗口内处理就行了。
func addApplicationNotifyEvent(vv reflect.Value, eventMethods map[string]reflect.Method) {
app := reflect.ValueOf(Application)
if app.IsValid() {
findMethod := func(event string) (reflect.Method, bool) {
v, ok := eventMethods[fmt.Sprintf("OnApplication%s", event)]
return v, ok
}
for eventName, fn := range _appEvents {
if m, ok := findMethod(eventName); ok {
callSetEventMethod(app, eventName, Application, vv, m, fn)
}
}
}
}
//var typ TNotifyEvent
//var fnx = func(args []reflect.Value) []reflect.Value {
// m.Func.Call([]reflect.Value{vv, vCtl})
// return nil
//}
//fn := reflect.MakeFunc(reflect.TypeOf(typ), fnx)
//event.Call([]reflect.Value{fn})