Import from 1.9a8 tarball
[mozilla-extra.git] / extensions / manticore / browser / browserwindow.cs
blob2e786fc22abb6a5dfbf1d13b53527c79e9ec2d7c
1 /* -*- Mode: C#; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Manticore.
18 * The Initial Developer of the Original Code is
19 * Silverstone Interactive.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Ben Goodger <ben@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 namespace Silverstone.Manticore.Browser
42 using System;
43 using System.ComponentModel;
44 using System.Drawing;
45 using System.Windows.Forms;
46 using System.Net;
48 using Silverstone.Manticore.Core;
49 using Silverstone.Manticore.App;
50 using Silverstone.Manticore.Toolkit;
51 using Silverstone.Manticore.Layout;
52 using Silverstone.Manticore.Bookmarks;
54 public class BrowserWindow : ManticoreWindow, IController
56 private MenuBuilder mMenuBuilder;
57 private BrowserToolbarBuilder mToolbarBuilder;
59 private LocationBar mLocationBar;
61 private WebBrowser mWebBrowser;
62 public WebBrowser WebBrowser
64 get
66 return mWebBrowser;
70 private StatusBar mStatusBar;
71 private StatusBarPanel mProgressMeter;
72 private StatusBarPanel mStatusPanel;
74 private String mSessionURL = "";
76 /// <summary>
77 /// Webpage Title
78 /// </summary>
79 private String mTitle = "";
81 public BrowserWindow()
83 Init();
86 public BrowserWindow(String aURL)
88 mSessionURL = aURL;
90 Init();
93 protected void Init()
95 mType = "BrowserWindow";
97 // Set up UI
98 InitializeComponent();
100 base.Init();
103 private void InitializeComponent()
105 // XXX read these from a settings file
106 this.Width = 640;
107 this.Height = 480;
109 this.Text = "Manticore"; // XXX localize
111 mMenuBuilder = new MenuBuilder("browser\\browser-menu.xml", this);
112 mMenuBuilder.Build();
113 mMenuBuilder.OnCommand += new EventHandler(OnMenuCommand);
115 // Show the resize handle
116 this.SizeGripStyle = SizeGripStyle.Auto;
118 // Set up the Status Bar
119 mStatusBar = new StatusBar();
121 StatusBarPanel docStatePanel = new StatusBarPanel();
122 mStatusPanel = new StatusBarPanel();
123 mProgressMeter = new StatusBarPanel();
124 StatusBarPanel zonePanel = new StatusBarPanel();
126 docStatePanel.Text = "X";
127 zonePanel.Text = "Internet Region";
128 mStatusPanel.Text = "Document Done";
129 mStatusPanel.AutoSize = StatusBarPanelAutoSize.Spring;
132 mStatusBar.Panels.AddRange(new StatusBarPanel[] {docStatePanel, mStatusPanel, mProgressMeter, zonePanel});
133 mStatusBar.ShowPanels = true;
135 this.Controls.Add(mStatusBar);
137 mToolbarBuilder = new BrowserToolbarBuilder("browser\\browser-toolbar.xml", this);
139 mLocationBar = new LocationBar();
140 mLocationBar.Top = mToolbarBuilder.Bounds.Top + mToolbarBuilder.Bounds.Height;
141 mLocationBar.Left = 0;
142 mLocationBar.Width = ClientRectangle.Width;
143 mLocationBar.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
144 mLocationBar.LocationBarCommit += new LocationBar.LocationBarEventHandler(OnLocationCommit);
145 mLocationBar.LocationBarModified += new LocationBar.LocationBarEventHandler(OnLocationModified);
146 this.Controls.Add(mLocationBar);
148 mWebBrowser = new WebBrowser(this);
149 mWebBrowser.Dock = DockStyle.Bottom;
150 mWebBrowser.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
151 mWebBrowser.Top = mLocationBar.Top + mLocationBar.Height;
152 mWebBrowser.Width = ClientRectangle.Width;
153 mWebBrowser.Height = ClientRectangle.Height - mWebBrowser.Top - mStatusBar.Height;
154 this.Controls.Add(mWebBrowser);
156 // Start Page handler
157 this.VisibleChanged += new EventHandler(LoadStartPage);
160 ///////////////////////////////////////////////////////////////////////////
161 // URL Loading
163 /// <summary>
164 /// The currently loaded document's URL.
165 /// </summary>
166 public String URL
168 get {
169 return mWebBrowser.URL;
173 public void LoadURL(String aURL)
175 mUpdatedURLBar = false;
176 mWebBrowser.LoadURL(aURL, false);
179 protected bool mShouldLoadHomePage = true;
180 public bool ShouldLoadHomePage
182 get
184 return mShouldLoadHomePage;
186 set
188 if (value != mShouldLoadHomePage)
189 mShouldLoadHomePage = value;
193 private void LoadStartPage(object sender, EventArgs e)
195 if (!mShouldLoadHomePage)
196 return;
198 int startMode = ServiceManager.Preferences.GetIntPref("browser.homepage.mode");
199 switch (startMode) {
200 case 0:
201 // Don't initialize jack.
202 break;
203 case 1:
204 // Load the homepage
205 mWebBrowser.GoHome();
206 break;
207 case 2:
208 // Load the session document.
209 mWebBrowser.LoadURL(mSessionURL, false);
210 break;
214 ///////////////////////////////////////////////////////////////////////////
215 // Location Bar
216 protected void OnLocationCommit(Object aSender, LocationBarEventArgs aLbea)
218 string url = ServiceManager.Bookmarks.ResolveKeyword(aLbea.Text);
219 if (url == "")
220 url = aLbea.Text;
222 mUserTyped = false;
223 LoadURL(url);
226 protected bool mUserTyped = false;
227 protected bool mUpdatedURLBar = false;
228 protected void OnLocationModified(Object aSender, LocationBarEventArgs aLbea)
230 mUserTyped = true;
233 ///////////////////////////////////////////////////////////////////////////
234 // Window Creation
235 public static BrowserWindow OpenBrowser()
237 BrowserWindow window = new BrowserWindow();
238 window.Show();
239 return window;
242 public static BrowserWindow OpenBrowserWithURL(String aURL)
244 BrowserWindow window = new BrowserWindow(aURL);
245 window.Show();
246 return window;
249 ///////////////////////////////////////////////////////////////////////////
250 // Menu Command Handlers
252 public void Open()
254 OpenDialog dlg = new OpenDialog();
255 if (dlg.ShowDialog() == DialogResult.OK)
256 mWebBrowser.LoadURL(dlg.URL, false);
259 public void SavePageAs()
261 SaveFileDialog dlg = new SaveFileDialog();
262 dlg.AddExtension = true;
263 dlg.InitialDirectory = FileLocator.GetFolderPath(FileLocator.SpecialFolders.ssfPERSONAL); // XXX persist this.
265 // XXX I want to point out that this is a really lame hack. We need
266 // a URL parser (not a URI parser, a URL parser).
267 string name = mTitle.Replace("\"", "'");
268 name = name.Replace("*", " ");
269 name = name.Replace(":", " ");
270 name = name.Replace("?", " ");
271 name = name.Replace("<", "(");
272 name = name.Replace(">", ")");
273 name = name.Replace("\\", "-");
274 name = name.Replace("/", "-");
275 name = name.Replace("|", "-");
276 dlg.FileName = name;
278 dlg.Title = "Save Page As...";
279 dlg.ValidateNames = true;
280 dlg.OverwritePrompt = true;
282 WebRequest req = WebRequest.Create(mWebBrowser.URL);
283 WebResponse resp = req.GetResponse();
284 string contentType = resp.ContentType;
285 switch (contentType)
287 case "text/html":
288 case "text/xhtml":
289 dlg.Filter = "Web Page, complete (*.htm;*.html)|*.htm*|Web Page, HTML only (*.htm;*.html)|*.htm*|Text only (*.txt)|*.txt";
290 dlg.DefaultExt = "html";
291 break;
292 default:
293 string extension = MIMEService.GetExtensionForMIMEType(contentType);
294 string description = MIMEService.GetDescriptionForMIMEType(contentType);
295 description += " (*" + extension + ")";
296 dlg.Filter = description + "|*" + extension + "|All Files (*.*)|*.*";
297 dlg.DefaultExt = extension.Substring(1, extension.Length-1);
298 break;
301 dlg.FileOk += new CancelEventHandler(OnSavePageAsOK);
302 dlg.ShowDialog();
305 public void OnSavePageAsOK(Object sender, CancelEventArgs e)
307 if (e.Cancel != true)
309 SaveFileDialog dlg = sender as SaveFileDialog;
310 Console.WriteLine("{0}", dlg.FileName);
314 public void Quit()
316 ManticoreApp app = ServiceManager.App;
317 app.Quit();
320 public Object currentLayoutEngine
322 get {
323 return mWebBrowser.currentLayoutEngine;
327 private int previousProgress = 0;
328 public void OnProgress(int aProgress, int aProgressMax)
330 if (aProgress > 0 && aProgress > previousProgress) {
331 int percentage = (int) (aProgress / aProgressMax);
332 String text = percentage + "% complete";
333 mProgressMeter.Text = text;
335 // XXX we really would rather set this in BeforeNavigate2, but we
336 // can't get that event to fire for some reason.
337 if (mUpdatedURLBar)
338 mUpdatedURLBar = false;
341 // XXX we probably will need to extend this to take as a parameter
342 // more data from the NavigateComplete event
343 public void OnNavigateComplete2(string aURL)
345 if (!mUpdatedURLBar && !mUserTyped)
347 mLocationBar.Text = aURL;
348 mUpdatedURLBar = true;
352 public void OnTitleChange(String aTitle)
354 mTitle = aTitle;
355 this.Text = (aTitle == "about:blank") ? "No page to display" : aTitle;
358 public void OnStatusTextChange(String aStatusText)
360 mStatusPanel.Text = aStatusText;
363 /// <summary>
364 /// Fired when a |MenuItem| built by the |MenuBuilder| is selected.
365 /// </summary>
366 /// <param name="sender"></param>
367 /// <param name="e"></param>
368 public void OnMenuCommand(Object sender, EventArgs e)
370 ManticoreMenuItem item = sender as ManticoreMenuItem;
371 DoCommand(item.Command, item.Data);
374 ///////////////////////////////////////////////////////////////////////////
375 // IController Implementation
377 public void DoCommand(String aCommand)
379 DoCommand(aCommand, null);
382 public void DoCommand(String aCommand, Object aData)
384 Console.WriteLine(aCommand);
385 switch (aCommand)
387 case "file-new-window":
388 OpenBrowser();
389 break;
390 case "file-open":
391 Open();
392 break;
393 case "file-save-as":
394 SavePageAs();
395 break;
396 case "file-save-form":
397 TestForm frm = new TestForm();
398 frm.Show();
399 break;
400 case "file-exit":
401 Quit();
402 break;
403 case "view-statusbar":
404 if (mStatusBar.Visible)
405 mStatusBar.Hide();
406 else
407 mStatusBar.Show();
408 break;
409 case "view-go-back":
410 mWebBrowser.GoBack();
411 break;
412 case "view-go-forward":
413 mWebBrowser.GoForward();
414 break;
415 case "view-go-home":
416 mWebBrowser.GoHome();
417 break;
418 case "view-reload":
419 mWebBrowser.RefreshPage();
420 break;
421 case "view-stop":
422 mWebBrowser.Stop();
423 break;
424 case "view-layout-gecko":
425 mWebBrowser.SwitchLayoutEngine("gecko");
426 break;
427 case "view-layout-ie":
428 mWebBrowser.SwitchLayoutEngine("trident");
429 break;
430 case "bookmarks-manage":
431 BookmarksWindow bm = new BookmarksWindow();
432 bm.Show();
433 break;
434 case "bookmarks-item":
435 String url = ServiceManager.Bookmarks.GetBookmarkAttribute(aData as String, "url");
436 LoadURL(url);
437 break;
438 case "bookmarks-add":
439 // XXX need to allow user to customize this.
440 Bookmarks bmks = ServiceManager.Bookmarks;
441 String bookmarkID = bmks.CreateBookmark(mTitle, "Bookmarks", -1);
442 bmks.SetBookmarkAttribute(bookmarkID, "url", URL);
443 break;
444 case "bookmarks-file":
445 // XXX work on this
446 FileBookmark fpWindow = new FileBookmark(URL, mTitle);
447 fpWindow.ShowDialog();
448 break;
449 case "help-about":
450 AboutDialog aboutDialog = new AboutDialog(this);
451 aboutDialog.ShowDialog();
452 break;
453 case "tools-options":
454 PrefsDialog prefsDialog = new PrefsDialog(this);
455 prefsDialog.ShowDialog();
456 break;
460 public bool SupportsCommand(String aCommand)
462 // XXX implement me
463 return true;
466 public bool IsCommandEnabled(String aCommand)
468 // XXX implement me
469 return true;
473 public class BrowserToolbarBuilder : ToolbarBuilder
475 public BrowserToolbarBuilder(String aFile, Form aForm) : base(aFile, aForm)
479 public override void OnCommand(Object sender, ToolBarButtonClickEventArgs e)
481 CommandButtonItem item = e.Button as CommandButtonItem;
482 (mForm as BrowserWindow).DoCommand(item.Command);