ole32: Cleanup IMultiQI interface of proxy manager.
[wine.git] / server / window.c
blob2b4bc50acd5c40dcf2505ae6368ec918a91ed76f
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 lparam_t user_data; /* user-specific data */
87 WCHAR *text; /* window caption text */
88 unsigned int paint_flags; /* various painting flags */
89 int prop_inuse; /* number of in-use window properties */
90 int prop_alloc; /* number of allocated window properties */
91 struct property *properties; /* window properties array */
92 int nb_extra_bytes; /* number of extra bytes */
93 char extra_bytes[1]; /* extra bytes storage */
96 /* flags that can be set by the client */
97 #define PAINT_HAS_SURFACE SET_WINPOS_PAINT_SURFACE
98 #define PAINT_HAS_PIXEL_FORMAT SET_WINPOS_PIXEL_FORMAT
99 #define PAINT_CLIENT_FLAGS (PAINT_HAS_SURFACE | PAINT_HAS_PIXEL_FORMAT)
100 /* flags only manipulated by the server */
101 #define PAINT_INTERNAL 0x0010 /* internal WM_PAINT pending */
102 #define PAINT_ERASE 0x0020 /* needs WM_ERASEBKGND */
103 #define PAINT_NONCLIENT 0x0040 /* needs WM_NCPAINT */
104 #define PAINT_DELAYED_ERASE 0x0080 /* still needs erase after WM_ERASEBKGND */
105 #define PAINT_PIXEL_FORMAT_CHILD 0x0100 /* at least one child has a custom pixel format */
107 /* growable array of user handles */
108 struct user_handle_array
110 user_handle_t *handles;
111 int count;
112 int total;
115 /* global window pointers */
116 static struct window *shell_window;
117 static struct window *shell_listview;
118 static struct window *progman_window;
119 static struct window *taskman_window;
121 /* magic HWND_TOP etc. pointers */
122 #define WINPTR_TOP ((struct window *)1L)
123 #define WINPTR_BOTTOM ((struct window *)2L)
124 #define WINPTR_TOPMOST ((struct window *)3L)
125 #define WINPTR_NOTOPMOST ((struct window *)4L)
127 /* retrieve a pointer to a window from its handle */
128 static inline struct window *get_window( user_handle_t handle )
130 struct window *ret = get_user_object( handle, USER_WINDOW );
131 if (!ret) set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
132 return ret;
135 /* check if window is the desktop */
136 static inline int is_desktop_window( const struct window *win )
138 return !win->parent; /* only desktop windows have no parent */
141 /* get next window in Z-order list */
142 static inline struct window *get_next_window( struct window *win )
144 struct list *ptr = list_next( &win->parent->children, &win->entry );
145 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
148 /* get previous window in Z-order list */
149 static inline struct window *get_prev_window( struct window *win )
151 struct list *ptr = list_prev( &win->parent->children, &win->entry );
152 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
155 /* get first child in Z-order list */
156 static inline struct window *get_first_child( struct window *win )
158 struct list *ptr = list_head( &win->children );
159 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
162 /* get last child in Z-order list */
163 static inline struct window *get_last_child( struct window *win )
165 struct list *ptr = list_tail( &win->children );
166 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
169 /* set the PAINT_PIXEL_FORMAT_CHILD flag on all the parents */
170 /* note: we never reset the flag, it's just a heuristic */
171 static inline void update_pixel_format_flags( struct window *win )
173 for (win = win->parent; win && win->parent; win = win->parent)
174 win->paint_flags |= PAINT_PIXEL_FORMAT_CHILD;
177 /* link a window at the right place in the siblings list */
178 static void link_window( struct window *win, struct window *previous )
180 if (previous == WINPTR_NOTOPMOST)
182 if (!(win->ex_style & WS_EX_TOPMOST) && win->is_linked) return; /* nothing to do */
183 win->ex_style &= ~WS_EX_TOPMOST;
184 previous = WINPTR_TOP; /* fallback to the HWND_TOP case */
187 list_remove( &win->entry ); /* unlink it from the previous location */
189 if (previous == WINPTR_BOTTOM)
191 list_add_tail( &win->parent->children, &win->entry );
192 win->ex_style &= ~WS_EX_TOPMOST;
194 else if (previous == WINPTR_TOPMOST)
196 list_add_head( &win->parent->children, &win->entry );
197 win->ex_style |= WS_EX_TOPMOST;
199 else if (previous == WINPTR_TOP)
201 struct list *entry = win->parent->children.next;
202 if (!(win->ex_style & WS_EX_TOPMOST)) /* put it above the first non-topmost window */
204 while (entry != &win->parent->children)
206 struct window *next = LIST_ENTRY( entry, struct window, entry );
207 if (!(next->ex_style & WS_EX_TOPMOST)) break;
208 if (next->handle == win->owner) /* keep it above owner */
210 win->ex_style |= WS_EX_TOPMOST;
211 break;
213 entry = entry->next;
216 list_add_before( entry, &win->entry );
218 else
220 list_add_after( &previous->entry, &win->entry );
221 if (!(previous->ex_style & WS_EX_TOPMOST)) win->ex_style &= ~WS_EX_TOPMOST;
222 else
224 struct window *next = get_next_window( win );
225 if (next && (next->ex_style & WS_EX_TOPMOST)) win->ex_style |= WS_EX_TOPMOST;
229 win->is_linked = 1;
232 /* change the parent of a window (or unlink the window if the new parent is NULL) */
233 static int set_parent_window( struct window *win, struct window *parent )
235 struct window *ptr;
237 /* make sure parent is not a child of window */
238 for (ptr = parent; ptr; ptr = ptr->parent)
240 if (ptr == win)
242 set_error( STATUS_INVALID_PARAMETER );
243 return 0;
247 if (parent)
249 win->parent = parent;
250 link_window( win, WINPTR_TOP );
252 /* if parent belongs to a different thread and the window isn't */
253 /* top-level, attach the two threads */
254 if (parent->thread && parent->thread != win->thread && !is_desktop_window(parent))
255 attach_thread_input( win->thread, parent->thread );
257 if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
259 else /* move it to parent unlinked list */
261 list_remove( &win->entry ); /* unlink it from the previous location */
262 list_add_head( &win->parent->unlinked, &win->entry );
263 win->is_linked = 0;
265 return 1;
268 /* append a user handle to a handle array */
269 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
271 if (array->count >= array->total)
273 int new_total = max( array->total * 2, 32 );
274 user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
275 if (!new_array)
277 free( array->handles );
278 set_error( STATUS_NO_MEMORY );
279 return 0;
281 array->handles = new_array;
282 array->total = new_total;
284 array->handles[array->count++] = handle;
285 return 1;
288 /* set a window property */
289 static void set_property( struct window *win, atom_t atom, lparam_t data, enum property_type type )
291 int i, free = -1;
292 struct property *new_props;
294 /* check if it exists already */
295 for (i = 0; i < win->prop_inuse; i++)
297 if (win->properties[i].type == PROP_TYPE_FREE)
299 free = i;
300 continue;
302 if (win->properties[i].atom == atom)
304 win->properties[i].type = type;
305 win->properties[i].data = data;
306 return;
310 /* need to add an entry */
311 if (!grab_global_atom( NULL, atom )) return;
312 if (free == -1)
314 /* no free entry */
315 if (win->prop_inuse >= win->prop_alloc)
317 /* need to grow the array */
318 if (!(new_props = realloc( win->properties,
319 sizeof(*new_props) * (win->prop_alloc + 16) )))
321 set_error( STATUS_NO_MEMORY );
322 release_global_atom( NULL, atom );
323 return;
325 win->prop_alloc += 16;
326 win->properties = new_props;
328 free = win->prop_inuse++;
330 win->properties[free].atom = atom;
331 win->properties[free].type = type;
332 win->properties[free].data = data;
335 /* remove a window property */
336 static lparam_t remove_property( struct window *win, atom_t atom )
338 int i;
340 for (i = 0; i < win->prop_inuse; i++)
342 if (win->properties[i].type == PROP_TYPE_FREE) continue;
343 if (win->properties[i].atom == atom)
345 release_global_atom( NULL, atom );
346 win->properties[i].type = PROP_TYPE_FREE;
347 return win->properties[i].data;
350 /* FIXME: last error? */
351 return 0;
354 /* find a window property */
355 static lparam_t get_property( struct window *win, atom_t atom )
357 int i;
359 for (i = 0; i < win->prop_inuse; i++)
361 if (win->properties[i].type == PROP_TYPE_FREE) continue;
362 if (win->properties[i].atom == atom) return win->properties[i].data;
364 /* FIXME: last error? */
365 return 0;
368 /* destroy all properties of a window */
369 static inline void destroy_properties( struct window *win )
371 int i;
373 if (!win->properties) return;
374 for (i = 0; i < win->prop_inuse; i++)
376 if (win->properties[i].type == PROP_TYPE_FREE) continue;
377 release_global_atom( NULL, win->properties[i].atom );
379 free( win->properties );
382 /* detach a window from its owner thread but keep the window around */
383 static void detach_window_thread( struct window *win )
385 struct thread *thread = win->thread;
387 if (!thread) return;
388 if (thread->queue)
390 if (win->update_region) inc_queue_paint_count( thread, -1 );
391 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
392 queue_cleanup_window( thread, win->handle );
394 assert( thread->desktop_users > 0 );
395 thread->desktop_users--;
396 release_class( win->class );
397 win->class = NULL;
399 /* don't hold a reference to the desktop so that the desktop window can be */
400 /* destroyed when the desktop ref count reaches zero */
401 release_object( win->desktop );
402 win->thread = NULL;
405 /* get the process owning the top window of a given desktop */
406 struct process *get_top_window_owner( struct desktop *desktop )
408 struct window *win = desktop->top_window;
409 if (!win || !win->thread) return NULL;
410 return win->thread->process;
413 /* get the top window size of a given desktop */
414 void get_top_window_rectangle( struct desktop *desktop, rectangle_t *rect )
416 struct window *win = desktop->top_window;
417 if (!win) rect->left = rect->top = rect->right = rect->bottom = 0;
418 else *rect = win->window_rect;
421 /* post a message to the desktop window */
422 void post_desktop_message( struct desktop *desktop, unsigned int message,
423 lparam_t wparam, lparam_t lparam )
425 struct window *win = desktop->top_window;
426 if (win && win->thread) post_message( win->handle, message, wparam, lparam );
429 /* create a new window structure (note: the window is not linked in the window tree) */
430 static struct window *create_window( struct window *parent, struct window *owner,
431 atom_t atom, mod_handle_t instance )
433 static const rectangle_t empty_rect;
434 int extra_bytes;
435 struct window *win = NULL;
436 struct desktop *desktop;
437 struct window_class *class;
439 if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
441 if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
443 release_object( desktop );
444 return NULL;
447 if (!parent) /* null parent is only allowed for desktop or HWND_MESSAGE top window */
449 if (is_desktop_class( class ))
450 parent = desktop->top_window; /* use existing desktop if any */
451 else if (is_hwnd_message_class( class ))
452 /* use desktop window if message window is already created */
453 parent = desktop->msg_window ? desktop->top_window : NULL;
454 else if (!(parent = desktop->top_window)) /* must already have a desktop then */
456 set_error( STATUS_ACCESS_DENIED );
457 goto failed;
461 /* parent must be on the same desktop */
462 if (parent && parent->desktop != desktop)
464 set_error( STATUS_ACCESS_DENIED );
465 goto failed;
468 if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
469 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
471 win->parent = parent;
472 win->owner = owner ? owner->handle : 0;
473 win->thread = current;
474 win->desktop = desktop;
475 win->class = class;
476 win->atom = atom;
477 win->last_active = win->handle;
478 win->win_region = NULL;
479 win->update_region = NULL;
480 win->style = 0;
481 win->ex_style = 0;
482 win->id = 0;
483 win->instance = 0;
484 win->is_unicode = 1;
485 win->is_linked = 0;
486 win->is_layered = 0;
487 win->user_data = 0;
488 win->text = NULL;
489 win->paint_flags = 0;
490 win->prop_inuse = 0;
491 win->prop_alloc = 0;
492 win->properties = NULL;
493 win->nb_extra_bytes = extra_bytes;
494 win->window_rect = win->visible_rect = win->client_rect = empty_rect;
495 memset( win->extra_bytes, 0, extra_bytes );
496 list_init( &win->children );
497 list_init( &win->unlinked );
499 /* if parent belongs to a different thread and the window isn't */
500 /* top-level, attach the two threads */
501 if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
503 if (!attach_thread_input( current, parent->thread )) goto failed;
505 else /* otherwise just make sure that the thread has a message queue */
507 if (!current->queue && !init_thread_queue( current )) goto failed;
510 /* put it on parent unlinked list */
511 if (parent) list_add_head( &parent->unlinked, &win->entry );
512 else
514 list_init( &win->entry );
515 if (is_desktop_class( class ))
517 assert( !desktop->top_window );
518 desktop->top_window = win;
519 set_process_default_desktop( current->process, desktop, current->desktop );
521 else
523 assert( !desktop->msg_window );
524 desktop->msg_window = win;
528 current->desktop_users++;
529 return win;
531 failed:
532 if (win)
534 if (win->handle) free_user_handle( win->handle );
535 free( win );
537 release_object( desktop );
538 release_class( class );
539 return NULL;
542 /* destroy all windows belonging to a given thread */
543 void destroy_thread_windows( struct thread *thread )
545 user_handle_t handle = 0;
546 struct window *win;
548 while ((win = next_user_handle( &handle, USER_WINDOW )))
550 if (win->thread != thread) continue;
551 if (is_desktop_window( win )) detach_window_thread( win );
552 else destroy_window( win );
556 /* get the desktop window */
557 static struct window *get_desktop_window( struct thread *thread )
559 struct window *top_window;
560 struct desktop *desktop = get_thread_desktop( thread, 0 );
562 if (!desktop) return NULL;
563 top_window = desktop->top_window;
564 release_object( desktop );
565 return top_window;
568 /* check whether child is a descendant of parent */
569 int is_child_window( user_handle_t parent, user_handle_t child )
571 struct window *child_ptr = get_user_object( child, USER_WINDOW );
572 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
574 if (!child_ptr || !parent_ptr) return 0;
575 while (child_ptr->parent)
577 if (child_ptr->parent == parent_ptr) return 1;
578 child_ptr = child_ptr->parent;
580 return 0;
583 /* check whether window is a top-level window */
584 int is_top_level_window( user_handle_t window )
586 struct window *win = get_user_object( window, USER_WINDOW );
587 return (win && (is_desktop_window(win) || is_desktop_window(win->parent)));
590 /* make a window active if possible */
591 int make_window_active( user_handle_t window )
593 struct window *owner, *win = get_window( window );
595 if (!win) return 0;
597 /* set last active for window and its owners */
598 owner = win;
599 while (owner)
601 owner->last_active = win->handle;
602 owner = get_user_object( owner->owner, USER_WINDOW );
604 return 1;
607 /* increment (or decrement) the window paint count */
608 static inline void inc_window_paint_count( struct window *win, int incr )
610 if (win->thread) inc_queue_paint_count( win->thread, incr );
613 /* check if window and all its ancestors are visible */
614 static int is_visible( const struct window *win )
616 while (win)
618 if (!(win->style & WS_VISIBLE)) return 0;
619 win = win->parent;
620 /* if parent is minimized children are not visible */
621 if (win && (win->style & WS_MINIMIZE)) return 0;
623 return 1;
626 /* same as is_visible but takes a window handle */
627 int is_window_visible( user_handle_t window )
629 struct window *win = get_user_object( window, USER_WINDOW );
630 if (!win) return 0;
631 return is_visible( win );
634 int is_window_transparent( user_handle_t window )
636 struct window *win = get_user_object( window, USER_WINDOW );
637 if (!win) return 0;
638 return (win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT);
641 /* check if point is inside the window */
642 static inline int is_point_in_window( struct window *win, int x, int y )
644 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
645 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
646 return 0; /* disabled child */
647 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
648 return 0; /* transparent */
649 if (x < win->visible_rect.left || x >= win->visible_rect.right ||
650 y < win->visible_rect.top || y >= win->visible_rect.bottom)
651 return 0; /* not in window */
652 if (win->win_region &&
653 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
654 return 0; /* not in window region */
655 return 1;
658 /* fill an array with the handles of the children of a specified window */
659 static unsigned int get_children_windows( struct window *parent, atom_t atom, thread_id_t tid,
660 user_handle_t *handles, unsigned int max_count )
662 struct window *ptr;
663 unsigned int count = 0;
665 if (!parent) return 0;
667 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
669 if (atom && get_class_atom(ptr->class) != atom) continue;
670 if (tid && get_thread_id(ptr->thread) != tid) continue;
671 if (handles)
673 if (count >= max_count) break;
674 handles[count] = ptr->handle;
676 count++;
678 return count;
681 /* find child of 'parent' that contains the given point (in parent-relative coords) */
682 static struct window *child_window_from_point( struct window *parent, int x, int y )
684 struct window *ptr;
686 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
688 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
690 /* if window is minimized or disabled, return at once */
691 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
693 /* if point is not in client area, return at once */
694 if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
695 y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
696 return ptr;
698 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
700 return parent; /* not found any child */
703 /* find all children of 'parent' that contain the given point */
704 static int get_window_children_from_point( struct window *parent, int x, int y,
705 struct user_handle_array *array )
707 struct window *ptr;
709 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
711 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
713 /* if point is in client area, and window is not minimized or disabled, check children */
714 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
715 x >= ptr->client_rect.left && x < ptr->client_rect.right &&
716 y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
718 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
719 y - ptr->client_rect.top, array ))
720 return 0;
723 /* now add window to the array */
724 if (!add_handle_to_array( array, ptr->handle )) return 0;
726 return 1;
729 /* find window containing point (in absolute coords) */
730 user_handle_t window_from_point( struct desktop *desktop, int x, int y )
732 struct window *ret;
734 if (!desktop->top_window) return 0;
735 ret = child_window_from_point( desktop->top_window, x, y );
736 return ret->handle;
739 /* return list of all windows containing point (in absolute coords) */
740 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
742 struct window *ptr;
744 /* make point relative to top window */
745 for (ptr = top->parent; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
747 x -= ptr->client_rect.left;
748 y -= ptr->client_rect.top;
751 if (!is_point_in_window( top, x, y )) return 1;
753 /* if point is in client area, and window is not minimized or disabled, check children */
754 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
755 x >= top->client_rect.left && x < top->client_rect.right &&
756 y >= top->client_rect.top && y < top->client_rect.bottom)
758 if (!is_desktop_window(top))
760 x -= top->client_rect.left;
761 y -= top->client_rect.top;
763 if (!get_window_children_from_point( top, x, y, array )) return 0;
765 /* now add window to the array */
766 if (!add_handle_to_array( array, top->handle )) return 0;
767 return 1;
771 /* return the thread owning a window */
772 struct thread *get_window_thread( user_handle_t handle )
774 struct window *win = get_user_object( handle, USER_WINDOW );
775 if (!win || !win->thread) return NULL;
776 return (struct thread *)grab_object( win->thread );
780 /* check if any area of a window needs repainting */
781 static inline int win_needs_repaint( struct window *win )
783 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
787 /* find a child of the specified window that needs repainting */
788 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
790 struct window *ptr, *ret = NULL;
792 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
794 if (!(ptr->style & WS_VISIBLE)) continue;
795 if (ptr->thread == thread && win_needs_repaint( ptr ))
796 ret = ptr;
797 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
798 ret = find_child_to_repaint( ptr, thread );
799 if (ret) break;
802 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
804 /* transparent window, check for non-transparent sibling to paint first */
805 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
807 if (!(ptr->style & WS_VISIBLE)) continue;
808 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
809 if (ptr->thread != thread) continue;
810 if (win_needs_repaint( ptr )) return ptr;
813 return ret;
817 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
818 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
820 struct window *ptr, *win, *top_window = get_desktop_window( thread );
822 if (!top_window) return 0;
824 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
825 else win = find_child_to_repaint( top_window, thread );
827 if (win && parent)
829 /* check that it is a child of the specified parent */
830 for (ptr = win; ptr; ptr = ptr->parent)
831 if (ptr->handle == parent) break;
832 /* otherwise don't return any window, we don't repaint a child before its parent */
833 if (!ptr) win = NULL;
835 if (!win) return 0;
836 win->paint_flags &= ~PAINT_INTERNAL;
837 return win->handle;
841 /* intersect the window region with the specified region, relative to the window parent */
842 static struct region *intersect_window_region( struct region *region, struct window *win )
844 /* make region relative to window rect */
845 offset_region( region, -win->window_rect.left, -win->window_rect.top );
846 if (!intersect_region( region, region, win->win_region )) return NULL;
847 /* make region relative to parent again */
848 offset_region( region, win->window_rect.left, win->window_rect.top );
849 return region;
853 /* convert coordinates from client to screen coords */
854 static inline void client_to_screen( struct window *win, int *x, int *y )
856 for ( ; win && !is_desktop_window(win); win = win->parent)
858 *x += win->client_rect.left;
859 *y += win->client_rect.top;
863 /* convert coordinates from client to screen coords */
864 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
866 for ( ; win && !is_desktop_window(win); win = win->parent)
868 rect->left += win->client_rect.left;
869 rect->right += win->client_rect.left;
870 rect->top += win->client_rect.top;
871 rect->bottom += win->client_rect.top;
875 /* map the region from window to screen coordinates */
876 static inline void map_win_region_to_screen( struct window *win, struct region *region )
878 if (!is_desktop_window(win))
880 int x = win->window_rect.left;
881 int y = win->window_rect.top;
882 client_to_screen( win->parent, &x, &y );
883 offset_region( region, x, y );
888 /* clip all children of a given window out of the visible region */
889 static struct region *clip_children( struct window *parent, struct window *last,
890 struct region *region, int offset_x, int offset_y )
892 struct window *ptr;
893 struct region *tmp = create_empty_region();
895 if (!tmp) return NULL;
896 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
898 if (ptr == last) break;
899 if (!(ptr->style & WS_VISIBLE)) continue;
900 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
901 set_region_rect( tmp, &ptr->visible_rect );
902 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
904 free_region( tmp );
905 return NULL;
907 offset_region( tmp, offset_x, offset_y );
908 if (!(region = subtract_region( region, region, tmp ))) break;
909 if (is_region_empty( region )) break;
911 free_region( tmp );
912 return region;
916 /* offset the coordinates of a rectangle */
917 static inline void offset_rect( rectangle_t *rect, int offset_x, int offset_y )
919 rect->left += offset_x;
920 rect->top += offset_y;
921 rect->right += offset_x;
922 rect->bottom += offset_y;
926 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
927 static void set_region_client_rect( struct region *region, struct window *win )
929 rectangle_t rect;
931 intersect_rect( &rect, &win->window_rect, &win->client_rect );
932 set_region_rect( region, &rect );
936 /* get the top-level window to clip against for a given window */
937 static inline struct window *get_top_clipping_window( struct window *win )
939 while (!(win->paint_flags & PAINT_HAS_SURFACE) && win->parent && !is_desktop_window(win->parent))
940 win = win->parent;
941 return win;
945 /* compute the visible region of a window, in window coordinates */
946 static struct region *get_visible_region( struct window *win, unsigned int flags )
948 struct region *tmp = NULL, *region;
949 int offset_x, offset_y;
951 if (!(region = create_empty_region())) return NULL;
953 /* first check if all ancestors are visible */
955 if (!is_visible( win )) return region; /* empty region */
957 /* create a region relative to the window itself */
959 if ((flags & DCX_PARENTCLIP) && win->parent && !is_desktop_window(win->parent))
961 set_region_client_rect( region, win->parent );
962 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
964 else if (flags & DCX_WINDOW)
966 set_region_rect( region, &win->visible_rect );
967 if (win->win_region && !intersect_window_region( region, win )) goto error;
969 else
971 set_region_client_rect( region, win );
972 if (win->win_region && !intersect_window_region( region, win )) goto error;
975 /* clip children */
977 if (flags & DCX_CLIPCHILDREN)
979 if (is_desktop_window(win)) offset_x = offset_y = 0;
980 else
982 offset_x = win->client_rect.left;
983 offset_y = win->client_rect.top;
985 if (!clip_children( win, NULL, region, offset_x, offset_y )) goto error;
988 /* clip siblings of ancestors */
990 if (is_desktop_window(win)) offset_x = offset_y = 0;
991 else
993 offset_x = win->window_rect.left;
994 offset_y = win->window_rect.top;
997 if ((tmp = create_empty_region()) != NULL)
999 while (win->parent)
1001 /* we don't clip out top-level siblings as that's up to the native windowing system */
1002 if ((win->style & WS_CLIPSIBLINGS) && !is_desktop_window( win->parent ))
1004 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
1005 if (is_region_empty( region )) break;
1007 /* clip to parent client area */
1008 win = win->parent;
1009 if (!is_desktop_window(win))
1011 offset_x += win->client_rect.left;
1012 offset_y += win->client_rect.top;
1013 offset_region( region, win->client_rect.left, win->client_rect.top );
1015 set_region_client_rect( tmp, win );
1016 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
1017 if (!intersect_region( region, region, tmp )) goto error;
1018 if (is_region_empty( region )) break;
1020 free_region( tmp );
1022 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
1023 return region;
1025 error:
1026 if (tmp) free_region( tmp );
1027 free_region( region );
1028 return NULL;
1032 /* clip all children with a custom pixel format out of the visible region */
1033 static struct region *clip_pixel_format_children( struct window *parent, struct region *parent_clip,
1034 struct region *region, int offset_x, int offset_y )
1036 struct window *ptr;
1037 struct region *clip = create_empty_region();
1039 if (!clip) return NULL;
1041 LIST_FOR_EACH_ENTRY_REV( ptr, &parent->children, struct window, entry )
1043 if (!(ptr->style & WS_VISIBLE)) continue;
1044 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1046 /* add the visible rect */
1047 set_region_rect( clip, &ptr->visible_rect );
1048 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1049 offset_region( clip, offset_x, offset_y );
1050 if (!intersect_region( clip, clip, parent_clip )) break;
1051 if (!union_region( region, region, clip )) break;
1052 if (!(ptr->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))) continue;
1054 /* subtract the client rect if it uses a custom pixel format */
1055 set_region_rect( clip, &ptr->client_rect );
1056 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1057 offset_region( clip, offset_x, offset_y );
1058 if (!intersect_region( clip, clip, parent_clip )) break;
1059 if ((ptr->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1060 break;
1062 if (!clip_pixel_format_children( ptr, clip, region, offset_x + ptr->client_rect.left,
1063 offset_y + ptr->client_rect.top ))
1064 break;
1066 free_region( clip );
1067 return region;
1071 /* compute the visible surface region of a window, in parent coordinates */
1072 static struct region *get_surface_region( struct window *win )
1074 struct region *region, *clip;
1075 int offset_x, offset_y;
1077 /* create a region relative to the window itself */
1079 if (!(region = create_empty_region())) return NULL;
1080 if (!(clip = create_empty_region())) goto error;
1081 set_region_rect( region, &win->visible_rect );
1082 if (win->win_region && !intersect_window_region( region, win )) goto error;
1083 set_region_rect( clip, &win->client_rect );
1084 if (win->win_region && !intersect_window_region( clip, win )) goto error;
1086 /* clip children */
1088 if (!is_desktop_window(win))
1090 offset_x = win->client_rect.left;
1091 offset_y = win->client_rect.top;
1093 else offset_x = offset_y = 0;
1095 if (!clip_pixel_format_children( win, clip, region, offset_x, offset_y )) goto error;
1097 free_region( clip );
1098 return region;
1100 error:
1101 if (clip) free_region( clip );
1102 free_region( region );
1103 return NULL;
1107 /* get the window class of a window */
1108 struct window_class* get_window_class( user_handle_t window )
1110 struct window *win;
1111 if (!(win = get_window( window ))) return NULL;
1112 if (!win->class) set_error( STATUS_ACCESS_DENIED );
1113 return win->class;
1116 /* determine the window visible rectangle, i.e. window or client rect cropped by parent rects */
1117 /* the returned rectangle is in window coordinates; return 0 if rectangle is empty */
1118 static int get_window_visible_rect( struct window *win, rectangle_t *rect, int frame )
1120 int offset_x = 0, offset_y = 0;
1122 if (!(win->style & WS_VISIBLE)) return 0;
1124 *rect = frame ? win->window_rect : win->client_rect;
1125 if (!is_desktop_window(win))
1127 offset_x = win->window_rect.left;
1128 offset_y = win->window_rect.top;
1131 while (win->parent)
1133 win = win->parent;
1134 if (!(win->style & WS_VISIBLE) || win->style & WS_MINIMIZE) return 0;
1135 if (!is_desktop_window(win))
1137 offset_x += win->client_rect.left;
1138 offset_y += win->client_rect.top;
1139 offset_rect( rect, win->client_rect.left, win->client_rect.top );
1141 if (!intersect_rect( rect, rect, &win->client_rect )) return 0;
1142 if (!intersect_rect( rect, rect, &win->window_rect )) return 0;
1144 offset_rect( rect, -offset_x, -offset_y );
1145 return 1;
1148 /* return a copy of the specified region cropped to the window client or frame rectangle, */
1149 /* and converted from client to window coordinates. Helper for (in)validate_window. */
1150 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
1152 rectangle_t rect;
1153 struct region *tmp;
1155 if (!get_window_visible_rect( win, &rect, frame )) return NULL;
1156 if (!(tmp = create_empty_region())) return NULL;
1157 set_region_rect( tmp, &rect );
1159 if (region)
1161 /* map it to client coords */
1162 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1163 win->window_rect.top - win->client_rect.top );
1165 /* intersect specified region with bounding rect */
1166 if (!intersect_region( tmp, region, tmp )) goto done;
1167 if (is_region_empty( tmp )) goto done;
1169 /* map it back to window coords */
1170 offset_region( tmp, win->client_rect.left - win->window_rect.left,
1171 win->client_rect.top - win->window_rect.top );
1173 return tmp;
1175 done:
1176 free_region( tmp );
1177 return NULL;
1181 /* set a region as new update region for the window */
1182 static void set_update_region( struct window *win, struct region *region )
1184 if (region && !is_region_empty( region ))
1186 if (!win->update_region) inc_window_paint_count( win, 1 );
1187 else free_region( win->update_region );
1188 win->update_region = region;
1190 else
1192 if (win->update_region)
1194 inc_window_paint_count( win, -1 );
1195 free_region( win->update_region );
1197 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
1198 win->update_region = NULL;
1199 if (region) free_region( region );
1204 /* add a region to the update region; the passed region is freed or reused */
1205 static int add_update_region( struct window *win, struct region *region )
1207 if (win->update_region && !union_region( region, win->update_region, region ))
1209 free_region( region );
1210 return 0;
1212 set_update_region( win, region );
1213 return 1;
1217 /* crop the update region of children to the specified rectangle, in client coords */
1218 static void crop_children_update_region( struct window *win, rectangle_t *rect )
1220 struct window *child;
1221 struct region *tmp;
1222 rectangle_t child_rect;
1224 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1226 if (!(child->style & WS_VISIBLE)) continue;
1227 if (!rect) /* crop everything out */
1229 crop_children_update_region( child, NULL );
1230 set_update_region( child, NULL );
1231 continue;
1234 /* nothing to do if child is completely inside rect */
1235 if (child->window_rect.left >= rect->left &&
1236 child->window_rect.top >= rect->top &&
1237 child->window_rect.right <= rect->right &&
1238 child->window_rect.bottom <= rect->bottom) continue;
1240 /* map to child client coords and crop grand-children */
1241 child_rect = *rect;
1242 offset_rect( &child_rect, -child->client_rect.left, -child->client_rect.top );
1243 crop_children_update_region( child, &child_rect );
1245 /* now crop the child itself */
1246 if (!child->update_region) continue;
1247 if (!(tmp = create_empty_region())) continue;
1248 set_region_rect( tmp, rect );
1249 offset_region( tmp, -child->window_rect.left, -child->window_rect.top );
1250 if (intersect_region( tmp, child->update_region, tmp )) set_update_region( child, tmp );
1251 else free_region( tmp );
1256 /* validate the non client area of a window */
1257 static void validate_non_client( struct window *win )
1259 struct region *tmp;
1260 rectangle_t rect;
1262 if (!win->update_region) return; /* nothing to do */
1264 /* get client rect in window coords */
1265 rect.left = win->client_rect.left - win->window_rect.left;
1266 rect.top = win->client_rect.top - win->window_rect.top;
1267 rect.right = win->client_rect.right - win->window_rect.left;
1268 rect.bottom = win->client_rect.bottom - win->window_rect.top;
1270 if ((tmp = create_empty_region()))
1272 set_region_rect( tmp, &rect );
1273 if (intersect_region( tmp, win->update_region, tmp ))
1274 set_update_region( win, tmp );
1275 else
1276 free_region( tmp );
1278 win->paint_flags &= ~PAINT_NONCLIENT;
1282 /* validate a window completely so that we don't get any further paint messages for it */
1283 static void validate_whole_window( struct window *win )
1285 set_update_region( win, NULL );
1287 if (win->paint_flags & PAINT_INTERNAL)
1289 win->paint_flags &= ~PAINT_INTERNAL;
1290 inc_window_paint_count( win, -1 );
1295 /* validate a window's children so that we don't get any further paint messages for it */
1296 static void validate_children( struct window *win )
1298 struct window *child;
1300 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1302 if (!(child->style & WS_VISIBLE)) continue;
1303 validate_children(child);
1304 validate_whole_window(child);
1309 /* validate the update region of a window on all parents; helper for get_update_region */
1310 static void validate_parents( struct window *child )
1312 int offset_x = 0, offset_y = 0;
1313 struct window *win = child;
1314 struct region *tmp = NULL;
1316 if (!child->update_region) return;
1318 while (win->parent)
1320 /* map to parent client coords */
1321 offset_x += win->window_rect.left;
1322 offset_y += win->window_rect.top;
1324 win = win->parent;
1326 /* and now map to window coords */
1327 offset_x += win->client_rect.left - win->window_rect.left;
1328 offset_y += win->client_rect.top - win->window_rect.top;
1330 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1332 if (!tmp && !(tmp = create_empty_region())) return;
1333 offset_region( child->update_region, offset_x, offset_y );
1334 if (subtract_region( tmp, win->update_region, child->update_region ))
1336 set_update_region( win, tmp );
1337 tmp = NULL;
1339 /* restore child coords */
1340 offset_region( child->update_region, -offset_x, -offset_y );
1343 if (tmp) free_region( tmp );
1347 /* add/subtract a region (in client coordinates) to the update region of the window */
1348 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1350 struct region *tmp;
1351 struct window *child;
1353 if (flags & RDW_INVALIDATE)
1355 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1357 if (!add_update_region( win, tmp )) return;
1359 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1360 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1362 else if (flags & RDW_VALIDATE)
1364 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1366 set_update_region( win, NULL );
1368 else if (win->update_region)
1370 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1372 if (!subtract_region( tmp, win->update_region, tmp ))
1374 free_region( tmp );
1375 return;
1377 set_update_region( win, tmp );
1379 if (flags & RDW_NOFRAME) validate_non_client( win );
1380 if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1384 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1386 win->paint_flags |= PAINT_INTERNAL;
1387 inc_window_paint_count( win, 1 );
1389 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1391 win->paint_flags &= ~PAINT_INTERNAL;
1392 inc_window_paint_count( win, -1 );
1395 /* now process children recursively */
1397 if (flags & RDW_NOCHILDREN) return;
1398 if (win->style & WS_MINIMIZE) return;
1399 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1401 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1403 /* map to client coordinates */
1404 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1405 win->window_rect.top - win->client_rect.top );
1407 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1409 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1411 if (!(child->style & WS_VISIBLE)) continue;
1412 if (!rect_in_region( tmp, &child->window_rect )) continue;
1413 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1414 redraw_window( child, tmp, 1, flags );
1415 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1417 free_region( tmp );
1421 /* retrieve the update flags for a window depending on the state of the update region */
1422 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1424 unsigned int ret = 0;
1426 if (flags & UPDATE_NONCLIENT)
1428 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1430 if (flags & UPDATE_ERASE)
1432 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1434 if (flags & UPDATE_PAINT)
1436 if (win->update_region)
1438 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1439 ret |= UPDATE_PAINT;
1442 if (flags & UPDATE_INTERNALPAINT)
1444 if (win->paint_flags & PAINT_INTERNAL)
1446 ret |= UPDATE_INTERNALPAINT;
1447 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1450 return ret;
1454 /* iterate through the children of the given window until we find one with some update flags */
1455 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1456 unsigned int flags, struct window **child )
1458 struct window *ptr;
1459 unsigned int ret = 0;
1461 /* first make sure we want to iterate children at all */
1463 if (win->style & WS_MINIMIZE) return 0;
1465 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1466 * here we only want to repaint children of windows that clip them, others
1467 * need to wait for WM_PAINT to be done in the parent first.
1469 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1471 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1473 if (from_child) /* skip all children until from_child is found */
1475 if (ptr == from_child) from_child = NULL;
1476 continue;
1478 if (!(ptr->style & WS_VISIBLE)) continue;
1479 if ((ret = get_update_flags( ptr, flags )) != 0)
1481 *child = ptr;
1482 break;
1484 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1486 return ret;
1489 /* iterate through children and siblings of the given window until we find one with some update flags */
1490 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1491 unsigned int flags, struct window **child )
1493 unsigned int ret;
1494 struct window *ptr, *from_sibling = NULL;
1496 /* if some parent is not visible start from the next sibling */
1498 if (!is_visible( win )) return 0;
1499 for (ptr = from_child; ptr; ptr = ptr->parent)
1501 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1502 if (ptr == win) break;
1505 /* non-client painting must be delayed if one of the parents is going to
1506 * be repainted and doesn't clip children */
1508 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1510 for (ptr = win->parent; ptr; ptr = ptr->parent)
1512 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1513 return 0;
1515 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1517 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1519 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1520 if (ptr == win) break;
1526 /* check window itself (only if not restarting from a child) */
1528 if (!from_child)
1530 if ((ret = get_update_flags( win, flags )))
1532 *child = win;
1533 return ret;
1535 from_child = win;
1538 /* now check children */
1540 if (flags & UPDATE_NOCHILDREN) return 0;
1541 if (!from_sibling)
1543 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1544 from_sibling = from_child;
1547 /* then check siblings and parent siblings */
1549 while (from_sibling->parent && from_sibling != win)
1551 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1552 return ret;
1553 from_sibling = from_sibling->parent;
1555 return 0;
1559 /* expose the areas revealed by a vis region change on the window parent */
1560 /* returns the region exposed on the window itself (in client coordinates) */
1561 static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
1562 struct region *old_vis_rgn )
1564 struct region *new_vis_rgn, *exposed_rgn;
1566 if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
1568 if ((exposed_rgn = create_empty_region()))
1570 if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
1572 /* make it relative to the new client area */
1573 offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
1574 win->window_rect.top - win->client_rect.top );
1576 else
1578 free_region( exposed_rgn );
1579 exposed_rgn = NULL;
1583 if (win->parent)
1585 /* make it relative to the old window pos for subtracting */
1586 offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
1587 win->window_rect.top - old_window_rect->top );
1589 if ((win->parent->style & WS_CLIPCHILDREN) ?
1590 subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) :
1591 xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1593 if (!is_region_empty( new_vis_rgn ))
1595 /* make it relative to parent */
1596 offset_region( new_vis_rgn, old_window_rect->left, old_window_rect->top );
1597 redraw_window( win->parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1601 free_region( new_vis_rgn );
1602 return exposed_rgn;
1606 /* set the window and client rectangles, updating the update region if necessary */
1607 static void set_window_pos( struct window *win, struct window *previous,
1608 unsigned int swp_flags, const rectangle_t *window_rect,
1609 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1610 const rectangle_t *valid_rects )
1612 struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
1613 const rectangle_t old_window_rect = win->window_rect;
1614 const rectangle_t old_visible_rect = win->visible_rect;
1615 const rectangle_t old_client_rect = win->client_rect;
1616 rectangle_t rect;
1617 int client_changed, frame_changed;
1618 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1620 if (win->parent && !is_visible( win->parent )) visible = 0;
1622 if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
1624 /* set the new window info before invalidating anything */
1626 win->window_rect = *window_rect;
1627 win->visible_rect = *visible_rect;
1628 win->client_rect = *client_rect;
1629 if (!(swp_flags & SWP_NOZORDER) && win->parent) link_window( win, previous );
1630 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1631 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1633 /* keep children at the same position relative to top right corner when the parent is mirrored */
1634 if (win->ex_style & WS_EX_LAYOUTRTL)
1636 struct window *child;
1637 int old_size = old_client_rect.right - old_client_rect.left;
1638 int new_size = win->client_rect.right - win->client_rect.left;
1640 if (old_size != new_size) LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1642 offset_rect( &child->window_rect, new_size - old_size, 0 );
1643 offset_rect( &child->visible_rect, new_size - old_size, 0 );
1644 offset_rect( &child->client_rect, new_size - old_size, 0 );
1648 /* reset cursor clip rectangle when the desktop changes size */
1649 if (win == win->desktop->top_window) win->desktop->cursor.clip = *window_rect;
1651 /* if the window is not visible, everything is easy */
1652 if (!visible) return;
1654 /* expose anything revealed by the change */
1656 if (!(swp_flags & SWP_NOREDRAW))
1657 exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
1659 if (!(win->style & WS_VISIBLE))
1661 /* clear the update region since the window is no longer visible */
1662 validate_whole_window( win );
1663 validate_children( win );
1664 goto done;
1667 /* crop update region to the new window rect */
1669 if (win->update_region)
1671 if (get_window_visible_rect( win, &rect, 1 ))
1673 struct region *tmp = create_empty_region();
1674 if (tmp)
1676 set_region_rect( tmp, &rect );
1677 if (intersect_region( tmp, win->update_region, tmp ))
1678 set_update_region( win, tmp );
1679 else
1680 free_region( tmp );
1683 else set_update_region( win, NULL ); /* visible rect is empty */
1686 /* crop children regions to the new window rect */
1688 if (get_window_visible_rect( win, &rect, 0 ))
1690 /* map to client coords */
1691 offset_rect( &rect, win->window_rect.left - win->client_rect.left,
1692 win->window_rect.top - win->client_rect.top );
1693 crop_children_update_region( win, &rect );
1695 else crop_children_update_region( win, NULL );
1697 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1699 /* expose the whole non-client area if it changed in any way */
1701 if (swp_flags & SWP_NOCOPYBITS)
1703 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1704 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1705 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
1706 client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
1708 else
1710 /* assume the bits have been moved to follow the window rect */
1711 int x_offset = window_rect->left - old_window_rect.left;
1712 int y_offset = window_rect->top - old_window_rect.top;
1713 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1714 window_rect->right - old_window_rect.right != x_offset ||
1715 window_rect->bottom - old_window_rect.bottom != y_offset ||
1716 visible_rect->left - old_visible_rect.left != x_offset ||
1717 visible_rect->right - old_visible_rect.right != x_offset ||
1718 visible_rect->top - old_visible_rect.top != y_offset ||
1719 visible_rect->bottom - old_visible_rect.bottom != y_offset);
1720 client_changed = (client_rect->left - old_client_rect.left != x_offset ||
1721 client_rect->right - old_client_rect.right != x_offset ||
1722 client_rect->top - old_client_rect.top != y_offset ||
1723 client_rect->bottom - old_client_rect.bottom != y_offset ||
1724 !valid_rects ||
1725 memcmp( &valid_rects[0], client_rect, sizeof(*client_rect) ));
1726 /* if part of the non-client area was exposed, consider it changed */
1727 if (exposed_rgn && !frame_changed)
1729 get_region_extents( exposed_rgn, &rect );
1730 offset_rect( &rect, client_rect->left, client_rect->top );
1731 frame_changed = (rect.left < client_rect->left || rect.top < client_rect->top ||
1732 rect.right > client_rect->right || rect.bottom > client_rect->bottom);
1736 if (frame_changed || client_changed)
1738 struct region *win_rgn = old_vis_rgn; /* reuse previous region */
1740 set_region_rect( win_rgn, window_rect );
1741 if (valid_rects)
1743 /* subtract the valid portion of client rect from the total region */
1744 struct region *tmp = create_empty_region();
1745 if (tmp)
1747 set_region_rect( tmp, &valid_rects[0] );
1748 /* subtract update region since invalid parts of the valid rect won't be copied */
1749 if (win->update_region)
1751 offset_region( tmp, -window_rect->left, -window_rect->top );
1752 subtract_region( tmp, tmp, win->update_region );
1753 offset_region( tmp, window_rect->left, window_rect->top );
1755 if (subtract_region( tmp, win_rgn, tmp )) win_rgn = tmp;
1756 else free_region( tmp );
1759 if (!is_desktop_window(win))
1760 offset_region( win_rgn, -client_rect->left, -client_rect->top );
1761 if (exposed_rgn)
1763 union_region( exposed_rgn, exposed_rgn, win_rgn );
1764 if (win_rgn != old_vis_rgn) free_region( win_rgn );
1766 else
1768 exposed_rgn = win_rgn;
1769 if (win_rgn == old_vis_rgn) old_vis_rgn = NULL;
1773 if (exposed_rgn)
1774 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1776 done:
1777 if (old_vis_rgn) free_region( old_vis_rgn );
1778 if (exposed_rgn) free_region( exposed_rgn );
1779 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1783 /* set the window region, updating the update region if necessary */
1784 static void set_window_region( struct window *win, struct region *region, int redraw )
1786 struct region *old_vis_rgn = NULL, *exposed_rgn;
1788 /* no need to redraw if window is not visible */
1789 if (redraw && !is_visible( win )) redraw = 0;
1791 if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
1793 if (win->win_region) free_region( win->win_region );
1794 win->win_region = region;
1796 /* expose anything revealed by the change */
1797 if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
1799 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1800 free_region( exposed_rgn );
1803 if (old_vis_rgn) free_region( old_vis_rgn );
1804 clear_error(); /* we ignore out of memory errors since the region has been set */
1808 /* destroy a window */
1809 void destroy_window( struct window *win )
1811 /* hide the window */
1812 if (is_visible(win))
1814 struct region *vis_rgn = get_visible_region( win, DCX_WINDOW );
1815 win->style &= ~WS_VISIBLE;
1816 if (vis_rgn)
1818 struct region *exposed_rgn = expose_window( win, &win->window_rect, vis_rgn );
1819 if (exposed_rgn) free_region( exposed_rgn );
1820 free_region( vis_rgn );
1822 validate_whole_window( win );
1823 validate_children( win );
1826 /* destroy all children */
1827 while (!list_empty(&win->children))
1828 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
1829 while (!list_empty(&win->unlinked))
1830 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
1832 /* reset global window pointers, if the corresponding window is destroyed */
1833 if (win == shell_window) shell_window = NULL;
1834 if (win == shell_listview) shell_listview = NULL;
1835 if (win == progman_window) progman_window = NULL;
1836 if (win == taskman_window) taskman_window = NULL;
1837 free_hotkeys( win->desktop, win->handle );
1838 free_user_handle( win->handle );
1839 destroy_properties( win );
1840 list_remove( &win->entry );
1841 if (is_desktop_window(win))
1843 struct desktop *desktop = win->desktop;
1844 assert( desktop->top_window == win || desktop->msg_window == win );
1845 if (desktop->top_window == win) desktop->top_window = NULL;
1846 else desktop->msg_window = NULL;
1848 detach_window_thread( win );
1849 if (win->win_region) free_region( win->win_region );
1850 if (win->update_region) free_region( win->update_region );
1851 if (win->class) release_class( win->class );
1852 free( win->text );
1853 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
1854 free( win );
1858 /* create a window */
1859 DECL_HANDLER(create_window)
1861 struct window *win, *parent = NULL, *owner = NULL;
1862 struct unicode_str cls_name;
1863 atom_t atom;
1865 reply->handle = 0;
1866 if (req->parent && !(parent = get_window( req->parent ))) return;
1868 if (req->owner)
1870 if (!(owner = get_window( req->owner ))) return;
1871 if (is_desktop_window(owner)) owner = NULL;
1872 else if (parent && !is_desktop_window(parent))
1874 /* an owned window must be created as top-level */
1875 set_error( STATUS_ACCESS_DENIED );
1876 return;
1878 else /* owner must be a top-level window */
1879 while (!is_desktop_window(owner->parent)) owner = owner->parent;
1882 get_req_unicode_str( &cls_name );
1883 atom = cls_name.len ? find_global_atom( NULL, &cls_name ) : req->atom;
1885 if (!(win = create_window( parent, owner, atom, req->instance ))) return;
1887 reply->handle = win->handle;
1888 reply->parent = win->parent ? win->parent->handle : 0;
1889 reply->owner = win->owner;
1890 reply->extra = win->nb_extra_bytes;
1891 reply->class_ptr = get_class_client_ptr( win->class );
1895 /* set the parent of a window */
1896 DECL_HANDLER(set_parent)
1898 struct window *win, *parent = NULL;
1900 if (!(win = get_window( req->handle ))) return;
1901 if (req->parent && !(parent = get_window( req->parent ))) return;
1903 if (is_desktop_window(win))
1905 set_error( STATUS_INVALID_PARAMETER );
1906 return;
1908 reply->old_parent = win->parent->handle;
1909 reply->full_parent = parent ? parent->handle : 0;
1910 set_parent_window( win, parent );
1914 /* destroy a window */
1915 DECL_HANDLER(destroy_window)
1917 struct window *win = get_window( req->handle );
1918 if (win)
1920 if (!is_desktop_window(win)) destroy_window( win );
1921 else if (win->thread == current) detach_window_thread( win );
1922 else set_error( STATUS_ACCESS_DENIED );
1927 /* retrieve the desktop window for the current thread */
1928 DECL_HANDLER(get_desktop_window)
1930 struct desktop *desktop = get_thread_desktop( current, 0 );
1932 if (!desktop) return;
1934 if (!desktop->top_window && req->force) /* create it */
1936 if ((desktop->top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
1938 detach_window_thread( desktop->top_window );
1939 desktop->top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1943 if (!desktop->msg_window && req->force) /* create it */
1945 static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
1946 static const struct unicode_str name = { messageW, sizeof(messageW) };
1947 atom_t atom = add_global_atom( NULL, &name );
1948 if (atom && (desktop->msg_window = create_window( NULL, NULL, atom, 0 )))
1950 detach_window_thread( desktop->msg_window );
1951 desktop->msg_window->style = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1955 reply->top_window = desktop->top_window ? desktop->top_window->handle : 0;
1956 reply->msg_window = desktop->msg_window ? desktop->msg_window->handle : 0;
1957 release_object( desktop );
1961 /* set a window owner */
1962 DECL_HANDLER(set_window_owner)
1964 struct window *win = get_window( req->handle );
1965 struct window *owner = NULL, *ptr;
1967 if (!win) return;
1968 if (req->owner && !(owner = get_window( req->owner ))) return;
1969 if (is_desktop_window(win))
1971 set_error( STATUS_ACCESS_DENIED );
1972 return;
1975 /* make sure owner is not a successor of window */
1976 for (ptr = owner; ptr; ptr = ptr->owner ? get_window( ptr->owner ) : NULL)
1978 if (ptr == win)
1980 set_error( STATUS_INVALID_PARAMETER );
1981 return;
1985 reply->prev_owner = win->owner;
1986 reply->full_owner = win->owner = owner ? owner->handle : 0;
1990 /* get information from a window handle */
1991 DECL_HANDLER(get_window_info)
1993 struct window *win = get_window( req->handle );
1995 reply->full_handle = 0;
1996 reply->tid = reply->pid = 0;
1997 if (win)
1999 reply->full_handle = win->handle;
2000 reply->last_active = win->handle;
2001 reply->is_unicode = win->is_unicode;
2002 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
2003 if (win->thread)
2005 reply->tid = get_thread_id( win->thread );
2006 reply->pid = get_process_id( win->thread->process );
2007 reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
2013 /* set some information in a window */
2014 DECL_HANDLER(set_window_info)
2016 struct window *win = get_window( req->handle );
2018 if (!win) return;
2019 if (req->flags && is_desktop_window(win) && win->thread != current)
2021 set_error( STATUS_ACCESS_DENIED );
2022 return;
2024 if (req->extra_size > sizeof(req->extra_value) ||
2025 req->extra_offset < -1 ||
2026 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
2028 set_win32_error( ERROR_INVALID_INDEX );
2029 return;
2031 if (req->extra_offset != -1)
2033 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
2035 else if (req->flags & SET_WIN_EXTRA)
2037 set_win32_error( ERROR_INVALID_INDEX );
2038 return;
2040 reply->old_style = win->style;
2041 reply->old_ex_style = win->ex_style;
2042 reply->old_id = win->id;
2043 reply->old_instance = win->instance;
2044 reply->old_user_data = win->user_data;
2045 if (req->flags & SET_WIN_STYLE) win->style = req->style;
2046 if (req->flags & SET_WIN_EXSTYLE)
2048 /* WS_EX_TOPMOST can only be changed for unlinked windows */
2049 if (!win->is_linked) win->ex_style = req->ex_style;
2050 else win->ex_style = (req->ex_style & ~WS_EX_TOPMOST) | (win->ex_style & WS_EX_TOPMOST);
2051 if (!(win->ex_style & WS_EX_LAYERED)) win->is_layered = 0;
2053 if (req->flags & SET_WIN_ID) win->id = req->id;
2054 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
2055 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
2056 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
2057 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
2058 &req->extra_value, req->extra_size );
2060 /* changing window style triggers a non-client paint */
2061 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
2065 /* get a list of the window parents, up to the root of the tree */
2066 DECL_HANDLER(get_window_parents)
2068 struct window *ptr, *win = get_window( req->handle );
2069 int total = 0;
2070 user_handle_t *data;
2071 data_size_t len;
2073 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
2075 reply->count = total;
2076 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2077 if (len && ((data = set_reply_data_size( len ))))
2079 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
2080 *data++ = ptr->handle;
2085 /* get a list of the window children */
2086 DECL_HANDLER(get_window_children)
2088 struct window *parent = NULL;
2089 unsigned int total;
2090 user_handle_t *data;
2091 data_size_t len;
2092 struct unicode_str cls_name;
2093 atom_t atom = req->atom;
2094 struct desktop *desktop = NULL;
2096 get_req_unicode_str( &cls_name );
2097 if (cls_name.len && !(atom = find_global_atom( NULL, &cls_name ))) return;
2099 if (req->desktop)
2101 if (!(desktop = get_desktop_obj( current->process, req->desktop, DESKTOP_ENUMERATE ))) return;
2102 parent = desktop->top_window;
2104 else
2106 if (req->parent && !(parent = get_window( req->parent ))) return;
2107 if (!parent && !(desktop = get_thread_desktop( current, 0 ))) return;
2110 if (parent)
2111 total = get_children_windows( parent, atom, req->tid, NULL, 0 );
2112 else
2113 total = get_children_windows( desktop->top_window, atom, req->tid, NULL, 0 ) +
2114 get_children_windows( desktop->msg_window, atom, req->tid, NULL, 0 );
2116 reply->count = total;
2117 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2118 if (len && ((data = set_reply_data_size( len ))))
2120 if (parent) get_children_windows( parent, atom, req->tid, data, len / sizeof(user_handle_t) );
2121 else
2123 total = get_children_windows( desktop->top_window, atom, req->tid,
2124 data, len / sizeof(user_handle_t) );
2125 data += total;
2126 len -= total * sizeof(user_handle_t);
2127 if (len >= sizeof(user_handle_t))
2128 get_children_windows( desktop->msg_window, atom, req->tid,
2129 data, len / sizeof(user_handle_t) );
2132 if (desktop) release_object( desktop );
2136 /* get a list of the window children that contain a given point */
2137 DECL_HANDLER(get_window_children_from_point)
2139 struct user_handle_array array;
2140 struct window *parent = get_window( req->parent );
2141 data_size_t len;
2143 if (!parent) return;
2145 array.handles = NULL;
2146 array.count = 0;
2147 array.total = 0;
2148 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
2150 reply->count = array.count;
2151 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
2152 if (len) set_reply_data_ptr( array.handles, len );
2153 else free( array.handles );
2157 /* get window tree information from a window handle */
2158 DECL_HANDLER(get_window_tree)
2160 struct window *ptr, *win = get_window( req->handle );
2162 if (!win) return;
2164 reply->parent = 0;
2165 reply->owner = 0;
2166 reply->next_sibling = 0;
2167 reply->prev_sibling = 0;
2168 reply->first_sibling = 0;
2169 reply->last_sibling = 0;
2170 reply->first_child = 0;
2171 reply->last_child = 0;
2173 if (win->parent)
2175 struct window *parent = win->parent;
2176 reply->parent = parent->handle;
2177 reply->owner = win->owner;
2178 if (win->is_linked)
2180 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
2181 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
2183 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
2184 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
2186 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
2187 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
2191 /* set the position and Z order of a window */
2192 DECL_HANDLER(set_window_pos)
2194 rectangle_t window_rect, client_rect, visible_rect;
2195 struct window *previous = NULL;
2196 struct window *top, *win = get_window( req->handle );
2197 unsigned int flags = req->swp_flags;
2199 if (!win) return;
2200 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
2202 if (!(flags & SWP_NOZORDER))
2204 switch ((int)req->previous)
2206 case 0: /* HWND_TOP */
2207 previous = WINPTR_TOP;
2208 break;
2209 case 1: /* HWND_BOTTOM */
2210 previous = WINPTR_BOTTOM;
2211 break;
2212 case -1: /* HWND_TOPMOST */
2213 previous = WINPTR_TOPMOST;
2214 break;
2215 case -2: /* HWND_NOTOPMOST */
2216 previous = WINPTR_NOTOPMOST;
2217 break;
2218 default:
2219 if (!(previous = get_window( req->previous ))) return;
2220 /* previous must be a sibling */
2221 if (previous->parent != win->parent)
2223 set_error( STATUS_INVALID_PARAMETER );
2224 return;
2226 break;
2228 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
2231 /* windows that use UpdateLayeredWindow don't trigger repaints */
2232 if ((win->ex_style & WS_EX_LAYERED) && !win->is_layered) flags |= SWP_NOREDRAW;
2234 /* window rectangle must be ordered properly */
2235 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
2237 set_error( STATUS_INVALID_PARAMETER );
2238 return;
2241 window_rect = visible_rect = req->window;
2242 client_rect = req->client;
2243 if (get_req_data_size() >= sizeof(rectangle_t))
2244 memcpy( &visible_rect, get_req_data(), sizeof(rectangle_t) );
2245 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2247 mirror_rect( &win->parent->client_rect, &window_rect );
2248 mirror_rect( &win->parent->client_rect, &visible_rect );
2249 mirror_rect( &win->parent->client_rect, &client_rect );
2252 win->paint_flags = (win->paint_flags & ~PAINT_CLIENT_FLAGS) | (req->paint_flags & PAINT_CLIENT_FLAGS);
2253 if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
2255 if (get_req_data_size() >= 3 * sizeof(rectangle_t))
2257 rectangle_t valid_rects[2];
2258 memcpy( valid_rects, (const rectangle_t *)get_req_data() + 1, 2 * sizeof(rectangle_t) );
2259 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2261 mirror_rect( &win->parent->client_rect, &valid_rects[0] );
2262 mirror_rect( &win->parent->client_rect, &valid_rects[1] );
2264 set_window_pos( win, previous, flags, &window_rect, &client_rect, &visible_rect, valid_rects );
2266 else set_window_pos( win, previous, flags, &window_rect, &client_rect, &visible_rect, NULL );
2268 reply->new_style = win->style;
2269 reply->new_ex_style = win->ex_style;
2271 top = get_top_clipping_window( win );
2272 if (is_visible( top ) &&
2273 (top->paint_flags & PAINT_HAS_SURFACE) &&
2274 (top->paint_flags & PAINT_PIXEL_FORMAT_CHILD))
2275 reply->surface_win = top->handle;
2279 /* get the window and client rectangles of a window */
2280 DECL_HANDLER(get_window_rectangles)
2282 struct window *win = get_window( req->handle );
2284 if (!win) return;
2286 reply->window = win->window_rect;
2287 reply->visible = win->visible_rect;
2288 reply->client = win->client_rect;
2290 switch (req->relative)
2292 case COORDS_CLIENT:
2293 offset_rect( &reply->window, -win->client_rect.left, -win->client_rect.top );
2294 offset_rect( &reply->visible, -win->client_rect.left, -win->client_rect.top );
2295 offset_rect( &reply->client, -win->client_rect.left, -win->client_rect.top );
2296 if (win->ex_style & WS_EX_LAYOUTRTL)
2298 mirror_rect( &win->client_rect, &reply->window );
2299 mirror_rect( &win->client_rect, &reply->visible );
2301 break;
2302 case COORDS_WINDOW:
2303 offset_rect( &reply->window, -win->window_rect.left, -win->window_rect.top );
2304 offset_rect( &reply->visible, -win->window_rect.left, -win->window_rect.top );
2305 offset_rect( &reply->client, -win->window_rect.left, -win->window_rect.top );
2306 if (win->ex_style & WS_EX_LAYOUTRTL)
2308 mirror_rect( &win->window_rect, &reply->visible );
2309 mirror_rect( &win->window_rect, &reply->client );
2311 break;
2312 case COORDS_PARENT:
2313 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2315 mirror_rect( &win->parent->client_rect, &reply->window );
2316 mirror_rect( &win->parent->client_rect, &reply->visible );
2317 mirror_rect( &win->parent->client_rect, &reply->client );
2319 break;
2320 case COORDS_SCREEN:
2321 client_to_screen_rect( win->parent, &reply->window );
2322 client_to_screen_rect( win->parent, &reply->visible );
2323 client_to_screen_rect( win->parent, &reply->client );
2324 break;
2325 default:
2326 set_error( STATUS_INVALID_PARAMETER );
2327 break;
2332 /* get the window text */
2333 DECL_HANDLER(get_window_text)
2335 struct window *win = get_window( req->handle );
2337 if (win && win->text)
2339 data_size_t len = strlenW( win->text ) * sizeof(WCHAR);
2340 if (len > get_reply_max_size()) len = get_reply_max_size();
2341 set_reply_data( win->text, len );
2346 /* set the window text */
2347 DECL_HANDLER(set_window_text)
2349 struct window *win = get_window( req->handle );
2351 if (win)
2353 WCHAR *text = NULL;
2354 data_size_t len = get_req_data_size() / sizeof(WCHAR);
2355 if (len)
2357 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
2358 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
2359 text[len] = 0;
2361 free( win->text );
2362 win->text = text;
2367 /* get the coordinates offset between two windows */
2368 DECL_HANDLER(get_windows_offset)
2370 struct window *win;
2371 int mirror_from = 0, mirror_to = 0;
2373 reply->x = reply->y = 0;
2374 if (req->from)
2376 if (!(win = get_window( req->from ))) return;
2377 if (win->ex_style & WS_EX_LAYOUTRTL)
2379 mirror_from = 1;
2380 reply->x += win->client_rect.right - win->client_rect.left;
2382 while (win && !is_desktop_window(win))
2384 reply->x += win->client_rect.left;
2385 reply->y += win->client_rect.top;
2386 win = win->parent;
2389 if (req->to)
2391 if (!(win = get_window( req->to ))) return;
2392 if (win->ex_style & WS_EX_LAYOUTRTL)
2394 mirror_to = 1;
2395 reply->x -= win->client_rect.right - win->client_rect.left;
2397 while (win && !is_desktop_window(win))
2399 reply->x -= win->client_rect.left;
2400 reply->y -= win->client_rect.top;
2401 win = win->parent;
2404 if (mirror_from) reply->x = -reply->x;
2405 reply->mirror = mirror_from ^ mirror_to;
2409 /* get the visible region of a window */
2410 DECL_HANDLER(get_visible_region)
2412 struct region *region;
2413 struct window *top, *win = get_window( req->window );
2415 if (!win) return;
2417 top = get_top_clipping_window( win );
2418 if ((region = get_visible_region( win, req->flags )))
2420 rectangle_t *data;
2421 map_win_region_to_screen( win, region );
2422 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2423 if (data) set_reply_data_ptr( data, reply->total_size );
2425 reply->top_win = top->handle;
2426 reply->top_rect = top->visible_rect;
2428 if (!is_desktop_window(win))
2430 reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
2431 client_to_screen_rect( top->parent, &reply->top_rect );
2432 client_to_screen_rect( win->parent, &reply->win_rect );
2434 else
2436 reply->win_rect.left = 0;
2437 reply->win_rect.top = 0;
2438 reply->win_rect.right = win->client_rect.right - win->client_rect.left;
2439 reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
2444 /* get the surface visible region of a window */
2445 DECL_HANDLER(get_surface_region)
2447 struct region *region;
2448 struct window *win = get_window( req->window );
2450 if (!win || !is_visible( win )) return;
2452 if ((region = get_surface_region( win )))
2454 rectangle_t *data;
2455 if (win->parent) map_win_region_to_screen( win->parent, region );
2456 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2457 if (data) set_reply_data_ptr( data, reply->total_size );
2459 reply->visible_rect = win->visible_rect;
2460 if (win->parent) client_to_screen_rect( win->parent, &reply->visible_rect );
2464 /* get the window region */
2465 DECL_HANDLER(get_window_region)
2467 rectangle_t *data;
2468 struct window *win = get_window( req->window );
2470 if (!win) return;
2471 if (!win->win_region) return;
2473 if (win->ex_style & WS_EX_LAYOUTRTL)
2475 struct region *region = create_empty_region();
2477 if (!region) return;
2478 if (!copy_region( region, win->win_region ))
2480 free_region( region );
2481 return;
2483 mirror_region( &win->window_rect, region );
2484 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2486 else data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
2488 if (data) set_reply_data_ptr( data, reply->total_size );
2492 /* set the window region */
2493 DECL_HANDLER(set_window_region)
2495 struct region *region = NULL;
2496 struct window *win = get_window( req->window );
2498 if (!win) return;
2500 if (get_req_data_size()) /* no data means remove the region completely */
2502 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2503 return;
2504 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->window_rect, region );
2506 set_window_region( win, region, req->redraw );
2510 /* get a window update region */
2511 DECL_HANDLER(get_update_region)
2513 rectangle_t *data;
2514 unsigned int flags = req->flags;
2515 struct window *from_child = NULL;
2516 struct window *win = get_window( req->window );
2518 reply->flags = 0;
2519 if (!win) return;
2521 if (req->from_child)
2523 struct window *ptr;
2525 if (!(from_child = get_window( req->from_child ))) return;
2527 /* make sure from_child is a child of win */
2528 ptr = from_child;
2529 while (ptr && ptr != win) ptr = ptr->parent;
2530 if (!ptr)
2532 set_error( STATUS_INVALID_PARAMETER );
2533 return;
2537 if (flags & UPDATE_DELAYED_ERASE) /* this means that the previous call didn't erase */
2539 if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
2540 else win->paint_flags |= PAINT_DELAYED_ERASE;
2543 reply->flags = get_window_update_flags( win, from_child, flags, &win );
2544 reply->child = win->handle;
2546 if (flags & UPDATE_NOREGION) return;
2548 if (win->update_region)
2550 /* convert update region to screen coordinates */
2551 struct region *region = create_empty_region();
2553 if (!region) return;
2554 if (!copy_region( region, win->update_region ))
2556 free_region( region );
2557 return;
2559 map_win_region_to_screen( win, region );
2560 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
2561 &reply->total_size ))) return;
2562 set_reply_data_ptr( data, reply->total_size );
2565 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
2567 validate_parents( win );
2568 validate_whole_window( win );
2570 else
2572 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
2573 if (reply->flags & UPDATE_ERASE)
2575 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
2576 /* desktop window only gets erased, not repainted */
2577 if (is_desktop_window(win)) validate_whole_window( win );
2583 /* update the z order of a window so that a given rectangle is fully visible */
2584 DECL_HANDLER(update_window_zorder)
2586 rectangle_t tmp, rect = req->rect;
2587 struct window *ptr, *win = get_window( req->window );
2589 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
2590 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &rect );
2591 offset_rect( &rect, win->client_rect.left, win->client_rect.top );
2593 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2595 if (ptr == win) break;
2596 if (!(ptr->style & WS_VISIBLE)) continue;
2597 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2598 if (ptr->is_layered && (ptr->layered_flags & LWA_COLORKEY)) continue;
2599 if (!intersect_rect( &tmp, &ptr->visible_rect, &rect )) continue;
2600 if (ptr->win_region)
2602 tmp = rect;
2603 offset_rect( &tmp, -ptr->window_rect.left, -ptr->window_rect.top );
2604 if (!rect_in_region( ptr->win_region, &tmp )) continue;
2606 /* found a window obscuring the rectangle, now move win above this one */
2607 /* making sure to not violate the topmost rule */
2608 if (!(ptr->ex_style & WS_EX_TOPMOST) || (win->ex_style & WS_EX_TOPMOST))
2610 list_remove( &win->entry );
2611 list_add_before( &ptr->entry, &win->entry );
2613 break;
2618 /* mark parts of a window as needing a redraw */
2619 DECL_HANDLER(redraw_window)
2621 struct region *region = NULL;
2622 struct window *win = get_window( req->window );
2624 if (!win) return;
2625 if (!is_visible( win )) return; /* nothing to do */
2627 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
2629 if (get_req_data_size()) /* no data means whole rectangle */
2631 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2632 return;
2633 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->client_rect, region );
2637 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
2638 req->flags );
2639 if (region) free_region( region );
2643 /* set a window property */
2644 DECL_HANDLER(set_window_property)
2646 struct unicode_str name;
2647 struct window *win = get_window( req->window );
2649 if (!win) return;
2651 get_req_unicode_str( &name );
2652 if (name.len)
2654 atom_t atom = add_global_atom( NULL, &name );
2655 if (atom)
2657 set_property( win, atom, req->data, PROP_TYPE_STRING );
2658 release_global_atom( NULL, atom );
2661 else set_property( win, req->atom, req->data, PROP_TYPE_ATOM );
2665 /* remove a window property */
2666 DECL_HANDLER(remove_window_property)
2668 struct unicode_str name;
2669 struct window *win = get_window( req->window );
2671 get_req_unicode_str( &name );
2672 if (win)
2674 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2675 if (atom) reply->data = remove_property( win, atom );
2680 /* get a window property */
2681 DECL_HANDLER(get_window_property)
2683 struct unicode_str name;
2684 struct window *win = get_window( req->window );
2686 get_req_unicode_str( &name );
2687 if (win)
2689 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2690 if (atom) reply->data = get_property( win, atom );
2695 /* get the list of properties of a window */
2696 DECL_HANDLER(get_window_properties)
2698 property_data_t *data;
2699 int i, count, max = get_reply_max_size() / sizeof(*data);
2700 struct window *win = get_window( req->window );
2702 reply->total = 0;
2703 if (!win) return;
2705 for (i = count = 0; i < win->prop_inuse; i++)
2706 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2707 reply->total = count;
2709 if (count > max) count = max;
2710 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2712 for (i = 0; i < win->prop_inuse && count; i++)
2714 if (win->properties[i].type == PROP_TYPE_FREE) continue;
2715 data->atom = win->properties[i].atom;
2716 data->string = (win->properties[i].type == PROP_TYPE_STRING);
2717 data->data = win->properties[i].data;
2718 data++;
2719 count--;
2724 /* get the new window pointer for a global window, checking permissions */
2725 /* helper for set_global_windows request */
2726 static int get_new_global_window( struct window **win, user_handle_t handle )
2728 if (!handle)
2730 *win = NULL;
2731 return 1;
2733 else if (*win)
2735 set_error( STATUS_ACCESS_DENIED );
2736 return 0;
2738 *win = get_window( handle );
2739 return (*win != NULL);
2742 /* Set/get the global windows */
2743 DECL_HANDLER(set_global_windows)
2745 struct window *new_shell_window = shell_window;
2746 struct window *new_shell_listview = shell_listview;
2747 struct window *new_progman_window = progman_window;
2748 struct window *new_taskman_window = taskman_window;
2750 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2751 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2752 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2753 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2755 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2757 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2758 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2760 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2762 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2764 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2766 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2768 shell_window = new_shell_window;
2769 shell_listview = new_shell_listview;
2770 progman_window = new_progman_window;
2771 taskman_window = new_taskman_window;
2774 /* retrieve layered info for a window */
2775 DECL_HANDLER(get_window_layered_info)
2777 struct window *win = get_window( req->handle );
2779 if (!win) return;
2781 if (win->is_layered)
2783 reply->color_key = win->color_key;
2784 reply->alpha = win->alpha;
2785 reply->flags = win->layered_flags;
2787 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2791 /* set layered info for a window */
2792 DECL_HANDLER(set_window_layered_info)
2794 struct window *win = get_window( req->handle );
2796 if (!win) return;
2798 if (win->ex_style & WS_EX_LAYERED)
2800 if (req->flags & LWA_ALPHA) win->alpha = req->alpha;
2801 else if (!win->is_layered) win->alpha = 0; /* alpha init value is 0 */
2803 win->color_key = req->color_key;
2804 win->layered_flags = req->flags;
2805 win->is_layered = 1;
2807 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );