NavigationCallback for WebGadget

Linux specific forum
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

NavigationCallback for WebGadget

Post by Shardik »

Currently the navigation callback for the WebGadget

Code: Select all

SetGadgetAttribute(#WebGadget, #PB_Web_NavigationCallback, @Callback())
is only implemented for Windows. The following example code demonstrates how this might be done in Linux (tested in Kubuntu 12.04 x64 and OpenSUSE 12.3 x86 in ASCII and Unicode mode). I have also posted a feature request to ask for an implemention of this navigation callback in PureBasic natively for Linux and MacOS X (there I also posted this code example and a second one for MacOS X). The example code below does the same as the 2nd example from PB help for the Webgadget: it intercepts a click onto the hyperlink for "News" and displays a message requester.

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
mdp
Enthusiast
Enthusiast
Posts: 115
Joined: Mon Apr 18, 2005 8:28 pm

Re: NavigationCallback for WebGadget

Post by mdp »

My goodness me Shardik mate,
you are offering me your hand quite frequently in these sessions, I have to say.

Why I ABSOLUTELY second such request, how could any such multiplatform feature have been missing! A few minutes ago I was quite taken aback and bewildered at my failing code - "what, not for Linux?!? Impossbile..."

I remember many years ago I produced a few applications that would have a good part of the GUI put inside a webgadget, using links such as "<a href='command:DoThisAndThat'>" that would trigger procedures in the PB code. Very convenient in some cases.
mdp
Enthusiast
Enthusiast
Posts: 115
Joined: Mon Apr 18, 2005 8:28 pm

Re: NavigationCallback for WebGadget

Post by mdp »

Just one note (and Shardik thanks thanks thanks again),
here (on Ubuntu 12.04 and 'GNU ld (GNU Binutils for Ubuntu) 2.22') it requires a full path to find the library:

Code: Select all

ImportC "/usr/lib/libwebkitgtk-1.0.so.0"
  webkit_web_navigation_action_get_original_uri(*NavigationAction)
  webkit_web_policy_decision_ignore(*PolicyDecision)
  webkit_web_policy_decision_use(*PolicyDecision)
EndImport
The path '/usr/lib' is in the 'ld' search path (obtained with command
ldconfig -v 2>/dev/null | grep -v ^$'\t'
). I can't figure out what is wrong with the '-l' option.
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: NavigationCallback for WebGadget

Post by fsw »

Shardik wrote:Currently the navigation callback for the WebGadget

Code: Select all

SetGadgetAttribute(#WebGadget, #PB_Web_NavigationCallback, @Callback())
is only implemented for Windows. The following example code demonstrates how this might be done in Linux (tested in Kubuntu 12.04 x64 and OpenSUSE 12.3 x86 in ASCII and Unicode mode). I have also posted a feature request to ask for an implemention of this navigation callback in PureBasic natively for Linux and MacOS X (there I also posted this code example and a second one for MacOS X). The example code below does the same as the 2nd example from PB help for the Webgadget: it intercepts a click onto the hyperlink for "News" and displays a message requester.
Just wanted to let you know that your code works flawlessly on Manjaro.
No changes/additons needed.

Thank you.

8)

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: NavigationCallback for WebGadget

Post by Shardik »

mdp wrote:Just one note (and Shardik thanks thanks thanks again),
here (on Ubuntu 12.04 and 'GNU ld (GNU Binutils for Ubuntu) 2.22') it requires a full path to find the library:

Code: Select all

ImportC "/usr/lib/libwebkitgtk-1.0.so.0"
  webkit_web_navigation_action_get_original_uri(*NavigationAction)
  webkit_web_policy_decision_ignore(*PolicyDecision)
  webkit_web_policy_decision_use(*PolicyDecision)
EndImport
When testing with different distributions I found out that all the tested distributions below needed the full path behind ImportC as posted by mdp:

- Kubuntu 12.04 x86:
Only libwebkitgtk-3.0.so.0 was installed but throws an error because it's compiled for GTK3 and mixing symbols from GTK2 and GTK3 is not possible.
After installation of libwebkitgtk-1.0-0 and libwebkitgtk-1.0-common with Synaptic my example worked using the full path.

- Lubuntu 12.10 x86:
After installation of libwebkitgtk-1.0-0 and libwebkitgtk-1.0-common with Synaptic my example worked using the full path.

- Xubuntu 13.04 x86:
Both libwebkitgtk-1.0-0 and libwebkitgtk-1.0-common were already installed together with libwebkitgtk-3.0-0. My example worked using the full path.

- Fedora 18 x86:
After
> sudo yum install libwebkitgtk-1.0.so.0
my example worked using the full path.

- Linux Mint 15 x86 with Cinnamon:
Both libwebkitgtk-1.0-0 and libwebkitgtk-1.0-common were already installed together with libwebkitgtk-3.0-0. My example worked using the full path.

Hint: In some distributions the warning "DEBUG: NP_Initialize succeeded" was displayed. How to eliminate this warning I demonstrated already here.
User avatar
Frarth
Enthusiast
Enthusiast
Posts: 241
Joined: Tue Jul 21, 2009 11:11 am
Location: On the planet
Contact:

Re: NavigationCallback for WebGadget

Post by Frarth »

Shardik wrote:Currently the navigation callback for the WebGadget

Code: Select all

SetGadgetAttribute(#WebGadget, #PB_Web_NavigationCallback, @Callback())
is only implemented for Windows. The following example code demonstrates how this might be done in Linux (tested in Kubuntu 12.04 x64 and OpenSUSE 12.3 x86 in ASCII and Unicode mode). I have also posted a feature request to ask for an implemention of this navigation callback in PureBasic natively for Linux and MacOS X (there I also posted this code example and a second one for MacOS X). The example code below does the same as the 2nd example from PB help for the Webgadget: it intercepts a click onto the hyperlink for "News" and displays a message requester.

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
Shardik, have you tried this code using webkitgtk-3.0 ? Because version 1.0 throws me an error about mixing gtk2 and gtk3 symbols.
PureBasic 5.41 LTS | Xubuntu 16.04 (x32) | Windows 7 (x64)
User avatar
Frarth
Enthusiast
Enthusiast
Posts: 241
Joined: Tue Jul 21, 2009 11:11 am
Location: On the planet
Contact:

Re: NavigationCallback for WebGadget

Post by Frarth »

First of all, on LinuxMint 17 the webkitgtk3 library is in the directory /usr/lib/i386-linux-gnu/

Second, when I run the above code using libwebkitgtk-3.0.so.0 I get the following CRITICAL errors on WaitWindowEvent:

[22:02:53] Waiting for executable to start...
[22:02:53] Executable type: Linux - x86 (32bit, Unicode, Thread, Purifier)
[22:02:53] Executable started.
[22:02:54] [WARNING] webgadget6.pb (Line: 28)
[22:02:54] [WARNING] DEBUG: NP_Initialize
[22:02:54] [WARNING] webgadget6.pb (Line: 28)
[22:02:54] [WARNING] DEBUG: NP_Initialize succeeded
[22:02:54] [WARNING] webgadget6.pb (Line: 28)
[22:02:54] [WARNING] DEBUG: NP_Initialize
[22:02:54] [WARNING] webgadget6.pb (Line: 28)
[22:02:54] [WARNING] DEBUG: NP_Initialize succeeded
[22:02:54] [WARNING] webgadget6.pb (Line: 28)
[22:02:54] [WARNING] DEBUG: NP_Initialize
[22:02:54] [WARNING] webgadget6.pb (Line: 28)
[22:02:54] [WARNING] DEBUG: NP_Initialize succeeded
[22:02:54] [WARNING] webgadget6.pb (Line: 28)
[22:02:54] [WARNING] DEBUG: NP_Initialize
[22:02:54] [WARNING] webgadget6.pb (Line: 28)
[22:02:54] [WARNING] DEBUG: NP_Initialize succeeded
[22:02:54] [WARNING] webgadget6.pb (Line: 33)
[22:02:54] [WARNING] GLib-GObject (CRITICAL): g_object_ref: assertion 'object->ref_count > 0' failed
[22:02:54] [WARNING] webgadget6.pb (Line: 33)
[22:02:54] [WARNING] GLib-GObject (CRITICAL): g_object_unref: assertion 'object->ref_count > 0' failed
[22:02:57] The Program execution has finished.
PureBasic 5.41 LTS | Xubuntu 16.04 (x32) | Windows 7 (x64)
User avatar
Frarth
Enthusiast
Enthusiast
Posts: 241
Joined: Tue Jul 21, 2009 11:11 am
Location: On the planet
Contact:

Re: NavigationCallback for WebGadget

Post by Frarth »

PureBasic 5.41 LTS | Xubuntu 16.04 (x32) | Windows 7 (x64)
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: NavigationCallback for WebGadget

Post by Shardik »

Since PB 5.40 the default GTK version is no longer GTK2 but GTK3 although it's possible in PB 5.4x to use GTK2 as a subsystem (select "Compiler > Compiler Options..." in the menu bar of the IDE, select the tab "Compiler Options" and behind "Library Subsystem:" enter "gtk2").

In order to use the WebGadget you have to utilize WebKitGTK which requires one of these 2 libraries (you only have to install one of them if you don't need both):
- libwebkitgtk-1.0-0 (for GTK2)
- libwebkitgtk-3.0-0 (for GTK3)

By the way: WebKitGTK is deprecated. The current version WebKitGTK2 is not supported by PureBasic because the WebGadget is still compiled with the old WebKitGTK...

Depending on your distribution one or both of these libraries might be installed already.

I have modified my code example from above to now work equally well with both GTK2 and GTK3 in both PB 5.3x and PB 5.4x and in x86 and x64 distributions. For the following distributions I had to install the following libraries with the appropriate console commands (of course you may also use Synaptic or Aptitude on Debian/Ubuntu derived distributions):

Kubuntu 16.04 x86 with KDE, Ubuntu 16.04 x86 with Unity and Xubuntu 16.04 x86 with Xfce
I found out that installing libwebkitgtk-1.0/libwebkitgtk-3.0 would require a whopping 1.669 MB of additional disk space because the dev and debug versions would also be installed. I therefore decided to install libwebkitgtk-1.0-0/libwebkitgtk-3.0-0 which only required about 40 MB. The drawback is that you have to always specify the complete path in your ImportC statement. When installing the dev version a simple

Code: Select all

ImportC "-lwebkitgtk-1.0"
would be sufficient...

These are the console commands to install the libraries on Debian/Ubuntu derived distributions (it's only necessary to install one of these if you don't need both GTK2 and GTK3):

Code: Select all

sudo apt-get install libwebkitgtk-1.0-0
sudo apt-get install libwebkitgtk-3.0-0
In order to compile GTK2 programs you also have to install the GTK2 dev libs:

Code: Select all

sudo apt-get install libgtk2.0-dev
Lubuntu 14.04 x86 with LXDE (necessary only for GTK2)

Code: Select all

sudo apt-get install libwebkitgtk-1.0-0
Ubuntu MATE 14.04 x86 with MATE (necessary only for GTK2)

Code: Select all

sudo apt-get install libwebkitgtk-1.0-0
Linux Mint 17.3 x64 "Rosa" with Cinnamon
Nothing had to be done. All libraries for GTK2 and GTK3 were already preinstalled.


The distributions above are all derived from Debian/Ubuntu and utilize the Debian package management. Therefore I also tried a distribution with RPM packages derived from Red Hat (unfortunately the package names differ):

Fedora 23 x86 with Gnome 3
For GTK2:

Code: Select all

sudo dnf install webkitgtk
webkitgtk2 is already installed by default!

And finally this is my modified example (for Fedora you have to put the ImportC block in comments and uncomment the Fedora ImportC line):

Code: Select all

EnableExplicit

ImportC "-lgobject-2.0"
  g_signal_connect_data(*Instance, Signal.P-UTF8, *Callback, *UserData, *ClosureNotify, ConnectFlags.I)
EndImport

CompilerIf (#PB_Compiler_Version < 540 And Subsystem("gtk3") = #False) Or
  (#PB_Compiler_Version >= 540 And Subsystem("gtk2") = #True)
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    ImportC "/usr/lib/i386-linux-gnu/libwebkitgtk-1.0.so.0"   ; Ubuntu/Kubuntu/Xubuntu x86 with GTK2
  CompilerElse
    ImportC "/usr/lib/x86_64-linux-gnu/libwebkitgtk-1.0.so.0" ; Ubuntu/Kubuntu/Xubuntu x64 with GTK2
  CompilerEndIf
;   ImportC "/usr/lib/libwebkitgtk-1.0.so.0"                  ; Fedora 23 with GTK2
CompilerElse
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    ImportC "/usr/lib/i386-linux-gnu/libwebkitgtk-3.0.so.0"   ; Ubuntu/Kubuntu/Xubuntu x86 with GTK3
  CompilerElse
    ImportC "/usr/lib/x86_64-linux-gnu/libwebkitgtk-3.0.so.0" ; Ubuntu/Kubuntu/Xubuntu x64 with GTK3
  CompilerEndIf
;   ImportC "/usr/lib/libwebkitgtk-3.0.so.0"                  ; Fedora 23 x86 with GTK3
CompilerEndIf
    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
At last these were the warnings I did encounter (only with GTK3):

Fedora 23 x86 with Gnome 3, Ubuntu 16.04 x86 with Unity, Xubuntu 16.04 x86 with Xfce
[WARNING] Gtk(MESSAGE): GtkDialog mapped without a transient parent. This is discouraged.
This warning has already been posted several times in the Linux bugs subforum. Fred's response:
Fred wrote:even gedit shipped with Ubuntu has some gtk error here when launched from CLI, so I don't think it's worth the time investigating.
Linux Mint 17.3 x64 "Rosa" with Cinnamon, Lubuntu 14.04 x86 with LXDE, Ubuntu MATE 14.04 x86 with LXDE
[WARNING] GLib-GObject (CRITICAL): g_object_ref: assertion 'object->ref_count > 0' failed

Fedora 23 x86 with Gnome 3
[WARNING] Gnome Shell Browser Plugin (DEBUG): plugin loaded

The only distributions without warnings were Kubuntu 14.04 x86 and Kubuntu 16.04 x86 both with KDE.
Last edited by Shardik on Tue Sep 06, 2016 8:23 am, edited 1 time in total.
User avatar
Frarth
Enthusiast
Enthusiast
Posts: 241
Joined: Tue Jul 21, 2009 11:11 am
Location: On the planet
Contact:

Re: NavigationCallback for WebGadget

Post by Frarth »

What PB version 5.4x did you use? With my example shown in this thread (http://www.purebasic.fr/english/viewtop ... 15&t=65951) I did not have any error message using PB 5.41 on Xubuntu 16.04.
PureBasic 5.41 LTS | Xubuntu 16.04 (x32) | Windows 7 (x64)
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: NavigationCallback for WebGadget

Post by Shardik »

I tested with PB 5.42 (and in some cases also with PB 5.31). And what I posted are warnings (not errors that terminate the compilation) from the debugger's error log (IDE's menu: Debugger > Error Log > Show Error Log). If you have disabled "Show Error Log", you won't see them.
User avatar
Frarth
Enthusiast
Enthusiast
Posts: 241
Joined: Tue Jul 21, 2009 11:11 am
Location: On the planet
Contact:

Re: NavigationCallback for WebGadget

Post by Frarth »

Show Error Log is enabled here. I was not talking about fatal compilation errors but [CRITICAL] warnings like:

Code: Select all

GLib-GObject (CRITICAL): g_object_ref: assertion 'object->ref_count > 0' failed
I have not found the time yet to try your code, but I'll try to do that this weekend.
PureBasic 5.41 LTS | Xubuntu 16.04 (x32) | Windows 7 (x64)
User avatar
Frarth
Enthusiast
Enthusiast
Posts: 241
Joined: Tue Jul 21, 2009 11:11 am
Location: On the planet
Contact:

Re: NavigationCallback for WebGadget

Post by Frarth »

@Shardik: I tested your code and had no warning or error messages. I checked again and debug error messages are turned on. I use the Integrated IDE Debugger with warning level set to Display Warnings.

Code: Select all

[18:04:26] Waiting for executable to start...
[18:04:27] Executable type: Linux - x86  (32bit, Unicode, Thread, Purifier)
[18:04:27] Executable started.
[18:04:34] The Program execution has finished.
As I said earlier, I use Xubuntu 16.04 with PB 5.41

These are the webkit related libraries installed:

- gir1.2-javascriptcoregtk-3.0
- gir1.2-javascriptcoregtk-4.0
- gir1.2-webkit2-3.0
- libjavascriptcoregtk-1.0-0
- libjavascriptcoregtk-1.0-dev
- libjavascriptcoregtk-3.0-0
- libjavascriptcoregtk-3.0-dev
- libjavascriptcoregtk-4.0-18
- libwebkit2gtk-3.0-25 (API layer)
- libwebkit2gtk-3.0-dev
- libwebkit2gtk-4.0-37
- libwebkit2gtk-4.0-37-gtk2
- libwebkitgtk-1.0-0
- libwebkitgtk-1.0-common
- libwebkitgtk-3.0-0
- libwebkitgtk-3.0-common
- libwebkitgtk-3.0-dev
- libwebkitgtk-common-dev
- libwebkitgtk-dev

EDIT: BTW, I never had this warning message you mentioned: "[WARNING] Gtk(MESSAGE): GtkDialog mapped without a transient parent. This is discouraged." The post you referred to mentioned the MessageRequester, but the warning has not shown up so far with PB 5.41.
PureBasic 5.41 LTS | Xubuntu 16.04 (x32) | Windows 7 (x64)
Post Reply