I have put together a test that uses all these methods. I see different people are getting different results.
My computers the only one working fully is the one using Safari - but on other computers that seem to print the wrong page from time to time. On my computers the webprint userlib seem to chop off images (most likely because they dont have time to load).
Code: Select all
;EnableExplicit
; *** import the required C functions ***
ImportC ""
HIWebViewGetWebView(inView)
oClassAddMethod(class, selector, imp, types.p-ascii) As "_class_addMethod"
oClass(name.p-ascii) As "_objc_lookUpClass"
oCreateClass(superclass, name.p-ascii, extraBytes = 0) As "_objc_allocateClassPair"
oInstance(class, extraBytes = 0) As "_class_createInstance"
CocoaMessage(receiver, selector, arg1 = #Null, arg2 = #Null, arg3 = #Null) As "_objc_msgSend"
oRegisterClass(class) As "_objc_registerClassPair"
oSel(name.p-ascii) As "_sel_registerName"
EndImport
; *** selectors that are used a lot ***
Global SEL_absoluteString = oSel("absoluteString")
Global SEL_dataSource = oSel("dataSource")
Global SEL_frameView = oSel("frameView")
Global SEL_getCharacters_range_ = oSel("getCharacters:range:")
Global SEL_length = oSel("length")
Global SEL_print = oSel("print:")
Global SEL_request = oSel("request")
Global SEL_URL = oSel("URL")
; *** functions to get frame url ***
Procedure.s StringFromNSString(nsstring)
Protected len = CocoaMessage(nsstring, SEL_length)
Protected *mem = malloc_(len << 1); allocate 16 byte aligned uninitialized memory
CocoaMessage(nsstring, SEL_getCharacters_range_, *mem, 0, len)
Protected result.s = PeekS(*mem, len, #PB_Unicode)
free_(*mem)
ProcedureReturn result
EndProcedure
Procedure.s GetFrameURL(frame, provisional = #False)
Protected source = CocoaMessage(frame, SEL_dataSource)
Protected urlString = CocoaMessage(CocoaMessage(CocoaMessage(source, SEL_request), SEL_URL), SEL_absoluteString)
ProcedureReturn StringFromNSString(urlString)
EndProcedure
; *** the functions for our delegate class ***
ProcedureC LoadComplete(obj, sel, webview, frame); load complete
Debug "Finished loading : " + GetFrameURL(frame)
Protected frameView = CocoaMessage(frame, SEL_frameView)
CocoaMessage(frameView, SEL_print)
EndProcedure
; *** create the delegate class and instance ***
Define FrameLoadDelegateClass = oCreateClass(oClass("NSObject"), "PB_FrameLoadDelegate"); PB_FrameLoadDelegate class extends NSObject
oClassAddMethod(FrameLoadDelegateClass, oSel("webView:didFinishLoadForFrame:"), @LoadComplete(), "v@:@@")
oRegisterClass(FrameLoadDelegateClass)
Define FrameLoadDelegate = oInstance(FrameLoadDelegateClass)
; *** class and instance are created now ***
OpenWindow(0,0,0,640,480,"TEST 1")
ButtonGadget(1,0,0,200,30,"TEST 1")
ButtonGadget(2,0,40,200,30,"TEST 2")
ButtonGadget(3,0,80,200,30,"TEST 3")
ButtonGadget(4,0,120,200,30,"TEST 4")
Define PBWebView = WebGadget(#PB_Any, 0, 250, 580, 280, "")
Define WebView = HIWebViewGetWebView(GadgetID(PBWebView))
CocoaMessage(WebView, oSel("setFrameLoadDelegate:"), FrameLoadDelegate)
Repeat
a=WaitWindowEvent()
If a=#PB_Event_Gadget And EventGadget()=1
WebPrint_ToPrinter()
WebPrint_SetPrintBackgrounds(#True)
WebPrint_SetShowDialog(#True)
WebPrint_PrintURL("http://www.purebasic.com")
ElseIf a=#PB_Event_Gadget And EventGadget()=2
WebPrint_ToPrinter()
WebPrint_SetPrintBackgrounds(#True)
WebPrint_SetShowDialog(#False)
WebPrint_PrintURL("http://www.purebasic.com")
ElseIf a=#PB_Event_Gadget And EventGadget()=3
SetGadgetText(PBWebView, "http://www.purebasic.com")
ElseIf a=#PB_Event_Gadget And EventGadget()=4
aa.s="tell application "+Chr(34)+"Safari"+Chr(34)+#CRLF$
aa.s+"make new document at end of documents"+#CRLF$
aa.s+"set URL of document 1 to "+Chr(34)+"http://www.purebasic.com"+Chr(34)+#CRLF$
aa.s+"try"+#CRLF$
aa.s+"repeat"+#CRLF$
aa.s+"delay 0.5"+#CRLF$
aa.s+"tell application "+Chr(34)+"System Events"+Chr(34)+" to "
aa.s+"tell application process "+Chr(34)+"Safari"+Chr(34)+#CRLF$
aa.s+"if (name of static text 1 of group 1 of window 1 as text) begins with "+Chr(34)+"Contacting"+Chr(34)
aa.s+" or (name of Static text 1 of group 1 of window 1 as text) begins with "+Chr(34)+"Loading"+Chr(34)+" then "+#CRLF$
aa.s+"else"+#CRLF$
aa.s+"exit repeat"+#CRLF$
aa.s+"end if"+#CRLF$
aa.s+"end tell"+#CRLF$
aa.s+"end repeat"+#CRLF$
aa.s+"on error"+#CRLF$
aa.s+"delay 3"+#CRLF$
aa.s+"end try"+#CRLF$
aa.s+"tell front document"+#CRLF$
aa.s+"print"+#CRLF$
aa.s+"close"+#CRLF$
aa.s+"end tell"+#CRLF$
aa.s+"close"+#CRLF$
aa.s+"end tell"+#CRLF$
COCOA_AppleScript(aa.s)
EndIf
Until a=#PB_Event_CloseWindow