shell32/tests: Add tests for SHGetStockIconInfo.
[wine.git] / server / window.c
blobe141f934cef54822f36e008a6c468b8d0c18bb0f
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 | PAINT_PIXEL_FORMAT_CHILD))
258 update_pixel_format_flags( win );
260 else /* move it to parent unlinked list */
262 list_remove( &win->entry ); /* unlink it from the previous location */
263 list_add_head( &win->parent->unlinked, &win->entry );
264 win->is_linked = 0;
266 return 1;
269 /* append a user handle to a handle array */
270 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
272 if (array->count >= array->total)
274 int new_total = max( array->total * 2, 32 );
275 user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
276 if (!new_array)
278 free( array->handles );
279 set_error( STATUS_NO_MEMORY );
280 return 0;
282 array->handles = new_array;
283 array->total = new_total;
285 array->handles[array->count++] = handle;
286 return 1;
289 /* set a window property */
290 static void set_property( struct window *win, atom_t atom, lparam_t data, enum property_type type )
292 int i, free = -1;
293 struct property *new_props;
295 /* check if it exists already */
296 for (i = 0; i < win->prop_inuse; i++)
298 if (win->properties[i].type == PROP_TYPE_FREE)
300 free = i;
301 continue;
303 if (win->properties[i].atom == atom)
305 win->properties[i].type = type;
306 win->properties[i].data = data;
307 return;
311 /* need to add an entry */
312 if (!grab_global_atom( NULL, atom )) return;
313 if (free == -1)
315 /* no free entry */
316 if (win->prop_inuse >= win->prop_alloc)
318 /* need to grow the array */
319 if (!(new_props = realloc( win->properties,
320 sizeof(*new_props) * (win->prop_alloc + 16) )))
322 set_error( STATUS_NO_MEMORY );
323 release_global_atom( NULL, atom );
324 return;
326 win->prop_alloc += 16;
327 win->properties = new_props;
329 free = win->prop_inuse++;
331 win->properties[free].atom = atom;
332 win->properties[free].type = type;
333 win->properties[free].data = data;
336 /* remove a window property */
337 static lparam_t remove_property( struct window *win, atom_t atom )
339 int i;
341 for (i = 0; i < win->prop_inuse; i++)
343 if (win->properties[i].type == PROP_TYPE_FREE) continue;
344 if (win->properties[i].atom == atom)
346 release_global_atom( NULL, atom );
347 win->properties[i].type = PROP_TYPE_FREE;
348 return win->properties[i].data;
351 /* FIXME: last error? */
352 return 0;
355 /* find a window property */
356 static lparam_t get_property( struct window *win, atom_t atom )
358 int i;
360 for (i = 0; i < win->prop_inuse; i++)
362 if (win->properties[i].type == PROP_TYPE_FREE) continue;
363 if (win->properties[i].atom == atom) return win->properties[i].data;
365 /* FIXME: last error? */
366 return 0;
369 /* destroy all properties of a window */
370 static inline void destroy_properties( struct window *win )
372 int i;
374 if (!win->properties) return;
375 for (i = 0; i < win->prop_inuse; i++)
377 if (win->properties[i].type == PROP_TYPE_FREE) continue;
378 release_global_atom( NULL, win->properties[i].atom );
380 free( win->properties );
383 /* detach a window from its owner thread but keep the window around */
384 static void detach_window_thread( struct window *win )
386 struct thread *thread = win->thread;
388 if (!thread) return;
389 if (thread->queue)
391 if (win->update_region) inc_queue_paint_count( thread, -1 );
392 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
393 queue_cleanup_window( thread, win->handle );
395 assert( thread->desktop_users > 0 );
396 thread->desktop_users--;
397 release_class( win->class );
398 win->class = NULL;
400 /* don't hold a reference to the desktop so that the desktop window can be */
401 /* destroyed when the desktop ref count reaches zero */
402 release_object( win->desktop );
403 win->thread = NULL;
406 /* get the process owning the top window of a given desktop */
407 struct process *get_top_window_owner( struct desktop *desktop )
409 struct window *win = desktop->top_window;
410 if (!win || !win->thread) return NULL;
411 return win->thread->process;
414 /* get the top window size of a given desktop */
415 void get_top_window_rectangle( struct desktop *desktop, rectangle_t *rect )
417 struct window *win = desktop->top_window;
418 if (!win) rect->left = rect->top = rect->right = rect->bottom = 0;
419 else *rect = win->window_rect;
422 /* post a message to the desktop window */
423 void post_desktop_message( struct desktop *desktop, unsigned int message,
424 lparam_t wparam, lparam_t lparam )
426 struct window *win = desktop->top_window;
427 if (win && win->thread) post_message( win->handle, message, wparam, lparam );
430 /* create a new window structure (note: the window is not linked in the window tree) */
431 static struct window *create_window( struct window *parent, struct window *owner,
432 atom_t atom, mod_handle_t instance )
434 static const rectangle_t empty_rect;
435 int extra_bytes;
436 struct window *win = NULL;
437 struct desktop *desktop;
438 struct window_class *class;
440 if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
442 if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
444 release_object( desktop );
445 return NULL;
448 if (!parent) /* null parent is only allowed for desktop or HWND_MESSAGE top window */
450 if (is_desktop_class( class ))
451 parent = desktop->top_window; /* use existing desktop if any */
452 else if (is_hwnd_message_class( class ))
453 /* use desktop window if message window is already created */
454 parent = desktop->msg_window ? desktop->top_window : NULL;
455 else if (!(parent = desktop->top_window)) /* must already have a desktop then */
457 set_error( STATUS_ACCESS_DENIED );
458 goto failed;
462 /* parent must be on the same desktop */
463 if (parent && parent->desktop != desktop)
465 set_error( STATUS_ACCESS_DENIED );
466 goto failed;
469 if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
470 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
472 win->parent = parent;
473 win->owner = owner ? owner->handle : 0;
474 win->thread = current;
475 win->desktop = desktop;
476 win->class = class;
477 win->atom = atom;
478 win->last_active = win->handle;
479 win->win_region = NULL;
480 win->update_region = NULL;
481 win->style = 0;
482 win->ex_style = 0;
483 win->id = 0;
484 win->instance = 0;
485 win->is_unicode = 1;
486 win->is_linked = 0;
487 win->is_layered = 0;
488 win->user_data = 0;
489 win->text = NULL;
490 win->paint_flags = 0;
491 win->prop_inuse = 0;
492 win->prop_alloc = 0;
493 win->properties = NULL;
494 win->nb_extra_bytes = extra_bytes;
495 win->window_rect = win->visible_rect = win->client_rect = empty_rect;
496 memset( win->extra_bytes, 0, extra_bytes );
497 list_init( &win->children );
498 list_init( &win->unlinked );
500 /* if parent belongs to a different thread and the window isn't */
501 /* top-level, attach the two threads */
502 if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
504 if (!attach_thread_input( current, parent->thread )) goto failed;
506 else /* otherwise just make sure that the thread has a message queue */
508 if (!current->queue && !init_thread_queue( current )) goto failed;
511 /* put it on parent unlinked list */
512 if (parent) list_add_head( &parent->unlinked, &win->entry );
513 else
515 list_init( &win->entry );
516 if (is_desktop_class( class ))
518 assert( !desktop->top_window );
519 desktop->top_window = win;
520 set_process_default_desktop( current->process, desktop, current->desktop );
522 else
524 assert( !desktop->msg_window );
525 desktop->msg_window = win;
529 current->desktop_users++;
530 return win;
532 failed:
533 if (win)
535 if (win->handle) free_user_handle( win->handle );
536 free( win );
538 release_object( desktop );
539 release_class( class );
540 return NULL;
543 /* destroy all windows belonging to a given thread */
544 void destroy_thread_windows( struct thread *thread )
546 user_handle_t handle = 0;
547 struct window *win;
549 while ((win = next_user_handle( &handle, USER_WINDOW )))
551 if (win->thread != thread) continue;
552 if (is_desktop_window( win )) detach_window_thread( win );
553 else destroy_window( win );
557 /* get the desktop window */
558 static struct window *get_desktop_window( struct thread *thread )
560 struct window *top_window;
561 struct desktop *desktop = get_thread_desktop( thread, 0 );
563 if (!desktop) return NULL;
564 top_window = desktop->top_window;
565 release_object( desktop );
566 return top_window;
569 /* check whether child is a descendant of parent */
570 int is_child_window( user_handle_t parent, user_handle_t child )
572 struct window *child_ptr = get_user_object( child, USER_WINDOW );
573 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
575 if (!child_ptr || !parent_ptr) return 0;
576 while (child_ptr->parent)
578 if (child_ptr->parent == parent_ptr) return 1;
579 child_ptr = child_ptr->parent;
581 return 0;
584 /* check whether window is a top-level window */
585 int is_top_level_window( user_handle_t window )
587 struct window *win = get_user_object( window, USER_WINDOW );
588 return (win && (is_desktop_window(win) || is_desktop_window(win->parent)));
591 /* make a window active if possible */
592 int make_window_active( user_handle_t window )
594 struct window *owner, *win = get_window( window );
596 if (!win) return 0;
598 /* set last active for window and its owners */
599 owner = win;
600 while (owner)
602 owner->last_active = win->handle;
603 owner = get_user_object( owner->owner, USER_WINDOW );
605 return 1;
608 /* increment (or decrement) the window paint count */
609 static inline void inc_window_paint_count( struct window *win, int incr )
611 if (win->thread) inc_queue_paint_count( win->thread, incr );
614 /* check if window and all its ancestors are visible */
615 static int is_visible( const struct window *win )
617 while (win)
619 if (!(win->style & WS_VISIBLE)) return 0;
620 win = win->parent;
621 /* if parent is minimized children are not visible */
622 if (win && (win->style & WS_MINIMIZE)) return 0;
624 return 1;
627 /* same as is_visible but takes a window handle */
628 int is_window_visible( user_handle_t window )
630 struct window *win = get_user_object( window, USER_WINDOW );
631 if (!win) return 0;
632 return is_visible( win );
635 int is_window_transparent( user_handle_t window )
637 struct window *win = get_user_object( window, USER_WINDOW );
638 if (!win) return 0;
639 return (win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT);
642 /* check if point is inside the window */
643 static inline int is_point_in_window( struct window *win, int x, int y )
645 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
646 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
647 return 0; /* disabled child */
648 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
649 return 0; /* transparent */
650 if (x < win->visible_rect.left || x >= win->visible_rect.right ||
651 y < win->visible_rect.top || y >= win->visible_rect.bottom)
652 return 0; /* not in window */
653 if (win->win_region &&
654 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
655 return 0; /* not in window region */
656 return 1;
659 /* fill an array with the handles of the children of a specified window */
660 static unsigned int get_children_windows( struct window *parent, atom_t atom, thread_id_t tid,
661 user_handle_t *handles, unsigned int max_count )
663 struct window *ptr;
664 unsigned int count = 0;
666 if (!parent) return 0;
668 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
670 if (atom && get_class_atom(ptr->class) != atom) continue;
671 if (tid && get_thread_id(ptr->thread) != tid) continue;
672 if (handles)
674 if (count >= max_count) break;
675 handles[count] = ptr->handle;
677 count++;
679 return count;
682 /* find child of 'parent' that contains the given point (in parent-relative coords) */
683 static struct window *child_window_from_point( struct window *parent, int x, int y )
685 struct window *ptr;
687 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
689 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
691 /* if window is minimized or disabled, return at once */
692 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
694 /* if point is not in client area, return at once */
695 if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
696 y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
697 return ptr;
699 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
701 return parent; /* not found any child */
704 /* find all children of 'parent' that contain the given point */
705 static int get_window_children_from_point( struct window *parent, int x, int y,
706 struct user_handle_array *array )
708 struct window *ptr;
710 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
712 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
714 /* if point is in client area, and window is not minimized or disabled, check children */
715 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
716 x >= ptr->client_rect.left && x < ptr->client_rect.right &&
717 y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
719 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
720 y - ptr->client_rect.top, array ))
721 return 0;
724 /* now add window to the array */
725 if (!add_handle_to_array( array, ptr->handle )) return 0;
727 return 1;
730 /* find window containing point (in absolute coords) */
731 user_handle_t window_from_point( struct desktop *desktop, int x, int y )
733 struct window *ret;
735 if (!desktop->top_window) return 0;
736 ret = child_window_from_point( desktop->top_window, x, y );
737 return ret->handle;
740 /* return list of all windows containing point (in absolute coords) */
741 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
743 struct window *ptr;
745 /* make point relative to top window */
746 for (ptr = top->parent; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
748 x -= ptr->client_rect.left;
749 y -= ptr->client_rect.top;
752 if (!is_point_in_window( top, x, y )) return 1;
754 /* if point is in client area, and window is not minimized or disabled, check children */
755 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
756 x >= top->client_rect.left && x < top->client_rect.right &&
757 y >= top->client_rect.top && y < top->client_rect.bottom)
759 if (!is_desktop_window(top))
761 x -= top->client_rect.left;
762 y -= top->client_rect.top;
764 if (!get_window_children_from_point( top, x, y, array )) return 0;
766 /* now add window to the array */
767 if (!add_handle_to_array( array, top->handle )) return 0;
768 return 1;
772 /* return the thread owning a window */
773 struct thread *get_window_thread( user_handle_t handle )
775 struct window *win = get_user_object( handle, USER_WINDOW );
776 if (!win || !win->thread) return NULL;
777 return (struct thread *)grab_object( win->thread );
781 /* check if any area of a window needs repainting */
782 static inline int win_needs_repaint( struct window *win )
784 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
788 /* find a child of the specified window that needs repainting */
789 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
791 struct window *ptr, *ret = NULL;
793 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
795 if (!(ptr->style & WS_VISIBLE)) continue;
796 if (ptr->thread == thread && win_needs_repaint( ptr ))
797 ret = ptr;
798 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
799 ret = find_child_to_repaint( ptr, thread );
800 if (ret) break;
803 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
805 /* transparent window, check for non-transparent sibling to paint first */
806 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
808 if (!(ptr->style & WS_VISIBLE)) continue;
809 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
810 if (ptr->thread != thread) continue;
811 if (win_needs_repaint( ptr )) return ptr;
814 return ret;
818 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
819 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
821 struct window *ptr, *win, *top_window = get_desktop_window( thread );
823 if (!top_window) return 0;
825 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
826 else win = find_child_to_repaint( top_window, thread );
828 if (win && parent)
830 /* check that it is a child of the specified parent */
831 for (ptr = win; ptr; ptr = ptr->parent)
832 if (ptr->handle == parent) break;
833 /* otherwise don't return any window, we don't repaint a child before its parent */
834 if (!ptr) win = NULL;
836 if (!win) return 0;
837 win->paint_flags &= ~PAINT_INTERNAL;
838 return win->handle;
842 /* intersect the window region with the specified region, relative to the window parent */
843 static struct region *intersect_window_region( struct region *region, struct window *win )
845 /* make region relative to window rect */
846 offset_region( region, -win->window_rect.left, -win->window_rect.top );
847 if (!intersect_region( region, region, win->win_region )) return NULL;
848 /* make region relative to parent again */
849 offset_region( region, win->window_rect.left, win->window_rect.top );
850 return region;
854 /* convert coordinates from client to screen coords */
855 static inline void client_to_screen( struct window *win, int *x, int *y )
857 for ( ; win && !is_desktop_window(win); win = win->parent)
859 *x += win->client_rect.left;
860 *y += win->client_rect.top;
864 /* convert coordinates from client to screen coords */
865 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
867 for ( ; win && !is_desktop_window(win); win = win->parent)
869 rect->left += win->client_rect.left;
870 rect->right += win->client_rect.left;
871 rect->top += win->client_rect.top;
872 rect->bottom += win->client_rect.top;
876 /* map the region from window to screen coordinates */
877 static inline void map_win_region_to_screen( struct window *win, struct region *region )
879 if (!is_desktop_window(win))
881 int x = win->window_rect.left;
882 int y = win->window_rect.top;
883 client_to_screen( win->parent, &x, &y );
884 offset_region( region, x, y );
889 /* clip all children of a given window out of the visible region */
890 static struct region *clip_children( struct window *parent, struct window *last,
891 struct region *region, int offset_x, int offset_y )
893 struct window *ptr;
894 struct region *tmp = create_empty_region();
896 if (!tmp) return NULL;
897 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
899 if (ptr == last) break;
900 if (!(ptr->style & WS_VISIBLE)) continue;
901 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
902 set_region_rect( tmp, &ptr->visible_rect );
903 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
905 free_region( tmp );
906 return NULL;
908 offset_region( tmp, offset_x, offset_y );
909 if (!(region = subtract_region( region, region, tmp ))) break;
910 if (is_region_empty( region )) break;
912 free_region( tmp );
913 return region;
917 /* offset the coordinates of a rectangle */
918 static inline void offset_rect( rectangle_t *rect, int offset_x, int offset_y )
920 rect->left += offset_x;
921 rect->top += offset_y;
922 rect->right += offset_x;
923 rect->bottom += offset_y;
927 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
928 static void set_region_client_rect( struct region *region, struct window *win )
930 rectangle_t rect;
932 intersect_rect( &rect, &win->window_rect, &win->client_rect );
933 set_region_rect( region, &rect );
937 /* get the top-level window to clip against for a given window */
938 static inline struct window *get_top_clipping_window( struct window *win )
940 while (!(win->paint_flags & PAINT_HAS_SURFACE) && win->parent && !is_desktop_window(win->parent))
941 win = win->parent;
942 return win;
946 /* compute the visible region of a window, in window coordinates */
947 static struct region *get_visible_region( struct window *win, unsigned int flags )
949 struct region *tmp = NULL, *region;
950 int offset_x, offset_y;
952 if (!(region = create_empty_region())) return NULL;
954 /* first check if all ancestors are visible */
956 if (!is_visible( win )) return region; /* empty region */
958 /* create a region relative to the window itself */
960 if ((flags & DCX_PARENTCLIP) && win->parent && !is_desktop_window(win->parent))
962 set_region_client_rect( region, win->parent );
963 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
965 else if (flags & DCX_WINDOW)
967 set_region_rect( region, &win->visible_rect );
968 if (win->win_region && !intersect_window_region( region, win )) goto error;
970 else
972 set_region_client_rect( region, win );
973 if (win->win_region && !intersect_window_region( region, win )) goto error;
976 /* clip children */
978 if (flags & DCX_CLIPCHILDREN)
980 if (is_desktop_window(win)) offset_x = offset_y = 0;
981 else
983 offset_x = win->client_rect.left;
984 offset_y = win->client_rect.top;
986 if (!clip_children( win, NULL, region, offset_x, offset_y )) goto error;
989 /* clip siblings of ancestors */
991 if (is_desktop_window(win)) offset_x = offset_y = 0;
992 else
994 offset_x = win->window_rect.left;
995 offset_y = win->window_rect.top;
998 if ((tmp = create_empty_region()) != NULL)
1000 while (win->parent)
1002 /* we don't clip out top-level siblings as that's up to the native windowing system */
1003 if ((win->style & WS_CLIPSIBLINGS) && !is_desktop_window( win->parent ))
1005 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
1006 if (is_region_empty( region )) break;
1008 /* clip to parent client area */
1009 win = win->parent;
1010 if (!is_desktop_window(win))
1012 offset_x += win->client_rect.left;
1013 offset_y += win->client_rect.top;
1014 offset_region( region, win->client_rect.left, win->client_rect.top );
1016 set_region_client_rect( tmp, win );
1017 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
1018 if (!intersect_region( region, region, tmp )) goto error;
1019 if (is_region_empty( region )) break;
1021 free_region( tmp );
1023 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
1024 return region;
1026 error:
1027 if (tmp) free_region( tmp );
1028 free_region( region );
1029 return NULL;
1033 /* clip all children with a custom pixel format out of the visible region */
1034 static struct region *clip_pixel_format_children( struct window *parent, struct region *parent_clip,
1035 struct region *region, int offset_x, int offset_y )
1037 struct window *ptr;
1038 struct region *clip = create_empty_region();
1040 if (!clip) return NULL;
1042 LIST_FOR_EACH_ENTRY_REV( ptr, &parent->children, struct window, entry )
1044 if (!(ptr->style & WS_VISIBLE)) continue;
1045 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1047 /* add the visible rect */
1048 set_region_rect( clip, &ptr->visible_rect );
1049 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1050 offset_region( clip, offset_x, offset_y );
1051 if (!intersect_region( clip, clip, parent_clip )) break;
1052 if (!union_region( region, region, clip )) break;
1053 if (!(ptr->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))) continue;
1055 /* subtract the client rect if it uses a custom pixel format */
1056 set_region_rect( clip, &ptr->client_rect );
1057 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1058 offset_region( clip, offset_x, offset_y );
1059 if (!intersect_region( clip, clip, parent_clip )) break;
1060 if ((ptr->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1061 break;
1063 if (!clip_pixel_format_children( ptr, clip, region, offset_x + ptr->client_rect.left,
1064 offset_y + ptr->client_rect.top ))
1065 break;
1067 free_region( clip );
1068 return region;
1072 /* compute the visible surface region of a window, in parent coordinates */
1073 static struct region *get_surface_region( struct window *win )
1075 struct region *region, *clip;
1076 int offset_x, offset_y;
1078 /* create a region relative to the window itself */
1080 if (!(region = create_empty_region())) return NULL;
1081 if (!(clip = create_empty_region())) goto error;
1082 set_region_rect( region, &win->visible_rect );
1083 if (win->win_region && !intersect_window_region( region, win )) goto error;
1084 set_region_rect( clip, &win->client_rect );
1085 if (win->win_region && !intersect_window_region( clip, win )) goto error;
1087 if ((win->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1088 goto error;
1090 /* clip children */
1092 if (!is_desktop_window(win))
1094 offset_x = win->client_rect.left;
1095 offset_y = win->client_rect.top;
1097 else offset_x = offset_y = 0;
1099 if (!clip_pixel_format_children( win, clip, region, offset_x, offset_y )) goto error;
1101 free_region( clip );
1102 return region;
1104 error:
1105 if (clip) free_region( clip );
1106 free_region( region );
1107 return NULL;
1111 /* get the window class of a window */
1112 struct window_class* get_window_class( user_handle_t window )
1114 struct window *win;
1115 if (!(win = get_window( window ))) return NULL;
1116 if (!win->class) set_error( STATUS_ACCESS_DENIED );
1117 return win->class;
1120 /* determine the window visible rectangle, i.e. window or client rect cropped by parent rects */
1121 /* the returned rectangle is in window coordinates; return 0 if rectangle is empty */
1122 static int get_window_visible_rect( struct window *win, rectangle_t *rect, int frame )
1124 int offset_x = 0, offset_y = 0;
1126 if (!(win->style & WS_VISIBLE)) return 0;
1128 *rect = frame ? win->window_rect : win->client_rect;
1129 if (!is_desktop_window(win))
1131 offset_x = win->window_rect.left;
1132 offset_y = win->window_rect.top;
1135 while (win->parent)
1137 win = win->parent;
1138 if (!(win->style & WS_VISIBLE) || win->style & WS_MINIMIZE) return 0;
1139 if (!is_desktop_window(win))
1141 offset_x += win->client_rect.left;
1142 offset_y += win->client_rect.top;
1143 offset_rect( rect, win->client_rect.left, win->client_rect.top );
1145 if (!intersect_rect( rect, rect, &win->client_rect )) return 0;
1146 if (!intersect_rect( rect, rect, &win->window_rect )) return 0;
1148 offset_rect( rect, -offset_x, -offset_y );
1149 return 1;
1152 /* return a copy of the specified region cropped to the window client or frame rectangle, */
1153 /* and converted from client to window coordinates. Helper for (in)validate_window. */
1154 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
1156 rectangle_t rect;
1157 struct region *tmp;
1159 if (!get_window_visible_rect( win, &rect, frame )) return NULL;
1160 if (!(tmp = create_empty_region())) return NULL;
1161 set_region_rect( tmp, &rect );
1163 if (region)
1165 /* map it to client coords */
1166 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1167 win->window_rect.top - win->client_rect.top );
1169 /* intersect specified region with bounding rect */
1170 if (!intersect_region( tmp, region, tmp )) goto done;
1171 if (is_region_empty( tmp )) goto done;
1173 /* map it back to window coords */
1174 offset_region( tmp, win->client_rect.left - win->window_rect.left,
1175 win->client_rect.top - win->window_rect.top );
1177 return tmp;
1179 done:
1180 free_region( tmp );
1181 return NULL;
1185 /* set a region as new update region for the window */
1186 static void set_update_region( struct window *win, struct region *region )
1188 if (region && !is_region_empty( region ))
1190 if (!win->update_region) inc_window_paint_count( win, 1 );
1191 else free_region( win->update_region );
1192 win->update_region = region;
1194 else
1196 if (win->update_region)
1198 inc_window_paint_count( win, -1 );
1199 free_region( win->update_region );
1201 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
1202 win->update_region = NULL;
1203 if (region) free_region( region );
1208 /* add a region to the update region; the passed region is freed or reused */
1209 static int add_update_region( struct window *win, struct region *region )
1211 if (win->update_region && !union_region( region, win->update_region, region ))
1213 free_region( region );
1214 return 0;
1216 set_update_region( win, region );
1217 return 1;
1221 /* crop the update region of children to the specified rectangle, in client coords */
1222 static void crop_children_update_region( struct window *win, rectangle_t *rect )
1224 struct window *child;
1225 struct region *tmp;
1226 rectangle_t child_rect;
1228 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1230 if (!(child->style & WS_VISIBLE)) continue;
1231 if (!rect) /* crop everything out */
1233 crop_children_update_region( child, NULL );
1234 set_update_region( child, NULL );
1235 continue;
1238 /* nothing to do if child is completely inside rect */
1239 if (child->window_rect.left >= rect->left &&
1240 child->window_rect.top >= rect->top &&
1241 child->window_rect.right <= rect->right &&
1242 child->window_rect.bottom <= rect->bottom) continue;
1244 /* map to child client coords and crop grand-children */
1245 child_rect = *rect;
1246 offset_rect( &child_rect, -child->client_rect.left, -child->client_rect.top );
1247 crop_children_update_region( child, &child_rect );
1249 /* now crop the child itself */
1250 if (!child->update_region) continue;
1251 if (!(tmp = create_empty_region())) continue;
1252 set_region_rect( tmp, rect );
1253 offset_region( tmp, -child->window_rect.left, -child->window_rect.top );
1254 if (intersect_region( tmp, child->update_region, tmp )) set_update_region( child, tmp );
1255 else free_region( tmp );
1260 /* validate the non client area of a window */
1261 static void validate_non_client( struct window *win )
1263 struct region *tmp;
1264 rectangle_t rect;
1266 if (!win->update_region) return; /* nothing to do */
1268 /* get client rect in window coords */
1269 rect.left = win->client_rect.left - win->window_rect.left;
1270 rect.top = win->client_rect.top - win->window_rect.top;
1271 rect.right = win->client_rect.right - win->window_rect.left;
1272 rect.bottom = win->client_rect.bottom - win->window_rect.top;
1274 if ((tmp = create_empty_region()))
1276 set_region_rect( tmp, &rect );
1277 if (intersect_region( tmp, win->update_region, tmp ))
1278 set_update_region( win, tmp );
1279 else
1280 free_region( tmp );
1282 win->paint_flags &= ~PAINT_NONCLIENT;
1286 /* validate a window completely so that we don't get any further paint messages for it */
1287 static void validate_whole_window( struct window *win )
1289 set_update_region( win, NULL );
1291 if (win->paint_flags & PAINT_INTERNAL)
1293 win->paint_flags &= ~PAINT_INTERNAL;
1294 inc_window_paint_count( win, -1 );
1299 /* validate a window's children so that we don't get any further paint messages for it */
1300 static void validate_children( struct window *win )
1302 struct window *child;
1304 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1306 if (!(child->style & WS_VISIBLE)) continue;
1307 validate_children(child);
1308 validate_whole_window(child);
1313 /* validate the update region of a window on all parents; helper for get_update_region */
1314 static void validate_parents( struct window *child )
1316 int offset_x = 0, offset_y = 0;
1317 struct window *win = child;
1318 struct region *tmp = NULL;
1320 if (!child->update_region) return;
1322 while (win->parent)
1324 /* map to parent client coords */
1325 offset_x += win->window_rect.left;
1326 offset_y += win->window_rect.top;
1328 win = win->parent;
1330 /* and now map to window coords */
1331 offset_x += win->client_rect.left - win->window_rect.left;
1332 offset_y += win->client_rect.top - win->window_rect.top;
1334 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1336 if (!tmp && !(tmp = create_empty_region())) return;
1337 offset_region( child->update_region, offset_x, offset_y );
1338 if (subtract_region( tmp, win->update_region, child->update_region ))
1340 set_update_region( win, tmp );
1341 tmp = NULL;
1343 /* restore child coords */
1344 offset_region( child->update_region, -offset_x, -offset_y );
1347 if (tmp) free_region( tmp );
1351 /* add/subtract a region (in client coordinates) to the update region of the window */
1352 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1354 struct region *tmp;
1355 struct window *child;
1357 if (flags & RDW_INVALIDATE)
1359 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1361 if (!add_update_region( win, tmp )) return;
1363 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1364 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1366 else if (flags & RDW_VALIDATE)
1368 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1370 set_update_region( win, NULL );
1372 else if (win->update_region)
1374 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1376 if (!subtract_region( tmp, win->update_region, tmp ))
1378 free_region( tmp );
1379 return;
1381 set_update_region( win, tmp );
1383 if (flags & RDW_NOFRAME) validate_non_client( win );
1384 if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1388 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1390 win->paint_flags |= PAINT_INTERNAL;
1391 inc_window_paint_count( win, 1 );
1393 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1395 win->paint_flags &= ~PAINT_INTERNAL;
1396 inc_window_paint_count( win, -1 );
1399 /* now process children recursively */
1401 if (flags & RDW_NOCHILDREN) return;
1402 if (win->style & WS_MINIMIZE) return;
1403 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1405 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1407 /* map to client coordinates */
1408 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1409 win->window_rect.top - win->client_rect.top );
1411 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1413 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1415 if (!(child->style & WS_VISIBLE)) continue;
1416 if (!rect_in_region( tmp, &child->window_rect )) continue;
1417 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1418 redraw_window( child, tmp, 1, flags );
1419 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1421 free_region( tmp );
1425 /* retrieve the update flags for a window depending on the state of the update region */
1426 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1428 unsigned int ret = 0;
1430 if (flags & UPDATE_NONCLIENT)
1432 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1434 if (flags & UPDATE_ERASE)
1436 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1438 if (flags & UPDATE_PAINT)
1440 if (win->update_region)
1442 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1443 ret |= UPDATE_PAINT;
1446 if (flags & UPDATE_INTERNALPAINT)
1448 if (win->paint_flags & PAINT_INTERNAL)
1450 ret |= UPDATE_INTERNALPAINT;
1451 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1454 return ret;
1458 /* iterate through the children of the given window until we find one with some update flags */
1459 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1460 unsigned int flags, struct window **child )
1462 struct window *ptr;
1463 unsigned int ret = 0;
1465 /* first make sure we want to iterate children at all */
1467 if (win->style & WS_MINIMIZE) return 0;
1469 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1470 * here we only want to repaint children of windows that clip them, others
1471 * need to wait for WM_PAINT to be done in the parent first.
1473 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1475 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1477 if (from_child) /* skip all children until from_child is found */
1479 if (ptr == from_child) from_child = NULL;
1480 continue;
1482 if (!(ptr->style & WS_VISIBLE)) continue;
1483 if ((ret = get_update_flags( ptr, flags )) != 0)
1485 *child = ptr;
1486 break;
1488 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1490 return ret;
1493 /* iterate through children and siblings of the given window until we find one with some update flags */
1494 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1495 unsigned int flags, struct window **child )
1497 unsigned int ret;
1498 struct window *ptr, *from_sibling = NULL;
1500 /* if some parent is not visible start from the next sibling */
1502 if (!is_visible( win )) return 0;
1503 for (ptr = from_child; ptr; ptr = ptr->parent)
1505 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1506 if (ptr == win) break;
1509 /* non-client painting must be delayed if one of the parents is going to
1510 * be repainted and doesn't clip children */
1512 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1514 for (ptr = win->parent; ptr; ptr = ptr->parent)
1516 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1517 return 0;
1519 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1521 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1523 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1524 if (ptr == win) break;
1530 /* check window itself (only if not restarting from a child) */
1532 if (!from_child)
1534 if ((ret = get_update_flags( win, flags )))
1536 *child = win;
1537 return ret;
1539 from_child = win;
1542 /* now check children */
1544 if (flags & UPDATE_NOCHILDREN) return 0;
1545 if (!from_sibling)
1547 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1548 from_sibling = from_child;
1551 /* then check siblings and parent siblings */
1553 while (from_sibling->parent && from_sibling != win)
1555 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1556 return ret;
1557 from_sibling = from_sibling->parent;
1559 return 0;
1563 /* expose the areas revealed by a vis region change on the window parent */
1564 /* returns the region exposed on the window itself (in client coordinates) */
1565 static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
1566 struct region *old_vis_rgn )
1568 struct region *new_vis_rgn, *exposed_rgn;
1570 if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
1572 if ((exposed_rgn = create_empty_region()))
1574 if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
1576 /* make it relative to the new client area */
1577 offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
1578 win->window_rect.top - win->client_rect.top );
1580 else
1582 free_region( exposed_rgn );
1583 exposed_rgn = NULL;
1587 if (win->parent && !is_desktop_window( win->parent ))
1589 /* make it relative to the old window pos for subtracting */
1590 offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
1591 win->window_rect.top - old_window_rect->top );
1593 if ((win->parent->style & WS_CLIPCHILDREN) ?
1594 subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) :
1595 xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1597 if (!is_region_empty( new_vis_rgn ))
1599 /* make it relative to parent */
1600 offset_region( new_vis_rgn, old_window_rect->left, old_window_rect->top );
1601 redraw_window( win->parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1605 free_region( new_vis_rgn );
1606 return exposed_rgn;
1610 /* set the window and client rectangles, updating the update region if necessary */
1611 static void set_window_pos( struct window *win, struct window *previous,
1612 unsigned int swp_flags, const rectangle_t *window_rect,
1613 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1614 const rectangle_t *valid_rects )
1616 struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
1617 const rectangle_t old_window_rect = win->window_rect;
1618 const rectangle_t old_visible_rect = win->visible_rect;
1619 const rectangle_t old_client_rect = win->client_rect;
1620 rectangle_t rect;
1621 int client_changed, frame_changed;
1622 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1624 if (win->parent && !is_visible( win->parent )) visible = 0;
1626 if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
1628 /* set the new window info before invalidating anything */
1630 win->window_rect = *window_rect;
1631 win->visible_rect = *visible_rect;
1632 win->client_rect = *client_rect;
1633 if (!(swp_flags & SWP_NOZORDER) && win->parent) link_window( win, previous );
1634 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1635 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1637 /* keep children at the same position relative to top right corner when the parent is mirrored */
1638 if (win->ex_style & WS_EX_LAYOUTRTL)
1640 struct window *child;
1641 int old_size = old_client_rect.right - old_client_rect.left;
1642 int new_size = win->client_rect.right - win->client_rect.left;
1644 if (old_size != new_size) LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1646 offset_rect( &child->window_rect, new_size - old_size, 0 );
1647 offset_rect( &child->visible_rect, new_size - old_size, 0 );
1648 offset_rect( &child->client_rect, new_size - old_size, 0 );
1652 /* reset cursor clip rectangle when the desktop changes size */
1653 if (win == win->desktop->top_window) win->desktop->cursor.clip = *window_rect;
1655 /* if the window is not visible, everything is easy */
1656 if (!visible) return;
1658 /* expose anything revealed by the change */
1660 if (!(swp_flags & SWP_NOREDRAW))
1661 exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
1663 if (!(win->style & WS_VISIBLE))
1665 /* clear the update region since the window is no longer visible */
1666 validate_whole_window( win );
1667 validate_children( win );
1668 goto done;
1671 /* crop update region to the new window rect */
1673 if (win->update_region)
1675 if (get_window_visible_rect( win, &rect, 1 ))
1677 struct region *tmp = create_empty_region();
1678 if (tmp)
1680 set_region_rect( tmp, &rect );
1681 if (intersect_region( tmp, win->update_region, tmp ))
1682 set_update_region( win, tmp );
1683 else
1684 free_region( tmp );
1687 else set_update_region( win, NULL ); /* visible rect is empty */
1690 /* crop children regions to the new window rect */
1692 if (get_window_visible_rect( win, &rect, 0 ))
1694 /* map to client coords */
1695 offset_rect( &rect, win->window_rect.left - win->client_rect.left,
1696 win->window_rect.top - win->client_rect.top );
1697 crop_children_update_region( win, &rect );
1699 else crop_children_update_region( win, NULL );
1701 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1703 /* expose the whole non-client area if it changed in any way */
1705 if (swp_flags & SWP_NOCOPYBITS)
1707 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1708 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1709 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
1710 client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
1712 else
1714 /* assume the bits have been moved to follow the window rect */
1715 int x_offset = window_rect->left - old_window_rect.left;
1716 int y_offset = window_rect->top - old_window_rect.top;
1717 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1718 window_rect->right - old_window_rect.right != x_offset ||
1719 window_rect->bottom - old_window_rect.bottom != y_offset ||
1720 visible_rect->left - old_visible_rect.left != x_offset ||
1721 visible_rect->right - old_visible_rect.right != x_offset ||
1722 visible_rect->top - old_visible_rect.top != y_offset ||
1723 visible_rect->bottom - old_visible_rect.bottom != y_offset);
1724 client_changed = (client_rect->left - old_client_rect.left != x_offset ||
1725 client_rect->right - old_client_rect.right != x_offset ||
1726 client_rect->top - old_client_rect.top != y_offset ||
1727 client_rect->bottom - old_client_rect.bottom != y_offset ||
1728 !valid_rects ||
1729 memcmp( &valid_rects[0], client_rect, sizeof(*client_rect) ));
1732 if (frame_changed || client_changed)
1734 struct region *win_rgn = old_vis_rgn; /* reuse previous region */
1736 set_region_rect( win_rgn, window_rect );
1737 if (valid_rects)
1739 /* subtract the valid portion of client rect from the total region */
1740 struct region *tmp = create_empty_region();
1741 if (tmp)
1743 set_region_rect( tmp, &valid_rects[0] );
1744 /* subtract update region since invalid parts of the valid rect won't be copied */
1745 if (win->update_region)
1747 offset_region( tmp, -window_rect->left, -window_rect->top );
1748 subtract_region( tmp, tmp, win->update_region );
1749 offset_region( tmp, window_rect->left, window_rect->top );
1751 if (subtract_region( tmp, win_rgn, tmp )) win_rgn = tmp;
1752 else free_region( tmp );
1755 if (!is_desktop_window(win))
1756 offset_region( win_rgn, -client_rect->left, -client_rect->top );
1757 if (exposed_rgn)
1759 union_region( exposed_rgn, exposed_rgn, win_rgn );
1760 if (win_rgn != old_vis_rgn) free_region( win_rgn );
1762 else
1764 exposed_rgn = win_rgn;
1765 if (win_rgn == old_vis_rgn) old_vis_rgn = NULL;
1769 if (exposed_rgn)
1770 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1772 done:
1773 if (old_vis_rgn) free_region( old_vis_rgn );
1774 if (exposed_rgn) free_region( exposed_rgn );
1775 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1779 /* set the window region, updating the update region if necessary */
1780 static void set_window_region( struct window *win, struct region *region, int redraw )
1782 struct region *old_vis_rgn = NULL, *exposed_rgn;
1784 /* no need to redraw if window is not visible */
1785 if (redraw && !is_visible( win )) redraw = 0;
1787 if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
1789 if (win->win_region) free_region( win->win_region );
1790 win->win_region = region;
1792 /* expose anything revealed by the change */
1793 if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
1795 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1796 free_region( exposed_rgn );
1799 if (old_vis_rgn) free_region( old_vis_rgn );
1800 clear_error(); /* we ignore out of memory errors since the region has been set */
1804 /* destroy a window */
1805 void destroy_window( struct window *win )
1807 /* hide the window */
1808 if (is_visible(win))
1810 struct region *vis_rgn = get_visible_region( win, DCX_WINDOW );
1811 win->style &= ~WS_VISIBLE;
1812 if (vis_rgn)
1814 struct region *exposed_rgn = expose_window( win, &win->window_rect, vis_rgn );
1815 if (exposed_rgn) free_region( exposed_rgn );
1816 free_region( vis_rgn );
1818 validate_whole_window( win );
1819 validate_children( win );
1822 /* destroy all children */
1823 while (!list_empty(&win->children))
1824 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
1825 while (!list_empty(&win->unlinked))
1826 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
1828 /* reset global window pointers, if the corresponding window is destroyed */
1829 if (win == shell_window) shell_window = NULL;
1830 if (win == shell_listview) shell_listview = NULL;
1831 if (win == progman_window) progman_window = NULL;
1832 if (win == taskman_window) taskman_window = NULL;
1833 free_hotkeys( win->desktop, win->handle );
1834 free_user_handle( win->handle );
1835 destroy_properties( win );
1836 list_remove( &win->entry );
1837 if (is_desktop_window(win))
1839 struct desktop *desktop = win->desktop;
1840 assert( desktop->top_window == win || desktop->msg_window == win );
1841 if (desktop->top_window == win) desktop->top_window = NULL;
1842 else desktop->msg_window = NULL;
1844 detach_window_thread( win );
1845 if (win->win_region) free_region( win->win_region );
1846 if (win->update_region) free_region( win->update_region );
1847 if (win->class) release_class( win->class );
1848 free( win->text );
1849 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
1850 free( win );
1854 /* create a window */
1855 DECL_HANDLER(create_window)
1857 struct window *win, *parent = NULL, *owner = NULL;
1858 struct unicode_str cls_name;
1859 atom_t atom;
1861 reply->handle = 0;
1862 if (req->parent && !(parent = get_window( req->parent ))) return;
1864 if (req->owner)
1866 if (!(owner = get_window( req->owner ))) return;
1867 if (is_desktop_window(owner)) owner = NULL;
1868 else if (parent && !is_desktop_window(parent))
1870 /* an owned window must be created as top-level */
1871 set_error( STATUS_ACCESS_DENIED );
1872 return;
1874 else /* owner must be a top-level window */
1875 while (!is_desktop_window(owner->parent)) owner = owner->parent;
1878 get_req_unicode_str( &cls_name );
1879 atom = cls_name.len ? find_global_atom( NULL, &cls_name ) : req->atom;
1881 if (!(win = create_window( parent, owner, atom, req->instance ))) return;
1883 reply->handle = win->handle;
1884 reply->parent = win->parent ? win->parent->handle : 0;
1885 reply->owner = win->owner;
1886 reply->extra = win->nb_extra_bytes;
1887 reply->class_ptr = get_class_client_ptr( win->class );
1891 /* set the parent of a window */
1892 DECL_HANDLER(set_parent)
1894 struct window *win, *parent = NULL;
1896 if (!(win = get_window( req->handle ))) return;
1897 if (req->parent && !(parent = get_window( req->parent ))) return;
1899 if (is_desktop_window(win))
1901 set_error( STATUS_INVALID_PARAMETER );
1902 return;
1904 reply->old_parent = win->parent->handle;
1905 reply->full_parent = parent ? parent->handle : 0;
1906 set_parent_window( win, parent );
1910 /* destroy a window */
1911 DECL_HANDLER(destroy_window)
1913 struct window *win = get_window( req->handle );
1914 if (win)
1916 if (!is_desktop_window(win)) destroy_window( win );
1917 else if (win->thread == current) detach_window_thread( win );
1918 else set_error( STATUS_ACCESS_DENIED );
1923 /* retrieve the desktop window for the current thread */
1924 DECL_HANDLER(get_desktop_window)
1926 struct desktop *desktop = get_thread_desktop( current, 0 );
1928 if (!desktop) return;
1930 if (!desktop->top_window && req->force) /* create it */
1932 if ((desktop->top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
1934 detach_window_thread( desktop->top_window );
1935 desktop->top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1939 if (!desktop->msg_window && req->force) /* create it */
1941 static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
1942 static const struct unicode_str name = { messageW, sizeof(messageW) };
1943 atom_t atom = add_global_atom( NULL, &name );
1944 if (atom && (desktop->msg_window = create_window( NULL, NULL, atom, 0 )))
1946 detach_window_thread( desktop->msg_window );
1947 desktop->msg_window->style = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1951 reply->top_window = desktop->top_window ? desktop->top_window->handle : 0;
1952 reply->msg_window = desktop->msg_window ? desktop->msg_window->handle : 0;
1953 release_object( desktop );
1957 /* set a window owner */
1958 DECL_HANDLER(set_window_owner)
1960 struct window *win = get_window( req->handle );
1961 struct window *owner = NULL, *ptr;
1963 if (!win) return;
1964 if (req->owner && !(owner = get_window( req->owner ))) return;
1965 if (is_desktop_window(win))
1967 set_error( STATUS_ACCESS_DENIED );
1968 return;
1971 /* make sure owner is not a successor of window */
1972 for (ptr = owner; ptr; ptr = ptr->owner ? get_window( ptr->owner ) : NULL)
1974 if (ptr == win)
1976 set_error( STATUS_INVALID_PARAMETER );
1977 return;
1981 reply->prev_owner = win->owner;
1982 reply->full_owner = win->owner = owner ? owner->handle : 0;
1986 /* get information from a window handle */
1987 DECL_HANDLER(get_window_info)
1989 struct window *win = get_window( req->handle );
1991 reply->full_handle = 0;
1992 reply->tid = reply->pid = 0;
1993 if (win)
1995 reply->full_handle = win->handle;
1996 reply->last_active = win->handle;
1997 reply->is_unicode = win->is_unicode;
1998 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
1999 if (win->thread)
2001 reply->tid = get_thread_id( win->thread );
2002 reply->pid = get_process_id( win->thread->process );
2003 reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
2009 /* set some information in a window */
2010 DECL_HANDLER(set_window_info)
2012 struct window *win = get_window( req->handle );
2014 if (!win) return;
2015 if (req->flags && is_desktop_window(win) && win->thread != current)
2017 set_error( STATUS_ACCESS_DENIED );
2018 return;
2020 if (req->extra_size > sizeof(req->extra_value) ||
2021 req->extra_offset < -1 ||
2022 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
2024 set_win32_error( ERROR_INVALID_INDEX );
2025 return;
2027 if (req->extra_offset != -1)
2029 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
2031 else if (req->flags & SET_WIN_EXTRA)
2033 set_win32_error( ERROR_INVALID_INDEX );
2034 return;
2036 reply->old_style = win->style;
2037 reply->old_ex_style = win->ex_style;
2038 reply->old_id = win->id;
2039 reply->old_instance = win->instance;
2040 reply->old_user_data = win->user_data;
2041 if (req->flags & SET_WIN_STYLE) win->style = req->style;
2042 if (req->flags & SET_WIN_EXSTYLE)
2044 /* WS_EX_TOPMOST can only be changed for unlinked windows */
2045 if (!win->is_linked) win->ex_style = req->ex_style;
2046 else win->ex_style = (req->ex_style & ~WS_EX_TOPMOST) | (win->ex_style & WS_EX_TOPMOST);
2047 if (!(win->ex_style & WS_EX_LAYERED)) win->is_layered = 0;
2049 if (req->flags & SET_WIN_ID) win->id = req->id;
2050 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
2051 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
2052 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
2053 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
2054 &req->extra_value, req->extra_size );
2056 /* changing window style triggers a non-client paint */
2057 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
2061 /* get a list of the window parents, up to the root of the tree */
2062 DECL_HANDLER(get_window_parents)
2064 struct window *ptr, *win = get_window( req->handle );
2065 int total = 0;
2066 user_handle_t *data;
2067 data_size_t len;
2069 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
2071 reply->count = total;
2072 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2073 if (len && ((data = set_reply_data_size( len ))))
2075 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
2076 *data++ = ptr->handle;
2081 /* get a list of the window children */
2082 DECL_HANDLER(get_window_children)
2084 struct window *parent = NULL;
2085 unsigned int total;
2086 user_handle_t *data;
2087 data_size_t len;
2088 struct unicode_str cls_name;
2089 atom_t atom = req->atom;
2090 struct desktop *desktop = NULL;
2092 get_req_unicode_str( &cls_name );
2093 if (cls_name.len && !(atom = find_global_atom( NULL, &cls_name ))) return;
2095 if (req->desktop)
2097 if (!(desktop = get_desktop_obj( current->process, req->desktop, DESKTOP_ENUMERATE ))) return;
2098 parent = desktop->top_window;
2100 else
2102 if (req->parent && !(parent = get_window( req->parent ))) return;
2103 if (!parent && !(desktop = get_thread_desktop( current, 0 ))) return;
2106 if (parent)
2107 total = get_children_windows( parent, atom, req->tid, NULL, 0 );
2108 else
2109 total = get_children_windows( desktop->top_window, atom, req->tid, NULL, 0 ) +
2110 get_children_windows( desktop->msg_window, atom, req->tid, NULL, 0 );
2112 reply->count = total;
2113 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2114 if (len && ((data = set_reply_data_size( len ))))
2116 if (parent) get_children_windows( parent, atom, req->tid, data, len / sizeof(user_handle_t) );
2117 else
2119 total = get_children_windows( desktop->top_window, atom, req->tid,
2120 data, len / sizeof(user_handle_t) );
2121 data += total;
2122 len -= total * sizeof(user_handle_t);
2123 if (len >= sizeof(user_handle_t))
2124 get_children_windows( desktop->msg_window, atom, req->tid,
2125 data, len / sizeof(user_handle_t) );
2128 if (desktop) release_object( desktop );
2132 /* get a list of the window children that contain a given point */
2133 DECL_HANDLER(get_window_children_from_point)
2135 struct user_handle_array array;
2136 struct window *parent = get_window( req->parent );
2137 data_size_t len;
2139 if (!parent) return;
2141 array.handles = NULL;
2142 array.count = 0;
2143 array.total = 0;
2144 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
2146 reply->count = array.count;
2147 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
2148 if (len) set_reply_data_ptr( array.handles, len );
2149 else free( array.handles );
2153 /* get window tree information from a window handle */
2154 DECL_HANDLER(get_window_tree)
2156 struct window *ptr, *win = get_window( req->handle );
2158 if (!win) return;
2160 reply->parent = 0;
2161 reply->owner = 0;
2162 reply->next_sibling = 0;
2163 reply->prev_sibling = 0;
2164 reply->first_sibling = 0;
2165 reply->last_sibling = 0;
2166 reply->first_child = 0;
2167 reply->last_child = 0;
2169 if (win->parent)
2171 struct window *parent = win->parent;
2172 reply->parent = parent->handle;
2173 reply->owner = win->owner;
2174 if (win->is_linked)
2176 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
2177 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
2179 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
2180 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
2182 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
2183 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
2187 /* set the position and Z order of a window */
2188 DECL_HANDLER(set_window_pos)
2190 rectangle_t window_rect, client_rect, visible_rect;
2191 struct window *previous = NULL;
2192 struct window *top, *win = get_window( req->handle );
2193 unsigned int flags = req->swp_flags;
2195 if (!win) return;
2196 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
2198 if (!(flags & SWP_NOZORDER))
2200 switch ((int)req->previous)
2202 case 0: /* HWND_TOP */
2203 previous = WINPTR_TOP;
2204 break;
2205 case 1: /* HWND_BOTTOM */
2206 previous = WINPTR_BOTTOM;
2207 break;
2208 case -1: /* HWND_TOPMOST */
2209 previous = WINPTR_TOPMOST;
2210 break;
2211 case -2: /* HWND_NOTOPMOST */
2212 previous = WINPTR_NOTOPMOST;
2213 break;
2214 default:
2215 if (!(previous = get_window( req->previous ))) return;
2216 /* previous must be a sibling */
2217 if (previous->parent != win->parent)
2219 set_error( STATUS_INVALID_PARAMETER );
2220 return;
2222 break;
2224 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
2227 /* windows that use UpdateLayeredWindow don't trigger repaints */
2228 if ((win->ex_style & WS_EX_LAYERED) && !win->is_layered) flags |= SWP_NOREDRAW;
2230 /* window rectangle must be ordered properly */
2231 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
2233 set_error( STATUS_INVALID_PARAMETER );
2234 return;
2237 window_rect = visible_rect = req->window;
2238 client_rect = req->client;
2239 if (get_req_data_size() >= sizeof(rectangle_t))
2240 memcpy( &visible_rect, get_req_data(), sizeof(rectangle_t) );
2241 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2243 mirror_rect( &win->parent->client_rect, &window_rect );
2244 mirror_rect( &win->parent->client_rect, &visible_rect );
2245 mirror_rect( &win->parent->client_rect, &client_rect );
2248 win->paint_flags = (win->paint_flags & ~PAINT_CLIENT_FLAGS) | (req->paint_flags & PAINT_CLIENT_FLAGS);
2249 if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
2251 if (get_req_data_size() >= 3 * sizeof(rectangle_t))
2253 rectangle_t valid_rects[2];
2254 memcpy( valid_rects, (const rectangle_t *)get_req_data() + 1, 2 * sizeof(rectangle_t) );
2255 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2257 mirror_rect( &win->parent->client_rect, &valid_rects[0] );
2258 mirror_rect( &win->parent->client_rect, &valid_rects[1] );
2260 set_window_pos( win, previous, flags, &window_rect, &client_rect, &visible_rect, valid_rects );
2262 else set_window_pos( win, previous, flags, &window_rect, &client_rect, &visible_rect, NULL );
2264 reply->new_style = win->style;
2265 reply->new_ex_style = win->ex_style;
2267 top = get_top_clipping_window( win );
2268 if (is_visible( top ) &&
2269 (top->paint_flags & PAINT_HAS_SURFACE) &&
2270 (top->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD)))
2271 reply->surface_win = top->handle;
2275 /* get the window and client rectangles of a window */
2276 DECL_HANDLER(get_window_rectangles)
2278 struct window *win = get_window( req->handle );
2280 if (!win) return;
2282 reply->window = win->window_rect;
2283 reply->visible = win->visible_rect;
2284 reply->client = win->client_rect;
2286 switch (req->relative)
2288 case COORDS_CLIENT:
2289 offset_rect( &reply->window, -win->client_rect.left, -win->client_rect.top );
2290 offset_rect( &reply->visible, -win->client_rect.left, -win->client_rect.top );
2291 offset_rect( &reply->client, -win->client_rect.left, -win->client_rect.top );
2292 if (win->ex_style & WS_EX_LAYOUTRTL)
2294 mirror_rect( &win->client_rect, &reply->window );
2295 mirror_rect( &win->client_rect, &reply->visible );
2297 break;
2298 case COORDS_WINDOW:
2299 offset_rect( &reply->window, -win->window_rect.left, -win->window_rect.top );
2300 offset_rect( &reply->visible, -win->window_rect.left, -win->window_rect.top );
2301 offset_rect( &reply->client, -win->window_rect.left, -win->window_rect.top );
2302 if (win->ex_style & WS_EX_LAYOUTRTL)
2304 mirror_rect( &win->window_rect, &reply->visible );
2305 mirror_rect( &win->window_rect, &reply->client );
2307 break;
2308 case COORDS_PARENT:
2309 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2311 mirror_rect( &win->parent->client_rect, &reply->window );
2312 mirror_rect( &win->parent->client_rect, &reply->visible );
2313 mirror_rect( &win->parent->client_rect, &reply->client );
2315 break;
2316 case COORDS_SCREEN:
2317 client_to_screen_rect( win->parent, &reply->window );
2318 client_to_screen_rect( win->parent, &reply->visible );
2319 client_to_screen_rect( win->parent, &reply->client );
2320 break;
2321 default:
2322 set_error( STATUS_INVALID_PARAMETER );
2323 break;
2328 /* get the window text */
2329 DECL_HANDLER(get_window_text)
2331 struct window *win = get_window( req->handle );
2333 if (win && win->text)
2335 data_size_t len = strlenW( win->text ) * sizeof(WCHAR);
2336 if (len > get_reply_max_size()) len = get_reply_max_size();
2337 set_reply_data( win->text, len );
2342 /* set the window text */
2343 DECL_HANDLER(set_window_text)
2345 struct window *win = get_window( req->handle );
2347 if (win)
2349 WCHAR *text = NULL;
2350 data_size_t len = get_req_data_size() / sizeof(WCHAR);
2351 if (len)
2353 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
2354 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
2355 text[len] = 0;
2357 free( win->text );
2358 win->text = text;
2363 /* get the coordinates offset between two windows */
2364 DECL_HANDLER(get_windows_offset)
2366 struct window *win;
2367 int mirror_from = 0, mirror_to = 0;
2369 reply->x = reply->y = 0;
2370 if (req->from)
2372 if (!(win = get_window( req->from ))) return;
2373 if (win->ex_style & WS_EX_LAYOUTRTL)
2375 mirror_from = 1;
2376 reply->x += win->client_rect.right - win->client_rect.left;
2378 while (win && !is_desktop_window(win))
2380 reply->x += win->client_rect.left;
2381 reply->y += win->client_rect.top;
2382 win = win->parent;
2385 if (req->to)
2387 if (!(win = get_window( req->to ))) return;
2388 if (win->ex_style & WS_EX_LAYOUTRTL)
2390 mirror_to = 1;
2391 reply->x -= win->client_rect.right - win->client_rect.left;
2393 while (win && !is_desktop_window(win))
2395 reply->x -= win->client_rect.left;
2396 reply->y -= win->client_rect.top;
2397 win = win->parent;
2400 if (mirror_from) reply->x = -reply->x;
2401 reply->mirror = mirror_from ^ mirror_to;
2405 /* get the visible region of a window */
2406 DECL_HANDLER(get_visible_region)
2408 struct region *region;
2409 struct window *top, *win = get_window( req->window );
2411 if (!win) return;
2413 top = get_top_clipping_window( win );
2414 if ((region = get_visible_region( win, req->flags )))
2416 rectangle_t *data;
2417 map_win_region_to_screen( win, region );
2418 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2419 if (data) set_reply_data_ptr( data, reply->total_size );
2421 reply->top_win = top->handle;
2422 reply->top_rect = top->visible_rect;
2424 if (!is_desktop_window(win))
2426 reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
2427 client_to_screen_rect( top->parent, &reply->top_rect );
2428 client_to_screen_rect( win->parent, &reply->win_rect );
2430 else
2432 reply->win_rect.left = 0;
2433 reply->win_rect.top = 0;
2434 reply->win_rect.right = win->client_rect.right - win->client_rect.left;
2435 reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
2437 reply->paint_flags = win->paint_flags & PAINT_CLIENT_FLAGS;
2441 /* get the surface visible region of a window */
2442 DECL_HANDLER(get_surface_region)
2444 struct region *region;
2445 struct window *win = get_window( req->window );
2447 if (!win || !is_visible( win )) return;
2449 if ((region = get_surface_region( win )))
2451 rectangle_t *data;
2452 if (win->parent) map_win_region_to_screen( win->parent, region );
2453 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2454 if (data) set_reply_data_ptr( data, reply->total_size );
2456 reply->visible_rect = win->visible_rect;
2457 if (win->parent) client_to_screen_rect( win->parent, &reply->visible_rect );
2461 /* get the window region */
2462 DECL_HANDLER(get_window_region)
2464 rectangle_t *data;
2465 struct window *win = get_window( req->window );
2467 if (!win) return;
2468 if (!win->win_region) return;
2470 if (win->ex_style & WS_EX_LAYOUTRTL)
2472 struct region *region = create_empty_region();
2474 if (!region) return;
2475 if (!copy_region( region, win->win_region ))
2477 free_region( region );
2478 return;
2480 mirror_region( &win->window_rect, region );
2481 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2483 else data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
2485 if (data) set_reply_data_ptr( data, reply->total_size );
2489 /* set the window region */
2490 DECL_HANDLER(set_window_region)
2492 struct region *region = NULL;
2493 struct window *win = get_window( req->window );
2495 if (!win) return;
2497 if (get_req_data_size()) /* no data means remove the region completely */
2499 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2500 return;
2501 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->window_rect, region );
2503 set_window_region( win, region, req->redraw );
2507 /* get a window update region */
2508 DECL_HANDLER(get_update_region)
2510 rectangle_t *data;
2511 unsigned int flags = req->flags;
2512 struct window *from_child = NULL;
2513 struct window *win = get_window( req->window );
2515 reply->flags = 0;
2516 if (!win) return;
2518 if (req->from_child)
2520 struct window *ptr;
2522 if (!(from_child = get_window( req->from_child ))) return;
2524 /* make sure from_child is a child of win */
2525 ptr = from_child;
2526 while (ptr && ptr != win) ptr = ptr->parent;
2527 if (!ptr)
2529 set_error( STATUS_INVALID_PARAMETER );
2530 return;
2534 if (flags & UPDATE_DELAYED_ERASE) /* this means that the previous call didn't erase */
2536 if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
2537 else win->paint_flags |= PAINT_DELAYED_ERASE;
2540 reply->flags = get_window_update_flags( win, from_child, flags, &win );
2541 reply->child = win->handle;
2543 if (flags & UPDATE_NOREGION) return;
2545 if (win->update_region)
2547 /* convert update region to screen coordinates */
2548 struct region *region = create_empty_region();
2550 if (!region) return;
2551 if (!copy_region( region, win->update_region ))
2553 free_region( region );
2554 return;
2556 map_win_region_to_screen( win, region );
2557 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
2558 &reply->total_size ))) return;
2559 set_reply_data_ptr( data, reply->total_size );
2562 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
2564 validate_parents( win );
2565 validate_whole_window( win );
2567 else
2569 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
2570 if (reply->flags & UPDATE_ERASE)
2572 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
2573 /* desktop window only gets erased, not repainted */
2574 if (is_desktop_window(win)) validate_whole_window( win );
2580 /* update the z order of a window so that a given rectangle is fully visible */
2581 DECL_HANDLER(update_window_zorder)
2583 rectangle_t tmp, rect = req->rect;
2584 struct window *ptr, *win = get_window( req->window );
2586 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
2587 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &rect );
2588 offset_rect( &rect, win->client_rect.left, win->client_rect.top );
2590 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2592 if (ptr == win) break;
2593 if (!(ptr->style & WS_VISIBLE)) continue;
2594 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2595 if (ptr->is_layered && (ptr->layered_flags & LWA_COLORKEY)) continue;
2596 if (!intersect_rect( &tmp, &ptr->visible_rect, &rect )) continue;
2597 if (ptr->win_region)
2599 tmp = rect;
2600 offset_rect( &tmp, -ptr->window_rect.left, -ptr->window_rect.top );
2601 if (!rect_in_region( ptr->win_region, &tmp )) continue;
2603 /* found a window obscuring the rectangle, now move win above this one */
2604 /* making sure to not violate the topmost rule */
2605 if (!(ptr->ex_style & WS_EX_TOPMOST) || (win->ex_style & WS_EX_TOPMOST))
2607 list_remove( &win->entry );
2608 list_add_before( &ptr->entry, &win->entry );
2610 break;
2615 /* mark parts of a window as needing a redraw */
2616 DECL_HANDLER(redraw_window)
2618 struct region *region = NULL;
2619 struct window *win = get_window( req->window );
2621 if (!win) return;
2622 if (!is_visible( win )) return; /* nothing to do */
2624 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
2626 if (get_req_data_size()) /* no data means whole rectangle */
2628 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2629 return;
2630 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->client_rect, region );
2634 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
2635 req->flags );
2636 if (region) free_region( region );
2640 /* set a window property */
2641 DECL_HANDLER(set_window_property)
2643 struct unicode_str name;
2644 struct window *win = get_window( req->window );
2646 if (!win) return;
2648 get_req_unicode_str( &name );
2649 if (name.len)
2651 atom_t atom = add_global_atom( NULL, &name );
2652 if (atom)
2654 set_property( win, atom, req->data, PROP_TYPE_STRING );
2655 release_global_atom( NULL, atom );
2658 else set_property( win, req->atom, req->data, PROP_TYPE_ATOM );
2662 /* remove a window property */
2663 DECL_HANDLER(remove_window_property)
2665 struct unicode_str name;
2666 struct window *win = get_window( req->window );
2668 get_req_unicode_str( &name );
2669 if (win)
2671 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2672 if (atom) reply->data = remove_property( win, atom );
2677 /* get a window property */
2678 DECL_HANDLER(get_window_property)
2680 struct unicode_str name;
2681 struct window *win = get_window( req->window );
2683 get_req_unicode_str( &name );
2684 if (win)
2686 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2687 if (atom) reply->data = get_property( win, atom );
2692 /* get the list of properties of a window */
2693 DECL_HANDLER(get_window_properties)
2695 property_data_t *data;
2696 int i, count, max = get_reply_max_size() / sizeof(*data);
2697 struct window *win = get_window( req->window );
2699 reply->total = 0;
2700 if (!win) return;
2702 for (i = count = 0; i < win->prop_inuse; i++)
2703 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2704 reply->total = count;
2706 if (count > max) count = max;
2707 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2709 for (i = 0; i < win->prop_inuse && count; i++)
2711 if (win->properties[i].type == PROP_TYPE_FREE) continue;
2712 data->atom = win->properties[i].atom;
2713 data->string = (win->properties[i].type == PROP_TYPE_STRING);
2714 data->data = win->properties[i].data;
2715 data++;
2716 count--;
2721 /* get the new window pointer for a global window, checking permissions */
2722 /* helper for set_global_windows request */
2723 static int get_new_global_window( struct window **win, user_handle_t handle )
2725 if (!handle)
2727 *win = NULL;
2728 return 1;
2730 else if (*win)
2732 set_error( STATUS_ACCESS_DENIED );
2733 return 0;
2735 *win = get_window( handle );
2736 return (*win != NULL);
2739 /* Set/get the global windows */
2740 DECL_HANDLER(set_global_windows)
2742 struct window *new_shell_window = shell_window;
2743 struct window *new_shell_listview = shell_listview;
2744 struct window *new_progman_window = progman_window;
2745 struct window *new_taskman_window = taskman_window;
2747 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2748 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2749 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2750 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2752 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2754 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2755 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2757 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2759 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2761 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2763 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2765 shell_window = new_shell_window;
2766 shell_listview = new_shell_listview;
2767 progman_window = new_progman_window;
2768 taskman_window = new_taskman_window;
2771 /* retrieve layered info for a window */
2772 DECL_HANDLER(get_window_layered_info)
2774 struct window *win = get_window( req->handle );
2776 if (!win) return;
2778 if (win->is_layered)
2780 reply->color_key = win->color_key;
2781 reply->alpha = win->alpha;
2782 reply->flags = win->layered_flags;
2784 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2788 /* set layered info for a window */
2789 DECL_HANDLER(set_window_layered_info)
2791 struct window *win = get_window( req->handle );
2793 if (!win) return;
2795 if (win->ex_style & WS_EX_LAYERED)
2797 if (req->flags & LWA_ALPHA) win->alpha = req->alpha;
2798 else if (!win->is_layered) win->alpha = 0; /* alpha init value is 0 */
2800 win->color_key = req->color_key;
2801 win->layered_flags = req->flags;
2802 win->is_layered = 1;
2804 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );