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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004-2006 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
28 // This driver understands the following environment variables: (Set the var to enable feature)
30 // MONO_XEXCEPTIONS = throw an exception when a X11 error is encountered;
31 // by default a message is displayed but execution continues
33 // MONO_XSYNC = perform all X11 commands synchronous; this is slower but
34 // helps in debugging errors
39 // define to log Window handles and relationships to stdout
42 // Extra detailed debug
43 #undef DriverDebugExtra
44 #undef DriverDebugParent
45 #undef DriverDebugCreate
46 #undef DriverDebugDestroy
47 #undef DriverDebugThreads
48 #undef DriverDebugXEmbed
54 using System.ComponentModel;
55 using System.Collections;
56 using System.Diagnostics;
58 using System.Drawing.Drawing2D;
59 using System.Drawing.Imaging;
62 using System.Net.Sockets;
63 using System.Reflection;
64 using System.Runtime.InteropServices;
65 using System.Runtime.Serialization;
66 using System.Runtime.Serialization.Formatters.Binary;
68 using System.Threading;
70 // Only do the poll when building with mono for now
72 using Mono.Unix.Native;
76 namespace System.Windows.Forms {
77 internal class XplatUIX11 : XplatUIDriver {
78 #region Local Variables
80 static volatile XplatUIX11 Instance;
82 static object XlibLock; // Our locking object
83 static bool themes_enabled;
86 static IntPtr DisplayHandle; // X11 handle to display
87 static int ScreenNo; // Screen number used
88 static IntPtr DefaultColormap; // Colormap for screen
89 static IntPtr CustomVisual; // Visual for window creation
90 static IntPtr CustomColormap; // Colormap for window creation
91 static IntPtr RootWindow; // Handle of the root window for the screen/display
92 static IntPtr FosterParent; // Container to hold child windows until their parent exists
93 static XErrorHandler ErrorHandler; // Error handler delegate
94 static bool ErrorExceptions; // Throw exceptions on X errors
95 int render_major_opcode;
96 int render_first_event;
97 int render_first_error;
100 static IntPtr ClipMagic;
101 static ClipboardData Clipboard; // Our clipboard
104 static IntPtr PostAtom; // PostMessage atom
105 static IntPtr AsyncAtom; // Support for async messages
108 static Hashtable MessageQueues; // Holds our thread-specific XEventQueues
109 static ArrayList unattached_timer_list; // holds timers that are enabled but not attached to a window.
111 static Pollfd[] pollfds; // For watching the X11 socket
112 static bool wake_waiting;
113 static object wake_waiting_lock = new object ();
115 static X11Keyboard Keyboard; //
117 static Socket listen; //
118 static Socket wake; //
119 static Socket wake_receive; //
120 static byte[] network_buffer; //
121 static bool detectable_key_auto_repeat;
124 static IntPtr ActiveWindow; // Handle of the active window
125 static IntPtr FocusWindow; // Handle of the window with keyboard focus (if any)
128 static Stack ModalWindows; // Stack of our modal windows
131 static IntPtr SystrayMgrWindow; // Handle of the Systray Manager window
134 static IntPtr LastCursorWindow; // The last window we set the cursor on
135 static IntPtr LastCursorHandle; // The handle that was last set on LastCursorWindow
136 static IntPtr OverrideCursorHandle; // The cursor that is set to override any other cursors
139 static CaretStruct Caret; //
141 // Last window containing the pointer
142 static IntPtr LastPointerWindow; // The last window containing the pointer
145 static IntPtr WM_PROTOCOLS;
146 static IntPtr WM_DELETE_WINDOW;
147 static IntPtr WM_TAKE_FOCUS;
148 //static IntPtr _NET_SUPPORTED;
149 //static IntPtr _NET_CLIENT_LIST;
150 //static IntPtr _NET_NUMBER_OF_DESKTOPS;
151 static IntPtr _NET_DESKTOP_GEOMETRY;
152 //static IntPtr _NET_DESKTOP_VIEWPORT;
153 static IntPtr _NET_CURRENT_DESKTOP;
154 //static IntPtr _NET_DESKTOP_NAMES;
155 static IntPtr _NET_ACTIVE_WINDOW;
156 static IntPtr _NET_WORKAREA;
157 //static IntPtr _NET_SUPPORTING_WM_CHECK;
158 //static IntPtr _NET_VIRTUAL_ROOTS;
159 //static IntPtr _NET_DESKTOP_LAYOUT;
160 //static IntPtr _NET_SHOWING_DESKTOP;
161 //static IntPtr _NET_CLOSE_WINDOW;
162 //static IntPtr _NET_MOVERESIZE_WINDOW;
163 static IntPtr _NET_WM_MOVERESIZE;
164 //static IntPtr _NET_RESTACK_WINDOW;
165 //static IntPtr _NET_REQUEST_FRAME_EXTENTS;
166 static IntPtr _NET_WM_NAME;
167 //static IntPtr _NET_WM_VISIBLE_NAME;
168 //static IntPtr _NET_WM_ICON_NAME;
169 //static IntPtr _NET_WM_VISIBLE_ICON_NAME;
170 //static IntPtr _NET_WM_DESKTOP;
171 static IntPtr _NET_WM_WINDOW_TYPE;
172 static IntPtr _NET_WM_STATE;
173 //static IntPtr _NET_WM_ALLOWED_ACTIONS;
174 //static IntPtr _NET_WM_STRUT;
175 //static IntPtr _NET_WM_STRUT_PARTIAL;
176 //static IntPtr _NET_WM_ICON_GEOMETRY;
177 static IntPtr _NET_WM_ICON;
178 //static IntPtr _NET_WM_PID;
179 //static IntPtr _NET_WM_HANDLED_ICONS;
180 static IntPtr _NET_WM_USER_TIME;
181 static IntPtr _NET_FRAME_EXTENTS;
182 //static IntPtr _NET_WM_PING;
183 //static IntPtr _NET_WM_SYNC_REQUEST;
184 static IntPtr _NET_SYSTEM_TRAY_S;
185 //static IntPtr _NET_SYSTEM_TRAY_ORIENTATION;
186 static IntPtr _NET_SYSTEM_TRAY_OPCODE;
187 static IntPtr _NET_WM_STATE_MAXIMIZED_HORZ;
188 static IntPtr _NET_WM_STATE_MAXIMIZED_VERT;
189 static IntPtr _XEMBED;
190 static IntPtr _XEMBED_INFO;
191 static IntPtr _MOTIF_WM_HINTS;
192 static IntPtr _NET_WM_STATE_SKIP_TASKBAR;
193 static IntPtr _NET_WM_STATE_ABOVE;
194 static IntPtr _NET_WM_STATE_MODAL;
195 static IntPtr _NET_WM_STATE_HIDDEN;
196 static IntPtr _NET_WM_CONTEXT_HELP;
197 static IntPtr _NET_WM_WINDOW_OPACITY;
198 //static IntPtr _NET_WM_WINDOW_TYPE_DESKTOP;
199 //static IntPtr _NET_WM_WINDOW_TYPE_DOCK;
200 //static IntPtr _NET_WM_WINDOW_TYPE_TOOLBAR;
201 //static IntPtr _NET_WM_WINDOW_TYPE_MENU;
202 static IntPtr _NET_WM_WINDOW_TYPE_UTILITY;
203 //static IntPtr _NET_WM_WINDOW_TYPE_SPLASH;
204 // static IntPtr _NET_WM_WINDOW_TYPE_DIALOG;
205 static IntPtr _NET_WM_WINDOW_TYPE_NORMAL;
206 static IntPtr CLIPBOARD;
207 static IntPtr PRIMARY;
209 static IntPtr OEMTEXT;
210 static IntPtr UTF8_STRING;
211 static IntPtr UTF16_STRING;
212 static IntPtr RICHTEXTFORMAT;
213 static IntPtr TARGETS;
215 // mouse hover message generation
216 static HoverStruct HoverState; //
218 // double click message generation
219 static ClickStruct ClickPending; //
221 // Support for mouse grab
222 static GrabStruct Grab; //
225 Point mouse_position; // Last position of mouse, in screen coords
226 internal static MouseButtons MouseState; // Last state of mouse buttons
227 internal static bool in_doevents;
229 static int DoubleClickInterval; // msec; max interval between clicks to count as double click
231 const EventMask SelectInputMask = (EventMask.ButtonPressMask |
232 EventMask.ButtonReleaseMask |
233 EventMask.KeyPressMask |
234 EventMask.KeyReleaseMask |
235 EventMask.EnterWindowMask |
236 EventMask.LeaveWindowMask |
237 EventMask.ExposureMask |
238 EventMask.FocusChangeMask |
239 EventMask.PointerMotionMask |
240 EventMask.PointerMotionHintMask |
241 EventMask.SubstructureNotifyMask);
243 static readonly object lockobj = new object ();
245 // messages WaitForHwndMwssage is waiting on
246 static Hashtable messageHold;
248 #endregion // Local Variables
252 // Handle singleton stuff first
256 // Now regular initialization
257 XlibLock = new object ();
258 X11Keyboard.XlibLock = XlibLock;
259 MessageQueues = Hashtable.Synchronized (new Hashtable(7));
260 unattached_timer_list = ArrayList.Synchronized (new ArrayList (3));
261 messageHold = Hashtable.Synchronized (new Hashtable(3));
262 Clipboard = new ClipboardData ();
265 ErrorExceptions = false;
267 // X11 Initialization
268 SetDisplay(XOpenDisplay(IntPtr.Zero));
269 X11DesktopColors.Initialize();
272 // Disable keyboard autorepeat
274 XkbSetDetectableAutoRepeat (DisplayHandle, true, IntPtr.Zero);
275 detectable_key_auto_repeat = true;
277 Console.Error.WriteLine ("Could not disable keyboard auto repeat, will attempt to disable manually.");
278 detectable_key_auto_repeat = false;
281 // Handle any upcoming errors; we re-set it here, X11DesktopColor stuff might have stolen it (gtk does)
282 ErrorHandler = new XErrorHandler(HandleError);
283 XSetErrorHandler(ErrorHandler);
287 // Remove our display handle from S.D
288 Graphics.FromHdcInternal (IntPtr.Zero);
291 #endregion // Constructors
293 #region Singleton Specific Code
294 public static XplatUIX11 GetInstance() {
296 if (Instance == null) {
297 Instance=new XplatUIX11();
304 public int Reference {
311 #region Internal Properties
312 internal static IntPtr Display {
314 return DisplayHandle;
318 XplatUIX11.GetInstance().SetDisplay(value);
322 internal static int Screen {
332 internal static IntPtr RootWindowHandle {
342 internal static IntPtr Visual {
348 CustomVisual = value;
352 internal static IntPtr ColorMap {
354 return CustomColormap;
358 CustomColormap = value;
363 internal static IntPtr DefaultColorMap {
365 return DefaultColormap;
371 #region XExceptionClass
372 internal class XException : ApplicationException {
376 XRequest RequestCode;
380 public XException(IntPtr Display, IntPtr ResourceID, IntPtr Serial, byte ErrorCode, XRequest RequestCode, byte MinorCode) {
381 this.Display = Display;
382 this.ResourceID = ResourceID;
383 this.Serial = Serial;
384 this.RequestCode = RequestCode;
385 this.ErrorCode = ErrorCode;
386 this.MinorCode = MinorCode;
389 public override string Message {
391 return GetMessage(Display, ResourceID, Serial, ErrorCode, RequestCode, MinorCode);
395 public static string GetMessage(IntPtr Display, IntPtr ResourceID, IntPtr Serial, byte ErrorCode, XRequest RequestCode, byte MinorCode) {
404 sb = new StringBuilder(160);
405 XGetErrorText(Display, ErrorCode, sb, sb.Capacity);
406 x_error_text = sb.ToString();
407 hwnd = Hwnd.ObjectFromHandle(ResourceID);
409 hwnd_text = hwnd.ToString();
410 c = Control.FromHandle(hwnd.Handle);
412 control_text = c.ToString();
414 control_text = String.Format("<handle {0:X} non-existant>", hwnd.Handle.ToInt32());
417 hwnd_text = "<null>";
418 control_text = "<null>";
422 error = String.Format("\n Error: {0}\n Request: {1:D} ({2})\n Resource ID: 0x{3:X}\n Serial: {4}\n Hwnd: {5}\n Control: {6}", x_error_text, RequestCode, MinorCode, ResourceID.ToInt32(), Serial, hwnd_text, control_text);
426 #endregion // XExceptionClass
428 #region Internal Methods
429 internal void SetDisplay(IntPtr display_handle)
431 if (display_handle != IntPtr.Zero) {
434 if ((DisplayHandle != IntPtr.Zero) && (FosterParent != IntPtr.Zero)) {
435 hwnd = Hwnd.ObjectFromHandle(FosterParent);
436 XDestroyWindow(DisplayHandle, FosterParent);
440 if (DisplayHandle != IntPtr.Zero) {
441 XCloseDisplay(DisplayHandle);
444 DisplayHandle=display_handle;
446 // We need to tell System.Drawing our DisplayHandle. FromHdcInternal has
447 // been hacked to do this for us.
448 Graphics.FromHdcInternal (DisplayHandle);
450 // query for the render extension so
451 // we can ignore the spurious
452 // BadPicture errors that are
453 // generated by cairo/render.
454 XQueryExtension (DisplayHandle, "RENDER",
455 ref render_major_opcode, ref render_first_event, ref render_first_error);
458 if (Environment.GetEnvironmentVariable ("MONO_XSYNC") != null) {
459 XSynchronize(DisplayHandle, true);
462 if (Environment.GetEnvironmentVariable ("MONO_XEXCEPTIONS") != null) {
463 ErrorExceptions = true;
467 ScreenNo = XDefaultScreen(DisplayHandle);
468 RootWindow = XRootWindow(DisplayHandle, ScreenNo);
469 DefaultColormap = XDefaultColormap(DisplayHandle, ScreenNo);
471 // Create the foster parent
472 // it is important that border_width is kept in synch with the other XCreateWindow calls
473 FosterParent=XCreateSimpleWindow(DisplayHandle, RootWindow, 0, 0, 1, 1, 0, UIntPtr.Zero, UIntPtr.Zero);
474 if (FosterParent==IntPtr.Zero) {
475 Console.WriteLine("XplatUIX11 Constructor failed to create FosterParent");
478 DebugHelper.WriteLine ("FosterParent created 0x{0:x}", FosterParent.ToInt32());
481 hwnd.Queue = ThreadQueue(Thread.CurrentThread);
482 hwnd.WholeWindow = FosterParent;
483 hwnd.ClientWindow = FosterParent;
485 // Create a HWND for RootWIndow as well, so our queue doesn't eat the events
487 hwnd.Queue = ThreadQueue(Thread.CurrentThread);
488 hwnd.whole_window = RootWindow;
489 hwnd.ClientWindow = RootWindow;
491 // For sleeping on the X11 socket
492 listen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
493 IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 0);
497 // To wake up when a timer is ready
498 network_buffer = new byte[10];
500 wake = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
501 wake.Connect(listen.LocalEndPoint);
503 // Make this non-blocking, so it doesn't
504 // deadlock if too many wakes are sent
505 // before the wake_receive end is polled
506 wake.Blocking = false;
508 wake_receive = listen.Accept();
511 pollfds = new Pollfd [2];
512 pollfds [0] = new Pollfd ();
513 pollfds [0].fd = XConnectionNumber (DisplayHandle);
514 pollfds [0].events = PollEvents.POLLIN;
516 pollfds [1] = new Pollfd ();
517 pollfds [1].fd = wake_receive.Handle.ToInt32 ();
518 pollfds [1].events = PollEvents.POLLIN;
521 Keyboard = new X11Keyboard(DisplayHandle, FosterParent);
522 Dnd = new X11Dnd (DisplayHandle, Keyboard);
524 DoubleClickInterval = 500;
526 HoverState.Interval = 500;
527 HoverState.Timer = new Timer();
528 HoverState.Timer.Enabled = false;
529 HoverState.Timer.Interval = HoverState.Interval;
530 HoverState.Timer.Tick += new EventHandler(MouseHover);
531 HoverState.Size = new Size(4, 4);
535 ActiveWindow = IntPtr.Zero;
536 FocusWindow = IntPtr.Zero;
537 ModalWindows = new Stack(3);
539 MouseState = MouseButtons.None;
540 mouse_position = new Point(0, 0);
542 Caret.Timer = new Timer();
543 Caret.Timer.Interval = 500; // FIXME - where should this number come from?
544 Caret.Timer.Tick += new EventHandler(CaretCallback);
548 // Grab atom changes off the root window to catch certain WM events
549 XSelectInput(DisplayHandle, RootWindow, new IntPtr ((int) (EventMask.PropertyChangeMask | Keyboard.KeyEventMask)));
551 // Handle any upcoming errors
552 ErrorHandler = new XErrorHandler(HandleError);
553 XSetErrorHandler(ErrorHandler);
555 throw new ArgumentNullException("Display", "Could not open display (X-Server required. Check you DISPLAY environment variable)");
558 #endregion // Internal Methods
561 [Conditional ("DriverDebug")]
562 static void DriverDebug (string format, params object [] args)
564 Console.WriteLine (String.Format (format, args));
568 TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
570 return (int) t.TotalSeconds;
573 static void SetupAtoms() {
574 // make sure this array stays in sync with the statements below
575 string [] atom_names = new string[] {
580 //"_NET_CLIENT_LIST",
581 //"_NET_NUMBER_OF_DESKTOPS",
582 "_NET_DESKTOP_GEOMETRY",
583 //"_NET_DESKTOP_VIEWPORT",
584 "_NET_CURRENT_DESKTOP",
585 //"_NET_DESKTOP_NAMES",
586 "_NET_ACTIVE_WINDOW",
588 //"_NET_SUPPORTING_WM_CHECK",
589 //"_NET_VIRTUAL_ROOTS",
590 //"_NET_DESKTOP_LAYOUT",
591 //"_NET_SHOWING_DESKTOP",
592 //"_NET_CLOSE_WINDOW",
593 //"_NET_MOVERESIZE_WINDOW",
594 "_NET_WM_MOVERESIZE",
595 //"_NET_RESTACK_WINDOW",
596 //"_NET_REQUEST_FRAME_EXTENTS",
598 //"_NET_WM_VISIBLE_NAME",
599 //"_NET_WM_ICON_NAME",
600 //"_NET_WM_VISIBLE_ICON_NAME",
602 "_NET_WM_WINDOW_TYPE",
604 //"_NET_WM_ALLOWED_ACTIONS",
606 //"_NET_WM_STRUT_PARTIAL",
607 //"_NET_WM_ICON_GEOMETRY",
610 //"_NET_WM_HANDLED_ICONS",
612 "_NET_FRAME_EXTENTS",
614 //"_NET_WM_SYNC_REQUEST",
615 "_NET_SYSTEM_TRAY_OPCODE",
616 //"_NET_SYSTEM_TRAY_ORIENTATION",
617 "_NET_WM_STATE_MAXIMIZED_HORZ",
618 "_NET_WM_STATE_MAXIMIZED_VERT",
619 "_NET_WM_STATE_HIDDEN",
623 "_NET_WM_STATE_SKIP_TASKBAR",
624 "_NET_WM_STATE_ABOVE",
625 "_NET_WM_STATE_MODAL",
626 "_NET_WM_CONTEXT_HELP",
627 "_NET_WM_WINDOW_OPACITY",
628 //"_NET_WM_WINDOW_TYPE_DESKTOP",
629 //"_NET_WM_WINDOW_TYPE_DOCK",
630 //"_NET_WM_WINDOW_TYPE_TOOLBAR",
631 //"_NET_WM_WINDOW_TYPE_MENU",
632 "_NET_WM_WINDOW_TYPE_UTILITY",
633 // "_NET_WM_WINDOW_TYPE_DIALOG",
634 //"_NET_WM_WINDOW_TYPE_SPLASH",
635 "_NET_WM_WINDOW_TYPE_NORMAL",
644 "_SWF_PostMessageAtom",
647 IntPtr[] atoms = new IntPtr [atom_names.Length];;
649 XInternAtoms (DisplayHandle, atom_names, atom_names.Length, false, atoms);
652 WM_PROTOCOLS = atoms [off++];
653 WM_DELETE_WINDOW = atoms [off++];
654 WM_TAKE_FOCUS = atoms [off++];
655 //_NET_SUPPORTED = atoms [off++];
656 //_NET_CLIENT_LIST = atoms [off++];
657 //_NET_NUMBER_OF_DESKTOPS = atoms [off++];
658 _NET_DESKTOP_GEOMETRY = atoms [off++];
659 //_NET_DESKTOP_VIEWPORT = atoms [off++];
660 _NET_CURRENT_DESKTOP = atoms [off++];
661 //_NET_DESKTOP_NAMES = atoms [off++];
662 _NET_ACTIVE_WINDOW = atoms [off++];
663 _NET_WORKAREA = atoms [off++];
664 //_NET_SUPPORTING_WM_CHECK = atoms [off++];
665 //_NET_VIRTUAL_ROOTS = atoms [off++];
666 //_NET_DESKTOP_LAYOUT = atoms [off++];
667 //_NET_SHOWING_DESKTOP = atoms [off++];
668 //_NET_CLOSE_WINDOW = atoms [off++];
669 //_NET_MOVERESIZE_WINDOW = atoms [off++];
670 _NET_WM_MOVERESIZE = atoms [off++];
671 //_NET_RESTACK_WINDOW = atoms [off++];
672 //_NET_REQUEST_FRAME_EXTENTS = atoms [off++];
673 _NET_WM_NAME = atoms [off++];
674 //_NET_WM_VISIBLE_NAME = atoms [off++];
675 //_NET_WM_ICON_NAME = atoms [off++];
676 //_NET_WM_VISIBLE_ICON_NAME = atoms [off++];
677 //_NET_WM_DESKTOP = atoms [off++];
678 _NET_WM_WINDOW_TYPE = atoms [off++];
679 _NET_WM_STATE = atoms [off++];
680 //_NET_WM_ALLOWED_ACTIONS = atoms [off++];
681 //_NET_WM_STRUT = atoms [off++];
682 //_NET_WM_STRUT_PARTIAL = atoms [off++];
683 //_NET_WM_ICON_GEOMETRY = atoms [off++];
684 _NET_WM_ICON = atoms [off++];
685 //_NET_WM_PID = atoms [off++];
686 //_NET_WM_HANDLED_ICONS = atoms [off++];
687 _NET_WM_USER_TIME = atoms [off++];
688 _NET_FRAME_EXTENTS = atoms [off++];
689 //_NET_WM_PING = atoms [off++];
690 //_NET_WM_SYNC_REQUEST = atoms [off++];
691 _NET_SYSTEM_TRAY_OPCODE = atoms [off++];
692 //_NET_SYSTEM_TRAY_ORIENTATION = atoms [off++];
693 _NET_WM_STATE_MAXIMIZED_HORZ = atoms [off++];
694 _NET_WM_STATE_MAXIMIZED_VERT = atoms [off++];
695 _NET_WM_STATE_HIDDEN = atoms [off++];
696 _XEMBED = atoms [off++];
697 _XEMBED_INFO = atoms [off++];
698 _MOTIF_WM_HINTS = atoms [off++];
699 _NET_WM_STATE_SKIP_TASKBAR = atoms [off++];
700 _NET_WM_STATE_ABOVE = atoms [off++];
701 _NET_WM_STATE_MODAL = atoms [off++];
702 _NET_WM_CONTEXT_HELP = atoms [off++];
703 _NET_WM_WINDOW_OPACITY = atoms [off++];
704 //_NET_WM_WINDOW_TYPE_DESKTOP = atoms [off++];
705 //_NET_WM_WINDOW_TYPE_DOCK = atoms [off++];
706 //_NET_WM_WINDOW_TYPE_TOOLBAR = atoms [off++];
707 //_NET_WM_WINDOW_TYPE_MENU = atoms [off++];
708 _NET_WM_WINDOW_TYPE_UTILITY = atoms [off++];
709 // _NET_WM_WINDOW_TYPE_DIALOG = atoms [off++];
710 //_NET_WM_WINDOW_TYPE_SPLASH = atoms [off++];
711 _NET_WM_WINDOW_TYPE_NORMAL = atoms [off++];
712 CLIPBOARD = atoms [off++];
713 PRIMARY = atoms [off++];
714 OEMTEXT = atoms [off++];
715 UTF8_STRING = atoms [off++];
716 UTF16_STRING = atoms [off++];
717 RICHTEXTFORMAT = atoms [off++];
718 TARGETS = atoms [off++];
719 AsyncAtom = atoms [off++];
720 PostAtom = atoms [off++];
721 HoverState.Atom = atoms [off++];
723 //DIB = (IntPtr)Atom.XA_PIXMAP;
724 _NET_SYSTEM_TRAY_S = XInternAtom (DisplayHandle, "_NET_SYSTEM_TRAY_S" + ScreenNo.ToString(), false);
727 void GetSystrayManagerWindow() {
728 XGrabServer(DisplayHandle);
729 SystrayMgrWindow = XGetSelectionOwner(DisplayHandle, _NET_SYSTEM_TRAY_S);
730 XUngrabServer(DisplayHandle);
731 XFlush(DisplayHandle);
734 void SendNetWMMessage(IntPtr window, IntPtr message_type, IntPtr l0, IntPtr l1, IntPtr l2) {
735 SendNetWMMessage (window, message_type, l0, l1, l2, IntPtr.Zero);
738 void SendNetWMMessage(IntPtr window, IntPtr message_type, IntPtr l0, IntPtr l1, IntPtr l2, IntPtr l3) {
742 xev.ClientMessageEvent.type = XEventName.ClientMessage;
743 xev.ClientMessageEvent.send_event = true;
744 xev.ClientMessageEvent.window = window;
745 xev.ClientMessageEvent.message_type = message_type;
746 xev.ClientMessageEvent.format = 32;
747 xev.ClientMessageEvent.ptr1 = l0;
748 xev.ClientMessageEvent.ptr2 = l1;
749 xev.ClientMessageEvent.ptr3 = l2;
750 xev.ClientMessageEvent.ptr4 = l3;
751 XSendEvent(DisplayHandle, RootWindow, false, new IntPtr ((int) (EventMask.SubstructureRedirectMask | EventMask.SubstructureNotifyMask)), ref xev);
754 void SendNetClientMessage(IntPtr window, IntPtr message_type, IntPtr l0, IntPtr l1, IntPtr l2) {
758 xev.ClientMessageEvent.type = XEventName.ClientMessage;
759 xev.ClientMessageEvent.send_event = true;
760 xev.ClientMessageEvent.window = window;
761 xev.ClientMessageEvent.message_type = message_type;
762 xev.ClientMessageEvent.format = 32;
763 xev.ClientMessageEvent.ptr1 = l0;
764 xev.ClientMessageEvent.ptr2 = l1;
765 xev.ClientMessageEvent.ptr3 = l2;
766 XSendEvent(DisplayHandle, window, false, new IntPtr ((int)EventMask.NoEventMask), ref xev);
769 // For WM_LBUTTONDOWN, WM_MBUTTONDOWN, WM_RBUTTONDOWN, WM_XBUTTONDOWN
770 // WM_CREATE and WM_DESTROY causes
771 void SendParentNotify(IntPtr child, Msg cause, int x, int y)
775 if (child == IntPtr.Zero) {
779 hwnd = Hwnd.GetObjectFromWindow (child);
785 if (hwnd.Handle == IntPtr.Zero) {
789 if (ExStyleSet ((int) hwnd.initial_ex_style, WindowExStyles.WS_EX_NOPARENTNOTIFY)) {
793 if (hwnd.Parent == null) {
797 if (hwnd.Parent.Handle == IntPtr.Zero) {
801 if (cause == Msg.WM_CREATE || cause == Msg.WM_DESTROY) {
802 SendMessage(hwnd.Parent.Handle, Msg.WM_PARENTNOTIFY, Control.MakeParam((int)cause, 0), child);
804 SendMessage(hwnd.Parent.Handle, Msg.WM_PARENTNOTIFY, Control.MakeParam((int)cause, 0), Control.MakeParam(x, y));
807 SendParentNotify (hwnd.Parent.Handle, cause, x, y);
810 bool StyleSet (int s, WindowStyles ws)
812 return (s & (int)ws) == (int)ws;
815 bool ExStyleSet (int ex, WindowExStyles exws)
817 return (ex & (int)exws) == (int)exws;
820 internal static Rectangle TranslateClientRectangleToXClientRectangle (Hwnd hwnd)
822 return TranslateClientRectangleToXClientRectangle (hwnd, Control.FromHandle (hwnd.Handle));
825 internal static Rectangle TranslateClientRectangleToXClientRectangle (Hwnd hwnd, Control ctrl)
828 * If this is a form with no window manager, X is handling all the border and caption painting
829 * so remove that from the area (since the area we set of the window here is the part of the window
830 * we're painting in only)
832 Rectangle rect = hwnd.ClientRect;
833 Form form = ctrl as Form;
834 CreateParams cp = null;
837 cp = form.GetCreateParams ();
839 if (form != null && (form.window_manager == null && !cp.IsSet (WindowExStyles.WS_EX_TOOLWINDOW))) {
840 Hwnd.Borders borders = Hwnd.GetBorders (cp, null);
841 Rectangle xrect = rect;
843 xrect.Y -= borders.top;
844 xrect.X -= borders.left;
845 xrect.Width += borders.left + borders.right;
846 xrect.Height += borders.top + borders.bottom;
851 if (rect.Width < 1 || rect.Height < 1) {
861 internal static Size TranslateWindowSizeToXWindowSize (CreateParams cp)
863 return TranslateWindowSizeToXWindowSize (cp, new Size (cp.Width, cp.Height));
866 internal static Size TranslateWindowSizeToXWindowSize (CreateParams cp, Size size)
869 * If this is a form with no window manager, X is handling all the border and caption painting
870 * so remove that from the area (since the area we set of the window here is the part of the window
871 * we're painting in only)
873 Form form = cp.control as Form;
874 if (form != null && (form.window_manager == null && !cp.IsSet (WindowExStyles.WS_EX_TOOLWINDOW))) {
875 Hwnd.Borders borders = Hwnd.GetBorders (cp, null);
878 xrect.Width -= borders.left + borders.right;
879 xrect.Height -= borders.top + borders.bottom;
883 if (size.Height == 0)
890 internal static Size TranslateXWindowSizeToWindowSize (CreateParams cp, int xWidth, int xHeight)
893 * If this is a form with no window manager, X is handling all the border and caption painting
894 * so remove that from the area (since the area we set of the window here is the part of the window
895 * we're painting in only)
897 Size rect = new Size (xWidth, xHeight);
898 Form form = cp.control as Form;
899 if (form != null && (form.window_manager == null && !cp.IsSet (WindowExStyles.WS_EX_TOOLWINDOW))) {
900 Hwnd.Borders borders = Hwnd.GetBorders (cp, null);
903 xrect.Width += borders.left + borders.right;
904 xrect.Height += borders.top + borders.bottom;
911 internal static Point GetTopLevelWindowLocation (Hwnd hwnd)
917 XTranslateCoordinates (DisplayHandle, hwnd.whole_window, RootWindow, 0, 0, out x, out y, out dummy);
918 frame = FrameExtents (hwnd.whole_window);
923 return new Point (x, y);
926 void DeriveStyles(int Style, int ExStyle, out FormBorderStyle border_style, out bool border_static, out TitleStyle title_style, out int caption_height, out int tool_caption_height) {
929 tool_caption_height = 19;
930 border_static = false;
932 if (StyleSet (Style, WindowStyles.WS_CHILD)) {
933 if (ExStyleSet (ExStyle, WindowExStyles.WS_EX_CLIENTEDGE)) {
934 border_style = FormBorderStyle.Fixed3D;
935 } else if (ExStyleSet (ExStyle, WindowExStyles.WS_EX_STATICEDGE)) {
936 border_style = FormBorderStyle.Fixed3D;
937 border_static = true;
938 } else if (!StyleSet (Style, WindowStyles.WS_BORDER)) {
939 border_style = FormBorderStyle.None;
941 border_style = FormBorderStyle.FixedSingle;
943 title_style = TitleStyle.None;
945 if (StyleSet (Style, WindowStyles.WS_CAPTION)) {
947 if (ExStyleSet (ExStyle, WindowExStyles.WS_EX_TOOLWINDOW)) {
948 title_style = TitleStyle.Tool;
950 title_style = TitleStyle.Normal;
954 if (ExStyleSet (ExStyle, WindowExStyles.WS_EX_MDICHILD)) {
957 if (StyleSet (Style, WindowStyles.WS_OVERLAPPEDWINDOW) ||
958 ExStyleSet (ExStyle, WindowExStyles.WS_EX_TOOLWINDOW)) {
959 border_style = (FormBorderStyle) 0xFFFF;
961 border_style = FormBorderStyle.None;
966 title_style = TitleStyle.None;
967 if (StyleSet (Style, WindowStyles.WS_CAPTION)) {
968 if (ExStyleSet (ExStyle, WindowExStyles.WS_EX_TOOLWINDOW)) {
969 title_style = TitleStyle.Tool;
971 title_style = TitleStyle.Normal;
975 border_style = FormBorderStyle.None;
977 if (StyleSet (Style, WindowStyles.WS_THICKFRAME)) {
978 if (ExStyleSet (ExStyle, WindowExStyles.WS_EX_TOOLWINDOW)) {
979 border_style = FormBorderStyle.SizableToolWindow;
981 border_style = FormBorderStyle.Sizable;
984 if (StyleSet (Style, WindowStyles.WS_CAPTION)) {
985 if (ExStyleSet (ExStyle, WindowExStyles.WS_EX_CLIENTEDGE)) {
986 border_style = FormBorderStyle.Fixed3D;
987 } else if (ExStyleSet (ExStyle, WindowExStyles.WS_EX_STATICEDGE)) {
988 border_style = FormBorderStyle.Fixed3D;
989 border_static = true;
990 } else if (ExStyleSet (ExStyle, WindowExStyles.WS_EX_DLGMODALFRAME)) {
991 border_style = FormBorderStyle.FixedDialog;
992 } else if (ExStyleSet (ExStyle, WindowExStyles.WS_EX_TOOLWINDOW)) {
993 border_style = FormBorderStyle.FixedToolWindow;
994 } else if (StyleSet (Style, WindowStyles.WS_BORDER)) {
995 border_style = FormBorderStyle.FixedSingle;
998 if (StyleSet (Style, WindowStyles.WS_BORDER)) {
999 border_style = FormBorderStyle.FixedSingle;
1006 void SetHwndStyles(Hwnd hwnd, CreateParams cp) {
1007 DeriveStyles(cp.Style, cp.ExStyle, out hwnd.border_style, out hwnd.border_static, out hwnd.title_style, out hwnd.caption_height, out hwnd.tool_caption_height);
1010 void SetWMStyles(Hwnd hwnd, CreateParams cp) {
1011 MotifWmHints mwmHints;
1012 MotifFunctions functions;
1013 MotifDecorations decorations;
1016 Rectangle client_rect;
1019 bool hide_from_taskbar;
1020 IntPtr transient_for_parent;
1022 // Windows we manage ourselves don't need WM window styles.
1023 if (cp.HasWindowManager && !cp.IsSet (WindowExStyles.WS_EX_TOOLWINDOW)) {
1028 mwmHints = new MotifWmHints();
1031 window_type = _NET_WM_WINDOW_TYPE_NORMAL;
1032 transient_for_parent = IntPtr.Zero;
1034 mwmHints.flags = (IntPtr)(MotifFlags.Functions | MotifFlags.Decorations);
1035 mwmHints.functions = (IntPtr)0;
1036 mwmHints.decorations = (IntPtr)0;
1038 form = cp.control as Form;
1040 if (ExStyleSet (cp.ExStyle, WindowExStyles.WS_EX_TOOLWINDOW)) {
1041 /* tool windows get no window manager
1045 /* just because the window doesn't get any decorations doesn't
1046 mean we should disable the functions. for instance, without
1047 MotifFunctions.Maximize, changing the windowstate to Maximized
1048 is ignored by metacity. */
1049 functions |= MotifFunctions.Move | MotifFunctions.Resize | MotifFunctions.Minimize | MotifFunctions.Maximize;
1050 } else if (form != null && form.FormBorderStyle == FormBorderStyle.None) {
1051 /* allow borderless window to be maximized */
1052 functions |= MotifFunctions.All | MotifFunctions.Resize;
1054 if (StyleSet (cp.Style, WindowStyles.WS_CAPTION)) {
1055 functions |= MotifFunctions.Move;
1056 decorations |= MotifDecorations.Title | MotifDecorations.Menu;
1059 if (StyleSet (cp.Style, WindowStyles.WS_THICKFRAME)) {
1060 functions |= MotifFunctions.Move | MotifFunctions.Resize;
1061 decorations |= MotifDecorations.Border | MotifDecorations.ResizeH;
1064 if (StyleSet (cp.Style, WindowStyles.WS_MINIMIZEBOX)) {
1065 functions |= MotifFunctions.Minimize;
1066 decorations |= MotifDecorations.Minimize;
1069 if (StyleSet (cp.Style, WindowStyles.WS_MAXIMIZEBOX)) {
1070 functions |= MotifFunctions.Maximize;
1071 decorations |= MotifDecorations.Maximize;
1074 if (StyleSet (cp.Style, WindowStyles.WS_SIZEBOX)) {
1075 functions |= MotifFunctions.Resize;
1076 decorations |= MotifDecorations.ResizeH;
1079 if (ExStyleSet (cp.ExStyle, WindowExStyles.WS_EX_DLGMODALFRAME)) {
1080 decorations |= MotifDecorations.Border;
1083 if (StyleSet (cp.Style, WindowStyles.WS_BORDER)) {
1084 decorations |= MotifDecorations.Border;
1087 if (StyleSet (cp.Style, WindowStyles.WS_DLGFRAME)) {
1088 decorations |= MotifDecorations.Border;
1091 if (StyleSet (cp.Style, WindowStyles.WS_SYSMENU)) {
1092 functions |= MotifFunctions.Close;
1095 functions &= ~(MotifFunctions.Maximize | MotifFunctions.Minimize | MotifFunctions.Close);
1096 decorations &= ~(MotifDecorations.Menu | MotifDecorations.Maximize | MotifDecorations.Minimize);
1097 if (cp.Caption == "") {
1098 functions &= ~MotifFunctions.Move;
1099 decorations &= ~(MotifDecorations.Title | MotifDecorations.ResizeH);
1104 if ((functions & MotifFunctions.Resize) == 0) {
1105 hwnd.fixed_size = true;
1106 Rectangle fixed_rectangle = new Rectangle (cp.X, cp.Y, cp.Width, cp.Height);
1107 SetWindowMinMax(hwnd.Handle, fixed_rectangle, fixed_rectangle.Size, fixed_rectangle.Size, cp);
1109 hwnd.fixed_size = false;
1112 mwmHints.functions = (IntPtr)functions;
1113 mwmHints.decorations = (IntPtr)decorations;
1115 DriverDebug ("SetWMStyles ({0}, {1}) functions = {2}, decorations = {3}", hwnd, cp, functions, decorations);
1117 if (cp.IsSet (WindowExStyles.WS_EX_TOOLWINDOW)) {
1118 // needed! map toolwindows to _NET_WM_WINDOW_TYPE_UTILITY to make newer metacity versions happy
1119 // and get those windows in front of their parents
1120 window_type = _NET_WM_WINDOW_TYPE_UTILITY;
1122 window_type = _NET_WM_WINDOW_TYPE_NORMAL;
1125 if (!cp.IsSet (WindowExStyles.WS_EX_APPWINDOW)) {
1126 hide_from_taskbar = true;
1127 } else if (cp.IsSet (WindowExStyles.WS_EX_TOOLWINDOW) && form != null && form.Parent != null && !form.ShowInTaskbar) {
1128 hide_from_taskbar = true;
1130 hide_from_taskbar = false;
1133 if (ExStyleSet (cp.ExStyle, WindowExStyles.WS_EX_TOOLWINDOW)) {
1134 if (form != null && !hwnd.reparented) {
1135 if (form.Owner != null && form.Owner.Handle != IntPtr.Zero) {
1136 Hwnd owner_hwnd = Hwnd.ObjectFromHandle (form.Owner.Handle);
1137 if (owner_hwnd != null)
1138 transient_for_parent = owner_hwnd.whole_window;
1142 if (StyleSet (cp.Style, WindowStyles.WS_POPUP) && (hwnd.parent != null) && (hwnd.parent.whole_window != IntPtr.Zero)) {
1143 transient_for_parent = hwnd.parent.whole_window;
1146 FormWindowState current_state = GetWindowState (hwnd.Handle);
1147 if (current_state == (FormWindowState)(-1))
1148 current_state = FormWindowState.Normal;
1150 client_rect = TranslateClientRectangleToXClientRectangle (hwnd);
1155 atoms [0] = window_type.ToInt32 ();
1156 XChangeProperty (DisplayHandle, hwnd.whole_window, _NET_WM_WINDOW_TYPE, (IntPtr)Atom.XA_ATOM, 32, PropertyMode.Replace, atoms, 1);
1158 XChangeProperty(DisplayHandle, hwnd.whole_window, _MOTIF_WM_HINTS, _MOTIF_WM_HINTS, 32, PropertyMode.Replace, ref mwmHints, 5);
1160 if (transient_for_parent != IntPtr.Zero) {
1161 XSetTransientForHint (DisplayHandle, hwnd.whole_window, transient_for_parent);
1164 MoveResizeWindow(DisplayHandle, hwnd.client_window, client_rect.X, client_rect.Y, client_rect.Width, client_rect.Height);
1166 if (hide_from_taskbar) {
1167 /* this line keeps the window from showing up in gnome's taskbar */
1168 atoms[atom_count++] = _NET_WM_STATE_SKIP_TASKBAR.ToInt32();
1170 /* we need to add these atoms in the
1171 * event we're maximized, since we're
1172 * replacing the existing
1173 * _NET_WM_STATE here. If we don't
1174 * add them, future calls to
1175 * GetWindowState will return Normal
1176 * for a window which is maximized. */
1177 if (current_state == FormWindowState.Maximized) {
1178 atoms[atom_count++] = _NET_WM_STATE_MAXIMIZED_HORZ.ToInt32();
1179 atoms[atom_count++] = _NET_WM_STATE_MAXIMIZED_VERT.ToInt32();
1182 if (form != null && form.Modal) {
1183 atoms[atom_count++] = _NET_WM_STATE_MODAL.ToInt32 ();
1186 XChangeProperty(DisplayHandle, hwnd.whole_window, _NET_WM_STATE, (IntPtr)Atom.XA_ATOM, 32, PropertyMode.Replace, atoms, atom_count);
1189 IntPtr[] atom_ptrs = new IntPtr[2];
1190 atom_ptrs[atom_count++] = WM_DELETE_WINDOW;
1191 if (ExStyleSet (cp.ExStyle, WindowExStyles.WS_EX_CONTEXTHELP)) {
1192 atom_ptrs[atom_count++] = _NET_WM_CONTEXT_HELP;
1195 XSetWMProtocols(DisplayHandle, hwnd.whole_window, atom_ptrs, atom_count);
1199 void SetIcon(Hwnd hwnd, Icon icon)
1204 // This really needs to do whatever it
1205 // takes to remove the window manager
1206 // menu, not just delete the ICON
1207 // property. This will cause metacity
1208 // to use the "no icon set" icon, and
1209 // we'll still have an icon.
1210 XDeleteProperty (DisplayHandle, hwnd.whole_window, _NET_WM_ICON);
1218 bitmap = icon.ToBitmap();
1220 size = bitmap.Width * bitmap.Height + 2;
1221 data = new IntPtr[size];
1223 data[index++] = (IntPtr)bitmap.Width;
1224 data[index++] = (IntPtr)bitmap.Height;
1226 for (int y = 0; y < bitmap.Height; y++) {
1227 for (int x = 0; x < bitmap.Width; x++) {
1228 data[index++] = (IntPtr)bitmap.GetPixel (x, y).ToArgb ();
1232 XChangeProperty (DisplayHandle, hwnd.whole_window,
1233 _NET_WM_ICON, (IntPtr)Atom.XA_CARDINAL, 32,
1234 PropertyMode.Replace, data, size);
1238 void WakeupMain () {
1240 wake.Send (new byte [] { 0xFF });
1241 } catch (SocketException ex) {
1242 if (ex.SocketErrorCode != SocketError.WouldBlock) {
1248 XEventQueue ThreadQueue(Thread thread) {
1251 queue = (XEventQueue)MessageQueues[thread];
1252 if (queue == null) {
1253 queue = new XEventQueue(thread);
1254 MessageQueues[thread] = queue;
1260 void TranslatePropertyToClipboard(IntPtr property) {
1265 IntPtr prop = IntPtr.Zero;
1267 Clipboard.Item = null;
1269 XGetWindowProperty(DisplayHandle, FosterParent, property, IntPtr.Zero, new IntPtr (0x7fffffff), true, (IntPtr)Atom.AnyPropertyType, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
1271 if ((long)nitems > 0) {
1272 if (property == (IntPtr)Atom.XA_STRING) {
1273 // Xamarin-5116: PtrToStringAnsi expects to get UTF-8, but we might have
1275 var s = Marshal.PtrToStringAnsi (prop);
1276 if (string.IsNullOrEmpty (s)) {
1277 var sb = new StringBuilder ();
1278 for (int i = 0; i < (int)nitems; i++) {
1279 var b = Marshal.ReadByte (prop, i);
1280 sb.Append ((char)b);
1284 // Some X managers/apps pass unicode chars as escaped strings, so
1285 // we may need to unescape them.
1286 Clipboard.Item = UnescapeUnicodeFromAnsi (s);
1287 } else if (property == (IntPtr)Atom.XA_BITMAP) {
1288 // FIXME - convert bitmap to image
1289 } else if (property == (IntPtr)Atom.XA_PIXMAP) {
1290 // FIXME - convert pixmap to image
1291 } else if (property == OEMTEXT) {
1292 Clipboard.Item = UnescapeUnicodeFromAnsi (Marshal.PtrToStringAnsi(prop));
1293 } else if (property == UTF8_STRING) {
1294 byte [] buffer = new byte [(int)nitems];
1295 for (int i = 0; i < (int)nitems; i++)
1296 buffer [i] = Marshal.ReadByte (prop, i);
1297 Clipboard.Item = Encoding.UTF8.GetString (buffer);
1298 } else if (property == UTF16_STRING) {
1299 Clipboard.Item = Marshal.PtrToStringUni (prop, Encoding.Unicode.GetMaxCharCount ((int)nitems));
1300 } else if (property == RICHTEXTFORMAT)
1301 Clipboard.Item = Marshal.PtrToStringAnsi(prop);
1302 else if (DataFormats.ContainsFormat (property.ToInt32 ())) {
1303 if (DataFormats.GetFormat (property.ToInt32 ()).is_serializable) {
1304 MemoryStream memory_stream = new MemoryStream ((int)nitems);
1305 for (int i = 0; i < (int)nitems; i++)
1306 memory_stream.WriteByte (Marshal.ReadByte (prop, i));
1308 memory_stream.Position = 0;
1309 BinaryFormatter formatter = new BinaryFormatter ();
1310 Clipboard.Item = formatter.Deserialize (memory_stream);
1311 memory_stream.Close ();
1319 string UnescapeUnicodeFromAnsi (string value)
1321 if (value == null || value.IndexOf ("\\u") == -1)
1324 StringBuilder sb = new StringBuilder (value.Length);
1328 while (start < value.Length) {
1329 pos = value.IndexOf ("\\u", start);
1333 sb.Append (value, start, pos - start);
1338 while (pos < value.Length) {
1339 if (!ValidHexDigit (value [pos]))
1346 if (!Int32.TryParse (value.Substring (start, length), System.Globalization.NumberStyles.HexNumber,
1348 return value; // Error, return the unescaped original value.
1350 sb.Append ((char)res);
1354 // Append any remaining data.
1355 if (start < value.Length)
1356 sb.Append (value, start, value.Length - start);
1358 return sb.ToString ();
1361 private static bool ValidHexDigit (char e)
1363 return Char.IsDigit (e) || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
1366 void AddExpose (Hwnd hwnd, bool client, int x, int y, int width, int height) {
1368 if ((hwnd == null) || (x > hwnd.Width) || (y > hwnd.Height) || ((x + width) < 0) || ((y + height) < 0)) {
1372 // Keep the invalid area as small as needed
1373 if ((x + width) > hwnd.width) {
1374 width = hwnd.width - x;
1377 if ((y + height) > hwnd.height) {
1378 height = hwnd.height - y;
1382 hwnd.AddInvalidArea(x, y, width, height);
1383 if (!hwnd.expose_pending) {
1384 if (!hwnd.nc_expose_pending) {
1385 hwnd.Queue.Paint.Enqueue(hwnd);
1387 hwnd.expose_pending = true;
1390 hwnd.AddNcInvalidArea (x, y, width, height);
1392 if (!hwnd.nc_expose_pending) {
1393 if (!hwnd.expose_pending) {
1394 hwnd.Queue.Paint.Enqueue(hwnd);
1396 hwnd.nc_expose_pending = true;
1401 static Hwnd.Borders FrameExtents (IntPtr window)
1407 IntPtr prop = IntPtr.Zero;
1408 Hwnd.Borders rect = new Hwnd.Borders ();
1410 XGetWindowProperty (DisplayHandle, window, _NET_FRAME_EXTENTS, IntPtr.Zero, new IntPtr (16), false, (IntPtr)Atom.XA_CARDINAL, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
1411 if (prop != IntPtr.Zero) {
1412 if (nitems.ToInt32 () == 4) {
1413 rect.left = Marshal.ReadInt32 (prop, 0);
1414 rect.right = Marshal.ReadInt32 (prop, IntPtr.Size);
1415 rect.top = Marshal.ReadInt32 (prop, 2 * IntPtr.Size);
1416 rect.bottom = Marshal.ReadInt32 (prop, 3 * IntPtr.Size);
1424 void AddConfigureNotify (XEvent xevent) {
1427 hwnd = Hwnd.GetObjectFromWindow(xevent.ConfigureEvent.window);
1430 if (hwnd == null || hwnd.zombie) {
1433 if ((xevent.ConfigureEvent.window == hwnd.whole_window)/* && (xevent.ConfigureEvent.window == xevent.ConfigureEvent.xevent)*/) {
1434 if (hwnd.parent == null) {
1435 // The location given by the event is not reliable between different wm's,
1436 // so use an alternative way of getting it.
1437 Point location = GetTopLevelWindowLocation (hwnd);
1438 hwnd.x = location.X;
1439 hwnd.y = location.Y;
1442 // XXX this sucks. this isn't thread safe
1443 Control ctrl = Control.FromHandle (hwnd.Handle);
1444 Size TranslatedSize;
1446 TranslatedSize = TranslateXWindowSizeToWindowSize (ctrl.GetCreateParams (), xevent.ConfigureEvent.width, xevent.ConfigureEvent.height);
1448 TranslatedSize = new Size (xevent.ConfigureEvent.width, xevent.ConfigureEvent.height);
1450 hwnd.width = TranslatedSize.Width;
1451 hwnd.height = TranslatedSize.Height;
1452 hwnd.ClientRect = Rectangle.Empty;
1454 DriverDebug ("AddConfigureNotify (hwnd.Handle = {1}, final hwnd.rect = {0}, reported rect={2})",
1455 new Rectangle (hwnd.x, hwnd.y, hwnd.width, hwnd.height), hwnd.Handle,
1456 new Rectangle (xevent.ConfigureEvent.x, xevent.ConfigureEvent.y, xevent.ConfigureEvent.width, xevent.ConfigureEvent.width));
1457 lock (hwnd.configure_lock) {
1458 if (!hwnd.configure_pending) {
1459 hwnd.Queue.EnqueueLocked (xevent);
1460 hwnd.configure_pending = true;
1464 // We drop configure events for Client windows
1468 if ((Caret.gc == IntPtr.Zero) || Caret.On) {
1474 XDrawLine(DisplayHandle, Caret.Window, Caret.gc, Caret.X, Caret.Y, Caret.X, Caret.Y + Caret.Height);
1479 if ((Caret.gc == IntPtr.Zero) || !Caret.On) {
1485 XDrawLine(DisplayHandle, Caret.Window, Caret.gc, Caret.X, Caret.Y, Caret.X, Caret.Y + Caret.Height);
1489 int NextTimeout (ArrayList timers, DateTime now) {
1492 foreach (Timer timer in timers) {
1493 int next = (int) (timer.Expires - now).TotalMilliseconds;
1495 return 0; // Have a timer that has already expired
1498 if (next < timeout) {
1502 if (timeout < Timer.Minimum) {
1503 timeout = Timer.Minimum;
1511 void CheckTimers (ArrayList timers, DateTime now) {
1514 count = timers.Count;
1519 for (int i = 0; i < timers.Count; i++) {
1522 timer = (Timer) timers [i];
1524 if (timer.Enabled && timer.Expires <= now && !timer.Busy) {
1526 // - Before MainForm.OnLoad if DoEvents () is called.
1527 // - After MainForm.OnLoad if not.
1530 (Application.MWFThread.Current.Context != null &&
1531 (Application.MWFThread.Current.Context.MainForm == null ||
1532 Application.MWFThread.Current.Context.MainForm.IsLoaded))) {
1542 void WaitForHwndMessage (Hwnd hwnd, Msg message) {
1543 WaitForHwndMessage (hwnd, message, false);
1547 void WaitForHwndMessage (Hwnd hwnd, Msg message, bool process) {
1548 MSG msg = new MSG ();
1551 queue = ThreadQueue(Thread.CurrentThread);
1553 queue.DispatchIdle = false;
1556 string key = hwnd.Handle + ":" + message;
1557 if (!messageHold.ContainsKey (key))
1558 messageHold.Add (key, 1);
1560 messageHold[key] = ((int)messageHold[key]) + 1;
1565 DebugHelper.WriteLine ("Waiting for message " + message + " on hwnd " + String.Format("0x{0:x}", hwnd.Handle.ToInt32 ()));
1566 DebugHelper.Indent ();
1568 if (PeekMessage(queue, ref msg, IntPtr.Zero, 0, 0, (uint)PeekMessageFlags.PM_REMOVE)) {
1569 if ((Msg)msg.message == Msg.WM_QUIT) {
1570 PostQuitMessage (0);
1575 DebugHelper.WriteLine ("PeekMessage got " + msg);
1577 if (msg.hwnd == hwnd.Handle) {
1578 if ((Msg)msg.message == message) {
1580 TranslateMessage (ref msg);
1581 DispatchMessage (ref msg);
1585 else if ((Msg)msg.message == Msg.WM_DESTROY)
1589 TranslateMessage (ref msg);
1590 DispatchMessage (ref msg);
1594 done = !messageHold.ContainsKey (key) || ((int)messageHold[key] < 1) || done;
1597 messageHold.Remove (key);
1599 DebugHelper.Unindent ();
1600 DebugHelper.WriteLine ("Finished waiting for " + key);
1602 queue.DispatchIdle = true;
1606 void MapWindow(Hwnd hwnd, WindowType windows) {
1608 Form f = Control.FromHandle(hwnd.Handle) as Form;
1610 if (f.WindowState == FormWindowState.Normal) {
1611 f.waiting_showwindow = true;
1612 SendMessage(hwnd.Handle, Msg.WM_SHOWWINDOW, (IntPtr)1, IntPtr.Zero);
1616 // it's possible that our Hwnd is no
1617 // longer valid after making that
1618 // SendMessage call, so check here.
1623 // Most window managers will respect the _NET_WM_STATE property.
1624 // If not, use XMapRaised to map the window at the top level as
1625 // a last ditch effort.
1626 if ((windows & WindowType.Whole) != 0) {
1627 XMapRaised(DisplayHandle, hwnd.whole_window);
1629 if ((windows & WindowType.Client) != 0) {
1630 XMapRaised(DisplayHandle, hwnd.client_window);
1633 if ((windows & WindowType.Whole) != 0) {
1634 XMapWindow(DisplayHandle, hwnd.whole_window);
1636 if ((windows & WindowType.Client) != 0) {
1637 XMapWindow(DisplayHandle, hwnd.client_window);
1644 if (f.waiting_showwindow) {
1645 WaitForHwndMessage (hwnd, Msg.WM_SHOWWINDOW);
1646 CreateParams cp = f.GetCreateParams();
1647 if (!ExStyleSet (cp.ExStyle, WindowExStyles.WS_EX_MDICHILD) &&
1648 !StyleSet (cp.Style, WindowStyles.WS_CHILD)) {
1649 WaitForHwndMessage (hwnd, Msg.WM_ACTIVATE, true);
1656 void UnmapWindow(Hwnd hwnd, WindowType windows) {
1659 if (Control.FromHandle(hwnd.Handle) is Form) {
1660 f = Control.FromHandle(hwnd.Handle) as Form;
1661 if (f.WindowState == FormWindowState.Normal) {
1662 f.waiting_showwindow = true;
1663 SendMessage(hwnd.Handle, Msg.WM_SHOWWINDOW, IntPtr.Zero, IntPtr.Zero);
1667 // it's possible that our Hwnd is no
1668 // longer valid after making that
1669 // SendMessage call, so check here.
1670 // FIXME: it is likely wrong, as it has already sent WM_SHOWWINDOW
1674 if ((windows & WindowType.Client) != 0) {
1675 XUnmapWindow(DisplayHandle, hwnd.client_window);
1677 if ((windows & WindowType.Whole) != 0) {
1678 XUnmapWindow(DisplayHandle, hwnd.whole_window);
1681 hwnd.mapped = false;
1684 if (f.waiting_showwindow) {
1685 WaitForHwndMessage (hwnd, Msg.WM_SHOWWINDOW);
1686 CreateParams cp = f.GetCreateParams();
1687 if (!ExStyleSet (cp.ExStyle, WindowExStyles.WS_EX_MDICHILD) &&
1688 !StyleSet (cp.Style, WindowStyles.WS_CHILD)) {
1689 WaitForHwndMessage (hwnd, Msg.WM_ACTIVATE, true);
1696 void UpdateMessageQueue (XEventQueue queue) {
1697 UpdateMessageQueue(queue, true);
1700 void UpdateMessageQueue (XEventQueue queue, bool allowIdle) {
1705 now = DateTime.UtcNow;
1708 pending = XPending (DisplayHandle);
1711 if (pending == 0 && allowIdle) {
1712 if ((queue == null || queue.DispatchIdle) && Idle != null) {
1713 Idle (this, EventArgs.Empty);
1717 pending = XPending (DisplayHandle);
1724 if (queue != null) {
1725 if (queue.Paint.Count > 0)
1728 timeout = NextTimeout (queue.timer_list, now);
1733 int length = pollfds.Length - 1;
1734 lock (wake_waiting_lock) {
1735 if (wake_waiting == false) {
1737 wake_waiting = true;
1741 Syscall.poll (pollfds, (uint)length, timeout);
1742 // Clean out buffer, so we're not busy-looping on the same data
1743 if (length == pollfds.Length) {
1744 if (pollfds[1].revents != 0)
1745 wake_receive.Receive(network_buffer, 0, 1, SocketFlags.None);
1746 lock (wake_waiting_lock) {
1747 wake_waiting = false;
1752 pending = XPending (DisplayHandle);
1758 CheckTimers (queue.timer_list, now);
1761 XEvent xevent = new XEvent ();
1764 if (XPending (DisplayHandle) == 0)
1767 XNextEvent (DisplayHandle, ref xevent);
1769 if (xevent.AnyEvent.type == XEventName.KeyPress ||
1770 xevent.AnyEvent.type == XEventName.KeyRelease) {
1771 // PreFilter() handles "shift key state updates.
1772 Keyboard.PreFilter (xevent);
1773 if (XFilterEvent (ref xevent, Keyboard.ClientWindow)) {
1774 // probably here we could raise WM_IME_KEYDOWN and
1775 // WM_IME_KEYUP, but I'm not sure it is worthy.
1779 else if (XFilterEvent (ref xevent, IntPtr.Zero))
1783 hwnd = Hwnd.GetObjectFromWindow(xevent.AnyEvent.window);
1787 DebugHelper.WriteLine ("UpdateMessageQueue got Event: " + xevent.ToString ());
1789 switch (xevent.type) {
1790 case XEventName.Expose:
1791 AddExpose (hwnd, xevent.ExposeEvent.window == hwnd.ClientWindow, xevent.ExposeEvent.x, xevent.ExposeEvent.y, xevent.ExposeEvent.width, xevent.ExposeEvent.height);
1794 case XEventName.SelectionClear: {
1795 // Should we do something?
1799 case XEventName.SelectionRequest: {
1800 if (Dnd.HandleSelectionRequestEvent (ref xevent))
1804 sel_event = new XEvent();
1805 sel_event.SelectionEvent.type = XEventName.SelectionNotify;
1806 sel_event.SelectionEvent.send_event = true;
1807 sel_event.SelectionEvent.display = DisplayHandle;
1808 sel_event.SelectionEvent.selection = xevent.SelectionRequestEvent.selection;
1809 sel_event.SelectionEvent.target = xevent.SelectionRequestEvent.target;
1810 sel_event.SelectionEvent.requestor = xevent.SelectionRequestEvent.requestor;
1811 sel_event.SelectionEvent.time = xevent.SelectionRequestEvent.time;
1812 sel_event.SelectionEvent.property = IntPtr.Zero;
1814 IntPtr format_atom = xevent.SelectionRequestEvent.target;
1816 // Seems that some apps support asking for supported types
1817 if (format_atom == TARGETS) {
1824 if (Clipboard.IsSourceText) {
1825 atoms[atom_count++] = (int)Atom.XA_STRING;
1826 atoms[atom_count++] = (int)OEMTEXT;
1827 atoms[atom_count++] = (int)UTF8_STRING;
1828 atoms[atom_count++] = (int)UTF16_STRING;
1829 atoms[atom_count++] = (int)RICHTEXTFORMAT;
1830 } else if (Clipboard.IsSourceImage) {
1831 atoms[atom_count++] = (int)Atom.XA_PIXMAP;
1832 atoms[atom_count++] = (int)Atom.XA_BITMAP;
1834 // FIXME - handle other types
1837 XChangeProperty(DisplayHandle, xevent.SelectionRequestEvent.requestor, (IntPtr)xevent.SelectionRequestEvent.property,
1838 (IntPtr)xevent.SelectionRequestEvent.target, 32, PropertyMode.Replace, atoms, atom_count);
1839 sel_event.SelectionEvent.property = xevent.SelectionRequestEvent.property;
1840 } else if (format_atom == (IntPtr)RICHTEXTFORMAT) {
1841 string rtf_text = Clipboard.GetRtfText ();
1842 if (rtf_text != null) {
1843 // The RTF spec mentions that ascii is enough to contain it
1844 Byte [] bytes = Encoding.ASCII.GetBytes (rtf_text);
1845 int buflen = bytes.Length;
1846 IntPtr buffer = Marshal.AllocHGlobal (buflen);
1848 for (int i = 0; i < buflen; i++)
1849 Marshal.WriteByte (buffer, i, bytes[i]);
1851 XChangeProperty(DisplayHandle, xevent.SelectionRequestEvent.requestor, (IntPtr)xevent.SelectionRequestEvent.property,
1852 (IntPtr)xevent.SelectionRequestEvent.target, 8, PropertyMode.Replace, buffer, buflen);
1853 sel_event.SelectionEvent.property = xevent.SelectionRequestEvent.property;
1854 Marshal.FreeHGlobal(buffer);
1856 } else if (Clipboard.IsSourceText &&
1857 (format_atom == (IntPtr)Atom.XA_STRING
1858 || format_atom == OEMTEXT
1859 || format_atom == UTF16_STRING
1860 || format_atom == UTF8_STRING)) {
1861 IntPtr buffer = IntPtr.Zero;
1863 Encoding encoding = null;
1867 // Select an encoding depending on the target
1868 IntPtr target_atom = xevent.SelectionRequestEvent.target;
1869 if (target_atom == (IntPtr)Atom.XA_STRING || target_atom == OEMTEXT)
1870 // FIXME - EOMTEXT should encode into ISO2022
1871 encoding = Encoding.ASCII;
1872 else if (target_atom == UTF16_STRING)
1873 encoding = Encoding.Unicode;
1874 else if (target_atom == UTF8_STRING)
1875 encoding = Encoding.UTF8;
1879 bytes = encoding.GetBytes (Clipboard.GetPlainText ());
1880 buffer = Marshal.AllocHGlobal (bytes.Length);
1881 buflen = bytes.Length;
1883 for (int i = 0; i < buflen; i++)
1884 Marshal.WriteByte (buffer, i, bytes [i]);
1886 if (buffer != IntPtr.Zero) {
1887 XChangeProperty(DisplayHandle, xevent.SelectionRequestEvent.requestor, (IntPtr)xevent.SelectionRequestEvent.property, (IntPtr)xevent.SelectionRequestEvent.target, 8, PropertyMode.Replace, buffer, buflen);
1888 sel_event.SelectionEvent.property = xevent.SelectionRequestEvent.property;
1889 Marshal.FreeHGlobal(buffer);
1891 } else if (Clipboard.GetSource (format_atom.ToInt32 ()) != null) { // check if we have an available value of this format
1892 if (DataFormats.GetFormat (format_atom.ToInt32 ()).is_serializable) {
1893 object serializable = Clipboard.GetSource (format_atom.ToInt32 ());
1895 BinaryFormatter formatter = new BinaryFormatter ();
1896 MemoryStream memory_stream = new MemoryStream ();
1897 formatter.Serialize (memory_stream, serializable);
1899 int buflen = (int)memory_stream.Length;
1900 IntPtr buffer = Marshal.AllocHGlobal (buflen);
1901 memory_stream.Position = 0;
1902 for (int i = 0; i < buflen; i++)
1903 Marshal.WriteByte (buffer, i, (byte)memory_stream.ReadByte ());
1904 memory_stream.Close ();
1906 XChangeProperty (DisplayHandle, xevent.SelectionRequestEvent.requestor, (IntPtr)xevent.SelectionRequestEvent.property, (IntPtr)xevent.SelectionRequestEvent.target,
1907 8, PropertyMode.Replace, buffer, buflen);
1908 sel_event.SelectionEvent.property = xevent.SelectionRequestEvent.property;
1909 Marshal.FreeHGlobal (buffer);
1912 } else if (Clipboard.IsSourceImage) {
1913 if (xevent.SelectionEvent.target == (IntPtr)Atom.XA_PIXMAP) {
1914 // FIXME - convert image and store as property
1915 } else if (xevent.SelectionEvent.target == (IntPtr)Atom.XA_PIXMAP) {
1916 // FIXME - convert image and store as property
1920 XSendEvent(DisplayHandle, xevent.SelectionRequestEvent.requestor, false, new IntPtr ((int)EventMask.NoEventMask), ref sel_event);
1924 case XEventName.SelectionNotify: {
1925 if (Clipboard.Enumerating) {
1926 Clipboard.Enumerating = false;
1927 if (xevent.SelectionEvent.property != IntPtr.Zero) {
1928 XDeleteProperty(DisplayHandle, FosterParent, (IntPtr)xevent.SelectionEvent.property);
1929 if (!Clipboard.Formats.Contains(xevent.SelectionEvent.property)) {
1930 Clipboard.Formats.Add(xevent.SelectionEvent.property);
1931 DriverDebug("Got supported clipboard atom format: {0}", xevent.SelectionEvent.property);
1934 } else if (Clipboard.Retrieving) {
1935 Clipboard.Retrieving = false;
1936 if (xevent.SelectionEvent.property != IntPtr.Zero) {
1937 TranslatePropertyToClipboard(xevent.SelectionEvent.property);
1939 Clipboard.ClearSources ();
1940 Clipboard.Item = null;
1943 Dnd.HandleSelectionNotifyEvent (ref xevent);
1948 case XEventName.KeyRelease:
1949 if (!detectable_key_auto_repeat && XPending (DisplayHandle) != 0) {
1950 XEvent nextevent = new XEvent ();
1952 XPeekEvent (DisplayHandle, ref nextevent);
1954 if (nextevent.type == XEventName.KeyPress &&
1955 nextevent.KeyEvent.keycode == xevent.KeyEvent.keycode &&
1956 nextevent.KeyEvent.time == xevent.KeyEvent.time) {
1960 goto case XEventName.KeyPress;
1962 case XEventName.MotionNotify: {
1965 /* we can't do motion compression across threads, so just punt if we don't match up */
1966 if (Thread.CurrentThread == hwnd.Queue.Thread && hwnd.Queue.Count > 0) {
1967 peek = hwnd.Queue.Peek();
1968 if (peek.AnyEvent.type == XEventName.MotionNotify) {
1972 goto case XEventName.KeyPress;
1975 case XEventName.KeyPress:
1976 hwnd.Queue.EnqueueLocked (xevent);
1977 /* Process KeyPresses immediately. Otherwise multiple Compose messages as a result of a
1978 * single physical keypress are not processed correctly */
1980 case XEventName.ButtonPress:
1981 case XEventName.ButtonRelease:
1982 case XEventName.EnterNotify:
1983 case XEventName.LeaveNotify:
1984 case XEventName.CreateNotify:
1985 case XEventName.DestroyNotify:
1986 case XEventName.FocusIn:
1987 case XEventName.FocusOut:
1988 case XEventName.ClientMessage:
1989 case XEventName.ReparentNotify:
1990 case XEventName.MapNotify:
1991 case XEventName.UnmapNotify:
1992 hwnd.Queue.EnqueueLocked (xevent);
1995 case XEventName.ConfigureNotify:
1996 AddConfigureNotify(xevent);
1999 case XEventName.PropertyNotify:
2000 DriverDebug ("UpdateMessageQueue (), got Event: {0}", xevent.ToString ());
2001 if (xevent.PropertyEvent.atom == _NET_ACTIVE_WINDOW) {
2006 IntPtr prop = IntPtr.Zero;
2009 prev_active = ActiveWindow;
2010 XGetWindowProperty(DisplayHandle, RootWindow, _NET_ACTIVE_WINDOW, IntPtr.Zero, new IntPtr (1), false, (IntPtr)Atom.XA_WINDOW, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
2011 if (((long)nitems > 0) && (prop != IntPtr.Zero)) {
2012 ActiveWindow = Hwnd.GetHandleFromWindow((IntPtr)Marshal.ReadInt32(prop));
2015 DebugHelper.WriteLine ("PropertyNotify: _NET_ACTIVE_WINDOW: previous = 0x{0:x}, new = 0x{1:x}", prev_active.ToInt32 (), ActiveWindow.ToInt32 ());
2017 if (prev_active != ActiveWindow) {
2018 if (prev_active != IntPtr.Zero) {
2019 PostMessage(prev_active, Msg.WM_ACTIVATE, (IntPtr)WindowActiveFlags.WA_INACTIVE, IntPtr.Zero);
2021 if (ActiveWindow != IntPtr.Zero) {
2022 PostMessage(ActiveWindow, Msg.WM_ACTIVATE, (IntPtr)WindowActiveFlags.WA_ACTIVE, IntPtr.Zero);
2025 if (ModalWindows.Count == 0) {
2028 // Modality Handling
2030 // If there is a modal window on the stack and the new active
2031 // window is MWF window, but not the modal one and not a non-modal
2032 // child of the modal one, switch back to the modal window.
2034 // To identify if a non-modal form is child of a modal form
2035 // we match their ApplicationContexts, which will be the same.
2036 // This is because each modal form runs the loop with a
2037 // new ApplicationContext, which is inherited by the non-modal
2040 Form activeForm = Control.FromHandle (ActiveWindow) as Form;
2041 if (activeForm != null) {
2042 Form modalForm = Control.FromHandle ((IntPtr)ModalWindows.Peek()) as Form;
2043 if (ActiveWindow != (IntPtr)ModalWindows.Peek() &&
2044 (modalForm == null || activeForm.context == modalForm.context)) {
2045 Activate((IntPtr)ModalWindows.Peek());
2052 else if (xevent.PropertyEvent.atom == _NET_WM_STATE) {
2053 // invalidate our cache - we'll query again the next time someone does GetWindowState.
2054 hwnd.cached_window_state = (FormWindowState)(-1);
2055 PostMessage (hwnd.Handle, Msg.WM_WINDOWPOSCHANGED, IntPtr.Zero, IntPtr.Zero);
2063 IntPtr GetMousewParam(int Delta) {
2066 if ((MouseState & MouseButtons.Left) != 0) {
2067 result |= (int)MsgButtons.MK_LBUTTON;
2070 if ((MouseState & MouseButtons.Middle) != 0) {
2071 result |= (int)MsgButtons.MK_MBUTTON;
2074 if ((MouseState & MouseButtons.Right) != 0) {
2075 result |= (int)MsgButtons.MK_RBUTTON;
2078 Keys mods = ModifierKeys;
2079 if ((mods & Keys.Control) != 0) {
2080 result |= (int)MsgButtons.MK_CONTROL;
2083 if ((mods & Keys.Shift) != 0) {
2084 result |= (int)MsgButtons.MK_SHIFT;
2087 result |= Delta << 16;
2089 return (IntPtr)result;
2091 IntPtr XGetParent(IntPtr handle) {
2098 XQueryTree(DisplayHandle, handle, out Root, out Parent, out Children, out ChildCount);
2101 if (Children!=IntPtr.Zero) {
2109 int HandleError (IntPtr display, ref XErrorEvent error_event)
2111 // we need to workaround a problem with the
2112 // ordering of destruction of Drawables and
2113 // Pictures that exists between cairo and
2114 // RENDER on the server.
2115 if (error_event.request_code == (XRequest)render_major_opcode
2116 && error_event.minor_code == 7 /* X_RenderFreePicture from render.h */
2117 && error_event.error_code == render_first_error + 1 /* BadPicture from render.h */) {
2121 if (ErrorExceptions) {
2122 XUngrabPointer (display, IntPtr.Zero);
2123 throw new XException (error_event.display, error_event.resourceid,
2124 error_event.serial, error_event.error_code,
2125 error_event.request_code, error_event.minor_code);
2127 Console.WriteLine("X11 Error encountered: {0}{1}\n",
2128 XException.GetMessage (error_event.display, error_event.resourceid,
2129 error_event.serial, error_event.error_code,
2130 error_event.request_code, error_event.minor_code),
2131 Environment.StackTrace);
2136 void AccumulateDestroyedHandles (Control c, ArrayList list)
2138 DebugHelper.Enter ();
2141 Control[] controls = c.Controls.GetAllControls ();
2143 DebugHelper.WriteLine ("Checking control:0x{0:x}", c.IsHandleCreated ? c.Handle.ToInt32() : 0);
2145 if (c.IsHandleCreated && !c.IsDisposed) {
2146 Hwnd hwnd = Hwnd.ObjectFromHandle(c.Handle);
2148 DriverDebug (" + adding {0} to the list of zombie windows", XplatUI.Window (hwnd.Handle));
2149 DriverDebug (" + parent X window is {0:X}", XGetParent (hwnd.whole_window).ToInt32());
2152 CleanupCachedWindows (hwnd);
2155 for (int i = 0; i < controls.Length; i ++) {
2156 AccumulateDestroyedHandles (controls[i], list);
2159 DebugHelper.Leave ();
2162 void CleanupCachedWindows (Hwnd hwnd)
2164 if (ActiveWindow == hwnd.Handle) {
2165 SendMessage(hwnd.client_window, Msg.WM_ACTIVATE, (IntPtr)WindowActiveFlags.WA_INACTIVE, IntPtr.Zero);
2166 ActiveWindow = IntPtr.Zero;
2169 if (FocusWindow == hwnd.Handle) {
2170 SendMessage(hwnd.client_window, Msg.WM_KILLFOCUS, IntPtr.Zero, IntPtr.Zero);
2171 FocusWindow = IntPtr.Zero;
2174 if (Grab.Hwnd == hwnd.Handle) {
2175 Grab.Hwnd = IntPtr.Zero;
2176 Grab.Confined = false;
2179 DestroyCaret (hwnd.Handle);
2182 void PerformNCCalc(Hwnd hwnd) {
2183 XplatUIWin32.NCCALCSIZE_PARAMS ncp;
2187 rect = new Rectangle (0, 0, hwnd.Width, hwnd.Height);
2189 ncp = new XplatUIWin32.NCCALCSIZE_PARAMS();
2190 ptr = Marshal.AllocHGlobal(Marshal.SizeOf(ncp));
2192 ncp.rgrc1.left = rect.Left;
2193 ncp.rgrc1.top = rect.Top;
2194 ncp.rgrc1.right = rect.Right;
2195 ncp.rgrc1.bottom = rect.Bottom;
2197 Marshal.StructureToPtr(ncp, ptr, true);
2198 NativeWindow.WndProc(hwnd.client_window, Msg.WM_NCCALCSIZE, (IntPtr)1, ptr);
2199 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(ptr, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
2200 Marshal.FreeHGlobal(ptr);
2203 rect = new Rectangle(ncp.rgrc1.left, ncp.rgrc1.top, ncp.rgrc1.right - ncp.rgrc1.left, ncp.rgrc1.bottom - ncp.rgrc1.top);
2204 hwnd.ClientRect = rect;
2206 rect = TranslateClientRectangleToXClientRectangle (hwnd);
2209 MoveResizeWindow (DisplayHandle, hwnd.client_window, rect.X, rect.Y, rect.Width, rect.Height);
2212 AddExpose (hwnd, hwnd.WholeWindow == hwnd.ClientWindow, 0, 0, hwnd.Width, hwnd.Height);
2214 #endregion // Methods
2217 void MouseHover(object sender, EventArgs e) {
2221 HoverState.Timer.Enabled = false;
2223 if (HoverState.Window != IntPtr.Zero) {
2224 hwnd = Hwnd.GetObjectFromWindow(HoverState.Window);
2226 xevent = new XEvent ();
2228 xevent.type = XEventName.ClientMessage;
2229 xevent.ClientMessageEvent.display = DisplayHandle;
2230 xevent.ClientMessageEvent.window = HoverState.Window;
2231 xevent.ClientMessageEvent.message_type = HoverState.Atom;
2232 xevent.ClientMessageEvent.format = 32;
2233 xevent.ClientMessageEvent.ptr1 = (IntPtr) (HoverState.Y << 16 | HoverState.X);
2235 hwnd.Queue.EnqueueLocked (xevent);
2242 void CaretCallback(object sender, EventArgs e) {
2246 Caret.On = !Caret.On;
2248 XDrawLine(DisplayHandle, Caret.Hwnd, Caret.gc, Caret.X, Caret.Y, Caret.X, Caret.Y + Caret.Height);
2250 #endregion // Callbacks
2252 #region Public Properties
2254 internal override int CaptionHeight {
2260 internal override Size CursorSize {
2265 if (XQueryBestCursor(DisplayHandle, RootWindow, 32, 32, out x, out y) != 0) {
2266 return new Size(x, y);
2268 return new Size(16, 16);
2273 internal override bool DragFullWindows {
2279 internal override Size DragSize {
2281 return new Size(4, 4);
2285 internal override Size FrameBorderSize {
2287 return new Size (4, 4);
2291 internal override Size IconSize {
2297 if (XGetIconSizes(DisplayHandle, RootWindow, out list, out count) != 0) {
2301 current = (long)list;
2304 size = new XIconSize();
2306 for (int i = 0; i < count; i++) {
2307 size = (XIconSize)Marshal.PtrToStructure((IntPtr)current, size.GetType());
2308 current += Marshal.SizeOf(size);
2310 // Look for our preferred size
2311 if (size.min_width == 32) {
2313 return new Size(32, 32);
2316 if (size.max_width == 32) {
2318 return new Size(32, 32);
2321 if (size.min_width < 32 && size.max_width > 32) {
2324 // check if we can fit one
2326 while (x < size.max_width) {
2327 x += size.width_inc;
2330 return new Size(32, 32);
2335 if (largest < size.max_width) {
2336 largest = size.max_width;
2340 // We didn't find a match or we wouldn't be here
2341 return new Size(largest, largest);
2344 return new Size(32, 32);
2349 internal override int KeyboardSpeed {
2352 // A lot harder: need to do:
2353 // XkbQueryExtension(0x08051008, 0xbfffdf4c, 0xbfffdf50, 0xbfffdf54, 0xbfffdf58) = 1
2354 // XkbAllocKeyboard(0x08051008, 0xbfffdf4c, 0xbfffdf50, 0xbfffdf54, 0xbfffdf58) = 0x080517a8
2355 // XkbGetControls(0x08051008, 1, 0x080517a8, 0xbfffdf54, 0xbfffdf58) = 0
2357 // And from that we can tell the repetition rate
2359 // Notice, the values must map to:
2360 // [0, 31] which maps to 2.5 to 30 repetitions per second.
2366 internal override int KeyboardDelay {
2369 // Return values must range from 0 to 4, 0 meaning 250ms,
2370 // and 4 meaning 1000 ms.
2372 return 1; // ie, 500 ms
2376 internal override Size MaxWindowTrackSize {
2378 return new Size (WorkingArea.Width, WorkingArea.Height);
2382 internal override bool MenuAccessKeysUnderlined {
2388 internal override Size MinimizedWindowSpacingSize {
2390 return new Size(1, 1);
2394 internal override Size MinimumWindowSize {
2396 return new Size(110, 22);
2400 internal override Size MinimumFixedToolWindowSize {
2401 get { return new Size (27, 22); }
2404 internal override Size MinimumSizeableToolWindowSize {
2405 get { return new Size (37, 22); }
2408 internal override Size MinimumNoBorderWindowSize {
2409 get { return new Size (2, 2); }
2412 internal override Keys ModifierKeys {
2414 return Keyboard.ModifierKeys;
2418 internal override Size SmallIconSize {
2424 if (XGetIconSizes(DisplayHandle, RootWindow, out list, out count) != 0) {
2428 current = (long)list;
2431 size = new XIconSize();
2433 for (int i = 0; i < count; i++) {
2434 size = (XIconSize)Marshal.PtrToStructure((IntPtr)current, size.GetType());
2435 current += Marshal.SizeOf(size);
2437 // Look for our preferred size
2438 if (size.min_width == 16) {
2440 return new Size(16, 16);
2443 if (size.max_width == 16) {
2445 return new Size(16, 16);
2448 if (size.min_width < 16 && size.max_width > 16) {
2451 // check if we can fit one
2453 while (x < size.max_width) {
2454 x += size.width_inc;
2457 return new Size(16, 16);
2462 if (smallest == 0 || smallest > size.min_width) {
2463 smallest = size.min_width;
2467 // We didn't find a match or we wouldn't be here
2468 return new Size(smallest, smallest);
2471 return new Size(16, 16);
2476 internal override int MouseButtonCount {
2482 internal override bool MouseButtonsSwapped {
2484 return false; // FIXME - how to detect?
2488 internal override Point MousePosition {
2490 return mouse_position;
2494 internal override Size MouseHoverSize {
2496 return new Size (1, 1);
2500 internal override int MouseHoverTime {
2502 return HoverState.Interval;
2508 internal override bool MouseWheelPresent {
2510 return true; // FIXME - how to detect?
2514 internal override MouseButtons MouseButtons {
2520 internal override Rectangle VirtualScreen {
2526 IntPtr prop = IntPtr.Zero;
2530 XGetWindowProperty(DisplayHandle, RootWindow, _NET_DESKTOP_GEOMETRY, IntPtr.Zero, new IntPtr (256), false, (IntPtr)Atom.XA_CARDINAL, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
2531 if ((long)nitems < 2)
2534 width = Marshal.ReadIntPtr(prop, 0).ToInt32();
2535 height = Marshal.ReadIntPtr(prop, IntPtr.Size).ToInt32();
2539 return new Rectangle(0, 0, width, height);
2542 XWindowAttributes attributes=new XWindowAttributes();
2545 XGetWindowAttributes(DisplayHandle, XRootWindow(DisplayHandle, 0), ref attributes);
2548 return new Rectangle(0, 0, attributes.width, attributes.height);
2552 internal override Rectangle WorkingArea {
2558 IntPtr prop = IntPtr.Zero;
2561 int current_desktop;
2565 XGetWindowProperty(DisplayHandle, RootWindow, _NET_CURRENT_DESKTOP, IntPtr.Zero, new IntPtr(1), false, (IntPtr)Atom.XA_CARDINAL, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
2566 if ((long)nitems < 1) {
2570 current_desktop = Marshal.ReadIntPtr(prop, 0).ToInt32();
2573 XGetWindowProperty(DisplayHandle, RootWindow, _NET_WORKAREA, IntPtr.Zero, new IntPtr (256), false, (IntPtr)Atom.XA_CARDINAL, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
2574 if ((long)nitems < 4 * (current_desktop + 1)) {
2578 x = Marshal.ReadIntPtr(prop, IntPtr.Size * 4 * current_desktop).ToInt32();
2579 y = Marshal.ReadIntPtr(prop, IntPtr.Size * 4 * current_desktop + IntPtr.Size).ToInt32();
2580 width = Marshal.ReadIntPtr(prop, IntPtr.Size * 4 * current_desktop + IntPtr.Size * 2).ToInt32();
2581 height = Marshal.ReadIntPtr(prop, IntPtr.Size * 4 * current_desktop + IntPtr.Size * 3).ToInt32();
2584 return new Rectangle(x, y, width, height);
2587 XWindowAttributes attributes=new XWindowAttributes();
2590 XGetWindowAttributes(DisplayHandle, XRootWindow(DisplayHandle, 0), ref attributes);
2593 return new Rectangle(0, 0, attributes.width, attributes.height);
2597 internal override Screen[] AllScreens {
2599 if (!XineramaIsActive (DisplayHandle))
2602 IntPtr xineramaScreens = XineramaQueryScreens (DisplayHandle, out nScreens);
2603 var screens = new Screen [nScreens];
2604 IntPtr current = xineramaScreens;
2605 for (int i = 0; i < nScreens; i++) {
2606 var screen = (XineramaScreenInfo)Marshal.PtrToStructure (current,
2607 typeof (XineramaScreenInfo));
2608 var screenRect = new Rectangle (screen.x_org, screen.y_org, screen.width,
2610 var name = string.Format ("Display {0}", screen.screen_number);
2611 screens [i] = new Screen (i == 0, name, screenRect, screenRect);
2612 current = (IntPtr)( (ulong)current + (ulong)Marshal.SizeOf(typeof (XineramaScreenInfo)));
2614 XFree (xineramaScreens);
2619 internal override bool ThemesEnabled {
2621 return XplatUIX11.themes_enabled;
2626 #endregion // Public properties
2628 #region Public Static Methods
2629 internal override void RaiseIdle (EventArgs e)
2635 internal override IntPtr InitializeDriver()
2638 if (DisplayHandle==IntPtr.Zero) {
2639 SetDisplay(XOpenDisplay(IntPtr.Zero));
2645 internal override void ShutdownDriver(IntPtr token)
2648 if (DisplayHandle!=IntPtr.Zero) {
2649 XCloseDisplay(DisplayHandle);
2650 DisplayHandle=IntPtr.Zero;
2655 internal override void EnableThemes()
2657 themes_enabled = true;
2661 internal override void Activate(IntPtr handle)
2665 hwnd = Hwnd.ObjectFromHandle(handle);
2669 if (true /* the window manager supports NET_ACTIVE_WINDOW */) {
2670 SendNetWMMessage(hwnd.whole_window, _NET_ACTIVE_WINDOW, (IntPtr)1, IntPtr.Zero, IntPtr.Zero);
2671 XEventQueue q = null;
2672 lock (unattached_timer_list) {
2673 foreach (Timer t in unattached_timer_list) {
2675 q= (XEventQueue) MessageQueues [Thread.CurrentThread];
2676 t.thread = q.Thread;
2677 q.timer_list.Add (t);
2679 unattached_timer_list.Clear ();
2683 // XRaiseWindow(DisplayHandle, handle);
2689 internal override void AudibleAlert(AlertType alert)
2691 XBell(DisplayHandle, 0);
2696 internal override void CaretVisible(IntPtr handle, bool visible)
2698 if (Caret.Hwnd == handle) {
2700 if (!Caret.Visible) {
2701 Caret.Visible = true;
2703 Caret.Timer.Start();
2706 Caret.Visible = false;
2713 internal override bool CalculateWindowRect(ref Rectangle ClientRect, CreateParams cp, Menu menu, out Rectangle WindowRect)
2715 WindowRect = Hwnd.GetWindowRectangle (cp, menu, ClientRect);
2719 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y)
2726 hwnd = Hwnd.ObjectFromHandle(handle);
2729 XTranslateCoordinates(DisplayHandle, hwnd.client_window, RootWindow, x, y, out dest_x_return, out dest_y_return, out child);
2736 internal override int[] ClipboardAvailableFormats(IntPtr handle)
2738 DataFormats.Format f;
2741 f = DataFormats.Format.List;
2743 if (XGetSelectionOwner(DisplayHandle, CLIPBOARD) == IntPtr.Zero) {
2747 Clipboard.Formats = new ArrayList();
2750 XConvertSelection(DisplayHandle, CLIPBOARD, (IntPtr)f.Id, (IntPtr)f.Id, FosterParent, IntPtr.Zero);
2752 Clipboard.Enumerating = true;
2753 while (Clipboard.Enumerating) {
2754 UpdateMessageQueue(null, false);
2759 result = new int[Clipboard.Formats.Count];
2761 for (int i = 0; i < Clipboard.Formats.Count; i++) {
2762 result[i] = ((IntPtr)Clipboard.Formats[i]).ToInt32 ();
2765 Clipboard.Formats = null;
2769 internal override void ClipboardClose(IntPtr handle)
2771 if (handle != ClipMagic) {
2772 throw new ArgumentException("handle is not a valid clipboard handle");
2777 internal override int ClipboardGetID(IntPtr handle, string format)
2779 if (handle != ClipMagic) {
2780 throw new ArgumentException("handle is not a valid clipboard handle");
2783 if (format == "Text" ) return (int)Atom.XA_STRING;
2784 else if (format == "Bitmap" ) return (int)Atom.XA_BITMAP;
2785 //else if (format == "MetaFilePict" ) return 3;
2786 //else if (format == "SymbolicLink" ) return 4;
2787 //else if (format == "DataInterchangeFormat" ) return 5;
2788 //else if (format == "Tiff" ) return 6;
2789 else if (format == "OEMText" ) return OEMTEXT.ToInt32();
2790 else if (format == "DeviceIndependentBitmap" ) return (int)Atom.XA_PIXMAP;
2791 else if (format == "Palette" ) return (int)Atom.XA_COLORMAP; // Useless
2792 //else if (format == "PenData" ) return 10;
2793 //else if (format == "RiffAudio" ) return 11;
2794 //else if (format == "WaveAudio" ) return 12;
2795 else if (format == "UnicodeText" ) return UTF16_STRING.ToInt32();
2796 //else if (format == "EnhancedMetafile" ) return 14;
2797 //else if (format == "FileDrop" ) return 15;
2798 //else if (format == "Locale" ) return 16;
2799 else if (format == "Rich Text Format") return RICHTEXTFORMAT.ToInt32 ();
2801 return XInternAtom(DisplayHandle, format, false).ToInt32();
2804 internal override IntPtr ClipboardOpen(bool primary_selection)
2806 if (!primary_selection)
2807 ClipMagic = CLIPBOARD;
2809 ClipMagic = PRIMARY;
2813 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter)
2815 XConvertSelection(DisplayHandle, handle, (IntPtr)type, (IntPtr)type, FosterParent, IntPtr.Zero);
2817 Clipboard.Retrieving = true;
2818 while (Clipboard.Retrieving) {
2819 UpdateMessageQueue(null, false);
2822 return Clipboard.Item;
2825 internal override void ClipboardStore (IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter, bool copy)
2827 Clipboard.Converter = converter;
2830 Clipboard.AddSource (type, obj);
2831 XSetSelectionOwner (DisplayHandle, CLIPBOARD, FosterParent, IntPtr.Zero);
2835 var clipboardAtom = gdk_atom_intern ("CLIPBOARD", true);
2836 var clipboard = gtk_clipboard_get (clipboardAtom);
2837 if (clipboard != null) {
2838 // for now we only store text
2839 var text = Clipboard.GetRtfText ();
2840 if (string.IsNullOrEmpty (text))
2841 text = Clipboard.GetPlainText ();
2842 if (!string.IsNullOrEmpty (text)) {
2843 gtk_clipboard_set_text (clipboard, text, text.Length);
2844 gtk_clipboard_store (clipboard);
2848 // ignore any errors - most likely because gtk isn't installed?
2852 // Clearing the selection
2853 Clipboard.ClearSources ();
2854 XSetSelectionOwner (DisplayHandle, CLIPBOARD, IntPtr.Zero, IntPtr.Zero);
2858 internal override void CreateCaret (IntPtr handle, int width, int height)
2860 XGCValues gc_values;
2863 hwnd = Hwnd.ObjectFromHandle(handle);
2865 if (Caret.Hwnd != IntPtr.Zero) {
2866 DestroyCaret(Caret.Hwnd);
2869 Caret.Hwnd = handle;
2870 Caret.Window = hwnd.client_window;
2871 Caret.Width = width;
2872 Caret.Height = height;
2873 Caret.Visible = false;
2876 gc_values = new XGCValues();
2877 gc_values.line_width = width;
2879 Caret.gc = XCreateGC(DisplayHandle, Caret.Window, new IntPtr ((int)GCFunction.GCLineWidth), ref gc_values);
2880 if (Caret.gc == IntPtr.Zero) {
2881 Caret.Hwnd = IntPtr.Zero;
2885 XSetFunction(DisplayHandle, Caret.gc, GXFunction.GXinvert);
2888 internal override IntPtr CreateWindow (CreateParams cp)
2890 XSetWindowAttributes Attributes;
2892 Hwnd parent_hwnd = null;
2897 IntPtr ParentHandle;
2899 IntPtr ClientWindow;
2900 SetWindowValuemask ValueMask;
2905 Attributes = new XSetWindowAttributes();
2911 if (Width<1) Width=1;
2912 if (Height<1) Height=1;
2914 if (cp.Parent != IntPtr.Zero) {
2915 parent_hwnd = Hwnd.ObjectFromHandle(cp.Parent);
2916 ParentHandle = parent_hwnd.client_window;
2918 if (StyleSet (cp.Style, WindowStyles.WS_CHILD)) {
2919 // We need to use our foster parent window until this poor child gets it's parent assigned
2920 ParentHandle=FosterParent;
2922 ParentHandle=RootWindow;
2926 // Set the default location location for forms.
2928 if (cp.control is Form) {
2929 next = Hwnd.GetNextStackedFormLocation (cp, parent_hwnd);
2933 ValueMask = SetWindowValuemask.BitGravity | SetWindowValuemask.WinGravity;
2935 Attributes.bit_gravity = Gravity.NorthWestGravity;
2936 Attributes.win_gravity = Gravity.NorthWestGravity;
2938 // Save what's under the toolwindow
2939 if (ExStyleSet (cp.ExStyle, WindowExStyles.WS_EX_TOOLWINDOW)) {
2940 Attributes.save_under = true;
2941 ValueMask |= SetWindowValuemask.SaveUnder;
2945 // If we're a popup without caption we override the WM
2946 if (StyleSet (cp.Style, WindowStyles.WS_POPUP) && !StyleSet (cp.Style, WindowStyles.WS_CAPTION)) {
2947 Attributes.override_redirect = true;
2948 ValueMask |= SetWindowValuemask.OverrideRedirect;
2954 hwnd.height = Height;
2955 hwnd.parent = Hwnd.ObjectFromHandle(cp.Parent);
2956 hwnd.initial_style = cp.WindowStyle;
2957 hwnd.initial_ex_style = cp.WindowExStyle;
2959 if (StyleSet (cp.Style, WindowStyles.WS_DISABLED)) {
2960 hwnd.enabled = false;
2963 ClientWindow = IntPtr.Zero;
2965 Size XWindowSize = TranslateWindowSizeToXWindowSize (cp);
2966 Rectangle XClientRect = TranslateClientRectangleToXClientRectangle (hwnd, cp.control);
2969 WholeWindow = XCreateWindow(DisplayHandle, ParentHandle, X, Y, XWindowSize.Width, XWindowSize.Height, 0, (int)CreateWindowArgs.CopyFromParent, (int)CreateWindowArgs.InputOutput, IntPtr.Zero, new UIntPtr ((uint)ValueMask), ref Attributes);
2970 if (WholeWindow != IntPtr.Zero) {
2971 ValueMask &= ~(SetWindowValuemask.OverrideRedirect | SetWindowValuemask.SaveUnder);
2973 if (CustomVisual != IntPtr.Zero && CustomColormap != IntPtr.Zero) {
2974 ValueMask = SetWindowValuemask.ColorMap;
2975 Attributes.colormap = CustomColormap;
2977 ClientWindow = XCreateWindow(DisplayHandle, WholeWindow, XClientRect.X, XClientRect.Y, XClientRect.Width, XClientRect.Height, 0, (int)CreateWindowArgs.CopyFromParent, (int)CreateWindowArgs.InputOutput, CustomVisual, new UIntPtr ((uint)ValueMask), ref Attributes);
2981 if ((WholeWindow == IntPtr.Zero) || (ClientWindow == IntPtr.Zero)) {
2982 throw new Exception("Could not create X11 windows");
2985 hwnd.Queue = ThreadQueue(Thread.CurrentThread);
2986 hwnd.WholeWindow = WholeWindow;
2987 hwnd.ClientWindow = ClientWindow;
2989 DriverDebug("Created window {0:X} / {1:X} parent {2:X}, Style {3}, ExStyle {4}", ClientWindow.ToInt32(), WholeWindow.ToInt32(), hwnd.parent != null ? hwnd.parent.Handle.ToInt32() : 0, (WindowStyles)cp.Style, (WindowExStyles)cp.ExStyle);
2991 if (!StyleSet (cp.Style, WindowStyles.WS_CHILD)) {
2992 if ((X != unchecked((int)0x80000000)) && (Y != unchecked((int)0x80000000))) {
2995 hints = new XSizeHints();
2998 hints.flags = (IntPtr)(XSizeHintsFlags.USPosition | XSizeHintsFlags.PPosition);
2999 XSetWMNormalHints(DisplayHandle, WholeWindow, ref hints);
3004 XSelectInput(DisplayHandle, hwnd.whole_window, new IntPtr ((int)(SelectInputMask | EventMask.StructureNotifyMask | EventMask.PropertyChangeMask | Keyboard.KeyEventMask)));
3005 if (hwnd.whole_window != hwnd.client_window)
3006 XSelectInput(DisplayHandle, hwnd.client_window, new IntPtr ((int)(SelectInputMask | EventMask.StructureNotifyMask | Keyboard.KeyEventMask)));
3009 if (ExStyleSet (cp.ExStyle, WindowExStyles.WS_EX_TOPMOST))
3010 SetTopmost(hwnd.whole_window, true);
3012 SetWMStyles(hwnd, cp);
3014 // set the group leader
3015 XWMHints wm_hints = new XWMHints ();
3017 wm_hints.flags = (IntPtr)(XWMHintsFlags.InputHint | XWMHintsFlags.StateHint | XWMHintsFlags.WindowGroupHint);
3018 wm_hints.input = !StyleSet (cp.Style, WindowStyles.WS_DISABLED);
3019 wm_hints.initial_state = StyleSet (cp.Style, WindowStyles.WS_MINIMIZE) ? XInitialState.IconicState : XInitialState.NormalState;
3021 if (ParentHandle != RootWindow) {
3022 wm_hints.window_group = hwnd.whole_window;
3024 wm_hints.window_group = ParentHandle;
3028 XSetWMHints(DisplayHandle, hwnd.whole_window, ref wm_hints );
3031 if (StyleSet (cp.Style, WindowStyles.WS_MINIMIZE)) {
3032 SetWindowState(hwnd.Handle, FormWindowState.Minimized);
3033 } else if (StyleSet (cp.Style, WindowStyles.WS_MAXIMIZE)) {
3034 SetWindowState(hwnd.Handle, FormWindowState.Maximized);
3037 // for now make all windows dnd enabled
3038 Dnd.SetAllowDrop (hwnd, true);
3040 // Set caption/window title
3041 Text(hwnd.Handle, cp.Caption);
3043 SendMessage (hwnd.Handle, Msg.WM_CREATE, (IntPtr)1, IntPtr.Zero /* XXX unused */);
3044 SendParentNotify (hwnd.Handle, Msg.WM_CREATE, int.MaxValue, int.MaxValue);
3046 if (StyleSet (cp.Style, WindowStyles.WS_VISIBLE)) {
3047 hwnd.visible = true;
3048 MapWindow(hwnd, WindowType.Both);
3049 if (!(Control.FromHandle(hwnd.Handle) is Form))
3050 SendMessage(hwnd.Handle, Msg.WM_SHOWWINDOW, (IntPtr)1, IntPtr.Zero);
3056 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height)
3058 CreateParams create_params = new CreateParams();
3060 create_params.Caption = "";
3061 create_params.X = X;
3062 create_params.Y = Y;
3063 create_params.Width = Width;
3064 create_params.Height = Height;
3066 create_params.ClassName=XplatUI.GetDefaultClassName (GetType ());
3067 create_params.ClassStyle = 0;
3068 create_params.ExStyle=0;
3069 create_params.Parent=IntPtr.Zero;
3070 create_params.Param=0;
3072 return CreateWindow(create_params);
3075 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot)
3078 Bitmap cursor_bitmap;
3086 IntPtr cursor_pixmap;
3093 if (XQueryBestCursor(DisplayHandle, RootWindow, bitmap.Width, bitmap.Height, out width, out height) == 0) {
3097 // Win32 only allows creation cursors of a certain size
3098 if ((bitmap.Width != width) || (bitmap.Width != height)) {
3099 cursor_bitmap = new Bitmap(bitmap, new Size(width, height));
3100 cursor_mask = new Bitmap(mask, new Size(width, height));
3102 cursor_bitmap = bitmap;
3106 width = cursor_bitmap.Width;
3107 height = cursor_bitmap.Height;
3109 cursor_bits = new Byte[(width / 8) * height];
3110 mask_bits = new Byte[(width / 8) * height];
3112 for (int y = 0; y < height; y++) {
3113 for (int x = 0; x < width; x++) {
3114 c_pixel = cursor_bitmap.GetPixel(x, y);
3115 m_pixel = cursor_mask.GetPixel(x, y);
3117 and = c_pixel == cursor_pixel;
3118 xor = m_pixel == mask_pixel;
3122 // cursor_bits[y * width / 8 + x / 8] &= (byte)~((1 << (x % 8))); // The bit already is 0
3123 mask_bits[y * width / 8 + x / 8] |= (byte)(1 << (x % 8));
3124 } else if (and && !xor) {
3126 cursor_bits[y * width / 8 + x / 8] |= (byte)(1 << (x % 8));
3127 mask_bits[y * width / 8 + x / 8] |= (byte)(1 << (x % 8));
3129 } else if (and && !xor) {
3131 } else if (and && xor) {
3134 // X11 doesn't know the 'reverse screen' concept, so we'll treat them the same
3135 // we want both to be 0 so nothing to be done
3136 //cursor_bits[y * width / 8 + x / 8] &= (byte)~((1 << (x % 8)));
3137 //mask_bits[y * width / 8 + x / 8] |= (byte)(01 << (x % 8));
3143 cursor_pixmap = XCreatePixmapFromBitmapData(DisplayHandle, RootWindow, cursor_bits, width, height, (IntPtr)1, (IntPtr)0, 1);
3144 mask_pixmap = XCreatePixmapFromBitmapData(DisplayHandle, RootWindow, mask_bits, width, height, (IntPtr)1, (IntPtr)0, 1);
3148 fg.pixel = XWhitePixel(DisplayHandle, ScreenNo);
3149 fg.red = (ushort)65535;
3150 fg.green = (ushort)65535;
3151 fg.blue = (ushort)65535;
3153 bg.pixel = XBlackPixel(DisplayHandle, ScreenNo);
3155 cursor = XCreatePixmapCursor(DisplayHandle, cursor_pixmap, mask_pixmap, ref fg, ref bg, xHotSpot, yHotSpot);
3157 XFreePixmap(DisplayHandle, cursor_pixmap);
3158 XFreePixmap(DisplayHandle, mask_pixmap);
3163 internal override Bitmap DefineStdCursorBitmap (StdCursor id)
3165 CursorFontShape shape;
3172 shape = StdCursorToFontShape (id);
3173 name = shape.ToString ().Replace ("XC_", string.Empty);
3174 size = XcursorGetDefaultSize (DisplayHandle);
3175 theme = XcursorGetTheme (DisplayHandle);
3176 IntPtr images_ptr = XcursorLibraryLoadImages (name, theme, size);
3177 DriverDebug ("DefineStdCursorBitmap, id={0}, #id={1}, name{2}, size={3}, theme: {4}, images_ptr={5}", id, (int) id, name, size, Marshal.PtrToStringAnsi (theme), images_ptr);
3179 if (images_ptr == IntPtr.Zero) {
3183 XcursorImages images = (XcursorImages) Marshal.PtrToStructure (images_ptr, typeof (XcursorImages));
3184 DriverDebug ("DefineStdCursorBitmap, cursor has {0} images", images.nimage);
3186 if (images.nimage > 0) {
3187 // We only care about the first image.
3188 XcursorImage image = (XcursorImage)Marshal.PtrToStructure (Marshal.ReadIntPtr (images.images), typeof (XcursorImage));
3190 DriverDebug ("DefineStdCursorBitmap, loaded image <size={0}, height={1}, width={2}, xhot={3}, yhot={4}, pixels={5}", image.size, image.height, image.width, image.xhot, image.yhot, image.pixels);
3192 if (image.width <= short.MaxValue && image.height <= short.MaxValue) {
3193 int [] pixels = new int [image.width * image.height];
3194 Marshal.Copy (image.pixels, pixels, 0, pixels.Length);
3195 bmp = new Bitmap (image.width, image.height);
3196 for (int w = 0; w < image.width; w++) {
3197 for (int h = 0; h < image.height; h++) {
3198 bmp.SetPixel (w, h, Color.FromArgb (pixels [h * image.width + w]));
3204 XcursorImagesDestroy (images_ptr);
3206 } catch (DllNotFoundException ex) {
3207 Console.WriteLine ("Could not load libXcursor: " + ex.Message + " (" + ex.GetType ().Name + ")");
3215 internal override IntPtr DefineStdCursor(StdCursor id)
3217 CursorFontShape shape;
3220 shape = StdCursorToFontShape (id);
3223 cursor = XCreateFontCursor(DisplayHandle, shape);
3228 internal static CursorFontShape StdCursorToFontShape (StdCursor id)
3230 CursorFontShape shape;
3231 // FIXME - define missing shapes
3234 case StdCursor.AppStarting: {
3235 shape = CursorFontShape.XC_watch;
3239 case StdCursor.Arrow: {
3240 shape = CursorFontShape.XC_top_left_arrow;
3244 case StdCursor.Cross: {
3245 shape = CursorFontShape.XC_crosshair;
3249 case StdCursor.Default: {
3250 shape = CursorFontShape.XC_top_left_arrow;
3254 case StdCursor.Hand: {
3255 shape = CursorFontShape.XC_hand1;
3259 case StdCursor.Help: {
3260 shape = CursorFontShape.XC_question_arrow;
3264 case StdCursor.HSplit: {
3265 shape = CursorFontShape.XC_sb_v_double_arrow;
3269 case StdCursor.IBeam: {
3270 shape = CursorFontShape.XC_xterm;
3274 case StdCursor.No: {
3275 shape = CursorFontShape.XC_circle;
3279 case StdCursor.NoMove2D: {
3280 shape = CursorFontShape.XC_fleur;
3284 case StdCursor.NoMoveHoriz: {
3285 shape = CursorFontShape.XC_fleur;
3289 case StdCursor.NoMoveVert: {
3290 shape = CursorFontShape.XC_fleur;
3294 case StdCursor.PanEast: {
3295 shape = CursorFontShape.XC_fleur;
3299 case StdCursor.PanNE: {
3300 shape = CursorFontShape.XC_fleur;
3304 case StdCursor.PanNorth: {
3305 shape = CursorFontShape.XC_fleur;
3309 case StdCursor.PanNW: {
3310 shape = CursorFontShape.XC_fleur;
3314 case StdCursor.PanSE: {
3315 shape = CursorFontShape.XC_fleur;
3319 case StdCursor.PanSouth: {
3320 shape = CursorFontShape.XC_fleur;
3324 case StdCursor.PanSW: {
3325 shape = CursorFontShape.XC_fleur;
3329 case StdCursor.PanWest: {
3330 shape = CursorFontShape.XC_sizing;
3334 case StdCursor.SizeAll: {
3335 shape = CursorFontShape.XC_fleur;
3339 case StdCursor.SizeNESW: {
3340 shape = CursorFontShape.XC_top_right_corner;
3344 case StdCursor.SizeNS: {
3345 shape = CursorFontShape.XC_sb_v_double_arrow;
3349 case StdCursor.SizeNWSE: {
3350 shape = CursorFontShape.XC_top_left_corner;
3354 case StdCursor.SizeWE: {
3355 shape = CursorFontShape.XC_sb_h_double_arrow;
3359 case StdCursor.UpArrow: {
3360 shape = CursorFontShape.XC_center_ptr;
3364 case StdCursor.VSplit: {
3365 shape = CursorFontShape.XC_sb_h_double_arrow;
3369 case StdCursor.WaitCursor: {
3370 shape = CursorFontShape.XC_watch;
3375 shape = (CursorFontShape) 0;
3383 internal override IntPtr DefWndProc(ref Message msg)
3385 switch ((Msg)msg.Msg) {
3387 case Msg.WM_IME_COMPOSITION:
3388 string s = Keyboard.GetCompositionString ();
3389 foreach (char c in s)
3390 SendMessage (msg.HWnd, Msg.WM_IME_CHAR, (IntPtr) c, msg.LParam);
3393 case Msg.WM_IME_CHAR:
3394 // On Windows API it sends two WM_CHAR messages for each byte, but
3395 // I wonder if it is worthy to emulate it (also no idea how to
3396 // reconstruct those bytes into chars).
3397 SendMessage (msg.HWnd, Msg.WM_CHAR, msg.WParam, msg.LParam);
3400 case Msg.WM_PAINT: {
3403 hwnd = Hwnd.GetObjectFromWindow(msg.HWnd);
3405 hwnd.expose_pending = false;
3411 case Msg.WM_NCPAINT: {
3414 hwnd = Hwnd.GetObjectFromWindow(msg.HWnd);
3416 hwnd.nc_expose_pending = false;
3422 case Msg.WM_NCCALCSIZE: {
3425 if (msg.WParam == (IntPtr)1) {
3426 hwnd = Hwnd.GetObjectFromWindow (msg.HWnd);
3428 XplatUIWin32.NCCALCSIZE_PARAMS ncp;
3429 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure (msg.LParam, typeof (XplatUIWin32.NCCALCSIZE_PARAMS));
3431 // Add all the stuff X is supposed to draw.
3432 Control ctrl = Control.FromHandle (hwnd.Handle);
3435 Hwnd.Borders rect = Hwnd.GetBorders (ctrl.GetCreateParams (), null);
3437 ncp.rgrc1.top += rect.top;
3438 ncp.rgrc1.bottom -= rect.bottom;
3439 ncp.rgrc1.left += rect.left;
3440 ncp.rgrc1.right -= rect.right;
3442 Marshal.StructureToPtr (ncp, msg.LParam, true);
3449 case Msg.WM_CONTEXTMENU: {
3452 hwnd = Hwnd.GetObjectFromWindow(msg.HWnd);
3454 if ((hwnd != null) && (hwnd.parent != null)) {
3455 SendMessage(hwnd.parent.client_window, Msg.WM_CONTEXTMENU, msg.WParam, msg.LParam);
3460 case Msg.WM_MOUSEWHEEL: {
3463 hwnd = Hwnd.GetObjectFromWindow(msg.HWnd);
3465 if ((hwnd != null) && (hwnd.parent != null)) {
3466 SendMessage(hwnd.parent.client_window, Msg.WM_MOUSEWHEEL, msg.WParam, msg.LParam);
3467 if (msg.Result == IntPtr.Zero) {
3474 case Msg.WM_SETCURSOR: {
3477 hwnd = Hwnd.GetObjectFromWindow(msg.HWnd);
3479 break; // not sure how this happens, but it does
3481 // Pass to parent window first
3482 while ((hwnd.parent != null) && (msg.Result == IntPtr.Zero)) {
3484 msg.Result = NativeWindow.WndProc(hwnd.Handle, Msg.WM_SETCURSOR, msg.HWnd, msg.LParam);
3487 if (msg.Result == IntPtr.Zero) {
3490 switch((HitTest)(msg.LParam.ToInt32() & 0xffff)) {
3491 case HitTest.HTBOTTOM: handle = Cursors.SizeNS.handle; break;
3492 case HitTest.HTBORDER: handle = Cursors.SizeNS.handle; break;
3493 case HitTest.HTBOTTOMLEFT: handle = Cursors.SizeNESW.handle; break;
3494 case HitTest.HTBOTTOMRIGHT: handle = Cursors.SizeNWSE.handle; break;
3495 case HitTest.HTERROR: if ((msg.LParam.ToInt32() >> 16) == (int)Msg.WM_LBUTTONDOWN) {
3496 AudibleAlert(AlertType.Default);
3498 handle = Cursors.Default.handle;
3501 case HitTest.HTHELP: handle = Cursors.Help.handle; break;
3502 case HitTest.HTLEFT: handle = Cursors.SizeWE.handle; break;
3503 case HitTest.HTRIGHT: handle = Cursors.SizeWE.handle; break;
3504 case HitTest.HTTOP: handle = Cursors.SizeNS.handle; break;
3505 case HitTest.HTTOPLEFT: handle = Cursors.SizeNWSE.handle; break;
3506 case HitTest.HTTOPRIGHT: handle = Cursors.SizeNESW.handle; break;
3509 case HitTest.HTGROWBOX:
3510 case HitTest.HTSIZE:
3511 case HitTest.HTZOOM:
3512 case HitTest.HTVSCROLL:
3513 case HitTest.HTSYSMENU:
3514 case HitTest.HTREDUCE:
3515 case HitTest.HTNOWHERE:
3516 case HitTest.HTMAXBUTTON:
3517 case HitTest.HTMINBUTTON:
3518 case HitTest.HTMENU:
3519 case HitTest.HSCROLL:
3520 case HitTest.HTBOTTOM:
3521 case HitTest.HTCAPTION:
3522 case HitTest.HTCLIENT:
3523 case HitTest.HTCLOSE:
3525 default: handle = Cursors.Default.handle; break;
3527 SetCursor(msg.HWnd, handle);
3535 internal override void DestroyCaret(IntPtr handle)
3537 if (Caret.Hwnd == handle) {
3538 if (Caret.Visible) {
3542 if (Caret.gc != IntPtr.Zero) {
3543 XFreeGC(DisplayHandle, Caret.gc);
3544 Caret.gc = IntPtr.Zero;
3546 Caret.Hwnd = IntPtr.Zero;
3547 Caret.Visible = false;
3552 internal override void DestroyCursor(IntPtr cursor)
3555 XFreeCursor(DisplayHandle, cursor);
3559 internal override void DestroyWindow(IntPtr handle)
3562 hwnd = Hwnd.ObjectFromHandle(handle);
3564 // The window should never ever be a zombie here, since we should
3565 // wait until it's completely dead before returning from
3566 // "destroying" calls, but just in case....
3567 if (hwnd == null || hwnd.zombie) {
3568 DriverDebug ("window {0:X} already destroyed", handle.ToInt32());
3572 DriverDebug ("Destroying window {0}", XplatUI.Window(hwnd.client_window));
3574 SendParentNotify (hwnd.Handle, Msg.WM_DESTROY, int.MaxValue, int.MaxValue);
3576 CleanupCachedWindows (hwnd);
3578 ArrayList windows = new ArrayList ();
3580 AccumulateDestroyedHandles (Control.ControlNativeWindow.ControlFromHandle(hwnd.Handle), windows);
3583 foreach (Hwnd h in windows) {
3584 SendMessage (h.Handle, Msg.WM_DESTROY, IntPtr.Zero, IntPtr.Zero);
3589 if (hwnd.whole_window != IntPtr.Zero) {
3590 DriverDebug ("XDestroyWindow (whole_window = {0:X})", hwnd.whole_window.ToInt32());
3591 Keyboard.DestroyICForWindow (hwnd.whole_window);
3592 XDestroyWindow(DisplayHandle, hwnd.whole_window);
3594 else if (hwnd.client_window != IntPtr.Zero) {
3595 DriverDebug ("XDestroyWindow (client_window = {0:X})", hwnd.client_window.ToInt32());
3596 Keyboard.DestroyICForWindow (hwnd.client_window);
3597 XDestroyWindow(DisplayHandle, hwnd.client_window);
3603 internal override IntPtr DispatchMessage(ref MSG msg)
3605 return NativeWindow.WndProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);
3608 IntPtr GetReversibleScreenGC (Color backColor)
3610 XGCValues gc_values;
3614 XColor xcolor = new XColor();
3615 xcolor.red = (ushort)(backColor.R * 257);
3616 xcolor.green = (ushort)(backColor.G * 257);
3617 xcolor.blue = (ushort)(backColor.B * 257);
3618 XAllocColor(DisplayHandle, DefaultColormap, ref xcolor);
3619 pixel = (uint)xcolor.pixel.ToInt32();
3622 gc_values = new XGCValues();
3624 gc_values.subwindow_mode = GCSubwindowMode.IncludeInferiors;
3625 gc_values.foreground = (IntPtr)pixel;
3627 gc = XCreateGC(DisplayHandle, RootWindow, new IntPtr ((int) (GCFunction.GCSubwindowMode | GCFunction.GCForeground)), ref gc_values);
3628 XSetForeground(DisplayHandle, gc, (UIntPtr)pixel);
3629 XSetFunction(DisplayHandle, gc, GXFunction.GXxor);
3634 IntPtr GetReversibleControlGC (Control control, int line_width)
3636 XGCValues gc_values;
3639 gc_values = new XGCValues();
3641 gc_values.subwindow_mode = GCSubwindowMode.IncludeInferiors;
3642 gc_values.line_width = line_width;
3643 gc_values.foreground = XBlackPixel(DisplayHandle, ScreenNo);
3645 // This logic will give us true rubber bands: (libsx, SANE_XOR)
3646 //mask = foreground ^ background;
3647 //XSetForeground(DisplayHandle, gc, 0xffffffff);
3648 //XSetBackground(DisplayHandle, gc, background);
3649 //XSetFunction(DisplayHandle, gc, GXxor);
3650 //XSetPlaneMask(DisplayHandle, gc, mask);
3653 gc = XCreateGC(DisplayHandle, control.Handle, new IntPtr ((int) (GCFunction.GCSubwindowMode | GCFunction.GCLineWidth | GCFunction.GCForeground)), ref gc_values);
3657 XColor xcolor = new XColor();
3659 xcolor.red = (ushort)(control.ForeColor.R * 257);
3660 xcolor.green = (ushort)(control.ForeColor.G * 257);
3661 xcolor.blue = (ushort)(control.ForeColor.B * 257);
3662 XAllocColor(DisplayHandle, DefaultColormap, ref xcolor);
3663 foreground = (uint)xcolor.pixel.ToInt32();
3665 xcolor.red = (ushort)(control.BackColor.R * 257);
3666 xcolor.green = (ushort)(control.BackColor.G * 257);
3667 xcolor.blue = (ushort)(control.BackColor.B * 257);
3668 XAllocColor(DisplayHandle, DefaultColormap, ref xcolor);
3669 background = (uint)xcolor.pixel.ToInt32();
3671 uint mask = foreground ^ background;
3673 XSetForeground(DisplayHandle, gc, (UIntPtr)0xffffffff);
3674 XSetBackground(DisplayHandle, gc, (UIntPtr)background);
3675 XSetFunction(DisplayHandle, gc, GXFunction.GXxor);
3676 XSetPlaneMask(DisplayHandle, gc, (IntPtr)mask);
3681 internal override void DrawReversibleLine(Point start, Point end, Color backColor)
3683 if (backColor.GetBrightness() < 0.5)
3684 backColor = Color.FromArgb(255 - backColor.R, 255 - backColor.G, 255 - backColor.B);
3686 IntPtr gc = GetReversibleScreenGC (backColor);
3688 XDrawLine (DisplayHandle, RootWindow, gc, start.X, start.Y, end.X, end.Y);
3690 XFreeGC(DisplayHandle, gc);
3693 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style)
3695 if (backColor.GetBrightness() < 0.5)
3696 backColor = Color.FromArgb(255 - backColor.R, 255 - backColor.G, 255 - backColor.B);
3698 IntPtr gc = GetReversibleScreenGC (backColor);
3700 if (rectangle.Width < 0) {
3701 rectangle.X += rectangle.Width;
3702 rectangle.Width = -rectangle.Width;
3704 if (rectangle.Height < 0) {
3705 rectangle.Y += rectangle.Height;
3706 rectangle.Height = -rectangle.Height;
3710 GCLineStyle line_style = GCLineStyle.LineSolid;
3711 GCCapStyle cap_style = GCCapStyle.CapButt;
3712 GCJoinStyle join_style = GCJoinStyle.JoinMiter;
3715 case FrameStyle.Dashed:
3716 line_style = GCLineStyle.LineOnOffDash;
3718 case FrameStyle.Thick:
3723 XSetLineAttributes (DisplayHandle, gc, line_width, line_style, cap_style, join_style);
3725 XDrawRectangle(DisplayHandle, RootWindow, gc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
3727 XFreeGC(DisplayHandle, gc);
3730 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor)
3732 if (backColor.GetBrightness() < 0.5)
3733 backColor = Color.FromArgb(255 - backColor.R, 255 - backColor.G, 255 - backColor.B);
3735 IntPtr gc = GetReversibleScreenGC (backColor);
3737 if (rectangle.Width < 0) {
3738 rectangle.X += rectangle.Width;
3739 rectangle.Width = -rectangle.Width;
3741 if (rectangle.Height < 0) {
3742 rectangle.Y += rectangle.Height;
3743 rectangle.Height = -rectangle.Height;
3745 XFillRectangle(DisplayHandle, RootWindow, gc, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
3747 XFreeGC(DisplayHandle, gc);
3750 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width)
3753 Control control = Control.FromHandle(handle);
3755 gc = GetReversibleControlGC (control, line_width);
3757 if ((rect.Width > 0) && (rect.Height > 0)) {
3758 XDrawRectangle(DisplayHandle, control.Handle, gc, rect.Left, rect.Top, rect.Width, rect.Height);
3760 if (rect.Width > 0) {
3761 XDrawLine(DisplayHandle, control.Handle, gc, rect.X, rect.Y, rect.Right, rect.Y);
3763 XDrawLine(DisplayHandle, control.Handle, gc, rect.X, rect.Y, rect.X, rect.Bottom);
3766 XFreeGC(DisplayHandle, gc);
3769 internal override void DoEvents()
3771 DebugHelper.Enter ();
3773 MSG msg = new MSG ();
3776 if (OverrideCursorHandle != IntPtr.Zero) {
3777 OverrideCursorHandle = IntPtr.Zero;
3780 queue = ThreadQueue(Thread.CurrentThread);
3782 queue.DispatchIdle = false;
3785 while (PeekMessage(queue, ref msg, IntPtr.Zero, 0, 0, (uint)PeekMessageFlags.PM_REMOVE)) {
3786 Message m = Message.Create (msg.hwnd, (int)msg.message, msg.wParam, msg.lParam);
3788 if (Application.FilterMessage (ref m))
3791 TranslateMessage (ref msg);
3792 DispatchMessage (ref msg);
3794 string key = msg.hwnd + ":" + msg.message;
3795 if (messageHold[key] != null) {
3796 messageHold[key] = ((int)messageHold[key]) - 1;
3797 DebugHelper.WriteLine ("Got " + msg + " for " + key);
3801 in_doevents = false;
3802 queue.DispatchIdle = true;
3804 DebugHelper.Leave ();
3807 internal override void EnableWindow(IntPtr handle, bool Enable)
3811 hwnd = Hwnd.ObjectFromHandle(handle);
3813 hwnd.Enabled = Enable;
3817 internal override void EndLoop(Thread thread)
3819 // This is where we one day will shut down the loop for the thread
3822 internal override IntPtr GetActive()
3828 IntPtr prop = IntPtr.Zero;
3829 IntPtr active = IntPtr.Zero;
3831 XGetWindowProperty(DisplayHandle, RootWindow, _NET_ACTIVE_WINDOW, IntPtr.Zero, new IntPtr (1), false, (IntPtr)Atom.XA_WINDOW, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
3832 if (((long)nitems > 0) && (prop != IntPtr.Zero)) {
3833 active = (IntPtr)Marshal.ReadInt32(prop);
3836 // The window manager does not support _NET_ACTIVE_WINDOW. Fall back to XGetInputFocus.
3837 IntPtr revert_to = IntPtr.Zero;
3838 XGetInputFocus(DisplayHandle, out active, out revert_to);
3841 if (active != IntPtr.Zero) {
3844 hwnd = Hwnd.GetObjectFromWindow(active);
3846 active = hwnd.Handle;
3848 active = IntPtr.Zero;
3854 internal override Region GetClipRegion(IntPtr handle)
3858 hwnd = Hwnd.ObjectFromHandle(handle);
3860 return hwnd.UserClip;
3866 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y)
3874 internal override void GetDisplaySize(out Size size)
3876 XWindowAttributes attributes=new XWindowAttributes();
3879 // FIXME - use _NET_WM messages instead?
3880 XGetWindowAttributes(DisplayHandle, XRootWindow(DisplayHandle, 0), ref attributes);
3883 size = new Size(attributes.width, attributes.height);
3886 internal override SizeF GetAutoScaleSize(Font font)
3890 string magic_string = "The quick brown fox jumped over the lazy dog.";
3891 double magic_number = 44.549996948242189;
3893 g = Graphics.FromHwnd(FosterParent);
3895 width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
3896 return new SizeF(width, font.Height);
3899 internal override IntPtr GetParent(IntPtr handle)
3903 hwnd = Hwnd.ObjectFromHandle(handle);
3904 if (hwnd != null && hwnd.parent != null) {
3905 return hwnd.parent.Handle;
3910 // This is a nop on win32 and x11
3911 internal override IntPtr GetPreviousWindow(IntPtr handle)
3916 internal override void GetCursorPos(IntPtr handle, out int x, out int y)
3927 if (handle != IntPtr.Zero) {
3928 use_handle = Hwnd.ObjectFromHandle(handle).client_window;
3930 use_handle = RootWindow;
3934 QueryPointer (DisplayHandle, use_handle, out root, out child, out root_x, out root_y, out win_x, out win_y, out keys_buttons);
3937 if (handle != IntPtr.Zero) {
3946 internal override IntPtr GetFocus()
3952 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent)
3954 FontFamily ff = font.FontFamily;
3955 ascent = ff.GetCellAscent (font.Style);
3956 descent = ff.GetCellDescent (font.Style);
3960 internal override Point GetMenuOrigin(IntPtr handle)
3964 hwnd = Hwnd.ObjectFromHandle(handle);
3967 return hwnd.MenuOrigin;
3972 [MonoTODO("Implement filtering")]
3973 internal override bool GetMessage(Object queue_id, ref MSG msg, IntPtr handle, int wFilterMin, int wFilterMax)
3981 if (((XEventQueue)queue_id).Count > 0) {
3982 xevent = (XEvent) ((XEventQueue)queue_id).Dequeue ();
3984 UpdateMessageQueue ((XEventQueue)queue_id);
3986 if (((XEventQueue)queue_id).Count > 0) {
3987 xevent = (XEvent) ((XEventQueue)queue_id).Dequeue ();
3988 } else if (((XEventQueue)queue_id).Paint.Count > 0) {
3989 xevent = ((XEventQueue)queue_id).Paint.Dequeue();
3991 msg.hwnd= IntPtr.Zero;
3992 msg.message = Msg.WM_ENTERIDLE;
3997 hwnd = Hwnd.GetObjectFromWindow(xevent.AnyEvent.window);
3999 #if DriverDebugDestroy
4002 Console.WriteLine ( "GetMessage zombie, got Event: " + xevent.ToString () + " for 0x{0:x}", hwnd.Handle.ToInt32());
4004 Console.WriteLine ( "GetMessage, got Event: " + xevent.ToString () + " for 0x{0:x}", hwnd.Handle.ToInt32());
4006 // Handle messages for windows that are already or are about to be destroyed.
4008 // we need a special block for this because unless we remove the hwnd from the paint
4009 // queue it will always stay there (since we don't handle the expose), and we'll
4010 // effectively loop infinitely trying to repaint a non-existant window.
4011 if (hwnd != null && hwnd.zombie && xevent.type == XEventName.Expose) {
4012 hwnd.expose_pending = hwnd.nc_expose_pending = false;
4013 hwnd.Queue.Paint.Remove (hwnd);
4014 goto ProcessNextMessage;
4017 // We need to make sure we only allow DestroyNotify events through for zombie
4018 // hwnds, since much of the event handling code makes requests using the hwnd's
4019 // client_window, and that'll result in BadWindow errors if there's some lag
4020 // between the XDestroyWindow call and the DestroyNotify event.
4021 if (hwnd == null || hwnd.zombie && xevent.AnyEvent.type != XEventName.ClientMessage) {
4022 DriverDebug("GetMessage(): Got message {0} for non-existent or already destroyed window {1:X}", xevent.type, xevent.AnyEvent.window.ToInt32());
4023 goto ProcessNextMessage;
4027 // If we get here, that means the window is no more but there are Client Messages
4028 // to be processed, probably a Posted message (for instance, an WM_ACTIVATE message)
4029 // We don't want anything else to run but the ClientMessage block, so reset all hwnd
4030 // properties that might cause other processing to occur.
4032 hwnd.resizing_or_moving = false;
4035 if (hwnd.client_window == xevent.AnyEvent.window) {
4037 //Console.WriteLine("Client message {1}, sending to window {0:X}", msg.hwnd.ToInt32(), xevent.type);
4040 //Console.WriteLine("Non-Client message, sending to window {0:X}", msg.hwnd.ToInt32());
4043 msg.hwnd = hwnd.Handle;
4045 // Windows sends WM_ENTERSIZEMOVE when a form resize/move operation starts and WM_EXITSIZEMOVE
4046 // when it is done. The problem in X11 is that there is no concept of start-end of a moving/sizing.
4047 // Configure events ("this window has resized/moved") are sent for each step of the resize. We send a
4048 // WM_ENTERSIZEMOVE when we get the first Configure event. The problem is the WM_EXITSIZEMOVE.
4050 // - There is no way for us to know which is the last Configure event. We can't traverse the events
4051 // queue, because the next configure event might not be pending yet.
4052 // - We can't get ButtonPress/Release events for the window decorations, because they are not part
4053 // of the window(s) we manage.
4054 // - We can't rely on the mouse state to change to "up" before the last Configure event. It doesn't.
4056 // We are almost 100% guaranteed to get another event (e.g Expose or other), but we can't know for sure
4057 // which, so we have here to check if the mouse buttons state is "up" and send the WM_EXITSIZEMOVE
4059 if (hwnd.resizing_or_moving) {
4060 int root_x, root_y, win_x, win_y, keys_buttons;
4062 XQueryPointer (DisplayHandle, hwnd.Handle, out root, out child, out root_x, out root_y,
4063 out win_x, out win_y, out keys_buttons);
4064 if ((keys_buttons & (int)MouseKeyMasks.Button1Mask) == 0 &&
4065 (keys_buttons & (int)MouseKeyMasks.Button2Mask) == 0 &&
4066 (keys_buttons & (int)MouseKeyMasks.Button3Mask) == 0) {
4067 hwnd.resizing_or_moving = false;
4068 SendMessage (hwnd.Handle, Msg.WM_EXITSIZEMOVE, IntPtr.Zero, IntPtr.Zero);
4073 // If you add a new event to this switch make sure to add it in
4074 // UpdateMessage also unless it is not coming through the X event system.
4076 switch(xevent.type) {
4077 case XEventName.KeyPress: {
4078 Keyboard.KeyEvent (FocusWindow, xevent, ref msg);
4080 // F1 key special case - WM_HELP sending
4081 if (msg.wParam == (IntPtr)VirtualKeys.VK_F1 || msg.wParam == (IntPtr)VirtualKeys.VK_HELP) {
4082 // Send wM_HELP and then return it as a keypress message in
4083 // case it needs to be preproccessed.
4084 HELPINFO helpInfo = new HELPINFO ();
4085 GetCursorPos (IntPtr.Zero, out helpInfo.MousePos.x, out helpInfo.MousePos.y);
4086 IntPtr helpInfoPtr = Marshal.AllocHGlobal (Marshal.SizeOf (helpInfo));
4087 Marshal.StructureToPtr (helpInfo, helpInfoPtr, true);
4088 NativeWindow.WndProc (FocusWindow, Msg.WM_HELP, IntPtr.Zero, helpInfoPtr);
4089 Marshal.FreeHGlobal (helpInfoPtr);
4094 case XEventName.KeyRelease: {
4095 Keyboard.KeyEvent (FocusWindow, xevent, ref msg);
4099 case XEventName.ButtonPress: {
4100 switch(xevent.ButtonEvent.button) {
4102 MouseState |= MouseButtons.Left;
4104 msg.message = Msg.WM_LBUTTONDOWN;
4105 msg.wParam = GetMousewParam (0);
4107 msg.message = Msg.WM_NCLBUTTONDOWN;
4108 msg.wParam = (IntPtr) NCHitTest (hwnd, xevent.MotionEvent.x, xevent.MotionEvent.y);
4109 MenuToScreen (xevent.AnyEvent.window, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
4115 MouseState |= MouseButtons.Middle;
4117 msg.message = Msg.WM_MBUTTONDOWN;
4118 msg.wParam = GetMousewParam (0);
4120 msg.message = Msg.WM_NCMBUTTONDOWN;
4121 msg.wParam = (IntPtr) NCHitTest (hwnd, xevent.MotionEvent.x, xevent.MotionEvent.y);
4122 MenuToScreen (xevent.AnyEvent.window, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
4128 MouseState |= MouseButtons.Right;
4130 msg.message = Msg.WM_RBUTTONDOWN;
4131 msg.wParam = GetMousewParam (0);
4133 msg.message = Msg.WM_NCRBUTTONDOWN;
4134 msg.wParam = (IntPtr) NCHitTest (hwnd, xevent.MotionEvent.x, xevent.MotionEvent.y);
4135 MenuToScreen (xevent.AnyEvent.window, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
4141 msg.hwnd = FocusWindow;
4142 msg.message=Msg.WM_MOUSEWHEEL;
4143 msg.wParam=GetMousewParam(120);
4148 msg.hwnd = FocusWindow;
4149 msg.message=Msg.WM_MOUSEWHEEL;
4150 msg.wParam=GetMousewParam(-120);
4156 msg.lParam=(IntPtr) (xevent.ButtonEvent.y << 16 | xevent.ButtonEvent.x);
4157 mouse_position.X = xevent.ButtonEvent.x;
4158 mouse_position.Y = xevent.ButtonEvent.y;
4160 if (!hwnd.Enabled) {
4163 msg.hwnd = hwnd.EnabledHwnd;
4164 XTranslateCoordinates(DisplayHandle, xevent.AnyEvent.window, Hwnd.ObjectFromHandle(msg.hwnd).ClientWindow, xevent.ButtonEvent.x, xevent.ButtonEvent.y, out xevent.ButtonEvent.x, out xevent.ButtonEvent.y, out dummy);
4165 msg.lParam = (IntPtr)(mouse_position.Y << 16 | mouse_position.X);
4168 if (Grab.Hwnd != IntPtr.Zero) {
4169 msg.hwnd = Grab.Hwnd;
4172 if (ClickPending.Pending && ((((long)xevent.ButtonEvent.time - ClickPending.Time) < DoubleClickInterval) && (msg.wParam == ClickPending.wParam) && (msg.lParam == ClickPending.lParam) && (msg.message == ClickPending.Message))) {
4173 // Looks like a genuine double click, clicked twice on the same spot with the same keys
4174 switch(xevent.ButtonEvent.button) {
4176 msg.message = client ? Msg.WM_LBUTTONDBLCLK : Msg.WM_NCLBUTTONDBLCLK;
4181 msg.message = client ? Msg.WM_MBUTTONDBLCLK : Msg.WM_NCMBUTTONDBLCLK;
4186 msg.message = client ? Msg.WM_RBUTTONDBLCLK : Msg.WM_NCRBUTTONDBLCLK;
4190 ClickPending.Pending = false;
4192 ClickPending.Pending = true;
4193 ClickPending.Hwnd = msg.hwnd;
4194 ClickPending.Message = msg.message;
4195 ClickPending.wParam = msg.wParam;
4196 ClickPending.lParam = msg.lParam;
4197 ClickPending.Time = (long)xevent.ButtonEvent.time;
4200 if (msg.message == Msg.WM_LBUTTONDOWN || msg.message == Msg.WM_MBUTTONDOWN || msg.message == Msg.WM_RBUTTONDOWN) {
4201 SendParentNotify(msg.hwnd, msg.message, mouse_position.X, mouse_position.Y);
4207 case XEventName.ButtonRelease: {
4208 switch(xevent.ButtonEvent.button) {
4211 msg.message = Msg.WM_LBUTTONUP;
4213 msg.message = Msg.WM_NCLBUTTONUP;
4214 msg.wParam = (IntPtr) NCHitTest (hwnd, xevent.MotionEvent.x, xevent.MotionEvent.y);
4215 MenuToScreen (xevent.AnyEvent.window, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
4217 MouseState &= ~MouseButtons.Left;
4218 msg.wParam = GetMousewParam (0);
4224 msg.message = Msg.WM_MBUTTONUP;
4226 msg.message = Msg.WM_NCMBUTTONUP;
4227 msg.wParam = (IntPtr) NCHitTest (hwnd, xevent.MotionEvent.x, xevent.MotionEvent.y);
4228 MenuToScreen (xevent.AnyEvent.window, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
4230 MouseState &= ~MouseButtons.Middle;
4231 msg.wParam = GetMousewParam (0);
4237 msg.message = Msg.WM_RBUTTONUP;
4239 msg.message = Msg.WM_NCRBUTTONUP;
4240 msg.wParam = (IntPtr) NCHitTest (hwnd, xevent.MotionEvent.x, xevent.MotionEvent.y);
4241 MenuToScreen (xevent.AnyEvent.window, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
4243 MouseState &= ~MouseButtons.Right;
4244 msg.wParam = GetMousewParam (0);
4249 goto ProcessNextMessage;
4253 goto ProcessNextMessage;
4257 if (!hwnd.Enabled) {
4260 msg.hwnd = hwnd.EnabledHwnd;
4261 XTranslateCoordinates(DisplayHandle, xevent.AnyEvent.window, Hwnd.ObjectFromHandle(msg.hwnd).ClientWindow, xevent.ButtonEvent.x, xevent.ButtonEvent.y, out xevent.ButtonEvent.x, out xevent.ButtonEvent.y, out dummy);
4262 msg.lParam = (IntPtr)(mouse_position.Y << 16 | mouse_position.X);
4265 if (Grab.Hwnd != IntPtr.Zero) {
4266 msg.hwnd = Grab.Hwnd;
4269 msg.lParam=(IntPtr) (xevent.ButtonEvent.y << 16 | xevent.ButtonEvent.x);
4270 mouse_position.X = xevent.ButtonEvent.x;
4271 mouse_position.Y = xevent.ButtonEvent.y;
4273 // Win32 splurts MouseMove events all over the place, regardless of whether the mouse is actually moving or
4274 // not, especially after mousedown and mouseup. To support apps relying on mousemove events between and after
4275 // mouse clicks to repaint or whatever, we generate a mousemove event here. *sigh*
4276 if (msg.message == Msg.WM_LBUTTONUP || msg.message == Msg.WM_MBUTTONUP || msg.message == Msg.WM_RBUTTONUP) {
4277 XEvent motionEvent = new XEvent ();
4278 motionEvent.type = XEventName.MotionNotify;
4279 motionEvent.MotionEvent.display = DisplayHandle;
4280 motionEvent.MotionEvent.window = xevent.ButtonEvent.window;
4281 motionEvent.MotionEvent.x = xevent.ButtonEvent.x;
4282 motionEvent.MotionEvent.y = xevent.ButtonEvent.y;
4283 hwnd.Queue.EnqueueLocked (motionEvent);
4288 case XEventName.MotionNotify: {
4290 DriverDebug("GetMessage(): Window {0:X} MotionNotify x={1} y={2}",
4291 client ? hwnd.client_window.ToInt32() : hwnd.whole_window.ToInt32(),
4292 xevent.MotionEvent.x, xevent.MotionEvent.y);
4294 if (Grab.Hwnd != IntPtr.Zero) {
4295 msg.hwnd = Grab.Hwnd;
4298 NativeWindow.WndProc(msg.hwnd, Msg.WM_SETCURSOR, msg.hwnd, (IntPtr)HitTest.HTCLIENT);
4302 if (xevent.MotionEvent.is_hint != 0)
4306 XQueryPointer (DisplayHandle, xevent.AnyEvent.window,
4307 out root, out child,
4308 out xevent.MotionEvent.x_root,
4309 out xevent.MotionEvent.y_root,
4310 out xevent.MotionEvent.x,
4311 out xevent.MotionEvent.y, out mask);
4314 msg.message = Msg.WM_MOUSEMOVE;
4315 msg.wParam = GetMousewParam(0);
4316 msg.lParam = (IntPtr) (xevent.MotionEvent.y << 16 | xevent.MotionEvent.x & 0xFFFF);
4318 if (!hwnd.Enabled) {
4321 msg.hwnd = hwnd.EnabledHwnd;
4322 XTranslateCoordinates(DisplayHandle, xevent.AnyEvent.window, Hwnd.ObjectFromHandle(msg.hwnd).ClientWindow, xevent.MotionEvent.x, xevent.MotionEvent.y, out xevent.MotionEvent.x, out xevent.MotionEvent.y, out dummy);
4323 msg.lParam = (IntPtr)(mouse_position.Y << 16 | mouse_position.X);
4326 mouse_position.X = xevent.MotionEvent.x;
4327 mouse_position.Y = xevent.MotionEvent.y;
4329 if ((HoverState.Timer.Enabled) &&
4330 (((mouse_position.X + HoverState.Size.Width) < HoverState.X) ||
4331 ((mouse_position.X - HoverState.Size.Width) > HoverState.X) ||
4332 ((mouse_position.Y + HoverState.Size.Height) < HoverState.Y) ||
4333 ((mouse_position.Y - HoverState.Size.Height) > HoverState.Y))) {
4334 HoverState.Timer.Stop();
4335 HoverState.Timer.Start();
4336 HoverState.X = mouse_position.X;
4337 HoverState.Y = mouse_position.Y;
4345 DriverDebug("GetMessage(): non-client area {0:X} MotionNotify x={1} y={2}",
4346 client ? hwnd.client_window.ToInt32() : hwnd.whole_window.ToInt32(),
4347 xevent.MotionEvent.x, xevent.MotionEvent.y);
4348 msg.message = Msg.WM_NCMOUSEMOVE;
4350 if (!hwnd.Enabled) {
4351 msg.hwnd = hwnd.EnabledHwnd;
4352 XTranslateCoordinates(DisplayHandle, xevent.AnyEvent.window, Hwnd.ObjectFromHandle(msg.hwnd).ClientWindow, xevent.MotionEvent.x, xevent.MotionEvent.y, out xevent.MotionEvent.x, out xevent.MotionEvent.y, out dummy);
4353 msg.lParam = (IntPtr)(mouse_position.Y << 16 | mouse_position.X);
4356 ht = NCHitTest (hwnd, xevent.MotionEvent.x, xevent.MotionEvent.y);
4357 NativeWindow.WndProc(hwnd.client_window, Msg.WM_SETCURSOR, msg.hwnd, (IntPtr)ht);
4359 mouse_position.X = xevent.MotionEvent.x;
4360 mouse_position.Y = xevent.MotionEvent.y;
4366 case XEventName.EnterNotify: {
4367 if (!hwnd.Enabled) {
4368 goto ProcessNextMessage;
4370 if (xevent.CrossingEvent.mode == NotifyMode.NotifyGrab || xevent.AnyEvent.window != hwnd.client_window) {
4371 goto ProcessNextMessage;
4373 if (xevent.CrossingEvent.mode == NotifyMode.NotifyUngrab) { // Pseudo motion caused by grabbing
4374 if (LastPointerWindow == xevent.AnyEvent.window)
4375 goto ProcessNextMessage;
4377 if (LastPointerWindow != IntPtr.Zero) {
4378 Point enter_loc = new Point (xevent.ButtonEvent.x, xevent.ButtonEvent.y);
4380 // We need this due to EnterNotify being fired on all the parent controls
4381 // of the Control being grabbed, and obviously in that scenario we are not
4382 // actuallty entering them
4383 Control ctrl = Control.FromHandle (hwnd.client_window);
4384 foreach (Control child_control in ctrl.Controls.GetAllControls ())
4385 if (child_control.Bounds.Contains (enter_loc))
4386 goto ProcessNextMessage;
4388 // A MouseLeave/LeaveNotify event is sent to the previous window
4389 // until the mouse is ungrabbed, not when actually leaving its bounds
4390 int x = xevent.CrossingEvent.x_root;
4391 int y = xevent.CrossingEvent.y_root;
4392 ScreenToClient (LastPointerWindow, ref x, ref y);
4394 XEvent leaveEvent = new XEvent ();
4395 leaveEvent.type = XEventName.LeaveNotify;
4396 leaveEvent.CrossingEvent.display = DisplayHandle;
4397 leaveEvent.CrossingEvent.window = LastPointerWindow;
4398 leaveEvent.CrossingEvent.x = x;
4399 leaveEvent.CrossingEvent.y = y;
4400 leaveEvent.CrossingEvent.mode = NotifyMode.NotifyNormal;
4401 Hwnd last_pointer_hwnd = Hwnd.ObjectFromHandle (LastPointerWindow);
4402 last_pointer_hwnd.Queue.EnqueueLocked (leaveEvent);
4406 LastPointerWindow = xevent.AnyEvent.window;
4408 msg.message = Msg.WM_MOUSE_ENTER;
4409 HoverState.X = xevent.CrossingEvent.x;
4410 HoverState.Y = xevent.CrossingEvent.y;
4411 HoverState.Timer.Enabled = true;
4412 HoverState.Window = xevent.CrossingEvent.window;
4414 // Win32 sends a WM_MOUSEMOVE after mouse enter
4415 XEvent motionEvent = new XEvent ();
4416 motionEvent.type = XEventName.MotionNotify;
4417 motionEvent.MotionEvent.display = DisplayHandle;
4418 motionEvent.MotionEvent.window = xevent.ButtonEvent.window;
4419 motionEvent.MotionEvent.x = xevent.ButtonEvent.x;
4420 motionEvent.MotionEvent.y = xevent.ButtonEvent.y;
4421 hwnd.Queue.EnqueueLocked (motionEvent);
4425 case XEventName.LeaveNotify: {
4426 if (xevent.CrossingEvent.mode == NotifyMode.NotifyUngrab) {
4427 WindowUngrabbed (hwnd.Handle);
4428 goto ProcessNextMessage;
4430 if (!hwnd.Enabled) {
4431 goto ProcessNextMessage;
4433 if ((xevent.CrossingEvent.mode != NotifyMode.NotifyNormal) || (xevent.CrossingEvent.window != hwnd.client_window)) {
4434 goto ProcessNextMessage;
4436 // If a grab is taking place, ignore it - we handle it in EnterNotify
4437 if (Grab.Hwnd != IntPtr.Zero)
4438 goto ProcessNextMessage;
4440 // Reset the cursor explicitly on X11.
4441 // X11 remembers the last set cursor for the window and in cases where
4442 // the control won't get a WM_SETCURSOR X11 will restore the last
4443 // known cursor, which we don't want.
4445 SetCursor (hwnd.client_window, IntPtr.Zero);
4447 msg.message=Msg.WM_MOUSELEAVE;
4448 HoverState.Timer.Enabled = false;
4449 HoverState.Window = IntPtr.Zero;
4454 case XEventName.CreateNotify: {
4455 if (client && (xevent.ConfigureEvent.xevent == xevent.ConfigureEvent.window)) {
4456 msg.message = WM_CREATE;
4457 // Set up CreateStruct
4459 goto ProcessNextMessage;
4466 case XEventName.ReparentNotify: {
4467 if (hwnd.parent == null) { // Toplevel
4468 if ((xevent.ReparentEvent.parent != IntPtr.Zero) && (xevent.ReparentEvent.window == hwnd.whole_window)) {
4469 hwnd.Reparented = true;
4471 // The location given by the event is not reliable between different wm's,
4472 // so use an alternative way of getting it.
4473 Point location = GetTopLevelWindowLocation (hwnd);
4474 hwnd.X = location.X;
4475 hwnd.Y = location.Y;
4477 if (hwnd.opacity != 0xffffffff) {
4480 opacity = (IntPtr)(Int32)hwnd.opacity;
4481 XChangeProperty(DisplayHandle, XGetParent(hwnd.whole_window), _NET_WM_WINDOW_OPACITY, (IntPtr)Atom.XA_CARDINAL, 32, PropertyMode.Replace, ref opacity, 1);
4483 SendMessage(msg.hwnd, Msg.WM_WINDOWPOSCHANGED, msg.wParam, msg.lParam);
4484 goto ProcessNextMessage;
4486 hwnd.Reparented = false;
4487 goto ProcessNextMessage;
4490 goto ProcessNextMessage;
4493 case XEventName.ConfigureNotify: {
4494 if (!client && (xevent.ConfigureEvent.xevent == xevent.ConfigureEvent.window)) { // Ignore events for children (SubstructureNotify) and client areas
4495 DriverDebug("GetMessage(): Window {0:X} ConfigureNotify x={1} y={2} width={3} height={4}",
4496 hwnd.client_window.ToInt32(), xevent.ConfigureEvent.x,
4497 xevent.ConfigureEvent.y, xevent.ConfigureEvent.width, xevent.ConfigureEvent.height);
4499 lock (hwnd.configure_lock) {
4500 Form form = Control.FromHandle (hwnd.client_window) as Form;
4501 if (form != null && !hwnd.resizing_or_moving) {
4502 if (hwnd.x != form.Bounds.X || hwnd.y != form.Bounds.Y) {
4503 SendMessage (form.Handle, Msg.WM_SYSCOMMAND, (IntPtr)SystemCommands.SC_MOVE, IntPtr.Zero);
4504 hwnd.resizing_or_moving = true;
4505 } else if (hwnd.width != form.Bounds.Width || hwnd.height != form.Bounds.Height) {
4506 SendMessage (form.Handle, Msg.WM_SYSCOMMAND, (IntPtr)SystemCommands.SC_SIZE, IntPtr.Zero);
4507 hwnd.resizing_or_moving = true;
4509 if (hwnd.resizing_or_moving)
4510 SendMessage (form.Handle, Msg.WM_ENTERSIZEMOVE, IntPtr.Zero, IntPtr.Zero);
4513 SendMessage(msg.hwnd, Msg.WM_WINDOWPOSCHANGED, IntPtr.Zero, IntPtr.Zero);
4514 hwnd.configure_pending = false;
4516 // We need to adjust our client window to track the resize of whole_window
4517 if (hwnd.whole_window != hwnd.client_window)
4518 PerformNCCalc(hwnd);
4521 goto ProcessNextMessage;
4524 case XEventName.FocusIn: {
4525 // We received focus. We use X11 focus only to know if the app window does or does not have focus
4526 // We do not track the actual focussed window via it. Instead, this is done via FocusWindow internally
4527 // Receiving focus means we've gotten activated and therefore we need to let the actual FocusWindow know
4528 // about it having focus again
4529 if (xevent.FocusChangeEvent.detail != NotifyDetail.NotifyNonlinear) {
4530 goto ProcessNextMessage;
4534 if (FocusWindow == IntPtr.Zero) {
4535 Control c = Control.FromHandle (hwnd.client_window);
4538 goto ProcessNextMessage;
4539 Form form = c.FindForm ();
4541 goto ProcessNextMessage;
4543 if (ActiveWindow != form.Handle) {
4544 ActiveWindow = form.Handle;
4545 SendMessage (ActiveWindow, Msg.WM_ACTIVATE, (IntPtr) WindowActiveFlags.WA_ACTIVE, IntPtr.Zero);
4547 goto ProcessNextMessage;
4549 Keyboard.FocusIn (FocusWindow);
4550 SendMessage(FocusWindow, Msg.WM_SETFOCUS, IntPtr.Zero, IntPtr.Zero);
4551 goto ProcessNextMessage;
4554 case XEventName.FocusOut: {
4555 // Se the comment for our FocusIn handler
4556 if (xevent.FocusChangeEvent.detail != NotifyDetail.NotifyNonlinear) {
4557 goto ProcessNextMessage;
4560 while (Keyboard.ResetKeyState(FocusWindow, ref msg)) {
4561 SendMessage(FocusWindow, msg.message, msg.wParam, msg.lParam);
4564 Keyboard.FocusOut(hwnd.client_window);
4565 SendMessage(FocusWindow, Msg.WM_KILLFOCUS, IntPtr.Zero, IntPtr.Zero);
4566 goto ProcessNextMessage;
4569 // We are already firing WM_SHOWWINDOW messages in the proper places, but I'm leaving this code
4570 // in case we break a scenario not taken into account in the tests
4571 case XEventName.MapNotify: {
4572 /*if (client && (xevent.ConfigureEvent.xevent == xevent.ConfigureEvent.window)) { // Ignore events for children (SubstructureNotify) and client areas
4574 msg.message = Msg.WM_SHOWWINDOW;
4575 msg.wParam = (IntPtr) 1;
4576 // XXX we're missing the lParam..
4579 goto ProcessNextMessage;
4582 case XEventName.UnmapNotify: {
4583 /*if (client && (xevent.ConfigureEvent.xevent == xevent.ConfigureEvent.window)) { // Ignore events for children (SubstructureNotify) and client areas
4584 hwnd.mapped = false;
4585 msg.message = Msg.WM_SHOWWINDOW;
4586 msg.wParam = (IntPtr) 0;
4587 // XXX we're missing the lParam..
4590 goto ProcessNextMessage;
4593 case XEventName.Expose: {
4596 hwnd.expose_pending = false;
4598 hwnd.nc_expose_pending = false;
4600 goto ProcessNextMessage;
4604 if (!hwnd.expose_pending) {
4605 goto ProcessNextMessage;
4608 if (!hwnd.nc_expose_pending) {
4609 goto ProcessNextMessage;
4612 switch (hwnd.border_style) {
4613 case FormBorderStyle.Fixed3D: {
4616 g = Graphics.FromHwnd(hwnd.whole_window);
4617 if (hwnd.border_static)
4618 ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Border3DStyle.SunkenOuter);
4620 ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Border3DStyle.Sunken);
4625 case FormBorderStyle.FixedSingle: {
4628 g = Graphics.FromHwnd(hwnd.whole_window);
4629 ControlPaint.DrawBorder(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Color.Black, ButtonBorderStyle.Solid);
4634 DriverDebug("GetMessage(): Window {0:X} Exposed non-client area {1},{2} {3}x{4}",
4635 hwnd.client_window.ToInt32(), xevent.ExposeEvent.x, xevent.ExposeEvent.y,
4636 xevent.ExposeEvent.width, xevent.ExposeEvent.height);
4638 Rectangle rect = new Rectangle (xevent.ExposeEvent.x, xevent.ExposeEvent.y, xevent.ExposeEvent.width, xevent.ExposeEvent.height);
4639 Region region = new Region (rect);
4640 IntPtr hrgn = region.GetHrgn (null); // Graphics object isn't needed
4641 msg.message = Msg.WM_NCPAINT;
4642 msg.wParam = hrgn == IntPtr.Zero ? (IntPtr)1 : hrgn;
4643 msg.refobject = region;
4646 DriverDebug("GetMessage(): Window {0:X} Exposed area {1},{2} {3}x{4}",
4647 hwnd.client_window.ToInt32(), xevent.ExposeEvent.x, xevent.ExposeEvent.y,
4648 xevent.ExposeEvent.width, xevent.ExposeEvent.height);
4649 if (Caret.Visible == true) {
4650 Caret.Paused = true;
4654 if (Caret.Visible == true) {
4656 Caret.Paused = false;
4658 msg.message = Msg.WM_PAINT;
4662 case XEventName.DestroyNotify: {
4664 // This is a bit tricky, we don't receive our own DestroyNotify, we only get those for our children
4665 hwnd = Hwnd.ObjectFromHandle(xevent.DestroyWindowEvent.window);
4667 // We may get multiple for the same window, act only one the first (when Hwnd still knows about it)
4668 if ((hwnd != null) && (hwnd.client_window == xevent.DestroyWindowEvent.window)) {
4669 CleanupCachedWindows (hwnd);
4671 DriverDebug("Received X11 Destroy Notification for {0}", XplatUI.Window(hwnd.client_window));
4673 msg.hwnd = hwnd.client_window;
4674 msg.message=Msg.WM_DESTROY;
4677 goto ProcessNextMessage;
4683 case XEventName.ClientMessage: {
4684 if (Dnd.HandleClientMessage (ref xevent)) {
4685 goto ProcessNextMessage;
4688 if (xevent.ClientMessageEvent.message_type == AsyncAtom) {
4689 XplatUIDriverSupport.ExecuteClientMessage((GCHandle)xevent.ClientMessageEvent.ptr1);
4690 goto ProcessNextMessage;
4693 if (xevent.ClientMessageEvent.message_type == HoverState.Atom) {
4694 msg.message = Msg.WM_MOUSEHOVER;
4695 msg.wParam = GetMousewParam(0);
4696 msg.lParam = (IntPtr) (xevent.ClientMessageEvent.ptr1);
4700 if (xevent.ClientMessageEvent.message_type == (IntPtr)PostAtom) {
4701 DebugHelper.Indent ();
4702 DebugHelper.WriteLine (String.Format ("Posted message:" + (Msg) xevent.ClientMessageEvent.ptr2.ToInt32 () + " for 0x{0:x}", xevent.ClientMessageEvent.ptr1.ToInt32 ()));
4703 DebugHelper.Unindent ();
4704 msg.hwnd = xevent.ClientMessageEvent.ptr1;
4705 msg.message = (Msg) xevent.ClientMessageEvent.ptr2.ToInt32 ();
4706 msg.wParam = xevent.ClientMessageEvent.ptr3;
4707 msg.lParam = xevent.ClientMessageEvent.ptr4;
4708 if (msg.message == (Msg)Msg.WM_QUIT)
4714 if (xevent.ClientMessageEvent.message_type == _XEMBED) {
4715 #if DriverDebugXEmbed
4716 Console.WriteLine("GOT EMBED MESSAGE {0:X}, detail {1:X}", xevent.ClientMessageEvent.ptr2.ToInt32(), xevent.ClientMessageEvent.ptr3.ToInt32());
4719 if (xevent.ClientMessageEvent.ptr2.ToInt32() == (int)XEmbedMessage.EmbeddedNotify) {
4720 XSizeHints hints = new XSizeHints();
4723 XGetWMNormalHints(DisplayHandle, hwnd.whole_window, ref hints, out dummy);
4725 hwnd.width = hints.max_width;
4726 hwnd.height = hints.max_height;
4727 hwnd.ClientRect = Rectangle.Empty;
4728 SendMessage(msg.hwnd, Msg.WM_WINDOWPOSCHANGED, IntPtr.Zero, IntPtr.Zero);
4732 if (xevent.ClientMessageEvent.message_type == WM_PROTOCOLS) {
4733 if (xevent.ClientMessageEvent.ptr1 == WM_DELETE_WINDOW) {
4734 SendMessage (msg.hwnd, Msg.WM_SYSCOMMAND, (IntPtr)SystemCommands.SC_CLOSE, IntPtr.Zero);
4735 msg.message = Msg.WM_CLOSE;
4739 // We should not get this, but I'll leave the code in case we need it in the future
4740 if (xevent.ClientMessageEvent.ptr1 == WM_TAKE_FOCUS) {
4741 goto ProcessNextMessage;
4744 goto ProcessNextMessage;
4748 goto ProcessNextMessage;
4755 HitTest NCHitTest (Hwnd hwnd, int x, int y)
4757 // The hit test is sent in screen coordinates
4759 int screen_x, screen_y;
4760 XTranslateCoordinates (DisplayHandle, hwnd.WholeWindow, RootWindow, x, y, out screen_x, out screen_y, out dummy);
4761 return (HitTest) NativeWindow.WndProc (hwnd.client_window, Msg.WM_NCHITTEST, IntPtr.Zero,
4762 (IntPtr) (screen_y << 16 | screen_x & 0xFFFF));
4765 // Our very basic implementation of MoveResize - we can extend it later
4767 internal override void BeginMoveResize (IntPtr handle)
4769 // We *need* to ungrab the pointer in the current display
4770 XplatUI.UngrabWindow (Grab.Hwnd);
4773 GetCursorPos (IntPtr.Zero, out x_root, out y_root);
4775 Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
4776 SendNetWMMessage (hwnd.whole_window, _NET_WM_MOVERESIZE, (IntPtr) x_root, (IntPtr) y_root,
4777 (IntPtr) NetWmMoveResize._NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT,
4778 (IntPtr) 1); // left button
4781 internal override bool GetText(IntPtr handle, out string text)
4789 IntPtr prop = IntPtr.Zero;
4791 XGetWindowProperty(DisplayHandle, handle,
4792 _NET_WM_NAME, IntPtr.Zero, new IntPtr (1), false,
4793 UTF8_STRING, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
4795 if ((long)nitems > 0 && prop != IntPtr.Zero) {
4796 text = Marshal.PtrToStringUni (prop, (int)nitems);
4801 // fallback on the non-_NET property
4804 textptr = IntPtr.Zero;
4806 XFetchName(DisplayHandle, Hwnd.ObjectFromHandle(handle).whole_window, ref textptr);
4807 if (textptr != IntPtr.Zero) {
4808 text = Marshal.PtrToStringAnsi(textptr);
4819 internal override void GetWindowPos(IntPtr handle, bool is_toplevel, out int x, out int y, out int width, out int height, out int client_width, out int client_height)
4823 hwnd = Hwnd.ObjectFromHandle(handle);
4829 height = hwnd.height;
4831 PerformNCCalc(hwnd);
4833 client_width = hwnd.ClientRect.Width;
4834 client_height = hwnd.ClientRect.Height;
4839 // Should we throw an exception or fail silently?
4840 // throw new ArgumentException("Called with an invalid window handle", "handle");
4850 internal override FormWindowState GetWindowState(IntPtr handle)
4854 hwnd = Hwnd.ObjectFromHandle(handle);
4856 if (hwnd.cached_window_state == (FormWindowState)(-1))
4857 hwnd.cached_window_state = UpdateWindowState (handle);
4859 return hwnd.cached_window_state;
4862 FormWindowState UpdateWindowState (IntPtr handle) {
4867 IntPtr prop = IntPtr.Zero;
4871 XWindowAttributes attributes;
4874 hwnd = Hwnd.ObjectFromHandle(handle);
4878 XGetWindowProperty(DisplayHandle, hwnd.whole_window, _NET_WM_STATE, IntPtr.Zero, new IntPtr (256), false, (IntPtr)Atom.XA_ATOM, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
4879 if (((long)nitems > 0) && (prop != IntPtr.Zero)) {
4880 for (int i = 0; i < (long)nitems; i++) {
4881 atom = (IntPtr)Marshal.ReadInt32(prop, i * 4);
4882 if ((atom == _NET_WM_STATE_MAXIMIZED_HORZ) || (atom == _NET_WM_STATE_MAXIMIZED_VERT)) {
4884 } else if (atom == _NET_WM_STATE_HIDDEN) {
4892 return FormWindowState.Minimized;
4893 } else if (maximized == 2) {
4894 return FormWindowState.Maximized;
4897 attributes = new XWindowAttributes();
4898 XGetWindowAttributes(DisplayHandle, hwnd.client_window, ref attributes);
4899 if (attributes.map_state == MapState.IsUnmapped) {
4900 return (FormWindowState)(-1);
4904 return FormWindowState.Normal;
4907 internal override void GrabInfo(out IntPtr handle, out bool GrabConfined, out Rectangle GrabArea)
4910 GrabConfined = Grab.Confined;
4911 GrabArea = Grab.Area;
4914 internal override void GrabWindow(IntPtr handle, IntPtr confine_to_handle)
4917 IntPtr confine_to_window;
4919 confine_to_window = IntPtr.Zero;
4921 if (confine_to_handle != IntPtr.Zero) {
4922 XWindowAttributes attributes = new XWindowAttributes();
4924 hwnd = Hwnd.ObjectFromHandle(confine_to_handle);
4927 XGetWindowAttributes(DisplayHandle, hwnd.client_window, ref attributes);
4929 Grab.Area.X = attributes.x;
4930 Grab.Area.Y = attributes.y;
4931 Grab.Area.Width = attributes.width;
4932 Grab.Area.Height = attributes.height;
4933 Grab.Confined = true;
4934 confine_to_window = hwnd.client_window;
4939 hwnd = Hwnd.ObjectFromHandle(handle);
4942 XGrabPointer(DisplayHandle, hwnd.client_window, false,
4943 EventMask.ButtonPressMask | EventMask.ButtonMotionMask |
4944 EventMask.ButtonReleaseMask | EventMask.PointerMotionMask |
4945 EventMask.PointerMotionHintMask | EventMask.LeaveWindowMask,
4946 GrabMode.GrabModeAsync, GrabMode.GrabModeAsync, confine_to_window, IntPtr.Zero, IntPtr.Zero);
4950 internal override void UngrabWindow(IntPtr hwnd)
4953 XUngrabPointer(DisplayHandle, IntPtr.Zero);
4954 XFlush(DisplayHandle);
4956 WindowUngrabbed (hwnd);
4959 void WindowUngrabbed (IntPtr hwnd) {
4960 bool was_grabbed = Grab.Hwnd != IntPtr.Zero;
4962 Grab.Hwnd = IntPtr.Zero;
4963 Grab.Confined = false;
4966 // lparam should be the handle to the window gaining the mouse capture,
4967 // but X doesn't seem to give us that information.
4968 // Also only generate WM_CAPTURECHANGED if the window actually was grabbed.
4969 // X will send a NotifyUngrab, but since it comes late sometimes we're
4970 // calling WindowUngrabbed directly from UngrabWindow in order to send
4971 // this WM right away.
4972 SendMessage (hwnd, Msg.WM_CAPTURECHANGED, IntPtr.Zero, IntPtr.Zero);
4976 internal override void HandleException(Exception e)
4978 StackTrace st = new StackTrace(e, true);
4979 Console.WriteLine("Exception '{0}'", e.Message+st.ToString());
4980 Console.WriteLine("{0}{1}", e.Message, st.ToString());
4983 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear)
4987 hwnd = Hwnd.ObjectFromHandle(handle);
4990 AddExpose (hwnd, true, hwnd.X, hwnd.Y, hwnd.Width, hwnd.Height);
4992 AddExpose (hwnd, true, rc.X, rc.Y, rc.Width, rc.Height);
4996 internal override void InvalidateNC (IntPtr handle)
5000 hwnd = Hwnd.ObjectFromHandle(handle);
5002 AddExpose (hwnd, hwnd.WholeWindow == hwnd.ClientWindow, 0, 0, hwnd.Width, hwnd.Height);
5005 internal override bool IsEnabled(IntPtr handle)
5007 Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
5008 return (hwnd != null && hwnd.Enabled);
5011 internal override bool IsVisible(IntPtr handle)
5013 Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
5014 return (hwnd != null && hwnd.visible);
5017 internal override void KillTimer(Timer timer)
5019 XEventQueue queue = (XEventQueue) MessageQueues [timer.thread];
5021 if (queue == null) {
5022 // This isn't really an error, MS doesn't start the timer if
5023 // it has no assosciated queue. In this case, remove the timer
5024 // from the list of unattached timers (if it was enabled).
5025 lock (unattached_timer_list) {
5026 if (unattached_timer_list.Contains (timer))
5027 unattached_timer_list.Remove (timer);
5031 queue.timer_list.Remove (timer);
5034 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y)
5041 hwnd = Hwnd.ObjectFromHandle(handle);
5044 XTranslateCoordinates(DisplayHandle, hwnd.whole_window, RootWindow, x, y, out dest_x_return, out dest_y_return, out child);
5051 internal override void OverrideCursor(IntPtr cursor)
5053 if (Grab.Hwnd != IntPtr.Zero) {
5054 XChangeActivePointerGrab (DisplayHandle,
5055 EventMask.ButtonMotionMask |
5056 EventMask.PointerMotionMask |
5057 EventMask.PointerMotionHintMask |
5058 EventMask.ButtonPressMask |
5059 EventMask.ButtonReleaseMask,
5060 cursor, IntPtr.Zero);
5064 OverrideCursorHandle = cursor;
5067 internal override PaintEventArgs PaintEventStart(ref Message msg, IntPtr handle, bool client)
5069 PaintEventArgs paint_event;
5074 // handle (and paint_hwnd) refers to the window that is should be painted.
5075 // msg.HWnd (and hwnd) refers to the window that got the paint message.
5078 hwnd = Hwnd.ObjectFromHandle(msg.HWnd);
5079 if (msg.HWnd == handle) {
5082 paint_hwnd = Hwnd.ObjectFromHandle (handle);
5085 if (Caret.Visible == true) {
5086 Caret.Paused = true;
5093 dc = Graphics.FromHwnd (paint_hwnd.client_window);
5095 Region clip_region = new Region ();
5096 clip_region.MakeEmpty();
5098 foreach (Rectangle r in hwnd.ClipRectangles) {
5099 /* Expand the region slightly.
5102 Rectangle r2 = Rectangle.FromLTRB (r.Left, r.Top, r.Right, r.Bottom + 1);
5103 clip_region.Union (r2);
5106 if (hwnd.UserClip != null) {
5107 clip_region.Intersect(hwnd.UserClip);
5110 dc.Clip = clip_region;
5111 paint_event = new PaintEventArgs(dc, hwnd.Invalid);
5112 hwnd.expose_pending = false;
5114 hwnd.ClearInvalidArea();
5116 hwnd.drawing_stack.Push (paint_event);
5117 hwnd.drawing_stack.Push (dc);
5121 dc = Graphics.FromHwnd (paint_hwnd.whole_window);
5123 if (!hwnd.nc_invalid.IsEmpty) {
5124 dc.SetClip (hwnd.nc_invalid);
5125 paint_event = new PaintEventArgs(dc, hwnd.nc_invalid);
5127 paint_event = new PaintEventArgs(dc, new Rectangle(0, 0, hwnd.width, hwnd.height));
5129 hwnd.nc_expose_pending = false;
5131 hwnd.ClearNcInvalidArea ();
5133 hwnd.drawing_stack.Push (paint_event);
5134 hwnd.drawing_stack.Push (dc);
5140 internal override void PaintEventEnd(ref Message msg, IntPtr handle, bool client)
5144 hwnd = Hwnd.ObjectFromHandle (msg.HWnd);
5146 Graphics dc = (Graphics)hwnd.drawing_stack.Pop ();
5150 PaintEventArgs pe = (PaintEventArgs)hwnd.drawing_stack.Pop();
5151 pe.SetGraphics (null);
5154 if (Caret.Visible == true) {
5156 Caret.Paused = false;
5160 [MonoTODO("Implement filtering and PM_NOREMOVE")]
5161 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags)
5163 XEventQueue queue = (XEventQueue) queue_id;
5166 if ((flags & (uint)PeekMessageFlags.PM_REMOVE) == 0) {
5167 throw new NotImplementedException("PeekMessage PM_NOREMOVE is not implemented yet"); // FIXME - Implement PM_NOREMOVE flag
5171 if (queue.Count > 0) {
5174 // Only call UpdateMessageQueue if real events are pending
5175 // otherwise we go to sleep on the socket
5176 if (XPending(DisplayHandle) != 0) {
5177 UpdateMessageQueue((XEventQueue)queue_id);
5179 } else if (((XEventQueue)queue_id).Paint.Count > 0) {
5184 CheckTimers(queue.timer_list, DateTime.UtcNow);
5189 return GetMessage(queue_id, ref msg, hWnd, wFilterMin, wFilterMax);
5192 internal override bool PostMessage (IntPtr handle, Msg message, IntPtr wparam, IntPtr lparam)
5194 XEvent xevent = new XEvent ();
5195 Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
5197 xevent.type = XEventName.ClientMessage;
5198 xevent.ClientMessageEvent.display = DisplayHandle;
5201 xevent.ClientMessageEvent.window = hwnd.whole_window;
5203 xevent.ClientMessageEvent.window = IntPtr.Zero;
5206 xevent.ClientMessageEvent.message_type = (IntPtr) PostAtom;
5207 xevent.ClientMessageEvent.format = 32;
5208 xevent.ClientMessageEvent.ptr1 = handle;
5209 xevent.ClientMessageEvent.ptr2 = (IntPtr) message;
5210 xevent.ClientMessageEvent.ptr3 = wparam;
5211 xevent.ClientMessageEvent.ptr4 = lparam;
5214 hwnd.Queue.EnqueueLocked (xevent);
5216 ThreadQueue(Thread.CurrentThread).EnqueueLocked (xevent);
5221 internal override void PostQuitMessage(int exitCode)
5223 ApplicationContext ctx = Application.MWFThread.Current.Context;
5224 Form f = ctx != null ? ctx.MainForm : null;
5226 PostMessage (Application.MWFThread.Current.Context.MainForm.window.Handle, Msg.WM_QUIT, IntPtr.Zero, IntPtr.Zero);
5228 PostMessage (FosterParent, Msg.WM_QUIT, IntPtr.Zero, IntPtr.Zero);
5229 XFlush(DisplayHandle);
5232 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave)
5237 internal override void RequestNCRecalc(IntPtr handle)
5241 hwnd = Hwnd.ObjectFromHandle(handle);
5247 PerformNCCalc(hwnd);
5248 SendMessage(handle, Msg.WM_WINDOWPOSCHANGED, IntPtr.Zero, IntPtr.Zero);
5249 InvalidateNC(handle);
5252 internal override void ResetMouseHover(IntPtr handle)
5256 hwnd = Hwnd.ObjectFromHandle(handle);
5261 HoverState.Timer.Enabled = true;
5262 HoverState.X = mouse_position.X;
5263 HoverState.Y = mouse_position.Y;
5264 HoverState.Window = handle;
5268 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y)
5275 hwnd = Hwnd.ObjectFromHandle(handle);
5278 XTranslateCoordinates (DisplayHandle, RootWindow, hwnd.client_window, x, y, out dest_x_return, out dest_y_return, out child);
5285 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y)
5292 hwnd = Hwnd.ObjectFromHandle(handle);
5295 XTranslateCoordinates (DisplayHandle, RootWindow, hwnd.whole_window, x, y, out dest_x_return, out dest_y_return, out child);
5298 Form form = Control.FromHandle (handle) as Form;
5299 if (form != null && form.window_manager != null) {
5300 dest_y_return -= form.window_manager.TitleBarHeight;
5307 bool GraphicsExposePredicate (IntPtr display, ref XEvent xevent, IntPtr arg)
5309 return (xevent.type == XEventName.GraphicsExpose || xevent.type == XEventName.NoExpose) &&
5310 arg == xevent.GraphicsExposeEvent.drawable;
5313 delegate bool EventPredicate (IntPtr display, ref XEvent xevent, IntPtr arg);
5315 void ProcessGraphicsExpose (Hwnd hwnd)
5317 XEvent xevent = new XEvent ();
5318 IntPtr handle = Hwnd.HandleFromObject (hwnd);
5319 EventPredicate predicate = GraphicsExposePredicate;
5322 XIfEvent (Display, ref xevent, predicate, handle);
5323 if (xevent.type != XEventName.GraphicsExpose)
5326 AddExpose (hwnd, xevent.ExposeEvent.window == hwnd.ClientWindow, xevent.GraphicsExposeEvent.x, xevent.GraphicsExposeEvent.y,
5327 xevent.GraphicsExposeEvent.width, xevent.GraphicsExposeEvent.height);
5329 if (xevent.GraphicsExposeEvent.count == 0)
5334 internal override void ScrollWindow(IntPtr handle, Rectangle area, int XAmount, int YAmount, bool with_children)
5338 XGCValues gc_values;
5340 hwnd = Hwnd.ObjectFromHandle(handle);
5342 Rectangle r = Rectangle.Intersect (hwnd.Invalid, area);
5344 /* We have an invalid area in the window we're scrolling.
5345 Adjust our stored invalid rectangle to to match the scrolled amount */
5360 if (area.Contains (hwnd.Invalid))
5361 hwnd.ClearInvalidArea ();
5362 hwnd.AddInvalidArea(r);
5365 gc_values = new XGCValues();
5367 if (with_children) {
5368 gc_values.subwindow_mode = GCSubwindowMode.IncludeInferiors;
5371 gc = XCreateGC(DisplayHandle, hwnd.client_window, IntPtr.Zero, ref gc_values);
5373 Rectangle visible_rect = GetTotalVisibleArea (hwnd.client_window);
5374 visible_rect.Intersect (area);
5376 Rectangle dest_rect = visible_rect;
5377 dest_rect.Y += YAmount;
5378 dest_rect.X += XAmount;
5379 dest_rect.Intersect (area);
5381 Point src = new Point (dest_rect.X - XAmount, dest_rect.Y - YAmount);
5382 XCopyArea (DisplayHandle, hwnd.client_window, hwnd.client_window, gc, src.X, src.Y,
5383 dest_rect.Width, dest_rect.Height, dest_rect.X, dest_rect.Y);
5385 Rectangle dirty_area = GetDirtyArea (area, dest_rect, XAmount, YAmount);
5386 AddExpose (hwnd, true, dirty_area.X, dirty_area.Y, dirty_area.Width, dirty_area.Height);
5388 ProcessGraphicsExpose (hwnd);
5390 XFreeGC(DisplayHandle, gc);
5393 internal override void ScrollWindow(IntPtr handle, int XAmount, int YAmount, bool with_children)
5398 hwnd = Hwnd.GetObjectFromWindow(handle);
5400 rect = hwnd.ClientRect;
5403 ScrollWindow(handle, rect, XAmount, YAmount, with_children);
5406 Rectangle GetDirtyArea (Rectangle total_area, Rectangle valid_area, int XAmount, int YAmount)
5408 Rectangle dirty_area = total_area;
5411 dirty_area.Height -= valid_area.Height;
5412 else if (YAmount < 0) {
5413 dirty_area.Height -= valid_area.Height;
5414 dirty_area.Y += valid_area.Height;
5418 dirty_area.Width -= valid_area.Width;
5419 else if (XAmount < 0) {
5420 dirty_area.Width -= valid_area.Width;
5421 dirty_area.X += valid_area.Width;
5427 Rectangle GetTotalVisibleArea (IntPtr handle)
5429 Control c = Control.FromHandle (handle);
5431 Rectangle visible_area = c.ClientRectangle;
5432 visible_area.Location = c.PointToScreen (Point.Empty);
5434 for (Control parent = c.Parent; parent != null; parent = parent.Parent) {
5435 if (!parent.IsHandleCreated || !parent.Visible)
5436 return visible_area; // Non visible, not need to finish computations
5438 Rectangle r = parent.ClientRectangle;
5439 r.Location = parent.PointToScreen (Point.Empty);
5441 visible_area.Intersect (r);
5444 visible_area.Location = c.PointToClient (visible_area.Location);
5445 return visible_area;
5448 internal override void SendAsyncMethod (AsyncMethodData method)
5451 XEvent xevent = new XEvent ();
5453 hwnd = Hwnd.ObjectFromHandle(method.Handle);
5455 xevent.type = XEventName.ClientMessage;
5456 xevent.ClientMessageEvent.display = DisplayHandle;
5457 xevent.ClientMessageEvent.window = method.Handle;
5458 xevent.ClientMessageEvent.message_type = (IntPtr)AsyncAtom;
5459 xevent.ClientMessageEvent.format = 32;
5460 xevent.ClientMessageEvent.ptr1 = (IntPtr) GCHandle.Alloc (method);
5462 hwnd.Queue.EnqueueLocked (xevent);
5467 delegate IntPtr WndProcDelegate (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam);
5469 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam)
5472 h = Hwnd.ObjectFromHandle(hwnd);
5474 if (h != null && h.queue != ThreadQueue (Thread.CurrentThread)) {
5475 AsyncMethodResult result;
5476 AsyncMethodData data;
5478 result = new AsyncMethodResult ();
5479 data = new AsyncMethodData ();
5482 data.Method = new WndProcDelegate (NativeWindow.WndProc);
5483 data.Args = new object[] { hwnd, message, wParam, lParam };
5484 data.Result = result;
5486 SendAsyncMethod (data);
5487 DriverDebug("Sending {0} message across.", message);
5491 string key = hwnd + ":" + message;
5492 if (messageHold[key] != null)
5493 messageHold[key] = ((int)messageHold[key]) - 1;
5494 return NativeWindow.WndProc(hwnd, message, wParam, lParam);
5497 internal override int SendInput(IntPtr handle, Queue keys)
5499 if (handle == IntPtr.Zero)
5502 int count = keys.Count;
5503 Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
5505 while (keys.Count > 0) {
5507 MSG msg = (MSG)keys.Dequeue();
5509 XEvent xevent = new XEvent ();
5511 xevent.type = (msg.message == Msg.WM_KEYUP ? XEventName.KeyRelease : XEventName.KeyPress);
5512 xevent.KeyEvent.display = DisplayHandle;
5515 xevent.KeyEvent.window = hwnd.whole_window;
5517 xevent.KeyEvent.window = IntPtr.Zero;
5520 xevent.KeyEvent.keycode = Keyboard.ToKeycode((int)msg.wParam);
5522 hwnd.Queue.EnqueueLocked (xevent);
5527 internal override void SetAllowDrop (IntPtr handle, bool value)
5529 // We allow drop on all windows
5532 internal override DragDropEffects StartDrag (IntPtr handle, object data,
5533 DragDropEffects allowed_effects)
5535 Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
5538 throw new ArgumentException ("Attempt to begin drag from invalid window handle (" + handle.ToInt32 () + ").");
5540 return Dnd.StartDrag (hwnd.client_window, data, allowed_effects);
5543 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style)
5545 Form form = Control.FromHandle (handle) as Form;
5546 if (form != null && form.window_manager == null) {
5547 CreateParams cp = form.GetCreateParams ();
5548 if (border_style == FormBorderStyle.FixedToolWindow ||
5549 border_style == FormBorderStyle.SizableToolWindow ||
5550 cp.IsSet (WindowExStyles.WS_EX_TOOLWINDOW)) {
5551 form.window_manager = new ToolWindowManager (form);
5555 RequestNCRecalc(handle);
5558 internal override void SetCaretPos(IntPtr handle, int x, int y)
5560 if (Caret.Hwnd == handle) {
5567 Keyboard.SetCaretPos (Caret, handle, x, y);
5569 if (Caret.Visible == true) {
5571 Caret.Timer.Start();
5576 internal override void SetClipRegion(IntPtr handle, Region region)
5580 hwnd = Hwnd.ObjectFromHandle(handle);
5585 hwnd.UserClip = region;
5588 internal override void SetCursor(IntPtr handle, IntPtr cursor)
5592 if (OverrideCursorHandle == IntPtr.Zero) {
5593 if ((LastCursorWindow == handle) && (LastCursorHandle == cursor)) {
5597 LastCursorHandle = cursor;
5598 LastCursorWindow = handle;
5600 hwnd = Hwnd.ObjectFromHandle(handle);
5602 if (cursor != IntPtr.Zero) {
5603 XDefineCursor(DisplayHandle, hwnd.whole_window, cursor);
5605 XUndefineCursor(DisplayHandle, hwnd.whole_window);
5607 XFlush(DisplayHandle);
5612 hwnd = Hwnd.ObjectFromHandle(handle);
5614 XDefineCursor(DisplayHandle, hwnd.whole_window, OverrideCursorHandle);
5618 void QueryPointer (IntPtr display, IntPtr w, out IntPtr root, out IntPtr child,
5619 out int root_x, out int root_y, out int child_x, out int child_y,
5622 /* this code was written with the help of
5623 glance at gdk. I never would have realized we
5624 needed a loop in order to traverse down in the
5625 hierarchy. I would have assumed you'd get the
5626 most deeply nested child and have to do
5627 XQueryTree to move back up the hierarchy..
5628 stupid me, of course. */
5631 XGrabServer (display);
5633 XQueryPointer(display, w, out root, out c,
5634 out root_x, out root_y, out child_x, out child_y,
5640 IntPtr child_last = IntPtr.Zero;
5641 while (c != IntPtr.Zero) {
5643 XQueryPointer(display, c, out root, out c,
5644 out root_x, out root_y, out child_x, out child_y,
5647 XUngrabServer (display);
5653 internal override void SetCursorPos(IntPtr handle, int x, int y)
5655 if (handle == IntPtr.Zero) {
5658 int root_x, root_y, child_x, child_y, mask;
5661 * QueryPointer before warping
5662 * because if the warp is on
5663 * the RootWindow, the x/y are
5664 * relative to the current
5667 QueryPointer (DisplayHandle, RootWindow,
5670 out root_x, out root_y,
5671 out child_x, out child_y,
5674 XWarpPointer(DisplayHandle, IntPtr.Zero, IntPtr.Zero, 0, 0, 0, 0, x - root_x, y - root_y);
5676 XFlush (DisplayHandle);
5678 /* then we need to a
5679 * QueryPointer after warping
5680 * to manually generate a
5681 * motion event for the window
5684 QueryPointer (DisplayHandle, RootWindow,
5687 out root_x, out root_y,
5688 out child_x, out child_y,
5691 Hwnd child_hwnd = Hwnd.ObjectFromHandle(child);
5692 if (child_hwnd == null) {
5696 XEvent xevent = new XEvent ();
5698 xevent.type = XEventName.MotionNotify;
5699 xevent.MotionEvent.display = DisplayHandle;
5700 xevent.MotionEvent.window = child_hwnd.client_window;
5701 xevent.MotionEvent.root = RootWindow;
5702 xevent.MotionEvent.x = child_x;
5703 xevent.MotionEvent.y = child_y;
5704 xevent.MotionEvent.x_root = root_x;
5705 xevent.MotionEvent.y_root = root_y;
5706 xevent.MotionEvent.state = mask;
5708 child_hwnd.Queue.EnqueueLocked (xevent);
5713 hwnd = Hwnd.ObjectFromHandle(handle);
5715 XWarpPointer(DisplayHandle, IntPtr.Zero, hwnd.client_window, 0, 0, 0, 0, x, y);
5720 internal override void SetFocus(IntPtr handle)
5723 IntPtr prev_focus_window;
5725 hwnd = Hwnd.ObjectFromHandle(handle);
5727 if (hwnd.client_window == FocusWindow) {
5731 // Win32 doesn't do anything if disabled
5735 prev_focus_window = FocusWindow;
5736 FocusWindow = hwnd.client_window;
5738 if (prev_focus_window != IntPtr.Zero) {
5739 SendMessage(prev_focus_window, Msg.WM_KILLFOCUS, FocusWindow, IntPtr.Zero);
5741 Keyboard.FocusIn (FocusWindow);
5742 SendMessage(FocusWindow, Msg.WM_SETFOCUS, prev_focus_window, IntPtr.Zero);
5744 //XSetInputFocus(DisplayHandle, Hwnd.ObjectFromHandle(handle).client_window, RevertTo.None, IntPtr.Zero);
5747 internal override void SetIcon(IntPtr handle, Icon icon)
5751 hwnd = Hwnd.ObjectFromHandle(handle);
5753 SetIcon(hwnd, icon);
5757 internal override void SetMenu(IntPtr handle, Menu menu)
5761 hwnd = Hwnd.ObjectFromHandle(handle);
5764 RequestNCRecalc(handle);
5767 internal override void SetModal(IntPtr handle, bool Modal)
5770 ModalWindows.Push(handle);
5772 if (ModalWindows.Contains(handle)) {
5775 if (ModalWindows.Count > 0) {
5776 Activate((IntPtr)ModalWindows.Peek());
5780 Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
5781 Control ctrl = Control.FromHandle (handle);
5782 SetWMStyles (hwnd, ctrl.GetCreateParams ());
5785 internal override IntPtr SetParent(IntPtr handle, IntPtr parent)
5789 hwnd = Hwnd.ObjectFromHandle(handle);
5790 hwnd.parent = Hwnd.ObjectFromHandle(parent);
5793 DriverDebug("Parent for window {0} = {1}", XplatUI.Window(hwnd.Handle), XplatUI.Window(hwnd.parent != null ? hwnd.parent.Handle : IntPtr.Zero));
5794 XReparentWindow(DisplayHandle, hwnd.whole_window, hwnd.parent == null ? FosterParent : hwnd.parent.client_window, hwnd.x, hwnd.y);
5800 internal override void SetTimer (Timer timer)
5802 XEventQueue queue = (XEventQueue) MessageQueues [timer.thread];
5804 if (queue == null) {
5805 // This isn't really an error, MS doesn't start the timer if
5806 // it has no assosciated queue at this stage (it will be
5807 // enabled when a window is activated).
5808 unattached_timer_list.Add (timer);
5811 queue.timer_list.Add (timer);
5815 internal override bool SetTopmost(IntPtr handle, bool enabled)
5818 Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
5819 hwnd.topmost = enabled;
5824 SendNetWMMessage(hwnd.WholeWindow, _NET_WM_STATE, (IntPtr) NetWmStateRequest._NET_WM_STATE_ADD, _NET_WM_STATE_ABOVE, IntPtr.Zero);
5826 int[] atoms = new int[8];
5827 atoms[0] = _NET_WM_STATE_ABOVE.ToInt32();
5828 XChangeProperty(DisplayHandle, hwnd.whole_window, _NET_WM_STATE, (IntPtr)Atom.XA_ATOM, 32, PropertyMode.Replace, atoms, 1);
5834 SendNetWMMessage(hwnd.WholeWindow, _NET_WM_STATE, (IntPtr) NetWmStateRequest._NET_WM_STATE_REMOVE, _NET_WM_STATE_ABOVE, IntPtr.Zero);
5836 XDeleteProperty(DisplayHandle, hwnd.whole_window, _NET_WM_STATE);
5842 internal override bool SetOwner(IntPtr handle, IntPtr handle_owner)
5847 hwnd = Hwnd.ObjectFromHandle(handle);
5849 if (handle_owner != IntPtr.Zero) {
5850 hwnd_owner = Hwnd.ObjectFromHandle(handle_owner);
5856 atoms[0] = _NET_WM_WINDOW_TYPE_NORMAL.ToInt32();
5857 XChangeProperty(DisplayHandle, hwnd.whole_window, _NET_WM_WINDOW_TYPE, (IntPtr)Atom.XA_ATOM, 32, PropertyMode.Replace, atoms, 1);
5859 if (hwnd_owner != null) {
5860 XSetTransientForHint(DisplayHandle, hwnd.whole_window, hwnd_owner.whole_window);
5862 XSetTransientForHint(DisplayHandle, hwnd.whole_window, RootWindow);
5867 XDeleteProperty(DisplayHandle, hwnd.whole_window, (IntPtr)Atom.XA_WM_TRANSIENT_FOR);
5873 internal override bool SetVisible (IntPtr handle, bool visible, bool activate)
5877 hwnd = Hwnd.ObjectFromHandle(handle);
5878 hwnd.visible = visible;
5882 MapWindow(hwnd, WindowType.Both);
5884 if (Control.FromHandle(handle) is Form) {
5887 s = ((Form)Control.FromHandle(handle)).WindowState;
5890 case FormWindowState.Minimized: SetWindowState(handle, FormWindowState.Minimized); break;
5891 case FormWindowState.Maximized: SetWindowState(handle, FormWindowState.Maximized); break;
5895 SendMessage(handle, Msg.WM_WINDOWPOSCHANGED, IntPtr.Zero, IntPtr.Zero);
5898 UnmapWindow(hwnd, WindowType.Both);
5904 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max)
5906 Control ctrl = Control.FromHandle (handle);
5907 SetWindowMinMax (handle, maximized, min, max, ctrl != null ? ctrl.GetCreateParams () : null);
5910 internal void SetWindowMinMax (IntPtr handle, Rectangle maximized, Size min, Size max, CreateParams cp)
5916 hwnd = Hwnd.ObjectFromHandle(handle);
5921 min.Width = Math.Max (min.Width, SystemInformation.MinimumWindowSize.Width);
5922 min.Height = Math.Max (min.Height, SystemInformation.MinimumWindowSize.Height);
5924 hints = new XSizeHints();
5926 XGetWMNormalHints(DisplayHandle, hwnd.whole_window, ref hints, out dummy);
5927 if ((min != Size.Empty) && (min.Width > 0) && (min.Height > 0)) {
5929 min = TranslateWindowSizeToXWindowSize (cp, min);
5930 hints.flags = (IntPtr)((int)hints.flags | (int)XSizeHintsFlags.PMinSize);
5931 hints.min_width = min.Width;
5932 hints.min_height = min.Height;
5935 if ((max != Size.Empty) && (max.Width > 0) && (max.Height > 0)) {
5937 max = TranslateWindowSizeToXWindowSize (cp, max);
5938 hints.flags = (IntPtr)((int)hints.flags | (int)XSizeHintsFlags.PMaxSize);
5939 hints.max_width = max.Width;
5940 hints.max_height = max.Height;
5943 if (hints.flags != IntPtr.Zero) {
5944 // The Metacity team has decided that they won't care about this when clicking the maximize icon,
5945 // they will maximize the window to fill the screen/parent no matter what.
5946 // http://bugzilla.ximian.com/show_bug.cgi?id=80021
5947 XSetWMNormalHints(DisplayHandle, hwnd.whole_window, ref hints);
5950 if ((maximized != Rectangle.Empty) && (maximized.Width > 0) && (maximized.Height > 0)) {
5952 maximized.Size = TranslateWindowSizeToXWindowSize (cp);
5953 hints.flags = (IntPtr)XSizeHintsFlags.PPosition;
5954 hints.x = maximized.X;
5955 hints.y = maximized.Y;
5956 hints.width = maximized.Width;
5957 hints.height = maximized.Height;
5959 // Metacity does not seem to follow this constraint for maximized (zoomed) windows
5960 XSetZoomHints(DisplayHandle, hwnd.whole_window, ref hints);
5965 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height)
5969 hwnd = Hwnd.ObjectFromHandle(handle);
5975 // Win32 automatically changes negative width/height to 0.
5981 // X requires a sanity check for width & height; otherwise it dies
5982 if (hwnd.zero_sized && width > 0 && height > 0) {
5984 MapWindow(hwnd, WindowType.Whole);
5986 hwnd.zero_sized = false;
5989 if ((width < 1) || (height < 1)) {
5990 hwnd.zero_sized = true;
5991 UnmapWindow(hwnd, WindowType.Whole);
5994 // Save a server roundtrip (and prevent a feedback loop)
5995 if ((hwnd.x == x) && (hwnd.y == y) &&
5996 (hwnd.width == width) && (hwnd.height == height)) {
6000 if (!hwnd.zero_sized) {
6005 hwnd.height = height;
6006 SendMessage(hwnd.client_window, Msg.WM_WINDOWPOSCHANGED, IntPtr.Zero, IntPtr.Zero);
6008 if (hwnd.fixed_size) {
6009 SetWindowMinMax(handle, Rectangle.Empty, new Size(width, height), new Size(width, height));
6013 Control ctrl = Control.FromHandle (handle);
6014 Size TranslatedSize = TranslateWindowSizeToXWindowSize (ctrl.GetCreateParams (), new Size (width, height));
6015 MoveResizeWindow (DisplayHandle, hwnd.whole_window, x, y, TranslatedSize.Width, TranslatedSize.Height);
6016 PerformNCCalc(hwnd);
6020 // Update our position/size immediately, so
6021 // that future calls to SetWindowPos aren't
6022 // kept from calling XMoveResizeWindow (by the
6023 // "Save a server roundtrip" block above).
6027 hwnd.height = height;
6028 hwnd.ClientRect = Rectangle.Empty;
6031 internal override void SetWindowState(IntPtr handle, FormWindowState state)
6033 FormWindowState current_state;
6036 hwnd = Hwnd.ObjectFromHandle(handle);
6038 current_state = GetWindowState(handle);
6040 if (current_state == state) {
6045 case FormWindowState.Normal: {
6047 if (current_state == FormWindowState.Minimized) {
6048 MapWindow(hwnd, WindowType.Both);
6049 } else if (current_state == FormWindowState.Maximized) {
6050 SendNetWMMessage(hwnd.whole_window, _NET_WM_STATE, (IntPtr)2 /* toggle */, _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT);
6057 case FormWindowState.Minimized: {
6059 if (current_state == FormWindowState.Maximized) {
6060 SendNetWMMessage(hwnd.whole_window, _NET_WM_STATE, (IntPtr)2 /* toggle */, _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT);
6062 XIconifyWindow(DisplayHandle, hwnd.whole_window, ScreenNo);
6067 case FormWindowState.Maximized: {
6069 if (current_state == FormWindowState.Minimized) {
6070 MapWindow(hwnd, WindowType.Both);
6073 SendNetWMMessage(hwnd.whole_window, _NET_WM_STATE, (IntPtr)1 /* Add */, _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT);
6081 internal override void SetWindowStyle(IntPtr handle, CreateParams cp)
6085 hwnd = Hwnd.ObjectFromHandle(handle);
6086 SetHwndStyles(hwnd, cp);
6087 SetWMStyles(hwnd, cp);
6090 internal override double GetWindowTransparency(IntPtr handle)
6095 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key)
6100 hwnd = Hwnd.ObjectFromHandle(handle);
6106 hwnd.opacity = (uint)(0xffffffff * transparency);
6107 opacity = (IntPtr)((int)hwnd.opacity);
6109 IntPtr w = hwnd.whole_window;
6110 if (hwnd.reparented)
6111 w = XGetParent (hwnd.whole_window);
6112 XChangeProperty(DisplayHandle, w, _NET_WM_WINDOW_OPACITY, (IntPtr)Atom.XA_CARDINAL, 32, PropertyMode.Replace, ref opacity, 1);
6115 internal override bool SetZOrder(IntPtr handle, IntPtr after_handle, bool top, bool bottom)
6117 Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
6125 XRaiseWindow(DisplayHandle, hwnd.whole_window);
6128 } else if (!bottom) {
6129 Hwnd after_hwnd = null;
6131 if (after_handle != IntPtr.Zero) {
6132 after_hwnd = Hwnd.ObjectFromHandle(after_handle);
6135 XWindowChanges values = new XWindowChanges();
6137 if (after_hwnd == null) {
6138 // Work around metacity 'issues'
6142 atoms[0] = unixtime();
6143 XChangeProperty(DisplayHandle, hwnd.whole_window, _NET_WM_USER_TIME, (IntPtr)Atom.XA_CARDINAL, 32, PropertyMode.Replace, atoms, 1);
6145 XRaiseWindow(DisplayHandle, hwnd.whole_window);
6146 SendNetWMMessage(hwnd.whole_window, _NET_ACTIVE_WINDOW, (IntPtr)1, IntPtr.Zero, IntPtr.Zero);
6148 //throw new ArgumentNullException("after_handle", "Need sibling to adjust z-order");
6151 values.sibling = after_hwnd.whole_window;
6152 values.stack_mode = StackMode.Below;
6155 XConfigureWindow(DisplayHandle, hwnd.whole_window, ChangeWindowFlags.CWStackMode | ChangeWindowFlags.CWSibling, ref values);
6160 XLowerWindow(DisplayHandle, hwnd.whole_window);
6167 internal override void ShowCursor(bool show)
6169 ; // FIXME - X11 doesn't 'hide' the cursor. we could create an empty cursor
6172 internal override object StartLoop(Thread thread)
6174 XEventQueue q = ThreadQueue(thread);
6178 internal override TransparencySupport SupportsTransparency()
6180 // We need to check if the x compositing manager is running
6181 return TransparencySupport.Set;
6184 internal override bool SystrayAdd(IntPtr handle, string tip, Icon icon, out ToolTip tt)
6186 GetSystrayManagerWindow();
6188 if (SystrayMgrWindow != IntPtr.Zero) {
6189 XSizeHints size_hints;
6192 hwnd = Hwnd.ObjectFromHandle(handle);
6193 DriverDebug("Adding Systray Whole:{0:X}, Client:{1:X}", hwnd.whole_window.ToInt32(), hwnd.client_window.ToInt32());
6196 if (hwnd.client_window != hwnd.whole_window) {
6197 Keyboard.DestroyICForWindow (hwnd.client_window);
6198 XDestroyWindow(DisplayHandle, hwnd.client_window);
6199 hwnd.client_window = hwnd.whole_window;
6202 /* by virtue of the way the tests are ordered when determining if it's PAINT
6203 or NCPAINT, client_window == whole_window will always be PAINT. So, if we're
6204 waiting on an nc_expose, drop it and remove the hwnd from the list (unless
6205 there's a pending expose). */
6206 if (hwnd.nc_expose_pending) {
6207 hwnd.nc_expose_pending = false;
6208 if (!hwnd.expose_pending)
6209 hwnd.Queue.Paint.Remove (hwnd);
6212 // We are going to be directly mapped by the system tray, so mark as mapped
6213 // so we can later properly unmap it.
6216 size_hints = new XSizeHints();
6218 size_hints.flags = (IntPtr)(XSizeHintsFlags.PMinSize | XSizeHintsFlags.PMaxSize | XSizeHintsFlags.PBaseSize);
6220 size_hints.min_width = 24;
6221 size_hints.min_height = 24;
6222 size_hints.max_width = 24;
6223 size_hints.max_height = 24;
6224 size_hints.base_width = 24;
6225 size_hints.base_height = 24;
6227 XSetWMNormalHints(DisplayHandle, hwnd.whole_window, ref size_hints);
6229 int[] atoms = new int[2];
6230 atoms [0] = 1; // Version 1
6231 atoms [1] = 1; // we want to be mapped
6233 // This line cost me 3 days...
6234 XChangeProperty(DisplayHandle, hwnd.whole_window, _XEMBED_INFO, _XEMBED_INFO, 32, PropertyMode.Replace, atoms, 2);
6236 // Need to pick some reasonable defaults
6238 tt.AutomaticDelay = 350;
6239 tt.InitialDelay = 250;
6240 tt.ReshowDelay = 250;
6241 tt.ShowAlways = true;
6243 if ((tip != null) && (tip != string.Empty)) {
6244 tt.SetToolTip(Control.FromHandle(handle), tip);
6250 SendNetClientMessage(SystrayMgrWindow, _NET_SYSTEM_TRAY_OPCODE, IntPtr.Zero, (IntPtr)SystrayRequest.SYSTEM_TRAY_REQUEST_DOCK, hwnd.whole_window);
6258 internal override bool SystrayChange(IntPtr handle, string tip, Icon icon, ref ToolTip tt)
6262 control = Control.FromHandle(handle);
6263 if (control != null && tt != null) {
6264 tt.SetToolTip(control, tip);
6266 SendMessage(handle, Msg.WM_PAINT, IntPtr.Zero, IntPtr.Zero);
6273 internal override void SystrayRemove(IntPtr handle, ref ToolTip tt)
6276 SetVisible (handle, false, false);
6278 // The caller can now re-dock it later...
6283 // Close any balloon window *we* fired.
6284 ThemeEngine.Current.HideBalloonWindow (handle);
6287 internal override void SystrayBalloon(IntPtr handle, int timeout, string title, string text, ToolTipIcon icon)
6289 ThemeEngine.Current.ShowBalloonWindow (handle, timeout, title, text, icon);
6290 SendMessage(handle, Msg.WM_USER, IntPtr.Zero, (IntPtr) Msg.NIN_BALLOONSHOW);
6293 internal override bool Text(IntPtr handle, string text)
6297 hwnd = Hwnd.ObjectFromHandle(handle);
6300 XChangeProperty(DisplayHandle, hwnd.whole_window, _NET_WM_NAME, UTF8_STRING, 8,
6301 PropertyMode.Replace, text, Encoding.UTF8.GetByteCount (text));
6303 // XXX this has problems with UTF8.
6304 // we need to either use the actual
6305 // text if it's latin-1, or convert it
6306 // to compound text if it's in a
6307 // different charset.
6308 XStoreName(DisplayHandle, Hwnd.ObjectFromHandle(handle).whole_window, text);
6313 internal override bool TranslateMessage(ref MSG msg)
6315 return Keyboard.TranslateMessage (ref msg);
6318 internal override void UpdateWindow(IntPtr handle)
6322 hwnd = Hwnd.ObjectFromHandle(handle);
6324 if (!hwnd.visible || !hwnd.expose_pending || !hwnd.Mapped) {
6328 SendMessage(handle, Msg.WM_PAINT, IntPtr.Zero, IntPtr.Zero);
6329 hwnd.Queue.Paint.Remove(hwnd);
6332 internal override void CreateOffscreenDrawable (IntPtr handle,
6333 int width, int height,
6334 out object offscreen_drawable)
6337 int x_out, y_out, width_out, height_out, border_width_out, depth_out;
6339 XGetGeometry (DisplayHandle, handle,
6341 out x_out, out y_out,
6342 out width_out, out height_out,
6343 out border_width_out, out depth_out);
6345 IntPtr pixmap = XCreatePixmap (DisplayHandle, handle, width, height, depth_out);
6347 offscreen_drawable = pixmap;
6351 internal override void DestroyOffscreenDrawable (object offscreen_drawable)
6353 XFreePixmap (DisplayHandle, (IntPtr)offscreen_drawable);
6356 internal override Graphics GetOffscreenGraphics (object offscreen_drawable)
6358 return Graphics.FromHwnd ((IntPtr) offscreen_drawable);
6361 internal override void BlitFromOffscreen (IntPtr dest_handle,
6363 object offscreen_drawable,
6364 Graphics offscreen_dc,
6367 XGCValues gc_values;
6370 gc_values = new XGCValues();
6372 gc = XCreateGC (DisplayHandle, dest_handle, IntPtr.Zero, ref gc_values);
6374 XCopyArea (DisplayHandle, (IntPtr)offscreen_drawable, dest_handle,
6375 gc, r.X, r.Y, r.Width, r.Height, r.X, r.Y);
6377 XFreeGC (DisplayHandle, gc);
6380 #endregion // Public Static Methods
6383 internal override event EventHandler Idle;
6384 #endregion // Events
6389 #region Xcursor imports
6390 [DllImport ("libXcursor", EntryPoint = "XcursorLibraryLoadCursor")]
6391 internal extern static IntPtr XcursorLibraryLoadCursor (IntPtr display, [MarshalAs (UnmanagedType.LPStr)] string name);
6393 [DllImport ("libXcursor", EntryPoint = "XcursorLibraryLoadImages")]
6394 internal extern static IntPtr XcursorLibraryLoadImages ([MarshalAs (UnmanagedType.LPStr)] string file, IntPtr theme, int size);
6396 [DllImport ("libXcursor", EntryPoint = "XcursorImagesDestroy")]
6397 internal extern static void XcursorImagesDestroy (IntPtr images);
6399 [DllImport ("libXcursor", EntryPoint = "XcursorGetDefaultSize")]
6400 internal extern static int XcursorGetDefaultSize (IntPtr display);
6402 [DllImport ("libXcursor", EntryPoint = "XcursorImageLoadCursor")]
6403 internal extern static IntPtr XcursorImageLoadCursor (IntPtr display, IntPtr image);
6405 [DllImport ("libXcursor", EntryPoint = "XcursorGetTheme")]
6406 internal extern static IntPtr XcursorGetTheme (IntPtr display);
6409 [DllImport ("libX11", EntryPoint="XOpenDisplay")]
6410 internal extern static IntPtr XOpenDisplay(IntPtr display);
6411 [DllImport ("libX11", EntryPoint="XCloseDisplay")]
6412 internal extern static int XCloseDisplay(IntPtr display);
6413 [DllImport ("libX11", EntryPoint="XSynchronize")]
6414 internal extern static IntPtr XSynchronize(IntPtr display, bool onoff);
6416 [DllImport ("libX11", EntryPoint="XCreateWindow")]
6417 internal extern static IntPtr _XCreateWindow(IntPtr display, IntPtr parent, int x, int y, int width, int height, int border_width, int depth, int xclass, IntPtr visual, UIntPtr valuemask, ref XSetWindowAttributes attributes);
6418 internal static IntPtr XCreateWindow(IntPtr display, IntPtr parent, int x, int y, int width, int height, int border_width, int depth, int xclass, IntPtr visual, UIntPtr valuemask, ref XSetWindowAttributes attributes)
6420 DebugHelper.TraceWriteLine ("XCreateWindow");
6421 return _XCreateWindow(display, parent, x, y, width, height,
6422 border_width, depth, xclass, visual, valuemask, ref attributes);
6424 [DllImport ("libX11", EntryPoint="XCreateSimpleWindow")]
6425 internal extern static IntPtr _XCreateSimpleWindow(IntPtr display, IntPtr parent, int x, int y, int width, int height, int border_width, UIntPtr border, UIntPtr background);
6426 internal static IntPtr XCreateSimpleWindow(IntPtr display, IntPtr parent, int x, int y, int width, int height, int border_width, UIntPtr border, UIntPtr background)
6428 DebugHelper.TraceWriteLine ("XCreateSimpleWindow");
6429 return _XCreateSimpleWindow(display, parent, x, y, width, height, border_width, border, background);
6431 [DllImport ("libX11", EntryPoint="XMapWindow")]
6432 internal extern static int _XMapWindow(IntPtr display, IntPtr window);
6433 internal static int XMapWindow(IntPtr display, IntPtr window)
6435 DebugHelper.TraceWriteLine ("XMapWindow");
6436 return _XMapWindow(display, window);
6438 [DllImport ("libX11", EntryPoint="XMapRaised")]
6439 internal extern static int _XMapRaised(IntPtr display, IntPtr window);
6440 internal static int XMapRaised(IntPtr display, IntPtr window)
6442 DebugHelper.TraceWriteLine ("XMapRaised");
6443 return _XMapRaised(display, window);
6445 [DllImport ("libX11", EntryPoint="XUnmapWindow")]
6446 internal extern static int _XUnmapWindow(IntPtr display, IntPtr window);
6447 internal static int XUnmapWindow(IntPtr display, IntPtr window)
6449 DebugHelper.TraceWriteLine ("XUnmapWindow");
6450 return _XUnmapWindow(display, window);
6452 [DllImport ("libX11", EntryPoint="XMapSubwindows")]
6453 internal extern static int _XMapSubindows(IntPtr display, IntPtr window);
6454 internal static int XMapSubindows(IntPtr display, IntPtr window)
6456 DebugHelper.TraceWriteLine ("XMapSubindows");
6457 return _XMapSubindows(display, window);
6459 [DllImport ("libX11", EntryPoint="XUnmapSubwindows")]
6460 internal extern static int _XUnmapSubwindows(IntPtr display, IntPtr window);
6461 internal static int XUnmapSubwindows(IntPtr display, IntPtr window)
6463 DebugHelper.TraceWriteLine ("XUnmapSubwindows");
6464 return _XUnmapSubwindows(display, window);
6466 [DllImport ("libX11", EntryPoint="XRootWindow")]
6467 internal extern static IntPtr _XRootWindow(IntPtr display, int screen_number);
6468 internal static IntPtr XRootWindow(IntPtr display, int screen_number)
6470 DebugHelper.TraceWriteLine ("XRootWindow");
6471 return _XRootWindow(display, screen_number);
6473 [DllImport ("libX11", EntryPoint="XNextEvent")]
6474 internal extern static IntPtr _XNextEvent(IntPtr display, ref XEvent xevent);
6475 internal static IntPtr XNextEvent(IntPtr display, ref XEvent xevent)
6477 DebugHelper.TraceWriteLine ("XNextEvent");
6478 return _XNextEvent(display, ref xevent);
6480 [DllImport ("libX11", EntryPoint="XConnectionNumber")]
6481 internal extern static int _XConnectionNumber (IntPtr display);
6482 internal static int XConnectionNumber (IntPtr display)
6484 DebugHelper.TraceWriteLine ("XConnectionNumber");
6485 return _XConnectionNumber (display);
6487 [DllImport ("libX11", EntryPoint="XPending")]
6488 internal extern static int _XPending (IntPtr display);
6489 internal static int XPending (IntPtr display)
6491 DebugHelper.TraceWriteLine ("XPending");
6492 DebugHelper.DumpCallers (3);
6493 return _XPending (display);
6495 [DllImport ("libX11", EntryPoint="XSelectInput")]
6496 internal extern static IntPtr _XSelectInput(IntPtr display, IntPtr window, IntPtr mask);
6497 internal static IntPtr XSelectInput(IntPtr display, IntPtr window, IntPtr mask)
6499 DebugHelper.TraceWriteLine ("XSelectInput");
6500 return _XSelectInput(display, window, mask);
6503 [DllImport ("libX11", EntryPoint="XDestroyWindow")]
6504 internal extern static int _XDestroyWindow(IntPtr display, IntPtr window);
6505 internal static int XDestroyWindow(IntPtr display, IntPtr window)
6507 DebugHelper.TraceWriteLine ("XDestroyWindow 0x{0:x}", window.ToInt32());
6508 return _XDestroyWindow(display, window);
6511 [DllImport ("libX11", EntryPoint="XReparentWindow")]
6512 internal extern static int _XReparentWindow(IntPtr display, IntPtr window, IntPtr parent, int x, int y);
6513 internal static int XReparentWindow(IntPtr display, IntPtr window, IntPtr parent, int x, int y)
6515 DebugHelper.TraceWriteLine ("XReparentWindow");
6516 return _XReparentWindow(display, window, parent, x, y);
6519 [DllImport ("libX11", EntryPoint="XMoveResizeWindow")]
6520 extern static int _XMoveResizeWindow(IntPtr display, IntPtr window, int x, int y, int width, int height);
6521 static int XMoveResizeWindow(IntPtr display, IntPtr window, int x, int y, int width, int height) {
6522 DebugHelper.TraceWriteLine ("XMoveResizeWindow");
6523 return _XMoveResizeWindow(display, window, x, y, width, height);
6526 internal static int MoveResizeWindow(IntPtr display, IntPtr window, int x, int y, int width, int height)
6528 int ret = XMoveResizeWindow (display, window, x, y, width, height);
6529 Keyboard.MoveCurrentCaretPos ();
6533 [DllImport ("libX11", EntryPoint="XResizeWindow")]
6534 internal extern static int _XResizeWindow(IntPtr display, IntPtr window, int width, int height);
6535 internal static int XResizeWindow(IntPtr display, IntPtr window, int width, int height)
6537 DebugHelper.TraceWriteLine ("XResizeWindow");
6538 return _XResizeWindow(display, window, width, height);
6541 [DllImport ("libX11", EntryPoint="XGetWindowAttributes")]
6542 internal extern static int _XGetWindowAttributes(IntPtr display, IntPtr window, ref XWindowAttributes attributes);
6543 internal static int XGetWindowAttributes(IntPtr display, IntPtr window, ref XWindowAttributes attributes)
6545 DebugHelper.TraceWriteLine ("XGetWindowAttributes");
6546 return _XGetWindowAttributes(display, window, ref attributes);
6549 [DllImport ("libX11", EntryPoint="XFlush")]
6550 internal extern static int _XFlush(IntPtr display);
6551 internal static int XFlush(IntPtr display)
6553 DebugHelper.TraceWriteLine ("XFlush");
6554 return _XFlush(display);
6557 [DllImport ("libX11", EntryPoint="XSetWMName")]
6558 internal extern static int _XSetWMName(IntPtr display, IntPtr window, ref XTextProperty text_prop);
6559 internal static int XSetWMName(IntPtr display, IntPtr window, ref XTextProperty text_prop)
6561 DebugHelper.TraceWriteLine ("XSetWMName");
6562 return _XSetWMName(display, window, ref text_prop);
6565 [DllImport ("libX11", EntryPoint="XStoreName")]
6566 internal extern static int _XStoreName(IntPtr display, IntPtr window, string window_name);
6567 internal static int XStoreName(IntPtr display, IntPtr window, string window_name)
6569 DebugHelper.TraceWriteLine ("XStoreName");
6570 return _XStoreName(display, window, window_name);
6573 [DllImport ("libX11", EntryPoint="XFetchName")]
6574 internal extern static int _XFetchName(IntPtr display, IntPtr window, ref IntPtr window_name);
6575 internal static int XFetchName(IntPtr display, IntPtr window, ref IntPtr window_name)
6577 DebugHelper.TraceWriteLine ("XFetchName");
6578 return _XFetchName(display, window, ref window_name);
6581 [DllImport ("libX11", EntryPoint="XSendEvent")]
6582 internal extern static int _XSendEvent(IntPtr display, IntPtr window, bool propagate, IntPtr event_mask, ref XEvent send_event);
6583 internal static int XSendEvent(IntPtr display, IntPtr window, bool propagate, IntPtr event_mask, ref XEvent send_event)
6585 DebugHelper.TraceWriteLine ("XSendEvent");
6586 return _XSendEvent(display, window, propagate, event_mask, ref send_event);
6589 [DllImport ("libX11", EntryPoint="XQueryTree")]
6590 internal extern static int _XQueryTree(IntPtr display, IntPtr window, out IntPtr root_return, out IntPtr parent_return, out IntPtr children_return, out int nchildren_return);
6591 internal static int XQueryTree(IntPtr display, IntPtr window, out IntPtr root_return, out IntPtr parent_return, out IntPtr children_return, out int nchildren_return)
6593 DebugHelper.TraceWriteLine ("XQueryTree");
6594 return _XQueryTree(display, window, out root_return, out parent_return, out children_return, out nchildren_return);
6597 [DllImport ("libX11", EntryPoint="XFree")]
6598 internal extern static int _XFree(IntPtr data);
6599 internal static int XFree(IntPtr data)
6601 DebugHelper.TraceWriteLine ("XFree");
6602 return _XFree(data);
6605 [DllImport ("libX11", EntryPoint="XRaiseWindow")]
6606 internal extern static int _XRaiseWindow(IntPtr display, IntPtr window);
6607 internal static int XRaiseWindow(IntPtr display, IntPtr window)
6609 DebugHelper.TraceWriteLine ("XRaiseWindow");
6610 return _XRaiseWindow(display, window);
6613 [DllImport ("libX11", EntryPoint="XLowerWindow")]
6614 internal extern static uint _XLowerWindow(IntPtr display, IntPtr window);
6615 internal static uint XLowerWindow(IntPtr display, IntPtr window)
6617 DebugHelper.TraceWriteLine ("XLowerWindow");
6618 return _XLowerWindow(display, window);
6621 [DllImport ("libX11", EntryPoint="XConfigureWindow")]
6622 internal extern static uint _XConfigureWindow(IntPtr display, IntPtr window, ChangeWindowFlags value_mask, ref XWindowChanges values);
6623 internal static uint XConfigureWindow(IntPtr display, IntPtr window, ChangeWindowFlags value_mask, ref XWindowChanges values)
6625 DebugHelper.TraceWriteLine ("XConfigureWindow");
6626 return _XConfigureWindow(display, window, value_mask, ref values);
6629 [DllImport ("libX11", EntryPoint="XInternAtom")]
6630 internal extern static IntPtr _XInternAtom(IntPtr display, string atom_name, bool only_if_exists);
6631 internal static IntPtr XInternAtom(IntPtr display, string atom_name, bool only_if_exists)
6633 DebugHelper.TraceWriteLine ("XInternAtom");
6634 return _XInternAtom(display, atom_name, only_if_exists);
6637 [DllImport ("libX11", EntryPoint="XInternAtoms")]
6638 internal extern static int _XInternAtoms(IntPtr display, string[] atom_names, int atom_count, bool only_if_exists, IntPtr[] atoms);
6639 internal static int XInternAtoms(IntPtr display, string[] atom_names, int atom_count, bool only_if_exists, IntPtr[] atoms)
6641 DebugHelper.TraceWriteLine ("XInternAtoms");
6642 return _XInternAtoms(display, atom_names, atom_count, only_if_exists, atoms);
6645 [DllImport ("libX11", EntryPoint="XSetWMProtocols")]
6646 internal extern static int _XSetWMProtocols(IntPtr display, IntPtr window, IntPtr[] protocols, int count);
6647 internal static int XSetWMProtocols(IntPtr display, IntPtr window, IntPtr[] protocols, int count)
6649 DebugHelper.TraceWriteLine ("XSetWMProtocols");
6650 return _XSetWMProtocols(display, window, protocols, count);
6653 [DllImport ("libX11", EntryPoint="XGrabPointer")]
6654 internal extern static int _XGrabPointer(IntPtr display, IntPtr window, bool owner_events, EventMask event_mask, GrabMode pointer_mode, GrabMode keyboard_mode, IntPtr confine_to, IntPtr cursor, IntPtr timestamp);
6655 internal static int XGrabPointer(IntPtr display, IntPtr window, bool owner_events, EventMask event_mask, GrabMode pointer_mode, GrabMode keyboard_mode, IntPtr confine_to, IntPtr cursor, IntPtr timestamp)
6657 DebugHelper.TraceWriteLine ("XGrabPointer");
6658 return _XGrabPointer(display, window, owner_events, event_mask, pointer_mode, keyboard_mode, confine_to, cursor, timestamp);
6661 [DllImport ("libX11", EntryPoint="XUngrabPointer")]
6662 internal extern static int _XUngrabPointer(IntPtr display, IntPtr timestamp);
6663 internal static int XUngrabPointer(IntPtr display, IntPtr timestamp)
6665 DebugHelper.TraceWriteLine ("XUngrabPointer");
6666 return _XUngrabPointer(display, timestamp);
6669 [DllImport ("libX11", EntryPoint="XQueryPointer")]
6670 internal extern static bool _XQueryPointer(IntPtr display, IntPtr window, out IntPtr root, out IntPtr child, out int root_x, out int root_y, out int win_x, out int win_y, out int keys_buttons);
6671 internal static bool XQueryPointer(IntPtr display, IntPtr window, out IntPtr root, out IntPtr child, out int root_x, out int root_y, out int win_x, out int win_y, out int keys_buttons)
6673 DebugHelper.TraceWriteLine ("XQueryPointer");
6674 return _XQueryPointer(display, window, out root, out child, out root_x, out root_y, out win_x, out win_y, out keys_buttons);
6677 [DllImport ("libX11", EntryPoint="XTranslateCoordinates")]
6678 internal extern static bool _XTranslateCoordinates (IntPtr display, IntPtr src_w, IntPtr dest_w, int src_x, int src_y, out int intdest_x_return, out int dest_y_return, out IntPtr child_return);
6679 internal static bool XTranslateCoordinates (IntPtr display, IntPtr src_w, IntPtr dest_w, int src_x, int src_y, out int intdest_x_return, out int dest_y_return, out IntPtr child_return)
6681 DebugHelper.TraceWriteLine ("XTranslateCoordinates");
6682 return _XTranslateCoordinates (display, src_w, dest_w, src_x, src_y, out intdest_x_return, out dest_y_return, out child_return);
6685 [DllImport ("libX11", EntryPoint="XGetGeometry")]
6686 internal extern static bool _XGetGeometry(IntPtr display, IntPtr window, out IntPtr root, out int x, out int y, out int width, out int height, out int border_width, out int depth);
6687 internal static bool XGetGeometry(IntPtr display, IntPtr window, out IntPtr root, out int x, out int y, out int width, out int height, out int border_width, out int depth)
6689 DebugHelper.TraceWriteLine ("XGetGeometry");
6690 return _XGetGeometry(display, window, out root, out x, out y, out width, out height, out border_width, out depth);
6693 [DllImport ("libX11", EntryPoint="XGetGeometry")]
6694 internal extern static bool _XGetGeometry(IntPtr display, IntPtr window, IntPtr root, out int x, out int y, out int width, out int height, IntPtr border_width, IntPtr depth);
6695 internal static bool XGetGeometry(IntPtr display, IntPtr window, IntPtr root, out int x, out int y, out int width, out int height, IntPtr border_width, IntPtr depth)
6697 DebugHelper.TraceWriteLine ("XGetGeometry");
6698 return _XGetGeometry(display, window, root, out x, out y, out width, out height, border_width, depth);
6701 [DllImport ("libX11", EntryPoint="XGetGeometry")]
6702 internal extern static bool _XGetGeometry(IntPtr display, IntPtr window, IntPtr root, out int x, out int y, IntPtr width, IntPtr height, IntPtr border_width, IntPtr depth);
6703 internal static bool XGetGeometry(IntPtr display, IntPtr window, IntPtr root, out int x, out int y, IntPtr width, IntPtr height, IntPtr border_width, IntPtr depth)
6705 DebugHelper.TraceWriteLine ("XGetGeometry");
6706 return _XGetGeometry(display, window, root, out x, out y, width, height, border_width, depth);
6709 [DllImport ("libX11", EntryPoint="XGetGeometry")]
6710 internal extern static bool _XGetGeometry(IntPtr display, IntPtr window, IntPtr root, IntPtr x, IntPtr y, out int width, out int height, IntPtr border_width, IntPtr depth);
6711 internal static bool XGetGeometry(IntPtr display, IntPtr window, IntPtr root, IntPtr x, IntPtr y, out int width, out int height, IntPtr border_width, IntPtr depth)
6713 DebugHelper.TraceWriteLine ("XGetGeometry");
6714 return _XGetGeometry(display, window, root, x, y, out width, out height, border_width, depth);
6717 [DllImport ("libX11", EntryPoint="XWarpPointer")]
6718 internal extern static uint _XWarpPointer(IntPtr display, IntPtr src_w, IntPtr dest_w, int src_x, int src_y, uint src_width, uint src_height, int dest_x, int dest_y);
6719 internal static uint XWarpPointer(IntPtr display, IntPtr src_w, IntPtr dest_w, int src_x, int src_y, uint src_width, uint src_height, int dest_x, int dest_y)
6721 DebugHelper.TraceWriteLine ("XWarpPointer");
6722 return _XWarpPointer(display, src_w, dest_w, src_x, src_y, src_width, src_height, dest_x, dest_y);
6725 [DllImport ("libX11", EntryPoint="XClearWindow")]
6726 internal extern static int _XClearWindow(IntPtr display, IntPtr window);
6727 internal static int XClearWindow(IntPtr display, IntPtr window)
6729 DebugHelper.TraceWriteLine ("XClearWindow");
6730 return _XClearWindow(display, window);
6733 [DllImport ("libX11", EntryPoint="XClearArea")]
6734 internal extern static int _XClearArea(IntPtr display, IntPtr window, int x, int y, int width, int height, bool exposures);
6735 internal static int XClearArea(IntPtr display, IntPtr window, int x, int y, int width, int height, bool exposures)
6737 DebugHelper.TraceWriteLine ("XClearArea");
6738 return _XClearArea(display, window, x, y, width, height, exposures);
6742 [DllImport ("libX11", EntryPoint="XDefaultScreenOfDisplay")]
6743 internal extern static IntPtr _XDefaultScreenOfDisplay(IntPtr display);
6744 internal static IntPtr XDefaultScreenOfDisplay(IntPtr display)
6746 DebugHelper.TraceWriteLine ("XDefaultScreenOfDisplay");
6747 return _XDefaultScreenOfDisplay(display);
6750 [DllImport ("libX11", EntryPoint="XScreenNumberOfScreen")]
6751 internal extern static int _XScreenNumberOfScreen(IntPtr display, IntPtr Screen);
6752 internal static int XDefaultScreenOfDisplay(IntPtr display, IntPtr Screen)
6754 DebugHelper.TraceWriteLine ("XDefaultScreenOfDisplay");
6755 return _XScreenNumberOfScreen(display, Screen);
6758 [DllImport ("libX11", EntryPoint="XDefaultVisual")]
6759 internal extern static IntPtr _XDefaultVisual(IntPtr display, int screen_number);
6760 internal static IntPtr XDefaultScreenOfDisplay(IntPtr display, int screen_number)
6762 DebugHelper.TraceWriteLine ("XDefaultScreenOfDisplay");
6763 return _XDefaultVisual(display, screen_number);
6766 [DllImport ("libX11", EntryPoint="XDefaultDepth")]
6767 internal extern static uint _XDefaultDepth(IntPtr display, int screen_number);
6768 internal static uint XDefaultDepth(IntPtr display, int screen_number)
6770 DebugHelper.TraceWriteLine ("XDefaultDepth");
6771 return _XDefaultDepth(display, screen_number);
6774 [DllImport ("libX11", EntryPoint="XDefaultScreen")]
6775 internal extern static int _XDefaultScreen(IntPtr display);
6776 internal static int XDefaultScreen(IntPtr display)
6778 DebugHelper.TraceWriteLine ("XDefaultScreen");
6779 return _XDefaultScreen(display);
6782 [DllImport ("libX11", EntryPoint="XDefaultColormap")]
6783 internal extern static IntPtr _XDefaultColormap(IntPtr display, int screen_number);
6784 internal static IntPtr XDefaultColormap(IntPtr display, int screen_number)
6786 DebugHelper.TraceWriteLine ("XDefaultColormap");
6787 return _XDefaultColormap(display, screen_number);
6790 [DllImport ("libX11", EntryPoint="XLookupColor")]
6791 internal extern static int _XLookupColor(IntPtr display, IntPtr Colormap, string Coloranem, ref XColor exact_def_color, ref XColor screen_def_color);
6792 internal static int XLookupColor(IntPtr display, IntPtr Colormap, string Coloranem, ref XColor exact_def_color, ref XColor screen_def_color)
6794 DebugHelper.TraceWriteLine ("XLookupColor");
6795 return _XLookupColor(display, Colormap, Coloranem, ref exact_def_color, ref screen_def_color);
6798 [DllImport ("libX11", EntryPoint="XAllocColor")]
6799 internal extern static int _XAllocColor(IntPtr display, IntPtr Colormap, ref XColor colorcell_def);
6800 internal static int XAllocColor(IntPtr display, IntPtr Colormap, ref XColor colorcell_def)
6802 DebugHelper.TraceWriteLine ("XAllocColor");
6803 return _XAllocColor(display, Colormap, ref colorcell_def);
6806 [DllImport ("libX11", EntryPoint="XSetTransientForHint")]
6807 internal extern static int _XSetTransientForHint(IntPtr display, IntPtr window, IntPtr prop_window);
6808 internal static int XSetTransientForHint(IntPtr display, IntPtr window, IntPtr prop_window)
6810 DebugHelper.TraceWriteLine ("XSetTransientForHint");
6811 return _XSetTransientForHint(display, window, prop_window);
6814 [DllImport ("libX11", EntryPoint="XChangeProperty")]
6815 internal extern static int _XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref MotifWmHints data, int nelements);
6816 internal static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref MotifWmHints data, int nelements)
6818 DebugHelper.TraceWriteLine ("XChangeProperty");
6819 return _XChangeProperty(display, window, property, type, format, mode, ref data, nelements);
6822 [DllImport ("libX11", EntryPoint="XChangeProperty")]
6823 internal extern static int _XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref uint value, int nelements);
6824 internal static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref uint value, int nelements)
6826 DebugHelper.TraceWriteLine ("XChangeProperty");
6827 return _XChangeProperty(display, window, property, type, format, mode, ref value, nelements);
6830 [DllImport ("libX11", EntryPoint="XChangeProperty")]
6831 internal extern static int _XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref IntPtr value, int nelements);
6832 internal static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref IntPtr value, int nelements)
6834 DebugHelper.TraceWriteLine ("XChangeProperty");
6835 return _XChangeProperty(display, window, property, type, format, mode, ref value, nelements);
6838 [DllImport ("libX11", EntryPoint="XChangeProperty")]
6839 internal extern static int _XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, uint[] data, int nelements);
6840 internal static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, uint[] data, int nelements)
6842 DebugHelper.TraceWriteLine ("XChangeProperty");
6843 return _XChangeProperty(display, window, property, type, format, mode, data, nelements);
6846 [DllImport ("libX11", EntryPoint="XChangeProperty")]
6847 internal extern static int _XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, int[] data, int nelements);
6848 internal static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, int[] data, int nelements)
6850 DebugHelper.TraceWriteLine ("XChangeProperty");
6851 return _XChangeProperty(display, window, property, type, format, mode, data, nelements);
6854 [DllImport ("libX11", EntryPoint="XChangeProperty")]
6855 internal extern static int _XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, IntPtr[] data, int nelements);
6856 internal static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, IntPtr[] data, int nelements)
6858 DebugHelper.TraceWriteLine ("XChangeProperty");
6859 return _XChangeProperty(display, window, property, type, format, mode, data, nelements);
6862 [DllImport ("libX11", EntryPoint="XChangeProperty")]
6863 internal extern static int _XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, IntPtr atoms, int nelements);
6864 internal static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, IntPtr atoms, int nelements)
6866 DebugHelper.TraceWriteLine ("XChangeProperty");
6867 return _XChangeProperty(display, window, property, type, format, mode, atoms, nelements);
6870 [DllImport ("libX11", EntryPoint="XChangeProperty", CharSet=CharSet.Ansi)]
6871 internal extern static int _XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, string text, int text_length);
6872 internal static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, string text, int text_length)
6874 DebugHelper.TraceWriteLine ("XChangeProperty");
6875 return _XChangeProperty(display, window, property, type, format, mode, text, text_length);
6878 [DllImport ("libX11", EntryPoint="XDeleteProperty")]
6879 internal extern static int _XDeleteProperty(IntPtr display, IntPtr window, IntPtr property);
6880 internal static int XDeleteProperty(IntPtr display, IntPtr window, IntPtr property)
6882 DebugHelper.TraceWriteLine ("XDeleteProperty");
6883 return _XDeleteProperty(display, window, property);
6887 [DllImport ("libX11", EntryPoint="XCreateGC")]
6888 internal extern static IntPtr _XCreateGC(IntPtr display, IntPtr window, IntPtr valuemask, ref XGCValues values);
6889 internal static IntPtr XCreateGC(IntPtr display, IntPtr window, IntPtr valuemask, ref XGCValues values)
6891 DebugHelper.TraceWriteLine ("XCreateGC");
6892 return _XCreateGC(display, window, valuemask, ref values);
6895 [DllImport ("libX11", EntryPoint="XFreeGC")]
6896 internal extern static int _XFreeGC(IntPtr display, IntPtr gc);
6897 internal static int XFreeGC(IntPtr display, IntPtr gc)
6899 DebugHelper.TraceWriteLine ("XFreeGC");
6900 return _XFreeGC(display, gc);
6903 [DllImport ("libX11", EntryPoint="XSetFunction")]
6904 internal extern static int _XSetFunction(IntPtr display, IntPtr gc, GXFunction function);
6905 internal static int XSetFunction(IntPtr display, IntPtr gc, GXFunction function)
6907 DebugHelper.TraceWriteLine ("XSetFunction");
6908 return _XSetFunction(display, gc, function);
6911 [DllImport ("libX11", EntryPoint="XSetLineAttributes")]
6912 internal extern static int _XSetLineAttributes(IntPtr display, IntPtr gc, int line_width, GCLineStyle line_style, GCCapStyle cap_style, GCJoinStyle join_style);
6913 internal static int XSetLineAttributes(IntPtr display, IntPtr gc, int line_width, GCLineStyle line_style, GCCapStyle cap_style, GCJoinStyle join_style)
6915 DebugHelper.TraceWriteLine ("XSetLineAttributes");
6916 return _XSetLineAttributes(display, gc, line_width, line_style, cap_style, join_style);
6919 [DllImport ("libX11", EntryPoint="XDrawLine")]
6920 internal extern static int _XDrawLine(IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int x2, int y2);
6921 internal static int XDrawLine(IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int x2, int y2)
6923 DebugHelper.TraceWriteLine ("XDrawLine");
6924 return _XDrawLine(display, drawable, gc, x1, y1, x2, y2);
6927 [DllImport ("libX11", EntryPoint="XDrawRectangle")]
6928 internal extern static int _XDrawRectangle(IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int width, int height);
6929 internal static int XDrawRectangle(IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int width, int height)
6931 DebugHelper.TraceWriteLine ("XDrawRectangle");
6932 return _XDrawRectangle(display, drawable, gc, x1, y1, width, height);
6935 [DllImport ("libX11", EntryPoint="XFillRectangle")]
6936 internal extern static int _XFillRectangle(IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int width, int height);
6937 internal static int XFillRectangle(IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int width, int height)
6939 DebugHelper.TraceWriteLine ("XFillRectangle");
6940 return _XFillRectangle(display, drawable, gc, x1, y1, width, height);
6943 [DllImport ("libX11", EntryPoint="XSetWindowBackground")]
6944 internal extern static int _XSetWindowBackground(IntPtr display, IntPtr window, IntPtr background);
6945 internal static int XSetWindowBackground(IntPtr display, IntPtr window, IntPtr background)
6947 DebugHelper.TraceWriteLine ("XSetWindowBackground");
6948 return _XSetWindowBackground(display, window, background);
6951 [DllImport ("libX11", EntryPoint="XCopyArea")]
6952 internal extern static int _XCopyArea(IntPtr display, IntPtr src, IntPtr dest, IntPtr gc, int src_x, int src_y, int width, int height, int dest_x, int dest_y);
6953 internal static int XCopyArea(IntPtr display, IntPtr src, IntPtr dest, IntPtr gc, int src_x, int src_y, int width, int height, int dest_x, int dest_y)
6955 DebugHelper.TraceWriteLine ("XCopyArea");
6956 return _XCopyArea(display, src, dest, gc, src_x, src_y, width, height, dest_x, dest_y);
6959 [DllImport ("libX11", EntryPoint="XGetWindowProperty")]
6960 internal extern static int _XGetWindowProperty(IntPtr display, IntPtr window, IntPtr atom, IntPtr long_offset, IntPtr long_length, bool delete, IntPtr req_type, out IntPtr actual_type, out int actual_format, out IntPtr nitems, out IntPtr bytes_after, ref IntPtr prop);
6961 internal static int XGetWindowProperty(IntPtr display, IntPtr window, IntPtr atom, IntPtr long_offset, IntPtr long_length, bool delete, IntPtr req_type, out IntPtr actual_type, out int actual_format, out IntPtr nitems, out IntPtr bytes_after, ref IntPtr prop)
6963 DebugHelper.TraceWriteLine ("XGetWindowProperty");
6964 return _XGetWindowProperty(display, window, atom, long_offset, long_length, delete, req_type, out actual_type, out actual_format, out nitems, out bytes_after, ref prop);
6967 [DllImport ("libX11", EntryPoint="XSetInputFocus")]
6968 internal extern static int _XSetInputFocus(IntPtr display, IntPtr window, RevertTo revert_to, IntPtr time);
6969 internal static int XSetInputFocus(IntPtr display, IntPtr window, RevertTo revert_to, IntPtr time)
6971 DebugHelper.TraceWriteLine ("XSetInputFocus");
6972 return _XSetInputFocus(display, window, revert_to, time);
6975 [DllImport ("libX11", EntryPoint="XIconifyWindow")]
6976 internal extern static int _XIconifyWindow(IntPtr display, IntPtr window, int screen_number);
6977 internal static int XIconifyWindow(IntPtr display, IntPtr window, int screen_number)
6979 DebugHelper.TraceWriteLine ("XIconifyWindow");
6980 return _XIconifyWindow(display, window, screen_number);
6983 [DllImport ("libX11", EntryPoint="XDefineCursor")]
6984 internal extern static int _XDefineCursor(IntPtr display, IntPtr window, IntPtr cursor);
6985 internal static int XDefineCursor(IntPtr display, IntPtr window, IntPtr cursor)
6987 DebugHelper.TraceWriteLine ("XDefineCursor");
6988 return _XDefineCursor(display, window, cursor);
6991 [DllImport ("libX11", EntryPoint="XUndefineCursor")]
6992 internal extern static int _XUndefineCursor(IntPtr display, IntPtr window);
6993 internal static int XUndefineCursor(IntPtr display, IntPtr window)
6995 DebugHelper.TraceWriteLine ("XUndefineCursor");
6996 return _XUndefineCursor(display, window);
6999 [DllImport ("libX11", EntryPoint="XFreeCursor")]
7000 internal extern static int _XFreeCursor(IntPtr display, IntPtr cursor);
7001 internal static int XFreeCursor(IntPtr display, IntPtr cursor)
7003 DebugHelper.TraceWriteLine ("XFreeCursor");
7004 return _XFreeCursor(display, cursor);
7007 [DllImport ("libX11", EntryPoint="XCreateFontCursor")]
7008 internal extern static IntPtr _XCreateFontCursor(IntPtr display, CursorFontShape shape);
7009 internal static IntPtr XCreateFontCursor(IntPtr display, CursorFontShape shape)
7011 DebugHelper.TraceWriteLine ("XCreateFontCursor");
7012 return _XCreateFontCursor(display, shape);
7015 [DllImport ("libX11", EntryPoint="XCreatePixmapCursor")]
7016 internal extern static IntPtr _XCreatePixmapCursor(IntPtr display, IntPtr source, IntPtr mask, ref XColor foreground_color, ref XColor background_color, int x_hot, int y_hot);
7017 internal static IntPtr XCreatePixmapCursor(IntPtr display, IntPtr source, IntPtr mask, ref XColor foreground_color, ref XColor background_color, int x_hot, int y_hot)
7019 DebugHelper.TraceWriteLine ("XCreatePixmapCursor");
7020 return _XCreatePixmapCursor(display, source, mask, ref foreground_color, ref background_color, x_hot, y_hot);
7023 [DllImport ("libX11", EntryPoint="XCreatePixmapFromBitmapData")]
7024 internal extern static IntPtr _XCreatePixmapFromBitmapData(IntPtr display, IntPtr drawable, byte[] data, int width, int height, IntPtr fg, IntPtr bg, int depth);
7025 internal static IntPtr XCreatePixmapFromBitmapData(IntPtr display, IntPtr drawable, byte[] data, int width, int height, IntPtr fg, IntPtr bg, int depth)
7027 DebugHelper.TraceWriteLine ("XCreatePixmapFromBitmapData");
7028 return _XCreatePixmapFromBitmapData(display, drawable, data, width, height, fg, bg, depth);
7031 [DllImport ("libX11", EntryPoint="XCreatePixmap")]
7032 internal extern static IntPtr _XCreatePixmap(IntPtr display, IntPtr d, int width, int height, int depth);
7033 internal static IntPtr XCreatePixmap(IntPtr display, IntPtr d, int width, int height, int depth)
7035 DebugHelper.TraceWriteLine ("XCreatePixmap");
7036 return _XCreatePixmap(display, d, width, height, depth);
7039 [DllImport ("libX11", EntryPoint="XFreePixmap")]
7040 internal extern static IntPtr _XFreePixmap(IntPtr display, IntPtr pixmap);
7041 internal static IntPtr XFreePixmap(IntPtr display, IntPtr pixmap)
7043 DebugHelper.TraceWriteLine ("XFreePixmap");
7044 return _XFreePixmap(display, pixmap);
7047 [DllImport ("libX11", EntryPoint="XQueryBestCursor")]
7048 internal extern static int _XQueryBestCursor(IntPtr display, IntPtr drawable, int width, int height, out int best_width, out int best_height);
7049 internal static int XQueryBestCursor(IntPtr display, IntPtr drawable, int width, int height, out int best_width, out int best_height)
7051 DebugHelper.TraceWriteLine ("XQueryBestCursor");
7052 return _XQueryBestCursor(display, drawable, width, height, out best_width, out best_height);
7055 [DllImport ("libX11", EntryPoint="XQueryExtension")]
7056 internal extern static int _XQueryExtension(IntPtr display, string extension_name, ref int major, ref int first_event, ref int first_error);
7057 internal static int XQueryExtension(IntPtr display, string extension_name, ref int major, ref int first_event, ref int first_error)
7059 DebugHelper.TraceWriteLine ("XQueryExtension");
7060 return _XQueryExtension(display, extension_name, ref major, ref first_event, ref first_error);
7063 [DllImport ("libX11", EntryPoint="XWhitePixel")]
7064 internal extern static IntPtr _XWhitePixel(IntPtr display, int screen_no);
7065 internal static IntPtr XWhitePixel(IntPtr display, int screen_no)
7067 DebugHelper.TraceWriteLine ("XWhitePixel");
7068 return _XWhitePixel(display, screen_no);
7071 [DllImport ("libX11", EntryPoint="XBlackPixel")]
7072 internal extern static IntPtr _XBlackPixel(IntPtr display, int screen_no);
7073 internal static IntPtr XBlackPixel(IntPtr display, int screen_no)
7075 DebugHelper.TraceWriteLine ("XBlackPixel");
7076 return _XBlackPixel(display, screen_no);
7079 [DllImport ("libX11", EntryPoint="XGrabServer")]
7080 internal extern static void _XGrabServer(IntPtr display);
7081 internal static void XGrabServer(IntPtr display)
7083 DebugHelper.TraceWriteLine ("XGrabServer");
7084 _XGrabServer(display);
7087 [DllImport ("libX11", EntryPoint="XUngrabServer")]
7088 internal extern static void _XUngrabServer(IntPtr display);
7089 internal static void XUngrabServer(IntPtr display)
7091 DebugHelper.TraceWriteLine ("XUngrabServer");
7092 _XUngrabServer(display);
7095 [DllImport ("libX11", EntryPoint="XGetWMNormalHints")]
7096 internal extern static void _XGetWMNormalHints(IntPtr display, IntPtr window, ref XSizeHints hints, out IntPtr supplied_return);
7097 internal static void XGetWMNormalHints(IntPtr display, IntPtr window, ref XSizeHints hints, out IntPtr supplied_return)
7099 DebugHelper.TraceWriteLine ("XGetWMNormalHints");
7100 _XGetWMNormalHints(display, window, ref hints, out supplied_return);
7103 [DllImport ("libX11", EntryPoint="XSetWMNormalHints")]
7104 internal extern static void _XSetWMNormalHints(IntPtr display, IntPtr window, ref XSizeHints hints);
7105 internal static void XSetWMNormalHints(IntPtr display, IntPtr window, ref XSizeHints hints)
7107 DebugHelper.TraceWriteLine ("XSetWMNormalHints");
7108 _XSetWMNormalHints(display, window, ref hints);
7111 [DllImport ("libX11", EntryPoint="XSetZoomHints")]
7112 internal extern static void _XSetZoomHints(IntPtr display, IntPtr window, ref XSizeHints hints);
7113 internal static void XSetZoomHints(IntPtr display, IntPtr window, ref XSizeHints hints)
7115 DebugHelper.TraceWriteLine ("XSetZoomHints");
7116 _XSetZoomHints(display, window, ref hints);
7119 [DllImport ("libX11", EntryPoint="XSetWMHints")]
7120 internal extern static void _XSetWMHints(IntPtr display, IntPtr window, ref XWMHints wmhints);
7121 internal static void XSetWMHints(IntPtr display, IntPtr window, ref XWMHints wmhints)
7123 DebugHelper.TraceWriteLine ("XSetWMHints");
7124 _XSetWMHints(display, window, ref wmhints);
7127 [DllImport ("libX11", EntryPoint="XGetIconSizes")]
7128 internal extern static int _XGetIconSizes(IntPtr display, IntPtr window, out IntPtr size_list, out int count);
7129 internal static int XGetIconSizes(IntPtr display, IntPtr window, out IntPtr size_list, out int count)
7131 DebugHelper.TraceWriteLine ("XGetIconSizes");
7132 return _XGetIconSizes(display, window, out size_list, out count);
7135 [DllImport ("libX11", EntryPoint="XSetErrorHandler")]
7136 internal extern static IntPtr _XSetErrorHandler(XErrorHandler error_handler);
7137 internal static IntPtr XSetErrorHandler(XErrorHandler error_handler)
7139 DebugHelper.TraceWriteLine ("XSetErrorHandler");
7140 return _XSetErrorHandler(error_handler);
7143 [DllImport ("libX11", EntryPoint="XGetErrorText")]
7144 internal extern static IntPtr _XGetErrorText(IntPtr display, byte code, StringBuilder buffer, int length);
7145 internal static IntPtr XGetErrorText(IntPtr display, byte code, StringBuilder buffer, int length)
7147 DebugHelper.TraceWriteLine ("XGetErrorText");
7148 return _XGetErrorText(display, code, buffer, length);
7151 [DllImport ("libX11", EntryPoint="XInitThreads")]
7152 internal extern static int _XInitThreads();
7153 internal static int XInitThreads()
7155 DebugHelper.TraceWriteLine ("XInitThreads");
7156 return _XInitThreads();
7159 [DllImport ("libX11", EntryPoint="XConvertSelection")]
7160 internal extern static int _XConvertSelection(IntPtr display, IntPtr selection, IntPtr target, IntPtr property, IntPtr requestor, IntPtr time);
7161 internal static int XConvertSelection(IntPtr display, IntPtr selection, IntPtr target, IntPtr property, IntPtr requestor, IntPtr time)
7163 DebugHelper.TraceWriteLine ("XConvertSelection");
7164 return _XConvertSelection(display, selection, target, property, requestor, time);
7167 [DllImport ("libX11", EntryPoint="XGetSelectionOwner")]
7168 internal extern static IntPtr _XGetSelectionOwner(IntPtr display, IntPtr selection);
7169 internal static IntPtr XGetSelectionOwner(IntPtr display, IntPtr selection)
7171 DebugHelper.TraceWriteLine ("XGetSelectionOwner");
7172 return _XGetSelectionOwner(display, selection);
7175 [DllImport ("libX11", EntryPoint="XSetSelectionOwner")]
7176 internal extern static int _XSetSelectionOwner(IntPtr display, IntPtr selection, IntPtr owner, IntPtr time);
7177 internal static int XSetSelectionOwner(IntPtr display, IntPtr selection, IntPtr owner, IntPtr time)
7179 DebugHelper.TraceWriteLine ("XSetSelectionOwner");
7180 return _XSetSelectionOwner(display, selection, owner, time);
7183 [DllImport ("libX11", EntryPoint="XSetPlaneMask")]
7184 internal extern static int _XSetPlaneMask(IntPtr display, IntPtr gc, IntPtr mask);
7185 internal static int XSetPlaneMask(IntPtr display, IntPtr gc, IntPtr mask)
7187 DebugHelper.TraceWriteLine ("XSetPlaneMask");
7188 return _XSetPlaneMask(display, gc, mask);
7191 [DllImport ("libX11", EntryPoint="XSetForeground")]
7192 internal extern static int _XSetForeground(IntPtr display, IntPtr gc, UIntPtr foreground);
7193 internal static int XSetForeground(IntPtr display, IntPtr gc, UIntPtr foreground)
7195 DebugHelper.TraceWriteLine ("XSetForeground");
7196 return _XSetForeground(display, gc, foreground);
7199 [DllImport ("libX11", EntryPoint="XSetBackground")]
7200 internal extern static int _XSetBackground(IntPtr display, IntPtr gc, UIntPtr background);
7201 internal static int XSetBackground(IntPtr display, IntPtr gc, UIntPtr background)
7203 DebugHelper.TraceWriteLine ("XSetBackground");
7204 return _XSetBackground(display, gc, background);
7207 [DllImport ("libX11", EntryPoint="XBell")]
7208 internal extern static int _XBell(IntPtr display, int percent);
7209 internal static int XBell(IntPtr display, int percent)
7211 DebugHelper.TraceWriteLine ("XBell");
7212 return _XBell(display, percent);
7215 [DllImport ("libX11", EntryPoint="XChangeActivePointerGrab")]
7216 internal extern static int _XChangeActivePointerGrab (IntPtr display, EventMask event_mask, IntPtr cursor, IntPtr time);
7217 internal static int XChangeActivePointerGrab (IntPtr display, EventMask event_mask, IntPtr cursor, IntPtr time)
7219 DebugHelper.TraceWriteLine ("XChangeActivePointerGrab");
7220 return _XChangeActivePointerGrab (display, event_mask, cursor, time);
7223 [DllImport ("libX11", EntryPoint="XFilterEvent")]
7224 internal extern static bool _XFilterEvent(ref XEvent xevent, IntPtr window);
7225 internal static bool XFilterEvent(ref XEvent xevent, IntPtr window)
7227 DebugHelper.TraceWriteLine ("XFilterEvent");
7228 return _XFilterEvent(ref xevent, window);
7231 [DllImport ("libX11", EntryPoint="XkbSetDetectableAutoRepeat")]
7232 internal extern static void _XkbSetDetectableAutoRepeat (IntPtr display, bool detectable, IntPtr supported);
7233 internal static void XkbSetDetectableAutoRepeat (IntPtr display, bool detectable, IntPtr supported)
7235 DebugHelper.TraceWriteLine ("XkbSetDetectableAutoRepeat");
7236 _XkbSetDetectableAutoRepeat (display, detectable, supported);
7239 [DllImport ("libX11", EntryPoint="XPeekEvent")]
7240 internal extern static void _XPeekEvent (IntPtr display, ref XEvent xevent);
7241 internal static void XPeekEvent (IntPtr display, ref XEvent xevent)
7243 DebugHelper.TraceWriteLine ("XPeekEvent");
7244 _XPeekEvent (display, ref xevent);
7247 [DllImport ("libX11", EntryPoint="XIfEvent")]
7248 internal extern static void _XIfEvent (IntPtr display, ref XEvent xevent, Delegate event_predicate, IntPtr arg);
7249 internal static void XIfEvent (IntPtr display, ref XEvent xevent, Delegate event_predicate, IntPtr arg)
7251 DebugHelper.TraceWriteLine ("XIfEvent");
7252 _XIfEvent (display, ref xevent, event_predicate, arg);
7256 #region Xinerama imports
7257 [DllImport ("libXinerama", EntryPoint="XineramaQueryScreens")]
7258 extern static IntPtr _XineramaQueryScreens (IntPtr display, out int number);
7259 internal static IntPtr XineramaQueryScreens (IntPtr display, out int number)
7261 DebugHelper.TraceWriteLine ("XineramaQueryScreens");
7262 return _XineramaQueryScreens (display, out number);
7265 [DllImport ("libXinerama", EntryPoint="XineramaIsActive")]
7266 extern static bool _XineramaIsActive (IntPtr display);
7267 static bool XineramaNotInstalled;
7269 internal static bool XineramaIsActive (IntPtr display)
7271 DebugHelper.TraceWriteLine ("XineramaIsActive");
7273 if (XineramaNotInstalled)
7276 return _XineramaIsActive (display);
7277 } catch (DllNotFoundException) {
7278 // Xinerama isn't installed
7279 XineramaNotInstalled = true;
7285 #else //no TRACE defined
7287 #region Xcursor imports
7288 [DllImport ("libXcursor", EntryPoint = "XcursorLibraryLoadCursor")]
7289 internal extern static IntPtr XcursorLibraryLoadCursor (IntPtr display, [MarshalAs (UnmanagedType.LPStr)] string name);
7291 [DllImport ("libXcursor", EntryPoint = "XcursorLibraryLoadImages")]
7292 internal extern static IntPtr XcursorLibraryLoadImages ([MarshalAs (UnmanagedType.LPStr)] string file, IntPtr theme, int size);
7294 [DllImport ("libXcursor", EntryPoint = "XcursorImagesDestroy")]
7295 internal extern static void XcursorImagesDestroy (IntPtr images);
7297 [DllImport ("libXcursor", EntryPoint = "XcursorGetDefaultSize")]
7298 internal extern static int XcursorGetDefaultSize (IntPtr display);
7300 [DllImport ("libXcursor", EntryPoint = "XcursorImageLoadCursor")]
7301 internal extern static IntPtr XcursorImageLoadCursor (IntPtr display, IntPtr image);
7303 [DllImport ("libXcursor", EntryPoint = "XcursorGetTheme")]
7304 internal extern static IntPtr XcursorGetTheme (IntPtr display);
7307 [DllImport ("libX11", EntryPoint="XOpenDisplay")]
7308 internal extern static IntPtr XOpenDisplay(IntPtr display);
7309 [DllImport ("libX11", EntryPoint="XCloseDisplay")]
7310 internal extern static int XCloseDisplay(IntPtr display);
7311 [DllImport ("libX11", EntryPoint="XSynchronize")]
7312 internal extern static IntPtr XSynchronize(IntPtr display, bool onoff);
7314 [DllImport ("libX11", EntryPoint="XCreateWindow")]
7315 internal extern static IntPtr XCreateWindow(IntPtr display, IntPtr parent, int x, int y, int width, int height, int border_width, int depth, int xclass, IntPtr visual, UIntPtr valuemask, ref XSetWindowAttributes attributes);
7317 [DllImport ("libX11", EntryPoint="XCreateSimpleWindow")]
7318 internal extern static IntPtr XCreateSimpleWindow(IntPtr display, IntPtr parent, int x, int y, int width, int height, int border_width, UIntPtr border, UIntPtr background);
7320 [DllImport ("libX11", EntryPoint="XMapWindow")]
7321 internal extern static int XMapWindow(IntPtr display, IntPtr window);
7323 [DllImport ("libX11", EntryPoint="XMapRaised")]
7324 internal extern static int XMapRaised(IntPtr display, IntPtr window);
7326 [DllImport ("libX11", EntryPoint="XUnmapWindow")]
7327 internal extern static int XUnmapWindow(IntPtr display, IntPtr window);
7329 [DllImport ("libX11", EntryPoint="XMapSubwindows")]
7330 internal extern static int XMapSubindows(IntPtr display, IntPtr window);
7332 [DllImport ("libX11", EntryPoint="XUnmapSubwindows")]
7333 internal extern static int XUnmapSubwindows(IntPtr display, IntPtr window);
7335 [DllImport ("libX11", EntryPoint="XRootWindow")]
7336 internal extern static IntPtr XRootWindow(IntPtr display, int screen_number);
7338 [DllImport ("libX11", EntryPoint="XNextEvent")]
7339 internal extern static IntPtr XNextEvent(IntPtr display, ref XEvent xevent);
7341 [DllImport ("libX11", EntryPoint="XConnectionNumber")]
7342 internal extern static int XConnectionNumber (IntPtr display);
7344 [DllImport ("libX11", EntryPoint="XPending")]
7345 internal extern static int XPending (IntPtr display);
7347 [DllImport ("libX11", EntryPoint="XSelectInput")]
7348 internal extern static IntPtr XSelectInput(IntPtr display, IntPtr window, IntPtr mask);
7350 [DllImport ("libX11", EntryPoint="XDestroyWindow")]
7351 internal extern static int XDestroyWindow(IntPtr display, IntPtr window);
7353 [DllImport ("libX11", EntryPoint="XReparentWindow")]
7354 internal extern static int XReparentWindow(IntPtr display, IntPtr window, IntPtr parent, int x, int y);
7356 [DllImport ("libX11", EntryPoint="XMoveResizeWindow")]
7357 extern static int XMoveResizeWindow(IntPtr display, IntPtr window, int x, int y, int width, int height);
7358 internal static int MoveResizeWindow(IntPtr display, IntPtr window, int x, int y, int width, int height)
7360 int ret = XMoveResizeWindow (display, window, x, y, width, height);
7361 Keyboard.MoveCurrentCaretPos ();
7365 [DllImport ("libX11", EntryPoint="XResizeWindow")]
7366 internal extern static int XResizeWindow(IntPtr display, IntPtr window, int width, int height);
7368 [DllImport ("libX11", EntryPoint="XGetWindowAttributes")]
7369 internal extern static int XGetWindowAttributes(IntPtr display, IntPtr window, ref XWindowAttributes attributes);
7371 [DllImport ("libX11", EntryPoint="XFlush")]
7372 internal extern static int XFlush(IntPtr display);
7374 [DllImport ("libX11", EntryPoint="XSetWMName")]
7375 internal extern static int XSetWMName(IntPtr display, IntPtr window, ref XTextProperty text_prop);
7377 [DllImport ("libX11", EntryPoint="XStoreName")]
7378 internal extern static int XStoreName(IntPtr display, IntPtr window, string window_name);
7380 [DllImport ("libX11", EntryPoint="XFetchName")]
7381 internal extern static int XFetchName(IntPtr display, IntPtr window, ref IntPtr window_name);
7383 [DllImport ("libX11", EntryPoint="XSendEvent")]
7384 internal extern static int XSendEvent(IntPtr display, IntPtr window, bool propagate, IntPtr event_mask, ref XEvent send_event);
7386 [DllImport ("libX11", EntryPoint="XQueryTree")]
7387 internal extern static int XQueryTree(IntPtr display, IntPtr window, out IntPtr root_return, out IntPtr parent_return, out IntPtr children_return, out int nchildren_return);
7389 [DllImport ("libX11", EntryPoint="XFree")]
7390 internal extern static int XFree(IntPtr data);
7392 [DllImport ("libX11", EntryPoint="XRaiseWindow")]
7393 internal extern static int XRaiseWindow(IntPtr display, IntPtr window);
7395 [DllImport ("libX11", EntryPoint="XLowerWindow")]
7396 internal extern static uint XLowerWindow(IntPtr display, IntPtr window);
7398 [DllImport ("libX11", EntryPoint="XConfigureWindow")]
7399 internal extern static uint XConfigureWindow(IntPtr display, IntPtr window, ChangeWindowFlags value_mask, ref XWindowChanges values);
7401 [DllImport ("libX11", EntryPoint="XInternAtom")]
7402 internal extern static IntPtr XInternAtom(IntPtr display, string atom_name, bool only_if_exists);
7404 [DllImport ("libX11", EntryPoint="XInternAtoms")]
7405 internal extern static int XInternAtoms(IntPtr display, string[] atom_names, int atom_count, bool only_if_exists, IntPtr[] atoms);
7407 [DllImport ("libX11", EntryPoint="XSetWMProtocols")]
7408 internal extern static int XSetWMProtocols(IntPtr display, IntPtr window, IntPtr[] protocols, int count);
7410 [DllImport ("libX11", EntryPoint="XGrabPointer")]
7411 internal extern static int XGrabPointer(IntPtr display, IntPtr window, bool owner_events, EventMask event_mask, GrabMode pointer_mode, GrabMode keyboard_mode, IntPtr confine_to, IntPtr cursor, IntPtr timestamp);
7413 [DllImport ("libX11", EntryPoint="XUngrabPointer")]
7414 internal extern static int XUngrabPointer(IntPtr display, IntPtr timestamp);
7416 [DllImport ("libX11", EntryPoint="XQueryPointer")]
7417 internal extern static bool XQueryPointer(IntPtr display, IntPtr window, out IntPtr root, out IntPtr child, out int root_x, out int root_y, out int win_x, out int win_y, out int keys_buttons);
7419 [DllImport ("libX11", EntryPoint="XTranslateCoordinates")]
7420 internal extern static bool XTranslateCoordinates (IntPtr display, IntPtr src_w, IntPtr dest_w, int src_x, int src_y, out int intdest_x_return, out int dest_y_return, out IntPtr child_return);
7422 [DllImport ("libX11", EntryPoint="XGetGeometry")]
7423 internal extern static bool XGetGeometry(IntPtr display, IntPtr window, out IntPtr root, out int x, out int y, out int width, out int height, out int border_width, out int depth);
7425 [DllImport ("libX11", EntryPoint="XGetGeometry")]
7426 internal extern static bool XGetGeometry(IntPtr display, IntPtr window, IntPtr root, out int x, out int y, out int width, out int height, IntPtr border_width, IntPtr depth);
7428 [DllImport ("libX11", EntryPoint="XGetGeometry")]
7429 internal extern static bool XGetGeometry(IntPtr display, IntPtr window, IntPtr root, out int x, out int y, IntPtr width, IntPtr height, IntPtr border_width, IntPtr depth);
7431 [DllImport ("libX11", EntryPoint="XGetGeometry")]
7432 internal extern static bool XGetGeometry(IntPtr display, IntPtr window, IntPtr root, IntPtr x, IntPtr y, out int width, out int height, IntPtr border_width, IntPtr depth);
7434 [DllImport ("libX11", EntryPoint="XWarpPointer")]
7435 internal extern static uint XWarpPointer(IntPtr display, IntPtr src_w, IntPtr dest_w, int src_x, int src_y, uint src_width, uint src_height, int dest_x, int dest_y);
7437 [DllImport ("libX11", EntryPoint="XClearWindow")]
7438 internal extern static int XClearWindow(IntPtr display, IntPtr window);
7440 [DllImport ("libX11", EntryPoint="XClearArea")]
7441 internal extern static int XClearArea(IntPtr display, IntPtr window, int x, int y, int width, int height, bool exposures);
7444 [DllImport ("libX11", EntryPoint="XDefaultScreenOfDisplay")]
7445 internal extern static IntPtr XDefaultScreenOfDisplay(IntPtr display);
7447 [DllImport ("libX11", EntryPoint="XScreenNumberOfScreen")]
7448 internal extern static int XScreenNumberOfScreen(IntPtr display, IntPtr Screen);
7450 [DllImport ("libX11", EntryPoint="XDefaultVisual")]
7451 internal extern static IntPtr XDefaultVisual(IntPtr display, int screen_number);
7453 [DllImport ("libX11", EntryPoint="XDefaultDepth")]
7454 internal extern static uint XDefaultDepth(IntPtr display, int screen_number);
7456 [DllImport ("libX11", EntryPoint="XDefaultScreen")]
7457 internal extern static int XDefaultScreen(IntPtr display);
7459 [DllImport ("libX11", EntryPoint="XDefaultColormap")]
7460 internal extern static IntPtr XDefaultColormap(IntPtr display, int screen_number);
7462 [DllImport ("libX11", EntryPoint="XLookupColor")]
7463 internal extern static int XLookupColor(IntPtr display, IntPtr Colormap, string Coloranem, ref XColor exact_def_color, ref XColor screen_def_color);
7465 [DllImport ("libX11", EntryPoint="XAllocColor")]
7466 internal extern static int XAllocColor(IntPtr display, IntPtr Colormap, ref XColor colorcell_def);
7468 [DllImport ("libX11", EntryPoint="XSetTransientForHint")]
7469 internal extern static int XSetTransientForHint(IntPtr display, IntPtr window, IntPtr prop_window);
7471 [DllImport ("libX11", EntryPoint="XChangeProperty")]
7472 internal extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref MotifWmHints data, int nelements);
7474 [DllImport ("libX11", EntryPoint="XChangeProperty")]
7475 internal extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref uint value, int nelements);
7477 [DllImport ("libX11", EntryPoint="XChangeProperty")]
7478 internal extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref IntPtr value, int nelements);
7480 [DllImport ("libX11", EntryPoint="XChangeProperty")]
7481 internal extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, uint[] data, int nelements);
7483 [DllImport ("libX11", EntryPoint="XChangeProperty")]
7484 internal extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, int[] data, int nelements);
7486 [DllImport ("libX11", EntryPoint="XChangeProperty")]
7487 internal extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, IntPtr[] data, int nelements);
7489 [DllImport ("libX11", EntryPoint="XChangeProperty")]
7490 internal extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, IntPtr atoms, int nelements);
7492 [DllImport ("libX11", EntryPoint="XChangeProperty", CharSet=CharSet.Ansi)]
7493 internal extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, string text, int text_length);
7495 [DllImport ("libX11", EntryPoint="XDeleteProperty")]
7496 internal extern static int XDeleteProperty(IntPtr display, IntPtr window, IntPtr property);
7499 [DllImport ("libX11", EntryPoint="XCreateGC")]
7500 internal extern static IntPtr XCreateGC(IntPtr display, IntPtr window, IntPtr valuemask, ref XGCValues values);
7502 [DllImport ("libX11", EntryPoint="XFreeGC")]
7503 internal extern static int XFreeGC(IntPtr display, IntPtr gc);
7505 [DllImport ("libX11", EntryPoint="XSetFunction")]
7506 internal extern static int XSetFunction(IntPtr display, IntPtr gc, GXFunction function);
7508 [DllImport ("libX11", EntryPoint="XSetLineAttributes")]
7509 internal extern static int XSetLineAttributes(IntPtr display, IntPtr gc, int line_width, GCLineStyle line_style, GCCapStyle cap_style, GCJoinStyle join_style);
7511 [DllImport ("libX11", EntryPoint="XDrawLine")]
7512 internal extern static int XDrawLine(IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int x2, int y2);
7514 [DllImport ("libX11", EntryPoint="XDrawRectangle")]
7515 internal extern static int XDrawRectangle(IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int width, int height);
7517 [DllImport ("libX11", EntryPoint="XFillRectangle")]
7518 internal extern static int XFillRectangle(IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int width, int height);
7520 [DllImport ("libX11", EntryPoint="XSetWindowBackground")]
7521 internal extern static int XSetWindowBackground(IntPtr display, IntPtr window, IntPtr background);
7523 [DllImport ("libX11", EntryPoint="XCopyArea")]
7524 internal extern static int XCopyArea(IntPtr display, IntPtr src, IntPtr dest, IntPtr gc, int src_x, int src_y, int width, int height, int dest_x, int dest_y);
7526 [DllImport ("libX11", EntryPoint="XGetWindowProperty")]
7527 internal extern static int XGetWindowProperty(IntPtr display, IntPtr window, IntPtr atom, IntPtr long_offset, IntPtr long_length, bool delete, IntPtr req_type, out IntPtr actual_type, out int actual_format, out IntPtr nitems, out IntPtr bytes_after, ref IntPtr prop);
7529 [DllImport ("libX11", EntryPoint="XSetInputFocus")]
7530 internal extern static int XSetInputFocus(IntPtr display, IntPtr window, RevertTo revert_to, IntPtr time);
7532 [DllImport ("libX11", EntryPoint="XIconifyWindow")]
7533 internal extern static int XIconifyWindow(IntPtr display, IntPtr window, int screen_number);
7535 [DllImport ("libX11", EntryPoint="XDefineCursor")]
7536 internal extern static int XDefineCursor(IntPtr display, IntPtr window, IntPtr cursor);
7538 [DllImport ("libX11", EntryPoint="XUndefineCursor")]
7539 internal extern static int XUndefineCursor(IntPtr display, IntPtr window);
7541 [DllImport ("libX11", EntryPoint="XFreeCursor")]
7542 internal extern static int XFreeCursor(IntPtr display, IntPtr cursor);
7544 [DllImport ("libX11", EntryPoint="XCreateFontCursor")]
7545 internal extern static IntPtr XCreateFontCursor(IntPtr display, CursorFontShape shape);
7547 [DllImport ("libX11", EntryPoint="XCreatePixmapCursor")]
7548 internal extern static IntPtr XCreatePixmapCursor(IntPtr display, IntPtr source, IntPtr mask, ref XColor foreground_color, ref XColor background_color, int x_hot, int y_hot);
7550 [DllImport ("libX11", EntryPoint="XCreatePixmapFromBitmapData")]
7551 internal extern static IntPtr XCreatePixmapFromBitmapData(IntPtr display, IntPtr drawable, byte[] data, int width, int height, IntPtr fg, IntPtr bg, int depth);
7553 [DllImport ("libX11", EntryPoint="XCreatePixmap")]
7554 internal extern static IntPtr XCreatePixmap(IntPtr display, IntPtr d, int width, int height, int depth);
7556 [DllImport ("libX11", EntryPoint="XFreePixmap")]
7557 internal extern static IntPtr XFreePixmap(IntPtr display, IntPtr pixmap);
7559 [DllImport ("libX11", EntryPoint="XQueryBestCursor")]
7560 internal extern static int XQueryBestCursor(IntPtr display, IntPtr drawable, int width, int height, out int best_width, out int best_height);
7562 [DllImport ("libX11", EntryPoint="XQueryExtension")]
7563 internal extern static int XQueryExtension(IntPtr display, string extension_name, ref int major, ref int first_event, ref int first_error);
7565 [DllImport ("libX11", EntryPoint="XWhitePixel")]
7566 internal extern static IntPtr XWhitePixel(IntPtr display, int screen_no);
7568 [DllImport ("libX11", EntryPoint="XBlackPixel")]
7569 internal extern static IntPtr XBlackPixel(IntPtr display, int screen_no);
7571 [DllImport ("libX11", EntryPoint="XGrabServer")]
7572 internal extern static void XGrabServer(IntPtr display);
7574 [DllImport ("libX11", EntryPoint="XUngrabServer")]
7575 internal extern static void XUngrabServer(IntPtr display);
7577 [DllImport ("libX11", EntryPoint="XGetWMNormalHints")]
7578 internal extern static void XGetWMNormalHints(IntPtr display, IntPtr window, ref XSizeHints hints, out IntPtr supplied_return);
7580 [DllImport ("libX11", EntryPoint="XSetWMNormalHints")]
7581 internal extern static void XSetWMNormalHints(IntPtr display, IntPtr window, ref XSizeHints hints);
7583 [DllImport ("libX11", EntryPoint="XSetZoomHints")]
7584 internal extern static void XSetZoomHints(IntPtr display, IntPtr window, ref XSizeHints hints);
7586 [DllImport ("libX11", EntryPoint="XSetWMHints")]
7587 internal extern static void XSetWMHints(IntPtr display, IntPtr window, ref XWMHints wmhints);
7589 [DllImport ("libX11", EntryPoint="XGetIconSizes")]
7590 internal extern static int XGetIconSizes(IntPtr display, IntPtr window, out IntPtr size_list, out int count);
7592 [DllImport ("libX11", EntryPoint="XSetErrorHandler")]
7593 internal extern static IntPtr XSetErrorHandler(XErrorHandler error_handler);
7595 [DllImport ("libX11", EntryPoint="XGetErrorText")]
7596 internal extern static IntPtr XGetErrorText(IntPtr display, byte code, StringBuilder buffer, int length);
7598 [DllImport ("libX11", EntryPoint="XInitThreads")]
7599 internal extern static int XInitThreads();
7601 [DllImport ("libX11", EntryPoint="XConvertSelection")]
7602 internal extern static int XConvertSelection(IntPtr display, IntPtr selection, IntPtr target, IntPtr property, IntPtr requestor, IntPtr time);
7604 [DllImport ("libX11", EntryPoint="XGetSelectionOwner")]
7605 internal extern static IntPtr XGetSelectionOwner(IntPtr display, IntPtr selection);
7607 [DllImport ("libX11", EntryPoint="XSetSelectionOwner")]
7608 internal extern static int XSetSelectionOwner(IntPtr display, IntPtr selection, IntPtr owner, IntPtr time);
7610 [DllImport ("libX11", EntryPoint="XSetPlaneMask")]
7611 internal extern static int XSetPlaneMask(IntPtr display, IntPtr gc, IntPtr mask);
7613 [DllImport ("libX11", EntryPoint="XSetForeground")]
7614 internal extern static int XSetForeground(IntPtr display, IntPtr gc, UIntPtr foreground);
7616 [DllImport ("libX11", EntryPoint="XSetBackground")]
7617 internal extern static int XSetBackground(IntPtr display, IntPtr gc, UIntPtr background);
7619 [DllImport ("libX11", EntryPoint="XBell")]
7620 internal extern static int XBell(IntPtr display, int percent);
7622 [DllImport ("libX11", EntryPoint="XChangeActivePointerGrab")]
7623 internal extern static int XChangeActivePointerGrab (IntPtr display, EventMask event_mask, IntPtr cursor, IntPtr time);
7625 [DllImport ("libX11", EntryPoint="XFilterEvent")]
7626 internal extern static bool XFilterEvent(ref XEvent xevent, IntPtr window);
7628 [DllImport ("libX11", EntryPoint="XkbSetDetectableAutoRepeat")]
7629 internal extern static void XkbSetDetectableAutoRepeat (IntPtr display, bool detectable, IntPtr supported);
7631 [DllImport ("libX11", EntryPoint="XPeekEvent")]
7632 internal extern static void XPeekEvent (IntPtr display, ref XEvent xevent);
7634 [DllImport ("libX11", EntryPoint="XIfEvent")]
7635 internal extern static void XIfEvent (IntPtr display, ref XEvent xevent, Delegate event_predicate, IntPtr arg);
7637 [DllImport ("libX11", EntryPoint="XGetInputFocus")]
7638 internal extern static void XGetInputFocus (IntPtr display, out IntPtr focus, out IntPtr revert_to);
7640 #region Gtk/Gdk imports
7641 [DllImport("libgdk-x11-2.0")]
7642 internal extern static IntPtr gdk_atom_intern (string atomName, bool onlyIfExists);
7644 [DllImport("libgtk-x11-2.0")]
7645 internal extern static IntPtr gtk_clipboard_get (IntPtr atom);
7647 [DllImport("libgtk-x11-2.0")]
7648 internal extern static void gtk_clipboard_store (IntPtr clipboard);
7650 [DllImport("libgtk-x11-2.0")]
7651 internal extern static void gtk_clipboard_set_text (IntPtr clipboard, string text, int len);
7655 #region Xinerama imports
7656 [DllImport ("libXinerama")]
7657 internal extern static IntPtr XineramaQueryScreens (IntPtr display, out int number);
7659 [DllImport ("libXinerama", EntryPoint = "XineramaIsActive")]
7660 extern static bool _XineramaIsActive (IntPtr display);
7661 static bool XineramaNotInstalled;
7663 internal static bool XineramaIsActive (IntPtr display)
7665 if (XineramaNotInstalled)
7668 return _XineramaIsActive (display);
7669 } catch (DllNotFoundException) {
7670 // Xinerama isn't installed
7671 XineramaNotInstalled = true;