Typo error
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / HtmlWindow.cs
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) 2007, 2008 Novell, Inc.
21 //
22 // Authors:
23 //      Andreia Gaita (avidigal@novell.com)
24 //
25
26
27 using System;
28 using System.Drawing;
29 using System.ComponentModel;
30
31 using Mono.WebBrowser.DOM;
32
33 namespace System.Windows.Forms
34 {
35         public sealed class HtmlWindow
36         {
37                 private EventHandlerList event_handlers;
38                 private IWindow window;
39                 private Mono.WebBrowser.IWebBrowser webHost;
40                 private WebBrowser owner;
41                 
42                 internal HtmlWindow (WebBrowser owner, Mono.WebBrowser.IWebBrowser webHost, IWindow iWindow)
43                 {
44                         this.window = iWindow;
45                         this.webHost = webHost;
46                         this.owner = owner;
47                         this.window.Load += new EventHandler (OnLoad);
48                         this.window.Unload += new EventHandler (OnUnload);
49                 }
50
51                 private EventHandlerList Events {
52                         get {
53                                 // Note: space vs. time tradeoff
54                                 // We create the object here if it's never be accessed before.  This potentially 
55                                 // saves space. However, we must check each time the propery is accessed to
56                                 // determine whether we need to create the object, which increases overhead.
57                                 // We could put the creation in the contructor, but that would waste space
58                                 // if it were never used.  However, accessing this property would be faster.
59                                 if (null == event_handlers)
60                                         event_handlers = new EventHandlerList();
61
62                                 return event_handlers;
63                         }
64                 }
65
66 #region Properties
67                 public HtmlDocument Document {
68                         get { return new HtmlDocument (owner, webHost, this.window.Document); }
69                 }
70                 
71                 public object DomWindow {
72                         get { throw new NotSupportedException ("Retrieving a reference to an mshtml interface is not supported. Sorry."); } 
73                 }
74
75                 public HtmlWindowCollection Frames {
76                         get { return new HtmlWindowCollection (owner, webHost, this.window.Frames); }
77                 }
78
79                 public HtmlHistory History {
80                         get { return new HtmlHistory (this.webHost, window.History); }
81                 }
82
83                 [MonoTODO ("Windows are always open")]
84                 public bool IsClosed {
85                         get { return false; }
86                 }
87
88                 public string Name {
89                         get { return this.window.Name; }
90                         set { this.window.Name = value; }
91                 }
92                 
93                 [MonoTODO ("Separate windows are not supported yet")]
94                 public HtmlWindow Opener {
95                         get { return null; }
96                 }
97
98                 public HtmlWindow Parent {
99                         get { return new HtmlWindow (owner, webHost, this.window.Parent); }
100                 }
101
102                 public Point Position {
103                         get { return owner.Location; }
104                 }
105
106                 public Size Size {
107                         get { return owner.Size; }
108                         set { }
109                 }
110                 
111                 public string StatusBarText {
112                         get { return this.window.StatusText; }
113                         set { }
114                 }
115
116                 public HtmlElement WindowFrameElement {
117                         get { 
118                                 return new HtmlElement (owner, webHost, window.Document.DocumentElement);
119                         }
120                 }
121                 
122                 public Uri Url {
123                         get { return this.Document.Url; }
124                 }
125 #endregion
126
127 #region Methods
128                 public void Alert (string message) 
129                 {
130                         MessageBox.Show ("Alert", message);
131                 }
132
133                 public bool Confirm (string message) 
134                 {
135                         DialogResult ret = MessageBox.Show (message, "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
136                         return ret == DialogResult.OK;
137                 }
138                 
139                 public string Prompt (string message, string defaultInputValue)
140                 {
141                         WebBrowserDialogs.Prompt prompt = new WebBrowserDialogs.Prompt ("Prompt", message, defaultInputValue);
142                         prompt.Show ();
143                         return prompt.Text;
144                 }
145                 
146                 public void Navigate (string urlString)
147                 {
148                         webHost.Navigation.Go (urlString);
149                 }
150
151                 public void Navigate (Uri url)
152                 {
153                         webHost.Navigation.Go (url.ToString ());
154                 }
155                 
156                 public void ScrollTo (Point point)
157                 {
158                         ScrollTo (point.X, point.Y);
159                 }
160
161                 public void ScrollTo (int x, int y)
162                 {
163                         this.window.ScrollTo (x, y);
164                 }
165
166                 [MonoTODO("Blank opens in current window at the moment. Missing media and search implementations. No options implemented")]
167                 public HtmlWindow Open (Uri url, string target, string windowOptions, bool replaceEntry)
168                 {
169                         return Open (url.ToString(), target, windowOptions, replaceEntry);
170                 }
171
172                 [MonoTODO("Blank opens in current window at the moment. Missing media and search implementations. No options implemented")]
173                 public HtmlWindow Open (string urlString, string target, string windowOptions, bool replaceEntry)
174                 {
175                         switch (target) {
176                                 case "_blank":
177                                         this.window.Open (urlString);
178                                         break;
179                                 case "_media":
180                                         break;
181                                 case "_parent":
182                                         this.window.Parent.Open (urlString);
183                                         break;
184                                 case "_search":
185                                         break;
186                                 case "_self":
187                                         this.window.Open (urlString);
188                                         break;
189                                 case "_top":
190                                         this.window.Top.Open (urlString);
191                                         break;
192                         }
193                         return this;
194                 }
195                 
196                 [MonoTODO("Opens in current window at the moment.")]
197                 public HtmlWindow OpenNew (string urlString, string windowOptions)
198                 {
199                         return Open (urlString, "_blank", windowOptions, false);
200                 }
201
202                 [MonoTODO("Opens in current window at the moment.")]
203                 public HtmlWindow OpenNew (Uri url, string windowOptions)
204                 {
205                         return OpenNew (url.ToString (), windowOptions);
206                 }
207
208                 public void AttachEventHandler (string eventName, EventHandler eventHandler)
209                 {
210                         this.window.AttachEventHandler (eventName, eventHandler);
211                 }
212                 
213                 public void Close ()
214                 {
215                         throw new NotImplementedException ();
216                 }
217                 
218                 public void DetachEventHandler (string eventName, EventHandler eventHandler)
219                 {
220                         this.window.DetachEventHandler (eventName, eventHandler);
221                 }
222                 
223                 public void Focus ()
224                 {
225                         this.window.Focus ();
226                 }
227                 
228                 public void MoveTo (Point point)
229                 {
230                         throw new NotImplementedException ();
231                 }
232                 
233                 public void MoveTo (int x, int y)
234                 {
235                         throw new NotImplementedException ();
236                 }
237                 
238                 public void RemoveFocus ()
239                 {
240                         webHost.FocusOut ();
241                 }
242                 
243                 public void ResizeTo (Size size)
244                 {
245                         throw new NotImplementedException ();
246                 }
247                 
248                 public void ResizeTo (int width, int height)
249                 {
250                         throw new NotImplementedException ();
251                 }
252 #endregion
253
254 #region Events
255                 static object ErrorEvent = new object ();
256                 public event HtmlElementErrorEventHandler Error
257                 {
258                         add { 
259                                 Events.AddHandler (ErrorEvent, value); 
260                                 window.Error += new EventHandler (OnError);
261                         }
262                         remove { 
263                                 Events.RemoveHandler (ErrorEvent, value); 
264                                 window.Error -= new EventHandler (OnError);
265                         }
266                 }
267
268                 internal void OnError (object sender, EventArgs ev)
269                 {
270                         HtmlElementErrorEventHandler eh = (HtmlElementErrorEventHandler) (Events[ErrorEvent]);
271                         if (eh != null) {
272                                 HtmlElementErrorEventArgs e = new HtmlElementErrorEventArgs (String.Empty, 0, null);
273                                 eh (this, e);
274                         }
275                 }
276
277                 static object GotFocusEvent = new object ();
278                 public event HtmlElementEventHandler GotFocus
279                 {
280                         add { 
281                                 Events.AddHandler (GotFocusEvent, value);
282                                 window.OnFocus += new EventHandler (OnGotFocus);
283                         }
284                         remove { 
285                                 Events.RemoveHandler (GotFocusEvent, value); 
286                                 window.OnFocus -= new EventHandler (OnGotFocus);
287                         }
288                 }
289
290                 internal void OnGotFocus (object sender, EventArgs ev)
291                 {
292                         HtmlElementEventHandler eh = (HtmlElementEventHandler) (Events[GotFocusEvent]);
293                         if (eh != null) {
294                                 HtmlElementEventArgs e = new HtmlElementEventArgs ();
295                                 eh (this, e);
296                         }               
297                 }
298
299                 static object LostFocusEvent = new object ();
300                 public event HtmlElementEventHandler LostFocus
301                 {
302                         add { 
303                                 Events.AddHandler (LostFocusEvent, value); 
304                                 window.OnBlur += new EventHandler (OnLostFocus);
305                         }
306                         remove { 
307                                 Events.RemoveHandler (LostFocusEvent, value); 
308                                 window.OnBlur -= new EventHandler (OnLostFocus);
309                         }
310                 }
311
312                 internal void OnLostFocus (object sender, EventArgs ev)
313                 {
314                         HtmlElementEventHandler eh = (HtmlElementEventHandler) (Events[LostFocusEvent]);
315                         if (eh != null) {
316                                 HtmlElementEventArgs e = new HtmlElementEventArgs ();
317                                 eh (this, e);
318                         }
319                 }
320
321                 static object LoadEvent = new object ();
322                 public event HtmlElementEventHandler Load
323                 {
324                         add { 
325                                 Events.AddHandler (LoadEvent, value); 
326                                 window.Load += new EventHandler (OnLoad);
327                         }
328                         remove { 
329                                 Events.RemoveHandler (LoadEvent, value); 
330                                 window.Load -= new EventHandler (OnLoad);
331                         }
332                 }
333
334                 internal void OnLoad (object sender, EventArgs ev)
335                 {
336                         HtmlElementEventHandler eh = (HtmlElementEventHandler) (Events[LoadEvent]);
337                         if (eh != null) {
338                                 HtmlElementEventArgs e = new HtmlElementEventArgs ();
339                                 eh (this, e);
340                         }
341                 }
342
343                 static object UnloadEvent = new object ();
344                 public event HtmlElementEventHandler Unload {
345                         add { 
346                                 Events.AddHandler (UnloadEvent, value); 
347                                 window.Unload += new EventHandler (OnUnload);
348                         }
349                         remove { 
350                                 Events.RemoveHandler (UnloadEvent, value); 
351                                 window.Unload -= new EventHandler (OnUnload);
352                         }
353                 }
354
355                 internal void OnUnload (object sender, EventArgs ev)
356                 {
357                         HtmlElementEventHandler eh = (HtmlElementEventHandler) (Events[UnloadEvent]);
358                         if (eh != null) {
359                                 HtmlElementEventArgs e = new HtmlElementEventArgs ();
360                                 eh (this, e);
361                         }
362                 }
363
364                 static object ScrollEvent = new object ();
365                 public event HtmlElementEventHandler Scroll {
366                         add { 
367                                 Events.AddHandler (ScrollEvent, value);
368                                 window.Scroll += new EventHandler (OnScroll);
369                         }
370                         remove { 
371                                 Events.RemoveHandler (ScrollEvent, value);
372                                 window.Scroll -= new EventHandler (OnScroll);
373                         }
374                 }
375
376                 internal void OnScroll (object sender, EventArgs ev)
377                 {
378                         HtmlElementEventHandler eh = (HtmlElementEventHandler) (Events[ScrollEvent]);
379                         if (eh != null) {
380                                 HtmlElementEventArgs e = new HtmlElementEventArgs ();
381                                 eh (this, e);
382                         }
383                 }
384
385                 static object ResizeEvent = new object ();
386                 public event HtmlElementEventHandler Resize {
387                         add { Events.AddHandler (ResizeEvent, value); }
388                         remove { Events.RemoveHandler (ResizeEvent, value); }
389                 }
390
391                 internal void OnResize (object sender, EventArgs ev)
392                 {
393                         HtmlElementEventHandler eh = (HtmlElementEventHandler) (Events[ResizeEvent]);
394                         if (eh != null) {
395                                 HtmlElementEventArgs e = new HtmlElementEventArgs ();
396                                 eh (this, e);
397                         }
398                 }
399 #endregion
400
401 #region Standard stuff
402                 public override int GetHashCode ()
403                 {
404                         if (window == null)
405                                 return 0;                                               
406                         return window.GetHashCode ();
407                 }
408
409                 public override bool Equals (object obj)
410                 {
411                         return this == (HtmlWindow) obj;
412                 }
413                 
414                 public static bool operator == (HtmlWindow left, HtmlWindow right)
415                 {
416                         if ((object)left == (object)right)
417                                 return true;
418
419                         if ((object)left == null || (object)right == null)
420                                 return false;
421
422                         return left.window.Equals (right.window);
423                 }
424
425                 public static bool operator != (HtmlWindow left, HtmlWindow right)
426                 {
427                         return !(left == right);
428                 }
429 #endregion
430         }
431 }