A few weeks ago Andreia (a Portuguese like me :)) published in her blog about the latest improvements she has made towards a MWF WebBrowser control. I’m sure that, like me, many people become excited about this and gave it a try. In my case, I trust people when they say something is working on the Mono tree as long as its on Linux systems, so I wanted to test it on Windows and Andreia told me it was entirely possible. So hands to work and here is my progress so far, a bit reordered from my original path… for your sanity.
First step is to read a bit about Mozilla Build Prerequisites and in my case, because I already had Visual Studio .Net 2003 with C#/C++ support and Visual Studio .Net 2005 with C#, was to get the pre-built Win Tools. You need to extract it somewhere and run one of the “start-*.bat” files according to your main Visual Studio. I did nothing more than that, it is suggested by the Mozilla Build Prerequisites page that you setup a variable and install it, I didn’t do it and it worked fine.
Next step is to get the mozembed Mono code. It can be obtained in several ways, including the Subversion Mono repository. Please note that is you do use Subversion to get the code you might have some problems, I use TortoiseSVN subversion and the “_svn” feature but maybe it would happen with “.svn” too, so I needed to delete the _svn folders from inside the mozembed/build directory – the correct thing to do is to checkout the code to any place in your system and then export it. Another noteworthy thing is that you need to export/place the mozembed in a path without spaces or special chars, something like c:\mozembed, because Mozilla builds on windows don’t handle spaces in paths correctly.
Once you’ve your mozembed tree the first step is to prepare some headers and extra files. This is done using the shell provided by the Mozilla Win Tools package. Just “cd c:\mozembed\build”. You need to get xulrunner and xulrunner sdk files extracted here, and for that there is a script called get-gecko-sdk.sh in the build folder that won’t work on Windows. Instead go to this Mozilla ftp folder, download and extract win32/xulrunner-22.214.171.124.en-US.win32.zip and sdk/xulrunner-126.96.36.199.en-US.win32.sdk.zip files into your mozembed/build folder. You also need libIDL-0.6.dll and glib-1.2.dll in that folder, these files can be found in the Mozilla Win Tools mozilla-build\moztools-180compat\bin folder. After that you just do “make” in the shell and all the headers shall be created and places in the correct folders.
Now its time to open the solution, mozembed/xulbrowser/xulbrowser.sln or the 2k5 version according to your Visual Studio version. In my case I removed the other projects from the solution until all I had left was the xulbrowser project. It also happens that the current (2003) solution lacks some files, so after the solution is open you need to “Add existing file..” and add this files: SecurityWarningDialog.cpp PromptService.cpp LocationProvider.cpp SecurityWarningDialog.h and PromptService.hLocationProvider.h; all are in the xulbrowser project directory.
Next, you open the project properties “C/C++ > Include Directories” and add C:\mozembed\build\gecko-sdk\include and C:\mozembed\build\include. The same goes to “Linker > Additional Library Directories” where you need to add c:\mozembed\build\lib. Finally your project should be ready to build except for a missing file that I really don’t understand where it is supposed to come from… so I just googled for “nsAppDirectoryServiceDefs.h” and downloaded the first that I saw. Place it in one of the include paths.
You should now have a complete xulbrowser.dll in the project folder.
Creating a Test application
Now, we need to get Mono.Mozilla, in my case from the Subversion Mono repository. Just open and build it, the only problem I came across was the Mono Assembly key that I removed. After this we’ve most of whats needed to run a Mozilla web browser in our applications, but we still lack a control implementation to be used. As I’m very lazy I just shortcutted it and got the Mono one from here. What we need is WebBrowser.cs and WebBrowserBase.cs.
I created a new Windows.Forms project in Visual Studio and added the 2 files (WebBrowser.cs and WebBrowserBase.cs) to it. The files obviously won’t build out-the-box, you need to remove or deactivate those MonoTODO attributes (a find and replace of “[MonoTODO” by “//[MonoTODO”) and change the namespace to something else, in my case I just appended a “1” to it leaving me with System.Windows.Forms1.
I then edited the form Visual Studio generated when creating the project and added a WebBrowser control from the toolbox and edited the URL property to point to “http://whatsmyuseragent.com/”. After that all I needed was to edit the code and change the namespace of the control to the new namespace assigned to the WebBrowser.cs implementation.
Lastly, your application should build but not run correctly. You need to place xulbrowser.dll next to the .exe of your application, but that leads to an ungly memory exception. There are some files missing and I’m not sure which files, but copying the .exe, Mono.Browser.dll and xulbrowser.dll to the c:\mozembed/build/xulrunner folder leaves you with a running application.
This is a nice result as long as it can be seen in the screenshot but the truth is that its not working except for clicks and selecting text. I can’t input any text to textboxes or anything else. But hey! At least its mozilla rendering it! Now all we need is bug fixing 😉
This is of course a fast and hackish way of getting things to work, but I hope its a start and soon we can have something really usefull to developers who whish to use Mozilla from Windows.Forms applications.