-
Notifications
You must be signed in to change notification settings - Fork 7
/
DynamicForm.prg
2629 lines (2052 loc) · 88.4 KB
/
DynamicForm.prg
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
*=======================================================================================================
* Dynamic Form - 1.9.1
*---------------------------------------------------------------------------------------
* By: Matt Slay
*-------------------------------------------------------------------------------------------------------
*--
*-- Web Site: https://github.com/mattslay/DynamicForms
*--
*--
*-- You can automatically download this Component and its updates through "Thor - Check for Updates".
*-- To learn more about Thor and its Tools and Updaters for FoxPro, see:
*-- https://github.com/VFPX/Thor/blob/master/Docs/Thor_one-click_update.md
*--
*-- User Discussions Group: https://groups.google.com/forum/#!forum/foxprodynamicforms
*--
*-- after JOINING the Group above, you can post to the web forum or
*-- email to: [email protected]
*--
*--=======================================================================================
*--
*-- This source code PRG contains the class definitions needed to create Dynamic Forms in your apps,
*-- and is also a ready-to-run sample to show you a rendered form sample.
*--
*-- Just run this PRG to see a Dynamic Form sample from the code below. The sample creates a Modal form
*-- which is bound to a few Private variables and an simple data object (the data object is created in the code
*-- at run time simply to mock a real data object and to prevent the demo from having to ship with a sample dbf.)
*-- You can also easily bind to local cursors via the cAlias property, rather than data objects. Please
*-- see the documentation link below for more details on using Dynamic Forms with cursors, and advanced
*-- uses like creating Modeless forms or working with Business Objects to save the data when binding to
*-- an oDataObject.
*--
*--=======================================================================================
*-- DOCUMENTAION - Visit this link to see FULL DOCUMENTAION pages:
*--
*-- https://github.com/mattslay/DynamicForms
*--
*-----------------------------------------------------------------------------------------
*-- VIDEOS -
*--
*-- Video #1 ? Introduction and Demos (8:15) View here: http://bit.ly/DynamicForms-Video-1
*--
*-- Video #2 ? Exploring the class PRG and code sample (9:09) View here: http://bit.ly/Dynamic-Forms-Video-2
*--
*-----------------------------------------------------------------------------------------
*--
*-- Version History
*--
*-- 1.9.1 2017-08-30 Fixed spelling, capitalization, formatting (no functional changes).
*-- 1.9.0 Production release - August 27, 2017 - 20170827 (Migrated from VFPx/CodePlex to GitHub)
*-- 1.8.2 Beta - September 29, 2014 - 2014-09-29
*-- 1.7.0 Alpha - October 31, 2013 - 2013-10-31
*-- 2012-10-18 Alpha 1.5.0 released.
*-- 2012-10-26 Alpha 1.4.1 released.
*-- 2012-10-23 Alpha 1.4.0 released.
*-- 2012-10-08 Alpha 1.3.0 released.
*-- 2012-09-25 Alpha 1.2.0 released.
*-- 2012-09-18 Alpha 1.0.0 released.
*-- 2012-09-04 Public alpha release 0.9.0 released on VFPx.
*-- 2012-08-31 Dynamic Forms accepted as an official VFPx Project.
*-- 2012-08-24 Project proposal submitted to VFPx admins.
*-- 2012-05-08 Initial concept class created and the first ever Dynamic Form was generated from a few lines of code.
*--
*---------------------------------------------------------------------------------------
*-- Example usage:
Private lnPrice, laOptions[1], lnOption
Private loForm as 'DynamicForm'
Local loObject, lcBodyMarkup
*---- Step 1: Prepare the data...
*-- As noted in the Documentation you can bind the form to table aliases and cursors, private or public variables, or Data Objects.
*-- See https://github.com/mattslay/DynamicForms#binding
*--
*-- In this example, we'll build a Data Object in code so that we do not have to distribute a sample dbf with this project package.
*-- Often, "Data Objects" come from table rows, via the Scatter command, or other object-building techniques. You then pass that
*-- object into this Dynamic Form per this code sample. Watch the video series to see examples of working with cursors.
*--
loObject = CreateObject('Empty')
AddProperty(loObject , 'id', '12345')
AddProperty(loObject , 'first_name', 'Joe')
AddProperty(loObject , 'mid_init', 'N.')
AddProperty(loObject , 'last_name', 'Coderman')
AddProperty(loObject , 'ad_type', 'Banner')
AddProperty(loObject , 'notes', 'This man came here and wrote some codez.')
AddProperty(loObject , 'still_here', .f.)
AddProperty(loObject , 'has_laptop', .t.)
AddProperty(loObject , 'bool_1', .t.)
AddProperty(loObject , 'bool_2', .t.)
AddProperty(loObject , 'bool_3', .t.)
AddProperty(loObject , 'weight', 185)
*-- Step 2: Define the UI layout string (similar to HTML / XAML markup) --------------------------------------------
*-- See https://github.com/mattslay/DynamicForms#markup_syntax
Text to lcBodyMarkup NoShow
.lLabelsAbove = .t. |
id .enabled = .f.
.fontbold = .t.
.label.FontBold = .t. |
ad_type .class = 'combobox'
.RowSource = 'laOptions'
.RowSourceType = 5
.row-increment = 0 |
bool_3 .caption = 'You can specify BOLD captions.'
.FontBold = .t.
.width = 400 |
first_name .set-focus = .t. |
mid_init .row-increment = 0 |
last_name .row-increment = 0 |
notes .class = 'editbox'
.width = 400
.height = 80
.anchor = 10|
lnPrice .label.caption = 'List Price'
.label.alignment = 1 |
weight .row-increment = 0
.label.alignment = 1 |
lnOption .class = 'optiongroup'
.caption = 'Color options.'
.buttoncount = 2
.width = 200
.height = 60
.option1.caption = 'Red with orange stripes'
.option1.autosize = .t.
.option2.caption = 'Purple with black dots'
.option2.autosize = .t. |
.class = 'label' .caption = 'Thank you for trying DynamicForm.'
.autosize = .t.
.render-if = (Day(Date()) > 1)
EndText
*-- Example of a Private variables that can be bound to also
lnPrice = 107.15
lnOption = 2
*-- Array of options/values to display in the ComboBox defined in cMarkup above (note that it's declared Private above ---
Dimension laOptions[3]
laOptions[1] = 'Banner'
laOptions[2] = 'Placard'
laOptions[3] = 'Name Tag'
*-- Step 3. Create an instance of DynamicForm class
*-- See https://github.com/mattslay/DynamicForms#step3
loForm = CreateObject('DynamicForm')
*-- Step 4. Set a few properties on loForm to wire everything up and set rendering options...
*-- See https://github.com/mattslay/DynamicForms#step4
loForm.oDataObject = loObject && Set the data object that the form fields bind to
loForm.Caption = 'Dynamic Forms'
loForm.oRenderEngine.lLabelsAbove = .t. && Generate field labels above each control. Default is .F., meaning "inline with controls", to the left.
*-- Setup Header area (or disable it)-------------
loForm.cHeading = 'Sample Form'
loForm.nHeadingFontSize = 14 && You can set heading label font size as desired
*loForm.cHeaderMarkup = '' && Set to empty to disable automatic Header markup. Or,
&& assign custom markup string to customize the header area. See http://vfpx.codeplex.com/wikipage?title=Dynamic%20Form%20Main%20Form%20Layout
*-- Set the main body area markup ---------
loForm.cBodyMarkup = lcBodyMarkup
* loForm.cFooterMarkup = '' && Set to empty to disable use of automatic Footer markup. Or,
&& assign custom markup string to customize the footer area. See http://vfpx.codeplex.com/wikipage?title=Dynamic%20Form%20Main%20Form%20Layout
*-- Step 5. Call Render method to create the controls in the Form
*-- See https://github.com/mattslay/DynamicForms#step5
llResult = loForm.Render()
loForm.MinHeight = loForm.cntMain.Height
loForm.MinWidth = loForm.cntMain.Width
*-- Step 6. Show the form to the user
*-- See https://github.com/mattslay/DynamicForms#step6
If llResult = .t.
*-- Note. You have a chance here to programmatically change anything on the form or controls
*-- in any way needed before showing the form to the user...
loForm.Show()
*loForm.Show(1, '300,10') && 0 = Modeless, 1 = Modal. See http://vfpx.codeplex.com/wikipage?title=Dynamic%20Forms%20Properties#show
&& You can also pass a 'left,top' pair to position the form at a fixed point.
Else
MessageBox(loForm.oRenderEngine.GetErrorsAsString() , 0, 'Notice.')
*-- If there were any rendering errors (llResult = .f.), then you can read loForm.oRenderEngine.nErrorCount property
*-- and loForm.oRenderEngine.oErrors collection for a detail of each error. Or call loForm.oRenderEngine.GetErrorsAsString().
loForm.Show(1)
Endif
*-- At this point, the user is interacting with the form, and it will eventually be closed when they click
*-- Save, Cancel, or the [X] button. At that time, flow will return here, and we can then read any property
*-- on loForm and loForm.oRenderEngine, and even access the rendered controls.
*-- Step 7. Proceed with program flow based on whether user clicked Save or Cancel/closed the form.
*-- See https://github.com/mattslay/DynamicForms#step7
If Vartype(loForm) = 'O' and Lower(loForm.cReturn) = 'save'
*-- If Save is clicked, the controlsources are already updated with the new values from the UI.
*-- Do whatever local processing you need following the Save click by the user...
Release loForm
Else
*-- Do whatever processing for Close/Cancel user action...
*-- If using the Button Bar or and instance of DF_CancelButton on the form and Cancel was clicked,
*-- and the property loForm.lRestoreDataOnCancel = .t. (default), then the controlsources will already
*-- be restored to their original value by the Form class.
EndIf
*-- After the preceding Save/Cancel processing, we can now Release the loForm object.
*-- Class definitions follow:
*=======================================================================================
#DEFINE CR Chr(13)
#DEFINE CRCR Chr(13)
Define Class DynamicForm as Form
cVersion = '1.9.1'
cVersionFull = '1.9.1 Production Release - August 30, 2017'
Caption = ''
*-- Binding form fields to a cursor/alias...
cAlias = '' && The cursor/alias that your form fields bind to. Make sure this alias is opened and positioned to the correct record.
*-- Binding form fields to a DataObject for data, and optionally setting an oBusinessObject for Saving the data.
oDataObject = .null. && The object which has the data properties that you form fields bind to.
oBusinessObject = .null. && oBusinessObject often has a Save() method to save the values from oDataObject back to its table.
cBusinessObjectSaveMethod = 'Save()' && This method on the oBusinessObject will be called when the Save button is clicked.
cDataObjectRef = 'Thisform.oDataObject'
lRestoreDataOnCancel = .t. && When .T., changes to oDataObject or cAlias will be restored to their original values if form is Cancelled by user.
lClearEventsOnClose = .f. && Only use if you want to call Clear Events when this form is closed.
oRenderEngine = .null. && Will be populated in Thisform.Init() event. You can override with your own instance of a Render Engine after this form in Initlialized.
cHeading = .null. && This text is displayed in the default Header area of the form.
cSaveButtonCaption = .null.
cCancelButtonCaption = .null.
nHeadingFontSize = 14 && The font size for the label in the Header area.
cHeaderMarkup = .null.
cBodyMarkup = .null.
cFooterMarkup = .null.
cPopupFormBodyMarkup = .null.
MinWidth = 400
MinHeight = 250
*-- Consider these as ReadOnly after the form has been Hidden by one of the form Buttons --------
lSaveClicked = .f.
lCancelClicked = .f.
cReturn = ''
cHandle = '' && A unique string used to store a a reference to this form on _screen. This is used in handling Modeless forms.
Width = 10000 && This high values will be set to actual rendered size in the Show() method
Height = 10000 && This high values will be set to actual rendered size in the Show() method
DataSession = 1
Add Object cntMain as Container With ;
Top = 0, ;
Left = 0, ;
Width = 1, ; && Will be resized by RenderEngine to size required to hold all controls
Height = 1, ;
BorderWidth = 0, ;
Anchor = 15, ;
margin_left = 0, ;
margin_right = 0, ;
margin_top = 0, ;
margin_bottom = 0
*---------------------------------------------------------------------------------------
Procedure cSaveButtonCaption_Assign()tcCaption
This.oRenderEngine.cSaveButtonCaption = tcCaption
Endproc
*---------------------------------------------------------------------------------------
Procedure cCancelButtonCaption_Assign(tcCaption)
This.oRenderEngine.cCancelButtonCaption = tcCaption
Endproc
*---------------------------------------------------------------------------------------
Procedure cHeading_assign(tcCaption)
This.oRenderEngine.cHeading = tcCaption
Endproc
*---------------------------------------------------------------------------------------
Procedure nHeadingFontSize_assign(tnFontSize)
This.oRenderEngine.nHeadingFontSize = tnFontSize
Endproc
*---------------------------------------------------------------------------------------
Procedure cHeaderMarkup_assign(tcMarkup)
This.oRenderEngine.cHeaderMarkup = tcMarkup
EndProc
*---------------------------------------------------------------------------------------
Procedure cBodyMarkup_assign(tcMarkup)
This.cBodyMarkup = tcMarkup
This.oRenderEngine.cBodyMarkup = tcMarkup
Endproc
*---------------------------------------------------------------------------------------
Procedure cFooterMarkup_assign(tcMarkup)
This.oRenderEngine.cFooterMarkup = tcMarkup
EndProc
*---------------------------------------------------------------------------------------
Procedure Init()
This.cHandle = 'DF_' + Sys(2015) && Used to keep a ref to modeless forms alive
This.oRenderEngine = CreateObject('DynamicFormRenderEngine')
Endproc
*---------------------------------------------------------------------------------------
Procedure Save
&& Add any code to be called when the Save() button is clicked.
&& You can also set a BindEvent() call to this method to react to the Save button click.
EndProc
*---------------------------------------------------------------------------------------
Procedure Activate
This.Refresh()
Endproc
*---------------------------------------------------------------------------------------
Procedure Destroy
If Vartype(This.oRenderEngine) = 'O'
This.oRenderEngine.Destroy() && This will force objects on RE to get released
EndIf
This.oRenderEngine = .null.
This.oBusinessObject = .null.
This.oDataObject = .null.
Store .null. to (This.cHandle)
RemoveProperty(_screen, This.cHandle)
If This.lClearEventsOnClose
Clear Events
Endif
EndProc
*---------------------------------------------------------------------------------------
Procedure QueryUnload
*-- This Event is triggered when the Form's close button [X] is clicked.
If This.lRestoreDataOnCancel = .t.
This.RestoreData()
Endif
EndProc
*---------------------------------------------------------------------------------------
Procedure Show(tnStyle, toHostForm )
*-- Params:
*-- tnStyle. 1 = Modal (Default), 0 = Modeless
*-- toHostForm. (Optional) The form this was called from. If passed, we will center this form in the center of host form.
Local lnAnchor, lnStyle, lnX, loControl
*-- lnAnchor = This.cntMain.Anchor
*-- This.cntMain.Anchor = 0
*-- This.cntMain.Width = Max(This.MinWidth, This.Width)
*-- This.cntMain.Height = Max(This.MinHeight, This.Height)
*-- This.cntMain.Anchor = lnAnchor
If This.oRenderEngine.lRendered = .f.
This.Render()
EndIf
If Pcount() < 2 and (Version(2) <> 0) && No host passed, and working in dev mode
This.Left = Max(Int(Min(_vfp.Width,1600) - This.Width) / 2, 0)
This.Top = Max(Int((_vfp.Height - This.Height) / 2) - Sysmetric(9) - 0, 0)
Endif
*-- If a reference to the calling form was passed, then center this form in host form
If Vartype(toHostForm) = 'O'
Do Case
Case PemStatus(toHostForm, 'ShowWindow', 5) and toHostForm.ShowWindow = 1
This.Left = Max(Int(toHostForm.Width - This.Width) / 2 + toHostForm.Left, 0)
This.Top = Max(Int(toHostForm.Height - This.Height - Sysmetric(9)) / 2 + toHostForm.Top, 0)
Case PemStatus(toHostForm, 'ShowWindow', 5) and toHostForm.ShowWindow = 2
This.Left = Max(Int(toHostForm.Width - This.Width) / 2, 0)
This.Top = Max(Int(toHostForm.Height - This.Height - Sysmetric(9)) / 2, 0)
Otherwise
This.Left = Max(Int(toHostForm.Width - This.Width) / 2 + toHostForm.Left, 0)
This.Top = Max(Int(toHostForm.Height - This.Height) / 2 + toHostForm.Top, 0)
Endcase
Endif
If Vartype(toHostForm) = 'C'
This.Top = Val(GetWordNum(toHostForm, 2, ','))
This.Left = Val(GetWordNum(toHostForm, 1, ','))
Endif
If Vartype(tnStyle) # 'N' or tnStyle < 0 or tnStyle > 1
lnStyle = 1
Else
lnStyle = tnStyle
Endif
If lnStyle = 0 && Modeless
AddProperty(_screen, This.cHandle, This)
Endif
This.WindowType = lnStyle
DoDefault(lnStyle)
*-- Set Focus handling...
If Type('This.cntMain.DF_oSetFocus') = 'O'
This.cntMain.DF_oSetFocus.SetFocus()
Else && Set focus to first enabled control
For lnX = 1 to This.cntMain.ControlCount
loControl = This.cntMain.Controls(lnX)
If PemStatus(loControl, 'Enabled', 5) and PemStatus(loControl, 'SetFocus', 5) and loControl.Enabled = .t.
loControl.Setfocus()
Exit
Endif
EndFor
Endif
*-- For some reason the Save button will not appear unless the form is resized. Crazy. So, I jiggle the size around and it appears!!!
Thisform.Width = Thisform.Width + 1
Thisform.Width = Thisform.Width - 1
EndProc
*---------------------------------------------------------------------------------------
Procedure Hide
DoDefault()
If This.lClearEventsOnClose
Clear Events
Endif
Endproc
*---------------------------------------------------------------------------------------
Procedure Render(tcBodyMarkup)
Local lcRenderSizeMessage, llReturn, lnAnchor, lnRenderHeight, lnRenderWidth
If Vartype(tcBodyMarkup) = 'C'
This.cBodyMarkup = tcBodyMarkup
EndIf
If This.oRenderEngine.lRendered = .f.
This.SetupRenderEngine()
llReturn = This.oRenderEngine.Render()
EndIf
lnAnchor = This.cntMain.Anchor
This.cntMain.Anchor = 0
*-- Move continer for any margin-top or margin-bottom that was set
This.cntMain.Left = This.cntMain.Left + This.cntMain.margin_left
This.cntMain.Top = This.cntMain.Top + This.cntMain.margin_top
*-- If form it still at its default size, then resize to fit the size of cntMain, which now has all its controls in it.
If This.Width = 10000
With This.cntMain
This.Width = Max(.Width + .Left + .margin_right, This.MinWidth)
Endwith
EndIf
If This.Height = 10000
With This.cntMain
This.Height = Max(.Top + .Height + .margin_bottom , This.MinHeight)
Endwith
EndIf
*-- Make sure container Width and Height fills up the entire width of the form.
If !PemStatus(This.cntMain, 'container_width', 5)
This.cntMain.Width = Thisform.Width - This.cntMain.Left - This.cntMain.margin_right
EndIf
If !PemStatus(This.cntMain, 'container_height', 5)
This.cntMain.Height = Thisform.Height - This.cntMain.Top - This.cntMain.margin_bottom
Endif
lcRenderSizeMessage = ''
lnRenderWidth = This.cntMain.Left + This.cntMain.Width + This.cntMain.margin_right
If lnRenderWidth > This.Width
lcRenderSizeMessage = 'Warning: Rendered control area is wider than form width.' + CRCR + ;
'[' + Transform(lnRenderWidth) + ' vs.' + Transform(This.Width) + ']'
lnAnchor = lnAnchor - 8
Endif
lnRenderHeight = This.cntMain.Top + This.cntMain.Height + This.cntMain.margin_bottom
If lnRenderHeight > This.Height
lcRenderSizeMessage = Iif(!Empty(lcRenderSizeMessage), CRCR + lcRenderSizeMessage, '') + ;
'Rendered control area it taller than form height. '+ CRCR +;
'[' + Transform(lnRenderHeight) + ' vs.' + Transform(This.height) + ']'
lnAnchor = lnAnchor - 4
Endif
If !Empty(lcRenderSizeMessage)
MessageBox(lcRenderSizeMessage, 64, 'Render size warning:')
This.oRenderEngine.AddError(lcRenderSizeMessage, .null.)
EndIf
This.cntMain.Anchor = Iif(lnAnchor > 0, lnAnchor, 0)
Return llReturn
EndProc
*---------------------------------------------------------------------------------------
Procedure RestoreData
If Vartype(This.oRenderEngine) = 'O'
This.oRenderEngine.RestoreData()
Endif
EndProc
*---------------------------------------------------------------------------------------
Procedure SetupRenderEngine
With This.oRenderEngine
.cAlias = This.cAlias
.oBusinessObject = This.oBusinessObject
.oDataObject = This.oDataObject
.cDataObjectRef = This.cDataObjectRef
.cBusinessObjectSaveMethod = This.cBusinessObjectSaveMethod
.oContainer = This.cntMain
.lResizeContainer = .t.
EndWith
EndProc
*---------------------------------------------------------------------------------------
*-- This method allows you to pass in toBusinessObject and toDataObject all at once.
Procedure BindBusinessAndDataObjects(toBusinessObject, toDataObject)
This.oBusinessObject = Evl(toBusinessObject, .null.)
This.oDataObject = Evl(toDataObject, .null.)
Endproc
EndDefine
*=======================================================================================
Define Class DynamicFormRenderEngine as Custom
*-- See website for complete documentation.
*-- https://github.com/mattslay/DynamicForms
cVersion = '1.9.1'
cVersionFull = '1.9.1 Production Release - August 30, 2017'
cAlias = '' && The name of a cursor or alias to which the cMarkup controls are bound
*-- These properties deal with the data object and properties/fields on it
oBusinessObject = .null.
oDataObject = .null. && The Object to which the cMarkup controls are bound.
cDataObjectRef = '' && The reference to the oDataObject to be used in the ControlSource property of each control that gets generated..
&& I.e. 'Thisform.oBusObj.oData'
cBusinessObjectSaveMethod = ''
cSkipFields = ''
*cDisabledFields = '' 2012-09-26 Support for this feature has been removed.
cAttributeNameDelimiterPattern = ':'
cAttributeValueDelimiterPattern = '=>'
cFieldDelimiterPattern = '|' && Caution. Don't use a comma as delimiter. It will likely break things!
oContainer = .null. && The container in which controls will be rendered. Set by the calling form.
*-- These properties are only used when a BusinessObject is configured to handle the Save button click.
lShowSaveErrors = .t. && Determines if an error dialog will appear if the call to the Business Object Save() method returns .f.
cSaveErrorMsg = 'Could not save data in Business Object.'
cSaveErrorCaption = 'Warning...'
cHeading = ''
cSaveButtonCaption = .null.
cCancelButtonCaption = .null.
nHeadingFontSize = 14
cHeaderMarkup = .null. && See GetHeaderMarkup() for default markup string
cBodyMarkup = .null. && The markup/field list for the "body" of the form.
cFooterMarkup = .null. && See GetFooterMarkup() for default markup string
cPopupFormBodyMarkup = .null. && See GetPopupFormBodyMarkupMarkup() for default markup string
*-- These properties control the visual layout and flow of the UI controls
nControlLeft = .null. && See Render method for calculation of default value
nFirstControlTop = .null. && See Render method for calculation of default value
nVerticalSpacing = .null. && See Render method for calculation of default value
nVerticalSpacingNonControlSourceControls = .null.
nHorizontalSpacing = .null. && Only used when rendering on the same row with .row=increment = '0'
nHorizontalLineLeft = 10
nControlHeight = 24 && I.e. the Height of Textboxes
nCheckboxHeight = 24 && Default Height for Checkboxes
nCommandButtonHeight = This.nControlHeight
nHorizontalLabelGap = 8 && The horizontal spacing between the label and the input control (when label are NOT above the inputs)
lLabelsAbove = .f. && Default position if for labels to be inline with the input control, to its left. Set this property to .t. to have the labels placed ABOVE the input control.
lAutoAdjustVerticalPositionAndHeight = .f. && Forces the .Top and .Height of each control to 'snap'? to a grid system based on increments on
&& nControlHeight and nVerticalSpacing. The helps keeps control vertically aligned when form spans two
&& columns or more. When enabling this feature, any .Top and .Height values specified in attributes may
&& be adjusted to �snap� to the grid system at its incremental points.
lResizeContainer = .f. &&Indicates if engine should resize (enlarge) oContainer to fit controls as they are added.
lGenerateEditButtonForEditBoxes = .t. && If the field is form a cursor or table and it is a mem data type
&& DF can render a small command button beside the editbox which can be used
&& to pop-up a larger editbox for the memo field. This pop-up can also be activated
&& by double-clicking in the editbox.
*-- Properties related to the popup editbox form feature
cPopupFormEditboxClass = 'editbox'
nPopupFormEditboxWidth = 500
nPopupFormEditboxHeight = 300
*-- Fields related to columns. See. http.//vfpx.codeplex.com/wikipage?title=Dynamic%20Forms#columns
nColumnWidth = 200
nColumnHeight = 800 && The host container can grow to this height before engine will switch to next column
nTextBoxWidth = 100
nEditBoxWidth = 200
nNumericFieldTextboxWidth = 100
nDateFieldTextboxWidth = 100
nDateTimeFieldTextboxWidth = 150
nCheckBoxWidth = 100
nControlWidth = 100 && For any other controls besides the specific ones above
nCheckBoxAlignment = 0 && 0 = Middle Left (Default in VFP) - Places caption to the right of checkbox.
&& 1 = Middle Right - Places caption to the left of checkbox.
*nWidth = 0 && The _Assign method for this property will set oContianer Width to this value
*nHeight = 0 && The _Assign method for this property will set oContianer Height to this value
*-- Default classes used to create UI controls (You can override these at run time to use your own custom classes.)
cLabelClass = 'DF_Label'
cLabelClassLib = ''
cTextboxClass = 'textbox'
cTextboxClassLib = ''
cEditboxClass = 'DF_MemoFieldEditBox'
cEditboxCLassLib = ''
cCommandButtonClass = 'DF_ResultButton'
cCommandButtonClassLib = ''
cOptionGroupClass = 'optiongroup'
cOptionGroupClassLib = ''
cCheckboxClass = 'DF_Checkbox'
cCheckboxClassLib = ''
cComboboxClass = 'combobox'
cComboboxClassLib = ''
cListboxClass = 'listbox'
cListboxClassLib = ''
cSpinnerClass = 'spinner'
cSpinnerClassLib = ''
cGridClass = 'grid'
cGridClassLib = ''
cImageClass = 'image'
cImageClassLib = ''
cTimerClass = 'timer'
cTimerClassLib = ''
cPageframeGroupClass = 'pageframe'
cPageframeClassLib = ''
cLineClass = 'line'
cLineClassLib = ''
cShapeClass = 'shape'
cShapeClassLib = ''
cContainerClass = 'container'
cContainerClassLib = ''
cClassLib = '' && General classlib where controls can be found, if not specified above.
*---------------------------------------------------------------------------------------
* Control classes based on data types. If specified, will override the default classes.
cCharacterClass = ''
cCharacterClassLib = ''
cNumericClass = ''
cNumericClassLib = ''
cDateClass = ''
cDateClassLib = ''
cDateTimeClass = ''
cDateTimeClassLib = ''
*-- Consider these read only ---
nErrorCount = 0
oErrors = .null.
oRegex = .null.
*=======================================================================================
*-- Private properties used/maintained by this class only!!
*Hidden nFieldCount
*Hidden nColumnCount
nNextControlTop = 0
nColumnCount = 1
nFieldsInCurrentColumn = 1
oFieldList = .null.
nLastControlTop = 0
nLastControlBottom = 0
nLastControlLeft = 0
nLastControlRight = 0
nControlCount = 0
lInHeader = .f.
lInBody = .f.
lInFooter = .f.
lLastControlRendered = .f.
lRendered = .f.
cMarkup = ''
Dimension aBackup[1]
Dimension aColumnWidths[1]
*---------------------------------------------------------------------------------------
Procedure Init()
This.oFieldList = CreateObject('Collection')
This.oErrors = CreateObject('Collection')
EndProc
*---------------------------------------------------------------------------------------
Procedure Destroy
This.oContainer = .null.
This.oBusinessObject = .null.
This.oDataObject = .null.
This.oErrors = .null.
This.oFieldList = .null.
This.oRegex = .null.
Endproc
*---------------------------------------------------------------------------------------
Procedure Render(toContainer)
Local lcCode, lcControlSource, loField
*-- Open table if cAlias points to something not already open
If !Empty(This.cAlias) and !Used(JustStem(This.cAlias))
llReturn = This.OpenTable()
If !lLReturn
Return .f.
Endif
Endif
This.cAlias = JustStem(This.cAlias)
This.oContainer = Iif(Vartype(toContainer) = 'O', toContainer, This.oContainer)
If Vartype(This.oContainer) = 'U'
MessageBox('Must pass container object into Render() method, or set .oContainer property.', 0, 'Warning.')
Return -1
Else
AddProperty(This.oContainer, 'oRenderEngine', This) && Temporary. This reference will be cleared out at the end of this method.
EndIf
This.cBodyMarkup = Nvl(This.cBodyMarkup, This.GetBodyMarkupForAll())
If Vartype(This.oRegex) <> 'O'
This.oRegex = CreateObject('VBScript.RegExp')
This.PrepareRegex()
Endif
This.PreProcessBodyMarkup()
This.BuildMarkup() && Merges Header, Body, and Footer marker, and adds some special formatting to help with rendering.
This.BuildFieldList() && Build a collection of controls to be rendered by parsing the cMarkup built in BuildMarkup().
*-- Set default values for various class properties, if the user has not set any values to them...
This.nControlLeft = Nvl(This.nControlLeft , Iif(This.lLabelsAbove = .t., 20, 120))
This.nFirstControlTop = Nvl(This.nFirstControlTop , Iif(This.lLabelsAbove = .t., 30, 10))
This.nHorizontalSpacing = Nvl(This.nHorizontalSpacing, 15) && Only used when rendering on the same row with .row=increment = '0'
If This.lAutoAdjustVerticalPositionAndHeight = .t.
If This.lLabelsAbove = .t.
This.nVerticalSpacing = Nvl(This.nVerticalSpacing, 50) && Value is distance from the .Top of the last control to the the .Top of the next control
Else
This.nVerticalSpacing = Nvl(This.nVerticalSpacing, 30)
Endif
Else
If This.lLabelsAbove = .t.
This.nVerticalSpacing = Nvl(This.nVerticalSpacing, 30) && Value is distance from the BOTTOM of the last control to the the .Top of the next control
Else
This.nVerticalSpacing = Nvl(This.nVerticalSpacing, 15)
Endif
EndIf
This.nVerticalSpacingNonControlSourceControls = Nvl(This.nVerticalSpacingNonControlSourceControls, 10)
This.nNextControlTop = This.nFirstControlTop
*This.nLastControlTop = This.nFirstControlTop
This.nLastControlRight = This.nControlLeft - This.nHorizontalSpacing
This.aColumnWidths[1] = This.nColumnWidth
This.cSkipFields = ' ' + Strtran(This.cSkipFields, ',', ' ') + ' '
*-- Loop over the FieldList collection to render each control, or execute embedded code...
For Each loField in This.oFieldList FOXOBJECT
lcControlSource = loField.ControlSource
If Left(lcControlSource, 1) + Right(lcControlSource, 1)= '()' && If ControlSource element is wrapped in (), then it's to be executed as a VFP code block, Execute it!!
lcCode = Substr(lcControlSource, 2, Len(lcControlSource) - 2)
Try
&lcCode
Catch
This.AddError('Error executing code block.', loField)
EndTry
Else
If Empty(lcControlSource) or !(' ' + Upper(lcControlSource) + ' ' $ Upper(This.cSkipFields))
This.GenerateControl(loField)
EndIf
Endif
EndFor
This.lRendered = .t. && This indicates that the Render method has been called and has completed.
*-- Remove the reference to this Render Engine from the oContainer
This.oContainer.oRenderEngine = .null.
RemoveProperty(This.oContainer, 'oRenderEngine')
Return (This.nErrorCount = 0)
EndProc
*---------------------------------------------------------------------------------------
*-- If cAlias is specified, but not open, then attempt to open it...
Procedure OpenTable
Local lcAlias, loException
Try
Use (This.cAlias) Again In 0
This.cAlias = JustStem(This.cAlias) && Now that it's open, trim off any path and extension
Catch to loException
This.oErrors.Add(loException)
Return .F.
Endtry
Endproc
*---------------------------------------------------------------------------------------
*-- This method combines the Header, Body, and Footer markup together, and mixes in a
*-- little extra markup between each section that will help the rendering process keep
*-- track of where it is working.
Procedure BuildMarkup
This.cHeaderMarkup = Nvl(This.cHeaderMarkup, This.GetHeaderMarkup())
*-- See PreProcessBodyMarkup() method to see how it is preparied for use here
This.cFooterMarkup = Nvl(This.cFooterMarkup, This.GetFooterMarkup())
Text to This.cMarkup NoShow TextMerge
(This.lInHeader = .t.) |
<<This.cHeaderMarkup>> |
(This.lInHeader = .f.) |
(This.nFirstControlTop = This.nLastControlTop + This.nFirstControlTop) |
(This.nFieldsInCurrentColumn = 1) |
(This.lInBody = .t.) |
<<This.cBodyMarkup>> |
(This.lInBody = .f.) |
(This.nLastControlBottom = This.oContainer.Height) |
(This.lInFooter = .t.) |
<<This.cFooterMarkup>> |
(This.lInFooter = .f.) |
EndText
This.cMarkup = Chrtran(This.cMarkup, Chr(13) + Chr(10), ' ')
EndProc
*---------------------------------------------------------------------------------------
Procedure PropertyMatch(tcField, tcList)
Local llSkip, x
For x = 1 to GetWordCount(tcList, ', ')
If Like(Upper(Alltrim(GetWordNum(tcList, x, ', '))), Upper(tcField))
Return .t.
Endif
Endfor
Return .f.
EndProc
*---------------------------------------------------------------------------------------
Procedure AddControl(tcClass, tcClassLib, tcControlSourceField, tcDataType)
Local lcBaseClass, lcClass, lcClassLib, lcControlName, lcPrefix, llNewObject, loControl
Do Case
Case Lower(tcClass) == 'label'
lcClass = This.cLabelClass
lcClassLib = Evl(This.cLabelClasslib, This.cClassLib)
Case Lower(tcClass) == 'textbox'
lcClass = This.cTextBoxClass
lcClassLib = Evl(This.cTextboxClasslib, This.cClassLib)
*-- These properties, if set, override the default class determined
If Vartype(tcDataType) = 'C'
Do Case
Case tcDataType = 'C'
lcClass = Evl(This.cCharacterClass, lcClass)
lcClassLib = Evl(This.cCharacterClassLib, lcClassLib)
Case tcDataType = 'N'
lcClass = Evl(This.cNumericClass, lcClass)
lcClassLib = Evl(This.cNumericClassLib, lcClassLib)
Case tcDataType = 'D'
lcClass = Evl(This.cDateClass, lcClass)
lcClassLib = Evl(This.cDateTimeClassLib, lcClassLib)
Case tcDataType = 'T'
lcClass = Evl(This.cDateTimeClass, lcClass)
lcClassLib = Evl(This.cDateTimeClassLib, lcClassLib)
Endcase
Endif
Case Lower(tcClass) == 'editbox'
lcClass = This.cEditboxClass
lcClassLib = Evl(This.cEditboxClasslib, This.cClassLib)
Case Lower(tcClass) == 'commandbutton'
lcClass = This.cCommandButtonClass
lcClassLib = Evl(This.cCommandButtonClasslib, This.cClassLib)
Case Lower(tcClass) == 'optiongroup'
lcClass = This.cOptionGroupClass
lcClassLib = Evl(This.cOptionGroupClasslib, This.cClassLib)
Case Lower(tcClass) == 'checkbox'
lcClass = This.cCheckboxClass
lcClassLib = Evl(This.cCheckboxClasslib, This.cClassLib)
Case Lower(tcClass) == 'combobox'
lcClass = This.cComboboxClass
lcClassLib = Evl(This.cComboboxClasslib, This.cClassLib)
Case Lower(tcClass) == 'listbox'
lcClass = This.cListboxClass
lcClassLib = Evl(This.cListboxClasslib, This.cClassLib)
Case Lower(tcClass) == 'spinner'
lcClass = This.cSpinnerClass
lcClassLib = Evl(This.cSpinnerClasslib, This.cClassLib)
Case Lower(tcClass) == 'grid'
lcClass = This.cGridClass
lcClassLib = Evl(This.cGridClasslib, This.cClassLib)
Case Lower(tcClass) == 'image'
lcClass = This.cImageClass
lcClassLib = Evl(This.cImageClasslib, This.cClassLib)
Case Lower(tcClass) == 'timer'
lcClass = This.cTimerClass
lcClassLib = Evl(This.cTimerClasslib, This.cClassLib)
Case Lower(tcClass) == 'pageframe'
lcClass = This.cPageframeClass
lcClassLib = Evl(This.cPageframeClasslib, This.cClassLib)
Case Lower(tcClass) == 'line'
lcClass = This.cLineClass
lcClassLib = Evl(This.cLineClasslib, This.cClassLib)
Case Lower(tcClass) == 'shape'
lcClass = This.cShapeClass
lcClassLib = Evl(This.cShapeClasslib, This.cClassLib)
Case Lower(tcClass) == 'container'
lcClass = This.cContainerClass
lcClassLib = Evl(This.cContainerClasslib, This.cClassLib)
Otherwise
lcClass = tcClass
lcClassLib = Evl(tcClassLib, This.cClassLib)
EndCase
Try
llNewObject = This.oContainer.NewObject(Sys(2015), lcClass, lcClassLib) && Sys(2015) = Random name for object. Will rename below...
This.nControlCount = This.nControlCount + 1
Catch
llNewObject = .f.
Endtry
If llNewObject = .t.