1 // Permission is hereby granted, free of charge, to any person obtaining 2 // a copy of this software and associated documentation files (the 3 // "Software"), to deal in the Software without restriction, including 4 // without limitation the rights to use, copy, modify, merge, publish, 5 // distribute, sublicense, and/or sell copies of the Software, and to 6 // permit persons to whom the Software is furnished to do so, subject to 7 // the following conditions: 8 // 9 // The above copyright notice and this permission notice shall be 10 // included in all copies or substantial portions of the Software. 11 // 12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 // 20 // Copyright (c) 2008 Novell, Inc. 21 // 22 // Authors: 23 // Andreia Gaita (avidigal@novell.com) 24 // 25 26 using System; 27 using System.Text; 28 using System.Runtime.InteropServices; 29 using System.Collections; 30 using Mono.WebBrowser; 31 using Mono.WebBrowser.DOM; 32 33 namespace Mono.Mozilla.DOM 34 { 35 internal class Window : DOMObject, IWindow 36 { 37 internal nsIDOMWindow window; 38 private EventListener eventListener; 39 int hashcode; 40 Window(WebBrowser control, nsIDOMWindow domWindow)41 public Window(WebBrowser control, nsIDOMWindow domWindow) : base (control) 42 { 43 this.hashcode = domWindow.GetHashCode (); 44 this.window = domWindow; 45 } 46 47 #region IDisposable Members Dispose(bool disposing)48 protected override void Dispose (bool disposing) 49 { 50 if (!disposed) { 51 if (disposing) { 52 this.resources.Clear (); 53 this.window = null; 54 } 55 } 56 base.Dispose(disposing); 57 } 58 #endregion 59 FindDocument(ref nsIDOMWindow window, int docHashcode)60 internal static bool FindDocument (ref nsIDOMWindow window, int docHashcode) { 61 nsIDOMDocument doc; 62 window.getDocument (out doc); 63 64 if (doc.GetHashCode () == docHashcode) { 65 return true; 66 } else { 67 uint len = 1; 68 nsIDOMWindowCollection col; 69 70 window.getFrames (out col); 71 col.getLength (out len); 72 73 for (uint i = 0; i < len; ++i) { 74 col.item (i, out window); 75 if (Window.FindDocument (ref window, docHashcode)) 76 return true; 77 } 78 } 79 return false; 80 } 81 82 #region Properties 83 public IDocument Document { 84 get { 85 nsIDOMDocument doc; 86 this.window.getDocument (out doc); 87 if (!control.documents.ContainsKey (doc.GetHashCode ())) 88 control.documents.Add (doc.GetHashCode (), new Document (control, (nsIDOMHTMLDocument) doc)); 89 return control.documents[doc.GetHashCode ()] as IDocument; 90 } 91 } 92 93 public IWindowCollection Frames { 94 get { 95 nsIDOMWindowCollection windows; 96 this.window.getFrames (out windows); 97 return new WindowCollection (control, windows); 98 } 99 } 100 101 public string Name { 102 get { 103 this.window.getName (storage); 104 return Base.StringGet (storage); 105 } 106 set { 107 Base.StringSet (storage, value); 108 this.window.setName (storage); 109 } 110 } 111 112 public IWindow Parent { 113 get { 114 nsIDOMWindow parent; 115 this.window.getParent (out parent); 116 return new Window (control, parent); 117 } 118 } 119 120 public IWindow Top { 121 get { 122 nsIDOMWindow top; 123 this.window.getTop (out top); 124 return new Window (control, top); 125 } 126 } 127 128 public string StatusText { 129 get { 130 return control.StatusText; 131 132 } 133 } 134 135 public IHistory History { 136 get { 137 Navigation nav = new Navigation (this.control, window as nsIWebNavigation); 138 return new History (this.control, nav); 139 } 140 } 141 #endregion 142 143 #region Methods 144 private EventListener EventListener { 145 get { 146 if (eventListener == null) 147 eventListener = new EventListener (this.window as nsIDOMEventTarget, this); 148 return eventListener; 149 } 150 } 151 AttachEventHandler(string eventName, EventHandler handler)152 public void AttachEventHandler (string eventName, EventHandler handler) 153 { 154 EventListener.AddHandler (handler, eventName); 155 } 156 DetachEventHandler(string eventName, EventHandler handler)157 public void DetachEventHandler (string eventName, EventHandler handler) 158 { 159 EventListener.RemoveHandler (handler, eventName); 160 } 161 Focus()162 public void Focus () { 163 nsIWebBrowserFocus focus = (nsIWebBrowserFocus) this.window; 164 focus.setFocusedWindow (this.window); 165 } 166 Open(string url)167 public void Open (string url) 168 { 169 nsIWebNavigation webnav = (nsIWebNavigation) this.window; 170 webnav.loadURI (url, (uint)LoadFlags.None, null, null, null); 171 } 172 ScrollTo(int x, int y)173 public void ScrollTo (int x, int y) 174 { 175 this.window.scrollTo (x, y); 176 } 177 Equals(object obj)178 public override bool Equals (object obj) 179 { 180 return this == obj as Window; 181 } 182 operator ==(Window left, Window right)183 public static bool operator == (Window left, Window right) 184 { 185 if ((object)left == (object)right) 186 return true; 187 188 if ((object)left == null || (object)right == null) 189 return false; 190 191 return left.hashcode == right.hashcode; 192 } 193 operator !=(Window left, Window right)194 public static bool operator != (Window left, Window right) 195 { 196 return !(left == right); 197 } 198 GetHashCode()199 public override int GetHashCode () { 200 return hashcode; 201 } 202 203 #endregion 204 205 #region Events 206 static object LoadEvent = new object (); 207 public event EventHandler Load { 208 add { 209 Events.AddHandler (LoadEvent, value); 210 AttachEventHandler ("load", value); 211 } 212 remove { 213 Events.RemoveHandler (LoadEvent, value); 214 DetachEventHandler ("load", value); 215 } 216 } 217 218 static object UnloadEvent = new object (); 219 public event EventHandler Unload { 220 add { 221 Events.AddHandler (UnloadEvent, value); 222 AttachEventHandler ("unload", value); 223 } 224 remove { 225 Events.RemoveHandler (UnloadEvent, value); 226 DetachEventHandler ("unload", value); 227 } 228 } 229 230 public event EventHandler OnFocus { 231 add { AttachEventHandler ("focus", value); } 232 remove { DetachEventHandler ("focus", value); } 233 } 234 235 public event EventHandler OnBlur { 236 add { AttachEventHandler ("blur", value); } 237 remove { DetachEventHandler ("blur", value); } 238 } 239 240 public event EventHandler Error { 241 add { AttachEventHandler ("error", value); } 242 remove { DetachEventHandler ("error", value); } 243 } 244 245 public event EventHandler Scroll { 246 add { AttachEventHandler ("scroll", value); } 247 remove { DetachEventHandler ("scroll", value); } 248 } 249 OnLoad()250 public void OnLoad () 251 { 252 EventHandler eh = (EventHandler) (Events[LoadEvent]); 253 if (eh != null) { 254 EventArgs e = new EventArgs (); 255 eh (this, e); 256 } 257 } 258 OnUnload()259 public void OnUnload () 260 { 261 EventHandler eh = (EventHandler) (Events[UnloadEvent]); 262 if (eh != null) { 263 EventArgs e = new EventArgs (); 264 eh (this, e); 265 } 266 } 267 268 #endregion 269 } 270 } 271