Back to index

lightning-sunbird  0.9+nobinonly
layoutabstraction.cs
Go to the documentation of this file.
00001 /* -*- Mode: C#; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Manticore.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Silverstone Interactive.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Ben Goodger <ben@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 namespace Silverstone.Manticore.Layout
00041 {
00042   using System;
00043   using Microsoft.Win32;
00044   using System.Drawing;
00045   using System.ComponentModel;
00046   using System.Windows.Forms;
00047   using System.Runtime.InteropServices;
00048 
00049   // Trident
00050   using AxSHDocVw;
00051   
00052   // Gecko
00053   using AxMOZILLACONTROLLib;
00054   using MOZILLACONTROLLib;
00055 
00056   using Silverstone.Manticore.Core;
00057   using Silverstone.Manticore.Browser;
00058 
00059   public class WebBrowser : UserControl
00060   {
00061     protected AxWebBrowser trident;
00062     public AxWebBrowser Trident
00063     {
00064       get 
00065       {
00066         return trident;
00067       }
00068     }
00069 
00070     protected AxMozillaBrowser gecko;
00071     public AxMozillaBrowser Gecko
00072     {
00073       get 
00074       {
00075         return gecko;
00076       }
00077     }
00078 
00079     private BrowserWindow mBrowserWindow;
00080 
00081     private bool mBlankView = false;
00082 
00083     public WebBrowser(BrowserWindow aBrowserWindow)
00084     {
00085       mBrowserWindow = aBrowserWindow;
00086     }
00087 
00088     protected override void OnPaint(PaintEventArgs e)
00089     {
00090       // Draw an inset border around the client area for
00091       // blank documents. 
00092       Graphics g = e.Graphics;
00093 
00094       g.DrawLine(SystemPens.ControlDark, 
00095                  new Point(0, 0), 
00096                  new Point(this.Width, 0));
00097       g.DrawLine(SystemPens.ControlDark,
00098                  new Point(0, 0),
00099                  new Point(0, this.Height));
00100       g.DrawLine(SystemPens.ControlDarkDark, 
00101                  new Point(1, 1),
00102                  new Point(1 + (this.Width - 1), 1));
00103       g.DrawLine(SystemPens.ControlDarkDark,
00104                  new Point(1, 1),
00105                  new Point(1, 1 + (this.Height - 1)));
00106       g.DrawLine(SystemPens.ControlLightLight, 
00107                  new Point(this.Width - 1, 0),
00108                  new Point(this.Width - 1, this.Height));
00109       g.DrawLine(SystemPens.ControlLightLight,
00110                  new Point(0, this.Height - 1),
00111                  new Point(this.Width, this.Height - 1));
00112       g.DrawLine(SystemPens.ControlLight,
00113                  new Point(this.Width - 2, 1),
00114                  new Point(this.Width - 2, this.Height - 2));
00115       g.DrawLine(SystemPens.ControlLight,
00116                  new Point(1, this.Height - 2),
00117                  new Point(this.Width - 2, this.Height - 2));
00118     }
00119 
00120     protected override void OnResize(EventArgs e)
00121     {
00122       if (mBlankView)
00123         Invalidate();
00124       if (gecko != null)
00125         gecko.Size = new Size(this.ClientSize.Width, this.ClientSize.Height);
00126       else if (trident != null)
00127         trident.Size = new Size(this.ClientSize.Width, this.ClientSize.Height);
00128     }
00129 
00130     public void RealizeLayoutEngine()
00131     {
00132       if (gecko == null && trident == null) 
00133       {
00134         String layoutEngine = ServiceManager.Preferences.GetStringPref("browser.layoutengine");
00135         if (layoutEngine == "") 
00136           layoutEngine = "trident";
00137         SwitchLayoutEngine(layoutEngine);
00138       }
00139     }
00140 
00141     public void SwitchLayoutEngine(String id)
00142     {
00143       AxHost host = null;
00144       String url = "about:blank";
00145       
00146       switch (id) {
00147       case "trident":
00148         if (gecko != null) {
00149           url = gecko.LocationURL;
00150           this.Controls.Remove(gecko as AxHost);
00151           gecko = null;
00152         }
00153 
00154         if (trident == null) {
00155           trident = new AxWebBrowser();
00156           host = trident as AxHost;
00157         }
00158 
00159         break;
00160       default:
00161         if (trident != null) {
00162           url = trident.LocationURL;
00163           this.Controls.Remove(trident as AxHost);
00164           trident = null;
00165         }
00166 
00167         if (gecko == null) {
00168           gecko = new AxMozillaBrowser(); 
00169           host = gecko as AxHost;
00170         }
00171         break;
00172       }
00173 
00174       if (host != null) {
00175         host.BeginInit();
00176         host.TabIndex = 1;
00177         host.Dock = DockStyle.Fill;
00178         host.EndInit();
00179         this.Controls.Add(host);
00180       }
00181       
00182       // Add appropriate content area listeners
00183       AddListeners();
00184 
00185       ServiceManager.Preferences.SetStringPref("browser.layoutengine", id);
00186 
00187       LoadURL(url, false);
00188     }
00189 
00190     public Object currentLayoutEngine
00191     {
00192       get {
00193         if (gecko != null) 
00194           return gecko;
00195         else if (trident != null)
00196           return trident;
00197         return null;
00198       }
00199     }
00200 
00201     public void LoadURL(String url, Boolean bypassCache)
00202     {
00203       if (!mBlankView)
00204         mBlankView = true;
00205 
00206       // XXX - neither IE nor Mozilla implement all of the
00207       //       load flags properly. Mozilla should at least be 
00208       //       made to support ignore-cache and ignore-history.
00209       //       This will require modification to the ActiveX
00210       //       control.
00211 
00212       RealizeLayoutEngine();
00213       Object o = null;
00214       if (gecko != null)
00215         gecko.Navigate(url, ref o, ref o, ref o, ref o);
00216       else if (trident != null)
00217         trident.Navigate(url, ref o, ref o, ref o, ref o);
00218     }
00219 
00220     public String URL
00221     {
00222       get {
00223         if (gecko != null)
00224           return gecko.LocationURL;
00225         else if (trident != null)
00226           return trident.LocationURL;
00227         return "";
00228       }
00229       set {
00230         LoadURL(value, false);
00231         // XXX why can't we return |value| here?
00232       }
00233     }
00234 
00235     public void RefreshPage() 
00236     {
00237       // XXX Should take a refresh level and use Refresh2.
00238       RealizeLayoutEngine();
00239       Object o = 0 as Object;
00240       if (gecko != null)
00241         gecko.Refresh2(ref o);
00242       else if (trident != null)
00243         trident.Refresh2(ref o);
00244     }
00245 
00246     public void Stop()
00247     {
00248       RealizeLayoutEngine();
00249       if (gecko != null)
00250         gecko.Stop();
00251       else if (trident != null)
00252         trident.Stop();
00253     }
00254 
00255     public void GoBack()
00256     {
00257       RealizeLayoutEngine();
00258       if (gecko != null)
00259         gecko.GoBack();
00260       else if (trident != null)
00261         trident.GoBack();
00262     }
00263 
00264     public void GoForward()
00265     {
00266       RealizeLayoutEngine();
00267       if (gecko != null)
00268         gecko.GoForward();
00269       else if (trident != null)
00270         trident.GoForward();
00271     }
00272 
00273     public void GoHome()
00274     {
00275       // XXX - need to implement "Home" preference
00276       String homepageURL = ServiceManager.Preferences.GetStringPref("browser.homepage");
00277       LoadURL(homepageURL, false);
00278     }
00279 
00280     private void AddListeners()
00281     {
00282       AddProgressListener();
00283       AddNavigateComplete2Listener();
00284       AddTitleChangeListener();
00285       AddStatusChangeListener();
00286       AddNewWindowListener();
00287     }
00288 
00289     private bool mProgressChangeGecko = false;
00290     private bool mProgressChangeTrident = false;
00291     private void AddProgressListener() 
00292     {
00293       if (gecko != null && !mProgressChangeGecko) {
00294         gecko.ProgressChange += new AxMOZILLACONTROLLib.DWebBrowserEvents2_ProgressChangeEventHandler(OnProgressGecko);
00295         mProgressChangeGecko = true;
00296       }
00297       else if (trident != null && !mProgressChangeTrident) {
00298         trident.ProgressChange += new AxSHDocVw.DWebBrowserEvents2_ProgressChangeEventHandler(OnProgressTrident);
00299         mProgressChangeTrident = true;
00300       }
00301     }
00302     public void OnProgressGecko(Object sender, AxMOZILLACONTROLLib.DWebBrowserEvents2_ProgressChangeEvent e) 
00303     {
00304       mBrowserWindow.OnProgress(e.progress, e.progressMax);
00305     }
00306     public void OnProgressTrident(Object sender, AxSHDocVw.DWebBrowserEvents2_ProgressChangeEvent e) 
00307     {
00308       mBrowserWindow.OnProgress(e.progress, e.progressMax);
00309     }
00310 
00311     private bool mNavigateComplete2Gecko = false;
00312     private bool mNavigateComplete2Trident = false;
00313     private void AddNavigateComplete2Listener() 
00314     {
00315       if (gecko != null && !mNavigateComplete2Gecko) 
00316       {
00317         gecko.NavigateComplete2 += new AxMOZILLACONTROLLib.DWebBrowserEvents2_NavigateComplete2EventHandler(OnNavigateComplete2Gecko);
00318         mNavigateComplete2Gecko = true;
00319       }
00320       else if (trident != null && !mNavigateComplete2Trident) 
00321       {
00322         trident.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(OnNavigateComplete2Trident);
00323         mNavigateComplete2Trident = true;
00324       }
00325     }
00326     public void OnNavigateComplete2Gecko(Object sender, AxMOZILLACONTROLLib.DWebBrowserEvents2_NavigateComplete2Event e) 
00327     {
00328       mBrowserWindow.OnNavigateComplete2(e.uRL as string);
00329     }
00330     public void OnNavigateComplete2Trident(Object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e) 
00331     {
00332       mBrowserWindow.OnNavigateComplete2(e.uRL as string);
00333     }
00334 
00335     private bool mTitleChangeGecko = false;
00336     private bool mTitleChangeTrident = false;
00337     private void AddTitleChangeListener()
00338     {
00339       if (gecko != null && !mTitleChangeGecko) {
00340         gecko.TitleChange += new AxMOZILLACONTROLLib.DWebBrowserEvents2_TitleChangeEventHandler(OnTitleChangeGecko);
00341         mTitleChangeGecko = true;
00342       }
00343       else if (trident != null && !mTitleChangeTrident) {
00344         trident.TitleChange += new AxSHDocVw.DWebBrowserEvents2_TitleChangeEventHandler(OnTitleChangeTrident);
00345         mTitleChangeTrident = true;
00346       }
00347     }
00348     public void OnTitleChangeGecko(Object sender, AxMOZILLACONTROLLib.DWebBrowserEvents2_TitleChangeEvent e)
00349     {
00350       mBrowserWindow.OnTitleChange(e.text);
00351     }
00352     public void OnTitleChangeTrident(Object sender, AxSHDocVw.DWebBrowserEvents2_TitleChangeEvent e)
00353     {
00354       mBrowserWindow.OnTitleChange(e.text);
00355     }
00356 
00357     private bool mStatusChangeGecko = false;
00358     private bool mStatusChangeTrident = false;
00359     private void AddStatusChangeListener()
00360     {
00361       if (gecko != null && !mStatusChangeGecko) {
00362         gecko.StatusTextChange += new AxMOZILLACONTROLLib.DWebBrowserEvents2_StatusTextChangeEventHandler(OnStatusChangeGecko);
00363         mStatusChangeGecko = true;
00364       }
00365       else if (trident != null && !mStatusChangeTrident) {
00366         trident.StatusTextChange += new AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEventHandler(OnStatusChangeTrident);
00367         mStatusChangeTrident = true;
00368       }
00369     }
00370     public void OnStatusChangeGecko(Object sender, AxMOZILLACONTROLLib.DWebBrowserEvents2_StatusTextChangeEvent e)
00371     {
00372       mBrowserWindow.OnStatusTextChange(e.text);
00373     }
00374     public void OnStatusChangeTrident(Object sender, AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEvent e)
00375     {
00376       mBrowserWindow.OnStatusTextChange(e.text);
00377     }
00378 
00379     private bool mNewWindowGecko = false;
00380     private bool mNewWindowTrident = false;
00381     private void AddNewWindowListener()
00382     {
00383       if (gecko != null && !mNewWindowGecko) {
00384         gecko.NewWindow2 += new AxMOZILLACONTROLLib.DWebBrowserEvents2_NewWindow2EventHandler(OnNewWindowGecko);
00385         mNewWindowGecko = true;
00386       }
00387       else if (trident != null && !mNewWindowTrident) {
00388         trident.NewWindow2 += new AxSHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(OnNewWindowTrident);
00389         mNewWindowTrident = true;
00390       }
00391     }
00392     public void OnNewWindowGecko(Object sender, AxMOZILLACONTROLLib.DWebBrowserEvents2_NewWindow2Event e)
00393     {
00394       bool allowPopups = ServiceManager.Preferences.GetBoolPref("browser.allowpopups");
00395       if (allowPopups) 
00396       {
00397         BrowserWindow window = new BrowserWindow();
00398         window.WebBrowser.RealizeLayoutEngine();
00399         window.Show();
00400         e.ppDisp = window.WebBrowser.Gecko;
00401       }
00402       else 
00403         e.cancel = true;
00404     }
00405     public void OnNewWindowTrident(Object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
00406     {
00407       bool allowPopups = ServiceManager.Preferences.GetBoolPref("browser.allowpopups");
00408       if (allowPopups) 
00409       {
00410         BrowserWindow window = new BrowserWindow();
00411 //        window.ShouldLoadHomePage = false;
00412         window.WebBrowser.RealizeLayoutEngine();
00413         window.Show();
00414         e.ppDisp = window.WebBrowser.Trident;
00415       }
00416       else 
00417         e.cancel = true;
00418     }
00419     
00420     private bool mFileDownloadGecko = false;
00421     private bool mFileDownloadTrident = false;
00422     private void AddFileDownloadListener()
00423     {
00424       if (gecko != null && !mFileDownloadGecko) 
00425       {
00426         mFileDownloadGecko = true;
00427       }
00428       else if (trident != null && !mFileDownloadTrident) 
00429       {
00430         trident.FileDownload += new AxSHDocVw.DWebBrowserEvents2_FileDownloadEventHandler(OnFileDownloadTrident);
00431         mFileDownloadTrident = true;
00432       }
00433     }
00434     public void OnFileDownloadTrident(Object sender, AxSHDocVw.DWebBrowserEvents2_FileDownloadEvent e)
00435     {
00436       e.cancel = false;
00437     }
00438   }
00439 }
00440