Since several years many requests for implementing the missing functions also for Linux and MacOS have been made. In several threads in the English or German forum I have already tried to help out by implementing some of the missing functions for either Linux or MacOS. In order to centralize these efforts I now have programmed two cross-platform examples which demonstrate how to use the 5 most requested functions for the WebGadget in cross-platform code.Documentation for the WebGadget wrote: - SetGadgetItemText(): With #PB_Web_HtmlCode as 'Item' html code can be streamed into the Gadget. (Windows only)
- GetGadgetItemText(): The following constants can be used to get information (Windows only):
#PB_Web_HtmlCode : Get the html code from the gadget.
#PB_Web_PageTitle : Get the current title for the displayed page.
#PB_Web_StatusMessage: Get the current statusbar message.
#PB_Web_SelectedText : Get the currently selected text inside the gadget.
- SetGadgetAttribute(): Set the following attributes (Windows only):
#PB_Web_ScrollX : Set the horizontal scrolling position.
#PB_Web_ScrollY : Set the vertical scrolling position.
#PB_Web_BlockPopups : Block popup windows. #PB_EventType_PopupWindow is fired if this setting is enabled.
#PB_Web_BlockPopupMenu: Block standard the popup menu. #PB_EventType_PopupMenu is fired if this setting is enabled.
#PB_Web_NavigationCallback: Set a callback for monitoring (and disabling) navigation.
The Navigation callback must have the following format:
Procedure NavigationCallback(Gadget, Url$)
;
; Return #True to allow this navigation or #False to deny it.
;
ProcedureReturn #True
EndProcedure
- GetGadgetAttribute(): Get the following attributes (Windows only):
#PB_Web_ScrollX : Get the horizontal scrolling position.
#PB_Web_ScrollY : Get the vertical scrolling position.
#PB_Web_Busy : Returns nonzero if the gadget is busy loading a page.
#PB_Web_Progress : Returns the current (sometimes estimated) progress after a #PB_EventType_DownloadProgress event.
#PB_Web_ProgressMax : Returns the current (sometimes estimated) maximum progress after a #PB_EventType_DownloadProgress event.
#PB_Web_BlockPopups : Get the current popupwindow blocking setting.
#PB_Web_BlockPopupMenu: Get the current popupmenu blocking setting.
#PB_Web_NavigationCallback: Get the current navigation callback (if any).
The following types of events can happen for this gadget:
#PB_EventType_TitleChange : The page title changed (Windows only).
#PB_EventType_StatusChange : The status message changed (Windows only).
#PB_EventType_DownloadStart : A page download started (Windows, OS X).
#PB_EventType_DownloadProgress: Progress info is available with GetGadgetAttribute() (Windows only).
#PB_EventType_DownloadEnd : A page download ended or aborted (Windows, OS X).
#PB_EventType_PopupWindow : A popup window was blocked (Windows only).
#PB_EventType_PopupMenu : The popup menu was blocked (display a custom menu here) (Windows only).
Example 1: Navigation callback (combining the code for Linux and MacOS from my feature request 5 years ago with the native callback on Windows)
Code: Select all
; Successful tests:
; -----------------
; + Linux Mint 18.3 x64 'Sylvia' with PB 5.46 x64 using GTK2 and GTK3 in
; ASCII- and Unicode mode and with PB 5.62 x64 using GTK2 and GTK3
; + MacOS 10.6.8 'Snow Leopard' with PB 5.46 x86 in ASCII- and Unicode mode and
; PB 5.62 x86
; + MacOS 10.13.6 'High Sierra' with PB 5.46 x86 and x64 in ASCII- and
; Unicode mode and PB 5.62 x86 and x64
; + Windows 7 SP1 x64 with PB 5.46 x86 and x64 in ASCII- and Unicode mode and
; PB 5.62 x86 and x64
EnableExplicit
#URL = "https://www.purebasic.com/news.php"
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
ImportC "-lgobject-2.0"
g_signal_connect_data(*Instance, Signal.P-UTF8, *Callback, *UserData,
*ClosureNotify, ConnectFlags.I)
EndImport
CompilerIf Subsystem("gtk2")
ImportC "-lwebkitgtk-1.0"
CompilerElse
ImportC "-lwebkitgtk-3.0"
CompilerEndIf
webkit_web_navigation_action_get_original_uri(*NavigationAction)
webkit_web_policy_decision_ignore(*PolicyDecision)
webkit_web_policy_decision_use(*PolicyDecision)
EndImport
ProcedureC NavigationCallback(*WebView, *Frame, *Request,
*NavigationAction, *PolicyDecision, UserData)
If PeekS(webkit_web_navigation_action_get_original_uri(*NavigationAction),
-1, #PB_UTF8) = #URL
MessageRequester("Info", "No news today!")
webkit_web_policy_decision_ignore(*PolicyDecision)
Else
webkit_web_policy_decision_use(*PolicyDecision)
EndIf
EndProcedure
CompilerCase #PB_OS_MacOS
Define AppDelegate = CocoaMessage(0,
CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Define DelegateClass = CocoaMessage(0, AppDelegate, "class")
ProcedureC PolicyDecisionMaker(Object.I, Selector.I, WebView.I,
ActionInformation.I, Request.I, *FrameName, Listener.I)
Protected *URL = CocoaMessage(0, Request, "URL")
Protected URL.S = PeekS(CocoaMessage(0,
CocoaMessage(0, *URL, "absoluteString"), "UTF8String"), -1, #PB_UTF8)
If URL = #URL
MessageRequester("Info", "No news today!")
Else
CocoaMessage(0, Listener, "use")
EndIf
EndProcedure
class_addMethod_(DelegateClass, sel_registerName_("webView:" +
"decidePolicyForNavigationAction:request:frame:decisionListener:"),
@PolicyDecisionMaker(), "v@:@@@@@")
CompilerCase #PB_OS_Windows
Procedure NavigationCallback(WebGadget.I, URL.S)
If URL = #URL
MessageRequester("Info", "No news today!")
ProcedureReturn #False
Else
ProcedureReturn #True
EndIf
EndProcedure
CompilerEndSelect
OpenWindow(0, 100, 100, 600, 300, "WebGadget")
WebGadget(0, 10, 10, 580, 280, "http://www.purebasic.com")
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
g_signal_connect_data(GadgetID(0), "navigation-policy-decision-requested",
@NavigationCallback(), 0, 0, 0)
CompilerCase #PB_OS_MacOS
CocoaMessage(0, GadgetID(0), "setPolicyDelegate:", AppDelegate)
CompilerCase #PB_OS_Windows
SetGadgetAttribute(0, #PB_Web_NavigationCallback, @NavigationCallback())
CompilerEndSelect
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Implementation of Linux and MacOS code combined with these native Windows calls:
- GetGadgetItemText(#WebGadget, #PB_Web_HTMLCode)
- GetGadgetItemText(#WebGadget, #PB_Web_PageTitle)
- GetGadgetItemText(#WebGadget, #PB_Web_SelectedText)
- SetGadgetItemText(#WebGadget, #PB_Web_HTMLCode, HTML$)
Code: Select all
; Successful tests:
; -----------------
; + Linux Mint 18.3 x64 'Sylvia' with PB 5.46 x64 using GTK2 and GTK3 in
; ASCII- and Unicode mode and with PB 5.62 x64 using GTK2 and GTK3
; + MacOS 10.6.8 'Snow Leopard' with PB 5.46 x86 in ASCII- and Unicode mode and
; PB 5.62 x86
; + MacOS 10.13.6 'High Sierra' with PB 5.46 x86 and x64 in ASCII- and
; Unicode mode and PB 5.62 x86 and x64
; + Windows 7 SP1 x64 with PB 5.46 x86 and x64 in ASCII- and Unicode mode and
; PB 5.62 x86 and x64
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 DOMRange.I
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
CompilerCase #PB_OS_MacOS ; -------------------------------------
JavaScriptString = "document.getElementsByTagName('title')[0]" +
".innerHTML;"
*HTML = CocoaMessage(0, GadgetID(6),
"stringByEvaluatingJavaScriptFromString:$", @JavaScriptString)
Title = PeekS(CocoaMessage(0, *HTML, "UTF8String"), -1, #PB_UTF8)
CompilerCase #PB_OS_Windows ; -------------------------------------
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
CompilerCase #PB_OS_MacOS ; -------------------------------------
DOMRange = CocoaMessage(0, GadgetID(6), "selectedDOMRange")
If DOMRange
SelectedText = PeekS(CocoaMessage(0, CocoaMessage(0, DOMRange,
"text"), "UTF8String"), -1, #PB_UTF8)
EndIf
CompilerCase #PB_OS_Windows ; -------------------------------------
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