Back to index

lightning-sunbird  0.9+nobinonly
browserwindow.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.Browser
00041 {
00042   using System;
00043   using System.ComponentModel;
00044   using System.Drawing;
00045   using System.Windows.Forms;
00046   using System.Net;
00047 
00048   using Silverstone.Manticore.Core;
00049   using Silverstone.Manticore.App;
00050   using Silverstone.Manticore.Toolkit;
00051   using Silverstone.Manticore.Layout;
00052   using Silverstone.Manticore.Bookmarks;
00053 
00054   public class BrowserWindow : ManticoreWindow, IController
00055   {
00056     private MenuBuilder mMenuBuilder;
00057     private BrowserToolbarBuilder mToolbarBuilder;
00058 
00059     private LocationBar mLocationBar;
00060 
00061     private WebBrowser mWebBrowser;
00062     public WebBrowser WebBrowser
00063     {
00064       get 
00065       {
00066         return mWebBrowser;
00067       }
00068     }
00069 
00070     private StatusBar mStatusBar;
00071     private StatusBarPanel mProgressMeter;
00072     private StatusBarPanel mStatusPanel;
00073 
00074     private String mSessionURL = "";
00075 
00079     private String mTitle = "";
00080 
00081     public BrowserWindow()
00082     {
00083       Init();
00084     }
00085 
00086     public BrowserWindow(String aURL)
00087     {
00088       mSessionURL = aURL;
00089 
00090       Init();
00091     }
00092 
00093     protected void Init()
00094     {
00095       mType = "BrowserWindow";
00096 
00097       // Set up UI
00098       InitializeComponent();
00099 
00100       base.Init();
00101     }
00102 
00103     private void InitializeComponent()
00104     {
00105       // XXX read these from a settings file
00106       this.Width = 640;
00107       this.Height = 480;
00108       
00109       this.Text = "Manticore"; // XXX localize
00110 
00111       mMenuBuilder = new MenuBuilder("browser\\browser-menu.xml", this);
00112       mMenuBuilder.Build();
00113       mMenuBuilder.OnCommand += new EventHandler(OnMenuCommand);
00114 
00115       // Show the resize handle
00116       this.SizeGripStyle = SizeGripStyle.Auto;
00117 
00118       // Set up the Status Bar
00119       mStatusBar = new StatusBar();
00120       
00121       StatusBarPanel docStatePanel = new StatusBarPanel();
00122       mStatusPanel = new StatusBarPanel();
00123       mProgressMeter = new StatusBarPanel();
00124       StatusBarPanel zonePanel = new StatusBarPanel();
00125 
00126       docStatePanel.Text = "X";
00127       zonePanel.Text = "Internet Region";
00128       mStatusPanel.Text = "Document Done";
00129       mStatusPanel.AutoSize = StatusBarPanelAutoSize.Spring;
00130       
00131 
00132       mStatusBar.Panels.AddRange(new StatusBarPanel[] {docStatePanel, mStatusPanel, mProgressMeter, zonePanel});
00133       mStatusBar.ShowPanels = true;
00134       
00135       this.Controls.Add(mStatusBar);
00136 
00137       mToolbarBuilder = new BrowserToolbarBuilder("browser\\browser-toolbar.xml", this);
00138 
00139       mLocationBar = new LocationBar();
00140       mLocationBar.Top = mToolbarBuilder.Bounds.Top + mToolbarBuilder.Bounds.Height;
00141       mLocationBar.Left = 0;
00142       mLocationBar.Width = ClientRectangle.Width;
00143       mLocationBar.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
00144       mLocationBar.LocationBarCommit += new LocationBar.LocationBarEventHandler(OnLocationCommit);
00145       mLocationBar.LocationBarModified += new LocationBar.LocationBarEventHandler(OnLocationModified);
00146       this.Controls.Add(mLocationBar);
00147 
00148       mWebBrowser = new WebBrowser(this);
00149       mWebBrowser.Dock = DockStyle.Bottom;
00150       mWebBrowser.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
00151       mWebBrowser.Top = mLocationBar.Top + mLocationBar.Height;
00152       mWebBrowser.Width = ClientRectangle.Width;
00153       mWebBrowser.Height = ClientRectangle.Height - mWebBrowser.Top - mStatusBar.Height;
00154       this.Controls.Add(mWebBrowser);
00155 
00156       // Start Page handler
00157       this.VisibleChanged += new EventHandler(LoadStartPage);
00158     }
00159 
00161     // URL Loading
00162 
00166     public String URL 
00167     {
00168       get {
00169         return mWebBrowser.URL;
00170       }
00171     }
00172 
00173     public void LoadURL(String aURL)
00174     {
00175       mUpdatedURLBar = false;
00176       mWebBrowser.LoadURL(aURL, false);
00177     }
00178 
00179     protected bool mShouldLoadHomePage = true;
00180     public bool ShouldLoadHomePage
00181     {
00182       get 
00183       {
00184         return mShouldLoadHomePage;
00185       }
00186       set 
00187       {
00188         if (value != mShouldLoadHomePage)
00189           mShouldLoadHomePage = value;
00190       }
00191     }
00192     
00193     private void LoadStartPage(object sender, EventArgs e)
00194     {
00195       if (!mShouldLoadHomePage)
00196         return;
00197 
00198       int startMode = ServiceManager.Preferences.GetIntPref("browser.homepage.mode");
00199       switch (startMode) {
00200       case 0:
00201         // Don't initialize jack.
00202         break;
00203       case 1:
00204         // Load the homepage
00205         mWebBrowser.GoHome();
00206         break;
00207       case 2:
00208         // Load the session document.
00209         mWebBrowser.LoadURL(mSessionURL, false);
00210         break;
00211       }
00212     }
00213 
00215     // Location Bar
00216     protected void OnLocationCommit(Object aSender, LocationBarEventArgs aLbea)
00217     {
00218       string url = ServiceManager.Bookmarks.ResolveKeyword(aLbea.Text);
00219       if (url == "") 
00220         url = aLbea.Text;
00221 
00222       mUserTyped = false;
00223       LoadURL(url);
00224     }
00225 
00226     protected bool mUserTyped = false;
00227     protected bool mUpdatedURLBar = false;
00228     protected void OnLocationModified(Object aSender, LocationBarEventArgs aLbea)
00229     {
00230       mUserTyped = true;
00231     }
00232 
00234     // Window Creation
00235     public static BrowserWindow OpenBrowser()
00236     {
00237       BrowserWindow window = new BrowserWindow();
00238       window.Show();
00239       return window;
00240     }
00241 
00242     public static BrowserWindow OpenBrowserWithURL(String aURL)
00243     {
00244       BrowserWindow window = new BrowserWindow(aURL);
00245       window.Show();
00246       return window;
00247     }
00248 
00250     // Menu Command Handlers
00251 
00252     public void Open()
00253     {
00254       OpenDialog dlg = new OpenDialog();
00255       if (dlg.ShowDialog() == DialogResult.OK)
00256         mWebBrowser.LoadURL(dlg.URL, false);
00257     }
00258 
00259     public void SavePageAs()
00260     {
00261       SaveFileDialog dlg = new SaveFileDialog();
00262       dlg.AddExtension = true;
00263       dlg.InitialDirectory = FileLocator.GetFolderPath(FileLocator.SpecialFolders.ssfPERSONAL); // XXX persist this. 
00264 
00265       // XXX I want to point out that this is a really lame hack. We need
00266       //     a URL parser (not a URI parser, a URL parser). 
00267       string name = mTitle.Replace("\"", "'");
00268       name = name.Replace("*", " ");
00269       name = name.Replace(":", " ");
00270       name = name.Replace("?", " ");
00271       name = name.Replace("<", "(");
00272       name = name.Replace(">", ")");
00273       name = name.Replace("\\", "-");
00274       name = name.Replace("/", "-");
00275       name = name.Replace("|", "-");
00276       dlg.FileName = name;
00277 
00278       dlg.Title = "Save Page As...";
00279       dlg.ValidateNames = true;
00280       dlg.OverwritePrompt = true;
00281 
00282       WebRequest req = WebRequest.Create(mWebBrowser.URL);
00283       WebResponse resp = req.GetResponse();
00284       string contentType = resp.ContentType;
00285       switch (contentType) 
00286       {
00287         case "text/html":
00288         case "text/xhtml":
00289           dlg.Filter = "Web Page, complete (*.htm;*.html)|*.htm*|Web Page, HTML only (*.htm;*.html)|*.htm*|Text only (*.txt)|*.txt";
00290           dlg.DefaultExt = "html";
00291           break;
00292         default:
00293           string extension = MIMEService.GetExtensionForMIMEType(contentType);
00294           string description = MIMEService.GetDescriptionForMIMEType(contentType);
00295           description += " (*" + extension + ")";
00296           dlg.Filter = description + "|*" + extension + "|All Files (*.*)|*.*";
00297           dlg.DefaultExt = extension.Substring(1, extension.Length-1);
00298           break;
00299       }
00300 
00301       dlg.FileOk += new CancelEventHandler(OnSavePageAsOK);
00302       dlg.ShowDialog();
00303     }
00304 
00305     public void OnSavePageAsOK(Object sender, CancelEventArgs e)
00306     {
00307       if (e.Cancel != true) 
00308       {
00309         SaveFileDialog dlg = sender as SaveFileDialog;
00310         Console.WriteLine("{0}", dlg.FileName);
00311       }
00312     }
00313 
00314     public void Quit() 
00315     {
00316       ManticoreApp app = ServiceManager.App;
00317       app.Quit();
00318     }
00319 
00320     public Object currentLayoutEngine
00321     {
00322       get {
00323         return mWebBrowser.currentLayoutEngine;
00324       }
00325     }
00326 
00327     private int previousProgress = 0;
00328     public void OnProgress(int aProgress, int aProgressMax) 
00329     {
00330       if (aProgress > 0 && aProgress > previousProgress) {
00331         int percentage = (int) (aProgress / aProgressMax);
00332         String text = percentage + "% complete";
00333         mProgressMeter.Text = text;
00334       }
00335       // XXX we really would rather set this in BeforeNavigate2, but we
00336       //     can't get that event to fire for some reason. 
00337       if (mUpdatedURLBar) 
00338         mUpdatedURLBar = false;
00339     }
00340 
00341     // XXX we probably will need to extend this to take as a parameter
00342     //     more data from the NavigateComplete event
00343     public void OnNavigateComplete2(string aURL)
00344     {
00345       if (!mUpdatedURLBar && !mUserTyped) 
00346       {
00347         mLocationBar.Text = aURL;
00348         mUpdatedURLBar = true;
00349       }
00350     }
00351 
00352     public void OnTitleChange(String aTitle)
00353     {
00354       mTitle = aTitle;
00355       this.Text = (aTitle == "about:blank") ? "No page to display" : aTitle;
00356     }
00357 
00358     public void OnStatusTextChange(String aStatusText)
00359     {
00360       mStatusPanel.Text = aStatusText;
00361     }
00362 
00368     public void OnMenuCommand(Object sender, EventArgs e)
00369     {
00370       ManticoreMenuItem item = sender as ManticoreMenuItem;
00371       DoCommand(item.Command, item.Data);
00372     }
00373 
00375     // IController Implementation
00376 
00377     public void DoCommand(String aCommand)
00378     {
00379       DoCommand(aCommand, null);
00380     }
00381 
00382     public void DoCommand(String aCommand, Object aData) 
00383     {
00384       Console.WriteLine(aCommand);
00385       switch (aCommand) 
00386       {
00387         case "file-new-window":
00388           OpenBrowser();
00389           break;
00390         case "file-open":
00391           Open();
00392           break;
00393         case "file-save-as":
00394           SavePageAs();
00395           break;
00396         case "file-save-form":
00397           TestForm frm = new TestForm();
00398           frm.Show();
00399           break;
00400         case "file-exit":
00401           Quit();
00402           break;
00403         case "view-statusbar":
00404           if (mStatusBar.Visible)
00405             mStatusBar.Hide();
00406           else
00407             mStatusBar.Show();
00408           break;
00409         case "view-go-back":
00410           mWebBrowser.GoBack();
00411           break;
00412         case "view-go-forward":
00413           mWebBrowser.GoForward();
00414           break;
00415         case "view-go-home":
00416           mWebBrowser.GoHome();
00417           break;
00418         case "view-reload":
00419           mWebBrowser.RefreshPage();
00420           break;
00421         case "view-stop":
00422           mWebBrowser.Stop();
00423           break;
00424         case "view-layout-gecko":
00425           mWebBrowser.SwitchLayoutEngine("gecko");
00426           break;
00427         case "view-layout-ie":
00428           mWebBrowser.SwitchLayoutEngine("trident");
00429           break;
00430         case "bookmarks-manage":
00431           BookmarksWindow bm = new BookmarksWindow();
00432           bm.Show();
00433           break;
00434         case "bookmarks-item":
00435           String url = ServiceManager.Bookmarks.GetBookmarkAttribute(aData as String, "url");
00436           LoadURL(url);
00437           break;
00438         case "bookmarks-add":
00439           // XXX need to allow user to customize this. 
00440           Bookmarks bmks = ServiceManager.Bookmarks;
00441           String bookmarkID = bmks.CreateBookmark(mTitle, "Bookmarks", -1);
00442           bmks.SetBookmarkAttribute(bookmarkID, "url", URL);
00443           break;
00444         case "bookmarks-file":
00445           // XXX work on this
00446           FileBookmark fpWindow = new FileBookmark(URL, mTitle);
00447           fpWindow.ShowDialog();
00448           break;
00449         case "help-about":
00450           AboutDialog aboutDialog = new AboutDialog(this);
00451           aboutDialog.ShowDialog();
00452           break;
00453         case "tools-options":
00454           PrefsDialog prefsDialog = new PrefsDialog(this);
00455           prefsDialog.ShowDialog();
00456           break;
00457       }
00458     }
00459 
00460     public bool SupportsCommand(String aCommand)
00461     {
00462       // XXX implement me
00463       return true;
00464     }
00465 
00466     public bool IsCommandEnabled(String aCommand)
00467     {
00468       // XXX implement me
00469       return true;
00470     }
00471   }
00472 
00473   public class BrowserToolbarBuilder : ToolbarBuilder
00474   {
00475     public BrowserToolbarBuilder(String aFile, Form aForm) : base(aFile, aForm)
00476     {
00477     }
00478     
00479     public override void OnCommand(Object sender, ToolBarButtonClickEventArgs e)
00480     {
00481       CommandButtonItem item = e.Button as CommandButtonItem;
00482       (mForm as BrowserWindow).DoCommand(item.Command);
00483     }
00484   }
00485 }
00486 
00487