[Mono-osx] Using native Cocoa WebKit for System.Windows.Forms.WebBrowser on Mac OS X

Alex Shulgin alexander.shulgin at yessoftware.com
Wed Jun 17 09:42:40 EDT 2009


Recently I've been struggling to bring up native (Cocoa-based) WebKit as 
WinForms WebBrowser control on OS X.

The approach I've used works to some degree, but has problems for which 
I need your help to solve.

Here's what I've come up with:

* I've found out that there's an API to integrate Cocoa views into 
Carbon views (HICocoaViewCreate, HICocoaViewSetView et al).

For this to work with WinForms we'd need to subclass 
"com.apple.HICocoaView", otherwise system events won't be translated to 
WinForms messages.

I have taken a shortcut and added new internal field to CreateParams 
class and used it in CreateWindow to select special subclass name.

To support this on the WebBrowser control side, I've patched 
WebBrowserBase.cs to use CreateParams.internal_param when running on Mac OS.

This might look a bit clumsy, but it works.  See hicocoaview-subclass.patch.

* After that I've noticed that the actual browser engine to use is 
chosen in Mono.WebBrowser/Manager.cs.  I've patched it to use alternate 
implementation on Mac OS by default.  See Mono.WebBrowser.Manager.patch.

* Finally, I've added mono-cocoa-webkit.dll modeled after 
mono-webkit.dll to provide minimum required functionality: create WebKit 
& navigate to URL.

I've used Monobjc bridge to create WebKit instance for simplicity: 

See attached sources archive for details.  You can use make; sudo make 
install.  Also, you'll need to install Monobjc dlls to gac too for this 
to work:

e.g. in ~/src/Monobjc-2.0.357.0/dist/2.0/
$ sudo gacutil -i Monobjc.dll
$ sudo gacutil -i Monobjc.Cocoa.dll
$ sudo gacutil -i Monobjc.WebKit.dll

With all this I was able to use WinForms WebBrowser with native WebKit 
sitting inside it. :) But there are some problems. :(

With this approach the WebBrowser control is grabbing focus after a 
click on it and extra click on some other control is required to make it 
lose the focus.

Sample code to test is attached in SimpleBrowser.zip.

I've compiled System.Windows.Forms.dll with most of debugging macros 
defined, and it all boils down to the fact that during the click 
WebBrowser control is receiving button down event, but no button up.

The relevant part of debugging info is like this:

NativeWindow.cs: Message WM_MOUSEFIRST, result 0
CarbonInternal.EventHandler: callref=BFFFED20, eventref=9D9C80, 
handle=9BD070, klass=1836021107, kind=1
CarbonInternal.MouseHandler: callref=BFFFED20, eventref=9D9C80, 
handle=9BD070, kind=1
NativeWindow.cs (10227120, WM_LBUTTONDOWN, 1, 7405631): result 0
Control 'System.Windows.Forms.WebBrowser' (9C0DB0) received message 
msg=0x201 (WM_LBUTTONDOWN) hwnd=0x9c0db0 wparam=0x1 lparam=0x71003f 
IsEnabled('System.Windows.Forms.WebBrowser' (9C0DB0)): Called, Result=True
set_Capture: 'System.Windows.Forms.WebBrowser' (9C0DB0) value=True 
is_captured=False IsHandleCreated=True
GrabWindow('System.Windows.Forms.WebBrowser' (9C0DB0), '' (0)): Called
NativeWindow.cs: Message WM_LBUTTONDOWN, result 0

Moreover, after the click inside the browser it seems that it stops 
receiving Carbon events altogether, so there's no chance to translate 
them into WinForms messages...

So, basically I'm stuck here.  Any pointers & suggestions are much 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: mono-cocoa-webkit-0.1.0.tar.gz
Type: application/x-gzip
Size: 3237 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-osx/attachments/20090617/9bb2dda5/attachment.gz 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Mono.WebBrowser.Manager.patch
Type: text/x-patch
Size: 1684 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-osx/attachments/20090617/9bb2dda5/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SimpleBrowser.zip
Type: application/zip
Size: 2717 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-osx/attachments/20090617/9bb2dda5/attachment.zip 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: hicocoaview-subclass.patch
Type: text/x-patch
Size: 3688 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-osx/attachments/20090617/9bb2dda5/attachment-0001.bin 

More information about the Mono-osx mailing list