1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2002,2005 Hans Breuer
3 * Copyright (C) 2003 Tor Lillqvist
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 #include "gdkprivate-win32.h"
22 #include "gdkdisplayprivate.h"
23 #include "gdkwin32display.h"
24 #include "gdkwin32screen.h"
25 #include "gdkwin32window.h"
28 #define HAVE_MONITOR_INFO
30 #if defined(_MSC_VER) && (WINVER < 0x500) && (WINVER > 0x0400)
32 #elif defined(_MSC_VER) && (WINVER <= 0x0400)
33 #undef HAVE_MONITOR_INFO
37 _gdk_windowing_set_default_display (GdkDisplay *display)
39 g_assert (display == NULL || _gdk_display == display);
43 gdk_win32_display_get_next_serial (GdkDisplay *display)
48 #ifdef HAVE_MONITOR_INFO
50 count_monitor (HMONITOR hmonitor,
55 gint *n = (gint *) data;
63 enum_monitor (HMONITOR hmonitor,
68 /* The struct MONITORINFOEX definition is for some reason different
69 * in the winuser.h bundled with mingw64 from that in MSDN and the
70 * official 32-bit mingw (the MONITORINFO part is in a separate "mi"
71 * member). So to keep this easily compileable with either, repeat
72 * the MSDN definition it here.
74 typedef struct tagMONITORINFOEXA2 {
79 CHAR szDevice[CCHDEVICENAME];
82 MONITORINFOEXA2 monitor_info;
85 gint *index = (gint *) data;
86 GdkWin32Monitor *monitor;
88 g_assert (*index < _gdk_num_monitors);
90 monitor = _gdk_monitors + *index;
92 monitor_info.cbSize = sizeof (MONITORINFOEX);
93 GetMonitorInfoA (hmonitor, (MONITORINFO *) &monitor_info);
95 #ifndef MONITORINFOF_PRIMARY
96 #define MONITORINFOF_PRIMARY 1
99 monitor->name = g_strdup (monitor_info.szDevice);
100 hDC = CreateDCA ("DISPLAY", monitor_info.szDevice, NULL, NULL);
101 monitor->width_mm = GetDeviceCaps (hDC, HORZSIZE);
102 monitor->height_mm = GetDeviceCaps (hDC, VERTSIZE);
104 monitor->rect.x = monitor_info.rcMonitor.left;
105 monitor->rect.y = monitor_info.rcMonitor.top;
106 monitor->rect.width = monitor_info.rcMonitor.right - monitor_info.rcMonitor.left;
107 monitor->rect.height = monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top;
109 if (monitor_info.dwFlags & MONITORINFOF_PRIMARY &&
112 /* Put primary monitor at index 0, just in case somebody needs
113 * to know which one is the primary.
115 GdkWin32Monitor temp = *monitor;
116 *monitor = _gdk_monitors[0];
117 _gdk_monitors[0] = temp;
124 #endif /* HAVE_MONITOR_INFO */
127 _gdk_monitor_init (void)
129 #ifdef HAVE_MONITOR_INFO
132 _gdk_num_monitors = 0;
134 EnumDisplayMonitors (NULL, NULL, count_monitor, (LPARAM) &_gdk_num_monitors);
136 _gdk_monitors = g_renew (GdkWin32Monitor, _gdk_monitors, _gdk_num_monitors);
139 EnumDisplayMonitors (NULL, NULL, enum_monitor, (LPARAM) &index);
141 _gdk_offset_x = G_MININT;
142 _gdk_offset_y = G_MININT;
144 /* Calculate offset */
145 for (i = 0; i < _gdk_num_monitors; i++)
147 _gdk_offset_x = MAX (_gdk_offset_x, -_gdk_monitors[i].rect.x);
148 _gdk_offset_y = MAX (_gdk_offset_y, -_gdk_monitors[i].rect.y);
150 GDK_NOTE (MISC, g_print ("Multi-monitor offset: (%d,%d)\n",
151 _gdk_offset_x, _gdk_offset_y));
153 /* Translate monitor coords into GDK coordinate space */
154 for (i = 0; i < _gdk_num_monitors; i++)
156 _gdk_monitors[i].rect.x += _gdk_offset_x;
157 _gdk_monitors[i].rect.y += _gdk_offset_y;
158 GDK_NOTE (MISC, g_print ("Monitor %d: %dx%d@%+d%+d\n",
159 i, _gdk_monitors[i].rect.width,
160 _gdk_monitors[i].rect.height,
161 _gdk_monitors[i].rect.x,
162 _gdk_monitors[i].rect.y));
167 _gdk_num_monitors = 1;
168 _gdk_monitors = g_renew (GdkWin32Monitor, _gdk_monitors, 1);
170 _gdk_monitors[0].name = g_strdup ("DISPLAY");
172 _gdk_monitors[0].width_mm = GetDeviceCaps (hDC, HORZSIZE);
173 _gdk_monitors[0].height_mm = GetDeviceCaps (hDC, VERTSIZE);
174 ReleaseDC (NULL, hDC);
175 _gdk_monitors[0].rect.x = 0;
176 _gdk_monitors[0].rect.y = 0;
177 _gdk_monitors[0].rect.width = GetSystemMetrics (SM_CXSCREEN);
178 _gdk_monitors[0].rect.height = GetSystemMetrics (SM_CYSCREEN);
185 _gdk_win32_display_open (const gchar *display_name)
187 GDK_NOTE (MISC, g_print ("gdk_display_open: %s\n", (display_name ? display_name : "NULL")));
189 if (display_name == NULL ||
190 g_ascii_strcasecmp (display_name,
191 gdk_display_get_name (_gdk_display)) == 0)
193 if (_gdk_display != NULL)
195 GDK_NOTE (MISC, g_print ("... return _gdk_display\n"));
201 GDK_NOTE (MISC, g_print ("... return NULL\n"));
205 _gdk_display = g_object_new (GDK_TYPE_WIN32_DISPLAY, NULL);
206 _gdk_screen = g_object_new (GDK_TYPE_WIN32_SCREEN, NULL);
208 _gdk_monitor_init ();
210 _gdk_windowing_window_init (_gdk_screen);
212 _gdk_input_init (_gdk_display);
215 /* Precalculate display name */
216 (void) gdk_display_get_name (_gdk_display);
218 g_signal_emit_by_name (gdk_display_manager_get (),
219 "display_opened", _gdk_display);
221 GDK_NOTE (MISC, g_print ("... _gdk_display now set up\n"));
226 struct _GdkWin32Display
231 struct _GdkWin32DisplayClass
233 GdkDisplayClass display_class;
236 G_DEFINE_TYPE (GdkWin32Display, gdk_win32_display, GDK_TYPE_DISPLAY)
239 gdk_win32_display_get_name (GdkDisplay *display)
241 HDESK hdesk = GetThreadDesktop (GetCurrentThreadId ());
244 HWINSTA hwinsta = GetProcessWindowStation ();
245 char *window_station_name;
249 static const char *display_name_cache = NULL;
250 typedef BOOL (WINAPI *PFN_ProcessIdToSessionId) (DWORD, DWORD *);
251 PFN_ProcessIdToSessionId processIdToSessionId;
253 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
255 if (display_name_cache != NULL)
256 return display_name_cache;
259 GetUserObjectInformation (hdesk, UOI_NAME, &dummy, 0, &n);
261 desktop_name = "Default";
265 desktop_name = g_alloca (n + 1);
266 memset (desktop_name, 0, n + 1);
268 if (!GetUserObjectInformation (hdesk, UOI_NAME, desktop_name, n, &n))
269 desktop_name = "Default";
273 GetUserObjectInformation (hwinsta, UOI_NAME, &dummy, 0, &n);
275 window_station_name = "WinSta0";
279 window_station_name = g_alloca (n + 1);
280 memset (window_station_name, 0, n + 1);
282 if (!GetUserObjectInformation (hwinsta, UOI_NAME, window_station_name, n, &n))
283 window_station_name = "WinSta0";
286 processIdToSessionId = (PFN_ProcessIdToSessionId) GetProcAddress (GetModuleHandle ("kernel32.dll"), "ProcessIdToSessionId");
287 if (!processIdToSessionId || !processIdToSessionId (GetCurrentProcessId (), &session_id))
290 display_name = g_strdup_printf ("%ld\\%s\\%s",
295 GDK_NOTE (MISC, g_print ("gdk_win32_display_get_name: %s\n", display_name));
297 display_name_cache = display_name;
299 return display_name_cache;
303 gdk_win32_display_get_n_screens (GdkDisplay *display)
305 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
311 gdk_win32_display_get_screen (GdkDisplay *display,
314 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
315 g_return_val_if_fail (screen_num == 0, NULL);
321 gdk_win32_display_get_default_screen (GdkDisplay *display)
323 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
329 gdk_win32_display_get_default_group (GdkDisplay *display)
331 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
333 g_warning ("gdk_display_get_default_group not yet implemented");
339 gdk_win32_display_supports_selection_notification (GdkDisplay *display)
341 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
346 static HWND _hwnd_next_viewer = NULL;
347 static int debug_indent = 0;
350 * maybe this should be integrated with the default message loop - or maybe not ;-)
352 static LRESULT CALLBACK
353 inner_clipboard_window_procedure (HWND hwnd,
360 case WM_DESTROY: /* remove us from chain */
362 ChangeClipboardChain (hwnd, _hwnd_next_viewer);
366 case WM_CHANGECBCHAIN:
368 HWND hwndRemove = (HWND) wparam; /* handle of window being removed */
369 HWND hwndNext = (HWND) lparam; /* handle of next window in chain */
371 if (hwndRemove == _hwnd_next_viewer)
372 _hwnd_next_viewer = hwndNext == hwnd ? NULL : hwndNext;
373 else if (_hwnd_next_viewer != NULL)
374 return SendMessage (_hwnd_next_viewer, message, wparam, lparam);
378 #ifdef WM_CLIPBOARDUPDATE
379 case WM_CLIPBOARDUPDATE:
381 case WM_DRAWCLIPBOARD:
385 #ifdef G_ENABLE_DEBUG
391 success = OpenClipboard (hwnd);
392 g_return_val_if_fail (success, 0);
393 hwndOwner = GetClipboardOwner ();
394 owner = gdk_win32_window_lookup_for_display (_gdk_display, hwndOwner);
396 owner = gdk_win32_window_foreign_new_for_display (_gdk_display, hwndOwner);
398 GDK_NOTE (DND, g_print (" drawclipboard owner: %p", hwndOwner));
400 #ifdef G_ENABLE_DEBUG
401 if (_gdk_debug_flags & GDK_DEBUG_DND)
403 while ((nFormat = EnumClipboardFormats (nFormat)) != 0)
404 g_print ("%s ", _gdk_win32_cf_to_string (nFormat));
408 GDK_NOTE (DND, g_print (" \n"));
411 event = gdk_event_new (GDK_OWNER_CHANGE);
412 event->owner_change.window = _gdk_root;
413 event->owner_change.owner = owner;
414 event->owner_change.reason = GDK_OWNER_CHANGE_NEW_OWNER;
415 event->owner_change.selection = GDK_SELECTION_CLIPBOARD;
416 event->owner_change.time = _gdk_win32_get_next_tick (0);
417 event->owner_change.selection_time = GDK_CURRENT_TIME;
418 _gdk_win32_append_event (event);
422 if (_hwnd_next_viewer != NULL)
423 return SendMessage (_hwnd_next_viewer, message, wparam, lparam);
425 /* clear error to avoid confusing SetClipboardViewer() return */
430 /* Otherwise call DefWindowProcW(). */
431 GDK_NOTE (EVENTS, g_print (" DefWindowProcW"));
432 return DefWindowProc (hwnd, message, wparam, lparam);
436 static LRESULT CALLBACK
437 _clipboard_window_procedure (HWND hwnd,
444 GDK_NOTE (EVENTS, g_print ("%s%*s%s %p",
445 (debug_indent > 0 ? "\n" : ""),
447 _gdk_win32_message_to_string (message), hwnd));
449 retval = inner_clipboard_window_procedure (hwnd, message, wparam, lparam);
452 GDK_NOTE (EVENTS, g_print (" => %I64d%s", (gint64) retval, (debug_indent == 0 ? "\n" : "")));
458 * Creates a hidden window and adds it to the clipboard chain
461 _gdk_win32_register_clipboard_notification (void)
463 WNDCLASS wclass = { 0, };
467 wclass.lpszClassName = "GdkClipboardNotification";
468 wclass.lpfnWndProc = _clipboard_window_procedure;
469 wclass.hInstance = _gdk_app_hmodule;
471 klass = RegisterClass (&wclass);
475 hwnd = CreateWindow (MAKEINTRESOURCE (klass),
477 0, 0, 0, 0, NULL, NULL,
478 _gdk_app_hmodule, NULL);
483 _hwnd_next_viewer = SetClipboardViewer (hwnd);
485 if (_hwnd_next_viewer == NULL && GetLastError() != 0)
488 /* FIXME: http://msdn.microsoft.com/en-us/library/ms649033(v=VS.85).aspx */
489 /* This is only supported by Vista, and not yet by mingw64 */
490 /* if (AddClipboardFormatListener (hwnd) == FALSE) */
496 g_critical ("Failed to install clipboard viewer");
497 UnregisterClass (MAKEINTRESOURCE (klass), _gdk_app_hmodule);
502 gdk_win32_display_request_selection_notification (GdkDisplay *display,
506 static HWND hwndViewer = NULL;
507 gboolean ret = FALSE;
510 g_print ("gdk_display_request_selection_notification (..., %s)",
511 gdk_atom_name (selection)));
513 if (selection == GDK_SELECTION_CLIPBOARD ||
514 selection == GDK_SELECTION_PRIMARY)
518 hwndViewer = _gdk_win32_register_clipboard_notification ();
519 GDK_NOTE (DND, g_print (" registered"));
521 ret = (hwndViewer != NULL);
525 GDK_NOTE (DND, g_print (" unsupported"));
529 GDK_NOTE (DND, g_print (" -> %s\n", ret ? "TRUE" : "FALSE"));
534 gdk_win32_display_supports_clipboard_persistence (GdkDisplay *display)
540 gdk_win32_display_store_clipboard (GdkDisplay *display,
541 GdkWindow *clipboard_window,
543 const GdkAtom *targets,
549 gdk_win32_display_supports_shapes (GdkDisplay *display)
551 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
557 gdk_win32_display_supports_input_shapes (GdkDisplay *display)
559 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
561 /* Not yet implemented. See comment in
562 * gdk_window_input_shape_combine_mask().
569 gdk_win32_display_supports_composite (GdkDisplay *display)
575 gdk_win32_display_beep (GdkDisplay *display)
577 g_return_if_fail (display == gdk_display_get_default());
578 if (!MessageBeep (-1))
583 gdk_win32_display_flush (GdkDisplay * display)
585 g_return_if_fail (display == _gdk_display);
592 gdk_win32_display_sync (GdkDisplay * display)
594 g_return_if_fail (display == _gdk_display);
600 gdk_win32_display_dispose (GObject *object)
605 gdk_win32_display_finalize (GObject *object)
610 gdk_win32_display_init(GdkWin32Display *display)
615 gdk_win32_display_before_process_all_updates (GdkDisplay *display)
620 gdk_win32_display_after_process_all_updates (GdkDisplay *display)
625 gdk_win32_display_notify_startup_complete (GdkDisplay *display,
626 const gchar *startup_id)
631 gdk_win32_display_event_data_copy (GdkDisplay *display,
638 gdk_win32_display_event_data_free (GdkDisplay *display,
644 gdk_win32_display_push_error_trap (GdkDisplay *display)
649 gdk_win32_display_pop_error_trap (GdkDisplay *display,
655 gdk_win32_display_class_init (GdkWin32DisplayClass *klass)
657 GObjectClass *object_class = G_OBJECT_CLASS (klass);
658 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (klass);
660 object_class->dispose = gdk_win32_display_dispose;
661 object_class->finalize = gdk_win32_display_finalize;
663 display_class->window_type = GDK_TYPE_WIN32_WINDOW;
665 display_class->get_name = gdk_win32_display_get_name;
666 display_class->get_n_screens = gdk_win32_display_get_n_screens;
667 display_class->get_screen = gdk_win32_display_get_screen;
668 display_class->get_default_screen = gdk_win32_display_get_default_screen;
669 display_class->beep = gdk_win32_display_beep;
670 display_class->sync = gdk_win32_display_sync;
671 display_class->flush = gdk_win32_display_flush;
672 display_class->has_pending = _gdk_win32_display_has_pending;
673 display_class->queue_events = _gdk_win32_display_queue_events;
674 display_class->get_default_group = gdk_win32_display_get_default_group;
676 display_class->supports_selection_notification = gdk_win32_display_supports_selection_notification;
677 display_class->request_selection_notification = gdk_win32_display_request_selection_notification;
678 display_class->supports_clipboard_persistence = gdk_win32_display_supports_clipboard_persistence;
679 display_class->store_clipboard = gdk_win32_display_store_clipboard;
680 display_class->supports_shapes = gdk_win32_display_supports_shapes;
681 display_class->supports_input_shapes = gdk_win32_display_supports_input_shapes;
682 display_class->supports_composite = gdk_win32_display_supports_composite;
684 display_class->list_devices = _gdk_win32_display_list_devices;
685 //? display_class->get_app_launch_context = _gdk_win32_display_get_app_launch_context;
686 display_class->get_cursor_for_type = _gdk_win32_display_get_cursor_for_type;
687 display_class->get_cursor_for_name = _gdk_win32_display_get_cursor_for_name;
688 display_class->get_cursor_for_pixbuf = _gdk_win32_display_get_cursor_for_pixbuf;
689 display_class->get_default_cursor_size = _gdk_win32_display_get_default_cursor_size;
690 display_class->get_maximal_cursor_size = _gdk_win32_display_get_maximal_cursor_size;
691 display_class->supports_cursor_alpha = _gdk_win32_display_supports_cursor_alpha;
692 display_class->supports_cursor_color = _gdk_win32_display_supports_cursor_color;
694 display_class->before_process_all_updates = gdk_win32_display_before_process_all_updates;
695 display_class->after_process_all_updates = gdk_win32_display_after_process_all_updates;
696 display_class->get_next_serial = gdk_win32_display_get_next_serial;
697 display_class->notify_startup_complete = gdk_win32_display_notify_startup_complete;
698 display_class->event_data_copy = gdk_win32_display_event_data_copy;
699 display_class->event_data_free = gdk_win32_display_event_data_free;
700 display_class->create_window_impl = _gdk_win32_display_create_window_impl;
702 display_class->get_keymap = _gdk_win32_display_get_keymap;
703 display_class->push_error_trap = gdk_win32_display_push_error_trap;
704 display_class->pop_error_trap = gdk_win32_display_pop_error_trap;
705 display_class->get_selection_owner = _gdk_win32_display_get_selection_owner;
706 display_class->set_selection_owner = _gdk_win32_display_set_selection_owner;
707 display_class->send_selection_notify = _gdk_win32_display_send_selection_notify;
708 display_class->get_selection_property = _gdk_win32_display_get_selection_property;
709 display_class->convert_selection = _gdk_win32_display_convert_selection;
710 display_class->text_property_to_utf8_list = _gdk_win32_display_text_property_to_utf8_list;
711 display_class->utf8_to_string_target = _gdk_win32_display_utf8_to_string_target;