1 // Permission is hereby granted, free of charge, to any person obtaining
\r
2 // a copy of this software and associated documentation files (the
\r
3 // "Software"), to deal in the Software without restriction, including
\r
4 // without limitation the rights to use, copy, modify, merge, publish,
\r
5 // distribute, sublicense, and/or sell copies of the Software, and to
\r
6 // permit persons to whom the Software is furnished to do so, subject to
\r
7 // the following conditions:
\r
9 // The above copyright notice and this permission notice shall be
\r
10 // included in all copies or substantial portions of the Software.
\r
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
20 // Copyright (c) 2007, 2008 Novell, Inc.
\r
23 // Andreia Gaita <avidigal@novell.com>
\r
27 using System.ComponentModel;
\r
28 using System.Runtime.InteropServices;
\r
29 using System.Reflection;
\r
30 using System.Windows.Forms;
\r
32 using System.Drawing;
\r
34 namespace System.Windows.Forms
\r
36 [DefaultProperty ("Url")]
\r
37 [DefaultEvent ("DocumentCompleted")]
\r
38 [Docking (DockingBehavior.AutoDock)]
\r
39 [ClassInterface (ClassInterfaceType.AutoDispatch)]
\r
41 [Designer("System.Windows.Forms.Design.WebBrowserDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
\r
42 public class WebBrowser : WebBrowserBase
\r
44 bool allowNavigation; // if this is true, no other navigation is allowed
\r
46 bool allowWebBrowserDrop = true;
\r
47 bool isWebBrowserContextMenuEnabled;
\r
48 object objectForScripting;
\r
49 bool webBrowserShortcutsEnabled;
\r
50 bool scrollbarsEnabled = true;
\r
52 WebBrowserReadyState readyState;
\r
54 HtmlDocument document;
\r
56 WebBrowserEncryptionLevel securityLevel;
\r
63 #region Public Properties
\r
65 [DefaultValue(true)]
\r
66 public bool AllowNavigation {
\r
67 get { return allowNavigation; }
\r
68 set { allowNavigation = value; }
\r
71 [DefaultValue (true)]
\r
72 public bool AllowWebBrowserDrop {
\r
73 get { return allowWebBrowserDrop; }
\r
74 set { allowWebBrowserDrop = value; }
\r
77 [BrowsableAttribute(false)]
\r
78 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
79 public bool CanGoBack {
\r
80 get { return this.WebHost.Navigation.CanGoBack; }
\r
83 [BrowsableAttribute(false)]
\r
84 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
85 public bool CanGoForward {
\r
86 get { return this.WebHost.Navigation.CanGoForward; }
\r
89 [BrowsableAttribute(false)]
\r
90 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
91 public HtmlDocument Document {
\r
93 if (document == null && documentReady)
\r
94 document = new HtmlDocument (this, this.WebHost);
\r
99 [BrowsableAttribute(false)]
\r
100 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
101 public Stream DocumentStream {
\r
103 if (WebHost.Document == null || WebHost.Document.DocumentElement == null)
\r
106 return null; //WebHost.Document.DocumentElement.ContentStream;
\r
109 if (this.allowNavigation)
\r
112 this.Url = new Uri ("about:blank");
\r
115 isStreamSet = true;
\r
119 [BrowsableAttribute(false)]
\r
120 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
121 public string DocumentText {
\r
123 if (WebHost.Document == null || WebHost.Document.DocumentElement == null)
\r
124 return String.Empty;
\r
125 return WebHost.Document.DocumentElement.OuterHTML;
\r
128 if (WebHost.Document != null && WebHost.Document.DocumentElement != null)
\r
129 WebHost.Document.DocumentElement.OuterHTML = value;
\r
133 [BrowsableAttribute(false)]
\r
134 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
135 public string DocumentTitle {
\r
137 if (document != null)
\r
138 return document.Title;
\r
139 return String.Empty;
\r
143 [BrowsableAttribute(false)]
\r
144 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
145 public string DocumentType {
\r
147 if (document != null)
\r
148 return document.DocType;
\r
149 return String.Empty;
\r
153 [BrowsableAttribute(false)]
\r
154 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
155 public WebBrowserEncryptionLevel EncryptionLevel {
\r
156 get { return securityLevel; }
\r
159 public override bool Focused {
\r
160 get { return base.Focused; }
\r
163 [BrowsableAttribute(false)]
\r
164 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
165 public bool IsBusy {
\r
166 get { return !documentReady; }
\r
169 [BrowsableAttribute(false)]
\r
170 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
171 public bool IsOffline {
\r
172 get { return WebHost.Offline; }
\r
175 [MonoTODO ("Stub, not implemented")]
\r
176 [DefaultValue(true)]
\r
177 public bool IsWebBrowserContextMenuEnabled {
\r
178 get { return isWebBrowserContextMenuEnabled; }
\r
179 set { isWebBrowserContextMenuEnabled = value; }
\r
182 [MonoTODO ("Stub, not implemented")]
\r
183 [BrowsableAttribute(false)]
\r
184 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
185 public object ObjectForScripting {
\r
186 get { return objectForScripting; }
\r
187 set { objectForScripting = value; }
\r
190 [BrowsableAttribute(false)]
\r
191 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
192 public WebBrowserReadyState ReadyState {
\r
193 get { return readyState; }
\r
196 [DefaultValue(false)]
\r
197 public bool ScriptErrorsSuppressed {
\r
198 get { return SuppressDialogs; }
\r
199 set { SuppressDialogs = value; }
\r
202 [DefaultValue(true)]
\r
203 public bool ScrollBarsEnabled {
\r
204 get { return scrollbarsEnabled; }
\r
206 scrollbarsEnabled = value;
\r
207 if (document != null)
\r
212 [BrowsableAttribute(false)]
\r
213 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
214 public virtual string StatusText {
\r
215 get { return base.status; }
\r
218 [BindableAttribute(true)]
\r
219 [DefaultValue(null)]
\r
220 [TypeConverter(typeof(WebBrowserUriTypeConverter))]
\r
224 return new Uri (url);
\r
225 if (WebHost.Document != null && WebHost.Document.Url != null)
\r
226 return new Uri (WebHost.Document.Url);
\r
231 this.Navigate (value);
\r
235 [BrowsableAttribute(false)]
\r
236 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
237 public Version Version {
\r
239 Assembly ass = WebHost.GetType().Assembly;
\r
240 return ass.GetName().Version;
\r
244 [MonoTODO ("Stub, not implemented")]
\r
245 [DefaultValue(true)]
\r
246 public bool WebBrowserShortcutsEnabled {
\r
247 get { return webBrowserShortcutsEnabled; }
\r
248 set { webBrowserShortcutsEnabled = value; }
\r
251 protected override Size DefaultSize {
\r
252 get { return base.DefaultSize; }
\r
255 [BrowsableAttribute(false)]
\r
256 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
\r
257 [EditorBrowsable (EditorBrowsableState.Never)]
\r
258 public new Padding Padding {
\r
259 get { return base.Padding; }
\r
260 set { base.Padding = value; }
\r
265 [MonoTODO ("WebBrowser control is only supported on Linux/Windows. No support for OSX.")]
\r
266 public WebBrowser ()
\r
270 #region Public Methods
\r
272 public bool GoBack ()
\r
274 documentReady = false;
\r
276 return WebHost.Navigation.Back ();
\r
279 public bool GoForward ()
\r
281 documentReady = false;
\r
283 return WebHost.Navigation.Forward ();
\r
286 public void GoHome ()
\r
288 documentReady = false;
\r
290 WebHost.Navigation.Home ();
\r
293 public void Navigate (string urlString)
\r
295 documentReady = false;
\r
297 WebHost.Navigation.Go (urlString);
\r
300 public void Navigate (Uri url)
\r
302 documentReady = false;
\r
304 WebHost.Navigation.Go (url.ToString ());
\r
307 public void Navigate (string urlString, bool newWindow)
\r
309 documentReady = false;
\r
311 WebHost.Navigation.Go (urlString);
\r
314 public void Navigate (string urlString, string targetFrameName)
\r
316 documentReady = false;
\r
318 WebHost.Navigation.Go (urlString);
\r
321 public void Navigate (Uri url, bool newWindow)
\r
323 documentReady = false;
\r
325 WebHost.Navigation.Go (url.ToString ());
\r
328 public void Navigate (Uri url, string targetFrameName)
\r
330 documentReady = false;
\r
332 WebHost.Navigation.Go (url.ToString ());
\r
335 public void Navigate (string urlString, string targetFrameName, byte[] postData, string additionalHeaders)
\r
337 documentReady = false;
\r
339 WebHost.Navigation.Go (urlString);
\r
342 public void Navigate (Uri url, string targetFrameName, byte[] postData, string additionalHeaders)
\r
344 documentReady = false;
\r
346 WebHost.Navigation.Go (url.ToString ());
\r
349 public override void Refresh ()
\r
351 Refresh (WebBrowserRefreshOption.IfExpired);
\r
354 public void Refresh (WebBrowserRefreshOption opt)
\r
356 documentReady = false;
\r
359 case WebBrowserRefreshOption.Normal:
\r
360 WebHost.Navigation.Reload (Mono.WebBrowser.ReloadOption.Proxy);
\r
362 case WebBrowserRefreshOption.IfExpired:
\r
363 WebHost.Navigation.Reload (Mono.WebBrowser.ReloadOption.None);
\r
365 case WebBrowserRefreshOption.Completely:
\r
366 WebHost.Navigation.Reload (Mono.WebBrowser.ReloadOption.Full);
\r
371 public void Stop ()
\r
373 WebHost.Navigation.Stop ();
\r
376 public void GoSearch ()
\r
378 string url = "http://www.google.com";
\r
380 Microsoft.Win32.RegistryKey reg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey (@"Software\Microsoft\Internet Explorer\Main\Search Page");
\r
382 object searchUrl = reg.GetValue ("Default_Search_URL");
\r
383 if (searchUrl != null && searchUrl is string) {
\r
385 if (System.Uri.TryCreate (searchUrl as string, UriKind.Absolute, out uri))
\r
386 url = uri.ToString ();
\r
394 public void Print ()
\r
396 throw new NotImplementedException ();
\r
399 public void ShowPageSetupDialog ()
\r
401 throw new NotImplementedException ();
\r
404 public void ShowPrintDialog()
\r
406 throw new NotImplementedException ();
\r
409 public void ShowPrintPreviewDialog()
\r
411 throw new NotImplementedException ();
\r
414 public void ShowPropertiesDialog()
\r
416 throw new NotImplementedException ();
\r
419 public void ShowSaveAsDialog()
\r
421 throw new NotImplementedException ();
\r
426 #region Protected Overridden Methods
\r
428 [MonoTODO ("Stub, not implemented")]
\r
429 protected override void AttachInterfaces (object nativeActiveXObject)
\r
431 base.AttachInterfaces (nativeActiveXObject);
\r
434 [MonoTODO ("Stub, not implemented")]
\r
435 protected override void CreateSink ()
\r
437 base.CreateSink ();
\r
440 [MonoTODO ("Stub, not implemented")]
\r
441 protected override WebBrowserSiteBase CreateWebBrowserSiteBase ()
\r
443 return base.CreateWebBrowserSiteBase ();
\r
446 [MonoTODO ("Stub, not implemented")]
\r
447 protected override void DetachInterfaces ()
\r
449 base.DetachInterfaces ();
\r
452 [MonoTODO ("Stub, not implemented")]
\r
453 protected override void DetachSink ()
\r
455 base.DetachSink ();
\r
458 protected override void Dispose (bool disposing)
\r
460 base.Dispose (disposing);
\r
463 protected override void WndProc (ref Message m)
\r
465 base.WndProc (ref m);
\r
470 #region OnXXX methods
\r
472 protected virtual void OnCanGoBackChanged(EventArgs e)
\r
474 if (CanGoBackChanged != null)
\r
475 CanGoBackChanged (this, e);
\r
478 protected virtual void OnCanGoForwardChanged(EventArgs e)
\r
480 if (CanGoForwardChanged != null)
\r
481 CanGoForwardChanged (this, e);
\r
484 protected virtual void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
\r
486 if (DocumentCompleted != null)
\r
487 DocumentCompleted (this, e);
\r
490 protected virtual void OnDocumentTitleChanged(EventArgs e)
\r
492 if (DocumentTitleChanged != null)
\r
493 DocumentTitleChanged (this, e);
\r
496 protected virtual void OnEncryptionLevelChanged(EventArgs e)
\r
498 if (EncryptionLevelChanged != null)
\r
499 EncryptionLevelChanged (this, e);
\r
502 protected virtual void OnFileDownload(EventArgs e)
\r
504 if (FileDownload != null)
\r
505 FileDownload (this, e);
\r
508 protected virtual void OnNavigated(WebBrowserNavigatedEventArgs e)
\r
510 if (Navigated != null)
\r
511 Navigated (this, e);
\r
514 protected virtual void OnNavigating(WebBrowserNavigatingEventArgs e)
\r
516 if (Navigating != null)
\r
517 Navigating (this, e);
\r
520 protected virtual void OnNewWindow(CancelEventArgs e)
\r
522 if (NewWindow != null)
\r
523 NewWindow (this, e);
\r
526 protected virtual void OnProgressChanged(WebBrowserProgressChangedEventArgs e)
\r
528 if (ProgressChanged != null)
\r
529 ProgressChanged (this, e);
\r
532 protected virtual void OnStatusTextChanged(EventArgs e)
\r
534 if (StatusTextChanged != null)
\r
535 StatusTextChanged (this, e);
\r
541 [BrowsableAttribute(false)]
\r
542 public event EventHandler CanGoBackChanged;
\r
544 [BrowsableAttribute(false)]
\r
545 public event EventHandler CanGoForwardChanged;
\r
547 public event WebBrowserDocumentCompletedEventHandler DocumentCompleted;
\r
549 [BrowsableAttribute(false)]
\r
550 public event EventHandler DocumentTitleChanged;
\r
552 [BrowsableAttribute(false)]
\r
553 public event EventHandler EncryptionLevelChanged;
\r
555 public event EventHandler FileDownload;
\r
557 public event WebBrowserNavigatedEventHandler Navigated;
\r
559 public event WebBrowserNavigatingEventHandler Navigating;
\r
561 public event CancelEventHandler NewWindow;
\r
563 public event WebBrowserProgressChangedEventHandler ProgressChanged;
\r
565 [BrowsableAttribute(false)]
\r
566 public event EventHandler StatusTextChanged;
\r
568 [Browsable (false)]
\r
569 [EditorBrowsable (EditorBrowsableState.Never)]
\r
570 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
\r
571 public new event EventHandler PaddingChanged;
\r
576 internal override bool OnNewWindowInternal ()
\r
578 CancelEventArgs c = new CancelEventArgs ();
\r
583 internal override void OnWebHostLoadStarted (object sender, Mono.WebBrowser.LoadStartedEventArgs e)
\r
585 documentReady = false;
\r
587 readyState = WebBrowserReadyState.Loading;
\r
588 WebBrowserNavigatingEventArgs n = new WebBrowserNavigatingEventArgs (new Uri (e.Uri), e.FrameName);
\r
592 internal override void OnWebHostLoadCommited (object sender, Mono.WebBrowser.LoadCommitedEventArgs e)
\r
594 readyState = WebBrowserReadyState.Loaded;
\r
597 WebBrowserNavigatedEventArgs n = new WebBrowserNavigatedEventArgs (new Uri (e.Uri));
\r
600 internal override void OnWebHostProgressChanged (object sender, Mono.WebBrowser.ProgressChangedEventArgs e)
\r
602 readyState = WebBrowserReadyState.Interactive;
\r
603 WebBrowserProgressChangedEventArgs n = new WebBrowserProgressChangedEventArgs (e.Progress, e.MaxProgress);
\r
604 OnProgressChanged (n);
\r
607 internal override void OnWebHostLoadFinished (object sender, Mono.WebBrowser.LoadFinishedEventArgs e)
\r
610 documentReady = true;
\r
611 readyState = WebBrowserReadyState.Complete;
\r
613 byte[] buffer = new byte [data.Length];
\r
614 long len = data.Length;
\r
618 count = data.Read (buffer, (int) data.Position, (int) (len - data.Position));
\r
619 } while (count > 0);
\r
620 WebHost.Render (buffer);
\r
622 isStreamSet = false;
\r
625 WebBrowserDocumentCompletedEventArgs n = new WebBrowserDocumentCompletedEventArgs (new Uri (e.Uri));
\r
626 OnDocumentCompleted (n);
\r
629 internal override void OnWebHostSecurityChanged (object sender, Mono.WebBrowser.SecurityChangedEventArgs e)
\r
632 case Mono.WebBrowser.SecurityLevel.Insecure:
\r
633 securityLevel = WebBrowserEncryptionLevel.Insecure;
\r
635 case Mono.WebBrowser.SecurityLevel.Mixed:
\r
636 securityLevel = WebBrowserEncryptionLevel.Mixed;
\r
638 case Mono.WebBrowser.SecurityLevel.Secure:
\r
639 securityLevel = WebBrowserEncryptionLevel.Bit56;
\r
644 internal override void OnWebHostContextMenuShown (object sender, Mono.WebBrowser.ContextMenuEventArgs e) {
\r
645 if (!isWebBrowserContextMenuEnabled)
\r
648 ContextMenu menu = new ContextMenu();
\r
650 MenuItem item = new MenuItem("Back", delegate {
\r
653 item.Enabled = this.CanGoBack;
\r
654 menu.MenuItems.Add (item);
\r
656 item = new MenuItem("Forward", delegate {
\r
659 item.Enabled = this.CanGoForward;
\r
660 menu.MenuItems.Add (item);
\r
662 item = new MenuItem("Refresh", delegate {
\r
665 menu.MenuItems.Add (item);
\r
667 menu.MenuItems.Add (new MenuItem ("-"));
\r
669 menu.Show(this, PointToClient(MousePosition));
\r
672 internal override void OnWebHostStatusChanged (object sender, Mono.WebBrowser.StatusChangedEventArgs e) {
\r
673 base.status = e.Message;
\r
674 OnStatusTextChanged (null);
\r
680 void SetScrollbars () {
\r
681 //if (!scrollbarsEnabled)
\r
682 // WebHost.ExecuteScript ("document.body.style.overflow='hidden';");
\r
684 // WebHost.ExecuteScript ("document.body.style.overflow='auto';");
\r
687 [MonoTODO ("Stub, not implemented")]
\r
688 [ComVisible (false)]
\r
689 protected class WebBrowserSite : WebBrowserSiteBase
\r
691 [MonoTODO ("Stub, not implemented")]
\r
692 public WebBrowserSite (WebBrowser host)
\r
699 internal class WebBrowserUriTypeConverter : UriTypeConverter
\r