In System.Windows.Forms:
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms.X11Internal / XplatUIX11-new.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) 2004-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //      Chris Toshok    toshok@ximian.com
25 //
26 //
27
28 // NOTE:
29 //      This driver understands the following environment variables: (Set the var to enable feature)
30 //
31 //      MONO_XEXCEPTIONS        = throw an exception when a X11 error is encountered;
32 //                                by default a message is displayed but execution continues
33 //
34 //      MONO_XSYNC              = perform all X11 commands synchronous; this is slower but
35 //                                helps in debugging errors
36 //
37
38 // NOT COMPLETE
39
40 // define to log Window handles and relationships to stdout
41 #undef DriverDebug
42
43 // Extra detailed debug
44 #undef DriverDebugExtra
45 #undef DriverDebugParent
46 #undef DriverDebugCreate
47 #undef DriverDebugDestroy
48 #undef DriverDebugThreads
49 #undef DriverDebugXEmbed
50
51 using System;
52 using System.ComponentModel;
53 using System.Collections;
54 using System.Diagnostics;
55 using System.Drawing;
56 using System.Drawing.Drawing2D;
57 using System.Drawing.Imaging;
58 using System.IO;
59 using System.Net;
60 using System.Net.Sockets;
61 using System.Reflection;
62 using System.Runtime.InteropServices;
63 using System.Text;
64 using System.Threading;
65 using System.Windows.Forms;
66
67 /// X11 Version
68 namespace System.Windows.Forms.X11Internal {
69         internal class XplatUIX11_new : XplatUIDriver {
70                 #region Local Variables
71                 // General
72                 static volatile XplatUIX11_new Instance;
73                 static readonly object lockobj = new object ();
74                 static int RefCount;
75                 static bool themes_enabled;
76
77                 static Hashtable MessageQueues; // Holds our thread-specific X11ThreadQueues
78
79                 X11Display display;
80
81                 #endregion      // Local Variables
82                 #region Constructors
83                 private XplatUIX11_new() {
84                         // Handle singleton stuff first
85                         RefCount = 0;
86
87                         // Now regular initialization
88                         MessageQueues = Hashtable.Synchronized (new Hashtable(7));
89                         if (Xlib.XInitThreads() == 0) {
90                                 Console.WriteLine ("Failed XInitThreads.  The X11 event loop will not function properly");
91                         }
92                 }
93
94                 private void InitializeDisplay ()
95                 {
96                         display = new X11Display (Xlib.XOpenDisplay(IntPtr.Zero));
97
98                         Graphics.FromHdcInternal (display.Handle);
99                 }
100
101                 ~XplatUIX11_new() {
102                         // Remove our display handle from S.D
103                         Graphics.FromHdcInternal (IntPtr.Zero);
104                 }
105
106                 #endregion      // Constructors
107
108                 #region Singleton Specific Code
109                 public static XplatUIX11_new GetInstance() {
110                         lock (lockobj) {
111                                 if (Instance == null) {
112                                         Instance = new XplatUIX11_new ();
113
114                                         Instance.InitializeDisplay ();
115                                 }
116                                 RefCount++;
117                         }
118                         return Instance;
119                 }
120
121                 public int Reference {
122                         get {
123                                 return RefCount;
124                         }
125                 }
126                 #endregion
127
128                 #region Internal Methods
129                 internal static void Where() {
130                         Console.WriteLine("Here: {0}\n", GetInstance().display.WhereString());
131                 }
132
133                 #endregion      // Internal Methods
134
135                 #region Private Methods
136
137                 internal X11ThreadQueue ThreadQueue (Thread thread)
138                 {
139                         X11ThreadQueue  queue;
140
141                         queue = (X11ThreadQueue)MessageQueues[thread];
142                         if (queue == null) {
143                                 queue = new X11ThreadQueue(thread);
144                                 MessageQueues[thread] = queue;
145                         }
146
147                         return queue;
148                 }
149                 #endregion      // Private Methods
150
151
152                 #region Public Properties
153                 internal override int Caption {
154                         get { return 19; }
155                 }
156
157                 internal override Size CursorSize {
158                         get { return display.CursorSize; }
159                 }
160
161                 internal override bool DragFullWindows {
162                         get { return true; }
163                 } 
164
165                 internal override Size DragSize {
166                         get { return new Size(4, 4); }
167                 } 
168
169                 internal override Size FrameBorderSize { 
170                         get { return new Size (4, 4); }
171                 }
172
173                 internal override Size IconSize {
174                         get { return display.IconSize; }
175                 }
176
177                 internal override int KeyboardSpeed {
178                         get { return display.KeyboardSpeed; }
179                 }
180
181                 internal override int KeyboardDelay {
182                         get { return display.KeyboardSpeed; }
183                 }
184
185                 internal override Size MaxWindowTrackSize {
186                         get { return new Size (WorkingArea.Width, WorkingArea.Height); }
187                 } 
188
189                 internal override Size MinimizedWindowSize {
190                         get { return new Size(1, 1); }
191                 } 
192
193                 internal override Size MinimizedWindowSpacingSize {
194                         get { return new Size(1, 1); }
195                 } 
196
197                 internal override Size MinimumWindowSize {
198                         get { return new Size(1, 1); }
199                 } 
200
201                 internal override Size MinWindowTrackSize {
202                         get { return new Size(1, 1); }
203                 }
204
205                 internal override Keys ModifierKeys {
206                         get { return display.ModifierKeys; }
207                 }
208
209                 internal override Size SmallIconSize {
210                         get { return display.SmallIconSize; }
211                 }
212
213                 internal override int MouseButtonCount {
214                         get { return 3; /* FIXME - should detect this somehow.. */}
215                 } 
216
217                 internal override bool MouseButtonsSwapped {
218                         get { return false; /*FIXME - how to detect? */}
219                 } 
220
221                 internal override Size MouseHoverSize {
222                         get { return new Size (1, 1); }
223                 }
224
225                 internal override int MouseHoverTime {
226                         get { return display.MouseHoverTime; }
227                 }
228
229                 internal override bool MouseWheelPresent {
230                         get { return true; /* FIXME - how to detect? */ }
231                 } 
232
233                 internal override Rectangle VirtualScreen {
234                         get { return display.VirtualScreen; }
235                 } 
236
237                 internal override Rectangle WorkingArea {
238                         get { return display.WorkingArea; }
239                 }
240
241                 internal override bool ThemesEnabled {
242                         get { return XplatUIX11_new.themes_enabled; }
243                 }
244  
245
246                 #endregion      // Public properties
247
248                 #region Public Static Methods
249                 internal override IntPtr InitializeDriver()
250                 {
251                         lock (this) {
252                                 if (display == null)
253                                         display = new X11Display (Xlib.XOpenDisplay(IntPtr.Zero));
254                         }
255                         return IntPtr.Zero;
256                 }
257
258                 internal override void ShutdownDriver(IntPtr token)
259                 {
260                         lock (this) {
261                                 if (display != null) {
262                                         display.Close ();
263                                         display = null;
264                                 }
265                         }
266                 }
267
268                 internal override void EnableThemes()
269                 {
270                         themes_enabled = true;
271                 }
272
273                 internal override void Activate (IntPtr handle)
274                 {
275                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
276
277                         if (hwnd != null)
278                                 hwnd.Activate ();
279                 }
280
281                 internal override void AudibleAlert()
282                 {
283                         display.AudibleAlert ();
284                 }
285
286
287                 internal override void CaretVisible (IntPtr handle, bool visible)
288                 {
289                         display.CaretVisible (handle, visible);
290                 }
291
292                 // XXX this implementation should probably be shared between all non-win32 backends
293                 internal override bool CalculateWindowRect (ref Rectangle ClientRect, int Style, int ExStyle, Menu menu, out Rectangle WindowRect)
294                 {
295                         FormBorderStyle border_style;
296                         TitleStyle      title_style;
297                         bool border_static;
298                         int caption_height;
299                         int tool_caption_height;
300
301                         // XXX this method should be static on Hwnd, not X11Hwnd
302                         X11Hwnd.DeriveStyles (Style, ExStyle, out border_style, out border_static, out title_style,
303                                               out caption_height, out tool_caption_height);
304
305                         WindowRect = Hwnd.GetWindowRectangle(border_style, border_static, menu, title_style,
306                                                              caption_height, tool_caption_height,
307                                                              ClientRect);
308                         return true;
309                 }
310
311                 internal override void ClientToScreen (IntPtr handle, ref int x, ref int y)
312                 {
313                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
314
315                         if (hwnd != null)
316                                 hwnd.ClientToScreen (ref x, ref y);
317                 }
318
319                 internal override int[] ClipboardAvailableFormats (IntPtr handle)
320                 {
321                         return display.ClipboardAvailableFormats (handle);
322                 }
323
324                 internal override void ClipboardClose (IntPtr handle)
325                 {
326                         display.ClipboardClose (handle);
327                 }
328
329                 internal override int ClipboardGetID (IntPtr handle, string format)
330                 {
331                         return display.ClipboardGetID (handle, format);
332                 }
333
334                 internal override IntPtr ClipboardOpen (bool primary_selection)
335                 {
336                         return display.ClipboardOpen (primary_selection);
337                 }
338
339                 internal override object ClipboardRetrieve (IntPtr handle, int type, XplatUI.ClipboardToObject converter)
340                 {
341                         return display.ClipboardRetrieve (handle, type, converter);
342                 }
343
344                 internal override void ClipboardStore (IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter)
345                 {
346                         display.ClipboardStore (handle, obj, type, converter);
347                 }
348
349                 internal override void CreateCaret (IntPtr handle, int width, int height)
350                 {
351                         display.CreateCaret (handle, width, height);
352                 }
353
354                 internal override IntPtr CreateWindow (CreateParams cp)
355                 {
356                         X11Hwnd hwnd = new X11Hwnd (display);
357
358                         hwnd.CreateWindow (cp);
359
360                         return hwnd.Handle;
361                 }
362
363                 internal override IntPtr CreateWindow (IntPtr Parent, int X, int Y, int Width, int Height)
364                 {
365                         CreateParams create_params = new CreateParams();
366
367                         create_params.Caption = "";
368                         create_params.X = X;
369                         create_params.Y = Y;
370                         create_params.Width = Width;
371                         create_params.Height = Height;
372
373                         create_params.ClassName = XplatUI.DefaultClassName;
374                         create_params.ClassStyle = 0;
375                         create_params.ExStyle = 0;
376                         create_params.Parent = IntPtr.Zero;
377                         create_params.Param = 0;
378
379                         return CreateWindow (create_params);
380                 }
381
382                 internal override IntPtr DefineCursor (Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot)
383                 {
384                         return display.DefineCursor (bitmap, mask, cursor_pixel, mask_pixel, xHotSpot, yHotSpot);
385                 }
386
387                 internal override IntPtr DefineStdCursor (StdCursor id)
388                 {
389                         return display.DefineStdCursor (id);
390                 }
391
392                 internal override IntPtr DefWndProc(ref Message msg)
393                 {
394                         X11Hwnd hwnd = (X11Hwnd)Hwnd.GetObjectFromWindow(msg.HWnd);
395
396                         if (hwnd == null)
397                                 return IntPtr.Zero;
398
399                         return hwnd.DefWndProc (ref msg);
400                 }
401
402                 internal override void DestroyCaret (IntPtr handle)
403                 {
404                         display.DestroyCaret (handle);
405                 }
406
407                 internal override void DestroyCursor(IntPtr cursor)
408                 {
409                         display.DestroyCursor (cursor);
410                 }
411
412                 internal override void DestroyWindow (IntPtr handle) {
413                         X11Hwnd hwnd;
414
415                         hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
416
417                         if (hwnd == null) {
418 #if DriverDebug || DriverDebugDestroy
419                                 Console.WriteLine("window {0:X} already destroyed", handle.ToInt32());
420 #endif
421                                 return;
422                         }
423
424 #if DriverDebug || DriverDebugDestroy
425                         Console.WriteLine("Destroying window {0}", XplatUI.Window(hwnd.ClientWindow));
426 #endif
427
428                         display.DestroyWindow (hwnd);
429                 }
430
431                 internal override IntPtr DispatchMessage(ref MSG msg)
432                 {
433                         return display.DispatchMessage (ref msg);
434                 }
435
436                 internal override void DrawReversibleLine (Point start, Point end, Color backColor)
437                 {
438                         display.DrawReversibleLine (start, end, backColor);
439                 }
440
441                 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
442                 {
443                         display.FillReversibleRectangle (rectangle, backColor);
444                 }
445
446                 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style)
447                 {
448                         display.DrawReversibleFrame (rectangle, backColor, style);
449                 }
450
451                 internal override void DrawReversibleRectangle (IntPtr handle, Rectangle rect, int line_width)
452                 {
453                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
454
455                         if (hwnd != null)
456                                 hwnd.DrawReversibleRectangle (rect, line_width);
457                 }
458
459                 internal override void DoEvents ()
460                 {
461                         X11ThreadQueue queue = ThreadQueue (Thread.CurrentThread);
462                         display.DoEvents (queue);
463                 }
464
465                 internal override void EnableWindow (IntPtr handle, bool Enable)
466                 {
467                         Hwnd    hwnd;
468
469                         hwnd = Hwnd.ObjectFromHandle(handle);
470                         if (hwnd != null)
471                                 hwnd.Enabled = Enable;
472                 }
473
474                 internal override void EndLoop (Thread thread)
475                 {
476                         // This is where we one day will shut down the loop for the thread
477                 }
478
479
480                 internal override IntPtr GetActive()
481                 {
482                         X11Hwnd hwnd = display.GetActiveWindow ();
483
484                         return (hwnd == null) ? IntPtr.Zero : hwnd.Handle;
485                 }
486
487                 internal override Region GetClipRegion (IntPtr handle)
488                 {
489                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
490
491                         return (hwnd == null) ? null : hwnd.GetClipRegion ();
492                 }
493
494                 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y)
495                 {
496                         width = 20;
497                         height = 20;
498                         hotspot_x = 0;
499                         hotspot_y = 0;
500                 }
501
502                 internal override void GetDisplaySize(out Size size)
503                 {
504                         display.GetDisplaySize (out size);
505                 }
506
507                 internal override SizeF GetAutoScaleSize (Font font)
508                 {
509                         return display.GetAutoScaleSize (font);
510                 }
511
512                 // XXX this should be someplace shareable by all non-win32 backends..  like in Hwnd itself.
513                 // maybe a Hwnd.ParentHandle property
514                 internal override IntPtr GetParent (IntPtr handle)
515                 {
516                         Hwnd    hwnd;
517
518                         hwnd = Hwnd.ObjectFromHandle(handle);
519                         if (hwnd != null && hwnd.parent != null) {
520                                 return hwnd.parent.Handle;
521                         }
522                         return IntPtr.Zero;
523                 }
524
525                 internal override void GetCursorPos (IntPtr handle, out int x, out int y)
526                 {
527                         display.GetCursorPos ((X11Hwnd)Hwnd.ObjectFromHandle(handle),
528                                               out x, out y);
529                 }
530
531                 internal override IntPtr GetFocus()
532                 {
533                         return display.GetFocus ();
534                 }
535
536                 // XXX this should be shared amongst non-win32 backends
537                 internal override bool GetFontMetrics (Graphics g, Font font, out int ascent, out int descent)
538                 {
539                         return Xlib.GetFontMetrics(g.GetHdc(), font.ToHfont(), out ascent, out descent);
540                 }
541
542
543                 // XXX this should be shared amongst non-win32 backends
544                 internal override Point GetMenuOrigin (IntPtr handle)
545                 {
546                         Hwnd    hwnd;
547
548                         hwnd = Hwnd.ObjectFromHandle(handle);
549
550                         if (hwnd != null)
551                                 return hwnd.MenuOrigin;
552
553                         return Point.Empty;
554                 }
555
556                 internal override bool GetMessage (object queue_id, ref MSG msg, IntPtr handle, int wFilterMin, int wFilterMax)
557                 {
558                         return display.GetMessage (queue_id, ref msg, handle, wFilterMin, wFilterMax);
559                 }
560
561                 internal override bool GetText (IntPtr handle, out string text)
562                 {
563                         X11Hwnd hwnd = (X11Hwnd) Hwnd.ObjectFromHandle(handle);
564
565                         text = "";
566                         return hwnd != null && hwnd.GetText (out text);
567                 }
568
569                 internal override void GetWindowPos (IntPtr handle, bool is_toplevel,
570                                                      out int x, out int y,
571                                                      out int width, out int height,
572                                                      out int client_width, out int client_height)
573                 {
574                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
575
576                         if (hwnd != null) {
577                                 hwnd.GetPosition (is_toplevel, out x, out y, out width, out height, out client_width, out client_height);
578                         }
579                         else {
580                                 // Should we throw an exception or fail silently?
581                                 // throw new ArgumentException("Called with an invalid window handle", "handle");
582
583                                 x = 0;
584                                 y = 0;
585                                 width = 0;
586                                 height = 0;
587                                 client_width = 0;
588                                 client_height = 0;
589                         }
590                 }
591
592                 internal override FormWindowState GetWindowState (IntPtr handle)
593                 {
594                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
595
596                         if (hwnd == null)
597                                 return FormWindowState.Normal; // XXX should we throw an exception here?  probably
598
599                         return hwnd.GetWindowState ();
600                 }
601
602                 internal override void GrabInfo (out IntPtr handle, out bool GrabConfined, out Rectangle GrabArea)
603                 {
604                         display.GrabInfo (out handle, out GrabConfined, out GrabArea);
605                 }
606
607                 internal override void GrabWindow (IntPtr handle, IntPtr confine_to_handle)
608                 {
609                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
610                         X11Hwnd confine_to_hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(confine_to_handle);
611
612                         display.GrabWindow (hwnd, confine_to_hwnd);
613                 }
614
615                 internal override void UngrabWindow (IntPtr handle)
616                 {
617                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
618
619                         display.UngrabWindow (hwnd);
620                 }
621
622                 internal override void HandleException(Exception e) {
623                         StackTrace st = new StackTrace(e, true);
624                         Console.WriteLine("Exception '{0}'", e.Message+st.ToString());
625                         Console.WriteLine("{0}{1}", e.Message, st.ToString());
626                 }
627
628                 internal override void Invalidate (IntPtr handle, Rectangle rc, bool clear)
629                 {
630                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
631
632                         hwnd.Invalidate (rc, clear);
633                 }
634
635                 internal override void InvalidateNC (IntPtr handle)
636                 {
637                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
638
639                         hwnd.InvalidateNC ();
640                 }
641
642                 internal override bool IsEnabled(IntPtr handle)
643                 {
644                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle (handle);
645
646                         return hwnd != null && hwnd.Enabled;
647                 }
648                 
649                 internal override bool IsVisible(IntPtr handle)
650                 {
651                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle (handle);
652
653                         return hwnd != null && hwnd.Visible;
654                 }
655
656                 internal override void KillTimer (Timer timer)
657                 {
658                         X11ThreadQueue queue = (X11ThreadQueue) MessageQueues [timer.thread];
659
660                         if (queue == null) {
661                                 // This isn't really an error, MS doesn't start the timer if
662                                 // it has no assosciated queue
663                                 return;
664                         }
665                         queue.KillTimer (timer);
666                 }
667
668                 internal override void MenuToScreen (IntPtr handle, ref int x, ref int y)
669                 {
670                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
671
672                         if (hwnd != null)
673                                 hwnd.MenuToScreen (ref x, ref y);
674                 }
675
676                 internal override void OverrideCursor (IntPtr cursor)
677                 {
678                         display.OverrideCursor = cursor;
679                 }
680
681                 internal override PaintEventArgs PaintEventStart (IntPtr handle, bool client)
682                 {
683                         return display.PaintEventStart (handle, client);
684                 }
685
686                 internal override void PaintEventEnd (IntPtr handle, bool client)
687                 {
688                         display.PaintEventEnd (handle, client);
689                 }
690
691
692                 internal override bool PeekMessage (object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags)
693                 {
694                         return display.PeekMessage (queue_id, ref msg, hWnd, wFilterMin, wFilterMax, flags);
695                 }
696
697                 internal override bool PostMessage (IntPtr handle, Msg message, IntPtr wparam, IntPtr lparam)
698                 {
699                         return display.PostMessage (handle, message, wparam, lparam);
700                 }
701
702                 internal override void PostQuitMessage(int exitCode)
703                 {
704                         display.PostMessage (display.FosterParent.Handle, Msg.WM_QUIT, IntPtr.Zero, IntPtr.Zero);
705                         display.Flush ();
706                 }
707
708                 [MonoTODO]
709                 internal override void RequestAdditionalWM_NCMessages (IntPtr handle, bool hover, bool leave)
710                 {
711                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
712
713                         if (hwnd != null)
714                                 hwnd.RequestAdditionalWM_NCMessages (hover, leave);
715                 }
716                 
717                 internal override void RequestNCRecalc (IntPtr handle)
718                 {
719                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
720
721                         if (hwnd != null)
722                                 hwnd.RequestNCRecalc ();
723                 }
724
725                 internal override void ResetMouseHover (IntPtr handle)
726                 {
727                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
728
729                         display.ResetMouseHover (hwnd);
730                 }
731
732                 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
733                 {
734                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
735
736                         if (hwnd != null)
737                                 hwnd.ScreenToClient (ref x, ref y);
738                 }
739
740                 internal override void ScreenToMenu (IntPtr handle, ref int x, ref int y)
741                 {
742                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
743
744                         if (hwnd != null)
745                                 hwnd.ScreenToMenu (ref x, ref y);
746                 }
747
748                 internal override void ScrollWindow (IntPtr handle, Rectangle area, int XAmount, int YAmount, bool with_children)
749                 {
750                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
751
752                         if (hwnd != null)
753                                 hwnd.ScrollWindow (area, XAmount, YAmount, with_children);
754                 }
755
756                 internal override void ScrollWindow(IntPtr handle, int XAmount, int YAmount, bool with_children)
757                 {
758                         X11Hwnd hwnd = (X11Hwnd)Hwnd.GetObjectFromWindow(handle);
759
760                         if (hwnd != null) {
761                                 Rectangle       rect;
762
763                                 rect = hwnd.ClientRect;
764                                 rect.X = 0;
765                                 rect.Y = 0;
766                                 hwnd.ScrollWindow (rect, XAmount, YAmount, with_children);
767                         }
768                 }
769
770                 internal override void SendAsyncMethod (AsyncMethodData method)
771                 {
772                         display.SendAsyncMethod (method);
773                 }
774
775                 // XXX this is likely shareable amongst other backends
776                 internal override IntPtr SendMessage (IntPtr handle, Msg message, IntPtr wParam, IntPtr lParam)
777                 {
778                         return display.SendMessage (handle, message, wParam, lParam);
779                 }
780
781                 internal override int SendInput(IntPtr handle, Queue keys) { 
782                         return display.SendInput(handle, keys);
783                 }
784
785
786                 internal override void SetAllowDrop (IntPtr handle, bool value)
787                 {
788                         // We allow drop on all windows
789                 }
790
791                 internal override DragDropEffects StartDrag (IntPtr handle, object data,
792                                                              DragDropEffects allowed_effects)
793                 {
794                         return display.StartDrag (handle, data, allowed_effects);
795                 }
796
797                 internal override void SetBorderStyle (IntPtr handle, FormBorderStyle border_style)
798                 {
799                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
800
801                         if (hwnd != null)
802                                 hwnd.SetBorderStyle (border_style);
803                 }
804
805                 internal override void SetCaretPos (IntPtr handle, int x, int y)
806                 {
807                         display.SetCaretPos (handle, x, y);
808                 }
809
810                 internal override void SetClipRegion (IntPtr handle, Region region)
811                 {
812                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
813
814                         if (hwnd != null)
815                                 hwnd.SetClipRegion (region);
816                 }
817
818                 internal override void SetCursor (IntPtr handle, IntPtr cursor)
819                 {
820                         display.SetCursor (handle, cursor);
821                 }
822
823                 internal override void SetCursorPos (IntPtr handle, int x, int y)
824                 {
825                         if (handle == IntPtr.Zero) {
826                                 display.SetCursorPos (x, y);
827                         }
828                         else {
829                                 X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
830
831                                 hwnd.SetCursorPos (x, y);
832                         }
833                 }
834
835                 internal override void SetFocus (IntPtr handle)
836                 {
837                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
838
839                         display.SetFocus (hwnd);
840                 }
841
842                 internal override void SetIcon(IntPtr handle, Icon icon)
843                 {
844                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);;
845
846                         if (hwnd != null)
847                                 hwnd.SetIcon (icon);
848                 }
849
850                 internal override void SetMenu(IntPtr handle, Menu menu)
851                 {
852                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
853
854                         hwnd.SetMenu (menu);
855                 }
856
857                 internal override void SetModal(IntPtr handle, bool Modal)
858                 {
859                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
860
861                         if (hwnd != null)
862                                 display.SetModal (hwnd, Modal);
863                 }
864
865                 internal override IntPtr SetParent(IntPtr handle, IntPtr parent)
866                 {
867                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
868                         X11Hwnd parent_hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(parent);
869
870                         if (hwnd != null)
871                                 hwnd.SetParent (parent_hwnd);
872
873                         return IntPtr.Zero;
874                 }
875
876                 internal override void SetTimer (Timer timer)
877                 {
878                         X11ThreadQueue queue = (X11ThreadQueue) MessageQueues [timer.thread];
879
880                         if (queue == null) {
881                                 // This isn't really an error, MS doesn't start the timer if
882                                 // it has no assosciated queue
883                                 return;
884                         }
885                         queue.SetTimer (timer);
886                 }
887
888                 internal override bool SetTopmost(IntPtr handle, bool enabled)
889                 {
890                         X11Hwnd hwnd = (X11Hwnd) Hwnd.ObjectFromHandle (handle);
891
892                         if (hwnd == null)
893                                 return false;
894
895                         return hwnd.SetTopmost (enabled);
896                 }
897
898                 internal override bool SetOwner(IntPtr handle, IntPtr handle_owner)
899                 {
900                         X11Hwnd hwnd;
901
902                         hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
903
904                         if (hwnd == null)
905                                 return false;
906
907                         X11Hwnd hwnd_owner = (X11Hwnd)Hwnd.ObjectFromHandle(handle_owner);
908
909                         return hwnd.SetOwner (hwnd_owner);
910                 }
911
912                 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
913                 {
914                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
915
916                         return hwnd != null && hwnd.SetVisible (visible, activate);
917                 }
918
919                 internal override void SetWindowMinMax (IntPtr handle, Rectangle maximized, Size min, Size max)
920                 {
921                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
922
923                         if (hwnd == null)
924                                 return;
925
926                         hwnd.SetMinMax (maximized, min, max);
927                 }
928
929                 internal override void SetWindowPos (IntPtr handle, int x, int y, int width, int height)
930                 {
931                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
932
933                         if (hwnd != null)
934                                 hwnd.SetPosition (x, y, width, height);
935                 }
936
937                 internal override void SetWindowState (IntPtr handle, FormWindowState state)
938                 {
939                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
940
941                         if (hwnd != null)
942                                 hwnd.SetWindowState (state);
943                 }
944
945                 internal override void SetWindowStyle (IntPtr handle, CreateParams cp)
946                 {
947                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
948
949                         if (hwnd != null) {
950                                 hwnd.SetHwndStyles (cp);
951                                 hwnd.SetWMStyles (cp);
952                         }
953                 }
954
955                 internal override double GetWindowTransparency (IntPtr handle)
956                 {
957                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
958
959                         if (hwnd != null)
960                                 return hwnd.GetWindowTransparency ();
961                         else
962                                 return 0.0;
963                 }
964
965                 internal override void SetWindowTransparency (IntPtr handle, double transparency, Color key)
966                 {
967                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
968
969                         if (hwnd != null)
970                                 hwnd.SetWindowTransparency (transparency, key);
971                 }
972
973                 internal override bool SetZOrder (IntPtr handle, IntPtr after_handle, bool top, bool bottom)
974                 {
975                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
976
977                         if (hwnd == null || !hwnd.mapped)
978                                 return false;
979
980                         X11Hwnd after_hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(after_handle);
981
982                         return hwnd.SetZOrder (after_hwnd, top, bottom);
983                 }
984
985                 internal override void ShowCursor (bool show)
986                 {
987                         display.ShowCursor (show);
988                 }
989
990                 internal override object StartLoop(Thread thread)
991                 {
992                         return (object) ThreadQueue(thread);
993                 }
994
995                 internal override TransparencySupport SupportsTransparency()
996                 {
997                         return display.SupportsTransparency ();
998                 }
999
1000                 internal override bool SystrayAdd (IntPtr handle, string tip, Icon icon, out ToolTip tt)
1001                 {
1002                         return display.SystrayAdd (handle, tip, icon, out tt);
1003                 }
1004
1005                 internal override bool SystrayChange (IntPtr handle, string tip, Icon icon, ref ToolTip tt)
1006                 {
1007                         return display.SystrayChange (handle, tip, icon, ref tt);
1008                 }
1009
1010                 internal override void SystrayRemove (IntPtr handle, ref ToolTip tt)
1011                 {
1012                         display.SystrayRemove (handle, ref tt);
1013                 }
1014
1015 #if NET_2_0
1016                 internal override void SystrayBalloon(IntPtr handle, int timeout, string title, string text, ToolTipIcon icon)
1017                 {
1018                         Control control = Control.FromHandle(handle);
1019                         
1020                         if (control == null)
1021                                 return;
1022
1023                         NotifyIcon.BalloonWindow form = new NotifyIcon.BalloonWindow (handle);
1024                         form.Title = title;
1025                         form.Text = text;
1026                         form.Timeout = timeout;
1027                         form.Show ();
1028                         
1029                         SendMessage(handle, Msg.WM_USER, IntPtr.Zero, (IntPtr) Msg.NIN_BALLOONSHOW);    
1030                 }
1031 #endif
1032
1033                 internal override bool Text (IntPtr handle, string text)
1034                 {
1035                         X11Hwnd hwnd = (X11Hwnd) Hwnd.ObjectFromHandle(handle);
1036
1037                         if (hwnd != null)
1038                                 hwnd.Text = text;
1039
1040                         return true;
1041                 }
1042
1043                 internal override bool TranslateMessage (ref MSG msg)
1044                 {
1045                         return display.TranslateMessage (ref msg);
1046                 }
1047
1048                 internal override void UpdateWindow (IntPtr handle)
1049                 {
1050                         X11Hwnd hwnd = (X11Hwnd)Hwnd.ObjectFromHandle(handle);
1051
1052                         if (hwnd != null)
1053                                 hwnd.Update ();
1054                 }
1055
1056                 internal override void CreateOffscreenDrawable (IntPtr handle,
1057                                                                 int width, int height,
1058                                                                 out object offscreen_drawable)
1059                 {
1060                         display.CreateOffscreenDrawable (handle, width, height,
1061                                                          out offscreen_drawable);
1062                 }
1063
1064                 internal override void DestroyOffscreenDrawable (object offscreen_drawable)
1065                 {
1066                         display.DestroyOffscreenDrawable (offscreen_drawable);
1067                 }
1068
1069                 internal override Graphics GetOffscreenGraphics (object offscreen_drawable)
1070                 {
1071                         return display.GetOffscreenGraphics (offscreen_drawable);
1072                 }
1073
1074                 internal override void BlitFromOffscreen (IntPtr dest_handle,
1075                                                           Graphics dest_dc,
1076                                                           object offscreen_drawable,
1077                                                           Graphics offscreen_dc,
1078                                                           Rectangle r)
1079                 {
1080                         display.BlitFromOffscreen (dest_handle, dest_dc, offscreen_drawable, offscreen_dc, r);
1081                 }
1082
1083                 #endregion      // Public Static Methods
1084
1085                 #region Events
1086                 internal override event EventHandler Idle {
1087                         add {
1088                                 Console.WriteLine ("adding idle handler for thread {0}", Thread.CurrentThread.GetHashCode());
1089                                 X11ThreadQueue queue = ThreadQueue(Thread.CurrentThread);
1090                                 queue.Idle += value;
1091                         }
1092                         remove {
1093                                 X11ThreadQueue queue = ThreadQueue(Thread.CurrentThread);
1094                                 queue.Idle += value;
1095                         }
1096                 }
1097                 #endregion      // Events
1098         }
1099 }