1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2006 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
24 // Alexander Olk alex.olk@googlemail.com
29 // This driver understands the following environment variables: (Set the var to enable feature)
31 // MONO_XEXCEPTIONS = throw an exception when a X11 error is encountered;
32 // by default a message is displayed but execution continues
34 // MONO_XSYNC = perform all X11 commands synchronous; this is slower but
35 // helps in debugging errors
38 // NOT COMPLETE - WORK IN PROGRESS
40 // One feature of the driver is, that PaintEventstart returns a graphics context created from a offscreen drawable (pixmap)
42 // define to log Window handles and relationships to stdout
45 // Extra detailed debug
46 #undef DriverDebugExtra
49 using System.ComponentModel;
50 using System.Collections;
51 using System.Diagnostics;
53 using System.Drawing.Drawing2D;
54 using System.Drawing.Imaging;
57 using System.Net.Sockets;
58 using System.Reflection;
59 using System.Runtime.InteropServices;
61 using System.Threading;
63 // Only do the poll when building with mono for now
65 using Mono.Unix.Native;
69 namespace System.Windows.Forms {
70 internal class XplatUIX11GTK : XplatUIDriver {
72 internal enum GdkWindowClass {
77 internal enum GdkWindowType {
86 internal enum GdkWindowHints {
87 GDK_HINT_POS = 1 << 0,
88 GDK_HINT_MIN_SIZE = 1 << 1,
89 GDK_HINT_MAX_SIZE = 1 << 2,
90 GDK_HINT_BASE_SIZE = 1 << 3,
91 GDK_HINT_ASPECT = 1 << 4,
92 GDK_HINT_RESIZE_INC = 1 << 5,
93 GDK_HINT_WIN_GRAVITY = 1 << 6,
94 GDK_HINT_USER_POS = 1 << 7,
95 GDK_HINT_USER_SIZE = 1 << 8
98 internal enum GdkGravity {
99 GDK_GRAVITY_NORTH_WEST = 1,
101 GDK_GRAVITY_NORTH_EAST,
105 GDK_GRAVITY_SOUTH_WEST,
107 GDK_GRAVITY_SOUTH_EAST,
111 internal enum GdkWindowEdge {
112 GDK_WINDOW_EDGE_NORTH_WEST,
113 GDK_WINDOW_EDGE_NORTH,
114 GDK_WINDOW_EDGE_NORTH_EAST,
115 GDK_WINDOW_EDGE_WEST,
116 GDK_WINDOW_EDGE_EAST,
117 GDK_WINDOW_EDGE_SOUTH_WEST,
118 GDK_WINDOW_EDGE_SOUTH,
119 GDK_WINDOW_EDGE_SOUTH_EAST
122 internal enum GdkWindowTypeHint {
123 GDK_WINDOW_TYPE_HINT_NORMAL,
124 GDK_WINDOW_TYPE_HINT_DIALOG,
125 GDK_WINDOW_TYPE_HINT_MENU,
126 GDK_WINDOW_TYPE_HINT_TOOLBAR,
127 GDK_WINDOW_TYPE_HINT_SPLASHSCREEN,
128 GDK_WINDOW_TYPE_HINT_UTILITY,
129 GDK_WINDOW_TYPE_HINT_DOCK,
130 GDK_WINDOW_TYPE_HINT_DESKTOP
133 internal enum GdkWindowAttributesType {
134 GDK_WA_TITLE = 1 << 1,
137 GDK_WA_CURSOR = 1 << 4,
138 GDK_WA_COLORMAP = 1 << 5,
139 GDK_WA_VISUAL = 1 << 6,
140 GDK_WA_WMCLASS = 1 << 7,
141 GDK_WA_NOREDIR = 1 << 8
144 internal enum GdkEventMask {
145 GDK_EXPOSURE_MASK = 1 << 1,
146 GDK_POINTER_MOTION_MASK = 1 << 2,
147 GDK_POINTER_MOTION_HINT_MASK = 1 << 3,
148 GDK_BUTTON_MOTION_MASK = 1 << 4,
149 GDK_BUTTON1_MOTION_MASK = 1 << 5,
150 GDK_BUTTON2_MOTION_MASK = 1 << 6,
151 GDK_BUTTON3_MOTION_MASK = 1 << 7,
152 GDK_BUTTON_PRESS_MASK = 1 << 8,
153 GDK_BUTTON_RELEASE_MASK = 1 << 9,
154 GDK_KEY_PRESS_MASK = 1 << 10,
155 GDK_KEY_RELEASE_MASK = 1 << 11,
156 GDK_ENTER_NOTIFY_MASK = 1 << 12,
157 GDK_LEAVE_NOTIFY_MASK = 1 << 13,
158 GDK_FOCUS_CHANGE_MASK = 1 << 14,
159 GDK_STRUCTURE_MASK = 1 << 15,
160 GDK_PROPERTY_CHANGE_MASK = 1 << 16,
161 GDK_VISIBILITY_NOTIFY_MASK = 1 << 17,
162 GDK_PROXIMITY_IN_MASK = 1 << 18,
163 GDK_PROXIMITY_OUT_MASK = 1 << 19,
164 GDK_SUBSTRUCTURE_MASK = 1 << 20,
165 GDK_SCROLL_MASK = 1 << 21,
166 GDK_ALL_EVENTS_MASK = 0x3FFFFE
169 internal enum GdkEventType {
174 GDK_MOTION_NOTIFY = 3,
175 GDK_BUTTON_PRESS = 4,
176 GDK_2BUTTON_PRESS = 5,
177 GDK_3BUTTON_PRESS = 6,
178 GDK_BUTTON_RELEASE = 7,
181 GDK_ENTER_NOTIFY = 10,
182 GDK_LEAVE_NOTIFY = 11,
183 GDK_FOCUS_CHANGE = 12,
187 GDK_PROPERTY_NOTIFY = 16,
188 GDK_SELECTION_CLEAR = 17,
189 GDK_SELECTION_REQUEST = 18,
190 GDK_SELECTION_NOTIFY = 19,
191 GDK_PROXIMITY_IN = 20,
192 GDK_PROXIMITY_OUT = 21,
195 GDK_DRAG_MOTION = 24,
196 GDK_DRAG_STATUS = 25,
198 GDK_DROP_FINISHED = 27,
199 GDK_CLIENT_EVENT = 28,
200 GDK_VISIBILITY_NOTIFY = 29,
203 GDK_WINDOW_STATE = 32,
205 GDK_OWNER_CHANGE = 34,
209 internal enum GdkWMDecoration {
210 GDK_DECOR_ALL = 1 << 0,
211 GDK_DECOR_BORDER = 1 << 1,
212 GDK_DECOR_RESIZEH = 1 << 2,
213 GDK_DECOR_TITLE = 1 << 3,
214 GDK_DECOR_MENU = 1 << 4,
215 GDK_DECOR_MINIMIZE = 1 << 5,
216 GDK_DECOR_MAXIMIZE = 1 << 6
219 internal enum GdkWMFunction {
220 GDK_FUNC_ALL = 1 << 0,
221 GDK_FUNC_RESIZE = 1 << 1,
222 GDK_FUNC_MOVE = 1 << 2,
223 GDK_FUNC_MINIMIZE = 1 << 3,
224 GDK_FUNC_MAXIMIZE = 1 << 4,
225 GDK_FUNC_CLOSE = 1 << 5
228 internal enum GdkCursorType {
231 GDK_BASED_ARROW_DOWN = 4,
232 GDK_BASED_ARROW_UP = 6,
235 GDK_BOTTOM_LEFT_CORNER = 12,
236 GDK_BOTTOM_RIGHT_CORNER = 14,
237 GDK_BOTTOM_SIDE = 16,
245 GDK_CROSS_REVERSE = 32,
247 GDK_DIAMOND_CROSS = 36,
250 GDK_DOUBLE_ARROW = 42,
251 GDK_DRAFT_LARGE = 44,
252 GDK_DRAFT_SMALL = 46,
270 GDK_MIDDLEBUTTON = 82,
275 GDK_QUESTION_ARROW = 92,
279 GDK_RIGHTBUTTON = 100,
282 GDK_SB_DOWN_ARROW = 106,
283 GDK_SB_H_DOUBLE_ARROW = 108,
284 GDK_SB_LEFT_ARROW = 110,
285 GDK_SB_RIGHT_ARROW = 112,
286 GDK_SB_UP_ARROW = 114,
287 GDK_SB_V_DOUBLE_ARROW = 116,
295 GDK_TOP_LEFT_ARROW = 132,
296 GDK_TOP_LEFT_CORNER = 134,
297 GDK_TOP_RIGHT_CORNER = 136,
307 GDK_CURSOR_IS_PIXMAP = -1
310 internal enum GdkPropMode {
311 GDK_PROP_MODE_REPLACE,
312 GDK_PROP_MODE_PREPEND,
316 [StructLayout (LayoutKind.Sequential)]
317 internal struct GdkGeometry {
318 internal int min_width;
319 internal int min_height;
320 internal int max_width;
321 internal int max_height;
322 internal int base_width;
323 internal int base_height;
324 internal int width_inc;
325 internal int height_inc;
326 internal double min_aspect;
327 internal double max_aspect;
328 internal GdkGravity win_gravity;
331 [StructLayout (LayoutKind.Sequential)]
332 internal struct GdkWindowAttr {
333 internal string title;
334 internal int event_mask;
338 internal GdkWindowClass wclass;
339 internal IntPtr visual;
340 internal IntPtr colormap;
341 internal GdkWindowType window_type;
342 internal IntPtr cursor;
343 internal string wmclass_name;
344 internal string wmclass_class;
345 internal bool override_redirect;
348 #region Local Variables
350 static volatile XplatUIX11GTK Instance;
351 private static int RefCount;
352 private static object XlibLock; // Our locking object
353 private static bool ThemesEnabled;
356 private static IntPtr DisplayHandle; // X11 handle to display
357 private static IntPtr GdkDisplayHandle; // gdk handle to display
358 private static int ScreenNo; // Screen number used
359 private static IntPtr GdkScreen;
360 private static IntPtr DefaultColormap; // Colormap for screen
361 private static IntPtr GdkDefaultColormap; // Gdk Colormap for screen
362 private static IntPtr CustomVisual; // Visual for window creation
363 private static IntPtr GdkCustomVisual;
364 private static IntPtr CustomColormap; // Colormap for window creation
365 private static IntPtr GdkCustomColormap;
366 private static int VisualBestDepth;
367 private static IntPtr RootWindow; // Handle of the root window for the screen/display
368 private static IntPtr GdkRootWindow; // Gdk handle of the root window for the screen/display
369 private static IntPtr FosterParent; // Container to hold child windows until their parent exists
370 private static IntPtr GdkFosterParent; // Container to hold child windows until their parent exists
371 private static XErrorHandler ErrorHandler; // Error handler delegate
372 private static bool ErrorExceptions; // Throw exceptions on X errors
373 private static bool PostQuitState; // True if we've got an pending exit
376 private static IntPtr ClipMagic = new IntPtr(27051977);
377 private static ClipboardStruct Clipboard; // Our clipboard
380 private static int PostAtom; // PostMessage atom
381 private static int AsyncAtom; // Support for async messages
384 private static XEventQueue MessageQueue; // Holds our queued up events
386 private static Pollfd[] pollfds; // For watching the X11 socket
388 private static X11Keyboard Keyboard; //
389 private static X11Dnd Dnd;
390 private static Socket listen; //
391 private static Socket wake; //
392 private static Socket wake_receive; //
393 private static byte[] network_buffer; //
397 private static IntPtr ActiveWindow; // Handle of the active window
398 private static IntPtr FocusWindow; // Handle of the window with keyboard focus (if any)
401 private static Stack ModalWindows; // Stack of our modal windows
404 private static IntPtr SystrayMgrWindow; // Handle of the Systray Manager window
407 private static IntPtr LastCursorWindow; // The last window we set the cursor on
408 private static IntPtr LastCursorHandle; // The handle that was last set on LastCursorWindow
409 private static IntPtr OverrideCursorHandle; // The cursor that is set to override any other cursors
412 private static CaretStruct Caret; //
414 // Support for Window Styles
415 private static int[] NetAtoms; // All atoms we know
417 // mouse hover message generation
418 private static HoverStruct HoverState; //
420 // double click message generation
421 private static ClickStruct ClickPending; //
423 // Support for mouse grab
424 private static GrabStruct Grab; //
427 private static Point MousePosition; // Last position of mouse, in screen coords
428 internal static MouseButtons MouseState; // Last state of mouse buttons
431 private static ArrayList TimerList; // Holds SWF.Timers
434 private static int DoubleClickInterval; // msec; max interval between clicks to count as double click
436 const GdkEventMask GdkSelectInputMask = GdkEventMask.GDK_BUTTON_PRESS_MASK |
437 GdkEventMask.GDK_BUTTON_RELEASE_MASK |
438 GdkEventMask.GDK_KEY_PRESS_MASK |
439 GdkEventMask.GDK_KEY_RELEASE_MASK |
440 GdkEventMask.GDK_ENTER_NOTIFY_MASK |
441 GdkEventMask.GDK_LEAVE_NOTIFY_MASK |
442 GdkEventMask.GDK_EXPOSURE_MASK |
443 GdkEventMask.GDK_FOCUS_CHANGE_MASK |
444 GdkEventMask.GDK_POINTER_MOTION_MASK |
445 GdkEventMask.GDK_VISIBILITY_NOTIFY_MASK |
446 GdkEventMask.GDK_SUBSTRUCTURE_MASK |
447 GdkEventMask.GDK_STRUCTURE_MASK;
449 static readonly object lockobj = new object ();
451 static Hashtable backing_store = new Hashtable (5);
453 #endregion // Local Variables
455 private XplatUIX11GTK ()
457 Console.WriteLine ("XplatUIX11GTK ctor...");
458 // Handle singleton stuff first
462 gdk_init_check (IntPtr.Zero, IntPtr.Zero);
464 // Now regular initialization
465 XlibLock = new object ();
466 MessageQueue = new XEventQueue ();
467 TimerList = new ArrayList ();
470 ErrorExceptions = false;
472 // X11 Initialization
473 SetDisplay (gdk_x11_display_get_xdisplay (gdk_display_get_default ()));
474 X11DesktopColors.Initialize ();
476 // Handle any upcoming errors; we re-set it here, X11DesktopColor stuff might have stolen it (gtk does)
477 ErrorHandler = new XErrorHandler (HandleError);
478 XSetErrorHandler (ErrorHandler);
480 #endregion // Constructors
482 #region Singleton Specific Code
483 public static XplatUIX11GTK GetInstance ()
486 if (Instance == null) {
487 Instance = new XplatUIX11GTK ();
494 public int Reference {
501 #region Internal Properties
502 internal static IntPtr Display {
504 return DisplayHandle;
508 XplatUIX11GTK.GetInstance ().SetDisplay (value);
512 internal static int Screen {
522 internal static IntPtr RootWindowHandle {
532 internal static IntPtr Visual {
538 CustomVisual = value;
542 internal static IntPtr ColorMap {
544 return CustomColormap;
548 CustomColormap = value;
553 #region XExceptionClass
554 internal class XException : ApplicationException {
558 XRequest RequestCode;
562 public XException (IntPtr Display, IntPtr ResourceID, IntPtr Serial, byte ErrorCode, XRequest RequestCode, byte MinorCode)
564 this.Display = Display;
565 this.ResourceID = ResourceID;
566 this.Serial = Serial;
567 this.RequestCode = RequestCode;
568 this.ErrorCode = ErrorCode;
569 this.MinorCode = MinorCode;
572 public override string Message {
574 return GetMessage (Display, ResourceID, Serial, ErrorCode, RequestCode, MinorCode);
578 public static string GetMessage (IntPtr Display, IntPtr ResourceID, IntPtr Serial, byte ErrorCode, XRequest RequestCode, byte MinorCode)
584 sb = new StringBuilder (160);
585 XGetErrorText (Display, ErrorCode, sb, sb.Capacity);
586 x_error_text = sb.ToString ();
588 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);
592 #endregion // XExceptionClass
594 #region Internal Methods
595 // native X display handle
596 internal void SetDisplay (IntPtr display_handle)
598 if (display_handle != IntPtr.Zero) {
601 if ((GdkDisplayHandle != IntPtr.Zero) && (GdkFosterParent != IntPtr.Zero)) {
602 hwnd = Hwnd.ObjectFromHandle (gdk_x11_drawable_get_xid (GdkFosterParent));
603 gdk_window_destroy (GdkFosterParent);
607 if (GdkDisplayHandle != IntPtr.Zero) {
608 gdk_display_close (GdkDisplayHandle);
611 DisplayHandle = display_handle;
612 GdkDisplayHandle = gdk_x11_lookup_xdisplay (display_handle);
614 // We need to tell System.Drawing our DisplayHandle. FromHdcInternal has
615 // been hacked to do this for us.
616 Graphics.FromHdcInternal (DisplayHandle);
619 if (Environment.GetEnvironmentVariable ("MONO_XSYNC") != null) {
620 XSynchronize (DisplayHandle, true);
623 if (Environment.GetEnvironmentVariable ("MONO_XEXCEPTIONS") != null) {
624 ErrorExceptions = true;
628 GdkScreen = gdk_screen_get_default ();
629 // or gdk_x11_get_default_screen
630 ScreenNo = gdk_screen_get_number (GdkScreen);
631 GdkRootWindow = gdk_get_default_root_window ();
632 RootWindow = gdk_x11_drawable_get_xid (GdkRootWindow);
633 GdkDefaultColormap = gdk_colormap_get_system ();
634 DefaultColormap = gdk_x11_colormap_get_xcolormap (GdkDefaultColormap);
636 VisualBestDepth = gdk_visual_get_best_depth ();
637 //Console.WriteLine (VisualBestDepth);
639 // Create the foster parent
640 FosterParent = XCreateSimpleWindow (DisplayHandle, RootWindow, 0, 0, 1, 1, 4, 0, 0);
641 GdkFosterParent = gdk_window_foreign_new (FosterParent);
643 if (GdkFosterParent == IntPtr.Zero) {
644 Console.WriteLine ("XplatUIX11GTK Constructor failed to create FosterParent");
649 hwnd.WholeWindow = FosterParent;
650 hwnd.ClientWindow = FosterParent;
652 // For sleeping on the X11 socket
653 listen = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
654 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 0);
658 // To wake up when a timer is ready
659 network_buffer = new byte [10];
661 wake = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
662 wake.Connect (listen.LocalEndPoint);
663 wake_receive = listen.Accept ();
666 pollfds = new Pollfd [2];
667 pollfds [0] = new Pollfd ();
668 pollfds [0].fd = XConnectionNumber (DisplayHandle);
669 pollfds [0].events = PollEvents.POLLIN;
671 pollfds [1] = new Pollfd ();
672 pollfds [1].fd = wake_receive.Handle.ToInt32 ();
673 pollfds [1].events = PollEvents.POLLIN;
676 Keyboard = new X11Keyboard (DisplayHandle);
677 Dnd = new X11Dnd (DisplayHandle);
679 PostQuitState = false;
681 DoubleClickInterval = 500;
683 HoverState.Interval = 500;
684 HoverState.Timer = new Timer ();
685 HoverState.Timer.Enabled = false;
686 HoverState.Timer.Interval = HoverState.Interval;
687 HoverState.Timer.Tick += new EventHandler (MouseHover);
691 ActiveWindow = IntPtr.Zero;
692 FocusWindow = IntPtr.Zero;
693 ModalWindows = new Stack (3);
695 MouseState = MouseButtons.None;
696 MousePosition = new Point (0, 0);
698 Caret.Timer = new Timer ();
699 Caret.Timer.Interval = 500; // FIXME - where should this number come from?
700 Caret.Timer.Tick += new EventHandler (CaretCallback);
704 // Grab atom changes off the root window to catch certain WM events
705 gdk_window_set_events (GdkRootWindow, (int)GdkEventMask.GDK_PROPERTY_CHANGE_MASK);
707 // Handle any upcoming errors
708 ErrorHandler = new XErrorHandler (HandleError);
709 XSetErrorHandler (ErrorHandler);
711 throw new ArgumentNullException ("Display", "Could not open display (X-Server required. Check you DISPLAY environment variable)");
715 internal static void Where ()
717 Console.WriteLine ("Here: {0}\n", WhereString ());
720 internal static string WhereString ()
729 newline = String.Format ("{0}\t {1} ", Environment.NewLine, Locale.GetText ("at"));
730 unknown = Locale.GetText ("<unknown method>");
731 sb = new StringBuilder ();
732 stack = new StackTrace (true);
734 for (int i = 0; i < stack.FrameCount; i++) {
735 frame = stack.GetFrame (i);
738 method = frame.GetMethod ();
739 if (method != null) {
741 sb.AppendFormat(frame.ToString());
743 if (frame.GetFileLineNumber () != 0) {
744 sb.AppendFormat ("{0}.{1} () [{2}:{3}]", method.DeclaringType.FullName, method.Name, Path.GetFileName (frame.GetFileName ()), frame.GetFileLineNumber ());
746 sb.AppendFormat ("{0}.{1} ()", method.DeclaringType.FullName, method.Name);
752 return sb.ToString ();
754 #endregion // Internal Methods
756 #region Private Methods
757 private static void SetupAtoms ()
759 NetAtoms = new int [(int)NA.LAST_NET_ATOM];
761 NetAtoms [(int)NA.WM_PROTOCOLS] = XInternAtom (DisplayHandle, "WM_PROTOCOLS", false);
762 NetAtoms [(int)NA.WM_DELETE_WINDOW] = XInternAtom (DisplayHandle, "WM_DELETE_WINDOW", false);
763 NetAtoms [(int)NA.WM_TAKE_FOCUS] = XInternAtom (DisplayHandle, "WM_TAKE_FOCUS", false);
765 NetAtoms [(int)NA._NET_SUPPORTED] = XInternAtom (DisplayHandle, "_NET_SUPPORTED", false);
766 NetAtoms [(int)NA._NET_CLIENT_LIST] = XInternAtom (DisplayHandle, "_NET_CLIENT_LIST", false);
767 NetAtoms [(int)NA._NET_NUMBER_OF_DESKTOPS] = XInternAtom (DisplayHandle, "_NET_NUMBER_OF_DESKTOPS", false);
768 NetAtoms [(int)NA._NET_DESKTOP_GEOMETRY] = XInternAtom (DisplayHandle, "_NET_DESKTOP_GEOMETRY", false);
769 NetAtoms [(int)NA._NET_DESKTOP_VIEWPORT] = XInternAtom (DisplayHandle, "_NET_DESKTOP_VIEWPORT", false);
770 NetAtoms [(int)NA._NET_CURRENT_DESKTOP] = XInternAtom (DisplayHandle, "_NET_CURRENT_DESKTOP", false);
771 NetAtoms [(int)NA._NET_DESKTOP_NAMES] = XInternAtom (DisplayHandle, "_NET_DESKTOP_NAMES", false);
772 NetAtoms [(int)NA._NET_ACTIVE_WINDOW] = XInternAtom (DisplayHandle, "_NET_ACTIVE_WINDOW", false);
773 NetAtoms [(int)NA._NET_WORKAREA] = XInternAtom (DisplayHandle, "_NET_WORKAREA", false);
774 NetAtoms [(int)NA._NET_SUPPORTING_WM_CHECK] = XInternAtom (DisplayHandle, "_NET_SUPPORTING_WM_CHECK", false);
775 NetAtoms [(int)NA._NET_VIRTUAL_ROOTS] = XInternAtom (DisplayHandle, "_NET_VIRTUAL_ROOTS", false);
776 NetAtoms [(int)NA._NET_DESKTOP_LAYOUT] = XInternAtom (DisplayHandle, "_NET_DESKTOP_LAYOUT", false);
777 NetAtoms [(int)NA._NET_SHOWING_DESKTOP] = XInternAtom (DisplayHandle, "_NET_SHOWING_DESKTOP", false);
779 NetAtoms [(int)NA._NET_CLOSE_WINDOW] = XInternAtom (DisplayHandle, "_NET_CLOSE_WINDOW", false);
780 NetAtoms [(int)NA._NET_MOVERESIZE_WINDOW] = XInternAtom (DisplayHandle, "_NET_MOVERESIZE_WINDOW", false);
781 NetAtoms [(int)NA._NET_WM_MOVERESIZE] = XInternAtom (DisplayHandle, "_NET_WM_MOVERESIZE", false);
782 NetAtoms [(int)NA._NET_RESTACK_WINDOW] = XInternAtom (DisplayHandle, "_NET_RESTACK_WINDOW", false);
783 NetAtoms [(int)NA._NET_REQUEST_FRAME_EXTENTS] = XInternAtom (DisplayHandle, "_NET_REQUEST_FRAME_EXTENTS", false);
785 NetAtoms [(int)NA._NET_WM_NAME] = XInternAtom (DisplayHandle, "_NET_WM_NAME", false);
786 NetAtoms [(int)NA._NET_WM_VISIBLE_NAME] = XInternAtom (DisplayHandle, "_NET_WM_VISIBLE_NAME", false);
787 NetAtoms [(int)NA._NET_WM_ICON_NAME] = XInternAtom (DisplayHandle, "_NET_WM_ICON_NAME", false);
788 NetAtoms [(int)NA._NET_WM_VISIBLE_ICON_NAME] = XInternAtom (DisplayHandle, "_NET_WM_VISIBLE_ICON_NAME", false);
789 NetAtoms [(int)NA._NET_WM_DESKTOP] = XInternAtom (DisplayHandle, "_NET_WM_DESKTOP", false);
790 NetAtoms [(int)NA._NET_WM_WINDOW_TYPE] = XInternAtom (DisplayHandle, "_NET_WM_WINDOW_TYPE", false);
791 NetAtoms [(int)NA._NET_WM_STATE] = XInternAtom (DisplayHandle, "_NET_WM_STATE", false);
792 NetAtoms [(int)NA._NET_WM_ALLOWED_ACTIONS] = XInternAtom (DisplayHandle, "_NET_WM_ALLOWED_ACTIONS", false);
793 NetAtoms [(int)NA._NET_WM_STRUT] = XInternAtom (DisplayHandle, "_NET_WM_STRUT", false);
794 NetAtoms [(int)NA._NET_WM_STRUT_PARTIAL] = XInternAtom (DisplayHandle, "_NET_WM_STRUT_PARTIAL", false);
795 NetAtoms [(int)NA._NET_WM_ICON_GEOMETRY] = XInternAtom (DisplayHandle, "_NET_WM_ICON_GEOMETRY", false);
796 NetAtoms [(int)NA._NET_WM_ICON] = XInternAtom (DisplayHandle, "_NET_WM_ICON", false);
797 NetAtoms [(int)NA._NET_WM_PID] = XInternAtom (DisplayHandle, "_NET_WM_PID", false);
798 NetAtoms [(int)NA._NET_WM_HANDLED_ICONS] = XInternAtom (DisplayHandle, "_NET_WM_HANDLED_ICONS", false);
799 NetAtoms [(int)NA._NET_WM_USER_TIME] = XInternAtom (DisplayHandle, "_NET_WM_USER_TIME", false);
800 NetAtoms [(int)NA._NET_FRAME_EXTENTS] = XInternAtom (DisplayHandle, "_NET_FRAME_EXTENTS", false);
802 NetAtoms [(int)NA._NET_WM_PING] = XInternAtom (DisplayHandle, "_NET_WM_PING", false);
803 NetAtoms [(int)NA._NET_WM_SYNC_REQUEST] = XInternAtom (DisplayHandle, "_NET_WM_SYNC_REQUEST", false);
805 NetAtoms [(int)NA._NET_SYSTEM_TRAY_S] = XInternAtom (DisplayHandle, "_NET_SYSTEM_TRAY_S" + ScreenNo.ToString (), false);
806 NetAtoms [(int)NA._NET_SYSTEM_TRAY_OPCODE] = XInternAtom (DisplayHandle, "_NET_SYSTEM_TRAY_OPCODE", false);
807 NetAtoms [(int)NA._NET_SYSTEM_TRAY_ORIENTATION] = XInternAtom (DisplayHandle, "_NET_SYSTEM_TRAY_ORIENTATION", false);
809 NetAtoms [(int)NA._NET_WM_STATE_MAXIMIZED_HORZ] = XInternAtom (DisplayHandle, "_NET_WM_STATE_MAXIMIZED_HORZ", false);
810 NetAtoms [(int)NA._NET_WM_STATE_MAXIMIZED_VERT] = XInternAtom (DisplayHandle, "_NET_WM_STATE_MAXIMIZED_VERT", false);
811 NetAtoms [(int)NA._NET_WM_STATE_HIDDEN] = XInternAtom (DisplayHandle, "_NET_WM_STATE_HIDDEN", false);
813 NetAtoms [(int)NA._XEMBED] = XInternAtom (DisplayHandle, "_XEMBED", false);
814 NetAtoms [(int)NA._XEMBED_INFO] = XInternAtom (DisplayHandle, "_XEMBED_INFO", false);
816 NetAtoms [(int)NA._MOTIF_WM_HINTS] = XInternAtom (DisplayHandle, "_MOTIF_WM_HINTS", false);
818 NetAtoms [(int)NA._NET_WM_STATE_NO_TASKBAR] = XInternAtom (DisplayHandle, "_NET_WM_STATE_NO_TASKBAR", false);
819 NetAtoms [(int)NA._NET_WM_STATE_ABOVE] = XInternAtom (DisplayHandle, "_NET_WM_STATE_ABOVE", false);
820 NetAtoms [(int)NA._NET_WM_STATE_MODAL] = XInternAtom (DisplayHandle, "_NET_WM_STATE_MODAL", false);
821 NetAtoms [(int)NA._NET_WM_CONTEXT_HELP] = XInternAtom (DisplayHandle, "_NET_WM_CONTEXT_HELP", false);
822 NetAtoms [(int)NA._NET_WM_WINDOW_OPACITY] = XInternAtom (DisplayHandle, "_NET_WM_WINDOW_OPACITY", false);
825 NetAtoms [(int)NA.CLIPBOARD] = XInternAtom (DisplayHandle, "CLIPBOARD", false);
826 NetAtoms [(int)NA.DIB] = (int)Atom.XA_PIXMAP;
827 NetAtoms [(int)NA.OEMTEXT] = XInternAtom (DisplayHandle, "COMPOUND_TEXT", false);
828 NetAtoms [(int)NA.UNICODETEXT] = XInternAtom (DisplayHandle, "UTF8_STRING", false);
829 NetAtoms [(int)NA.TARGETS] = XInternAtom (DisplayHandle, "TARGETS", false);
832 AsyncAtom = XInternAtom (DisplayHandle, "_SWF_AsyncAtom", false);
833 PostAtom = XInternAtom (DisplayHandle, "_SWF_PostMessageAtom", false);
834 HoverState.Atom = XInternAtom (DisplayHandle, "_SWF_HoverAtom", false);
837 private void GetSystrayManagerWindow ()
839 gdk_x11_grab_server ();
840 SystrayMgrWindow = XGetSelectionOwner (DisplayHandle, NetAtoms [(int)NA._NET_SYSTEM_TRAY_S]);
841 gdk_x11_ungrab_server ();
842 gdk_display_flush (GdkDisplayHandle);
845 private void SendNetWMMessage (IntPtr window, IntPtr message_type, IntPtr l0, IntPtr l1, IntPtr l2)
850 xev.ClientMessageEvent.type = XEventName.ClientMessage;
851 xev.ClientMessageEvent.send_event = true;
852 xev.ClientMessageEvent.window = window;
853 xev.ClientMessageEvent.message_type = message_type;
854 xev.ClientMessageEvent.format = 32;
855 xev.ClientMessageEvent.ptr1 = l0;
856 xev.ClientMessageEvent.ptr2 = l1;
857 xev.ClientMessageEvent.ptr3 = l2;
858 XSendEvent (DisplayHandle, RootWindow, false, EventMask.SubstructureRedirectMask | EventMask.SubstructureNotifyMask, ref xev);
861 private void SendNetClientMessage (IntPtr window, IntPtr message_type, IntPtr l0, IntPtr l1, IntPtr l2)
866 xev.ClientMessageEvent.type = XEventName.ClientMessage;
867 xev.ClientMessageEvent.send_event = true;
868 xev.ClientMessageEvent.window = window;
869 xev.ClientMessageEvent.message_type = message_type;
870 xev.ClientMessageEvent.format = 32;
871 xev.ClientMessageEvent.ptr1 = l0;
872 xev.ClientMessageEvent.ptr2 = l1;
873 xev.ClientMessageEvent.ptr3 = l2;
874 XSendEvent (DisplayHandle, window, false, EventMask.NoEventMask, ref xev);
877 private void DeriveStyles (IntPtr handle, int Style, int ExStyle, out FormBorderStyle border_style, out TitleStyle title_style, out int caption_height, out int tool_caption_height)
880 // Only MDI windows get caption_heights
882 tool_caption_height = 19;
884 if ((Style & (int) WindowStyles.WS_CHILD) != 0) {
885 if ((Style & (int) WindowStyles.WS_BORDER) == 0) {
886 border_style = FormBorderStyle.None;
887 } else if ((ExStyle & (int) WindowStyles.WS_EX_CLIENTEDGE) != 0) {
888 border_style = FormBorderStyle.Fixed3D;
890 border_style = FormBorderStyle.FixedSingle;
892 title_style = TitleStyle.None;
896 if ((ExStyle & (int) WindowStyles.WS_EX_MDICHILD) != 0) {
901 title_style = TitleStyle.None;
902 if ((Style & (int)WindowStyles.WS_CAPTION) != 0) {
903 if ((ExStyle & (int)WindowStyles.WS_EX_TOOLWINDOW) != 0) {
904 title_style = TitleStyle.Tool;
906 title_style = TitleStyle.Normal;
911 border_style = FormBorderStyle.None;
913 if ((Style & (int)WindowStyles.WS_THICKFRAME) != 0) {
914 if ((ExStyle & (int)WindowStyles.WS_EX_TOOLWINDOW) != 0) {
915 border_style = FormBorderStyle.SizableToolWindow;
917 border_style = FormBorderStyle.Sizable;
920 if ((ExStyle & (int)WindowStyles.WS_EX_CLIENTEDGE) != 0) {
921 border_style = FormBorderStyle.Fixed3D;
922 } else if ((ExStyle & (int)WindowStyles.WS_EX_DLGMODALFRAME) != 0) {
923 border_style = FormBorderStyle.FixedDialog;
924 } else if ((ExStyle & (int)WindowStyles.WS_EX_TOOLWINDOW) != 0) {
925 border_style = FormBorderStyle.FixedToolWindow;
926 } else if ((Style & (int)WindowStyles.WS_BORDER) != 0) {
927 border_style = FormBorderStyle.Sizable;
929 border_style = FormBorderStyle.None;
933 if ((Style & (int) WindowStyles.WS_OVERLAPPEDWINDOW) != 0 ||
934 (ExStyle & (int) WindowStyles.WS_EX_TOOLWINDOW) != 0) {
935 border_style = (FormBorderStyle) 0xFFFF;
937 border_style = FormBorderStyle.None;
943 private void SetHwndStyles (Hwnd hwnd, CreateParams cp)
945 DeriveStyles (hwnd.Handle, cp.Style, cp.ExStyle, out hwnd.border_style, out hwnd.title_style, out hwnd.caption_height, out hwnd.tool_caption_height);
948 private void SetWMStyles (Hwnd hwnd, CreateParams cp)
950 GdkWMDecoration decorations = GdkWMDecoration.GDK_DECOR_ALL;
952 if ((cp.Style & (int)WindowStyles.WS_CAPTION) != 0) {
953 decorations |= GdkWMDecoration.GDK_DECOR_TITLE | GdkWMDecoration.GDK_DECOR_MENU;
956 if ((cp.Style & ((int)WindowStyles.WS_THICKFRAME)) != 0) {
957 decorations |= GdkWMDecoration.GDK_DECOR_BORDER | GdkWMDecoration.GDK_DECOR_RESIZEH;
959 if ((cp.Style & ((int)WindowStyles.WS_MINIMIZEBOX)) != 0) {
960 decorations |= GdkWMDecoration.GDK_DECOR_MINIMIZE;
963 if ((cp.Style & ((int)WindowStyles.WS_MAXIMIZEBOX)) != 0) {
964 decorations |= GdkWMDecoration.GDK_DECOR_MAXIMIZE;
967 // is this needed ? most window managers do not even honour any MotifFunctions...
968 // if ((cp.Style & ((int)WindowStyles.WS_SYSMENU)) != 0) {
969 // functions |= MotifFunctions.Close;
972 if ((cp.ExStyle & ((int)WindowStyles.WS_EX_DLGMODALFRAME)) != 0) {
973 decorations |= GdkWMDecoration.GDK_DECOR_BORDER;
976 if ((cp.Style & ((int)WindowStyles.WS_DLGFRAME)) != 0) {
977 decorations |= GdkWMDecoration.GDK_DECOR_BORDER;
980 if ((cp.Style & ((int)WindowStyles.WS_BORDER)) != 0) {
981 decorations |= GdkWMDecoration.GDK_DECOR_BORDER;
984 if ((cp.ExStyle & ((int)WindowStyles.WS_EX_TOOLWINDOW)) != 0) {
988 gdk_window_set_decorations (gdk_window_foreign_new (hwnd.whole_window), (int)decorations);
991 private void SetIcon (Hwnd hwnd, Icon icon)
998 bitmap = icon.ToBitmap ();
1000 size = bitmap.Width * bitmap.Height + 2;
1001 data = new uint [size];
1003 data [index++] = (uint)bitmap.Width;
1004 data [index++] = (uint)bitmap.Height;
1006 for (int y = 0; y < bitmap.Height; y++) {
1007 for (int x = 0; x < bitmap.Width; x++) {
1008 data [index++] = (uint)bitmap.GetPixel (x, y).ToArgb ();
1011 XChangeProperty (DisplayHandle, hwnd.whole_window, NetAtoms [(int)NA._NET_WM_ICON], Atom.XA_CARDINAL, 32, PropertyMode.Replace, data, size);
1014 private IntPtr ImageToPixmap (Image image)
1019 private void WakeupMain ()
1021 wake.Send (new byte [] { 0xFF });
1024 private void TranslatePropertyToClipboard (int property)
1030 IntPtr prop = IntPtr.Zero;
1032 Clipboard.Item = null;
1034 XGetWindowProperty (DisplayHandle, FosterParent, property, 0, 0x7fffffff, true, Atom.AnyPropertyType, out actual_atom, out actual_format, out nitems, out bytes_after, ref prop);
1037 if (property == (int)Atom.XA_STRING) {
1038 Clipboard.Item = Marshal.PtrToStringAnsi (prop);
1039 } else if (property == (int)Atom.XA_BITMAP) {
1040 // FIXME - convert bitmap to image
1041 } else if (property == (int)Atom.XA_PIXMAP) {
1042 // FIXME - convert pixmap to image
1043 } else if (property == NetAtoms [(int)NA.OEMTEXT]) {
1044 Clipboard.Item = Marshal.PtrToStringAnsi (prop);
1045 } else if (property == NetAtoms [(int)NA.UNICODETEXT]) {
1046 Clipboard.Item = Marshal.PtrToStringAnsi (prop);
1053 private void AddExpose (XEvent xevent)
1057 hwnd = Hwnd.GetObjectFromWindow (xevent.AnyEvent.window);
1064 if (xevent.AnyEvent.window == hwnd.client_window) {
1065 hwnd.AddInvalidArea (xevent.ExposeEvent.x, xevent.ExposeEvent.y, xevent.ExposeEvent.width, xevent.ExposeEvent.height);
1066 if (!hwnd.expose_pending) {
1067 MessageQueue.Enqueue (xevent);
1068 hwnd.expose_pending = true;
1071 if (!hwnd.nc_expose_pending) {
1072 MessageQueue.Enqueue (xevent);
1073 hwnd.nc_expose_pending = true;
1078 private void InvalidateWholeWindow (IntPtr handle)
1082 hwnd = Hwnd.ObjectFromHandle (handle);
1084 InvalidateWholeWindow (handle, new Rectangle (0, 0, hwnd.Width, hwnd.Height));
1087 private void InvalidateWholeWindow (IntPtr handle, Rectangle rectangle)
1092 hwnd = Hwnd.ObjectFromHandle (handle);
1095 xevent = new XEvent ();
1096 xevent.type = XEventName.Expose;
1097 xevent.ExposeEvent.display = DisplayHandle;
1098 xevent.ExposeEvent.window = hwnd.whole_window;
1100 xevent.ExposeEvent.x = rectangle.X;
1101 xevent.ExposeEvent.y = rectangle.Y;
1102 xevent.ExposeEvent.width = rectangle.Width;
1103 xevent.ExposeEvent.height = rectangle.Height;
1108 private void WholeToScreen (IntPtr handle, ref int x, ref int y)
1114 hwnd = Hwnd.ObjectFromHandle (handle);
1117 gdk_window_get_origin (gdk_window_lookup (hwnd.whole_window), out dest_x_return, out dest_y_return);
1124 private void AddConfigureNotify (XEvent xevent)
1128 hwnd = Hwnd.GetObjectFromWindow (xevent.ConfigureEvent.window);
1135 if (xevent.ConfigureEvent.window == hwnd.whole_window) {
1136 if (!hwnd.reparented) {
1137 hwnd.x = xevent.ConfigureEvent.x;
1138 hwnd.y = xevent.ConfigureEvent.y;
1142 gdk_window_get_geometry (gdk_window_lookup (hwnd.whole_window), out hwnd.x, out hwnd.y, out dummy_int, out dummy_int, out dummy_int);
1145 hwnd.width = xevent.ConfigureEvent.width;
1146 hwnd.height = xevent.ConfigureEvent.height;
1148 if (!hwnd.configure_pending) {
1149 MessageQueue.Enqueue (xevent);
1150 hwnd.configure_pending = true;
1153 // We drop configure events for Client windows
1156 private void ShowCaret ()
1158 if ((Caret.gc == IntPtr.Zero) || Caret.On) {
1163 // gdk_gc_set_foreground
1166 XDrawLine (DisplayHandle, Caret.Window, Caret.gc, Caret.X, Caret.Y, Caret.X, Caret.Y + Caret.Height);
1170 private void HideCaret ()
1172 if ((Caret.gc == IntPtr.Zero) || !Caret.On) {
1177 // gdk_gc_set_foreground
1180 XDrawLine (DisplayHandle, Caret.Window, Caret.gc, Caret.X, Caret.Y, Caret.X, Caret.Y + Caret.Height);
1184 private int NextTimeout (DateTime now)
1186 int timeout = Int32.MaxValue;
1188 foreach (Timer timer in TimerList) {
1189 int next = (int) (timer.Expires - now).TotalMilliseconds;
1191 return 0; // Have a timer that has already expired
1194 if (next < timeout) {
1199 if (timeout < Timer.Minimum) {
1200 timeout = Timer.Minimum;
1206 private void CheckTimers (DateTime now)
1211 count = TimerList.Count;
1217 for (int i = 0; i < TimerList.Count; i++) {
1220 timer = (Timer) TimerList [i];
1222 if (timer.Enabled && timer.Expires <= now) {
1230 private void UpdateMessageQueue ()
1238 pending = XPending (DisplayHandle);
1243 Idle (this, EventArgs.Empty);
1247 pending = XPending (DisplayHandle);
1254 timeout = NextTimeout (now);
1257 Syscall.poll (pollfds, (uint) pollfds.Length, timeout);
1258 // Clean out buffer, so we're not busy-looping on the same data
1259 if (pollfds[1].revents != 0) {
1260 wake_receive.Receive(network_buffer, 0, 1, SocketFlags.None);
1264 pending = XPending (DisplayHandle);
1273 pending = XPending (DisplayHandle);
1277 while (pending > 0) {
1278 XEvent xevent = new XEvent ();
1281 XNextEvent (DisplayHandle, ref xevent);
1283 //Console.WriteLine("Got x event {0}", xevent);
1284 switch (xevent.type) {
1285 case XEventName.Expose:
1289 case XEventName.SelectionClear: {
1290 // Should we do something?
1294 case XEventName.SelectionRequest: {
1295 if (Dnd.HandleSelectionRequestEvent (ref xevent))
1299 sel_event = new XEvent ();
1300 sel_event.SelectionEvent.type = XEventName.SelectionNotify;
1301 sel_event.SelectionEvent.send_event = true;
1302 sel_event.SelectionEvent.display = DisplayHandle;
1303 sel_event.SelectionEvent.selection = xevent.SelectionRequestEvent.selection;
1304 sel_event.SelectionEvent.target = xevent.SelectionRequestEvent.target;
1305 sel_event.SelectionEvent.requestor = xevent.SelectionRequestEvent.requestor;
1306 sel_event.SelectionEvent.time = xevent.SelectionRequestEvent.time;
1307 sel_event.SelectionEvent.property = 0;
1309 // Seems that some apps support asking for supported types
1310 if (xevent.SelectionEvent.target == NetAtoms [(int)NA.TARGETS]) {
1314 atoms = new uint [5];
1317 if (Clipboard.Item is String) {
1318 atoms [atom_count++] = (uint)Atom.XA_STRING;
1319 atoms [atom_count++] = (uint)NetAtoms [(int)NA.OEMTEXT];
1320 atoms [atom_count++] = (uint)NetAtoms [(int)NA.UNICODETEXT];
1321 } else if (Clipboard.Item is Image) {
1322 atoms [atom_count++] = (uint)Atom.XA_PIXMAP;
1323 atoms [atom_count++] = (uint)Atom.XA_BITMAP;
1325 // FIXME - handle other types
1328 XChangeProperty (DisplayHandle, xevent.SelectionEvent.requestor, xevent.SelectionRequestEvent.property, xevent.SelectionRequestEvent.target, 32, PropertyMode.Replace, atoms, atom_count);
1329 } else if (Clipboard.Item is string) {
1335 if (xevent.SelectionRequestEvent.target == (int)Atom.XA_STRING) {
1338 bytes = new ASCIIEncoding ().GetBytes ((string)Clipboard.Item);
1339 buffer = Marshal.AllocHGlobal (bytes.Length);
1340 buflen = bytes.Length;
1342 for (int i = 0; i < buflen; i++) {
1343 Marshal.WriteByte (buffer, i, bytes [i]);
1345 } else if (xevent.SelectionRequestEvent.target == NetAtoms [(int)NA.OEMTEXT]) {
1346 // FIXME - this should encode into ISO2022
1347 buffer = Marshal.StringToHGlobalAnsi ((string)Clipboard.Item);
1348 while (Marshal.ReadByte (buffer, buflen) != 0) {
1351 } else if (xevent.SelectionRequestEvent.target == NetAtoms [(int)NA.UNICODETEXT]) {
1352 buffer = Marshal.StringToHGlobalAnsi ((string)Clipboard.Item);
1353 while (Marshal.ReadByte (buffer, buflen) != 0) {
1357 buffer = IntPtr.Zero;
1360 if (buffer != IntPtr.Zero) {
1361 XChangeProperty (DisplayHandle, xevent.SelectionRequestEvent.requestor, xevent.SelectionRequestEvent.property, xevent.SelectionRequestEvent.target, 8, PropertyMode.Replace, buffer, buflen);
1362 sel_event.SelectionEvent.property = xevent.SelectionRequestEvent.property;
1363 Marshal.FreeHGlobal (buffer);
1365 } else if (Clipboard.Item is Image) {
1366 if (xevent.SelectionEvent.target == (int)Atom.XA_PIXMAP) {
1367 // FIXME - convert image and store as property
1368 } else if (xevent.SelectionEvent.target == (int)Atom.XA_PIXMAP) {
1369 // FIXME - convert image and store as property
1373 XSendEvent (DisplayHandle, xevent.SelectionRequestEvent.requestor, false, EventMask.NoEventMask, ref sel_event);
1377 case XEventName.SelectionNotify: {
1378 if (Clipboard.Enumerating) {
1379 Clipboard.Enumerating = false;
1380 if (xevent.SelectionEvent.property != 0) {
1381 XDeleteProperty (DisplayHandle, FosterParent, xevent.SelectionEvent.property);
1382 if (!Clipboard.Formats.Contains (xevent.SelectionEvent.property)) {
1383 Clipboard.Formats.Add (xevent.SelectionEvent.property);
1384 #if DriverDebugExtra
1385 Console.WriteLine("Got supported clipboard atom format: {0}", xevent.SelectionEvent.property);
1389 } else if (Clipboard.Retrieving) {
1390 Clipboard.Retrieving = false;
1391 if (xevent.SelectionEvent.property != 0) {
1392 TranslatePropertyToClipboard (xevent.SelectionEvent.property);
1394 Clipboard.Item = null;
1397 Dnd.HandleSelectionNotifyEvent (ref xevent);
1402 case XEventName.KeyPress:
1403 case XEventName.KeyRelease:
1404 case XEventName.ButtonPress:
1405 case XEventName.ButtonRelease:
1406 case XEventName.MotionNotify:
1407 case XEventName.EnterNotify:
1408 case XEventName.LeaveNotify:
1409 case XEventName.CreateNotify:
1410 case XEventName.DestroyNotify:
1411 case XEventName.FocusIn:
1412 case XEventName.FocusOut:
1413 case XEventName.ClientMessage:
1414 case XEventName.ReparentNotify:
1415 MessageQueue.Enqueue (xevent);
1418 case XEventName.ConfigureNotify:
1419 AddConfigureNotify (xevent);
1422 case XEventName.PropertyNotify:
1423 if (xevent.PropertyEvent.atom == NetAtoms [(int)NA._NET_ACTIVE_WINDOW]) {
1428 IntPtr prop = IntPtr.Zero;
1429 IntPtr prev_active;;
1431 prev_active = ActiveWindow;
1432 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);
1433 if ((nitems > 0) && (prop != IntPtr.Zero)) {
1434 ActiveWindow = Hwnd.GetHandleFromWindow ((IntPtr)Marshal.ReadInt32 (prop));
1437 if (prev_active != ActiveWindow) {
1438 if (prev_active != IntPtr.Zero) {
1439 PostMessage (prev_active, Msg.WM_ACTIVATE, (IntPtr)WindowActiveFlags.WA_INACTIVE, IntPtr.Zero);
1441 if (ActiveWindow != IntPtr.Zero) {
1442 PostMessage (ActiveWindow, Msg.WM_ACTIVATE, (IntPtr)WindowActiveFlags.WA_ACTIVE, IntPtr.Zero);
1445 if (ModalWindows.Count == 0) {
1448 // Modality handling, if we are modal and the new active window is one
1449 // of ours but not the modal one, switch back to the modal window
1451 if (NativeWindow.FindWindow (ActiveWindow) != null) {
1452 if (ActiveWindow != (IntPtr)ModalWindows.Peek ()) {
1453 Activate ((IntPtr)ModalWindows.Peek ());
1465 pending = XPending (DisplayHandle);
1470 private IntPtr GetMousewParam (int Delta)
1474 if ((MouseState & MouseButtons.Left) != 0) {
1475 result |= (int)MsgButtons.MK_LBUTTON;
1478 if ((MouseState & MouseButtons.Middle) != 0) {
1479 result |= (int)MsgButtons.MK_MBUTTON;
1482 if ((MouseState & MouseButtons.Right) != 0) {
1483 result |= (int)MsgButtons.MK_RBUTTON;
1486 Keys mods = ModifierKeys;
1487 if ((mods & Keys.Control) != 0) {
1488 result |= (int)MsgButtons.MK_CONTROL;
1491 if ((mods & Keys.Shift) != 0) {
1492 result |= (int)MsgButtons.MK_SHIFT;
1495 result |= Delta << 16;
1497 return (IntPtr)result;
1499 private IntPtr XGetParent (IntPtr handle)
1501 return gdk_x11_drawable_get_xid (gdk_window_get_parent (gdk_window_lookup (handle)));
1504 private int HandleError (IntPtr display, ref XErrorEvent error_event)
1506 if (ErrorExceptions) {
1507 throw new XException (error_event.display, error_event.resourceid, error_event.serial, error_event.error_code, error_event.request_code, error_event.minor_code);
1509 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 ());
1514 private void DestroyChildWindow (Control c)
1521 controls = c.child_controls.GetAllControls ();
1523 for (i = 0; i < controls.Length; i++) {
1524 if (controls [i].IsHandleCreated) {
1525 hwnd = Hwnd.ObjectFromHandle (controls [i].Handle);
1526 SendMessage (controls [i].Handle, Msg.WM_DESTROY, IntPtr.Zero, IntPtr.Zero);
1529 DestroyChildWindow (controls [i]);
1534 #endregion // Private Methods
1537 private void MouseHover (object sender, EventArgs e)
1539 if ((HoverState.X == MousePosition.X) && (HoverState.Y == MousePosition.Y)) {
1542 HoverState.Timer.Enabled = false;
1544 if (HoverState.Window != IntPtr.Zero) {
1545 xevent = new XEvent ();
1547 xevent.type = XEventName.ClientMessage;
1548 xevent.ClientMessageEvent.display = DisplayHandle;
1549 xevent.ClientMessageEvent.window = (IntPtr)HoverState.Window;
1550 xevent.ClientMessageEvent.message_type = (IntPtr)HoverState.Atom;
1551 xevent.ClientMessageEvent.format = 32;
1552 xevent.ClientMessageEvent.ptr1 = (IntPtr) (HoverState.Y << 16 | HoverState.X);
1554 MessageQueue.EnqueueLocked (xevent);
1561 private void CaretCallback (object sender, EventArgs e)
1566 Caret.On = !Caret.On;
1568 XDrawLine (DisplayHandle, Caret.Hwnd, Caret.gc, Caret.X, Caret.Y, Caret.X, Caret.Y + Caret.Height);
1570 #endregion // Callbacks
1572 #region Public Properties
1574 internal override int Caption {
1580 internal override Size CursorSize {
1584 gdk_display_get_maximal_cursor_size (GdkDisplayHandle, out x, out y);
1586 return new Size ((int)x, (int)y);
1590 internal override bool DragFullWindows {
1596 internal override Size DragSize {
1598 return new Size (4, 4);
1602 internal override Size FrameBorderSize {
1604 throw new NotImplementedException ();
1608 internal override Size IconSize {
1614 if (XGetIconSizes (DisplayHandle, RootWindow, out list, out count) != 0) {
1618 current = (long)list;
1621 size = new XIconSize ();
1623 for (int i = 0; i < count; i++) {
1624 size = (XIconSize)Marshal.PtrToStructure ((IntPtr)current, size.GetType ());
1625 current += Marshal.SizeOf (size);
1627 // Look for our preferred size
1628 if (size.min_width == 32) {
1630 return new Size (32, 32);
1633 if (size.max_width == 32) {
1635 return new Size (32, 32);
1638 if (size.min_width < 32 && size.max_width > 32) {
1641 // check if we can fit one
1643 while (x < size.max_width) {
1644 x += size.width_inc;
1647 return new Size (32, 32);
1652 if (largest < size.max_width) {
1653 largest = size.max_width;
1657 // We didn't find a match or we wouldn't be here
1658 return new Size (largest, largest);
1661 return new Size (32, 32);
1666 internal override int KeyboardSpeed {
1669 // A lot harder: need to do:
1670 // XkbQueryExtension(0x08051008, 0xbfffdf4c, 0xbfffdf50, 0xbfffdf54, 0xbfffdf58) = 1
1671 // XkbAllocKeyboard(0x08051008, 0xbfffdf4c, 0xbfffdf50, 0xbfffdf54, 0xbfffdf58) = 0x080517a8
1672 // XkbGetControls(0x08051008, 1, 0x080517a8, 0xbfffdf54, 0xbfffdf58) = 0
1674 // And from that we can tell the repetition rate
1676 // Notice, the values must map to:
1677 // [0, 31] which maps to 2.5 to 30 repetitions per second.
1683 internal override int KeyboardDelay {
1686 // Return values must range from 0 to 4, 0 meaning 250ms,
1687 // and 4 meaning 1000 ms.
1689 return 1; // ie, 500 ms
1693 internal override Size MaxWindowTrackSize {
1695 return new Size (WorkingArea.Width, WorkingArea.Height);
1699 internal override Size MinimizedWindowSize {
1701 return new Size (1, 1);
1705 internal override Size MinimizedWindowSpacingSize {
1707 return new Size (1, 1);
1711 internal override Size MinimumWindowSize {
1713 return new Size (1, 1);
1717 internal override Size MinWindowTrackSize {
1719 return new Size (1, 1);
1723 internal override Keys ModifierKeys {
1725 return Keyboard.ModifierKeys;
1729 internal override Size SmallIconSize {
1735 if (XGetIconSizes (DisplayHandle, RootWindow, out list, out count) != 0) {
1739 current = (long)list;
1742 size = new XIconSize ();
1744 for (int i = 0; i < count; i++) {
1745 size = (XIconSize)Marshal.PtrToStructure ((IntPtr)current, size.GetType ());
1746 current += Marshal.SizeOf (size);
1748 // Look for our preferred size
1749 if (size.min_width == 16) {
1751 return new Size (16, 16);
1754 if (size.max_width == 16) {
1756 return new Size (16, 16);
1759 if (size.min_width < 16 && size.max_width > 16) {
1762 // check if we can fit one
1764 while (x < size.max_width) {
1765 x += size.width_inc;
1768 return new Size (16, 16);
1773 if (smallest == 0 || smallest > size.min_width) {
1774 smallest = size.min_width;
1778 // We didn't find a match or we wouldn't be here
1779 return new Size (smallest, smallest);
1782 return new Size (16, 16);
1787 internal override int MouseButtonCount {
1793 internal override bool MouseButtonsSwapped {
1795 return false; // FIXME - how to detect?
1799 internal override bool MouseWheelPresent {
1801 return true; // FIXME - how to detect?
1805 internal override Rectangle VirtualScreen {
1811 internal override Rectangle WorkingArea {
1817 IntPtr prop = IntPtr.Zero;
1821 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);
1822 if ((nitems == 2) && (prop != IntPtr.Zero)) {
1823 width = Marshal.ReadInt32 (prop, 0);
1824 height = Marshal.ReadInt32 (prop, 4);
1827 return new Rectangle (0, 0, width, height);
1829 XWindowAttributes attributes=new XWindowAttributes ();
1832 XGetWindowAttributes (DisplayHandle, XRootWindow (DisplayHandle, ScreenNo), ref attributes);
1835 return new Rectangle (0, 0, attributes.width, attributes.height);
1839 #endregion // Public properties
1841 #region Public Static Methods
1842 internal override IntPtr InitializeDriver ()
1845 if (GdkDisplayHandle == IntPtr.Zero) {
1846 SetDisplay (gdk_x11_display_get_xdisplay (gdk_display_get_default ()));
1852 internal override void ShutdownDriver (IntPtr token)
1855 if (GdkDisplayHandle != IntPtr.Zero) {
1856 gdk_display_close (GdkDisplayHandle);
1857 DisplayHandle = IntPtr.Zero;
1858 GdkDisplayHandle = IntPtr.Zero;
1863 internal override void EnableThemes ()
1865 ThemesEnabled = true;
1869 internal override void Activate (IntPtr handle)
1873 hwnd = Hwnd.ObjectFromHandle (handle);
1875 if (hwnd != null) lock (XlibLock) {
1876 SendNetWMMessage (hwnd.whole_window, (IntPtr)NetAtoms [(int)NA._NET_ACTIVE_WINDOW], IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
1877 //XRaiseWindow(DisplayHandle, handle);
1882 internal override void AudibleAlert ()
1884 gdk_display_beep (gdk_x11_lookup_xdisplay (DisplayHandle));
1889 internal override void CaretVisible (IntPtr handle, bool visible)
1891 // Visible is cumulative; two hides require two shows before the caret is visible again
1892 if (Caret.Hwnd == handle) {
1894 if (Caret.Visible < 1) {
1897 if (Caret.Visible == 1) {
1899 Caret.Timer.Start ();
1904 if (Caret.Visible == 0) {
1905 Caret.Timer.Stop ();
1912 internal override bool CalculateWindowRect (IntPtr handle, ref Rectangle ClientRect, int Style, int ExStyle, Menu menu, out Rectangle WindowRect)
1914 FormBorderStyle border_style;
1915 TitleStyle title_style;
1917 int tool_caption_height;
1919 DeriveStyles (handle, Style, ExStyle, out border_style, out title_style,
1920 out caption_height, out tool_caption_height);
1922 WindowRect = Hwnd.GetWindowRectangle (border_style, menu, title_style,
1923 caption_height, tool_caption_height,
1929 internal override void ClientToScreen (IntPtr handle, ref int x, ref int y)
1936 hwnd = Hwnd.ObjectFromHandle (handle);
1939 XTranslateCoordinates (DisplayHandle, hwnd.client_window, RootWindow, x, y, out dest_x_return, out dest_y_return, out child);
1946 internal override int[] ClipboardAvailableFormats (IntPtr handle)
1948 DataFormats.Format f;
1951 f = DataFormats.Format.List;
1953 if (XGetSelectionOwner (DisplayHandle, NetAtoms [(int)NA.CLIPBOARD]) == IntPtr.Zero) {
1957 Clipboard.Formats = new ArrayList ();
1960 XConvertSelection (DisplayHandle, NetAtoms [(int)NA.CLIPBOARD], f.Id, f.Id, FosterParent, IntPtr.Zero);
1962 Clipboard.Enumerating = true;
1963 while (Clipboard.Enumerating) {
1964 UpdateMessageQueue ();
1969 result = new int [Clipboard.Formats.Count];
1971 for (int i = 0; i < Clipboard.Formats.Count; i++) {
1972 result [i] = (int)Clipboard.Formats [i];
1975 Clipboard.Formats = null;
1979 internal override void ClipboardClose (IntPtr handle)
1981 if (handle != ClipMagic) {
1982 throw new ArgumentException ("handle is not a valid clipboard handle");
1987 internal override int ClipboardGetID (IntPtr handle, string format)
1989 if (handle != ClipMagic) {
1990 throw new ArgumentException ("handle is not a valid clipboard handle");
1993 if (format == "Text") return (int)Atom.XA_STRING;
1994 else if (format == "Bitmap") return (int)Atom.XA_BITMAP;
1995 //else if (format == "MetaFilePict" ) return 3;
1996 //else if (format == "SymbolicLink" ) return 4;
1997 //else if (format == "DataInterchangeFormat" ) return 5;
1998 //else if (format == "Tiff" ) return 6;
1999 else if (format == "OEMText") return XInternAtom (DisplayHandle, "COMPOUND_TEXT", false);
2000 else if (format == "DeviceIndependentBitmap") return (int)Atom.XA_PIXMAP;
2001 else if (format == "Palette") return (int)Atom.XA_COLORMAP; // Useless
2002 //else if (format == "PenData" ) return 10;
2003 //else if (format == "RiffAudio" ) return 11;
2004 //else if (format == "WaveAudio" ) return 12;
2005 else if (format == "UnicodeText") return XInternAtom (DisplayHandle, "UTF8_STRING", false);
2006 //else if (format == "EnhancedMetafile" ) return 14;
2007 //else if (format == "FileDrop" ) return 15;
2008 //else if (format == "Locale" ) return 16;
2010 return XInternAtom (DisplayHandle, format, false);
2013 internal override IntPtr ClipboardOpen ()
2018 internal override object ClipboardRetrieve (IntPtr handle, int type, XplatUI.ClipboardToObject converter)
2020 XConvertSelection (DisplayHandle, NetAtoms [(int)NA.CLIPBOARD], type, type, FosterParent, IntPtr.Zero);
2022 Clipboard.Retrieving = true;
2023 while (Clipboard.Retrieving) {
2024 UpdateMessageQueue ();
2027 return Clipboard.Item;
2030 internal override void ClipboardStore (IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter)
2032 Clipboard.Item = obj;
2033 Clipboard.Type = type;
2034 Clipboard.Converter = converter;
2037 XSetSelectionOwner (DisplayHandle, NetAtoms [(int)NA.CLIPBOARD], FosterParent, IntPtr.Zero);
2039 // Clearing the selection
2040 XSetSelectionOwner (DisplayHandle, NetAtoms [(int)NA.CLIPBOARD], IntPtr.Zero, IntPtr.Zero);
2044 internal override void CreateCaret (IntPtr handle, int width, int height)
2046 XGCValues gc_values;
2049 hwnd = Hwnd.ObjectFromHandle (handle);
2051 if (Caret.Hwnd != IntPtr.Zero) {
2052 DestroyCaret (Caret.Hwnd);
2055 Caret.Hwnd = handle;
2056 Caret.Window = hwnd.client_window;
2057 Caret.Width = width;
2058 Caret.Height = height;
2062 gc_values = new XGCValues ();
2063 gc_values.line_width = width;
2065 Caret.gc = XCreateGC (DisplayHandle, Caret.Window, GCFunction.GCLineWidth, ref gc_values);
2066 if (Caret.gc == IntPtr.Zero) {
2067 Caret.Hwnd = IntPtr.Zero;
2071 XSetFunction (DisplayHandle, Caret.gc, GXFunction.GXinvert);
2074 internal override IntPtr CreateWindow (CreateParams cp)
2076 GdkWindowAttr gdk_window_attributes;
2077 GdkWindowAttributesType attributes_mask = 0;
2083 IntPtr GdkParentHandle;
2084 IntPtr GdkWholeWindow;
2085 IntPtr GdkClientWindow;
2086 Rectangle ClientRect;
2087 GdkWindowType gdk_window_type;
2092 gdk_window_attributes = new GdkWindowAttr ();
2099 if (Width < 1) Width = 1;
2100 if (Height < 1) Height = 1;
2102 gdk_window_type = GdkWindowType.GDK_WINDOW_CHILD;
2104 if (cp.Parent != IntPtr.Zero) {
2105 GdkParentHandle = gdk_window_lookup (Hwnd.ObjectFromHandle (cp.Parent).client_window);
2107 if ((cp.Style & (int)WindowStyles.WS_CHILD) != 0) {
2108 // We need to use our foster parent window until this poor child gets it's parent assigned
2109 GdkParentHandle = GdkFosterParent;
2110 } else if ((cp.Style & (int)WindowStyles.WS_POPUP) != 0) {
2111 GdkParentHandle = GdkRootWindow;
2113 // Default position on screen, if window manager doesn't place us somewhere else
2116 GdkParentHandle = GdkRootWindow;
2120 // ValueMask = SetWindowValuemask.BitGravity | SetWindowValuemask.WinGravity;
2122 // Attributes.bit_gravity = Gravity.NorthWestGravity;
2123 // Attributes.win_gravity = Gravity.NorthWestGravity;
2125 // FIXME: does gdk need that ?
2126 // Save what's under the toolwindow
2127 if ((cp.ExStyle & (int)WindowStyles.WS_EX_TOOLWINDOW) != 0) {
2128 // Attributes.save_under = true;
2129 // ValueMask |= SetWindowValuemask.SaveUnder;
2132 // If we're a popup without caption we override the WM
2133 if ((cp.Style & ((int)WindowStyles.WS_POPUP)) != 0) {
2134 if ((cp.Style & (int)WindowStyles.WS_CAPTION) == 0) {
2135 gdk_window_attributes.override_redirect = true;
2136 attributes_mask |= GdkWindowAttributesType.GDK_WA_NOREDIR;
2143 hwnd.height = Height;
2144 hwnd.parent = Hwnd.ObjectFromHandle (cp.Parent);
2146 if ((cp.Style & ((int)WindowStyles.WS_DISABLED)) != 0) {
2147 hwnd.enabled = false;
2150 ClientRect = hwnd.ClientRect;
2151 GdkClientWindow = IntPtr.Zero;
2153 gdk_window_attributes.x = X;
2154 gdk_window_attributes.y = Y;
2155 gdk_window_attributes.width = Width;
2156 gdk_window_attributes.height = Height;
2157 gdk_window_attributes.window_type = gdk_window_type;
2159 attributes_mask |= GdkWindowAttributesType.GDK_WA_X | GdkWindowAttributesType.GDK_WA_Y;
2161 gdk_window_attributes.wclass = GdkWindowClass.GDK_INPUT_OUTPUT;
2164 GdkWholeWindow = gdk_window_new (GdkParentHandle, ref gdk_window_attributes, (int)attributes_mask);
2166 if (GdkWholeWindow != IntPtr.Zero) {
2167 attributes_mask &= ~GdkWindowAttributesType.GDK_WA_NOREDIR;
2169 if (GdkCustomVisual != IntPtr.Zero && GdkCustomColormap != IntPtr.Zero) {
2170 attributes_mask |= GdkWindowAttributesType.GDK_WA_COLORMAP | GdkWindowAttributesType.GDK_WA_VISUAL;
2171 gdk_window_attributes.colormap = GdkCustomColormap;
2172 gdk_window_attributes.visual = GdkCustomVisual;
2175 gdk_window_attributes.x = ClientRect.X;
2176 gdk_window_attributes.y = ClientRect.Y;
2177 gdk_window_attributes.width = ClientRect.Width;
2178 gdk_window_attributes.height = ClientRect.Height;
2180 GdkClientWindow = gdk_window_new (GdkWholeWindow, ref gdk_window_attributes, (int)attributes_mask);
2184 if ((GdkWholeWindow == IntPtr.Zero) || (GdkClientWindow == IntPtr.Zero)) {
2185 throw new Exception ("Could not create X11 Gdk windows");
2188 hwnd.WholeWindow = gdk_x11_drawable_get_xid (GdkWholeWindow);
2189 hwnd.ClientWindow = gdk_x11_drawable_get_xid (GdkClientWindow);
2192 Console.WriteLine("Created window {0:X} / {1:X} parent {2:X}", ClientWindow.ToInt32(), WholeWindow.ToInt32(), hwnd.parent != null ? hwnd.parent.Handle.ToInt32() : 0);
2196 gdk_window_set_events (GdkWholeWindow, (int)GdkSelectInputMask);
2197 gdk_window_set_events (GdkClientWindow, (int)GdkSelectInputMask);
2199 if ((cp.Style & (int)WindowStyles.WS_VISIBLE) != 0) {
2200 gdk_window_show (GdkWholeWindow);
2201 gdk_window_show (GdkClientWindow);
2202 hwnd.visible = true;
2206 SetWMStyles (hwnd, cp);
2208 if ((cp.Style & (int)WindowStyles.WS_MINIMIZE) != 0) {
2209 SetWindowState (hwnd.Handle, FormWindowState.Minimized);
2210 } else if ((cp.Style & (int)WindowStyles.WS_MAXIMIZE) != 0) {
2211 SetWindowState (hwnd.Handle, FormWindowState.Maximized);
2214 // for now make all windows dnd enabled
2215 Dnd.SetAllowDrop (hwnd, true);
2217 // Set caption/window title
2218 Text (hwnd.Handle, cp.Caption);
2223 internal override IntPtr CreateWindow (IntPtr Parent, int X, int Y, int Width, int Height)
2225 CreateParams create_params = new CreateParams ();
2227 create_params.Caption = "";
2228 create_params.X = X;
2229 create_params.Y = Y;
2230 create_params.Width = Width;
2231 create_params.Height = Height;
2233 create_params.ClassName = XplatUI.DefaultClassName;
2234 create_params.ClassStyle = 0;
2235 create_params.ExStyle = 0;
2236 create_params.Parent = IntPtr.Zero;
2237 create_params.Param = 0;
2239 return CreateWindow (create_params);
2242 internal override IntPtr DefineCursor (Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot)
2245 Bitmap cursor_bitmap;
2253 IntPtr cursor_pixmap;
2260 Size cursor_size = CursorSize;
2261 width = cursor_size.Width;
2262 height = cursor_size.Height;
2264 // Win32 only allows creation cursors of a certain size
2265 if ((bitmap.Width != width) || (bitmap.Width != height)) {
2266 cursor_bitmap = new Bitmap (bitmap, new Size (width, height));
2267 cursor_mask = new Bitmap (mask, new Size (width, height));
2269 cursor_bitmap = bitmap;
2273 width = cursor_bitmap.Width;
2274 height = cursor_bitmap.Height;
2276 cursor_bits = new Byte [(width / 8) * height];
2277 mask_bits = new Byte [(width / 8) * height];
2279 for (int y = 0; y < height; y++) {
2280 for (int x = 0; x < width; x++) {
2281 c_pixel = cursor_bitmap.GetPixel (x, y);
2282 m_pixel = cursor_mask.GetPixel (x, y);
2284 and = c_pixel == cursor_pixel;
2285 xor = m_pixel == mask_pixel;
2289 // cursor_bits[y * width / 8 + x / 8] &= (byte)~((1 << (x % 8))); // The bit already is 0
2290 mask_bits [y * width / 8 + x / 8] |= (byte)(1 << (x % 8));
2291 } else if (and && !xor) {
2293 cursor_bits [y * width / 8 + x / 8] |= (byte)(1 << (x % 8));
2294 mask_bits [y * width / 8 + x / 8] |= (byte)(1 << (x % 8));
2296 } else if (and && !xor) {
2298 } else if (and && xor) {
2301 // X11 doesn't know the 'reverse screen' concept, so we'll treat them the same
2302 // we want both to be 0 so nothing to be done
2303 //cursor_bits[y * width / 8 + x / 8] &= (byte)~((1 << (x % 8)));
2304 //mask_bits[y * width / 8 + x / 8] |= (byte)(01 << (x % 8));
2310 cursor_pixmap = XCreatePixmapFromBitmapData (DisplayHandle, RootWindow, cursor_bits, width, height, (IntPtr)1, (IntPtr)0, 1);
2311 mask_pixmap = XCreatePixmapFromBitmapData (DisplayHandle, RootWindow, mask_bits, width, height, (IntPtr)1, (IntPtr)0, 1);
2315 fg.pixel = XWhitePixel (DisplayHandle, ScreenNo);
2316 fg.red = (ushort)65535;
2317 fg.green = (ushort)65535;
2318 fg.blue = (ushort)65535;
2320 bg.pixel = XBlackPixel (DisplayHandle, ScreenNo);
2322 cursor = XCreatePixmapCursor (DisplayHandle, cursor_pixmap, mask_pixmap, ref fg, ref bg, xHotSpot, yHotSpot);
2324 XFreePixmap (DisplayHandle, cursor_pixmap);
2325 XFreePixmap (DisplayHandle, mask_pixmap);
2330 internal override IntPtr DefineStdCursor (StdCursor id)
2332 CursorFontShape shape;
2335 // FIXME - define missing shapes
2338 case StdCursor.AppStarting: {
2339 shape = CursorFontShape.XC_watch;
2343 case StdCursor.Arrow: {
2347 case StdCursor.Cross: {
2348 shape = CursorFontShape.XC_crosshair;
2352 case StdCursor.Default: {
2356 case StdCursor.Hand: {
2357 shape = CursorFontShape.XC_hand1;
2361 case StdCursor.Help: {
2362 shape = CursorFontShape.XC_question_arrow;
2366 case StdCursor.HSplit: {
2367 shape = CursorFontShape.XC_sb_v_double_arrow;
2371 case StdCursor.IBeam: {
2372 shape = CursorFontShape.XC_xterm;
2376 case StdCursor.No: {
2377 shape = CursorFontShape.XC_circle;
2381 case StdCursor.NoMove2D: {
2382 shape = CursorFontShape.XC_fleur;
2386 case StdCursor.NoMoveHoriz: {
2387 shape = CursorFontShape.XC_fleur;
2391 case StdCursor.NoMoveVert: {
2392 shape = CursorFontShape.XC_fleur;
2396 case StdCursor.PanEast: {
2397 shape = CursorFontShape.XC_fleur;
2401 case StdCursor.PanNE: {
2402 shape = CursorFontShape.XC_fleur;
2406 case StdCursor.PanNorth: {
2407 shape = CursorFontShape.XC_fleur;
2411 case StdCursor.PanNW: {
2412 shape = CursorFontShape.XC_fleur;
2416 case StdCursor.PanSE: {
2417 shape = CursorFontShape.XC_fleur;
2421 case StdCursor.PanSouth: {
2422 shape = CursorFontShape.XC_fleur;
2426 case StdCursor.PanSW: {
2427 shape = CursorFontShape.XC_fleur;
2431 case StdCursor.PanWest: {
2432 shape = CursorFontShape.XC_sizing;
2436 case StdCursor.SizeAll: {
2437 shape = CursorFontShape.XC_fleur;
2441 case StdCursor.SizeNESW: {
2442 shape = CursorFontShape.XC_top_right_corner;
2446 case StdCursor.SizeNS: {
2447 shape = CursorFontShape.XC_sb_v_double_arrow;
2451 case StdCursor.SizeNWSE: {
2452 shape = CursorFontShape.XC_top_left_corner;
2456 case StdCursor.SizeWE: {
2457 shape = CursorFontShape.XC_sb_h_double_arrow;
2461 case StdCursor.UpArrow: {
2462 shape = CursorFontShape.XC_center_ptr;
2466 case StdCursor.VSplit: {
2467 shape = CursorFontShape.XC_sb_h_double_arrow;
2471 case StdCursor.WaitCursor: {
2472 shape = CursorFontShape.XC_watch;
2482 cursor = XCreateFontCursor (DisplayHandle, shape);
2487 internal override IntPtr DefWndProc (ref Message msg)
2492 internal override void DestroyCaret (IntPtr handle)
2494 if (Caret.Hwnd == handle) {
2495 if (Caret.Visible == 1) {
2496 Caret.Timer.Stop ();
2499 if (Caret.gc != IntPtr.Zero) {
2500 XFreeGC (DisplayHandle, Caret.gc);
2501 Caret.gc = IntPtr.Zero;
2503 Caret.Hwnd = IntPtr.Zero;
2509 internal override void DestroyCursor (IntPtr cursor)
2512 XFreeCursor (DisplayHandle, cursor);
2516 internal override void DestroyWindow (IntPtr handle)
2520 hwnd = Hwnd.ObjectFromHandle (handle);
2524 Console.WriteLine("window {0:X} already destroyed", handle.ToInt32());
2530 Console.WriteLine("Destroying window {0:X}", handle.ToInt32());
2533 // Make sure if the caret is in the window, that we destroy the caret, too
2534 if (Caret.Hwnd == hwnd.client_window) {
2535 DestroyCaret (handle);
2538 // Mark our children as gone as well
2539 DestroyChildWindow (Control.ControlNativeWindow.ControlFromHandle (handle));
2541 // Send destroy message
2542 SendMessage (handle, Msg.WM_DESTROY, IntPtr.Zero, IntPtr.Zero);
2545 if (hwnd.client_window != IntPtr.Zero) {
2546 gdk_window_destroy (gdk_window_lookup (hwnd.client_window));
2549 if ((hwnd.whole_window != IntPtr.Zero) && (hwnd.whole_window != hwnd.client_window)) {
2550 gdk_window_destroy (gdk_window_lookup (hwnd.whole_window));
2556 internal override IntPtr DispatchMessage (ref MSG msg)
2558 return NativeWindow.WndProc (msg.hwnd, msg.message, msg.wParam, msg.lParam);
2561 internal override void DrawReversibleRectangle (IntPtr handle, Rectangle rect, int line_width)
2564 XGCValues gc_values;
2567 hwnd = Hwnd.ObjectFromHandle (handle);
2569 gc_values = new XGCValues ();
2571 gc_values.subwindow_mode = GCSubwindowMode.IncludeInferiors;
2572 gc_values.line_width = line_width;
2573 gc_values.foreground = XBlackPixel (DisplayHandle, ScreenNo);
2575 // This logic will give us true rubber bands: (libsx, SANE_XOR)
2576 //mask = foreground ^ background;
2577 //XSetForeground(DisplayHandle, gc, 0xffffffff);
2578 //XSetBackground(DisplayHandle, gc, background);
2579 //XSetFunction(DisplayHandle, gc, GXxor);
2580 //XSetPlaneMask(DisplayHandle, gc, mask);
2583 gc = XCreateGC (DisplayHandle, hwnd.client_window, GCFunction.GCSubwindowMode | GCFunction.GCLineWidth | GCFunction.GCForeground, ref gc_values);
2588 control = Control.FromHandle (handle);
2590 XColor xcolor = new XColor ();
2592 xcolor.red = (ushort)(control.ForeColor.R * 257);
2593 xcolor.green = (ushort)(control.ForeColor.G * 257);
2594 xcolor.blue = (ushort)(control.ForeColor.B * 257);
2595 XAllocColor (DisplayHandle, DefaultColormap, ref xcolor);
2596 foreground = (uint)xcolor.pixel.ToInt32 ();
2598 xcolor.red = (ushort)(control.BackColor.R * 257);
2599 xcolor.green = (ushort)(control.BackColor.G * 257);
2600 xcolor.blue = (ushort)(control.BackColor.B * 257);
2601 XAllocColor (DisplayHandle, DefaultColormap, ref xcolor);
2602 background = (uint)xcolor.pixel.ToInt32 ();
2604 uint mask = foreground ^ background;
2606 XSetForeground (DisplayHandle, gc, 0xffffffff);
2607 XSetBackground (DisplayHandle, gc, background);
2608 XSetFunction (DisplayHandle, gc, GXFunction.GXxor);
2609 XSetPlaneMask (DisplayHandle, gc, mask);
2611 if ((rect.Width > 0) && (rect.Height > 0)) {
2612 XDrawRectangle (DisplayHandle, hwnd.client_window, gc, rect.Left, rect.Top, rect.Width, rect.Height);
2614 if (rect.Width > 0) {
2615 XDrawLine (DisplayHandle, hwnd.client_window, gc, rect.X, rect.Y, rect.Right, rect.Y);
2617 XDrawLine (DisplayHandle, hwnd.client_window, gc, rect.X, rect.Y, rect.X, rect.Bottom);
2620 XFreeGC (DisplayHandle, gc);
2623 internal override void DoEvents ()
2625 MSG msg = new MSG ();
2627 if (OverrideCursorHandle != IntPtr.Zero) {
2628 OverrideCursorHandle = IntPtr.Zero;
2631 while (PeekMessage (ref msg, IntPtr.Zero, 0, 0, (uint)PeekMessageFlags.PM_REMOVE)) {
2632 TranslateMessage (ref msg);
2633 DispatchMessage (ref msg);
2637 internal override void EnableWindow (IntPtr handle, bool Enable)
2641 hwnd = Hwnd.ObjectFromHandle (handle);
2643 hwnd.Enabled = Enable;
2647 internal override void EndLoop (Thread thread)
2649 // This is where we one day will shut down the loop for the thread
2653 internal override IntPtr GetActive ()
2659 IntPtr prop = IntPtr.Zero;
2660 IntPtr active = IntPtr.Zero;
2662 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);
2663 if ((nitems > 0) && (prop != IntPtr.Zero)) {
2664 active = (IntPtr)Marshal.ReadInt32 (prop);
2668 if (active != IntPtr.Zero) {
2671 hwnd = Hwnd.GetObjectFromWindow (active);
2673 active = hwnd.Handle;
2675 active = IntPtr.Zero;
2681 internal override void GetCursorInfo (IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y)
2683 throw new NotImplementedException ();
2686 internal override void GetDisplaySize (out Size size)
2688 XWindowAttributes attributes=new XWindowAttributes ();
2691 // FIXME - use _NET_WM messages instead?
2692 XGetWindowAttributes (DisplayHandle, XRootWindow (DisplayHandle, ScreenNo), ref attributes);
2695 size = new Size (attributes.width, attributes.height);
2698 internal override SizeF GetAutoScaleSize (Font font)
2702 string magic_string = "The quick brown fox jumped over the lazy dog.";
2703 double magic_number = 44.549996948242189;
2705 g = Graphics.FromHwnd (FosterParent);
2707 width = (float) (g.MeasureString (magic_string, font).Width / magic_number);
2708 return new SizeF (width, font.Height);
2711 internal override IntPtr GetParent (IntPtr handle)
2715 hwnd = Hwnd.ObjectFromHandle (handle);
2716 if (hwnd != null && hwnd.parent != null) {
2717 return hwnd.parent.Handle;
2722 internal override void GetCursorPos (IntPtr handle, out int x, out int y)
2733 if (handle != IntPtr.Zero) {
2734 use_handle = Hwnd.ObjectFromHandle (handle).client_window;
2736 use_handle = RootWindow;
2740 XQueryPointer (DisplayHandle, use_handle, out root, out child, out root_x, out root_y, out win_x, out win_y, out keys_buttons);
2743 if (handle != IntPtr.Zero) {
2752 internal override bool GetFontMetrics (Graphics g, Font font, out int ascent, out int descent)
2754 return GetFontMetrics (g.GetHdc (), font.ToHfont (), out ascent, out descent);
2757 internal override Point GetMenuOrigin (IntPtr handle)
2761 hwnd = Hwnd.ObjectFromHandle (handle);
2764 return hwnd.MenuOrigin;
2769 internal override bool GetMessage (ref MSG msg, IntPtr handle, int wFilterMin, int wFilterMax)
2777 if (MessageQueue.Count > 0) {
2778 xevent = (XEvent) MessageQueue.Dequeue ();
2780 UpdateMessageQueue ();
2782 if (MessageQueue.Count > 0) {
2783 xevent = (XEvent) MessageQueue.Dequeue ();
2785 if (!PostQuitState) {
2786 msg.hwnd = IntPtr.Zero;
2787 msg.message = Msg.WM_ENTERIDLE;
2791 // We reset ourselves so GetMessage can be called again
2792 PostQuitState = false;
2798 // FIXME - handle filtering
2800 hwnd = Hwnd.GetObjectFromWindow (xevent.AnyEvent.window);
2802 // Handle messages for windows that are already or are about to be destroyed
2805 Console.WriteLine("GetMessage(): Got message {0} for non-existent or already destroyed window {1:X}", xevent.type, xevent.AnyEvent.window.ToInt32());
2807 goto ProcessNextMessage;
2810 if (hwnd.client_window == xevent.AnyEvent.window) {
2812 //Console.WriteLine("Client message, sending to window {0:X}", msg.hwnd.ToInt32());
2815 //Console.WriteLine("Non-Client message, sending to window {0:X}", msg.hwnd.ToInt32());
2818 msg.hwnd = hwnd.Handle;
2821 // If you add a new event to this switch make sure to add it in
2822 // UpdateMessage also unless it is not coming through the X event system.
2824 switch (xevent.type) {
2825 case XEventName.KeyPress: {
2826 Keyboard.KeyEvent (FocusWindow, xevent, ref msg);
2830 case XEventName.KeyRelease: {
2831 Keyboard.KeyEvent (FocusWindow, xevent, ref msg);
2835 case XEventName.ButtonPress: {
2836 switch (xevent.ButtonEvent.button) {
2838 MouseState |= MouseButtons.Left;
2840 msg.message = Msg.WM_LBUTTONDOWN;
2842 msg.message = Msg.WM_NCLBUTTONDOWN;
2843 WholeToScreen (msg.hwnd, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2845 // TODO: For WM_NCLBUTTONDOWN wParam specifies a hit-test value not the virtual keys down
2846 msg.wParam = GetMousewParam (0);
2851 MouseState |= MouseButtons.Middle;
2853 msg.message = Msg.WM_MBUTTONDOWN;
2855 msg.message = Msg.WM_NCMBUTTONDOWN;
2856 WholeToScreen (msg.hwnd, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2858 msg.wParam = GetMousewParam (0);
2863 MouseState |= MouseButtons.Right;
2865 msg.message = Msg.WM_RBUTTONDOWN;
2867 msg.message = Msg.WM_NCRBUTTONDOWN;
2868 WholeToScreen (msg.hwnd, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2870 msg.wParam = GetMousewParam (0);
2875 msg.message = Msg.WM_MOUSEWHEEL;
2876 msg.wParam = GetMousewParam (120);
2881 msg.message = Msg.WM_MOUSEWHEEL;
2882 msg.wParam = GetMousewParam (-120);
2888 msg.lParam = (IntPtr) (xevent.ButtonEvent.y << 16 | xevent.ButtonEvent.x);
2889 MousePosition.X = xevent.ButtonEvent.x;
2890 MousePosition.Y = xevent.ButtonEvent.y;
2892 if (!hwnd.Enabled) {
2895 msg.hwnd = hwnd.EnabledHwnd;
2896 XTranslateCoordinates (DisplayHandle, xevent.AnyEvent.window, Hwnd.ObjectFromHandle (msg.hwnd).ClientWindow, xevent.ButtonEvent.x, xevent.ButtonEvent.y, out xevent.ButtonEvent.x, out xevent.ButtonEvent.y, out dummy);
2897 msg.lParam = (IntPtr)(MousePosition.Y << 16 | MousePosition.X);
2900 if (Grab.Hwnd != IntPtr.Zero) {
2901 msg.hwnd = Grab.Hwnd;
2904 if (!ClickPending.Pending) {
2905 ClickPending.Pending = true;
2906 ClickPending.Hwnd = msg.hwnd;
2907 ClickPending.Message = msg.message;
2908 ClickPending.wParam = msg.wParam;
2909 ClickPending.lParam = msg.lParam;
2910 ClickPending.Time = (long)xevent.ButtonEvent.time;
2912 if ((((long)xevent.ButtonEvent.time - ClickPending.Time) < DoubleClickInterval) && (msg.wParam == ClickPending.wParam) && (msg.lParam == ClickPending.lParam) && (msg.message == ClickPending.Message)) {
2913 // Looks like a genuine double click, clicked twice on the same spot with the same keys
2914 switch (xevent.ButtonEvent.button) {
2916 msg.message = client ? Msg.WM_LBUTTONDBLCLK : Msg.WM_NCLBUTTONDBLCLK;
2921 msg.message = client ? Msg.WM_MBUTTONDBLCLK : Msg.WM_NCMBUTTONDBLCLK;
2926 msg.message = client ? Msg.WM_RBUTTONDBLCLK : Msg.WM_NCRBUTTONDBLCLK;
2931 ClickPending.Pending = false;
2937 case XEventName.ButtonRelease: {
2938 Dnd.HandleButtonRelease (ref xevent);
2939 switch (xevent.ButtonEvent.button) {
2941 MouseState &= ~MouseButtons.Left;
2943 msg.message = Msg.WM_LBUTTONUP;
2945 msg.message = Msg.WM_NCLBUTTONUP;
2946 WholeToScreen (msg.hwnd, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2948 msg.wParam = GetMousewParam (0);
2953 MouseState &= ~MouseButtons.Middle;
2955 msg.message = Msg.WM_MBUTTONUP;
2957 msg.message = Msg.WM_NCMBUTTONUP;
2958 WholeToScreen (msg.hwnd, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2960 msg.wParam = GetMousewParam (0);
2965 MouseState &= ~MouseButtons.Right;
2967 msg.message = Msg.WM_RBUTTONUP;
2969 msg.message = Msg.WM_NCRBUTTONUP;
2970 WholeToScreen (msg.hwnd, ref xevent.ButtonEvent.x, ref xevent.ButtonEvent.y);
2972 msg.wParam = GetMousewParam (0);
2977 goto ProcessNextMessage;
2981 goto ProcessNextMessage;
2985 if (!hwnd.Enabled) {
2988 msg.hwnd = hwnd.EnabledHwnd;
2989 XTranslateCoordinates (DisplayHandle, xevent.AnyEvent.window, Hwnd.ObjectFromHandle (msg.hwnd).ClientWindow, xevent.ButtonEvent.x, xevent.ButtonEvent.y, out xevent.ButtonEvent.x, out xevent.ButtonEvent.y, out dummy);
2990 msg.lParam = (IntPtr)(MousePosition.Y << 16 | MousePosition.X);
2993 if (Grab.Hwnd != IntPtr.Zero) {
2994 msg.hwnd = Grab.Hwnd;
2997 msg.lParam = (IntPtr) (xevent.ButtonEvent.y << 16 | xevent.ButtonEvent.x);
2998 MousePosition.X = xevent.ButtonEvent.x;
2999 MousePosition.Y = xevent.ButtonEvent.y;
3003 case XEventName.MotionNotify: {
3005 #if DriverDebugExtra
3006 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);
3009 if (Dnd.HandleMotionNotify (ref xevent))
3010 goto ProcessNextMessage;
3011 if (Grab.Hwnd != IntPtr.Zero) {
3012 msg.hwnd = Grab.Hwnd;
3014 NativeWindow.WndProc (msg.hwnd, Msg.WM_SETCURSOR, msg.hwnd, (IntPtr)HitTest.HTCLIENT);
3017 msg.message = Msg.WM_MOUSEMOVE;
3018 msg.wParam = GetMousewParam (0);
3019 msg.lParam = (IntPtr) (xevent.MotionEvent.y << 16 | xevent.MotionEvent.x & 0xFFFF);
3021 if (!hwnd.Enabled) {
3024 msg.hwnd = hwnd.EnabledHwnd;
3025 XTranslateCoordinates (DisplayHandle, xevent.AnyEvent.window, Hwnd.ObjectFromHandle (msg.hwnd).ClientWindow, xevent.MotionEvent.x, xevent.MotionEvent.y, out xevent.MotionEvent.x, out xevent.MotionEvent.y, out dummy);
3026 msg.lParam = (IntPtr)(MousePosition.Y << 16 | MousePosition.X);
3029 HoverState.X = MousePosition.X = xevent.MotionEvent.x;
3030 HoverState.Y = MousePosition.Y = xevent.MotionEvent.y;
3034 #if DriverDebugExtra
3035 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);
3037 msg.message = Msg.WM_NCMOUSEMOVE;
3038 msg.lParam = (IntPtr) (xevent.MotionEvent.y << 16 | xevent.MotionEvent.x & 0xFFFF);
3040 if (!hwnd.Enabled) {
3043 msg.hwnd = hwnd.EnabledHwnd;
3044 XTranslateCoordinates (DisplayHandle, xevent.AnyEvent.window, Hwnd.ObjectFromHandle (msg.hwnd).ClientWindow, xevent.MotionEvent.x, xevent.MotionEvent.y, out xevent.MotionEvent.x, out xevent.MotionEvent.y, out dummy);
3045 msg.lParam = (IntPtr)(MousePosition.Y << 16 | MousePosition.X);
3049 // Not sure we need this...
3051 ht = NativeWindow.WndProc(hwnd.client_window, Msg.WM_NCHITTEST, IntPtr.Zero, msg.lParam);
3054 MousePosition.X = xevent.MotionEvent.x;
3055 MousePosition.Y = xevent.MotionEvent.y;
3061 case XEventName.EnterNotify: {
3062 if (!hwnd.Enabled) {
3063 goto ProcessNextMessage;
3065 if (xevent.CrossingEvent.mode != NotifyMode.NotifyNormal) {
3066 goto ProcessNextMessage;
3068 msg.message = Msg.WM_MOUSE_ENTER;
3069 HoverState.Timer.Enabled = true;
3070 HoverState.Window = xevent.CrossingEvent.window;
3074 case XEventName.LeaveNotify: {
3075 if (!hwnd.Enabled) {
3076 goto ProcessNextMessage;
3078 if (xevent.CrossingEvent.mode != NotifyMode.NotifyNormal) {
3079 goto ProcessNextMessage;
3081 msg.message = Msg.WM_MOUSE_LEAVE;
3082 HoverState.Timer.Enabled = false;
3083 HoverState.Window = IntPtr.Zero;
3088 case XEventName.CreateNotify: {
3089 if (client && (xevent.ConfigureEvent.xevent == xevent.ConfigureEvent.window)) {
3090 msg.message = WM_CREATE;
3091 // Set up CreateStruct
3093 goto ProcessNextMessage;
3100 case XEventName.ReparentNotify: {
3101 if (hwnd.parent == null) { // Toplevel
3102 if (xevent.ReparentEvent.parent != IntPtr.Zero) {
3103 // We need to adjust x/y
3106 hwnd.Reparented = true;
3108 gdk_window_get_geometry (gdk_window_lookup (hwnd.whole_window), out hwnd.x, out hwnd.y, out dummy_int, out dummy_int, out dummy_int);
3109 msg.message = Msg.WM_WINDOWPOSCHANGED;
3110 if (hwnd.opacity != 0xffffffff) {
3113 opacity = hwnd.opacity;
3114 XChangeProperty (DisplayHandle, XGetParent (hwnd.whole_window), NetAtoms [(int)NA._NET_WM_WINDOW_OPACITY], Atom.XA_CARDINAL, 32, PropertyMode.Replace, ref opacity, 1);
3117 hwnd.Reparented = false;
3118 goto ProcessNextMessage;
3124 case XEventName.ConfigureNotify: {
3125 if (!client && (xevent.ConfigureEvent.xevent == xevent.ConfigureEvent.window)) { // Ignore events for children (SubstructureNotify) and client areas
3126 XplatUIWin32.NCCALCSIZE_PARAMS ncp;
3130 #if DriverDebugExtra
3131 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);
3133 msg.message = Msg.WM_WINDOWPOSCHANGED;
3134 hwnd.configure_pending = false;
3136 // We need to adjust our client window to track the resize of whole_window
3137 rect = hwnd.DefaultClientRect;
3139 ncp = new XplatUIWin32.NCCALCSIZE_PARAMS ();
3140 ptr = Marshal.AllocHGlobal (Marshal.SizeOf (ncp));
3142 ncp.rgrc1.left = rect.Left;
3143 ncp.rgrc1.top = rect.Top;
3144 ncp.rgrc1.right = rect.Right;
3145 ncp.rgrc1.bottom = rect.Bottom;
3147 Marshal.StructureToPtr (ncp, ptr, true);
3148 NativeWindow.WndProc (hwnd.client_window, Msg.WM_NCCALCSIZE, (IntPtr)1, ptr);
3149 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure (ptr, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
3150 Marshal.FreeHGlobal (ptr);
3152 // FIXME - debug this with Menus, need to set hwnd.ClientRect
3154 rect = new Rectangle (ncp.rgrc1.left, ncp.rgrc1.top, ncp.rgrc1.right - ncp.rgrc1.left, ncp.rgrc1.bottom - ncp.rgrc1.top);
3155 //Console.WriteLine("CreateOffscreenbuffer...");
3156 // CreateOffscreenBuffer (ref hwnd.client_offscreen, rect.Width, rect.Height);
3158 XMoveResizeWindow (DisplayHandle, hwnd.client_window, rect.X, rect.Y, rect.Width, rect.Height);
3160 goto ProcessNextMessage;
3163 msg.lParam = IntPtr.Zero; // FIXME - Generate LPWINDOWPOS structure and pass on
3167 case XEventName.FocusIn: {
3168 if (!hwnd.Enabled) {
3169 goto ProcessNextMessage;
3171 msg.message = Msg.WM_SETFOCUS;
3172 msg.wParam = IntPtr.Zero;
3176 case XEventName.FocusOut: {
3177 if (!hwnd.Enabled) {
3178 goto ProcessNextMessage;
3180 msg.message = Msg.WM_KILLFOCUS;
3181 msg.wParam = IntPtr.Zero;
3185 case XEventName.Expose: {
3186 if (PostQuitState) {
3187 goto ProcessNextMessage;
3192 if (!hwnd.expose_pending) {
3193 goto ProcessNextMessage;
3196 if (!hwnd.nc_expose_pending) {
3197 goto ProcessNextMessage;
3200 switch (hwnd.border_style) {
3201 case FormBorderStyle.Fixed3D: {
3204 g = Graphics.FromHwnd (hwnd.whole_window);
3205 ControlPaint.DrawBorder3D (g, new Rectangle (0, 0, hwnd.Width, hwnd.Height));
3210 case FormBorderStyle.FixedSingle: {
3213 g = Graphics.FromHwnd (hwnd.whole_window);
3214 ControlPaint.DrawBorder (g, new Rectangle (0, 0, hwnd.Width, hwnd.Height), Color.Black, ButtonBorderStyle.Solid);
3219 #if DriverDebugExtra
3220 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);
3223 Rectangle rect = new Rectangle (xevent.ExposeEvent.x, xevent.ExposeEvent.y, xevent.ExposeEvent.width, xevent.ExposeEvent.height);
3224 Region region = new Region (rect);
3225 IntPtr hrgn = region.GetHrgn (null); // Graphics object isn't needed
3226 msg.message = Msg.WM_NCPAINT;
3228 hwnd.nc_expose_pending = false;
3231 #if DriverDebugExtra
3232 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);
3234 if (Caret.Visible == 1) {
3235 Caret.Paused = true;
3239 if (Caret.Visible == 1) {
3241 Caret.Paused = false;
3243 msg.message = Msg.WM_PAINT;
3247 case XEventName.DestroyNotify: {
3249 // This is a bit tricky, we don't receive our own DestroyNotify, we only get those for our children
3250 hwnd = Hwnd.ObjectFromHandle (xevent.DestroyWindowEvent.window);
3252 // We may get multiple for the same window, act only one the first (when Hwnd still knows about it)
3253 if (hwnd.client_window == xevent.DestroyWindowEvent.window) {
3254 msg.hwnd = hwnd.client_window;
3255 msg.message = Msg.WM_DESTROY;
3259 Console.WriteLine("Got DestroyNotify on Window {0:X}", msg.hwnd.ToInt32());
3262 goto ProcessNextMessage;
3268 case XEventName.ClientMessage: {
3269 if (Dnd.HandleClientMessage (ref xevent)) {
3270 goto ProcessNextMessage;
3273 if (xevent.ClientMessageEvent.message_type == (IntPtr)AsyncAtom) {
3274 XplatUIDriverSupport.ExecuteClientMessage ((GCHandle)xevent.ClientMessageEvent.ptr1);
3278 if (xevent.ClientMessageEvent.message_type == (IntPtr)HoverState.Atom) {
3279 msg.message = Msg.WM_MOUSEHOVER;
3280 msg.wParam = GetMousewParam (0);
3281 msg.lParam = (IntPtr) (xevent.ClientMessageEvent.ptr1);
3285 if (xevent.ClientMessageEvent.message_type == (IntPtr)PostAtom) {
3286 msg.hwnd = xevent.ClientMessageEvent.ptr1;
3287 msg.message = (Msg) xevent.ClientMessageEvent.ptr2.ToInt32 ();
3288 msg.wParam = xevent.ClientMessageEvent.ptr3;
3289 msg.lParam = xevent.ClientMessageEvent.ptr4;
3294 if (xevent.ClientMessageEvent.message_type == (IntPtr)NetAtoms[(int)NA._XEMBED]) {
3295 Console.WriteLine("GOT EMBED MESSAGE {0:X}", xevent.ClientMessageEvent.ptr2.ToInt32());
3300 if (xevent.ClientMessageEvent.message_type == (IntPtr)NetAtoms [(int)NA.WM_PROTOCOLS]) {
3301 if (xevent.ClientMessageEvent.ptr1 == (IntPtr)NetAtoms [(int)NA.WM_DELETE_WINDOW]) {
3302 msg.message = Msg.WM_CLOSE;
3303 Graphics.FromHdcInternal (IntPtr.Zero);
3307 // We should not get this, but I'll leave the code in case we need it in the future
3308 if (xevent.ClientMessageEvent.ptr1 == (IntPtr)NetAtoms [(int)NA.WM_TAKE_FOCUS]) {
3309 goto ProcessNextMessage;
3315 case XEventName.TimerNotify: {
3316 xevent.TimerNotifyEvent.handler (this, EventArgs.Empty);
3321 goto ProcessNextMessage;
3328 internal override bool GetText (IntPtr handle, out string text)
3332 textptr = IntPtr.Zero;
3335 // FIXME - use _NET properties
3336 XFetchName (DisplayHandle, Hwnd.ObjectFromHandle (handle).whole_window, ref textptr);
3338 if (textptr != IntPtr.Zero) {
3339 text = Marshal.PtrToStringAnsi (textptr);
3348 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)
3353 hwnd = Hwnd.ObjectFromHandle (handle);
3359 height = hwnd.height;
3361 rect = Hwnd.GetClientRectangle (hwnd.border_style, hwnd.menu, hwnd.title_style, hwnd.caption_height, hwnd.tool_caption_height, width, height);
3363 client_width = rect.Width;
3364 client_height = rect.Height;
3369 // Should we throw an exception or fail silently?
3370 // throw new ArgumentException("Called with an invalid window handle", "handle");
3380 internal override FormWindowState GetWindowState (IntPtr handle)
3386 IntPtr prop = IntPtr.Zero;
3390 XWindowAttributes attributes;
3393 hwnd = Hwnd.ObjectFromHandle (handle);
3397 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);
3398 if ((nitems > 0) && (prop != IntPtr.Zero)) {
3399 for (int i = 0; i < nitems; i++) {
3400 atom = (IntPtr)Marshal.ReadInt32 (prop, i * 4);
3401 if ((atom == (IntPtr)NetAtoms [(int)NA._NET_WM_STATE_MAXIMIZED_HORZ]) || (atom == (IntPtr)NetAtoms [(int)NA._NET_WM_STATE_MAXIMIZED_VERT])) {
3403 } else if (atom == (IntPtr)NetAtoms [(int)NA._NET_WM_STATE_HIDDEN]) {
3411 return FormWindowState.Minimized;
3412 } else if (maximized == 2) {
3413 return FormWindowState.Maximized;
3416 attributes = new XWindowAttributes ();
3417 XGetWindowAttributes (DisplayHandle, handle, ref attributes);
3418 if (attributes.map_state == MapState.IsUnmapped) {
3419 throw new NotSupportedException ("Cannot retrieve the state of an unmapped window");
3423 return FormWindowState.Normal;
3426 internal override void GrabInfo (out IntPtr handle, out bool GrabConfined, out Rectangle GrabArea)
3429 GrabConfined = Grab.Confined;
3430 GrabArea = Grab.Area;
3433 internal override void GrabWindow (IntPtr handle, IntPtr confine_to_handle)
3436 IntPtr confine_to_window;
3438 confine_to_window = IntPtr.Zero;
3440 if (confine_to_handle != IntPtr.Zero) {
3441 XWindowAttributes attributes = new XWindowAttributes ();
3443 hwnd = Hwnd.ObjectFromHandle (confine_to_handle);
3446 XGetWindowAttributes (DisplayHandle, hwnd.client_window, ref attributes);
3448 Grab.Area.X = attributes.x;
3449 Grab.Area.Y = attributes.y;
3450 Grab.Area.Width = attributes.width;
3451 Grab.Area.Height = attributes.height;
3452 Grab.Confined = true;
3453 confine_to_window = hwnd.client_window;
3458 hwnd = Hwnd.ObjectFromHandle (handle);
3461 XGrabPointer (DisplayHandle, hwnd.client_window, false,
3462 EventMask.ButtonPressMask | EventMask.ButtonMotionMask |
3463 EventMask.ButtonReleaseMask | EventMask.PointerMotionMask,
3464 GrabMode.GrabModeAsync, GrabMode.GrabModeAsync, confine_to_window, 0, 0);
3468 internal override void UngrabWindow (IntPtr hwnd)
3471 XUngrabPointer (DisplayHandle, 0);
3472 XFlush (DisplayHandle);
3474 Grab.Hwnd = IntPtr.Zero;
3475 Grab.Confined = false;
3478 internal override void HandleException (Exception e)
3480 StackTrace st = new StackTrace (e, true);
3481 Console.WriteLine ("Exception '{0}'", e.Message + st.ToString ());
3482 Console.WriteLine ("{0}{1}", e.Message, st.ToString ());
3485 internal override void Invalidate (IntPtr handle, Rectangle rc, bool clear)
3490 hwnd = Hwnd.ObjectFromHandle (handle);
3493 xevent = new XEvent ();
3494 xevent.type = XEventName.Expose;
3495 xevent.ExposeEvent.display = DisplayHandle;
3496 xevent.ExposeEvent.window = hwnd.client_window;
3499 xevent.ExposeEvent.x = hwnd.X;
3500 xevent.ExposeEvent.y = hwnd.Y;
3501 xevent.ExposeEvent.width = hwnd.Width;
3502 xevent.ExposeEvent.height = hwnd.Height;
3504 xevent.ExposeEvent.x = rc.X;
3505 xevent.ExposeEvent.y = rc.Y;
3506 xevent.ExposeEvent.width = rc.Width;
3507 xevent.ExposeEvent.height = rc.Height;
3513 internal override bool IsEnabled (IntPtr handle)
3515 return Hwnd.ObjectFromHandle (handle).Enabled;
3518 internal override bool IsVisible (IntPtr handle)
3520 return Hwnd.ObjectFromHandle (handle).visible;
3523 internal override void KillTimer (Timer timer)
3526 TimerList.Remove (timer);
3530 internal override void MenuToScreen (IntPtr handle, ref int x, ref int y)
3537 hwnd = Hwnd.ObjectFromHandle (handle);
3540 XTranslateCoordinates (DisplayHandle, hwnd.whole_window, RootWindow, x, y, out dest_x_return, out dest_y_return, out child);
3547 internal override void OverrideCursor (IntPtr cursor)
3549 OverrideCursorHandle = cursor;
3552 internal override PaintEventArgs PaintEventStart (IntPtr handle, bool client)
3554 PaintEventArgs paint_event;
3557 hwnd = Hwnd.ObjectFromHandle (handle);
3559 if (Caret.Visible == 1) {
3560 Caret.Paused = true;
3565 // handle backing store
3566 IntPtr gdk_window = gdk_window_lookup (hwnd.client_window);
3567 IntPtr gdk_pixmap = NewPixmap (gdk_window, hwnd.ClientRect.Width, hwnd.ClientRect.Height);
3569 backing_store [gdk_window] = gdk_pixmap;
3571 hwnd.client_dc = Graphics.FromHwnd (gdk_x11_drawable_get_xid (gdk_pixmap));
3572 hwnd.client_dc.SetClip (hwnd.invalid);
3573 paint_event = new PaintEventArgs (hwnd.client_dc, hwnd.invalid);
3574 hwnd.expose_pending = false;
3578 hwnd.client_dc = Graphics.FromHwnd (hwnd.whole_window);
3579 paint_event = new PaintEventArgs (hwnd.client_dc, new Rectangle (0, 0, hwnd.width, hwnd.height));
3580 hwnd.nc_expose_pending = false;
3586 internal override void PaintEventEnd (IntPtr handle, bool client)
3590 hwnd = Hwnd.ObjectFromHandle (handle);
3592 hwnd.client_dc.Flush ();
3595 // clients are already drawn to a backing store pixmap
3596 IntPtr gdk_window = gdk_window_lookup (hwnd.client_window);
3597 IntPtr gdk_pixmap = (IntPtr)backing_store [gdk_window];
3599 BlitOffscreenPixmap (gdk_pixmap, gdk_window, hwnd.Invalid);
3601 g_object_unref (gdk_pixmap);
3602 backing_store.Remove (gdk_pixmap);
3604 hwnd.ClearInvalidArea ();
3607 hwnd.client_dc.Dispose ();
3608 hwnd.client_dc = null;
3610 if (Caret.Visible == 1) {
3612 Caret.Paused = false;
3616 internal override bool PeekMessage (ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags)
3620 // FIXME - imlement filtering
3622 if ((flags & (uint)PeekMessageFlags.PM_REMOVE) == 0) {
3623 throw new NotImplementedException ("PeekMessage PM_NOREMOVE is not implemented yet"); // FIXME - Implement PM_NOREMOVE flag
3627 if (MessageQueue.Count > 0) {
3630 // Only call UpdateMessageQueue if real events are pending
3631 // otherwise we go to sleep on the socket
3632 if (XPending (DisplayHandle) != 0) {
3633 UpdateMessageQueue ();
3638 CheckTimers (DateTime.Now);
3643 return GetMessage (ref msg, hWnd, wFilterMin, wFilterMax);
3646 // FIXME - I think this should just enqueue directly
3647 internal override bool PostMessage (IntPtr handle, Msg message, IntPtr wparam, IntPtr lparam)
3649 XEvent xevent = new XEvent ();
3650 Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
3652 xevent.type = XEventName.ClientMessage;
3653 xevent.ClientMessageEvent.display = DisplayHandle;
3656 xevent.ClientMessageEvent.window = hwnd.whole_window;
3658 xevent.ClientMessageEvent.window = IntPtr.Zero;
3661 xevent.ClientMessageEvent.message_type = (IntPtr) PostAtom;
3662 xevent.ClientMessageEvent.format = 32;
3663 xevent.ClientMessageEvent.ptr1 = handle;
3664 xevent.ClientMessageEvent.ptr2 = (IntPtr) message;
3665 xevent.ClientMessageEvent.ptr3 = wparam;
3666 xevent.ClientMessageEvent.ptr4 = lparam;
3668 MessageQueue.Enqueue (xevent);
3673 internal override void PostQuitMessage (int exitCode)
3675 XFlush (DisplayHandle);
3676 PostQuitState = true;
3678 // Remove our display handle from S.D
3679 Graphics.FromHdcInternal (IntPtr.Zero);
3682 internal override void ScreenToClient (IntPtr handle, ref int x, ref int y)
3689 hwnd = Hwnd.ObjectFromHandle (handle);
3692 XTranslateCoordinates (DisplayHandle, RootWindow, hwnd.client_window, x, y, out dest_x_return, out dest_y_return, out child);
3699 internal override void ScreenToMenu (IntPtr handle, ref int x, ref int y)
3706 hwnd = Hwnd.ObjectFromHandle (handle);
3709 XTranslateCoordinates (DisplayHandle, RootWindow, hwnd.whole_window, x, y, out dest_x_return, out dest_y_return, out child);
3716 internal override void ScrollWindow (IntPtr handle, Rectangle area, int XAmount, int YAmount, bool with_children)
3720 XGCValues gc_values;
3722 hwnd = Hwnd.ObjectFromHandle (handle);
3724 if (hwnd.invalid != Rectangle.Empty) {
3725 // BIG FAT WARNING. This only works with how we use this function right now
3726 // where we basically still scroll the whole window, but work around areas
3727 // that are covered by our children
3729 hwnd.invalid.X += XAmount;
3730 hwnd.invalid.Y += YAmount;
3732 if (hwnd.invalid.X < 0) {
3733 hwnd.invalid.Width += hwnd.invalid.X;
3737 if (hwnd.invalid.Y < 0) {
3738 hwnd.invalid.Height += hwnd.invalid.Y;
3743 gc_values = new XGCValues ();
3745 if (with_children) {
3746 gc_values.subwindow_mode = GCSubwindowMode.IncludeInferiors;
3749 gc = XCreateGC (DisplayHandle, hwnd.client_window, 0, ref gc_values);
3751 XCopyArea (DisplayHandle, hwnd.client_window, hwnd.client_window, gc, area.X - XAmount, area.Y - YAmount, area.Width, area.Height, area.X, area.Y);
3753 // Generate an expose for the area exposed by the horizontal scroll
3755 hwnd.AddInvalidArea (area.X, area.Y, XAmount, area.Height);
3756 } else if (XAmount < 0) {
3757 hwnd.AddInvalidArea (XAmount + area.X + area.Width, area.Y, -XAmount, area.Height);
3760 // Generate an expose for the area exposed by the vertical scroll
3762 hwnd.AddInvalidArea (area.X, area.Y, area.Width, YAmount);
3763 } else if (YAmount < 0) {
3764 hwnd.AddInvalidArea (area.X, YAmount + area.Y + area.Height, area.Width, -YAmount);
3766 XFreeGC (DisplayHandle, gc);
3768 UpdateWindow (handle);
3771 internal override void ScrollWindow (IntPtr handle, int XAmount, int YAmount, bool with_children)
3775 hwnd = Hwnd.GetObjectFromWindow (handle);
3777 ScrollWindow (handle, hwnd.ClientRect, XAmount, YAmount, with_children);
3780 internal override void SendAsyncMethod (AsyncMethodData method)
3782 XEvent xevent = new XEvent ();
3784 xevent.type = XEventName.ClientMessage;
3785 xevent.ClientMessageEvent.display = DisplayHandle;
3786 xevent.ClientMessageEvent.window = FosterParent;
3787 xevent.ClientMessageEvent.message_type = (IntPtr)AsyncAtom;
3788 xevent.ClientMessageEvent.format = 32;
3789 xevent.ClientMessageEvent.ptr1 = (IntPtr) GCHandle.Alloc (method);
3791 MessageQueue.EnqueueLocked (xevent);
3796 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam)
3798 return NativeWindow.WndProc (hwnd, message, wParam, lParam);
3801 internal override void SetAllowDrop (IntPtr handle, bool value)
3803 // We allow drop on all windows
3806 internal override DragDropEffects StartDrag (IntPtr handle, object data,
3807 DragDropEffects allowed_effects)
3809 Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
3812 throw new ArgumentException ("Attempt to begin drag from invalid window handle (" + handle.ToInt32 () + ").");
3814 return Dnd.StartDrag (hwnd.client_window, data, allowed_effects);
3817 internal override void SetBorderStyle (IntPtr handle, FormBorderStyle border_style)
3821 hwnd = Hwnd.ObjectFromHandle (handle);
3823 hwnd.border_style = border_style;
3825 XMoveResizeWindow (DisplayHandle, hwnd.client_window, hwnd.ClientRect.X, hwnd.ClientRect.Y, hwnd.ClientRect.Width, hwnd.ClientRect.Height);
3827 InvalidateWholeWindow (handle);
3830 internal override void SetCaretPos (IntPtr handle, int x, int y)
3832 if (Caret.Hwnd == handle) {
3833 Caret.Timer.Stop ();
3839 if (Caret.Visible == 1) {
3841 Caret.Timer.Start ();
3846 internal override void SetCursor (IntPtr handle, IntPtr cursor)
3850 if (OverrideCursorHandle == IntPtr.Zero) {
3851 if ((LastCursorWindow == handle) && (LastCursorHandle == cursor)) {
3855 LastCursorHandle = cursor;
3856 LastCursorWindow = handle;
3858 hwnd = Hwnd.ObjectFromHandle (handle);
3860 if (cursor != IntPtr.Zero) {
3861 XDefineCursor (DisplayHandle, hwnd.whole_window, cursor);
3863 XUndefineCursor (DisplayHandle, hwnd.whole_window);
3869 hwnd = Hwnd.ObjectFromHandle (handle);
3871 XDefineCursor (DisplayHandle, hwnd.whole_window, OverrideCursorHandle);
3875 internal override void SetCursorPos (IntPtr handle, int x, int y)
3877 if (handle == IntPtr.Zero) {
3879 XWarpPointer (DisplayHandle, IntPtr.Zero, IntPtr.Zero, 0, 0, 0, 0, x, y);
3885 hwnd = Hwnd.ObjectFromHandle (handle);
3887 XWarpPointer (DisplayHandle, IntPtr.Zero, hwnd.client_window, 0, 0, 0, 0, x, y);
3893 internal override void SetFocus (IntPtr handle)
3897 hwnd = Hwnd.ObjectFromHandle (handle);
3899 if (FocusWindow != IntPtr.Zero) {
3900 PostMessage (FocusWindow, Msg.WM_KILLFOCUS, hwnd.client_window, IntPtr.Zero);
3902 PostMessage (hwnd.client_window, Msg.WM_SETFOCUS, FocusWindow, IntPtr.Zero);
3903 FocusWindow = hwnd.client_window;
3905 //XSetInputFocus(DisplayHandle, Hwnd.ObjectFromHandle(handle).client_window, RevertTo.None, IntPtr.Zero);
3908 internal override void SetIcon (IntPtr handle, Icon icon)
3912 hwnd = Hwnd.ObjectFromHandle (handle);
3914 SetIcon (hwnd, icon);
3918 internal override void SetMenu (IntPtr handle, Menu menu)
3922 hwnd = Hwnd.ObjectFromHandle (handle);
3925 // FIXME - do we need to trigger some resize?
3928 internal override void SetModal (IntPtr handle, bool Modal)
3931 ModalWindows.Push (handle);
3933 if (ModalWindows.Contains (handle)) {
3934 ModalWindows.Pop ();
3936 if (ModalWindows.Count > 0) {
3937 Activate ((IntPtr)ModalWindows.Peek ());
3942 internal override IntPtr SetParent (IntPtr handle, IntPtr parent)
3946 hwnd = Hwnd.ObjectFromHandle (handle);
3947 hwnd.parent = Hwnd.ObjectFromHandle (parent);
3951 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());
3953 XReparentWindow (DisplayHandle, hwnd.whole_window, hwnd.parent.client_window, hwnd.x, hwnd.y);
3959 internal override void SetTimer (Timer timer)
3962 TimerList.Add (timer);
3967 internal override bool SetTopmost (IntPtr handle, IntPtr handle_owner, bool enabled)
3972 hwnd = Hwnd.ObjectFromHandle (handle);
3974 if (handle_owner != IntPtr.Zero) {
3975 hwnd_owner = Hwnd.ObjectFromHandle (handle_owner);
3982 if (hwnd_owner != null) {
3983 XSetTransientForHint (DisplayHandle, hwnd.whole_window, hwnd_owner.whole_window);
3985 XSetTransientForHint (DisplayHandle, hwnd.whole_window, FosterParent);
3990 XDeleteProperty (DisplayHandle, hwnd.whole_window, (int)Atom.XA_WM_TRANSIENT_FOR);
3996 internal override bool SetVisible (IntPtr handle, bool visible)
4000 hwnd = Hwnd.ObjectFromHandle (handle);
4001 hwnd.visible = visible;
4005 if (Control.FromHandle (handle) is Form) {
4008 s = ((Form)Control.FromHandle (handle)).WindowState;
4010 XMapWindow (DisplayHandle, hwnd.whole_window);
4011 XMapWindow (DisplayHandle, hwnd.client_window);
4014 case FormWindowState.Minimized: SetWindowState (handle, FormWindowState.Minimized); break;
4015 case FormWindowState.Maximized: SetWindowState (handle, FormWindowState.Maximized); break;
4018 XMapWindow (DisplayHandle, hwnd.whole_window);
4019 XMapWindow (DisplayHandle, hwnd.client_window);
4022 XUnmapWindow (DisplayHandle, hwnd.whole_window);
4028 internal override void SetWindowMinMax (IntPtr handle, Rectangle maximized, Size min, Size max)
4033 hwnd = Hwnd.ObjectFromHandle (handle);
4038 hints = new XSizeHints ();
4040 if (min != Size.Empty) {
4041 hints.flags = (IntPtr)((int)hints.flags | (int)XSizeHintsFlags.PMinSize);
4042 hints.min_width = min.Width;
4043 hints.min_height = min.Height;
4046 if (max != Size.Empty) {
4047 hints.flags = (IntPtr)((int)hints.flags | (int)XSizeHintsFlags.PMaxSize);
4048 hints.max_width = max.Width;
4049 hints.max_height = max.Height;
4052 XSetWMNormalHints (DisplayHandle, hwnd.whole_window, ref hints);
4054 if (maximized != Rectangle.Empty) {
4055 hints.flags = (IntPtr)XSizeHintsFlags.PPosition;
4056 hints.x = maximized.X;
4057 hints.y = maximized.Y;
4058 hints.width = maximized.Width;
4059 hints.height = maximized.Height;
4061 // Metacity does not seem to follow this constraint for maximized (zoomed) windows
4062 XSetZoomHints (DisplayHandle, hwnd.whole_window, ref hints);
4067 internal override void SetWindowPos (IntPtr handle, int x, int y, int width, int height)
4070 Rectangle client_rect;
4072 hwnd = Hwnd.ObjectFromHandle (handle);
4074 // X requires a sanity check for width & height; otherwise it dies
4075 if (hwnd.zero_sized && width > 0 && height > 0) {
4077 XMapWindow (DisplayHandle, hwnd.whole_window);
4079 hwnd.zero_sized = false;
4083 hwnd.zero_sized = true;
4084 XUnmapWindow (DisplayHandle, hwnd.whole_window);
4088 hwnd.zero_sized = true;
4089 XUnmapWindow (DisplayHandle, hwnd.whole_window);
4092 client_rect = Hwnd.GetClientRectangle (hwnd.border_style, hwnd.menu, hwnd.title_style, hwnd.caption_height, hwnd.tool_caption_height, width, height);
4094 // Save a server roundtrip (and prevent a feedback loop)
4095 if ((hwnd.x == x) && (hwnd.y == y) &&
4096 (hwnd.width == width) && (hwnd.height == height) &&
4097 (hwnd.ClientRect == client_rect)) {
4101 if (!hwnd.zero_sized) {
4103 XMoveResizeWindow (DisplayHandle, hwnd.whole_window, x, y, width, height);
4104 XMoveResizeWindow (DisplayHandle, hwnd.client_window, client_rect.X, client_rect.Y, client_rect.Width, client_rect.Height);
4108 // Prevent an old queued ConfigureNotify from setting our width with outdated data, set it now
4110 hwnd.height = height;
4113 internal override void SetWindowState (IntPtr handle, FormWindowState state)
4115 FormWindowState current_state;
4118 hwnd = Hwnd.ObjectFromHandle (handle);
4120 current_state = GetWindowState (handle);
4122 if (current_state == state) {
4127 case FormWindowState.Normal: {
4129 if (current_state == FormWindowState.Minimized) {
4130 XMapWindow (DisplayHandle, hwnd.whole_window);
4131 XMapWindow (DisplayHandle, hwnd.client_window);
4132 } else if (current_state == FormWindowState.Maximized) {
4133 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]);
4140 case FormWindowState.Minimized: {
4142 if (current_state == FormWindowState.Maximized) {
4143 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]);
4145 XIconifyWindow (DisplayHandle, hwnd.whole_window, ScreenNo);
4150 case FormWindowState.Maximized: {
4152 if (current_state == FormWindowState.Minimized) {
4153 XMapWindow (DisplayHandle, hwnd.whole_window);
4154 XMapWindow (DisplayHandle, hwnd.client_window);
4157 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]);
4165 internal override void SetWindowStyle (IntPtr handle, CreateParams cp)
4169 hwnd = Hwnd.ObjectFromHandle (handle);
4170 SetHwndStyles (hwnd, cp);
4171 SetWMStyles (hwnd, cp);
4174 internal override void SetWindowTransparency (IntPtr handle, double transparency, Color key)
4179 hwnd = Hwnd.ObjectFromHandle (handle);
4185 hwnd.opacity = (uint)(0xffffffff * transparency);
4186 opacity = hwnd.opacity;
4188 if (hwnd.reparented) {
4189 XChangeProperty (DisplayHandle, XGetParent (hwnd.whole_window), NetAtoms [(int)NA._NET_WM_WINDOW_OPACITY], Atom.XA_CARDINAL, 32, PropertyMode.Replace, ref opacity, 1);
4193 internal override bool SetZOrder (IntPtr handle, IntPtr after_handle, bool top, bool bottom)
4195 Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
4199 XRaiseWindow (DisplayHandle, hwnd.whole_window);
4202 } else if (!bottom) {
4203 Hwnd after_hwnd = null;
4205 if (after_handle != IntPtr.Zero) {
4206 after_hwnd = Hwnd.ObjectFromHandle (after_handle);
4209 XWindowChanges values = new XWindowChanges ();
4211 if (after_hwnd == null) {
4212 throw new ArgumentNullException ("after_handle", "Need sibling to adjust z-order");
4214 values.sibling = after_hwnd.whole_window;
4215 values.stack_mode = StackMode.Below;
4218 XConfigureWindow (DisplayHandle, hwnd.whole_window, ChangeWindowFlags.CWStackMode | ChangeWindowFlags.CWSibling, ref values);
4223 XLowerWindow (DisplayHandle, hwnd.whole_window);
4230 internal override void ShowCursor (bool show)
4232 ; // FIXME - X11 doesn't 'hide' the cursor. we could create an empty cursor
4235 internal override void StartLoop (Thread thread)
4237 // Future place for prepping a new queue for this specific thread
4240 internal override bool SupportsTransparency ()
4242 // We need to check if the x compositing manager is running
4246 internal override bool SystrayAdd (IntPtr handle, string tip, Icon icon, out ToolTip tt)
4248 GetSystrayManagerWindow ();
4250 if (SystrayMgrWindow != IntPtr.Zero) {
4252 XSizeHints size_hints;
4255 hwnd = Hwnd.ObjectFromHandle (handle);
4257 Console.WriteLine("Adding Systray Whole:{0:X}, Client:{1:X}", hwnd.whole_window.ToInt32(), hwnd.client_window.ToInt32());
4260 XUnmapWindow (DisplayHandle, hwnd.whole_window);
4261 XUnmapWindow (DisplayHandle, hwnd.client_window);
4264 gdk_window_destroy (gdk_window_lookup (hwnd.client_window));
4265 hwnd.client_window = hwnd.whole_window;
4267 size_hints = new XSizeHints ();
4269 size_hints.flags = (IntPtr)(XSizeHintsFlags.PMinSize | XSizeHintsFlags.PMaxSize | XSizeHintsFlags.PBaseSize);
4270 size_hints.min_width = icon.Width;
4271 size_hints.min_height = icon.Height;
4273 size_hints.max_width = icon.Width;
4274 size_hints.max_height = icon.Height;
4276 size_hints.base_width = icon.Width;
4277 size_hints.base_height = icon.Height;
4278 XSetWMNormalHints (DisplayHandle, hwnd.whole_window, ref size_hints);
4280 atoms = new uint [2];
4281 atoms [0] = 1; // Version 1
4282 atoms [1] = 0; // We're not mapped
4284 // This line cost me 3 days...
4285 XChangeProperty (DisplayHandle, hwnd.whole_window, NetAtoms [(int)NA._XEMBED_INFO], NetAtoms [(int)NA._XEMBED_INFO], 32, PropertyMode.Replace, atoms, 2);
4287 // Need to pick some reasonable defaults
4288 tt = new ToolTip ();
4289 tt.AutomaticDelay = 100;
4290 tt.InitialDelay = 250;
4291 tt.ReshowDelay = 250;
4292 tt.ShowAlways = true;
4294 if ((tip != null) && (tip != string.Empty)) {
4295 tt.SetToolTip (Control.FromHandle (handle), tip);
4301 // Make sure the window exists
4302 XSync (DisplayHandle, hwnd.whole_window);
4304 SendNetClientMessage (SystrayMgrWindow, (IntPtr)NetAtoms [(int)NA._NET_SYSTEM_TRAY_OPCODE], IntPtr.Zero, (IntPtr)SystrayRequest.SYSTEM_TRAY_REQUEST_DOCK, hwnd.whole_window);
4312 internal override bool SystrayChange (IntPtr handle, string tip, Icon icon, ref ToolTip tt)
4316 control = Control.FromHandle (handle);
4317 if (control != null && tt != null) {
4318 tt.SetToolTip (control, tip);
4326 internal override void SystrayRemove (IntPtr handle, ref ToolTip tt)
4330 hwnd = Hwnd.ObjectFromHandle (handle);
4332 XUnmapWindow (DisplayHandle, hwnd.whole_window);
4333 SetParent (hwnd.whole_window, FosterParent);
4335 // The caller can now re-dock it later...
4342 internal override bool Text (IntPtr handle, string text)
4345 gdk_window_set_title (gdk_window_lookup (Hwnd.ObjectFromHandle (handle).whole_window), text);
4350 internal override bool TranslateMessage (ref MSG msg)
4352 return Keyboard.TranslateMessage (ref msg);
4355 internal override void UpdateWindow (IntPtr handle)
4360 hwnd = Hwnd.ObjectFromHandle (handle);
4362 if (!hwnd.visible || hwnd.expose_pending) {
4367 SendMessage(handle, Msg.WM_PAINT, IntPtr.Zero, IntPtr.Zero);
4369 xevent = new XEvent ();
4370 xevent.type = XEventName.Expose;
4371 xevent.ExposeEvent.display = DisplayHandle;
4372 xevent.ExposeEvent.window = hwnd.client_window;
4374 MessageQueue.Enqueue (xevent);
4375 hwnd.expose_pending = true;
4379 internal static IntPtr NewPixmap (IntPtr gdk_window, int width, int height)
4381 return gdk_pixmap_new (gdk_window, width, height, 24); // FIXME: instead of 24, get the correct display depth
4384 internal static void BlitOffscreenPixmap (IntPtr gdk_pixmap, IntPtr dest_drawable, Rectangle area)
4386 IntPtr gdk_gc = gdk_gc_new (gdk_pixmap);
4388 gdk_draw_drawable (dest_drawable, gdk_gc, gdk_pixmap, area.X, area.Y, area.X, area.Y, area.Width, area.Height);
4390 g_object_unref (gdk_gc);
4392 #endregion // Public Static Methods
4395 internal override event EventHandler Idle;
4396 #endregion // Events
4399 [DllImport ("libX11", EntryPoint="XSynchronize")]
4400 internal extern static IntPtr XSynchronize (IntPtr display, bool onoff);
4402 [DllImport ("libX11", EntryPoint="XCreateWindow")]
4403 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);
4404 [DllImport ("libX11", EntryPoint="XCreateSimpleWindow")]
4405 internal extern static IntPtr XCreateSimpleWindow (IntPtr display, IntPtr parent, int x, int y, int width, int height, int border_width, int border, int background);
4406 [DllImport ("libX11", EntryPoint="XMapWindow")]
4407 internal extern static int XMapWindow (IntPtr display, IntPtr window);
4408 [DllImport ("libX11", EntryPoint="XUnmapWindow")]
4409 internal extern static int XUnmapWindow (IntPtr display, IntPtr window);
4410 [DllImport ("libX11", EntryPoint="XMapSubwindows")]
4411 internal extern static int XMapSubindows (IntPtr display, IntPtr window);
4412 [DllImport ("libX11", EntryPoint="XUnmapSubwindows")]
4413 internal extern static int XUnmapSubwindows (IntPtr display, IntPtr window);
4414 [DllImport ("libX11", EntryPoint="XRootWindow")]
4415 internal extern static IntPtr XRootWindow (IntPtr display, int screen_number);
4416 [DllImport ("libX11", EntryPoint="XNextEvent")]
4417 internal extern static IntPtr XNextEvent (IntPtr display, ref XEvent xevent);
4418 [DllImport ("libX11")]
4419 internal extern static int XConnectionNumber (IntPtr diplay);
4420 [DllImport ("libX11")]
4421 internal extern static int XPending (IntPtr diplay);
4422 [DllImport ("libX11")]
4423 internal extern static bool XCheckWindowEvent (IntPtr display, IntPtr window, EventMask mask, ref XEvent xevent);
4424 [DllImport ("libX11")]
4425 internal extern static bool XCheckMaskEvent (IntPtr display, EventMask mask, ref XEvent xevent);
4426 [DllImport ("libX11", EntryPoint="XSelectInput")]
4427 internal extern static IntPtr XSelectInput (IntPtr display, IntPtr window, EventMask mask);
4429 [DllImport ("libX11", EntryPoint="XReparentWindow")]
4430 internal extern static int XReparentWindow (IntPtr display, IntPtr window, IntPtr parent, int x, int y);
4431 [DllImport ("libX11", EntryPoint="XMoveResizeWindow")]
4432 internal extern static int XMoveResizeWindow (IntPtr display, IntPtr window, int x, int y, int width, int height);
4434 [DllImport ("libX11", EntryPoint="XResizeWindow")]
4435 internal extern static int XResizeWindow (IntPtr display, IntPtr window, int width, int height);
4437 [DllImport ("libX11", EntryPoint="XGetWindowAttributes")]
4438 internal extern static int XGetWindowAttributes (IntPtr display, IntPtr window, ref XWindowAttributes attributes);
4440 [DllImport ("libX11", EntryPoint="XFlush")]
4441 internal extern static int XFlush (IntPtr display);
4443 [DllImport ("libX11", EntryPoint="XSetWMName")]
4444 internal extern static int XSetWMName (IntPtr display, IntPtr window, ref XTextProperty text_prop);
4446 [DllImport ("libX11", EntryPoint="XStoreName")]
4447 internal extern static int XStoreName (IntPtr display, IntPtr window, string window_name);
4449 [DllImport ("libX11", EntryPoint="XFetchName")]
4450 internal extern static int XFetchName (IntPtr display, IntPtr window, ref IntPtr window_name);
4452 [DllImport ("libX11", EntryPoint="XSendEvent")]
4453 internal extern static int XSendEvent (IntPtr display, IntPtr window, bool propagate, EventMask event_mask, ref XEvent send_event);
4455 [DllImport ("libX11", EntryPoint="XQueryTree")]
4456 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);
4458 [DllImport ("libX11", EntryPoint="XFree")]
4459 internal extern static int XFree (IntPtr data);
4461 [DllImport ("libX11", EntryPoint="XRaiseWindow")]
4462 internal extern static int XRaiseWindow (IntPtr display, IntPtr window);
4464 [DllImport ("libX11", EntryPoint="XLowerWindow")]
4465 internal extern static uint XLowerWindow (IntPtr display, IntPtr window);
4467 [DllImport ("libX11", EntryPoint="XConfigureWindow")]
4468 internal extern static uint XConfigureWindow (IntPtr display, IntPtr window, ChangeWindowFlags value_mask, ref XWindowChanges values);
4470 [DllImport ("libX11", EntryPoint="XInternAtom")]
4471 internal extern static int XInternAtom (IntPtr display, string atom_name, bool only_if_exists);
4473 [DllImport ("libX11", EntryPoint="XSetWMProtocols")]
4474 internal extern static int XSetWMProtocols (IntPtr display, IntPtr window, uint[] protocols, int count);
4476 [DllImport ("libX11", EntryPoint="XGrabPointer")]
4477 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);
4479 [DllImport ("libX11", EntryPoint="XUngrabPointer")]
4480 internal extern static int XUngrabPointer (IntPtr display, uint timestamp);
4482 [DllImport ("libX11", EntryPoint="XQueryPointer")]
4483 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);
4485 [DllImport ("libX11", EntryPoint="XTranslateCoordinates")]
4486 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);
4488 [DllImport ("libX11", EntryPoint="XWarpPointer")]
4489 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);
4491 [DllImport ("libX11", EntryPoint="XClearWindow")]
4492 internal extern static int XClearWindow (IntPtr display, IntPtr window);
4494 [DllImport ("libX11", EntryPoint="XClearArea")]
4495 internal extern static int XClearArea (IntPtr display, IntPtr window, int x, int y, int width, int height, bool exposures);
4498 [DllImport ("libX11", EntryPoint="XDefaultScreenOfDisplay")]
4499 internal extern static IntPtr XDefaultScreenOfDisplay (IntPtr display);
4501 [DllImport ("libX11", EntryPoint="XScreenNumberOfScreen")]
4502 internal extern static int XScreenNumberOfScreen (IntPtr display, IntPtr Screen);
4504 [DllImport ("libX11", EntryPoint="XDefaultVisual")]
4505 internal extern static IntPtr XDefaultVisual (IntPtr display, int screen_number);
4507 [DllImport ("libX11", EntryPoint="XDefaultDepth")]
4508 internal extern static uint XDefaultDepth (IntPtr display, int screen_number);
4510 [DllImport ("libX11", EntryPoint="XDefaultColormap")]
4511 internal extern static IntPtr XDefaultColormap (IntPtr display, int screen_number);
4513 [DllImport ("libX11", EntryPoint="XLookupColor")]
4514 internal extern static int XLookupColor (IntPtr display, IntPtr Colormap, string Coloranem, ref XColor exact_def_color, ref XColor screen_def_color);
4516 [DllImport ("libX11", EntryPoint="XAllocColor")]
4517 internal extern static int XAllocColor (IntPtr display, IntPtr Colormap, ref XColor colorcell_def);
4519 [DllImport ("libX11", EntryPoint="XSetTransientForHint")]
4520 internal extern static int XSetTransientForHint (IntPtr display, IntPtr window, IntPtr prop_window);
4522 [DllImport ("libX11", EntryPoint="XChangeProperty")]
4523 internal extern static int XChangeProperty (IntPtr display, IntPtr window, int property, int type, int format, PropertyMode mode, ref MotifWmHints data, int nelements);
4525 [DllImport ("libX11", EntryPoint="XChangeProperty")]
4526 internal extern static int XChangeProperty (IntPtr display, IntPtr window, int property, Atom format, int type, PropertyMode mode, uint[] atoms, int nelements);
4528 [DllImport ("libX11", EntryPoint="XChangeProperty")]
4529 internal extern static int XChangeProperty (IntPtr display, IntPtr window, int property, Atom format, int type, PropertyMode mode, ref uint value, int nelements);
4531 [DllImport ("libX11", EntryPoint="XChangeProperty")]
4532 internal extern static int XChangeProperty (IntPtr display, IntPtr window, int property, int format, int type, PropertyMode mode, uint[] atoms, int nelements);
4534 [DllImport ("libX11", EntryPoint="XChangeProperty")]
4535 internal extern static int XChangeProperty (IntPtr display, IntPtr window, int property, int format, int type, PropertyMode mode, IntPtr atoms, int nelements);
4537 [DllImport ("libX11", EntryPoint="XChangeProperty")]
4538 internal extern static int XChangeProperty (IntPtr display, IntPtr window, int property, Atom format, int type, PropertyMode mode, IntPtr atoms, int nelements);
4540 [DllImport ("libX11", EntryPoint="XChangeProperty", CharSet=CharSet.Ansi)]
4541 internal extern static int XChangeProperty (IntPtr display, IntPtr window, int property, int type, int format, PropertyMode mode, string text, int text_length);
4543 [DllImport ("libX11", EntryPoint="XDeleteProperty")]
4544 internal extern static int XDeleteProperty (IntPtr display, IntPtr window, int property);
4546 [DllImport ("gdiplus", EntryPoint="GetFontMetrics")]
4547 internal extern static bool GetFontMetrics (IntPtr graphicsObject, IntPtr nativeObject, out int ascent, out int descent);
4550 [DllImport ("libX11", EntryPoint="XCreateGC")]
4551 internal extern static IntPtr XCreateGC (IntPtr display, IntPtr window, GCFunction valuemask, ref XGCValues values);
4553 [DllImport ("libX11", EntryPoint="XFreeGC")]
4554 internal extern static int XFreeGC (IntPtr display, IntPtr gc);
4556 [DllImport ("libX11", EntryPoint="XSetFunction")]
4557 internal extern static int XSetFunction (IntPtr display, IntPtr gc, GXFunction function);
4559 [DllImport ("libX11", EntryPoint="XDrawLine")]
4560 internal extern static int XDrawLine (IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int x2, int y2);
4562 [DllImport ("libX11", EntryPoint="XDrawRectangle")]
4563 internal extern static int XDrawRectangle (IntPtr display, IntPtr drawable, IntPtr gc, int x1, int y1, int width, int height);
4565 [DllImport ("libX11", EntryPoint="XSetWindowBackground")]
4566 internal extern static int XSetWindowBackground (IntPtr display, IntPtr window, IntPtr background);
4568 [DllImport ("libX11", EntryPoint="XCopyArea")]
4569 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);
4571 [DllImport ("libX11", EntryPoint="XGetAtomName")]
4572 internal extern static string XGetAtomName (IntPtr display, int atom);
4574 [DllImport ("libX11", EntryPoint="XGetWindowProperty")]
4575 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);
4577 [DllImport ("libX11", EntryPoint="XSetInputFocus")]
4578 internal extern static int XSetInputFocus (IntPtr display, IntPtr window, RevertTo revert_to, IntPtr time);
4580 [DllImport ("libX11", EntryPoint="XIconifyWindow")]
4581 internal extern static int XIconifyWindow (IntPtr display, IntPtr window, int screen_number);
4583 [DllImport ("libX11", EntryPoint="XDefineCursor")]
4584 internal extern static int XDefineCursor (IntPtr display, IntPtr window, IntPtr cursor);
4586 [DllImport ("libX11", EntryPoint="XUndefineCursor")]
4587 internal extern static int XUndefineCursor (IntPtr display, IntPtr window);
4589 [DllImport ("libX11", EntryPoint="XFreeCursor")]
4590 internal extern static int XFreeCursor (IntPtr display, IntPtr cursor);
4592 [DllImport ("libX11", EntryPoint="XCreateFontCursor")]
4593 internal extern static IntPtr XCreateFontCursor (IntPtr display, CursorFontShape shape);
4595 [DllImport ("libX11", EntryPoint="XCreatePixmapCursor")]
4596 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);
4598 [DllImport ("libX11", EntryPoint="XCreatePixmapFromBitmapData")]
4599 internal extern static IntPtr XCreatePixmapFromBitmapData (IntPtr display, IntPtr drawable, byte[] data, int width, int height, IntPtr fg, IntPtr bg, int depth);
4601 [DllImport ("libX11", EntryPoint="XFreePixmap")]
4602 internal extern static IntPtr XFreePixmap (IntPtr display, IntPtr pixmap);
4604 [DllImport ("libX11", EntryPoint="XWhitePixel")]
4605 internal extern static IntPtr XWhitePixel (IntPtr display, int screen_no);
4607 [DllImport ("libX11", EntryPoint="XBlackPixel")]
4608 internal extern static IntPtr XBlackPixel (IntPtr display, int screen_no);
4610 [DllImport ("libX11", EntryPoint="XGrabServer")]
4611 internal extern static void XGrabServer (IntPtr display);
4613 [DllImport ("libX11", EntryPoint="XUngrabServer")]
4614 internal extern static void XUngrabServer (IntPtr display);
4616 [DllImport ("libX11", EntryPoint="XSetWMNormalHints")]
4617 internal extern static void XSetWMNormalHints (IntPtr display, IntPtr window, ref XSizeHints hints);
4619 [DllImport ("libX11", EntryPoint="XSetZoomHints")]
4620 internal extern static void XSetZoomHints (IntPtr display, IntPtr window, ref XSizeHints hints);
4622 [DllImport ("libX11", EntryPoint="XSetWMHints")]
4623 internal extern static void XSetWMHints (IntPtr display, IntPtr window, ref XWMHints wmhints);
4625 [DllImport ("libX11", EntryPoint="XSync")]
4626 internal extern static void XSync (IntPtr display, IntPtr window);
4628 [DllImport ("libX11", EntryPoint="XGetIconSizes")]
4629 internal extern static int XGetIconSizes (IntPtr display, IntPtr window, out IntPtr size_list, out int count);
4631 [DllImport ("libX11", EntryPoint="XSetErrorHandler")]
4632 internal extern static IntPtr XSetErrorHandler (XErrorHandler error_handler);
4634 [DllImport ("libX11", EntryPoint="XGetErrorText")]
4635 internal extern static IntPtr XGetErrorText (IntPtr display, byte code, StringBuilder buffer, int length);
4637 [DllImport ("libX11", EntryPoint="XInitThreads")]
4638 internal extern static int XInitThreads ();
4640 [DllImport ("libX11", EntryPoint="XConvertSelection")]
4641 internal extern static int XConvertSelection (IntPtr display, int selection, int target, int property, IntPtr requestor, IntPtr time);
4643 [DllImport ("libX11", EntryPoint="XGetSelectionOwner")]
4644 internal extern static IntPtr XGetSelectionOwner (IntPtr display, int selection);
4646 [DllImport ("libX11", EntryPoint="XSetSelectionOwner")]
4647 internal extern static int XSetSelectionOwner (IntPtr display, int selection, IntPtr owner, IntPtr time);
4649 [DllImport ("libX11", EntryPoint="XSetPlaneMask")]
4650 internal extern static int XSetPlaneMask (IntPtr display, IntPtr gc, uint mask);
4652 [DllImport ("libX11", EntryPoint="XSetForeground")]
4653 internal extern static int XSetForeground (IntPtr display, IntPtr gc, uint foreground);
4655 [DllImport ("libX11", EntryPoint="XSetBackground")]
4656 internal extern static int XSetBackground (IntPtr display, IntPtr gc, uint background);
4661 [DllImport("libgdk-x11-2.0.so")]
4662 static extern bool gdk_init_check (IntPtr argc, IntPtr argv);
4664 [DllImport("libgdk-x11-2.0.so")]
4665 internal static extern IntPtr gdk_x11_display_get_xdisplay (IntPtr display);
4667 [DllImport("libgdk-x11-2.0.so")]
4668 internal static extern IntPtr gdk_display_get_default ();
4670 [DllImport("libgdk-x11-2.0.so")]
4671 static extern IntPtr gdk_pixmap_new (IntPtr drawable, int width, int height, int depth);
4673 [DllImport("libgdk-x11-2.0.so")]
4674 static extern IntPtr gdk_x11_drawable_get_xid (IntPtr gdkdrawable);
4676 [DllImport("libgdk-x11-2.0.so")]
4677 static extern void gdk_draw_drawable (IntPtr drawable_dest, IntPtr gdk_gc, IntPtr drawable_src, int xsrc, int ysrc, int xdest, int ydest, int width, int height);
4679 [DllImport("libgdk-x11-2.0.so")]
4680 static extern IntPtr gdk_gc_new (IntPtr drawable);
4682 [DllImport("libgdk-x11-2.0.so")]
4683 static extern IntPtr gdk_window_foreign_new (IntPtr anid);
4685 [DllImport("libgdk-x11-2.0.so")]
4686 static extern IntPtr gdk_x11_lookup_xdisplay (IntPtr xdisplay);
4688 [DllImport("libgdk-x11-2.0.so")]
4689 static extern void gdk_display_close (IntPtr display);
4691 [DllImport("libgdk-x11-2.0.so")]
4692 static extern void gdk_display_beep (IntPtr display);
4694 [DllImport("libgdk-x11-2.0.so")]
4695 static extern void gdk_display_sync (IntPtr display);
4697 [DllImport("libgdk-x11-2.0.so")]
4698 static extern IntPtr gdk_get_default_root_window ();
4700 [DllImport("libgdk-x11-2.0.so")]
4701 static extern IntPtr gdk_colormap_get_system ();
4703 [DllImport("libgdk-x11-2.0.so")]
4704 static extern IntPtr gdk_x11_colormap_get_xcolormap (IntPtr gdk_colormap);
4706 [DllImport("libgdk-x11-2.0.so")]
4707 static extern void gdk_window_destroy (IntPtr gdk_window);
4709 [DllImport("libgdk-x11-2.0.so")]
4710 static extern void gdk_x11_grab_server ();
4712 [DllImport("libgdk-x11-2.0.so")]
4713 static extern void gdk_x11_ungrab_server ();
4715 [DllImport("libgdk-x11-2.0.so")]
4716 static extern void gdk_display_flush (IntPtr gdk_display);
4718 [DllImport("libgdk-x11-2.0.so")]
4719 static extern void gdk_window_iconify (IntPtr gdk_window);
4721 [DllImport("libgdk-x11-2.0.so")]
4722 static extern void gdk_window_deiconify (IntPtr gdk_window);
4724 [DllImport("libgdk-x11-2.0.so")]
4725 static extern void gdk_window_set_decorations (IntPtr gdk_window, int decorations);
4727 [DllImport("libgdk-x11-2.0.so")]
4728 static extern IntPtr gdk_screen_get_default ();
4730 [DllImport("libgdk-x11-2.0.so")]
4731 static extern int gdk_screen_get_number (IntPtr gdk_screen);
4733 [DllImport("libgdk-x11-2.0.so")]
4734 static extern IntPtr gdk_window_lookup (IntPtr anid);
4736 [DllImport("libgdk-x11-2.0.so")]
4737 static extern IntPtr gdk_window_new (IntPtr gdk_parent, ref GdkWindowAttr gdk_window_attributes, int attributes_mask);
4739 [DllImport("libgdk-x11-2.0.so")]
4740 static extern void gdk_window_set_events (IntPtr gdk_window, int event_mask);
4742 [DllImport("libgdk-x11-2.0.so")]
4743 static extern void gdk_window_show (IntPtr window);
4745 [DllImport("libgdk-x11-2.0.so")]
4746 static extern void gdk_window_set_title (IntPtr gdk_window, string title);
4748 [DllImport("libgdk-x11-2.0.so")]
4749 static extern int gdk_window_get_origin (IntPtr gdk_window, out int x, out int y);
4751 [DllImport("libgdk-x11-2.0.so")]
4752 static extern void gdk_window_get_geometry (IntPtr gdk_window, out int x, out int y, out int width, out int height, out int depth);
4754 [DllImport("libgdk-x11-2.0.so")]
4755 static extern void gdk_property_change (IntPtr gdk_window, /*GdkAtom*/IntPtr property, /*GdkAtom*/IntPtr type, int format, int gdk_prop_mode, /*const guchar **/ IntPtr data, int nelements);
4757 [DllImport("libgdk-x11-2.0.so")]
4758 static extern IntPtr gdk_window_get_parent (IntPtr gdk_window);
4760 [DllImport("libgdk-x11-2.0.so")]
4761 static extern void gdk_display_get_maximal_cursor_size (IntPtr gdk_display, out uint width, out uint height);
4763 [DllImport("libgdk-x11-2.0.so")]
4764 static extern int gdk_visual_get_best_depth ();
4767 #region gobject imports
4768 [DllImport("libglib-2.0.so")]
4769 static extern void g_free (IntPtr mem);
4771 [DllImport("libgobject-2.0.so")]
4772 static extern void g_object_unref (IntPtr nativeObject);