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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gdkprivate-win32.h"
24 #include "gdkdisplayprivate.h"
25 #include "gdkwin32display.h"
26 #include "gdkwin32screen.h"
27 #include "gdkwin32window.h"
29 #define HAVE_MONITOR_INFO
31 #if defined(_MSC_VER) && (WINVER < 0x500) && (WINVER > 0x0400)
33 #elif defined(_MSC_VER) && (WINVER <= 0x0400)
34 #undef HAVE_MONITOR_INFO
38 _gdk_windowing_set_default_display (GdkDisplay *display)
40 g_assert (display == NULL || _gdk_display == display);
44 gdk_win32_display_get_next_serial (GdkDisplay *display)
49 #ifdef HAVE_MONITOR_INFO
51 count_monitor (HMONITOR hmonitor,
56 gint *n = (gint *) data;
64 enum_monitor (HMONITOR hmonitor,
69 /* The struct MONITORINFOEX definition is for some reason different
70 * in the winuser.h bundled with mingw64 from that in MSDN and the
71 * official 32-bit mingw (the MONITORINFO part is in a separate "mi"
72 * member). So to keep this easily compileable with either, repeat
73 * the MSDN definition it here.
75 typedef struct tagMONITORINFOEXA2 {
80 CHAR szDevice[CCHDEVICENAME];
83 MONITORINFOEXA2 monitor_info;
86 gint *index = (gint *) data;
87 GdkWin32Monitor *monitor;
89 g_assert (*index < _gdk_num_monitors);
91 monitor = _gdk_monitors + *index;
93 monitor_info.cbSize = sizeof (MONITORINFOEX);
94 GetMonitorInfoA (hmonitor, (MONITORINFO *) &monitor_info);
96 #ifndef MONITORINFOF_PRIMARY
97 #define MONITORINFOF_PRIMARY 1
100 monitor->name = g_strdup (monitor_info.szDevice);
101 hDC = CreateDCA ("DISPLAY", monitor_info.szDevice, NULL, NULL);
102 monitor->width_mm = GetDeviceCaps (hDC, HORZSIZE);
103 monitor->height_mm = GetDeviceCaps (hDC, VERTSIZE);
105 monitor->rect.x = monitor_info.rcMonitor.left;
106 monitor->rect.y = monitor_info.rcMonitor.top;
107 monitor->rect.width = monitor_info.rcMonitor.right - monitor_info.rcMonitor.left;
108 monitor->rect.height = monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top;
110 if (monitor_info.dwFlags & MONITORINFOF_PRIMARY &&
113 /* Put primary monitor at index 0, just in case somebody needs
114 * to know which one is the primary.
116 GdkWin32Monitor temp = *monitor;
117 *monitor = _gdk_monitors[0];
118 _gdk_monitors[0] = temp;
125 #endif /* HAVE_MONITOR_INFO */
128 _gdk_monitor_init (void)
130 #ifdef HAVE_MONITOR_INFO
133 _gdk_num_monitors = 0;
135 EnumDisplayMonitors (NULL, NULL, count_monitor, (LPARAM) &_gdk_num_monitors);
137 _gdk_monitors = g_renew (GdkWin32Monitor, _gdk_monitors, _gdk_num_monitors);
140 EnumDisplayMonitors (NULL, NULL, enum_monitor, (LPARAM) &index);
142 _gdk_offset_x = G_MININT;
143 _gdk_offset_y = G_MININT;
145 /* Calculate offset */
146 for (i = 0; i < _gdk_num_monitors; i++)
148 _gdk_offset_x = MAX (_gdk_offset_x, -_gdk_monitors[i].rect.x);
149 _gdk_offset_y = MAX (_gdk_offset_y, -_gdk_monitors[i].rect.y);
151 GDK_NOTE (MISC, g_print ("Multi-monitor offset: (%d,%d)\n",
152 _gdk_offset_x, _gdk_offset_y));
154 /* Translate monitor coords into GDK coordinate space */
155 for (i = 0; i < _gdk_num_monitors; i++)
157 _gdk_monitors[i].rect.x += _gdk_offset_x;
158 _gdk_monitors[i].rect.y += _gdk_offset_y;
159 GDK_NOTE (MISC, g_print ("Monitor %d: %dx%d@%+d%+d\n",
160 i, _gdk_monitors[i].rect.width,
161 _gdk_monitors[i].rect.height,
162 _gdk_monitors[i].rect.x,
163 _gdk_monitors[i].rect.y));
168 _gdk_num_monitors = 1;
169 _gdk_monitors = g_renew (GdkWin32Monitor, _gdk_monitors, 1);
171 _gdk_monitors[0].name = g_strdup ("DISPLAY");
173 _gdk_monitors[0].width_mm = GetDeviceCaps (hDC, HORZSIZE);
174 _gdk_monitors[0].height_mm = GetDeviceCaps (hDC, VERTSIZE);
175 ReleaseDC (NULL, hDC);
176 _gdk_monitors[0].rect.x = 0;
177 _gdk_monitors[0].rect.y = 0;
178 _gdk_monitors[0].rect.width = GetSystemMetrics (SM_CXSCREEN);
179 _gdk_monitors[0].rect.height = GetSystemMetrics (SM_CYSCREEN);
186 _gdk_win32_display_open (const gchar *display_name)
188 GDK_NOTE (MISC, g_print ("gdk_display_open: %s\n", (display_name ? display_name : "NULL")));
190 if (display_name == NULL ||
191 g_ascii_strcasecmp (display_name,
192 gdk_display_get_name (_gdk_display)) == 0)
194 if (_gdk_display != NULL)
196 GDK_NOTE (MISC, g_print ("... return _gdk_display\n"));
202 GDK_NOTE (MISC, g_print ("... return NULL\n"));
206 _gdk_display = g_object_new (GDK_TYPE_WIN32_DISPLAY, NULL);
207 _gdk_screen = g_object_new (GDK_TYPE_WIN32_SCREEN, NULL);
209 _gdk_monitor_init ();
211 _gdk_windowing_window_init (_gdk_screen);
213 _gdk_input_init (_gdk_display);
216 /* Precalculate display name */
217 (void) gdk_display_get_name (_gdk_display);
219 g_signal_emit_by_name (gdk_display_manager_get (),
220 "display_opened", _gdk_display);
222 GDK_NOTE (MISC, g_print ("... _gdk_display now set up\n"));
227 struct _GdkWin32Display
232 struct _GdkWin32DisplayClass
234 GdkDisplayClass display_class;
237 G_DEFINE_TYPE (GdkWin32Display, gdk_win32_display, GDK_TYPE_DISPLAY)
240 gdk_win32_display_get_name (GdkDisplay *display)
242 HDESK hdesk = GetThreadDesktop (GetCurrentThreadId ());
245 HWINSTA hwinsta = GetProcessWindowStation ();
246 char *window_station_name;
250 static const char *display_name_cache = NULL;
251 typedef BOOL (WINAPI *PFN_ProcessIdToSessionId) (DWORD, DWORD *);
252 PFN_ProcessIdToSessionId processIdToSessionId;
254 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
256 if (display_name_cache != NULL)
257 return display_name_cache;
260 GetUserObjectInformation (hdesk, UOI_NAME, &dummy, 0, &n);
262 desktop_name = "Default";
266 desktop_name = g_alloca (n + 1);
267 memset (desktop_name, 0, n + 1);
269 if (!GetUserObjectInformation (hdesk, UOI_NAME, desktop_name, n, &n))
270 desktop_name = "Default";
274 GetUserObjectInformation (hwinsta, UOI_NAME, &dummy, 0, &n);
276 window_station_name = "WinSta0";
280 window_station_name = g_alloca (n + 1);
281 memset (window_station_name, 0, n + 1);
283 if (!GetUserObjectInformation (hwinsta, UOI_NAME, window_station_name, n, &n))
284 window_station_name = "WinSta0";
287 processIdToSessionId = (PFN_ProcessIdToSessionId) GetProcAddress (GetModuleHandle ("kernel32.dll"), "ProcessIdToSessionId");
288 if (!processIdToSessionId || !processIdToSessionId (GetCurrentProcessId (), &session_id))
291 display_name = g_strdup_printf ("%ld\\%s\\%s",
296 GDK_NOTE (MISC, g_print ("gdk_win32_display_get_name: %s\n", display_name));
298 display_name_cache = display_name;
300 return display_name_cache;
304 gdk_win32_display_get_n_screens (GdkDisplay *display)
306 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
312 gdk_win32_display_get_screen (GdkDisplay *display,
315 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
316 g_return_val_if_fail (screen_num == 0, NULL);
322 gdk_win32_display_get_default_screen (GdkDisplay *display)
324 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
330 gdk_win32_display_get_default_group (GdkDisplay *display)
332 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
334 g_warning ("gdk_display_get_default_group not yet implemented");
340 gdk_win32_display_supports_selection_notification (GdkDisplay *display)
342 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
347 static HWND _hwnd_next_viewer = NULL;
348 static int debug_indent = 0;
351 * maybe this should be integrated with the default message loop - or maybe not ;-)
353 static LRESULT CALLBACK
354 inner_clipboard_window_procedure (HWND hwnd,
361 case WM_DESTROY: /* remove us from chain */
363 ChangeClipboardChain (hwnd, _hwnd_next_viewer);
367 case WM_CHANGECBCHAIN:
369 HWND hwndRemove = (HWND) wparam; /* handle of window being removed */
370 HWND hwndNext = (HWND) lparam; /* handle of next window in chain */
372 if (hwndRemove == _hwnd_next_viewer)
373 _hwnd_next_viewer = hwndNext == hwnd ? NULL : hwndNext;
374 else if (_hwnd_next_viewer != NULL)
375 return SendMessage (_hwnd_next_viewer, message, wparam, lparam);
379 #ifdef WM_CLIPBOARDUPDATE
380 case WM_CLIPBOARDUPDATE:
382 case WM_DRAWCLIPBOARD:
390 success = OpenClipboard (hwnd);
391 g_return_val_if_fail (success, 0);
392 hwndOwner = GetClipboardOwner ();
393 owner = gdk_win32_window_lookup_for_display (_gdk_display, hwndOwner);
395 owner = gdk_win32_window_foreign_new_for_display (_gdk_display, hwndOwner);
397 GDK_NOTE (DND, g_print (" drawclipboard owner: %p", hwndOwner));
399 #ifdef G_ENABLE_DEBUG
400 if (_gdk_debug_flags & GDK_DEBUG_DND)
402 while ((nFormat = EnumClipboardFormats (nFormat)) != 0)
403 g_print ("%s ", _gdk_win32_cf_to_string (nFormat));
407 GDK_NOTE (DND, g_print (" \n"));
410 event = gdk_event_new (GDK_OWNER_CHANGE);
411 event->owner_change.window = _gdk_root;
412 event->owner_change.owner = owner;
413 event->owner_change.reason = GDK_OWNER_CHANGE_NEW_OWNER;
414 event->owner_change.selection = GDK_SELECTION_CLIPBOARD;
415 event->owner_change.time = _gdk_win32_get_next_tick (0);
416 event->owner_change.selection_time = GDK_CURRENT_TIME;
417 _gdk_win32_append_event (event);
421 if (_hwnd_next_viewer != NULL)
422 return SendMessage (_hwnd_next_viewer, message, wparam, lparam);
424 /* clear error to avoid confusing SetClipboardViewer() return */
429 /* Otherwise call DefWindowProcW(). */
430 GDK_NOTE (EVENTS, g_print (" DefWindowProcW"));
431 return DefWindowProc (hwnd, message, wparam, lparam);
435 static LRESULT CALLBACK
436 _clipboard_window_procedure (HWND hwnd,
443 GDK_NOTE (EVENTS, g_print ("%s%*s%s %p",
444 (debug_indent > 0 ? "\n" : ""),
446 _gdk_win32_message_to_string (message), hwnd));
448 retval = inner_clipboard_window_procedure (hwnd, message, wparam, lparam);
451 GDK_NOTE (EVENTS, g_print (" => %I64d%s", (gint64) retval, (debug_indent == 0 ? "\n" : "")));
457 * Creates a hidden window and adds it to the clipboard chain
460 _gdk_win32_register_clipboard_notification (void)
462 WNDCLASS wclass = { 0, };
466 wclass.lpszClassName = "GdkClipboardNotification";
467 wclass.lpfnWndProc = _clipboard_window_procedure;
468 wclass.hInstance = _gdk_app_hmodule;
470 klass = RegisterClass (&wclass);
474 hwnd = CreateWindow (MAKEINTRESOURCE (klass),
476 0, 0, 0, 0, NULL, NULL,
477 _gdk_app_hmodule, NULL);
482 _hwnd_next_viewer = SetClipboardViewer (hwnd);
484 if (_hwnd_next_viewer == NULL && GetLastError() != 0)
487 /* FIXME: http://msdn.microsoft.com/en-us/library/ms649033(v=VS.85).aspx */
488 /* This is only supported by Vista, and not yet by mingw64 */
489 /* if (AddClipboardFormatListener (hwnd) == FALSE) */
495 g_critical ("Failed to install clipboard viewer");
496 UnregisterClass (MAKEINTRESOURCE (klass), _gdk_app_hmodule);
501 gdk_win32_display_request_selection_notification (GdkDisplay *display,
505 static HWND hwndViewer = NULL;
506 gboolean ret = FALSE;
509 g_print ("gdk_display_request_selection_notification (..., %s)",
510 gdk_atom_name (selection)));
512 if (selection == GDK_SELECTION_CLIPBOARD ||
513 selection == GDK_SELECTION_PRIMARY)
517 hwndViewer = _gdk_win32_register_clipboard_notification ();
518 GDK_NOTE (DND, g_print (" registered"));
520 ret = (hwndViewer != NULL);
524 GDK_NOTE (DND, g_print (" unsupported"));
528 GDK_NOTE (DND, g_print (" -> %s\n", ret ? "TRUE" : "FALSE"));
533 gdk_win32_display_supports_clipboard_persistence (GdkDisplay *display)
539 gdk_win32_display_store_clipboard (GdkDisplay *display,
540 GdkWindow *clipboard_window,
542 const GdkAtom *targets,
548 gdk_win32_display_supports_shapes (GdkDisplay *display)
550 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
556 gdk_win32_display_supports_input_shapes (GdkDisplay *display)
558 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
560 /* Not yet implemented. See comment in
561 * gdk_window_input_shape_combine_mask().
568 gdk_win32_display_supports_composite (GdkDisplay *display)
574 gdk_win32_display_beep (GdkDisplay *display)
576 g_return_if_fail (display == gdk_display_get_default());
577 if (!MessageBeep (-1))
582 gdk_win32_display_flush (GdkDisplay * display)
584 g_return_if_fail (display == _gdk_display);
591 gdk_win32_display_sync (GdkDisplay * display)
593 g_return_if_fail (display == _gdk_display);
599 gdk_win32_display_dispose (GObject *object)
604 gdk_win32_display_finalize (GObject *object)
609 gdk_win32_display_init(GdkWin32Display *display)
614 gdk_win32_display_before_process_all_updates (GdkDisplay *display)
619 gdk_win32_display_after_process_all_updates (GdkDisplay *display)
624 gdk_win32_display_notify_startup_complete (GdkDisplay *display,
625 const gchar *startup_id)
630 gdk_win32_display_event_data_copy (GdkDisplay *display,
637 gdk_win32_display_event_data_free (GdkDisplay *display,
643 gdk_win32_display_push_error_trap (GdkDisplay *display)
648 gdk_win32_display_pop_error_trap (GdkDisplay *display,
654 gdk_win32_display_class_init (GdkWin32DisplayClass *klass)
656 GObjectClass *object_class = G_OBJECT_CLASS (klass);
657 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (klass);
659 object_class->dispose = gdk_win32_display_dispose;
660 object_class->finalize = gdk_win32_display_finalize;
662 display_class->window_type = GDK_TYPE_WIN32_WINDOW;
664 display_class->get_name = gdk_win32_display_get_name;
665 display_class->get_n_screens = gdk_win32_display_get_n_screens;
666 display_class->get_screen = gdk_win32_display_get_screen;
667 display_class->get_default_screen = gdk_win32_display_get_default_screen;
668 display_class->beep = gdk_win32_display_beep;
669 display_class->sync = gdk_win32_display_sync;
670 display_class->flush = gdk_win32_display_flush;
671 display_class->has_pending = _gdk_win32_display_has_pending;
672 display_class->queue_events = _gdk_win32_display_queue_events;
673 display_class->get_default_group = gdk_win32_display_get_default_group;
675 display_class->supports_selection_notification = gdk_win32_display_supports_selection_notification;
676 display_class->request_selection_notification = gdk_win32_display_request_selection_notification;
677 display_class->supports_clipboard_persistence = gdk_win32_display_supports_clipboard_persistence;
678 display_class->store_clipboard = gdk_win32_display_store_clipboard;
679 display_class->supports_shapes = gdk_win32_display_supports_shapes;
680 display_class->supports_input_shapes = gdk_win32_display_supports_input_shapes;
681 display_class->supports_composite = gdk_win32_display_supports_composite;
683 display_class->list_devices = _gdk_win32_display_list_devices;
684 //? display_class->get_app_launch_context = _gdk_win32_display_get_app_launch_context;
685 display_class->get_cursor_for_type = _gdk_win32_display_get_cursor_for_type;
686 display_class->get_cursor_for_name = _gdk_win32_display_get_cursor_for_name;
687 display_class->get_cursor_for_pixbuf = _gdk_win32_display_get_cursor_for_pixbuf;
688 display_class->get_default_cursor_size = _gdk_win32_display_get_default_cursor_size;
689 display_class->get_maximal_cursor_size = _gdk_win32_display_get_maximal_cursor_size;
690 display_class->supports_cursor_alpha = _gdk_win32_display_supports_cursor_alpha;
691 display_class->supports_cursor_color = _gdk_win32_display_supports_cursor_color;
693 display_class->before_process_all_updates = gdk_win32_display_before_process_all_updates;
694 display_class->after_process_all_updates = gdk_win32_display_after_process_all_updates;
695 display_class->get_next_serial = gdk_win32_display_get_next_serial;
696 display_class->notify_startup_complete = gdk_win32_display_notify_startup_complete;
697 display_class->event_data_copy = gdk_win32_display_event_data_copy;
698 display_class->event_data_free = gdk_win32_display_event_data_free;
699 display_class->create_window_impl = _gdk_win32_display_create_window_impl;
701 display_class->get_keymap = _gdk_win32_display_get_keymap;
702 display_class->push_error_trap = gdk_win32_display_push_error_trap;
703 display_class->pop_error_trap = gdk_win32_display_pop_error_trap;
704 display_class->get_selection_owner = _gdk_win32_display_get_selection_owner;
705 display_class->set_selection_owner = _gdk_win32_display_set_selection_owner;
706 display_class->send_selection_notify = _gdk_win32_display_send_selection_notify;
707 display_class->get_selection_property = _gdk_win32_display_get_selection_property;
708 display_class->convert_selection = _gdk_win32_display_convert_selection;
709 display_class->text_property_to_utf8_list = _gdk_win32_display_text_property_to_utf8_list;
710 display_class->utf8_to_string_target = _gdk_win32_display_utf8_to_string_target;