-
Notifications
You must be signed in to change notification settings - Fork 0
/
guiUtils.au3
2872 lines (2497 loc) · 122 KB
/
guiUtils.au3
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include-once
; #INDEX# =======================================================================================================================
; Title .........: GUI Utility UDF
; AutoIt Version : 1.0
; Description ...: Functions to simply create GUIs directly from KODA file or with a simple JSON definition, and handle them
; as advanced input boxes.
; Author(s) .....: matwachich
; Links .........: https://www.autoitscript.com/forum/topic/201594-guiutils-simply-create-guis-from-koda-input-dialogs-from-json-and-handle-them-like-inputbox-with-one-function/
; =================================================================================================
#cs
Bugs:
- quand on créé un dialog à partir d'un KXF, si le premier contrôle est à x=0,y=0, alors la taille de la fenêtre est mal calculée
#ce
#include <Date.au3>
#include <Math.au3>
#include <Array.au3>
#include <GuiTab.au3>
#include <String.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>
#include <GuiListBox.au3>
#include <GuiComboBox.au3>
#include <GUIConstants.au3>
#include <GuiIPAddress.au3>
#include <GuiStatusBar.au3>
#include <EditConstants.au3>
#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <ListBoxConstants.au3>
#include <WindowsConstants.au3>
#include <DateTimeConstants.au3>
#include <GuiDateTimePicker.au3>
#include <ListViewConstants.au3>
#include "Json.au3"
#include "Object.au3"
#include "StringSize.au3"
;~ #include <WinAPIDiag.au3>
;~ #include <WMDebug.au3>
;~ #include <_Dbug.au3>
; #VARIABLES# ===================================================================================================================
Global $__gGuiUtils_inputDialog_oCurrentForm = Null
Global $__gGuiUtils_fnCustomInputReader = Null, $__gGuiUtils_fnCustomInputWriter = Null
Global $__gGuiUtils_oForms = _objCreate() ; object that will hold pairs of hwnd:oFormObject
; =================================================================================================
; #CONSTANTS# ===================================================================================================================
; Default JSON form configuration
Global Const $__gGuiUtils_jsonParser_oDefaultConfig = Json_Decode("{" & _
'title:"InputBox" ' & _
'labelsMaxWidth:' & (@DesktopWidth / 5) & ' ' & _
'inputsWidth:' & (@DesktopWidth / 5) & ' ' & _
'maxHeight:' & (@DesktopHeight * 2 / 3) & ' ' & _
'margin:8 ' & _
'inputLabelVerticalPadding:3 ' & _
'submitBtn:{text:"OK" width:100 height:25} ' & _
'cancelBtn:{text:"Cancel" width:80 height:25}' & _
"}")
; =================================================================================================
; #CURRENT# =====================================================================================================================
; > Region - KODA file parsing
; _GUIUtils_CreateFromKODA
;
; > Region - JSON definition parsing (advanced InputBox)
; _GUIUtils_CreateFromJSON
;
; > Region - InputBox behaviour
; _GUIUtils_InputDialog
;
; > Region - MultiGUIRegisterMsg
; _GUIUtils_RegisterMsg
;
; > Region - Controls Subclassing
; _GUIUtils_SubclassSet
;
; > Region - FormObject Accessing
; _GUIUtils_Destroy
; _GUIUtils_ListForms
; _GUIUtils_SetAccels
; _GUIUtils_HWnd
; _GUIUtils_FormName
; _GUIUtils_CtrlSet
; _GUIUtils_CtrlID
; _GUIUtils_CtrlNameByID
; _GUIUtils_HCtrl
; _GUIUtils_CtrlNameByHandle
; _GUIUtils_CtrlList
; _GUIUtils_CtrlChildren
; _GUIUtils_UserDataSet
; _GUIUtils_UserDataGet
; _GUIUtils_UserDataExists
; _GUIUtils_UserDataDel
; _GUIUtils_UserDataEmpty
; _GUIUtils_SetInputs
; _GUIUtils_GetInputs
; _GUIUtils_SetButtons
; _GUIUtils_ReadInputs
; _GUIUtils_WriteInputs
; _GUIUtils_SetCustomInputsReader
; _GUIUtils_SetCustomInputsWriter
;
; > Region - Misc
; _GUIUtils_GetFont
; =================================================================================================
; #INTERNAL_USE_ONLY# ===========================================================================================================
; __guiUtils_registerMsgDispatcher
; __guiUtils_getFormHandleOrObject
; __guiUtils_getSupportedInputsList
; __guiUtils_identifyControl
; __guiUtils_kodaParser_createGUI
; __guiUtils_kodaParser_createControls
; __guiUtils_kodaParser_sortObjects
; __guiUtils_kodaParser_calculateGUIsize
; __guiUtils_kodaParser_readObjectProperties
; __guiUtils_kodaParser_readProperty
; __guiUtils_kodaParser_processFont
; __guiUtils_kodaParser_identifiers_fontStyle
; __guiUtils_kodaParser_identifiers_docking
; __guiUtils_kodaParser_identifiers_cursor
; __guiUtils_kodaParser_identifiers_colors
; __guiUtils_jsonParser_stringSize
; __guiUtils_jsonParser_getFont
; __guiUtils_jsonParser_controlSetFontAndColors
; __guiUtils_jsonParser_getArray
; __guiUtils_inputDialog_subClassProc
; __guiUtils_inputDialog_controlGet
; __guiUtils_inputDialog_controlSet
; =================================================================================================
# =================================================================================================
#Region - KODA file parsing
# =================================================================================================
; #FUNCTION# ====================================================================================================================
; Name ..........: _GUIUtils_CreateFromKODA
; Description ...: Parse a KODA Form file (.kxf) and builds a GUI
; Syntax ........: _GUIUtils_CreateFromKODA($sFileOrXML[, $hParent = Null[, $iWidth = -1[, $iHeight = -1]]])
; Parameters ....: $sFileOrXML - KODA file path or content.
; $hParent - [optional] parent GUI handle. Default is Null.
; $iWidth - [optional] override automatic GUI size calculation. Default is -1 for no override.
; $iHeight - [optional] override automatic GUI size calculation. Default is -1 for no override.
; Return values .: Object (scripting.dictionary) representing the GUI
; Author ........: matwachich
; Remarks .......: You can access to GUI hwnd and control IDs and handles by their names (using helper functions).
; GUI size is calculated to fit GUI content. It is not the size defined in KODA (I'm working to fix this).
; Unsupported things (todo):
; - Controls: Graphic, Updown, Avi, Tray Menu, COM Object, Status Bar, Tool Bar, Image List
; - Hotkeys (accelerator)
; Related .......: _GUIUtils_SetAccels, _GUIUtils_HWnd, _GUIUtils_CtrlID, _GUIUtils_HCtrl
; =================================================================================================
Func _GUIUtils_CreateFromKODA($sFileOrXML, $hParent = Null, $iWidth = -1, $iHeight = -1)
; returned object that will contain control IDs
Local $oForm = _objCreate()
; XML DOM object
Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.Async = False
; load XML
If FileExists($sFileOrXML) Then
Local $hF = FileOpen($sFileOrXML, 512)
$sFileOrXML = FileRead($hF)
FileClose($hF)
EndIf
If Not $oXML.LoadXML($sFileOrXML) Then Return SetError(1, 0, Null)
; create GUI
__guiUtils_kodaParser_createGUI($oForm, $oXML, $iWidth, $iHeight, __guiUtils_getFormHandleOrObject($hParent, True))
If @error Then Return SetError(1 + @error, 0, Null)
; get parsed gui properties
Local $oGuiProps = _objGet($oForm, "##guiProperties")
; create controls
Local $oControls = $oXML.selectNodes("/object/components/object")
__guiUtils_kodaParser_createControls($oForm, $oControls, 0, 0, Null, True)
; set size if not manualy provided
If $iWidth <= 0 Or $iHeight <= 0 Then
; calculate GUI size from calculated client area size
Local $tRect = _WinAPI_CreateRect(0, 0, _objGet($oForm, "##minX") + _objGet($oForm, "##maxX"), _objGet($oForm, "##minY") + _objGet($oForm, "##maxY"))
_WinAPI_AdjustWindowRectEx($tRect, $oGuiProps.Item("Style"), $oGuiProps.Item("ExStyle"), _objGet($oGuiProps, "Menu", "") <> "")
$iWidth = Abs($tRect.Left) + Abs($tRect.Right)
$iHeight = Abs($tRect.Top) + Abs($tRect.Bottom)
; set center position if needed
If _objGet($oGuiProps, "Left", -1) = -1 Then _objSet($oGuiProps, "Left", (@DesktopWidth / 2) - ($iWidth / 2))
If _objGet($oGuiProps, "Top", -1) = -1 Then _objSet($oGuiProps, "Top", (@DesktopHeight / 2) - ($iHeight / 2))
; resize
_WinAPI_MoveWindow($oForm.Item("hwnd"), _
_objGet($oGuiProps, "Left", -1), _objGet($oGuiProps, "Top", -1), _
$iWidth, $iHeight _
)
EndIf
; set visible
If _objGet($oGuiProps, "Visible", False) Then GUISetState(@SW_SHOW, _objGet($oForm, "hwnd"))
; clean and return
For $sKey In $oForm.Keys()
If StringLeft($sKey, 2) = "##" Then _objDel($oForm, $sKey)
Next
; store oForm object and return if
_objSet($__gGuiUtils_oForms, _GUIUtils_HWnd($oForm), $oForm, True)
Return $oForm
EndFunc
# =================================================================================================
#EndRegion
# =================================================================================================
# =================================================================================================
#Region - JSON definition parsing (advanced InputBox)
# =================================================================================================
; #FUNCTION# ====================================================================================================================
; Name ..........: _GUIUtils_CreateFromJSON
; Description ...: Creates an InputBox GUI from JSON definition
; Syntax ........: _GUIUtils_CreateFromJSON($vJson[, $hParent = Null])
; Parameters ....: $vJson - JSON definition.
; $hParent - [optional] parent GUI handle. Default is Null.
; Return values .: Object (scripting.dictionary) representing the GUI
; Author ........: matwachich
; Remarks .......: See JSON_Form_Definition.txt and examples.
; =================================================================================================
Func _GUIUtils_CreateFromJSON($vJson, $hParent = Null)
; parse form definition
Local $oJSON
If Not IsObj($vJson) Then
$oJSON = Json_Decode($vJson)
If Not IsObj($oJSON) Then Return SetError(1, 0, Null)
Else
$oJSON = $vJSON
EndIf
; ensure $hParent is a HWND
$hParent = __guiUtils_getFormHandleOrObject($hParent, True)
; set defaults
_objCopy($oJSON, $__gGuiUtils_jsonParser_oDefaultConfig)
; ---------------------------------------------------------------
; prepare GUI building
Local $aJSONControls = _objGet($oJSON, "controls")
If Not IsArray($aJSONControls) Then Return SetError(1, 0, Null)
; get default GUI font (defaults to parent's font if not provided ; if no parent, $aGUIFont will be Null)
Local $aGUIFont = __guiUtils_jsonParser_getFont($oJSON, _GUIUtils_GetFont($hParent))
; calculate labels Width (for all labels) and Heights (for each one)
Local $iLabelsWidth = 0
For $i = 0 To UBound($aJSONControls) - 1
; no control type is an error
If Not _objExists($aJSONControls[$i], "type") Then Return SetError(1, 0, Null)
; force id for all controls ...
If Not _objGet($aJSONControls[$i], "id") Then _
_objSet($aJSONControls[$i], "id", StringFormat(_objGet($aJSONControls[$i], "type") & "_%02d", $i), True)
Switch _objGet($aJSONControls[$i], "type")
Case "separator", "label" ; ignored
Case "check", "checkbox", "radio", "radiobox"
; ... and label for all but separator and label
If Not _objGet($aJSONControls[$i], "label") Then _
_objSet($aJSONControls[$i], "label", _StringTitleCase(_objGet($aJSONControls[$i], "id")), True)
Case Else
; ... and label for all but separator and label
If Not _objGet($aJSONControls[$i], "label") Then _
_objSet($aJSONControls[$i], "label", _StringTitleCase(_objGet($aJSONControls[$i], "id")), True)
; calculate dimensions of control label
$aSize = __guiUtils_jsonParser_stringSize( _
_objGet($aJSONControls[$i], "label"), _
__guiUtils_jsonParser_getFont($aJSONControls[$i], $aGUIFont), _
_objGet($oJSON, "labelsMaxWidth") _
)
If $aSize[2] > $iLabelsWidth Then $iLabelsWidth = $aSize[2]
; update control definition
_objSet($aJSONControls[$i], "label", $aSize[0]) ; update label string update with @CRLF (to fit in calculated rectangle)
;~ _objSet($aJSONControls[$i], "labelWidth", $aSize[2]) ; store calculated label rectangle (useless: all labels will have $iLabelsWidth width)
_objSet($aJSONControls[$i], "labelHeight", $aSize[3]) ; store calculated label rectangle
EndSwitch
Next
; when labels max width is calculated, re-iterate over controls to resize labels
For $i = 0 To UBound($aJSONControls) - 1
If _objGet($aJSONControls[$i], "type") = "label" Then
$aSize = __guiUtils_jsonParser_stringSize( _
_objGet($aJSONControls[$i], "text"), _
__guiUtils_jsonParser_getFont($aJSONControls[$i], $aGUIFont), _
$iLabelsWidth + _objGet($oJSON, "margin") + _objGet($oJSON, "inputsWidth") _
)
_objSet($aJSONControls[$i], "text", $aSize[0])
_objSet($aJSONControls[$i], "width", $aSize[2])
_objSet($aJSONControls[$i], "height", $aSize[3])
EndIf
Next
; ---------------------------------------------------------------
; build GUI
Local $oForm = _objCreate()
; unset WS_VISIBLE if set (will be reset in the end, after controls creation)
$iStyle = _objGet($oJSON, "style", $GUI_SS_DEFAULT_GUI)
If $iStyle <> -1 And BitAND($iStyle, $WS_VISIBLE) Then $iStyle -= $WS_VISIBLE
Local $hGUI = GUICreate(_objGet($oJSON, "title"), 500, 500, -1, -1, $iStyle, _objGet($oJSON, "exStyle", -1), $hParent)
_objSet($oForm, "hwnd", $hGUI)
_objSet($oForm, "formName", _objGet($oJSON, "title"))
; set GUI's font and colors
If UBound($aGUIFont) = 4 Then
GUISetFont($aGUIFont[0], $aGUIFont[1], $aGUIFont[2], $aGUIFont[3], $hGUI)
Else
; here it means that we dont have a default font because no Parent GUI has been passed.
; we need a default font, especially for the header (for the +2 and Bold)
$aGUIFont = _GUIUtils_GetFont($hGUI)
EndIf
$iColor = _objGet($oJSON, "bkColor", Null)
If $iColor <> Null Then GUISetBkColor($iColor, $hGUI)
$iColor = _objGet($oJSON, "defColor", Null)
If $iColor <> Null Then GUICtrlSetDefColor($iColor, $hGUI)
$iColor = _objGet($oJSON, "defBkColor", Null)
If $iColor <> Null Then GUICtrlSetDefBkColor($iColor, $hGUI)
; ---------------------------------------------------------------
; build controls
Local $oControls = _objCreate(), $oOptions = _objCreate(), $iCtrlID, $iLabelID
_objSet($oForm, "controls", $oControls)
_objSet($oForm, "options", $oOptions)
Local $aInputs[0]
Local $iMargin = _objGet($oJSON, "margin", 8)
Local $iNextX = $iMargin, $iNextY = $iMargin
Local $iMaxX = 0, $iMaxY = 0 ; will be used to calculate GUI size
For $i = 0 To UBound($aJSONControls) - 1
Switch _objGet($aJSONControls[$i], "type")
Case "separator"
; create separator
$iCtrlID = GUICtrlCreateLabel("", _
$iNextX, $iNextY, _
$iLabelsWidth + $iMargin + _objGet($oJSON, "inputsWidth"), 1, _
$SS_BLACKRECT _
)
_objSet($oControls, _objGet($aJSONControls[$i], "id"), $iCtrlID)
; advance
$iNextY += 1 + $iMargin
Case "label"
; create label
$iCtrlID = GUICtrlCreateLabel( _
_objGet($aJSONControls[$i], "text"), _
$iNextX, $iNextY, _
$iLabelsWidth + $iMargin + _objGet($oJSON, "inputsWidth"), _objGet($aJSONControls[$i], "height"), _
_objGet($aJSONControls[$i], "style", -1), _objGet($aJSONControls[$i], "exStyle", -1) _
)
_objSet($oControls, _objGet($aJSONControls[$i], "id"), $iCtrlID)
; advance
$iNextY += _objGet($aJSONControls[$i], "height") + $iMargin
Case "check", "checkbox", "radio", "radiobox"
; calculate input height
$aSize = __guiUtils_jsonParser_stringSize( _
_objGet($aJSONControls[$i], "label"), _
__guiUtils_jsonParser_getFont($aJSONControls[$i], $aGUIFont), _
_objGet($oJSON, "inputsWidth") _
)
; add multiline style if text is too long
If StringInStr($aSize[0], @CRLF) And Not BitAND(_objGet($aJSONControls[$i], "style", 0), $BS_MULTILINE) Then
_objSet($aJSONControls[$i], "style", BitOR(_objGet($aJSONControls[$i], "style", 0), $BS_MULTILINE))
EndIf
; create control (special check/radio placement)
If StringInStr(_objGet($aJSONControls[$i], "type"), "check") Then
; if previous control is not a checkbox, start a new group (because if previous control is a list, arrow keys will move between controls. don't know if it's a feature or a bug of WinAPI, but it seems bizarre to me)
If $i > 0 And Not StringInStr(_objGet($aJSONControls[$i - 1], "type"), "check") Then
_objSet($aJSONControls[$i], "style", BitOR(_objGet($aJSONControls[$i], "style", 0), $WS_GROUP))
EndIf
; create checkbox
$iCtrlID = GUICtrlCreateCheckbox($aSize[0], _
$iNextX + $iLabelsWidth + $iMargin, $iNextY, _
_objGet($oJSON, "inputsWidth"), $aSize[3], _
_objGet($aJSONControls[$i], "style", -1), _objGet($aJSONControls[$i], "exStyle", -1) _
)
; advance
$iNextY += $aSize[3]
If $i >= UBound($aJSONControls) - 1 Or Not StringInStr(_objGet($aJSONControls[$i + 1], "type"), "check") Or _objGet($aJSONControls[$i], "space", False) Then $iNextY += $iMargin
Else
; if property group = true, add $WS_GROUP style
; also, if previous control is not a radiobox, add $WS_GROUP style
If (_objGet($aJSONControls[$i], "group", False) And Not BitAND(_objGet($aJSONControls[$i], "style", 0), $WS_GROUP)) Or ($i > 0 And Not StringInStr(_objGet($aJSONControls[$i - 1], "type"), "radio")) Then
_objSet($aJSONControls[$i], "style", BitOR(_objGet($aJSONControls[$i], "style", 0), $WS_GROUP))
EndIf
; create radiobox
$iCtrlID = GUICtrlCreateRadio($aSize[0], _
$iNextX + $iLabelsWidth + $iMargin, $iNextY, _
_objGet($oJSON, "inputsWidth"), $aSize[3], _
_objGet($aJSONControls[$i], "style", 0), _objGet($aJSONControls[$i], "exStyle", -1) _
)
; advance
$iNextY += $aSize[3]
If $i >= UBound($aJSONControls) - 1 Or Not StringInStr(_objGet($aJSONControls[$i + 1], "type"), "radio") Or _objGet($aJSONControls[$i], "space", False) Then $iNextY += $iMargin
EndIf
_objSet($oControls, _objGet($aJSONControls[$i], "id"), $iCtrlID)
; set checked
If _objGet($aJSONControls[$i], "value", False) Then
GUICtrlSetState($iCtrlID, $GUI_CHECKED)
EndIf
; add to inputs list
_ArrayAdd($aInputs, _objGet($aJSONControls[$i], "id"))
Case Else ; means all controls that have a separate label
; create label
$iLabelID = GUICtrlCreateLabel( _
_objGet($aJSONControls[$i], "label"), _
$iNextX, $iNextY + _objGet($oJSON, "inputLabelVerticalPadding"), _
$iLabelsWidth, _objGet($aJSONControls[$i], "labelHeight"), _
_objGet($aJSONControls[$i], "labelStyle", $SS_RIGHT), _objGet($aJSONControls[$i], "labelExStyle", -1) _
)
_objSet($oControls, _objGet($aJSONControls[$i], "id") & "_label", $iLabelID)
; set label font and colors
__guiUtils_jsonParser_controlSetFontAndColors( _
$iLabelID, _
__guiUtils_jsonParser_getFont($aJSONControls[$i], $aGUIFont, "labelFont"), _
_objGet($aJSONControls[$i], "labelColor", Null), _
_objGet($aJSONControls[$i], "labelBkColor", Null) _
)
; create control
Switch _objGet($aJSONControls[$i], "type")
Case "input", "password"
; calculate input height
$iInputHeight = __guiUtils_jsonParser_stringSize("A", __guiUtils_jsonParser_getFont($aJSONControls[$i], $aGUIFont), 0)
$iInputHeight = $iInputHeight[3] + 4
; check if password
$iStyle = _objGet($aJSONControls[$i], "style", $GUI_SS_DEFAULT_INPUT)
If _objGet($aJSONControls[$i], "type") = "password" And Not BitAND($iStyle, $ES_PASSWORD) Then $iStyle += $ES_PASSWORD
; create control
$iCtrlID = GUICtrlCreateInput( _
_objGet($aJSONControls[$i], "value", ""), _
$iNextX + $iLabelsWidth + $iMargin, $iNextY, _
_objGet($oJSON, "inputsWidth"), $iInputHeight, _
$iStyle, _objGet($aJSONControls[$i], "exStyle", -1) _
)
_objSet($oControls, _objGet($aJSONControls[$i], "id"), $iCtrlID)
; placeholder
$sPlaceholder = _objGet($aJSONControls[$i], "placeholder")
If $sPlaceholder Then _GUICtrlEdit_SetCueBanner(GUICtrlGetHandle(-1), $sPlaceholder, True)
Case "edit", "text"
; calculate input height
$iInputHeight = __guiUtils_jsonParser_stringSize( _
StringStripWS(_StringRepeat("Line" & @CRLF, _objGet($aJSONControls[$i], "lines", 3)), 3), _
__guiUtils_jsonParser_getFont($aJSONControls[$i], $aGUIFont), _
0 _
)
$iInputHeight = $iInputHeight[3] + _objGet($aJSONControls[$i], "lines", 3) + (BitAND(_objGet($aJSONControls[$i], "style", $GUI_SS_DEFAULT_EDIT), $WS_HSCROLL) ? _WinAPI_GetSystemMetrics($SM_CYHSCROLL) : 0)
; create edit
$iCtrlID = GUICtrlCreateEdit( _
_objGet($aJSONControls[$i], "value", ""), _
$iNextX + $iLabelsWidth + $iMargin, $iNextY, _
_objGet($oJSON, "inputsWidth"), $iInputHeight, _
_objGet($aJSONControls[$i], "style", BitOR($ES_WANTRETURN, $WS_VSCROLL, $ES_AUTOVSCROLL)), _
_objGet($aJSONControls[$i], "exStyle", -1) _
)
_objSet($oControls, _objGet($aJSONControls[$i], "id"), $iCtrlID)
Case "combo", "combobox"
; calculate input height
$iInputHeight = __guiUtils_jsonParser_stringSize("A", __guiUtils_jsonParser_getFont($aJSONControls[$i], $aGUIFont), 0)
$iInputHeight = $iInputHeight[3] + 4
; set control style
$iStyle = _objGet($aJSONControls[$i], "style", $GUI_SS_DEFAULT_COMBO)
If _objExists($aJSONControls[$i], "editable") Then
If _objGet($aJSONControls[$i], "editable") Then
If Not BitAND($iStyle, $CBS_DROPDOWN) Then $iStyle += $CBS_DROPDOWN
Else
Switch BitAND($iStyle, $CBS_DROPDOWNLIST) ; because $CBS_DROPDOWNLIST (3) = $CBS_DROPDOWN (2) + $CBS_SIMPLE (1)
Case $CBS_DROPDOWN
$iStyle += $CBS_SIMPLE
Case 0
$iStyle += $CBS_DROPDOWNLIST
EndSwitch
EndIf
EndIf
; create control
$iCtrlID = GUICtrlCreateCombo("", _
$iNextX + $iLabelsWidth + $iMargin, $iNextY, _
_objGet($oJSON, "inputsWidth"), $iInputHeight, _
$iStyle, _objGet($aJSONControls[$i], "exStyle", -1) _
)
_objSet($oControls, _objGet($aJSONControls[$i], "id"), $iCtrlID)
; placeholder
$sPlaceholder = _objGet($aJSONControls[$i], "placeholder")
If $sPlaceholder Then _GUICtrlEdit_SetCueBanner(GUICtrlGetHandle(-1), $sPlaceholder, True)
; fill options
$vOptions = __guiUtils_jsonParser_getArray($aJSONControls[$i], "options", Null)
If UBound($vOptions) > 0 Then GUICtrlSetData(-1, "|" & _ArrayToString($vOptions, Opt("GUIDataSeparatorChar")))
; set initial selection
$vValue = _objGet($aJSONControls[$i], "value", Null)
If $vValue <> Null Then
If IsInt($vValue) Then
_GUICtrlComboBox_SetCurSel(GUICtrlGetHandle(-1), Int($vValue))
Else
$vValue = _GUICtrlComboBox_FindStringExact(GUICtrlGetHandle(-1), String($vValue))
If $vValue >= 0 Then
_GUICtrlComboBox_SetCurSel(GUICtrlGetHandle(-1), Int($vValue))
Else
_GUICtrlComboBox_SetEditText(GUICtrlGetHandle(-1), String($vValue))
EndIf
EndIf
EndIf
; set extendedUI
_GUICtrlComboBox_SetExtendedUI(GUICtrlGetHandle(-1), _objGet($aJSONControls[$i], "extendedUI", True))
Case "list", "listbox"
; get lines count
$vOptions = __guiUtils_jsonParser_getArray($aJSONControls[$i], "options", Null)
$iLines = _objGet($aJSONControls[$i], "lines", UBound($vOptions))
If $iLines <= 0 Then $iLines = 3
If _objGet($aJSONControls[$i], "maxLines", 0) > 0 And $iLines > _objGet($aJSONControls[$i], "maxLines") Then $iLines = _objGet($aJSONControls[$i], "maxLines")
; set control style
$iStyle = _objGet($aJSONControls[$i], "style", BitOR($WS_BORDER,$WS_VSCROLL,$LBS_MULTIPLESEL,$LBS_NOINTEGRALHEIGHT))
If _objExists($aJSONControls[$i], "multisel") Then
If _objGet($aJSONControls[$i], "multisel") Then
If Not BitAND($iStyle, $LBS_MULTIPLESEL) Then $iStyle += $LBS_MULTIPLESEL
Else
If BitAND($iStyle, $LBS_MULTIPLESEL) Then $iStyle -= $LBS_MULTIPLESEL
EndIf
EndIf
; calculate input height
$iInputHeight = __guiUtils_jsonParser_stringSize(StringStripWS(_StringRepeat("Line" & @CRLF, $iLines), 3), __guiUtils_jsonParser_getFont($aJSONControls[$i], $aGUIFont), 0)
$iInputHeight = $iInputHeight[3] + $iLines + (BitAND($iStyle, $WS_HSCROLL) ? _WinAPI_GetSystemMetrics($SM_CYHSCROLL) : 0)
; create control
$iCtrlID = GUICtrlCreateList("", _
$iNextX + $iLabelsWidth + $iMargin, $iNextY, _
_objGet($oJSON, "inputsWidth"), $iInputHeight, _
$iStyle, _objGet($aJSONControls[$i], "exStyle", -1) _
)
_objSet($oControls, _objGet($aJSONControls[$i], "id"), $iCtrlID)
; fill options
;~ $vOptions = __guiUtils_jsonParser_getArray($aJSONControls[$i], "options", Null) ; get $vOptions before calculating control height
If UBound($vOptions) > 0 Then GUICtrlSetData(-1, _ArrayToString($vOptions, Opt("GUIDataSeparatorChar")))
; set initial selection
$aValues = __guiUtils_jsonParser_getArray($aJSONControls[$i], "value", Null)
If UBound($aValues) > 0 Then
If BitAND($iStyle, $LBS_MULTIPLESEL) Then
; multiselect listbox: select all elements
For $j = 0 To UBound($aValues) - 1
If Not IsInt($aValues[$j]) Then $aValues[$j] = _GUICtrlListBox_FindString(GUICtrlGetHandle(-1), String($aValues[$j]), True)
If $aValues[$j] >= 0 Then _GUICtrlListBox_SetSel(GUICtrlGetHandle(-1), $aValues[$j], True)
Next
Else
; singleselect listbox: select only one element
If Not IsInt($aValues[0]) Then $aValues[0] = _GUICtrlListBox_FindString(GUICtrlGetHandle(-1), String($aValues[0]), True)
If $aValues[0] >= 0 Then _GUICtrlListBox_SetCurSel(GUICtrlGetHandle(-1), $aValues[0])
EndIf
EndIf
Case "date", "datepick", "datepicker"
; calculate input height
$iInputHeight = __guiUtils_jsonParser_stringSize("A", __guiUtils_jsonParser_getFont($aJSONControls[$i], $aGUIFont), 0)
$iInputHeight = $iInputHeight[3] + 4
; set style
$iStyle = _objGet($aJSONControls[$i], "style", $DTS_SHORTDATEFORMAT)
If _objGet($aJSONControls[$i], "nullable", False) And Not BitAND($iStyle, $DTS_SHOWNONE) Then
$iStyle = BitOR($iStyle, $DTS_SHOWNONE)
EndIf
; create control
$iCtrlID = GUICtrlCreateDate( _
_objGet($aJSONControls[$i], "value"), _
$iNextX + $iLabelsWidth + $iMargin, $iNextY, _
_objGet($oJSON, "inputsWidth"), $iInputHeight, _
$iStyle, _objGet($aJSONControls[$i], "exStyle", -1) _
)
_objSet($oControls, _objGet($aJSONControls[$i], "id"), $iCtrlID)
; store options
If _objExists($aJSONControls[$i], "format") Then
_objSet($oOptions, _objGet($aJSONControls[$i], "id") & "_format", _objGet($aJSONControls[$i], "format"))
EndIf
Case "time", "timepick", "timepicker"
; calculate input height
$iInputHeight = __guiUtils_jsonParser_stringSize("A", __guiUtils_jsonParser_getFont($aJSONControls[$i], $aGUIFont), 0)
$iInputHeight = $iInputHeight[3] + 4
; set style
$iStyle = _objGet($aJSONControls[$i], "style", $DTS_TIMEFORMAT)
If _objGet($aJSONControls[$i], "nullable", False) And Not BitAND($iStyle, $DTS_SHOWNONE) Then
$iStyle = BitOR($iStyle, $DTS_SHOWNONE)
EndIf
; create control
$iCtrlID = GUICtrlCreateDate( _
_objGet($aJSONControls[$i], "value"), _
$iNextX + $iLabelsWidth + $iMargin, $iNextY, _
_objGet($oJSON, "inputsWidth"), $iInputHeight, _
$iStyle, _objGet($aJSONControls[$i], "exStyle", -1) _
)
_objSet($oControls, _objGet($aJSONControls[$i], "id"), $iCtrlID)
; store options
If _objExists($aJSONControls[$i], "format") Then
_objSet($oOptions, _objGet($aJSONControls[$i], "id") & "_format", _objGet($aJSONControls[$i], "format"))
EndIf
EndSwitch
; add to inputs list
_ArrayAdd($aInputs, _objGet($aJSONControls[$i], "id"))
; advance
$iNextY += _Max(_objGet($aJSONControls[$i], "labelHeight"), $iInputHeight) + $iMargin
EndSwitch
; set tooltip
__guiUtils_jsonParser_controlSetTip($iCtrlID, $aJSONControls[$i])
; set control font and colors
__guiUtils_jsonParser_controlSetFontAndColors( _
$iCtrlID, _
__guiUtils_jsonParser_getFont($aJSONControls[$i], $aGUIFont), _
_objGet($aJSONControls[$i], "color", Null), _objGet($aJSONControls[$i], "bkColor", Null) _
)
; check if new column is needed
If _objGet($aJSONControls[$i], "new_col", False) Or (_objGet($aJSONControls[$i], "type") <> "separator" And $iNextY >= _objGet($oJSON, "maxHeight")) Then
$iNextY = $iMargin
$iNextX = $iNextX + $iMargin + $iLabelsWidth + $iMargin + _objGet($oJSON, "inputsWidth") + $iMargin
EndIf
; calculate GUI size
If $iCtrlID > 0 Then
$aPos = ControlGetPos($hGUI, "", $iCtrlID)
$iMaxX = _Max($aPos[0] + $aPos[2], $iMaxX)
$iMaxY = _Max($aPos[1] + $aPos[3], $iMaxY)
$iCtrlID = 0
EndIf
If $iLabelID > 0 Then
$aPos = ControlGetPos($hGUI, "", $iLabelID)
$iMaxX = _Max($aPos[0] + $aPos[2], $iMaxX)
$iMaxY = _Max($aPos[1] + $aPos[3], $iMaxY)
$iLabelID = 0
EndIf
Next
_objSet($oForm, "inputs", $aInputs)
; ---------------------------------------------------------------
; bottom separator
_objSet( _
$oControls, "footerSeparator", _
GUICtrlCreateLabel("", $iMargin, $iMaxY + $iMargin, $iMaxX - $iMargin, 1, $SS_BLACKRECT) _
)
$iMaxY += $iMargin + 1
; submit button (mandatory)
$oSubmitBtn = _objGet($oJSON, "submitBtn", "OK")
If $oSubmitBtn And Not IsObj($oSubmitBtn) Then
$oSubmitBtn = _objCreate()
_objSet($oSubmitBtn, "text", _objGet($oJSON, "submitBtn"))
EndIf
_objSet($oControls, "submitBtn", _
GUICtrlCreateButton( _
_objGet($oSubmitBtn, "text", "OK"), _
$iMaxX - _objGet($oSubmitBtn, "width", 100), $iMaxY + $iMargin, _
_objGet($oSubmitBtn, "width", 100), _objGet($oSubmitBtn, "height", 25), _
_objGet($oSubmitBtn, "style", $BS_DEFPUSHBUTTON), _objGet($oSubmitBtn, "exStyle", -1) _
) _
)
__guiUtils_jsonParser_controlSetTip(-1, $oSubmitBtn)
_objSet($oForm, "submitBtn", "submitBtn")
$iButtonsWidth = _objGet($oSubmitBtn, "width", 100)
; cancel button (optional)
$oCancelBtn = _objGet($oJSON, "cancelBtn", Null)
If $oCancelBtn Or IsObj($oCancelBtn) Then
If Not IsObj($oCancelBtn) Then
$oCancelBtn = _objCreate()
_objSet($oCancelBtn, "text", _objGet($oJSON, "cancelBtn"))
EndIf
_objSet($oControls, "cancelBtn", _
GUICtrlCreateButton( _
_objGet($oCancelBtn, "text", "Cancel"), _
$iMaxX - _objGet($oSubmitBtn, "width", 100) - $iMargin - _objGet($oCancelBtn, "width", 80), $iMaxY + $iMargin, _
_objGet($oCancelBtn, "width", 80), _objGet($oCancelBtn, "height", 25), _
_objGet($oCancelBtn, "style", -1), _objGet($oCancelBtn, "exStyle", -1) _
) _
)
__guiUtils_jsonParser_controlSetTip(-1, $oCancelBtn)
_objSet($oForm, "cancelBtn", "cancelBtn")
$iButtonsWidth += _objGet($oCancelBtn, "width", 80) + $iMargin
EndIf
$iMaxY += _objGet($oSubmitBtn, "height", 25) + $iMargin
; header
$oHeader = _objGet($oJSON, "header", Null)
If $oHeader And Not IsObj($oHeader) Then
$oHeader = _objCreate()
_objSet($oHeader, "text", _objGet($oJSON, "header"))
EndIf
If _objGet($oHeader, "text", "") Then
$aHeaderFont = $aGUIFont
If IsArray($aHeaderFont) Then
$aHeaderFont[0] += 2
$aHeaderFont[1] = 800
EndIf
$aHeaderFont = __guiUtils_jsonParser_getFont($oHeader, $aHeaderFont)
$aSize = __guiUtils_jsonParser_stringSize(_objGet($oHeader, "text"), $aHeaderFont, $iMaxX)
$iHeaderLabel = GUICtrlCreateLabel($aSize[0], _
$iMargin, $iMargin, $iMaxX, $aSize[3], _
_objGet($oHeader, "style", $SS_CENTER), _objGet($oHeader, "exStyle", -1) _
)
__guiUtils_jsonParser_controlSetTip(-1, $oHeader)
$iHeaderSeparator = GUICtrlCreateLabel("", $iMargin, $iMargin + $aSize[3] + $iMargin, $iMaxX - $iMargin, 1, $SS_BLACKRECT)
__guiUtils_jsonParser_controlSetFontAndColors($iHeaderLabel, $aHeaderFont, _objGet($oHeader, "color", Null), _objGet($oHeader, "bkColor", Null))
; move all controls to make place for the header and its separator
For $sKey In _objKeys($oControls)
$aPos = ControlGetPos($hGUI, "", _objGet($oControls, $sKey))
GUICtrlSetPos(_objGet($oControls, $sKey), $aPos[0], $aPos[1] + $aSize[3] + $iMargin + 1 + $iMargin)
Next
$iMaxY += $aSize[3] + $iMargin + 1 + $iMargin
; add header controls to form (AFTER moving other controls)
_objSet($oControls, "headerLabel", $iHeaderLabel)
_objSet($oControls, "headerSeparator", $iHeaderSeparator)
EndIf
; ---------------------------------------------------------------
; resize GUI
Local $tRect = _WinAPI_CreateRect(0, 0, $iMaxX + $iMargin, $iMaxY + $iMargin)
_WinAPI_AdjustWindowRectEx($tRect, _objGet($oJSON, "style", $GUI_SS_DEFAULT_GUI), _objGet($oJSON, "exStyle", 0))
$tRect.Right = Abs($tRect.Left) + Abs($tRect.Right)
$tRect.Bottom = Abs($tRect.Top) + Abs($tRect.Bottom)
WinMove($hGUI, "", (@DesktopWidth / 2) - ($tRect.Right / 2), (@DesktopHeight / 2) - ($tRect.Bottom / 2), $tRect.Right, $tRect.Bottom)
; ---------------------------------------------------------------
; show GUI if WS_VISIBLE is set
If BitAND(_objGet($oJSON, "style", 0), $WS_VISIBLE) Then GUISetState(@SW_SHOW, $hGUI)
; store/set initial focused control name
; used by _GUIUtils_InputDialog
If _objExists($oJSON, "focus") Then
ControlFocus($hGUI, "", _objGet($oControls, _objGet($oJSON, "focus")))
_objSet($oForm, "initialFocus", _objGet($oJSON, "focus"))
EndIf
; store oForm object and return
_objSet($__gGuiUtils_oForms, _GUIUtils_HWnd($oForm), $oForm, True)
Return $oForm
EndFunc
# =================================================================================================
#EndRegion
# =================================================================================================
# =================================================================================================
#Region - InputBox behaviour
# =================================================================================================
; #FUNCTION# ====================================================================================================================
; Name ..........: _GUIUtils_InputDialog
; Description ...: Takes a KODA/JSON created GUI, defined by it's representing object $oForm, and displays it as a modal input
; dialog box.
; Syntax ........: _GUIUtils_InputDialog($oForm[, $oInitialData = Null[, $fnOnSubmit = Null[, $fnOnInit = Null[,
; $fnOnChange = Null[, $vUserData = Null]]]]])
; Parameters ....: $oForm - GUI object.
; $oInitialData - [optional] initial inputs data ({inputName: inputData, ...}). Default is Null.
; $fnOnSubmit - [optional] function called when user press dialog's OK button. Default is Null.
; $fnOnInit - [optional] function called just before showing the input, when initial data is filled.
; Default is Null.
; $fnOnChange - [optional] function called when the data of some input is changed. Default is Null.
; $vUserData - [optional] user data that is passed to callback functions. Default is Null.
; Return values .: Object containing inputs data ({inputName: inputData, ...})
; Or Null and @error = 1 if dialog canceled
; Author ........: matwachich
; Remarks .......:
; - About inputs:
; -------------
; by default, all supported inputs will be writen/read by the function. Supported inputs are:
; input, edit, combobox, listbox, checkbox, radio, dateTimePicker.
; Or, if you want to write/read only a subset of input controls, you can specify them by using
; _GUIUtils_SetInputs($oForm, inputControlNames)
;
; - About validation and cancelling buttons:
; ----------------------------------------
; by default, if the GUI contains a DEFPUSHBUTTON it will be used as validation button.
; If it doesn't contain any DEFBUSHBUTTON, you MUST define the validation button you want to use by calling
; _GUIUtils_SetButtons().
; You can also use _GUIUtils_SetButtons() to define a cancel button control, but it is not mandatory.
;
; - About initial dialog data:
; --------------------------
; You can set initial data for input controls by passing $oInitialData an object: {controlName: data...}.
; See _GUIUtils_WriteItems remarks about data format of each control type.
; - You can also set the initially focused input control by specifying "controlName:focus" = True in the
; initial data object.
;
; - About callback functions:
; -------------------------
; This function accepts 3 callback functions, none of them is mendatory.
; All of them takes 3 parameters: $oForm, $vData, $vUserData. $oForm and $vUserData dont need further
; explanations. $vData is different for each callback.
; - $fnOnSubmit:
; if set, this callback is called when Validation Button is pressed.
; $vData is an object containing all inputs values ({inputName: inputData, ...}).
; you can check the data for validity and completeness in this function, then return:
; (> 0) to validate them (the dialog will be closed and the data returned)
; (< 0) to close dialog, but returning Null and @error = 1
; (= 0) to do nothing (keep dialog open)
; - $fnOnInit:
; if set, this callback is called after $oInitialData is filled into the dialog inputs, and just before
; the dialog is shown.
; $vData is an object containing all inputs values ({inputName: inputData, ...}).
; - $fnOnChange:
; if set, called every time the content/data of an input control gets changed.
; $vData is the inputName of the modified control
; =================================================================================================
Func _GUIUtils_InputDialog($oForm, $oInitialData = Null, $fnOnSubmit = Null, $fnOnInit = Null, $fnOnChange = Null, $vUserData = Null)
If IsHWnd($oForm) Then $oForm = _objGet($__gGuiUtils_oForms, $oForm, Null)
If Not IsObj($oForm) Then Return SetError(1, 0, Null)
; get input control names as an array
Local $aInputCtrlNames = _objGet($oForm, "inputs", Null)
If $aInputCtrlNames = Null Then
$aInputCtrlNames = __guiUtils_getSupportedInputsList($oForm)
; store for further use
_objSet($oForm, "inputs", $aInputCtrlNames)
Else
If Not IsArray($aInputCtrlNames) Then
; convert string to array, and store it in this form (optimisation)
$aInputCtrlNames = StringSplit($aInputCtrlNames, " ,-|" & Opt("GUIDataSeparatorChar"))
_ArrayDelete($aInputCtrlNames, 0)
EndIf
EndIf
If UBound($aInputCtrlNames) <= 0 Then Return SetError(1, 0, Null) ; impossible to make an InputDialog without input controls
; check submitBtn
Local $sSubmitBtnName = _objGet($oForm, "submitBtn", Null)
If $sSubmitBtnName = Null Then
; search for any DEFPUSHBUTTON and set it as submitBtn
Local $aCtrlList = _objKeys(_GUIUtils_CtrlList($oForm)), $iStyle
For $i = 0 To UBound($aCtrlList) - 1
$iStyle = _WinAPI_GetWindowLong(_GUIUtils_HCtrl($oForm, $aCtrlList[$i]), $GWL_STYLE)
If _WinAPI_GetClassName(_GUIUtils_HCtrl($oForm, $aCtrlList[$i])) = "Button" And Not BitAND($iStyle, $BS_CHECKBOX) And BitAND($iStyle, $BS_DEFPUSHBUTTON) Then ; must be a button, must not be a check/radio, and must have DEFPUSHBUTTON style
$sSubmitBtnName = $aCtrlList[$i]
ExitLoop
EndIf
Next
; or return ERROR (cannot handle an input dialog box without submitBtn)
If Not $sSubmitBtnName Then Return SetError(1, 0, Null)
EndIf
; get cancelBtnID
Local $iCancelBtnID = $GUI_EVENT_CLOSE
If _objExists($oForm, "cancelBtn") Then
$iCancelBtnID = _GUIUtils_CtrlID($oForm, _objGet($oForm, "cancelBtn"))
EndIf
; set subclass (to catch modifications and send them to caller)
Local $pfnSubclassProc = DllCallbackRegister("__guiUtils_inputDialog_subClassProc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr")
_WinAPI_SetWindowSubclass(_GUIUtils_HWnd($oForm), DllCallbackGetPtr($pfnSubclassProc), 1000)
; create a dummy control that will be used to notify about controls content/data change
Local $hPreviousGUI = GUISwitch(_GUIUtils_HWnd($oForm))
If Not _objExists($oForm, "###___onChangeDummy") Then _objSet($oForm, "###___onChangeDummy", GUICtrlCreateDummy())
; set controls initial data if provided (don't use _GUIUtils_WriteInputs because it doesn't support focus control)
Local $sInitialFocus = ""
If IsObj($oInitialData) Then
For $i = 0 To UBound($aInputCtrlNames) - 1
__guiUtils_inputDialog_controlSet( _
$oForm, $aInputCtrlNames[$i], _
_objGet($oInitialData, $aInputCtrlNames[$i], Null), _
_objGet($oInitialData, $aInputCtrlNames[$i] & ":options", Null) _
)
If _objGet($oInitialData, $aInputCtrlNames[$i] & ":focus", False) Then $sInitialFocus = $aInputCtrlNames[$i]
Next
EndIf
; always save initially focused control, and restore it after
Local $focusCtrl = ControlGetFocus(_GUIUtils_HWnd($oForm))
; then, set focus
If $sInitialFocus Then
; either to the control provided in $oInitialData
ControlFocus(_GUIUtils_HWnd($oForm), "", _GUIUtils_CtrlID($oForm, $sInitialFocus))
Else
; or to the focused control when the Form was created, or lastly, to the first input control
ControlFocus(_GUIUtils_HWnd($oForm), "", _GUIUtils_CtrlID($oForm, _objGet($oForm, "initialFocus", $aInputCtrlNames[0])))
EndIf
; call onInit
If IsFunc($fnOnInit) Then
$fnOnInit($oForm, _GUIUtils_ReadInputs($oForm), $vUserData)
EndIf
; show and activate window
$__gGuiUtils_inputDialog_oCurrentForm = $oForm
GUISetState(@SW_SHOW, _GUIUtils_HWnd($oForm))
WinActivate(_GUIUtils_HWnd($oForm))
; set close on Escape
$iOldCloseOnEscValue = Opt("GUICloseOnEsc", 1)
; enter main loop
Local $aMsg, $oRead = _objCreate(), $iError = 0
While 1
$aMsg = GUIGetMsg(1)
If $aMsg[1] = _GUIUtils_HWnd($oForm) Then
Switch $aMsg[0]
Case $GUI_EVENT_CLOSE, $iCancelBtnID
; dialog canceled
$oRead = Null
$iError = 1
ExitLoop
Case _GUIUtils_CtrlID($oForm, $sSubmitBtnName)
; read values
_objEmpty($oRead)
For $i = 0 To UBound($aInputCtrlNames) - 1
_objSet($oRead, $aInputCtrlNames[$i], __guiUtils_inputDialog_controlGet($oForm, $aInputCtrlNames[$i]))
Next
; call validation function if needed, or exitloop directly
If Not IsFunc($fnOnSubmit) Then
ExitLoop
Else
Local $iRet = $fnOnSubmit($oForm, $oRead, $vUserData)
Select
Case $iRet > 0 ; return success
ExitLoop
Case $iRet < 0 ; return error
$oRead = Null
$iError = 1
ExitLoop
; else (0), just continu looping (do nothing)