Make Gecko objects hold on to the System.Windows.Form that they're drawing in to hide HWND extraction code from embedders. Rely more on exceptions for error propagation. Not part of the build.

git-svn-id: svn://10.0.0.236/trunk@142812 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
jst%netscape.com 2003-05-23 10:13:09 +00:00
parent a2edefd4d6
commit 65b0893deb
3 changed files with 59 additions and 53 deletions

View File

@ -71,7 +71,7 @@ namespace MSDotNETCSEmbed
//
// TODO: Add any constructor code after InitializeComponent call
//
myGecko = new Gecko();
myGecko = new Gecko(this);
}
/// <summary>
@ -116,9 +116,6 @@ namespace MSDotNETCSEmbed
[STAThread]
static void Main()
{
// initialize gecko
Gecko.InitEmbedding();
Application.Run(new MSDotNETCSEmbedForm());
// terminate gecko
@ -128,14 +125,14 @@ namespace MSDotNETCSEmbed
private void MSDotNETCSEmbedForm_Load(object sender, System.EventArgs e)
{
myURL = "www.mozilla.org";
myGecko.OpenURL(this.Handle, myURL);
myGecko.OpenURL(myURL);
this.Text = "MSDotNETCSEmbed [UNSUPPORTED] - " + myURL;
myURL = "";
}
private void MSDotNETCSEmbedForm_Resize(object sender, System.EventArgs e)
{
myGecko.Resize(this.Handle);
myGecko.Resize();
}
private void MSDotNETCSEmbedForm_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
@ -143,7 +140,7 @@ namespace MSDotNETCSEmbed
switch (e.KeyChar)
{
case '\r':
myGecko.OpenURL(this.Handle, myURL);
myGecko.OpenURL(myURL);
myURL = "";
break;

View File

@ -37,7 +37,6 @@
*
* ***** END LICENSE BLOCK ***** */
#using <mscorlib.dll>
#include <vcclr.h>
#include "nsCOMPtr.h"
#include "nsIWebBrowser.h"
@ -52,9 +51,6 @@
#include "DotNETEmbed.h"
#include "umWebChrome.h"
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace Mozilla::Embedding;
// Silly class for holding on to a static UTF8Encoding so that we
@ -111,26 +107,23 @@ Mozilla::Embedding::ThrowIfFailed(nsresult rv)
#define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
bool ResizeEmbedding(HWND hWnd, nsIWebBrowserChrome* chrome);
bool OpenWebPage(HWND hWnd, const wchar_t* url);
nsresult CreateBrowserWindow(HWND hWnd, PRUint32 aChromeFlags,
nsIWebBrowserChrome *aParent,
nsIWebBrowserChrome **aNewWindow);
nsCOMPtr<nsIWebBrowserChrome> chrome;
bool
void
InitializeEmbedding()
{
nsresult rv = NS_InitEmbedding(nsnull, nsnull);
NS_ASSERTION(NS_SUCCEEDED(rv), "Could not Initialize Gecko Engine");
return (NS_OK == rv) ? true : false;
ThrowIfFailed(rv);
}
bool
void
TerminateEmbedding()
{
nsresult rv = NS_TermEmbedding();
NS_ASSERTION(NS_SUCCEEDED(rv), "Could not Terminate Gecko Engine");
return (NS_OK == rv) ? true : false;
ThrowIfFailed(rv);
}
nsresult
@ -168,34 +161,26 @@ CreateBrowserWindow(HWND hWnd, PRUint32 aChromeFlags,
return NS_OK;
}
bool
nsresult
OpenWebPage(HWND hWnd, const wchar_t *url)
{
nsresult rv;
if (!chrome)
{
CreateBrowserWindow(hWnd, nsIWebBrowserChrome::CHROME_ALL,
nsnull, getter_AddRefs(chrome));
nsresult rv = CreateBrowserWindow(hWnd, nsIWebBrowserChrome::CHROME_ALL,
nsnull, getter_AddRefs(chrome));
NS_ENSURE_SUCCESS(rv, rv);
}
if (chrome)
{
SetWindowLong(hWnd, GWL_USERDATA,
(LONG)NS_STATIC_CAST(nsIWebBrowserChrome*, chrome));
SetWindowLong(hWnd, GWL_USERDATA,
(LONG)NS_STATIC_CAST(nsIWebBrowserChrome*, chrome));
// Start loading a page
nsCOMPtr<nsIWebBrowser> newBrowser;
chrome->GetWebBrowser(getter_AddRefs(newBrowser));
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(newBrowser));
// Start loading a page
nsCOMPtr<nsIWebBrowser> newBrowser;
chrome->GetWebBrowser(getter_AddRefs(newBrowser));
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(newBrowser));
rv = webNav->LoadURI(url, nsIWebNavigation::LOAD_FLAGS_NONE, nsnull,
return webNav->LoadURI(url, nsIWebNavigation::LOAD_FLAGS_NONE, nsnull,
nsnull, nsnull);
return NS_SUCCEEDED(rv);
}
return false;
}
bool
@ -433,28 +418,41 @@ WebBrowserChrome::CreateBrowser(HWND hWnd, PRInt32 aX, PRInt32 aY, PRInt32 aCX,
}
#pragma managed
bool
Gecko::InitEmbedding()
Gecko::Gecko(Form *aForm)
: mForm(aForm)
{
return InitializeEmbedding();
if (!sIsInitialized) {
InitializeEmbedding();
sIsInitialized = true;
}
}
bool
void
Gecko::TermEmbedding()
{
return TerminateEmbedding();
if (!sIsInitialized) {
return;
}
sIsInitialized = false;
TerminateEmbedding();
}
bool
Gecko::OpenURL(IntPtr hWnd, String *url)
void
Gecko::OpenURL(String *url)
{
const wchar_t __pin * pURL = PtrToStringChars(url);
return OpenWebPage((HWND)hWnd.ToInt32(), pURL);
nsresult rv = OpenWebPage((HWND)mForm->Handle.ToInt32(), pURL);
ThrowIfFailed(rv);
}
bool
Gecko::Resize(IntPtr hWnd)
void
Gecko::Resize()
{
return ResizeEmbedding((HWND)hWnd.ToInt32(), NULL);
ResizeEmbedding((HWND)mForm->Handle.ToInt32(), NULL);
}

View File

@ -39,7 +39,12 @@
#include "nsString.h"
#using <mscorlib.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>
using namespace System;
using namespace System::Windows::Forms;
namespace Mozilla
{
@ -47,12 +52,18 @@ namespace Mozilla
{
public __gc class Gecko
{
public:
static bool InitEmbedding();
static bool TermEmbedding();
public:
Gecko(Form *Form);
bool OpenURL(IntPtr hWnd, String *url);
bool Resize(IntPtr hWnd);
static void TermEmbedding();
void OpenURL(String *url);
void Resize();
private:
Form *mForm;
static bool sIsInitialized = false;
}; // class Gecko
// Throw an exception if NS_FAILED(rv)