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-2005 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
46 using System.ComponentModel;
47 using System.Collections;
48 using System.Diagnostics;
50 using System.Drawing.Drawing2D;
51 using System.Drawing.Imaging;
55 using System.Net.Sockets;
56 using System.Reflection;
57 using System.Runtime.InteropServices;
59 using System.Threading;
61 // Only do the poll when building with mono for now
63 using Mono.Unix.Native;
67 namespace System.Windows.Forms {
68 internal class XplatUIX11 : XplatUIDriver {
69 #region Local Variables
71 static volatile XplatUIX11 Instance;
72 private static int RefCount;
73 private static object XlibLock; // Our locking object
74 private static bool ThemesEnabled;
77 private static IntPtr DisplayHandle; // X11 handle to display
78 private static int ScreenNo; // Screen number used
79 private static IntPtr DefaultColormap; // Colormap for screen
80 private static IntPtr CustomVisual; // Visual for window creation
81 private static IntPtr CustomColormap; // Colormap for window creation
82 private static IntPtr RootWindow; // Handle of the root window for the screen/display
83 private static IntPtr FosterParent; // Container to hold child windows until their parent exists
84 private static XErrorHandler ErrorHandler; // Error handler delegate
85 private static bool ErrorExceptions; // Throw exceptions on X errors
88 private static IntPtr ClipMagic = new IntPtr(27051977);
89 private static ClipboardStruct Clipboard; // Our clipboard
92 private static int PostAtom; // PostMessage atom
93 private static int AsyncAtom; // Support for async messages
96 private static bool GetMessageResult; // Value returned by GetMessage()
97 private static XEventQueue MessageQueue; // Holds our queued up events
99 private static Pollfd[] pollfds; // For watching the X11 socket
101 private static X11Keyboard Keyboard; //
102 private static X11Dnd Dnd;
103 private static Socket listen; //
104 private static Socket wake; //
105 private static Socket wake_receive; //
106 private static byte[] network_buffer; //
110 private static IntPtr ActiveWindow; // Handle of the active window
111 private static IntPtr FocusWindow; // Handle of the window with keyboard focus (if any)
114 private static Stack ModalWindows; // Stack of our modal windows
117 private static IntPtr SystrayMgrWindow; // Handle of the Systray Manager window
120 private static IntPtr LastCursorWindow; // The last window we set the cursor on
121 private static IntPtr LastCursorHandle; // The handle that was last set on LastCursorWindow
122 private static IntPtr OverrideCursorHandle; // The cursor that is set to override any other cursors
125 private static CaretStruct Caret; //
127 // Support for Window Styles
128 private static int[] NetAtoms; // All atoms we know
130 // mouse hover message generation
131 private static HoverStruct HoverState; //
133 // double click message generation
134 private static ClickStruct ClickPending; //
136 // Support for mouse grab
137 private static GrabStruct Grab; //
140 private static Point MousePosition; // Last position of mouse, in screen coords
141 private static MouseButtons MouseState; // Last state of mouse buttons
144 private static ArrayList TimerList; // Holds SWF.Timers
147 private static int DoubleClickInterval; // msec; max interval between clicks to count as double click
149 const EventMask SelectInputMask = EventMask.ButtonPressMask |
150 EventMask.ButtonReleaseMask |
151 EventMask.KeyPressMask |
152 EventMask.KeyReleaseMask |
153 EventMask.EnterWindowMask |
154 EventMask.LeaveWindowMask |
155 EventMask.ExposureMask |
156 EventMask.FocusChangeMask |
157 EventMask.PointerMotionMask |
158 EventMask.VisibilityChangeMask |
159 EventMask.SubstructureNotifyMask |
160 EventMask.StructureNotifyMask;
162 static readonly object lockobj = new object ();
164 #endregion // Local Variables
167 private XplatUIX11() {
168 // Handle singleton stuff first
171 // Now regular initialization
172 XlibLock = new object ();
173 MessageQueue = new XEventQueue ();
174 TimerList = new ArrayList ();
177 ErrorExceptions = false;
179 // X11 Initialization
180 SetDisplay(XOpenDisplay(IntPtr.Zero));
181 X11DesktopColors.Initialize();
183 // Handle any upcoming errors; we re-set it here, X11DesktopColor stuff might have stolen it (gtk does)
184 ErrorHandler = new XErrorHandler(HandleError);
185 XSetErrorHandler(ErrorHandler);
187 #endregion // Constructors
189 #region Singleton Specific Code
190 public static XplatUIX11 GetInstance() {
192 if (Instance == null) {
193 Instance=new XplatUIX11();
200 public int Reference {
207 #region Internal Properties
208 internal static IntPtr Display {
210 return DisplayHandle;
214 XplatUIX11.GetInstance().SetDisplay(value);
218 internal static int Screen {
228 internal static IntPtr RootWindowHandle {
238 internal static IntPtr Visual {
244 CustomVisual = value;
248 internal static IntPtr ColorMap {
250 return CustomColormap;
254 CustomColormap = value;
259 #region XExceptionClass
260 internal class XException : ApplicationException {
264 XRequest RequestCode;
268 public XException(IntPtr Display, IntPtr ResourceID, IntPtr Serial, byte ErrorCode, XRequest RequestCode, byte MinorCode) {
269 this.Display = Display;
270 this.ResourceID = ResourceID;
271 this.Serial = Serial;
272 this.RequestCode = RequestCode;
273 this.ErrorCode = ErrorCode;
274 this.MinorCode = MinorCode;
277 public override string Message {
279 return GetMessage(Display, ResourceID, Serial, ErrorCode, RequestCode, MinorCode);
283 public static string GetMessage(IntPtr Display, IntPtr ResourceID, IntPtr Serial, byte ErrorCode, XRequest RequestCode, byte MinorCode) {
288 sb = new StringBuilder(160);
289 XGetErrorText(Display, ErrorCode, sb, sb.Capacity);
290 x_error_text = sb.ToString();
292 error = String.Format("\n Error: {0}\n Request: {1:D} ({2})\n Resource ID: 0x{3:x}\n Serial: {4}", x_error_text, RequestCode, RequestCode, ResourceID.ToInt32(), Serial);
296 #endregion // XExceptionClass
298 #region Internal Methods
299 internal void SetDisplay(IntPtr display_handle) {
300 if (display_handle != IntPtr.Zero) {
303 if ((DisplayHandle != IntPtr.Zero) && (FosterParent != IntPtr.Zero)) {
304 hwnd = Hwnd.ObjectFromHandle(FosterParent);
305 XDestroyWindow(DisplayHandle, FosterParent);
309 if (DisplayHandle != IntPtr.Zero) {
310 XCloseDisplay(DisplayHandle);
313 DisplayHandle=display_handle;
315 // We need to tell System.Drawing our DisplayHandle. FromHdcInternal has
316 // been hacked to do this for us.
317 Graphics.FromHdcInternal (DisplayHandle);
320 if (Environment.GetEnvironmentVariable ("MONO_XSYNC") != null) {
321 XSynchronize(DisplayHandle, true);
324 if (Environment.GetEnvironmentVariable ("MONO_XEXCEPTIONS") != null) {
325 ErrorExceptions = true;
330 RootWindow = XRootWindow(DisplayHandle, ScreenNo);
331 DefaultColormap = XDefaultColormap(DisplayHandle, ScreenNo);
333 // Create the foster parent
334 FosterParent=XCreateSimpleWindow(DisplayHandle, RootWindow, 0, 0, 1, 1, 4, 0, 0);
335 if (FosterParent==IntPtr.Zero) {
336 Console.WriteLine("XplatUIX11 Constructor failed to create FosterParent");
340 hwnd.WholeWindow = FosterParent;
341 hwnd.ClientWindow = FosterParent;
343 // For sleeping on the X11 socket
344 listen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
345 IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 0);
349 // To wake up when a timer is ready
350 network_buffer = new byte[10];
352 wake = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
353 wake.Connect(listen.LocalEndPoint);
354 wake_receive = listen.Accept();
357 pollfds = new Pollfd [2];
358 pollfds [0] = new Pollfd ();
359 pollfds [0].fd = XConnectionNumber (DisplayHandle);
360 pollfds [0].events = PollEvents.POLLIN;
362 pollfds [1] = new Pollfd ();
363 pollfds [1].fd = wake_receive.Handle.ToInt32 ();
364 pollfds [1].events = PollEvents.POLLIN;
367 Keyboard = new X11Keyboard(DisplayHandle);
368 Dnd = new X11Dnd (DisplayHandle);
370 GetMessageResult = true;
372 DoubleClickInterval = 500;
374 HoverState.Interval = 500;
375 HoverState.Timer = new Timer();
376 HoverState.Timer.Enabled = false;
377 HoverState.Timer.Interval = HoverState.Interval;
378 HoverState.Timer.Tick +=new EventHandler(MouseHover);
382 ActiveWindow = IntPtr.Zero;
383 FocusWindow = IntPtr.Zero;
384 ModalWindows = new Stack(3);
386 MouseState = MouseButtons.None;
387 MousePosition = new Point(0, 0);
389 Caret.Timer = new Timer();
390 Caret.Timer.Interval = 500; // FIXME - where should this number come from?
391 Caret.Timer.Tick += new EventHandler(CaretCallback);
395 // Grab atom changes off the root window to catch certain WM events
396 XSelectInput(DisplayHandle, RootWindow, EventMask.PropertyChangeMask);
398 // Handle any upcoming errors
399 ErrorHandler = new XErrorHandler(HandleError);
400 XSetErrorHandler(ErrorHandler);
402 throw new ArgumentNullException("Display", "Could not open display (X-Server required. Check you DISPLAY environment variable)");
406 internal static void Where() {
407 Console.WriteLine("Here: {0}\n", WhereString());
410 internal static string WhereString() {
418 newline = String.Format("{0}\t {1} ", Environment.NewLine, Locale.GetText("at"));
419 unknown = Locale.GetText("<unknown method>");
420 sb = new StringBuilder();
421 stack = new StackTrace(true);
423 for (int i = 0; i < stack.FrameCount; i++) {
424 frame = stack.GetFrame(i);
427 method = frame.GetMethod();
428 if (method != null) {
430 sb.AppendFormat(frame.ToString());
432 if (frame.GetFileLineNumber() != 0) {
433 sb.AppendFormat("{0}.{1} () [{2}:{3}]", method.DeclaringType.FullName, method.Name, Path.GetFileName(frame.GetFileName()), frame.GetFileLineNumber());
435 sb.AppendFormat("{0}.{1} ()", method.DeclaringType.FullName, method.Name);
441 return sb.ToString();
443 #endregion // Internal Methods
445 #region Private Methods
446 private static void SetupAtoms() {
447 NetAtoms = new int[(int)NA.LAST_NET_ATOM];
449 NetAtoms[(int)NA.WM_PROTOCOLS] = XInternAtom(DisplayHandle, "WM_PROTOCOLS", false);
450 NetAtoms[(int)NA.WM_DELETE_WINDOW] = XInternAtom(DisplayHandle, "WM_DELETE_WINDOW", false);
451 NetAtoms[(int)NA.WM_TAKE_FOCUS] = XInternAtom(DisplayHandle, "WM_TAKE_FOCUS", false);
453 NetAtoms[(int)NA._NET_SUPPORTED] = XInternAtom(DisplayHandle, "_NET_SUPPORTED", false);
454 NetAtoms[(int)NA._NET_CLIENT_LIST] = XInternAtom(DisplayHandle, "_NET_CLIENT_LIST", false);
455 NetAtoms[(int)NA._NET_NUMBER_OF_DESKTOPS] = XInternAtom(DisplayHandle, "_NET_NUMBER_OF_DESKTOPS", false);
456 NetAtoms[(int)NA._NET_DESKTOP_GEOMETRY] = XInternAtom(DisplayHandle, "_NET_DESKTOP_GEOMETRY", false);
457 NetAtoms[(int)NA._NET_DESKTOP_VIEWPORT] = XInternAtom(DisplayHandle, "_NET_DESKTOP_VIEWPORT", false);
458 NetAtoms[(int)NA._NET_CURRENT_DESKTOP] = XInternAtom(DisplayHandle, "_NET_CURRENT_DESKTOP", false);
459 NetAtoms[(int)NA._NET_DESKTOP_NAMES] = XInternAtom(DisplayHandle, "_NET_DESKTOP_NAMES", false);
460 NetAtoms[(int)NA._NET_ACTIVE_WINDOW] = XInternAtom(DisplayHandle, "_NET_ACTIVE_WINDOW", false);
461 NetAtoms[(int)NA._NET_WORKAREA] = XInternAtom(DisplayHandle, "_NET_WORKAREA", false);
462 NetAtoms[(int)NA._NET_SUPPORTING_WM_CHECK] = XInternAtom(DisplayHandle, "_NET_SUPPORTING_WM_CHECK", false);
463 NetAtoms[(int)NA._NET_VIRTUAL_ROOTS] = XInternAtom(DisplayHandle, "_NET_VIRTUAL_ROOTS", false);
464 NetAtoms[(int)NA._NET_DESKTOP_LAYOUT] = XInternAtom(DisplayHandle, "_NET_DESKTOP_LAYOUT", false);
465 NetAtoms[(int)NA._NET_SHOWING_DESKTOP] = XInternAtom(DisplayHandle, "_NET_SHOWING_DESKTOP", false);
467 NetAtoms[(int)NA._NET_CLOSE_WINDOW] = XInternAtom(DisplayHandle, "_NET_CLOSE_WINDOW", false);
468 NetAtoms[(int)NA._NET_MOVERESIZE_WINDOW] = XInternAtom(DisplayHandle, "_NET_MOVERESIZE_WINDOW", false);
469 NetAtoms[(int)NA._NET_WM_MOVERESIZE] = XInternAtom(DisplayHandle, "_NET_WM_MOVERESIZE", false);
470 NetAtoms[(int)NA._NET_RESTACK_WINDOW] = XInternAtom(DisplayHandle, "_NET_RESTACK_WINDOW", false);
471 NetAtoms[(int)NA._NET_REQUEST_FRAME_EXTENTS] = XInternAtom(DisplayHandle, "_NET_REQUEST_FRAME_EXTENTS", false);
473 NetAtoms[(int)NA._NET_WM_NAME] = XInternAtom(DisplayHandle, "_NET_WM_NAME", false);
474 NetAtoms[(int)NA._NET_WM_VISIBLE_NAME] = XInternAtom(DisplayHandle, "_NET_WM_VISIBLE_NAME", false);
475 NetAtoms[(int)NA._NET_WM_ICON_NAME] = XInternAtom(DisplayHandle, "_NET_WM_ICON_NAME", false);
476 NetAtoms[(int)NA._NET_WM_VISIBLE_ICON_NAME] = XInternAtom(DisplayHandle, "_NET_WM_VISIBLE_ICON_NAME", false);
477 NetAtoms[(int)NA._NET_WM_DESKTOP] = XInternAtom(DisplayHandle, "_NET_WM_DESKTOP", false);
478 NetAtoms[(int)NA._NET_WM_WINDOW_TYPE] = XInternAtom(DisplayHandle, "_NET_WM_WINDOW_TYPE", false);
479 NetAtoms[(int)NA._NET_WM_STATE] = XInternAtom(DisplayHandle, "_NET_WM_STATE", false);
480 NetAtoms[(int)NA._NET_WM_ALLOWED_ACTIONS] = XInternAtom(DisplayHandle, "_NET_WM_ALLOWED_ACTIONS", false);
481 NetAtoms[(int)NA._NET_WM_STRUT] = XInternAtom(DisplayHandle, "_NET_WM_STRUT", false);
482 NetAtoms[(int)NA._NET_WM_STRUT_PARTIAL] = XInternAtom(DisplayHandle, "_NET_WM_STRUT_PARTIAL", false);
483 NetAtoms[(int)NA._NET_WM_ICON_GEOMETRY] = XInternAtom(DisplayHandle, "_NET_WM_ICON_GEOMETRY", false);
484 NetAtoms[(int)NA._NET_WM_ICON] = XInternAtom(DisplayHandle, "_NET_WM_ICON", false);
485 NetAtoms[(int)NA._NET_WM_PID] = XInternAtom(DisplayHandle, "_NET_WM_PID", false);
486 NetAtoms[(int)NA._NET_WM_HANDLED_ICONS] = XInternAtom(DisplayHandle, "_NET_WM_HANDLED_ICONS", false);
487 NetAtoms[(int)NA._NET_WM_USER_TIME] = XInternAtom(DisplayHandle, "_NET_WM_USER_TIME", false);
488 NetAtoms[(int)NA._NET_FRAME_EXTENTS] = XInternAtom(DisplayHandle, "_NET_FRAME_EXTENTS", false);
490 NetAtoms[(int)NA._NET_WM_PING] = XInternAtom(DisplayHandle, "_NET_WM_PING", false);
491 NetAtoms[(int)NA._NET_WM_SYNC_REQUEST] = XInternAtom(DisplayHandle, "_NET_WM_SYNC_REQUEST", false);
493 NetAtoms[(int)NA._NET_SYSTEM_TRAY_S] = XInternAtom(DisplayHandle, "_NET_SYSTEM_TRAY_S" + ScreenNo.ToString(), false);
494 NetAtoms[(int)NA._NET_SYSTEM_TRAY_OPCODE] = XInternAtom(DisplayHandle, "_NET_SYSTEM_TRAY_OPCODE", false);
495 NetAtoms[(int)NA._NET_SYSTEM_TRAY_ORIENTATION] = XInternAtom(DisplayHandle, "_NET_SYSTEM_TRAY_ORIENTATION", false);
497 NetAtoms[(int)NA._NET_WM_STATE_MAXIMIZED_HORZ] = XInternAtom(DisplayHandle, "_NET_WM_STATE_MAXIMIZED_HORZ", false);
498 NetAtoms[(int)NA._NET_WM_STATE_MAXIMIZED_VERT] = XInternAtom(DisplayHandle, "_NET_WM_STATE_MAXIMIZED_VERT", false);
500 NetAtoms[(int)NA._XEMBED] = XInternAtom(DisplayHandle, "_XEMBED", false);
501 NetAtoms[(int)NA._XEMBED_INFO] = XInternAtom(DisplayHandle, "_XEMBED_INFO", false);
503 NetAtoms[(int)NA._MOTIF_WM_HINTS] = XInternAtom(DisplayHandle, "_MOTIF_WM_HINTS", false);
505 NetAtoms[(int)NA._NET_WM_STATE_NO_TASKBAR] = XInternAtom(DisplayHandle, "_NET_WM_STATE_NO_TASKBAR", false);
506 NetAtoms[(int)NA._NET_WM_STATE_ABOVE] = XInternAtom(DisplayHandle, "_NET_WM_STATE_ABOVE", false);
507 NetAtoms[(int)NA._NET_WM_STATE_MODAL] = XInternAtom(DisplayHandle, "_NET_WM_STATE_MODAL", false);
508 NetAtoms[(int)NA._NET_WM_CONTEXT_HELP] = XInternAtom(DisplayHandle, "_NET_WM_CONTEXT_HELP", false);
511 //NetAtoms[(int)NA.CLIPBOARD] = XInternAtom(DisplayHandle, "CLIPBOARD", false);
512 NetAtoms[(int)NA.CLIPBOARD] = (int)Atom.XA_PRIMARY;
513 NetAtoms[(int)NA.DIB] = (int)Atom.XA_PIXMAP;
514 NetAtoms[(int)NA.OEMTEXT] = XInternAtom(DisplayHandle, "COMPOUND_TEXT", false);
515 NetAtoms[(int)NA.UNICODETEXT] = XInternAtom(DisplayHandle, "UTF8_STRING", false);
516 NetAtoms[(int)NA.TARGETS] = XInternAtom(DisplayHandle, "TARGETS", false);
519 AsyncAtom = XInternAtom(DisplayHandle, "_SWF_AsyncAtom", false);
520 PostAtom = XInternAtom (DisplayHandle, "_SWF_PostMessageAtom", false);
521 HoverState.Atom = XInternAtom(DisplayHandle, "_SWF_HoverAtom", false);
524 private void GetSystrayManagerWindow() {
525 XGrabServer(DisplayHandle);
526 SystrayMgrWindow = XGetSelectionOwner(DisplayHandle, NetAtoms[(int)NA._NET_SYSTEM_TRAY_S]);
527 XUngrabServer(DisplayHandle);
528 XFlush(DisplayHandle);
531 private void SendNetWMMessage(IntPtr window, IntPtr message_type, IntPtr l0, IntPtr l1, IntPtr l2) {
535 xev.ClientMessageEvent.type = XEventName.ClientMessage;
536 xev.ClientMessageEvent.send_event = true;
537 xev.ClientMessageEvent.window = window;
538 xev.ClientMessageEvent.message_type = message_type;
539 xev.ClientMessageEvent.format = 32;
540 xev.ClientMessageEvent.ptr1 = l0;
541 xev.ClientMessageEvent.ptr2 = l1;
542 xev.ClientMessageEvent.ptr3 = l2;
543 XSendEvent(DisplayHandle, RootWindow, false, EventMask.SubstructureRedirectMask | EventMask.SubstructureNotifyMask, ref xev);
546 private void SendNetClientMessage(IntPtr window, IntPtr message_type, IntPtr l0, IntPtr l1, IntPtr l2) {
550 xev.ClientMessageEvent.type = XEventName.ClientMessage;
551 xev.ClientMessageEvent.send_event = true;
552 xev.ClientMessageEvent.window = window;
553 xev.ClientMessageEvent.message_type = message_type;
554 xev.ClientMessageEvent.format = 32;
555 xev.ClientMessageEvent.ptr1 = l0;
556 xev.ClientMessageEvent.ptr2 = l1;
557 xev.ClientMessageEvent.ptr3 = l2;
558 XSendEvent(DisplayHandle, window, false, EventMask.NoEventMask, ref xev);
562 private void DeriveStyles(IntPtr handle, int Style, int ExStyle, out FormBorderStyle border_style, out TitleStyle title_style) {
565 control = Control.FromHandle(handle);
567 if ( !(control is Form)) {
568 if (control != null) {
569 border_style = (FormBorderStyle)control.border_style;
571 border_style = FormBorderStyle.None;
573 title_style = TitleStyle.None;
575 title_style = TitleStyle.None;
576 if ((Style & (int)WindowStyles.WS_CAPTION) != 0) {
577 if ((ExStyle & (int)WindowStyles.WS_EX_TOOLWINDOW) != 0) {
578 title_style = TitleStyle.Tool;
580 title_style = TitleStyle.Normal;
584 border_style = FormBorderStyle.None;
585 if ((ExStyle & (int)WindowStyles.WS_EX_WINDOWEDGE) != 0) {
586 if ((ExStyle & (int)WindowStyles.WS_EX_TOOLWINDOW) != 0) {
587 if ((Style & (int)WindowStyles.WS_THICKFRAME) != 0) {
588 border_style = FormBorderStyle.SizableToolWindow;
590 border_style = FormBorderStyle.FixedToolWindow;
592 } else if ((ExStyle & (int)WindowStyles.WS_EX_DLGMODALFRAME) != 0) {
593 border_style = FormBorderStyle.FixedDialog;
594 } else if ((ExStyle & (int)WindowStyles.WS_THICKFRAME) != 0) {
595 border_style = FormBorderStyle.Sizable;
597 border_style = FormBorderStyle.FixedSingle;
600 border_style = FormBorderStyle.Fixed3D;
605 private void SetHwndStyles(Hwnd hwnd, CreateParams cp) {
606 DeriveStyles(hwnd.Handle, cp.Style, cp.ExStyle, out hwnd.border_style, out hwnd.title_style);
609 private void SetWMStyles(Hwnd hwnd, CreateParams cp) {
610 MotifWmHints mwmHints;
611 MotifFunctions functions;
612 MotifDecorations decorations;
615 Rectangle client_rect;
617 mwmHints = new MotifWmHints();
621 mwmHints.flags = (IntPtr)(MotifFlags.Functions | MotifFlags.Decorations);
622 mwmHints.functions = (IntPtr)0;
623 mwmHints.decorations = (IntPtr)0;
625 if ((cp.Style & (int)WindowStyles.WS_CAPTION) != 0) {
626 functions |= MotifFunctions.Move;
627 decorations |= MotifDecorations.Title | MotifDecorations.Menu;
630 if ((cp.Style & ((int)WindowStyles.WS_THICKFRAME)) != 0) {
631 functions |= MotifFunctions.Move | MotifFunctions.Resize;
632 decorations |= MotifDecorations.Border | MotifDecorations.ResizeH;
635 if ((cp.Style & ((int)WindowStyles.WS_MINIMIZEBOX)) != 0) {
636 functions |= MotifFunctions.Minimize;
637 decorations |= MotifDecorations.Minimize;
640 if ((cp.Style & ((int)WindowStyles.WS_MAXIMIZEBOX)) != 0) {
641 functions |= MotifFunctions.Maximize;
642 decorations |= MotifDecorations.Maximize;
645 if ((cp.Style & ((int)WindowStyles.WS_SYSMENU)) != 0) {
646 functions |= MotifFunctions.Close;
649 if ((cp.ExStyle & ((int)WindowStyles.WS_EX_DLGMODALFRAME)) != 0) {
650 decorations |= MotifDecorations.Border;
653 if ((cp.Style & ((int)WindowStyles.WS_DLGFRAME)) != 0) {
654 decorations |= MotifDecorations.Border;
657 if ((cp.Style & ((int)WindowStyles.WS_BORDER)) != 0) {
658 decorations |= MotifDecorations.Border;
661 if ((cp.ExStyle & ((int)WindowStyles.WS_EX_TOOLWINDOW)) != 0) {
666 mwmHints.functions = (IntPtr)functions;
667 mwmHints.decorations = (IntPtr)decorations;
670 client_rect = hwnd.ClientRect;
672 XChangeProperty(DisplayHandle, hwnd.whole_window, NetAtoms[(int)NA._MOTIF_WM_HINTS], NetAtoms[(int)NA._MOTIF_WM_HINTS], 32, PropertyMode.Replace, ref mwmHints, 5);
674 if (((cp.Style & (int)WindowStyles.WS_POPUP) != 0) && (hwnd.parent != null) && (hwnd.parent.whole_window != IntPtr.Zero)) {
675 XSetTransientForHint(DisplayHandle, hwnd.whole_window, hwnd.parent.whole_window);
677 XMoveResizeWindow(DisplayHandle, hwnd.client_window, client_rect.X, client_rect.Y, client_rect.Width, client_rect.Height);
682 if ((cp.ExStyle & ((int)WindowStyles.WS_EX_TOOLWINDOW)) != 0) {
683 atoms[atom_count++] = (uint)NetAtoms[(int)NA._NET_WM_STATE_ABOVE];
684 atoms[atom_count++] = (uint)NetAtoms[(int)NA._NET_WM_STATE_NO_TASKBAR];
687 XChangeProperty(DisplayHandle, hwnd.whole_window, NetAtoms[(int)NA._NET_WM_STATE], Atom.XA_ATOM, 32, PropertyMode.Replace, atoms, atom_count);
690 atoms[atom_count++] = (uint)NetAtoms[(int)NA.WM_DELETE_WINDOW];
691 if ((cp.ExStyle & (int)WindowStyles.WS_EX_CONTEXTHELP) != 0) {
692 atoms[atom_count++] = (uint)NetAtoms[(int)NA._NET_WM_CONTEXT_HELP];
695 XSetWMProtocols(DisplayHandle, hwnd.whole_window, atoms, atom_count);
699 private void SetIcon(Hwnd hwnd, Icon icon) {
705 bitmap = icon.ToBitmap();
707 size = bitmap.Width * bitmap.Height + 2;
708 data = new uint[size];
710 data[index++] = (uint)bitmap.Width;
711 data[index++] = (uint)bitmap.Height;
713 for (int y = 0; y < bitmap.Height; y++) {
714 for (int x = 0; x < bitmap.Width; x++) {
715 data[index++] = (uint)bitmap.GetPixel(x, y).ToArgb();
718 XChangeProperty(DisplayHandle, hwnd.whole_window, NetAtoms[(int)NA._NET_WM_ICON], Atom.XA_CARDINAL, 32, PropertyMode.Replace, data, size);
721 private IntPtr ImageToPixmap(Image image) {
725 private void WakeupMain () {
726 wake.Send (new byte [] { 0xFF });
729 private void TranslatePropertyToClipboard(int property) {
734 IntPtr prop = IntPtr.Zero;
736 Clipboard.Item = null;
738 XGetWindowProperty(DisplayHandle, FosterParent, property, 0, 0x7fffffff, true, Atom.AnyPropertyType, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
741 if (property == (int)Atom.XA_STRING) {
742 Clipboard.Item = Marshal.PtrToStringAnsi(prop);
743 } else if (property == (int)Atom.XA_BITMAP) {
744 // FIXME - convert bitmap to image
745 } else if (property == (int)Atom.XA_PIXMAP) {
746 // FIXME - convert pixmap to image
747 } else if (property == NetAtoms[(int)NA.OEMTEXT]) {
748 Clipboard.Item = Marshal.PtrToStringAnsi(prop);
749 } else if (property == NetAtoms[(int)NA.UNICODETEXT]) {
750 Clipboard.Item = Marshal.PtrToStringAnsi(prop);
757 private void AddExpose (XEvent xevent) {
760 hwnd = Hwnd.GetObjectFromWindow(xevent.AnyEvent.window);
763 //if (hwnd == null || !hwnd.visible) { // Can'd check visible; we might loose expose for whole_windows
768 if (xevent.AnyEvent.window == hwnd.client_window) {
769 hwnd.AddInvalidArea(xevent.ExposeEvent.x, xevent.ExposeEvent.y, xevent.ExposeEvent.width, xevent.ExposeEvent.height);
771 if (!hwnd.expose_pending) {
772 MessageQueue.Enqueue(xevent);
773 hwnd.expose_pending = true;
776 if (!hwnd.nc_expose_pending) {
777 MessageQueue.Enqueue(xevent);
778 hwnd.nc_expose_pending = true;
783 private void InvalidateWholeWindow(IntPtr handle) {
786 hwnd = Hwnd.ObjectFromHandle(handle);
788 InvalidateWholeWindow(handle, new Rectangle(0, 0, hwnd.Width, hwnd.Height));
791 private void InvalidateWholeWindow(IntPtr handle, Rectangle rectangle) {
795 hwnd = Hwnd.ObjectFromHandle(handle);
798 xevent = new XEvent ();
799 xevent.type = XEventName.Expose;
800 xevent.ExposeEvent.display = DisplayHandle;
801 xevent.ExposeEvent.window = hwnd.whole_window;
803 xevent.ExposeEvent.x = rectangle.X;
804 xevent.ExposeEvent.y = rectangle.Y;
805 xevent.ExposeEvent.width = rectangle.Width;
806 xevent.ExposeEvent.height = rectangle.Height;
811 private void WholeToScreen(IntPtr handle, ref int x, ref int y) {
817 hwnd = Hwnd.ObjectFromHandle(handle);
820 XTranslateCoordinates(DisplayHandle, hwnd.whole_window, RootWindow, x, y, out dest_x_return, out dest_y_return, out child);
827 private void AddConfigureNotify (XEvent xevent) {
830 hwnd = Hwnd.GetObjectFromWindow(xevent.ConfigureEvent.window);
837 if (xevent.ConfigureEvent.window == hwnd.whole_window) {
838 if (hwnd.parent != null) {
839 hwnd.x = xevent.ConfigureEvent.x;
840 hwnd.y = xevent.ConfigureEvent.y;
848 // We need to 'discount' the window the WM has put us in
849 XTranslateCoordinates(DisplayHandle, hwnd.whole_window, RootWindow, xevent.ConfigureEvent.x, xevent.ConfigureEvent.y, out whole_x, out whole_y, out child);
850 XTranslateCoordinates(DisplayHandle, XGetParent(hwnd.whole_window), RootWindow, xevent.ConfigureEvent.x, xevent.ConfigureEvent.y, out decoration_x, out decoration_y, out child);
851 hwnd.x = xevent.ConfigureEvent.x - whole_x + decoration_x;
852 hwnd.y = xevent.ConfigureEvent.y - whole_y + decoration_y;
855 hwnd.width = xevent.ConfigureEvent.width;
856 hwnd.height = xevent.ConfigureEvent.height;
858 if (!hwnd.configure_pending) {
859 MessageQueue.Enqueue(xevent);
860 hwnd.configure_pending = true;
863 // We drop configure events for Client windows
866 private void ShowCaret() {
867 if ((Caret.gc == IntPtr.Zero) || Caret.On) {
873 XDrawLine(DisplayHandle, Caret.Window, Caret.gc, Caret.X, Caret.Y, Caret.X, Caret.Y + Caret.Height);
877 private void HideCaret() {
878 if ((Caret.gc == IntPtr.Zero) || !Caret.On) {
884 XDrawLine(DisplayHandle, Caret.Window, Caret.gc, Caret.X, Caret.Y, Caret.X, Caret.Y + Caret.Height);
888 private int NextTimeout (DateTime now) {
889 int timeout = Int32.MaxValue;
891 foreach (Timer timer in TimerList) {
892 int next = (int) (timer.Expires - now).TotalMilliseconds;
894 return 0; // Have a timer that has already expired
897 if (next < timeout) {
902 if (timeout < Timer.Minimum) {
903 timeout = Timer.Minimum;
909 private void CheckTimers (DateTime now) {
913 count = TimerList.Count;
919 for (int i = 0; i < TimerList.Count; i++) {
922 timer = (Timer) TimerList[i];
924 if (timer.Enabled && timer.Expires <= now) {
932 private void UpdateMessageQueue () {
939 pending = XPending (DisplayHandle);
944 Idle (this, EventArgs.Empty);
948 pending = XPending (DisplayHandle);
955 timeout = NextTimeout (now);
958 Syscall.poll (pollfds, (uint) pollfds.Length, timeout);
959 // Clean out buffer, so we're not busy-looping on the same data
960 if (pollfds[1].revents != 0) {
961 wake_receive.Receive(network_buffer, 0, 1, SocketFlags.None);
965 pending = XPending (DisplayHandle);
974 pending = XPending (DisplayHandle);
978 while (pending > 0) {
979 XEvent xevent = new XEvent ();
982 XNextEvent (DisplayHandle, ref xevent);
985 switch (xevent.type) {
986 case XEventName.Expose:
990 case XEventName.SelectionClear: {
991 // Should we do something?
995 case XEventName.SelectionRequest: {
998 sel_event = new XEvent();
999 sel_event.SelectionEvent.type = XEventName.SelectionNotify;
1000 sel_event.SelectionEvent.send_event = true;
1001 sel_event.SelectionEvent.display = DisplayHandle;
1002 sel_event.SelectionEvent.selection = xevent.SelectionRequestEvent.selection;
1003 sel_event.SelectionEvent.target = xevent.SelectionRequestEvent.target;
1004 sel_event.SelectionEvent.requestor = xevent.SelectionRequestEvent.requestor;
1005 sel_event.SelectionEvent.time = xevent.SelectionRequestEvent.time;
1006 sel_event.SelectionEvent.property = 0;
1008 // Seems that some apps support asking for supported types
1009 if (xevent.SelectionEvent.target == NetAtoms[(int)NA.TARGETS]) {
1013 atoms = new uint[5];
1016 if (Clipboard.Item is String) {
1017 atoms[atom_count++] = (uint)Atom.XA_STRING;
1018 atoms[atom_count++] = (uint)NetAtoms[(int)NA.OEMTEXT];
1019 atoms[atom_count++] = (uint)NetAtoms[(int)NA.UNICODETEXT];
1020 } else if (Clipboard.Item is Image) {
1021 atoms[atom_count++] = (uint)Atom.XA_PIXMAP;
1022 atoms[atom_count++] = (uint)Atom.XA_BITMAP;
1024 // FIXME - handle other types
1027 XChangeProperty(DisplayHandle, xevent.SelectionEvent.requestor, xevent.SelectionRequestEvent.property, xevent.SelectionRequestEvent.target, 32, PropertyMode.Replace, atoms, atom_count);
1028 } else if (Clipboard.Item is string) {
1034 if (xevent.SelectionRequestEvent.target == (int)Atom.XA_STRING) {
1037 bytes = new ASCIIEncoding().GetBytes((string)Clipboard.Item);
1038 buffer = Marshal.AllocHGlobal(bytes.Length);
1039 buflen = bytes.Length;
1041 for (int i = 0; i < buflen; i++) {
1042 Marshal.WriteByte(buffer, i, bytes[i]);
1044 } else if (xevent.SelectionRequestEvent.target == NetAtoms[(int)NA.OEMTEXT]) {
1045 // FIXME - this should encode into ISO2022
1046 buffer = Marshal.StringToHGlobalAnsi((string)Clipboard.Item);
1047 while (Marshal.ReadByte(buffer, buflen) != 0) {
1050 } else if (xevent.SelectionRequestEvent.target == NetAtoms[(int)NA.UNICODETEXT]) {
1051 buffer = Marshal.StringToHGlobalAnsi((string)Clipboard.Item);
1052 while (Marshal.ReadByte(buffer, buflen) != 0) {
1056 buffer = IntPtr.Zero;
1059 if (buffer != IntPtr.Zero) {
1060 XChangeProperty(DisplayHandle, xevent.SelectionRequestEvent.requestor, xevent.SelectionRequestEvent.property, xevent.SelectionRequestEvent.target, 8, PropertyMode.Replace, buffer, buflen);
1061 sel_event.SelectionEvent.property = xevent.SelectionRequestEvent.property;
1062 Marshal.FreeHGlobal(buffer);
1064 } else if (Clipboard.Item is Image) {
1065 if (xevent.SelectionEvent.target == (int)Atom.XA_PIXMAP) {
1066 // FIXME - convert image and store as property
1067 } else if (xevent.SelectionEvent.target == (int)Atom.XA_PIXMAP) {
1068 // FIXME - convert image and store as property
1072 XSendEvent(DisplayHandle, xevent.SelectionRequestEvent.requestor, false, EventMask.NoEventMask, ref sel_event);
1076 case XEventName.SelectionNotify: {
1077 if (Dnd.HandleSelectionNotifyEvent (ref xevent)) {
1080 if (Clipboard.Enumerating) {
1081 Clipboard.Enumerating = false;
1082 if (xevent.SelectionEvent.property != 0) {
1083 XDeleteProperty(DisplayHandle, FosterParent, xevent.SelectionEvent.property);
1084 if (!Clipboard.Formats.Contains(xevent.SelectionEvent.property)) {
1085 Clipboard.Formats.Add(xevent.SelectionEvent.property);
1086 #if DriverDebugExtra
1087 Console.WriteLine("Got supported clipboard atom format: {0}", xevent.SelectionEvent.property);
1091 } else if (Clipboard.Retrieving) {
1092 Clipboard.Retrieving = false;
1093 if (xevent.SelectionEvent.property != 0) {
1094 TranslatePropertyToClipboard(xevent.SelectionEvent.property);
1096 Clipboard.Item = null;
1102 case XEventName.KeyPress:
1103 case XEventName.KeyRelease:
1104 case XEventName.ButtonPress:
1105 case XEventName.ButtonRelease:
1106 case XEventName.MotionNotify:
1107 case XEventName.EnterNotify:
1108 case XEventName.LeaveNotify:
1109 case XEventName.CreateNotify:
1110 case XEventName.DestroyNotify:
1111 case XEventName.FocusIn:
1112 case XEventName.FocusOut:
1113 case XEventName.ClientMessage:
1114 MessageQueue.Enqueue (xevent);
1117 case XEventName.ConfigureNotify:
1118 AddConfigureNotify(xevent);
1121 case XEventName.PropertyNotify:
1122 if (xevent.PropertyEvent.atom == NetAtoms[(int)NA._NET_ACTIVE_WINDOW]) {
1127 IntPtr prop = IntPtr.Zero;
1128 IntPtr prev_active;;
1130 prev_active = ActiveWindow;
1131 XGetWindowProperty(DisplayHandle, RootWindow, NetAtoms[(int)NA._NET_ACTIVE_WINDOW], 0, 1, false, Atom.XA_WINDOW, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
1132 if ((nitems > 0) && (prop != IntPtr.Zero)) {
1133 ActiveWindow = Hwnd.GetHandleFromWindow((IntPtr)Marshal.ReadInt32(prop));
1136 if (prev_active != ActiveWindow) {
1137 if (prev_active != IntPtr.Zero) {
1138 PostMessage(prev_active, Msg.WM_ACTIVATE, (IntPtr)WindowActiveFlags.WA_INACTIVE, IntPtr.Zero);
1140 if (ActiveWindow != IntPtr.Zero) {
1141 PostMessage(ActiveWindow, Msg.WM_ACTIVATE, (IntPtr)WindowActiveFlags.WA_ACTIVE, IntPtr.Zero);
1144 if (ModalWindows.Count == 0) {
1147 // Modality handling, if we are modal and the new active window is one
1148 // of ours but not the modal one, switch back to the modal window
1150 if (NativeWindow.FindWindow(ActiveWindow) != null) {
1151 if (ActiveWindow != (IntPtr)ModalWindows.Peek()) {
1152 Activate((IntPtr)ModalWindows.Peek());
1164 pending = XPending (DisplayHandle);
1169 private IntPtr GetMousewParam(int Delta) {
1172 if ((MouseState & MouseButtons.Left) != 0) {
1173 result |= (int)MsgButtons.MK_LBUTTON;
1176 if ((MouseState & MouseButtons.Middle) != 0) {
1177 result |= (int)MsgButtons.MK_MBUTTON;
1180 if ((MouseState & MouseButtons.Right) != 0) {
1181 result |= (int)MsgButtons.MK_RBUTTON;
1184 Keys mods = ModifierKeys;
1185 if ((mods & Keys.Control) != 0) {
1186 result |= (int)MsgButtons.MK_CONTROL;
1189 if ((mods & Keys.Shift) != 0) {
1190 result |= (int)MsgButtons.MK_SHIFT;
1193 result |= Delta << 16;
1195 return (IntPtr)result;
1197 private IntPtr XGetParent(IntPtr handle) {
1204 XQueryTree(DisplayHandle, handle, out Root, out Parent, out Children, out ChildCount);
1207 if (Children!=IntPtr.Zero) {
1215 private int HandleError(IntPtr display, ref XErrorEvent error_event) {
1216 if (ErrorExceptions) {
1217 throw new XException(error_event.display, error_event.resourceid, error_event.serial, error_event.error_code, error_event.request_code, error_event.minor_code);
1219 Console.WriteLine("X11 Error encountered: {0}{1}\n", XException.GetMessage(error_event.display, error_event.resourceid, error_event.serial, error_event.error_code, error_event.request_code, error_event.minor_code), WhereString());
1223 #endregion // Private Methods
1226 private void MouseHover(object sender, EventArgs e) {
1227 if ((HoverState.X == MousePosition.X) && (HoverState.Y == MousePosition.Y)) {
1230 HoverState.Timer.Enabled = false;
1232 if (HoverState.Window != IntPtr.Zero) {
1233 xevent = new XEvent ();
1235 xevent.type = XEventName.ClientMessage;
1236 xevent.ClientMessageEvent.display = DisplayHandle;
1237 xevent.ClientMessageEvent.window = (IntPtr)HoverState.Window;
1238 xevent.ClientMessageEvent.message_type = (IntPtr)HoverState.Atom;
1239 xevent.ClientMessageEvent.format = 32;
1240 xevent.ClientMessageEvent.ptr1 = (IntPtr) (HoverState.Y << 16 | HoverState.X);
1242 MessageQueue.EnqueueLocked (xevent);
1249 private void CaretCallback(object sender, EventArgs e) {
1253 Caret.On = !Caret.On;
1255 XDrawLine(DisplayHandle, Caret.Hwnd, Caret.gc, Caret.X, Caret.Y, Caret.X, Caret.Y + Caret.Height);
1257 #endregion // Callbacks
1259 #region Public Properties
1261 internal override int Caption {
1263 throw new NotImplementedException();
1267 internal override Size CursorSize {
1272 if (XQueryBestCursor(DisplayHandle, RootWindow, 32, 32, out x, out y) != 0) {
1273 return new Size(x, y);
1275 return new Size(16, 16);
1280 internal override bool DragFullWindows {
1286 internal override Size DragSize {
1288 return new Size(4, 4);
1292 internal override Size FrameBorderSize {
1294 throw new NotImplementedException();
1298 internal override Size IconSize {
1304 if (XGetIconSizes(DisplayHandle, RootWindow, out list, out count) != 0) {
1308 current = (long)list;
1311 size = new XIconSize();
1313 for (int i = 0; i < count; i++) {
1314 size = (XIconSize)Marshal.PtrToStructure((IntPtr)current, size.GetType());
1315 current += Marshal.SizeOf(size);
1317 // Look for our preferred size
1318 if (size.min_width == 32) {
1320 return new Size(32, 32);
1323 if (size.max_width == 32) {
1325 return new Size(32, 32);
1328 if (size.min_width < 32 && size.max_width > 32) {
1331 // check if we can fit one
1333 while (x < size.max_width) {
1334 x += size.width_inc;
1337 return new Size(32, 32);
1342 if (largest < size.max_width) {
1343 largest = size.max_width;
1347 // We didn't find a match or we wouldn't be here
1348 return new Size(largest, largest);
1351 return new Size(32, 32);
1356 internal override int KeyboardSpeed {
1359 // A lot harder: need to do:
1360 // XkbQueryExtension(0x08051008, 0xbfffdf4c, 0xbfffdf50, 0xbfffdf54, 0xbfffdf58) = 1
1361 // XkbAllocKeyboard(0x08051008, 0xbfffdf4c, 0xbfffdf50, 0xbfffdf54, 0xbfffdf58) = 0x080517a8
1362 // XkbGetControls(0x08051008, 1, 0x080517a8, 0xbfffdf54, 0xbfffdf58) = 0
1364 // And from that we can tell the repetition rate
1366 // Notice, the values must map to:
1367 // [0, 31] which maps to 2.5 to 30 repetitions per second.
1373 internal override int KeyboardDelay {
1376 // Return values must range from 0 to 4, 0 meaning 250ms,
1377 // and 4 meaning 1000 ms.
1379 return 1; // ie, 500 ms
1383 internal override Size MaxWindowTrackSize {
1385 return new Size (WorkingArea.Width, WorkingArea.Height);
1389 internal override Size MinimizedWindowSize {
1391 return new Size(1, 1);
1395 internal override Size MinimizedWindowSpacingSize {
1397 return new Size(1, 1);
1401 internal override Size MinimumWindowSize {
1403 return new Size(1, 1);
1407 internal override Size MinWindowTrackSize {
1409 return new Size(1, 1);
1413 internal override Keys ModifierKeys {
1415 return Keyboard.ModifierKeys;
1419 internal override Size SmallIconSize {
1425 if (XGetIconSizes(DisplayHandle, RootWindow, out list, out count) != 0) {
1429 current = (long)list;
1432 size = new XIconSize();
1434 for (int i = 0; i < count; i++) {
1435 size = (XIconSize)Marshal.PtrToStructure((IntPtr)current, size.GetType());
1436 current += Marshal.SizeOf(size);
1438 // Look for our preferred size
1439 if (size.min_width == 16) {
1441 return new Size(16, 16);
1444 if (size.max_width == 16) {
1446 return new Size(16, 16);
1449 if (size.min_width < 16 && size.max_width > 16) {
1452 // check if we can fit one
1454 while (x < size.max_width) {
1455 x += size.width_inc;
1458 return new Size(16, 16);
1463 if (smallest == 0 || smallest > size.min_width) {
1464 smallest = size.min_width;
1468 // We didn't find a match or we wouldn't be here
1469 return new Size(smallest, smallest);
1472 return new Size(16, 16);
1477 internal override int MouseButtonCount {
1483 internal override bool MouseButtonsSwapped {
1485 return false; // FIXME - how to detect?
1489 internal override bool MouseWheelPresent {
1491 return true; // FIXME - how to detect?
1495 internal override Rectangle VirtualScreen {
1501 internal override Rectangle WorkingArea {
1507 IntPtr prop = IntPtr.Zero;
1511 XGetWindowProperty(DisplayHandle, RootWindow, NetAtoms[(int)NA._NET_DESKTOP_GEOMETRY], 0, 256, false, Atom.XA_CARDINAL, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
1512 if ((nitems == 2) && (prop != IntPtr.Zero)) {
1513 width = Marshal.ReadInt32(prop, 0);
1514 height = Marshal.ReadInt32(prop, 4);
1517 return new Rectangle(0, 0, width, height);
1519 XWindowAttributes attributes=new XWindowAttributes();
1522 XGetWindowAttributes(DisplayHandle, XRootWindow(DisplayHandle, 0), ref attributes);
1525 return new Rectangle(0, 0, attributes.width, attributes.height);
1529 #endregion // Public properties
1531 #region Public Static Methods
1532 internal override IntPtr InitializeDriver() {
1534 if (DisplayHandle==IntPtr.Zero) {
1535 SetDisplay(XOpenDisplay(IntPtr.Zero));
1541 internal override void ShutdownDriver(IntPtr token) {
1543 if (DisplayHandle!=IntPtr.Zero) {
1544 XCloseDisplay(DisplayHandle);
1545 DisplayHandle=IntPtr.Zero;
1550 internal override void EnableThemes() {
1551 ThemesEnabled = true;
1555 internal override void Activate(IntPtr handle) {
1557 SendNetWMMessage(Hwnd.ObjectFromHandle(handle).whole_window, (IntPtr)NetAtoms[(int)NA._NET_ACTIVE_WINDOW], IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
1558 //XRaiseWindow(DisplayHandle, handle);
1563 internal override void CaretVisible(IntPtr handle, bool visible) {
1564 // Visible is cumulative; two hides require two shows before the caret is visible again
1565 if (Caret.Hwnd == handle) {
1567 if (Caret.Visible < 1) {
1570 if (Caret.Visible == 1) {
1572 Caret.Timer.Start();
1577 if (Caret.Visible == 0) {
1585 internal override bool CalculateWindowRect(IntPtr handle, ref Rectangle ClientRect, int Style, int ExStyle, IntPtr MenuHandle, out Rectangle WindowRect) {
1586 FormBorderStyle border_style;
1587 TitleStyle title_style;
1589 DeriveStyles(handle, Style, ExStyle, out border_style, out title_style);
1590 WindowRect = Hwnd.GetWindowRectangle(border_style, MenuHandle, title_style, ClientRect);
1595 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {
1601 hwnd = Hwnd.ObjectFromHandle(handle);
1604 XTranslateCoordinates(DisplayHandle, hwnd.client_window, RootWindow, x, y, out dest_x_return, out dest_y_return, out child);
1611 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
1612 DataFormats.Format f;
1615 f = DataFormats.Format.List;
1617 if (XGetSelectionOwner(DisplayHandle, NetAtoms[(int)NA.CLIPBOARD]) == IntPtr.Zero) {
1621 Clipboard.Formats = new ArrayList();
1624 XConvertSelection(DisplayHandle, NetAtoms[(int)NA.CLIPBOARD], f.Id, f.Id, FosterParent, IntPtr.Zero);
1626 Clipboard.Enumerating = true;
1627 while (Clipboard.Enumerating) {
1628 UpdateMessageQueue();
1633 result = new int[Clipboard.Formats.Count];
1635 for (int i = 0; i < Clipboard.Formats.Count; i++) {
1636 result[i] = (int)Clipboard.Formats[i];
1639 Clipboard.Formats = null;
1643 internal override void ClipboardClose(IntPtr handle) {
1644 if (handle != ClipMagic) {
1645 throw new ArgumentException("handle is not a valid clipboard handle");
1650 internal override int ClipboardGetID(IntPtr handle, string format) {
1651 if (handle != ClipMagic) {
1652 throw new ArgumentException("handle is not a valid clipboard handle");
1655 if (format == "Text" ) return (int)Atom.XA_STRING;
1656 else if (format == "Bitmap" ) return (int)Atom.XA_BITMAP;
1657 //else if (format == "MetaFilePict" ) return 3;
1658 //else if (format == "SymbolicLink" ) return 4;
1659 //else if (format == "DataInterchangeFormat" ) return 5;
1660 //else if (format == "Tiff" ) return 6;
1661 else if (format == "OEMText" ) return XInternAtom(DisplayHandle, "COMPOUND_TEXT", false);
1662 else if (format == "DeviceIndependentBitmap" ) return (int)Atom.XA_PIXMAP;
1663 else if (format == "Palette" ) return (int)Atom.XA_COLORMAP; // Useless
1664 //else if (format == "PenData" ) return 10;
1665 //else if (format == "RiffAudio" ) return 11;
1666 //else if (format == "WaveAudio" ) return 12;
1667 else if (format == "UnicodeText" ) return XInternAtom(DisplayHandle, "UTF8_STRING", false);
1668 //else if (format == "EnhancedMetafile" ) return 14;
1669 //else if (format == "FileDrop" ) return 15;
1670 //else if (format == "Locale" ) return 16;
1672 return XInternAtom(DisplayHandle, format, false);
1675 internal override IntPtr ClipboardOpen() {
1679 internal override object ClipboardRetrieve(IntPtr handle, int type, XplatUI.ClipboardToObject converter) {
1680 XConvertSelection(DisplayHandle, NetAtoms[(int)NA.CLIPBOARD], type, type, FosterParent, IntPtr.Zero);
1682 Clipboard.Retrieving = true;
1683 while (Clipboard.Retrieving) {
1684 UpdateMessageQueue();
1687 return Clipboard.Item;
1690 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
1691 Clipboard.Item = obj;
1692 Clipboard.Type = type;
1693 Clipboard.Converter = converter;
1696 XSetSelectionOwner(DisplayHandle, NetAtoms[(int)NA.CLIPBOARD], FosterParent, IntPtr.Zero);
1698 // Clearing the selection
1699 XSetSelectionOwner(DisplayHandle, NetAtoms[(int)NA.CLIPBOARD], IntPtr.Zero, IntPtr.Zero);
1703 internal override void CreateCaret(IntPtr handle, int width, int height) {
1704 XGCValues gc_values;
1707 hwnd = Hwnd.ObjectFromHandle(handle);
1709 if (Caret.Hwnd != IntPtr.Zero) {
1710 DestroyCaret(Caret.Hwnd);
1713 Caret.Hwnd = handle;
1714 Caret.Window = hwnd.client_window;
1715 Caret.Width = width;
1716 Caret.Height = height;
1720 gc_values = new XGCValues();
1721 gc_values.line_width = width;
1723 Caret.gc = XCreateGC(DisplayHandle, Caret.Window, GCFunction.GCLineWidth, ref gc_values);
1724 if (Caret.gc == IntPtr.Zero) {
1725 Caret.Hwnd = IntPtr.Zero;
1729 XSetFunction(DisplayHandle, Caret.gc, GXFunction.GXinvert);
1732 internal override IntPtr CreateWindow(CreateParams cp) {
1733 XSetWindowAttributes Attributes;
1739 IntPtr ParentHandle;
1741 IntPtr ClientWindow;
1742 Rectangle ClientRect;
1743 SetWindowValuemask ValueMask;
1748 Attributes = new XSetWindowAttributes();
1754 if (Width<1) Width=1;
1755 if (Height<1) Height=1;
1757 if (cp.Parent != IntPtr.Zero) {
1758 ParentHandle = Hwnd.ObjectFromHandle(cp.Parent).client_window;
1760 if ((cp.Style & (int)WindowStyles.WS_CHILD) != 0) {
1761 // We need to use our foster parent window until this poor child gets it's parent assigned
1762 ParentHandle=FosterParent;
1763 } else if ((cp.Style & (int)WindowStyles.WS_POPUP) != 0) {
1764 ParentHandle=RootWindow;
1766 // Default position on screen, if window manager doesn't place us somewhere else
1769 ParentHandle=RootWindow;
1773 ValueMask = SetWindowValuemask.BitGravity | SetWindowValuemask.WinGravity;
1775 Attributes.bit_gravity = Gravity.NorthWestGravity;
1776 Attributes.win_gravity = Gravity.NorthWestGravity;
1778 // Save what's under the toolwindow
1779 if ((cp.ExStyle & (int)WindowStyles.WS_EX_TOOLWINDOW) != 0) {
1780 Attributes.save_under = true;
1781 ValueMask |= SetWindowValuemask.SaveUnder;
1785 // If we're a popup without caption we override the WM
1786 if ((cp.Style & ((int)WindowStyles.WS_POPUP)) != 0) {
1787 if ((cp.Style & (int)WindowStyles.WS_CAPTION) == 0) {
1788 Attributes.override_redirect = true;
1789 ValueMask |= SetWindowValuemask.OverrideRedirect;
1796 hwnd.height = Height;
1797 hwnd.parent = Hwnd.ObjectFromHandle(cp.Parent);
1799 ClientRect = hwnd.ClientRect;
1800 ClientWindow = IntPtr.Zero;
1803 WholeWindow = XCreateWindow(DisplayHandle, ParentHandle, X, Y, Width, Height, 0, (int)CreateWindowArgs.CopyFromParent, (int)CreateWindowArgs.InputOutput, IntPtr.Zero, ValueMask, ref Attributes);
1804 if (WholeWindow != IntPtr.Zero) {
1805 ValueMask &= ~(SetWindowValuemask.OverrideRedirect | SetWindowValuemask.SaveUnder);
1807 if (CustomVisual != IntPtr.Zero && CustomColormap != IntPtr.Zero) {
1808 ValueMask = SetWindowValuemask.ColorMap;
1809 Attributes.colormap = CustomColormap;
1811 ClientWindow = XCreateWindow(DisplayHandle, WholeWindow, ClientRect.X, ClientRect.Y, ClientRect.Width, ClientRect.Height, 0, (int)CreateWindowArgs.CopyFromParent, (int)CreateWindowArgs.InputOutput, CustomVisual, ValueMask, ref Attributes);
1815 if ((WholeWindow == IntPtr.Zero) || (ClientWindow == IntPtr.Zero)) {
1816 throw new Exception("Could not create X11 windows");
1819 hwnd.WholeWindow = WholeWindow;
1820 hwnd.ClientWindow = ClientWindow;
1822 SetHwndStyles(hwnd, cp);
1825 Console.WriteLine("Created window {0:X} / {1:X} parent {2:X}", ClientWindow.ToInt32(), WholeWindow.ToInt32(), hwnd.parent != null ? hwnd.parent.Handle.ToInt32() : 0);
1829 XSelectInput(DisplayHandle, hwnd.whole_window, SelectInputMask);
1830 XSelectInput(DisplayHandle, hwnd.client_window, SelectInputMask);
1832 if ((cp.Style & (int)WindowStyles.WS_VISIBLE) != 0) {
1833 XMapWindow(DisplayHandle, hwnd.whole_window);
1834 XMapWindow(DisplayHandle, hwnd.client_window);
1835 hwnd.visible = true;
1839 SetWMStyles(hwnd, cp);
1841 // for now make all windows dnd enabled
1842 Dnd.SetAllowDrop (hwnd, true);
1844 // Set caption/window title
1845 Text(hwnd.Handle, cp.Caption);
1850 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1851 CreateParams create_params = new CreateParams();
1853 create_params.Caption = "";
1854 create_params.X = X;
1855 create_params.Y = Y;
1856 create_params.Width = Width;
1857 create_params.Height = Height;
1859 create_params.ClassName=XplatUI.DefaultClassName;
1860 create_params.ClassStyle = 0;
1861 create_params.ExStyle=0;
1862 create_params.Parent=IntPtr.Zero;
1863 create_params.Param=0;
1865 return CreateWindow(create_params);
1868 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
1870 Bitmap cursor_bitmap;
1878 IntPtr cursor_pixmap;
1885 if (XQueryBestCursor(DisplayHandle, RootWindow, bitmap.Width, bitmap.Height, out width, out height) == 0) {
1889 // Win32 only allows creation cursors of a certain size
1890 if ((bitmap.Width != width) || (bitmap.Width != height)) {
1891 cursor_bitmap = new Bitmap(bitmap, new Size(width, height));
1892 cursor_mask = new Bitmap(mask, new Size(width, height));
1894 cursor_bitmap = bitmap;
1898 width = cursor_bitmap.Width;
1899 height = cursor_bitmap.Height;
1901 cursor_bits = new Byte[(width / 8) * height];
1902 mask_bits = new Byte[(width / 8) * height];
1904 for (int y = 0; y < height; y++) {
1905 for (int x = 0; x < width; x++) {
1906 c_pixel = cursor_bitmap.GetPixel(x, y);
1907 m_pixel = cursor_mask.GetPixel(x, y);
1909 and = c_pixel == cursor_pixel;
1910 xor = m_pixel == mask_pixel;
1914 // cursor_bits[y * width / 8 + x / 8] &= (byte)~((1 << (x % 8))); // The bit already is 0
1915 mask_bits[y * width / 8 + x / 8] |= (byte)(1 << (x % 8));
1916 } else if (and && !xor) {
1918 cursor_bits[y * width / 8 + x / 8] |= (byte)(1 << (x % 8));
1919 mask_bits[y * width / 8 + x / 8] |= (byte)(1 << (x % 8));
1921 } else if (and && !xor) {
1923 } else if (and && xor) {
1926 // X11 doesn't know the 'reverse screen' concept, so we'll treat them the same
1927 // we want both to be 0 so nothing to be done
1928 //cursor_bits[y * width / 8 + x / 8] &= (byte)~((1 << (x % 8)));
1929 //mask_bits[y * width / 8 + x / 8] |= (byte)(01 << (x % 8));
1935 cursor_pixmap = XCreatePixmapFromBitmapData(DisplayHandle, RootWindow, cursor_bits, width, height, (IntPtr)1, (IntPtr)0, 1);
1936 mask_pixmap = XCreatePixmapFromBitmapData(DisplayHandle, RootWindow, mask_bits, width, height, (IntPtr)1, (IntPtr)0, 1);
1940 fg.pixel = XWhitePixel(DisplayHandle, ScreenNo);
1941 fg.red = (ushort)65535;
1942 fg.green = (ushort)65535;
1943 fg.blue = (ushort)65535;
1945 bg.pixel = XBlackPixel(DisplayHandle, ScreenNo);
1947 cursor = XCreatePixmapCursor(DisplayHandle, cursor_pixmap, mask_pixmap, ref fg, ref bg, xHotSpot, yHotSpot);
1949 XFreePixmap(DisplayHandle, cursor_pixmap);
1950 XFreePixmap(DisplayHandle, mask_pixmap);
1955 internal override IntPtr DefineStdCursor(StdCursor id) {
1956 CursorFontShape shape;
1959 // FIXME - define missing shapes
1962 case StdCursor.AppStarting: {
1963 shape = CursorFontShape.XC_watch;
1967 case StdCursor.Arrow: {
1971 case StdCursor.Cross: {
1972 shape = CursorFontShape.XC_crosshair;
1976 case StdCursor.Default: {
1980 case StdCursor.Hand: {
1981 shape = CursorFontShape.XC_hand1;
1985 case StdCursor.Help: {
1986 shape = CursorFontShape.XC_question_arrow;
1990 case StdCursor.HSplit: {
1991 shape = CursorFontShape.XC_sb_v_double_arrow;
1995 case StdCursor.IBeam: {
1996 shape = CursorFontShape.XC_xterm;
2000 case StdCursor.No: {
2001 shape = CursorFontShape.XC_circle;
2005 case StdCursor.NoMove2D: {
2006 shape = CursorFontShape.XC_fleur;
2010 case StdCursor.NoMoveHoriz: {
2011 shape = CursorFontShape.XC_fleur;
2015 case StdCursor.NoMoveVert: {
2016 shape = CursorFontShape.XC_fleur;
2020 case StdCursor.PanEast: {
2021 shape = CursorFontShape.XC_fleur;
2025 case StdCursor.PanNE: {
2026 shape = CursorFontShape.XC_fleur;
2030 case StdCursor.PanNorth: {
2031 shape = CursorFontShape.XC_fleur;
2035 case StdCursor.PanNW: {
2036 shape = CursorFontShape.XC_fleur;
2040 case StdCursor.PanSE: {
2041 shape = CursorFontShape.XC_fleur;
2045 case StdCursor.PanSouth: {
2046 shape = CursorFontShape.XC_fleur;
2050 case StdCursor.PanSW: {
2051 shape = CursorFontShape.XC_fleur;
2055 case StdCursor.PanWest: {
2056 shape = CursorFontShape.XC_sizing;
2060 case StdCursor.SizeAll: {
2061 shape = CursorFontShape.XC_fleur;
2065 case StdCursor.SizeNESW: {
2066 shape = CursorFontShape.XC_top_right_corner;
2070 case StdCursor.SizeNS: {
2071 shape = CursorFontShape.XC_sb_v_double_arrow;
2075 case StdCursor.SizeNWSE: {
2076 shape = CursorFontShape.XC_top_left_corner;
2080 case StdCursor.SizeWE: {
2081 shape = CursorFontShape.XC_sb_h_double_arrow;
2085 case StdCursor.UpArrow: {
2086 shape = CursorFontShape.XC_center_ptr;
2090 case StdCursor.VSplit: {
2091 shape = CursorFontShape.XC_sb_h_double_arrow;
2095 case StdCursor.WaitCursor: {
2096 shape = CursorFontShape.XC_watch;
2106 cursor = XCreateFontCursor(DisplayHandle, shape);
2111 internal override IntPtr DefWndProc(ref Message msg) {
2115 internal override void DestroyCaret(IntPtr handle) {
2116 if (Caret.Hwnd == handle) {
2117 if (Caret.Visible == 1) {
2121 if (Caret.gc != IntPtr.Zero) {
2122 XFreeGC(DisplayHandle, Caret.gc);
2123 Caret.gc = IntPtr.Zero;
2125 Caret.Hwnd = IntPtr.Zero;
2131 internal override void DestroyCursor(IntPtr cursor) {
2133 XFreeCursor(DisplayHandle, cursor);
2137 internal override void DestroyWindow(IntPtr handle) {
2140 hwnd = Hwnd.ObjectFromHandle(handle);
2144 Console.WriteLine("window {0:X} already destroyed", handle.ToInt32());
2150 Console.WriteLine("Destroying window {0:X}", handle.ToInt32());
2153 // Make sure if the caret is in the window, that we destroy the caret, too
2154 if (Caret.Hwnd == hwnd.client_window) {
2155 DestroyCaret(handle);
2159 if (hwnd.client_window != IntPtr.Zero) {
2160 XDestroyWindow(DisplayHandle, hwnd.client_window);
2163 if ((hwnd.whole_window != IntPtr.Zero) && (hwnd.whole_window != hwnd.client_window)) {
2164 XDestroyWindow(DisplayHandle, hwnd.whole_window);
2171 internal override IntPtr DispatchMessage(ref MSG msg) {
2172 return NativeWindow.WndProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);
2175 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
2177 XGCValues gc_values;
2180 hwnd = Hwnd.ObjectFromHandle(handle);
2182 gc_values = new XGCValues();
2184 gc_values.subwindow_mode = GCSubwindowMode.IncludeInferiors;
2185 gc_values.line_width = line_width;
2186 gc_values.foreground = XBlackPixel(DisplayHandle, ScreenNo);
2188 // This logic will give us true rubber bands: (libsx, SANE_XOR)
2189 //mask = foreground ^ background;
2190 //XSetForeground(DisplayHandle, gc, 0xffffffff);
2191 //XSetBackground(DisplayHandle, gc, background);
2192 //XSetFunction(DisplayHandle, gc, GXxor);
2193 //XSetPlaneMask(DisplayHandle, gc, mask);
2196 gc = XCreateGC(DisplayHandle, hwnd.client_window, GCFunction.GCSubwindowMode | GCFunction.GCLineWidth | GCFunction.GCForeground, ref gc_values);
2201 control = Control.FromHandle(handle);
2203 XColor xcolor = new XColor();
2205 xcolor.red = (ushort)(control.ForeColor.R * 257);
2206 xcolor.green = (ushort)(control.ForeColor.G * 257);
2207 xcolor.blue = (ushort)(control.ForeColor.B * 257);
2208 XAllocColor(DisplayHandle, DefaultColormap, ref xcolor);
2209 foreground = (uint)xcolor.pixel.ToInt32();
2211 xcolor.red = (ushort)(control.BackColor.R * 257);
2212 xcolor.green = (ushort)(control.BackColor.G * 257);
2213 xcolor.blue = (ushort)(control.BackColor.B * 257);
2214 XAllocColor(DisplayHandle, DefaultColormap, ref xcolor);
2215 background = (uint)xcolor.pixel.ToInt32();
2217 uint mask = foreground ^ background;
2219 XSetForeground(DisplayHandle, gc, 0xffffffff);
2220 XSetBackground(DisplayHandle, gc, background);
2221 XSetFunction(DisplayHandle, gc, GXFunction.GXxor);
2222 XSetPlaneMask(DisplayHandle, gc, mask);
2224 if ((rect.Width > 0) && (rect.Height > 0)) {
2225 XDrawRectangle(DisplayHandle, hwnd.client_window, gc, rect.Left, rect.Top, rect.Width, rect.Height);
2227 if (rect.Width > 0) {
2228 XDrawLine(DisplayHandle, hwnd.client_window, gc, rect.X, rect.Y, rect.Right, rect.Y);
2230 XDrawLine(DisplayHandle, hwnd.client_window, gc, rect.X, rect.Y, rect.X, rect.Bottom);
2233 XFreeGC(DisplayHandle, gc);
2236 internal override void DoEvents() {
2237 MSG msg = new MSG ();
2239 if (OverrideCursorHandle != IntPtr.Zero) {
2240 OverrideCursorHandle = IntPtr.Zero;
2243 while (PeekMessage(ref msg, IntPtr.Zero, 0, 0, (uint)PeekMessageFlags.PM_REMOVE)) {
2244 if (msg.message == Msg.WM_PAINT) {
2245 TranslateMessage (ref msg);
2246 DispatchMessage (ref msg);
2251 internal override void EnableWindow(IntPtr handle, bool Enable) {
2252 // We do nothing; On X11 SetModal is used to create modal dialogs, on Win32 this function is used (see comment there)
2255 internal override void Exit() {
2256 GetMessageResult = false;
2257 Graphics.FromHdcInternal (IntPtr.Zero);
2260 internal override IntPtr GetActive() {
2265 IntPtr prop = IntPtr.Zero;
2266 IntPtr active = IntPtr.Zero;
2268 XGetWindowProperty(DisplayHandle, RootWindow, NetAtoms[(int)NA._NET_ACTIVE_WINDOW], 0, 1, false, Atom.XA_WINDOW, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
2269 if ((nitems > 0) && (prop != IntPtr.Zero)) {
2270 active = (IntPtr)Marshal.ReadInt32(prop);
2274 if (active != IntPtr.Zero) {
2277 hwnd = Hwnd.GetObjectFromWindow(active);
2279 active = hwnd.Handle;
2281 active = IntPtr.Zero;
2287 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
2288 throw new NotImplementedException ();
2291 internal override void GetDisplaySize(out Size size) {
2292 XWindowAttributes attributes=new XWindowAttributes();
2295 // FIXME - use _NET_WM messages instead?
2296 XGetWindowAttributes(DisplayHandle, XRootWindow(DisplayHandle, 0), ref attributes);
2299 size = new Size(attributes.width, attributes.height);
2302 internal override SizeF GetAutoScaleSize(Font font) {
2305 string magic_string = "The quick brown fox jumped over the lazy dog.";
2306 double magic_number = 44.549996948242189;
2308 g = Graphics.FromHwnd(FosterParent);
2310 width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
2311 return new SizeF(width, font.Height);
2314 internal override IntPtr GetParent(IntPtr handle) {
2317 hwnd = Hwnd.ObjectFromHandle(handle);
2318 if (hwnd != null && hwnd.parent != null) {
2319 return hwnd.parent.Handle;
2324 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
2334 if (handle != IntPtr.Zero) {
2335 use_handle = Hwnd.ObjectFromHandle(handle).client_window;
2337 use_handle = RootWindow;
2341 XQueryPointer(DisplayHandle, use_handle, out root, out child, out root_x, out root_y, out win_x, out win_y, out keys_buttons);
2344 if (handle != IntPtr.Zero) {
2353 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
2354 return GetFontMetrics(g.GetHdc(), font.ToHfont(), out ascent, out descent);
2357 internal override Graphics GetMenuDC(IntPtr handle, IntPtr ncpaint_region) {
2360 hwnd = Hwnd.ObjectFromHandle(handle);
2362 return Graphics.FromHwnd(hwnd.whole_window);
2365 internal override Point GetMenuOrigin(IntPtr handle) {
2368 hwnd = Hwnd.ObjectFromHandle(handle);
2371 return hwnd.MenuOrigin;
2376 internal override bool GetMessage(ref MSG msg, IntPtr handle, int wFilterMin, int wFilterMax) {
2383 if (MessageQueue.Count > 0) {
2384 xevent = (XEvent) MessageQueue.Dequeue ();
2386 UpdateMessageQueue ();
2388 if (MessageQueue.Count > 0) {
2389 xevent = (XEvent) MessageQueue.Dequeue ();
2391 msg.hwnd= IntPtr.Zero;
2392 msg.message = Msg.WM_ENTERIDLE;
2397 // FIXME - handle filtering
2399 hwnd = Hwnd.GetObjectFromWindow(xevent.AnyEvent.window);
2401 // Handle messages for windows that are already destroyed
2404 Console.WriteLine("GetMessage(): Got message {0} for non-existent window {1:X}", xevent.type, xevent.AnyEvent.window.ToInt32());
2406 goto ProcessNextMessage;
2409 if (hwnd.client_window == xevent.AnyEvent.window) {
2411 //Console.WriteLine("Client message, sending to window {0:X}", msg.hwnd.ToInt32());
2414 //Console.WriteLine("Non-Client message, sending to window {0:X}", msg.hwnd.ToInt32());
2417 msg.hwnd = hwnd.Handle;
2420 // If you add a new event to this switch make sure to add it in
2421 // UpdateMessage also unless it is not coming through the X event system.
2423 switch(xevent.type) {
2424 case XEventName.KeyPress: {
2425 Keyboard.KeyEvent (FocusWindow, xevent, ref msg);
2429 case XEventName.KeyRelease: {
2430 Keyboard.KeyEvent (FocusWindow, xevent, ref msg);
2434 case XEventName.ButtonPress: {
2435 switch(xevent.ButtonEvent.button) {
2437 MouseState |= MouseButtons.Left;
2439 msg.message = Msg.WM_LBUTTONDOWN;
2441 msg.message = Msg.WM_NCLBUTTONDOWN;
2442 WholeToScreen (msg.hwnd, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2444 // TODO: For WM_NCLBUTTONDOWN wParam specifies a hit-test value not the virtual keys down
2445 msg.wParam=GetMousewParam(0);
2450 MouseState |= MouseButtons.Middle;
2452 msg.message = Msg.WM_MBUTTONDOWN;
2454 msg.message = Msg.WM_NCMBUTTONDOWN;
2455 WholeToScreen (msg.hwnd, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2457 msg.wParam=GetMousewParam(0);
2462 MouseState |= MouseButtons.Right;
2464 msg.message = Msg.WM_RBUTTONDOWN;
2466 msg.message = Msg.WM_NCRBUTTONDOWN;
2467 WholeToScreen (msg.hwnd, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2469 msg.wParam=GetMousewParam(0);
2474 msg.message=Msg.WM_MOUSEWHEEL;
2475 msg.wParam=GetMousewParam(120);
2480 msg.message=Msg.WM_MOUSEWHEEL;
2481 msg.wParam=GetMousewParam(-120);
2487 msg.lParam=(IntPtr) (xevent.ButtonEvent.y << 16 | xevent.ButtonEvent.x);
2488 MousePosition.X = xevent.ButtonEvent.x;
2489 MousePosition.Y = xevent.ButtonEvent.y;
2491 if (!ClickPending.Pending) {
2492 ClickPending.Pending = true;
2493 ClickPending.Hwnd = msg.hwnd;
2494 ClickPending.Message = msg.message;
2495 ClickPending.wParam = msg.wParam;
2496 ClickPending.lParam = msg.lParam;
2497 ClickPending.Time = (long)xevent.ButtonEvent.time;
2499 if ((((long)xevent.ButtonEvent.time - ClickPending.Time) < DoubleClickInterval) && (msg.wParam == ClickPending.wParam) && (msg.lParam == ClickPending.lParam) && (msg.message == ClickPending.Message)) {
2500 // Looks like a genuine double click, clicked twice on the same spot with the same keys
2501 switch(xevent.ButtonEvent.button) {
2503 msg.message = client ? Msg.WM_LBUTTONDBLCLK : Msg.WM_NCLBUTTONDBLCLK;
2508 msg.message = client ? Msg.WM_MBUTTONDBLCLK : Msg.WM_NCMBUTTONDBLCLK;
2513 msg.message = client ? Msg.WM_RBUTTONDBLCLK : Msg.WM_NCRBUTTONDBLCLK;
2518 ClickPending.Pending = false;
2524 case XEventName.ButtonRelease: {
2525 switch(xevent.ButtonEvent.button) {
2527 MouseState &= ~MouseButtons.Left;
2529 msg.message = Msg.WM_LBUTTONUP;
2531 msg.message = Msg.WM_NCLBUTTONUP;
2532 WholeToScreen (msg.hwnd, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2534 msg.wParam=GetMousewParam(0);
2539 MouseState &= ~MouseButtons.Middle;
2541 msg.message = Msg.WM_MBUTTONUP;
2543 msg.message = Msg.WM_NCMBUTTONUP;
2544 WholeToScreen (msg.hwnd, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2546 msg.wParam=GetMousewParam(0);
2551 MouseState &= ~MouseButtons.Right;
2553 msg.message = Msg.WM_RBUTTONUP;
2555 msg.message = Msg.WM_NCRBUTTONUP;
2556 WholeToScreen (msg.hwnd, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2558 msg.wParam=GetMousewParam(0);
2563 goto ProcessNextMessage;
2567 goto ProcessNextMessage;
2571 msg.lParam=(IntPtr) (xevent.ButtonEvent.y << 16 | xevent.ButtonEvent.x);
2572 MousePosition.X = xevent.ButtonEvent.x;
2573 MousePosition.Y = xevent.ButtonEvent.y;
2577 case XEventName.MotionNotify: {
2579 #if DriverDebugExtra
2580 Console.WriteLine("GetMessage(): Window {0:X} MotionNotify x={1} y={2}", client ? hwnd.client_window.ToInt32() : hwnd.whole_window.ToInt32(), xevent.MotionEvent.x, xevent.MotionEvent.y);
2582 NativeWindow.WndProc(msg.hwnd, Msg.WM_SETCURSOR, msg.hwnd, (IntPtr)HitTest.HTCLIENT);
2584 msg.message = Msg.WM_MOUSEMOVE;
2585 msg.wParam = GetMousewParam(0);
2586 msg.lParam = (IntPtr) (xevent.MotionEvent.y << 16 | xevent.MotionEvent.x & 0xFFFF);
2588 HoverState.X = MousePosition.X = xevent.MotionEvent.x;
2589 HoverState.Y = MousePosition.Y = xevent.MotionEvent.y;
2593 #if DriverDebugExtra
2594 Console.WriteLine("GetMessage(): non-client area {0:X} MotionNotify x={1} y={2}", client ? hwnd.client_window.ToInt32() : hwnd.whole_window.ToInt32(), xevent.MotionEvent.x, xevent.MotionEvent.y);
2596 msg.message = Msg.WM_NCMOUSEMOVE;
2597 msg.lParam = (IntPtr) (xevent.MotionEvent.y << 16 | xevent.MotionEvent.x & 0xFFFF);
2600 // Not sure we need this...
2602 ht = NativeWindow.WndProc(hwnd.client_window, Msg.WM_NCHITTEST, IntPtr.Zero, msg.lParam);
2605 MousePosition.X = xevent.MotionEvent.x;
2606 MousePosition.Y = xevent.MotionEvent.y;
2612 case XEventName.EnterNotify: {
2613 if (xevent.CrossingEvent.mode != NotifyMode.NotifyNormal) {
2614 goto ProcessNextMessage;
2616 msg.message = Msg.WM_MOUSE_ENTER;
2617 HoverState.Timer.Enabled = true;
2618 HoverState.Window = xevent.CrossingEvent.window;
2622 case XEventName.LeaveNotify: {
2623 if (xevent.CrossingEvent.mode != NotifyMode.NotifyNormal) {
2624 goto ProcessNextMessage;
2626 msg.message=Msg.WM_MOUSE_LEAVE;
2627 HoverState.Timer.Enabled = false;
2628 HoverState.Window = IntPtr.Zero;
2633 case XEventName.CreateNotify: {
2634 if (client && (xevent.ConfigureEvent.xevent == xevent.ConfigureEvent.window)) {
2635 msg.message = WM_CREATE;
2636 // Set up CreateStruct
2638 goto ProcessNextMessage;
2644 case XEventName.ConfigureNotify: {
2645 if (!client && (xevent.ConfigureEvent.xevent == xevent.ConfigureEvent.window)) { // Ignore events for children (SubstructureNotify) and client areas
2646 XplatUIWin32.NCCALCSIZE_PARAMS ncp;
2650 #if DriverDebugExtra
2651 Console.WriteLine("GetMessage(): Window {0:X} ConfigureNotify x={1} y={2} width={3} height={4}", hwnd.client_window.ToInt32(), xevent.ConfigureEvent.x, xevent.ConfigureEvent.y, xevent.ConfigureEvent.width, xevent.ConfigureEvent.height);
2653 msg.message = Msg.WM_WINDOWPOSCHANGED;
2654 hwnd.configure_pending = false;
2656 // We need to adjust our client window to track the resize of whole_window
2657 rect = hwnd.DefaultClientRect;
2659 ncp = new XplatUIWin32.NCCALCSIZE_PARAMS();
2660 ptr = Marshal.AllocHGlobal(Marshal.SizeOf(ncp));
2662 ncp.rgrc1.left = rect.Left;
2663 ncp.rgrc1.top = rect.Top;
2664 ncp.rgrc1.right = rect.Right;
2665 ncp.rgrc1.bottom = rect.Bottom;
2667 Marshal.StructureToPtr(ncp, ptr, true);
2668 NativeWindow.WndProc(hwnd.client_window, Msg.WM_NCCALCSIZE, (IntPtr)1, ptr);
2669 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(ptr, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
2670 Marshal.FreeHGlobal(ptr);
2672 // FIXME - debug this with Menus, need to set hwnd.ClientRect
2674 rect = new Rectangle(ncp.rgrc1.left, ncp.rgrc1.top, ncp.rgrc1.right - ncp.rgrc1.left, ncp.rgrc1.bottom - ncp.rgrc1.top);
2676 XMoveResizeWindow(DisplayHandle, hwnd.client_window, rect.X, rect.Y, rect.Width, rect.Height);
2678 goto ProcessNextMessage;
2681 msg.lParam=IntPtr.Zero; // FIXME - Generate LPWINDOWPOS structure and pass on
2685 case XEventName.FocusIn: {
2686 msg.message=Msg.WM_SETFOCUS;
2687 msg.wParam=IntPtr.Zero;
2691 case XEventName.FocusOut: {
2692 msg.message=Msg.WM_KILLFOCUS;
2693 msg.wParam=IntPtr.Zero;
2697 case XEventName.Expose: {
2699 switch (hwnd.border_style) {
2700 case FormBorderStyle.Fixed3D: {
2703 g = Graphics.FromHwnd(hwnd.whole_window);
2704 ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height));
2709 case FormBorderStyle.FixedSingle: {
2712 g = Graphics.FromHwnd(hwnd.whole_window);
2713 ControlPaint.DrawBorder(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Color.Black, ButtonBorderStyle.Solid);
2718 #if DriverDebugExtra
2719 Console.WriteLine("GetMessage(): Window {0:X} Exposed non-client area {1},{2} {3}x{4}", hwnd.client_window.ToInt32(), xevent.ExposeEvent.x, xevent.ExposeEvent.y, xevent.ExposeEvent.width, xevent.ExposeEvent.height);
2721 msg.message = Msg.WM_NCPAINT;
2722 hwnd.nc_expose_pending = false;
2725 #if DriverDebugExtra
2726 Console.WriteLine("GetMessage(): Window {0:X} Exposed area {1},{2} {3}x{4}", hwnd.client_window.ToInt32(), xevent.ExposeEvent.x, xevent.ExposeEvent.y, xevent.ExposeEvent.width, xevent.ExposeEvent.height);
2728 if (Caret.Visible == 1) {
2729 Caret.Paused = true;
2733 if (Caret.Visible == 1) {
2735 Caret.Paused = false;
2737 msg.message = Msg.WM_PAINT;
2741 case XEventName.DestroyNotify: {
2743 // This is a bit tricky, we don't receive our own DestroyNotify, we only get those for our children
2744 hwnd = Hwnd.ObjectFromHandle(xevent.DestroyWindowEvent.window);
2746 // We may get multiple for the same window, act only one the first (when Hwnd still knows about it)
2748 msg.hwnd = hwnd.client_window;
2749 msg.message=Msg.WM_DESTROY;
2753 Console.WriteLine("Got DestroyNotify on Window {0:X}", msg.hwnd.ToInt32());
2756 goto ProcessNextMessage;
2762 case XEventName.ClientMessage: {
2763 if (Dnd.HandleClientMessage (ref xevent)) {
2764 goto ProcessNextMessage;
2767 if (xevent.ClientMessageEvent.message_type == (IntPtr)AsyncAtom) {
2768 XplatUIDriverSupport.ExecuteClientMessage((GCHandle)xevent.ClientMessageEvent.ptr1);
2772 if (xevent.ClientMessageEvent.message_type == (IntPtr)HoverState.Atom) {
2773 msg.message = Msg.WM_MOUSEHOVER;
2774 msg.wParam = GetMousewParam(0);
2775 msg.lParam = (IntPtr) (xevent.ClientMessageEvent.ptr1);
2779 if (xevent.ClientMessageEvent.message_type == (IntPtr)PostAtom) {
2780 msg.hwnd = xevent.ClientMessageEvent.ptr1;
2781 msg.message = (Msg) xevent.ClientMessageEvent.ptr2.ToInt32 ();
2782 msg.wParam = xevent.ClientMessageEvent.ptr3;
2783 msg.lParam = xevent.ClientMessageEvent.ptr4;
2788 if (xevent.ClientMessageEvent.message_type == (IntPtr)NetAtoms[(int)NA._XEMBED]) {
2789 Console.WriteLine("GOT EMBED MESSAGE {0:X}", xevent.ClientMessageEvent.ptr2.ToInt32());
2794 if (xevent.ClientMessageEvent.message_type == (IntPtr)NetAtoms[(int)NA.WM_PROTOCOLS]) {
2795 if (xevent.ClientMessageEvent.ptr1 == (IntPtr)NetAtoms[(int)NA.WM_DELETE_WINDOW]) {
2796 msg.message = Msg.WM_CLOSE;
2797 Graphics.FromHdcInternal (IntPtr.Zero);
2801 // We should not get this, but I'll leave the code in case we need it in the future
2802 if (xevent.ClientMessageEvent.ptr1 == (IntPtr)NetAtoms[(int)NA.WM_TAKE_FOCUS]) {
2803 goto ProcessNextMessage;
2809 case XEventName.TimerNotify: {
2810 xevent.TimerNotifyEvent.handler (this, EventArgs.Empty);
2815 goto ProcessNextMessage;
2819 return GetMessageResult;
2822 internal override bool GetText(IntPtr handle, out string text) {
2825 textptr = IntPtr.Zero;
2828 // FIXME - use _NET properties
2829 XFetchName(DisplayHandle, Hwnd.ObjectFromHandle(handle).whole_window, ref textptr);
2831 if (textptr != IntPtr.Zero) {
2832 text = Marshal.PtrToStringAnsi(textptr);
2841 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) {
2845 hwnd = Hwnd.ObjectFromHandle(handle);
2851 height = hwnd.height;
2853 rect = Hwnd.GetClientRectangle(hwnd.border_style, hwnd.menu_handle, hwnd.title_style, width, height);
2855 client_width = rect.Width;
2856 client_height = rect.Height;
2861 // Should we throw an exception or fail silently?
2862 // throw new ArgumentException("Called with an invalid window handle", "handle");
2872 internal override FormWindowState GetWindowState(IntPtr handle) {
2877 IntPtr prop = IntPtr.Zero;
2880 XWindowAttributes attributes;
2883 hwnd = Hwnd.ObjectFromHandle(handle);
2886 XGetWindowProperty(DisplayHandle, hwnd.whole_window, NetAtoms[(int)NA._NET_WM_STATE], 0, 256, false, Atom.XA_ATOM, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
2887 if ((nitems > 0) && (prop != IntPtr.Zero)) {
2888 for (int i = 0; i < nitems; i++) {
2889 atom = (IntPtr)Marshal.ReadInt32(prop, i * 4);
2890 if ((atom == (IntPtr)NetAtoms[(int)NA._NET_WM_STATE_MAXIMIZED_HORZ]) || (atom == (IntPtr)NetAtoms[(int)NA._NET_WM_STATE_MAXIMIZED_VERT])) {
2897 if (maximized == 2) {
2898 return FormWindowState.Maximized;
2902 attributes = new XWindowAttributes();
2903 XGetWindowAttributes(DisplayHandle, handle, ref attributes);
2904 if (attributes.map_state == MapState.IsUnmapped) {
2905 return FormWindowState.Minimized;
2908 return FormWindowState.Normal;
2911 internal override void GrabInfo(out IntPtr handle, out bool GrabConfined, out Rectangle GrabArea) {
2913 GrabConfined = Grab.Confined;
2914 GrabArea = Grab.Area;
2917 internal override void GrabWindow(IntPtr handle, IntPtr confine_to_handle) {
2919 IntPtr confine_to_window;
2921 confine_to_window = IntPtr.Zero;
2923 if (confine_to_handle != IntPtr.Zero) {
2924 XWindowAttributes attributes = new XWindowAttributes();
2926 hwnd = Hwnd.ObjectFromHandle(confine_to_handle);
2929 XGetWindowAttributes(DisplayHandle, hwnd.client_window, ref attributes);
2931 Grab.Area.X = attributes.x;
2932 Grab.Area.Y = attributes.y;
2933 Grab.Area.Width = attributes.width;
2934 Grab.Area.Height = attributes.height;
2935 Grab.Confined = true;
2936 confine_to_window = hwnd.client_window;
2941 hwnd = Hwnd.ObjectFromHandle(handle);
2944 XGrabPointer(DisplayHandle, hwnd.client_window, false,
2945 EventMask.ButtonPressMask | EventMask.ButtonMotionMask |
2946 EventMask.ButtonReleaseMask | EventMask.PointerMotionMask,
2947 GrabMode.GrabModeAsync, GrabMode.GrabModeAsync, confine_to_window, 0, 0);
2951 internal override void UngrabWindow(IntPtr hwnd) {
2953 XUngrabPointer(DisplayHandle, 0);
2954 XFlush(DisplayHandle);
2956 Grab.Hwnd = IntPtr.Zero;
2957 Grab.Confined = false;
2960 internal override void HandleException(Exception e) {
2961 StackTrace st = new StackTrace(e, true);
2962 Console.WriteLine("Exception '{0}'", e.Message+st.ToString());
2963 Console.WriteLine("{0}{1}", e.Message, st.ToString());
2966 internal override void Invalidate(IntPtr handle, Rectangle rc, bool clear) {
2970 hwnd = Hwnd.ObjectFromHandle(handle);
2973 xevent = new XEvent ();
2974 xevent.type = XEventName.Expose;
2975 xevent.ExposeEvent.display = DisplayHandle;
2976 xevent.ExposeEvent.window = hwnd.client_window;
2979 xevent.ExposeEvent.x = hwnd.X;
2980 xevent.ExposeEvent.y = hwnd.Y;
2981 xevent.ExposeEvent.width = hwnd.Width;
2982 xevent.ExposeEvent.height = hwnd.Height;
2984 xevent.ExposeEvent.x = rc.X;
2985 xevent.ExposeEvent.y = rc.Y;
2986 xevent.ExposeEvent.width = rc.Width;
2987 xevent.ExposeEvent.height = rc.Height;
2993 internal override bool IsVisible(IntPtr handle) {
2994 return Hwnd.ObjectFromHandle(handle).visible;
2997 internal override void KillTimer(Timer timer) {
2999 TimerList.Remove(timer);
3003 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {
3009 hwnd = Hwnd.ObjectFromHandle(handle);
3012 XTranslateCoordinates(DisplayHandle, hwnd.whole_window, RootWindow, x, y, out dest_x_return, out dest_y_return, out child);
3019 internal override void OverrideCursor(IntPtr cursor) {
3020 OverrideCursorHandle = cursor;
3023 internal override PaintEventArgs PaintEventStart(IntPtr handle, bool client) {
3024 PaintEventArgs paint_event;
3027 hwnd = Hwnd.ObjectFromHandle(handle);
3029 if (Caret.Visible == 1) {
3030 Caret.Paused = true;
3035 hwnd.client_dc = Graphics.FromHwnd (hwnd.client_window);
3036 hwnd.client_dc.SetClip(hwnd.invalid);
3037 paint_event = new PaintEventArgs(hwnd.client_dc, hwnd.invalid);
3041 hwnd.client_dc = Graphics.FromHwnd (hwnd.whole_window);
3042 paint_event = new PaintEventArgs(hwnd.client_dc, new Rectangle(0, 0, hwnd.width, hwnd.height));
3048 internal override void PaintEventEnd(IntPtr handle, bool client) {
3051 hwnd = Hwnd.ObjectFromHandle(handle);
3054 hwnd.ClearInvalidArea();
3057 hwnd.client_dc.Flush();
3058 hwnd.client_dc.Dispose();
3059 hwnd.client_dc = null;
3061 if (Caret.Visible == 1) {
3063 Caret.Paused = false;
3067 internal override bool PeekMessage(ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
3070 // FIXME - imlement filtering
3072 if ((flags & (uint)PeekMessageFlags.PM_REMOVE) == 0) {
3073 throw new NotImplementedException("PeekMessage PM_NOREMOVE is not implemented yet"); // FIXME - Implement PM_NOREMOVE flag
3077 if (MessageQueue.Count > 0) {
3080 // Only call UpdateMessageQueue if real events are pending
3081 // otherwise we go to sleep on the socket
3082 if (XPending(DisplayHandle) != 0) {
3083 UpdateMessageQueue();
3088 CheckTimers(DateTime.Now);
3093 return GetMessage(ref msg, hWnd, wFilterMin, wFilterMax);
3096 internal static void PostMessage (IntPtr handle, Msg message, IntPtr wparam, IntPtr lparam) {
3097 XEvent xevent = new XEvent ();
3098 Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
3100 xevent.type = XEventName.ClientMessage;
3101 xevent.ClientMessageEvent.display = DisplayHandle;
3104 xevent.ClientMessageEvent.window = hwnd.whole_window;
3106 xevent.ClientMessageEvent.window = IntPtr.Zero;
3108 xevent.ClientMessageEvent.message_type = (IntPtr) PostAtom;
3109 xevent.ClientMessageEvent.format = 32;
3110 xevent.ClientMessageEvent.ptr1 = handle;
3111 xevent.ClientMessageEvent.ptr2 = (IntPtr) message;
3112 xevent.ClientMessageEvent.ptr3 = wparam;
3113 xevent.ClientMessageEvent.ptr4 = lparam;
3115 MessageQueue.Enqueue (xevent);
3118 internal override void ReleaseMenuDC(IntPtr handle, Graphics dc) {
3122 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y) {
3128 hwnd = Hwnd.ObjectFromHandle(handle);
3131 XTranslateCoordinates (DisplayHandle, RootWindow, hwnd.client_window, x, y, out dest_x_return, out dest_y_return, out child);
3138 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
3144 hwnd = Hwnd.ObjectFromHandle(handle);
3147 XTranslateCoordinates (DisplayHandle, RootWindow, hwnd.whole_window, x, y, out dest_x_return, out dest_y_return, out child);
3154 internal override void ScrollWindow(IntPtr handle, Rectangle area, int XAmount, int YAmount, bool with_children) {
3157 XGCValues gc_values;
3159 hwnd = Hwnd.ObjectFromHandle(handle);
3161 if (hwnd.invalid != Rectangle.Empty) {
3162 // BIG FAT WARNING. This only works with how we use this function right now
3163 // where we basically still scroll the whole window, but work around areas
3164 // that are covered by our children
3166 hwnd.invalid.X += XAmount;
3167 hwnd.invalid.Y += YAmount;
3169 if (hwnd.invalid.X < 0) {
3170 hwnd.invalid.Width += hwnd.invalid.X;
3174 if (hwnd.invalid.Y < 0) {
3175 hwnd.invalid.Height += hwnd.invalid.Y;
3180 gc_values = new XGCValues();
3182 if (with_children) {
3183 gc_values.subwindow_mode = GCSubwindowMode.IncludeInferiors;
3186 gc = XCreateGC(DisplayHandle, hwnd.client_window, 0, ref gc_values);
3188 XCopyArea(DisplayHandle, hwnd.client_window, hwnd.client_window, gc, area.X - XAmount, area.Y - YAmount, area.Width, area.Height, area.X, area.Y);
3190 // Generate an expose for the area exposed by the horizontal scroll
3192 hwnd.AddInvalidArea (area.X, area.Y, XAmount, area.Height);
3193 } else if (XAmount < 0) {
3194 hwnd.AddInvalidArea (XAmount + area.X + area.Width, area.Y, -XAmount, area.Height);
3197 // Generate an expose for the area exposed by the vertical scroll
3199 hwnd.AddInvalidArea (area.X, area.Y, area.Width, YAmount);
3200 } else if (YAmount < 0) {
3201 hwnd.AddInvalidArea (area.X, YAmount + area.Y + area.Height, area.Width, -YAmount);
3203 XFreeGC(DisplayHandle, gc);
3205 UpdateWindow(handle);
3208 internal override void ScrollWindow(IntPtr handle, int XAmount, int YAmount, bool with_children) {
3211 hwnd = Hwnd.GetObjectFromWindow(handle);
3213 ScrollWindow(handle, hwnd.ClientRect, XAmount, YAmount, with_children);
3216 internal override void SendAsyncMethod (AsyncMethodData method) {
3217 XEvent xevent = new XEvent ();
3219 xevent.type = XEventName.ClientMessage;
3220 xevent.ClientMessageEvent.display = DisplayHandle;
3221 xevent.ClientMessageEvent.window = FosterParent;
3222 xevent.ClientMessageEvent.message_type = (IntPtr)AsyncAtom;
3223 xevent.ClientMessageEvent.format = 32;
3224 xevent.ClientMessageEvent.ptr1 = (IntPtr) GCHandle.Alloc (method);
3226 MessageQueue.EnqueueLocked (xevent);
3231 internal override void SetAllowDrop (IntPtr handle, bool value)
3233 // We allow drop on all windows
3236 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
3239 hwnd = Hwnd.ObjectFromHandle(handle);
3241 hwnd.border_style = border_style;
3243 XMoveResizeWindow(DisplayHandle, hwnd.client_window, hwnd.ClientRect.X, hwnd.ClientRect.Y, hwnd.ClientRect.Width, hwnd.ClientRect.Height);
3245 InvalidateWholeWindow(handle);
3248 internal override void SetCaretPos(IntPtr handle, int x, int y) {
3249 if (Caret.Hwnd == handle) {
3256 if (Caret.Visible == 1) {
3258 Caret.Timer.Start();
3263 internal override void SetCursor(IntPtr handle, IntPtr cursor) {
3266 if (OverrideCursorHandle == IntPtr.Zero) {
3267 if ((LastCursorWindow == handle) && (LastCursorHandle == cursor)) {
3271 LastCursorHandle = cursor;
3272 LastCursorWindow = handle;
3274 hwnd = Hwnd.ObjectFromHandle(handle);
3276 if (cursor != IntPtr.Zero) {
3277 XDefineCursor(DisplayHandle, hwnd.whole_window, cursor);
3279 XUndefineCursor(DisplayHandle, hwnd.whole_window);
3285 hwnd = Hwnd.ObjectFromHandle(handle);
3287 XDefineCursor(DisplayHandle, hwnd.whole_window, OverrideCursorHandle);
3291 internal override void SetCursorPos(IntPtr handle, int x, int y) {
3292 if (handle == IntPtr.Zero) {
3294 XWarpPointer(DisplayHandle, IntPtr.Zero, IntPtr.Zero, 0, 0, 0, 0, x, y);
3300 hwnd = Hwnd.ObjectFromHandle(handle);
3302 XWarpPointer(DisplayHandle, IntPtr.Zero, hwnd.client_window, 0, 0, 0, 0, x, y);
3308 internal override void SetFocus(IntPtr handle) {
3311 hwnd = Hwnd.ObjectFromHandle(handle);
3313 if (FocusWindow != IntPtr.Zero) {
3314 PostMessage(FocusWindow, Msg.WM_KILLFOCUS, hwnd.client_window, IntPtr.Zero);
3316 PostMessage(hwnd.client_window, Msg.WM_SETFOCUS, FocusWindow, IntPtr.Zero);
3317 FocusWindow = hwnd.client_window;
3319 //XSetInputFocus(DisplayHandle, Hwnd.ObjectFromHandle(handle).client_window, RevertTo.None, IntPtr.Zero);
3322 internal override void SetIcon(IntPtr handle, Icon icon) {
3325 hwnd = Hwnd.ObjectFromHandle(handle);
3327 SetIcon(hwnd, icon);
3331 internal override void SetMenu(IntPtr handle, IntPtr menu_handle) {
3334 hwnd = Hwnd.ObjectFromHandle(handle);
3335 hwnd.menu_handle = menu_handle;
3337 // FIXME - do we need to trigger some resize?
3340 internal override void SetModal(IntPtr handle, bool Modal) {
3342 ModalWindows.Push(handle);
3344 if (ModalWindows.Contains(handle)) {
3347 if (ModalWindows.Count > 0) {
3348 Activate((IntPtr)ModalWindows.Peek());
3353 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
3356 hwnd = Hwnd.ObjectFromHandle(handle);
3357 hwnd.parent = Hwnd.ObjectFromHandle(parent);
3361 Console.WriteLine("Parent for window {0:X} / {1:X} = {2:X} (Handle:{3:X})", hwnd.ClientWindow.ToInt32(), hwnd.WholeWindow.ToInt32(), hwnd.parent != null ? hwnd.parent.Handle.ToInt32() : 0, parent.ToInt32());
3363 XReparentWindow(DisplayHandle, hwnd.whole_window, hwnd.parent.client_window, hwnd.x, hwnd.y);
3369 internal override void SetTimer (Timer timer) {
3371 TimerList.Add(timer);
3376 internal override bool SetTopmost(IntPtr handle, IntPtr handle_owner, bool enabled) {
3380 hwnd = Hwnd.ObjectFromHandle(handle);
3382 if (handle_owner != IntPtr.Zero) {
3383 hwnd_owner = Hwnd.ObjectFromHandle(handle_owner);
3390 if (hwnd_owner != null) {
3391 XSetTransientForHint(DisplayHandle, hwnd.whole_window, hwnd_owner.whole_window);
3393 XSetTransientForHint(DisplayHandle, hwnd.whole_window, FosterParent);
3398 XDeleteProperty(DisplayHandle, hwnd.whole_window, (int)Atom.XA_WM_TRANSIENT_FOR);
3404 internal override bool SetVisible(IntPtr handle, bool visible) {
3407 hwnd = Hwnd.ObjectFromHandle(handle);
3408 hwnd.visible = visible;
3412 XMapWindow(DisplayHandle, hwnd.whole_window);
3413 XMapWindow(DisplayHandle, hwnd.client_window);
3415 XUnmapWindow(DisplayHandle, hwnd.whole_window);
3421 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
3423 Rectangle client_rect;
3425 hwnd = Hwnd.ObjectFromHandle(handle);
3427 // X requires a sanity check for width & height; otherwise it dies
3428 if (hwnd.zero_sized && width > 0 && height > 0) {
3430 XMapWindow(DisplayHandle, hwnd.whole_window);
3432 hwnd.zero_sized = false;
3436 hwnd.zero_sized = true;
3437 XUnmapWindow(DisplayHandle, hwnd.whole_window);
3441 hwnd.zero_sized = true;
3442 XUnmapWindow(DisplayHandle, hwnd.whole_window);
3445 client_rect = Hwnd.GetClientRectangle(hwnd.border_style, hwnd.menu_handle, hwnd.title_style, width, height);
3447 // Save a server roundtrip (and prevent a feedback loop)
3448 if ((hwnd.x == x) && (hwnd.y == y) &&
3449 (hwnd.width == width) && (hwnd.height == height) &&
3450 (hwnd.ClientRect == client_rect)) {
3454 if (!hwnd.zero_sized) {
3456 XMoveResizeWindow(DisplayHandle, hwnd.whole_window, x, y, width, height);
3457 XMoveResizeWindow(DisplayHandle, hwnd.client_window, client_rect.X, client_rect.Y, client_rect.Width, client_rect.Height);
3461 // Prevent an old queued ConfigureNotify from setting our width with outdated data, set it now
3463 hwnd.height = height;
3466 internal override void SetWindowState(IntPtr handle, FormWindowState state) {
3467 FormWindowState current_state;
3470 hwnd = Hwnd.ObjectFromHandle(handle);
3472 current_state = GetWindowState(handle);
3474 if (current_state == state) {
3479 case FormWindowState.Normal: {
3481 if (current_state == FormWindowState.Minimized) {
3482 XMapWindow(DisplayHandle, hwnd.whole_window);
3483 XMapWindow(DisplayHandle, hwnd.client_window);
3484 } else if (current_state == FormWindowState.Maximized) {
3485 SendNetWMMessage(hwnd.whole_window, (IntPtr)(uint)NetAtoms[(int)NA._NET_WM_STATE], (IntPtr)2 /* toggle */, (IntPtr)NetAtoms[(int)NA._NET_WM_STATE_MAXIMIZED_HORZ], (IntPtr)NetAtoms[(int)NA._NET_WM_STATE_MAXIMIZED_VERT]);
3492 case FormWindowState.Minimized: {
3494 if (current_state == FormWindowState.Maximized) {
3495 SendNetWMMessage(hwnd.whole_window, (IntPtr)NetAtoms[(int)NA._NET_WM_STATE], (IntPtr)2 /* toggle */, (IntPtr)NetAtoms[(int)NA._NET_WM_STATE_MAXIMIZED_HORZ], (IntPtr)NetAtoms[(int)NA._NET_WM_STATE_MAXIMIZED_VERT]);
3497 XIconifyWindow(DisplayHandle, hwnd.whole_window, 0);
3502 case FormWindowState.Maximized: {
3504 if (current_state == FormWindowState.Minimized) {
3505 XMapWindow(DisplayHandle, hwnd.whole_window);
3506 XMapWindow(DisplayHandle, hwnd.client_window);
3509 SendNetWMMessage(hwnd.whole_window, (IntPtr)NetAtoms[(int)NA._NET_WM_STATE], (IntPtr)1 /* Add */, (IntPtr)NetAtoms[(int)NA._NET_WM_STATE_MAXIMIZED_HORZ], (IntPtr)NetAtoms[(int)NA._NET_WM_STATE_MAXIMIZED_VERT]);
3517 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
3520 hwnd = Hwnd.ObjectFromHandle(handle);
3521 SetHwndStyles(hwnd, cp);
3522 SetWMStyles(hwnd, cp);
3524 internal override bool SetZOrder(IntPtr handle, IntPtr after_handle, bool top, bool bottom) {
3525 Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
3529 XRaiseWindow(DisplayHandle, hwnd.whole_window);
3532 } else if (!bottom) {
3533 Hwnd after_hwnd = null;
3535 if (after_handle != IntPtr.Zero) {
3536 after_hwnd = Hwnd.ObjectFromHandle(after_handle);
3539 XWindowChanges values = new XWindowChanges();
3541 if (after_hwnd == null) {
3542 throw new ArgumentNullException("after_handle", "Need sibling to adjust z-order");
3544 values.sibling = after_hwnd.whole_window;
3545 values.stack_mode = StackMode.Below;
3548 XConfigureWindow(DisplayHandle, hwnd.whole_window, ChangeWindowFlags.CWStackMode | ChangeWindowFlags.CWSibling, ref values);
3553 XLowerWindow(DisplayHandle, hwnd.whole_window);
3560 internal override void ShowCursor(bool show) {
3561 ; // FIXME - X11 doesn't 'hide' the cursor. we could create an empty cursor
3564 internal override bool SystrayAdd(IntPtr handle, string tip, Icon icon, out ToolTip tt) {
3565 GetSystrayManagerWindow();
3567 if (SystrayMgrWindow != IntPtr.Zero) {
3569 XSizeHints size_hints;
3572 hwnd = Hwnd.ObjectFromHandle(handle);
3574 Console.WriteLine("Adding Systray Whole:{0:X}, Client:{1:X}", hwnd.whole_window.ToInt32(), hwnd.client_window.ToInt32());
3577 XUnmapWindow(DisplayHandle, hwnd.whole_window);
3578 XUnmapWindow(DisplayHandle, hwnd.client_window);
3581 XDestroyWindow(DisplayHandle, hwnd.client_window);
3582 hwnd.client_window = hwnd.whole_window;
3584 size_hints = new XSizeHints();
3586 size_hints.flags = (IntPtr)(XSizeHintsFlags.PMinSize | XSizeHintsFlags.PMaxSize | XSizeHintsFlags.PBaseSize);
3587 size_hints.min_width = icon.Width;
3588 size_hints.min_height = icon.Height;
3590 size_hints.max_width = icon.Width;
3591 size_hints.max_height = icon.Height;
3593 size_hints.base_width = icon.Width;
3594 size_hints.base_height = icon.Height;
3595 XSetWMNormalHints(DisplayHandle, hwnd.whole_window, ref size_hints);
3597 atoms = new uint[2];
3598 atoms[0] = 1; // Version 1
3599 atoms[1] = 0; // We're not mapped
3601 // This line cost me 3 days...
3602 XChangeProperty(DisplayHandle, hwnd.whole_window, NetAtoms[(int)NA._XEMBED_INFO], NetAtoms[(int)NA._XEMBED_INFO], 32, PropertyMode.Replace, atoms, 2);
3604 // Need to pick some reasonable defaults
3606 tt.AutomaticDelay = 100;
3607 tt.InitialDelay = 250;
3608 tt.ReshowDelay = 250;
3609 tt.ShowAlways = true;
3611 if ((tip != null) && (tip != string.Empty)) {
3612 tt.SetToolTip(Control.FromHandle(handle), tip);
3618 // Make sure the window exists
3619 XSync(DisplayHandle, hwnd.whole_window);
3621 SendNetClientMessage(SystrayMgrWindow, (IntPtr)NetAtoms[(int)NA._NET_SYSTEM_TRAY_OPCODE], IntPtr.Zero, (IntPtr)SystrayRequest.SYSTEM_TRAY_REQUEST_DOCK, hwnd.whole_window);
3628 internal override bool SystrayChange(IntPtr handle, string tip, Icon icon, ref ToolTip tt) {
3631 control = Control.FromHandle(handle);
3632 if (control != null && tt != null) {
3633 tt.SetToolTip(control, tip);
3641 internal override void SystrayRemove(IntPtr handle, ref ToolTip tt) {
3644 hwnd = Hwnd.ObjectFromHandle(handle);
3646 XUnmapWindow(DisplayHandle, hwnd.whole_window);
3647 SetParent(hwnd.whole_window, FosterParent);
3649 // The caller can now re-dock it later...
3656 internal override bool Text(IntPtr handle, string text) {
3658 // FIXME - use _NET properties
3659 XStoreName(DisplayHandle, Hwnd.ObjectFromHandle(handle).whole_window, text);
3664 internal override bool TranslateMessage(ref MSG msg) {
3665 return Keyboard.TranslateMessage (ref msg);
3668 internal override void UpdateWindow(IntPtr handle) {
3672 hwnd = Hwnd.ObjectFromHandle(handle);
3674 if (!hwnd.visible || hwnd.expose_pending) {
3678 xevent = new XEvent();
3679 xevent.type = XEventName.Expose;
3680 xevent.ExposeEvent.display = DisplayHandle;
3681 xevent.ExposeEvent.window = hwnd.client_window;
3683 MessageQueue.Enqueue(xevent);
3684 hwnd.expose_pending = true;
3686 // This would force an immediate paint (SendMessage, instead of PostMessage)
3687 if (!hwnd.visible) {
3691 if (!hwnd.expose_pending) {
3692 xevent = new XEvent();
3693 xevent.type = XEventName.Expose;
3694 xevent.ExposeEvent.display = DisplayHandle;
3695 xevent.ExposeEvent.window = hwnd.client_window;
3697 MessageQueue.Enqueue(xevent);
3698 hwnd.expose_pending = true;
3700 NativeWindow.WndProc(hwnd.client_window, Msg.WM_PAINT, IntPtr.Zero, IntPtr.Zero);
3703 #endregion // Public Static Methods
3706 internal override event EventHandler Idle;
3707 #endregion // Events
3710 [DllImport ("libX11", EntryPoint="XOpenDisplay")]
3711 internal extern static IntPtr XOpenDisplay(IntPtr display);
3712 [DllImport ("libX11", EntryPoint="XCloseDisplay")]
3713 internal extern static int XCloseDisplay(IntPtr display);
3714 [DllImport ("libX11", EntryPoint="XSynchronize")]
3715 internal extern static IntPtr XSynchronize(IntPtr display, bool onoff);
3717 [DllImport ("libX11", EntryPoint="XCreateWindow")]
3718 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, SetWindowValuemask valuemask, ref XSetWindowAttributes attributes);
3719 [DllImport ("libX11", EntryPoint="XCreateSimpleWindow")]
3720 internal extern static IntPtr XCreateSimpleWindow(IntPtr display, IntPtr parent, int x, int y, int width, int height, int border_width, int border, int background);
3721 [DllImport ("libX11", EntryPoint="XMapWindow")]
3722 internal extern static int XMapWindow(IntPtr display, IntPtr window);
3723 [DllImport ("libX11", EntryPoint="XUnmapWindow")]
3724 internal extern static int XUnmapWindow(IntPtr display, IntPtr window);
3725 [DllImport ("libX11", EntryPoint="XMapSubwindows")]
3726 internal extern static int XMapSubindows(IntPtr display, IntPtr window);
3727 [DllImport ("libX11", EntryPoint="XUnmapSubwindows")]
3728 internal extern static int XUnmapSubwindows(IntPtr display, IntPtr window);
3729 [DllImport ("libX11", EntryPoint="XRootWindow")]
3730 internal extern static IntPtr XRootWindow(IntPtr display, int screen_number);
3731 [DllImport ("libX11", EntryPoint="XNextEvent")]
3732 internal extern static IntPtr XNextEvent(IntPtr display, ref XEvent xevent);
3733 [DllImport ("libX11")]
3734 internal extern static int XConnectionNumber (IntPtr diplay);
3735 [DllImport ("libX11")]
3736 internal extern static int XPending (IntPtr diplay);
3737 [DllImport ("libX11")]
3738 internal extern static bool XCheckWindowEvent (IntPtr display, IntPtr window, EventMask mask, ref XEvent xevent);
3739 [DllImport ("libX11")]
3740 internal extern static bool XCheckMaskEvent (IntPtr display, EventMask mask, ref XEvent xevent);
3741 [DllImport ("libX11", EntryPoint="XSelectInput")]
3742 internal extern static IntPtr XSelectInput(IntPtr display, IntPtr window, EventMask mask);
3744 [DllImport ("libX11", EntryPoint="XDestroyWindow")]
3745 internal extern static int XDestroyWindow(IntPtr display, IntPtr window);
3747 [DllImport ("libX11", EntryPoint="XReparentWindow")]
3748 internal extern static int XReparentWindow(IntPtr display, IntPtr window, IntPtr parent, int x, int y);
3749 [DllImport ("libX11", EntryPoint="XMoveResizeWindow")]
3750 internal extern static int XMoveResizeWindow(IntPtr display, IntPtr window, int x, int y, int width, int height);
3752 [DllImport ("libX11", EntryPoint="XResizeWindow")]
3753 internal extern static int XResizeWindow(IntPtr display, IntPtr window, int width, int height);
3755 [DllImport ("libX11", EntryPoint="XGetWindowAttributes")]
3756 internal extern static int XGetWindowAttributes(IntPtr display, IntPtr window, ref XWindowAttributes attributes);
3758 [DllImport ("libX11", EntryPoint="XFlush")]
3759 internal extern static int XFlush(IntPtr display);
3761 [DllImport ("libX11", EntryPoint="XSetWMName")]
3762 internal extern static int XSetWMName(IntPtr display, IntPtr window, ref XTextProperty text_prop);
3764 [DllImport ("libX11", EntryPoint="XStoreName")]
3765 internal extern static int XStoreName(IntPtr display, IntPtr window, string window_name);
3767 [DllImport ("libX11", EntryPoint="XFetchName")]
3768 internal extern static int XFetchName(IntPtr display, IntPtr window, ref IntPtr window_name);
3770 [DllImport ("libX11", EntryPoint="XSendEvent")]
3771 internal extern static int XSendEvent(IntPtr display, IntPtr window, bool propagate, EventMask event_mask, ref XEvent send_event);
3773 [DllImport ("libX11", EntryPoint="XQueryTree")]
3774 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);
3776 [DllImport ("libX11", EntryPoint="XFree")]
3777 internal extern static int XFree(IntPtr data);
3779 [DllImport ("libX11", EntryPoint="XRaiseWindow")]
3780 internal extern static int XRaiseWindow(IntPtr display, IntPtr window);
3782 [DllImport ("libX11", EntryPoint="XLowerWindow")]
3783 internal extern static uint XLowerWindow(IntPtr display, IntPtr window);
3785 [DllImport ("libX11", EntryPoint="XConfigureWindow")]
3786 internal extern static uint XConfigureWindow(IntPtr display, IntPtr window, ChangeWindowFlags value_mask, ref XWindowChanges values);
3788 [DllImport ("libX11", EntryPoint="XInternAtom")]
3789 internal extern static int XInternAtom(IntPtr display, string atom_name, bool only_if_exists);
3791 [DllImport ("libX11", EntryPoint="XSetWMProtocols")]
3792 internal extern static int XSetWMProtocols(IntPtr display, IntPtr window, uint[] protocols, int count);
3794 [DllImport ("libX11", EntryPoint="XGrabPointer")]
3795 internal extern static int XGrabPointer(IntPtr display, IntPtr window, bool owner_events, EventMask event_mask, GrabMode pointer_mode, GrabMode keyboard_mode, IntPtr confine_to, uint cursor, uint timestamp);
3797 [DllImport ("libX11", EntryPoint="XUngrabPointer")]
3798 internal extern static int XUngrabPointer(IntPtr display, uint timestamp);
3800 [DllImport ("libX11", EntryPoint="XQueryPointer")]
3801 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);
3803 [DllImport ("libX11", EntryPoint="XTranslateCoordinates")]
3804 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);
3806 [DllImport ("libX11", EntryPoint="XGetGeometry")]
3807 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);
3809 [DllImport ("libX11", EntryPoint="XGetGeometry")]
3810 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);
3812 [DllImport ("libX11", EntryPoint="XGetGeometry")]
3813 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);
3815 [DllImport ("libX11", EntryPoint="XGetGeometry")]
3816 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);
3818 [DllImport ("libX11", EntryPoint="XWarpPointer")]
3819 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);
3821 [DllImport ("libX11", EntryPoint="XClearWindow")]
3822 internal extern static int XClearWindow(IntPtr display, IntPtr window);
3824 [DllImport ("libX11", EntryPoint="XClearArea")]
3825 internal extern static int XClearArea(IntPtr display, IntPtr window, int x, int y, int width, int height, bool exposures);
3828 [DllImport ("libX11", EntryPoint="XDefaultScreenOfDisplay")]
3829 internal extern static IntPtr XDefaultScreenOfDisplay(IntPtr display);
3831 [DllImport ("libX11", EntryPoint="XScreenNumberOfScreen")]
3832 internal extern static int XScreenNumberOfScreen(IntPtr display, IntPtr Screen);
3834 [DllImport ("libX11", EntryPoint="XDefaultVisual")]
3835 internal extern static IntPtr XDefaultVisual(IntPtr display, int screen_number);
3837 [DllImport ("libX11", EntryPoint="XDefaultDepth")]
3838 internal extern static uint XDefaultDepth(IntPtr display, int screen_number);
3840 [DllImport ("libX11", EntryPoint="XDefaultColormap")]
3841 internal extern static IntPtr XDefaultColormap(IntPtr display, int screen_number);
3843 [DllImport ("libX11", EntryPoint="XLookupColor")]
3844 internal extern static int XLookupColor(IntPtr display, IntPtr Colormap, string Coloranem, ref XColor exact_def_color, ref XColor screen_def_color);
3846 [DllImport ("libX11", EntryPoint="XAllocColor")]
3847 internal extern static int XAllocColor(IntPtr display, IntPtr Colormap, ref XColor colorcell_def);
3849 [DllImport ("libX11", EntryPoint="XSetTransientForHint")]
3850 internal extern static int XSetTransientForHint(IntPtr display, IntPtr window, IntPtr prop_window);
3852 [DllImport ("libX11", EntryPoint="XChangeProperty")]
3853 internal extern static int XChangeProperty(IntPtr display, IntPtr window, int property, int type, int format, PropertyMode mode, ref MotifWmHints data, int nelements);
3855 [DllImport ("libX11", EntryPoint="XChangeProperty")]
3856 internal extern static int XChangeProperty(IntPtr display, IntPtr window, int property, Atom format, int type, PropertyMode mode, uint[] atoms, int nelements);
3858 [DllImport ("libX11", EntryPoint="XChangeProperty")]
3859 internal extern static int XChangeProperty(IntPtr display, IntPtr window, int property, int format, int type, PropertyMode mode, uint[] atoms, int nelements);
3861 [DllImport ("libX11", EntryPoint="XChangeProperty")]
3862 internal extern static int XChangeProperty(IntPtr display, IntPtr window, int property, int format, int type, PropertyMode mode, IntPtr atoms, int nelements);
3864 [DllImport ("libX11", EntryPoint="XChangeProperty")]
3865 internal extern static int XChangeProperty(IntPtr display, IntPtr window, int property, Atom format, int type, PropertyMode mode, IntPtr atoms, int nelements);
3867 [DllImport ("libX11", EntryPoint="XChangeProperty", CharSet=CharSet.Ansi)]
3868 internal extern static int XChangeProperty(IntPtr display, IntPtr window, int property, int type, int format, PropertyMode mode, string text, int text_length);
3870 [DllImport ("libX11", EntryPoint="XDeleteProperty")]
3871 internal extern static int XDeleteProperty(IntPtr display, IntPtr window, int property);
3873 [DllImport ("gdiplus", EntryPoint="GetFontMetrics")]
3874 internal extern static bool GetFontMetrics(IntPtr graphicsObject, IntPtr nativeObject, out int ascent, out int descent);
3877 [DllImport ("libX11", EntryPoint="XCreateGC")]
3878 internal extern static IntPtr XCreateGC(IntPtr display, IntPtr window, GCFunction valuemask, ref XGCValues values);
3880 [DllImport ("libX11", EntryPoint="XFreeGC")]
3881 internal extern static int XFreeGC(IntPtr display, IntPtr gc);
3883 [DllImport ("libX11", EntryPoint="XSetFunction")]
3884 internal extern static int XSetFunction(IntPtr display, IntPtr gc, GXFunction function);
3886 [DllImport ("libX11", EntryPoint="XDrawLine")]
3887 internal extern static int XDrawLine(IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int x2, int y2);
3889 [DllImport ("libX11", EntryPoint="XDrawRectangle")]
3890 internal extern static int XDrawRectangle(IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int width, int height);
3892 [DllImport ("libX11", EntryPoint="XSetWindowBackground")]
3893 internal extern static int XSetWindowBackground(IntPtr display, IntPtr window, IntPtr background);
3895 [DllImport ("libX11", EntryPoint="XCopyArea")]
3896 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);
3898 [DllImport ("libX11", EntryPoint="XGetAtomName")]
3899 internal extern static string XGetAtomName(IntPtr display, int atom);
3901 [DllImport ("libX11", EntryPoint="XGetWindowProperty")]
3902 internal extern static int XGetWindowProperty(IntPtr display, IntPtr window, int atom, int long_offset, int long_length, bool delete, Atom req_type, out Atom actual_type, out int actual_format, out int nitems, out int bytes_after, ref IntPtr prop);
3904 [DllImport ("libX11", EntryPoint="XSetInputFocus")]
3905 internal extern static int XSetInputFocus(IntPtr display, IntPtr window, RevertTo revert_to, IntPtr time);
3907 [DllImport ("libX11", EntryPoint="XIconifyWindow")]
3908 internal extern static int XIconifyWindow(IntPtr display, IntPtr window, int screen_number);
3910 [DllImport ("libX11", EntryPoint="XDefineCursor")]
3911 internal extern static int XDefineCursor(IntPtr display, IntPtr window, IntPtr cursor);
3913 [DllImport ("libX11", EntryPoint="XUndefineCursor")]
3914 internal extern static int XUndefineCursor(IntPtr display, IntPtr window);
3916 [DllImport ("libX11", EntryPoint="XFreeCursor")]
3917 internal extern static int XFreeCursor(IntPtr display, IntPtr cursor);
3919 [DllImport ("libX11", EntryPoint="XCreateFontCursor")]
3920 internal extern static IntPtr XCreateFontCursor(IntPtr display, CursorFontShape shape);
3922 [DllImport ("libX11", EntryPoint="XCreatePixmapCursor")]
3923 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);
3925 [DllImport ("libX11", EntryPoint="XCreatePixmapFromBitmapData")]
3926 internal extern static IntPtr XCreatePixmapFromBitmapData(IntPtr display, IntPtr drawable, byte[] data, int width, int height, IntPtr fg, IntPtr bg, int depth);
3928 [DllImport ("libX11", EntryPoint="XFreePixmap")]
3929 internal extern static IntPtr XFreePixmap(IntPtr display, IntPtr pixmap);
3931 [DllImport ("libX11", EntryPoint="XQueryBestCursor")]
3932 internal extern static int XQueryBestCursor(IntPtr display, IntPtr drawable, int width, int height, out int best_width, out int best_height);
3934 [DllImport ("libX11", EntryPoint="XWhitePixel")]
3935 internal extern static IntPtr XWhitePixel(IntPtr display, int screen_no);
3937 [DllImport ("libX11", EntryPoint="XBlackPixel")]
3938 internal extern static IntPtr XBlackPixel(IntPtr display, int screen_no);
3940 [DllImport ("libX11", EntryPoint="XGrabServer")]
3941 internal extern static void XGrabServer(IntPtr display);
3943 [DllImport ("libX11", EntryPoint="XUngrabServer")]
3944 internal extern static void XUngrabServer(IntPtr display);
3946 [DllImport ("libX11", EntryPoint="XSetWMNormalHints")]
3947 internal extern static void XSetWMNormalHints(IntPtr display, IntPtr window, ref XSizeHints hints);
3949 [DllImport ("libX11", EntryPoint="XSetWMHints")]
3950 internal extern static void XSetWMHints(IntPtr display, IntPtr window, ref XWMHints wmhints);
3952 [DllImport ("libX11", EntryPoint="XSync")]
3953 internal extern static void XSync(IntPtr display, IntPtr window);
3955 [DllImport ("libX11", EntryPoint="XGetIconSizes")]
3956 internal extern static int XGetIconSizes(IntPtr display, IntPtr window, out IntPtr size_list, out int count);
3958 [DllImport ("libX11", EntryPoint="XSetErrorHandler")]
3959 internal extern static IntPtr XSetErrorHandler(XErrorHandler error_handler);
3961 [DllImport ("libX11", EntryPoint="XGetErrorText")]
3962 internal extern static IntPtr XGetErrorText(IntPtr display, byte code, StringBuilder buffer, int length);
3964 [DllImport ("libX11", EntryPoint="XInitThreads")]
3965 internal extern static int XInitThreads();
3967 [DllImport ("libX11", EntryPoint="XConvertSelection")]
3968 internal extern static int XConvertSelection(IntPtr display, int selection, int target, int property, IntPtr requestor, IntPtr time);
3970 [DllImport ("libX11", EntryPoint="XGetSelectionOwner")]
3971 internal extern static IntPtr XGetSelectionOwner(IntPtr display, int selection);
3973 [DllImport ("libX11", EntryPoint="XSetSelectionOwner")]
3974 internal extern static int XSetSelectionOwner(IntPtr display, int selection, IntPtr owner, IntPtr time);
3976 [DllImport ("libX11", EntryPoint="XSetPlaneMask")]
3977 internal extern static int XSetPlaneMask(IntPtr display, IntPtr gc, uint mask);
3979 [DllImport ("libX11", EntryPoint="XSetForeground")]
3980 internal extern static int XSetForeground(IntPtr display, IntPtr gc, uint foreground);
3982 [DllImport ("libX11", EntryPoint="XSetBackground")]
3983 internal extern static int XSetBackground(IntPtr display, IntPtr gc, uint background);