-
Notifications
You must be signed in to change notification settings - Fork 33
/
modConvertForm.bas
329 lines (290 loc) · 12 KB
/
modConvertForm.bas
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
Attribute VB_Name = "modConvertForm"
Option Explicit
' Convert a given form to the xaml
Private EventStubs As String
Public FormControlArrays As String
Public Function Frm2Xml(ByVal F As String) As String
Dim Sp() As String, L As Variant, I As Long
Dim R As String
Sp = Split(F, vbCrLf)
For Each L In Sp
L = Trim(L)
If L = "" Then GoTo NextLine
If Left(L, 10) = "Attribute " Or Left(L, 8) = "VERSION " Then
ElseIf Left(L, 6) = "Begin " Then
R = R & sSpace(I * SpIndent) & "<item type=""" & SplitWord(L, 2) & """ name=""" & SplitWord(L, 3) & """>" & vbCrLf
I = I + 1
ElseIf L = "End" Then
I = I - 1
R = R & sSpace(I * SpIndent) & "</item>" & vbCrLf
Else
R = R & sSpace(I * SpIndent) & "<prop name=""" & SplitWord(L, 1, "=") & """ value=""" & SplitWord(L, 2, "=", , True) & """ />" & vbCrLf
End If
NextLine:
Next
Frm2Xml = R
End Function
Public Function FormControls(ByVal Src As String, ByVal F As String, Optional ByVal asLocal As Boolean = True) As String
Dim Sp() As String, L As Variant, I As Long
Dim R As String, T As String
Dim Nm As String, Ty As String
Sp = Split(F, vbCrLf)
For Each L In Sp
L = Trim(L)
If L = "" Then GoTo NextLine
If Left(L, 6) = "Begin " Then
Ty = SplitWord(L, 2)
Nm = SplitWord(L, 3)
Select Case Ty
Case "VB.Form"
Case Else
T = Src & ":" & IIf(asLocal, "", Src & ".") & Nm & ":Control:" & Ty
If Right(R, Len(T)) <> T Then R = R & vbCrLf & T
End Select
End If
NextLine:
Next
FormControls = R
End Function
Public Function ConvertFormUi(ByVal F As String, ByVal CodeSection As String) As String
Dim Stck(0 To 100) As String
Dim Sp() As String, L As Variant, J As Long, K As Long, I As Long, Tag As String
Dim M As String
Dim R As String
Dim Prefix As String
Dim Props As Collection, pK As String, pV As String
Sp = Split(F, vbCrLf)
EventStubs = ""
FormControlArrays = ""
For K = LBound(Sp) To UBound(Sp)
L = Trim(Sp(K))
If L = "" Then GoTo NextLine
If Left(L, 10) = "Attribute " Or Left(L, 8) = "VERSION " Then
ElseIf Left(L, 6) = "Begin " Then
Set Props = New Collection
J = 0
Do
J = J + 1
M = Trim(Sp(K + J))
If LMatch(M, "Begin ") Or M = "End" Then Exit Do
If LMatch(M, "BeginProperty ") Then
Prefix = LCase(Prefix & SplitWord(M, 2) & ".")
ElseIf LMatch(M, "EndProperty") Then
Prefix = Left(Prefix, Len(Prefix) - 1)
If Not IsInStr(Prefix, ".") Then
Prefix = ""
Else
Prefix = Left(Prefix, InStrRev(Left(Prefix, Len(Prefix) - 1), "."))
End If
Else
pK = Prefix & LCase(SplitWord(M, 1, "="))
pV = ConvertProperty(SplitWord(M, 2, "=", , True))
On Error Resume Next
Props.Add pV, pK
On Error GoTo 0
End If
Loop While True
K = K + J - 1
R = R & sSpace(I * SpIndent) & StartControl(L, Props, LMatch(M, "End"), CodeSection, Tag) & vbCrLf
I = I + 1
Stck(I) = Tag
ElseIf L = "End" Then
Set Props = Nothing
Tag = Stck(I)
I = I - 1
If Tag <> "" Then
R = R & sSpace(I * SpIndent) & EndControl(Tag) & vbCrLf
End If
End If
NextLine:
Next
ConvertFormUi = R
End Function
Private Function ConvertProperty(ByVal S As String) As String
S = deQuote(S)
S = DeComment(S)
ConvertProperty = S
End Function
Private Function StartControl(ByVal L As String, ByVal Props As Collection, ByVal DoEmpty As Boolean, ByVal Code As String, ByRef TagType As String) As String
Dim cType As String, oName As String, cName As String, cIndex As String
Dim tType As String, tCont As Boolean, tDef As String, Features As String
Dim S As String, N As String, M As String
Dim V As String
N = vbCrLf
TagType = ""
cType = SplitWord(L, 2)
oName = SplitWord(L, 3)
cIndex = cValP(Props, "Index")
ControlData cType, tType, tCont, tDef, Features
If cIndex <> "" Then
If InStr(FormControlArrays, "[" & oName & ",") = 0 Then FormControlArrays = FormControlArrays & "[" & oName & "," & tType & "]"
cName = oName & "_" & cIndex
Else
cName = oName
End If
S = ""
On Error Resume Next
If tType = "Line" Or tType = "Shape" Or tType = "Timer" Then
Exit Function
ElseIf tType = "Window" Then
S = S & M & "<Window x:Class=""" & AssemblyName & ".Forms." & cName & """"
S = S & N & " xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"""
S = S & N & " xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"""
S = S & N & " xmlns:d=""http://schemas.microsoft.com/expression/blend/2008"""
S = S & N & " xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006"""
S = S & N & " xmlns:local=""clr-namespace:" & AssemblyName & ".Forms"""
S = S & N & " xmlns:usercontrols=""clr-namespace:" & AssemblyName & ".UserControls"""
S = S & N & " mc:Ignorable=""d"""
S = S & N & " Title=" & Quote(cValP(Props, "caption"))
S = S & M & " Height=" & Quote(Px(cValP(Props, "clientheight", 0) + 435))
S = S & M & " Width=" & Quote(Px(cValP(Props, "clientwidth", 0) + 435))
S = S & CheckControlEvents("Window", "Form", Code)
S = S & M & ">"
S = S & N & " <Grid"
ElseIf tType = "GroupBox" Then
S = S & "<" & tType
S = S & " x:Name=""" & cName & """"
S = S & " Margin=" & Quote(Px(cValP(Props, "left")) & "," & Px(cValP(Props, "top")) & ",0,0")
S = S & " Width=" & Quote(Px(cValP(Props, "width")))
S = S & " Height=" & Quote(Px(cValP(Props, "height")))
S = S & " VerticalAlignment=""Top"""
S = S & " HorizontalAlignment=""Left"""
S = S & " FontFamily=" & Quote(cValP(Props, "font.name", "Calibri"))
S = S & " FontSize=" & Quote(cValP(Props, "font.size", 10))
S = S & " Header=""" & cValP(Props, "caption") & """"
S = S & "> <Grid Margin=""0,-15,0,0"""
ElseIf tType = "Canvas" Then
S = S & "<" & tType
S = S & " x:Name=""" & cName & """"
S = S & " Margin=" & Quote(Px(cValP(Props, "left")) & "," & Px(cValP(Props, "top")) & ",0,0")
S = S & " Width=" & Quote(Px(cValP(Props, "width")))
S = S & " Height=" & Quote(Px(cValP(Props, "height")))
ElseIf tType = "Image" Then
S = S & "<" & tType
S = S & " x:Name=""" & cName & """"
S = S & " Margin=" & Quote(Px(cValP(Props, "left")) & "," & Px(cValP(Props, "top")) & ",0,0")
S = S & " Width=" & Quote(Px(cValP(Props, "width")))
S = S & " Height=" & Quote(Px(cValP(Props, "height")))
S = S & " VerticalAlignment=" & Quote("Top")
S = S & " HorizontalAlignment=" & Quote("Left")
Else
S = ""
S = S & "<" & tType
S = S & " x:Name=""" & cName & """"
S = S & " Margin=" & Quote(Px(cValP(Props, "left")) & "," & Px(cValP(Props, "top")) & ",0,0")
S = S & " Padding=" & Quote("2,2,2,2")
S = S & " Width=" & Quote(Px(cValP(Props, "width")))
S = S & " Height=" & Quote(Px(cValP(Props, "height")))
S = S & " VerticalAlignment=" & Quote("Top")
S = S & " HorizontalAlignment=" & Quote("Left")
End If
If IsInStr(Features, "Font") Then
S = S & " FontFamily=" & Quote(cValP(Props, "font.name", "Calibri"))
S = S & " FontSize=" & Quote(cValP(Props, "font.size", 10))
If Val(cValP(Props, "font.weight", "400")) > 400 Then S = S & " FontWeight=" & Quote("Bold")
End If
If IsInStr(Features, "Content") Then
S = S & " Content=" & QuoteXML(cValP(Props, "caption") & cValP(Props, "text"))
End If
If IsInStr(Features, "Header") Then
S = S & " Content=" & QuoteXML(cValP(Props, "caption") & cValP(Props, "text"))
End If
V = cValP(Props, "caption") & cValP(Props, "text")
If IsInStr(Features, "Text") And V <> "" Then
S = S & " Text=" & QuoteXML(V)
End If
V = cValP(Props, "ToolTipText")
If IsInStr(Features, "ToolTip") And V <> "" Then
S = S & " ToolTip=" & Quote(V)
End If
S = S & CheckControlEvents(tType, cName, Code)
If DoEmpty Then
S = S & " />"
TagType = ""
Else
S = S & ">"
TagType = tType
End If
StartControl = S
End Function
Public Function CheckControlEvents(ByVal ControlType As String, ByVal ControlName As String, Optional ByVal CodeSection As String = "") As String
Dim Res As String
Dim HasClick As Boolean, HasFocus As Boolean, HasChange As Boolean, IsWindow As Boolean
HasClick = True
HasFocus = Not IsInStr("GroupBox", ControlType)
HasChange = IsInStr("TextBox,ListBox", ControlType)
IsWindow = ControlType = "Window"
Res = ""
Res = Res & CheckEvent("MouseMove", ControlName, ControlType, CodeSection)
If HasFocus Then
Res = Res & CheckEvent("GotFocus", ControlName, ControlType, CodeSection)
Res = Res & CheckEvent("LostFocus", ControlName, ControlType, CodeSection)
Res = Res & CheckEvent("KeyDown", ControlName, ControlType, CodeSection)
Res = Res & CheckEvent("KeyUp", ControlName, ControlType, CodeSection)
End If
If HasClick Then
Res = Res & CheckEvent("Click", ControlName, ControlType, CodeSection)
Res = Res & CheckEvent("DblClick", ControlName, ControlType, CodeSection)
End If
If HasChange Then
Res = Res & CheckEvent("Change", ControlName, ControlType, CodeSection)
End If
If IsWindow Then
Res = Res & CheckEvent("Load", ControlName, ControlType, CodeSection)
Res = Res & CheckEvent("Unload", ControlName, ControlType, CodeSection)
' Res = Res & CheckEvent("QueryUnload", ControlName, ControlType, CodeSection)
End If
CheckControlEvents = Res
End Function
Public Function CheckEvent(ByVal EventName As String, ByVal ControlName As String, ByVal ControlType As String, Optional ByVal CodeSection As String = "") As String
Dim Search As String, Target As String, N As String
Dim L As Long, V As String
N = ControlName & "_" & EventName
Search = " " & N & "("
Target = EventName
Select Case EventName
Case "DblClick": Target = "MouseDoubleClick"
Case "Change":
If ControlType = "TextBox" Then Target = "TextChanged"
Case "Load": Target = "Loaded"
Case "Unload": Target = "Unloaded"
End Select
L = InStr(1, CodeSection, Search, vbTextCompare)
If L > 0 Then
V = Mid(CodeSection, L + 1, Len(N)) ' Get exact capitalization from source....
CheckEvent = " " & Target & "=""" & V & """"
Else
CheckEvent = ""
End If
End Function
Public Function EndControl(ByVal tType As String) As String
Select Case tType
Case "Line", "Shape", "Timer":
EndControl = ""
Case "Window": EndControl = " </Grid>" & vbCrLf & "</Window>"
Case "GroupBox": EndControl = "</Grid> </GroupBox>"
Case Else: EndControl = "</" & tType & ">"
End Select
End Function
Public Function IsEvent(ByVal Str As String) As Boolean
IsEvent = EventStub(Str) <> ""
End Function
Public Function EventStub(ByVal fName As String) As String
Dim S As String, C As String, K As String
C = SplitWord(fName, 1, "_")
K = SplitWord(fName, 2, "_")
Select Case K
Case "Click", "DblClick", "Load", "GotFocus", "LostFocus"
S = "private void " & fName & "(object sender, RoutedEventArgs e) { " & fName & "(); }" & vbCrLf
Case "Change"
S = "private void " & C & "_Change(object sender, System.Windows.Controls.TextChangedEventArgs e) { " & fName & "(); }" & vbCrLf
Case "QueryUnload"
S = "private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { int c = 0, u = 0 ; " & fName & "(out c, ref u); e.Cancel = c != 0; }" & vbCrLf
' V = " long doCancel; long UnloadMode; " & FName & "(ref doCancel, ref UnloadMode);"
Case "Validate", "Unload"
' V = "long doCancel; " & FName & "(ref doCancel);"
Case "KeyDown", "KeyUp", "KeyPress"
Case "MouseMove", "MouseDown", "MouseUp"
End Select
EventStub = S
End Function