* Makefile.am:
[monodevelop.git] / extras / GeckoWebBrowser / GeckoWebBrowser.cs
bloba49fb6f3270638fb09a9c279ed2c0ce99b22084c
1 //
2 // GeckoWebBrowser.cs
3 //
4 // Author:
5 // Michael Hutchinson <mhutchinson@novell.com>
6 //
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System;
30 using System.IO;
31 using Gecko;
33 using MonoDevelop.Core.Gui.WebBrowser;
35 namespace MonoDevelop.WebBrowsers
38 public class GeckoWebBrowser : WebControl, IWebBrowser
40 string delayedUrl;
41 string oldTempFile;
42 bool reShown = false;
44 bool suppressLinkClickedBecauseCausedByLoadUrlCall = false;
46 public GeckoWebBrowser ()
48 WebControl.SetProfilePath ("/tmp", "MonoDevelop");
50 //FIXME: On{Event} doesn't fire
51 this.ExposeEvent += exposeHandler;
52 this.OpenUri += delegate (object o, OpenUriArgs args) {
53 args.RetVal = OnLocationChanging (args.AURI);
55 this.LocChange += delegate (object sender, EventArgs e) {
56 OnLocationChanged ();
58 this.Progress += delegate (object sender, ProgressArgs e) {
59 OnLoadingProgressChanged (e.Curprogress / e.Maxprogress);
61 this.ECMAStatus += delegate {
62 OnJSStatusChanged ();
64 this.LinkStatusChanged += delegate {
65 OnLinkStatusChanged ();
67 this.TitleChange += delegate {
68 OnTitleChanged ();
72 //FIXME: OnExposeEvent doesn't fire, but ExposeEvent does
73 void exposeHandler (object sender, Gtk.ExposeEventArgs e)
75 if (delayedUrl != null) {
76 realLoadUrl (delayedUrl);
77 delayedUrl = null;
80 //FIXME: suppress a strange bug with control not getting drawn first time it's shown, or when docking changed.
81 //For some reason this event only fires when control 'appears' or is re-docked, which corresponds 1:1 to the bug.
82 if (!reShown) {
83 Hide ();
84 Show ();
85 //Normally we would expect this event to fire with every redraw event, so put in a limiter
86 //in case this is fixed in future.
87 reShown = true;
88 GLib.Timeout.Add (1000, delegate { reShown = false; return false; } );
92 string IWebBrowser.Title {
93 get { return base.Title; }
96 string IWebBrowser.JSStatus {
97 get { return base.JsStatus; }
100 string IWebBrowser.LinkStatus {
101 get { return base.LinkMessage; }
104 string IWebBrowser.Location {
105 get { return base.Location; }
108 bool IWebBrowser.CanGoBack {
109 get { return base.CanGoBack (); }
112 bool IWebBrowser.CanGoForward {
113 get { return base.CanGoForward (); }
116 void IWebBrowser.GoForward ()
118 base.GoForward ();
121 void IWebBrowser.GoBack ()
123 base.GoBack ();
126 void IWebBrowser.LoadUrl (string url)
128 realLoadUrl (url);
131 void IWebBrowser.LoadHtml (string html)
133 string tempFile = System.IO.Path.GetTempFileName ();
135 StreamWriter writer = null;
136 try {
137 writer = File.CreateText (tempFile);
138 writer.Write (html);
139 } catch (Exception ex) {
140 MonoDevelop.Core.LoggingService.LogError ("Could not write temporary HTML file '{0}' for Gecko web control\n{1}", tempFile, ex.ToString ());
141 } finally {
142 if (writer != null)
143 writer.Close ();
146 realLoadUrl ("tempfile://" + tempFile);
149 void realLoadUrl (string url)
151 if (url == null)
152 throw new ArgumentNullException ("url");
154 if (!this.IsRealized) {
155 delayedUrl = url;
156 return;
159 if (url == delayedUrl) {
160 delayedUrl = null;
163 if (oldTempFile != null) {
164 try {
165 File.Delete (oldTempFile);
166 } catch (Exception ex) {
167 MonoDevelop.Core.LoggingService.LogError ("Could not delete temp file '{0}'\n{1]", oldTempFile, ex.ToString ());
169 oldTempFile = null;
172 suppressLinkClickedBecauseCausedByLoadUrlCall = true;
174 if (url.StartsWith ("tempfile://")) {
175 oldTempFile = url.Substring (11);
176 base.LoadUrl (oldTempFile);
177 } else {
178 base.LoadUrl (url);
182 void IWebBrowser.Reload ()
184 base.Reload ((int) ReloadFlags.Reloadnormal);
187 void IWebBrowser.StopLoad ()
189 base.StopLoad ();
192 public event PageLoadedHandler PageLoaded;
193 public event LocationChangingHandler LocationChanging;
194 public event LocationChangingHandler LinkClicked;
195 public event LocationChangedHandler LocationChanged;
196 public event TitleChangedHandler TitleChanged;
197 public event StatusMessageChangedHandler JSStatusChanged;
198 public event StatusMessageChangedHandler LinkStatusChanged;
199 public event LoadingProgressChangedHandler LoadingProgressChanged;
201 protected bool OnLocationChanging (string aURI)
203 LocationChangingEventArgs args = new LocationChangingEventArgs (aURI, false);
204 args.SuppressChange = false;
205 if (LocationChanging != null)
206 LocationChanging (this, args);
207 OnLinkClicked (args);
208 return args.SuppressChange;
211 protected virtual void OnLinkClicked (LocationChangingEventArgs args)
213 if (suppressLinkClickedBecauseCausedByLoadUrlCall) {
214 suppressLinkClickedBecauseCausedByLoadUrlCall = false;
215 return;
217 if (LinkClicked != null)
218 LinkClicked (this, args);
221 protected virtual void OnLocationChanged ()
223 if (LocationChanged != null)
224 LocationChanged (this, new LocationChangedEventArgs (null));
227 protected virtual void OnLoadingProgressChanged (float progress)
229 if (LoadingProgressChanged != null)
230 LoadingProgressChanged (this, new LoadingProgressChangedEventArgs (progress));
233 protected virtual void OnJSStatusChanged ()
235 if (JSStatusChanged != null)
236 JSStatusChanged (this, new StatusMessageChangedEventArgs (base.JsStatus));
239 protected virtual void OnLinkStatusChanged ()
241 if (LinkStatusChanged != null)
242 LinkStatusChanged (this, new StatusMessageChangedEventArgs (base.LinkMessage));
245 protected virtual void OnTitleChanged ()
247 if (TitleChanged != null)
248 TitleChanged (this, new TitleChangedEventArgs (base.Title));