server: Don't clip update regions to the desktop window.
[wine.git] / server / window.c
blob322001d8e32f5c74c28e6f1b61d7a5b1cec1847b
1 /*
2 * Server-side window handling
4 * Copyright (C) 2001 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winternl.h"
35 #include "object.h"
36 #include "request.h"
37 #include "thread.h"
38 #include "process.h"
39 #include "user.h"
40 #include "unicode.h"
42 /* a window property */
43 struct property
45 unsigned short type; /* property type (see below) */
46 atom_t atom; /* property atom */
47 lparam_t data; /* property data (user-defined storage) */
50 enum property_type
52 PROP_TYPE_FREE, /* free entry */
53 PROP_TYPE_STRING, /* atom that was originally a string */
54 PROP_TYPE_ATOM /* plain atom */
58 struct window
60 struct window *parent; /* parent window */
61 user_handle_t owner; /* owner of this window */
62 struct list children; /* list of children in Z-order */
63 struct list unlinked; /* list of children not linked in the Z-order list */
64 struct list entry; /* entry in parent's children list */
65 user_handle_t handle; /* full handle for this window */
66 struct thread *thread; /* thread owning the window */
67 struct desktop *desktop; /* desktop that the window belongs to */
68 struct window_class *class; /* window class */
69 atom_t atom; /* class atom */
70 user_handle_t last_active; /* last active popup */
71 rectangle_t window_rect; /* window rectangle (relative to parent client area) */
72 rectangle_t visible_rect; /* visible part of window rect (relative to parent client area) */
73 rectangle_t client_rect; /* client rectangle (relative to parent client area) */
74 struct region *win_region; /* region for shaped windows (relative to window rect) */
75 struct region *update_region; /* update region (relative to window rect) */
76 unsigned int style; /* window style */
77 unsigned int ex_style; /* window extended style */
78 unsigned int id; /* window id */
79 mod_handle_t instance; /* creator instance */
80 unsigned int is_unicode : 1; /* ANSI or unicode */
81 unsigned int is_linked : 1; /* is it linked into the parent z-order list? */
82 unsigned int is_layered : 1; /* has layered info been set? */
83 unsigned int color_key; /* color key for a layered window */
84 unsigned int alpha; /* alpha value for a layered window */
85 unsigned int layered_flags; /* flags for a layered window */
86 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 unsigned int paint_flags; /* various painting flags */
91 int prop_inuse; /* number of in-use window properties */
92 int prop_alloc; /* number of allocated window properties */
93 struct property *properties; /* window properties array */
94 int nb_extra_bytes; /* number of extra bytes */
95 char extra_bytes[1]; /* extra bytes storage */
98 /* flags that can be set by the client */
99 #define PAINT_HAS_SURFACE SET_WINPOS_PAINT_SURFACE
100 #define PAINT_HAS_PIXEL_FORMAT SET_WINPOS_PIXEL_FORMAT
101 #define PAINT_CLIENT_FLAGS (PAINT_HAS_SURFACE | PAINT_HAS_PIXEL_FORMAT)
102 /* flags only manipulated by the server */
103 #define PAINT_INTERNAL 0x0010 /* internal WM_PAINT pending */
104 #define PAINT_ERASE 0x0020 /* needs WM_ERASEBKGND */
105 #define PAINT_NONCLIENT 0x0040 /* needs WM_NCPAINT */
106 #define PAINT_DELAYED_ERASE 0x0080 /* still needs erase after WM_ERASEBKGND */
107 #define PAINT_PIXEL_FORMAT_CHILD 0x0100 /* at least one child has a custom pixel format */
109 /* growable array of user handles */
110 struct user_handle_array
112 user_handle_t *handles;
113 int count;
114 int total;
117 static const rectangle_t empty_rect;
119 /* global window pointers */
120 static struct window *shell_window;
121 static struct window *shell_listview;
122 static struct window *progman_window;
123 static struct window *taskman_window;
125 /* magic HWND_TOP etc. pointers */
126 #define WINPTR_TOP ((struct window *)1L)
127 #define WINPTR_BOTTOM ((struct window *)2L)
128 #define WINPTR_TOPMOST ((struct window *)3L)
129 #define WINPTR_NOTOPMOST ((struct window *)4L)
131 /* retrieve a pointer to a window from its handle */
132 static inline struct window *get_window( user_handle_t handle )
134 struct window *ret = get_user_object( handle, USER_WINDOW );
135 if (!ret) set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
136 return ret;
139 /* check if window is the desktop */
140 static inline int is_desktop_window( const struct window *win )
142 return !win->parent; /* only desktop windows have no parent */
145 /* get next window in Z-order list */
146 static inline struct window *get_next_window( struct window *win )
148 struct list *ptr = list_next( &win->parent->children, &win->entry );
149 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
152 /* get previous window in Z-order list */
153 static inline struct window *get_prev_window( struct window *win )
155 struct list *ptr = list_prev( &win->parent->children, &win->entry );
156 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
159 /* get first child in Z-order list */
160 static inline struct window *get_first_child( struct window *win )
162 struct list *ptr = list_head( &win->children );
163 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
166 /* get last child in Z-order list */
167 static inline struct window *get_last_child( struct window *win )
169 struct list *ptr = list_tail( &win->children );
170 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
173 /* set the PAINT_PIXEL_FORMAT_CHILD flag on all the parents */
174 /* note: we never reset the flag, it's just a heuristic */
175 static inline void update_pixel_format_flags( struct window *win )
177 for (win = win->parent; win && win->parent; win = win->parent)
178 win->paint_flags |= PAINT_PIXEL_FORMAT_CHILD;
181 /* get the per-monitor DPI for a window */
182 static unsigned int get_monitor_dpi( struct window *win )
184 /* FIXME: we return the desktop window DPI for now */
185 while (!is_desktop_window( win )) win = win->parent;
186 return win->dpi ? win->dpi : USER_DEFAULT_SCREEN_DPI;
189 /* link a window at the right place in the siblings list */
190 static void link_window( struct window *win, struct window *previous )
192 if (previous == WINPTR_NOTOPMOST)
194 if (!(win->ex_style & WS_EX_TOPMOST) && win->is_linked) return; /* nothing to do */
195 win->ex_style &= ~WS_EX_TOPMOST;
196 previous = WINPTR_TOP; /* fallback to the HWND_TOP case */
199 list_remove( &win->entry ); /* unlink it from the previous location */
201 if (previous == WINPTR_BOTTOM)
203 list_add_tail( &win->parent->children, &win->entry );
204 win->ex_style &= ~WS_EX_TOPMOST;
206 else if (previous == WINPTR_TOPMOST)
208 list_add_head( &win->parent->children, &win->entry );
209 win->ex_style |= WS_EX_TOPMOST;
211 else if (previous == WINPTR_TOP)
213 struct list *entry = win->parent->children.next;
214 if (!(win->ex_style & WS_EX_TOPMOST)) /* put it above the first non-topmost window */
216 while (entry != &win->parent->children)
218 struct window *next = LIST_ENTRY( entry, struct window, entry );
219 if (!(next->ex_style & WS_EX_TOPMOST)) break;
220 if (next->handle == win->owner) /* keep it above owner */
222 win->ex_style |= WS_EX_TOPMOST;
223 break;
225 entry = entry->next;
228 list_add_before( entry, &win->entry );
230 else
232 list_add_after( &previous->entry, &win->entry );
233 if (!(previous->ex_style & WS_EX_TOPMOST)) win->ex_style &= ~WS_EX_TOPMOST;
234 else
236 struct window *next = get_next_window( win );
237 if (next && (next->ex_style & WS_EX_TOPMOST)) win->ex_style |= WS_EX_TOPMOST;
241 win->is_linked = 1;
244 /* change the parent of a window (or unlink the window if the new parent is NULL) */
245 static int set_parent_window( struct window *win, struct window *parent )
247 struct window *ptr;
249 /* make sure parent is not a child of window */
250 for (ptr = parent; ptr; ptr = ptr->parent)
252 if (ptr == win)
254 set_error( STATUS_INVALID_PARAMETER );
255 return 0;
259 if (parent)
261 win->parent = parent;
262 link_window( win, WINPTR_TOP );
264 if (!is_desktop_window( parent ))
266 win->dpi = parent->dpi;
267 win->dpi_awareness = parent->dpi_awareness;
270 /* if parent belongs to a different thread and the window isn't */
271 /* top-level, attach the two threads */
272 if (parent->thread && parent->thread != win->thread && !is_desktop_window(parent))
273 attach_thread_input( win->thread, parent->thread );
275 if (win->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))
276 update_pixel_format_flags( win );
278 else /* move it to parent unlinked list */
280 list_remove( &win->entry ); /* unlink it from the previous location */
281 list_add_head( &win->parent->unlinked, &win->entry );
282 win->is_linked = 0;
284 return 1;
287 /* append a user handle to a handle array */
288 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
290 if (array->count >= array->total)
292 int new_total = max( array->total * 2, 32 );
293 user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
294 if (!new_array)
296 free( array->handles );
297 set_error( STATUS_NO_MEMORY );
298 return 0;
300 array->handles = new_array;
301 array->total = new_total;
303 array->handles[array->count++] = handle;
304 return 1;
307 /* set a window property */
308 static void set_property( struct window *win, atom_t atom, lparam_t data, enum property_type type )
310 int i, free = -1;
311 struct property *new_props;
313 /* check if it exists already */
314 for (i = 0; i < win->prop_inuse; i++)
316 if (win->properties[i].type == PROP_TYPE_FREE)
318 free = i;
319 continue;
321 if (win->properties[i].atom == atom)
323 win->properties[i].type = type;
324 win->properties[i].data = data;
325 return;
329 /* need to add an entry */
330 if (!grab_global_atom( NULL, atom )) return;
331 if (free == -1)
333 /* no free entry */
334 if (win->prop_inuse >= win->prop_alloc)
336 /* need to grow the array */
337 if (!(new_props = realloc( win->properties,
338 sizeof(*new_props) * (win->prop_alloc + 16) )))
340 set_error( STATUS_NO_MEMORY );
341 release_global_atom( NULL, atom );
342 return;
344 win->prop_alloc += 16;
345 win->properties = new_props;
347 free = win->prop_inuse++;
349 win->properties[free].atom = atom;
350 win->properties[free].type = type;
351 win->properties[free].data = data;
354 /* remove a window property */
355 static lparam_t remove_property( struct window *win, atom_t atom )
357 int i;
359 for (i = 0; i < win->prop_inuse; i++)
361 if (win->properties[i].type == PROP_TYPE_FREE) continue;
362 if (win->properties[i].atom == atom)
364 release_global_atom( NULL, atom );
365 win->properties[i].type = PROP_TYPE_FREE;
366 return win->properties[i].data;
369 /* FIXME: last error? */
370 return 0;
373 /* find a window property */
374 static lparam_t get_property( struct window *win, atom_t atom )
376 int i;
378 for (i = 0; i < win->prop_inuse; i++)
380 if (win->properties[i].type == PROP_TYPE_FREE) continue;
381 if (win->properties[i].atom == atom) return win->properties[i].data;
383 /* FIXME: last error? */
384 return 0;
387 /* destroy all properties of a window */
388 static inline void destroy_properties( struct window *win )
390 int i;
392 if (!win->properties) return;
393 for (i = 0; i < win->prop_inuse; i++)
395 if (win->properties[i].type == PROP_TYPE_FREE) continue;
396 release_global_atom( NULL, win->properties[i].atom );
398 free( win->properties );
401 /* detach a window from its owner thread but keep the window around */
402 static void detach_window_thread( struct window *win )
404 struct thread *thread = win->thread;
406 if (!thread) return;
407 if (thread->queue)
409 if (win->update_region) inc_queue_paint_count( thread, -1 );
410 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
411 queue_cleanup_window( thread, win->handle );
413 assert( thread->desktop_users > 0 );
414 thread->desktop_users--;
415 release_class( win->class );
416 win->class = NULL;
418 /* don't hold a reference to the desktop so that the desktop window can be */
419 /* destroyed when the desktop ref count reaches zero */
420 release_object( win->desktop );
421 win->thread = NULL;
424 /* get the process owning the top window of a given desktop */
425 struct process *get_top_window_owner( struct desktop *desktop )
427 struct window *win = desktop->top_window;
428 if (!win || !win->thread) return NULL;
429 return win->thread->process;
432 /* get the top window size of a given desktop */
433 void get_top_window_rectangle( struct desktop *desktop, rectangle_t *rect )
435 struct window *win = desktop->top_window;
436 *rect = win ? win->window_rect : empty_rect;
439 /* post a message to the desktop window */
440 void post_desktop_message( struct desktop *desktop, unsigned int message,
441 lparam_t wparam, lparam_t lparam )
443 struct window *win = desktop->top_window;
444 if (win && win->thread) post_message( win->handle, message, wparam, lparam );
447 /* create a new window structure (note: the window is not linked in the window tree) */
448 static struct window *create_window( struct window *parent, struct window *owner,
449 atom_t atom, mod_handle_t instance )
451 int extra_bytes;
452 struct window *win = NULL;
453 struct desktop *desktop;
454 struct window_class *class;
456 if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
458 if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
460 release_object( desktop );
461 return NULL;
464 if (!parent) /* null parent is only allowed for desktop or HWND_MESSAGE top window */
466 if (is_desktop_class( class ))
467 parent = desktop->top_window; /* use existing desktop if any */
468 else if (is_hwnd_message_class( class ))
469 /* use desktop window if message window is already created */
470 parent = desktop->msg_window ? desktop->top_window : NULL;
471 else if (!(parent = desktop->top_window)) /* must already have a desktop then */
473 set_error( STATUS_ACCESS_DENIED );
474 goto failed;
478 /* parent must be on the same desktop */
479 if (parent && parent->desktop != desktop)
481 set_error( STATUS_ACCESS_DENIED );
482 goto failed;
485 if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
486 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
488 win->parent = parent;
489 win->owner = owner ? owner->handle : 0;
490 win->thread = current;
491 win->desktop = desktop;
492 win->class = class;
493 win->atom = atom;
494 win->last_active = win->handle;
495 win->win_region = NULL;
496 win->update_region = NULL;
497 win->style = 0;
498 win->ex_style = 0;
499 win->id = 0;
500 win->instance = 0;
501 win->is_unicode = 1;
502 win->is_linked = 0;
503 win->is_layered = 0;
504 win->dpi_awareness = DPI_AWARENESS_PER_MONITOR_AWARE;
505 win->dpi = 0;
506 win->user_data = 0;
507 win->text = NULL;
508 win->paint_flags = 0;
509 win->prop_inuse = 0;
510 win->prop_alloc = 0;
511 win->properties = NULL;
512 win->nb_extra_bytes = extra_bytes;
513 win->window_rect = win->visible_rect = win->client_rect = empty_rect;
514 memset( win->extra_bytes, 0, extra_bytes );
515 list_init( &win->children );
516 list_init( &win->unlinked );
518 /* if parent belongs to a different thread and the window isn't */
519 /* top-level, attach the two threads */
520 if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
522 if (!attach_thread_input( current, parent->thread )) goto failed;
524 else /* otherwise just make sure that the thread has a message queue */
526 if (!current->queue && !init_thread_queue( current )) goto failed;
529 /* put it on parent unlinked list */
530 if (parent) list_add_head( &parent->unlinked, &win->entry );
531 else
533 list_init( &win->entry );
534 if (is_desktop_class( class ))
536 assert( !desktop->top_window );
537 desktop->top_window = win;
538 set_process_default_desktop( current->process, desktop, current->desktop );
540 else
542 assert( !desktop->msg_window );
543 desktop->msg_window = win;
547 current->desktop_users++;
548 return win;
550 failed:
551 if (win)
553 if (win->handle) free_user_handle( win->handle );
554 free( win );
556 release_object( desktop );
557 release_class( class );
558 return NULL;
561 /* destroy all windows belonging to a given thread */
562 void destroy_thread_windows( struct thread *thread )
564 user_handle_t handle = 0;
565 struct window *win;
567 while ((win = next_user_handle( &handle, USER_WINDOW )))
569 if (win->thread != thread) continue;
570 if (is_desktop_window( win )) detach_window_thread( win );
571 else destroy_window( win );
575 /* get the desktop window */
576 static struct window *get_desktop_window( struct thread *thread )
578 struct window *top_window;
579 struct desktop *desktop = get_thread_desktop( thread, 0 );
581 if (!desktop) return NULL;
582 top_window = desktop->top_window;
583 release_object( desktop );
584 return top_window;
587 /* check whether child is a descendant of parent */
588 int is_child_window( user_handle_t parent, user_handle_t child )
590 struct window *child_ptr = get_user_object( child, USER_WINDOW );
591 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
593 if (!child_ptr || !parent_ptr) return 0;
594 while (child_ptr->parent)
596 if (child_ptr->parent == parent_ptr) return 1;
597 child_ptr = child_ptr->parent;
599 return 0;
602 /* check if window can be set as foreground window */
603 int is_valid_foreground_window( user_handle_t window )
605 struct window *win = get_user_object( window, USER_WINDOW );
606 return win && (win->style & (WS_POPUP|WS_CHILD)) != WS_CHILD;
609 /* make a window active if possible */
610 int make_window_active( user_handle_t window )
612 struct window *owner, *win = get_window( window );
614 if (!win) return 0;
616 /* set last active for window and its owners */
617 owner = win;
618 while (owner)
620 owner->last_active = win->handle;
621 owner = get_user_object( owner->owner, USER_WINDOW );
623 return 1;
626 /* increment (or decrement) the window paint count */
627 static inline void inc_window_paint_count( struct window *win, int incr )
629 if (win->thread) inc_queue_paint_count( win->thread, incr );
632 /* check if window and all its ancestors are visible */
633 static int is_visible( const struct window *win )
635 while (win)
637 if (!(win->style & WS_VISIBLE)) return 0;
638 win = win->parent;
639 /* if parent is minimized children are not visible */
640 if (win && (win->style & WS_MINIMIZE)) return 0;
642 return 1;
645 /* same as is_visible but takes a window handle */
646 int is_window_visible( user_handle_t window )
648 struct window *win = get_user_object( window, USER_WINDOW );
649 if (!win) return 0;
650 return is_visible( win );
653 int is_window_transparent( user_handle_t window )
655 struct window *win = get_user_object( window, USER_WINDOW );
656 if (!win) return 0;
657 return (win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT);
660 /* check if point is inside the window */
661 static inline int is_point_in_window( struct window *win, int x, int y )
663 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
664 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
665 return 0; /* disabled child */
666 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
667 return 0; /* transparent */
668 if (!point_in_rect( &win->visible_rect, x, y ))
669 return 0; /* not in window */
670 if (win->win_region &&
671 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
672 return 0; /* not in window region */
673 return 1;
676 /* fill an array with the handles of the children of a specified window */
677 static unsigned int get_children_windows( struct window *parent, atom_t atom, thread_id_t tid,
678 user_handle_t *handles, unsigned int max_count )
680 struct window *ptr;
681 unsigned int count = 0;
683 if (!parent) return 0;
685 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
687 if (atom && get_class_atom(ptr->class) != atom) continue;
688 if (tid && get_thread_id(ptr->thread) != tid) continue;
689 if (handles)
691 if (count >= max_count) break;
692 handles[count] = ptr->handle;
694 count++;
696 return count;
699 /* find child of 'parent' that contains the given point (in parent-relative coords) */
700 static struct window *child_window_from_point( struct window *parent, int x, int y )
702 struct window *ptr;
704 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
706 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
708 /* if window is minimized or disabled, return at once */
709 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
711 /* if point is not in client area, return at once */
712 if (!point_in_rect( &ptr->client_rect, x, y )) return ptr;
714 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
716 return parent; /* not found any child */
719 /* find all children of 'parent' that contain the given point */
720 static int get_window_children_from_point( struct window *parent, int x, int y,
721 struct user_handle_array *array )
723 struct window *ptr;
725 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
727 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
729 /* if point is in client area, and window is not minimized or disabled, check children */
730 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) && point_in_rect( &ptr->client_rect, x, y ))
732 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
733 y - ptr->client_rect.top, array ))
734 return 0;
737 /* now add window to the array */
738 if (!add_handle_to_array( array, ptr->handle )) return 0;
740 return 1;
743 /* get handle of root of top-most window containing point */
744 user_handle_t shallow_window_from_point( struct desktop *desktop, int x, int y )
746 struct window *ptr;
748 if (!desktop->top_window) return 0;
750 LIST_FOR_EACH_ENTRY( ptr, &desktop->top_window->children, struct window, entry )
752 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
753 return ptr->handle;
755 return desktop->top_window->handle;
758 /* return thread of top-most window containing point (in absolute coords) */
759 struct thread *window_thread_from_point( user_handle_t scope, int x, int y )
761 struct window *win = get_user_object( scope, USER_WINDOW );
762 struct window *ptr;
764 if (!win) return NULL;
766 for (ptr = win; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
768 x -= ptr->client_rect.left;
769 y -= ptr->client_rect.top;
772 ptr = child_window_from_point( win, x, y );
773 if (!ptr->thread) return NULL;
774 return (struct thread *)grab_object( ptr->thread );
777 /* return list of all windows containing point (in absolute coords) */
778 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
780 struct window *ptr;
782 /* make point relative to top window */
783 for (ptr = top->parent; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
785 x -= ptr->client_rect.left;
786 y -= ptr->client_rect.top;
789 if (!is_point_in_window( top, x, y )) return 1;
791 /* if point is in client area, and window is not minimized or disabled, check children */
792 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) && point_in_rect( &top->client_rect, x, y ))
794 if (!is_desktop_window(top))
796 x -= top->client_rect.left;
797 y -= top->client_rect.top;
799 if (!get_window_children_from_point( top, x, y, array )) return 0;
801 /* now add window to the array */
802 if (!add_handle_to_array( array, top->handle )) return 0;
803 return 1;
807 /* return the thread owning a window */
808 struct thread *get_window_thread( user_handle_t handle )
810 struct window *win = get_user_object( handle, USER_WINDOW );
811 if (!win || !win->thread) return NULL;
812 return (struct thread *)grab_object( win->thread );
816 /* check if any area of a window needs repainting */
817 static inline int win_needs_repaint( struct window *win )
819 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
823 /* find a child of the specified window that needs repainting */
824 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
826 struct window *ptr, *ret = NULL;
828 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
830 if (!(ptr->style & WS_VISIBLE)) continue;
831 if (ptr->thread == thread && win_needs_repaint( ptr ))
832 ret = ptr;
833 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
834 ret = find_child_to_repaint( ptr, thread );
835 if (ret) break;
838 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
840 /* transparent window, check for non-transparent sibling to paint first */
841 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
843 if (!(ptr->style & WS_VISIBLE)) continue;
844 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
845 if (ptr->thread != thread) continue;
846 if (win_needs_repaint( ptr )) return ptr;
849 return ret;
853 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
854 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
856 struct window *ptr, *win, *top_window = get_desktop_window( thread );
858 if (!top_window) return 0;
860 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
861 else win = find_child_to_repaint( top_window, thread );
863 if (win && parent)
865 /* check that it is a child of the specified parent */
866 for (ptr = win; ptr; ptr = ptr->parent)
867 if (ptr->handle == parent) break;
868 /* otherwise don't return any window, we don't repaint a child before its parent */
869 if (!ptr) win = NULL;
871 if (!win) return 0;
872 win->paint_flags &= ~PAINT_INTERNAL;
873 return win->handle;
877 /* intersect the window region with the specified region, relative to the window parent */
878 static struct region *intersect_window_region( struct region *region, struct window *win )
880 /* make region relative to window rect */
881 offset_region( region, -win->window_rect.left, -win->window_rect.top );
882 if (!intersect_region( region, region, win->win_region )) return NULL;
883 /* make region relative to parent again */
884 offset_region( region, win->window_rect.left, win->window_rect.top );
885 return region;
889 /* convert coordinates from client to screen coords */
890 static inline void client_to_screen( struct window *win, int *x, int *y )
892 for ( ; win && !is_desktop_window(win); win = win->parent)
894 *x += win->client_rect.left;
895 *y += win->client_rect.top;
899 /* convert coordinates from client to screen coords */
900 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
902 for ( ; win && !is_desktop_window(win); win = win->parent)
903 offset_rect( rect, win->client_rect.left, win->client_rect.top );
906 /* map the region from window to screen coordinates */
907 static inline void map_win_region_to_screen( struct window *win, struct region *region )
909 if (!is_desktop_window(win))
911 int x = win->window_rect.left;
912 int y = win->window_rect.top;
913 client_to_screen( win->parent, &x, &y );
914 offset_region( region, x, y );
919 /* clip all children of a given window out of the visible region */
920 static struct region *clip_children( struct window *parent, struct window *last,
921 struct region *region, int offset_x, int offset_y )
923 struct window *ptr;
924 struct region *tmp = create_empty_region();
926 if (!tmp) return NULL;
927 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
929 if (ptr == last) break;
930 if (!(ptr->style & WS_VISIBLE)) continue;
931 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
932 set_region_rect( tmp, &ptr->visible_rect );
933 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
935 free_region( tmp );
936 return NULL;
938 offset_region( tmp, offset_x, offset_y );
939 if (!(region = subtract_region( region, region, tmp ))) break;
940 if (is_region_empty( region )) break;
942 free_region( tmp );
943 return region;
947 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
948 static void set_region_client_rect( struct region *region, struct window *win )
950 rectangle_t rect;
952 intersect_rect( &rect, &win->window_rect, &win->client_rect );
953 set_region_rect( region, &rect );
957 /* get the top-level window to clip against for a given window */
958 static inline struct window *get_top_clipping_window( struct window *win )
960 while (!(win->paint_flags & PAINT_HAS_SURFACE) && win->parent && !is_desktop_window(win->parent))
961 win = win->parent;
962 return win;
966 /* compute the visible region of a window, in window coordinates */
967 static struct region *get_visible_region( struct window *win, unsigned int flags )
969 struct region *tmp = NULL, *region;
970 int offset_x, offset_y;
972 if (!(region = create_empty_region())) return NULL;
974 /* first check if all ancestors are visible */
976 if (!is_visible( win )) return region; /* empty region */
978 /* create a region relative to the window itself */
980 if ((flags & DCX_PARENTCLIP) && win->parent && !is_desktop_window(win->parent))
982 set_region_client_rect( region, win->parent );
983 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
985 else if (flags & DCX_WINDOW)
987 set_region_rect( region, &win->visible_rect );
988 if (win->win_region && !intersect_window_region( region, win )) goto error;
990 else
992 set_region_client_rect( region, win );
993 if (win->win_region && !intersect_window_region( region, win )) goto error;
996 /* clip children */
998 if (flags & DCX_CLIPCHILDREN)
1000 if (is_desktop_window(win)) offset_x = offset_y = 0;
1001 else
1003 offset_x = win->client_rect.left;
1004 offset_y = win->client_rect.top;
1006 if (!clip_children( win, NULL, region, offset_x, offset_y )) goto error;
1009 /* clip siblings of ancestors */
1011 if (is_desktop_window(win)) offset_x = offset_y = 0;
1012 else
1014 offset_x = win->window_rect.left;
1015 offset_y = win->window_rect.top;
1018 if ((tmp = create_empty_region()) != NULL)
1020 while (win->parent)
1022 /* we don't clip out top-level siblings as that's up to the native windowing system */
1023 if ((win->style & WS_CLIPSIBLINGS) && !is_desktop_window( win->parent ))
1025 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
1026 if (is_region_empty( region )) break;
1028 /* clip to parent client area */
1029 win = win->parent;
1030 if (!is_desktop_window(win))
1032 offset_x += win->client_rect.left;
1033 offset_y += win->client_rect.top;
1034 offset_region( region, win->client_rect.left, win->client_rect.top );
1036 set_region_client_rect( tmp, win );
1037 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
1038 if (!intersect_region( region, region, tmp )) goto error;
1039 if (is_region_empty( region )) break;
1041 free_region( tmp );
1043 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
1044 return region;
1046 error:
1047 if (tmp) free_region( tmp );
1048 free_region( region );
1049 return NULL;
1053 /* clip all children with a custom pixel format out of the visible region */
1054 static struct region *clip_pixel_format_children( struct window *parent, struct region *parent_clip,
1055 struct region *region, int offset_x, int offset_y )
1057 struct window *ptr;
1058 struct region *clip = create_empty_region();
1060 if (!clip) return NULL;
1062 LIST_FOR_EACH_ENTRY_REV( ptr, &parent->children, struct window, entry )
1064 if (!(ptr->style & WS_VISIBLE)) continue;
1065 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1067 /* add the visible rect */
1068 set_region_rect( clip, &ptr->visible_rect );
1069 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1070 offset_region( clip, offset_x, offset_y );
1071 if (!intersect_region( clip, clip, parent_clip )) break;
1072 if (!union_region( region, region, clip )) break;
1073 if (!(ptr->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))) continue;
1075 /* subtract the client rect if it uses a custom pixel format */
1076 set_region_rect( clip, &ptr->client_rect );
1077 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1078 offset_region( clip, offset_x, offset_y );
1079 if (!intersect_region( clip, clip, parent_clip )) break;
1080 if ((ptr->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1081 break;
1083 if (!clip_pixel_format_children( ptr, clip, region, offset_x + ptr->client_rect.left,
1084 offset_y + ptr->client_rect.top ))
1085 break;
1087 free_region( clip );
1088 return region;
1092 /* compute the visible surface region of a window, in parent coordinates */
1093 static struct region *get_surface_region( struct window *win )
1095 struct region *region, *clip;
1096 int offset_x, offset_y;
1098 /* create a region relative to the window itself */
1100 if (!(region = create_empty_region())) return NULL;
1101 if (!(clip = create_empty_region())) goto error;
1102 set_region_rect( region, &win->visible_rect );
1103 if (win->win_region && !intersect_window_region( region, win )) goto error;
1104 set_region_rect( clip, &win->client_rect );
1105 if (win->win_region && !intersect_window_region( clip, win )) goto error;
1107 if ((win->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1108 goto error;
1110 /* clip children */
1112 if (!is_desktop_window(win))
1114 offset_x = win->client_rect.left;
1115 offset_y = win->client_rect.top;
1117 else offset_x = offset_y = 0;
1119 if (!clip_pixel_format_children( win, clip, region, offset_x, offset_y )) goto error;
1121 free_region( clip );
1122 return region;
1124 error:
1125 if (clip) free_region( clip );
1126 free_region( region );
1127 return NULL;
1131 /* get the window class of a window */
1132 struct window_class* get_window_class( user_handle_t window )
1134 struct window *win;
1135 if (!(win = get_window( window ))) return NULL;
1136 if (!win->class) set_error( STATUS_ACCESS_DENIED );
1137 return win->class;
1140 /* determine the window visible rectangle, i.e. window or client rect cropped by parent rects */
1141 /* the returned rectangle is in window coordinates; return 0 if rectangle is empty */
1142 static int get_window_visible_rect( struct window *win, rectangle_t *rect, int frame )
1144 int offset_x = win->window_rect.left, offset_y = win->window_rect.top;
1146 *rect = frame ? win->window_rect : win->client_rect;
1148 if (!(win->style & WS_VISIBLE)) return 0;
1149 if (is_desktop_window( win )) return 1;
1151 while (!is_desktop_window( win->parent ))
1153 win = win->parent;
1154 if (!(win->style & WS_VISIBLE) || win->style & WS_MINIMIZE) return 0;
1155 offset_x += win->client_rect.left;
1156 offset_y += win->client_rect.top;
1157 offset_rect( rect, win->client_rect.left, win->client_rect.top );
1158 if (!intersect_rect( rect, rect, &win->client_rect )) return 0;
1159 if (!intersect_rect( rect, rect, &win->window_rect )) return 0;
1161 offset_rect( rect, -offset_x, -offset_y );
1162 return 1;
1165 /* return a copy of the specified region cropped to the window client or frame rectangle, */
1166 /* and converted from client to window coordinates. Helper for (in)validate_window. */
1167 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
1169 rectangle_t rect;
1170 struct region *tmp;
1172 if (!get_window_visible_rect( win, &rect, frame )) return NULL;
1173 if (!(tmp = create_empty_region())) return NULL;
1174 set_region_rect( tmp, &rect );
1176 if (region)
1178 /* map it to client coords */
1179 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1180 win->window_rect.top - win->client_rect.top );
1182 /* intersect specified region with bounding rect */
1183 if (!intersect_region( tmp, region, tmp )) goto done;
1184 if (is_region_empty( tmp )) goto done;
1186 /* map it back to window coords */
1187 offset_region( tmp, win->client_rect.left - win->window_rect.left,
1188 win->client_rect.top - win->window_rect.top );
1190 return tmp;
1192 done:
1193 free_region( tmp );
1194 return NULL;
1198 /* set a region as new update region for the window */
1199 static void set_update_region( struct window *win, struct region *region )
1201 if (region && !is_region_empty( region ))
1203 if (!win->update_region) inc_window_paint_count( win, 1 );
1204 else free_region( win->update_region );
1205 win->update_region = region;
1207 else
1209 if (win->update_region)
1211 inc_window_paint_count( win, -1 );
1212 free_region( win->update_region );
1214 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
1215 win->update_region = NULL;
1216 if (region) free_region( region );
1221 /* add a region to the update region; the passed region is freed or reused */
1222 static int add_update_region( struct window *win, struct region *region )
1224 if (win->update_region && !union_region( region, win->update_region, region ))
1226 free_region( region );
1227 return 0;
1229 set_update_region( win, region );
1230 return 1;
1234 /* crop the update region of children to the specified rectangle, in client coords */
1235 static void crop_children_update_region( struct window *win, rectangle_t *rect )
1237 struct window *child;
1238 struct region *tmp;
1239 rectangle_t child_rect;
1241 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1243 if (!(child->style & WS_VISIBLE)) continue;
1244 if (!rect) /* crop everything out */
1246 crop_children_update_region( child, NULL );
1247 set_update_region( child, NULL );
1248 continue;
1251 /* nothing to do if child is completely inside rect */
1252 if (child->window_rect.left >= rect->left &&
1253 child->window_rect.top >= rect->top &&
1254 child->window_rect.right <= rect->right &&
1255 child->window_rect.bottom <= rect->bottom) continue;
1257 /* map to child client coords and crop grand-children */
1258 child_rect = *rect;
1259 offset_rect( &child_rect, -child->client_rect.left, -child->client_rect.top );
1260 crop_children_update_region( child, &child_rect );
1262 /* now crop the child itself */
1263 if (!child->update_region) continue;
1264 if (!(tmp = create_empty_region())) continue;
1265 set_region_rect( tmp, rect );
1266 offset_region( tmp, -child->window_rect.left, -child->window_rect.top );
1267 if (intersect_region( tmp, child->update_region, tmp )) set_update_region( child, tmp );
1268 else free_region( tmp );
1273 /* validate the non client area of a window */
1274 static void validate_non_client( struct window *win )
1276 struct region *tmp;
1277 rectangle_t rect;
1279 if (!win->update_region) return; /* nothing to do */
1281 /* get client rect in window coords */
1282 rect.left = win->client_rect.left - win->window_rect.left;
1283 rect.top = win->client_rect.top - win->window_rect.top;
1284 rect.right = win->client_rect.right - win->window_rect.left;
1285 rect.bottom = win->client_rect.bottom - win->window_rect.top;
1287 if ((tmp = create_empty_region()))
1289 set_region_rect( tmp, &rect );
1290 if (intersect_region( tmp, win->update_region, tmp ))
1291 set_update_region( win, tmp );
1292 else
1293 free_region( tmp );
1295 win->paint_flags &= ~PAINT_NONCLIENT;
1299 /* validate a window completely so that we don't get any further paint messages for it */
1300 static void validate_whole_window( struct window *win )
1302 set_update_region( win, NULL );
1304 if (win->paint_flags & PAINT_INTERNAL)
1306 win->paint_flags &= ~PAINT_INTERNAL;
1307 inc_window_paint_count( win, -1 );
1312 /* validate a window's children so that we don't get any further paint messages for it */
1313 static void validate_children( struct window *win )
1315 struct window *child;
1317 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1319 if (!(child->style & WS_VISIBLE)) continue;
1320 validate_children(child);
1321 validate_whole_window(child);
1326 /* validate the update region of a window on all parents; helper for get_update_region */
1327 static void validate_parents( struct window *child )
1329 int offset_x = 0, offset_y = 0;
1330 struct window *win = child;
1331 struct region *tmp = NULL;
1333 if (!child->update_region) return;
1335 while (win->parent)
1337 /* map to parent client coords */
1338 offset_x += win->window_rect.left;
1339 offset_y += win->window_rect.top;
1341 win = win->parent;
1343 /* and now map to window coords */
1344 offset_x += win->client_rect.left - win->window_rect.left;
1345 offset_y += win->client_rect.top - win->window_rect.top;
1347 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1349 if (!tmp && !(tmp = create_empty_region())) return;
1350 offset_region( child->update_region, offset_x, offset_y );
1351 if (subtract_region( tmp, win->update_region, child->update_region ))
1353 set_update_region( win, tmp );
1354 tmp = NULL;
1356 /* restore child coords */
1357 offset_region( child->update_region, -offset_x, -offset_y );
1360 if (tmp) free_region( tmp );
1364 /* add/subtract a region (in client coordinates) to the update region of the window */
1365 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1367 struct region *tmp;
1368 struct window *child;
1370 if (flags & RDW_INVALIDATE)
1372 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1374 if (!add_update_region( win, tmp )) return;
1376 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1377 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1379 else if (flags & RDW_VALIDATE)
1381 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1383 set_update_region( win, NULL );
1385 else if (win->update_region)
1387 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1389 if (!subtract_region( tmp, win->update_region, tmp ))
1391 free_region( tmp );
1392 return;
1394 set_update_region( win, tmp );
1396 if (flags & RDW_NOFRAME) validate_non_client( win );
1397 if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1401 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1403 win->paint_flags |= PAINT_INTERNAL;
1404 inc_window_paint_count( win, 1 );
1406 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1408 win->paint_flags &= ~PAINT_INTERNAL;
1409 inc_window_paint_count( win, -1 );
1412 /* now process children recursively */
1414 if (flags & RDW_NOCHILDREN) return;
1415 if (win->style & WS_MINIMIZE) return;
1416 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1418 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1420 /* map to client coordinates */
1421 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1422 win->window_rect.top - win->client_rect.top );
1424 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1426 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1428 if (!(child->style & WS_VISIBLE)) continue;
1429 if (!rect_in_region( tmp, &child->window_rect )) continue;
1430 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1431 redraw_window( child, tmp, 1, flags );
1432 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1434 free_region( tmp );
1438 /* retrieve the update flags for a window depending on the state of the update region */
1439 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1441 unsigned int ret = 0;
1443 if (flags & UPDATE_NONCLIENT)
1445 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1447 if (flags & UPDATE_ERASE)
1449 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1451 if (flags & UPDATE_PAINT)
1453 if (win->update_region)
1455 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1456 ret |= UPDATE_PAINT;
1459 if (flags & UPDATE_INTERNALPAINT)
1461 if (win->paint_flags & PAINT_INTERNAL)
1463 ret |= UPDATE_INTERNALPAINT;
1464 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1467 return ret;
1471 /* iterate through the children of the given window until we find one with some update flags */
1472 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1473 unsigned int flags, struct window **child )
1475 struct window *ptr;
1476 unsigned int ret = 0;
1478 /* first make sure we want to iterate children at all */
1480 if (win->style & WS_MINIMIZE) return 0;
1482 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1483 * here we only want to repaint children of windows that clip them, others
1484 * need to wait for WM_PAINT to be done in the parent first.
1486 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1488 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1490 if (from_child) /* skip all children until from_child is found */
1492 if (ptr == from_child) from_child = NULL;
1493 continue;
1495 if (!(ptr->style & WS_VISIBLE)) continue;
1496 if ((ret = get_update_flags( ptr, flags )) != 0)
1498 *child = ptr;
1499 break;
1501 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1503 return ret;
1506 /* iterate through children and siblings of the given window until we find one with some update flags */
1507 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1508 unsigned int flags, struct window **child )
1510 unsigned int ret;
1511 struct window *ptr, *from_sibling = NULL;
1513 /* if some parent is not visible start from the next sibling */
1515 if (!is_visible( win )) return 0;
1516 for (ptr = from_child; ptr; ptr = ptr->parent)
1518 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1519 if (ptr == win) break;
1522 /* non-client painting must be delayed if one of the parents is going to
1523 * be repainted and doesn't clip children */
1525 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1527 for (ptr = win->parent; ptr; ptr = ptr->parent)
1529 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1530 return 0;
1532 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1534 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1536 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1537 if (ptr == win) break;
1543 /* check window itself (only if not restarting from a child) */
1545 if (!from_child)
1547 if ((ret = get_update_flags( win, flags )))
1549 *child = win;
1550 return ret;
1552 from_child = win;
1555 /* now check children */
1557 if (flags & UPDATE_NOCHILDREN) return 0;
1558 if (!from_sibling)
1560 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1561 from_sibling = from_child;
1564 /* then check siblings and parent siblings */
1566 while (from_sibling->parent && from_sibling != win)
1568 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1569 return ret;
1570 from_sibling = from_sibling->parent;
1572 return 0;
1576 /* expose the areas revealed by a vis region change on the window parent */
1577 /* returns the region exposed on the window itself (in client coordinates) */
1578 static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
1579 struct region *old_vis_rgn )
1581 struct region *new_vis_rgn, *exposed_rgn;
1583 if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
1585 if ((exposed_rgn = create_empty_region()))
1587 if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
1589 /* make it relative to the new client area */
1590 offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
1591 win->window_rect.top - win->client_rect.top );
1593 else
1595 free_region( exposed_rgn );
1596 exposed_rgn = NULL;
1600 if (win->parent && !is_desktop_window( win->parent ))
1602 /* make it relative to the old window pos for subtracting */
1603 offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
1604 win->window_rect.top - old_window_rect->top );
1606 if ((win->parent->style & WS_CLIPCHILDREN) ?
1607 subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) :
1608 xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1610 if (!is_region_empty( new_vis_rgn ))
1612 /* make it relative to parent */
1613 offset_region( new_vis_rgn, old_window_rect->left, old_window_rect->top );
1614 redraw_window( win->parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1618 free_region( new_vis_rgn );
1619 return exposed_rgn;
1623 /* set the window and client rectangles, updating the update region if necessary */
1624 static void set_window_pos( struct window *win, struct window *previous,
1625 unsigned int swp_flags, const rectangle_t *window_rect,
1626 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1627 const rectangle_t *valid_rect )
1629 struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
1630 const rectangle_t old_window_rect = win->window_rect;
1631 const rectangle_t old_visible_rect = win->visible_rect;
1632 const rectangle_t old_client_rect = win->client_rect;
1633 rectangle_t rect;
1634 int client_changed, frame_changed;
1635 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1637 if (win->parent && !is_visible( win->parent )) visible = 0;
1639 if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
1641 /* set the new window info before invalidating anything */
1643 win->window_rect = *window_rect;
1644 win->visible_rect = *visible_rect;
1645 win->client_rect = *client_rect;
1646 if (!(swp_flags & SWP_NOZORDER) && win->parent) link_window( win, previous );
1647 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1648 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1650 /* keep children at the same position relative to top right corner when the parent is mirrored */
1651 if (win->ex_style & WS_EX_LAYOUTRTL)
1653 struct window *child;
1654 int old_size = old_client_rect.right - old_client_rect.left;
1655 int new_size = win->client_rect.right - win->client_rect.left;
1657 if (old_size != new_size) LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1659 offset_rect( &child->window_rect, new_size - old_size, 0 );
1660 offset_rect( &child->visible_rect, new_size - old_size, 0 );
1661 offset_rect( &child->client_rect, new_size - old_size, 0 );
1665 /* reset cursor clip rectangle when the desktop changes size */
1666 if (win == win->desktop->top_window) win->desktop->cursor.clip = *window_rect;
1668 /* if the window is not visible, everything is easy */
1669 if (!visible) return;
1671 /* expose anything revealed by the change */
1673 if (!(swp_flags & SWP_NOREDRAW))
1674 exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
1676 if (!(win->style & WS_VISIBLE))
1678 /* clear the update region since the window is no longer visible */
1679 validate_whole_window( win );
1680 validate_children( win );
1681 goto done;
1684 /* crop update region to the new window rect */
1686 if (win->update_region)
1688 if (get_window_visible_rect( win, &rect, 1 ))
1690 struct region *tmp = create_empty_region();
1691 if (tmp)
1693 set_region_rect( tmp, &rect );
1694 if (intersect_region( tmp, win->update_region, tmp ))
1695 set_update_region( win, tmp );
1696 else
1697 free_region( tmp );
1700 else set_update_region( win, NULL ); /* visible rect is empty */
1703 /* crop children regions to the new window rect */
1705 if (get_window_visible_rect( win, &rect, 0 ))
1707 /* map to client coords */
1708 offset_rect( &rect, win->window_rect.left - win->client_rect.left,
1709 win->window_rect.top - win->client_rect.top );
1710 crop_children_update_region( win, &rect );
1712 else crop_children_update_region( win, NULL );
1714 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1716 /* expose the whole non-client area if it changed in any way */
1718 if (swp_flags & SWP_NOCOPYBITS)
1720 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1721 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1722 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
1723 client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
1725 else
1727 /* assume the bits have been moved to follow the window rect */
1728 int x_offset = window_rect->left - old_window_rect.left;
1729 int y_offset = window_rect->top - old_window_rect.top;
1730 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1731 window_rect->right - old_window_rect.right != x_offset ||
1732 window_rect->bottom - old_window_rect.bottom != y_offset ||
1733 visible_rect->left - old_visible_rect.left != x_offset ||
1734 visible_rect->right - old_visible_rect.right != x_offset ||
1735 visible_rect->top - old_visible_rect.top != y_offset ||
1736 visible_rect->bottom - old_visible_rect.bottom != y_offset);
1737 client_changed = (client_rect->left - old_client_rect.left != x_offset ||
1738 client_rect->right - old_client_rect.right != x_offset ||
1739 client_rect->top - old_client_rect.top != y_offset ||
1740 client_rect->bottom - old_client_rect.bottom != y_offset ||
1741 !valid_rect || memcmp( valid_rect, client_rect, sizeof(*client_rect) ));
1744 if (frame_changed || client_changed)
1746 struct region *win_rgn = old_vis_rgn; /* reuse previous region */
1748 set_region_rect( win_rgn, window_rect );
1749 if (valid_rect)
1751 /* subtract the valid portion of client rect from the total region */
1752 struct region *tmp = create_empty_region();
1753 if (tmp)
1755 set_region_rect( tmp, valid_rect );
1756 /* subtract update region since invalid parts of the valid rect won't be copied */
1757 if (win->update_region)
1759 offset_region( tmp, -window_rect->left, -window_rect->top );
1760 subtract_region( tmp, tmp, win->update_region );
1761 offset_region( tmp, window_rect->left, window_rect->top );
1763 if (subtract_region( tmp, win_rgn, tmp )) win_rgn = tmp;
1764 else free_region( tmp );
1767 if (!is_desktop_window(win))
1768 offset_region( win_rgn, -client_rect->left, -client_rect->top );
1769 if (exposed_rgn)
1771 union_region( exposed_rgn, exposed_rgn, win_rgn );
1772 if (win_rgn != old_vis_rgn) free_region( win_rgn );
1774 else
1776 exposed_rgn = win_rgn;
1777 if (win_rgn == old_vis_rgn) old_vis_rgn = NULL;
1781 if (exposed_rgn)
1782 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1784 done:
1785 if (old_vis_rgn) free_region( old_vis_rgn );
1786 if (exposed_rgn) free_region( exposed_rgn );
1787 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1791 /* set the window region, updating the update region if necessary */
1792 static void set_window_region( struct window *win, struct region *region, int redraw )
1794 struct region *old_vis_rgn = NULL, *exposed_rgn;
1796 /* no need to redraw if window is not visible */
1797 if (redraw && !is_visible( win )) redraw = 0;
1799 if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
1801 if (win->win_region) free_region( win->win_region );
1802 win->win_region = region;
1804 /* expose anything revealed by the change */
1805 if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
1807 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1808 free_region( exposed_rgn );
1811 if (old_vis_rgn) free_region( old_vis_rgn );
1812 clear_error(); /* we ignore out of memory errors since the region has been set */
1816 /* destroy a window */
1817 void destroy_window( struct window *win )
1819 /* hide the window */
1820 if (is_visible(win))
1822 struct region *vis_rgn = get_visible_region( win, DCX_WINDOW );
1823 win->style &= ~WS_VISIBLE;
1824 if (vis_rgn)
1826 struct region *exposed_rgn = expose_window( win, &win->window_rect, vis_rgn );
1827 if (exposed_rgn) free_region( exposed_rgn );
1828 free_region( vis_rgn );
1830 validate_whole_window( win );
1831 validate_children( win );
1834 /* destroy all children */
1835 while (!list_empty(&win->children))
1836 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
1837 while (!list_empty(&win->unlinked))
1838 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
1840 /* reset global window pointers, if the corresponding window is destroyed */
1841 if (win == shell_window) shell_window = NULL;
1842 if (win == shell_listview) shell_listview = NULL;
1843 if (win == progman_window) progman_window = NULL;
1844 if (win == taskman_window) taskman_window = NULL;
1845 free_hotkeys( win->desktop, win->handle );
1846 cleanup_clipboard_window( win->desktop, win->handle );
1847 free_user_handle( win->handle );
1848 destroy_properties( win );
1849 list_remove( &win->entry );
1850 if (is_desktop_window(win))
1852 struct desktop *desktop = win->desktop;
1853 assert( desktop->top_window == win || desktop->msg_window == win );
1854 if (desktop->top_window == win) desktop->top_window = NULL;
1855 else desktop->msg_window = NULL;
1857 else if (is_desktop_window( win->parent ))
1859 post_message( win->parent->handle, WM_PARENTNOTIFY, WM_DESTROY, win->handle );
1862 detach_window_thread( win );
1863 if (win->win_region) free_region( win->win_region );
1864 if (win->update_region) free_region( win->update_region );
1865 if (win->class) release_class( win->class );
1866 free( win->text );
1867 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
1868 free( win );
1872 /* create a window */
1873 DECL_HANDLER(create_window)
1875 struct window *win, *parent = NULL, *owner = NULL;
1876 struct unicode_str cls_name = get_req_unicode_str();
1877 atom_t atom;
1879 reply->handle = 0;
1880 if (req->parent && !(parent = get_window( req->parent ))) return;
1882 if (req->owner)
1884 if (!(owner = get_window( req->owner ))) return;
1885 if (is_desktop_window(owner)) owner = NULL;
1886 else if (parent && !is_desktop_window(parent))
1888 /* an owned window must be created as top-level */
1889 set_error( STATUS_ACCESS_DENIED );
1890 return;
1892 else /* owner must be a top-level window */
1893 while ((owner->style & (WS_POPUP|WS_CHILD)) == WS_CHILD && !is_desktop_window(owner->parent))
1894 owner = owner->parent;
1897 atom = cls_name.len ? find_global_atom( NULL, &cls_name ) : req->atom;
1899 if (!(win = create_window( parent, owner, atom, req->instance ))) return;
1901 if (parent && !is_desktop_window( parent ))
1903 win->dpi_awareness = parent->dpi_awareness;
1904 win->dpi = parent->dpi;
1906 else if (!parent || req->awareness != DPI_AWARENESS_PER_MONITOR_AWARE)
1908 win->dpi_awareness = req->awareness;
1909 win->dpi = req->dpi;
1912 reply->handle = win->handle;
1913 reply->parent = win->parent ? win->parent->handle : 0;
1914 reply->owner = win->owner;
1915 reply->extra = win->nb_extra_bytes;
1916 reply->dpi = win->dpi;
1917 reply->awareness = win->dpi_awareness;
1918 reply->class_ptr = get_class_client_ptr( win->class );
1922 /* set the parent of a window */
1923 DECL_HANDLER(set_parent)
1925 struct window *win, *parent = NULL;
1927 if (!(win = get_window( req->handle ))) return;
1928 if (req->parent && !(parent = get_window( req->parent ))) return;
1930 if (is_desktop_window(win))
1932 set_error( STATUS_INVALID_PARAMETER );
1933 return;
1935 reply->old_parent = win->parent->handle;
1936 reply->full_parent = parent ? parent->handle : 0;
1937 set_parent_window( win, parent );
1938 reply->dpi = win->dpi;
1939 reply->awareness = win->dpi_awareness;
1943 /* destroy a window */
1944 DECL_HANDLER(destroy_window)
1946 struct window *win = get_window( req->handle );
1947 if (win)
1949 if (!is_desktop_window(win)) destroy_window( win );
1950 else if (win->thread == current) detach_window_thread( win );
1951 else set_error( STATUS_ACCESS_DENIED );
1956 /* retrieve the desktop window for the current thread */
1957 DECL_HANDLER(get_desktop_window)
1959 struct desktop *desktop = get_thread_desktop( current, 0 );
1960 int force;
1962 if (!desktop) return;
1964 /* if winstation is invisible, then avoid roundtrip */
1965 force = req->force || !(desktop->winstation->flags & WSF_VISIBLE);
1967 if (!desktop->top_window && force) /* create it */
1969 if ((desktop->top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
1971 detach_window_thread( desktop->top_window );
1972 desktop->top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1976 if (!desktop->msg_window && force) /* create it */
1978 static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
1979 static const struct unicode_str name = { messageW, sizeof(messageW) };
1980 atom_t atom = add_global_atom( NULL, &name );
1981 if (atom && (desktop->msg_window = create_window( NULL, NULL, atom, 0 )))
1983 detach_window_thread( desktop->msg_window );
1984 desktop->msg_window->style = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1988 reply->top_window = desktop->top_window ? desktop->top_window->handle : 0;
1989 reply->msg_window = desktop->msg_window ? desktop->msg_window->handle : 0;
1990 release_object( desktop );
1994 /* set a window owner */
1995 DECL_HANDLER(set_window_owner)
1997 struct window *win = get_window( req->handle );
1998 struct window *owner = NULL, *ptr;
2000 if (!win) return;
2001 if (req->owner && !(owner = get_window( req->owner ))) return;
2002 if (is_desktop_window(win))
2004 set_error( STATUS_ACCESS_DENIED );
2005 return;
2008 /* make sure owner is not a successor of window */
2009 for (ptr = owner; ptr; ptr = ptr->owner ? get_window( ptr->owner ) : NULL)
2011 if (ptr == win)
2013 set_error( STATUS_INVALID_PARAMETER );
2014 return;
2018 reply->prev_owner = win->owner;
2019 reply->full_owner = win->owner = owner ? owner->handle : 0;
2023 /* get information from a window handle */
2024 DECL_HANDLER(get_window_info)
2026 struct window *win = get_window( req->handle );
2028 if (!win) return;
2030 reply->full_handle = win->handle;
2031 reply->last_active = win->handle;
2032 reply->is_unicode = win->is_unicode;
2033 reply->awareness = win->dpi_awareness;
2034 reply->dpi = win->dpi ? win->dpi : get_monitor_dpi( win );
2035 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
2036 if (win->thread)
2038 reply->tid = get_thread_id( win->thread );
2039 reply->pid = get_process_id( win->thread->process );
2040 reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
2045 /* set some information in a window */
2046 DECL_HANDLER(set_window_info)
2048 struct window *win = get_window( req->handle );
2050 if (!win) return;
2051 if (req->flags && is_desktop_window(win) && win->thread != current)
2053 set_error( STATUS_ACCESS_DENIED );
2054 return;
2056 if (req->extra_size > sizeof(req->extra_value) ||
2057 req->extra_offset < -1 ||
2058 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
2060 set_win32_error( ERROR_INVALID_INDEX );
2061 return;
2063 if (req->extra_offset != -1)
2065 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
2067 else if (req->flags & SET_WIN_EXTRA)
2069 set_win32_error( ERROR_INVALID_INDEX );
2070 return;
2072 reply->old_style = win->style;
2073 reply->old_ex_style = win->ex_style;
2074 reply->old_id = win->id;
2075 reply->old_instance = win->instance;
2076 reply->old_user_data = win->user_data;
2077 if (req->flags & SET_WIN_STYLE) win->style = req->style;
2078 if (req->flags & SET_WIN_EXSTYLE)
2080 /* WS_EX_TOPMOST can only be changed for unlinked windows */
2081 if (!win->is_linked) win->ex_style = req->ex_style;
2082 else win->ex_style = (req->ex_style & ~WS_EX_TOPMOST) | (win->ex_style & WS_EX_TOPMOST);
2083 if (!(win->ex_style & WS_EX_LAYERED)) win->is_layered = 0;
2085 if (req->flags & SET_WIN_ID) win->id = req->id;
2086 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
2087 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
2088 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
2089 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
2090 &req->extra_value, req->extra_size );
2092 /* changing window style triggers a non-client paint */
2093 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
2097 /* get a list of the window parents, up to the root of the tree */
2098 DECL_HANDLER(get_window_parents)
2100 struct window *ptr, *win = get_window( req->handle );
2101 int total = 0;
2102 user_handle_t *data;
2103 data_size_t len;
2105 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
2107 reply->count = total;
2108 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2109 if (len && ((data = set_reply_data_size( len ))))
2111 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
2112 *data++ = ptr->handle;
2117 /* get a list of the window children */
2118 DECL_HANDLER(get_window_children)
2120 struct window *parent = NULL;
2121 unsigned int total;
2122 user_handle_t *data;
2123 data_size_t len;
2124 struct unicode_str cls_name = get_req_unicode_str();
2125 atom_t atom = req->atom;
2126 struct desktop *desktop = NULL;
2128 if (cls_name.len && !(atom = find_global_atom( NULL, &cls_name ))) return;
2130 if (req->desktop)
2132 if (!(desktop = get_desktop_obj( current->process, req->desktop, DESKTOP_ENUMERATE ))) return;
2133 parent = desktop->top_window;
2135 else
2137 if (req->parent && !(parent = get_window( req->parent ))) return;
2138 if (!parent && !(desktop = get_thread_desktop( current, 0 ))) return;
2141 if (parent)
2142 total = get_children_windows( parent, atom, req->tid, NULL, 0 );
2143 else
2144 total = get_children_windows( desktop->top_window, atom, req->tid, NULL, 0 ) +
2145 get_children_windows( desktop->msg_window, atom, req->tid, NULL, 0 );
2147 reply->count = total;
2148 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2149 if (len && ((data = set_reply_data_size( len ))))
2151 if (parent) get_children_windows( parent, atom, req->tid, data, len / sizeof(user_handle_t) );
2152 else
2154 total = get_children_windows( desktop->top_window, atom, req->tid,
2155 data, len / sizeof(user_handle_t) );
2156 data += total;
2157 len -= total * sizeof(user_handle_t);
2158 if (len >= sizeof(user_handle_t))
2159 get_children_windows( desktop->msg_window, atom, req->tid,
2160 data, len / sizeof(user_handle_t) );
2163 if (desktop) release_object( desktop );
2167 /* get a list of the window children that contain a given point */
2168 DECL_HANDLER(get_window_children_from_point)
2170 struct user_handle_array array;
2171 struct window *parent = get_window( req->parent );
2172 data_size_t len;
2174 if (!parent) return;
2176 array.handles = NULL;
2177 array.count = 0;
2178 array.total = 0;
2179 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
2181 reply->count = array.count;
2182 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
2183 if (len) set_reply_data_ptr( array.handles, len );
2184 else free( array.handles );
2188 /* get window tree information from a window handle */
2189 DECL_HANDLER(get_window_tree)
2191 struct window *ptr, *win = get_window( req->handle );
2193 if (!win) return;
2195 reply->parent = 0;
2196 reply->owner = 0;
2197 reply->next_sibling = 0;
2198 reply->prev_sibling = 0;
2199 reply->first_sibling = 0;
2200 reply->last_sibling = 0;
2201 reply->first_child = 0;
2202 reply->last_child = 0;
2204 if (win->parent)
2206 struct window *parent = win->parent;
2207 reply->parent = parent->handle;
2208 reply->owner = win->owner;
2209 if (win->is_linked)
2211 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
2212 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
2214 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
2215 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
2217 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
2218 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
2222 /* set the position and Z order of a window */
2223 DECL_HANDLER(set_window_pos)
2225 rectangle_t window_rect, client_rect, visible_rect;
2226 struct window *previous = NULL;
2227 struct window *top, *win = get_window( req->handle );
2228 unsigned int flags = req->swp_flags;
2230 if (!win) return;
2231 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
2233 if (!(flags & SWP_NOZORDER))
2235 switch ((int)req->previous)
2237 case 0: /* HWND_TOP */
2238 previous = WINPTR_TOP;
2239 break;
2240 case 1: /* HWND_BOTTOM */
2241 previous = WINPTR_BOTTOM;
2242 break;
2243 case -1: /* HWND_TOPMOST */
2244 previous = WINPTR_TOPMOST;
2245 break;
2246 case -2: /* HWND_NOTOPMOST */
2247 previous = WINPTR_NOTOPMOST;
2248 break;
2249 default:
2250 if (!(previous = get_window( req->previous ))) return;
2251 /* previous must be a sibling */
2252 if (previous->parent != win->parent)
2254 set_error( STATUS_INVALID_PARAMETER );
2255 return;
2257 break;
2259 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
2262 /* windows that use UpdateLayeredWindow don't trigger repaints */
2263 if ((win->ex_style & WS_EX_LAYERED) && !win->is_layered) flags |= SWP_NOREDRAW;
2265 /* window rectangle must be ordered properly */
2266 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
2268 set_error( STATUS_INVALID_PARAMETER );
2269 return;
2272 window_rect = visible_rect = req->window;
2273 client_rect = req->client;
2274 if (get_req_data_size() >= sizeof(rectangle_t))
2275 memcpy( &visible_rect, get_req_data(), sizeof(rectangle_t) );
2276 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2278 mirror_rect( &win->parent->client_rect, &window_rect );
2279 mirror_rect( &win->parent->client_rect, &visible_rect );
2280 mirror_rect( &win->parent->client_rect, &client_rect );
2283 win->paint_flags = (win->paint_flags & ~PAINT_CLIENT_FLAGS) | (req->paint_flags & PAINT_CLIENT_FLAGS);
2284 if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
2286 if (get_req_data_size() >= 2 * sizeof(rectangle_t))
2288 rectangle_t valid_rect;
2289 memcpy( &valid_rect, (const rectangle_t *)get_req_data() + 1, sizeof(rectangle_t) );
2290 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2291 mirror_rect( &win->parent->client_rect, &valid_rect );
2292 set_window_pos( win, previous, flags, &window_rect, &client_rect, &visible_rect, &valid_rect );
2294 else set_window_pos( win, previous, flags, &window_rect, &client_rect, &visible_rect, NULL );
2296 reply->new_style = win->style;
2297 reply->new_ex_style = win->ex_style;
2299 top = get_top_clipping_window( win );
2300 if (is_visible( top ) && (top->paint_flags & PAINT_HAS_SURFACE))
2302 reply->surface_win = top->handle;
2303 reply->needs_update = !!(top->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD));
2308 /* get the window and client rectangles of a window */
2309 DECL_HANDLER(get_window_rectangles)
2311 struct window *win = get_window( req->handle );
2313 if (!win) return;
2315 reply->window = win->window_rect;
2316 reply->client = win->client_rect;
2318 switch (req->relative)
2320 case COORDS_CLIENT:
2321 offset_rect( &reply->window, -win->client_rect.left, -win->client_rect.top );
2322 offset_rect( &reply->client, -win->client_rect.left, -win->client_rect.top );
2323 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &reply->window );
2324 break;
2325 case COORDS_WINDOW:
2326 offset_rect( &reply->window, -win->window_rect.left, -win->window_rect.top );
2327 offset_rect( &reply->client, -win->window_rect.left, -win->window_rect.top );
2328 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->window_rect, &reply->client );
2329 break;
2330 case COORDS_PARENT:
2331 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2333 mirror_rect( &win->parent->client_rect, &reply->window );
2334 mirror_rect( &win->parent->client_rect, &reply->client );
2336 break;
2337 case COORDS_SCREEN:
2338 client_to_screen_rect( win->parent, &reply->window );
2339 client_to_screen_rect( win->parent, &reply->client );
2340 break;
2341 default:
2342 set_error( STATUS_INVALID_PARAMETER );
2343 break;
2348 /* get the window text */
2349 DECL_HANDLER(get_window_text)
2351 struct window *win = get_window( req->handle );
2353 if (win && win->text)
2355 reply->length = strlenW( win->text );
2356 set_reply_data( win->text, min( reply->length * sizeof(WCHAR), get_reply_max_size() ));
2361 /* set the window text */
2362 DECL_HANDLER(set_window_text)
2364 struct window *win = get_window( req->handle );
2366 if (win)
2368 WCHAR *text = NULL;
2369 data_size_t len = get_req_data_size() / sizeof(WCHAR);
2370 if (len)
2372 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
2373 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
2374 text[len] = 0;
2376 free( win->text );
2377 win->text = text;
2382 /* get the coordinates offset between two windows */
2383 DECL_HANDLER(get_windows_offset)
2385 struct window *win;
2386 int mirror_from = 0, mirror_to = 0;
2388 reply->x = reply->y = 0;
2389 if (req->from)
2391 if (!(win = get_window( req->from ))) return;
2392 if (win->ex_style & WS_EX_LAYOUTRTL)
2394 mirror_from = 1;
2395 reply->x += win->client_rect.right - win->client_rect.left;
2397 while (win && !is_desktop_window(win))
2399 reply->x += win->client_rect.left;
2400 reply->y += win->client_rect.top;
2401 win = win->parent;
2404 if (req->to)
2406 if (!(win = get_window( req->to ))) return;
2407 if (win->ex_style & WS_EX_LAYOUTRTL)
2409 mirror_to = 1;
2410 reply->x -= win->client_rect.right - win->client_rect.left;
2412 while (win && !is_desktop_window(win))
2414 reply->x -= win->client_rect.left;
2415 reply->y -= win->client_rect.top;
2416 win = win->parent;
2419 if (mirror_from) reply->x = -reply->x;
2420 reply->mirror = mirror_from ^ mirror_to;
2424 /* get the visible region of a window */
2425 DECL_HANDLER(get_visible_region)
2427 struct region *region;
2428 struct window *top, *win = get_window( req->window );
2430 if (!win) return;
2432 top = get_top_clipping_window( win );
2433 if ((region = get_visible_region( win, req->flags )))
2435 rectangle_t *data;
2436 map_win_region_to_screen( win, region );
2437 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2438 if (data) set_reply_data_ptr( data, reply->total_size );
2440 reply->top_win = top->handle;
2441 reply->top_rect = top->visible_rect;
2443 if (!is_desktop_window(win))
2445 reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
2446 client_to_screen_rect( top->parent, &reply->top_rect );
2447 client_to_screen_rect( win->parent, &reply->win_rect );
2449 else
2451 reply->win_rect.left = 0;
2452 reply->win_rect.top = 0;
2453 reply->win_rect.right = win->client_rect.right - win->client_rect.left;
2454 reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
2456 reply->paint_flags = win->paint_flags & PAINT_CLIENT_FLAGS;
2460 /* get the surface visible region of a window */
2461 DECL_HANDLER(get_surface_region)
2463 struct region *region;
2464 struct window *win = get_window( req->window );
2466 if (!win || !is_visible( win )) return;
2468 if ((region = get_surface_region( win )))
2470 rectangle_t *data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2471 if (data) set_reply_data_ptr( data, reply->total_size );
2473 reply->visible_rect = win->visible_rect;
2477 /* get the window region */
2478 DECL_HANDLER(get_window_region)
2480 rectangle_t *data;
2481 struct window *win = get_window( req->window );
2483 if (!win) return;
2484 if (!win->win_region) return;
2486 if (win->ex_style & WS_EX_LAYOUTRTL)
2488 struct region *region = create_empty_region();
2490 if (!region) return;
2491 if (!copy_region( region, win->win_region ))
2493 free_region( region );
2494 return;
2496 mirror_region( &win->window_rect, region );
2497 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2499 else data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
2501 if (data) set_reply_data_ptr( data, reply->total_size );
2505 /* set the window region */
2506 DECL_HANDLER(set_window_region)
2508 struct region *region = NULL;
2509 struct window *win = get_window( req->window );
2511 if (!win) return;
2513 if (get_req_data_size()) /* no data means remove the region completely */
2515 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2516 return;
2517 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->window_rect, region );
2519 set_window_region( win, region, req->redraw );
2523 /* get a window update region */
2524 DECL_HANDLER(get_update_region)
2526 rectangle_t *data;
2527 unsigned int flags = req->flags;
2528 struct window *from_child = NULL;
2529 struct window *win = get_window( req->window );
2531 reply->flags = 0;
2532 if (!win) return;
2534 if (req->from_child)
2536 struct window *ptr;
2538 if (!(from_child = get_window( req->from_child ))) return;
2540 /* make sure from_child is a child of win */
2541 ptr = from_child;
2542 while (ptr && ptr != win) ptr = ptr->parent;
2543 if (!ptr)
2545 set_error( STATUS_INVALID_PARAMETER );
2546 return;
2550 if (flags & UPDATE_DELAYED_ERASE) /* this means that the previous call didn't erase */
2552 if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
2553 else win->paint_flags |= PAINT_DELAYED_ERASE;
2556 reply->flags = get_window_update_flags( win, from_child, flags, &win );
2557 reply->child = win->handle;
2559 if (flags & UPDATE_NOREGION) return;
2561 if (win->update_region)
2563 /* convert update region to screen coordinates */
2564 struct region *region = create_empty_region();
2566 if (!region) return;
2567 if (!copy_region( region, win->update_region ))
2569 free_region( region );
2570 return;
2572 if ((flags & UPDATE_CLIPCHILDREN) && (win->style & WS_CLIPCHILDREN))
2573 clip_children( win, NULL, region, win->client_rect.left - win->window_rect.left,
2574 win->client_rect.top - win->window_rect.top );
2575 map_win_region_to_screen( win, region );
2576 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
2577 &reply->total_size ))) return;
2578 set_reply_data_ptr( data, reply->total_size );
2581 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
2583 validate_parents( win );
2584 validate_whole_window( win );
2586 else
2588 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
2589 if (reply->flags & UPDATE_ERASE)
2591 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
2592 /* desktop window only gets erased, not repainted */
2593 if (is_desktop_window(win)) validate_whole_window( win );
2599 /* update the z order of a window so that a given rectangle is fully visible */
2600 DECL_HANDLER(update_window_zorder)
2602 rectangle_t tmp, rect = req->rect;
2603 struct window *ptr, *win = get_window( req->window );
2605 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
2606 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &rect );
2607 offset_rect( &rect, win->client_rect.left, win->client_rect.top );
2609 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2611 if (ptr == win) break;
2612 if (!(ptr->style & WS_VISIBLE)) continue;
2613 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2614 if (ptr->is_layered && (ptr->layered_flags & LWA_COLORKEY)) continue;
2615 if (!intersect_rect( &tmp, &ptr->visible_rect, &rect )) continue;
2616 if (ptr->win_region)
2618 tmp = rect;
2619 offset_rect( &tmp, -ptr->window_rect.left, -ptr->window_rect.top );
2620 if (!rect_in_region( ptr->win_region, &tmp )) continue;
2622 /* found a window obscuring the rectangle, now move win above this one */
2623 /* making sure to not violate the topmost rule */
2624 if (!(ptr->ex_style & WS_EX_TOPMOST) || (win->ex_style & WS_EX_TOPMOST))
2626 list_remove( &win->entry );
2627 list_add_before( &ptr->entry, &win->entry );
2629 break;
2634 /* mark parts of a window as needing a redraw */
2635 DECL_HANDLER(redraw_window)
2637 struct region *region = NULL;
2638 struct window *win = get_window( req->window );
2640 if (!win) return;
2641 if (!is_visible( win )) return; /* nothing to do */
2643 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
2645 if (get_req_data_size()) /* no data means whole rectangle */
2647 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2648 return;
2649 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->client_rect, region );
2653 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
2654 req->flags );
2655 if (region) free_region( region );
2659 /* set a window property */
2660 DECL_HANDLER(set_window_property)
2662 struct unicode_str name = get_req_unicode_str();
2663 struct window *win = get_window( req->window );
2665 if (!win) return;
2667 if (name.len)
2669 atom_t atom = add_global_atom( NULL, &name );
2670 if (atom)
2672 set_property( win, atom, req->data, PROP_TYPE_STRING );
2673 release_global_atom( NULL, atom );
2676 else set_property( win, req->atom, req->data, PROP_TYPE_ATOM );
2680 /* remove a window property */
2681 DECL_HANDLER(remove_window_property)
2683 struct unicode_str name = get_req_unicode_str();
2684 struct window *win = get_window( req->window );
2686 if (win)
2688 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2689 if (atom) reply->data = remove_property( win, atom );
2694 /* get a window property */
2695 DECL_HANDLER(get_window_property)
2697 struct unicode_str name = get_req_unicode_str();
2698 struct window *win = get_window( req->window );
2700 if (win)
2702 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2703 if (atom) reply->data = get_property( win, atom );
2708 /* get the list of properties of a window */
2709 DECL_HANDLER(get_window_properties)
2711 property_data_t *data;
2712 int i, count, max = get_reply_max_size() / sizeof(*data);
2713 struct window *win = get_window( req->window );
2715 reply->total = 0;
2716 if (!win) return;
2718 for (i = count = 0; i < win->prop_inuse; i++)
2719 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2720 reply->total = count;
2722 if (count > max) count = max;
2723 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2725 for (i = 0; i < win->prop_inuse && count; i++)
2727 if (win->properties[i].type == PROP_TYPE_FREE) continue;
2728 data->atom = win->properties[i].atom;
2729 data->string = (win->properties[i].type == PROP_TYPE_STRING);
2730 data->data = win->properties[i].data;
2731 data++;
2732 count--;
2737 /* get the new window pointer for a global window, checking permissions */
2738 /* helper for set_global_windows request */
2739 static int get_new_global_window( struct window **win, user_handle_t handle )
2741 if (!handle)
2743 *win = NULL;
2744 return 1;
2746 else if (*win)
2748 set_error( STATUS_ACCESS_DENIED );
2749 return 0;
2751 *win = get_window( handle );
2752 return (*win != NULL);
2755 /* Set/get the global windows */
2756 DECL_HANDLER(set_global_windows)
2758 struct window *new_shell_window = shell_window;
2759 struct window *new_shell_listview = shell_listview;
2760 struct window *new_progman_window = progman_window;
2761 struct window *new_taskman_window = taskman_window;
2763 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2764 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2765 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2766 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2768 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2770 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2771 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2773 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2775 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2777 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2779 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2781 shell_window = new_shell_window;
2782 shell_listview = new_shell_listview;
2783 progman_window = new_progman_window;
2784 taskman_window = new_taskman_window;
2787 /* retrieve layered info for a window */
2788 DECL_HANDLER(get_window_layered_info)
2790 struct window *win = get_window( req->handle );
2792 if (!win) return;
2794 if (win->is_layered)
2796 reply->color_key = win->color_key;
2797 reply->alpha = win->alpha;
2798 reply->flags = win->layered_flags;
2800 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2804 /* set layered info for a window */
2805 DECL_HANDLER(set_window_layered_info)
2807 struct window *win = get_window( req->handle );
2809 if (!win) return;
2811 if (win->ex_style & WS_EX_LAYERED)
2813 int was_layered = win->is_layered;
2815 if (req->flags & LWA_ALPHA) win->alpha = req->alpha;
2816 else if (!win->is_layered) win->alpha = 0; /* alpha init value is 0 */
2818 win->color_key = req->color_key;
2819 win->layered_flags = req->flags;
2820 win->is_layered = 1;
2821 /* repaint since we know now it's not going to use UpdateLayeredWindow */
2822 if (!was_layered) redraw_window( win, 0, 1, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
2824 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );