hier mal ein kleines Beispiel, wie man SpiderBasic-Apps in PureBasic hosten kann. Vielleicht kann es der ein oder andere gebrauchen.
Der unten stehende Code demonstriert, wie man aus SpiderBasic heraus über PureBasic Dateien laden und speichern kann.
PureBasic-Code:
Code: Alles auswählen
EnableExplicit
Enumeration
#Window
#WebViewGadget
EndEnumeration
Structure sTransferObject
Status.i
FileName.s
FileContent.s
EndStructure
Procedure SizeWindowEvent()
ResizeGadget(#WebViewGadget, #PB_Ignore, #PB_Ignore, WindowWidth(#Window), WindowHeight(#Window))
EndProcedure
Procedure LoadButtonEventJS(JsonParameters.s)
Protected TransferObject.sTransferObject
Protected FileName.s
Protected FileContent.s
Protected JsonString.s
Protected Json
Protected FF
FileName = OpenFileRequester("Please choose file to load", "", "Text (*.txt;*.bat)|*.txt;*.bat|All files (*.*)|*.*", 0)
If FileName
TransferObject\FileName = FileName
FF = ReadFile(#PB_Any, FileName)
If FF
FileContent = ReadString(FF, #PB_File_IgnoreEOL)
CloseFile(FF)
TransferObject\Status = #True
TransferObject\FileContent = FileContent
Else
TransferObject\Status = #False
EndIf
Else
TransferObject\Status = #False
EndIf
Json = CreateJSON(#PB_Any)
InsertJSONStructure(JSONValue(Json), @TransferObject, sTransferObject)
JsonString = ComposeJSON(Json)
FreeJSON(Json)
ProcedureReturn UTF8(JsonString)
EndProcedure
Procedure SaveButtonEventJS(JsonParameters.s)
Protected FileName.s
Protected FileContent.s
Protected FF, Json
Dim Parameters.s(0)
Json = ParseJSON(#PB_Any, JsonParameters)
ExtractJSONArray(JSONValue(Json), Parameters())
FileContent = Parameters(0)
FileName = SaveFileRequester("Please choose file to save", "", "Text (*.txt)|*.txt;*.bat|PureBasic (*.pb)|*.pb|All files (*.*)|*.*", 0)
If FileName
If FileSize(FileName) > -1
If MessageRequester("The file already exists", "Overwrite?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_No
ProcedureReturn
EndIf
EndIf
FF = CreateFile(#PB_Any, FileName)
If FF
WriteString(FF, FileContent)
CloseFile(FF)
EndIf
EndIf
ProcedureReturn UTF8("ok")
EndProcedure
Procedure PB_LoadFile(JsonParameters.s)
Protected TransferObject.sTransferObject
Protected FileName.s
Protected JsonString.s
Protected Json
Protected FF
Dim Parameters.s(0)
Json = ParseJSON(#PB_Any, JsonParameters)
ExtractJSONArray(JSONValue(Json), Parameters())
FileName = Parameters(0)
FF = ReadFile(#PB_Any, FileName)
If FF
TransferObject\FileContent = ReadString(FF, #PB_File_IgnoreEOL)
CloseFile(FF)
EndIf
Json = CreateJSON(#PB_Any)
InsertJSONStructure(JSONValue(Json), @TransferObject, sTransferObject)
JsonString = ComposeJSON(Json)
FreeJSON(Json)
ProcedureReturn UTF8(JsonString)
EndProcedure
Define URL.s = GetPathPart(ProgramFilename()) + "SB/index.html"
OpenWindow (#Window, 0, 0, 1000, 800,"I'm the PureBasic-Host", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
WebViewGadget(#WebViewGadget, 0, 0, WindowWidth(#Window), WindowHeight(#Window) , #PB_WebView_Debug)
SetGadgetText(#WebViewGadget, URL)
BindEvent(#PB_Event_SizeWindow, @SizeWindowEvent(), #Window)
BindWebViewCallback(#WebViewGadget, "LoadButtonEvent", @LoadButtonEventJS())
BindWebViewCallback(#WebViewGadget, "SaveButtonEvent", @SaveButtonEventJS())
BindWebViewCallback(#WebViewGadget, "PB_LoadFile", @PB_LoadFile())
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Code: Alles auswählen
EnableExplicit
Enumeration
#Window
#LoadButton
#SaveButton
#SaveAsButton
#Editor
EndEnumeration
Procedure.s PB_LoadFile(FileName.s)
Debug "PB_LoadFile()"
Protected FileContent.s
! window.PB_LoadFile(v_filename).then(result => {
! v_filecontent = result.FileContent;
Debug FileContent
! return v_filecontent;
! });
EndProcedure
Procedure LoadButtonEvent()
Debug "LoadButtonEvent()"
Protected Status
Protected FileName.s
Protected FileContent.s
! window.LoadButtonEvent().then(result => {
! v_status = result.Status;
! v_filename = result.FileName;
! v_filecontent = result.FileContent;
If Status = #True
SetGadgetText(#Editor, FileContent)
EndIf
SetActiveGadget(#Editor)
! });
EndProcedure
Procedure SaveButtonEvent()
Debug "SaveButtonEvent()"
Protected FileContent.s = GetGadgetText(#Editor)
! window.SaveButtonEvent(v_filecontent).then(result => {
SetActiveGadget(#Editor)
! });
EndProcedure
! document.body.oncontextmenu=null;
OpenWindow(#Window, #PB_Ignore, #PB_Ignore, 800, 600, "I'm a SpiderBasic-WebApp", #PB_Window_ScreenCentered)
ButtonGadget(#LoadButton, 10, 10, 80, 30, "Load")
ButtonGadget(#SaveButton, 100, 10, 80, 30, "Save")
EditorGadget(#Editor, 10, 50, WindowWidth(#Window) - 20, WindowHeight(#Window) - 60)
BindGadgetEvent(#LoadButton, @LoadButtonEvent())
BindGadgetEvent(#SaveButton, @SaveButtonEvent())
Bei Fragen fragen.
Grüße ... Peter