NavigationCallback for WebGadget in Linux + MacOS
Posted: Tue Mar 19, 2013 11:52 am
Currently the navigation callback for the WebGadgetis only implemented for Windows. It would be nice to have it also available cross-platform for Linux and MacOS X. The following two code examples demonstrate how it might be done (doing the same as the second example from PB help for the WebGadget, i.e. blocking the switching to the News hyperlink on the PB website).
Linux version (works in ASCII and Unicode mode):
MacOS X version (Cocoa framework):
Code: Select all
SetGadgetAttribute(#WebGadget, #PB_Web_NavigationCallback, @Callback())
Linux version (works in ASCII and Unicode mode):
Code: Select all
EnableExplicit
ImportC "-lgobject-2.0"
g_signal_connect_data(*Instance, Signal.P-UTF8, *Callback, *UserData, *ClosureNotify, ConnectFlags.I)
EndImport
ImportC "-lwebkitgtk-1.0"
webkit_web_navigation_action_get_original_uri(*NavigationAction)
webkit_web_policy_decision_ignore(*PolicyDecision)
webkit_web_policy_decision_use(*PolicyDecision)
EndImport
Enumeration
#WEBKIT_NAVIGATION_RESPONSE_ACCEPT
#WEBKIT_NAVIGATION_RESPONSE_IGNORE
EndEnumeration
ProcedureC WebGadgetCallback(*WebView, *Frame, *Request, *NavigationAction, *PolicyDecision, UserData)
If PeekS(webkit_web_navigation_action_get_original_uri(*NavigationAction), -1, #PB_UTF8) = "http://www.purebasic.com/news.php"
MessageRequester("", "No news today!")
webkit_web_policy_decision_ignore(*PolicyDecision)
Else
webkit_web_policy_decision_use(*PolicyDecision)
EndIf
EndProcedure
If OpenWindow(0, 200, 100, 600, 300, "WebGadget")
WebGadget(0, 10, 10, 580, 280, "http://www.purebasic.com")
g_signal_connect_data(GadgetID(0), "navigation-policy-decision-requested", @WebGadgetCallback(), 0, 0, 0)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Code: Select all
EnableExplicit
ImportC ""
sel_registerName(str.p-ascii)
class_addMethod(class, selector, imp, types.p-ascii)
EndImport
Global appDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Global delegateClass = CocoaMessage(0, appDelegate, "class")
ProcedureC PolicyDecisionMaker(obj, sel, webView, actionInformation, request, frameName, listener)
Protected URL = CocoaMessage(0, request, "URL")
Protected URLString.s = PeekS(CocoaMessage(0, CocoaMessage(0, URL, "absoluteString"), "UTF8String"), -1, #PB_UTF8)
If URLString = "http://www.purebasic.com/news.php"
MessageRequester("", "No news today!")
Else
CocoaMessage(0, listener, "use")
EndIf
EndProcedure
class_addMethod(delegateClass, sel_registerName("webView:decidePolicyForNavigationAction:request:frame:decisionListener:"), @PolicyDecisionMaker(), "v@:@@@@@")
If OpenWindow(0, 270, 100, 600, 300, "WebGadget")
WebGadget(0, 10, 10, 580, 280, "http://www.purebasic.com")
CocoaMessage(0, GadgetID(0), "setPolicyDelegate:", appDelegate)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf