Update class library csproj files
[mono.git] / mcs / class / Mono.WebBrowser / Mono.Mozilla / Callback.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 Novell, Inc.
21 //
22 // Authors:
23 //      Andreia Gaita (avidigal@novell.com)
24 //
25
26 #undef debug
27
28 using System;
29 using System.Text;
30 using System.Runtime.InteropServices;
31 using System.Diagnostics;
32
33 namespace Mono.Mozilla {
34
35         using Mono.WebBrowser;
36         using Mono.WebBrowser.DOM;
37         
38         internal class Callback
39         {
40                 WebBrowser owner;
41                 string currentUri;
42                 bool calledLoadStarted;
43                 
44                 public Callback (WebBrowser owner) 
45                 {
46                         this.owner = owner;
47                         
48                 }
49
50                 #region Events
51
52                 public void OnWidgetLoaded ()
53                 {
54 #if debug
55                         OnGeneric ("OnWidgetLoaded");
56 #endif
57                 }
58
59
60                 public void OnStateChange (nsIWebProgress progress, nsIRequest request, Int32 status, UInt32 state)
61                 {
62                         if (!owner.created)
63                                 owner.created = true;
64
65 #if debug
66                         //OnGeneric ("OnStateChange");
67
68                         System.Text.StringBuilder s = new System.Text.StringBuilder ();
69                         if ((state & (uint) StateFlags.Start) != 0) {
70                                 s.Append ("Start\t");
71                         }
72                         if ((state & (uint) StateFlags.Redirecting) != 0) {
73                                 s.Append ("Redirecting\t");
74                         }
75                         if ((state & (uint) StateFlags.Transferring) != 0) {
76                                 s.Append ("Transferring\t");
77                         }
78                         if ((state & (uint) StateFlags.Negotiating) != 0) {
79                                 s.Append ("Negotiating\t");
80                         }
81                         if ((state & (uint) StateFlags.Stop) != 0) {
82                                 s.Append ("Stop\t");
83                         }
84                         if ((state & (uint) StateFlags.IsRequest) != 0) {
85                                 s.Append ("Request\t");
86                         }
87                         if ((state & (uint) StateFlags.IsDocument) != 0) {
88                                 s.Append ("Document\t");
89                         }
90                         if ((state & (uint) StateFlags.IsNetwork) != 0) {
91                                 s.Append ("Network\t");
92                         }
93                         if ((state & (uint) StateFlags.IsWindow) != 0) {
94                                 s.Append ("Window\t");
95                         }
96                         Console.Error.WriteLine (s.ToString ());
97 #endif
98
99                         bool _start = (state & (uint) StateFlags.Start) != 0;
100                         bool _transferring = (state & (uint) StateFlags.Transferring) != 0;
101                         bool _redirecting = (state & (uint) StateFlags.Redirecting) != 0;
102                         bool _stop = (state & (uint) StateFlags.Stop) != 0;
103                         bool _request = (state & (uint) StateFlags.IsRequest) != 0;
104                         bool _document = (state & (uint) StateFlags.IsDocument) != 0;
105                         bool _network = (state & (uint) StateFlags.IsNetwork) != 0;
106                         bool _window = (state & (uint) StateFlags.IsWindow) != 0;
107
108                         if (_start && _request && _document && !calledLoadStarted) {
109                                 nsIDOMWindow win;
110                                 progress.getDOMWindow (out win);
111                                 nsIChannel channel = (nsIChannel) request;
112                                 nsIURI uri;
113                                 channel.getURI (out uri);
114                                 if (uri == null)
115                                         currentUri = "about:blank";
116                                 else {
117                                         AsciiString spec = new AsciiString (String.Empty);
118                                         uri.getSpec (spec.Handle);
119                                         currentUri = spec.ToString ();
120                                 }
121
122                                 calledLoadStarted = true;
123                                 LoadStartedEventHandler eh = (LoadStartedEventHandler) (owner.Events [WebBrowser.LoadStartedEvent]);
124                                 if (eh != null) {
125
126                                         AsciiString name = new AsciiString (String.Empty);
127                                         win.getName (name.Handle);
128
129                                         LoadStartedEventArgs e = new LoadStartedEventArgs (currentUri, name.ToString ());
130                                         eh (this, e);
131                                         if (e.Cancel)
132                                                 request.cancel (2152398850); //NS_BINDING_ABORTED
133                                 }
134                                 return;
135
136                         }
137
138                         if (_document && _request && _transferring) {
139                                 nsIDOMWindow win;
140                                 progress.getDOMWindow (out win);
141                                 nsIChannel channel = (nsIChannel) request;
142                                 nsIURI uri;
143                                 channel.getURI (out uri);
144                                 if (uri == null)
145                                         currentUri = "about:blank";
146                                 else {
147                                         AsciiString spec = new AsciiString (String.Empty);
148                                         uri.getSpec (spec.Handle);
149                                         currentUri = spec.ToString ();
150                                 }
151
152                                 nsIDOMWindow topWin;
153                                 win.getTop (out topWin);
154                                 if (topWin == null || topWin.GetHashCode () == win.GetHashCode ()) {
155                                         owner.Reset ();
156                                         nsIDOMDocument doc;
157                                         win.getDocument (out doc);
158                                         if (doc != null)
159                                                 owner.document = new Mono.Mozilla.DOM.Document (owner, doc);
160                                 }
161
162                                 LoadCommitedEventHandler eh = (LoadCommitedEventHandler) (owner.Events[WebBrowser.LoadCommitedEvent]);
163                                 if (eh != null) {
164                                         LoadCommitedEventArgs e = new LoadCommitedEventArgs (currentUri);
165                                         eh (this, e);
166                                 }
167                                 return;
168                         }
169
170                         if (_document && _request && _redirecting) {
171                                 nsIDOMWindow win;
172                                 progress.getDOMWindow (out win);
173                                 nsIChannel channel = (nsIChannel) request;
174                                 nsIURI uri;
175                                 channel.getURI (out uri);
176                                 if (uri == null)
177                                         currentUri = "about:blank";
178                                 else {
179                                         AsciiString spec = new AsciiString (String.Empty);
180                                         uri.getSpec (spec.Handle);
181                                         currentUri = spec.ToString ();
182                                 }
183                                 return;
184                         }
185
186                         if (_stop && !_request && !_document && _network && _window) {
187                                 calledLoadStarted = false;
188                             LoadFinishedEventHandler eh1 = (LoadFinishedEventHandler) (owner.Events[WebBrowser.LoadFinishedEvent]);
189                             if (eh1 != null) {
190
191                                         nsIDOMWindow win;
192                                         progress.getDOMWindow (out win);
193                                 LoadFinishedEventArgs e = new LoadFinishedEventArgs (currentUri);
194                                 eh1 (this, e);
195
196                             }
197                                 return;
198                         }
199
200                         if (_stop && !_request && _document && !_network && !_window) {
201                                 nsIDOMWindow win;
202                                 progress.getDOMWindow (out win);
203                                 nsIDOMDocument doc;
204                                 win.getDocument (out doc);
205                                 if (doc != null) {
206                                         int hash = doc.GetHashCode ();
207                                         if (owner.documents.ContainsKey (hash)) {
208                                                 DOM.Document document = owner.documents[hash] as DOM.Document;
209                                                 
210                                                 EventHandler eh1 = (EventHandler)(document.Events[DOM.Document.LoadStoppedEvent]);
211                                                 if (eh1 != null)
212                                                         eh1 (this, null);
213                                     }
214                                 }
215                                 calledLoadStarted = false;
216                                 return;
217                         } 
218 #if debug
219                         Console.Error.WriteLine ("{0} completed", s.ToString ());
220 #endif
221                 }
222
223                 public void OnProgress (nsIWebProgress progress, nsIRequest request, Int32 currentTotalProgress, Int32 maxTotalProgress)
224                 {
225 #if debug
226                         OnGeneric ("OnProgress");
227 #endif                  
228                         ProgressChangedEventHandler eh = (ProgressChangedEventHandler) (owner.Events [Mono.Mozilla.WebBrowser.ProgressChangedEvent]);
229                     if (eh != null) {
230                         Mono.WebBrowser.ProgressChangedEventArgs e = new Mono.WebBrowser.ProgressChangedEventArgs (currentTotalProgress, maxTotalProgress);
231                         eh (this, e);
232                     }
233                 }
234
235                 public void OnLocationChanged (nsIWebProgress progress, nsIRequest request, nsIURI uri)
236                 {
237 #if debug
238                         OnGeneric ("OnLocationChanged");
239 #endif
240                 }
241
242                 public void OnStatusChange (nsIWebProgress progress, nsIRequest request, string message, Int32 status)
243                 {
244                         StatusChangedEventHandler eh = (StatusChangedEventHandler) (owner.Events[WebBrowser.StatusChangedEvent]);
245                         if (eh != null) {
246                                 StatusChangedEventArgs e = new StatusChangedEventArgs (message, status);
247                                 eh (this, e);
248                         }
249                 }
250
251                 public void OnSecurityChange (nsIWebProgress progress, nsIRequest request, uint status)
252                 {
253                         SecurityChangedEventHandler eh = (SecurityChangedEventHandler) (owner.Events[WebBrowser.SecurityChangedEvent]);
254                         if (eh != null) {
255                                 SecurityLevel state = SecurityLevel.Insecure;
256                                 switch (status) {
257                                 case 4: 
258                                         state = SecurityLevel.Insecure;
259                                         break;
260                                 case 1:
261                                         state = SecurityLevel.Mixed;
262                                         break;
263                                 case 2:
264                                         state = SecurityLevel.Secure;
265                                         break;
266                                 }
267
268                                 SecurityChangedEventArgs e = new SecurityChangedEventArgs (state);
269                                 eh (this, e);
270                         }
271                 }
272
273                 public bool OnClientDomKeyDown (KeyInfo keyInfo, ModifierKeys modifiers, nsIDOMNode target)
274                 {
275 #if debug
276                         OnGeneric ("OnClientDomKeyDown");
277                         Console.Error.WriteLine ("OnClientDomKeyDown");
278 #endif
279                         INode node = new Mono.Mozilla.DOM.Node (owner, target);                 
280                         string key = String.Intern (node.GetHashCode () + ":keydown");
281                         EventHandler eh1 = (EventHandler) owner.DomEvents[key];
282                         if (eh1 != null) {
283                                 EventArgs e1 = new EventArgs ();
284                                 eh1 (node, e1);
285                         }
286                         
287                         NodeEventHandler eh = (NodeEventHandler) (owner.Events[WebBrowser.KeyDownEvent]);
288                         if (eh != null) {
289                                 NodeEventArgs e = new NodeEventArgs (node);
290                                 eh (this, e);
291                                 return true;
292                         }
293                         return false;
294                 }
295
296                 public bool OnClientDomKeyUp (KeyInfo keyInfo, ModifierKeys modifiers, nsIDOMNode target)
297                 {
298 #if debug
299                         OnGeneric ("OnClientDomKeyUp");
300                         Console.Error.WriteLine ("OnClientDomKeyUp");
301 #endif
302                         INode node = new Mono.Mozilla.DOM.Node (owner, target);                 
303                         string key = String.Intern (node.GetHashCode () + ":keyup");
304                         EventHandler eh1 = (EventHandler) owner.DomEvents[key];
305                         if (eh1 != null) {
306                                 EventArgs e1 = new EventArgs ();
307                                 eh1 (node, e1);
308                         }
309                         
310                         NodeEventHandler eh = (NodeEventHandler) (owner.Events[WebBrowser.KeyUpEvent]);
311                         if (eh != null) {
312                                 NodeEventArgs e = new NodeEventArgs (node);
313                                 eh (this, e);
314                                 return true;
315                         }
316                         return false;
317                 }
318
319                 public bool OnClientDomKeyPress (KeyInfo keyInfo, ModifierKeys modifiers, nsIDOMNode target)
320                 {
321 #if debug
322                         OnGeneric ("OnClientDomKeyPress");
323                         Console.Error.WriteLine ("OnClientDomKeyPress");
324 #endif
325                         INode node = new Mono.Mozilla.DOM.Node (owner, target);                 
326                         string key = String.Intern (node.GetHashCode () + ":keypress");
327                         EventHandler eh1 = (EventHandler) owner.DomEvents[key];
328                         if (eh1 != null) {
329                                 EventArgs e1 = new EventArgs ();
330                                 eh1 (node, e1);
331                         }
332                         
333                         NodeEventHandler eh = (NodeEventHandler) (owner.Events[WebBrowser.KeyPressEvent]);
334                         if (eh != null) {
335                                 NodeEventArgs e = new NodeEventArgs (node);
336                                 eh (this, e);
337                                 return true;
338                         }
339                         return false;
340                 }
341
342                 public bool OnClientMouseDown (MouseInfo mouseInfo, ModifierKeys modifiers, nsIDOMNode target)
343                 {
344 #if debug
345                         OnGeneric ("OnClientMouseDown");
346                         Console.Error.WriteLine ("OnClientMouseDown");
347 #endif
348                         INode node = new Mono.Mozilla.DOM.Node (owner, target);                 
349                         string key = String.Intern (node.GetHashCode () + ":mousedown");
350                         EventHandler eh1 = (EventHandler) owner.DomEvents[key];
351                         if (eh1 != null) {
352                                 EventArgs e1 = new EventArgs ();
353                                 eh1 (node, e1);
354                         }
355                         
356                         NodeEventHandler eh = (NodeEventHandler) (owner.Events[WebBrowser.MouseDownEvent]);
357                         if (eh != null) {
358                                 NodeEventArgs e = new NodeEventArgs (node);
359                                 eh (this, e);
360                                 return true;
361                         }
362                         return false;
363                 }
364
365                 public bool OnClientMouseUp (MouseInfo mouseInfo, ModifierKeys modifiers, nsIDOMNode target)
366                 {
367 #if debug
368                         OnGeneric ("OnClientMouseUp");
369                         Console.Error.WriteLine ("OnClientMouseUp");
370 #endif
371                         INode node = new Mono.Mozilla.DOM.Node (owner, target);                 
372                         string key = String.Intern (node.GetHashCode () + ":mouseup");
373                         EventHandler eh1 = (EventHandler) owner.DomEvents[key];
374                         if (eh1 != null) {
375                                 EventArgs e1 = new EventArgs ();
376                                 eh1 (node, e1);
377                         }
378                         
379                         NodeEventHandler eh = (NodeEventHandler) (owner.Events[WebBrowser.MouseUpEvent]);
380                         if (eh != null) {
381                                 NodeEventArgs e = new NodeEventArgs (node);
382                                 eh (this, e);
383                                 return true;
384                         }
385                         return false;
386                 }
387
388                 public bool OnClientMouseClick (MouseInfo mouseInfo, ModifierKeys modifiers, nsIDOMNode target)
389                 {
390 #if debug
391                         OnGeneric ("OnClientMouseClick");
392                         Console.Error.WriteLine ("OnClientMouseClick");
393 #endif
394                         INode node = new Mono.Mozilla.DOM.Node (owner, target);                 
395                         string key = String.Intern (node.GetHashCode () + ":click");
396                         EventHandler eh1 = (EventHandler) owner.DomEvents[key];
397                         if (eh1 != null) {
398                                 EventArgs e1 = new EventArgs ();
399                                 eh1 (node, e1);
400                         }
401                         
402                         NodeEventHandler eh = (NodeEventHandler) (owner.Events[WebBrowser.MouseClickEvent]);
403                         if (eh != null) {
404                                 NodeEventArgs e = new NodeEventArgs (node);
405                                 eh (this, e);
406                                 return true;
407                         }
408                         return false;
409                 }
410
411                 public bool OnClientMouseDoubleClick (MouseInfo mouseInfo, ModifierKeys modifiers, nsIDOMNode target)
412                 {
413 #if debug
414                         OnGeneric ("OnClientMouseDoubleClick");
415                         Console.Error.WriteLine ("OnClientMouseDoubleClick");
416 #endif
417                         INode node = new Mono.Mozilla.DOM.Node (owner, target);                 
418                         string key = String.Intern (node.GetHashCode () + ":dblclick");
419                         EventHandler eh1 = (EventHandler) owner.DomEvents[key];
420                         if (eh1 != null) {
421                                 EventArgs e1 = new EventArgs ();
422                                 eh1 (node, e1);
423                         }
424                         
425                         NodeEventHandler eh = (NodeEventHandler) (owner.Events[WebBrowser.MouseDoubleClickEvent]);
426                         if (eh != null) {
427                                 NodeEventArgs e = new NodeEventArgs (node);
428                                 eh (this, e);
429                                 return true;
430                         }
431                         return false;
432                 }
433
434                 public bool OnClientMouseOver (MouseInfo mouseInfo, ModifierKeys modifiers, nsIDOMNode target)
435                 {
436 #if debug
437                         OnGeneric ("OnClientMouseOver");
438                         Console.Error.WriteLine ("OnClientMouseOver");
439 #endif
440                         DOM.DOMObject helper = new DOM.DOMObject(this.owner);
441                         INode node = helper.GetTypedNode  (target);
442                         string key = String.Intern (node.GetHashCode () + ":mouseover");
443                         EventHandler eh1 = (EventHandler) owner.DomEvents[key];
444                         if (eh1 != null) {
445                                 EventArgs e1 = new EventArgs ();
446                                 eh1 (node, e1);
447                         }
448                         
449                         NodeEventHandler eh = (NodeEventHandler) (owner.Events[WebBrowser.MouseEnterEvent]);
450                         if (eh != null) {
451                                 NodeEventArgs e = new NodeEventArgs (node);
452                                 eh (node, e);
453                                 return true;
454                         }
455                         return false;
456                 }
457
458                 public bool OnClientMouseOut (MouseInfo mouseInfo, ModifierKeys modifiers, nsIDOMNode target)
459                 {
460 #if debug
461                         OnGeneric ("OnClientMouseOut");
462                         Console.Error.WriteLine ("OnClientMouseOut");
463 #endif
464                         INode node = new Mono.Mozilla.DOM.Node (owner, target);                 
465                         string key = String.Intern (node.GetHashCode () + ":mouseout");
466                         EventHandler eh1 = (EventHandler) owner.DomEvents[key];
467                         if (eh1 != null) {
468                                 EventArgs e1 = new EventArgs ();
469                                 eh1 (node, e1);
470                         }
471                         
472                         NodeEventHandler eh = (NodeEventHandler) (owner.Events[WebBrowser.MouseLeaveEvent]);
473                         if (eh != null) {
474                                 NodeEventArgs e = new NodeEventArgs (node);
475                                 eh (this, e);
476                                 return true;
477                         }
478                         return false;
479                 }
480
481                 public bool OnClientActivate ()
482                 {
483 #if debug
484                         OnGeneric ("OnClientActivate");
485                         Console.Error.WriteLine ("OnClientActivate");
486 #endif
487                         // TODO:  Add WebBrowser.OnClientActivate implementation
488                         return false;
489                 }
490
491                 public bool OnClientFocus ()
492                 {
493 #if debug
494                         OnGeneric ("OnClientFocus");
495                         Console.Error.WriteLine ("OnClientFocus");
496 #endif
497                         EventHandler eh = (EventHandler) (owner.Events[WebBrowser.FocusEvent]);
498                         if (eh != null) {
499                                 EventArgs e = new EventArgs ();
500                                 eh (this, e);
501                         }
502                         return false;
503                 }
504
505                 public bool OnClientBlur ()
506                 {
507 #if debug
508                         OnGeneric ("OnClientBlur");
509                         Console.Error.WriteLine ("OnClientBlur");
510 #endif
511                         EventHandler eh = (EventHandler) (owner.Events[WebBrowser.BlurEvent]);
512                         if (eh != null) {
513                                 EventArgs e = new EventArgs ();
514                                 eh (this, e);
515                         }
516                         return false;
517                 }
518
519                 public bool OnCreateNewWindow ()
520                 {
521                         bool ret = false;
522
523 #if debug
524                         OnGeneric ("OnCreateNewWindow");
525                         Console.Error.WriteLine ("OnCreateNewWindow");
526 #endif
527                         CreateNewWindowEventHandler eh = (CreateNewWindowEventHandler) (owner.Events[WebBrowser.CreateNewWindowEvent]);
528                         if (eh != null) {
529                                 CreateNewWindowEventArgs e = new CreateNewWindowEventArgs (false);
530                                 ret = eh (this, e);
531                         }
532                         return ret;
533                 }
534
535                 public void OnAlert (IntPtr title, IntPtr text)
536                 {
537 #if debug
538                         OnGeneric ("OnAlert");
539                         Console.Error.WriteLine ("OnAlert");
540 #endif
541                         AlertEventHandler eh = (AlertEventHandler) (owner.Events[WebBrowser.AlertEvent]);
542                         if (eh != null) {
543                                 AlertEventArgs e = new AlertEventArgs ();
544                                 e.Type = DialogType.Alert;
545                                 if (title != IntPtr.Zero)
546                                         e.Title = Marshal.PtrToStringUni (title);
547                                 if (text != IntPtr.Zero)
548                                         e.Text = Marshal.PtrToStringUni (text);
549                                 eh (this, e);
550                         }
551                 }
552
553                 public bool OnAlertCheck (IntPtr title, IntPtr text, IntPtr chkMsg, ref bool chkState)
554                 {
555 #if debug
556                         OnGeneric ("OnAlertCheck");
557                         Console.Error.WriteLine ("OnAlertCheck");
558 #endif
559                         AlertEventHandler eh = (AlertEventHandler) (owner.Events[WebBrowser.AlertEvent]);
560                         if (eh != null) {
561                                 AlertEventArgs e = new AlertEventArgs ();
562                                 e.Type = DialogType.AlertCheck;
563                                 if (title != IntPtr.Zero)
564                                         e.Title = Marshal.PtrToStringUni (title);
565                                 if (text != IntPtr.Zero)
566                                         e.Text = Marshal.PtrToStringUni (text);
567                                 if (chkMsg != IntPtr.Zero)
568                                         e.CheckMessage = Marshal.PtrToStringUni (chkMsg);
569                                 e.CheckState = chkState;
570                                 eh (this, e);
571                                 return e.BoolReturn;
572                         }
573                         return false;
574                 }
575
576                 public bool OnConfirm (IntPtr title, IntPtr text)
577                 {
578 #if debug
579                         OnGeneric ("OnConfirm");
580                         Console.Error.WriteLine ("OnConfirm");
581 #endif
582                         AlertEventHandler eh = (AlertEventHandler) (owner.Events[WebBrowser.AlertEvent]);
583                         if (eh != null) {
584                                 AlertEventArgs e = new AlertEventArgs ();
585                                 e.Type = DialogType.Confirm;
586                                 if (title != IntPtr.Zero)
587                                         e.Title = Marshal.PtrToStringUni (title);
588                                 if (text != IntPtr.Zero)
589                                         e.Text = Marshal.PtrToStringUni (text);
590                                 eh (this, e);
591                                 return e.BoolReturn;
592                         }
593                         return false;
594                 }
595
596                 public bool OnConfirmCheck (IntPtr title, IntPtr text, IntPtr chkMsg, ref bool chkState)
597                 {
598 #if debug
599                         OnGeneric ("OnConfirmCheck");
600                         Console.Error.WriteLine ("OnConfirmCheck");
601 #endif
602                         AlertEventHandler eh = (AlertEventHandler) (owner.Events[WebBrowser.AlertEvent]);
603                         if (eh != null) {
604                                 AlertEventArgs e = new AlertEventArgs ();
605                                 e.Type = DialogType.ConfirmCheck;
606                                 if (title != IntPtr.Zero)
607                                         e.Title = Marshal.PtrToStringUni (title);
608                                 if (text != IntPtr.Zero)
609                                         e.Text = Marshal.PtrToStringUni (text);
610                                 if (chkMsg != IntPtr.Zero)
611                                         e.CheckMessage = Marshal.PtrToStringUni (chkMsg);
612                                 e.CheckState = chkState;
613                                 eh (this, e);
614                                 chkState = e.CheckState;
615                                 return e.BoolReturn;
616                         }
617                         return false;
618                 }
619
620                 public bool OnConfirmEx (IntPtr title, IntPtr text, DialogButtonFlags flags,
621                                                                 IntPtr title0, IntPtr title1, IntPtr title2,
622                                                                 IntPtr chkMsg, ref bool chkState, out Int32 retVal)
623                 {
624 #if debug
625                         OnGeneric ("OnConfirmEx");
626                         Console.Error.WriteLine ("OnConfirmEx");
627 #endif
628                         retVal = -1;
629
630                         AlertEventHandler eh = (AlertEventHandler) (owner.Events[WebBrowser.AlertEvent]);
631                         if (eh != null) {
632                                 AlertEventArgs e = new AlertEventArgs ();
633                                 e.Type = DialogType.ConfirmEx;
634                                 if (title != IntPtr.Zero)
635                                         e.Title = Marshal.PtrToStringUni (title);
636                                 if (text != IntPtr.Zero)
637                                         e.Text = Marshal.PtrToStringUni (text);
638                                 if (chkMsg != IntPtr.Zero)
639                                         e.CheckMessage = Marshal.PtrToStringUni (chkMsg);
640                                 e.CheckState = chkState;
641                                 eh (this, e);
642                                 chkState = e.CheckState;
643                                 return e.BoolReturn;
644                         }
645                         return false;
646                 }
647
648                 public bool OnPrompt (IntPtr title, IntPtr text, ref IntPtr retVal)
649                 {
650 #if debug
651                         OnGeneric ("OnPrompt");
652                         Console.Error.WriteLine ("OnPrompt");
653 #endif
654                         AlertEventHandler eh = (AlertEventHandler) (owner.Events[WebBrowser.AlertEvent]);
655                         if (eh != null) {
656                                 AlertEventArgs e = new AlertEventArgs ();
657                                 e.Type = DialogType.Prompt;
658                                 if (title != IntPtr.Zero)
659                                         e.Title = Marshal.PtrToStringUni (title);
660                                 if (text != IntPtr.Zero)
661                                         e.Text = Marshal.PtrToStringUni (text);
662                                 if (retVal != IntPtr.Zero)
663                                         e.Text2 = Marshal.PtrToStringUni (retVal);
664                                 eh (this, e);
665                                 retVal = Marshal.StringToHGlobalUni (e.StringReturn);
666                                 return e.BoolReturn;
667                         }
668                         return false;
669                 }
670
671                 public bool OnPromptUsernameAndPassword (IntPtr title, IntPtr text, IntPtr chkMsg, ref bool chkState, out IntPtr username, out IntPtr password)
672                 {
673 #if debug
674                         OnGeneric ("OnPromptUsernameAndPassword");
675                         Console.Error.WriteLine ("OnPromptUsernameAndPassword");
676 #endif
677                         username = IntPtr.Zero;
678                         password = IntPtr.Zero;
679                         AlertEventHandler eh = (AlertEventHandler) (owner.Events[WebBrowser.AlertEvent]);
680                         if (eh != null) {
681                                 AlertEventArgs e = new AlertEventArgs ();
682                                 e.Type = DialogType.PromptUsernamePassword;
683                                 if (title != IntPtr.Zero)
684                                         e.Title = Marshal.PtrToStringUni (title);
685                                 if (text != IntPtr.Zero)
686                                         e.Text = Marshal.PtrToStringUni (text);
687                                 if (chkMsg != IntPtr.Zero)
688                                         e.CheckMessage = Marshal.PtrToStringUni (chkMsg);
689                                 e.CheckState = chkState;
690                                 eh (this, e);
691                                 return e.BoolReturn;
692                         }
693                         return false;
694                 }
695
696                 public bool OnPromptPassword (IntPtr title, IntPtr text, IntPtr chkMsg, ref bool chkState, out IntPtr password)
697                 {
698 #if debug
699                         OnGeneric ("OnPromptPassword");
700                         Console.Error.WriteLine ("OnPromptPassword");
701 #endif
702                         password = IntPtr.Zero;
703                         AlertEventHandler eh = (AlertEventHandler) (owner.Events[WebBrowser.AlertEvent]);
704                         if (eh != null) {
705                                 AlertEventArgs e = new AlertEventArgs ();
706                                 e.Type = DialogType.PromptPassword;
707                                 if (title != IntPtr.Zero)
708                                         e.Title = Marshal.PtrToStringUni (title);
709                                 if (text != IntPtr.Zero)
710                                         e.Text = Marshal.PtrToStringUni (text);
711                                 if (chkMsg != IntPtr.Zero)
712                                         e.CheckMessage = Marshal.PtrToStringUni (chkMsg);
713                                 e.CheckState = chkState;
714                                 eh (this, e);
715                                 return e.BoolReturn;
716                         }
717                         return false;
718                 }
719
720                 public bool OnSelect (IntPtr title, IntPtr text, uint count, IntPtr list, out int retVal)
721                 {
722 #if debug
723                         OnGeneric ("OnSelect");
724                         Console.Error.WriteLine ("OnSelect");
725 #endif
726                         retVal = 0;
727                         AlertEventHandler eh = (AlertEventHandler) (owner.Events[WebBrowser.AlertEvent]);
728                         if (eh != null) {
729                                 AlertEventArgs e = new AlertEventArgs ();
730                                 e.Type = DialogType.Select;
731                                 if (title != IntPtr.Zero)
732                                         e.Title = Marshal.PtrToStringUni (title);
733                                 if (text != IntPtr.Zero)
734                                         e.Text = Marshal.PtrToStringUni (text);
735                                 eh (this, e);
736                                 return e.BoolReturn;
737                         }
738                         return false;
739                 }
740
741                 public void OnLoad ()
742                 {
743 #if debug
744                         OnGeneric ("OnLoad");
745                         Console.Error.WriteLine ("OnLoad");
746 #endif
747                         ((DOM.Window)owner.Window).OnLoad ();
748                 }
749
750                 public void OnUnload ()
751                 {
752 #if debug
753                         OnGeneric ("OnUnload");
754                         Console.Error.WriteLine ("OnUnload");
755 #endif
756                         ((DOM.Window)owner.Window).OnUnload ();
757                 }
758                 
759                 public void OnShowContextMenu (UInt32 contextFlags, 
760                                                [MarshalAs (UnmanagedType.Interface)] nsIDOMEvent eve, 
761                                                [MarshalAs (UnmanagedType.Interface)] nsIDOMNode node)
762                 {
763 #if debug
764                         OnGeneric ("OnShowContextMenu");
765                         Console.Error.WriteLine ("OnShowContextMenu");
766 #endif
767                         ContextMenuEventHandler eh = (ContextMenuEventHandler) (owner.Events[WebBrowser.ContextMenuEvent]);
768
769                         if (eh != null) {
770                                 nsIDOMMouseEvent mouseEvent = (nsIDOMMouseEvent) eve;
771                                 int x, y;
772                                 mouseEvent.getClientX (out x);
773                                 mouseEvent.getClientY (out y);
774                                 ContextMenuEventArgs args = new ContextMenuEventArgs(x, y);
775                                 eh (owner, args);
776                         }
777                         
778                 }
779                 
780                 public void OnGeneric (string type)
781                 {
782 #if debug
783 //                      string t = Marshal.PtrToStringUni (type);
784                         Console.Error.WriteLine ("Callback Generic:{0}", type);
785 #endif
786                         EventHandler eh = (EventHandler) (owner.Events[WebBrowser.GenericEvent]);
787                         if (eh != null) {
788                                 EventArgs e = new EventArgs ();
789                                 eh (type, e);
790                                 return;
791                         }
792                         return;
793
794                 }
795                 #endregion
796                 
797         }
798
799 #region Delegates
800
801         internal delegate void CallbackVoid ();
802
803         internal delegate void CallbackString (string arg1);
804         internal delegate void CallbackWString ([MarshalAs(UnmanagedType.LPWStr)] string arg1);
805         
806         internal delegate void CallbackStringString (string arg1, string arg2);
807         internal delegate void CallbackStringInt                (string arg1, Int32 arg2);
808         internal delegate void CallbackWStringInt ([MarshalAs (UnmanagedType.LPWStr)] string arg1, Int32 arg2);
809         internal delegate void CallbackStringIntInt     (string arg1, Int32 arg2, Int32 arg3);
810         internal delegate void CallbackStringIntUint    (string arg1, Int32 arg2, UInt32 arg3);
811
812
813         internal delegate void CallbackIntInt                   (Int32 arg1, Int32 arg2);
814         internal delegate void CallbackIntUint                  (Int32 arg2, UInt32 arg3);
815
816         internal delegate void CallbackUint                     (UInt32 arg1);
817         internal delegate void CallbackUintInt                  (UInt32 arg1, Int32 arg2);
818
819         internal delegate void CallbackPtrPtr                   (IntPtr arg1, IntPtr arg2);
820
821         //Don't have to worry about marshalling bool, PRBool seems very constant and uses 4 bit int underneath
822         internal delegate void CallbackBool                     (bool val);
823         
824         internal delegate bool KeyCallback                      (KeyInfo keyInfo, ModifierKeys modifiers, [MarshalAs (UnmanagedType.Interface)] nsIDOMNode target);
825         internal delegate bool MouseCallback                    (MouseInfo mouseInfo, ModifierKeys modifiers, [MarshalAs (UnmanagedType.Interface)] nsIDOMNode target);
826
827         internal delegate void GenericCallback                  (IntPtr type);
828         
829
830         internal delegate bool Callback2                                ();
831         internal delegate bool Callback2String                  (string arg1);
832
833
834         internal delegate bool CallbackOnAlertCheck     (IntPtr title, IntPtr text, IntPtr chkMsg, ref bool chkState);
835         internal delegate bool CallbackOnConfirm                (IntPtr title, IntPtr text);
836         internal delegate bool CallbackOnConfirmCheck   (IntPtr title, IntPtr text, IntPtr chkMsg, ref bool chkState);
837         internal delegate bool CallbackOnConfirmEx      (IntPtr title, IntPtr text, Mono.WebBrowser.DialogButtonFlags flags, 
838                                                                                                                          IntPtr title0, IntPtr title1, IntPtr title2,
839                                                                                                                          IntPtr chkMsg, ref bool chkState, out Int32 retVal);
840         internal delegate bool CallbackOnPrompt         (IntPtr title, IntPtr text,
841                                                                                                                          ref IntPtr retVal);
842         internal delegate bool CallbackOnPromptUsernameAndPassword 
843                                                                                                                         (IntPtr title, IntPtr text,
844                                                                                                                          IntPtr chkMsg, ref bool chkState, 
845                                                                                                                          out IntPtr username, out IntPtr password);
846         internal delegate bool CallbackOnPromptPassword
847                                                                                                                         (IntPtr title, IntPtr text,
848                                                                                                                          IntPtr chkMsg, ref bool chkState, 
849                                                                                                                          out IntPtr password);
850         internal delegate bool CallbackOnSelect         (IntPtr title, IntPtr text, 
851                                                                                                                          UInt32 count, IntPtr list, 
852                                                                                                                          out Int32 retVal);
853         
854         internal delegate void CallbackOnLocationChanged ([MarshalAs (UnmanagedType.Interface)] nsIWebProgress progress,
855                                                           [MarshalAs (UnmanagedType.Interface)] nsIRequest request,
856                                                           [MarshalAs (UnmanagedType.Interface)] nsIURI uri);
857
858         internal delegate void CallbackOnStatusChange ([MarshalAs (UnmanagedType.Interface)] nsIWebProgress progress,
859                                                        [MarshalAs (UnmanagedType.Interface)] nsIRequest request,
860                                                        [MarshalAs (UnmanagedType.LPWStr)] string message, Int32 status);
861         
862         internal delegate void CallbackOnSecurityChange ([MarshalAs (UnmanagedType.Interface)] nsIWebProgress progress,
863                                                        [MarshalAs (UnmanagedType.Interface)] nsIRequest request,
864                                                        uint status);
865
866         internal delegate void CallbackOnStateChange ([MarshalAs (UnmanagedType.Interface)] nsIWebProgress progress,
867                                                        [MarshalAs (UnmanagedType.Interface)] nsIRequest request,
868                                                        Int32 arg2, UInt32 arg3);
869         internal delegate void CallbackOnProgress ([MarshalAs (UnmanagedType.Interface)] nsIWebProgress progress,
870                                                        [MarshalAs (UnmanagedType.Interface)] nsIRequest request,
871                                                        Int32 arg2, Int32 arg3);
872         
873         internal delegate void CallbackOnShowContextMenu (UInt32 contextFlags, 
874                                                           [MarshalAs (UnmanagedType.Interface)] nsIDOMEvent eve, 
875                                                           [MarshalAs (UnmanagedType.Interface)] nsIDOMNode node);
876         
877
878
879 #endregion
880
881
882 #region Structs
883
884         [StructLayout (LayoutKind.Sequential)]
885         internal struct CallbackBinder {
886                 
887                 public CallbackVoid                     OnWidgetLoaded;
888
889                 public CallbackOnStateChange            OnStateChange;
890                 public CallbackOnProgress               OnProgress;
891                 public CallbackOnLocationChanged                OnLocationChanged;
892
893                 public CallbackOnStatusChange   OnStatusChange;
894                 public CallbackOnSecurityChange OnSecurityChange;
895
896                 public KeyCallback                      OnKeyDown;
897                 public KeyCallback                      OnKeyUp;
898                 public KeyCallback                      OnKeyPress;
899
900                 public MouseCallback            OnMouseDown;
901                 public MouseCallback            OnMouseUp;
902                 public MouseCallback            OnMouseClick;
903                 public MouseCallback            OnMouseDoubleClick;
904                 public MouseCallback            OnMouseOver;
905                 public MouseCallback            OnMouseOut;
906
907                 public Callback2                        OnActivate;
908                 public Callback2                        OnFocus;
909                 public Callback2                        OnBlur;
910
911                 public CallbackPtrPtr                                                   OnAlert;
912                 public CallbackOnAlertCheck                                             OnAlertCheck;
913                 public CallbackOnConfirm                                                OnConfirm;
914                 public CallbackOnConfirmCheck                                   OnConfirmCheck;
915                 public CallbackOnConfirmEx                                              OnConfirmEx;
916                 public CallbackOnPrompt                                                 OnPrompt;
917                 public CallbackOnPromptUsernameAndPassword              OnPromptUsernameAndPassword;
918                 public CallbackOnPromptPassword                                 OnPromptPassword;
919                 public CallbackOnSelect                                                 OnSelect;
920
921                 public CallbackVoid                             OnLoad;
922                 public CallbackVoid                             OnUnload;
923                 
924                 public CallbackOnShowContextMenu OnShowContextMenu;
925
926                 public CallbackWString          OnGeneric;
927                 
928                 internal CallbackBinder (Callback callback) {
929                         this.OnWidgetLoaded                     = new CallbackVoid (callback.OnWidgetLoaded);
930
931                         this.OnStateChange                      = new CallbackOnStateChange (callback.OnStateChange);
932
933                         this.OnProgress                         = new CallbackOnProgress (callback.OnProgress);
934                         this.OnLocationChanged          = new CallbackOnLocationChanged (callback.OnLocationChanged);
935                         this.OnStatusChange                     = new CallbackOnStatusChange (callback.OnStatusChange);
936                         this.OnSecurityChange           = new CallbackOnSecurityChange (callback.OnSecurityChange);
937
938                         this.OnKeyDown                          = new KeyCallback (callback.OnClientDomKeyDown);
939                         this.OnKeyUp                            = new KeyCallback (callback.OnClientDomKeyUp);
940                         this.OnKeyPress                         = new KeyCallback (callback.OnClientDomKeyPress);
941
942                         this.OnMouseDown                        = new MouseCallback (callback.OnClientMouseDown);
943                         this.OnMouseUp                          = new MouseCallback (callback.OnClientMouseUp);
944                         this.OnMouseClick                       = new MouseCallback (callback.OnClientMouseClick);
945                         this.OnMouseDoubleClick         = new MouseCallback (callback.OnClientMouseDoubleClick);
946                         this.OnMouseOver                        = new MouseCallback (callback.OnClientMouseOver);
947                         this.OnMouseOut                         = new MouseCallback (callback.OnClientMouseOut);
948
949                         this.OnActivate                         = new Callback2 (callback.OnClientActivate);
950                         this.OnFocus                            = new Callback2 (callback.OnClientFocus);
951                         this.OnBlur                                     = new Callback2 (callback.OnClientBlur);
952
953                         this.OnAlert                            = new CallbackPtrPtr (callback.OnAlert);
954                         this.OnAlertCheck                       = new CallbackOnAlertCheck (callback.OnAlertCheck);
955                         this.OnConfirm                          = new CallbackOnConfirm (callback.OnConfirm);
956                         this.OnConfirmCheck             = new CallbackOnConfirmCheck (callback.OnConfirmCheck);
957                         this.OnConfirmEx                        = new CallbackOnConfirmEx (callback.OnConfirmEx);
958                         this.OnPrompt                           = new CallbackOnPrompt (callback.OnPrompt);
959                         this.OnPromptUsernameAndPassword = new CallbackOnPromptUsernameAndPassword (callback.OnPromptUsernameAndPassword);
960                         this.OnPromptPassword           = new CallbackOnPromptPassword (callback.OnPromptPassword);
961                         this.OnSelect                           = new CallbackOnSelect (callback.OnSelect);
962
963                         this.OnLoad                             = new CallbackVoid (callback.OnLoad);
964                         this.OnUnload                           = new CallbackVoid (callback.OnUnload);
965                         
966                         this.OnShowContextMenu          = new CallbackOnShowContextMenu (callback.OnShowContextMenu);
967                         
968                         this.OnGeneric                          = new CallbackWString (callback.OnGeneric);
969                 }
970         }
971
972  
973         [StructLayout (LayoutKind.Sequential)]
974         internal struct SizeInfo
975         {
976                 public UInt32 width;
977                 public UInt32 height;
978         }
979
980         [StructLayout (LayoutKind.Sequential)]
981         internal struct ModifierKeys
982         {
983                 public Int32 altKey;
984                 public Int32 ctrlKey;
985                 public Int32 metaKey;
986                 public Int32 shiftKey;
987         }
988
989         [StructLayout (LayoutKind.Sequential)]
990         internal struct MouseInfo
991         {
992                 public UInt16 button;
993                 public Int32 clientX;
994                 public Int32 clientY;
995                 public Int32 screenX;
996                 public Int32 screenY;
997         }
998
999         [StructLayout (LayoutKind.Sequential)]
1000         internal struct KeyInfo
1001         {
1002                 public UInt32 charCode;
1003                 public UInt32 keyCode;
1004         }
1005         
1006         [Flags]
1007         internal enum StateFlags
1008         {
1009                 Start = 1,
1010                 Redirecting = 2,
1011                 Transferring = 4,
1012                 Negotiating = 8,
1013                 Stop = 16,
1014                 IsRequest = 65536,
1015                 IsDocument =    131072,
1016                 IsNetwork = 262144,
1017                 IsWindow = 524288,
1018                 Restoring = 16777216,
1019                 IsInsecure = 4,
1020                 IsBroken = 1,
1021                 IsSecure = 2,
1022                 SecureHigh = 262144,
1023                 SecureMed = 65536,
1024                 SecureLow = 131072
1025         }
1026 #endregion
1027 }