WebGadget does not print (or how to enable JavaScript?)

Mac OSX specific forum
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

WebGadget does not print (or how to enable JavaScript?)

Post by Kukulkan »

Hi,

I'm using such code to print the content of a WebGadget in my PureBasic application:

Code: Select all

File.s = "c:\temp\test.htm" ; Adapt to OS
If OpenFile(1, File.s)
  WriteString(1, "<html><body><p>This is a simple test text</p><script>window.print();</script></body></html>")
  CloseFile(1)
EndIf

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  WebGadget(0, 10, 10, 580, 280, "file://" + File.s) 
  Repeat 
  Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf
It works fine on Windows and Linux, but sadly on MacOS simply nothing happens. To test use this path for MacOS:

Code: Select all

File.s = "/tmp/test.htm"
How to make the WegGadget print? Is JavaScript disabled on MacOS?

Kukulkan
Last edited by Kukulkan on Tue Oct 02, 2012 3:16 pm, edited 1 time in total.
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: WebGadget does not print

Post by Kukulkan »

Hi,

I just found out that JavaScript is simply disabled:

Code: Select all

File.s = "/tmp/test.htm" ; Adapt to OS
If FileSize(File.s) > -1: DeleteFile(File.s): EndIf
If OpenFile(1, File.s)
  WriteString(1, "<html><body><p>This is a simple test text</p><script>alert('test');</script></body></html>")
  CloseFile(1)
EndIf

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  WebGadget(0, 10, 10, 580, 280, "file://" + File.s) 
  Repeat 
  Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf
How to enable?

Kukulkan
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: WebGadget does not print (or how to enable JavaScript?)

Post by wilbert »

JavaScript itself seems to be working.
Try this

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  
  WebGadget(0, 10, 10, 580, 240, "")
  SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><body><p>This is a simple test text</body></html>")
  
  ButtonGadget(1, 10, 260, 200, 30, "Evaluate script")
  
  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget
      Select EventGadget()
        Case 1
          
          ScriptObject = CocoaMessage(0, GadgetID(0), "windowScriptObject")
          CocoaMessage(0, ScriptObject, "evaluateWebScript:$", @"location.href='www.purebasic.com'")
          
      EndSelect
    EndIf
  Until EventID = #PB_Event_CloseWindow
  
EndIf
You will see it loads the PureBasic site when the script is evaluated.
Maybe print and alert aren't supported from within a WebGadget.

If you want to print, you can do it like this

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  
  WebGadget(0, 10, 10, 580, 240, "")
  SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><body><p>This is a simple test text</body></html>")
  
  ButtonGadget(1, 10, 260, 200, 30, "Print")
  
  Repeat
    EventID = WaitWindowEvent()
    If EventID = #PB_Event_Gadget
      Select EventGadget()
        Case 1
          
          PrintInfo = CocoaMessage(0, 0, "NSPrintInfo sharedPrintInfo")
          FrameView = CocoaMessage(0, CocoaMessage(0, GadgetID(0), "mainFrame"), "frameView")
          Operation = CocoaMessage(0, FrameView, "printOperationWithPrintInfo:", PrintInfo)
          CocoaMessage(0, Operation, "setShowsPrintPanel:", #True)
          CocoaMessage(0, Operation, "runOperation")
          
      EndSelect
    EndIf
  Until EventID = #PB_Event_CloseWindow
  
EndIf
Last edited by wilbert on Fri Feb 15, 2013 12:10 pm, edited 1 time in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: WebGadget does not print (or how to enable JavaScript?)

Post by wilbert »

After some more testing ...
There does seem to be an option to turn JavaScript on and off and it seems to remember the last setting and not being enabled or disabled by PureBasic.
Maybe Fred can enable it just to be sure

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
  
  WebGadget(0, 10, 10, 580, 240, "")
  
  Pref = CocoaMessage(0, GadgetID(0), "preferences")
  CocoaMessage(0, Pref, "setJavaScriptEnabled:", #True)
  CocoaMessage(0, GadgetID(0), "setPreferences:", Pref)
  
  SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><body><p>This is a <a href='javascript:location.href="+Chr(34)+"www.purebasic.com" + Chr(34)+ "'>simple</a> test text</body></html>")
  
  Repeat
    EventID = WaitWindowEvent()
  Until EventID = #PB_Event_CloseWindow
  
EndIf
Try to set enabled to false and the javascript won't work anymore.
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: WebGadget does not print (or how to enable JavaScript?)

Post by Kukulkan »

Hi,
JavaScript itself seems to be working.
No. Even if I replace the print script code with some alert('test'); message, it does not work. But it works if I open the html file in normal webbrowser.
There does seem to be an option to turn JavaScript on and off and it seems to remember the last setting and not being enabled or disabled by PureBasic.
This explains it much better. So I have to ensure that JS is enabled, otherwise my cross plattform print option does not work. I will try the outlined option and will report here tomorrow.

Thank you!

Kukulkan
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: WebGadget does not print (or how to enable JavaScript?)

Post by wilbert »

Unfortunately print and alert seem to be special javascript cases.

A little explanation ...
The PureBasic WebGadget is a subclass of the cocoa WebView class.
The interface of a WebView is handled by a delegate that needs to conform to the WebUIDelegate protocol.
There are two methods of the delegate that control print and alert
webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:
webView:printFrameView:

When those methods are not implemented, nothing happens.

It does make sense since it gives the application that embeds the WebView control of what should happen.
It could for example block printing of some sites, or allow alerts only from a specific site.
So basically javascript print and alert will only work if Fred implements those delegate methods or if you create a delegate class with PureBasic itself which is not very convenient.
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: WebGadget does not print (or how to enable JavaScript?)

Post by jesperbrannmark »

The windows version of Purebasic supports the

Code: Select all

 <body onload="window.print()">
call. It would make sense the Mac OS one also supports this...
(the pc one also supports <a target=_new> which the mac one doesnt)
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: WebGadget does not print (or how to enable JavaScript?)

Post by Kukulkan »

Hi,

Printing using enabled JavaScript does not work. Even with wilbert's tip. :(

Printing using printOperationWithPrintInfo: function works. It is still not good to miss a plattform independent print option but for now it's ok.

Kukulkan
spacebuddy
Enthusiast
Enthusiast
Posts: 356
Joined: Thu Jul 02, 2009 5:42 am

Re: WebGadget does not print (or how to enable JavaScript?)

Post by spacebuddy »

If OpenWindow(0, 0, 0, 600, 300, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

WebGadget(0, 10, 10, 580, 240, "")
SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><body><p>This is a simple test text</body></html>")

ButtonGadget(1, 10, 260, 200, 30, "Print")

Repeat
EventID = WaitWindowEvent()
If EventID = #PB_Event_Gadget
Select EventGadget()
Case 1

PrintInfo = CocoaMessage(0, 0, "NSPrintInfo sharedPrintInfo")
FrameView = CocoaMessage(0, CocoaMessage(0, GadgetID(0), "mainFrame"), "frameView")
Operation = CocoaMessage(0, FrameView, "printOperationWithPrintInfo:", PrintInfo)
CocoaMessage(0, Operation, "setShowsPrintPanel:", #True)
CocoaMessage(0, Operation, "runOperation")

EndSelect
EndIf
Until EventID = #PB_Event_CloseWindow

EndIf


Can this be adapted to work with the EditorGadGet or does it only work on a WebGadGet?
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: WebGadget does not print (or how to enable JavaScript?)

Post by Danilo »

spacebuddy wrote:Can this be adapted to work with the EditorGadGet or does it only work on a WebGadGet?

Code: Select all

    CocoaMessage(0,GadgetID(editorGadget),"print:",0) ; NSView Class
:?:
spacebuddy
Enthusiast
Enthusiast
Posts: 356
Joined: Thu Jul 02, 2009 5:42 am

Re: WebGadget does not print (or how to enable JavaScript?)

Post by spacebuddy »

Thank you Danilo :D
Post Reply