shell32: Add eject icon.
[wine.git] / server / window.c
blobe58d7e46d6841519d43c04ace2f29f0415584d83
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 surface_rect; /* window surface rectangle (relative to parent client area) */
74 rectangle_t client_rect; /* client rectangle (relative to parent client area) */
75 struct region *win_region; /* region for shaped windows (relative to window rect) */
76 struct region *update_region; /* update region (relative to window rect) */
77 unsigned int style; /* window style */
78 unsigned int ex_style; /* window extended style */
79 unsigned int id; /* window id */
80 mod_handle_t instance; /* creator instance */
81 unsigned int is_unicode : 1; /* ANSI or unicode */
82 unsigned int is_linked : 1; /* is it linked into the parent z-order list? */
83 unsigned int is_layered : 1; /* has layered info been set? */
84 unsigned int color_key; /* color key for a layered window */
85 unsigned int alpha; /* alpha value for a layered window */
86 unsigned int layered_flags; /* flags for a layered window */
87 unsigned int dpi; /* window DPI or 0 if per-monitor aware */
88 DPI_AWARENESS dpi_awareness; /* DPI awareness mode */
89 lparam_t user_data; /* user-specific data */
90 WCHAR *text; /* window caption text */
91 unsigned int paint_flags; /* various painting flags */
92 int prop_inuse; /* number of in-use window properties */
93 int prop_alloc; /* number of allocated window properties */
94 struct property *properties; /* window properties array */
95 int nb_extra_bytes; /* number of extra bytes */
96 char extra_bytes[1]; /* extra bytes storage */
99 /* flags that can be set by the client */
100 #define PAINT_HAS_SURFACE SET_WINPOS_PAINT_SURFACE
101 #define PAINT_HAS_PIXEL_FORMAT SET_WINPOS_PIXEL_FORMAT
102 #define PAINT_CLIENT_FLAGS (PAINT_HAS_SURFACE | PAINT_HAS_PIXEL_FORMAT)
103 /* flags only manipulated by the server */
104 #define PAINT_INTERNAL 0x0010 /* internal WM_PAINT pending */
105 #define PAINT_ERASE 0x0020 /* needs WM_ERASEBKGND */
106 #define PAINT_NONCLIENT 0x0040 /* needs WM_NCPAINT */
107 #define PAINT_DELAYED_ERASE 0x0080 /* still needs erase after WM_ERASEBKGND */
108 #define PAINT_PIXEL_FORMAT_CHILD 0x0100 /* at least one child has a custom pixel format */
110 /* growable array of user handles */
111 struct user_handle_array
113 user_handle_t *handles;
114 int count;
115 int total;
118 static const rectangle_t empty_rect;
120 /* global window pointers */
121 static struct window *shell_window;
122 static struct window *shell_listview;
123 static struct window *progman_window;
124 static struct window *taskman_window;
126 /* magic HWND_TOP etc. pointers */
127 #define WINPTR_TOP ((struct window *)1L)
128 #define WINPTR_BOTTOM ((struct window *)2L)
129 #define WINPTR_TOPMOST ((struct window *)3L)
130 #define WINPTR_NOTOPMOST ((struct window *)4L)
132 /* retrieve a pointer to a window from its handle */
133 static inline struct window *get_window( user_handle_t handle )
135 struct window *ret = get_user_object( handle, USER_WINDOW );
136 if (!ret) set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
137 return ret;
140 /* check if window is the desktop */
141 static inline int is_desktop_window( const struct window *win )
143 return !win->parent; /* only desktop windows have no parent */
146 /* get next window in Z-order list */
147 static inline struct window *get_next_window( struct window *win )
149 struct list *ptr = list_next( &win->parent->children, &win->entry );
150 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
153 /* get previous window in Z-order list */
154 static inline struct window *get_prev_window( struct window *win )
156 struct list *ptr = list_prev( &win->parent->children, &win->entry );
157 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
160 /* get first child in Z-order list */
161 static inline struct window *get_first_child( struct window *win )
163 struct list *ptr = list_head( &win->children );
164 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
167 /* get last child in Z-order list */
168 static inline struct window *get_last_child( struct window *win )
170 struct list *ptr = list_tail( &win->children );
171 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
174 /* set the PAINT_PIXEL_FORMAT_CHILD flag on all the parents */
175 /* note: we never reset the flag, it's just a heuristic */
176 static inline void update_pixel_format_flags( struct window *win )
178 for (win = win->parent; win && win->parent; win = win->parent)
179 win->paint_flags |= PAINT_PIXEL_FORMAT_CHILD;
182 /* get the per-monitor DPI for a window */
183 static unsigned int get_monitor_dpi( struct window *win )
185 /* FIXME: we return the desktop window DPI for now */
186 while (!is_desktop_window( win )) win = win->parent;
187 return win->dpi ? win->dpi : USER_DEFAULT_SCREEN_DPI;
190 /* link a window at the right place in the siblings list */
191 static void link_window( struct window *win, struct window *previous )
193 if (previous == WINPTR_NOTOPMOST)
195 if (!(win->ex_style & WS_EX_TOPMOST) && win->is_linked) return; /* nothing to do */
196 win->ex_style &= ~WS_EX_TOPMOST;
197 previous = WINPTR_TOP; /* fallback to the HWND_TOP case */
200 list_remove( &win->entry ); /* unlink it from the previous location */
202 if (previous == WINPTR_BOTTOM)
204 list_add_tail( &win->parent->children, &win->entry );
205 win->ex_style &= ~WS_EX_TOPMOST;
207 else if (previous == WINPTR_TOPMOST)
209 list_add_head( &win->parent->children, &win->entry );
210 win->ex_style |= WS_EX_TOPMOST;
212 else if (previous == WINPTR_TOP)
214 struct list *entry = win->parent->children.next;
215 if (!(win->ex_style & WS_EX_TOPMOST)) /* put it above the first non-topmost window */
217 while (entry != &win->parent->children)
219 struct window *next = LIST_ENTRY( entry, struct window, entry );
220 if (!(next->ex_style & WS_EX_TOPMOST)) break;
221 if (next->handle == win->owner) /* keep it above owner */
223 win->ex_style |= WS_EX_TOPMOST;
224 break;
226 entry = entry->next;
229 list_add_before( entry, &win->entry );
231 else
233 list_add_after( &previous->entry, &win->entry );
234 if (!(previous->ex_style & WS_EX_TOPMOST)) win->ex_style &= ~WS_EX_TOPMOST;
235 else
237 struct window *next = get_next_window( win );
238 if (next && (next->ex_style & WS_EX_TOPMOST)) win->ex_style |= WS_EX_TOPMOST;
242 win->is_linked = 1;
245 /* change the parent of a window (or unlink the window if the new parent is NULL) */
246 static int set_parent_window( struct window *win, struct window *parent )
248 struct window *ptr;
250 /* make sure parent is not a child of window */
251 for (ptr = parent; ptr; ptr = ptr->parent)
253 if (ptr == win)
255 set_error( STATUS_INVALID_PARAMETER );
256 return 0;
260 if (parent)
262 win->parent = parent;
263 link_window( win, WINPTR_TOP );
265 if (!is_desktop_window( parent ))
267 win->dpi = parent->dpi;
268 win->dpi_awareness = parent->dpi_awareness;
271 /* if parent belongs to a different thread and the window isn't */
272 /* top-level, attach the two threads */
273 if (parent->thread && parent->thread != win->thread && !is_desktop_window(parent))
274 attach_thread_input( win->thread, parent->thread );
276 if (win->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))
277 update_pixel_format_flags( win );
279 else /* move it to parent unlinked list */
281 list_remove( &win->entry ); /* unlink it from the previous location */
282 list_add_head( &win->parent->unlinked, &win->entry );
283 win->is_linked = 0;
285 return 1;
288 /* append a user handle to a handle array */
289 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
291 if (array->count >= array->total)
293 int new_total = max( array->total * 2, 32 );
294 user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
295 if (!new_array)
297 free( array->handles );
298 set_error( STATUS_NO_MEMORY );
299 return 0;
301 array->handles = new_array;
302 array->total = new_total;
304 array->handles[array->count++] = handle;
305 return 1;
308 /* set a window property */
309 static void set_property( struct window *win, atom_t atom, lparam_t data, enum property_type type )
311 int i, free = -1;
312 struct property *new_props;
314 /* check if it exists already */
315 for (i = 0; i < win->prop_inuse; i++)
317 if (win->properties[i].type == PROP_TYPE_FREE)
319 free = i;
320 continue;
322 if (win->properties[i].atom == atom)
324 win->properties[i].type = type;
325 win->properties[i].data = data;
326 return;
330 /* need to add an entry */
331 if (!grab_global_atom( NULL, atom )) return;
332 if (free == -1)
334 /* no free entry */
335 if (win->prop_inuse >= win->prop_alloc)
337 /* need to grow the array */
338 if (!(new_props = realloc( win->properties,
339 sizeof(*new_props) * (win->prop_alloc + 16) )))
341 set_error( STATUS_NO_MEMORY );
342 release_global_atom( NULL, atom );
343 return;
345 win->prop_alloc += 16;
346 win->properties = new_props;
348 free = win->prop_inuse++;
350 win->properties[free].atom = atom;
351 win->properties[free].type = type;
352 win->properties[free].data = data;
355 /* remove a window property */
356 static lparam_t remove_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)
365 release_global_atom( NULL, atom );
366 win->properties[i].type = PROP_TYPE_FREE;
367 return win->properties[i].data;
370 /* FIXME: last error? */
371 return 0;
374 /* find a window property */
375 static lparam_t get_property( struct window *win, atom_t atom )
377 int i;
379 for (i = 0; i < win->prop_inuse; i++)
381 if (win->properties[i].type == PROP_TYPE_FREE) continue;
382 if (win->properties[i].atom == atom) return win->properties[i].data;
384 /* FIXME: last error? */
385 return 0;
388 /* destroy all properties of a window */
389 static inline void destroy_properties( struct window *win )
391 int i;
393 if (!win->properties) return;
394 for (i = 0; i < win->prop_inuse; i++)
396 if (win->properties[i].type == PROP_TYPE_FREE) continue;
397 release_global_atom( NULL, win->properties[i].atom );
399 free( win->properties );
402 /* detach a window from its owner thread but keep the window around */
403 static void detach_window_thread( struct window *win )
405 struct thread *thread = win->thread;
407 if (!thread) return;
408 if (thread->queue)
410 if (win->update_region) inc_queue_paint_count( thread, -1 );
411 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
412 queue_cleanup_window( thread, win->handle );
414 assert( thread->desktop_users > 0 );
415 thread->desktop_users--;
416 release_class( win->class );
417 win->class = NULL;
419 /* don't hold a reference to the desktop so that the desktop window can be */
420 /* destroyed when the desktop ref count reaches zero */
421 release_object( win->desktop );
422 win->thread = NULL;
425 /* get the process owning the top window of a given desktop */
426 struct process *get_top_window_owner( struct desktop *desktop )
428 struct window *win = desktop->top_window;
429 if (!win || !win->thread) return NULL;
430 return win->thread->process;
433 /* get the top window size of a given desktop */
434 void get_top_window_rectangle( struct desktop *desktop, rectangle_t *rect )
436 struct window *win = desktop->top_window;
437 *rect = win ? win->window_rect : empty_rect;
440 /* post a message to the desktop window */
441 void post_desktop_message( struct desktop *desktop, unsigned int message,
442 lparam_t wparam, lparam_t lparam )
444 struct window *win = desktop->top_window;
445 if (win && win->thread) post_message( win->handle, message, wparam, lparam );
448 /* create a new window structure (note: the window is not linked in the window tree) */
449 static struct window *create_window( struct window *parent, struct window *owner,
450 atom_t atom, mod_handle_t instance )
452 int extra_bytes;
453 struct window *win = NULL;
454 struct desktop *desktop;
455 struct window_class *class;
457 if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
459 if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
461 release_object( desktop );
462 return NULL;
465 if (!parent) /* null parent is only allowed for desktop or HWND_MESSAGE top window */
467 if (is_desktop_class( class ))
468 parent = desktop->top_window; /* use existing desktop if any */
469 else if (is_hwnd_message_class( class ))
470 /* use desktop window if message window is already created */
471 parent = desktop->msg_window ? desktop->top_window : NULL;
472 else if (!(parent = desktop->top_window)) /* must already have a desktop then */
474 set_error( STATUS_ACCESS_DENIED );
475 goto failed;
479 /* parent must be on the same desktop */
480 if (parent && parent->desktop != desktop)
482 set_error( STATUS_ACCESS_DENIED );
483 goto failed;
486 if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
487 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
489 win->parent = parent;
490 win->owner = owner ? owner->handle : 0;
491 win->thread = current;
492 win->desktop = desktop;
493 win->class = class;
494 win->atom = atom;
495 win->last_active = win->handle;
496 win->win_region = NULL;
497 win->update_region = NULL;
498 win->style = 0;
499 win->ex_style = 0;
500 win->id = 0;
501 win->instance = 0;
502 win->is_unicode = 1;
503 win->is_linked = 0;
504 win->is_layered = 0;
505 win->dpi_awareness = DPI_AWARENESS_PER_MONITOR_AWARE;
506 win->dpi = 0;
507 win->user_data = 0;
508 win->text = NULL;
509 win->paint_flags = 0;
510 win->prop_inuse = 0;
511 win->prop_alloc = 0;
512 win->properties = NULL;
513 win->nb_extra_bytes = extra_bytes;
514 win->window_rect = win->visible_rect = win->surface_rect = win->client_rect = empty_rect;
515 memset( win->extra_bytes, 0, extra_bytes );
516 list_init( &win->children );
517 list_init( &win->unlinked );
519 /* if parent belongs to a different thread and the window isn't */
520 /* top-level, attach the two threads */
521 if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
523 if (!attach_thread_input( current, parent->thread )) goto failed;
525 else /* otherwise just make sure that the thread has a message queue */
527 if (!current->queue && !init_thread_queue( current )) goto failed;
530 /* put it on parent unlinked list */
531 if (parent) list_add_head( &parent->unlinked, &win->entry );
532 else
534 list_init( &win->entry );
535 if (is_desktop_class( class ))
537 assert( !desktop->top_window );
538 desktop->top_window = win;
539 set_process_default_desktop( current->process, desktop, current->desktop );
541 else
543 assert( !desktop->msg_window );
544 desktop->msg_window = win;
548 current->desktop_users++;
549 return win;
551 failed:
552 if (win)
554 if (win->handle) free_user_handle( win->handle );
555 free( win );
557 release_object( desktop );
558 release_class( class );
559 return NULL;
562 /* destroy all windows belonging to a given thread */
563 void destroy_thread_windows( struct thread *thread )
565 user_handle_t handle = 0;
566 struct window *win;
568 while ((win = next_user_handle( &handle, USER_WINDOW )))
570 if (win->thread != thread) continue;
571 if (is_desktop_window( win )) detach_window_thread( win );
572 else destroy_window( win );
576 /* get the desktop window */
577 static struct window *get_desktop_window( struct thread *thread )
579 struct window *top_window;
580 struct desktop *desktop = get_thread_desktop( thread, 0 );
582 if (!desktop) return NULL;
583 top_window = desktop->top_window;
584 release_object( desktop );
585 return top_window;
588 /* check whether child is a descendant of parent */
589 int is_child_window( user_handle_t parent, user_handle_t child )
591 struct window *child_ptr = get_user_object( child, USER_WINDOW );
592 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
594 if (!child_ptr || !parent_ptr) return 0;
595 while (child_ptr->parent)
597 if (child_ptr->parent == parent_ptr) return 1;
598 child_ptr = child_ptr->parent;
600 return 0;
603 /* check if window can be set as foreground window */
604 int is_valid_foreground_window( user_handle_t window )
606 struct window *win = get_user_object( window, USER_WINDOW );
607 return win && (win->style & (WS_POPUP|WS_CHILD)) != WS_CHILD;
610 /* make a window active if possible */
611 int make_window_active( user_handle_t window )
613 struct window *owner, *win = get_window( window );
615 if (!win) return 0;
617 /* set last active for window and its owners */
618 owner = win;
619 while (owner)
621 owner->last_active = win->handle;
622 owner = get_user_object( owner->owner, USER_WINDOW );
624 return 1;
627 /* increment (or decrement) the window paint count */
628 static inline void inc_window_paint_count( struct window *win, int incr )
630 if (win->thread) inc_queue_paint_count( win->thread, incr );
633 /* check if window and all its ancestors are visible */
634 static int is_visible( const struct window *win )
636 while (win)
638 if (!(win->style & WS_VISIBLE)) return 0;
639 win = win->parent;
640 /* if parent is minimized children are not visible */
641 if (win && (win->style & WS_MINIMIZE)) return 0;
643 return 1;
646 /* same as is_visible but takes a window handle */
647 int is_window_visible( user_handle_t window )
649 struct window *win = get_user_object( window, USER_WINDOW );
650 if (!win) return 0;
651 return is_visible( win );
654 int is_window_transparent( user_handle_t window )
656 struct window *win = get_user_object( window, USER_WINDOW );
657 if (!win) return 0;
658 return (win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT);
661 /* check if point is inside the window */
662 static inline int is_point_in_window( struct window *win, int x, int y )
664 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
665 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
666 return 0; /* disabled child */
667 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
668 return 0; /* transparent */
669 if (!point_in_rect( &win->visible_rect, x, y ))
670 return 0; /* not in window */
671 if (win->win_region &&
672 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
673 return 0; /* not in window region */
674 return 1;
677 /* fill an array with the handles of the children of a specified window */
678 static unsigned int get_children_windows( struct window *parent, atom_t atom, thread_id_t tid,
679 user_handle_t *handles, unsigned int max_count )
681 struct window *ptr;
682 unsigned int count = 0;
684 if (!parent) return 0;
686 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
688 if (atom && get_class_atom(ptr->class) != atom) continue;
689 if (tid && get_thread_id(ptr->thread) != tid) continue;
690 if (handles)
692 if (count >= max_count) break;
693 handles[count] = ptr->handle;
695 count++;
697 return count;
700 /* find child of 'parent' that contains the given point (in parent-relative coords) */
701 static struct window *child_window_from_point( struct window *parent, int x, int y )
703 struct window *ptr;
705 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
707 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
709 /* if window is minimized or disabled, return at once */
710 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
712 /* if point is not in client area, return at once */
713 if (!point_in_rect( &ptr->client_rect, x, y )) return ptr;
715 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
717 return parent; /* not found any child */
720 /* find all children of 'parent' that contain the given point */
721 static int get_window_children_from_point( struct window *parent, int x, int y,
722 struct user_handle_array *array )
724 struct window *ptr;
726 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
728 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
730 /* if point is in client area, and window is not minimized or disabled, check children */
731 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) && point_in_rect( &ptr->client_rect, x, y ))
733 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
734 y - ptr->client_rect.top, array ))
735 return 0;
738 /* now add window to the array */
739 if (!add_handle_to_array( array, ptr->handle )) return 0;
741 return 1;
744 /* get handle of root of top-most window containing point */
745 user_handle_t shallow_window_from_point( struct desktop *desktop, int x, int y )
747 struct window *ptr;
749 if (!desktop->top_window) return 0;
751 LIST_FOR_EACH_ENTRY( ptr, &desktop->top_window->children, struct window, entry )
753 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
754 return ptr->handle;
756 return desktop->top_window->handle;
759 /* return thread of top-most window containing point (in absolute coords) */
760 struct thread *window_thread_from_point( user_handle_t scope, int x, int y )
762 struct window *win = get_user_object( scope, USER_WINDOW );
763 struct window *ptr;
765 if (!win) return NULL;
767 for (ptr = win; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
769 x -= ptr->client_rect.left;
770 y -= ptr->client_rect.top;
773 ptr = child_window_from_point( win, x, y );
774 if (!ptr->thread) return NULL;
775 return (struct thread *)grab_object( ptr->thread );
778 /* return list of all windows containing point (in absolute coords) */
779 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
781 struct window *ptr;
783 /* make point relative to top window */
784 for (ptr = top->parent; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
786 x -= ptr->client_rect.left;
787 y -= ptr->client_rect.top;
790 if (!is_point_in_window( top, x, y )) return 1;
792 /* if point is in client area, and window is not minimized or disabled, check children */
793 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) && point_in_rect( &top->client_rect, x, y ))
795 if (!is_desktop_window(top))
797 x -= top->client_rect.left;
798 y -= top->client_rect.top;
800 if (!get_window_children_from_point( top, x, y, array )) return 0;
802 /* now add window to the array */
803 if (!add_handle_to_array( array, top->handle )) return 0;
804 return 1;
808 /* return the thread owning a window */
809 struct thread *get_window_thread( user_handle_t handle )
811 struct window *win = get_user_object( handle, USER_WINDOW );
812 if (!win || !win->thread) return NULL;
813 return (struct thread *)grab_object( win->thread );
817 /* check if any area of a window needs repainting */
818 static inline int win_needs_repaint( struct window *win )
820 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
824 /* find a child of the specified window that needs repainting */
825 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
827 struct window *ptr, *ret = NULL;
829 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
831 if (!(ptr->style & WS_VISIBLE)) continue;
832 if (ptr->thread == thread && win_needs_repaint( ptr ))
833 ret = ptr;
834 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
835 ret = find_child_to_repaint( ptr, thread );
836 if (ret) break;
839 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
841 /* transparent window, check for non-transparent sibling to paint first */
842 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
844 if (!(ptr->style & WS_VISIBLE)) continue;
845 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
846 if (ptr->thread != thread) continue;
847 if (win_needs_repaint( ptr )) return ptr;
850 return ret;
854 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
855 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
857 struct window *ptr, *win, *top_window = get_desktop_window( thread );
859 if (!top_window) return 0;
861 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
862 else win = find_child_to_repaint( top_window, thread );
864 if (win && parent)
866 /* check that it is a child of the specified parent */
867 for (ptr = win; ptr; ptr = ptr->parent)
868 if (ptr->handle == parent) break;
869 /* otherwise don't return any window, we don't repaint a child before its parent */
870 if (!ptr) win = NULL;
872 if (!win) return 0;
873 win->paint_flags &= ~PAINT_INTERNAL;
874 return win->handle;
878 /* intersect the window region with the specified region, relative to the window parent */
879 static struct region *intersect_window_region( struct region *region, struct window *win )
881 /* make region relative to window rect */
882 offset_region( region, -win->window_rect.left, -win->window_rect.top );
883 if (!intersect_region( region, region, win->win_region )) return NULL;
884 /* make region relative to parent again */
885 offset_region( region, win->window_rect.left, win->window_rect.top );
886 return region;
890 /* convert coordinates from client to screen coords */
891 static inline void client_to_screen( struct window *win, int *x, int *y )
893 for ( ; win && !is_desktop_window(win); win = win->parent)
895 *x += win->client_rect.left;
896 *y += win->client_rect.top;
900 /* convert coordinates from client to screen coords */
901 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
903 for ( ; win && !is_desktop_window(win); win = win->parent)
904 offset_rect( rect, win->client_rect.left, win->client_rect.top );
907 /* map the region from window to screen coordinates */
908 static inline void map_win_region_to_screen( struct window *win, struct region *region )
910 if (!is_desktop_window(win))
912 int x = win->window_rect.left;
913 int y = win->window_rect.top;
914 client_to_screen( win->parent, &x, &y );
915 offset_region( region, x, y );
920 /* clip all children of a given window out of the visible region */
921 static struct region *clip_children( struct window *parent, struct window *last,
922 struct region *region, int offset_x, int offset_y )
924 struct window *ptr;
925 struct region *tmp = create_empty_region();
927 if (!tmp) return NULL;
928 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
930 if (ptr == last) break;
931 if (!(ptr->style & WS_VISIBLE)) continue;
932 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
933 set_region_rect( tmp, &ptr->visible_rect );
934 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
936 free_region( tmp );
937 return NULL;
939 offset_region( tmp, offset_x, offset_y );
940 if (!(region = subtract_region( region, region, tmp ))) break;
941 if (is_region_empty( region )) break;
943 free_region( tmp );
944 return region;
948 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
949 static void set_region_client_rect( struct region *region, struct window *win )
951 rectangle_t rect;
953 intersect_rect( &rect, &win->window_rect, &win->client_rect );
954 intersect_rect( &rect, &rect, &win->surface_rect );
955 set_region_rect( region, &rect );
959 /* set the region to the visible rect clipped by the window surface, in parent-relative coordinates */
960 static void set_region_visible_rect( struct region *region, struct window *win )
962 rectangle_t rect;
964 intersect_rect( &rect, &win->visible_rect, &win->surface_rect );
965 set_region_rect( region, &rect );
969 /* get the top-level window to clip against for a given window */
970 static inline struct window *get_top_clipping_window( struct window *win )
972 while (!(win->paint_flags & PAINT_HAS_SURFACE) && win->parent && !is_desktop_window(win->parent))
973 win = win->parent;
974 return win;
978 /* compute the visible region of a window, in window coordinates */
979 static struct region *get_visible_region( struct window *win, unsigned int flags )
981 struct region *tmp = NULL, *region;
982 int offset_x, offset_y;
984 if (!(region = create_empty_region())) return NULL;
986 /* first check if all ancestors are visible */
988 if (!is_visible( win )) return region; /* empty region */
990 if (is_desktop_window( win ))
992 set_region_rect( region, &win->window_rect );
993 return region;
996 /* create a region relative to the window itself */
998 if ((flags & DCX_PARENTCLIP) && !is_desktop_window( win->parent ))
1000 set_region_client_rect( region, win->parent );
1001 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
1003 else if (flags & DCX_WINDOW)
1005 set_region_visible_rect( region, win );
1006 if (win->win_region && !intersect_window_region( region, win )) goto error;
1008 else
1010 set_region_client_rect( region, win );
1011 if (win->win_region && !intersect_window_region( region, win )) goto error;
1014 /* clip children */
1016 if (flags & DCX_CLIPCHILDREN)
1018 if (!clip_children( win, NULL, region, win->client_rect.left, win->client_rect.top )) goto error;
1021 /* clip siblings of ancestors */
1023 offset_x = win->window_rect.left;
1024 offset_y = win->window_rect.top;
1026 if ((tmp = create_empty_region()) != NULL)
1028 while (!is_desktop_window( win->parent ))
1030 /* we don't clip out top-level siblings as that's up to the native windowing system */
1031 if (win->style & WS_CLIPSIBLINGS)
1033 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
1034 if (is_region_empty( region )) break;
1036 /* clip to parent client area */
1037 win = win->parent;
1038 offset_x += win->client_rect.left;
1039 offset_y += win->client_rect.top;
1040 offset_region( region, win->client_rect.left, win->client_rect.top );
1041 set_region_client_rect( tmp, win );
1042 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
1043 if (!intersect_region( region, region, tmp )) goto error;
1044 if (is_region_empty( region )) break;
1046 free_region( tmp );
1048 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
1049 return region;
1051 error:
1052 if (tmp) free_region( tmp );
1053 free_region( region );
1054 return NULL;
1058 /* clip all children with a custom pixel format out of the visible region */
1059 static struct region *clip_pixel_format_children( struct window *parent, struct region *parent_clip,
1060 struct region *region, int offset_x, int offset_y )
1062 struct window *ptr;
1063 struct region *clip = create_empty_region();
1065 if (!clip) return NULL;
1067 LIST_FOR_EACH_ENTRY_REV( ptr, &parent->children, struct window, entry )
1069 if (!(ptr->style & WS_VISIBLE)) continue;
1070 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1072 /* add the visible rect */
1073 set_region_rect( clip, &ptr->visible_rect );
1074 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1075 offset_region( clip, offset_x, offset_y );
1076 if (!intersect_region( clip, clip, parent_clip )) break;
1077 if (!union_region( region, region, clip )) break;
1078 if (!(ptr->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))) continue;
1080 /* subtract the client rect if it uses a custom pixel format */
1081 set_region_rect( clip, &ptr->client_rect );
1082 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1083 offset_region( clip, offset_x, offset_y );
1084 if (!intersect_region( clip, clip, parent_clip )) break;
1085 if ((ptr->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1086 break;
1088 if (!clip_pixel_format_children( ptr, clip, region, offset_x + ptr->client_rect.left,
1089 offset_y + ptr->client_rect.top ))
1090 break;
1092 free_region( clip );
1093 return region;
1097 /* compute the visible surface region of a window, in parent coordinates */
1098 static struct region *get_surface_region( struct window *win )
1100 struct region *region, *clip;
1101 int offset_x, offset_y;
1103 /* create a region relative to the window itself */
1105 if (!(region = create_empty_region())) return NULL;
1106 if (!(clip = create_empty_region())) goto error;
1107 set_region_rect( region, &win->visible_rect );
1108 if (win->win_region && !intersect_window_region( region, win )) goto error;
1109 set_region_rect( clip, &win->client_rect );
1110 if (win->win_region && !intersect_window_region( clip, win )) goto error;
1112 if ((win->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1113 goto error;
1115 /* clip children */
1117 if (!is_desktop_window(win))
1119 offset_x = win->client_rect.left;
1120 offset_y = win->client_rect.top;
1122 else offset_x = offset_y = 0;
1124 if (!clip_pixel_format_children( win, clip, region, offset_x, offset_y )) goto error;
1126 free_region( clip );
1127 return region;
1129 error:
1130 if (clip) free_region( clip );
1131 free_region( region );
1132 return NULL;
1136 /* get the window class of a window */
1137 struct window_class* get_window_class( user_handle_t window )
1139 struct window *win;
1140 if (!(win = get_window( window ))) return NULL;
1141 if (!win->class) set_error( STATUS_ACCESS_DENIED );
1142 return win->class;
1145 /* determine the window visible rectangle, i.e. window or client rect cropped by parent rects */
1146 /* the returned rectangle is in window coordinates; return 0 if rectangle is empty */
1147 static int get_window_visible_rect( struct window *win, rectangle_t *rect, int frame )
1149 int offset_x = win->window_rect.left, offset_y = win->window_rect.top;
1151 *rect = frame ? win->window_rect : win->client_rect;
1153 if (!(win->style & WS_VISIBLE)) return 0;
1154 if (is_desktop_window( win )) return 1;
1156 while (!is_desktop_window( win->parent ))
1158 win = win->parent;
1159 if (!(win->style & WS_VISIBLE) || win->style & WS_MINIMIZE) return 0;
1160 offset_x += win->client_rect.left;
1161 offset_y += win->client_rect.top;
1162 offset_rect( rect, win->client_rect.left, win->client_rect.top );
1163 if (!intersect_rect( rect, rect, &win->client_rect )) return 0;
1164 if (!intersect_rect( rect, rect, &win->window_rect )) return 0;
1166 offset_rect( rect, -offset_x, -offset_y );
1167 return 1;
1170 /* return a copy of the specified region cropped to the window client or frame rectangle, */
1171 /* and converted from client to window coordinates. Helper for (in)validate_window. */
1172 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
1174 rectangle_t rect;
1175 struct region *tmp;
1177 if (!get_window_visible_rect( win, &rect, frame )) return NULL;
1178 if (!(tmp = create_empty_region())) return NULL;
1179 set_region_rect( tmp, &rect );
1181 if (region)
1183 /* map it to client coords */
1184 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1185 win->window_rect.top - win->client_rect.top );
1187 /* intersect specified region with bounding rect */
1188 if (!intersect_region( tmp, region, tmp )) goto done;
1189 if (is_region_empty( tmp )) goto done;
1191 /* map it back to window coords */
1192 offset_region( tmp, win->client_rect.left - win->window_rect.left,
1193 win->client_rect.top - win->window_rect.top );
1195 return tmp;
1197 done:
1198 free_region( tmp );
1199 return NULL;
1203 /* set a region as new update region for the window */
1204 static void set_update_region( struct window *win, struct region *region )
1206 if (region && !is_region_empty( region ))
1208 if (!win->update_region) inc_window_paint_count( win, 1 );
1209 else free_region( win->update_region );
1210 win->update_region = region;
1212 else
1214 if (win->update_region)
1216 inc_window_paint_count( win, -1 );
1217 free_region( win->update_region );
1219 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
1220 win->update_region = NULL;
1221 if (region) free_region( region );
1226 /* add a region to the update region; the passed region is freed or reused */
1227 static int add_update_region( struct window *win, struct region *region )
1229 if (win->update_region && !union_region( region, win->update_region, region ))
1231 free_region( region );
1232 return 0;
1234 set_update_region( win, region );
1235 return 1;
1239 /* crop the update region of children to the specified rectangle, in client coords */
1240 static void crop_children_update_region( struct window *win, rectangle_t *rect )
1242 struct window *child;
1243 struct region *tmp;
1244 rectangle_t child_rect;
1246 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1248 if (!(child->style & WS_VISIBLE)) continue;
1249 if (!rect) /* crop everything out */
1251 crop_children_update_region( child, NULL );
1252 set_update_region( child, NULL );
1253 continue;
1256 /* nothing to do if child is completely inside rect */
1257 if (child->window_rect.left >= rect->left &&
1258 child->window_rect.top >= rect->top &&
1259 child->window_rect.right <= rect->right &&
1260 child->window_rect.bottom <= rect->bottom) continue;
1262 /* map to child client coords and crop grand-children */
1263 child_rect = *rect;
1264 offset_rect( &child_rect, -child->client_rect.left, -child->client_rect.top );
1265 crop_children_update_region( child, &child_rect );
1267 /* now crop the child itself */
1268 if (!child->update_region) continue;
1269 if (!(tmp = create_empty_region())) continue;
1270 set_region_rect( tmp, rect );
1271 offset_region( tmp, -child->window_rect.left, -child->window_rect.top );
1272 if (intersect_region( tmp, child->update_region, tmp )) set_update_region( child, tmp );
1273 else free_region( tmp );
1278 /* validate the non client area of a window */
1279 static void validate_non_client( struct window *win )
1281 struct region *tmp;
1282 rectangle_t rect;
1284 if (!win->update_region) return; /* nothing to do */
1286 /* get client rect in window coords */
1287 rect.left = win->client_rect.left - win->window_rect.left;
1288 rect.top = win->client_rect.top - win->window_rect.top;
1289 rect.right = win->client_rect.right - win->window_rect.left;
1290 rect.bottom = win->client_rect.bottom - win->window_rect.top;
1292 if ((tmp = create_empty_region()))
1294 set_region_rect( tmp, &rect );
1295 if (intersect_region( tmp, win->update_region, tmp ))
1296 set_update_region( win, tmp );
1297 else
1298 free_region( tmp );
1300 win->paint_flags &= ~PAINT_NONCLIENT;
1304 /* validate a window completely so that we don't get any further paint messages for it */
1305 static void validate_whole_window( struct window *win )
1307 set_update_region( win, NULL );
1309 if (win->paint_flags & PAINT_INTERNAL)
1311 win->paint_flags &= ~PAINT_INTERNAL;
1312 inc_window_paint_count( win, -1 );
1317 /* validate a window's children so that we don't get any further paint messages for it */
1318 static void validate_children( struct window *win )
1320 struct window *child;
1322 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1324 if (!(child->style & WS_VISIBLE)) continue;
1325 validate_children(child);
1326 validate_whole_window(child);
1331 /* validate the update region of a window on all parents; helper for get_update_region */
1332 static void validate_parents( struct window *child )
1334 int offset_x = 0, offset_y = 0;
1335 struct window *win = child;
1336 struct region *tmp = NULL;
1338 if (!child->update_region) return;
1340 while (win->parent)
1342 /* map to parent client coords */
1343 offset_x += win->window_rect.left;
1344 offset_y += win->window_rect.top;
1346 win = win->parent;
1348 /* and now map to window coords */
1349 offset_x += win->client_rect.left - win->window_rect.left;
1350 offset_y += win->client_rect.top - win->window_rect.top;
1352 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1354 if (!tmp && !(tmp = create_empty_region())) return;
1355 offset_region( child->update_region, offset_x, offset_y );
1356 if (subtract_region( tmp, win->update_region, child->update_region ))
1358 set_update_region( win, tmp );
1359 tmp = NULL;
1361 /* restore child coords */
1362 offset_region( child->update_region, -offset_x, -offset_y );
1365 if (tmp) free_region( tmp );
1369 /* add/subtract a region (in client coordinates) to the update region of the window */
1370 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1372 struct region *tmp;
1373 struct window *child;
1375 if (flags & RDW_INVALIDATE)
1377 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1379 if (!add_update_region( win, tmp )) return;
1381 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1382 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1384 else if (flags & RDW_VALIDATE)
1386 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1388 set_update_region( win, NULL );
1390 else if (win->update_region)
1392 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1394 if (!subtract_region( tmp, win->update_region, tmp ))
1396 free_region( tmp );
1397 return;
1399 set_update_region( win, tmp );
1401 if (flags & RDW_NOFRAME) validate_non_client( win );
1402 if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1406 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1408 win->paint_flags |= PAINT_INTERNAL;
1409 inc_window_paint_count( win, 1 );
1411 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1413 win->paint_flags &= ~PAINT_INTERNAL;
1414 inc_window_paint_count( win, -1 );
1417 /* now process children recursively */
1419 if (flags & RDW_NOCHILDREN) return;
1420 if (win->style & WS_MINIMIZE) return;
1421 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1423 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1425 /* map to client coordinates */
1426 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1427 win->window_rect.top - win->client_rect.top );
1429 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1431 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1433 if (!(child->style & WS_VISIBLE)) continue;
1434 if (!rect_in_region( tmp, &child->window_rect )) continue;
1435 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1436 redraw_window( child, tmp, 1, flags );
1437 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1439 free_region( tmp );
1443 /* retrieve the update flags for a window depending on the state of the update region */
1444 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1446 unsigned int ret = 0;
1448 if (flags & UPDATE_NONCLIENT)
1450 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1452 if (flags & UPDATE_ERASE)
1454 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1456 if (flags & UPDATE_PAINT)
1458 if (win->update_region)
1460 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1461 ret |= UPDATE_PAINT;
1464 if (flags & UPDATE_INTERNALPAINT)
1466 if (win->paint_flags & PAINT_INTERNAL)
1468 ret |= UPDATE_INTERNALPAINT;
1469 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1472 return ret;
1476 /* iterate through the children of the given window until we find one with some update flags */
1477 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1478 unsigned int flags, struct window **child )
1480 struct window *ptr;
1481 unsigned int ret = 0;
1483 /* first make sure we want to iterate children at all */
1485 if (win->style & WS_MINIMIZE) return 0;
1487 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1488 * here we only want to repaint children of windows that clip them, others
1489 * need to wait for WM_PAINT to be done in the parent first.
1491 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1493 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1495 if (from_child) /* skip all children until from_child is found */
1497 if (ptr == from_child) from_child = NULL;
1498 continue;
1500 if (!(ptr->style & WS_VISIBLE)) continue;
1501 if ((ret = get_update_flags( ptr, flags )) != 0)
1503 *child = ptr;
1504 break;
1506 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1508 return ret;
1511 /* iterate through children and siblings of the given window until we find one with some update flags */
1512 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1513 unsigned int flags, struct window **child )
1515 unsigned int ret;
1516 struct window *ptr, *from_sibling = NULL;
1518 /* if some parent is not visible start from the next sibling */
1520 if (!is_visible( win )) return 0;
1521 for (ptr = from_child; ptr; ptr = ptr->parent)
1523 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1524 if (ptr == win) break;
1527 /* non-client painting must be delayed if one of the parents is going to
1528 * be repainted and doesn't clip children */
1530 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1532 for (ptr = win->parent; ptr; ptr = ptr->parent)
1534 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1535 return 0;
1537 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1539 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1541 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1542 if (ptr == win) break;
1548 /* check window itself (only if not restarting from a child) */
1550 if (!from_child)
1552 if ((ret = get_update_flags( win, flags )))
1554 *child = win;
1555 return ret;
1557 from_child = win;
1560 /* now check children */
1562 if (flags & UPDATE_NOCHILDREN) return 0;
1563 if (!from_sibling)
1565 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1566 from_sibling = from_child;
1569 /* then check siblings and parent siblings */
1571 while (from_sibling->parent && from_sibling != win)
1573 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1574 return ret;
1575 from_sibling = from_sibling->parent;
1577 return 0;
1581 /* expose the areas revealed by a vis region change on the window parent */
1582 /* returns the region exposed on the window itself (in client coordinates) */
1583 static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
1584 struct region *old_vis_rgn )
1586 struct region *new_vis_rgn, *exposed_rgn;
1588 if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
1590 if ((exposed_rgn = create_empty_region()))
1592 if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
1594 /* make it relative to the new client area */
1595 offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
1596 win->window_rect.top - win->client_rect.top );
1598 else
1600 free_region( exposed_rgn );
1601 exposed_rgn = NULL;
1605 if (win->parent && !is_desktop_window( win->parent ))
1607 /* make it relative to the old window pos for subtracting */
1608 offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
1609 win->window_rect.top - old_window_rect->top );
1611 if ((win->parent->style & WS_CLIPCHILDREN) ?
1612 subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) :
1613 xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1615 if (!is_region_empty( new_vis_rgn ))
1617 /* make it relative to parent */
1618 offset_region( new_vis_rgn, old_window_rect->left, old_window_rect->top );
1619 redraw_window( win->parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1623 free_region( new_vis_rgn );
1624 return exposed_rgn;
1628 /* set the window and client rectangles, updating the update region if necessary */
1629 static void set_window_pos( struct window *win, struct window *previous,
1630 unsigned int swp_flags, const rectangle_t *window_rect,
1631 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1632 const rectangle_t *surface_rect, const rectangle_t *valid_rect )
1634 struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
1635 const rectangle_t old_window_rect = win->window_rect;
1636 const rectangle_t old_visible_rect = win->visible_rect;
1637 const rectangle_t old_client_rect = win->client_rect;
1638 rectangle_t rect;
1639 int client_changed, frame_changed;
1640 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1642 if (win->parent && !is_visible( win->parent )) visible = 0;
1644 if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
1646 /* set the new window info before invalidating anything */
1648 win->window_rect = *window_rect;
1649 win->visible_rect = *visible_rect;
1650 win->surface_rect = *surface_rect;
1651 win->client_rect = *client_rect;
1652 if (!(swp_flags & SWP_NOZORDER) && win->parent) link_window( win, previous );
1653 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1654 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1656 /* keep children at the same position relative to top right corner when the parent is mirrored */
1657 if (win->ex_style & WS_EX_LAYOUTRTL)
1659 struct window *child;
1660 int old_size = old_client_rect.right - old_client_rect.left;
1661 int new_size = win->client_rect.right - win->client_rect.left;
1663 if (old_size != new_size) LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1665 offset_rect( &child->window_rect, new_size - old_size, 0 );
1666 offset_rect( &child->visible_rect, new_size - old_size, 0 );
1667 offset_rect( &child->surface_rect, new_size - old_size, 0 );
1668 offset_rect( &child->client_rect, new_size - old_size, 0 );
1672 /* reset cursor clip rectangle when the desktop changes size */
1673 if (win == win->desktop->top_window) win->desktop->cursor.clip = *window_rect;
1675 /* if the window is not visible, everything is easy */
1676 if (!visible) return;
1678 /* expose anything revealed by the change */
1680 if (!(swp_flags & SWP_NOREDRAW))
1681 exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
1683 if (!(win->style & WS_VISIBLE))
1685 /* clear the update region since the window is no longer visible */
1686 validate_whole_window( win );
1687 validate_children( win );
1688 goto done;
1691 /* crop update region to the new window rect */
1693 if (win->update_region)
1695 if (get_window_visible_rect( win, &rect, 1 ))
1697 struct region *tmp = create_empty_region();
1698 if (tmp)
1700 set_region_rect( tmp, &rect );
1701 if (intersect_region( tmp, win->update_region, tmp ))
1702 set_update_region( win, tmp );
1703 else
1704 free_region( tmp );
1707 else set_update_region( win, NULL ); /* visible rect is empty */
1710 /* crop children regions to the new window rect */
1712 if (get_window_visible_rect( win, &rect, 0 ))
1714 /* map to client coords */
1715 offset_rect( &rect, win->window_rect.left - win->client_rect.left,
1716 win->window_rect.top - win->client_rect.top );
1717 crop_children_update_region( win, &rect );
1719 else crop_children_update_region( win, NULL );
1721 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1723 /* expose the whole non-client area if it changed in any way */
1725 if (swp_flags & SWP_NOCOPYBITS)
1727 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1728 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1729 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
1730 client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
1732 else
1734 /* assume the bits have been moved to follow the window rect */
1735 int x_offset = window_rect->left - old_window_rect.left;
1736 int y_offset = window_rect->top - old_window_rect.top;
1737 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1738 window_rect->right - old_window_rect.right != x_offset ||
1739 window_rect->bottom - old_window_rect.bottom != y_offset ||
1740 visible_rect->left - old_visible_rect.left != x_offset ||
1741 visible_rect->right - old_visible_rect.right != x_offset ||
1742 visible_rect->top - old_visible_rect.top != y_offset ||
1743 visible_rect->bottom - old_visible_rect.bottom != y_offset);
1744 client_changed = (client_rect->left - old_client_rect.left != x_offset ||
1745 client_rect->right - old_client_rect.right != x_offset ||
1746 client_rect->top - old_client_rect.top != y_offset ||
1747 client_rect->bottom - old_client_rect.bottom != y_offset ||
1748 memcmp( valid_rect, client_rect, sizeof(*client_rect) ));
1751 if (frame_changed || client_changed)
1753 struct region *win_rgn = old_vis_rgn; /* reuse previous region */
1755 set_region_rect( win_rgn, window_rect );
1756 if (!is_rect_empty( valid_rect ))
1758 /* subtract the valid portion of client rect from the total region */
1759 struct region *tmp = create_empty_region();
1760 if (tmp)
1762 set_region_rect( tmp, valid_rect );
1763 /* subtract update region since invalid parts of the valid rect won't be copied */
1764 if (win->update_region)
1766 offset_region( tmp, -window_rect->left, -window_rect->top );
1767 subtract_region( tmp, tmp, win->update_region );
1768 offset_region( tmp, window_rect->left, window_rect->top );
1770 if (subtract_region( tmp, win_rgn, tmp )) win_rgn = tmp;
1771 else free_region( tmp );
1774 if (!is_desktop_window(win))
1775 offset_region( win_rgn, -client_rect->left, -client_rect->top );
1776 if (exposed_rgn)
1778 union_region( exposed_rgn, exposed_rgn, win_rgn );
1779 if (win_rgn != old_vis_rgn) free_region( win_rgn );
1781 else
1783 exposed_rgn = win_rgn;
1784 if (win_rgn == old_vis_rgn) old_vis_rgn = NULL;
1788 if (exposed_rgn)
1789 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1791 done:
1792 if (old_vis_rgn) free_region( old_vis_rgn );
1793 if (exposed_rgn) free_region( exposed_rgn );
1794 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1798 /* set the window region, updating the update region if necessary */
1799 static void set_window_region( struct window *win, struct region *region, int redraw )
1801 struct region *old_vis_rgn = NULL, *exposed_rgn;
1803 /* no need to redraw if window is not visible */
1804 if (redraw && !is_visible( win )) redraw = 0;
1806 if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
1808 if (win->win_region) free_region( win->win_region );
1809 win->win_region = region;
1811 /* expose anything revealed by the change */
1812 if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
1814 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1815 free_region( exposed_rgn );
1818 if (old_vis_rgn) free_region( old_vis_rgn );
1819 clear_error(); /* we ignore out of memory errors since the region has been set */
1823 /* destroy a window */
1824 void destroy_window( struct window *win )
1826 /* hide the window */
1827 if (is_visible(win))
1829 struct region *vis_rgn = get_visible_region( win, DCX_WINDOW );
1830 win->style &= ~WS_VISIBLE;
1831 if (vis_rgn)
1833 struct region *exposed_rgn = expose_window( win, &win->window_rect, vis_rgn );
1834 if (exposed_rgn) free_region( exposed_rgn );
1835 free_region( vis_rgn );
1837 validate_whole_window( win );
1838 validate_children( win );
1841 /* destroy all children */
1842 while (!list_empty(&win->children))
1843 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
1844 while (!list_empty(&win->unlinked))
1845 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
1847 /* reset global window pointers, if the corresponding window is destroyed */
1848 if (win == shell_window) shell_window = NULL;
1849 if (win == shell_listview) shell_listview = NULL;
1850 if (win == progman_window) progman_window = NULL;
1851 if (win == taskman_window) taskman_window = NULL;
1852 free_hotkeys( win->desktop, win->handle );
1853 cleanup_clipboard_window( win->desktop, win->handle );
1854 free_user_handle( win->handle );
1855 destroy_properties( win );
1856 list_remove( &win->entry );
1857 if (is_desktop_window(win))
1859 struct desktop *desktop = win->desktop;
1860 assert( desktop->top_window == win || desktop->msg_window == win );
1861 if (desktop->top_window == win) desktop->top_window = NULL;
1862 else desktop->msg_window = NULL;
1864 else if (is_desktop_window( win->parent ))
1866 post_message( win->parent->handle, WM_PARENTNOTIFY, WM_DESTROY, win->handle );
1869 detach_window_thread( win );
1870 if (win->win_region) free_region( win->win_region );
1871 if (win->update_region) free_region( win->update_region );
1872 if (win->class) release_class( win->class );
1873 free( win->text );
1874 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
1875 free( win );
1879 /* create a window */
1880 DECL_HANDLER(create_window)
1882 struct window *win, *parent = NULL, *owner = NULL;
1883 struct unicode_str cls_name = get_req_unicode_str();
1884 atom_t atom;
1886 reply->handle = 0;
1887 if (req->parent && !(parent = get_window( req->parent ))) return;
1889 if (req->owner)
1891 if (!(owner = get_window( req->owner ))) return;
1892 if (is_desktop_window(owner)) owner = NULL;
1893 else if (parent && !is_desktop_window(parent))
1895 /* an owned window must be created as top-level */
1896 set_error( STATUS_ACCESS_DENIED );
1897 return;
1899 else /* owner must be a top-level window */
1900 while ((owner->style & (WS_POPUP|WS_CHILD)) == WS_CHILD && !is_desktop_window(owner->parent))
1901 owner = owner->parent;
1904 atom = cls_name.len ? find_global_atom( NULL, &cls_name ) : req->atom;
1906 if (!(win = create_window( parent, owner, atom, req->instance ))) return;
1908 if (parent && !is_desktop_window( parent ))
1910 win->dpi_awareness = parent->dpi_awareness;
1911 win->dpi = parent->dpi;
1913 else if (!parent || req->awareness != DPI_AWARENESS_PER_MONITOR_AWARE)
1915 win->dpi_awareness = req->awareness;
1916 win->dpi = req->dpi;
1919 reply->handle = win->handle;
1920 reply->parent = win->parent ? win->parent->handle : 0;
1921 reply->owner = win->owner;
1922 reply->extra = win->nb_extra_bytes;
1923 reply->dpi = win->dpi;
1924 reply->awareness = win->dpi_awareness;
1925 reply->class_ptr = get_class_client_ptr( win->class );
1929 /* set the parent of a window */
1930 DECL_HANDLER(set_parent)
1932 struct window *win, *parent = NULL;
1934 if (!(win = get_window( req->handle ))) return;
1935 if (req->parent && !(parent = get_window( req->parent ))) return;
1937 if (is_desktop_window(win))
1939 set_error( STATUS_INVALID_PARAMETER );
1940 return;
1942 reply->old_parent = win->parent->handle;
1943 reply->full_parent = parent ? parent->handle : 0;
1944 set_parent_window( win, parent );
1945 reply->dpi = win->dpi;
1946 reply->awareness = win->dpi_awareness;
1950 /* destroy a window */
1951 DECL_HANDLER(destroy_window)
1953 struct window *win = get_window( req->handle );
1954 if (win)
1956 if (!is_desktop_window(win)) destroy_window( win );
1957 else if (win->thread == current) detach_window_thread( win );
1958 else set_error( STATUS_ACCESS_DENIED );
1963 /* retrieve the desktop window for the current thread */
1964 DECL_HANDLER(get_desktop_window)
1966 struct desktop *desktop = get_thread_desktop( current, 0 );
1967 int force;
1969 if (!desktop) return;
1971 /* if winstation is invisible, then avoid roundtrip */
1972 force = req->force || !(desktop->winstation->flags & WSF_VISIBLE);
1974 if (!desktop->top_window && force) /* create it */
1976 if ((desktop->top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
1978 detach_window_thread( desktop->top_window );
1979 desktop->top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1983 if (!desktop->msg_window && force) /* create it */
1985 static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
1986 static const struct unicode_str name = { messageW, sizeof(messageW) };
1987 atom_t atom = add_global_atom( NULL, &name );
1988 if (atom && (desktop->msg_window = create_window( NULL, NULL, atom, 0 )))
1990 detach_window_thread( desktop->msg_window );
1991 desktop->msg_window->style = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1995 reply->top_window = desktop->top_window ? desktop->top_window->handle : 0;
1996 reply->msg_window = desktop->msg_window ? desktop->msg_window->handle : 0;
1997 release_object( desktop );
2001 /* set a window owner */
2002 DECL_HANDLER(set_window_owner)
2004 struct window *win = get_window( req->handle );
2005 struct window *owner = NULL, *ptr;
2007 if (!win) return;
2008 if (req->owner && !(owner = get_window( req->owner ))) return;
2009 if (is_desktop_window(win))
2011 set_error( STATUS_ACCESS_DENIED );
2012 return;
2015 /* make sure owner is not a successor of window */
2016 for (ptr = owner; ptr; ptr = ptr->owner ? get_window( ptr->owner ) : NULL)
2018 if (ptr == win)
2020 set_error( STATUS_INVALID_PARAMETER );
2021 return;
2025 reply->prev_owner = win->owner;
2026 reply->full_owner = win->owner = owner ? owner->handle : 0;
2030 /* get information from a window handle */
2031 DECL_HANDLER(get_window_info)
2033 struct window *win = get_window( req->handle );
2035 if (!win) return;
2037 reply->full_handle = win->handle;
2038 reply->last_active = win->handle;
2039 reply->is_unicode = win->is_unicode;
2040 reply->awareness = win->dpi_awareness;
2041 reply->dpi = win->dpi ? win->dpi : get_monitor_dpi( win );
2042 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
2043 if (win->thread)
2045 reply->tid = get_thread_id( win->thread );
2046 reply->pid = get_process_id( win->thread->process );
2047 reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
2052 /* set some information in a window */
2053 DECL_HANDLER(set_window_info)
2055 struct window *win = get_window( req->handle );
2057 if (!win) return;
2058 if (req->flags && is_desktop_window(win) && win->thread != current)
2060 set_error( STATUS_ACCESS_DENIED );
2061 return;
2063 if (req->extra_size > sizeof(req->extra_value) ||
2064 req->extra_offset < -1 ||
2065 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
2067 set_win32_error( ERROR_INVALID_INDEX );
2068 return;
2070 if (req->extra_offset != -1)
2072 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
2074 else if (req->flags & SET_WIN_EXTRA)
2076 set_win32_error( ERROR_INVALID_INDEX );
2077 return;
2079 reply->old_style = win->style;
2080 reply->old_ex_style = win->ex_style;
2081 reply->old_id = win->id;
2082 reply->old_instance = win->instance;
2083 reply->old_user_data = win->user_data;
2084 if (req->flags & SET_WIN_STYLE) win->style = req->style;
2085 if (req->flags & SET_WIN_EXSTYLE)
2087 /* WS_EX_TOPMOST can only be changed for unlinked windows */
2088 if (!win->is_linked) win->ex_style = req->ex_style;
2089 else win->ex_style = (req->ex_style & ~WS_EX_TOPMOST) | (win->ex_style & WS_EX_TOPMOST);
2090 if (!(win->ex_style & WS_EX_LAYERED)) win->is_layered = 0;
2092 if (req->flags & SET_WIN_ID) win->id = req->id;
2093 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
2094 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
2095 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
2096 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
2097 &req->extra_value, req->extra_size );
2099 /* changing window style triggers a non-client paint */
2100 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
2104 /* get a list of the window parents, up to the root of the tree */
2105 DECL_HANDLER(get_window_parents)
2107 struct window *ptr, *win = get_window( req->handle );
2108 int total = 0;
2109 user_handle_t *data;
2110 data_size_t len;
2112 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
2114 reply->count = total;
2115 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2116 if (len && ((data = set_reply_data_size( len ))))
2118 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
2119 *data++ = ptr->handle;
2124 /* get a list of the window children */
2125 DECL_HANDLER(get_window_children)
2127 struct window *parent = NULL;
2128 unsigned int total;
2129 user_handle_t *data;
2130 data_size_t len;
2131 struct unicode_str cls_name = get_req_unicode_str();
2132 atom_t atom = req->atom;
2133 struct desktop *desktop = NULL;
2135 if (cls_name.len && !(atom = find_global_atom( NULL, &cls_name ))) return;
2137 if (req->desktop)
2139 if (!(desktop = get_desktop_obj( current->process, req->desktop, DESKTOP_ENUMERATE ))) return;
2140 parent = desktop->top_window;
2142 else
2144 if (req->parent && !(parent = get_window( req->parent ))) return;
2145 if (!parent && !(desktop = get_thread_desktop( current, 0 ))) return;
2148 if (parent)
2149 total = get_children_windows( parent, atom, req->tid, NULL, 0 );
2150 else
2151 total = get_children_windows( desktop->top_window, atom, req->tid, NULL, 0 ) +
2152 get_children_windows( desktop->msg_window, atom, req->tid, NULL, 0 );
2154 reply->count = total;
2155 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2156 if (len && ((data = set_reply_data_size( len ))))
2158 if (parent) get_children_windows( parent, atom, req->tid, data, len / sizeof(user_handle_t) );
2159 else
2161 total = get_children_windows( desktop->top_window, atom, req->tid,
2162 data, len / sizeof(user_handle_t) );
2163 data += total;
2164 len -= total * sizeof(user_handle_t);
2165 if (len >= sizeof(user_handle_t))
2166 get_children_windows( desktop->msg_window, atom, req->tid,
2167 data, len / sizeof(user_handle_t) );
2170 if (desktop) release_object( desktop );
2174 /* get a list of the window children that contain a given point */
2175 DECL_HANDLER(get_window_children_from_point)
2177 struct user_handle_array array;
2178 struct window *parent = get_window( req->parent );
2179 data_size_t len;
2181 if (!parent) return;
2183 array.handles = NULL;
2184 array.count = 0;
2185 array.total = 0;
2186 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
2188 reply->count = array.count;
2189 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
2190 if (len) set_reply_data_ptr( array.handles, len );
2191 else free( array.handles );
2195 /* get window tree information from a window handle */
2196 DECL_HANDLER(get_window_tree)
2198 struct window *ptr, *win = get_window( req->handle );
2200 if (!win) return;
2202 reply->parent = 0;
2203 reply->owner = 0;
2204 reply->next_sibling = 0;
2205 reply->prev_sibling = 0;
2206 reply->first_sibling = 0;
2207 reply->last_sibling = 0;
2208 reply->first_child = 0;
2209 reply->last_child = 0;
2211 if (win->parent)
2213 struct window *parent = win->parent;
2214 reply->parent = parent->handle;
2215 reply->owner = win->owner;
2216 if (win->is_linked)
2218 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
2219 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
2221 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
2222 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
2224 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
2225 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
2229 /* set the position and Z order of a window */
2230 DECL_HANDLER(set_window_pos)
2232 rectangle_t window_rect, client_rect, visible_rect, surface_rect, valid_rect;
2233 const rectangle_t *extra_rects = get_req_data();
2234 struct window *previous = NULL;
2235 struct window *top, *win = get_window( req->handle );
2236 unsigned int flags = req->swp_flags;
2238 if (!win) return;
2239 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
2241 if (!(flags & SWP_NOZORDER))
2243 switch ((int)req->previous)
2245 case 0: /* HWND_TOP */
2246 previous = WINPTR_TOP;
2247 break;
2248 case 1: /* HWND_BOTTOM */
2249 previous = WINPTR_BOTTOM;
2250 break;
2251 case -1: /* HWND_TOPMOST */
2252 previous = WINPTR_TOPMOST;
2253 break;
2254 case -2: /* HWND_NOTOPMOST */
2255 previous = WINPTR_NOTOPMOST;
2256 break;
2257 default:
2258 if (!(previous = get_window( req->previous ))) return;
2259 /* previous must be a sibling */
2260 if (previous->parent != win->parent)
2262 set_error( STATUS_INVALID_PARAMETER );
2263 return;
2265 break;
2267 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
2270 /* windows that use UpdateLayeredWindow don't trigger repaints */
2271 if ((win->ex_style & WS_EX_LAYERED) && !win->is_layered) flags |= SWP_NOREDRAW;
2273 /* window rectangle must be ordered properly */
2274 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
2276 set_error( STATUS_INVALID_PARAMETER );
2277 return;
2280 window_rect = req->window;
2281 client_rect = req->client;
2282 if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = extra_rects[0];
2283 else visible_rect = window_rect;
2284 if (get_req_data_size() >= 2 * sizeof(rectangle_t)) surface_rect = extra_rects[1];
2285 else surface_rect = visible_rect;
2286 if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rect = extra_rects[2];
2287 else valid_rect = empty_rect;
2288 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2290 mirror_rect( &win->parent->client_rect, &window_rect );
2291 mirror_rect( &win->parent->client_rect, &visible_rect );
2292 mirror_rect( &win->parent->client_rect, &client_rect );
2293 mirror_rect( &win->parent->client_rect, &surface_rect );
2294 mirror_rect( &win->parent->client_rect, &valid_rect );
2297 win->paint_flags = (win->paint_flags & ~PAINT_CLIENT_FLAGS) | (req->paint_flags & PAINT_CLIENT_FLAGS);
2298 if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
2300 set_window_pos( win, previous, flags, &window_rect, &client_rect,
2301 &visible_rect, &surface_rect, &valid_rect );
2303 reply->new_style = win->style;
2304 reply->new_ex_style = win->ex_style;
2306 top = get_top_clipping_window( win );
2307 if (is_visible( top ) && (top->paint_flags & PAINT_HAS_SURFACE))
2309 reply->surface_win = top->handle;
2310 reply->needs_update = !!(top->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD));
2315 /* get the window and client rectangles of a window */
2316 DECL_HANDLER(get_window_rectangles)
2318 struct window *win = get_window( req->handle );
2320 if (!win) return;
2322 reply->window = win->window_rect;
2323 reply->client = win->client_rect;
2325 switch (req->relative)
2327 case COORDS_CLIENT:
2328 offset_rect( &reply->window, -win->client_rect.left, -win->client_rect.top );
2329 offset_rect( &reply->client, -win->client_rect.left, -win->client_rect.top );
2330 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &reply->window );
2331 break;
2332 case COORDS_WINDOW:
2333 offset_rect( &reply->window, -win->window_rect.left, -win->window_rect.top );
2334 offset_rect( &reply->client, -win->window_rect.left, -win->window_rect.top );
2335 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->window_rect, &reply->client );
2336 break;
2337 case COORDS_PARENT:
2338 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2340 mirror_rect( &win->parent->client_rect, &reply->window );
2341 mirror_rect( &win->parent->client_rect, &reply->client );
2343 break;
2344 case COORDS_SCREEN:
2345 client_to_screen_rect( win->parent, &reply->window );
2346 client_to_screen_rect( win->parent, &reply->client );
2347 break;
2348 default:
2349 set_error( STATUS_INVALID_PARAMETER );
2350 break;
2355 /* get the window text */
2356 DECL_HANDLER(get_window_text)
2358 struct window *win = get_window( req->handle );
2360 if (win && win->text)
2362 reply->length = strlenW( win->text );
2363 set_reply_data( win->text, min( reply->length * sizeof(WCHAR), get_reply_max_size() ));
2368 /* set the window text */
2369 DECL_HANDLER(set_window_text)
2371 struct window *win = get_window( req->handle );
2373 if (win)
2375 WCHAR *text = NULL;
2376 data_size_t len = get_req_data_size() / sizeof(WCHAR);
2377 if (len)
2379 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
2380 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
2381 text[len] = 0;
2383 free( win->text );
2384 win->text = text;
2389 /* get the coordinates offset between two windows */
2390 DECL_HANDLER(get_windows_offset)
2392 struct window *win;
2393 int mirror_from = 0, mirror_to = 0;
2395 reply->x = reply->y = 0;
2396 if (req->from)
2398 if (!(win = get_window( req->from ))) return;
2399 if (win->ex_style & WS_EX_LAYOUTRTL)
2401 mirror_from = 1;
2402 reply->x += win->client_rect.right - win->client_rect.left;
2404 while (win && !is_desktop_window(win))
2406 reply->x += win->client_rect.left;
2407 reply->y += win->client_rect.top;
2408 win = win->parent;
2411 if (req->to)
2413 if (!(win = get_window( req->to ))) return;
2414 if (win->ex_style & WS_EX_LAYOUTRTL)
2416 mirror_to = 1;
2417 reply->x -= win->client_rect.right - win->client_rect.left;
2419 while (win && !is_desktop_window(win))
2421 reply->x -= win->client_rect.left;
2422 reply->y -= win->client_rect.top;
2423 win = win->parent;
2426 if (mirror_from) reply->x = -reply->x;
2427 reply->mirror = mirror_from ^ mirror_to;
2431 /* get the visible region of a window */
2432 DECL_HANDLER(get_visible_region)
2434 struct region *region;
2435 struct window *top, *win = get_window( req->window );
2437 if (!win) return;
2439 top = get_top_clipping_window( win );
2440 if ((region = get_visible_region( win, req->flags )))
2442 rectangle_t *data;
2443 map_win_region_to_screen( win, region );
2444 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2445 if (data) set_reply_data_ptr( data, reply->total_size );
2447 reply->top_win = top->handle;
2448 reply->top_rect = top->surface_rect;
2450 if (!is_desktop_window(win))
2452 reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
2453 client_to_screen_rect( top->parent, &reply->top_rect );
2454 client_to_screen_rect( win->parent, &reply->win_rect );
2456 else
2458 reply->win_rect.left = 0;
2459 reply->win_rect.top = 0;
2460 reply->win_rect.right = win->client_rect.right - win->client_rect.left;
2461 reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
2463 reply->paint_flags = win->paint_flags & PAINT_CLIENT_FLAGS;
2467 /* get the surface visible region of a window */
2468 DECL_HANDLER(get_surface_region)
2470 struct region *region;
2471 struct window *win = get_window( req->window );
2473 if (!win || !is_visible( win )) return;
2475 if ((region = get_surface_region( win )))
2477 rectangle_t *data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2478 if (data) set_reply_data_ptr( data, reply->total_size );
2480 reply->visible_rect = win->visible_rect;
2484 /* get the window region */
2485 DECL_HANDLER(get_window_region)
2487 rectangle_t *data;
2488 struct window *win = get_window( req->window );
2490 if (!win) return;
2491 if (!win->win_region) return;
2493 if (win->ex_style & WS_EX_LAYOUTRTL)
2495 struct region *region = create_empty_region();
2497 if (!region) return;
2498 if (!copy_region( region, win->win_region ))
2500 free_region( region );
2501 return;
2503 mirror_region( &win->window_rect, region );
2504 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2506 else data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
2508 if (data) set_reply_data_ptr( data, reply->total_size );
2512 /* set the window region */
2513 DECL_HANDLER(set_window_region)
2515 struct region *region = NULL;
2516 struct window *win = get_window( req->window );
2518 if (!win) return;
2520 if (get_req_data_size()) /* no data means remove the region completely */
2522 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2523 return;
2524 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->window_rect, region );
2526 set_window_region( win, region, req->redraw );
2530 /* get a window update region */
2531 DECL_HANDLER(get_update_region)
2533 rectangle_t *data;
2534 unsigned int flags = req->flags;
2535 struct window *from_child = NULL;
2536 struct window *win = get_window( req->window );
2538 reply->flags = 0;
2539 if (!win) return;
2541 if (req->from_child)
2543 struct window *ptr;
2545 if (!(from_child = get_window( req->from_child ))) return;
2547 /* make sure from_child is a child of win */
2548 ptr = from_child;
2549 while (ptr && ptr != win) ptr = ptr->parent;
2550 if (!ptr)
2552 set_error( STATUS_INVALID_PARAMETER );
2553 return;
2557 if (flags & UPDATE_DELAYED_ERASE) /* this means that the previous call didn't erase */
2559 if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
2560 else win->paint_flags |= PAINT_DELAYED_ERASE;
2563 reply->flags = get_window_update_flags( win, from_child, flags, &win );
2564 reply->child = win->handle;
2566 if (flags & UPDATE_NOREGION) return;
2568 if (win->update_region)
2570 /* convert update region to screen coordinates */
2571 struct region *region = create_empty_region();
2573 if (!region) return;
2574 if (!copy_region( region, win->update_region ))
2576 free_region( region );
2577 return;
2579 if ((flags & UPDATE_CLIPCHILDREN) && (win->style & WS_CLIPCHILDREN))
2580 clip_children( win, NULL, region, win->client_rect.left - win->window_rect.left,
2581 win->client_rect.top - win->window_rect.top );
2582 map_win_region_to_screen( win, region );
2583 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
2584 &reply->total_size ))) return;
2585 set_reply_data_ptr( data, reply->total_size );
2588 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
2590 validate_parents( win );
2591 validate_whole_window( win );
2593 else
2595 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
2596 if (reply->flags & UPDATE_ERASE)
2598 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
2599 /* desktop window only gets erased, not repainted */
2600 if (is_desktop_window(win)) validate_whole_window( win );
2606 /* update the z order of a window so that a given rectangle is fully visible */
2607 DECL_HANDLER(update_window_zorder)
2609 rectangle_t tmp, rect = req->rect;
2610 struct window *ptr, *win = get_window( req->window );
2612 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
2613 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &rect );
2614 offset_rect( &rect, win->client_rect.left, win->client_rect.top );
2616 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2618 if (ptr == win) break;
2619 if (!(ptr->style & WS_VISIBLE)) continue;
2620 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2621 if (ptr->is_layered && (ptr->layered_flags & LWA_COLORKEY)) continue;
2622 if (!intersect_rect( &tmp, &ptr->visible_rect, &rect )) continue;
2623 if (ptr->win_region)
2625 tmp = rect;
2626 offset_rect( &tmp, -ptr->window_rect.left, -ptr->window_rect.top );
2627 if (!rect_in_region( ptr->win_region, &tmp )) continue;
2629 /* found a window obscuring the rectangle, now move win above this one */
2630 /* making sure to not violate the topmost rule */
2631 if (!(ptr->ex_style & WS_EX_TOPMOST) || (win->ex_style & WS_EX_TOPMOST))
2633 list_remove( &win->entry );
2634 list_add_before( &ptr->entry, &win->entry );
2636 break;
2641 /* mark parts of a window as needing a redraw */
2642 DECL_HANDLER(redraw_window)
2644 unsigned int flags = req->flags;
2645 struct region *region = NULL;
2646 struct window *win;
2648 if (!req->window)
2650 if (!(win = get_desktop_window( current ))) return;
2652 else
2654 if (!(win = get_window( req->window ))) return;
2655 if (is_desktop_window( win )) flags &= ~RDW_ALLCHILDREN;
2658 if (!is_visible( win )) return; /* nothing to do */
2660 if (flags & (RDW_VALIDATE|RDW_INVALIDATE))
2662 if (get_req_data_size()) /* no data means whole rectangle */
2664 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2665 return;
2666 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->client_rect, region );
2670 redraw_window( win, region, (flags & RDW_INVALIDATE) && (flags & RDW_FRAME), flags );
2671 if (region) free_region( region );
2675 /* set a window property */
2676 DECL_HANDLER(set_window_property)
2678 struct unicode_str name = get_req_unicode_str();
2679 struct window *win = get_window( req->window );
2681 if (!win) return;
2683 if (name.len)
2685 atom_t atom = add_global_atom( NULL, &name );
2686 if (atom)
2688 set_property( win, atom, req->data, PROP_TYPE_STRING );
2689 release_global_atom( NULL, atom );
2692 else set_property( win, req->atom, req->data, PROP_TYPE_ATOM );
2696 /* remove a window property */
2697 DECL_HANDLER(remove_window_property)
2699 struct unicode_str name = get_req_unicode_str();
2700 struct window *win = get_window( req->window );
2702 if (win)
2704 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2705 if (atom) reply->data = remove_property( win, atom );
2710 /* get a window property */
2711 DECL_HANDLER(get_window_property)
2713 struct unicode_str name = get_req_unicode_str();
2714 struct window *win = get_window( req->window );
2716 if (win)
2718 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2719 if (atom) reply->data = get_property( win, atom );
2724 /* get the list of properties of a window */
2725 DECL_HANDLER(get_window_properties)
2727 property_data_t *data;
2728 int i, count, max = get_reply_max_size() / sizeof(*data);
2729 struct window *win = get_window( req->window );
2731 reply->total = 0;
2732 if (!win) return;
2734 for (i = count = 0; i < win->prop_inuse; i++)
2735 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2736 reply->total = count;
2738 if (count > max) count = max;
2739 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2741 for (i = 0; i < win->prop_inuse && count; i++)
2743 if (win->properties[i].type == PROP_TYPE_FREE) continue;
2744 data->atom = win->properties[i].atom;
2745 data->string = (win->properties[i].type == PROP_TYPE_STRING);
2746 data->data = win->properties[i].data;
2747 data++;
2748 count--;
2753 /* get the new window pointer for a global window, checking permissions */
2754 /* helper for set_global_windows request */
2755 static int get_new_global_window( struct window **win, user_handle_t handle )
2757 if (!handle)
2759 *win = NULL;
2760 return 1;
2762 else if (*win)
2764 set_error( STATUS_ACCESS_DENIED );
2765 return 0;
2767 *win = get_window( handle );
2768 return (*win != NULL);
2771 /* Set/get the global windows */
2772 DECL_HANDLER(set_global_windows)
2774 struct window *new_shell_window = shell_window;
2775 struct window *new_shell_listview = shell_listview;
2776 struct window *new_progman_window = progman_window;
2777 struct window *new_taskman_window = taskman_window;
2779 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2780 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2781 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2782 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2784 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2786 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2787 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2789 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2791 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2793 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2795 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2797 shell_window = new_shell_window;
2798 shell_listview = new_shell_listview;
2799 progman_window = new_progman_window;
2800 taskman_window = new_taskman_window;
2803 /* retrieve layered info for a window */
2804 DECL_HANDLER(get_window_layered_info)
2806 struct window *win = get_window( req->handle );
2808 if (!win) return;
2810 if (win->is_layered)
2812 reply->color_key = win->color_key;
2813 reply->alpha = win->alpha;
2814 reply->flags = win->layered_flags;
2816 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2820 /* set layered info for a window */
2821 DECL_HANDLER(set_window_layered_info)
2823 struct window *win = get_window( req->handle );
2825 if (!win) return;
2827 if (win->ex_style & WS_EX_LAYERED)
2829 int was_layered = win->is_layered;
2831 if (req->flags & LWA_ALPHA) win->alpha = req->alpha;
2832 else if (!win->is_layered) win->alpha = 0; /* alpha init value is 0 */
2834 win->color_key = req->color_key;
2835 win->layered_flags = req->flags;
2836 win->is_layered = 1;
2837 /* repaint since we know now it's not going to use UpdateLayeredWindow */
2838 if (!was_layered) redraw_window( win, 0, 1, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
2840 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );