scrrun: Generate temp name with tmp suffix.
[wine.git] / server / window.c
blob7ea91d2a7dc0d9c7143ca4abf3959d8abfd05acf
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"
23 #include <assert.h>
24 #include <stdarg.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winternl.h"
34 #include "object.h"
35 #include "request.h"
36 #include "thread.h"
37 #include "process.h"
38 #include "user.h"
39 #include "unicode.h"
41 /* a window property */
42 struct property
44 unsigned short type; /* property type (see below) */
45 atom_t atom; /* property atom */
46 lparam_t data; /* property data (user-defined storage) */
49 enum property_type
51 PROP_TYPE_FREE, /* free entry */
52 PROP_TYPE_STRING, /* atom that was originally a string */
53 PROP_TYPE_ATOM /* plain atom */
57 struct window
59 struct window *parent; /* parent window */
60 user_handle_t owner; /* owner of this window */
61 struct list children; /* list of children in Z-order */
62 struct list unlinked; /* list of children not linked in the Z-order list */
63 struct list entry; /* entry in parent's children list */
64 user_handle_t handle; /* full handle for this window */
65 struct thread *thread; /* thread owning the window */
66 struct desktop *desktop; /* desktop that the window belongs to */
67 struct window_class *class; /* window class */
68 atom_t atom; /* class atom */
69 user_handle_t last_active; /* last active popup */
70 rectangle_t window_rect; /* window rectangle (relative to parent client area) */
71 rectangle_t visible_rect; /* visible part of window rect (relative to parent client area) */
72 rectangle_t surface_rect; /* window surface rectangle (relative to parent client area) */
73 rectangle_t client_rect; /* client rectangle (relative to parent client area) */
74 struct region *win_region; /* region for shaped windows (relative to window rect) */
75 struct region *update_region; /* update region (relative to window rect) */
76 unsigned int style; /* window style */
77 unsigned int ex_style; /* window extended style */
78 unsigned int id; /* window id */
79 mod_handle_t instance; /* creator instance */
80 unsigned int is_unicode : 1; /* ANSI or unicode */
81 unsigned int is_linked : 1; /* is it linked into the parent z-order list? */
82 unsigned int is_layered : 1; /* has layered info been set? */
83 unsigned int color_key; /* color key for a layered window */
84 unsigned int alpha; /* alpha value for a layered window */
85 unsigned int layered_flags; /* flags for a layered window */
86 unsigned int dpi; /* window DPI or 0 if per-monitor aware */
87 DPI_AWARENESS dpi_awareness; /* DPI awareness mode */
88 lparam_t user_data; /* user-specific data */
89 WCHAR *text; /* window caption text */
90 data_size_t text_len; /* length of window caption */
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->text_len = 0;
510 win->paint_flags = 0;
511 win->prop_inuse = 0;
512 win->prop_alloc = 0;
513 win->properties = NULL;
514 win->nb_extra_bytes = extra_bytes;
515 win->window_rect = win->visible_rect = win->surface_rect = win->client_rect = empty_rect;
516 memset( win->extra_bytes, 0, extra_bytes );
517 list_init( &win->children );
518 list_init( &win->unlinked );
520 /* if parent belongs to a different thread and the window isn't */
521 /* top-level, attach the two threads */
522 if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
524 if (!attach_thread_input( current, parent->thread )) goto failed;
526 else /* otherwise just make sure that the thread has a message queue */
528 if (!current->queue && !init_thread_queue( current )) goto failed;
531 /* put it on parent unlinked list */
532 if (parent) list_add_head( &parent->unlinked, &win->entry );
533 else
535 list_init( &win->entry );
536 if (is_desktop_class( class ))
538 assert( !desktop->top_window );
539 desktop->top_window = win;
540 set_process_default_desktop( current->process, desktop, current->desktop );
542 else
544 assert( !desktop->msg_window );
545 desktop->msg_window = win;
549 current->desktop_users++;
550 return win;
552 failed:
553 if (win)
555 if (win->handle) free_user_handle( win->handle );
556 free( win );
558 release_object( desktop );
559 release_class( class );
560 return NULL;
563 /* destroy all windows belonging to a given thread */
564 void destroy_thread_windows( struct thread *thread )
566 user_handle_t handle = 0;
567 struct window *win;
569 while ((win = next_user_handle( &handle, USER_WINDOW )))
571 if (win->thread != thread) continue;
572 if (is_desktop_window( win )) detach_window_thread( win );
573 else destroy_window( win );
577 /* get the desktop window */
578 static struct window *get_desktop_window( struct thread *thread )
580 struct window *top_window;
581 struct desktop *desktop = get_thread_desktop( thread, 0 );
583 if (!desktop) return NULL;
584 top_window = desktop->top_window;
585 release_object( desktop );
586 return top_window;
589 /* check whether child is a descendant of parent */
590 int is_child_window( user_handle_t parent, user_handle_t child )
592 struct window *child_ptr = get_user_object( child, USER_WINDOW );
593 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
595 if (!child_ptr || !parent_ptr) return 0;
596 while (child_ptr->parent)
598 if (child_ptr->parent == parent_ptr) return 1;
599 child_ptr = child_ptr->parent;
601 return 0;
604 /* check if window can be set as foreground window */
605 int is_valid_foreground_window( user_handle_t window )
607 struct window *win = get_user_object( window, USER_WINDOW );
608 return win && (win->style & (WS_POPUP|WS_CHILD)) != WS_CHILD;
611 /* make a window active if possible */
612 int make_window_active( user_handle_t window )
614 struct window *owner, *win = get_window( window );
616 if (!win) return 0;
618 /* set last active for window and its owners */
619 owner = win;
620 while (owner)
622 owner->last_active = win->handle;
623 owner = get_user_object( owner->owner, USER_WINDOW );
625 return 1;
628 /* increment (or decrement) the window paint count */
629 static inline void inc_window_paint_count( struct window *win, int incr )
631 if (win->thread) inc_queue_paint_count( win->thread, incr );
634 /* map a point between different DPI scaling levels */
635 static void map_dpi_point( struct window *win, int *x, int *y, unsigned int from, unsigned int to )
637 if (!from) from = get_monitor_dpi( win );
638 if (!to) to = get_monitor_dpi( win );
639 if (from == to) return;
640 *x = scale_dpi( *x, from, to );
641 *y = scale_dpi( *y, from, to );
644 /* map a window rectangle between different DPI scaling levels */
645 static void map_dpi_rect( struct window *win, rectangle_t *rect, unsigned int from, unsigned int to )
647 if (!from) from = get_monitor_dpi( win );
648 if (!to) to = get_monitor_dpi( win );
649 if (from == to) return;
650 scale_dpi_rect( rect, from, to );
653 /* map a region between different DPI scaling levels */
654 static void map_dpi_region( struct window *win, struct region *region, unsigned int from, unsigned int to )
656 if (!from) from = get_monitor_dpi( win );
657 if (!to) to = get_monitor_dpi( win );
658 if (from == to) return;
659 scale_region( region, from, to );
662 /* convert coordinates from client to screen coords */
663 static inline void client_to_screen( struct window *win, int *x, int *y )
665 for ( ; win && !is_desktop_window(win); win = win->parent)
667 *x += win->client_rect.left;
668 *y += win->client_rect.top;
672 /* convert coordinates from screen to client coords and dpi */
673 static void screen_to_client( struct window *win, int *x, int *y, unsigned int dpi )
675 int offset_x = 0, offset_y = 0;
677 if (is_desktop_window( win )) return;
679 client_to_screen( win, &offset_x, &offset_y );
680 map_dpi_point( win, x, y, dpi, win->dpi );
681 *x -= offset_x;
682 *y -= offset_y;
685 /* check if window and all its ancestors are visible */
686 static int is_visible( const struct window *win )
688 while (win)
690 if (!(win->style & WS_VISIBLE)) return 0;
691 win = win->parent;
692 /* if parent is minimized children are not visible */
693 if (win && (win->style & WS_MINIMIZE)) return 0;
695 return 1;
698 /* same as is_visible but takes a window handle */
699 int is_window_visible( user_handle_t window )
701 struct window *win = get_user_object( window, USER_WINDOW );
702 if (!win) return 0;
703 return is_visible( win );
706 int is_window_transparent( user_handle_t window )
708 struct window *win = get_user_object( window, USER_WINDOW );
709 if (!win) return 0;
710 return (win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT);
713 /* check if point is inside the window, and map to window dpi */
714 static int is_point_in_window( struct window *win, int *x, int *y, unsigned int dpi )
716 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
717 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
718 return 0; /* disabled child */
719 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
720 return 0; /* transparent */
721 map_dpi_point( win, x, y, dpi, win->dpi );
722 if (!point_in_rect( &win->visible_rect, *x, *y ))
723 return 0; /* not in window */
724 if (win->win_region &&
725 !point_in_region( win->win_region, *x - win->window_rect.left, *y - win->window_rect.top ))
726 return 0; /* not in window region */
727 return 1;
730 /* fill an array with the handles of the children of a specified window */
731 static unsigned int get_children_windows( struct window *parent, atom_t atom, thread_id_t tid,
732 user_handle_t *handles, unsigned int max_count )
734 struct window *ptr;
735 unsigned int count = 0;
737 if (!parent) return 0;
739 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
741 if (atom && get_class_atom(ptr->class) != atom) continue;
742 if (tid && get_thread_id(ptr->thread) != tid) continue;
743 if (handles)
745 if (count >= max_count) break;
746 handles[count] = ptr->handle;
748 count++;
750 return count;
753 /* find child of 'parent' that contains the given point (in parent-relative coords) */
754 static struct window *child_window_from_point( struct window *parent, int x, int y )
756 struct window *ptr;
758 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
760 int x_child = x, y_child = y;
762 if (!is_point_in_window( ptr, &x_child, &y_child, parent->dpi )) continue; /* skip it */
764 /* if window is minimized or disabled, return at once */
765 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
767 /* if point is not in client area, return at once */
768 if (!point_in_rect( &ptr->client_rect, x_child, y_child )) return ptr;
770 return child_window_from_point( ptr, x_child - ptr->client_rect.left,
771 y_child - ptr->client_rect.top );
773 return parent; /* not found any child */
776 /* find all children of 'parent' that contain the given point */
777 static int get_window_children_from_point( struct window *parent, int x, int y,
778 struct user_handle_array *array )
780 struct window *ptr;
782 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
784 int x_child = x, y_child = y;
786 if (!is_point_in_window( ptr, &x_child, &y_child, parent->dpi )) continue; /* skip it */
788 /* if point is in client area, and window is not minimized or disabled, check children */
789 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) && point_in_rect( &ptr->client_rect, x_child, y_child ))
791 if (!get_window_children_from_point( ptr, x_child - ptr->client_rect.left,
792 y_child - ptr->client_rect.top, array ))
793 return 0;
796 /* now add window to the array */
797 if (!add_handle_to_array( array, ptr->handle )) return 0;
799 return 1;
802 /* get handle of root of top-most window containing point */
803 user_handle_t shallow_window_from_point( struct desktop *desktop, int x, int y )
805 struct window *ptr;
807 if (!desktop->top_window) return 0;
809 LIST_FOR_EACH_ENTRY( ptr, &desktop->top_window->children, struct window, entry )
811 int x_child = x, y_child = y;
813 if (!is_point_in_window( ptr, &x_child, &y_child, 0 )) continue; /* skip it */
814 return ptr->handle;
816 return desktop->top_window->handle;
819 /* return thread of top-most window containing point (in absolute coords) */
820 struct thread *window_thread_from_point( user_handle_t scope, int x, int y )
822 struct window *win = get_user_object( scope, USER_WINDOW );
824 if (!win) return NULL;
826 screen_to_client( win, &x, &y, 0 );
827 win = child_window_from_point( win, x, y );
828 if (!win->thread) return NULL;
829 return (struct thread *)grab_object( win->thread );
832 /* return list of all windows containing point (in absolute coords) */
833 static int all_windows_from_point( struct window *top, int x, int y, unsigned int dpi,
834 struct user_handle_array *array )
836 if (!is_desktop_window( top ) && !is_desktop_window( top->parent ))
838 screen_to_client( top->parent, &x, &y, dpi );
839 dpi = top->parent->dpi;
842 if (!is_point_in_window( top, &x, &y, dpi )) return 1;
843 /* if point is in client area, and window is not minimized or disabled, check children */
844 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) && point_in_rect( &top->client_rect, x, y ))
846 if (!is_desktop_window(top))
848 x -= top->client_rect.left;
849 y -= top->client_rect.top;
851 if (!get_window_children_from_point( top, x, y, array )) return 0;
853 /* now add window to the array */
854 if (!add_handle_to_array( array, top->handle )) return 0;
855 return 1;
859 /* return the thread owning a window */
860 struct thread *get_window_thread( user_handle_t handle )
862 struct window *win = get_user_object( handle, USER_WINDOW );
863 if (!win || !win->thread) return NULL;
864 return (struct thread *)grab_object( win->thread );
868 /* check if any area of a window needs repainting */
869 static inline int win_needs_repaint( struct window *win )
871 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
875 /* find a child of the specified window that needs repainting */
876 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
878 struct window *ptr, *ret = NULL;
880 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
882 if (!(ptr->style & WS_VISIBLE)) continue;
883 if (ptr->thread == thread && win_needs_repaint( ptr ))
884 ret = ptr;
885 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
886 ret = find_child_to_repaint( ptr, thread );
887 if (ret) break;
890 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
892 /* transparent window, check for non-transparent sibling to paint first */
893 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
895 if (!(ptr->style & WS_VISIBLE)) continue;
896 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
897 if (ptr->thread != thread) continue;
898 if (win_needs_repaint( ptr )) return ptr;
901 return ret;
905 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
906 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
908 struct window *ptr, *win, *top_window = get_desktop_window( thread );
910 if (!top_window) return 0;
912 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
913 else win = find_child_to_repaint( top_window, thread );
915 if (win && parent)
917 /* check that it is a child of the specified parent */
918 for (ptr = win; ptr; ptr = ptr->parent)
919 if (ptr->handle == parent) break;
920 /* otherwise don't return any window, we don't repaint a child before its parent */
921 if (!ptr) win = NULL;
923 if (!win) return 0;
924 win->paint_flags &= ~PAINT_INTERNAL;
925 return win->handle;
929 /* intersect the window region with the specified region, relative to the window parent */
930 static struct region *intersect_window_region( struct region *region, struct window *win )
932 /* make region relative to window rect */
933 offset_region( region, -win->window_rect.left, -win->window_rect.top );
934 if (!intersect_region( region, region, win->win_region )) return NULL;
935 /* make region relative to parent again */
936 offset_region( region, win->window_rect.left, win->window_rect.top );
937 return region;
941 /* convert coordinates from client to screen coords */
942 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
944 for ( ; win && !is_desktop_window(win); win = win->parent)
945 offset_rect( rect, win->client_rect.left, win->client_rect.top );
948 /* map the region from window to screen coordinates */
949 static inline void map_win_region_to_screen( struct window *win, struct region *region )
951 if (!is_desktop_window(win))
953 int x = win->window_rect.left;
954 int y = win->window_rect.top;
955 client_to_screen( win->parent, &x, &y );
956 offset_region( region, x, y );
961 /* clip all children of a given window out of the visible region */
962 static struct region *clip_children( struct window *parent, struct window *last,
963 struct region *region, int offset_x, int offset_y )
965 struct window *ptr;
966 struct region *tmp = create_empty_region();
968 if (!tmp) return NULL;
969 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
971 if (ptr == last) break;
972 if (!(ptr->style & WS_VISIBLE)) continue;
973 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
974 set_region_rect( tmp, &ptr->visible_rect );
975 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
977 free_region( tmp );
978 return NULL;
980 offset_region( tmp, offset_x, offset_y );
981 if (!(region = subtract_region( region, region, tmp ))) break;
982 if (is_region_empty( region )) break;
984 free_region( tmp );
985 return region;
989 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
990 static void set_region_client_rect( struct region *region, struct window *win )
992 rectangle_t rect;
994 intersect_rect( &rect, &win->window_rect, &win->client_rect );
995 intersect_rect( &rect, &rect, &win->surface_rect );
996 set_region_rect( region, &rect );
1000 /* set the region to the visible rect clipped by the window surface, in parent-relative coordinates */
1001 static void set_region_visible_rect( struct region *region, struct window *win )
1003 rectangle_t rect;
1005 intersect_rect( &rect, &win->visible_rect, &win->surface_rect );
1006 set_region_rect( region, &rect );
1010 /* get the top-level window to clip against for a given window */
1011 static inline struct window *get_top_clipping_window( struct window *win )
1013 while (!(win->paint_flags & PAINT_HAS_SURFACE) && win->parent && !is_desktop_window(win->parent))
1014 win = win->parent;
1015 return win;
1019 /* compute the visible region of a window, in window coordinates */
1020 static struct region *get_visible_region( struct window *win, unsigned int flags )
1022 struct region *tmp = NULL, *region;
1023 int offset_x, offset_y;
1025 if (!(region = create_empty_region())) return NULL;
1027 /* first check if all ancestors are visible */
1029 if (!is_visible( win )) return region; /* empty region */
1031 if (is_desktop_window( win ))
1033 set_region_rect( region, &win->window_rect );
1034 return region;
1037 /* create a region relative to the window itself */
1039 if ((flags & DCX_PARENTCLIP) && !is_desktop_window( win->parent ))
1041 set_region_client_rect( region, win->parent );
1042 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
1044 else if (flags & DCX_WINDOW)
1046 set_region_visible_rect( region, win );
1047 if (win->win_region && !intersect_window_region( region, win )) goto error;
1049 else
1051 set_region_client_rect( region, win );
1052 if (win->win_region && !intersect_window_region( region, win )) goto error;
1055 /* clip children */
1057 if (flags & DCX_CLIPCHILDREN)
1059 if (!clip_children( win, NULL, region, win->client_rect.left, win->client_rect.top )) goto error;
1062 /* clip siblings of ancestors */
1064 offset_x = win->window_rect.left;
1065 offset_y = win->window_rect.top;
1067 if ((tmp = create_empty_region()) != NULL)
1069 while (!is_desktop_window( win->parent ))
1071 /* we don't clip out top-level siblings as that's up to the native windowing system */
1072 if (win->style & WS_CLIPSIBLINGS)
1074 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
1075 if (is_region_empty( region )) break;
1077 /* clip to parent client area */
1078 win = win->parent;
1079 offset_x += win->client_rect.left;
1080 offset_y += win->client_rect.top;
1081 offset_region( region, win->client_rect.left, win->client_rect.top );
1082 set_region_client_rect( tmp, win );
1083 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
1084 if (!intersect_region( region, region, tmp )) goto error;
1085 if (is_region_empty( region )) break;
1087 free_region( tmp );
1089 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
1090 return region;
1092 error:
1093 if (tmp) free_region( tmp );
1094 free_region( region );
1095 return NULL;
1099 /* clip all children with a custom pixel format out of the visible region */
1100 static struct region *clip_pixel_format_children( struct window *parent, struct region *parent_clip,
1101 struct region *region, int offset_x, int offset_y )
1103 struct window *ptr;
1104 struct region *clip = create_empty_region();
1106 if (!clip) return NULL;
1108 LIST_FOR_EACH_ENTRY_REV( ptr, &parent->children, struct window, entry )
1110 if (!(ptr->style & WS_VISIBLE)) continue;
1111 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1113 /* add the visible rect */
1114 set_region_rect( clip, &ptr->visible_rect );
1115 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1116 offset_region( clip, offset_x, offset_y );
1117 if (!intersect_region( clip, clip, parent_clip )) break;
1118 if (!union_region( region, region, clip )) break;
1119 if (!(ptr->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))) continue;
1121 /* subtract the client rect if it uses a custom pixel format */
1122 set_region_rect( clip, &ptr->client_rect );
1123 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1124 offset_region( clip, offset_x, offset_y );
1125 if (!intersect_region( clip, clip, parent_clip )) break;
1126 if ((ptr->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1127 break;
1129 if (!clip_pixel_format_children( ptr, clip, region, offset_x + ptr->client_rect.left,
1130 offset_y + ptr->client_rect.top ))
1131 break;
1133 free_region( clip );
1134 return region;
1138 /* compute the visible surface region of a window, in parent coordinates */
1139 static struct region *get_surface_region( struct window *win )
1141 struct region *region, *clip;
1142 int offset_x, offset_y;
1144 /* create a region relative to the window itself */
1146 if (!(region = create_empty_region())) return NULL;
1147 if (!(clip = create_empty_region())) goto error;
1148 set_region_rect( region, &win->visible_rect );
1149 if (win->win_region && !intersect_window_region( region, win )) goto error;
1150 set_region_rect( clip, &win->client_rect );
1151 if (win->win_region && !intersect_window_region( clip, win )) goto error;
1153 if ((win->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1154 goto error;
1156 /* clip children */
1158 if (!is_desktop_window(win))
1160 offset_x = win->client_rect.left;
1161 offset_y = win->client_rect.top;
1163 else offset_x = offset_y = 0;
1165 if (!clip_pixel_format_children( win, clip, region, offset_x, offset_y )) goto error;
1167 free_region( clip );
1168 return region;
1170 error:
1171 if (clip) free_region( clip );
1172 free_region( region );
1173 return NULL;
1177 /* get the window class of a window */
1178 struct window_class* get_window_class( user_handle_t window )
1180 struct window *win;
1181 if (!(win = get_window( window ))) return NULL;
1182 if (!win->class) set_error( STATUS_ACCESS_DENIED );
1183 return win->class;
1186 /* determine the window visible rectangle, i.e. window or client rect cropped by parent rects */
1187 /* the returned rectangle is in window coordinates; return 0 if rectangle is empty */
1188 static int get_window_visible_rect( struct window *win, rectangle_t *rect, int frame )
1190 int offset_x = win->window_rect.left, offset_y = win->window_rect.top;
1192 *rect = frame ? win->window_rect : win->client_rect;
1194 if (!(win->style & WS_VISIBLE)) return 0;
1195 if (is_desktop_window( win )) return 1;
1197 while (!is_desktop_window( win->parent ))
1199 win = win->parent;
1200 if (!(win->style & WS_VISIBLE) || win->style & WS_MINIMIZE) return 0;
1201 offset_x += win->client_rect.left;
1202 offset_y += win->client_rect.top;
1203 offset_rect( rect, win->client_rect.left, win->client_rect.top );
1204 if (!intersect_rect( rect, rect, &win->client_rect )) return 0;
1205 if (!intersect_rect( rect, rect, &win->window_rect )) return 0;
1207 offset_rect( rect, -offset_x, -offset_y );
1208 return 1;
1211 /* return a copy of the specified region cropped to the window client or frame rectangle, */
1212 /* and converted from client to window coordinates. Helper for (in)validate_window. */
1213 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
1215 rectangle_t rect;
1216 struct region *tmp;
1218 if (!get_window_visible_rect( win, &rect, frame )) return NULL;
1219 if (!(tmp = create_empty_region())) return NULL;
1220 set_region_rect( tmp, &rect );
1222 if (region)
1224 /* map it to client coords */
1225 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1226 win->window_rect.top - win->client_rect.top );
1228 /* intersect specified region with bounding rect */
1229 if (!intersect_region( tmp, region, tmp )) goto done;
1230 if (is_region_empty( tmp )) goto done;
1232 /* map it back to window coords */
1233 offset_region( tmp, win->client_rect.left - win->window_rect.left,
1234 win->client_rect.top - win->window_rect.top );
1236 return tmp;
1238 done:
1239 free_region( tmp );
1240 return NULL;
1244 /* set a region as new update region for the window */
1245 static void set_update_region( struct window *win, struct region *region )
1247 if (region && !is_region_empty( region ))
1249 if (!win->update_region) inc_window_paint_count( win, 1 );
1250 else free_region( win->update_region );
1251 win->update_region = region;
1253 else
1255 if (win->update_region)
1257 inc_window_paint_count( win, -1 );
1258 free_region( win->update_region );
1260 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
1261 win->update_region = NULL;
1262 if (region) free_region( region );
1267 /* add a region to the update region; the passed region is freed or reused */
1268 static int add_update_region( struct window *win, struct region *region )
1270 if (win->update_region && !union_region( region, win->update_region, region ))
1272 free_region( region );
1273 return 0;
1275 set_update_region( win, region );
1276 return 1;
1280 /* crop the update region of children to the specified rectangle, in client coords */
1281 static void crop_children_update_region( struct window *win, rectangle_t *rect )
1283 struct window *child;
1284 struct region *tmp;
1285 rectangle_t child_rect;
1287 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1289 if (!(child->style & WS_VISIBLE)) continue;
1290 if (!rect) /* crop everything out */
1292 crop_children_update_region( child, NULL );
1293 set_update_region( child, NULL );
1294 continue;
1297 /* nothing to do if child is completely inside rect */
1298 if (child->window_rect.left >= rect->left &&
1299 child->window_rect.top >= rect->top &&
1300 child->window_rect.right <= rect->right &&
1301 child->window_rect.bottom <= rect->bottom) continue;
1303 /* map to child client coords and crop grand-children */
1304 child_rect = *rect;
1305 offset_rect( &child_rect, -child->client_rect.left, -child->client_rect.top );
1306 crop_children_update_region( child, &child_rect );
1308 /* now crop the child itself */
1309 if (!child->update_region) continue;
1310 if (!(tmp = create_empty_region())) continue;
1311 set_region_rect( tmp, rect );
1312 offset_region( tmp, -child->window_rect.left, -child->window_rect.top );
1313 if (intersect_region( tmp, child->update_region, tmp )) set_update_region( child, tmp );
1314 else free_region( tmp );
1319 /* validate the non client area of a window */
1320 static void validate_non_client( struct window *win )
1322 struct region *tmp;
1323 rectangle_t rect;
1325 if (!win->update_region) return; /* nothing to do */
1327 /* get client rect in window coords */
1328 rect.left = win->client_rect.left - win->window_rect.left;
1329 rect.top = win->client_rect.top - win->window_rect.top;
1330 rect.right = win->client_rect.right - win->window_rect.left;
1331 rect.bottom = win->client_rect.bottom - win->window_rect.top;
1333 if ((tmp = create_empty_region()))
1335 set_region_rect( tmp, &rect );
1336 if (intersect_region( tmp, win->update_region, tmp ))
1337 set_update_region( win, tmp );
1338 else
1339 free_region( tmp );
1341 win->paint_flags &= ~PAINT_NONCLIENT;
1345 /* validate a window completely so that we don't get any further paint messages for it */
1346 static void validate_whole_window( struct window *win )
1348 set_update_region( win, NULL );
1350 if (win->paint_flags & PAINT_INTERNAL)
1352 win->paint_flags &= ~PAINT_INTERNAL;
1353 inc_window_paint_count( win, -1 );
1358 /* validate a window's children so that we don't get any further paint messages for it */
1359 static void validate_children( struct window *win )
1361 struct window *child;
1363 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1365 if (!(child->style & WS_VISIBLE)) continue;
1366 validate_children(child);
1367 validate_whole_window(child);
1372 /* validate the update region of a window on all parents; helper for get_update_region */
1373 static void validate_parents( struct window *child )
1375 int offset_x = 0, offset_y = 0;
1376 struct window *win = child;
1377 struct region *tmp = NULL;
1379 if (!child->update_region) return;
1381 while (win->parent)
1383 /* map to parent client coords */
1384 offset_x += win->window_rect.left;
1385 offset_y += win->window_rect.top;
1387 win = win->parent;
1389 /* and now map to window coords */
1390 offset_x += win->client_rect.left - win->window_rect.left;
1391 offset_y += win->client_rect.top - win->window_rect.top;
1393 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1395 if (!tmp && !(tmp = create_empty_region())) return;
1396 offset_region( child->update_region, offset_x, offset_y );
1397 if (subtract_region( tmp, win->update_region, child->update_region ))
1399 set_update_region( win, tmp );
1400 tmp = NULL;
1402 /* restore child coords */
1403 offset_region( child->update_region, -offset_x, -offset_y );
1406 if (tmp) free_region( tmp );
1410 /* add/subtract a region (in client coordinates) to the update region of the window */
1411 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1413 struct region *child_rgn, *tmp;
1414 struct window *child;
1416 if (flags & RDW_INVALIDATE)
1418 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1420 if (!add_update_region( win, tmp )) return;
1422 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1423 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1425 else if (flags & RDW_VALIDATE)
1427 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1429 set_update_region( win, NULL );
1431 else if (win->update_region)
1433 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1435 if (!subtract_region( tmp, win->update_region, tmp ))
1437 free_region( tmp );
1438 return;
1440 set_update_region( win, tmp );
1442 if (flags & RDW_NOFRAME) validate_non_client( win );
1443 if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1447 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1449 win->paint_flags |= PAINT_INTERNAL;
1450 inc_window_paint_count( win, 1 );
1452 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1454 win->paint_flags &= ~PAINT_INTERNAL;
1455 inc_window_paint_count( win, -1 );
1458 /* now process children recursively */
1460 if (flags & RDW_NOCHILDREN) return;
1461 if (win->style & WS_MINIMIZE) return;
1462 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1464 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1466 /* map to client coordinates */
1467 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1468 win->window_rect.top - win->client_rect.top );
1470 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1472 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1474 if (!(child->style & WS_VISIBLE)) continue;
1475 if (!(child_rgn = create_empty_region())) continue;
1476 if (copy_region( child_rgn, tmp ))
1478 map_dpi_region( child, child_rgn, win->dpi, child->dpi );
1479 if (rect_in_region( child_rgn, &child->window_rect ))
1481 offset_region( child_rgn, -child->client_rect.left, -child->client_rect.top );
1482 redraw_window( child, child_rgn, 1, flags );
1485 free_region( child_rgn );
1488 free_region( tmp );
1492 /* retrieve the update flags for a window depending on the state of the update region */
1493 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1495 unsigned int ret = 0;
1497 if (flags & UPDATE_NONCLIENT)
1499 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1501 if (flags & UPDATE_ERASE)
1503 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1505 if (flags & UPDATE_PAINT)
1507 if (win->update_region)
1509 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1510 ret |= UPDATE_PAINT;
1513 if (flags & UPDATE_INTERNALPAINT)
1515 if (win->paint_flags & PAINT_INTERNAL)
1517 ret |= UPDATE_INTERNALPAINT;
1518 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1521 return ret;
1525 /* iterate through the children of the given window until we find one with some update flags */
1526 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1527 unsigned int flags, struct window **child )
1529 struct window *ptr;
1530 unsigned int ret = 0;
1532 /* first make sure we want to iterate children at all */
1534 if (win->style & WS_MINIMIZE) return 0;
1536 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1537 * here we only want to repaint children of windows that clip them, others
1538 * need to wait for WM_PAINT to be done in the parent first.
1540 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1542 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1544 if (from_child) /* skip all children until from_child is found */
1546 if (ptr == from_child) from_child = NULL;
1547 continue;
1549 if (!(ptr->style & WS_VISIBLE)) continue;
1550 if ((ret = get_update_flags( ptr, flags )) != 0)
1552 *child = ptr;
1553 break;
1555 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1557 return ret;
1560 /* iterate through children and siblings of the given window until we find one with some update flags */
1561 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1562 unsigned int flags, struct window **child )
1564 unsigned int ret;
1565 struct window *ptr, *from_sibling = NULL;
1567 /* if some parent is not visible start from the next sibling */
1569 if (!is_visible( win )) return 0;
1570 for (ptr = from_child; ptr; ptr = ptr->parent)
1572 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1573 if (ptr == win) break;
1576 /* non-client painting must be delayed if one of the parents is going to
1577 * be repainted and doesn't clip children */
1579 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1581 for (ptr = win->parent; ptr; ptr = ptr->parent)
1583 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1584 return 0;
1586 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1588 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1590 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1591 if (ptr == win) break;
1597 /* check window itself (only if not restarting from a child) */
1599 if (!from_child)
1601 if ((ret = get_update_flags( win, flags )))
1603 *child = win;
1604 return ret;
1606 from_child = win;
1609 /* now check children */
1611 if (flags & UPDATE_NOCHILDREN) return 0;
1612 if (!from_sibling)
1614 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1615 from_sibling = from_child;
1618 /* then check siblings and parent siblings */
1620 while (from_sibling->parent && from_sibling != win)
1622 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1623 return ret;
1624 from_sibling = from_sibling->parent;
1626 return 0;
1630 /* expose the areas revealed by a vis region change on the window parent */
1631 /* returns the region exposed on the window itself (in client coordinates) */
1632 static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
1633 struct region *old_vis_rgn )
1635 struct region *new_vis_rgn, *exposed_rgn;
1637 if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
1639 if ((exposed_rgn = create_empty_region()))
1641 if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
1643 /* make it relative to the new client area */
1644 offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
1645 win->window_rect.top - win->client_rect.top );
1647 else
1649 free_region( exposed_rgn );
1650 exposed_rgn = NULL;
1654 if (win->parent && !is_desktop_window( win->parent ))
1656 /* make it relative to the old window pos for subtracting */
1657 offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
1658 win->window_rect.top - old_window_rect->top );
1660 if ((win->parent->style & WS_CLIPCHILDREN) ?
1661 subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) :
1662 xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1664 if (!is_region_empty( new_vis_rgn ))
1666 /* make it relative to parent */
1667 offset_region( new_vis_rgn, old_window_rect->left, old_window_rect->top );
1668 redraw_window( win->parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1672 free_region( new_vis_rgn );
1673 return exposed_rgn;
1677 /* set the window and client rectangles, updating the update region if necessary */
1678 static void set_window_pos( struct window *win, struct window *previous,
1679 unsigned int swp_flags, const rectangle_t *window_rect,
1680 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1681 const rectangle_t *surface_rect, const rectangle_t *valid_rect )
1683 struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
1684 const rectangle_t old_window_rect = win->window_rect;
1685 const rectangle_t old_visible_rect = win->visible_rect;
1686 const rectangle_t old_client_rect = win->client_rect;
1687 rectangle_t rect;
1688 int client_changed, frame_changed;
1689 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1691 if (win->parent && !is_visible( win->parent )) visible = 0;
1693 if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
1695 /* set the new window info before invalidating anything */
1697 win->window_rect = *window_rect;
1698 win->visible_rect = *visible_rect;
1699 win->surface_rect = *surface_rect;
1700 win->client_rect = *client_rect;
1701 if (!(swp_flags & SWP_NOZORDER) && win->parent) link_window( win, previous );
1702 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1703 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1705 /* keep children at the same position relative to top right corner when the parent is mirrored */
1706 if (win->ex_style & WS_EX_LAYOUTRTL)
1708 struct window *child;
1709 int old_size = old_client_rect.right - old_client_rect.left;
1710 int new_size = win->client_rect.right - win->client_rect.left;
1712 if (old_size != new_size) LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1714 offset_rect( &child->window_rect, new_size - old_size, 0 );
1715 offset_rect( &child->visible_rect, new_size - old_size, 0 );
1716 offset_rect( &child->surface_rect, new_size - old_size, 0 );
1717 offset_rect( &child->client_rect, new_size - old_size, 0 );
1721 /* reset cursor clip rectangle when the desktop changes size */
1722 if (win == win->desktop->top_window) win->desktop->cursor.clip = *window_rect;
1724 /* if the window is not visible, everything is easy */
1725 if (!visible) return;
1727 /* expose anything revealed by the change */
1729 if (!(swp_flags & SWP_NOREDRAW))
1730 exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
1732 if (!(win->style & WS_VISIBLE))
1734 /* clear the update region since the window is no longer visible */
1735 validate_whole_window( win );
1736 validate_children( win );
1737 goto done;
1740 /* crop update region to the new window rect */
1742 if (win->update_region)
1744 if (get_window_visible_rect( win, &rect, 1 ))
1746 struct region *tmp = create_empty_region();
1747 if (tmp)
1749 set_region_rect( tmp, &rect );
1750 if (intersect_region( tmp, win->update_region, tmp ))
1751 set_update_region( win, tmp );
1752 else
1753 free_region( tmp );
1756 else set_update_region( win, NULL ); /* visible rect is empty */
1759 /* crop children regions to the new window rect */
1761 if (get_window_visible_rect( win, &rect, 0 ))
1763 /* map to client coords */
1764 offset_rect( &rect, win->window_rect.left - win->client_rect.left,
1765 win->window_rect.top - win->client_rect.top );
1766 crop_children_update_region( win, &rect );
1768 else crop_children_update_region( win, NULL );
1770 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1772 /* expose the whole non-client area if it changed in any way */
1774 if (swp_flags & SWP_NOCOPYBITS)
1776 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1777 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1778 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
1779 client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
1781 else
1783 /* assume the bits have been moved to follow the window rect */
1784 int x_offset = window_rect->left - old_window_rect.left;
1785 int y_offset = window_rect->top - old_window_rect.top;
1786 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1787 window_rect->right - old_window_rect.right != x_offset ||
1788 window_rect->bottom - old_window_rect.bottom != y_offset ||
1789 visible_rect->left - old_visible_rect.left != x_offset ||
1790 visible_rect->right - old_visible_rect.right != x_offset ||
1791 visible_rect->top - old_visible_rect.top != y_offset ||
1792 visible_rect->bottom - old_visible_rect.bottom != y_offset);
1793 client_changed = (client_rect->left - old_client_rect.left != x_offset ||
1794 client_rect->right - old_client_rect.right != x_offset ||
1795 client_rect->top - old_client_rect.top != y_offset ||
1796 client_rect->bottom - old_client_rect.bottom != y_offset ||
1797 memcmp( valid_rect, client_rect, sizeof(*client_rect) ));
1800 if (frame_changed || client_changed)
1802 struct region *win_rgn = old_vis_rgn; /* reuse previous region */
1804 set_region_rect( win_rgn, window_rect );
1805 if (!is_rect_empty( valid_rect ))
1807 /* subtract the valid portion of client rect from the total region */
1808 struct region *tmp = create_empty_region();
1809 if (tmp)
1811 set_region_rect( tmp, valid_rect );
1812 /* subtract update region since invalid parts of the valid rect won't be copied */
1813 if (win->update_region)
1815 offset_region( tmp, -window_rect->left, -window_rect->top );
1816 subtract_region( tmp, tmp, win->update_region );
1817 offset_region( tmp, window_rect->left, window_rect->top );
1819 if (subtract_region( tmp, win_rgn, tmp )) win_rgn = tmp;
1820 else free_region( tmp );
1823 if (!is_desktop_window(win))
1824 offset_region( win_rgn, -client_rect->left, -client_rect->top );
1825 if (exposed_rgn)
1827 union_region( exposed_rgn, exposed_rgn, win_rgn );
1828 if (win_rgn != old_vis_rgn) free_region( win_rgn );
1830 else
1832 exposed_rgn = win_rgn;
1833 if (win_rgn == old_vis_rgn) old_vis_rgn = NULL;
1837 if (exposed_rgn)
1838 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1840 done:
1841 if (old_vis_rgn) free_region( old_vis_rgn );
1842 if (exposed_rgn) free_region( exposed_rgn );
1843 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1847 /* set the window region, updating the update region if necessary */
1848 static void set_window_region( struct window *win, struct region *region, int redraw )
1850 struct region *old_vis_rgn = NULL, *exposed_rgn;
1852 /* no need to redraw if window is not visible */
1853 if (redraw && !is_visible( win )) redraw = 0;
1855 if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
1857 if (win->win_region) free_region( win->win_region );
1858 win->win_region = region;
1860 /* expose anything revealed by the change */
1861 if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
1863 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1864 free_region( exposed_rgn );
1867 if (old_vis_rgn) free_region( old_vis_rgn );
1868 clear_error(); /* we ignore out of memory errors since the region has been set */
1872 /* destroy a window */
1873 void destroy_window( struct window *win )
1875 /* hide the window */
1876 if (is_visible(win))
1878 struct region *vis_rgn = get_visible_region( win, DCX_WINDOW );
1879 win->style &= ~WS_VISIBLE;
1880 if (vis_rgn)
1882 struct region *exposed_rgn = expose_window( win, &win->window_rect, vis_rgn );
1883 if (exposed_rgn) free_region( exposed_rgn );
1884 free_region( vis_rgn );
1886 validate_whole_window( win );
1887 validate_children( win );
1890 /* destroy all children */
1891 while (!list_empty(&win->children))
1892 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
1893 while (!list_empty(&win->unlinked))
1894 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
1896 /* reset global window pointers, if the corresponding window is destroyed */
1897 if (win == shell_window) shell_window = NULL;
1898 if (win == shell_listview) shell_listview = NULL;
1899 if (win == progman_window) progman_window = NULL;
1900 if (win == taskman_window) taskman_window = NULL;
1901 free_hotkeys( win->desktop, win->handle );
1902 cleanup_clipboard_window( win->desktop, win->handle );
1903 free_user_handle( win->handle );
1904 destroy_properties( win );
1905 list_remove( &win->entry );
1906 if (is_desktop_window(win))
1908 struct desktop *desktop = win->desktop;
1909 assert( desktop->top_window == win || desktop->msg_window == win );
1910 if (desktop->top_window == win) desktop->top_window = NULL;
1911 else desktop->msg_window = NULL;
1913 else if (is_desktop_window( win->parent ))
1915 post_message( win->parent->handle, WM_PARENTNOTIFY, WM_DESTROY, win->handle );
1918 detach_window_thread( win );
1919 if (win->win_region) free_region( win->win_region );
1920 if (win->update_region) free_region( win->update_region );
1921 if (win->class) release_class( win->class );
1922 free( win->text );
1923 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
1924 free( win );
1928 /* create a window */
1929 DECL_HANDLER(create_window)
1931 struct window *win, *parent = NULL, *owner = NULL;
1932 struct unicode_str cls_name = get_req_unicode_str();
1933 atom_t atom;
1935 reply->handle = 0;
1936 if (req->parent && !(parent = get_window( req->parent ))) return;
1938 if (req->owner)
1940 if (!(owner = get_window( req->owner ))) return;
1941 if (is_desktop_window(owner)) owner = NULL;
1942 else if (parent && !is_desktop_window(parent))
1944 /* an owned window must be created as top-level */
1945 set_error( STATUS_ACCESS_DENIED );
1946 return;
1948 else /* owner must be a top-level window */
1949 while ((owner->style & (WS_POPUP|WS_CHILD)) == WS_CHILD && !is_desktop_window(owner->parent))
1950 owner = owner->parent;
1953 atom = cls_name.len ? find_global_atom( NULL, &cls_name ) : req->atom;
1955 if (!(win = create_window( parent, owner, atom, req->instance ))) return;
1957 if (parent && !is_desktop_window( parent ))
1959 win->dpi_awareness = parent->dpi_awareness;
1960 win->dpi = parent->dpi;
1962 else if (!parent || req->awareness != DPI_AWARENESS_PER_MONITOR_AWARE)
1964 win->dpi_awareness = req->awareness;
1965 win->dpi = req->dpi;
1967 win->style = req->style;
1968 win->ex_style = req->ex_style;
1970 reply->handle = win->handle;
1971 reply->parent = win->parent ? win->parent->handle : 0;
1972 reply->owner = win->owner;
1973 reply->extra = win->nb_extra_bytes;
1974 reply->dpi = win->dpi;
1975 reply->awareness = win->dpi_awareness;
1976 reply->class_ptr = get_class_client_ptr( win->class );
1980 /* set the parent of a window */
1981 DECL_HANDLER(set_parent)
1983 struct window *win, *parent = NULL;
1985 if (!(win = get_window( req->handle ))) return;
1986 if (req->parent && !(parent = get_window( req->parent ))) return;
1988 if (is_desktop_window(win))
1990 set_error( STATUS_INVALID_PARAMETER );
1991 return;
1993 reply->old_parent = win->parent->handle;
1994 reply->full_parent = parent ? parent->handle : 0;
1995 set_parent_window( win, parent );
1996 reply->dpi = win->dpi;
1997 reply->awareness = win->dpi_awareness;
2001 /* destroy a window */
2002 DECL_HANDLER(destroy_window)
2004 struct window *win = get_window( req->handle );
2005 if (win)
2007 if (!is_desktop_window(win)) destroy_window( win );
2008 else if (win->thread == current) detach_window_thread( win );
2009 else set_error( STATUS_ACCESS_DENIED );
2014 /* retrieve the desktop window for the current thread */
2015 DECL_HANDLER(get_desktop_window)
2017 struct desktop *desktop = get_thread_desktop( current, 0 );
2018 int force;
2020 if (!desktop) return;
2022 /* if winstation is invisible, then avoid roundtrip */
2023 force = req->force || !(desktop->winstation->flags & WSF_VISIBLE);
2025 if (!desktop->top_window && force) /* create it */
2027 if ((desktop->top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
2029 detach_window_thread( desktop->top_window );
2030 desktop->top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
2034 if (!desktop->msg_window && force) /* create it */
2036 static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
2037 static const struct unicode_str name = { messageW, sizeof(messageW) };
2038 atom_t atom = add_global_atom( NULL, &name );
2039 if (atom && (desktop->msg_window = create_window( NULL, NULL, atom, 0 )))
2041 detach_window_thread( desktop->msg_window );
2042 desktop->msg_window->style = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
2046 reply->top_window = desktop->top_window ? desktop->top_window->handle : 0;
2047 reply->msg_window = desktop->msg_window ? desktop->msg_window->handle : 0;
2048 release_object( desktop );
2052 /* set a window owner */
2053 DECL_HANDLER(set_window_owner)
2055 struct window *win = get_window( req->handle );
2056 struct window *owner = NULL, *ptr;
2058 if (!win) return;
2059 if (req->owner && !(owner = get_window( req->owner ))) return;
2060 if (is_desktop_window(win))
2062 set_error( STATUS_ACCESS_DENIED );
2063 return;
2066 /* make sure owner is not a successor of window */
2067 for (ptr = owner; ptr; ptr = ptr->owner ? get_window( ptr->owner ) : NULL)
2069 if (ptr == win)
2071 set_error( STATUS_INVALID_PARAMETER );
2072 return;
2076 reply->prev_owner = win->owner;
2077 reply->full_owner = win->owner = owner ? owner->handle : 0;
2081 /* get information from a window handle */
2082 DECL_HANDLER(get_window_info)
2084 struct window *win = get_window( req->handle );
2086 if (!win) return;
2088 reply->full_handle = win->handle;
2089 reply->last_active = win->handle;
2090 reply->is_unicode = win->is_unicode;
2091 reply->awareness = win->dpi_awareness;
2092 reply->dpi = win->dpi ? win->dpi : get_monitor_dpi( win );
2093 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
2094 if (win->thread)
2096 reply->tid = get_thread_id( win->thread );
2097 reply->pid = get_process_id( win->thread->process );
2098 reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
2103 /* set some information in a window */
2104 DECL_HANDLER(set_window_info)
2106 struct window *win = get_window( req->handle );
2108 if (!win) return;
2109 if (req->flags && is_desktop_window(win) && win->thread != current)
2111 set_error( STATUS_ACCESS_DENIED );
2112 return;
2114 if (req->extra_size > sizeof(req->extra_value) ||
2115 req->extra_offset < -1 ||
2116 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
2118 set_win32_error( ERROR_INVALID_INDEX );
2119 return;
2121 if (req->extra_offset != -1)
2123 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
2125 else if (req->flags & SET_WIN_EXTRA)
2127 set_win32_error( ERROR_INVALID_INDEX );
2128 return;
2130 reply->old_style = win->style;
2131 reply->old_ex_style = win->ex_style;
2132 reply->old_id = win->id;
2133 reply->old_instance = win->instance;
2134 reply->old_user_data = win->user_data;
2135 if (req->flags & SET_WIN_STYLE) win->style = req->style;
2136 if (req->flags & SET_WIN_EXSTYLE)
2138 /* WS_EX_TOPMOST can only be changed for unlinked windows */
2139 if (!win->is_linked) win->ex_style = req->ex_style;
2140 else win->ex_style = (req->ex_style & ~WS_EX_TOPMOST) | (win->ex_style & WS_EX_TOPMOST);
2141 if (!(win->ex_style & WS_EX_LAYERED)) win->is_layered = 0;
2143 if (req->flags & SET_WIN_ID) win->id = req->id;
2144 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
2145 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
2146 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
2147 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
2148 &req->extra_value, req->extra_size );
2150 /* changing window style triggers a non-client paint */
2151 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
2155 /* get a list of the window parents, up to the root of the tree */
2156 DECL_HANDLER(get_window_parents)
2158 struct window *ptr, *win = get_window( req->handle );
2159 int total = 0;
2160 user_handle_t *data;
2161 data_size_t len;
2163 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
2165 reply->count = total;
2166 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2167 if (len && ((data = set_reply_data_size( len ))))
2169 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
2170 *data++ = ptr->handle;
2175 /* get a list of the window children */
2176 DECL_HANDLER(get_window_children)
2178 struct window *parent = NULL;
2179 unsigned int total;
2180 user_handle_t *data;
2181 data_size_t len;
2182 struct unicode_str cls_name = get_req_unicode_str();
2183 atom_t atom = req->atom;
2184 struct desktop *desktop = NULL;
2186 if (cls_name.len && !(atom = find_global_atom( NULL, &cls_name ))) return;
2188 if (req->desktop)
2190 if (!(desktop = get_desktop_obj( current->process, req->desktop, DESKTOP_ENUMERATE ))) return;
2191 parent = desktop->top_window;
2193 else
2195 if (req->parent && !(parent = get_window( req->parent ))) return;
2196 if (!parent && !(desktop = get_thread_desktop( current, 0 ))) return;
2199 if (parent)
2200 total = get_children_windows( parent, atom, req->tid, NULL, 0 );
2201 else
2202 total = get_children_windows( desktop->top_window, atom, req->tid, NULL, 0 ) +
2203 get_children_windows( desktop->msg_window, atom, req->tid, NULL, 0 );
2205 reply->count = total;
2206 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2207 if (len && ((data = set_reply_data_size( len ))))
2209 if (parent) get_children_windows( parent, atom, req->tid, data, len / sizeof(user_handle_t) );
2210 else
2212 total = get_children_windows( desktop->top_window, atom, req->tid,
2213 data, len / sizeof(user_handle_t) );
2214 data += total;
2215 len -= total * sizeof(user_handle_t);
2216 if (len >= sizeof(user_handle_t))
2217 get_children_windows( desktop->msg_window, atom, req->tid,
2218 data, len / sizeof(user_handle_t) );
2221 if (desktop) release_object( desktop );
2225 /* get a list of the window children that contain a given point */
2226 DECL_HANDLER(get_window_children_from_point)
2228 struct user_handle_array array;
2229 struct window *parent = get_window( req->parent );
2230 data_size_t len;
2232 if (!parent) return;
2234 array.handles = NULL;
2235 array.count = 0;
2236 array.total = 0;
2237 if (!all_windows_from_point( parent, req->x, req->y, req->dpi, &array )) return;
2239 reply->count = array.count;
2240 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
2241 if (len) set_reply_data_ptr( array.handles, len );
2242 else free( array.handles );
2246 /* get window tree information from a window handle */
2247 DECL_HANDLER(get_window_tree)
2249 struct window *ptr, *win = get_window( req->handle );
2251 if (!win) return;
2253 reply->parent = 0;
2254 reply->owner = 0;
2255 reply->next_sibling = 0;
2256 reply->prev_sibling = 0;
2257 reply->first_sibling = 0;
2258 reply->last_sibling = 0;
2259 reply->first_child = 0;
2260 reply->last_child = 0;
2262 if (win->parent)
2264 struct window *parent = win->parent;
2265 reply->parent = parent->handle;
2266 reply->owner = win->owner;
2267 if (win->is_linked)
2269 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
2270 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
2272 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
2273 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
2275 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
2276 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
2280 /* set the position and Z order of a window */
2281 DECL_HANDLER(set_window_pos)
2283 rectangle_t window_rect, client_rect, visible_rect, surface_rect, valid_rect;
2284 const rectangle_t *extra_rects = get_req_data();
2285 struct window *previous = NULL;
2286 struct window *top, *win = get_window( req->handle );
2287 unsigned int flags = req->swp_flags;
2289 if (!win) return;
2290 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
2292 if (!(flags & SWP_NOZORDER))
2294 switch ((int)req->previous)
2296 case 0: /* HWND_TOP */
2297 previous = WINPTR_TOP;
2298 break;
2299 case 1: /* HWND_BOTTOM */
2300 previous = WINPTR_BOTTOM;
2301 break;
2302 case -1: /* HWND_TOPMOST */
2303 previous = WINPTR_TOPMOST;
2304 break;
2305 case -2: /* HWND_NOTOPMOST */
2306 previous = WINPTR_NOTOPMOST;
2307 break;
2308 default:
2309 if (!(previous = get_window( req->previous ))) return;
2310 /* previous must be a sibling */
2311 if (previous->parent != win->parent)
2313 set_error( STATUS_INVALID_PARAMETER );
2314 return;
2316 break;
2318 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
2321 /* windows that use UpdateLayeredWindow don't trigger repaints */
2322 if ((win->ex_style & WS_EX_LAYERED) && !win->is_layered) flags |= SWP_NOREDRAW;
2324 /* window rectangle must be ordered properly */
2325 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
2327 set_error( STATUS_INVALID_PARAMETER );
2328 return;
2331 window_rect = req->window;
2332 client_rect = req->client;
2333 if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = extra_rects[0];
2334 else visible_rect = window_rect;
2335 if (get_req_data_size() >= 2 * sizeof(rectangle_t)) surface_rect = extra_rects[1];
2336 else surface_rect = visible_rect;
2337 if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rect = extra_rects[2];
2338 else valid_rect = empty_rect;
2339 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2341 mirror_rect( &win->parent->client_rect, &window_rect );
2342 mirror_rect( &win->parent->client_rect, &visible_rect );
2343 mirror_rect( &win->parent->client_rect, &client_rect );
2344 mirror_rect( &win->parent->client_rect, &surface_rect );
2345 mirror_rect( &win->parent->client_rect, &valid_rect );
2348 win->paint_flags = (win->paint_flags & ~PAINT_CLIENT_FLAGS) | (req->paint_flags & PAINT_CLIENT_FLAGS);
2349 if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
2351 set_window_pos( win, previous, flags, &window_rect, &client_rect,
2352 &visible_rect, &surface_rect, &valid_rect );
2354 reply->new_style = win->style;
2355 reply->new_ex_style = win->ex_style;
2357 top = get_top_clipping_window( win );
2358 if (is_visible( top ) && (top->paint_flags & PAINT_HAS_SURFACE))
2360 reply->surface_win = top->handle;
2361 reply->needs_update = !!(top->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD));
2366 /* get the window and client rectangles of a window */
2367 DECL_HANDLER(get_window_rectangles)
2369 struct window *win = get_window( req->handle );
2371 if (!win) return;
2373 reply->window = win->window_rect;
2374 reply->client = win->client_rect;
2376 switch (req->relative)
2378 case COORDS_CLIENT:
2379 offset_rect( &reply->window, -win->client_rect.left, -win->client_rect.top );
2380 offset_rect( &reply->client, -win->client_rect.left, -win->client_rect.top );
2381 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &reply->window );
2382 break;
2383 case COORDS_WINDOW:
2384 offset_rect( &reply->window, -win->window_rect.left, -win->window_rect.top );
2385 offset_rect( &reply->client, -win->window_rect.left, -win->window_rect.top );
2386 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->window_rect, &reply->client );
2387 break;
2388 case COORDS_PARENT:
2389 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2391 mirror_rect( &win->parent->client_rect, &reply->window );
2392 mirror_rect( &win->parent->client_rect, &reply->client );
2394 break;
2395 case COORDS_SCREEN:
2396 client_to_screen_rect( win->parent, &reply->window );
2397 client_to_screen_rect( win->parent, &reply->client );
2398 break;
2399 default:
2400 set_error( STATUS_INVALID_PARAMETER );
2401 break;
2403 map_dpi_rect( win, &reply->window, win->dpi, req->dpi );
2404 map_dpi_rect( win, &reply->client, win->dpi, req->dpi );
2408 /* get the window text */
2409 DECL_HANDLER(get_window_text)
2411 struct window *win = get_window( req->handle );
2413 if (win && win->text_len)
2415 reply->length = win->text_len / sizeof(WCHAR);
2416 set_reply_data( win->text, min( win->text_len, get_reply_max_size() ));
2421 /* set the window text */
2422 DECL_HANDLER(set_window_text)
2424 data_size_t len;
2425 WCHAR *text = NULL;
2426 struct window *win = get_window( req->handle );
2428 if (!win) return;
2429 len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
2430 if (len && !(text = memdup( get_req_data(), len ))) return;
2431 free( win->text );
2432 win->text = text;
2433 win->text_len = len;
2437 /* get the coordinates offset between two windows */
2438 DECL_HANDLER(get_windows_offset)
2440 struct window *win;
2441 int x, y, mirror_from = 0, mirror_to = 0;
2443 reply->x = reply->y = 0;
2444 if (req->from)
2446 if (!(win = get_window( req->from ))) return;
2447 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_from = 1;
2448 x = mirror_from ? win->client_rect.right - win->client_rect.left : 0;
2449 y = 0;
2450 client_to_screen( win, &x, &y );
2451 map_dpi_point( win, &x, &y, win->dpi, req->dpi );
2452 reply->x += x;
2453 reply->y += y;
2455 if (req->to)
2457 if (!(win = get_window( req->to ))) return;
2458 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_to = 1;
2459 x = mirror_to ? win->client_rect.right - win->client_rect.left : 0;
2460 y = 0;
2461 client_to_screen( win, &x, &y );
2462 map_dpi_point( win, &x, &y, win->dpi, req->dpi );
2463 reply->x -= x;
2464 reply->y -= y;
2466 if (mirror_from) reply->x = -reply->x;
2467 reply->mirror = mirror_from ^ mirror_to;
2471 /* get the visible region of a window */
2472 DECL_HANDLER(get_visible_region)
2474 struct region *region;
2475 struct window *top, *win = get_window( req->window );
2477 if (!win) return;
2479 top = get_top_clipping_window( win );
2480 if ((region = get_visible_region( win, req->flags )))
2482 rectangle_t *data;
2483 map_win_region_to_screen( win, region );
2484 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2485 if (data) set_reply_data_ptr( data, reply->total_size );
2487 reply->top_win = top->handle;
2488 reply->top_rect = top->surface_rect;
2490 if (!is_desktop_window(win))
2492 reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
2493 client_to_screen_rect( top->parent, &reply->top_rect );
2494 client_to_screen_rect( win->parent, &reply->win_rect );
2496 else
2498 reply->win_rect.left = 0;
2499 reply->win_rect.top = 0;
2500 reply->win_rect.right = win->client_rect.right - win->client_rect.left;
2501 reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
2503 reply->paint_flags = win->paint_flags & PAINT_CLIENT_FLAGS;
2507 /* get the surface visible region of a window */
2508 DECL_HANDLER(get_surface_region)
2510 struct region *region;
2511 struct window *win = get_window( req->window );
2513 if (!win || !is_visible( win )) return;
2515 if ((region = get_surface_region( win )))
2517 rectangle_t *data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2518 if (data) set_reply_data_ptr( data, reply->total_size );
2520 reply->visible_rect = win->visible_rect;
2524 /* get the window region */
2525 DECL_HANDLER(get_window_region)
2527 rectangle_t *data;
2528 struct window *win = get_window( req->window );
2530 if (!win) return;
2531 if (!win->win_region) return;
2533 if (win->ex_style & WS_EX_LAYOUTRTL)
2535 struct region *region = create_empty_region();
2537 if (!region) return;
2538 if (!copy_region( region, win->win_region ))
2540 free_region( region );
2541 return;
2543 mirror_region( &win->window_rect, region );
2544 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2546 else data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
2548 if (data) set_reply_data_ptr( data, reply->total_size );
2552 /* set the window region */
2553 DECL_HANDLER(set_window_region)
2555 struct region *region = NULL;
2556 struct window *win = get_window( req->window );
2558 if (!win) return;
2560 if (get_req_data_size()) /* no data means remove the region completely */
2562 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2563 return;
2564 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->window_rect, region );
2566 set_window_region( win, region, req->redraw );
2570 /* get a window update region */
2571 DECL_HANDLER(get_update_region)
2573 rectangle_t *data;
2574 unsigned int flags = req->flags;
2575 struct window *from_child = NULL;
2576 struct window *win = get_window( req->window );
2578 reply->flags = 0;
2579 if (!win) return;
2581 if (req->from_child)
2583 struct window *ptr;
2585 if (!(from_child = get_window( req->from_child ))) return;
2587 /* make sure from_child is a child of win */
2588 ptr = from_child;
2589 while (ptr && ptr != win) ptr = ptr->parent;
2590 if (!ptr)
2592 set_error( STATUS_INVALID_PARAMETER );
2593 return;
2597 if (flags & UPDATE_DELAYED_ERASE) /* this means that the previous call didn't erase */
2599 if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
2600 else win->paint_flags |= PAINT_DELAYED_ERASE;
2603 reply->flags = get_window_update_flags( win, from_child, flags, &win );
2604 reply->child = win->handle;
2606 if (flags & UPDATE_NOREGION) return;
2608 if (win->update_region)
2610 /* convert update region to screen coordinates */
2611 struct region *region = create_empty_region();
2613 if (!region) return;
2614 if (!copy_region( region, win->update_region ))
2616 free_region( region );
2617 return;
2619 if ((flags & UPDATE_CLIPCHILDREN) && (win->style & WS_CLIPCHILDREN))
2620 clip_children( win, NULL, region, win->client_rect.left - win->window_rect.left,
2621 win->client_rect.top - win->window_rect.top );
2622 map_win_region_to_screen( win, region );
2623 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
2624 &reply->total_size ))) return;
2625 set_reply_data_ptr( data, reply->total_size );
2628 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
2630 validate_parents( win );
2631 validate_whole_window( win );
2633 else
2635 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
2636 if (reply->flags & UPDATE_ERASE)
2638 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
2639 /* desktop window only gets erased, not repainted */
2640 if (is_desktop_window(win)) validate_whole_window( win );
2646 /* update the z order of a window so that a given rectangle is fully visible */
2647 DECL_HANDLER(update_window_zorder)
2649 rectangle_t tmp, rect = req->rect;
2650 struct window *ptr, *win = get_window( req->window );
2652 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
2654 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2656 if (ptr == win) break;
2657 if (!(ptr->style & WS_VISIBLE)) continue;
2658 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2659 if (ptr->is_layered && (ptr->layered_flags & LWA_COLORKEY)) continue;
2660 tmp = rect;
2661 map_dpi_rect( win, &tmp, win->parent->dpi, win->dpi );
2662 if (!intersect_rect( &tmp, &tmp, &ptr->visible_rect )) continue;
2663 if (ptr->win_region)
2665 offset_rect( &tmp, -ptr->window_rect.left, -ptr->window_rect.top );
2666 if (!rect_in_region( ptr->win_region, &tmp )) continue;
2668 /* found a window obscuring the rectangle, now move win above this one */
2669 /* making sure to not violate the topmost rule */
2670 if (!(ptr->ex_style & WS_EX_TOPMOST) || (win->ex_style & WS_EX_TOPMOST))
2672 list_remove( &win->entry );
2673 list_add_before( &ptr->entry, &win->entry );
2675 break;
2680 /* mark parts of a window as needing a redraw */
2681 DECL_HANDLER(redraw_window)
2683 unsigned int flags = req->flags;
2684 struct region *region = NULL;
2685 struct window *win;
2687 if (!req->window)
2689 if (!(win = get_desktop_window( current ))) return;
2691 else
2693 if (!(win = get_window( req->window ))) return;
2694 if (is_desktop_window( win )) flags &= ~RDW_ALLCHILDREN;
2697 if (!is_visible( win )) return; /* nothing to do */
2699 if (flags & (RDW_VALIDATE|RDW_INVALIDATE))
2701 if (get_req_data_size()) /* no data means whole rectangle */
2703 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2704 return;
2705 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->client_rect, region );
2709 redraw_window( win, region, (flags & RDW_INVALIDATE) && (flags & RDW_FRAME), flags );
2710 if (region) free_region( region );
2714 /* set a window property */
2715 DECL_HANDLER(set_window_property)
2717 struct unicode_str name = get_req_unicode_str();
2718 struct window *win = get_window( req->window );
2720 if (!win) return;
2722 if (name.len)
2724 atom_t atom = add_global_atom( NULL, &name );
2725 if (atom)
2727 set_property( win, atom, req->data, PROP_TYPE_STRING );
2728 release_global_atom( NULL, atom );
2731 else set_property( win, req->atom, req->data, PROP_TYPE_ATOM );
2735 /* remove a window property */
2736 DECL_HANDLER(remove_window_property)
2738 struct unicode_str name = get_req_unicode_str();
2739 struct window *win = get_window( req->window );
2741 if (win)
2743 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2744 if (atom) reply->data = remove_property( win, atom );
2749 /* get a window property */
2750 DECL_HANDLER(get_window_property)
2752 struct unicode_str name = get_req_unicode_str();
2753 struct window *win = get_window( req->window );
2755 if (win)
2757 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2758 if (atom) reply->data = get_property( win, atom );
2763 /* get the list of properties of a window */
2764 DECL_HANDLER(get_window_properties)
2766 property_data_t *data;
2767 int i, count, max = get_reply_max_size() / sizeof(*data);
2768 struct window *win = get_window( req->window );
2770 reply->total = 0;
2771 if (!win) return;
2773 for (i = count = 0; i < win->prop_inuse; i++)
2774 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2775 reply->total = count;
2777 if (count > max) count = max;
2778 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2780 for (i = 0; i < win->prop_inuse && count; i++)
2782 if (win->properties[i].type == PROP_TYPE_FREE) continue;
2783 data->atom = win->properties[i].atom;
2784 data->string = (win->properties[i].type == PROP_TYPE_STRING);
2785 data->data = win->properties[i].data;
2786 data++;
2787 count--;
2792 /* get the new window pointer for a global window, checking permissions */
2793 /* helper for set_global_windows request */
2794 static int get_new_global_window( struct window **win, user_handle_t handle )
2796 if (!handle)
2798 *win = NULL;
2799 return 1;
2801 else if (*win)
2803 set_error( STATUS_ACCESS_DENIED );
2804 return 0;
2806 *win = get_window( handle );
2807 return (*win != NULL);
2810 /* Set/get the global windows */
2811 DECL_HANDLER(set_global_windows)
2813 struct window *new_shell_window = shell_window;
2814 struct window *new_shell_listview = shell_listview;
2815 struct window *new_progman_window = progman_window;
2816 struct window *new_taskman_window = taskman_window;
2818 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2819 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2820 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2821 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2823 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2825 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2826 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2828 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2830 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2832 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2834 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2836 shell_window = new_shell_window;
2837 shell_listview = new_shell_listview;
2838 progman_window = new_progman_window;
2839 taskman_window = new_taskman_window;
2842 /* retrieve layered info for a window */
2843 DECL_HANDLER(get_window_layered_info)
2845 struct window *win = get_window( req->handle );
2847 if (!win) return;
2849 if (win->is_layered)
2851 reply->color_key = win->color_key;
2852 reply->alpha = win->alpha;
2853 reply->flags = win->layered_flags;
2855 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2859 /* set layered info for a window */
2860 DECL_HANDLER(set_window_layered_info)
2862 struct window *win = get_window( req->handle );
2864 if (!win) return;
2866 if (win->ex_style & WS_EX_LAYERED)
2868 int was_layered = win->is_layered;
2870 if (req->flags & LWA_ALPHA) win->alpha = req->alpha;
2871 else if (!win->is_layered) win->alpha = 0; /* alpha init value is 0 */
2873 win->color_key = req->color_key;
2874 win->layered_flags = req->flags;
2875 win->is_layered = 1;
2876 /* repaint since we know now it's not going to use UpdateLayeredWindow */
2877 if (!was_layered) redraw_window( win, 0, 1, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
2879 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );