ole32: Use wide-char string literals.
[wine.git] / server / window.c
blob3a88b7f34fa188d90184d8ddcabc99958d26eeb6
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 data_size_t text_len; /* length of window caption */
92 unsigned int paint_flags; /* various painting flags */
93 int prop_inuse; /* number of in-use window properties */
94 int prop_alloc; /* number of allocated window properties */
95 struct property *properties; /* window properties array */
96 int nb_extra_bytes; /* number of extra bytes */
97 char extra_bytes[1]; /* extra bytes storage */
100 /* flags that can be set by the client */
101 #define PAINT_HAS_SURFACE SET_WINPOS_PAINT_SURFACE
102 #define PAINT_HAS_PIXEL_FORMAT SET_WINPOS_PIXEL_FORMAT
103 #define PAINT_CLIENT_FLAGS (PAINT_HAS_SURFACE | PAINT_HAS_PIXEL_FORMAT)
104 /* flags only manipulated by the server */
105 #define PAINT_INTERNAL 0x0010 /* internal WM_PAINT pending */
106 #define PAINT_ERASE 0x0020 /* needs WM_ERASEBKGND */
107 #define PAINT_NONCLIENT 0x0040 /* needs WM_NCPAINT */
108 #define PAINT_DELAYED_ERASE 0x0080 /* still needs erase after WM_ERASEBKGND */
109 #define PAINT_PIXEL_FORMAT_CHILD 0x0100 /* at least one child has a custom pixel format */
111 /* growable array of user handles */
112 struct user_handle_array
114 user_handle_t *handles;
115 int count;
116 int total;
119 static const rectangle_t empty_rect;
121 /* global window pointers */
122 static struct window *shell_window;
123 static struct window *shell_listview;
124 static struct window *progman_window;
125 static struct window *taskman_window;
127 /* magic HWND_TOP etc. pointers */
128 #define WINPTR_TOP ((struct window *)1L)
129 #define WINPTR_BOTTOM ((struct window *)2L)
130 #define WINPTR_TOPMOST ((struct window *)3L)
131 #define WINPTR_NOTOPMOST ((struct window *)4L)
133 /* retrieve a pointer to a window from its handle */
134 static inline struct window *get_window( user_handle_t handle )
136 struct window *ret = get_user_object( handle, USER_WINDOW );
137 if (!ret) set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
138 return ret;
141 /* check if window is the desktop */
142 static inline int is_desktop_window( const struct window *win )
144 return !win->parent; /* only desktop windows have no parent */
147 /* get next window in Z-order list */
148 static inline struct window *get_next_window( struct window *win )
150 struct list *ptr = list_next( &win->parent->children, &win->entry );
151 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
154 /* get previous window in Z-order list */
155 static inline struct window *get_prev_window( struct window *win )
157 struct list *ptr = list_prev( &win->parent->children, &win->entry );
158 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
161 /* get first child in Z-order list */
162 static inline struct window *get_first_child( struct window *win )
164 struct list *ptr = list_head( &win->children );
165 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
168 /* get last child in Z-order list */
169 static inline struct window *get_last_child( struct window *win )
171 struct list *ptr = list_tail( &win->children );
172 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
175 /* set the PAINT_PIXEL_FORMAT_CHILD flag on all the parents */
176 /* note: we never reset the flag, it's just a heuristic */
177 static inline void update_pixel_format_flags( struct window *win )
179 for (win = win->parent; win && win->parent; win = win->parent)
180 win->paint_flags |= PAINT_PIXEL_FORMAT_CHILD;
183 /* get the per-monitor DPI for a window */
184 static unsigned int get_monitor_dpi( struct window *win )
186 /* FIXME: we return the desktop window DPI for now */
187 while (!is_desktop_window( win )) win = win->parent;
188 return win->dpi ? win->dpi : USER_DEFAULT_SCREEN_DPI;
191 /* link a window at the right place in the siblings list */
192 static void link_window( struct window *win, struct window *previous )
194 if (previous == WINPTR_NOTOPMOST)
196 if (!(win->ex_style & WS_EX_TOPMOST) && win->is_linked) return; /* nothing to do */
197 win->ex_style &= ~WS_EX_TOPMOST;
198 previous = WINPTR_TOP; /* fallback to the HWND_TOP case */
201 list_remove( &win->entry ); /* unlink it from the previous location */
203 if (previous == WINPTR_BOTTOM)
205 list_add_tail( &win->parent->children, &win->entry );
206 win->ex_style &= ~WS_EX_TOPMOST;
208 else if (previous == WINPTR_TOPMOST)
210 list_add_head( &win->parent->children, &win->entry );
211 win->ex_style |= WS_EX_TOPMOST;
213 else if (previous == WINPTR_TOP)
215 struct list *entry = win->parent->children.next;
216 if (!(win->ex_style & WS_EX_TOPMOST)) /* put it above the first non-topmost window */
218 while (entry != &win->parent->children)
220 struct window *next = LIST_ENTRY( entry, struct window, entry );
221 if (!(next->ex_style & WS_EX_TOPMOST)) break;
222 if (next->handle == win->owner) /* keep it above owner */
224 win->ex_style |= WS_EX_TOPMOST;
225 break;
227 entry = entry->next;
230 list_add_before( entry, &win->entry );
232 else
234 list_add_after( &previous->entry, &win->entry );
235 if (!(previous->ex_style & WS_EX_TOPMOST)) win->ex_style &= ~WS_EX_TOPMOST;
236 else
238 struct window *next = get_next_window( win );
239 if (next && (next->ex_style & WS_EX_TOPMOST)) win->ex_style |= WS_EX_TOPMOST;
243 win->is_linked = 1;
246 /* change the parent of a window (or unlink the window if the new parent is NULL) */
247 static int set_parent_window( struct window *win, struct window *parent )
249 struct window *ptr;
251 /* make sure parent is not a child of window */
252 for (ptr = parent; ptr; ptr = ptr->parent)
254 if (ptr == win)
256 set_error( STATUS_INVALID_PARAMETER );
257 return 0;
261 if (parent)
263 win->parent = parent;
264 link_window( win, WINPTR_TOP );
266 if (!is_desktop_window( parent ))
268 win->dpi = parent->dpi;
269 win->dpi_awareness = parent->dpi_awareness;
272 /* if parent belongs to a different thread and the window isn't */
273 /* top-level, attach the two threads */
274 if (parent->thread && parent->thread != win->thread && !is_desktop_window(parent))
275 attach_thread_input( win->thread, parent->thread );
277 if (win->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))
278 update_pixel_format_flags( win );
280 else /* move it to parent unlinked list */
282 list_remove( &win->entry ); /* unlink it from the previous location */
283 list_add_head( &win->parent->unlinked, &win->entry );
284 win->is_linked = 0;
286 return 1;
289 /* append a user handle to a handle array */
290 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
292 if (array->count >= array->total)
294 int new_total = max( array->total * 2, 32 );
295 user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
296 if (!new_array)
298 free( array->handles );
299 set_error( STATUS_NO_MEMORY );
300 return 0;
302 array->handles = new_array;
303 array->total = new_total;
305 array->handles[array->count++] = handle;
306 return 1;
309 /* set a window property */
310 static void set_property( struct window *win, atom_t atom, lparam_t data, enum property_type type )
312 int i, free = -1;
313 struct property *new_props;
315 /* check if it exists already */
316 for (i = 0; i < win->prop_inuse; i++)
318 if (win->properties[i].type == PROP_TYPE_FREE)
320 free = i;
321 continue;
323 if (win->properties[i].atom == atom)
325 win->properties[i].type = type;
326 win->properties[i].data = data;
327 return;
331 /* need to add an entry */
332 if (!grab_global_atom( NULL, atom )) return;
333 if (free == -1)
335 /* no free entry */
336 if (win->prop_inuse >= win->prop_alloc)
338 /* need to grow the array */
339 if (!(new_props = realloc( win->properties,
340 sizeof(*new_props) * (win->prop_alloc + 16) )))
342 set_error( STATUS_NO_MEMORY );
343 release_global_atom( NULL, atom );
344 return;
346 win->prop_alloc += 16;
347 win->properties = new_props;
349 free = win->prop_inuse++;
351 win->properties[free].atom = atom;
352 win->properties[free].type = type;
353 win->properties[free].data = data;
356 /* remove a window property */
357 static lparam_t remove_property( struct window *win, atom_t atom )
359 int i;
361 for (i = 0; i < win->prop_inuse; i++)
363 if (win->properties[i].type == PROP_TYPE_FREE) continue;
364 if (win->properties[i].atom == atom)
366 release_global_atom( NULL, atom );
367 win->properties[i].type = PROP_TYPE_FREE;
368 return win->properties[i].data;
371 /* FIXME: last error? */
372 return 0;
375 /* find a window property */
376 static lparam_t get_property( struct window *win, atom_t atom )
378 int i;
380 for (i = 0; i < win->prop_inuse; i++)
382 if (win->properties[i].type == PROP_TYPE_FREE) continue;
383 if (win->properties[i].atom == atom) return win->properties[i].data;
385 /* FIXME: last error? */
386 return 0;
389 /* destroy all properties of a window */
390 static inline void destroy_properties( struct window *win )
392 int i;
394 if (!win->properties) return;
395 for (i = 0; i < win->prop_inuse; i++)
397 if (win->properties[i].type == PROP_TYPE_FREE) continue;
398 release_global_atom( NULL, win->properties[i].atom );
400 free( win->properties );
403 /* detach a window from its owner thread but keep the window around */
404 static void detach_window_thread( struct window *win )
406 struct thread *thread = win->thread;
408 if (!thread) return;
409 if (thread->queue)
411 if (win->update_region) inc_queue_paint_count( thread, -1 );
412 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
413 queue_cleanup_window( thread, win->handle );
415 assert( thread->desktop_users > 0 );
416 thread->desktop_users--;
417 release_class( win->class );
418 win->class = NULL;
420 /* don't hold a reference to the desktop so that the desktop window can be */
421 /* destroyed when the desktop ref count reaches zero */
422 release_object( win->desktop );
423 win->thread = NULL;
426 /* get the process owning the top window of a given desktop */
427 struct process *get_top_window_owner( struct desktop *desktop )
429 struct window *win = desktop->top_window;
430 if (!win || !win->thread) return NULL;
431 return win->thread->process;
434 /* get the top window size of a given desktop */
435 void get_top_window_rectangle( struct desktop *desktop, rectangle_t *rect )
437 struct window *win = desktop->top_window;
438 *rect = win ? win->window_rect : empty_rect;
441 /* post a message to the desktop window */
442 void post_desktop_message( struct desktop *desktop, unsigned int message,
443 lparam_t wparam, lparam_t lparam )
445 struct window *win = desktop->top_window;
446 if (win && win->thread) post_message( win->handle, message, wparam, lparam );
449 /* create a new window structure (note: the window is not linked in the window tree) */
450 static struct window *create_window( struct window *parent, struct window *owner,
451 atom_t atom, mod_handle_t instance )
453 int extra_bytes;
454 struct window *win = NULL;
455 struct desktop *desktop;
456 struct window_class *class;
458 if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
460 if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
462 release_object( desktop );
463 return NULL;
466 if (!parent) /* null parent is only allowed for desktop or HWND_MESSAGE top window */
468 if (is_desktop_class( class ))
469 parent = desktop->top_window; /* use existing desktop if any */
470 else if (is_hwnd_message_class( class ))
471 /* use desktop window if message window is already created */
472 parent = desktop->msg_window ? desktop->top_window : NULL;
473 else if (!(parent = desktop->top_window)) /* must already have a desktop then */
475 set_error( STATUS_ACCESS_DENIED );
476 goto failed;
480 /* parent must be on the same desktop */
481 if (parent && parent->desktop != desktop)
483 set_error( STATUS_ACCESS_DENIED );
484 goto failed;
487 if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
488 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
490 win->parent = parent;
491 win->owner = owner ? owner->handle : 0;
492 win->thread = current;
493 win->desktop = desktop;
494 win->class = class;
495 win->atom = atom;
496 win->last_active = win->handle;
497 win->win_region = NULL;
498 win->update_region = NULL;
499 win->style = 0;
500 win->ex_style = 0;
501 win->id = 0;
502 win->instance = 0;
503 win->is_unicode = 1;
504 win->is_linked = 0;
505 win->is_layered = 0;
506 win->dpi_awareness = DPI_AWARENESS_PER_MONITOR_AWARE;
507 win->dpi = 0;
508 win->user_data = 0;
509 win->text = NULL;
510 win->text_len = 0;
511 win->paint_flags = 0;
512 win->prop_inuse = 0;
513 win->prop_alloc = 0;
514 win->properties = NULL;
515 win->nb_extra_bytes = extra_bytes;
516 win->window_rect = win->visible_rect = win->surface_rect = win->client_rect = empty_rect;
517 memset( win->extra_bytes, 0, extra_bytes );
518 list_init( &win->children );
519 list_init( &win->unlinked );
521 /* if parent belongs to a different thread and the window isn't */
522 /* top-level, attach the two threads */
523 if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
525 if (!attach_thread_input( current, parent->thread )) goto failed;
527 else /* otherwise just make sure that the thread has a message queue */
529 if (!current->queue && !init_thread_queue( current )) goto failed;
532 /* put it on parent unlinked list */
533 if (parent) list_add_head( &parent->unlinked, &win->entry );
534 else
536 list_init( &win->entry );
537 if (is_desktop_class( class ))
539 assert( !desktop->top_window );
540 desktop->top_window = win;
541 set_process_default_desktop( current->process, desktop, current->desktop );
543 else
545 assert( !desktop->msg_window );
546 desktop->msg_window = win;
550 current->desktop_users++;
551 return win;
553 failed:
554 if (win)
556 if (win->handle) free_user_handle( win->handle );
557 free( win );
559 release_object( desktop );
560 release_class( class );
561 return NULL;
564 /* destroy all windows belonging to a given thread */
565 void destroy_thread_windows( struct thread *thread )
567 user_handle_t handle = 0;
568 struct window *win;
570 while ((win = next_user_handle( &handle, USER_WINDOW )))
572 if (win->thread != thread) continue;
573 if (is_desktop_window( win )) detach_window_thread( win );
574 else destroy_window( win );
578 /* get the desktop window */
579 static struct window *get_desktop_window( struct thread *thread )
581 struct window *top_window;
582 struct desktop *desktop = get_thread_desktop( thread, 0 );
584 if (!desktop) return NULL;
585 top_window = desktop->top_window;
586 release_object( desktop );
587 return top_window;
590 /* check whether child is a descendant of parent */
591 int is_child_window( user_handle_t parent, user_handle_t child )
593 struct window *child_ptr = get_user_object( child, USER_WINDOW );
594 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
596 if (!child_ptr || !parent_ptr) return 0;
597 while (child_ptr->parent)
599 if (child_ptr->parent == parent_ptr) return 1;
600 child_ptr = child_ptr->parent;
602 return 0;
605 /* check if window can be set as foreground window */
606 int is_valid_foreground_window( user_handle_t window )
608 struct window *win = get_user_object( window, USER_WINDOW );
609 return win && (win->style & (WS_POPUP|WS_CHILD)) != WS_CHILD;
612 /* make a window active if possible */
613 int make_window_active( user_handle_t window )
615 struct window *owner, *win = get_window( window );
617 if (!win) return 0;
619 /* set last active for window and its owners */
620 owner = win;
621 while (owner)
623 owner->last_active = win->handle;
624 owner = get_user_object( owner->owner, USER_WINDOW );
626 return 1;
629 /* increment (or decrement) the window paint count */
630 static inline void inc_window_paint_count( struct window *win, int incr )
632 if (win->thread) inc_queue_paint_count( win->thread, incr );
635 /* map a point between different DPI scaling levels */
636 static void map_dpi_point( struct window *win, int *x, int *y, unsigned int from, unsigned int to )
638 if (!from) from = get_monitor_dpi( win );
639 if (!to) to = get_monitor_dpi( win );
640 if (from == to) return;
641 *x = scale_dpi( *x, from, to );
642 *y = scale_dpi( *y, from, to );
645 /* map a window rectangle between different DPI scaling levels */
646 static void map_dpi_rect( struct window *win, rectangle_t *rect, unsigned int from, unsigned int to )
648 if (!from) from = get_monitor_dpi( win );
649 if (!to) to = get_monitor_dpi( win );
650 if (from == to) return;
651 scale_dpi_rect( rect, from, to );
654 /* map a region between different DPI scaling levels */
655 static void map_dpi_region( struct window *win, struct region *region, unsigned int from, unsigned int to )
657 if (!from) from = get_monitor_dpi( win );
658 if (!to) to = get_monitor_dpi( win );
659 if (from == to) return;
660 scale_region( region, from, to );
663 /* convert coordinates from client to screen coords */
664 static inline void client_to_screen( struct window *win, int *x, int *y )
666 for ( ; win && !is_desktop_window(win); win = win->parent)
668 *x += win->client_rect.left;
669 *y += win->client_rect.top;
673 /* convert coordinates from screen to client coords and dpi */
674 static void screen_to_client( struct window *win, int *x, int *y, unsigned int dpi )
676 int offset_x = 0, offset_y = 0;
678 if (is_desktop_window( win )) return;
680 client_to_screen( win, &offset_x, &offset_y );
681 map_dpi_point( win, x, y, dpi, win->dpi );
682 *x -= offset_x;
683 *y -= offset_y;
686 /* check if window and all its ancestors are visible */
687 static int is_visible( const struct window *win )
689 while (win)
691 if (!(win->style & WS_VISIBLE)) return 0;
692 win = win->parent;
693 /* if parent is minimized children are not visible */
694 if (win && (win->style & WS_MINIMIZE)) return 0;
696 return 1;
699 /* same as is_visible but takes a window handle */
700 int is_window_visible( user_handle_t window )
702 struct window *win = get_user_object( window, USER_WINDOW );
703 if (!win) return 0;
704 return is_visible( win );
707 int is_window_transparent( user_handle_t window )
709 struct window *win = get_user_object( window, USER_WINDOW );
710 if (!win) return 0;
711 return (win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT);
714 /* check if point is inside the window, and map to window dpi */
715 static int is_point_in_window( struct window *win, int *x, int *y, unsigned int dpi )
717 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
718 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
719 return 0; /* disabled child */
720 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
721 return 0; /* transparent */
722 map_dpi_point( win, x, y, dpi, win->dpi );
723 if (!point_in_rect( &win->visible_rect, *x, *y ))
724 return 0; /* not in window */
725 if (win->win_region &&
726 !point_in_region( win->win_region, *x - win->window_rect.left, *y - win->window_rect.top ))
727 return 0; /* not in window region */
728 return 1;
731 /* fill an array with the handles of the children of a specified window */
732 static unsigned int get_children_windows( struct window *parent, atom_t atom, thread_id_t tid,
733 user_handle_t *handles, unsigned int max_count )
735 struct window *ptr;
736 unsigned int count = 0;
738 if (!parent) return 0;
740 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
742 if (atom && get_class_atom(ptr->class) != atom) continue;
743 if (tid && get_thread_id(ptr->thread) != tid) continue;
744 if (handles)
746 if (count >= max_count) break;
747 handles[count] = ptr->handle;
749 count++;
751 return count;
754 /* find child of 'parent' that contains the given point (in parent-relative coords) */
755 static struct window *child_window_from_point( struct window *parent, int x, int y )
757 struct window *ptr;
759 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
761 int x_child = x, y_child = y;
763 if (!is_point_in_window( ptr, &x_child, &y_child, parent->dpi )) continue; /* skip it */
765 /* if window is minimized or disabled, return at once */
766 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
768 /* if point is not in client area, return at once */
769 if (!point_in_rect( &ptr->client_rect, x_child, y_child )) return ptr;
771 return child_window_from_point( ptr, x_child - ptr->client_rect.left,
772 y_child - ptr->client_rect.top );
774 return parent; /* not found any child */
777 /* find all children of 'parent' that contain the given point */
778 static int get_window_children_from_point( struct window *parent, int x, int y,
779 struct user_handle_array *array )
781 struct window *ptr;
783 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
785 int x_child = x, y_child = y;
787 if (!is_point_in_window( ptr, &x_child, &y_child, parent->dpi )) continue; /* skip it */
789 /* if point is in client area, and window is not minimized or disabled, check children */
790 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) && point_in_rect( &ptr->client_rect, x_child, y_child ))
792 if (!get_window_children_from_point( ptr, x_child - ptr->client_rect.left,
793 y_child - ptr->client_rect.top, array ))
794 return 0;
797 /* now add window to the array */
798 if (!add_handle_to_array( array, ptr->handle )) return 0;
800 return 1;
803 /* get handle of root of top-most window containing point */
804 user_handle_t shallow_window_from_point( struct desktop *desktop, int x, int y )
806 struct window *ptr;
808 if (!desktop->top_window) return 0;
810 LIST_FOR_EACH_ENTRY( ptr, &desktop->top_window->children, struct window, entry )
812 int x_child = x, y_child = y;
814 if (!is_point_in_window( ptr, &x_child, &y_child, 0 )) continue; /* skip it */
815 return ptr->handle;
817 return desktop->top_window->handle;
820 /* return thread of top-most window containing point (in absolute coords) */
821 struct thread *window_thread_from_point( user_handle_t scope, int x, int y )
823 struct window *win = get_user_object( scope, USER_WINDOW );
825 if (!win) return NULL;
827 screen_to_client( win, &x, &y, 0 );
828 win = child_window_from_point( win, x, y );
829 if (!win->thread) return NULL;
830 return (struct thread *)grab_object( win->thread );
833 /* return list of all windows containing point (in absolute coords) */
834 static int all_windows_from_point( struct window *top, int x, int y, unsigned int dpi,
835 struct user_handle_array *array )
837 if (!is_desktop_window( top ) && !is_desktop_window( top->parent ))
839 screen_to_client( top->parent, &x, &y, dpi );
840 dpi = top->parent->dpi;
843 if (!is_point_in_window( top, &x, &y, dpi )) return 1;
844 /* if point is in client area, and window is not minimized or disabled, check children */
845 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) && point_in_rect( &top->client_rect, x, y ))
847 if (!is_desktop_window(top))
849 x -= top->client_rect.left;
850 y -= top->client_rect.top;
852 if (!get_window_children_from_point( top, x, y, array )) return 0;
854 /* now add window to the array */
855 if (!add_handle_to_array( array, top->handle )) return 0;
856 return 1;
860 /* return the thread owning a window */
861 struct thread *get_window_thread( user_handle_t handle )
863 struct window *win = get_user_object( handle, USER_WINDOW );
864 if (!win || !win->thread) return NULL;
865 return (struct thread *)grab_object( win->thread );
869 /* check if any area of a window needs repainting */
870 static inline int win_needs_repaint( struct window *win )
872 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
876 /* find a child of the specified window that needs repainting */
877 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
879 struct window *ptr, *ret = NULL;
881 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
883 if (!(ptr->style & WS_VISIBLE)) continue;
884 if (ptr->thread == thread && win_needs_repaint( ptr ))
885 ret = ptr;
886 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
887 ret = find_child_to_repaint( ptr, thread );
888 if (ret) break;
891 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
893 /* transparent window, check for non-transparent sibling to paint first */
894 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
896 if (!(ptr->style & WS_VISIBLE)) continue;
897 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
898 if (ptr->thread != thread) continue;
899 if (win_needs_repaint( ptr )) return ptr;
902 return ret;
906 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
907 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
909 struct window *ptr, *win, *top_window = get_desktop_window( thread );
911 if (!top_window) return 0;
913 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
914 else win = find_child_to_repaint( top_window, thread );
916 if (win && parent)
918 /* check that it is a child of the specified parent */
919 for (ptr = win; ptr; ptr = ptr->parent)
920 if (ptr->handle == parent) break;
921 /* otherwise don't return any window, we don't repaint a child before its parent */
922 if (!ptr) win = NULL;
924 if (!win) return 0;
925 win->paint_flags &= ~PAINT_INTERNAL;
926 return win->handle;
930 /* intersect the window region with the specified region, relative to the window parent */
931 static struct region *intersect_window_region( struct region *region, struct window *win )
933 /* make region relative to window rect */
934 offset_region( region, -win->window_rect.left, -win->window_rect.top );
935 if (!intersect_region( region, region, win->win_region )) return NULL;
936 /* make region relative to parent again */
937 offset_region( region, win->window_rect.left, win->window_rect.top );
938 return region;
942 /* convert coordinates from client to screen coords */
943 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
945 for ( ; win && !is_desktop_window(win); win = win->parent)
946 offset_rect( rect, win->client_rect.left, win->client_rect.top );
949 /* map the region from window to screen coordinates */
950 static inline void map_win_region_to_screen( struct window *win, struct region *region )
952 if (!is_desktop_window(win))
954 int x = win->window_rect.left;
955 int y = win->window_rect.top;
956 client_to_screen( win->parent, &x, &y );
957 offset_region( region, x, y );
962 /* clip all children of a given window out of the visible region */
963 static struct region *clip_children( struct window *parent, struct window *last,
964 struct region *region, int offset_x, int offset_y )
966 struct window *ptr;
967 struct region *tmp = create_empty_region();
969 if (!tmp) return NULL;
970 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
972 if (ptr == last) break;
973 if (!(ptr->style & WS_VISIBLE)) continue;
974 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
975 set_region_rect( tmp, &ptr->visible_rect );
976 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
978 free_region( tmp );
979 return NULL;
981 offset_region( tmp, offset_x, offset_y );
982 if (!(region = subtract_region( region, region, tmp ))) break;
983 if (is_region_empty( region )) break;
985 free_region( tmp );
986 return region;
990 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
991 static void set_region_client_rect( struct region *region, struct window *win )
993 rectangle_t rect;
995 intersect_rect( &rect, &win->window_rect, &win->client_rect );
996 intersect_rect( &rect, &rect, &win->surface_rect );
997 set_region_rect( region, &rect );
1001 /* set the region to the visible rect clipped by the window surface, in parent-relative coordinates */
1002 static void set_region_visible_rect( struct region *region, struct window *win )
1004 rectangle_t rect;
1006 intersect_rect( &rect, &win->visible_rect, &win->surface_rect );
1007 set_region_rect( region, &rect );
1011 /* get the top-level window to clip against for a given window */
1012 static inline struct window *get_top_clipping_window( struct window *win )
1014 while (!(win->paint_flags & PAINT_HAS_SURFACE) && win->parent && !is_desktop_window(win->parent))
1015 win = win->parent;
1016 return win;
1020 /* compute the visible region of a window, in window coordinates */
1021 static struct region *get_visible_region( struct window *win, unsigned int flags )
1023 struct region *tmp = NULL, *region;
1024 int offset_x, offset_y;
1026 if (!(region = create_empty_region())) return NULL;
1028 /* first check if all ancestors are visible */
1030 if (!is_visible( win )) return region; /* empty region */
1032 if (is_desktop_window( win ))
1034 set_region_rect( region, &win->window_rect );
1035 return region;
1038 /* create a region relative to the window itself */
1040 if ((flags & DCX_PARENTCLIP) && !is_desktop_window( win->parent ))
1042 set_region_client_rect( region, win->parent );
1043 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
1045 else if (flags & DCX_WINDOW)
1047 set_region_visible_rect( region, win );
1048 if (win->win_region && !intersect_window_region( region, win )) goto error;
1050 else
1052 set_region_client_rect( region, win );
1053 if (win->win_region && !intersect_window_region( region, win )) goto error;
1056 /* clip children */
1058 if (flags & DCX_CLIPCHILDREN)
1060 if (!clip_children( win, NULL, region, win->client_rect.left, win->client_rect.top )) goto error;
1063 /* clip siblings of ancestors */
1065 offset_x = win->window_rect.left;
1066 offset_y = win->window_rect.top;
1068 if ((tmp = create_empty_region()) != NULL)
1070 while (!is_desktop_window( win->parent ))
1072 /* we don't clip out top-level siblings as that's up to the native windowing system */
1073 if (win->style & WS_CLIPSIBLINGS)
1075 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
1076 if (is_region_empty( region )) break;
1078 /* clip to parent client area */
1079 win = win->parent;
1080 offset_x += win->client_rect.left;
1081 offset_y += win->client_rect.top;
1082 offset_region( region, win->client_rect.left, win->client_rect.top );
1083 set_region_client_rect( tmp, win );
1084 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
1085 if (!intersect_region( region, region, tmp )) goto error;
1086 if (is_region_empty( region )) break;
1088 free_region( tmp );
1090 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
1091 return region;
1093 error:
1094 if (tmp) free_region( tmp );
1095 free_region( region );
1096 return NULL;
1100 /* clip all children with a custom pixel format out of the visible region */
1101 static struct region *clip_pixel_format_children( struct window *parent, struct region *parent_clip,
1102 struct region *region, int offset_x, int offset_y )
1104 struct window *ptr;
1105 struct region *clip = create_empty_region();
1107 if (!clip) return NULL;
1109 LIST_FOR_EACH_ENTRY_REV( ptr, &parent->children, struct window, entry )
1111 if (!(ptr->style & WS_VISIBLE)) continue;
1112 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1114 /* add the visible rect */
1115 set_region_rect( clip, &ptr->visible_rect );
1116 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1117 offset_region( clip, offset_x, offset_y );
1118 if (!intersect_region( clip, clip, parent_clip )) break;
1119 if (!union_region( region, region, clip )) break;
1120 if (!(ptr->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))) continue;
1122 /* subtract the client rect if it uses a custom pixel format */
1123 set_region_rect( clip, &ptr->client_rect );
1124 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1125 offset_region( clip, offset_x, offset_y );
1126 if (!intersect_region( clip, clip, parent_clip )) break;
1127 if ((ptr->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1128 break;
1130 if (!clip_pixel_format_children( ptr, clip, region, offset_x + ptr->client_rect.left,
1131 offset_y + ptr->client_rect.top ))
1132 break;
1134 free_region( clip );
1135 return region;
1139 /* compute the visible surface region of a window, in parent coordinates */
1140 static struct region *get_surface_region( struct window *win )
1142 struct region *region, *clip;
1143 int offset_x, offset_y;
1145 /* create a region relative to the window itself */
1147 if (!(region = create_empty_region())) return NULL;
1148 if (!(clip = create_empty_region())) goto error;
1149 set_region_rect( region, &win->visible_rect );
1150 if (win->win_region && !intersect_window_region( region, win )) goto error;
1151 set_region_rect( clip, &win->client_rect );
1152 if (win->win_region && !intersect_window_region( clip, win )) goto error;
1154 if ((win->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1155 goto error;
1157 /* clip children */
1159 if (!is_desktop_window(win))
1161 offset_x = win->client_rect.left;
1162 offset_y = win->client_rect.top;
1164 else offset_x = offset_y = 0;
1166 if (!clip_pixel_format_children( win, clip, region, offset_x, offset_y )) goto error;
1168 free_region( clip );
1169 return region;
1171 error:
1172 if (clip) free_region( clip );
1173 free_region( region );
1174 return NULL;
1178 /* get the window class of a window */
1179 struct window_class* get_window_class( user_handle_t window )
1181 struct window *win;
1182 if (!(win = get_window( window ))) return NULL;
1183 if (!win->class) set_error( STATUS_ACCESS_DENIED );
1184 return win->class;
1187 /* determine the window visible rectangle, i.e. window or client rect cropped by parent rects */
1188 /* the returned rectangle is in window coordinates; return 0 if rectangle is empty */
1189 static int get_window_visible_rect( struct window *win, rectangle_t *rect, int frame )
1191 int offset_x = win->window_rect.left, offset_y = win->window_rect.top;
1193 *rect = frame ? win->window_rect : win->client_rect;
1195 if (!(win->style & WS_VISIBLE)) return 0;
1196 if (is_desktop_window( win )) return 1;
1198 while (!is_desktop_window( win->parent ))
1200 win = win->parent;
1201 if (!(win->style & WS_VISIBLE) || win->style & WS_MINIMIZE) return 0;
1202 offset_x += win->client_rect.left;
1203 offset_y += win->client_rect.top;
1204 offset_rect( rect, win->client_rect.left, win->client_rect.top );
1205 if (!intersect_rect( rect, rect, &win->client_rect )) return 0;
1206 if (!intersect_rect( rect, rect, &win->window_rect )) return 0;
1208 offset_rect( rect, -offset_x, -offset_y );
1209 return 1;
1212 /* return a copy of the specified region cropped to the window client or frame rectangle, */
1213 /* and converted from client to window coordinates. Helper for (in)validate_window. */
1214 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
1216 rectangle_t rect;
1217 struct region *tmp;
1219 if (!get_window_visible_rect( win, &rect, frame )) return NULL;
1220 if (!(tmp = create_empty_region())) return NULL;
1221 set_region_rect( tmp, &rect );
1223 if (region)
1225 /* map it to client coords */
1226 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1227 win->window_rect.top - win->client_rect.top );
1229 /* intersect specified region with bounding rect */
1230 if (!intersect_region( tmp, region, tmp )) goto done;
1231 if (is_region_empty( tmp )) goto done;
1233 /* map it back to window coords */
1234 offset_region( tmp, win->client_rect.left - win->window_rect.left,
1235 win->client_rect.top - win->window_rect.top );
1237 return tmp;
1239 done:
1240 free_region( tmp );
1241 return NULL;
1245 /* set a region as new update region for the window */
1246 static void set_update_region( struct window *win, struct region *region )
1248 if (region && !is_region_empty( region ))
1250 if (!win->update_region) inc_window_paint_count( win, 1 );
1251 else free_region( win->update_region );
1252 win->update_region = region;
1254 else
1256 if (win->update_region)
1258 inc_window_paint_count( win, -1 );
1259 free_region( win->update_region );
1261 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
1262 win->update_region = NULL;
1263 if (region) free_region( region );
1268 /* add a region to the update region; the passed region is freed or reused */
1269 static int add_update_region( struct window *win, struct region *region )
1271 if (win->update_region && !union_region( region, win->update_region, region ))
1273 free_region( region );
1274 return 0;
1276 set_update_region( win, region );
1277 return 1;
1281 /* crop the update region of children to the specified rectangle, in client coords */
1282 static void crop_children_update_region( struct window *win, rectangle_t *rect )
1284 struct window *child;
1285 struct region *tmp;
1286 rectangle_t child_rect;
1288 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1290 if (!(child->style & WS_VISIBLE)) continue;
1291 if (!rect) /* crop everything out */
1293 crop_children_update_region( child, NULL );
1294 set_update_region( child, NULL );
1295 continue;
1298 /* nothing to do if child is completely inside rect */
1299 if (child->window_rect.left >= rect->left &&
1300 child->window_rect.top >= rect->top &&
1301 child->window_rect.right <= rect->right &&
1302 child->window_rect.bottom <= rect->bottom) continue;
1304 /* map to child client coords and crop grand-children */
1305 child_rect = *rect;
1306 offset_rect( &child_rect, -child->client_rect.left, -child->client_rect.top );
1307 crop_children_update_region( child, &child_rect );
1309 /* now crop the child itself */
1310 if (!child->update_region) continue;
1311 if (!(tmp = create_empty_region())) continue;
1312 set_region_rect( tmp, rect );
1313 offset_region( tmp, -child->window_rect.left, -child->window_rect.top );
1314 if (intersect_region( tmp, child->update_region, tmp )) set_update_region( child, tmp );
1315 else free_region( tmp );
1320 /* validate the non client area of a window */
1321 static void validate_non_client( struct window *win )
1323 struct region *tmp;
1324 rectangle_t rect;
1326 if (!win->update_region) return; /* nothing to do */
1328 /* get client rect in window coords */
1329 rect.left = win->client_rect.left - win->window_rect.left;
1330 rect.top = win->client_rect.top - win->window_rect.top;
1331 rect.right = win->client_rect.right - win->window_rect.left;
1332 rect.bottom = win->client_rect.bottom - win->window_rect.top;
1334 if ((tmp = create_empty_region()))
1336 set_region_rect( tmp, &rect );
1337 if (intersect_region( tmp, win->update_region, tmp ))
1338 set_update_region( win, tmp );
1339 else
1340 free_region( tmp );
1342 win->paint_flags &= ~PAINT_NONCLIENT;
1346 /* validate a window completely so that we don't get any further paint messages for it */
1347 static void validate_whole_window( struct window *win )
1349 set_update_region( win, NULL );
1351 if (win->paint_flags & PAINT_INTERNAL)
1353 win->paint_flags &= ~PAINT_INTERNAL;
1354 inc_window_paint_count( win, -1 );
1359 /* validate a window's children so that we don't get any further paint messages for it */
1360 static void validate_children( struct window *win )
1362 struct window *child;
1364 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1366 if (!(child->style & WS_VISIBLE)) continue;
1367 validate_children(child);
1368 validate_whole_window(child);
1373 /* validate the update region of a window on all parents; helper for get_update_region */
1374 static void validate_parents( struct window *child )
1376 int offset_x = 0, offset_y = 0;
1377 struct window *win = child;
1378 struct region *tmp = NULL;
1380 if (!child->update_region) return;
1382 while (win->parent)
1384 /* map to parent client coords */
1385 offset_x += win->window_rect.left;
1386 offset_y += win->window_rect.top;
1388 win = win->parent;
1390 /* and now map to window coords */
1391 offset_x += win->client_rect.left - win->window_rect.left;
1392 offset_y += win->client_rect.top - win->window_rect.top;
1394 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1396 if (!tmp && !(tmp = create_empty_region())) return;
1397 offset_region( child->update_region, offset_x, offset_y );
1398 if (subtract_region( tmp, win->update_region, child->update_region ))
1400 set_update_region( win, tmp );
1401 tmp = NULL;
1403 /* restore child coords */
1404 offset_region( child->update_region, -offset_x, -offset_y );
1407 if (tmp) free_region( tmp );
1411 /* add/subtract a region (in client coordinates) to the update region of the window */
1412 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1414 struct region *child_rgn, *tmp;
1415 struct window *child;
1417 if (flags & RDW_INVALIDATE)
1419 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1421 if (!add_update_region( win, tmp )) return;
1423 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1424 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1426 else if (flags & RDW_VALIDATE)
1428 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1430 set_update_region( win, NULL );
1432 else if (win->update_region)
1434 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1436 if (!subtract_region( tmp, win->update_region, tmp ))
1438 free_region( tmp );
1439 return;
1441 set_update_region( win, tmp );
1443 if (flags & RDW_NOFRAME) validate_non_client( win );
1444 if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1448 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1450 win->paint_flags |= PAINT_INTERNAL;
1451 inc_window_paint_count( win, 1 );
1453 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1455 win->paint_flags &= ~PAINT_INTERNAL;
1456 inc_window_paint_count( win, -1 );
1459 /* now process children recursively */
1461 if (flags & RDW_NOCHILDREN) return;
1462 if (win->style & WS_MINIMIZE) return;
1463 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1465 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1467 /* map to client coordinates */
1468 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1469 win->window_rect.top - win->client_rect.top );
1471 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1473 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1475 if (!(child->style & WS_VISIBLE)) continue;
1476 if (!(child_rgn = create_empty_region())) continue;
1477 if (copy_region( child_rgn, tmp ))
1479 map_dpi_region( child, child_rgn, win->dpi, child->dpi );
1480 if (rect_in_region( child_rgn, &child->window_rect ))
1482 offset_region( child_rgn, -child->client_rect.left, -child->client_rect.top );
1483 redraw_window( child, child_rgn, 1, flags );
1486 free_region( child_rgn );
1489 free_region( tmp );
1493 /* retrieve the update flags for a window depending on the state of the update region */
1494 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1496 unsigned int ret = 0;
1498 if (flags & UPDATE_NONCLIENT)
1500 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1502 if (flags & UPDATE_ERASE)
1504 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1506 if (flags & UPDATE_PAINT)
1508 if (win->update_region)
1510 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1511 ret |= UPDATE_PAINT;
1514 if (flags & UPDATE_INTERNALPAINT)
1516 if (win->paint_flags & PAINT_INTERNAL)
1518 ret |= UPDATE_INTERNALPAINT;
1519 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1522 return ret;
1526 /* iterate through the children of the given window until we find one with some update flags */
1527 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1528 unsigned int flags, struct window **child )
1530 struct window *ptr;
1531 unsigned int ret = 0;
1533 /* first make sure we want to iterate children at all */
1535 if (win->style & WS_MINIMIZE) return 0;
1537 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1538 * here we only want to repaint children of windows that clip them, others
1539 * need to wait for WM_PAINT to be done in the parent first.
1541 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1543 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1545 if (from_child) /* skip all children until from_child is found */
1547 if (ptr == from_child) from_child = NULL;
1548 continue;
1550 if (!(ptr->style & WS_VISIBLE)) continue;
1551 if ((ret = get_update_flags( ptr, flags )) != 0)
1553 *child = ptr;
1554 break;
1556 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1558 return ret;
1561 /* iterate through children and siblings of the given window until we find one with some update flags */
1562 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1563 unsigned int flags, struct window **child )
1565 unsigned int ret;
1566 struct window *ptr, *from_sibling = NULL;
1568 /* if some parent is not visible start from the next sibling */
1570 if (!is_visible( win )) return 0;
1571 for (ptr = from_child; ptr; ptr = ptr->parent)
1573 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1574 if (ptr == win) break;
1577 /* non-client painting must be delayed if one of the parents is going to
1578 * be repainted and doesn't clip children */
1580 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1582 for (ptr = win->parent; ptr; ptr = ptr->parent)
1584 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1585 return 0;
1587 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1589 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1591 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1592 if (ptr == win) break;
1598 /* check window itself (only if not restarting from a child) */
1600 if (!from_child)
1602 if ((ret = get_update_flags( win, flags )))
1604 *child = win;
1605 return ret;
1607 from_child = win;
1610 /* now check children */
1612 if (flags & UPDATE_NOCHILDREN) return 0;
1613 if (!from_sibling)
1615 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1616 from_sibling = from_child;
1619 /* then check siblings and parent siblings */
1621 while (from_sibling->parent && from_sibling != win)
1623 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1624 return ret;
1625 from_sibling = from_sibling->parent;
1627 return 0;
1631 /* expose the areas revealed by a vis region change on the window parent */
1632 /* returns the region exposed on the window itself (in client coordinates) */
1633 static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
1634 struct region *old_vis_rgn )
1636 struct region *new_vis_rgn, *exposed_rgn;
1638 if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
1640 if ((exposed_rgn = create_empty_region()))
1642 if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
1644 /* make it relative to the new client area */
1645 offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
1646 win->window_rect.top - win->client_rect.top );
1648 else
1650 free_region( exposed_rgn );
1651 exposed_rgn = NULL;
1655 if (win->parent && !is_desktop_window( win->parent ))
1657 /* make it relative to the old window pos for subtracting */
1658 offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
1659 win->window_rect.top - old_window_rect->top );
1661 if ((win->parent->style & WS_CLIPCHILDREN) ?
1662 subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) :
1663 xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1665 if (!is_region_empty( new_vis_rgn ))
1667 /* make it relative to parent */
1668 offset_region( new_vis_rgn, old_window_rect->left, old_window_rect->top );
1669 redraw_window( win->parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1673 free_region( new_vis_rgn );
1674 return exposed_rgn;
1678 /* set the window and client rectangles, updating the update region if necessary */
1679 static void set_window_pos( struct window *win, struct window *previous,
1680 unsigned int swp_flags, const rectangle_t *window_rect,
1681 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1682 const rectangle_t *surface_rect, const rectangle_t *valid_rect )
1684 struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
1685 const rectangle_t old_window_rect = win->window_rect;
1686 const rectangle_t old_visible_rect = win->visible_rect;
1687 const rectangle_t old_client_rect = win->client_rect;
1688 rectangle_t rect;
1689 int client_changed, frame_changed;
1690 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1692 if (win->parent && !is_visible( win->parent )) visible = 0;
1694 if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
1696 /* set the new window info before invalidating anything */
1698 win->window_rect = *window_rect;
1699 win->visible_rect = *visible_rect;
1700 win->surface_rect = *surface_rect;
1701 win->client_rect = *client_rect;
1702 if (!(swp_flags & SWP_NOZORDER) && win->parent) link_window( win, previous );
1703 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1704 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1706 /* keep children at the same position relative to top right corner when the parent is mirrored */
1707 if (win->ex_style & WS_EX_LAYOUTRTL)
1709 struct window *child;
1710 int old_size = old_client_rect.right - old_client_rect.left;
1711 int new_size = win->client_rect.right - win->client_rect.left;
1713 if (old_size != new_size) LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1715 offset_rect( &child->window_rect, new_size - old_size, 0 );
1716 offset_rect( &child->visible_rect, new_size - old_size, 0 );
1717 offset_rect( &child->surface_rect, new_size - old_size, 0 );
1718 offset_rect( &child->client_rect, new_size - old_size, 0 );
1722 /* reset cursor clip rectangle when the desktop changes size */
1723 if (win == win->desktop->top_window) win->desktop->cursor.clip = *window_rect;
1725 /* if the window is not visible, everything is easy */
1726 if (!visible) return;
1728 /* expose anything revealed by the change */
1730 if (!(swp_flags & SWP_NOREDRAW))
1731 exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
1733 if (!(win->style & WS_VISIBLE))
1735 /* clear the update region since the window is no longer visible */
1736 validate_whole_window( win );
1737 validate_children( win );
1738 goto done;
1741 /* crop update region to the new window rect */
1743 if (win->update_region)
1745 if (get_window_visible_rect( win, &rect, 1 ))
1747 struct region *tmp = create_empty_region();
1748 if (tmp)
1750 set_region_rect( tmp, &rect );
1751 if (intersect_region( tmp, win->update_region, tmp ))
1752 set_update_region( win, tmp );
1753 else
1754 free_region( tmp );
1757 else set_update_region( win, NULL ); /* visible rect is empty */
1760 /* crop children regions to the new window rect */
1762 if (get_window_visible_rect( win, &rect, 0 ))
1764 /* map to client coords */
1765 offset_rect( &rect, win->window_rect.left - win->client_rect.left,
1766 win->window_rect.top - win->client_rect.top );
1767 crop_children_update_region( win, &rect );
1769 else crop_children_update_region( win, NULL );
1771 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1773 /* expose the whole non-client area if it changed in any way */
1775 if (swp_flags & SWP_NOCOPYBITS)
1777 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1778 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1779 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
1780 client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
1782 else
1784 /* assume the bits have been moved to follow the window rect */
1785 int x_offset = window_rect->left - old_window_rect.left;
1786 int y_offset = window_rect->top - old_window_rect.top;
1787 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1788 window_rect->right - old_window_rect.right != x_offset ||
1789 window_rect->bottom - old_window_rect.bottom != y_offset ||
1790 visible_rect->left - old_visible_rect.left != x_offset ||
1791 visible_rect->right - old_visible_rect.right != x_offset ||
1792 visible_rect->top - old_visible_rect.top != y_offset ||
1793 visible_rect->bottom - old_visible_rect.bottom != y_offset);
1794 client_changed = (client_rect->left - old_client_rect.left != x_offset ||
1795 client_rect->right - old_client_rect.right != x_offset ||
1796 client_rect->top - old_client_rect.top != y_offset ||
1797 client_rect->bottom - old_client_rect.bottom != y_offset ||
1798 memcmp( valid_rect, client_rect, sizeof(*client_rect) ));
1801 if (frame_changed || client_changed)
1803 struct region *win_rgn = old_vis_rgn; /* reuse previous region */
1805 set_region_rect( win_rgn, window_rect );
1806 if (!is_rect_empty( valid_rect ))
1808 /* subtract the valid portion of client rect from the total region */
1809 struct region *tmp = create_empty_region();
1810 if (tmp)
1812 set_region_rect( tmp, valid_rect );
1813 /* subtract update region since invalid parts of the valid rect won't be copied */
1814 if (win->update_region)
1816 offset_region( tmp, -window_rect->left, -window_rect->top );
1817 subtract_region( tmp, tmp, win->update_region );
1818 offset_region( tmp, window_rect->left, window_rect->top );
1820 if (subtract_region( tmp, win_rgn, tmp )) win_rgn = tmp;
1821 else free_region( tmp );
1824 if (!is_desktop_window(win))
1825 offset_region( win_rgn, -client_rect->left, -client_rect->top );
1826 if (exposed_rgn)
1828 union_region( exposed_rgn, exposed_rgn, win_rgn );
1829 if (win_rgn != old_vis_rgn) free_region( win_rgn );
1831 else
1833 exposed_rgn = win_rgn;
1834 if (win_rgn == old_vis_rgn) old_vis_rgn = NULL;
1838 if (exposed_rgn)
1839 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1841 done:
1842 if (old_vis_rgn) free_region( old_vis_rgn );
1843 if (exposed_rgn) free_region( exposed_rgn );
1844 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1848 /* set the window region, updating the update region if necessary */
1849 static void set_window_region( struct window *win, struct region *region, int redraw )
1851 struct region *old_vis_rgn = NULL, *exposed_rgn;
1853 /* no need to redraw if window is not visible */
1854 if (redraw && !is_visible( win )) redraw = 0;
1856 if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
1858 if (win->win_region) free_region( win->win_region );
1859 win->win_region = region;
1861 /* expose anything revealed by the change */
1862 if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
1864 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1865 free_region( exposed_rgn );
1868 if (old_vis_rgn) free_region( old_vis_rgn );
1869 clear_error(); /* we ignore out of memory errors since the region has been set */
1873 /* destroy a window */
1874 void destroy_window( struct window *win )
1876 /* hide the window */
1877 if (is_visible(win))
1879 struct region *vis_rgn = get_visible_region( win, DCX_WINDOW );
1880 win->style &= ~WS_VISIBLE;
1881 if (vis_rgn)
1883 struct region *exposed_rgn = expose_window( win, &win->window_rect, vis_rgn );
1884 if (exposed_rgn) free_region( exposed_rgn );
1885 free_region( vis_rgn );
1887 validate_whole_window( win );
1888 validate_children( win );
1891 /* destroy all children */
1892 while (!list_empty(&win->children))
1893 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
1894 while (!list_empty(&win->unlinked))
1895 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
1897 /* reset global window pointers, if the corresponding window is destroyed */
1898 if (win == shell_window) shell_window = NULL;
1899 if (win == shell_listview) shell_listview = NULL;
1900 if (win == progman_window) progman_window = NULL;
1901 if (win == taskman_window) taskman_window = NULL;
1902 free_hotkeys( win->desktop, win->handle );
1903 cleanup_clipboard_window( win->desktop, win->handle );
1904 free_user_handle( win->handle );
1905 destroy_properties( win );
1906 list_remove( &win->entry );
1907 if (is_desktop_window(win))
1909 struct desktop *desktop = win->desktop;
1910 assert( desktop->top_window == win || desktop->msg_window == win );
1911 if (desktop->top_window == win) desktop->top_window = NULL;
1912 else desktop->msg_window = NULL;
1914 else if (is_desktop_window( win->parent ))
1916 post_message( win->parent->handle, WM_PARENTNOTIFY, WM_DESTROY, win->handle );
1919 detach_window_thread( win );
1920 if (win->win_region) free_region( win->win_region );
1921 if (win->update_region) free_region( win->update_region );
1922 if (win->class) release_class( win->class );
1923 free( win->text );
1924 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
1925 free( win );
1929 /* create a window */
1930 DECL_HANDLER(create_window)
1932 struct window *win, *parent = NULL, *owner = NULL;
1933 struct unicode_str cls_name = get_req_unicode_str();
1934 atom_t atom;
1936 reply->handle = 0;
1937 if (req->parent && !(parent = get_window( req->parent ))) return;
1939 if (req->owner)
1941 if (!(owner = get_window( req->owner ))) return;
1942 if (is_desktop_window(owner)) owner = NULL;
1943 else if (parent && !is_desktop_window(parent))
1945 /* an owned window must be created as top-level */
1946 set_error( STATUS_ACCESS_DENIED );
1947 return;
1949 else /* owner must be a top-level window */
1950 while ((owner->style & (WS_POPUP|WS_CHILD)) == WS_CHILD && !is_desktop_window(owner->parent))
1951 owner = owner->parent;
1954 atom = cls_name.len ? find_global_atom( NULL, &cls_name ) : req->atom;
1956 if (!(win = create_window( parent, owner, atom, req->instance ))) return;
1958 if (parent && !is_desktop_window( parent ))
1960 win->dpi_awareness = parent->dpi_awareness;
1961 win->dpi = parent->dpi;
1963 else if (!parent || req->awareness != DPI_AWARENESS_PER_MONITOR_AWARE)
1965 win->dpi_awareness = req->awareness;
1966 win->dpi = req->dpi;
1969 reply->handle = win->handle;
1970 reply->parent = win->parent ? win->parent->handle : 0;
1971 reply->owner = win->owner;
1972 reply->extra = win->nb_extra_bytes;
1973 reply->dpi = win->dpi;
1974 reply->awareness = win->dpi_awareness;
1975 reply->class_ptr = get_class_client_ptr( win->class );
1979 /* set the parent of a window */
1980 DECL_HANDLER(set_parent)
1982 struct window *win, *parent = NULL;
1984 if (!(win = get_window( req->handle ))) return;
1985 if (req->parent && !(parent = get_window( req->parent ))) return;
1987 if (is_desktop_window(win))
1989 set_error( STATUS_INVALID_PARAMETER );
1990 return;
1992 reply->old_parent = win->parent->handle;
1993 reply->full_parent = parent ? parent->handle : 0;
1994 set_parent_window( win, parent );
1995 reply->dpi = win->dpi;
1996 reply->awareness = win->dpi_awareness;
2000 /* destroy a window */
2001 DECL_HANDLER(destroy_window)
2003 struct window *win = get_window( req->handle );
2004 if (win)
2006 if (!is_desktop_window(win)) destroy_window( win );
2007 else if (win->thread == current) detach_window_thread( win );
2008 else set_error( STATUS_ACCESS_DENIED );
2013 /* retrieve the desktop window for the current thread */
2014 DECL_HANDLER(get_desktop_window)
2016 struct desktop *desktop = get_thread_desktop( current, 0 );
2017 int force;
2019 if (!desktop) return;
2021 /* if winstation is invisible, then avoid roundtrip */
2022 force = req->force || !(desktop->winstation->flags & WSF_VISIBLE);
2024 if (!desktop->top_window && force) /* create it */
2026 if ((desktop->top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
2028 detach_window_thread( desktop->top_window );
2029 desktop->top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
2033 if (!desktop->msg_window && force) /* create it */
2035 static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
2036 static const struct unicode_str name = { messageW, sizeof(messageW) };
2037 atom_t atom = add_global_atom( NULL, &name );
2038 if (atom && (desktop->msg_window = create_window( NULL, NULL, atom, 0 )))
2040 detach_window_thread( desktop->msg_window );
2041 desktop->msg_window->style = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
2045 reply->top_window = desktop->top_window ? desktop->top_window->handle : 0;
2046 reply->msg_window = desktop->msg_window ? desktop->msg_window->handle : 0;
2047 release_object( desktop );
2051 /* set a window owner */
2052 DECL_HANDLER(set_window_owner)
2054 struct window *win = get_window( req->handle );
2055 struct window *owner = NULL, *ptr;
2057 if (!win) return;
2058 if (req->owner && !(owner = get_window( req->owner ))) return;
2059 if (is_desktop_window(win))
2061 set_error( STATUS_ACCESS_DENIED );
2062 return;
2065 /* make sure owner is not a successor of window */
2066 for (ptr = owner; ptr; ptr = ptr->owner ? get_window( ptr->owner ) : NULL)
2068 if (ptr == win)
2070 set_error( STATUS_INVALID_PARAMETER );
2071 return;
2075 reply->prev_owner = win->owner;
2076 reply->full_owner = win->owner = owner ? owner->handle : 0;
2080 /* get information from a window handle */
2081 DECL_HANDLER(get_window_info)
2083 struct window *win = get_window( req->handle );
2085 if (!win) return;
2087 reply->full_handle = win->handle;
2088 reply->last_active = win->handle;
2089 reply->is_unicode = win->is_unicode;
2090 reply->awareness = win->dpi_awareness;
2091 reply->dpi = win->dpi ? win->dpi : get_monitor_dpi( win );
2092 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
2093 if (win->thread)
2095 reply->tid = get_thread_id( win->thread );
2096 reply->pid = get_process_id( win->thread->process );
2097 reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
2102 /* set some information in a window */
2103 DECL_HANDLER(set_window_info)
2105 struct window *win = get_window( req->handle );
2107 if (!win) return;
2108 if (req->flags && is_desktop_window(win) && win->thread != current)
2110 set_error( STATUS_ACCESS_DENIED );
2111 return;
2113 if (req->extra_size > sizeof(req->extra_value) ||
2114 req->extra_offset < -1 ||
2115 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
2117 set_win32_error( ERROR_INVALID_INDEX );
2118 return;
2120 if (req->extra_offset != -1)
2122 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
2124 else if (req->flags & SET_WIN_EXTRA)
2126 set_win32_error( ERROR_INVALID_INDEX );
2127 return;
2129 reply->old_style = win->style;
2130 reply->old_ex_style = win->ex_style;
2131 reply->old_id = win->id;
2132 reply->old_instance = win->instance;
2133 reply->old_user_data = win->user_data;
2134 if (req->flags & SET_WIN_STYLE) win->style = req->style;
2135 if (req->flags & SET_WIN_EXSTYLE)
2137 /* WS_EX_TOPMOST can only be changed for unlinked windows */
2138 if (!win->is_linked) win->ex_style = req->ex_style;
2139 else win->ex_style = (req->ex_style & ~WS_EX_TOPMOST) | (win->ex_style & WS_EX_TOPMOST);
2140 if (!(win->ex_style & WS_EX_LAYERED)) win->is_layered = 0;
2142 if (req->flags & SET_WIN_ID) win->id = req->id;
2143 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
2144 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
2145 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
2146 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
2147 &req->extra_value, req->extra_size );
2149 /* changing window style triggers a non-client paint */
2150 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
2154 /* get a list of the window parents, up to the root of the tree */
2155 DECL_HANDLER(get_window_parents)
2157 struct window *ptr, *win = get_window( req->handle );
2158 int total = 0;
2159 user_handle_t *data;
2160 data_size_t len;
2162 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
2164 reply->count = total;
2165 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2166 if (len && ((data = set_reply_data_size( len ))))
2168 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
2169 *data++ = ptr->handle;
2174 /* get a list of the window children */
2175 DECL_HANDLER(get_window_children)
2177 struct window *parent = NULL;
2178 unsigned int total;
2179 user_handle_t *data;
2180 data_size_t len;
2181 struct unicode_str cls_name = get_req_unicode_str();
2182 atom_t atom = req->atom;
2183 struct desktop *desktop = NULL;
2185 if (cls_name.len && !(atom = find_global_atom( NULL, &cls_name ))) return;
2187 if (req->desktop)
2189 if (!(desktop = get_desktop_obj( current->process, req->desktop, DESKTOP_ENUMERATE ))) return;
2190 parent = desktop->top_window;
2192 else
2194 if (req->parent && !(parent = get_window( req->parent ))) return;
2195 if (!parent && !(desktop = get_thread_desktop( current, 0 ))) return;
2198 if (parent)
2199 total = get_children_windows( parent, atom, req->tid, NULL, 0 );
2200 else
2201 total = get_children_windows( desktop->top_window, atom, req->tid, NULL, 0 ) +
2202 get_children_windows( desktop->msg_window, atom, req->tid, NULL, 0 );
2204 reply->count = total;
2205 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2206 if (len && ((data = set_reply_data_size( len ))))
2208 if (parent) get_children_windows( parent, atom, req->tid, data, len / sizeof(user_handle_t) );
2209 else
2211 total = get_children_windows( desktop->top_window, atom, req->tid,
2212 data, len / sizeof(user_handle_t) );
2213 data += total;
2214 len -= total * sizeof(user_handle_t);
2215 if (len >= sizeof(user_handle_t))
2216 get_children_windows( desktop->msg_window, atom, req->tid,
2217 data, len / sizeof(user_handle_t) );
2220 if (desktop) release_object( desktop );
2224 /* get a list of the window children that contain a given point */
2225 DECL_HANDLER(get_window_children_from_point)
2227 struct user_handle_array array;
2228 struct window *parent = get_window( req->parent );
2229 data_size_t len;
2231 if (!parent) return;
2233 array.handles = NULL;
2234 array.count = 0;
2235 array.total = 0;
2236 if (!all_windows_from_point( parent, req->x, req->y, req->dpi, &array )) return;
2238 reply->count = array.count;
2239 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
2240 if (len) set_reply_data_ptr( array.handles, len );
2241 else free( array.handles );
2245 /* get window tree information from a window handle */
2246 DECL_HANDLER(get_window_tree)
2248 struct window *ptr, *win = get_window( req->handle );
2250 if (!win) return;
2252 reply->parent = 0;
2253 reply->owner = 0;
2254 reply->next_sibling = 0;
2255 reply->prev_sibling = 0;
2256 reply->first_sibling = 0;
2257 reply->last_sibling = 0;
2258 reply->first_child = 0;
2259 reply->last_child = 0;
2261 if (win->parent)
2263 struct window *parent = win->parent;
2264 reply->parent = parent->handle;
2265 reply->owner = win->owner;
2266 if (win->is_linked)
2268 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
2269 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
2271 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
2272 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
2274 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
2275 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
2279 /* set the position and Z order of a window */
2280 DECL_HANDLER(set_window_pos)
2282 rectangle_t window_rect, client_rect, visible_rect, surface_rect, valid_rect;
2283 const rectangle_t *extra_rects = get_req_data();
2284 struct window *previous = NULL;
2285 struct window *top, *win = get_window( req->handle );
2286 unsigned int flags = req->swp_flags;
2288 if (!win) return;
2289 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
2291 if (!(flags & SWP_NOZORDER))
2293 switch ((int)req->previous)
2295 case 0: /* HWND_TOP */
2296 previous = WINPTR_TOP;
2297 break;
2298 case 1: /* HWND_BOTTOM */
2299 previous = WINPTR_BOTTOM;
2300 break;
2301 case -1: /* HWND_TOPMOST */
2302 previous = WINPTR_TOPMOST;
2303 break;
2304 case -2: /* HWND_NOTOPMOST */
2305 previous = WINPTR_NOTOPMOST;
2306 break;
2307 default:
2308 if (!(previous = get_window( req->previous ))) return;
2309 /* previous must be a sibling */
2310 if (previous->parent != win->parent)
2312 set_error( STATUS_INVALID_PARAMETER );
2313 return;
2315 break;
2317 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
2320 /* windows that use UpdateLayeredWindow don't trigger repaints */
2321 if ((win->ex_style & WS_EX_LAYERED) && !win->is_layered) flags |= SWP_NOREDRAW;
2323 /* window rectangle must be ordered properly */
2324 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
2326 set_error( STATUS_INVALID_PARAMETER );
2327 return;
2330 window_rect = req->window;
2331 client_rect = req->client;
2332 if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = extra_rects[0];
2333 else visible_rect = window_rect;
2334 if (get_req_data_size() >= 2 * sizeof(rectangle_t)) surface_rect = extra_rects[1];
2335 else surface_rect = visible_rect;
2336 if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rect = extra_rects[2];
2337 else valid_rect = empty_rect;
2338 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2340 mirror_rect( &win->parent->client_rect, &window_rect );
2341 mirror_rect( &win->parent->client_rect, &visible_rect );
2342 mirror_rect( &win->parent->client_rect, &client_rect );
2343 mirror_rect( &win->parent->client_rect, &surface_rect );
2344 mirror_rect( &win->parent->client_rect, &valid_rect );
2347 win->paint_flags = (win->paint_flags & ~PAINT_CLIENT_FLAGS) | (req->paint_flags & PAINT_CLIENT_FLAGS);
2348 if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
2350 set_window_pos( win, previous, flags, &window_rect, &client_rect,
2351 &visible_rect, &surface_rect, &valid_rect );
2353 reply->new_style = win->style;
2354 reply->new_ex_style = win->ex_style;
2356 top = get_top_clipping_window( win );
2357 if (is_visible( top ) && (top->paint_flags & PAINT_HAS_SURFACE))
2359 reply->surface_win = top->handle;
2360 reply->needs_update = !!(top->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD));
2365 /* get the window and client rectangles of a window */
2366 DECL_HANDLER(get_window_rectangles)
2368 struct window *win = get_window( req->handle );
2370 if (!win) return;
2372 reply->window = win->window_rect;
2373 reply->client = win->client_rect;
2375 switch (req->relative)
2377 case COORDS_CLIENT:
2378 offset_rect( &reply->window, -win->client_rect.left, -win->client_rect.top );
2379 offset_rect( &reply->client, -win->client_rect.left, -win->client_rect.top );
2380 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &reply->window );
2381 break;
2382 case COORDS_WINDOW:
2383 offset_rect( &reply->window, -win->window_rect.left, -win->window_rect.top );
2384 offset_rect( &reply->client, -win->window_rect.left, -win->window_rect.top );
2385 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->window_rect, &reply->client );
2386 break;
2387 case COORDS_PARENT:
2388 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2390 mirror_rect( &win->parent->client_rect, &reply->window );
2391 mirror_rect( &win->parent->client_rect, &reply->client );
2393 break;
2394 case COORDS_SCREEN:
2395 client_to_screen_rect( win->parent, &reply->window );
2396 client_to_screen_rect( win->parent, &reply->client );
2397 break;
2398 default:
2399 set_error( STATUS_INVALID_PARAMETER );
2400 break;
2402 map_dpi_rect( win, &reply->window, win->dpi, req->dpi );
2403 map_dpi_rect( win, &reply->client, win->dpi, req->dpi );
2407 /* get the window text */
2408 DECL_HANDLER(get_window_text)
2410 struct window *win = get_window( req->handle );
2412 if (win && win->text_len)
2414 reply->length = win->text_len / sizeof(WCHAR);
2415 set_reply_data( win->text, min( win->text_len, get_reply_max_size() ));
2420 /* set the window text */
2421 DECL_HANDLER(set_window_text)
2423 data_size_t len;
2424 WCHAR *text = NULL;
2425 struct window *win = get_window( req->handle );
2427 if (!win) return;
2428 len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
2429 if (len && !(text = memdup( get_req_data(), len ))) return;
2430 free( win->text );
2431 win->text = text;
2432 win->text_len = len;
2436 /* get the coordinates offset between two windows */
2437 DECL_HANDLER(get_windows_offset)
2439 struct window *win;
2440 int x, y, mirror_from = 0, mirror_to = 0;
2442 reply->x = reply->y = 0;
2443 if (req->from)
2445 if (!(win = get_window( req->from ))) return;
2446 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_from = 1;
2447 x = mirror_from ? win->client_rect.right - win->client_rect.left : 0;
2448 y = 0;
2449 client_to_screen( win, &x, &y );
2450 map_dpi_point( win, &x, &y, win->dpi, req->dpi );
2451 reply->x += x;
2452 reply->y += y;
2454 if (req->to)
2456 if (!(win = get_window( req->to ))) return;
2457 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_to = 1;
2458 x = mirror_to ? win->client_rect.right - win->client_rect.left : 0;
2459 y = 0;
2460 client_to_screen( win, &x, &y );
2461 map_dpi_point( win, &x, &y, win->dpi, req->dpi );
2462 reply->x -= x;
2463 reply->y -= y;
2465 if (mirror_from) reply->x = -reply->x;
2466 reply->mirror = mirror_from ^ mirror_to;
2470 /* get the visible region of a window */
2471 DECL_HANDLER(get_visible_region)
2473 struct region *region;
2474 struct window *top, *win = get_window( req->window );
2476 if (!win) return;
2478 top = get_top_clipping_window( win );
2479 if ((region = get_visible_region( win, req->flags )))
2481 rectangle_t *data;
2482 map_win_region_to_screen( win, region );
2483 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2484 if (data) set_reply_data_ptr( data, reply->total_size );
2486 reply->top_win = top->handle;
2487 reply->top_rect = top->surface_rect;
2489 if (!is_desktop_window(win))
2491 reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
2492 client_to_screen_rect( top->parent, &reply->top_rect );
2493 client_to_screen_rect( win->parent, &reply->win_rect );
2495 else
2497 reply->win_rect.left = 0;
2498 reply->win_rect.top = 0;
2499 reply->win_rect.right = win->client_rect.right - win->client_rect.left;
2500 reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
2502 reply->paint_flags = win->paint_flags & PAINT_CLIENT_FLAGS;
2506 /* get the surface visible region of a window */
2507 DECL_HANDLER(get_surface_region)
2509 struct region *region;
2510 struct window *win = get_window( req->window );
2512 if (!win || !is_visible( win )) return;
2514 if ((region = get_surface_region( win )))
2516 rectangle_t *data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2517 if (data) set_reply_data_ptr( data, reply->total_size );
2519 reply->visible_rect = win->visible_rect;
2523 /* get the window region */
2524 DECL_HANDLER(get_window_region)
2526 rectangle_t *data;
2527 struct window *win = get_window( req->window );
2529 if (!win) return;
2530 if (!win->win_region) return;
2532 if (win->ex_style & WS_EX_LAYOUTRTL)
2534 struct region *region = create_empty_region();
2536 if (!region) return;
2537 if (!copy_region( region, win->win_region ))
2539 free_region( region );
2540 return;
2542 mirror_region( &win->window_rect, region );
2543 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2545 else data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
2547 if (data) set_reply_data_ptr( data, reply->total_size );
2551 /* set the window region */
2552 DECL_HANDLER(set_window_region)
2554 struct region *region = NULL;
2555 struct window *win = get_window( req->window );
2557 if (!win) return;
2559 if (get_req_data_size()) /* no data means remove the region completely */
2561 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2562 return;
2563 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->window_rect, region );
2565 set_window_region( win, region, req->redraw );
2569 /* get a window update region */
2570 DECL_HANDLER(get_update_region)
2572 rectangle_t *data;
2573 unsigned int flags = req->flags;
2574 struct window *from_child = NULL;
2575 struct window *win = get_window( req->window );
2577 reply->flags = 0;
2578 if (!win) return;
2580 if (req->from_child)
2582 struct window *ptr;
2584 if (!(from_child = get_window( req->from_child ))) return;
2586 /* make sure from_child is a child of win */
2587 ptr = from_child;
2588 while (ptr && ptr != win) ptr = ptr->parent;
2589 if (!ptr)
2591 set_error( STATUS_INVALID_PARAMETER );
2592 return;
2596 if (flags & UPDATE_DELAYED_ERASE) /* this means that the previous call didn't erase */
2598 if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
2599 else win->paint_flags |= PAINT_DELAYED_ERASE;
2602 reply->flags = get_window_update_flags( win, from_child, flags, &win );
2603 reply->child = win->handle;
2605 if (flags & UPDATE_NOREGION) return;
2607 if (win->update_region)
2609 /* convert update region to screen coordinates */
2610 struct region *region = create_empty_region();
2612 if (!region) return;
2613 if (!copy_region( region, win->update_region ))
2615 free_region( region );
2616 return;
2618 if ((flags & UPDATE_CLIPCHILDREN) && (win->style & WS_CLIPCHILDREN))
2619 clip_children( win, NULL, region, win->client_rect.left - win->window_rect.left,
2620 win->client_rect.top - win->window_rect.top );
2621 map_win_region_to_screen( win, region );
2622 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
2623 &reply->total_size ))) return;
2624 set_reply_data_ptr( data, reply->total_size );
2627 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
2629 validate_parents( win );
2630 validate_whole_window( win );
2632 else
2634 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
2635 if (reply->flags & UPDATE_ERASE)
2637 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
2638 /* desktop window only gets erased, not repainted */
2639 if (is_desktop_window(win)) validate_whole_window( win );
2645 /* update the z order of a window so that a given rectangle is fully visible */
2646 DECL_HANDLER(update_window_zorder)
2648 rectangle_t tmp, rect = req->rect;
2649 struct window *ptr, *win = get_window( req->window );
2651 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
2653 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2655 if (ptr == win) break;
2656 if (!(ptr->style & WS_VISIBLE)) continue;
2657 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2658 if (ptr->is_layered && (ptr->layered_flags & LWA_COLORKEY)) continue;
2659 tmp = rect;
2660 map_dpi_rect( win, &tmp, win->parent->dpi, win->dpi );
2661 if (!intersect_rect( &tmp, &tmp, &ptr->visible_rect )) continue;
2662 if (ptr->win_region)
2664 offset_rect( &tmp, -ptr->window_rect.left, -ptr->window_rect.top );
2665 if (!rect_in_region( ptr->win_region, &tmp )) continue;
2667 /* found a window obscuring the rectangle, now move win above this one */
2668 /* making sure to not violate the topmost rule */
2669 if (!(ptr->ex_style & WS_EX_TOPMOST) || (win->ex_style & WS_EX_TOPMOST))
2671 list_remove( &win->entry );
2672 list_add_before( &ptr->entry, &win->entry );
2674 break;
2679 /* mark parts of a window as needing a redraw */
2680 DECL_HANDLER(redraw_window)
2682 unsigned int flags = req->flags;
2683 struct region *region = NULL;
2684 struct window *win;
2686 if (!req->window)
2688 if (!(win = get_desktop_window( current ))) return;
2690 else
2692 if (!(win = get_window( req->window ))) return;
2693 if (is_desktop_window( win )) flags &= ~RDW_ALLCHILDREN;
2696 if (!is_visible( win )) return; /* nothing to do */
2698 if (flags & (RDW_VALIDATE|RDW_INVALIDATE))
2700 if (get_req_data_size()) /* no data means whole rectangle */
2702 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2703 return;
2704 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->client_rect, region );
2708 redraw_window( win, region, (flags & RDW_INVALIDATE) && (flags & RDW_FRAME), flags );
2709 if (region) free_region( region );
2713 /* set a window property */
2714 DECL_HANDLER(set_window_property)
2716 struct unicode_str name = get_req_unicode_str();
2717 struct window *win = get_window( req->window );
2719 if (!win) return;
2721 if (name.len)
2723 atom_t atom = add_global_atom( NULL, &name );
2724 if (atom)
2726 set_property( win, atom, req->data, PROP_TYPE_STRING );
2727 release_global_atom( NULL, atom );
2730 else set_property( win, req->atom, req->data, PROP_TYPE_ATOM );
2734 /* remove a window property */
2735 DECL_HANDLER(remove_window_property)
2737 struct unicode_str name = get_req_unicode_str();
2738 struct window *win = get_window( req->window );
2740 if (win)
2742 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2743 if (atom) reply->data = remove_property( win, atom );
2748 /* get a window property */
2749 DECL_HANDLER(get_window_property)
2751 struct unicode_str name = get_req_unicode_str();
2752 struct window *win = get_window( req->window );
2754 if (win)
2756 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2757 if (atom) reply->data = get_property( win, atom );
2762 /* get the list of properties of a window */
2763 DECL_HANDLER(get_window_properties)
2765 property_data_t *data;
2766 int i, count, max = get_reply_max_size() / sizeof(*data);
2767 struct window *win = get_window( req->window );
2769 reply->total = 0;
2770 if (!win) return;
2772 for (i = count = 0; i < win->prop_inuse; i++)
2773 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2774 reply->total = count;
2776 if (count > max) count = max;
2777 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2779 for (i = 0; i < win->prop_inuse && count; i++)
2781 if (win->properties[i].type == PROP_TYPE_FREE) continue;
2782 data->atom = win->properties[i].atom;
2783 data->string = (win->properties[i].type == PROP_TYPE_STRING);
2784 data->data = win->properties[i].data;
2785 data++;
2786 count--;
2791 /* get the new window pointer for a global window, checking permissions */
2792 /* helper for set_global_windows request */
2793 static int get_new_global_window( struct window **win, user_handle_t handle )
2795 if (!handle)
2797 *win = NULL;
2798 return 1;
2800 else if (*win)
2802 set_error( STATUS_ACCESS_DENIED );
2803 return 0;
2805 *win = get_window( handle );
2806 return (*win != NULL);
2809 /* Set/get the global windows */
2810 DECL_HANDLER(set_global_windows)
2812 struct window *new_shell_window = shell_window;
2813 struct window *new_shell_listview = shell_listview;
2814 struct window *new_progman_window = progman_window;
2815 struct window *new_taskman_window = taskman_window;
2817 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2818 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2819 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2820 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2822 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2824 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2825 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2827 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2829 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2831 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2833 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2835 shell_window = new_shell_window;
2836 shell_listview = new_shell_listview;
2837 progman_window = new_progman_window;
2838 taskman_window = new_taskman_window;
2841 /* retrieve layered info for a window */
2842 DECL_HANDLER(get_window_layered_info)
2844 struct window *win = get_window( req->handle );
2846 if (!win) return;
2848 if (win->is_layered)
2850 reply->color_key = win->color_key;
2851 reply->alpha = win->alpha;
2852 reply->flags = win->layered_flags;
2854 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2858 /* set layered info for a window */
2859 DECL_HANDLER(set_window_layered_info)
2861 struct window *win = get_window( req->handle );
2863 if (!win) return;
2865 if (win->ex_style & WS_EX_LAYERED)
2867 int was_layered = win->is_layered;
2869 if (req->flags & LWA_ALPHA) win->alpha = req->alpha;
2870 else if (!win->is_layered) win->alpha = 0; /* alpha init value is 0 */
2872 win->color_key = req->color_key;
2873 win->layered_flags = req->flags;
2874 win->is_layered = 1;
2875 /* repaint since we know now it's not going to use UpdateLayeredWindow */
2876 if (!was_layered) redraw_window( win, 0, 1, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
2878 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );