dxdiagn: Add several more fields for DisplayDevices.
[wine.git] / server / window.c
blob4a1a27877db0a666183076306c7be61264a85ae1
1 /*
2 * Server-side window handling
4 * Copyright (C) 2001 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winternl.h"
35 #include "object.h"
36 #include "request.h"
37 #include "thread.h"
38 #include "process.h"
39 #include "user.h"
40 #include "unicode.h"
42 /* a window property */
43 struct property
45 unsigned short type; /* property type (see below) */
46 atom_t atom; /* property atom */
47 lparam_t data; /* property data (user-defined storage) */
50 enum property_type
52 PROP_TYPE_FREE, /* free entry */
53 PROP_TYPE_STRING, /* atom that was originally a string */
54 PROP_TYPE_ATOM /* plain atom */
58 struct window
60 struct window *parent; /* parent window */
61 user_handle_t owner; /* owner of this window */
62 struct list children; /* list of children in Z-order */
63 struct list unlinked; /* list of children not linked in the Z-order list */
64 struct list entry; /* entry in parent's children list */
65 user_handle_t handle; /* full handle for this window */
66 struct thread *thread; /* thread owning the window */
67 struct desktop *desktop; /* desktop that the window belongs to */
68 struct window_class *class; /* window class */
69 atom_t atom; /* class atom */
70 user_handle_t last_active; /* last active popup */
71 rectangle_t window_rect; /* window rectangle (relative to parent client area) */
72 rectangle_t visible_rect; /* visible part of window rect (relative to parent client area) */
73 rectangle_t client_rect; /* client rectangle (relative to parent client area) */
74 struct region *win_region; /* region for shaped windows (relative to window rect) */
75 struct region *update_region; /* update region (relative to window rect) */
76 unsigned int style; /* window style */
77 unsigned int ex_style; /* window extended style */
78 unsigned int id; /* window id */
79 mod_handle_t instance; /* creator instance */
80 unsigned int is_unicode : 1; /* ANSI or unicode */
81 unsigned int is_linked : 1; /* is it linked into the parent z-order list? */
82 unsigned int is_layered : 1; /* has layered info been set? */
83 unsigned int color_key; /* color key for a layered window */
84 unsigned int alpha; /* alpha value for a layered window */
85 unsigned int layered_flags; /* flags for a layered window */
86 unsigned int dpi; /* window DPI or 0 if per-monitor aware */
87 DPI_AWARENESS dpi_awareness; /* DPI awareness mode */
88 lparam_t user_data; /* user-specific data */
89 WCHAR *text; /* window caption text */
90 unsigned int paint_flags; /* various painting flags */
91 int prop_inuse; /* number of in-use window properties */
92 int prop_alloc; /* number of allocated window properties */
93 struct property *properties; /* window properties array */
94 int nb_extra_bytes; /* number of extra bytes */
95 char extra_bytes[1]; /* extra bytes storage */
98 /* flags that can be set by the client */
99 #define PAINT_HAS_SURFACE SET_WINPOS_PAINT_SURFACE
100 #define PAINT_HAS_PIXEL_FORMAT SET_WINPOS_PIXEL_FORMAT
101 #define PAINT_CLIENT_FLAGS (PAINT_HAS_SURFACE | PAINT_HAS_PIXEL_FORMAT)
102 /* flags only manipulated by the server */
103 #define PAINT_INTERNAL 0x0010 /* internal WM_PAINT pending */
104 #define PAINT_ERASE 0x0020 /* needs WM_ERASEBKGND */
105 #define PAINT_NONCLIENT 0x0040 /* needs WM_NCPAINT */
106 #define PAINT_DELAYED_ERASE 0x0080 /* still needs erase after WM_ERASEBKGND */
107 #define PAINT_PIXEL_FORMAT_CHILD 0x0100 /* at least one child has a custom pixel format */
109 /* growable array of user handles */
110 struct user_handle_array
112 user_handle_t *handles;
113 int count;
114 int total;
117 /* global window pointers */
118 static struct window *shell_window;
119 static struct window *shell_listview;
120 static struct window *progman_window;
121 static struct window *taskman_window;
123 /* magic HWND_TOP etc. pointers */
124 #define WINPTR_TOP ((struct window *)1L)
125 #define WINPTR_BOTTOM ((struct window *)2L)
126 #define WINPTR_TOPMOST ((struct window *)3L)
127 #define WINPTR_NOTOPMOST ((struct window *)4L)
129 /* retrieve a pointer to a window from its handle */
130 static inline struct window *get_window( user_handle_t handle )
132 struct window *ret = get_user_object( handle, USER_WINDOW );
133 if (!ret) set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
134 return ret;
137 /* check if window is the desktop */
138 static inline int is_desktop_window( const struct window *win )
140 return !win->parent; /* only desktop windows have no parent */
143 /* get next window in Z-order list */
144 static inline struct window *get_next_window( struct window *win )
146 struct list *ptr = list_next( &win->parent->children, &win->entry );
147 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
150 /* get previous window in Z-order list */
151 static inline struct window *get_prev_window( struct window *win )
153 struct list *ptr = list_prev( &win->parent->children, &win->entry );
154 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
157 /* get first child in Z-order list */
158 static inline struct window *get_first_child( struct window *win )
160 struct list *ptr = list_head( &win->children );
161 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
164 /* get last child in Z-order list */
165 static inline struct window *get_last_child( struct window *win )
167 struct list *ptr = list_tail( &win->children );
168 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
171 /* set the PAINT_PIXEL_FORMAT_CHILD flag on all the parents */
172 /* note: we never reset the flag, it's just a heuristic */
173 static inline void update_pixel_format_flags( struct window *win )
175 for (win = win->parent; win && win->parent; win = win->parent)
176 win->paint_flags |= PAINT_PIXEL_FORMAT_CHILD;
179 /* get the per-monitor DPI for a window */
180 static unsigned int get_monitor_dpi( struct window *win )
182 /* FIXME: we return the desktop window DPI for now */
183 while (!is_desktop_window( win )) win = win->parent;
184 return win->dpi ? win->dpi : USER_DEFAULT_SCREEN_DPI;
187 /* link a window at the right place in the siblings list */
188 static void link_window( struct window *win, struct window *previous )
190 if (previous == WINPTR_NOTOPMOST)
192 if (!(win->ex_style & WS_EX_TOPMOST) && win->is_linked) return; /* nothing to do */
193 win->ex_style &= ~WS_EX_TOPMOST;
194 previous = WINPTR_TOP; /* fallback to the HWND_TOP case */
197 list_remove( &win->entry ); /* unlink it from the previous location */
199 if (previous == WINPTR_BOTTOM)
201 list_add_tail( &win->parent->children, &win->entry );
202 win->ex_style &= ~WS_EX_TOPMOST;
204 else if (previous == WINPTR_TOPMOST)
206 list_add_head( &win->parent->children, &win->entry );
207 win->ex_style |= WS_EX_TOPMOST;
209 else if (previous == WINPTR_TOP)
211 struct list *entry = win->parent->children.next;
212 if (!(win->ex_style & WS_EX_TOPMOST)) /* put it above the first non-topmost window */
214 while (entry != &win->parent->children)
216 struct window *next = LIST_ENTRY( entry, struct window, entry );
217 if (!(next->ex_style & WS_EX_TOPMOST)) break;
218 if (next->handle == win->owner) /* keep it above owner */
220 win->ex_style |= WS_EX_TOPMOST;
221 break;
223 entry = entry->next;
226 list_add_before( entry, &win->entry );
228 else
230 list_add_after( &previous->entry, &win->entry );
231 if (!(previous->ex_style & WS_EX_TOPMOST)) win->ex_style &= ~WS_EX_TOPMOST;
232 else
234 struct window *next = get_next_window( win );
235 if (next && (next->ex_style & WS_EX_TOPMOST)) win->ex_style |= WS_EX_TOPMOST;
239 win->is_linked = 1;
242 /* change the parent of a window (or unlink the window if the new parent is NULL) */
243 static int set_parent_window( struct window *win, struct window *parent )
245 struct window *ptr;
247 /* make sure parent is not a child of window */
248 for (ptr = parent; ptr; ptr = ptr->parent)
250 if (ptr == win)
252 set_error( STATUS_INVALID_PARAMETER );
253 return 0;
257 if (parent)
259 win->parent = parent;
260 link_window( win, WINPTR_TOP );
262 if (!is_desktop_window( parent ))
264 win->dpi = parent->dpi;
265 win->dpi_awareness = parent->dpi_awareness;
268 /* if parent belongs to a different thread and the window isn't */
269 /* top-level, attach the two threads */
270 if (parent->thread && parent->thread != win->thread && !is_desktop_window(parent))
271 attach_thread_input( win->thread, parent->thread );
273 if (win->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))
274 update_pixel_format_flags( win );
276 else /* move it to parent unlinked list */
278 list_remove( &win->entry ); /* unlink it from the previous location */
279 list_add_head( &win->parent->unlinked, &win->entry );
280 win->is_linked = 0;
282 return 1;
285 /* append a user handle to a handle array */
286 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
288 if (array->count >= array->total)
290 int new_total = max( array->total * 2, 32 );
291 user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
292 if (!new_array)
294 free( array->handles );
295 set_error( STATUS_NO_MEMORY );
296 return 0;
298 array->handles = new_array;
299 array->total = new_total;
301 array->handles[array->count++] = handle;
302 return 1;
305 /* set a window property */
306 static void set_property( struct window *win, atom_t atom, lparam_t data, enum property_type type )
308 int i, free = -1;
309 struct property *new_props;
311 /* check if it exists already */
312 for (i = 0; i < win->prop_inuse; i++)
314 if (win->properties[i].type == PROP_TYPE_FREE)
316 free = i;
317 continue;
319 if (win->properties[i].atom == atom)
321 win->properties[i].type = type;
322 win->properties[i].data = data;
323 return;
327 /* need to add an entry */
328 if (!grab_global_atom( NULL, atom )) return;
329 if (free == -1)
331 /* no free entry */
332 if (win->prop_inuse >= win->prop_alloc)
334 /* need to grow the array */
335 if (!(new_props = realloc( win->properties,
336 sizeof(*new_props) * (win->prop_alloc + 16) )))
338 set_error( STATUS_NO_MEMORY );
339 release_global_atom( NULL, atom );
340 return;
342 win->prop_alloc += 16;
343 win->properties = new_props;
345 free = win->prop_inuse++;
347 win->properties[free].atom = atom;
348 win->properties[free].type = type;
349 win->properties[free].data = data;
352 /* remove a window property */
353 static lparam_t remove_property( struct window *win, atom_t atom )
355 int i;
357 for (i = 0; i < win->prop_inuse; i++)
359 if (win->properties[i].type == PROP_TYPE_FREE) continue;
360 if (win->properties[i].atom == atom)
362 release_global_atom( NULL, atom );
363 win->properties[i].type = PROP_TYPE_FREE;
364 return win->properties[i].data;
367 /* FIXME: last error? */
368 return 0;
371 /* find a window property */
372 static lparam_t get_property( struct window *win, atom_t atom )
374 int i;
376 for (i = 0; i < win->prop_inuse; i++)
378 if (win->properties[i].type == PROP_TYPE_FREE) continue;
379 if (win->properties[i].atom == atom) return win->properties[i].data;
381 /* FIXME: last error? */
382 return 0;
385 /* destroy all properties of a window */
386 static inline void destroy_properties( struct window *win )
388 int i;
390 if (!win->properties) return;
391 for (i = 0; i < win->prop_inuse; i++)
393 if (win->properties[i].type == PROP_TYPE_FREE) continue;
394 release_global_atom( NULL, win->properties[i].atom );
396 free( win->properties );
399 /* detach a window from its owner thread but keep the window around */
400 static void detach_window_thread( struct window *win )
402 struct thread *thread = win->thread;
404 if (!thread) return;
405 if (thread->queue)
407 if (win->update_region) inc_queue_paint_count( thread, -1 );
408 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
409 queue_cleanup_window( thread, win->handle );
411 assert( thread->desktop_users > 0 );
412 thread->desktop_users--;
413 release_class( win->class );
414 win->class = NULL;
416 /* don't hold a reference to the desktop so that the desktop window can be */
417 /* destroyed when the desktop ref count reaches zero */
418 release_object( win->desktop );
419 win->thread = NULL;
422 /* get the process owning the top window of a given desktop */
423 struct process *get_top_window_owner( struct desktop *desktop )
425 struct window *win = desktop->top_window;
426 if (!win || !win->thread) return NULL;
427 return win->thread->process;
430 /* get the top window size of a given desktop */
431 void get_top_window_rectangle( struct desktop *desktop, rectangle_t *rect )
433 struct window *win = desktop->top_window;
434 if (!win) rect->left = rect->top = rect->right = rect->bottom = 0;
435 else *rect = win->window_rect;
438 /* post a message to the desktop window */
439 void post_desktop_message( struct desktop *desktop, unsigned int message,
440 lparam_t wparam, lparam_t lparam )
442 struct window *win = desktop->top_window;
443 if (win && win->thread) post_message( win->handle, message, wparam, lparam );
446 /* create a new window structure (note: the window is not linked in the window tree) */
447 static struct window *create_window( struct window *parent, struct window *owner,
448 atom_t atom, mod_handle_t instance )
450 static const rectangle_t empty_rect;
451 int extra_bytes;
452 struct window *win = NULL;
453 struct desktop *desktop;
454 struct window_class *class;
456 if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
458 if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
460 release_object( desktop );
461 return NULL;
464 if (!parent) /* null parent is only allowed for desktop or HWND_MESSAGE top window */
466 if (is_desktop_class( class ))
467 parent = desktop->top_window; /* use existing desktop if any */
468 else if (is_hwnd_message_class( class ))
469 /* use desktop window if message window is already created */
470 parent = desktop->msg_window ? desktop->top_window : NULL;
471 else if (!(parent = desktop->top_window)) /* must already have a desktop then */
473 set_error( STATUS_ACCESS_DENIED );
474 goto failed;
478 /* parent must be on the same desktop */
479 if (parent && parent->desktop != desktop)
481 set_error( STATUS_ACCESS_DENIED );
482 goto failed;
485 if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
486 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
488 win->parent = parent;
489 win->owner = owner ? owner->handle : 0;
490 win->thread = current;
491 win->desktop = desktop;
492 win->class = class;
493 win->atom = atom;
494 win->last_active = win->handle;
495 win->win_region = NULL;
496 win->update_region = NULL;
497 win->style = 0;
498 win->ex_style = 0;
499 win->id = 0;
500 win->instance = 0;
501 win->is_unicode = 1;
502 win->is_linked = 0;
503 win->is_layered = 0;
504 win->dpi_awareness = DPI_AWARENESS_PER_MONITOR_AWARE;
505 win->dpi = 0;
506 win->user_data = 0;
507 win->text = NULL;
508 win->paint_flags = 0;
509 win->prop_inuse = 0;
510 win->prop_alloc = 0;
511 win->properties = NULL;
512 win->nb_extra_bytes = extra_bytes;
513 win->window_rect = win->visible_rect = win->client_rect = empty_rect;
514 memset( win->extra_bytes, 0, extra_bytes );
515 list_init( &win->children );
516 list_init( &win->unlinked );
518 /* if parent belongs to a different thread and the window isn't */
519 /* top-level, attach the two threads */
520 if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
522 if (!attach_thread_input( current, parent->thread )) goto failed;
524 else /* otherwise just make sure that the thread has a message queue */
526 if (!current->queue && !init_thread_queue( current )) goto failed;
529 /* put it on parent unlinked list */
530 if (parent) list_add_head( &parent->unlinked, &win->entry );
531 else
533 list_init( &win->entry );
534 if (is_desktop_class( class ))
536 assert( !desktop->top_window );
537 desktop->top_window = win;
538 set_process_default_desktop( current->process, desktop, current->desktop );
540 else
542 assert( !desktop->msg_window );
543 desktop->msg_window = win;
547 current->desktop_users++;
548 return win;
550 failed:
551 if (win)
553 if (win->handle) free_user_handle( win->handle );
554 free( win );
556 release_object( desktop );
557 release_class( class );
558 return NULL;
561 /* destroy all windows belonging to a given thread */
562 void destroy_thread_windows( struct thread *thread )
564 user_handle_t handle = 0;
565 struct window *win;
567 while ((win = next_user_handle( &handle, USER_WINDOW )))
569 if (win->thread != thread) continue;
570 if (is_desktop_window( win )) detach_window_thread( win );
571 else destroy_window( win );
575 /* get the desktop window */
576 static struct window *get_desktop_window( struct thread *thread )
578 struct window *top_window;
579 struct desktop *desktop = get_thread_desktop( thread, 0 );
581 if (!desktop) return NULL;
582 top_window = desktop->top_window;
583 release_object( desktop );
584 return top_window;
587 /* check whether child is a descendant of parent */
588 int is_child_window( user_handle_t parent, user_handle_t child )
590 struct window *child_ptr = get_user_object( child, USER_WINDOW );
591 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
593 if (!child_ptr || !parent_ptr) return 0;
594 while (child_ptr->parent)
596 if (child_ptr->parent == parent_ptr) return 1;
597 child_ptr = child_ptr->parent;
599 return 0;
602 /* check if window can be set as foreground window */
603 int is_valid_foreground_window( user_handle_t window )
605 struct window *win = get_user_object( window, USER_WINDOW );
606 return win && (win->style & (WS_POPUP|WS_CHILD)) != WS_CHILD;
609 /* make a window active if possible */
610 int make_window_active( user_handle_t window )
612 struct window *owner, *win = get_window( window );
614 if (!win) return 0;
616 /* set last active for window and its owners */
617 owner = win;
618 while (owner)
620 owner->last_active = win->handle;
621 owner = get_user_object( owner->owner, USER_WINDOW );
623 return 1;
626 /* increment (or decrement) the window paint count */
627 static inline void inc_window_paint_count( struct window *win, int incr )
629 if (win->thread) inc_queue_paint_count( win->thread, incr );
632 /* check if window and all its ancestors are visible */
633 static int is_visible( const struct window *win )
635 while (win)
637 if (!(win->style & WS_VISIBLE)) return 0;
638 win = win->parent;
639 /* if parent is minimized children are not visible */
640 if (win && (win->style & WS_MINIMIZE)) return 0;
642 return 1;
645 /* same as is_visible but takes a window handle */
646 int is_window_visible( user_handle_t window )
648 struct window *win = get_user_object( window, USER_WINDOW );
649 if (!win) return 0;
650 return is_visible( win );
653 int is_window_transparent( user_handle_t window )
655 struct window *win = get_user_object( window, USER_WINDOW );
656 if (!win) return 0;
657 return (win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT);
660 /* check if point is inside the window */
661 static inline int is_point_in_window( struct window *win, int x, int y )
663 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
664 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
665 return 0; /* disabled child */
666 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
667 return 0; /* transparent */
668 if (x < win->visible_rect.left || x >= win->visible_rect.right ||
669 y < win->visible_rect.top || y >= win->visible_rect.bottom)
670 return 0; /* not in window */
671 if (win->win_region &&
672 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
673 return 0; /* not in window region */
674 return 1;
677 /* fill an array with the handles of the children of a specified window */
678 static unsigned int get_children_windows( struct window *parent, atom_t atom, thread_id_t tid,
679 user_handle_t *handles, unsigned int max_count )
681 struct window *ptr;
682 unsigned int count = 0;
684 if (!parent) return 0;
686 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
688 if (atom && get_class_atom(ptr->class) != atom) continue;
689 if (tid && get_thread_id(ptr->thread) != tid) continue;
690 if (handles)
692 if (count >= max_count) break;
693 handles[count] = ptr->handle;
695 count++;
697 return count;
700 /* find child of 'parent' that contains the given point (in parent-relative coords) */
701 static struct window *child_window_from_point( struct window *parent, int x, int y )
703 struct window *ptr;
705 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
707 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
709 /* if window is minimized or disabled, return at once */
710 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
712 /* if point is not in client area, return at once */
713 if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
714 y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
715 return ptr;
717 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
719 return parent; /* not found any child */
722 /* find all children of 'parent' that contain the given point */
723 static int get_window_children_from_point( struct window *parent, int x, int y,
724 struct user_handle_array *array )
726 struct window *ptr;
728 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
730 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
732 /* if point is in client area, and window is not minimized or disabled, check children */
733 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
734 x >= ptr->client_rect.left && x < ptr->client_rect.right &&
735 y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
737 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
738 y - ptr->client_rect.top, array ))
739 return 0;
742 /* now add window to the array */
743 if (!add_handle_to_array( array, ptr->handle )) return 0;
745 return 1;
748 /* get handle of root of top-most window containing point */
749 user_handle_t shallow_window_from_point( struct desktop *desktop, int x, int y )
751 struct window *ptr;
753 if (!desktop->top_window) return 0;
755 LIST_FOR_EACH_ENTRY( ptr, &desktop->top_window->children, struct window, entry )
757 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
758 return ptr->handle;
760 return desktop->top_window->handle;
763 /* return thread of top-most window containing point (in absolute coords) */
764 struct thread *window_thread_from_point( user_handle_t scope, int x, int y )
766 struct window *win = get_user_object( scope, USER_WINDOW );
767 struct window *ptr;
769 if (!win) return NULL;
771 for (ptr = win; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
773 x -= ptr->client_rect.left;
774 y -= ptr->client_rect.top;
777 ptr = child_window_from_point( win, x, y );
778 if (!ptr->thread) return NULL;
779 return (struct thread *)grab_object( ptr->thread );
782 /* return list of all windows containing point (in absolute coords) */
783 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
785 struct window *ptr;
787 /* make point relative to top window */
788 for (ptr = top->parent; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
790 x -= ptr->client_rect.left;
791 y -= ptr->client_rect.top;
794 if (!is_point_in_window( top, x, y )) return 1;
796 /* if point is in client area, and window is not minimized or disabled, check children */
797 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
798 x >= top->client_rect.left && x < top->client_rect.right &&
799 y >= top->client_rect.top && y < top->client_rect.bottom)
801 if (!is_desktop_window(top))
803 x -= top->client_rect.left;
804 y -= top->client_rect.top;
806 if (!get_window_children_from_point( top, x, y, array )) return 0;
808 /* now add window to the array */
809 if (!add_handle_to_array( array, top->handle )) return 0;
810 return 1;
814 /* return the thread owning a window */
815 struct thread *get_window_thread( user_handle_t handle )
817 struct window *win = get_user_object( handle, USER_WINDOW );
818 if (!win || !win->thread) return NULL;
819 return (struct thread *)grab_object( win->thread );
823 /* check if any area of a window needs repainting */
824 static inline int win_needs_repaint( struct window *win )
826 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
830 /* find a child of the specified window that needs repainting */
831 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
833 struct window *ptr, *ret = NULL;
835 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
837 if (!(ptr->style & WS_VISIBLE)) continue;
838 if (ptr->thread == thread && win_needs_repaint( ptr ))
839 ret = ptr;
840 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
841 ret = find_child_to_repaint( ptr, thread );
842 if (ret) break;
845 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
847 /* transparent window, check for non-transparent sibling to paint first */
848 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
850 if (!(ptr->style & WS_VISIBLE)) continue;
851 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
852 if (ptr->thread != thread) continue;
853 if (win_needs_repaint( ptr )) return ptr;
856 return ret;
860 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
861 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
863 struct window *ptr, *win, *top_window = get_desktop_window( thread );
865 if (!top_window) return 0;
867 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
868 else win = find_child_to_repaint( top_window, thread );
870 if (win && parent)
872 /* check that it is a child of the specified parent */
873 for (ptr = win; ptr; ptr = ptr->parent)
874 if (ptr->handle == parent) break;
875 /* otherwise don't return any window, we don't repaint a child before its parent */
876 if (!ptr) win = NULL;
878 if (!win) return 0;
879 win->paint_flags &= ~PAINT_INTERNAL;
880 return win->handle;
884 /* intersect the window region with the specified region, relative to the window parent */
885 static struct region *intersect_window_region( struct region *region, struct window *win )
887 /* make region relative to window rect */
888 offset_region( region, -win->window_rect.left, -win->window_rect.top );
889 if (!intersect_region( region, region, win->win_region )) return NULL;
890 /* make region relative to parent again */
891 offset_region( region, win->window_rect.left, win->window_rect.top );
892 return region;
896 /* convert coordinates from client to screen coords */
897 static inline void client_to_screen( struct window *win, int *x, int *y )
899 for ( ; win && !is_desktop_window(win); win = win->parent)
901 *x += win->client_rect.left;
902 *y += win->client_rect.top;
906 /* convert coordinates from client to screen coords */
907 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
909 for ( ; win && !is_desktop_window(win); win = win->parent)
911 rect->left += win->client_rect.left;
912 rect->right += win->client_rect.left;
913 rect->top += win->client_rect.top;
914 rect->bottom += win->client_rect.top;
918 /* map the region from window to screen coordinates */
919 static inline void map_win_region_to_screen( struct window *win, struct region *region )
921 if (!is_desktop_window(win))
923 int x = win->window_rect.left;
924 int y = win->window_rect.top;
925 client_to_screen( win->parent, &x, &y );
926 offset_region( region, x, y );
931 /* clip all children of a given window out of the visible region */
932 static struct region *clip_children( struct window *parent, struct window *last,
933 struct region *region, int offset_x, int offset_y )
935 struct window *ptr;
936 struct region *tmp = create_empty_region();
938 if (!tmp) return NULL;
939 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
941 if (ptr == last) break;
942 if (!(ptr->style & WS_VISIBLE)) continue;
943 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
944 set_region_rect( tmp, &ptr->visible_rect );
945 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
947 free_region( tmp );
948 return NULL;
950 offset_region( tmp, offset_x, offset_y );
951 if (!(region = subtract_region( region, region, tmp ))) break;
952 if (is_region_empty( region )) break;
954 free_region( tmp );
955 return region;
959 /* offset the coordinates of a rectangle */
960 static inline void offset_rect( rectangle_t *rect, int offset_x, int offset_y )
962 rect->left += offset_x;
963 rect->top += offset_y;
964 rect->right += offset_x;
965 rect->bottom += offset_y;
969 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
970 static void set_region_client_rect( struct region *region, struct window *win )
972 rectangle_t rect;
974 intersect_rect( &rect, &win->window_rect, &win->client_rect );
975 set_region_rect( region, &rect );
979 /* get the top-level window to clip against for a given window */
980 static inline struct window *get_top_clipping_window( struct window *win )
982 while (!(win->paint_flags & PAINT_HAS_SURFACE) && win->parent && !is_desktop_window(win->parent))
983 win = win->parent;
984 return win;
988 /* compute the visible region of a window, in window coordinates */
989 static struct region *get_visible_region( struct window *win, unsigned int flags )
991 struct region *tmp = NULL, *region;
992 int offset_x, offset_y;
994 if (!(region = create_empty_region())) return NULL;
996 /* first check if all ancestors are visible */
998 if (!is_visible( win )) return region; /* empty region */
1000 /* create a region relative to the window itself */
1002 if ((flags & DCX_PARENTCLIP) && win->parent && !is_desktop_window(win->parent))
1004 set_region_client_rect( region, win->parent );
1005 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
1007 else if (flags & DCX_WINDOW)
1009 set_region_rect( region, &win->visible_rect );
1010 if (win->win_region && !intersect_window_region( region, win )) goto error;
1012 else
1014 set_region_client_rect( region, win );
1015 if (win->win_region && !intersect_window_region( region, win )) goto error;
1018 /* clip children */
1020 if (flags & DCX_CLIPCHILDREN)
1022 if (is_desktop_window(win)) offset_x = offset_y = 0;
1023 else
1025 offset_x = win->client_rect.left;
1026 offset_y = win->client_rect.top;
1028 if (!clip_children( win, NULL, region, offset_x, offset_y )) goto error;
1031 /* clip siblings of ancestors */
1033 if (is_desktop_window(win)) offset_x = offset_y = 0;
1034 else
1036 offset_x = win->window_rect.left;
1037 offset_y = win->window_rect.top;
1040 if ((tmp = create_empty_region()) != NULL)
1042 while (win->parent)
1044 /* we don't clip out top-level siblings as that's up to the native windowing system */
1045 if ((win->style & WS_CLIPSIBLINGS) && !is_desktop_window( win->parent ))
1047 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
1048 if (is_region_empty( region )) break;
1050 /* clip to parent client area */
1051 win = win->parent;
1052 if (!is_desktop_window(win))
1054 offset_x += win->client_rect.left;
1055 offset_y += win->client_rect.top;
1056 offset_region( region, win->client_rect.left, win->client_rect.top );
1058 set_region_client_rect( tmp, win );
1059 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
1060 if (!intersect_region( region, region, tmp )) goto error;
1061 if (is_region_empty( region )) break;
1063 free_region( tmp );
1065 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
1066 return region;
1068 error:
1069 if (tmp) free_region( tmp );
1070 free_region( region );
1071 return NULL;
1075 /* clip all children with a custom pixel format out of the visible region */
1076 static struct region *clip_pixel_format_children( struct window *parent, struct region *parent_clip,
1077 struct region *region, int offset_x, int offset_y )
1079 struct window *ptr;
1080 struct region *clip = create_empty_region();
1082 if (!clip) return NULL;
1084 LIST_FOR_EACH_ENTRY_REV( ptr, &parent->children, struct window, entry )
1086 if (!(ptr->style & WS_VISIBLE)) continue;
1087 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1089 /* add the visible rect */
1090 set_region_rect( clip, &ptr->visible_rect );
1091 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1092 offset_region( clip, offset_x, offset_y );
1093 if (!intersect_region( clip, clip, parent_clip )) break;
1094 if (!union_region( region, region, clip )) break;
1095 if (!(ptr->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))) continue;
1097 /* subtract the client rect if it uses a custom pixel format */
1098 set_region_rect( clip, &ptr->client_rect );
1099 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1100 offset_region( clip, offset_x, offset_y );
1101 if (!intersect_region( clip, clip, parent_clip )) break;
1102 if ((ptr->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1103 break;
1105 if (!clip_pixel_format_children( ptr, clip, region, offset_x + ptr->client_rect.left,
1106 offset_y + ptr->client_rect.top ))
1107 break;
1109 free_region( clip );
1110 return region;
1114 /* compute the visible surface region of a window, in parent coordinates */
1115 static struct region *get_surface_region( struct window *win )
1117 struct region *region, *clip;
1118 int offset_x, offset_y;
1120 /* create a region relative to the window itself */
1122 if (!(region = create_empty_region())) return NULL;
1123 if (!(clip = create_empty_region())) goto error;
1124 set_region_rect( region, &win->visible_rect );
1125 if (win->win_region && !intersect_window_region( region, win )) goto error;
1126 set_region_rect( clip, &win->client_rect );
1127 if (win->win_region && !intersect_window_region( clip, win )) goto error;
1129 if ((win->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1130 goto error;
1132 /* clip children */
1134 if (!is_desktop_window(win))
1136 offset_x = win->client_rect.left;
1137 offset_y = win->client_rect.top;
1139 else offset_x = offset_y = 0;
1141 if (!clip_pixel_format_children( win, clip, region, offset_x, offset_y )) goto error;
1143 free_region( clip );
1144 return region;
1146 error:
1147 if (clip) free_region( clip );
1148 free_region( region );
1149 return NULL;
1153 /* get the window class of a window */
1154 struct window_class* get_window_class( user_handle_t window )
1156 struct window *win;
1157 if (!(win = get_window( window ))) return NULL;
1158 if (!win->class) set_error( STATUS_ACCESS_DENIED );
1159 return win->class;
1162 /* determine the window visible rectangle, i.e. window or client rect cropped by parent rects */
1163 /* the returned rectangle is in window coordinates; return 0 if rectangle is empty */
1164 static int get_window_visible_rect( struct window *win, rectangle_t *rect, int frame )
1166 int offset_x = 0, offset_y = 0;
1168 if (!(win->style & WS_VISIBLE)) return 0;
1170 *rect = frame ? win->window_rect : win->client_rect;
1171 if (!is_desktop_window(win))
1173 offset_x = win->window_rect.left;
1174 offset_y = win->window_rect.top;
1177 while (win->parent)
1179 win = win->parent;
1180 if (!(win->style & WS_VISIBLE) || win->style & WS_MINIMIZE) return 0;
1181 if (!is_desktop_window(win))
1183 offset_x += win->client_rect.left;
1184 offset_y += win->client_rect.top;
1185 offset_rect( rect, win->client_rect.left, win->client_rect.top );
1187 if (!intersect_rect( rect, rect, &win->client_rect )) return 0;
1188 if (!intersect_rect( rect, rect, &win->window_rect )) return 0;
1190 offset_rect( rect, -offset_x, -offset_y );
1191 return 1;
1194 /* return a copy of the specified region cropped to the window client or frame rectangle, */
1195 /* and converted from client to window coordinates. Helper for (in)validate_window. */
1196 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
1198 rectangle_t rect;
1199 struct region *tmp;
1201 if (!get_window_visible_rect( win, &rect, frame )) return NULL;
1202 if (!(tmp = create_empty_region())) return NULL;
1203 set_region_rect( tmp, &rect );
1205 if (region)
1207 /* map it to client coords */
1208 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1209 win->window_rect.top - win->client_rect.top );
1211 /* intersect specified region with bounding rect */
1212 if (!intersect_region( tmp, region, tmp )) goto done;
1213 if (is_region_empty( tmp )) goto done;
1215 /* map it back to window coords */
1216 offset_region( tmp, win->client_rect.left - win->window_rect.left,
1217 win->client_rect.top - win->window_rect.top );
1219 return tmp;
1221 done:
1222 free_region( tmp );
1223 return NULL;
1227 /* set a region as new update region for the window */
1228 static void set_update_region( struct window *win, struct region *region )
1230 if (region && !is_region_empty( region ))
1232 if (!win->update_region) inc_window_paint_count( win, 1 );
1233 else free_region( win->update_region );
1234 win->update_region = region;
1236 else
1238 if (win->update_region)
1240 inc_window_paint_count( win, -1 );
1241 free_region( win->update_region );
1243 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
1244 win->update_region = NULL;
1245 if (region) free_region( region );
1250 /* add a region to the update region; the passed region is freed or reused */
1251 static int add_update_region( struct window *win, struct region *region )
1253 if (win->update_region && !union_region( region, win->update_region, region ))
1255 free_region( region );
1256 return 0;
1258 set_update_region( win, region );
1259 return 1;
1263 /* crop the update region of children to the specified rectangle, in client coords */
1264 static void crop_children_update_region( struct window *win, rectangle_t *rect )
1266 struct window *child;
1267 struct region *tmp;
1268 rectangle_t child_rect;
1270 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1272 if (!(child->style & WS_VISIBLE)) continue;
1273 if (!rect) /* crop everything out */
1275 crop_children_update_region( child, NULL );
1276 set_update_region( child, NULL );
1277 continue;
1280 /* nothing to do if child is completely inside rect */
1281 if (child->window_rect.left >= rect->left &&
1282 child->window_rect.top >= rect->top &&
1283 child->window_rect.right <= rect->right &&
1284 child->window_rect.bottom <= rect->bottom) continue;
1286 /* map to child client coords and crop grand-children */
1287 child_rect = *rect;
1288 offset_rect( &child_rect, -child->client_rect.left, -child->client_rect.top );
1289 crop_children_update_region( child, &child_rect );
1291 /* now crop the child itself */
1292 if (!child->update_region) continue;
1293 if (!(tmp = create_empty_region())) continue;
1294 set_region_rect( tmp, rect );
1295 offset_region( tmp, -child->window_rect.left, -child->window_rect.top );
1296 if (intersect_region( tmp, child->update_region, tmp )) set_update_region( child, tmp );
1297 else free_region( tmp );
1302 /* validate the non client area of a window */
1303 static void validate_non_client( struct window *win )
1305 struct region *tmp;
1306 rectangle_t rect;
1308 if (!win->update_region) return; /* nothing to do */
1310 /* get client rect in window coords */
1311 rect.left = win->client_rect.left - win->window_rect.left;
1312 rect.top = win->client_rect.top - win->window_rect.top;
1313 rect.right = win->client_rect.right - win->window_rect.left;
1314 rect.bottom = win->client_rect.bottom - win->window_rect.top;
1316 if ((tmp = create_empty_region()))
1318 set_region_rect( tmp, &rect );
1319 if (intersect_region( tmp, win->update_region, tmp ))
1320 set_update_region( win, tmp );
1321 else
1322 free_region( tmp );
1324 win->paint_flags &= ~PAINT_NONCLIENT;
1328 /* validate a window completely so that we don't get any further paint messages for it */
1329 static void validate_whole_window( struct window *win )
1331 set_update_region( win, NULL );
1333 if (win->paint_flags & PAINT_INTERNAL)
1335 win->paint_flags &= ~PAINT_INTERNAL;
1336 inc_window_paint_count( win, -1 );
1341 /* validate a window's children so that we don't get any further paint messages for it */
1342 static void validate_children( struct window *win )
1344 struct window *child;
1346 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1348 if (!(child->style & WS_VISIBLE)) continue;
1349 validate_children(child);
1350 validate_whole_window(child);
1355 /* validate the update region of a window on all parents; helper for get_update_region */
1356 static void validate_parents( struct window *child )
1358 int offset_x = 0, offset_y = 0;
1359 struct window *win = child;
1360 struct region *tmp = NULL;
1362 if (!child->update_region) return;
1364 while (win->parent)
1366 /* map to parent client coords */
1367 offset_x += win->window_rect.left;
1368 offset_y += win->window_rect.top;
1370 win = win->parent;
1372 /* and now map to window coords */
1373 offset_x += win->client_rect.left - win->window_rect.left;
1374 offset_y += win->client_rect.top - win->window_rect.top;
1376 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1378 if (!tmp && !(tmp = create_empty_region())) return;
1379 offset_region( child->update_region, offset_x, offset_y );
1380 if (subtract_region( tmp, win->update_region, child->update_region ))
1382 set_update_region( win, tmp );
1383 tmp = NULL;
1385 /* restore child coords */
1386 offset_region( child->update_region, -offset_x, -offset_y );
1389 if (tmp) free_region( tmp );
1393 /* add/subtract a region (in client coordinates) to the update region of the window */
1394 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1396 struct region *tmp;
1397 struct window *child;
1399 if (flags & RDW_INVALIDATE)
1401 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1403 if (!add_update_region( win, tmp )) return;
1405 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1406 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1408 else if (flags & RDW_VALIDATE)
1410 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1412 set_update_region( win, NULL );
1414 else if (win->update_region)
1416 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1418 if (!subtract_region( tmp, win->update_region, tmp ))
1420 free_region( tmp );
1421 return;
1423 set_update_region( win, tmp );
1425 if (flags & RDW_NOFRAME) validate_non_client( win );
1426 if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1430 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1432 win->paint_flags |= PAINT_INTERNAL;
1433 inc_window_paint_count( win, 1 );
1435 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1437 win->paint_flags &= ~PAINT_INTERNAL;
1438 inc_window_paint_count( win, -1 );
1441 /* now process children recursively */
1443 if (flags & RDW_NOCHILDREN) return;
1444 if (win->style & WS_MINIMIZE) return;
1445 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1447 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1449 /* map to client coordinates */
1450 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1451 win->window_rect.top - win->client_rect.top );
1453 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1455 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1457 if (!(child->style & WS_VISIBLE)) continue;
1458 if (!rect_in_region( tmp, &child->window_rect )) continue;
1459 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1460 redraw_window( child, tmp, 1, flags );
1461 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1463 free_region( tmp );
1467 /* retrieve the update flags for a window depending on the state of the update region */
1468 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1470 unsigned int ret = 0;
1472 if (flags & UPDATE_NONCLIENT)
1474 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1476 if (flags & UPDATE_ERASE)
1478 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1480 if (flags & UPDATE_PAINT)
1482 if (win->update_region)
1484 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1485 ret |= UPDATE_PAINT;
1488 if (flags & UPDATE_INTERNALPAINT)
1490 if (win->paint_flags & PAINT_INTERNAL)
1492 ret |= UPDATE_INTERNALPAINT;
1493 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1496 return ret;
1500 /* iterate through the children of the given window until we find one with some update flags */
1501 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1502 unsigned int flags, struct window **child )
1504 struct window *ptr;
1505 unsigned int ret = 0;
1507 /* first make sure we want to iterate children at all */
1509 if (win->style & WS_MINIMIZE) return 0;
1511 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1512 * here we only want to repaint children of windows that clip them, others
1513 * need to wait for WM_PAINT to be done in the parent first.
1515 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1517 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1519 if (from_child) /* skip all children until from_child is found */
1521 if (ptr == from_child) from_child = NULL;
1522 continue;
1524 if (!(ptr->style & WS_VISIBLE)) continue;
1525 if ((ret = get_update_flags( ptr, flags )) != 0)
1527 *child = ptr;
1528 break;
1530 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1532 return ret;
1535 /* iterate through children and siblings of the given window until we find one with some update flags */
1536 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1537 unsigned int flags, struct window **child )
1539 unsigned int ret;
1540 struct window *ptr, *from_sibling = NULL;
1542 /* if some parent is not visible start from the next sibling */
1544 if (!is_visible( win )) return 0;
1545 for (ptr = from_child; ptr; ptr = ptr->parent)
1547 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1548 if (ptr == win) break;
1551 /* non-client painting must be delayed if one of the parents is going to
1552 * be repainted and doesn't clip children */
1554 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1556 for (ptr = win->parent; ptr; ptr = ptr->parent)
1558 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1559 return 0;
1561 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1563 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1565 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1566 if (ptr == win) break;
1572 /* check window itself (only if not restarting from a child) */
1574 if (!from_child)
1576 if ((ret = get_update_flags( win, flags )))
1578 *child = win;
1579 return ret;
1581 from_child = win;
1584 /* now check children */
1586 if (flags & UPDATE_NOCHILDREN) return 0;
1587 if (!from_sibling)
1589 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1590 from_sibling = from_child;
1593 /* then check siblings and parent siblings */
1595 while (from_sibling->parent && from_sibling != win)
1597 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1598 return ret;
1599 from_sibling = from_sibling->parent;
1601 return 0;
1605 /* expose the areas revealed by a vis region change on the window parent */
1606 /* returns the region exposed on the window itself (in client coordinates) */
1607 static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
1608 struct region *old_vis_rgn )
1610 struct region *new_vis_rgn, *exposed_rgn;
1612 if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
1614 if ((exposed_rgn = create_empty_region()))
1616 if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
1618 /* make it relative to the new client area */
1619 offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
1620 win->window_rect.top - win->client_rect.top );
1622 else
1624 free_region( exposed_rgn );
1625 exposed_rgn = NULL;
1629 if (win->parent && !is_desktop_window( win->parent ))
1631 /* make it relative to the old window pos for subtracting */
1632 offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
1633 win->window_rect.top - old_window_rect->top );
1635 if ((win->parent->style & WS_CLIPCHILDREN) ?
1636 subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) :
1637 xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1639 if (!is_region_empty( new_vis_rgn ))
1641 /* make it relative to parent */
1642 offset_region( new_vis_rgn, old_window_rect->left, old_window_rect->top );
1643 redraw_window( win->parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1647 free_region( new_vis_rgn );
1648 return exposed_rgn;
1652 /* set the window and client rectangles, updating the update region if necessary */
1653 static void set_window_pos( struct window *win, struct window *previous,
1654 unsigned int swp_flags, const rectangle_t *window_rect,
1655 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1656 const rectangle_t *valid_rects )
1658 struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
1659 const rectangle_t old_window_rect = win->window_rect;
1660 const rectangle_t old_visible_rect = win->visible_rect;
1661 const rectangle_t old_client_rect = win->client_rect;
1662 rectangle_t rect;
1663 int client_changed, frame_changed;
1664 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1666 if (win->parent && !is_visible( win->parent )) visible = 0;
1668 if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
1670 /* set the new window info before invalidating anything */
1672 win->window_rect = *window_rect;
1673 win->visible_rect = *visible_rect;
1674 win->client_rect = *client_rect;
1675 if (!(swp_flags & SWP_NOZORDER) && win->parent) link_window( win, previous );
1676 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1677 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1679 /* keep children at the same position relative to top right corner when the parent is mirrored */
1680 if (win->ex_style & WS_EX_LAYOUTRTL)
1682 struct window *child;
1683 int old_size = old_client_rect.right - old_client_rect.left;
1684 int new_size = win->client_rect.right - win->client_rect.left;
1686 if (old_size != new_size) LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1688 offset_rect( &child->window_rect, new_size - old_size, 0 );
1689 offset_rect( &child->visible_rect, new_size - old_size, 0 );
1690 offset_rect( &child->client_rect, new_size - old_size, 0 );
1694 /* reset cursor clip rectangle when the desktop changes size */
1695 if (win == win->desktop->top_window) win->desktop->cursor.clip = *window_rect;
1697 /* if the window is not visible, everything is easy */
1698 if (!visible) return;
1700 /* expose anything revealed by the change */
1702 if (!(swp_flags & SWP_NOREDRAW))
1703 exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
1705 if (!(win->style & WS_VISIBLE))
1707 /* clear the update region since the window is no longer visible */
1708 validate_whole_window( win );
1709 validate_children( win );
1710 goto done;
1713 /* crop update region to the new window rect */
1715 if (win->update_region)
1717 if (get_window_visible_rect( win, &rect, 1 ))
1719 struct region *tmp = create_empty_region();
1720 if (tmp)
1722 set_region_rect( tmp, &rect );
1723 if (intersect_region( tmp, win->update_region, tmp ))
1724 set_update_region( win, tmp );
1725 else
1726 free_region( tmp );
1729 else set_update_region( win, NULL ); /* visible rect is empty */
1732 /* crop children regions to the new window rect */
1734 if (get_window_visible_rect( win, &rect, 0 ))
1736 /* map to client coords */
1737 offset_rect( &rect, win->window_rect.left - win->client_rect.left,
1738 win->window_rect.top - win->client_rect.top );
1739 crop_children_update_region( win, &rect );
1741 else crop_children_update_region( win, NULL );
1743 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1745 /* expose the whole non-client area if it changed in any way */
1747 if (swp_flags & SWP_NOCOPYBITS)
1749 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1750 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1751 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
1752 client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
1754 else
1756 /* assume the bits have been moved to follow the window rect */
1757 int x_offset = window_rect->left - old_window_rect.left;
1758 int y_offset = window_rect->top - old_window_rect.top;
1759 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1760 window_rect->right - old_window_rect.right != x_offset ||
1761 window_rect->bottom - old_window_rect.bottom != y_offset ||
1762 visible_rect->left - old_visible_rect.left != x_offset ||
1763 visible_rect->right - old_visible_rect.right != x_offset ||
1764 visible_rect->top - old_visible_rect.top != y_offset ||
1765 visible_rect->bottom - old_visible_rect.bottom != y_offset);
1766 client_changed = (client_rect->left - old_client_rect.left != x_offset ||
1767 client_rect->right - old_client_rect.right != x_offset ||
1768 client_rect->top - old_client_rect.top != y_offset ||
1769 client_rect->bottom - old_client_rect.bottom != y_offset ||
1770 !valid_rects ||
1771 memcmp( &valid_rects[0], client_rect, sizeof(*client_rect) ));
1774 if (frame_changed || client_changed)
1776 struct region *win_rgn = old_vis_rgn; /* reuse previous region */
1778 set_region_rect( win_rgn, window_rect );
1779 if (valid_rects)
1781 /* subtract the valid portion of client rect from the total region */
1782 struct region *tmp = create_empty_region();
1783 if (tmp)
1785 set_region_rect( tmp, &valid_rects[0] );
1786 /* subtract update region since invalid parts of the valid rect won't be copied */
1787 if (win->update_region)
1789 offset_region( tmp, -window_rect->left, -window_rect->top );
1790 subtract_region( tmp, tmp, win->update_region );
1791 offset_region( tmp, window_rect->left, window_rect->top );
1793 if (subtract_region( tmp, win_rgn, tmp )) win_rgn = tmp;
1794 else free_region( tmp );
1797 if (!is_desktop_window(win))
1798 offset_region( win_rgn, -client_rect->left, -client_rect->top );
1799 if (exposed_rgn)
1801 union_region( exposed_rgn, exposed_rgn, win_rgn );
1802 if (win_rgn != old_vis_rgn) free_region( win_rgn );
1804 else
1806 exposed_rgn = win_rgn;
1807 if (win_rgn == old_vis_rgn) old_vis_rgn = NULL;
1811 if (exposed_rgn)
1812 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1814 done:
1815 if (old_vis_rgn) free_region( old_vis_rgn );
1816 if (exposed_rgn) free_region( exposed_rgn );
1817 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1821 /* set the window region, updating the update region if necessary */
1822 static void set_window_region( struct window *win, struct region *region, int redraw )
1824 struct region *old_vis_rgn = NULL, *exposed_rgn;
1826 /* no need to redraw if window is not visible */
1827 if (redraw && !is_visible( win )) redraw = 0;
1829 if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
1831 if (win->win_region) free_region( win->win_region );
1832 win->win_region = region;
1834 /* expose anything revealed by the change */
1835 if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
1837 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1838 free_region( exposed_rgn );
1841 if (old_vis_rgn) free_region( old_vis_rgn );
1842 clear_error(); /* we ignore out of memory errors since the region has been set */
1846 /* destroy a window */
1847 void destroy_window( struct window *win )
1849 /* hide the window */
1850 if (is_visible(win))
1852 struct region *vis_rgn = get_visible_region( win, DCX_WINDOW );
1853 win->style &= ~WS_VISIBLE;
1854 if (vis_rgn)
1856 struct region *exposed_rgn = expose_window( win, &win->window_rect, vis_rgn );
1857 if (exposed_rgn) free_region( exposed_rgn );
1858 free_region( vis_rgn );
1860 validate_whole_window( win );
1861 validate_children( win );
1864 /* destroy all children */
1865 while (!list_empty(&win->children))
1866 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
1867 while (!list_empty(&win->unlinked))
1868 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
1870 /* reset global window pointers, if the corresponding window is destroyed */
1871 if (win == shell_window) shell_window = NULL;
1872 if (win == shell_listview) shell_listview = NULL;
1873 if (win == progman_window) progman_window = NULL;
1874 if (win == taskman_window) taskman_window = NULL;
1875 free_hotkeys( win->desktop, win->handle );
1876 cleanup_clipboard_window( win->desktop, win->handle );
1877 free_user_handle( win->handle );
1878 destroy_properties( win );
1879 list_remove( &win->entry );
1880 if (is_desktop_window(win))
1882 struct desktop *desktop = win->desktop;
1883 assert( desktop->top_window == win || desktop->msg_window == win );
1884 if (desktop->top_window == win) desktop->top_window = NULL;
1885 else desktop->msg_window = NULL;
1887 else if (is_desktop_window( win->parent ))
1889 post_message( win->parent->handle, WM_PARENTNOTIFY, WM_DESTROY, win->handle );
1892 detach_window_thread( win );
1893 if (win->win_region) free_region( win->win_region );
1894 if (win->update_region) free_region( win->update_region );
1895 if (win->class) release_class( win->class );
1896 free( win->text );
1897 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
1898 free( win );
1902 /* create a window */
1903 DECL_HANDLER(create_window)
1905 struct window *win, *parent = NULL, *owner = NULL;
1906 struct unicode_str cls_name = get_req_unicode_str();
1907 atom_t atom;
1909 reply->handle = 0;
1910 if (req->parent && !(parent = get_window( req->parent ))) return;
1912 if (req->owner)
1914 if (!(owner = get_window( req->owner ))) return;
1915 if (is_desktop_window(owner)) owner = NULL;
1916 else if (parent && !is_desktop_window(parent))
1918 /* an owned window must be created as top-level */
1919 set_error( STATUS_ACCESS_DENIED );
1920 return;
1922 else /* owner must be a top-level window */
1923 while ((owner->style & (WS_POPUP|WS_CHILD)) == WS_CHILD && !is_desktop_window(owner->parent))
1924 owner = owner->parent;
1927 atom = cls_name.len ? find_global_atom( NULL, &cls_name ) : req->atom;
1929 if (!(win = create_window( parent, owner, atom, req->instance ))) return;
1931 if (parent && !is_desktop_window( parent ))
1933 win->dpi_awareness = parent->dpi_awareness;
1934 win->dpi = parent->dpi;
1936 else if (!parent || req->awareness != DPI_AWARENESS_PER_MONITOR_AWARE)
1938 win->dpi_awareness = req->awareness;
1939 win->dpi = req->dpi;
1942 reply->handle = win->handle;
1943 reply->parent = win->parent ? win->parent->handle : 0;
1944 reply->owner = win->owner;
1945 reply->extra = win->nb_extra_bytes;
1946 reply->dpi = win->dpi;
1947 reply->awareness = win->dpi_awareness;
1948 reply->class_ptr = get_class_client_ptr( win->class );
1952 /* set the parent of a window */
1953 DECL_HANDLER(set_parent)
1955 struct window *win, *parent = NULL;
1957 if (!(win = get_window( req->handle ))) return;
1958 if (req->parent && !(parent = get_window( req->parent ))) return;
1960 if (is_desktop_window(win))
1962 set_error( STATUS_INVALID_PARAMETER );
1963 return;
1965 reply->old_parent = win->parent->handle;
1966 reply->full_parent = parent ? parent->handle : 0;
1967 set_parent_window( win, parent );
1968 reply->dpi = win->dpi;
1969 reply->awareness = win->dpi_awareness;
1973 /* destroy a window */
1974 DECL_HANDLER(destroy_window)
1976 struct window *win = get_window( req->handle );
1977 if (win)
1979 if (!is_desktop_window(win)) destroy_window( win );
1980 else if (win->thread == current) detach_window_thread( win );
1981 else set_error( STATUS_ACCESS_DENIED );
1986 /* retrieve the desktop window for the current thread */
1987 DECL_HANDLER(get_desktop_window)
1989 struct desktop *desktop = get_thread_desktop( current, 0 );
1990 int force;
1992 if (!desktop) return;
1994 /* if winstation is invisible, then avoid roundtrip */
1995 force = req->force || !(desktop->winstation->flags & WSF_VISIBLE);
1997 if (!desktop->top_window && force) /* create it */
1999 if ((desktop->top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
2001 detach_window_thread( desktop->top_window );
2002 desktop->top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
2006 if (!desktop->msg_window && force) /* create it */
2008 static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
2009 static const struct unicode_str name = { messageW, sizeof(messageW) };
2010 atom_t atom = add_global_atom( NULL, &name );
2011 if (atom && (desktop->msg_window = create_window( NULL, NULL, atom, 0 )))
2013 detach_window_thread( desktop->msg_window );
2014 desktop->msg_window->style = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
2018 reply->top_window = desktop->top_window ? desktop->top_window->handle : 0;
2019 reply->msg_window = desktop->msg_window ? desktop->msg_window->handle : 0;
2020 release_object( desktop );
2024 /* set a window owner */
2025 DECL_HANDLER(set_window_owner)
2027 struct window *win = get_window( req->handle );
2028 struct window *owner = NULL, *ptr;
2030 if (!win) return;
2031 if (req->owner && !(owner = get_window( req->owner ))) return;
2032 if (is_desktop_window(win))
2034 set_error( STATUS_ACCESS_DENIED );
2035 return;
2038 /* make sure owner is not a successor of window */
2039 for (ptr = owner; ptr; ptr = ptr->owner ? get_window( ptr->owner ) : NULL)
2041 if (ptr == win)
2043 set_error( STATUS_INVALID_PARAMETER );
2044 return;
2048 reply->prev_owner = win->owner;
2049 reply->full_owner = win->owner = owner ? owner->handle : 0;
2053 /* get information from a window handle */
2054 DECL_HANDLER(get_window_info)
2056 struct window *win = get_window( req->handle );
2058 if (!win) return;
2060 reply->full_handle = win->handle;
2061 reply->last_active = win->handle;
2062 reply->is_unicode = win->is_unicode;
2063 reply->awareness = win->dpi_awareness;
2064 reply->dpi = win->dpi ? win->dpi : get_monitor_dpi( win );
2065 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
2066 if (win->thread)
2068 reply->tid = get_thread_id( win->thread );
2069 reply->pid = get_process_id( win->thread->process );
2070 reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
2075 /* set some information in a window */
2076 DECL_HANDLER(set_window_info)
2078 struct window *win = get_window( req->handle );
2080 if (!win) return;
2081 if (req->flags && is_desktop_window(win) && win->thread != current)
2083 set_error( STATUS_ACCESS_DENIED );
2084 return;
2086 if (req->extra_size > sizeof(req->extra_value) ||
2087 req->extra_offset < -1 ||
2088 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
2090 set_win32_error( ERROR_INVALID_INDEX );
2091 return;
2093 if (req->extra_offset != -1)
2095 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
2097 else if (req->flags & SET_WIN_EXTRA)
2099 set_win32_error( ERROR_INVALID_INDEX );
2100 return;
2102 reply->old_style = win->style;
2103 reply->old_ex_style = win->ex_style;
2104 reply->old_id = win->id;
2105 reply->old_instance = win->instance;
2106 reply->old_user_data = win->user_data;
2107 if (req->flags & SET_WIN_STYLE) win->style = req->style;
2108 if (req->flags & SET_WIN_EXSTYLE)
2110 /* WS_EX_TOPMOST can only be changed for unlinked windows */
2111 if (!win->is_linked) win->ex_style = req->ex_style;
2112 else win->ex_style = (req->ex_style & ~WS_EX_TOPMOST) | (win->ex_style & WS_EX_TOPMOST);
2113 if (!(win->ex_style & WS_EX_LAYERED)) win->is_layered = 0;
2115 if (req->flags & SET_WIN_ID) win->id = req->id;
2116 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
2117 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
2118 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
2119 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
2120 &req->extra_value, req->extra_size );
2122 /* changing window style triggers a non-client paint */
2123 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
2127 /* get a list of the window parents, up to the root of the tree */
2128 DECL_HANDLER(get_window_parents)
2130 struct window *ptr, *win = get_window( req->handle );
2131 int total = 0;
2132 user_handle_t *data;
2133 data_size_t len;
2135 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
2137 reply->count = total;
2138 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2139 if (len && ((data = set_reply_data_size( len ))))
2141 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
2142 *data++ = ptr->handle;
2147 /* get a list of the window children */
2148 DECL_HANDLER(get_window_children)
2150 struct window *parent = NULL;
2151 unsigned int total;
2152 user_handle_t *data;
2153 data_size_t len;
2154 struct unicode_str cls_name = get_req_unicode_str();
2155 atom_t atom = req->atom;
2156 struct desktop *desktop = NULL;
2158 if (cls_name.len && !(atom = find_global_atom( NULL, &cls_name ))) return;
2160 if (req->desktop)
2162 if (!(desktop = get_desktop_obj( current->process, req->desktop, DESKTOP_ENUMERATE ))) return;
2163 parent = desktop->top_window;
2165 else
2167 if (req->parent && !(parent = get_window( req->parent ))) return;
2168 if (!parent && !(desktop = get_thread_desktop( current, 0 ))) return;
2171 if (parent)
2172 total = get_children_windows( parent, atom, req->tid, NULL, 0 );
2173 else
2174 total = get_children_windows( desktop->top_window, atom, req->tid, NULL, 0 ) +
2175 get_children_windows( desktop->msg_window, atom, req->tid, NULL, 0 );
2177 reply->count = total;
2178 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2179 if (len && ((data = set_reply_data_size( len ))))
2181 if (parent) get_children_windows( parent, atom, req->tid, data, len / sizeof(user_handle_t) );
2182 else
2184 total = get_children_windows( desktop->top_window, atom, req->tid,
2185 data, len / sizeof(user_handle_t) );
2186 data += total;
2187 len -= total * sizeof(user_handle_t);
2188 if (len >= sizeof(user_handle_t))
2189 get_children_windows( desktop->msg_window, atom, req->tid,
2190 data, len / sizeof(user_handle_t) );
2193 if (desktop) release_object( desktop );
2197 /* get a list of the window children that contain a given point */
2198 DECL_HANDLER(get_window_children_from_point)
2200 struct user_handle_array array;
2201 struct window *parent = get_window( req->parent );
2202 data_size_t len;
2204 if (!parent) return;
2206 array.handles = NULL;
2207 array.count = 0;
2208 array.total = 0;
2209 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
2211 reply->count = array.count;
2212 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
2213 if (len) set_reply_data_ptr( array.handles, len );
2214 else free( array.handles );
2218 /* get window tree information from a window handle */
2219 DECL_HANDLER(get_window_tree)
2221 struct window *ptr, *win = get_window( req->handle );
2223 if (!win) return;
2225 reply->parent = 0;
2226 reply->owner = 0;
2227 reply->next_sibling = 0;
2228 reply->prev_sibling = 0;
2229 reply->first_sibling = 0;
2230 reply->last_sibling = 0;
2231 reply->first_child = 0;
2232 reply->last_child = 0;
2234 if (win->parent)
2236 struct window *parent = win->parent;
2237 reply->parent = parent->handle;
2238 reply->owner = win->owner;
2239 if (win->is_linked)
2241 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
2242 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
2244 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
2245 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
2247 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
2248 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
2252 /* set the position and Z order of a window */
2253 DECL_HANDLER(set_window_pos)
2255 rectangle_t window_rect, client_rect, visible_rect;
2256 struct window *previous = NULL;
2257 struct window *top, *win = get_window( req->handle );
2258 unsigned int flags = req->swp_flags;
2260 if (!win) return;
2261 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
2263 if (!(flags & SWP_NOZORDER))
2265 switch ((int)req->previous)
2267 case 0: /* HWND_TOP */
2268 previous = WINPTR_TOP;
2269 break;
2270 case 1: /* HWND_BOTTOM */
2271 previous = WINPTR_BOTTOM;
2272 break;
2273 case -1: /* HWND_TOPMOST */
2274 previous = WINPTR_TOPMOST;
2275 break;
2276 case -2: /* HWND_NOTOPMOST */
2277 previous = WINPTR_NOTOPMOST;
2278 break;
2279 default:
2280 if (!(previous = get_window( req->previous ))) return;
2281 /* previous must be a sibling */
2282 if (previous->parent != win->parent)
2284 set_error( STATUS_INVALID_PARAMETER );
2285 return;
2287 break;
2289 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
2292 /* windows that use UpdateLayeredWindow don't trigger repaints */
2293 if ((win->ex_style & WS_EX_LAYERED) && !win->is_layered) flags |= SWP_NOREDRAW;
2295 /* window rectangle must be ordered properly */
2296 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
2298 set_error( STATUS_INVALID_PARAMETER );
2299 return;
2302 window_rect = visible_rect = req->window;
2303 client_rect = req->client;
2304 if (get_req_data_size() >= sizeof(rectangle_t))
2305 memcpy( &visible_rect, get_req_data(), sizeof(rectangle_t) );
2306 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2308 mirror_rect( &win->parent->client_rect, &window_rect );
2309 mirror_rect( &win->parent->client_rect, &visible_rect );
2310 mirror_rect( &win->parent->client_rect, &client_rect );
2313 win->paint_flags = (win->paint_flags & ~PAINT_CLIENT_FLAGS) | (req->paint_flags & PAINT_CLIENT_FLAGS);
2314 if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
2316 if (get_req_data_size() >= 3 * sizeof(rectangle_t))
2318 rectangle_t valid_rects[2];
2319 memcpy( valid_rects, (const rectangle_t *)get_req_data() + 1, 2 * sizeof(rectangle_t) );
2320 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2322 mirror_rect( &win->parent->client_rect, &valid_rects[0] );
2323 mirror_rect( &win->parent->client_rect, &valid_rects[1] );
2325 set_window_pos( win, previous, flags, &window_rect, &client_rect, &visible_rect, valid_rects );
2327 else set_window_pos( win, previous, flags, &window_rect, &client_rect, &visible_rect, NULL );
2329 reply->new_style = win->style;
2330 reply->new_ex_style = win->ex_style;
2332 top = get_top_clipping_window( win );
2333 if (is_visible( top ) && (top->paint_flags & PAINT_HAS_SURFACE))
2335 reply->surface_win = top->handle;
2336 reply->needs_update = !!(top->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD));
2341 /* get the window and client rectangles of a window */
2342 DECL_HANDLER(get_window_rectangles)
2344 struct window *win = get_window( req->handle );
2346 if (!win) return;
2348 reply->window = win->window_rect;
2349 reply->visible = win->visible_rect;
2350 reply->client = win->client_rect;
2352 switch (req->relative)
2354 case COORDS_CLIENT:
2355 offset_rect( &reply->window, -win->client_rect.left, -win->client_rect.top );
2356 offset_rect( &reply->visible, -win->client_rect.left, -win->client_rect.top );
2357 offset_rect( &reply->client, -win->client_rect.left, -win->client_rect.top );
2358 if (win->ex_style & WS_EX_LAYOUTRTL)
2360 mirror_rect( &win->client_rect, &reply->window );
2361 mirror_rect( &win->client_rect, &reply->visible );
2363 break;
2364 case COORDS_WINDOW:
2365 offset_rect( &reply->window, -win->window_rect.left, -win->window_rect.top );
2366 offset_rect( &reply->visible, -win->window_rect.left, -win->window_rect.top );
2367 offset_rect( &reply->client, -win->window_rect.left, -win->window_rect.top );
2368 if (win->ex_style & WS_EX_LAYOUTRTL)
2370 mirror_rect( &win->window_rect, &reply->visible );
2371 mirror_rect( &win->window_rect, &reply->client );
2373 break;
2374 case COORDS_PARENT:
2375 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2377 mirror_rect( &win->parent->client_rect, &reply->window );
2378 mirror_rect( &win->parent->client_rect, &reply->visible );
2379 mirror_rect( &win->parent->client_rect, &reply->client );
2381 break;
2382 case COORDS_SCREEN:
2383 client_to_screen_rect( win->parent, &reply->window );
2384 client_to_screen_rect( win->parent, &reply->visible );
2385 client_to_screen_rect( win->parent, &reply->client );
2386 break;
2387 default:
2388 set_error( STATUS_INVALID_PARAMETER );
2389 break;
2394 /* get the window text */
2395 DECL_HANDLER(get_window_text)
2397 struct window *win = get_window( req->handle );
2399 if (win && win->text)
2401 reply->length = strlenW( win->text );
2402 set_reply_data( win->text, min( reply->length * sizeof(WCHAR), get_reply_max_size() ));
2407 /* set the window text */
2408 DECL_HANDLER(set_window_text)
2410 struct window *win = get_window( req->handle );
2412 if (win)
2414 WCHAR *text = NULL;
2415 data_size_t len = get_req_data_size() / sizeof(WCHAR);
2416 if (len)
2418 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
2419 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
2420 text[len] = 0;
2422 free( win->text );
2423 win->text = text;
2428 /* get the coordinates offset between two windows */
2429 DECL_HANDLER(get_windows_offset)
2431 struct window *win;
2432 int mirror_from = 0, mirror_to = 0;
2434 reply->x = reply->y = 0;
2435 if (req->from)
2437 if (!(win = get_window( req->from ))) return;
2438 if (win->ex_style & WS_EX_LAYOUTRTL)
2440 mirror_from = 1;
2441 reply->x += win->client_rect.right - win->client_rect.left;
2443 while (win && !is_desktop_window(win))
2445 reply->x += win->client_rect.left;
2446 reply->y += win->client_rect.top;
2447 win = win->parent;
2450 if (req->to)
2452 if (!(win = get_window( req->to ))) return;
2453 if (win->ex_style & WS_EX_LAYOUTRTL)
2455 mirror_to = 1;
2456 reply->x -= win->client_rect.right - win->client_rect.left;
2458 while (win && !is_desktop_window(win))
2460 reply->x -= win->client_rect.left;
2461 reply->y -= win->client_rect.top;
2462 win = win->parent;
2465 if (mirror_from) reply->x = -reply->x;
2466 reply->mirror = mirror_from ^ mirror_to;
2470 /* get the visible region of a window */
2471 DECL_HANDLER(get_visible_region)
2473 struct region *region;
2474 struct window *top, *win = get_window( req->window );
2476 if (!win) return;
2478 top = get_top_clipping_window( win );
2479 if ((region = get_visible_region( win, req->flags )))
2481 rectangle_t *data;
2482 map_win_region_to_screen( win, region );
2483 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2484 if (data) set_reply_data_ptr( data, reply->total_size );
2486 reply->top_win = top->handle;
2487 reply->top_rect = top->visible_rect;
2489 if (!is_desktop_window(win))
2491 reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
2492 client_to_screen_rect( top->parent, &reply->top_rect );
2493 client_to_screen_rect( win->parent, &reply->win_rect );
2495 else
2497 reply->win_rect.left = 0;
2498 reply->win_rect.top = 0;
2499 reply->win_rect.right = win->client_rect.right - win->client_rect.left;
2500 reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
2502 reply->paint_flags = win->paint_flags & PAINT_CLIENT_FLAGS;
2506 /* get the surface visible region of a window */
2507 DECL_HANDLER(get_surface_region)
2509 struct region *region;
2510 struct window *win = get_window( req->window );
2512 if (!win || !is_visible( win )) return;
2514 if ((region = get_surface_region( win )))
2516 rectangle_t *data;
2517 if (win->parent) map_win_region_to_screen( win->parent, region );
2518 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2519 if (data) set_reply_data_ptr( data, reply->total_size );
2521 reply->visible_rect = win->visible_rect;
2522 if (win->parent) client_to_screen_rect( win->parent, &reply->visible_rect );
2526 /* get the window region */
2527 DECL_HANDLER(get_window_region)
2529 rectangle_t *data;
2530 struct window *win = get_window( req->window );
2532 if (!win) return;
2533 if (!win->win_region) return;
2535 if (win->ex_style & WS_EX_LAYOUTRTL)
2537 struct region *region = create_empty_region();
2539 if (!region) return;
2540 if (!copy_region( region, win->win_region ))
2542 free_region( region );
2543 return;
2545 mirror_region( &win->window_rect, region );
2546 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2548 else data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
2550 if (data) set_reply_data_ptr( data, reply->total_size );
2554 /* set the window region */
2555 DECL_HANDLER(set_window_region)
2557 struct region *region = NULL;
2558 struct window *win = get_window( req->window );
2560 if (!win) return;
2562 if (get_req_data_size()) /* no data means remove the region completely */
2564 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2565 return;
2566 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->window_rect, region );
2568 set_window_region( win, region, req->redraw );
2572 /* get a window update region */
2573 DECL_HANDLER(get_update_region)
2575 rectangle_t *data;
2576 unsigned int flags = req->flags;
2577 struct window *from_child = NULL;
2578 struct window *win = get_window( req->window );
2580 reply->flags = 0;
2581 if (!win) return;
2583 if (req->from_child)
2585 struct window *ptr;
2587 if (!(from_child = get_window( req->from_child ))) return;
2589 /* make sure from_child is a child of win */
2590 ptr = from_child;
2591 while (ptr && ptr != win) ptr = ptr->parent;
2592 if (!ptr)
2594 set_error( STATUS_INVALID_PARAMETER );
2595 return;
2599 if (flags & UPDATE_DELAYED_ERASE) /* this means that the previous call didn't erase */
2601 if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
2602 else win->paint_flags |= PAINT_DELAYED_ERASE;
2605 reply->flags = get_window_update_flags( win, from_child, flags, &win );
2606 reply->child = win->handle;
2608 if (flags & UPDATE_NOREGION) return;
2610 if (win->update_region)
2612 /* convert update region to screen coordinates */
2613 struct region *region = create_empty_region();
2615 if (!region) return;
2616 if (!copy_region( region, win->update_region ))
2618 free_region( region );
2619 return;
2621 if ((flags & UPDATE_CLIPCHILDREN) && (win->style & WS_CLIPCHILDREN))
2622 clip_children( win, NULL, region, win->client_rect.left - win->window_rect.left,
2623 win->client_rect.top - win->window_rect.top );
2624 map_win_region_to_screen( win, region );
2625 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
2626 &reply->total_size ))) return;
2627 set_reply_data_ptr( data, reply->total_size );
2630 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
2632 validate_parents( win );
2633 validate_whole_window( win );
2635 else
2637 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
2638 if (reply->flags & UPDATE_ERASE)
2640 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
2641 /* desktop window only gets erased, not repainted */
2642 if (is_desktop_window(win)) validate_whole_window( win );
2648 /* update the z order of a window so that a given rectangle is fully visible */
2649 DECL_HANDLER(update_window_zorder)
2651 rectangle_t tmp, rect = req->rect;
2652 struct window *ptr, *win = get_window( req->window );
2654 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
2655 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &rect );
2656 offset_rect( &rect, win->client_rect.left, win->client_rect.top );
2658 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2660 if (ptr == win) break;
2661 if (!(ptr->style & WS_VISIBLE)) continue;
2662 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2663 if (ptr->is_layered && (ptr->layered_flags & LWA_COLORKEY)) continue;
2664 if (!intersect_rect( &tmp, &ptr->visible_rect, &rect )) continue;
2665 if (ptr->win_region)
2667 tmp = rect;
2668 offset_rect( &tmp, -ptr->window_rect.left, -ptr->window_rect.top );
2669 if (!rect_in_region( ptr->win_region, &tmp )) continue;
2671 /* found a window obscuring the rectangle, now move win above this one */
2672 /* making sure to not violate the topmost rule */
2673 if (!(ptr->ex_style & WS_EX_TOPMOST) || (win->ex_style & WS_EX_TOPMOST))
2675 list_remove( &win->entry );
2676 list_add_before( &ptr->entry, &win->entry );
2678 break;
2683 /* mark parts of a window as needing a redraw */
2684 DECL_HANDLER(redraw_window)
2686 struct region *region = NULL;
2687 struct window *win = get_window( req->window );
2689 if (!win) return;
2690 if (!is_visible( win )) return; /* nothing to do */
2692 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
2694 if (get_req_data_size()) /* no data means whole rectangle */
2696 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2697 return;
2698 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->client_rect, region );
2702 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
2703 req->flags );
2704 if (region) free_region( region );
2708 /* set a window property */
2709 DECL_HANDLER(set_window_property)
2711 struct unicode_str name = get_req_unicode_str();
2712 struct window *win = get_window( req->window );
2714 if (!win) return;
2716 if (name.len)
2718 atom_t atom = add_global_atom( NULL, &name );
2719 if (atom)
2721 set_property( win, atom, req->data, PROP_TYPE_STRING );
2722 release_global_atom( NULL, atom );
2725 else set_property( win, req->atom, req->data, PROP_TYPE_ATOM );
2729 /* remove a window property */
2730 DECL_HANDLER(remove_window_property)
2732 struct unicode_str name = get_req_unicode_str();
2733 struct window *win = get_window( req->window );
2735 if (win)
2737 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2738 if (atom) reply->data = remove_property( win, atom );
2743 /* get a window property */
2744 DECL_HANDLER(get_window_property)
2746 struct unicode_str name = get_req_unicode_str();
2747 struct window *win = get_window( req->window );
2749 if (win)
2751 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2752 if (atom) reply->data = get_property( win, atom );
2757 /* get the list of properties of a window */
2758 DECL_HANDLER(get_window_properties)
2760 property_data_t *data;
2761 int i, count, max = get_reply_max_size() / sizeof(*data);
2762 struct window *win = get_window( req->window );
2764 reply->total = 0;
2765 if (!win) return;
2767 for (i = count = 0; i < win->prop_inuse; i++)
2768 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2769 reply->total = count;
2771 if (count > max) count = max;
2772 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2774 for (i = 0; i < win->prop_inuse && count; i++)
2776 if (win->properties[i].type == PROP_TYPE_FREE) continue;
2777 data->atom = win->properties[i].atom;
2778 data->string = (win->properties[i].type == PROP_TYPE_STRING);
2779 data->data = win->properties[i].data;
2780 data++;
2781 count--;
2786 /* get the new window pointer for a global window, checking permissions */
2787 /* helper for set_global_windows request */
2788 static int get_new_global_window( struct window **win, user_handle_t handle )
2790 if (!handle)
2792 *win = NULL;
2793 return 1;
2795 else if (*win)
2797 set_error( STATUS_ACCESS_DENIED );
2798 return 0;
2800 *win = get_window( handle );
2801 return (*win != NULL);
2804 /* Set/get the global windows */
2805 DECL_HANDLER(set_global_windows)
2807 struct window *new_shell_window = shell_window;
2808 struct window *new_shell_listview = shell_listview;
2809 struct window *new_progman_window = progman_window;
2810 struct window *new_taskman_window = taskman_window;
2812 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2813 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2814 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2815 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2817 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2819 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2820 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2822 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2824 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2826 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2828 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2830 shell_window = new_shell_window;
2831 shell_listview = new_shell_listview;
2832 progman_window = new_progman_window;
2833 taskman_window = new_taskman_window;
2836 /* retrieve layered info for a window */
2837 DECL_HANDLER(get_window_layered_info)
2839 struct window *win = get_window( req->handle );
2841 if (!win) return;
2843 if (win->is_layered)
2845 reply->color_key = win->color_key;
2846 reply->alpha = win->alpha;
2847 reply->flags = win->layered_flags;
2849 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2853 /* set layered info for a window */
2854 DECL_HANDLER(set_window_layered_info)
2856 struct window *win = get_window( req->handle );
2858 if (!win) return;
2860 if (win->ex_style & WS_EX_LAYERED)
2862 int was_layered = win->is_layered;
2864 if (req->flags & LWA_ALPHA) win->alpha = req->alpha;
2865 else if (!win->is_layered) win->alpha = 0; /* alpha init value is 0 */
2867 win->color_key = req->color_key;
2868 win->layered_flags = req->flags;
2869 win->is_layered = 1;
2870 /* repaint since we know now it's not going to use UpdateLayeredWindow */
2871 if (!was_layered) redraw_window( win, 0, 1, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
2873 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );