rpcrt4: Write the function header into the procedure format string.
[wine.git] / server / window.c
blobc9b131cba5dee465d916bc7d518e188b068f3596
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 /* map a point between different DPI scaling levels */
634 static void map_dpi_point( struct window *win, int *x, int *y, unsigned int from, unsigned int to )
636 if (!from) from = get_monitor_dpi( win );
637 if (!to) to = get_monitor_dpi( win );
638 if (from == to) return;
639 *x = scale_dpi( *x, from, to );
640 *y = scale_dpi( *y, from, to );
643 /* map a window rectangle between different DPI scaling levels */
644 static void map_dpi_rect( struct window *win, rectangle_t *rect, unsigned int from, unsigned int to )
646 if (!from) from = get_monitor_dpi( win );
647 if (!to) to = get_monitor_dpi( win );
648 if (from == to) return;
649 scale_dpi_rect( rect, from, to );
652 /* map a region between different DPI scaling levels */
653 static void map_dpi_region( struct window *win, struct region *region, unsigned int from, unsigned int to )
655 if (!from) from = get_monitor_dpi( win );
656 if (!to) to = get_monitor_dpi( win );
657 if (from == to) return;
658 scale_region( region, from, to );
661 /* convert coordinates from client to screen coords */
662 static inline void client_to_screen( struct window *win, int *x, int *y )
664 for ( ; win && !is_desktop_window(win); win = win->parent)
666 *x += win->client_rect.left;
667 *y += win->client_rect.top;
671 /* convert coordinates from screen to client coords and dpi */
672 static void screen_to_client( struct window *win, int *x, int *y, unsigned int dpi )
674 int offset_x = 0, offset_y = 0;
676 if (is_desktop_window( win )) return;
678 client_to_screen( win, &offset_x, &offset_y );
679 map_dpi_point( win, x, y, dpi, win->dpi );
680 *x -= offset_x;
681 *y -= offset_y;
684 /* check if window and all its ancestors are visible */
685 static int is_visible( const struct window *win )
687 while (win)
689 if (!(win->style & WS_VISIBLE)) return 0;
690 win = win->parent;
691 /* if parent is minimized children are not visible */
692 if (win && (win->style & WS_MINIMIZE)) return 0;
694 return 1;
697 /* same as is_visible but takes a window handle */
698 int is_window_visible( user_handle_t window )
700 struct window *win = get_user_object( window, USER_WINDOW );
701 if (!win) return 0;
702 return is_visible( win );
705 int is_window_transparent( user_handle_t window )
707 struct window *win = get_user_object( window, USER_WINDOW );
708 if (!win) return 0;
709 return (win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT);
712 /* check if point is inside the window, and map to window dpi */
713 static int is_point_in_window( struct window *win, int *x, int *y, unsigned int dpi )
715 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
716 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
717 return 0; /* disabled child */
718 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
719 return 0; /* transparent */
720 map_dpi_point( win, x, y, dpi, win->dpi );
721 if (!point_in_rect( &win->visible_rect, *x, *y ))
722 return 0; /* not in window */
723 if (win->win_region &&
724 !point_in_region( win->win_region, *x - win->window_rect.left, *y - win->window_rect.top ))
725 return 0; /* not in window region */
726 return 1;
729 /* fill an array with the handles of the children of a specified window */
730 static unsigned int get_children_windows( struct window *parent, atom_t atom, thread_id_t tid,
731 user_handle_t *handles, unsigned int max_count )
733 struct window *ptr;
734 unsigned int count = 0;
736 if (!parent) return 0;
738 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
740 if (atom && get_class_atom(ptr->class) != atom) continue;
741 if (tid && get_thread_id(ptr->thread) != tid) continue;
742 if (handles)
744 if (count >= max_count) break;
745 handles[count] = ptr->handle;
747 count++;
749 return count;
752 /* find child of 'parent' that contains the given point (in parent-relative coords) */
753 static struct window *child_window_from_point( struct window *parent, int x, int y )
755 struct window *ptr;
757 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
759 int x_child = x, y_child = y;
761 if (!is_point_in_window( ptr, &x_child, &y_child, parent->dpi )) continue; /* skip it */
763 /* if window is minimized or disabled, return at once */
764 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
766 /* if point is not in client area, return at once */
767 if (!point_in_rect( &ptr->client_rect, x_child, y_child )) return ptr;
769 return child_window_from_point( ptr, x_child - ptr->client_rect.left,
770 y_child - ptr->client_rect.top );
772 return parent; /* not found any child */
775 /* find all children of 'parent' that contain the given point */
776 static int get_window_children_from_point( struct window *parent, int x, int y,
777 struct user_handle_array *array )
779 struct window *ptr;
781 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
783 int x_child = x, y_child = y;
785 if (!is_point_in_window( ptr, &x_child, &y_child, parent->dpi )) continue; /* skip it */
787 /* if point is in client area, and window is not minimized or disabled, check children */
788 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) && point_in_rect( &ptr->client_rect, x_child, y_child ))
790 if (!get_window_children_from_point( ptr, x_child - ptr->client_rect.left,
791 y_child - ptr->client_rect.top, array ))
792 return 0;
795 /* now add window to the array */
796 if (!add_handle_to_array( array, ptr->handle )) return 0;
798 return 1;
801 /* get handle of root of top-most window containing point */
802 user_handle_t shallow_window_from_point( struct desktop *desktop, int x, int y )
804 struct window *ptr;
806 if (!desktop->top_window) return 0;
808 LIST_FOR_EACH_ENTRY( ptr, &desktop->top_window->children, struct window, entry )
810 int x_child = x, y_child = y;
812 if (!is_point_in_window( ptr, &x_child, &y_child, 0 )) continue; /* skip it */
813 return ptr->handle;
815 return desktop->top_window->handle;
818 /* return thread of top-most window containing point (in absolute coords) */
819 struct thread *window_thread_from_point( user_handle_t scope, int x, int y )
821 struct window *win = get_user_object( scope, USER_WINDOW );
823 if (!win) return NULL;
825 screen_to_client( win, &x, &y, 0 );
826 win = child_window_from_point( win, x, y );
827 if (!win->thread) return NULL;
828 return (struct thread *)grab_object( win->thread );
831 /* return list of all windows containing point (in absolute coords) */
832 static int all_windows_from_point( struct window *top, int x, int y, unsigned int dpi,
833 struct user_handle_array *array )
835 if (!is_desktop_window( top ) && !is_desktop_window( top->parent ))
837 screen_to_client( top->parent, &x, &y, dpi );
838 dpi = top->parent->dpi;
841 if (!is_point_in_window( top, &x, &y, dpi )) return 1;
842 /* if point is in client area, and window is not minimized or disabled, check children */
843 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) && point_in_rect( &top->client_rect, x, y ))
845 if (!is_desktop_window(top))
847 x -= top->client_rect.left;
848 y -= top->client_rect.top;
850 if (!get_window_children_from_point( top, x, y, array )) return 0;
852 /* now add window to the array */
853 if (!add_handle_to_array( array, top->handle )) return 0;
854 return 1;
858 /* return the thread owning a window */
859 struct thread *get_window_thread( user_handle_t handle )
861 struct window *win = get_user_object( handle, USER_WINDOW );
862 if (!win || !win->thread) return NULL;
863 return (struct thread *)grab_object( win->thread );
867 /* check if any area of a window needs repainting */
868 static inline int win_needs_repaint( struct window *win )
870 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
874 /* find a child of the specified window that needs repainting */
875 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
877 struct window *ptr, *ret = NULL;
879 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
881 if (!(ptr->style & WS_VISIBLE)) continue;
882 if (ptr->thread == thread && win_needs_repaint( ptr ))
883 ret = ptr;
884 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
885 ret = find_child_to_repaint( ptr, thread );
886 if (ret) break;
889 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
891 /* transparent window, check for non-transparent sibling to paint first */
892 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
894 if (!(ptr->style & WS_VISIBLE)) continue;
895 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
896 if (ptr->thread != thread) continue;
897 if (win_needs_repaint( ptr )) return ptr;
900 return ret;
904 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
905 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
907 struct window *ptr, *win, *top_window = get_desktop_window( thread );
909 if (!top_window) return 0;
911 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
912 else win = find_child_to_repaint( top_window, thread );
914 if (win && parent)
916 /* check that it is a child of the specified parent */
917 for (ptr = win; ptr; ptr = ptr->parent)
918 if (ptr->handle == parent) break;
919 /* otherwise don't return any window, we don't repaint a child before its parent */
920 if (!ptr) win = NULL;
922 if (!win) return 0;
923 win->paint_flags &= ~PAINT_INTERNAL;
924 return win->handle;
928 /* intersect the window region with the specified region, relative to the window parent */
929 static struct region *intersect_window_region( struct region *region, struct window *win )
931 /* make region relative to window rect */
932 offset_region( region, -win->window_rect.left, -win->window_rect.top );
933 if (!intersect_region( region, region, win->win_region )) return NULL;
934 /* make region relative to parent again */
935 offset_region( region, win->window_rect.left, win->window_rect.top );
936 return region;
940 /* convert coordinates from client to screen coords */
941 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
943 for ( ; win && !is_desktop_window(win); win = win->parent)
944 offset_rect( rect, win->client_rect.left, win->client_rect.top );
947 /* map the region from window to screen coordinates */
948 static inline void map_win_region_to_screen( struct window *win, struct region *region )
950 if (!is_desktop_window(win))
952 int x = win->window_rect.left;
953 int y = win->window_rect.top;
954 client_to_screen( win->parent, &x, &y );
955 offset_region( region, x, y );
960 /* clip all children of a given window out of the visible region */
961 static struct region *clip_children( struct window *parent, struct window *last,
962 struct region *region, int offset_x, int offset_y )
964 struct window *ptr;
965 struct region *tmp = create_empty_region();
967 if (!tmp) return NULL;
968 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
970 if (ptr == last) break;
971 if (!(ptr->style & WS_VISIBLE)) continue;
972 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
973 set_region_rect( tmp, &ptr->visible_rect );
974 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
976 free_region( tmp );
977 return NULL;
979 offset_region( tmp, offset_x, offset_y );
980 if (!(region = subtract_region( region, region, tmp ))) break;
981 if (is_region_empty( region )) break;
983 free_region( tmp );
984 return region;
988 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
989 static void set_region_client_rect( struct region *region, struct window *win )
991 rectangle_t rect;
993 intersect_rect( &rect, &win->window_rect, &win->client_rect );
994 intersect_rect( &rect, &rect, &win->surface_rect );
995 set_region_rect( region, &rect );
999 /* set the region to the visible rect clipped by the window surface, in parent-relative coordinates */
1000 static void set_region_visible_rect( struct region *region, struct window *win )
1002 rectangle_t rect;
1004 intersect_rect( &rect, &win->visible_rect, &win->surface_rect );
1005 set_region_rect( region, &rect );
1009 /* get the top-level window to clip against for a given window */
1010 static inline struct window *get_top_clipping_window( struct window *win )
1012 while (!(win->paint_flags & PAINT_HAS_SURFACE) && win->parent && !is_desktop_window(win->parent))
1013 win = win->parent;
1014 return win;
1018 /* compute the visible region of a window, in window coordinates */
1019 static struct region *get_visible_region( struct window *win, unsigned int flags )
1021 struct region *tmp = NULL, *region;
1022 int offset_x, offset_y;
1024 if (!(region = create_empty_region())) return NULL;
1026 /* first check if all ancestors are visible */
1028 if (!is_visible( win )) return region; /* empty region */
1030 if (is_desktop_window( win ))
1032 set_region_rect( region, &win->window_rect );
1033 return region;
1036 /* create a region relative to the window itself */
1038 if ((flags & DCX_PARENTCLIP) && !is_desktop_window( win->parent ))
1040 set_region_client_rect( region, win->parent );
1041 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
1043 else if (flags & DCX_WINDOW)
1045 set_region_visible_rect( region, win );
1046 if (win->win_region && !intersect_window_region( region, win )) goto error;
1048 else
1050 set_region_client_rect( region, win );
1051 if (win->win_region && !intersect_window_region( region, win )) goto error;
1054 /* clip children */
1056 if (flags & DCX_CLIPCHILDREN)
1058 if (!clip_children( win, NULL, region, win->client_rect.left, win->client_rect.top )) goto error;
1061 /* clip siblings of ancestors */
1063 offset_x = win->window_rect.left;
1064 offset_y = win->window_rect.top;
1066 if ((tmp = create_empty_region()) != NULL)
1068 while (!is_desktop_window( win->parent ))
1070 /* we don't clip out top-level siblings as that's up to the native windowing system */
1071 if (win->style & WS_CLIPSIBLINGS)
1073 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
1074 if (is_region_empty( region )) break;
1076 /* clip to parent client area */
1077 win = win->parent;
1078 offset_x += win->client_rect.left;
1079 offset_y += win->client_rect.top;
1080 offset_region( region, win->client_rect.left, win->client_rect.top );
1081 set_region_client_rect( tmp, win );
1082 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
1083 if (!intersect_region( region, region, tmp )) goto error;
1084 if (is_region_empty( region )) break;
1086 free_region( tmp );
1088 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
1089 return region;
1091 error:
1092 if (tmp) free_region( tmp );
1093 free_region( region );
1094 return NULL;
1098 /* clip all children with a custom pixel format out of the visible region */
1099 static struct region *clip_pixel_format_children( struct window *parent, struct region *parent_clip,
1100 struct region *region, int offset_x, int offset_y )
1102 struct window *ptr;
1103 struct region *clip = create_empty_region();
1105 if (!clip) return NULL;
1107 LIST_FOR_EACH_ENTRY_REV( ptr, &parent->children, struct window, entry )
1109 if (!(ptr->style & WS_VISIBLE)) continue;
1110 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1112 /* add the visible rect */
1113 set_region_rect( clip, &ptr->visible_rect );
1114 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1115 offset_region( clip, offset_x, offset_y );
1116 if (!intersect_region( clip, clip, parent_clip )) break;
1117 if (!union_region( region, region, clip )) break;
1118 if (!(ptr->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))) continue;
1120 /* subtract the client rect if it uses a custom pixel format */
1121 set_region_rect( clip, &ptr->client_rect );
1122 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1123 offset_region( clip, offset_x, offset_y );
1124 if (!intersect_region( clip, clip, parent_clip )) break;
1125 if ((ptr->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1126 break;
1128 if (!clip_pixel_format_children( ptr, clip, region, offset_x + ptr->client_rect.left,
1129 offset_y + ptr->client_rect.top ))
1130 break;
1132 free_region( clip );
1133 return region;
1137 /* compute the visible surface region of a window, in parent coordinates */
1138 static struct region *get_surface_region( struct window *win )
1140 struct region *region, *clip;
1141 int offset_x, offset_y;
1143 /* create a region relative to the window itself */
1145 if (!(region = create_empty_region())) return NULL;
1146 if (!(clip = create_empty_region())) goto error;
1147 set_region_rect( region, &win->visible_rect );
1148 if (win->win_region && !intersect_window_region( region, win )) goto error;
1149 set_region_rect( clip, &win->client_rect );
1150 if (win->win_region && !intersect_window_region( clip, win )) goto error;
1152 if ((win->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1153 goto error;
1155 /* clip children */
1157 if (!is_desktop_window(win))
1159 offset_x = win->client_rect.left;
1160 offset_y = win->client_rect.top;
1162 else offset_x = offset_y = 0;
1164 if (!clip_pixel_format_children( win, clip, region, offset_x, offset_y )) goto error;
1166 free_region( clip );
1167 return region;
1169 error:
1170 if (clip) free_region( clip );
1171 free_region( region );
1172 return NULL;
1176 /* get the window class of a window */
1177 struct window_class* get_window_class( user_handle_t window )
1179 struct window *win;
1180 if (!(win = get_window( window ))) return NULL;
1181 if (!win->class) set_error( STATUS_ACCESS_DENIED );
1182 return win->class;
1185 /* determine the window visible rectangle, i.e. window or client rect cropped by parent rects */
1186 /* the returned rectangle is in window coordinates; return 0 if rectangle is empty */
1187 static int get_window_visible_rect( struct window *win, rectangle_t *rect, int frame )
1189 int offset_x = win->window_rect.left, offset_y = win->window_rect.top;
1191 *rect = frame ? win->window_rect : win->client_rect;
1193 if (!(win->style & WS_VISIBLE)) return 0;
1194 if (is_desktop_window( win )) return 1;
1196 while (!is_desktop_window( win->parent ))
1198 win = win->parent;
1199 if (!(win->style & WS_VISIBLE) || win->style & WS_MINIMIZE) return 0;
1200 offset_x += win->client_rect.left;
1201 offset_y += win->client_rect.top;
1202 offset_rect( rect, win->client_rect.left, win->client_rect.top );
1203 if (!intersect_rect( rect, rect, &win->client_rect )) return 0;
1204 if (!intersect_rect( rect, rect, &win->window_rect )) return 0;
1206 offset_rect( rect, -offset_x, -offset_y );
1207 return 1;
1210 /* return a copy of the specified region cropped to the window client or frame rectangle, */
1211 /* and converted from client to window coordinates. Helper for (in)validate_window. */
1212 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
1214 rectangle_t rect;
1215 struct region *tmp;
1217 if (!get_window_visible_rect( win, &rect, frame )) return NULL;
1218 if (!(tmp = create_empty_region())) return NULL;
1219 set_region_rect( tmp, &rect );
1221 if (region)
1223 /* map it to client coords */
1224 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1225 win->window_rect.top - win->client_rect.top );
1227 /* intersect specified region with bounding rect */
1228 if (!intersect_region( tmp, region, tmp )) goto done;
1229 if (is_region_empty( tmp )) goto done;
1231 /* map it back to window coords */
1232 offset_region( tmp, win->client_rect.left - win->window_rect.left,
1233 win->client_rect.top - win->window_rect.top );
1235 return tmp;
1237 done:
1238 free_region( tmp );
1239 return NULL;
1243 /* set a region as new update region for the window */
1244 static void set_update_region( struct window *win, struct region *region )
1246 if (region && !is_region_empty( region ))
1248 if (!win->update_region) inc_window_paint_count( win, 1 );
1249 else free_region( win->update_region );
1250 win->update_region = region;
1252 else
1254 if (win->update_region)
1256 inc_window_paint_count( win, -1 );
1257 free_region( win->update_region );
1259 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
1260 win->update_region = NULL;
1261 if (region) free_region( region );
1266 /* add a region to the update region; the passed region is freed or reused */
1267 static int add_update_region( struct window *win, struct region *region )
1269 if (win->update_region && !union_region( region, win->update_region, region ))
1271 free_region( region );
1272 return 0;
1274 set_update_region( win, region );
1275 return 1;
1279 /* crop the update region of children to the specified rectangle, in client coords */
1280 static void crop_children_update_region( struct window *win, rectangle_t *rect )
1282 struct window *child;
1283 struct region *tmp;
1284 rectangle_t child_rect;
1286 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1288 if (!(child->style & WS_VISIBLE)) continue;
1289 if (!rect) /* crop everything out */
1291 crop_children_update_region( child, NULL );
1292 set_update_region( child, NULL );
1293 continue;
1296 /* nothing to do if child is completely inside rect */
1297 if (child->window_rect.left >= rect->left &&
1298 child->window_rect.top >= rect->top &&
1299 child->window_rect.right <= rect->right &&
1300 child->window_rect.bottom <= rect->bottom) continue;
1302 /* map to child client coords and crop grand-children */
1303 child_rect = *rect;
1304 offset_rect( &child_rect, -child->client_rect.left, -child->client_rect.top );
1305 crop_children_update_region( child, &child_rect );
1307 /* now crop the child itself */
1308 if (!child->update_region) continue;
1309 if (!(tmp = create_empty_region())) continue;
1310 set_region_rect( tmp, rect );
1311 offset_region( tmp, -child->window_rect.left, -child->window_rect.top );
1312 if (intersect_region( tmp, child->update_region, tmp )) set_update_region( child, tmp );
1313 else free_region( tmp );
1318 /* validate the non client area of a window */
1319 static void validate_non_client( struct window *win )
1321 struct region *tmp;
1322 rectangle_t rect;
1324 if (!win->update_region) return; /* nothing to do */
1326 /* get client rect in window coords */
1327 rect.left = win->client_rect.left - win->window_rect.left;
1328 rect.top = win->client_rect.top - win->window_rect.top;
1329 rect.right = win->client_rect.right - win->window_rect.left;
1330 rect.bottom = win->client_rect.bottom - win->window_rect.top;
1332 if ((tmp = create_empty_region()))
1334 set_region_rect( tmp, &rect );
1335 if (intersect_region( tmp, win->update_region, tmp ))
1336 set_update_region( win, tmp );
1337 else
1338 free_region( tmp );
1340 win->paint_flags &= ~PAINT_NONCLIENT;
1344 /* validate a window completely so that we don't get any further paint messages for it */
1345 static void validate_whole_window( struct window *win )
1347 set_update_region( win, NULL );
1349 if (win->paint_flags & PAINT_INTERNAL)
1351 win->paint_flags &= ~PAINT_INTERNAL;
1352 inc_window_paint_count( win, -1 );
1357 /* validate a window's children so that we don't get any further paint messages for it */
1358 static void validate_children( struct window *win )
1360 struct window *child;
1362 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1364 if (!(child->style & WS_VISIBLE)) continue;
1365 validate_children(child);
1366 validate_whole_window(child);
1371 /* validate the update region of a window on all parents; helper for get_update_region */
1372 static void validate_parents( struct window *child )
1374 int offset_x = 0, offset_y = 0;
1375 struct window *win = child;
1376 struct region *tmp = NULL;
1378 if (!child->update_region) return;
1380 while (win->parent)
1382 /* map to parent client coords */
1383 offset_x += win->window_rect.left;
1384 offset_y += win->window_rect.top;
1386 win = win->parent;
1388 /* and now map to window coords */
1389 offset_x += win->client_rect.left - win->window_rect.left;
1390 offset_y += win->client_rect.top - win->window_rect.top;
1392 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1394 if (!tmp && !(tmp = create_empty_region())) return;
1395 offset_region( child->update_region, offset_x, offset_y );
1396 if (subtract_region( tmp, win->update_region, child->update_region ))
1398 set_update_region( win, tmp );
1399 tmp = NULL;
1401 /* restore child coords */
1402 offset_region( child->update_region, -offset_x, -offset_y );
1405 if (tmp) free_region( tmp );
1409 /* add/subtract a region (in client coordinates) to the update region of the window */
1410 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1412 struct region *child_rgn, *tmp;
1413 struct window *child;
1415 if (flags & RDW_INVALIDATE)
1417 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1419 if (!add_update_region( win, tmp )) return;
1421 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1422 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1424 else if (flags & RDW_VALIDATE)
1426 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1428 set_update_region( win, NULL );
1430 else if (win->update_region)
1432 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1434 if (!subtract_region( tmp, win->update_region, tmp ))
1436 free_region( tmp );
1437 return;
1439 set_update_region( win, tmp );
1441 if (flags & RDW_NOFRAME) validate_non_client( win );
1442 if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1446 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1448 win->paint_flags |= PAINT_INTERNAL;
1449 inc_window_paint_count( win, 1 );
1451 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1453 win->paint_flags &= ~PAINT_INTERNAL;
1454 inc_window_paint_count( win, -1 );
1457 /* now process children recursively */
1459 if (flags & RDW_NOCHILDREN) return;
1460 if (win->style & WS_MINIMIZE) return;
1461 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1463 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1465 /* map to client coordinates */
1466 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1467 win->window_rect.top - win->client_rect.top );
1469 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1471 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1473 if (!(child->style & WS_VISIBLE)) continue;
1474 if (!(child_rgn = create_empty_region())) continue;
1475 if (copy_region( child_rgn, tmp ))
1477 map_dpi_region( child, child_rgn, win->dpi, child->dpi );
1478 if (rect_in_region( child_rgn, &child->window_rect ))
1480 offset_region( child_rgn, -child->client_rect.left, -child->client_rect.top );
1481 redraw_window( child, child_rgn, 1, flags );
1484 free_region( child_rgn );
1487 free_region( tmp );
1491 /* retrieve the update flags for a window depending on the state of the update region */
1492 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1494 unsigned int ret = 0;
1496 if (flags & UPDATE_NONCLIENT)
1498 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1500 if (flags & UPDATE_ERASE)
1502 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1504 if (flags & UPDATE_PAINT)
1506 if (win->update_region)
1508 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1509 ret |= UPDATE_PAINT;
1512 if (flags & UPDATE_INTERNALPAINT)
1514 if (win->paint_flags & PAINT_INTERNAL)
1516 ret |= UPDATE_INTERNALPAINT;
1517 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1520 return ret;
1524 /* iterate through the children of the given window until we find one with some update flags */
1525 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1526 unsigned int flags, struct window **child )
1528 struct window *ptr;
1529 unsigned int ret = 0;
1531 /* first make sure we want to iterate children at all */
1533 if (win->style & WS_MINIMIZE) return 0;
1535 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1536 * here we only want to repaint children of windows that clip them, others
1537 * need to wait for WM_PAINT to be done in the parent first.
1539 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1541 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1543 if (from_child) /* skip all children until from_child is found */
1545 if (ptr == from_child) from_child = NULL;
1546 continue;
1548 if (!(ptr->style & WS_VISIBLE)) continue;
1549 if ((ret = get_update_flags( ptr, flags )) != 0)
1551 *child = ptr;
1552 break;
1554 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1556 return ret;
1559 /* iterate through children and siblings of the given window until we find one with some update flags */
1560 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1561 unsigned int flags, struct window **child )
1563 unsigned int ret;
1564 struct window *ptr, *from_sibling = NULL;
1566 /* if some parent is not visible start from the next sibling */
1568 if (!is_visible( win )) return 0;
1569 for (ptr = from_child; ptr; ptr = ptr->parent)
1571 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1572 if (ptr == win) break;
1575 /* non-client painting must be delayed if one of the parents is going to
1576 * be repainted and doesn't clip children */
1578 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1580 for (ptr = win->parent; ptr; ptr = ptr->parent)
1582 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1583 return 0;
1585 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1587 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1589 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1590 if (ptr == win) break;
1596 /* check window itself (only if not restarting from a child) */
1598 if (!from_child)
1600 if ((ret = get_update_flags( win, flags )))
1602 *child = win;
1603 return ret;
1605 from_child = win;
1608 /* now check children */
1610 if (flags & UPDATE_NOCHILDREN) return 0;
1611 if (!from_sibling)
1613 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1614 from_sibling = from_child;
1617 /* then check siblings and parent siblings */
1619 while (from_sibling->parent && from_sibling != win)
1621 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1622 return ret;
1623 from_sibling = from_sibling->parent;
1625 return 0;
1629 /* expose the areas revealed by a vis region change on the window parent */
1630 /* returns the region exposed on the window itself (in client coordinates) */
1631 static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
1632 struct region *old_vis_rgn )
1634 struct region *new_vis_rgn, *exposed_rgn;
1636 if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
1638 if ((exposed_rgn = create_empty_region()))
1640 if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
1642 /* make it relative to the new client area */
1643 offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
1644 win->window_rect.top - win->client_rect.top );
1646 else
1648 free_region( exposed_rgn );
1649 exposed_rgn = NULL;
1653 if (win->parent && !is_desktop_window( win->parent ))
1655 /* make it relative to the old window pos for subtracting */
1656 offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
1657 win->window_rect.top - old_window_rect->top );
1659 if ((win->parent->style & WS_CLIPCHILDREN) ?
1660 subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) :
1661 xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1663 if (!is_region_empty( new_vis_rgn ))
1665 /* make it relative to parent */
1666 offset_region( new_vis_rgn, old_window_rect->left, old_window_rect->top );
1667 redraw_window( win->parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1671 free_region( new_vis_rgn );
1672 return exposed_rgn;
1676 /* set the window and client rectangles, updating the update region if necessary */
1677 static void set_window_pos( struct window *win, struct window *previous,
1678 unsigned int swp_flags, const rectangle_t *window_rect,
1679 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1680 const rectangle_t *surface_rect, const rectangle_t *valid_rect )
1682 struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
1683 const rectangle_t old_window_rect = win->window_rect;
1684 const rectangle_t old_visible_rect = win->visible_rect;
1685 const rectangle_t old_client_rect = win->client_rect;
1686 rectangle_t rect;
1687 int client_changed, frame_changed;
1688 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1690 if (win->parent && !is_visible( win->parent )) visible = 0;
1692 if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
1694 /* set the new window info before invalidating anything */
1696 win->window_rect = *window_rect;
1697 win->visible_rect = *visible_rect;
1698 win->surface_rect = *surface_rect;
1699 win->client_rect = *client_rect;
1700 if (!(swp_flags & SWP_NOZORDER) && win->parent) link_window( win, previous );
1701 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1702 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1704 /* keep children at the same position relative to top right corner when the parent is mirrored */
1705 if (win->ex_style & WS_EX_LAYOUTRTL)
1707 struct window *child;
1708 int old_size = old_client_rect.right - old_client_rect.left;
1709 int new_size = win->client_rect.right - win->client_rect.left;
1711 if (old_size != new_size) LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1713 offset_rect( &child->window_rect, new_size - old_size, 0 );
1714 offset_rect( &child->visible_rect, new_size - old_size, 0 );
1715 offset_rect( &child->surface_rect, new_size - old_size, 0 );
1716 offset_rect( &child->client_rect, new_size - old_size, 0 );
1720 /* reset cursor clip rectangle when the desktop changes size */
1721 if (win == win->desktop->top_window) win->desktop->cursor.clip = *window_rect;
1723 /* if the window is not visible, everything is easy */
1724 if (!visible) return;
1726 /* expose anything revealed by the change */
1728 if (!(swp_flags & SWP_NOREDRAW))
1729 exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
1731 if (!(win->style & WS_VISIBLE))
1733 /* clear the update region since the window is no longer visible */
1734 validate_whole_window( win );
1735 validate_children( win );
1736 goto done;
1739 /* crop update region to the new window rect */
1741 if (win->update_region)
1743 if (get_window_visible_rect( win, &rect, 1 ))
1745 struct region *tmp = create_empty_region();
1746 if (tmp)
1748 set_region_rect( tmp, &rect );
1749 if (intersect_region( tmp, win->update_region, tmp ))
1750 set_update_region( win, tmp );
1751 else
1752 free_region( tmp );
1755 else set_update_region( win, NULL ); /* visible rect is empty */
1758 /* crop children regions to the new window rect */
1760 if (get_window_visible_rect( win, &rect, 0 ))
1762 /* map to client coords */
1763 offset_rect( &rect, win->window_rect.left - win->client_rect.left,
1764 win->window_rect.top - win->client_rect.top );
1765 crop_children_update_region( win, &rect );
1767 else crop_children_update_region( win, NULL );
1769 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1771 /* expose the whole non-client area if it changed in any way */
1773 if (swp_flags & SWP_NOCOPYBITS)
1775 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1776 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1777 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
1778 client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
1780 else
1782 /* assume the bits have been moved to follow the window rect */
1783 int x_offset = window_rect->left - old_window_rect.left;
1784 int y_offset = window_rect->top - old_window_rect.top;
1785 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1786 window_rect->right - old_window_rect.right != x_offset ||
1787 window_rect->bottom - old_window_rect.bottom != y_offset ||
1788 visible_rect->left - old_visible_rect.left != x_offset ||
1789 visible_rect->right - old_visible_rect.right != x_offset ||
1790 visible_rect->top - old_visible_rect.top != y_offset ||
1791 visible_rect->bottom - old_visible_rect.bottom != y_offset);
1792 client_changed = (client_rect->left - old_client_rect.left != x_offset ||
1793 client_rect->right - old_client_rect.right != x_offset ||
1794 client_rect->top - old_client_rect.top != y_offset ||
1795 client_rect->bottom - old_client_rect.bottom != y_offset ||
1796 memcmp( valid_rect, client_rect, sizeof(*client_rect) ));
1799 if (frame_changed || client_changed)
1801 struct region *win_rgn = old_vis_rgn; /* reuse previous region */
1803 set_region_rect( win_rgn, window_rect );
1804 if (!is_rect_empty( valid_rect ))
1806 /* subtract the valid portion of client rect from the total region */
1807 struct region *tmp = create_empty_region();
1808 if (tmp)
1810 set_region_rect( tmp, valid_rect );
1811 /* subtract update region since invalid parts of the valid rect won't be copied */
1812 if (win->update_region)
1814 offset_region( tmp, -window_rect->left, -window_rect->top );
1815 subtract_region( tmp, tmp, win->update_region );
1816 offset_region( tmp, window_rect->left, window_rect->top );
1818 if (subtract_region( tmp, win_rgn, tmp )) win_rgn = tmp;
1819 else free_region( tmp );
1822 if (!is_desktop_window(win))
1823 offset_region( win_rgn, -client_rect->left, -client_rect->top );
1824 if (exposed_rgn)
1826 union_region( exposed_rgn, exposed_rgn, win_rgn );
1827 if (win_rgn != old_vis_rgn) free_region( win_rgn );
1829 else
1831 exposed_rgn = win_rgn;
1832 if (win_rgn == old_vis_rgn) old_vis_rgn = NULL;
1836 if (exposed_rgn)
1837 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1839 done:
1840 if (old_vis_rgn) free_region( old_vis_rgn );
1841 if (exposed_rgn) free_region( exposed_rgn );
1842 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1846 /* set the window region, updating the update region if necessary */
1847 static void set_window_region( struct window *win, struct region *region, int redraw )
1849 struct region *old_vis_rgn = NULL, *exposed_rgn;
1851 /* no need to redraw if window is not visible */
1852 if (redraw && !is_visible( win )) redraw = 0;
1854 if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
1856 if (win->win_region) free_region( win->win_region );
1857 win->win_region = region;
1859 /* expose anything revealed by the change */
1860 if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
1862 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1863 free_region( exposed_rgn );
1866 if (old_vis_rgn) free_region( old_vis_rgn );
1867 clear_error(); /* we ignore out of memory errors since the region has been set */
1871 /* destroy a window */
1872 void destroy_window( struct window *win )
1874 /* hide the window */
1875 if (is_visible(win))
1877 struct region *vis_rgn = get_visible_region( win, DCX_WINDOW );
1878 win->style &= ~WS_VISIBLE;
1879 if (vis_rgn)
1881 struct region *exposed_rgn = expose_window( win, &win->window_rect, vis_rgn );
1882 if (exposed_rgn) free_region( exposed_rgn );
1883 free_region( vis_rgn );
1885 validate_whole_window( win );
1886 validate_children( win );
1889 /* destroy all children */
1890 while (!list_empty(&win->children))
1891 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
1892 while (!list_empty(&win->unlinked))
1893 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
1895 /* reset global window pointers, if the corresponding window is destroyed */
1896 if (win == shell_window) shell_window = NULL;
1897 if (win == shell_listview) shell_listview = NULL;
1898 if (win == progman_window) progman_window = NULL;
1899 if (win == taskman_window) taskman_window = NULL;
1900 free_hotkeys( win->desktop, win->handle );
1901 cleanup_clipboard_window( win->desktop, win->handle );
1902 free_user_handle( win->handle );
1903 destroy_properties( win );
1904 list_remove( &win->entry );
1905 if (is_desktop_window(win))
1907 struct desktop *desktop = win->desktop;
1908 assert( desktop->top_window == win || desktop->msg_window == win );
1909 if (desktop->top_window == win) desktop->top_window = NULL;
1910 else desktop->msg_window = NULL;
1912 else if (is_desktop_window( win->parent ))
1914 post_message( win->parent->handle, WM_PARENTNOTIFY, WM_DESTROY, win->handle );
1917 detach_window_thread( win );
1918 if (win->win_region) free_region( win->win_region );
1919 if (win->update_region) free_region( win->update_region );
1920 if (win->class) release_class( win->class );
1921 free( win->text );
1922 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
1923 free( win );
1927 /* create a window */
1928 DECL_HANDLER(create_window)
1930 struct window *win, *parent = NULL, *owner = NULL;
1931 struct unicode_str cls_name = get_req_unicode_str();
1932 atom_t atom;
1934 reply->handle = 0;
1935 if (req->parent && !(parent = get_window( req->parent ))) return;
1937 if (req->owner)
1939 if (!(owner = get_window( req->owner ))) return;
1940 if (is_desktop_window(owner)) owner = NULL;
1941 else if (parent && !is_desktop_window(parent))
1943 /* an owned window must be created as top-level */
1944 set_error( STATUS_ACCESS_DENIED );
1945 return;
1947 else /* owner must be a top-level window */
1948 while ((owner->style & (WS_POPUP|WS_CHILD)) == WS_CHILD && !is_desktop_window(owner->parent))
1949 owner = owner->parent;
1952 atom = cls_name.len ? find_global_atom( NULL, &cls_name ) : req->atom;
1954 if (!(win = create_window( parent, owner, atom, req->instance ))) return;
1956 if (parent && !is_desktop_window( parent ))
1958 win->dpi_awareness = parent->dpi_awareness;
1959 win->dpi = parent->dpi;
1961 else if (!parent || req->awareness != DPI_AWARENESS_PER_MONITOR_AWARE)
1963 win->dpi_awareness = req->awareness;
1964 win->dpi = req->dpi;
1967 reply->handle = win->handle;
1968 reply->parent = win->parent ? win->parent->handle : 0;
1969 reply->owner = win->owner;
1970 reply->extra = win->nb_extra_bytes;
1971 reply->dpi = win->dpi;
1972 reply->awareness = win->dpi_awareness;
1973 reply->class_ptr = get_class_client_ptr( win->class );
1977 /* set the parent of a window */
1978 DECL_HANDLER(set_parent)
1980 struct window *win, *parent = NULL;
1982 if (!(win = get_window( req->handle ))) return;
1983 if (req->parent && !(parent = get_window( req->parent ))) return;
1985 if (is_desktop_window(win))
1987 set_error( STATUS_INVALID_PARAMETER );
1988 return;
1990 reply->old_parent = win->parent->handle;
1991 reply->full_parent = parent ? parent->handle : 0;
1992 set_parent_window( win, parent );
1993 reply->dpi = win->dpi;
1994 reply->awareness = win->dpi_awareness;
1998 /* destroy a window */
1999 DECL_HANDLER(destroy_window)
2001 struct window *win = get_window( req->handle );
2002 if (win)
2004 if (!is_desktop_window(win)) destroy_window( win );
2005 else if (win->thread == current) detach_window_thread( win );
2006 else set_error( STATUS_ACCESS_DENIED );
2011 /* retrieve the desktop window for the current thread */
2012 DECL_HANDLER(get_desktop_window)
2014 struct desktop *desktop = get_thread_desktop( current, 0 );
2015 int force;
2017 if (!desktop) return;
2019 /* if winstation is invisible, then avoid roundtrip */
2020 force = req->force || !(desktop->winstation->flags & WSF_VISIBLE);
2022 if (!desktop->top_window && force) /* create it */
2024 if ((desktop->top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
2026 detach_window_thread( desktop->top_window );
2027 desktop->top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
2031 if (!desktop->msg_window && force) /* create it */
2033 static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
2034 static const struct unicode_str name = { messageW, sizeof(messageW) };
2035 atom_t atom = add_global_atom( NULL, &name );
2036 if (atom && (desktop->msg_window = create_window( NULL, NULL, atom, 0 )))
2038 detach_window_thread( desktop->msg_window );
2039 desktop->msg_window->style = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
2043 reply->top_window = desktop->top_window ? desktop->top_window->handle : 0;
2044 reply->msg_window = desktop->msg_window ? desktop->msg_window->handle : 0;
2045 release_object( desktop );
2049 /* set a window owner */
2050 DECL_HANDLER(set_window_owner)
2052 struct window *win = get_window( req->handle );
2053 struct window *owner = NULL, *ptr;
2055 if (!win) return;
2056 if (req->owner && !(owner = get_window( req->owner ))) return;
2057 if (is_desktop_window(win))
2059 set_error( STATUS_ACCESS_DENIED );
2060 return;
2063 /* make sure owner is not a successor of window */
2064 for (ptr = owner; ptr; ptr = ptr->owner ? get_window( ptr->owner ) : NULL)
2066 if (ptr == win)
2068 set_error( STATUS_INVALID_PARAMETER );
2069 return;
2073 reply->prev_owner = win->owner;
2074 reply->full_owner = win->owner = owner ? owner->handle : 0;
2078 /* get information from a window handle */
2079 DECL_HANDLER(get_window_info)
2081 struct window *win = get_window( req->handle );
2083 if (!win) return;
2085 reply->full_handle = win->handle;
2086 reply->last_active = win->handle;
2087 reply->is_unicode = win->is_unicode;
2088 reply->awareness = win->dpi_awareness;
2089 reply->dpi = win->dpi ? win->dpi : get_monitor_dpi( win );
2090 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
2091 if (win->thread)
2093 reply->tid = get_thread_id( win->thread );
2094 reply->pid = get_process_id( win->thread->process );
2095 reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
2100 /* set some information in a window */
2101 DECL_HANDLER(set_window_info)
2103 struct window *win = get_window( req->handle );
2105 if (!win) return;
2106 if (req->flags && is_desktop_window(win) && win->thread != current)
2108 set_error( STATUS_ACCESS_DENIED );
2109 return;
2111 if (req->extra_size > sizeof(req->extra_value) ||
2112 req->extra_offset < -1 ||
2113 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
2115 set_win32_error( ERROR_INVALID_INDEX );
2116 return;
2118 if (req->extra_offset != -1)
2120 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
2122 else if (req->flags & SET_WIN_EXTRA)
2124 set_win32_error( ERROR_INVALID_INDEX );
2125 return;
2127 reply->old_style = win->style;
2128 reply->old_ex_style = win->ex_style;
2129 reply->old_id = win->id;
2130 reply->old_instance = win->instance;
2131 reply->old_user_data = win->user_data;
2132 if (req->flags & SET_WIN_STYLE) win->style = req->style;
2133 if (req->flags & SET_WIN_EXSTYLE)
2135 /* WS_EX_TOPMOST can only be changed for unlinked windows */
2136 if (!win->is_linked) win->ex_style = req->ex_style;
2137 else win->ex_style = (req->ex_style & ~WS_EX_TOPMOST) | (win->ex_style & WS_EX_TOPMOST);
2138 if (!(win->ex_style & WS_EX_LAYERED)) win->is_layered = 0;
2140 if (req->flags & SET_WIN_ID) win->id = req->id;
2141 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
2142 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
2143 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
2144 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
2145 &req->extra_value, req->extra_size );
2147 /* changing window style triggers a non-client paint */
2148 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
2152 /* get a list of the window parents, up to the root of the tree */
2153 DECL_HANDLER(get_window_parents)
2155 struct window *ptr, *win = get_window( req->handle );
2156 int total = 0;
2157 user_handle_t *data;
2158 data_size_t len;
2160 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
2162 reply->count = total;
2163 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2164 if (len && ((data = set_reply_data_size( len ))))
2166 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
2167 *data++ = ptr->handle;
2172 /* get a list of the window children */
2173 DECL_HANDLER(get_window_children)
2175 struct window *parent = NULL;
2176 unsigned int total;
2177 user_handle_t *data;
2178 data_size_t len;
2179 struct unicode_str cls_name = get_req_unicode_str();
2180 atom_t atom = req->atom;
2181 struct desktop *desktop = NULL;
2183 if (cls_name.len && !(atom = find_global_atom( NULL, &cls_name ))) return;
2185 if (req->desktop)
2187 if (!(desktop = get_desktop_obj( current->process, req->desktop, DESKTOP_ENUMERATE ))) return;
2188 parent = desktop->top_window;
2190 else
2192 if (req->parent && !(parent = get_window( req->parent ))) return;
2193 if (!parent && !(desktop = get_thread_desktop( current, 0 ))) return;
2196 if (parent)
2197 total = get_children_windows( parent, atom, req->tid, NULL, 0 );
2198 else
2199 total = get_children_windows( desktop->top_window, atom, req->tid, NULL, 0 ) +
2200 get_children_windows( desktop->msg_window, atom, req->tid, NULL, 0 );
2202 reply->count = total;
2203 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2204 if (len && ((data = set_reply_data_size( len ))))
2206 if (parent) get_children_windows( parent, atom, req->tid, data, len / sizeof(user_handle_t) );
2207 else
2209 total = get_children_windows( desktop->top_window, atom, req->tid,
2210 data, len / sizeof(user_handle_t) );
2211 data += total;
2212 len -= total * sizeof(user_handle_t);
2213 if (len >= sizeof(user_handle_t))
2214 get_children_windows( desktop->msg_window, atom, req->tid,
2215 data, len / sizeof(user_handle_t) );
2218 if (desktop) release_object( desktop );
2222 /* get a list of the window children that contain a given point */
2223 DECL_HANDLER(get_window_children_from_point)
2225 struct user_handle_array array;
2226 struct window *parent = get_window( req->parent );
2227 data_size_t len;
2229 if (!parent) return;
2231 array.handles = NULL;
2232 array.count = 0;
2233 array.total = 0;
2234 if (!all_windows_from_point( parent, req->x, req->y, req->dpi, &array )) return;
2236 reply->count = array.count;
2237 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
2238 if (len) set_reply_data_ptr( array.handles, len );
2239 else free( array.handles );
2243 /* get window tree information from a window handle */
2244 DECL_HANDLER(get_window_tree)
2246 struct window *ptr, *win = get_window( req->handle );
2248 if (!win) return;
2250 reply->parent = 0;
2251 reply->owner = 0;
2252 reply->next_sibling = 0;
2253 reply->prev_sibling = 0;
2254 reply->first_sibling = 0;
2255 reply->last_sibling = 0;
2256 reply->first_child = 0;
2257 reply->last_child = 0;
2259 if (win->parent)
2261 struct window *parent = win->parent;
2262 reply->parent = parent->handle;
2263 reply->owner = win->owner;
2264 if (win->is_linked)
2266 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
2267 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
2269 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
2270 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
2272 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
2273 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
2277 /* set the position and Z order of a window */
2278 DECL_HANDLER(set_window_pos)
2280 rectangle_t window_rect, client_rect, visible_rect, surface_rect, valid_rect;
2281 const rectangle_t *extra_rects = get_req_data();
2282 struct window *previous = NULL;
2283 struct window *top, *win = get_window( req->handle );
2284 unsigned int flags = req->swp_flags;
2286 if (!win) return;
2287 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
2289 if (!(flags & SWP_NOZORDER))
2291 switch ((int)req->previous)
2293 case 0: /* HWND_TOP */
2294 previous = WINPTR_TOP;
2295 break;
2296 case 1: /* HWND_BOTTOM */
2297 previous = WINPTR_BOTTOM;
2298 break;
2299 case -1: /* HWND_TOPMOST */
2300 previous = WINPTR_TOPMOST;
2301 break;
2302 case -2: /* HWND_NOTOPMOST */
2303 previous = WINPTR_NOTOPMOST;
2304 break;
2305 default:
2306 if (!(previous = get_window( req->previous ))) return;
2307 /* previous must be a sibling */
2308 if (previous->parent != win->parent)
2310 set_error( STATUS_INVALID_PARAMETER );
2311 return;
2313 break;
2315 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
2318 /* windows that use UpdateLayeredWindow don't trigger repaints */
2319 if ((win->ex_style & WS_EX_LAYERED) && !win->is_layered) flags |= SWP_NOREDRAW;
2321 /* window rectangle must be ordered properly */
2322 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
2324 set_error( STATUS_INVALID_PARAMETER );
2325 return;
2328 window_rect = req->window;
2329 client_rect = req->client;
2330 if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = extra_rects[0];
2331 else visible_rect = window_rect;
2332 if (get_req_data_size() >= 2 * sizeof(rectangle_t)) surface_rect = extra_rects[1];
2333 else surface_rect = visible_rect;
2334 if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rect = extra_rects[2];
2335 else valid_rect = empty_rect;
2336 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2338 mirror_rect( &win->parent->client_rect, &window_rect );
2339 mirror_rect( &win->parent->client_rect, &visible_rect );
2340 mirror_rect( &win->parent->client_rect, &client_rect );
2341 mirror_rect( &win->parent->client_rect, &surface_rect );
2342 mirror_rect( &win->parent->client_rect, &valid_rect );
2345 win->paint_flags = (win->paint_flags & ~PAINT_CLIENT_FLAGS) | (req->paint_flags & PAINT_CLIENT_FLAGS);
2346 if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
2348 set_window_pos( win, previous, flags, &window_rect, &client_rect,
2349 &visible_rect, &surface_rect, &valid_rect );
2351 reply->new_style = win->style;
2352 reply->new_ex_style = win->ex_style;
2354 top = get_top_clipping_window( win );
2355 if (is_visible( top ) && (top->paint_flags & PAINT_HAS_SURFACE))
2357 reply->surface_win = top->handle;
2358 reply->needs_update = !!(top->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD));
2363 /* get the window and client rectangles of a window */
2364 DECL_HANDLER(get_window_rectangles)
2366 struct window *win = get_window( req->handle );
2368 if (!win) return;
2370 reply->window = win->window_rect;
2371 reply->client = win->client_rect;
2373 switch (req->relative)
2375 case COORDS_CLIENT:
2376 offset_rect( &reply->window, -win->client_rect.left, -win->client_rect.top );
2377 offset_rect( &reply->client, -win->client_rect.left, -win->client_rect.top );
2378 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &reply->window );
2379 break;
2380 case COORDS_WINDOW:
2381 offset_rect( &reply->window, -win->window_rect.left, -win->window_rect.top );
2382 offset_rect( &reply->client, -win->window_rect.left, -win->window_rect.top );
2383 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->window_rect, &reply->client );
2384 break;
2385 case COORDS_PARENT:
2386 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2388 mirror_rect( &win->parent->client_rect, &reply->window );
2389 mirror_rect( &win->parent->client_rect, &reply->client );
2391 break;
2392 case COORDS_SCREEN:
2393 client_to_screen_rect( win->parent, &reply->window );
2394 client_to_screen_rect( win->parent, &reply->client );
2395 break;
2396 default:
2397 set_error( STATUS_INVALID_PARAMETER );
2398 break;
2400 map_dpi_rect( win, &reply->window, win->dpi, req->dpi );
2401 map_dpi_rect( win, &reply->client, win->dpi, req->dpi );
2405 /* get the window text */
2406 DECL_HANDLER(get_window_text)
2408 struct window *win = get_window( req->handle );
2410 if (win && win->text)
2412 reply->length = strlenW( win->text );
2413 set_reply_data( win->text, min( reply->length * sizeof(WCHAR), get_reply_max_size() ));
2418 /* set the window text */
2419 DECL_HANDLER(set_window_text)
2421 struct window *win = get_window( req->handle );
2423 if (win)
2425 WCHAR *text = NULL;
2426 data_size_t len = get_req_data_size() / sizeof(WCHAR);
2427 if (len)
2429 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
2430 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
2431 text[len] = 0;
2433 free( win->text );
2434 win->text = text;
2439 /* get the coordinates offset between two windows */
2440 DECL_HANDLER(get_windows_offset)
2442 struct window *win;
2443 int x, y, mirror_from = 0, mirror_to = 0;
2445 reply->x = reply->y = 0;
2446 if (req->from)
2448 if (!(win = get_window( req->from ))) return;
2449 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_from = 1;
2450 x = mirror_from ? win->client_rect.right - win->client_rect.left : 0;
2451 y = 0;
2452 client_to_screen( win, &x, &y );
2453 map_dpi_point( win, &x, &y, win->dpi, req->dpi );
2454 reply->x += x;
2455 reply->y += y;
2457 if (req->to)
2459 if (!(win = get_window( req->to ))) return;
2460 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_to = 1;
2461 x = mirror_to ? win->client_rect.right - win->client_rect.left : 0;
2462 y = 0;
2463 client_to_screen( win, &x, &y );
2464 map_dpi_point( win, &x, &y, win->dpi, req->dpi );
2465 reply->x -= x;
2466 reply->y -= y;
2468 if (mirror_from) reply->x = -reply->x;
2469 reply->mirror = mirror_from ^ mirror_to;
2473 /* get the visible region of a window */
2474 DECL_HANDLER(get_visible_region)
2476 struct region *region;
2477 struct window *top, *win = get_window( req->window );
2479 if (!win) return;
2481 top = get_top_clipping_window( win );
2482 if ((region = get_visible_region( win, req->flags )))
2484 rectangle_t *data;
2485 map_win_region_to_screen( win, region );
2486 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2487 if (data) set_reply_data_ptr( data, reply->total_size );
2489 reply->top_win = top->handle;
2490 reply->top_rect = top->surface_rect;
2492 if (!is_desktop_window(win))
2494 reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
2495 client_to_screen_rect( top->parent, &reply->top_rect );
2496 client_to_screen_rect( win->parent, &reply->win_rect );
2498 else
2500 reply->win_rect.left = 0;
2501 reply->win_rect.top = 0;
2502 reply->win_rect.right = win->client_rect.right - win->client_rect.left;
2503 reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
2505 reply->paint_flags = win->paint_flags & PAINT_CLIENT_FLAGS;
2509 /* get the surface visible region of a window */
2510 DECL_HANDLER(get_surface_region)
2512 struct region *region;
2513 struct window *win = get_window( req->window );
2515 if (!win || !is_visible( win )) return;
2517 if ((region = get_surface_region( win )))
2519 rectangle_t *data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2520 if (data) set_reply_data_ptr( data, reply->total_size );
2522 reply->visible_rect = win->visible_rect;
2526 /* get the window region */
2527 DECL_HANDLER(get_window_region)
2529 rectangle_t *data;
2530 struct window *win = get_window( req->window );
2532 if (!win) return;
2533 if (!win->win_region) return;
2535 if (win->ex_style & WS_EX_LAYOUTRTL)
2537 struct region *region = create_empty_region();
2539 if (!region) return;
2540 if (!copy_region( region, win->win_region ))
2542 free_region( region );
2543 return;
2545 mirror_region( &win->window_rect, region );
2546 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2548 else data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
2550 if (data) set_reply_data_ptr( data, reply->total_size );
2554 /* set the window region */
2555 DECL_HANDLER(set_window_region)
2557 struct region *region = NULL;
2558 struct window *win = get_window( req->window );
2560 if (!win) return;
2562 if (get_req_data_size()) /* no data means remove the region completely */
2564 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2565 return;
2566 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->window_rect, region );
2568 set_window_region( win, region, req->redraw );
2572 /* get a window update region */
2573 DECL_HANDLER(get_update_region)
2575 rectangle_t *data;
2576 unsigned int flags = req->flags;
2577 struct window *from_child = NULL;
2578 struct window *win = get_window( req->window );
2580 reply->flags = 0;
2581 if (!win) return;
2583 if (req->from_child)
2585 struct window *ptr;
2587 if (!(from_child = get_window( req->from_child ))) return;
2589 /* make sure from_child is a child of win */
2590 ptr = from_child;
2591 while (ptr && ptr != win) ptr = ptr->parent;
2592 if (!ptr)
2594 set_error( STATUS_INVALID_PARAMETER );
2595 return;
2599 if (flags & UPDATE_DELAYED_ERASE) /* this means that the previous call didn't erase */
2601 if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
2602 else win->paint_flags |= PAINT_DELAYED_ERASE;
2605 reply->flags = get_window_update_flags( win, from_child, flags, &win );
2606 reply->child = win->handle;
2608 if (flags & UPDATE_NOREGION) return;
2610 if (win->update_region)
2612 /* convert update region to screen coordinates */
2613 struct region *region = create_empty_region();
2615 if (!region) return;
2616 if (!copy_region( region, win->update_region ))
2618 free_region( region );
2619 return;
2621 if ((flags & UPDATE_CLIPCHILDREN) && (win->style & WS_CLIPCHILDREN))
2622 clip_children( win, NULL, region, win->client_rect.left - win->window_rect.left,
2623 win->client_rect.top - win->window_rect.top );
2624 map_win_region_to_screen( win, region );
2625 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
2626 &reply->total_size ))) return;
2627 set_reply_data_ptr( data, reply->total_size );
2630 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
2632 validate_parents( win );
2633 validate_whole_window( win );
2635 else
2637 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
2638 if (reply->flags & UPDATE_ERASE)
2640 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
2641 /* desktop window only gets erased, not repainted */
2642 if (is_desktop_window(win)) validate_whole_window( win );
2648 /* update the z order of a window so that a given rectangle is fully visible */
2649 DECL_HANDLER(update_window_zorder)
2651 rectangle_t tmp, rect = req->rect;
2652 struct window *ptr, *win = get_window( req->window );
2654 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
2656 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2658 if (ptr == win) break;
2659 if (!(ptr->style & WS_VISIBLE)) continue;
2660 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2661 if (ptr->is_layered && (ptr->layered_flags & LWA_COLORKEY)) continue;
2662 tmp = rect;
2663 map_dpi_rect( win, &tmp, win->parent->dpi, win->dpi );
2664 if (!intersect_rect( &tmp, &tmp, &ptr->visible_rect )) continue;
2665 if (ptr->win_region)
2667 offset_rect( &tmp, -ptr->window_rect.left, -ptr->window_rect.top );
2668 if (!rect_in_region( ptr->win_region, &tmp )) continue;
2670 /* found a window obscuring the rectangle, now move win above this one */
2671 /* making sure to not violate the topmost rule */
2672 if (!(ptr->ex_style & WS_EX_TOPMOST) || (win->ex_style & WS_EX_TOPMOST))
2674 list_remove( &win->entry );
2675 list_add_before( &ptr->entry, &win->entry );
2677 break;
2682 /* mark parts of a window as needing a redraw */
2683 DECL_HANDLER(redraw_window)
2685 unsigned int flags = req->flags;
2686 struct region *region = NULL;
2687 struct window *win;
2689 if (!req->window)
2691 if (!(win = get_desktop_window( current ))) return;
2693 else
2695 if (!(win = get_window( req->window ))) return;
2696 if (is_desktop_window( win )) flags &= ~RDW_ALLCHILDREN;
2699 if (!is_visible( win )) return; /* nothing to do */
2701 if (flags & (RDW_VALIDATE|RDW_INVALIDATE))
2703 if (get_req_data_size()) /* no data means whole rectangle */
2705 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2706 return;
2707 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->client_rect, region );
2711 redraw_window( win, region, (flags & RDW_INVALIDATE) && (flags & RDW_FRAME), flags );
2712 if (region) free_region( region );
2716 /* set a window property */
2717 DECL_HANDLER(set_window_property)
2719 struct unicode_str name = get_req_unicode_str();
2720 struct window *win = get_window( req->window );
2722 if (!win) return;
2724 if (name.len)
2726 atom_t atom = add_global_atom( NULL, &name );
2727 if (atom)
2729 set_property( win, atom, req->data, PROP_TYPE_STRING );
2730 release_global_atom( NULL, atom );
2733 else set_property( win, req->atom, req->data, PROP_TYPE_ATOM );
2737 /* remove a window property */
2738 DECL_HANDLER(remove_window_property)
2740 struct unicode_str name = get_req_unicode_str();
2741 struct window *win = get_window( req->window );
2743 if (win)
2745 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2746 if (atom) reply->data = remove_property( win, atom );
2751 /* get a window property */
2752 DECL_HANDLER(get_window_property)
2754 struct unicode_str name = get_req_unicode_str();
2755 struct window *win = get_window( req->window );
2757 if (win)
2759 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2760 if (atom) reply->data = get_property( win, atom );
2765 /* get the list of properties of a window */
2766 DECL_HANDLER(get_window_properties)
2768 property_data_t *data;
2769 int i, count, max = get_reply_max_size() / sizeof(*data);
2770 struct window *win = get_window( req->window );
2772 reply->total = 0;
2773 if (!win) return;
2775 for (i = count = 0; i < win->prop_inuse; i++)
2776 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2777 reply->total = count;
2779 if (count > max) count = max;
2780 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2782 for (i = 0; i < win->prop_inuse && count; i++)
2784 if (win->properties[i].type == PROP_TYPE_FREE) continue;
2785 data->atom = win->properties[i].atom;
2786 data->string = (win->properties[i].type == PROP_TYPE_STRING);
2787 data->data = win->properties[i].data;
2788 data++;
2789 count--;
2794 /* get the new window pointer for a global window, checking permissions */
2795 /* helper for set_global_windows request */
2796 static int get_new_global_window( struct window **win, user_handle_t handle )
2798 if (!handle)
2800 *win = NULL;
2801 return 1;
2803 else if (*win)
2805 set_error( STATUS_ACCESS_DENIED );
2806 return 0;
2808 *win = get_window( handle );
2809 return (*win != NULL);
2812 /* Set/get the global windows */
2813 DECL_HANDLER(set_global_windows)
2815 struct window *new_shell_window = shell_window;
2816 struct window *new_shell_listview = shell_listview;
2817 struct window *new_progman_window = progman_window;
2818 struct window *new_taskman_window = taskman_window;
2820 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2821 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2822 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2823 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2825 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2827 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2828 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2830 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2832 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2834 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2836 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2838 shell_window = new_shell_window;
2839 shell_listview = new_shell_listview;
2840 progman_window = new_progman_window;
2841 taskman_window = new_taskman_window;
2844 /* retrieve layered info for a window */
2845 DECL_HANDLER(get_window_layered_info)
2847 struct window *win = get_window( req->handle );
2849 if (!win) return;
2851 if (win->is_layered)
2853 reply->color_key = win->color_key;
2854 reply->alpha = win->alpha;
2855 reply->flags = win->layered_flags;
2857 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2861 /* set layered info for a window */
2862 DECL_HANDLER(set_window_layered_info)
2864 struct window *win = get_window( req->handle );
2866 if (!win) return;
2868 if (win->ex_style & WS_EX_LAYERED)
2870 int was_layered = win->is_layered;
2872 if (req->flags & LWA_ALPHA) win->alpha = req->alpha;
2873 else if (!win->is_layered) win->alpha = 0; /* alpha init value is 0 */
2875 win->color_key = req->color_key;
2876 win->layered_flags = req->flags;
2877 win->is_layered = 1;
2878 /* repaint since we know now it's not going to use UpdateLayeredWindow */
2879 if (!was_layered) redraw_window( win, 0, 1, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
2881 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );