more work. timers should be working now.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms.X11Internal / X11RootHwnd.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) 2006 Novell, Inc. (http://www.novell.com)
21 //
22 //
23
24 using System;
25 using System.Runtime.InteropServices;
26 using System.Windows.Forms;
27
28 namespace System.Windows.Forms.X11Internal {
29
30         internal class X11RootHwnd : X11Hwnd
31         {
32                 public X11RootHwnd (X11Display display, IntPtr window_handle) : base (display)
33                 {
34                         WholeWindow = ClientWindow = window_handle;
35
36                         Xlib.XSelectInput(display.Handle, WholeWindow, new IntPtr ((int)EventMask.PropertyChangeMask));
37                 }
38
39                 public override void CreateWindow (CreateParams cp)
40                 {
41                         // we don't do anything here
42                 }
43
44                 public override void PropertyChanged (XEvent xevent)
45                 {
46                         if (xevent.PropertyEvent.atom == Display.Atoms._NET_ACTIVE_WINDOW) {
47                                 IntPtr  actual_atom;
48                                 int     actual_format;
49                                 IntPtr  nitems;
50                                 IntPtr  bytes_after;
51                                 IntPtr  prop = IntPtr.Zero;
52                                 X11Hwnd prev_active;
53
54                                 prev_active = Display.ActiveWindow;
55                                 Xlib.XGetWindowProperty (Display.Handle, WholeWindow,
56                                                          Display.Atoms._NET_ACTIVE_WINDOW, IntPtr.Zero, new IntPtr (1), false,
57                                                          Display.Atoms.XA_WINDOW, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
58
59                                 if (((long)nitems > 0) && (prop != IntPtr.Zero)) {
60                                         // FIXME - is this 64 bit clean?
61                                         Display.ActiveWindow = (X11Hwnd)Hwnd.ObjectFromHandle((IntPtr)Marshal.ReadInt32(prop));
62                                         Xlib.XFree(prop);
63
64                                         if (prev_active != Display.ActiveWindow) {
65                                                 if (prev_active != null) {
66                                                         Display.PostMessage (prev_active.Handle, Msg.WM_ACTIVATE,
67                                                                              (IntPtr)WindowActiveFlags.WA_INACTIVE, IntPtr.Zero);
68                                                 }
69                                                 if (Display.ActiveWindow != null) {
70                                                         Display.PostMessage (Display.ActiveWindow.Handle, Msg.WM_ACTIVATE,
71                                                                              (IntPtr)WindowActiveFlags.WA_ACTIVE, IntPtr.Zero);
72                                                 }
73                                         }
74
75                                         // XXX this next bit should be in the X11Display.ActiveWindow setter
76
77                                         if (Display.ModalWindows.Count > 0) {
78                                                 // Modality handling, if we are modal and the new active window is one
79                                                 // of ours but not the modal one, switch back to the modal window
80
81                                                 if (Display.ActiveWindow != null &&
82                                                     NativeWindow.FindWindow (Display.ActiveWindow.Handle) != null) {
83                                                         if (Display.ActiveWindow != (X11Hwnd)Display.ModalWindows.Peek()) {
84                                                                 ((X11Hwnd)Display.ModalWindows.Peek()).Activate ();
85                                                         }
86                                                 }
87                                         }
88                                 }
89                         }
90                         else if (xevent.PropertyEvent.atom == Display.Atoms._NET_SUPPORTED) {
91                                 // we'll need to refetch the supported protocols list
92                                 refetch_net_supported = true;
93                                 _net_supported = null;
94                         }
95                         else
96                                 base.PropertyChanged (xevent);
97                 }
98
99                 bool refetch_net_supported = true;
100                 IntPtr[] _net_supported;
101                 public IntPtr[] _NET_SUPPORTED {
102                         get {
103                                 if (refetch_net_supported) {
104                                         _net_supported = GetAtomListProperty (Display.Atoms._NET_SUPPORTED);
105                                         refetch_net_supported = false;
106                                 }
107
108                                 return _net_supported;
109                         }
110                 }
111         }
112
113 }
114