forked from fantaisie-software/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebView.pb
314 lines (221 loc) · 10.4 KB
/
WebView.pb
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
; --------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
; --------------------------------------------------------------------------------------------
Structure WebViewBaseParameter
command.i
EndStructure
Structure WebViewInitParameter Extends WebViewBaseParameter
filename.s
sourcePath.s
List includes.s()
EndStructure
Structure WebViewErrorParameter Extends WebViewBaseParameter
lineId.i
text.s
EndStructure
Structure WebViewWarningParameter Extends WebViewBaseParameter
lineId.i
text.s
EndStructure
Structure WebViewDebugParameter Extends WebViewBaseParameter
text.s
EndStructure
Structure WebViewControlDebugOutputParameter Extends WebViewBaseParameter
value.i
filename.s
EndStructure
; Settings here are also in Global variables because they are needed elsewhere
Procedure WebView_Callback(JsonParameters$)
Debug JsonParameters$
; The callback always returns an array of parameters. In our case, we always have only one parameter, with a specific object
;
ParseJSON(0, JsonParameters$)
; Get the first element of the parameter array
ParameterJSON = GetJSONElement(JSONValue(0), 0)
; Get the base parameter, common to all commands
ExtractJSONStructure(ParameterJSON, @BaseParameter.WebViewBaseParameter, WebViewBaseParameter)
Debug BaseParameter\command
; Update the debugger structure
*WebViewDebugger\Command\Command = BaseParameter\command
*WebViewDebugger\Command\TimeStamp = Date()
Select BaseParameter\command
Case #COMMAND_Init ; Init
; Map the JSON to a PB structure
;
ExtractJSONStructure(ParameterJSON, @InitParameter.WebViewInitParameter, WebViewInitParameter)
Debug "****** Init"
Debug "filename: "+ InitParameter\filename
Debug "sourcePath: "+ InitParameter\sourcePath
Debug "ListSize " + ListSize(InitParameter\Includes())
ForEach InitParameter\Includes()
Debug InitParameter\Includes()
Next
; Map to the internal IDE debugger structure format, so we can reuse most of the code for the debugger
; It's a bit complex as it was though for native PureBasic executable
;
*WebViewDebugger\FileName$ = InitParameter\filename
*WebViewDebugger\NbIncludedFiles = ListSize(InitParameter\includes())
; Calc the needed size. The buffer contains the source path, the main filename and all included files as UTF-8
IncludeLength = Len(InitParameter\sourcePath) + 1
IncludeLength + Len(InitParameter\filename) + 1
ForEach InitParameter\Includes()
IncludeLength + Len(InitParameter\Includes()) + 1 ; Include the null terminated char
Next
*WebViewDebugger\IncludedFiles = ReAllocateMemory(*WebViewDebugger\IncludedFiles, IncludeLength * 4) ; At most the UTF-8 encoding take 4 bytes per char
*Output = *WebViewDebugger\IncludedFiles
; Write the source path as UTF-8 in the buffer
*Output + PokeS(*Output, InitParameter\sourcePath, -1, #PB_UTF8) + 1
; Write the main filename as UTF-8 in the buffer
*Output + PokeS(*Output, InitParameter\filename, -1, #PB_UTF8) + 1
; Write the includes as UTF-8 in the buffer
ForEach InitParameter\Includes()
*Output + PokeS(*Output, InitParameter\Includes(), -1, #PB_UTF8) + 1
Next
; Close any previous window before launching a new one
;
If IsWindow(*WebViewDebugger\Windows[#DEBUGGER_WINDOW_Debug])
CloseWindow(*WebViewDebugger\Windows[#DEBUGGER_WINDOW_Debug])
EndIf
; The output window is always created
CreateDebugWindow(*WebViewDebugger)
Case #COMMAND_Error
; Map the JSON To a PB Structure
;
ExtractJSONStructure(ParameterJSON, @ErrorParameter.WebViewErrorParameter, WebViewErrorParameter)
Debug "****** Error"
; Map to the internal IDE debugger structure format, so we can reuse most of the code for the debugger
;
*WebViewDebugger\Command\Value1 = ErrorParameter\lineId
*WebViewDebugger\CommandData = UTF8(ErrorParameter\text)
DebuggerCallback(*WebViewDebugger)
Case #COMMAND_Warning
; Map the JSON To a PB Structure
;
ExtractJSONStructure(ParameterJSON, @WarningParameter.WebViewWarningParameter, WebViewWarningParameter)
Debug "****** Warning"
; Map to the internal IDE debugger structure format, so we can reuse most of the code for the debugger
;
*WebViewDebugger\Command\Value1 = WarningParameter\lineId
*WebViewDebugger\CommandData = UTF8(WarningParameter\text)
DebuggerCallback(*WebViewDebugger)
Case #COMMAND_Debug
; Map the JSON To a PB Structure
;
ExtractJSONStructure(ParameterJSON, @DebugParameter.WebViewDebugParameter, WebViewDebugParameter)
Debug "****** Debug"
Debug DebugParameter\text
*WebViewDebugger\IsDebugMessage = #True
*WebViewDebugger\DebugMessage$ = DebugParameter\text
UpdateDebugOutputWindow(*WebViewDebugger)
; show the debug window if needed.
; Warning it needs to be the very last command of the 'Debug' block or the callback can recurse of HideWindow() and the message will be displayed out of order: https://forums.spiderbasic.com/viewtopic.php?t=2675
; It looks like activating a window process some event and the WebView callback is called again.
;
If *WebViewDebugger\OutputFirstVisible
OpenDebugWindow(*WebViewDebugger, #True)
EndIf
Case #COMMAND_ControlDebugOutput
; Map the JSON To a PB Structure
;
ExtractJSONStructure(ParameterJSON, @ControlDebugOutputParameter.WebViewControlDebugOutputParameter, WebViewControlDebugOutputParameter)
Debug "****** ControlDebugOutput"
Debug ControlDebugOutputParameter\value
*WebViewDebugger\Command\Value1 = ControlDebugOutputParameter\value
; Use a dynamic allocated memory for 'CommandData' as it could be automatically freed
*WebViewDebugger\CommandData = AllocateMemory((Len(ControlDebugOutputParameter\filename)+1)*SizeOf(Character))
PokeS(*WebViewDebugger\CommandData, ControlDebugOutputParameter\filename)
*WebViewDebugger\Command\DataSize = Len(ControlDebugOutputParameter\filename)
DebugOutput_DebuggerEvent(*WebViewDebugger)
EndSelect
ProcedureReturn UTF8("")
EndProcedure
Procedure WebView_CreateFunction(*Entry.ToolsPanelEntry)
StringGadget(#GADGET_WebView_Url, 4, 4, 0, 25, "", #PB_String_ReadOnly)
ButtonImageGadget(#GADGET_WebView_OpenBrowser, 0, 4, 25, 25, ImageID(#IMAGE_WebView_OpenBrowser))
WebViewGadget(#GADGET_WebView_WebView, 0, 33, 0, 0, #PB_WebView_Debug)
BindWebViewCallback(#GADGET_WebView_WebView, "spiderDebug", @WebView_Callback())
WebViewOpen = #True
EndProcedure
Procedure WebView_DestroyFunction(*Entry.ToolsPanelEntry)
WebViewOpen = #False
EndProcedure
Procedure WebView_ResizeHandler(*Entry.ToolsPanelEntry, PanelWidth, PanelHeight)
ResizeGadget(#GADGET_WebView_Url , #PB_Ignore , #PB_Ignore, PanelWidth-35, #PB_Ignore)
ResizeGadget(#GADGET_WebView_OpenBrowser, PanelWidth-27, #PB_Ignore, #PB_Ignore , #PB_Ignore)
ResizeGadget(#GADGET_WebView_WebView, #PB_Ignore, #PB_Ignore, PanelWidth, PanelHeight-GadgetY(#GADGET_WebView_WebView))
EndProcedure
Procedure WebView_EventHandler(*Entry.ToolsPanelEntry, EventGadgetID)
Select EventGadgetID
Case #GADGET_WebView_OpenBrowser
OpenSpiderWebBrowser(GetGadgetText(#GADGET_WebView_Url))
EndSelect
EndProcedure
Procedure WebView_PreferenceStart(*Entry.ToolsPanelEntry)
EndProcedure
Procedure WebView_PreferenceApply(*Entry.ToolsPanelEntry)
EndProcedure
Procedure WebView_PreferenceCreate(*Entry.ToolsPanelEntry)
EndProcedure
Procedure WebView_PreferenceDestroy(*Entry.ToolsPanelEntry)
EndProcedure
Procedure WebView_PreferenceEvents(*Entry.ToolsPanelEntry, EventGadgetID)
EndProcedure
Procedure WebView_PreferenceChanged(*Entry.ToolsPanelEntry, IsConfigOpen)
EndProcedure
Procedure SetWebViewUrl(Url$)
SetGadgetText(#GADGET_WebView_Url, Url$)
SetGadgetText(#GADGET_WebView_WebView, Url$)
ActivateTool("WebView")
SetActiveGadget(#GADGET_WebView_WebView)
EndProcedure
Procedure OpenSpiderWebBrowser(Url$)
If Url$ = ""
ProcedureReturn
EndIf
CompilerIf #CompileWindows
If OptionWebBrowser$
RunProgram(OptionWebBrowser$, Url$, "")
Else
RunProgram(Url$) ; Will launch the default browser
EndIf
CompilerElseIf #CompileLinux
If OptionWebBrowser$
RunProgram(OptionWebBrowser$, Url$, "")
Else
RunProgram("xdg-open", Url$, "") ; Will launch the default browser
EndIf
CompilerElseIf #CompileMac
If OptionWebBrowser$
RunProgram("open", "-a " + OptionWebBrowser$ + " " + Url$, "")
Else
RunProgram("open", Url$, "") ; Will launch the default browser
EndIf
CompilerEndIf
EndProcedure
;- Initialisation code
; This will make this Tool available to the editor
;
WebView_VT.ToolsPanelFunctions
WebView_VT\CreateFunction = @WebView_CreateFunction()
WebView_VT\DestroyFunction = @WebView_DestroyFunction()
WebView_VT\ResizeHandler = @WebView_ResizeHandler()
WebView_VT\EventHandler = @WebView_EventHandler()
WebView_VT\PreferenceStart = @WebView_PreferenceStart()
WebView_VT\PreferenceApply = @WebView_PreferenceApply()
WebView_VT\PreferenceCreate = @WebView_PreferenceCreate()
WebView_VT\PreferenceDestroy = @WebView_PreferenceDestroy()
WebView_VT\PreferenceEvents = @WebView_PreferenceEvents()
WebView_VT\PreferenceChanged = @WebView_PreferenceChanged()
AddElement(AvailablePanelTools())
AvailablePanelTools()\FunctionsVT = @WebView_VT
AvailablePanelTools()\NeedPreferences = 0 ; the one setting is stored in the global section
AvailablePanelTools()\NeedConfiguration = 0
AvailablePanelTools()\PreferencesWidth = 320
AvailablePanelTools()\PreferencesHeight = 80
AvailablePanelTools()\NeedDestroyFunction = 1
AvailablePanelTools()\ToolID$ = "WebView"
AvailablePanelTools()\PanelTitle$ = "WebViewShort"
AvailablePanelTools()\ToolName$ = "WebViewLong"
AvailablePanelTools()\PanelTabOrder = 5