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