PB6.20 - WebViewGadget - WaitWindowEvent() can not be called from a 'binded' event callback?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

PB6.20 - WebViewGadget - WaitWindowEvent() can not be called from a 'binded' event callback?

Post by Kukulkan »

Hi. How do I work around this limitation to prevent this error message?

Test code:

Code: Select all

Html$ =  ~"<button id=\"displayInfo\">Display Info</button>\n"+
         ~"<script>\n"+
         ~"  const displayInfoElement=document.getElementById(\"displayInfo\");\n"+
         ~"  document.addEventListener(\"DOMContentLoaded\", () => {\n"+
         ~"    displayInfoElement.addEventListener(\"click\", () => {\n"+
         ~"      window.displayInfo(1000, 2000).then(result => {\n"+
         ~"        displayInfoElement.textContent = result.sum;\n"+
         ~"      });\n"+
         ~"    });\n"+
         ~"  });\n"+
         ~"</script>";

Procedure.s showInputRequester(question.s, preFill.s = "", isPasswort.b = #False)
  Protected width.i = 350, height.i = 150, gap.i = 5
  Protected result.s = ""
  
  Protected win.i = OpenWindow(#PB_Any, 0, 0, width.i, height.i, "Question", #PB_Window_Tool | #PB_Window_ScreenCentered)
  AddKeyboardShortcut(win.i, #PB_Shortcut_Return, 10)
  AddKeyboardShortcut(win.i, #PB_Shortcut_Escape, 99)
  
  Protected flag.i = 0
  If isPasswort.b = #True
    flag.i = #PB_String_Password
  EndIf
  Protected cGad.i = TextGadget(#PB_Any, gap.i, gap.i, width - gap.i*2, height - 50 - gap.i*2, question.s)
  Protected sGad.i = StringGadget(#PB_Any, gap.i, height - 50, width - gap.i*2, 20, preFill.s, flag.i)
  Protected bGad.i = ButtonGadget(#PB_Any, width / 2 - 20, height - 25, 40, 20, "OK")
  
  SetActiveGadget(sGad.i)
  
  Protected quit.i = #False
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        quit.i = #True
      Case #PB_Event_Menu
        Select EventMenu()
          Case 10
            result.s = GetGadgetText(sGad.i)
            quit.i = #True
          Case 99
            quit.i = #True
        EndSelect
      Case #PB_Event_Gadget
        Select EventGadget()
          Case (bGad.i)
            result = GetGadgetText(sGad.i)
            quit.i = #True
        EndSelect
    EndSelect
  Until quit.i
  RemoveKeyboardShortcut(win.i, #PB_Shortcut_All)
  CloseWindow(win.i)
  ProcedureReturn result.s
EndProcedure
  
Procedure IncrementJS(JsonParameters$)
  Dim Parameters(0)
  ParseJSON(0, JsonParameters$)
  ExtractJSONArray(JSONValue(0), Parameters())

  ; call some value from the user for processing
  Protected test.s = showInputRequester("Please enter a value")
  
  ProcedureReturn UTF8(~"{ \"sum\": "+Str(Parameters(0) + Parameters(1))+ "}")
EndProcedure


OpenWindow(0, 100, 100, 400, 400, "Hello", #PB_Window_SystemMenu)
WebViewGadget(0, 0, 0, 400, 400)
SetGadgetItemText(0, #PB_WebView_HtmlCode, Html$)
BindWebViewCallback(0, "displayInfo", @IncrementJS())
Repeat 
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
Error:

Code: Select all

[11:51:31] [ERROR] Line: 33
[11:51:31] [ERROR] WaitWindowEvent(): WindowEvent() and WaitWindowEvent() can not be called from a 'binded' event callback.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: PB6.20 - WebViewGadget - WaitWindowEvent() can not be called from a 'binded' event callback?

Post by Justin »

You have to use a custom event, maybe something like this:

Code: Select all

EnableExplicit

Structure EVENT_DATA
	json.s
EndStructure

Enumeration #PB_Event_FirstCustomValue
	#WEBVIEW_EVENT
EndEnumeration

Declare IncrementJS_Event()


Define.s Html$
Html$ =  ~"<button id=\"displayInfo\">Display Info</button>\n"+
         ~"<script>\n"+
         ~"  const displayInfoElement=document.getElementById(\"displayInfo\");\n"+
         ~"  document.addEventListener(\"DOMContentLoaded\", () => {\n"+
         ~"    displayInfoElement.addEventListener(\"click\", () => {\n"+
         ~"      window.displayInfo(1000, 2000).then(result => {\n"+
         ~"        displayInfoElement.textContent = result.sum;\n"+
         ~"      });\n"+
         ~"    });\n"+
         ~"  });\n"+
         ~"</script>";

Procedure.s showInputRequester(question.s, preFill.s = "", isPasswort.b = #False)
  Protected width.i = 350, height.i = 150, gap.i = 5
  Protected result.s = ""
  
  OpenWindow(1, 0, 0, width.i, height.i, "Question", #PB_Window_Tool | #PB_Window_ScreenCentered)
  AddKeyboardShortcut(1, #PB_Shortcut_Return, 10)
  AddKeyboardShortcut(1, #PB_Shortcut_Escape, 99)
  
  Protected flag.i = 0
  If isPasswort.b = #True
    flag.i = #PB_String_Password
  EndIf
  Protected cGad.i = TextGadget(#PB_Any, gap.i, gap.i, width - gap.i*2, height - 50 - gap.i*2, question.s)
  Protected sGad.i = StringGadget(#PB_Any, gap.i, height - 50, width - gap.i*2, 20, preFill.s, flag.i)
  Protected bGad.i = ButtonGadget(#PB_Any, width / 2 - 20, height - 25, 40, 20, "OK")
  
  SetActiveGadget(sGad.i)
  
  Protected quit.i = #False
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        quit.i = #True
      	
      Case #PB_Event_Menu
        Select EventMenu()
          Case 10
            result.s = GetGadgetText(sGad.i)
            quit.i = #True
          Case 99
            quit.i = #True
        EndSelect
      Case #PB_Event_Gadget
        Select EventGadget()
          Case (bGad.i)
            result = GetGadgetText(sGad.i)
            quit.i = #True
        EndSelect
    EndSelect
  Until quit.i
  RemoveKeyboardShortcut(1, #PB_Shortcut_All)
  CloseWindow(1)
  ProcedureReturn result.s
EndProcedure
  
Procedure IncrementJS(JsonParameters$)
	Protected.EVENT_DATA *ed = AllocateStructure(EVENT_DATA)
	
	*ed\json = JsonParameters$

	PostEvent(#WEBVIEW_EVENT, 0, 0, 0, *ed)
EndProcedure

Procedure IncrementJS_Event()
	Protected.EVENT_DATA *ed = EventData()
	
  Dim Parameters(0)
  ParseJSON(0, *ed\json)
  ExtractJSONArray(JSONValue(0), Parameters())

  ; call some value from the user for processing
  Protected test.s = showInputRequester("Please enter a value")
  
  FreeStructure(*ed)
  
  ProcedureReturn UTF8(~"{ \"sum\": "+Str(Parameters(0) + Parameters(1))+ "}")
EndProcedure


Define.l event

OpenWindow(0, 100, 100, 400, 400, "Hello", #PB_Window_SystemMenu)
WebViewGadget(0, 0, 0, 400, 400)
SetGadgetItemText(0, #PB_WebView_HtmlCode, Html$)
BindWebViewCallback(0, "displayInfo", @IncrementJS())
Repeat 
  Event = WaitWindowEvent()
  Select event
		Case #WEBVIEW_EVENT
      Debug PeekS(IncrementJS_Event(), -1, #PB_UTF8)
   EndSelect
Until Event = #PB_Event_CloseWindow
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: PB6.20 - WebViewGadget - WaitWindowEvent() can not be called from a 'binded' event callback?

Post by Kukulkan »

Thanks, I will try this!
Post Reply