Re: 5 WebGadget functions implemented for Linux and MacOS
Posted: Mon Jul 23, 2018 4:37 pm
How could I have overlooked that GetGadgetItemText() with #PB_Web_HtmlCode, #PB_Web_PageTitle and #PB_Web_SelectedText is already implemented in PureBasic for Mac, especially after having found out that SetGadgetItemText(#WebGadget, #PB_Web_HtmlCode, HTML$) is working fine as you can see in my usage of CompilerDefault in "Case 5" (for MacOS and Windows) in my 2nd example above...wilbert wrote:On MacOS, this works fine.
It's only the documentation that's wrong.

But there still seems to be an error in the MacOS implementation of GetGadgetItemText(#WebGadget, #PB_Web_HtmlCode) when reading a real website. Your example is working fine but try to replace
Code: Select all
WebGadget(0, 10, 10, 580, 240, "")
ButtonGadget(1, 10, 260, 200, 30, "GetGadgetItemText")
SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><title>Test</title><body><p>This is a <i>simple</i> test</body></html>")
Code: Select all
WebGadget(0, 10, 10, 580, 240, "http://www.purebasic.com")
ButtonGadget(1, 10, 260, 200, 30, "GetGadgetItemText")
; SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><title>Test</title><body><p>This is a <i>simple</i> test</body></html>")
I have now modified my 2nd example from above to use CompilerDefault for MacOS and Windows when reading out title and selected text. I have left my 2nd example from above unchanged for those who want to know a possible technique for reading out title and selected text.
Code: Select all
EnableExplicit
#HTML = "<h1>Finally you can use the most important WebGadget functions " +
"cross-platform ;-)</h1>"
#URL = "http://www.purebasic.com"
Define SelectedText.S
Define Title.S
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
CompilerIf Subsystem("gtk2")
ImportC "-lwebkitgtk-1.0"
CompilerElse
ImportC "-lwebkitgtk-3.0"
CompilerEndIf
webkit_web_data_source_get_data(*WebDataSource)
webkit_web_frame_get_data_source(*WebFrame)
webkit_web_view_can_copy_clipboard(*WebView)
webkit_web_view_copy_clipboard(*WebView)
webkit_web_view_get_main_frame(*WebView)
webkit_web_view_get_title(*WebView)
webkit_web_view_has_selection(*WebView)
webkit_web_view_load_string(*WebView, Content.P-UTF8, *MimeType,
*Encoding, *BaseURI)
EndImport
Define *Title
Define WebData.I
Define WebFrame.I
Define *WebPageContent.GString
CompilerCase #PB_OS_MacOS
Define *HTML
Define JavaScriptString.S
CompilerEndSelect
OpenWindow(0, 100, 100, 590, 600, "Cross-platform WebGadget functions")
FrameGadget(0, 10, 10, WindowWidth(0) - 20, 65, "GetGadgetItemText()")
ButtonGadget(1, 20, 30, 160, 25, "#PB_Web_HTMLCode")
ButtonGadget(2, 215, 30, 160, 25, "#PB_Web_PageTitle")
ButtonGadget(3, 410, 30, 160, 25, "#PB_Web_SelectedText")
FrameGadget(4, 10, GadgetY(0) + GadgetHeight(0) + 20, WindowWidth(0) - 20, 65,
"SetGadgetItemText()")
ButtonGadget(5, 220, GadgetY(4) + 20, 150, 25, "#PB_Web_HTMLCode")
WebGadget(6, 10, GadgetY(4) + GadgetHeight(4) + 15, WindowWidth(0) - 20,
WindowHeight(0) - GadgetHeight(0) - 120, #URL)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
Select EventGadget()
Case 1
Debug GetGadgetText(1) + ":"
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
WebFrame = webkit_web_view_get_main_frame(GadgetID(6))
WebData = webkit_web_frame_get_data_source(WebFrame)
*WebPageContent = webkit_web_data_source_get_data(WebData)
Debug PeekS(*WebPageContent\str, -1, #PB_Ascii)
CompilerCase #PB_OS_MacOS
JavaScriptString = "document.documentElement.outerHTML;"
*HTML = CocoaMessage(0, GadgetID(6),
"stringByEvaluatingJavaScriptFromString:$", @JavaScriptString)
Debug PeekS(CocoaMessage(0, *HTML, "UTF8String"), -1, #PB_UTF8)
CompilerCase #PB_OS_Windows
Debug GetGadgetItemText(6, #PB_Web_HtmlCode)
CompilerEndSelect
Debug ""
Case 2
Title = ""
Debug GetGadgetText(2) + ":"
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
*Title = webkit_web_view_get_title(GadgetID(6))
If *Title
Title = PeekS(*Title, -1, #PB_UTF8)
EndIf
CompilerDefault
Title = GetGadgetItemText(6, #PB_Web_PageTitle)
CompilerEndSelect
If Title = ""
Title = "No title found!"
EndIf
Debug Title
Debug ""
Case 3
SelectedText = ""
Debug GetGadgetText(3) + ":"
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
If webkit_web_view_has_selection(GadgetID(6))
If webkit_web_view_can_copy_clipboard(GadgetID(6)) = #False
SelectedText = "The copying of selected text to the " +
"clipboard failed!"
Else
webkit_web_view_copy_clipboard(GadgetID(6))
SelectedText = GetClipboardText()
EndIf
EndIf
CompilerDefault
SelectedText = GetGadgetItemText(6, #PB_Web_SelectedText)
CompilerEndSelect
If SelectedText = ""
SelectedText = "No text is selected!"
EndIf
Debug SelectedText
Debug ""
Case 5
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
webkit_web_view_load_string(GadgetID(6), #HTML, 0, 0, 0)
CompilerDefault
SetGadgetItemText(6, #PB_Web_HtmlCode, #HTML)
CompilerEndSelect
EndSelect
EndSelect
ForEver