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
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.
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
43 using System
.ComponentModel
;
45 using System
.Windows
.Forms
;
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
70 private StatusBar mStatusBar
;
71 private StatusBarPanel mProgressMeter
;
72 private StatusBarPanel mStatusPanel
;
74 private String mSessionURL
= "";
79 private String mTitle
= "";
81 public BrowserWindow()
86 public BrowserWindow(String aURL
)
95 mType
= "BrowserWindow";
98 InitializeComponent();
103 private void InitializeComponent()
105 // XXX read these from a settings file
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 ///////////////////////////////////////////////////////////////////////////
164 /// The currently loaded document's URL.
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
184 return mShouldLoadHomePage
;
188 if (value != mShouldLoadHomePage
)
189 mShouldLoadHomePage
= value;
193 private void LoadStartPage(object sender
, EventArgs e
)
195 if (!mShouldLoadHomePage
)
198 int startMode
= ServiceManager
.Preferences
.GetIntPref("browser.homepage.mode");
201 // Don't initialize jack.
205 mWebBrowser
.GoHome();
208 // Load the session document.
209 mWebBrowser
.LoadURL(mSessionURL
, false);
214 ///////////////////////////////////////////////////////////////////////////
216 protected void OnLocationCommit(Object aSender
, LocationBarEventArgs aLbea
)
218 string url
= ServiceManager
.Bookmarks
.ResolveKeyword(aLbea
.Text
);
226 protected bool mUserTyped
= false;
227 protected bool mUpdatedURLBar
= false;
228 protected void OnLocationModified(Object aSender
, LocationBarEventArgs aLbea
)
233 ///////////////////////////////////////////////////////////////////////////
235 public static BrowserWindow
OpenBrowser()
237 BrowserWindow window
= new BrowserWindow();
242 public static BrowserWindow
OpenBrowserWithURL(String aURL
)
244 BrowserWindow window
= new BrowserWindow(aURL
);
249 ///////////////////////////////////////////////////////////////////////////
250 // Menu Command Handlers
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("|", "-");
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
;
289 dlg
.Filter
= "Web Page, complete (*.htm;*.html)|*.htm*|Web Page, HTML only (*.htm;*.html)|*.htm*|Text only (*.txt)|*.txt";
290 dlg
.DefaultExt
= "html";
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);
301 dlg
.FileOk
+= new CancelEventHandler(OnSavePageAsOK
);
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
);
316 ManticoreApp app
= ServiceManager
.App
;
320 public Object currentLayoutEngine
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.
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
)
355 this.Text
= (aTitle
== "about:blank") ? "No page to display" : aTitle
;
358 public void OnStatusTextChange(String aStatusText
)
360 mStatusPanel
.Text
= aStatusText
;
364 /// Fired when a |MenuItem| built by the |MenuBuilder| is selected.
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
);
387 case "file-new-window":
396 case "file-save-form":
397 TestForm frm
= new TestForm();
403 case "view-statusbar":
404 if (mStatusBar
.Visible
)
410 mWebBrowser
.GoBack();
412 case "view-go-forward":
413 mWebBrowser
.GoForward();
416 mWebBrowser
.GoHome();
419 mWebBrowser
.RefreshPage();
424 case "view-layout-gecko":
425 mWebBrowser
.SwitchLayoutEngine("gecko");
427 case "view-layout-ie":
428 mWebBrowser
.SwitchLayoutEngine("trident");
430 case "bookmarks-manage":
431 BookmarksWindow bm
= new BookmarksWindow();
434 case "bookmarks-item":
435 String url
= ServiceManager
.Bookmarks
.GetBookmarkAttribute(aData
as String
, "url");
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
);
444 case "bookmarks-file":
446 FileBookmark fpWindow
= new FileBookmark(URL
, mTitle
);
447 fpWindow
.ShowDialog();
450 AboutDialog aboutDialog
= new AboutDialog(this);
451 aboutDialog
.ShowDialog();
453 case "tools-options":
454 PrefsDialog prefsDialog
= new PrefsDialog(this);
455 prefsDialog
.ShowDialog();
460 public bool SupportsCommand(String aCommand
)
466 public bool IsCommandEnabled(String aCommand
)
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
);