user32: Don't copy window bits to or from the dummy surface.
[wine.git] / server / window.c
blob70812095d25f48b453ce791fca1fb0f8396db416
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 /* global window pointers */
118 static struct window *shell_window;
119 static struct window *shell_listview;
120 static struct window *progman_window;
121 static struct window *taskman_window;
123 /* magic HWND_TOP etc. pointers */
124 #define WINPTR_TOP ((struct window *)1L)
125 #define WINPTR_BOTTOM ((struct window *)2L)
126 #define WINPTR_TOPMOST ((struct window *)3L)
127 #define WINPTR_NOTOPMOST ((struct window *)4L)
129 /* retrieve a pointer to a window from its handle */
130 static inline struct window *get_window( user_handle_t handle )
132 struct window *ret = get_user_object( handle, USER_WINDOW );
133 if (!ret) set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
134 return ret;
137 /* check if window is the desktop */
138 static inline int is_desktop_window( const struct window *win )
140 return !win->parent; /* only desktop windows have no parent */
143 /* get next window in Z-order list */
144 static inline struct window *get_next_window( struct window *win )
146 struct list *ptr = list_next( &win->parent->children, &win->entry );
147 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
150 /* get previous window in Z-order list */
151 static inline struct window *get_prev_window( struct window *win )
153 struct list *ptr = list_prev( &win->parent->children, &win->entry );
154 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
157 /* get first child in Z-order list */
158 static inline struct window *get_first_child( struct window *win )
160 struct list *ptr = list_head( &win->children );
161 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
164 /* get last child in Z-order list */
165 static inline struct window *get_last_child( struct window *win )
167 struct list *ptr = list_tail( &win->children );
168 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
171 /* set the PAINT_PIXEL_FORMAT_CHILD flag on all the parents */
172 /* note: we never reset the flag, it's just a heuristic */
173 static inline void update_pixel_format_flags( struct window *win )
175 for (win = win->parent; win && win->parent; win = win->parent)
176 win->paint_flags |= PAINT_PIXEL_FORMAT_CHILD;
179 /* get the per-monitor DPI for a window */
180 static unsigned int get_monitor_dpi( struct window *win )
182 /* FIXME: we return the desktop window DPI for now */
183 while (!is_desktop_window( win )) win = win->parent;
184 return win->dpi ? win->dpi : USER_DEFAULT_SCREEN_DPI;
187 /* link a window at the right place in the siblings list */
188 static void link_window( struct window *win, struct window *previous )
190 if (previous == WINPTR_NOTOPMOST)
192 if (!(win->ex_style & WS_EX_TOPMOST) && win->is_linked) return; /* nothing to do */
193 win->ex_style &= ~WS_EX_TOPMOST;
194 previous = WINPTR_TOP; /* fallback to the HWND_TOP case */
197 list_remove( &win->entry ); /* unlink it from the previous location */
199 if (previous == WINPTR_BOTTOM)
201 list_add_tail( &win->parent->children, &win->entry );
202 win->ex_style &= ~WS_EX_TOPMOST;
204 else if (previous == WINPTR_TOPMOST)
206 list_add_head( &win->parent->children, &win->entry );
207 win->ex_style |= WS_EX_TOPMOST;
209 else if (previous == WINPTR_TOP)
211 struct list *entry = win->parent->children.next;
212 if (!(win->ex_style & WS_EX_TOPMOST)) /* put it above the first non-topmost window */
214 while (entry != &win->parent->children)
216 struct window *next = LIST_ENTRY( entry, struct window, entry );
217 if (!(next->ex_style & WS_EX_TOPMOST)) break;
218 if (next->handle == win->owner) /* keep it above owner */
220 win->ex_style |= WS_EX_TOPMOST;
221 break;
223 entry = entry->next;
226 list_add_before( entry, &win->entry );
228 else
230 list_add_after( &previous->entry, &win->entry );
231 if (!(previous->ex_style & WS_EX_TOPMOST)) win->ex_style &= ~WS_EX_TOPMOST;
232 else
234 struct window *next = get_next_window( win );
235 if (next && (next->ex_style & WS_EX_TOPMOST)) win->ex_style |= WS_EX_TOPMOST;
239 win->is_linked = 1;
242 /* change the parent of a window (or unlink the window if the new parent is NULL) */
243 static int set_parent_window( struct window *win, struct window *parent )
245 struct window *ptr;
247 /* make sure parent is not a child of window */
248 for (ptr = parent; ptr; ptr = ptr->parent)
250 if (ptr == win)
252 set_error( STATUS_INVALID_PARAMETER );
253 return 0;
257 if (parent)
259 win->parent = parent;
260 link_window( win, WINPTR_TOP );
262 if (!is_desktop_window( parent ))
264 win->dpi = parent->dpi;
265 win->dpi_awareness = parent->dpi_awareness;
268 /* if parent belongs to a different thread and the window isn't */
269 /* top-level, attach the two threads */
270 if (parent->thread && parent->thread != win->thread && !is_desktop_window(parent))
271 attach_thread_input( win->thread, parent->thread );
273 if (win->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))
274 update_pixel_format_flags( win );
276 else /* move it to parent unlinked list */
278 list_remove( &win->entry ); /* unlink it from the previous location */
279 list_add_head( &win->parent->unlinked, &win->entry );
280 win->is_linked = 0;
282 return 1;
285 /* append a user handle to a handle array */
286 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
288 if (array->count >= array->total)
290 int new_total = max( array->total * 2, 32 );
291 user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
292 if (!new_array)
294 free( array->handles );
295 set_error( STATUS_NO_MEMORY );
296 return 0;
298 array->handles = new_array;
299 array->total = new_total;
301 array->handles[array->count++] = handle;
302 return 1;
305 /* set a window property */
306 static void set_property( struct window *win, atom_t atom, lparam_t data, enum property_type type )
308 int i, free = -1;
309 struct property *new_props;
311 /* check if it exists already */
312 for (i = 0; i < win->prop_inuse; i++)
314 if (win->properties[i].type == PROP_TYPE_FREE)
316 free = i;
317 continue;
319 if (win->properties[i].atom == atom)
321 win->properties[i].type = type;
322 win->properties[i].data = data;
323 return;
327 /* need to add an entry */
328 if (!grab_global_atom( NULL, atom )) return;
329 if (free == -1)
331 /* no free entry */
332 if (win->prop_inuse >= win->prop_alloc)
334 /* need to grow the array */
335 if (!(new_props = realloc( win->properties,
336 sizeof(*new_props) * (win->prop_alloc + 16) )))
338 set_error( STATUS_NO_MEMORY );
339 release_global_atom( NULL, atom );
340 return;
342 win->prop_alloc += 16;
343 win->properties = new_props;
345 free = win->prop_inuse++;
347 win->properties[free].atom = atom;
348 win->properties[free].type = type;
349 win->properties[free].data = data;
352 /* remove a window property */
353 static lparam_t remove_property( struct window *win, atom_t atom )
355 int i;
357 for (i = 0; i < win->prop_inuse; i++)
359 if (win->properties[i].type == PROP_TYPE_FREE) continue;
360 if (win->properties[i].atom == atom)
362 release_global_atom( NULL, atom );
363 win->properties[i].type = PROP_TYPE_FREE;
364 return win->properties[i].data;
367 /* FIXME: last error? */
368 return 0;
371 /* find a window property */
372 static lparam_t get_property( struct window *win, atom_t atom )
374 int i;
376 for (i = 0; i < win->prop_inuse; i++)
378 if (win->properties[i].type == PROP_TYPE_FREE) continue;
379 if (win->properties[i].atom == atom) return win->properties[i].data;
381 /* FIXME: last error? */
382 return 0;
385 /* destroy all properties of a window */
386 static inline void destroy_properties( struct window *win )
388 int i;
390 if (!win->properties) return;
391 for (i = 0; i < win->prop_inuse; i++)
393 if (win->properties[i].type == PROP_TYPE_FREE) continue;
394 release_global_atom( NULL, win->properties[i].atom );
396 free( win->properties );
399 /* detach a window from its owner thread but keep the window around */
400 static void detach_window_thread( struct window *win )
402 struct thread *thread = win->thread;
404 if (!thread) return;
405 if (thread->queue)
407 if (win->update_region) inc_queue_paint_count( thread, -1 );
408 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
409 queue_cleanup_window( thread, win->handle );
411 assert( thread->desktop_users > 0 );
412 thread->desktop_users--;
413 release_class( win->class );
414 win->class = NULL;
416 /* don't hold a reference to the desktop so that the desktop window can be */
417 /* destroyed when the desktop ref count reaches zero */
418 release_object( win->desktop );
419 win->thread = NULL;
422 /* get the process owning the top window of a given desktop */
423 struct process *get_top_window_owner( struct desktop *desktop )
425 struct window *win = desktop->top_window;
426 if (!win || !win->thread) return NULL;
427 return win->thread->process;
430 /* get the top window size of a given desktop */
431 void get_top_window_rectangle( struct desktop *desktop, rectangle_t *rect )
433 struct window *win = desktop->top_window;
434 if (!win) rect->left = rect->top = rect->right = rect->bottom = 0;
435 else *rect = win->window_rect;
438 /* post a message to the desktop window */
439 void post_desktop_message( struct desktop *desktop, unsigned int message,
440 lparam_t wparam, lparam_t lparam )
442 struct window *win = desktop->top_window;
443 if (win && win->thread) post_message( win->handle, message, wparam, lparam );
446 /* create a new window structure (note: the window is not linked in the window tree) */
447 static struct window *create_window( struct window *parent, struct window *owner,
448 atom_t atom, mod_handle_t instance )
450 static const rectangle_t empty_rect;
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 (x < win->visible_rect.left || x >= win->visible_rect.right ||
669 y < win->visible_rect.top || y >= win->visible_rect.bottom)
670 return 0; /* not in window */
671 if (win->win_region &&
672 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
673 return 0; /* not in window region */
674 return 1;
677 /* fill an array with the handles of the children of a specified window */
678 static unsigned int get_children_windows( struct window *parent, atom_t atom, thread_id_t tid,
679 user_handle_t *handles, unsigned int max_count )
681 struct window *ptr;
682 unsigned int count = 0;
684 if (!parent) return 0;
686 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
688 if (atom && get_class_atom(ptr->class) != atom) continue;
689 if (tid && get_thread_id(ptr->thread) != tid) continue;
690 if (handles)
692 if (count >= max_count) break;
693 handles[count] = ptr->handle;
695 count++;
697 return count;
700 /* find child of 'parent' that contains the given point (in parent-relative coords) */
701 static struct window *child_window_from_point( struct window *parent, int x, int y )
703 struct window *ptr;
705 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
707 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
709 /* if window is minimized or disabled, return at once */
710 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
712 /* if point is not in client area, return at once */
713 if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
714 y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
715 return ptr;
717 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
719 return parent; /* not found any child */
722 /* find all children of 'parent' that contain the given point */
723 static int get_window_children_from_point( struct window *parent, int x, int y,
724 struct user_handle_array *array )
726 struct window *ptr;
728 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
730 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
732 /* if point is in client area, and window is not minimized or disabled, check children */
733 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
734 x >= ptr->client_rect.left && x < ptr->client_rect.right &&
735 y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
737 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
738 y - ptr->client_rect.top, array ))
739 return 0;
742 /* now add window to the array */
743 if (!add_handle_to_array( array, ptr->handle )) return 0;
745 return 1;
748 /* get handle of root of top-most window containing point */
749 user_handle_t shallow_window_from_point( struct desktop *desktop, int x, int y )
751 struct window *ptr;
753 if (!desktop->top_window) return 0;
755 LIST_FOR_EACH_ENTRY( ptr, &desktop->top_window->children, struct window, entry )
757 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
758 return ptr->handle;
760 return desktop->top_window->handle;
763 /* return thread of top-most window containing point (in absolute coords) */
764 struct thread *window_thread_from_point( user_handle_t scope, int x, int y )
766 struct window *win = get_user_object( scope, USER_WINDOW );
767 struct window *ptr;
769 if (!win) return NULL;
771 for (ptr = win; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
773 x -= ptr->client_rect.left;
774 y -= ptr->client_rect.top;
777 ptr = child_window_from_point( win, x, y );
778 if (!ptr->thread) return NULL;
779 return (struct thread *)grab_object( ptr->thread );
782 /* return list of all windows containing point (in absolute coords) */
783 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
785 struct window *ptr;
787 /* make point relative to top window */
788 for (ptr = top->parent; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
790 x -= ptr->client_rect.left;
791 y -= ptr->client_rect.top;
794 if (!is_point_in_window( top, x, y )) return 1;
796 /* if point is in client area, and window is not minimized or disabled, check children */
797 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
798 x >= top->client_rect.left && x < top->client_rect.right &&
799 y >= top->client_rect.top && y < top->client_rect.bottom)
801 if (!is_desktop_window(top))
803 x -= top->client_rect.left;
804 y -= top->client_rect.top;
806 if (!get_window_children_from_point( top, x, y, array )) return 0;
808 /* now add window to the array */
809 if (!add_handle_to_array( array, top->handle )) return 0;
810 return 1;
814 /* return the thread owning a window */
815 struct thread *get_window_thread( user_handle_t handle )
817 struct window *win = get_user_object( handle, USER_WINDOW );
818 if (!win || !win->thread) return NULL;
819 return (struct thread *)grab_object( win->thread );
823 /* check if any area of a window needs repainting */
824 static inline int win_needs_repaint( struct window *win )
826 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
830 /* find a child of the specified window that needs repainting */
831 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
833 struct window *ptr, *ret = NULL;
835 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
837 if (!(ptr->style & WS_VISIBLE)) continue;
838 if (ptr->thread == thread && win_needs_repaint( ptr ))
839 ret = ptr;
840 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
841 ret = find_child_to_repaint( ptr, thread );
842 if (ret) break;
845 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
847 /* transparent window, check for non-transparent sibling to paint first */
848 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
850 if (!(ptr->style & WS_VISIBLE)) continue;
851 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
852 if (ptr->thread != thread) continue;
853 if (win_needs_repaint( ptr )) return ptr;
856 return ret;
860 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
861 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
863 struct window *ptr, *win, *top_window = get_desktop_window( thread );
865 if (!top_window) return 0;
867 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
868 else win = find_child_to_repaint( top_window, thread );
870 if (win && parent)
872 /* check that it is a child of the specified parent */
873 for (ptr = win; ptr; ptr = ptr->parent)
874 if (ptr->handle == parent) break;
875 /* otherwise don't return any window, we don't repaint a child before its parent */
876 if (!ptr) win = NULL;
878 if (!win) return 0;
879 win->paint_flags &= ~PAINT_INTERNAL;
880 return win->handle;
884 /* intersect the window region with the specified region, relative to the window parent */
885 static struct region *intersect_window_region( struct region *region, struct window *win )
887 /* make region relative to window rect */
888 offset_region( region, -win->window_rect.left, -win->window_rect.top );
889 if (!intersect_region( region, region, win->win_region )) return NULL;
890 /* make region relative to parent again */
891 offset_region( region, win->window_rect.left, win->window_rect.top );
892 return region;
896 /* convert coordinates from client to screen coords */
897 static inline void client_to_screen( struct window *win, int *x, int *y )
899 for ( ; win && !is_desktop_window(win); win = win->parent)
901 *x += win->client_rect.left;
902 *y += win->client_rect.top;
906 /* convert coordinates from client to screen coords */
907 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
909 for ( ; win && !is_desktop_window(win); win = win->parent)
911 rect->left += win->client_rect.left;
912 rect->right += win->client_rect.left;
913 rect->top += win->client_rect.top;
914 rect->bottom += win->client_rect.top;
918 /* map the region from window to screen coordinates */
919 static inline void map_win_region_to_screen( struct window *win, struct region *region )
921 if (!is_desktop_window(win))
923 int x = win->window_rect.left;
924 int y = win->window_rect.top;
925 client_to_screen( win->parent, &x, &y );
926 offset_region( region, x, y );
931 /* clip all children of a given window out of the visible region */
932 static struct region *clip_children( struct window *parent, struct window *last,
933 struct region *region, int offset_x, int offset_y )
935 struct window *ptr;
936 struct region *tmp = create_empty_region();
938 if (!tmp) return NULL;
939 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
941 if (ptr == last) break;
942 if (!(ptr->style & WS_VISIBLE)) continue;
943 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
944 set_region_rect( tmp, &ptr->visible_rect );
945 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
947 free_region( tmp );
948 return NULL;
950 offset_region( tmp, offset_x, offset_y );
951 if (!(region = subtract_region( region, region, tmp ))) break;
952 if (is_region_empty( region )) break;
954 free_region( tmp );
955 return region;
959 /* offset the coordinates of a rectangle */
960 static inline void offset_rect( rectangle_t *rect, int offset_x, int offset_y )
962 rect->left += offset_x;
963 rect->top += offset_y;
964 rect->right += offset_x;
965 rect->bottom += offset_y;
969 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
970 static void set_region_client_rect( struct region *region, struct window *win )
972 rectangle_t rect;
974 intersect_rect( &rect, &win->window_rect, &win->client_rect );
975 set_region_rect( region, &rect );
979 /* get the top-level window to clip against for a given window */
980 static inline struct window *get_top_clipping_window( struct window *win )
982 while (!(win->paint_flags & PAINT_HAS_SURFACE) && win->parent && !is_desktop_window(win->parent))
983 win = win->parent;
984 return win;
988 /* compute the visible region of a window, in window coordinates */
989 static struct region *get_visible_region( struct window *win, unsigned int flags )
991 struct region *tmp = NULL, *region;
992 int offset_x, offset_y;
994 if (!(region = create_empty_region())) return NULL;
996 /* first check if all ancestors are visible */
998 if (!is_visible( win )) return region; /* empty region */
1000 /* create a region relative to the window itself */
1002 if ((flags & DCX_PARENTCLIP) && win->parent && !is_desktop_window(win->parent))
1004 set_region_client_rect( region, win->parent );
1005 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
1007 else if (flags & DCX_WINDOW)
1009 set_region_rect( region, &win->visible_rect );
1010 if (win->win_region && !intersect_window_region( region, win )) goto error;
1012 else
1014 set_region_client_rect( region, win );
1015 if (win->win_region && !intersect_window_region( region, win )) goto error;
1018 /* clip children */
1020 if (flags & DCX_CLIPCHILDREN)
1022 if (is_desktop_window(win)) offset_x = offset_y = 0;
1023 else
1025 offset_x = win->client_rect.left;
1026 offset_y = win->client_rect.top;
1028 if (!clip_children( win, NULL, region, offset_x, offset_y )) goto error;
1031 /* clip siblings of ancestors */
1033 if (is_desktop_window(win)) offset_x = offset_y = 0;
1034 else
1036 offset_x = win->window_rect.left;
1037 offset_y = win->window_rect.top;
1040 if ((tmp = create_empty_region()) != NULL)
1042 while (win->parent)
1044 /* we don't clip out top-level siblings as that's up to the native windowing system */
1045 if ((win->style & WS_CLIPSIBLINGS) && !is_desktop_window( win->parent ))
1047 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
1048 if (is_region_empty( region )) break;
1050 /* clip to parent client area */
1051 win = win->parent;
1052 if (!is_desktop_window(win))
1054 offset_x += win->client_rect.left;
1055 offset_y += win->client_rect.top;
1056 offset_region( region, win->client_rect.left, win->client_rect.top );
1058 set_region_client_rect( tmp, win );
1059 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
1060 if (!intersect_region( region, region, tmp )) goto error;
1061 if (is_region_empty( region )) break;
1063 free_region( tmp );
1065 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
1066 return region;
1068 error:
1069 if (tmp) free_region( tmp );
1070 free_region( region );
1071 return NULL;
1075 /* clip all children with a custom pixel format out of the visible region */
1076 static struct region *clip_pixel_format_children( struct window *parent, struct region *parent_clip,
1077 struct region *region, int offset_x, int offset_y )
1079 struct window *ptr;
1080 struct region *clip = create_empty_region();
1082 if (!clip) return NULL;
1084 LIST_FOR_EACH_ENTRY_REV( ptr, &parent->children, struct window, entry )
1086 if (!(ptr->style & WS_VISIBLE)) continue;
1087 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1089 /* add the visible rect */
1090 set_region_rect( clip, &ptr->visible_rect );
1091 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1092 offset_region( clip, offset_x, offset_y );
1093 if (!intersect_region( clip, clip, parent_clip )) break;
1094 if (!union_region( region, region, clip )) break;
1095 if (!(ptr->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))) continue;
1097 /* subtract the client rect if it uses a custom pixel format */
1098 set_region_rect( clip, &ptr->client_rect );
1099 if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1100 offset_region( clip, offset_x, offset_y );
1101 if (!intersect_region( clip, clip, parent_clip )) break;
1102 if ((ptr->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1103 break;
1105 if (!clip_pixel_format_children( ptr, clip, region, offset_x + ptr->client_rect.left,
1106 offset_y + ptr->client_rect.top ))
1107 break;
1109 free_region( clip );
1110 return region;
1114 /* compute the visible surface region of a window, in parent coordinates */
1115 static struct region *get_surface_region( struct window *win )
1117 struct region *region, *clip;
1118 int offset_x, offset_y;
1120 /* create a region relative to the window itself */
1122 if (!(region = create_empty_region())) return NULL;
1123 if (!(clip = create_empty_region())) goto error;
1124 set_region_rect( region, &win->visible_rect );
1125 if (win->win_region && !intersect_window_region( region, win )) goto error;
1126 set_region_rect( clip, &win->client_rect );
1127 if (win->win_region && !intersect_window_region( clip, win )) goto error;
1129 if ((win->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1130 goto error;
1132 /* clip children */
1134 if (!is_desktop_window(win))
1136 offset_x = win->client_rect.left;
1137 offset_y = win->client_rect.top;
1139 else offset_x = offset_y = 0;
1141 if (!clip_pixel_format_children( win, clip, region, offset_x, offset_y )) goto error;
1143 free_region( clip );
1144 return region;
1146 error:
1147 if (clip) free_region( clip );
1148 free_region( region );
1149 return NULL;
1153 /* get the window class of a window */
1154 struct window_class* get_window_class( user_handle_t window )
1156 struct window *win;
1157 if (!(win = get_window( window ))) return NULL;
1158 if (!win->class) set_error( STATUS_ACCESS_DENIED );
1159 return win->class;
1162 /* determine the window visible rectangle, i.e. window or client rect cropped by parent rects */
1163 /* the returned rectangle is in window coordinates; return 0 if rectangle is empty */
1164 static int get_window_visible_rect( struct window *win, rectangle_t *rect, int frame )
1166 int offset_x = 0, offset_y = 0;
1168 if (!(win->style & WS_VISIBLE)) return 0;
1170 *rect = frame ? win->window_rect : win->client_rect;
1171 if (!is_desktop_window(win))
1173 offset_x = win->window_rect.left;
1174 offset_y = win->window_rect.top;
1177 while (win->parent)
1179 win = win->parent;
1180 if (!(win->style & WS_VISIBLE) || win->style & WS_MINIMIZE) return 0;
1181 if (!is_desktop_window(win))
1183 offset_x += win->client_rect.left;
1184 offset_y += win->client_rect.top;
1185 offset_rect( rect, win->client_rect.left, win->client_rect.top );
1187 if (!intersect_rect( rect, rect, &win->client_rect )) return 0;
1188 if (!intersect_rect( rect, rect, &win->window_rect )) return 0;
1190 offset_rect( rect, -offset_x, -offset_y );
1191 return 1;
1194 /* return a copy of the specified region cropped to the window client or frame rectangle, */
1195 /* and converted from client to window coordinates. Helper for (in)validate_window. */
1196 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
1198 rectangle_t rect;
1199 struct region *tmp;
1201 if (!get_window_visible_rect( win, &rect, frame )) return NULL;
1202 if (!(tmp = create_empty_region())) return NULL;
1203 set_region_rect( tmp, &rect );
1205 if (region)
1207 /* map it to client coords */
1208 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1209 win->window_rect.top - win->client_rect.top );
1211 /* intersect specified region with bounding rect */
1212 if (!intersect_region( tmp, region, tmp )) goto done;
1213 if (is_region_empty( tmp )) goto done;
1215 /* map it back to window coords */
1216 offset_region( tmp, win->client_rect.left - win->window_rect.left,
1217 win->client_rect.top - win->window_rect.top );
1219 return tmp;
1221 done:
1222 free_region( tmp );
1223 return NULL;
1227 /* set a region as new update region for the window */
1228 static void set_update_region( struct window *win, struct region *region )
1230 if (region && !is_region_empty( region ))
1232 if (!win->update_region) inc_window_paint_count( win, 1 );
1233 else free_region( win->update_region );
1234 win->update_region = region;
1236 else
1238 if (win->update_region)
1240 inc_window_paint_count( win, -1 );
1241 free_region( win->update_region );
1243 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
1244 win->update_region = NULL;
1245 if (region) free_region( region );
1250 /* add a region to the update region; the passed region is freed or reused */
1251 static int add_update_region( struct window *win, struct region *region )
1253 if (win->update_region && !union_region( region, win->update_region, region ))
1255 free_region( region );
1256 return 0;
1258 set_update_region( win, region );
1259 return 1;
1263 /* crop the update region of children to the specified rectangle, in client coords */
1264 static void crop_children_update_region( struct window *win, rectangle_t *rect )
1266 struct window *child;
1267 struct region *tmp;
1268 rectangle_t child_rect;
1270 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1272 if (!(child->style & WS_VISIBLE)) continue;
1273 if (!rect) /* crop everything out */
1275 crop_children_update_region( child, NULL );
1276 set_update_region( child, NULL );
1277 continue;
1280 /* nothing to do if child is completely inside rect */
1281 if (child->window_rect.left >= rect->left &&
1282 child->window_rect.top >= rect->top &&
1283 child->window_rect.right <= rect->right &&
1284 child->window_rect.bottom <= rect->bottom) continue;
1286 /* map to child client coords and crop grand-children */
1287 child_rect = *rect;
1288 offset_rect( &child_rect, -child->client_rect.left, -child->client_rect.top );
1289 crop_children_update_region( child, &child_rect );
1291 /* now crop the child itself */
1292 if (!child->update_region) continue;
1293 if (!(tmp = create_empty_region())) continue;
1294 set_region_rect( tmp, rect );
1295 offset_region( tmp, -child->window_rect.left, -child->window_rect.top );
1296 if (intersect_region( tmp, child->update_region, tmp )) set_update_region( child, tmp );
1297 else free_region( tmp );
1302 /* validate the non client area of a window */
1303 static void validate_non_client( struct window *win )
1305 struct region *tmp;
1306 rectangle_t rect;
1308 if (!win->update_region) return; /* nothing to do */
1310 /* get client rect in window coords */
1311 rect.left = win->client_rect.left - win->window_rect.left;
1312 rect.top = win->client_rect.top - win->window_rect.top;
1313 rect.right = win->client_rect.right - win->window_rect.left;
1314 rect.bottom = win->client_rect.bottom - win->window_rect.top;
1316 if ((tmp = create_empty_region()))
1318 set_region_rect( tmp, &rect );
1319 if (intersect_region( tmp, win->update_region, tmp ))
1320 set_update_region( win, tmp );
1321 else
1322 free_region( tmp );
1324 win->paint_flags &= ~PAINT_NONCLIENT;
1328 /* validate a window completely so that we don't get any further paint messages for it */
1329 static void validate_whole_window( struct window *win )
1331 set_update_region( win, NULL );
1333 if (win->paint_flags & PAINT_INTERNAL)
1335 win->paint_flags &= ~PAINT_INTERNAL;
1336 inc_window_paint_count( win, -1 );
1341 /* validate a window's children so that we don't get any further paint messages for it */
1342 static void validate_children( struct window *win )
1344 struct window *child;
1346 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1348 if (!(child->style & WS_VISIBLE)) continue;
1349 validate_children(child);
1350 validate_whole_window(child);
1355 /* validate the update region of a window on all parents; helper for get_update_region */
1356 static void validate_parents( struct window *child )
1358 int offset_x = 0, offset_y = 0;
1359 struct window *win = child;
1360 struct region *tmp = NULL;
1362 if (!child->update_region) return;
1364 while (win->parent)
1366 /* map to parent client coords */
1367 offset_x += win->window_rect.left;
1368 offset_y += win->window_rect.top;
1370 win = win->parent;
1372 /* and now map to window coords */
1373 offset_x += win->client_rect.left - win->window_rect.left;
1374 offset_y += win->client_rect.top - win->window_rect.top;
1376 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1378 if (!tmp && !(tmp = create_empty_region())) return;
1379 offset_region( child->update_region, offset_x, offset_y );
1380 if (subtract_region( tmp, win->update_region, child->update_region ))
1382 set_update_region( win, tmp );
1383 tmp = NULL;
1385 /* restore child coords */
1386 offset_region( child->update_region, -offset_x, -offset_y );
1389 if (tmp) free_region( tmp );
1393 /* add/subtract a region (in client coordinates) to the update region of the window */
1394 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1396 struct region *tmp;
1397 struct window *child;
1399 if (flags & RDW_INVALIDATE)
1401 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1403 if (!add_update_region( win, tmp )) return;
1405 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1406 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1408 else if (flags & RDW_VALIDATE)
1410 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1412 set_update_region( win, NULL );
1414 else if (win->update_region)
1416 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1418 if (!subtract_region( tmp, win->update_region, tmp ))
1420 free_region( tmp );
1421 return;
1423 set_update_region( win, tmp );
1425 if (flags & RDW_NOFRAME) validate_non_client( win );
1426 if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1430 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1432 win->paint_flags |= PAINT_INTERNAL;
1433 inc_window_paint_count( win, 1 );
1435 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1437 win->paint_flags &= ~PAINT_INTERNAL;
1438 inc_window_paint_count( win, -1 );
1441 /* now process children recursively */
1443 if (flags & RDW_NOCHILDREN) return;
1444 if (win->style & WS_MINIMIZE) return;
1445 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1447 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1449 /* map to client coordinates */
1450 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1451 win->window_rect.top - win->client_rect.top );
1453 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1455 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1457 if (!(child->style & WS_VISIBLE)) continue;
1458 if (!rect_in_region( tmp, &child->window_rect )) continue;
1459 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1460 redraw_window( child, tmp, 1, flags );
1461 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1463 free_region( tmp );
1467 /* retrieve the update flags for a window depending on the state of the update region */
1468 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1470 unsigned int ret = 0;
1472 if (flags & UPDATE_NONCLIENT)
1474 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1476 if (flags & UPDATE_ERASE)
1478 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1480 if (flags & UPDATE_PAINT)
1482 if (win->update_region)
1484 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1485 ret |= UPDATE_PAINT;
1488 if (flags & UPDATE_INTERNALPAINT)
1490 if (win->paint_flags & PAINT_INTERNAL)
1492 ret |= UPDATE_INTERNALPAINT;
1493 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1496 return ret;
1500 /* iterate through the children of the given window until we find one with some update flags */
1501 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1502 unsigned int flags, struct window **child )
1504 struct window *ptr;
1505 unsigned int ret = 0;
1507 /* first make sure we want to iterate children at all */
1509 if (win->style & WS_MINIMIZE) return 0;
1511 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1512 * here we only want to repaint children of windows that clip them, others
1513 * need to wait for WM_PAINT to be done in the parent first.
1515 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1517 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1519 if (from_child) /* skip all children until from_child is found */
1521 if (ptr == from_child) from_child = NULL;
1522 continue;
1524 if (!(ptr->style & WS_VISIBLE)) continue;
1525 if ((ret = get_update_flags( ptr, flags )) != 0)
1527 *child = ptr;
1528 break;
1530 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1532 return ret;
1535 /* iterate through children and siblings of the given window until we find one with some update flags */
1536 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1537 unsigned int flags, struct window **child )
1539 unsigned int ret;
1540 struct window *ptr, *from_sibling = NULL;
1542 /* if some parent is not visible start from the next sibling */
1544 if (!is_visible( win )) return 0;
1545 for (ptr = from_child; ptr; ptr = ptr->parent)
1547 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1548 if (ptr == win) break;
1551 /* non-client painting must be delayed if one of the parents is going to
1552 * be repainted and doesn't clip children */
1554 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1556 for (ptr = win->parent; ptr; ptr = ptr->parent)
1558 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1559 return 0;
1561 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1563 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1565 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1566 if (ptr == win) break;
1572 /* check window itself (only if not restarting from a child) */
1574 if (!from_child)
1576 if ((ret = get_update_flags( win, flags )))
1578 *child = win;
1579 return ret;
1581 from_child = win;
1584 /* now check children */
1586 if (flags & UPDATE_NOCHILDREN) return 0;
1587 if (!from_sibling)
1589 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1590 from_sibling = from_child;
1593 /* then check siblings and parent siblings */
1595 while (from_sibling->parent && from_sibling != win)
1597 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1598 return ret;
1599 from_sibling = from_sibling->parent;
1601 return 0;
1605 /* expose the areas revealed by a vis region change on the window parent */
1606 /* returns the region exposed on the window itself (in client coordinates) */
1607 static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
1608 struct region *old_vis_rgn )
1610 struct region *new_vis_rgn, *exposed_rgn;
1612 if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
1614 if ((exposed_rgn = create_empty_region()))
1616 if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
1618 /* make it relative to the new client area */
1619 offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
1620 win->window_rect.top - win->client_rect.top );
1622 else
1624 free_region( exposed_rgn );
1625 exposed_rgn = NULL;
1629 if (win->parent && !is_desktop_window( win->parent ))
1631 /* make it relative to the old window pos for subtracting */
1632 offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
1633 win->window_rect.top - old_window_rect->top );
1635 if ((win->parent->style & WS_CLIPCHILDREN) ?
1636 subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) :
1637 xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1639 if (!is_region_empty( new_vis_rgn ))
1641 /* make it relative to parent */
1642 offset_region( new_vis_rgn, old_window_rect->left, old_window_rect->top );
1643 redraw_window( win->parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1647 free_region( new_vis_rgn );
1648 return exposed_rgn;
1652 /* set the window and client rectangles, updating the update region if necessary */
1653 static void set_window_pos( struct window *win, struct window *previous,
1654 unsigned int swp_flags, const rectangle_t *window_rect,
1655 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1656 const rectangle_t *valid_rect )
1658 struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
1659 const rectangle_t old_window_rect = win->window_rect;
1660 const rectangle_t old_visible_rect = win->visible_rect;
1661 const rectangle_t old_client_rect = win->client_rect;
1662 rectangle_t rect;
1663 int client_changed, frame_changed;
1664 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1666 if (win->parent && !is_visible( win->parent )) visible = 0;
1668 if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
1670 /* set the new window info before invalidating anything */
1672 win->window_rect = *window_rect;
1673 win->visible_rect = *visible_rect;
1674 win->client_rect = *client_rect;
1675 if (!(swp_flags & SWP_NOZORDER) && win->parent) link_window( win, previous );
1676 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1677 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1679 /* keep children at the same position relative to top right corner when the parent is mirrored */
1680 if (win->ex_style & WS_EX_LAYOUTRTL)
1682 struct window *child;
1683 int old_size = old_client_rect.right - old_client_rect.left;
1684 int new_size = win->client_rect.right - win->client_rect.left;
1686 if (old_size != new_size) LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1688 offset_rect( &child->window_rect, new_size - old_size, 0 );
1689 offset_rect( &child->visible_rect, new_size - old_size, 0 );
1690 offset_rect( &child->client_rect, new_size - old_size, 0 );
1694 /* reset cursor clip rectangle when the desktop changes size */
1695 if (win == win->desktop->top_window) win->desktop->cursor.clip = *window_rect;
1697 /* if the window is not visible, everything is easy */
1698 if (!visible) return;
1700 /* expose anything revealed by the change */
1702 if (!(swp_flags & SWP_NOREDRAW))
1703 exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
1705 if (!(win->style & WS_VISIBLE))
1707 /* clear the update region since the window is no longer visible */
1708 validate_whole_window( win );
1709 validate_children( win );
1710 goto done;
1713 /* crop update region to the new window rect */
1715 if (win->update_region)
1717 if (get_window_visible_rect( win, &rect, 1 ))
1719 struct region *tmp = create_empty_region();
1720 if (tmp)
1722 set_region_rect( tmp, &rect );
1723 if (intersect_region( tmp, win->update_region, tmp ))
1724 set_update_region( win, tmp );
1725 else
1726 free_region( tmp );
1729 else set_update_region( win, NULL ); /* visible rect is empty */
1732 /* crop children regions to the new window rect */
1734 if (get_window_visible_rect( win, &rect, 0 ))
1736 /* map to client coords */
1737 offset_rect( &rect, win->window_rect.left - win->client_rect.left,
1738 win->window_rect.top - win->client_rect.top );
1739 crop_children_update_region( win, &rect );
1741 else crop_children_update_region( win, NULL );
1743 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1745 /* expose the whole non-client area if it changed in any way */
1747 if (swp_flags & SWP_NOCOPYBITS)
1749 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1750 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1751 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
1752 client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
1754 else
1756 /* assume the bits have been moved to follow the window rect */
1757 int x_offset = window_rect->left - old_window_rect.left;
1758 int y_offset = window_rect->top - old_window_rect.top;
1759 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1760 window_rect->right - old_window_rect.right != x_offset ||
1761 window_rect->bottom - old_window_rect.bottom != y_offset ||
1762 visible_rect->left - old_visible_rect.left != x_offset ||
1763 visible_rect->right - old_visible_rect.right != x_offset ||
1764 visible_rect->top - old_visible_rect.top != y_offset ||
1765 visible_rect->bottom - old_visible_rect.bottom != y_offset);
1766 client_changed = (client_rect->left - old_client_rect.left != x_offset ||
1767 client_rect->right - old_client_rect.right != x_offset ||
1768 client_rect->top - old_client_rect.top != y_offset ||
1769 client_rect->bottom - old_client_rect.bottom != y_offset ||
1770 !valid_rect || memcmp( valid_rect, client_rect, sizeof(*client_rect) ));
1773 if (frame_changed || client_changed)
1775 struct region *win_rgn = old_vis_rgn; /* reuse previous region */
1777 set_region_rect( win_rgn, window_rect );
1778 if (valid_rect)
1780 /* subtract the valid portion of client rect from the total region */
1781 struct region *tmp = create_empty_region();
1782 if (tmp)
1784 set_region_rect( tmp, valid_rect );
1785 /* subtract update region since invalid parts of the valid rect won't be copied */
1786 if (win->update_region)
1788 offset_region( tmp, -window_rect->left, -window_rect->top );
1789 subtract_region( tmp, tmp, win->update_region );
1790 offset_region( tmp, window_rect->left, window_rect->top );
1792 if (subtract_region( tmp, win_rgn, tmp )) win_rgn = tmp;
1793 else free_region( tmp );
1796 if (!is_desktop_window(win))
1797 offset_region( win_rgn, -client_rect->left, -client_rect->top );
1798 if (exposed_rgn)
1800 union_region( exposed_rgn, exposed_rgn, win_rgn );
1801 if (win_rgn != old_vis_rgn) free_region( win_rgn );
1803 else
1805 exposed_rgn = win_rgn;
1806 if (win_rgn == old_vis_rgn) old_vis_rgn = NULL;
1810 if (exposed_rgn)
1811 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1813 done:
1814 if (old_vis_rgn) free_region( old_vis_rgn );
1815 if (exposed_rgn) free_region( exposed_rgn );
1816 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1820 /* set the window region, updating the update region if necessary */
1821 static void set_window_region( struct window *win, struct region *region, int redraw )
1823 struct region *old_vis_rgn = NULL, *exposed_rgn;
1825 /* no need to redraw if window is not visible */
1826 if (redraw && !is_visible( win )) redraw = 0;
1828 if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
1830 if (win->win_region) free_region( win->win_region );
1831 win->win_region = region;
1833 /* expose anything revealed by the change */
1834 if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
1836 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1837 free_region( exposed_rgn );
1840 if (old_vis_rgn) free_region( old_vis_rgn );
1841 clear_error(); /* we ignore out of memory errors since the region has been set */
1845 /* destroy a window */
1846 void destroy_window( struct window *win )
1848 /* hide the window */
1849 if (is_visible(win))
1851 struct region *vis_rgn = get_visible_region( win, DCX_WINDOW );
1852 win->style &= ~WS_VISIBLE;
1853 if (vis_rgn)
1855 struct region *exposed_rgn = expose_window( win, &win->window_rect, vis_rgn );
1856 if (exposed_rgn) free_region( exposed_rgn );
1857 free_region( vis_rgn );
1859 validate_whole_window( win );
1860 validate_children( win );
1863 /* destroy all children */
1864 while (!list_empty(&win->children))
1865 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
1866 while (!list_empty(&win->unlinked))
1867 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
1869 /* reset global window pointers, if the corresponding window is destroyed */
1870 if (win == shell_window) shell_window = NULL;
1871 if (win == shell_listview) shell_listview = NULL;
1872 if (win == progman_window) progman_window = NULL;
1873 if (win == taskman_window) taskman_window = NULL;
1874 free_hotkeys( win->desktop, win->handle );
1875 cleanup_clipboard_window( win->desktop, win->handle );
1876 free_user_handle( win->handle );
1877 destroy_properties( win );
1878 list_remove( &win->entry );
1879 if (is_desktop_window(win))
1881 struct desktop *desktop = win->desktop;
1882 assert( desktop->top_window == win || desktop->msg_window == win );
1883 if (desktop->top_window == win) desktop->top_window = NULL;
1884 else desktop->msg_window = NULL;
1886 else if (is_desktop_window( win->parent ))
1888 post_message( win->parent->handle, WM_PARENTNOTIFY, WM_DESTROY, win->handle );
1891 detach_window_thread( win );
1892 if (win->win_region) free_region( win->win_region );
1893 if (win->update_region) free_region( win->update_region );
1894 if (win->class) release_class( win->class );
1895 free( win->text );
1896 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
1897 free( win );
1901 /* create a window */
1902 DECL_HANDLER(create_window)
1904 struct window *win, *parent = NULL, *owner = NULL;
1905 struct unicode_str cls_name = get_req_unicode_str();
1906 atom_t atom;
1908 reply->handle = 0;
1909 if (req->parent && !(parent = get_window( req->parent ))) return;
1911 if (req->owner)
1913 if (!(owner = get_window( req->owner ))) return;
1914 if (is_desktop_window(owner)) owner = NULL;
1915 else if (parent && !is_desktop_window(parent))
1917 /* an owned window must be created as top-level */
1918 set_error( STATUS_ACCESS_DENIED );
1919 return;
1921 else /* owner must be a top-level window */
1922 while ((owner->style & (WS_POPUP|WS_CHILD)) == WS_CHILD && !is_desktop_window(owner->parent))
1923 owner = owner->parent;
1926 atom = cls_name.len ? find_global_atom( NULL, &cls_name ) : req->atom;
1928 if (!(win = create_window( parent, owner, atom, req->instance ))) return;
1930 if (parent && !is_desktop_window( parent ))
1932 win->dpi_awareness = parent->dpi_awareness;
1933 win->dpi = parent->dpi;
1935 else if (!parent || req->awareness != DPI_AWARENESS_PER_MONITOR_AWARE)
1937 win->dpi_awareness = req->awareness;
1938 win->dpi = req->dpi;
1941 reply->handle = win->handle;
1942 reply->parent = win->parent ? win->parent->handle : 0;
1943 reply->owner = win->owner;
1944 reply->extra = win->nb_extra_bytes;
1945 reply->dpi = win->dpi;
1946 reply->awareness = win->dpi_awareness;
1947 reply->class_ptr = get_class_client_ptr( win->class );
1951 /* set the parent of a window */
1952 DECL_HANDLER(set_parent)
1954 struct window *win, *parent = NULL;
1956 if (!(win = get_window( req->handle ))) return;
1957 if (req->parent && !(parent = get_window( req->parent ))) return;
1959 if (is_desktop_window(win))
1961 set_error( STATUS_INVALID_PARAMETER );
1962 return;
1964 reply->old_parent = win->parent->handle;
1965 reply->full_parent = parent ? parent->handle : 0;
1966 set_parent_window( win, parent );
1967 reply->dpi = win->dpi;
1968 reply->awareness = win->dpi_awareness;
1972 /* destroy a window */
1973 DECL_HANDLER(destroy_window)
1975 struct window *win = get_window( req->handle );
1976 if (win)
1978 if (!is_desktop_window(win)) destroy_window( win );
1979 else if (win->thread == current) detach_window_thread( win );
1980 else set_error( STATUS_ACCESS_DENIED );
1985 /* retrieve the desktop window for the current thread */
1986 DECL_HANDLER(get_desktop_window)
1988 struct desktop *desktop = get_thread_desktop( current, 0 );
1989 int force;
1991 if (!desktop) return;
1993 /* if winstation is invisible, then avoid roundtrip */
1994 force = req->force || !(desktop->winstation->flags & WSF_VISIBLE);
1996 if (!desktop->top_window && force) /* create it */
1998 if ((desktop->top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
2000 detach_window_thread( desktop->top_window );
2001 desktop->top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
2005 if (!desktop->msg_window && force) /* create it */
2007 static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
2008 static const struct unicode_str name = { messageW, sizeof(messageW) };
2009 atom_t atom = add_global_atom( NULL, &name );
2010 if (atom && (desktop->msg_window = create_window( NULL, NULL, atom, 0 )))
2012 detach_window_thread( desktop->msg_window );
2013 desktop->msg_window->style = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
2017 reply->top_window = desktop->top_window ? desktop->top_window->handle : 0;
2018 reply->msg_window = desktop->msg_window ? desktop->msg_window->handle : 0;
2019 release_object( desktop );
2023 /* set a window owner */
2024 DECL_HANDLER(set_window_owner)
2026 struct window *win = get_window( req->handle );
2027 struct window *owner = NULL, *ptr;
2029 if (!win) return;
2030 if (req->owner && !(owner = get_window( req->owner ))) return;
2031 if (is_desktop_window(win))
2033 set_error( STATUS_ACCESS_DENIED );
2034 return;
2037 /* make sure owner is not a successor of window */
2038 for (ptr = owner; ptr; ptr = ptr->owner ? get_window( ptr->owner ) : NULL)
2040 if (ptr == win)
2042 set_error( STATUS_INVALID_PARAMETER );
2043 return;
2047 reply->prev_owner = win->owner;
2048 reply->full_owner = win->owner = owner ? owner->handle : 0;
2052 /* get information from a window handle */
2053 DECL_HANDLER(get_window_info)
2055 struct window *win = get_window( req->handle );
2057 if (!win) return;
2059 reply->full_handle = win->handle;
2060 reply->last_active = win->handle;
2061 reply->is_unicode = win->is_unicode;
2062 reply->awareness = win->dpi_awareness;
2063 reply->dpi = win->dpi ? win->dpi : get_monitor_dpi( win );
2064 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
2065 if (win->thread)
2067 reply->tid = get_thread_id( win->thread );
2068 reply->pid = get_process_id( win->thread->process );
2069 reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
2074 /* set some information in a window */
2075 DECL_HANDLER(set_window_info)
2077 struct window *win = get_window( req->handle );
2079 if (!win) return;
2080 if (req->flags && is_desktop_window(win) && win->thread != current)
2082 set_error( STATUS_ACCESS_DENIED );
2083 return;
2085 if (req->extra_size > sizeof(req->extra_value) ||
2086 req->extra_offset < -1 ||
2087 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
2089 set_win32_error( ERROR_INVALID_INDEX );
2090 return;
2092 if (req->extra_offset != -1)
2094 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
2096 else if (req->flags & SET_WIN_EXTRA)
2098 set_win32_error( ERROR_INVALID_INDEX );
2099 return;
2101 reply->old_style = win->style;
2102 reply->old_ex_style = win->ex_style;
2103 reply->old_id = win->id;
2104 reply->old_instance = win->instance;
2105 reply->old_user_data = win->user_data;
2106 if (req->flags & SET_WIN_STYLE) win->style = req->style;
2107 if (req->flags & SET_WIN_EXSTYLE)
2109 /* WS_EX_TOPMOST can only be changed for unlinked windows */
2110 if (!win->is_linked) win->ex_style = req->ex_style;
2111 else win->ex_style = (req->ex_style & ~WS_EX_TOPMOST) | (win->ex_style & WS_EX_TOPMOST);
2112 if (!(win->ex_style & WS_EX_LAYERED)) win->is_layered = 0;
2114 if (req->flags & SET_WIN_ID) win->id = req->id;
2115 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
2116 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
2117 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
2118 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
2119 &req->extra_value, req->extra_size );
2121 /* changing window style triggers a non-client paint */
2122 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
2126 /* get a list of the window parents, up to the root of the tree */
2127 DECL_HANDLER(get_window_parents)
2129 struct window *ptr, *win = get_window( req->handle );
2130 int total = 0;
2131 user_handle_t *data;
2132 data_size_t len;
2134 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
2136 reply->count = total;
2137 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2138 if (len && ((data = set_reply_data_size( len ))))
2140 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
2141 *data++ = ptr->handle;
2146 /* get a list of the window children */
2147 DECL_HANDLER(get_window_children)
2149 struct window *parent = NULL;
2150 unsigned int total;
2151 user_handle_t *data;
2152 data_size_t len;
2153 struct unicode_str cls_name = get_req_unicode_str();
2154 atom_t atom = req->atom;
2155 struct desktop *desktop = NULL;
2157 if (cls_name.len && !(atom = find_global_atom( NULL, &cls_name ))) return;
2159 if (req->desktop)
2161 if (!(desktop = get_desktop_obj( current->process, req->desktop, DESKTOP_ENUMERATE ))) return;
2162 parent = desktop->top_window;
2164 else
2166 if (req->parent && !(parent = get_window( req->parent ))) return;
2167 if (!parent && !(desktop = get_thread_desktop( current, 0 ))) return;
2170 if (parent)
2171 total = get_children_windows( parent, atom, req->tid, NULL, 0 );
2172 else
2173 total = get_children_windows( desktop->top_window, atom, req->tid, NULL, 0 ) +
2174 get_children_windows( desktop->msg_window, atom, req->tid, NULL, 0 );
2176 reply->count = total;
2177 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2178 if (len && ((data = set_reply_data_size( len ))))
2180 if (parent) get_children_windows( parent, atom, req->tid, data, len / sizeof(user_handle_t) );
2181 else
2183 total = get_children_windows( desktop->top_window, atom, req->tid,
2184 data, len / sizeof(user_handle_t) );
2185 data += total;
2186 len -= total * sizeof(user_handle_t);
2187 if (len >= sizeof(user_handle_t))
2188 get_children_windows( desktop->msg_window, atom, req->tid,
2189 data, len / sizeof(user_handle_t) );
2192 if (desktop) release_object( desktop );
2196 /* get a list of the window children that contain a given point */
2197 DECL_HANDLER(get_window_children_from_point)
2199 struct user_handle_array array;
2200 struct window *parent = get_window( req->parent );
2201 data_size_t len;
2203 if (!parent) return;
2205 array.handles = NULL;
2206 array.count = 0;
2207 array.total = 0;
2208 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
2210 reply->count = array.count;
2211 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
2212 if (len) set_reply_data_ptr( array.handles, len );
2213 else free( array.handles );
2217 /* get window tree information from a window handle */
2218 DECL_HANDLER(get_window_tree)
2220 struct window *ptr, *win = get_window( req->handle );
2222 if (!win) return;
2224 reply->parent = 0;
2225 reply->owner = 0;
2226 reply->next_sibling = 0;
2227 reply->prev_sibling = 0;
2228 reply->first_sibling = 0;
2229 reply->last_sibling = 0;
2230 reply->first_child = 0;
2231 reply->last_child = 0;
2233 if (win->parent)
2235 struct window *parent = win->parent;
2236 reply->parent = parent->handle;
2237 reply->owner = win->owner;
2238 if (win->is_linked)
2240 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
2241 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
2243 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
2244 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
2246 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
2247 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
2251 /* set the position and Z order of a window */
2252 DECL_HANDLER(set_window_pos)
2254 rectangle_t window_rect, client_rect, visible_rect;
2255 struct window *previous = NULL;
2256 struct window *top, *win = get_window( req->handle );
2257 unsigned int flags = req->swp_flags;
2259 if (!win) return;
2260 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
2262 if (!(flags & SWP_NOZORDER))
2264 switch ((int)req->previous)
2266 case 0: /* HWND_TOP */
2267 previous = WINPTR_TOP;
2268 break;
2269 case 1: /* HWND_BOTTOM */
2270 previous = WINPTR_BOTTOM;
2271 break;
2272 case -1: /* HWND_TOPMOST */
2273 previous = WINPTR_TOPMOST;
2274 break;
2275 case -2: /* HWND_NOTOPMOST */
2276 previous = WINPTR_NOTOPMOST;
2277 break;
2278 default:
2279 if (!(previous = get_window( req->previous ))) return;
2280 /* previous must be a sibling */
2281 if (previous->parent != win->parent)
2283 set_error( STATUS_INVALID_PARAMETER );
2284 return;
2286 break;
2288 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
2291 /* windows that use UpdateLayeredWindow don't trigger repaints */
2292 if ((win->ex_style & WS_EX_LAYERED) && !win->is_layered) flags |= SWP_NOREDRAW;
2294 /* window rectangle must be ordered properly */
2295 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
2297 set_error( STATUS_INVALID_PARAMETER );
2298 return;
2301 window_rect = visible_rect = req->window;
2302 client_rect = req->client;
2303 if (get_req_data_size() >= sizeof(rectangle_t))
2304 memcpy( &visible_rect, get_req_data(), sizeof(rectangle_t) );
2305 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2307 mirror_rect( &win->parent->client_rect, &window_rect );
2308 mirror_rect( &win->parent->client_rect, &visible_rect );
2309 mirror_rect( &win->parent->client_rect, &client_rect );
2312 win->paint_flags = (win->paint_flags & ~PAINT_CLIENT_FLAGS) | (req->paint_flags & PAINT_CLIENT_FLAGS);
2313 if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
2315 if (get_req_data_size() >= 2 * sizeof(rectangle_t))
2317 rectangle_t valid_rect;
2318 memcpy( &valid_rect, (const rectangle_t *)get_req_data() + 1, sizeof(rectangle_t) );
2319 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2320 mirror_rect( &win->parent->client_rect, &valid_rect );
2321 set_window_pos( win, previous, flags, &window_rect, &client_rect, &visible_rect, &valid_rect );
2323 else set_window_pos( win, previous, flags, &window_rect, &client_rect, &visible_rect, NULL );
2325 reply->new_style = win->style;
2326 reply->new_ex_style = win->ex_style;
2328 top = get_top_clipping_window( win );
2329 if (is_visible( top ) && (top->paint_flags & PAINT_HAS_SURFACE))
2331 reply->surface_win = top->handle;
2332 reply->needs_update = !!(top->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD));
2337 /* get the window and client rectangles of a window */
2338 DECL_HANDLER(get_window_rectangles)
2340 struct window *win = get_window( req->handle );
2342 if (!win) return;
2344 reply->window = win->window_rect;
2345 reply->client = win->client_rect;
2347 switch (req->relative)
2349 case COORDS_CLIENT:
2350 offset_rect( &reply->window, -win->client_rect.left, -win->client_rect.top );
2351 offset_rect( &reply->client, -win->client_rect.left, -win->client_rect.top );
2352 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &reply->window );
2353 break;
2354 case COORDS_WINDOW:
2355 offset_rect( &reply->window, -win->window_rect.left, -win->window_rect.top );
2356 offset_rect( &reply->client, -win->window_rect.left, -win->window_rect.top );
2357 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->window_rect, &reply->client );
2358 break;
2359 case COORDS_PARENT:
2360 if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2362 mirror_rect( &win->parent->client_rect, &reply->window );
2363 mirror_rect( &win->parent->client_rect, &reply->client );
2365 break;
2366 case COORDS_SCREEN:
2367 client_to_screen_rect( win->parent, &reply->window );
2368 client_to_screen_rect( win->parent, &reply->client );
2369 break;
2370 default:
2371 set_error( STATUS_INVALID_PARAMETER );
2372 break;
2377 /* get the window text */
2378 DECL_HANDLER(get_window_text)
2380 struct window *win = get_window( req->handle );
2382 if (win && win->text)
2384 reply->length = strlenW( win->text );
2385 set_reply_data( win->text, min( reply->length * sizeof(WCHAR), get_reply_max_size() ));
2390 /* set the window text */
2391 DECL_HANDLER(set_window_text)
2393 struct window *win = get_window( req->handle );
2395 if (win)
2397 WCHAR *text = NULL;
2398 data_size_t len = get_req_data_size() / sizeof(WCHAR);
2399 if (len)
2401 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
2402 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
2403 text[len] = 0;
2405 free( win->text );
2406 win->text = text;
2411 /* get the coordinates offset between two windows */
2412 DECL_HANDLER(get_windows_offset)
2414 struct window *win;
2415 int mirror_from = 0, mirror_to = 0;
2417 reply->x = reply->y = 0;
2418 if (req->from)
2420 if (!(win = get_window( req->from ))) return;
2421 if (win->ex_style & WS_EX_LAYOUTRTL)
2423 mirror_from = 1;
2424 reply->x += win->client_rect.right - win->client_rect.left;
2426 while (win && !is_desktop_window(win))
2428 reply->x += win->client_rect.left;
2429 reply->y += win->client_rect.top;
2430 win = win->parent;
2433 if (req->to)
2435 if (!(win = get_window( req->to ))) return;
2436 if (win->ex_style & WS_EX_LAYOUTRTL)
2438 mirror_to = 1;
2439 reply->x -= win->client_rect.right - win->client_rect.left;
2441 while (win && !is_desktop_window(win))
2443 reply->x -= win->client_rect.left;
2444 reply->y -= win->client_rect.top;
2445 win = win->parent;
2448 if (mirror_from) reply->x = -reply->x;
2449 reply->mirror = mirror_from ^ mirror_to;
2453 /* get the visible region of a window */
2454 DECL_HANDLER(get_visible_region)
2456 struct region *region;
2457 struct window *top, *win = get_window( req->window );
2459 if (!win) return;
2461 top = get_top_clipping_window( win );
2462 if ((region = get_visible_region( win, req->flags )))
2464 rectangle_t *data;
2465 map_win_region_to_screen( win, region );
2466 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2467 if (data) set_reply_data_ptr( data, reply->total_size );
2469 reply->top_win = top->handle;
2470 reply->top_rect = top->visible_rect;
2472 if (!is_desktop_window(win))
2474 reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
2475 client_to_screen_rect( top->parent, &reply->top_rect );
2476 client_to_screen_rect( win->parent, &reply->win_rect );
2478 else
2480 reply->win_rect.left = 0;
2481 reply->win_rect.top = 0;
2482 reply->win_rect.right = win->client_rect.right - win->client_rect.left;
2483 reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
2485 reply->paint_flags = win->paint_flags & PAINT_CLIENT_FLAGS;
2489 /* get the surface visible region of a window */
2490 DECL_HANDLER(get_surface_region)
2492 struct region *region;
2493 struct window *win = get_window( req->window );
2495 if (!win || !is_visible( win )) return;
2497 if ((region = get_surface_region( win )))
2499 rectangle_t *data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2500 if (data) set_reply_data_ptr( data, reply->total_size );
2502 reply->visible_rect = win->visible_rect;
2506 /* get the window region */
2507 DECL_HANDLER(get_window_region)
2509 rectangle_t *data;
2510 struct window *win = get_window( req->window );
2512 if (!win) return;
2513 if (!win->win_region) return;
2515 if (win->ex_style & WS_EX_LAYOUTRTL)
2517 struct region *region = create_empty_region();
2519 if (!region) return;
2520 if (!copy_region( region, win->win_region ))
2522 free_region( region );
2523 return;
2525 mirror_region( &win->window_rect, region );
2526 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2528 else data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
2530 if (data) set_reply_data_ptr( data, reply->total_size );
2534 /* set the window region */
2535 DECL_HANDLER(set_window_region)
2537 struct region *region = NULL;
2538 struct window *win = get_window( req->window );
2540 if (!win) return;
2542 if (get_req_data_size()) /* no data means remove the region completely */
2544 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2545 return;
2546 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->window_rect, region );
2548 set_window_region( win, region, req->redraw );
2552 /* get a window update region */
2553 DECL_HANDLER(get_update_region)
2555 rectangle_t *data;
2556 unsigned int flags = req->flags;
2557 struct window *from_child = NULL;
2558 struct window *win = get_window( req->window );
2560 reply->flags = 0;
2561 if (!win) return;
2563 if (req->from_child)
2565 struct window *ptr;
2567 if (!(from_child = get_window( req->from_child ))) return;
2569 /* make sure from_child is a child of win */
2570 ptr = from_child;
2571 while (ptr && ptr != win) ptr = ptr->parent;
2572 if (!ptr)
2574 set_error( STATUS_INVALID_PARAMETER );
2575 return;
2579 if (flags & UPDATE_DELAYED_ERASE) /* this means that the previous call didn't erase */
2581 if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
2582 else win->paint_flags |= PAINT_DELAYED_ERASE;
2585 reply->flags = get_window_update_flags( win, from_child, flags, &win );
2586 reply->child = win->handle;
2588 if (flags & UPDATE_NOREGION) return;
2590 if (win->update_region)
2592 /* convert update region to screen coordinates */
2593 struct region *region = create_empty_region();
2595 if (!region) return;
2596 if (!copy_region( region, win->update_region ))
2598 free_region( region );
2599 return;
2601 if ((flags & UPDATE_CLIPCHILDREN) && (win->style & WS_CLIPCHILDREN))
2602 clip_children( win, NULL, region, win->client_rect.left - win->window_rect.left,
2603 win->client_rect.top - win->window_rect.top );
2604 map_win_region_to_screen( win, region );
2605 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
2606 &reply->total_size ))) return;
2607 set_reply_data_ptr( data, reply->total_size );
2610 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
2612 validate_parents( win );
2613 validate_whole_window( win );
2615 else
2617 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
2618 if (reply->flags & UPDATE_ERASE)
2620 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
2621 /* desktop window only gets erased, not repainted */
2622 if (is_desktop_window(win)) validate_whole_window( win );
2628 /* update the z order of a window so that a given rectangle is fully visible */
2629 DECL_HANDLER(update_window_zorder)
2631 rectangle_t tmp, rect = req->rect;
2632 struct window *ptr, *win = get_window( req->window );
2634 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
2635 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &rect );
2636 offset_rect( &rect, win->client_rect.left, win->client_rect.top );
2638 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2640 if (ptr == win) break;
2641 if (!(ptr->style & WS_VISIBLE)) continue;
2642 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2643 if (ptr->is_layered && (ptr->layered_flags & LWA_COLORKEY)) continue;
2644 if (!intersect_rect( &tmp, &ptr->visible_rect, &rect )) continue;
2645 if (ptr->win_region)
2647 tmp = rect;
2648 offset_rect( &tmp, -ptr->window_rect.left, -ptr->window_rect.top );
2649 if (!rect_in_region( ptr->win_region, &tmp )) continue;
2651 /* found a window obscuring the rectangle, now move win above this one */
2652 /* making sure to not violate the topmost rule */
2653 if (!(ptr->ex_style & WS_EX_TOPMOST) || (win->ex_style & WS_EX_TOPMOST))
2655 list_remove( &win->entry );
2656 list_add_before( &ptr->entry, &win->entry );
2658 break;
2663 /* mark parts of a window as needing a redraw */
2664 DECL_HANDLER(redraw_window)
2666 struct region *region = NULL;
2667 struct window *win = get_window( req->window );
2669 if (!win) return;
2670 if (!is_visible( win )) return; /* nothing to do */
2672 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
2674 if (get_req_data_size()) /* no data means whole rectangle */
2676 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2677 return;
2678 if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->client_rect, region );
2682 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
2683 req->flags );
2684 if (region) free_region( region );
2688 /* set a window property */
2689 DECL_HANDLER(set_window_property)
2691 struct unicode_str name = get_req_unicode_str();
2692 struct window *win = get_window( req->window );
2694 if (!win) return;
2696 if (name.len)
2698 atom_t atom = add_global_atom( NULL, &name );
2699 if (atom)
2701 set_property( win, atom, req->data, PROP_TYPE_STRING );
2702 release_global_atom( NULL, atom );
2705 else set_property( win, req->atom, req->data, PROP_TYPE_ATOM );
2709 /* remove a window property */
2710 DECL_HANDLER(remove_window_property)
2712 struct unicode_str name = get_req_unicode_str();
2713 struct window *win = get_window( req->window );
2715 if (win)
2717 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2718 if (atom) reply->data = remove_property( win, atom );
2723 /* get a window property */
2724 DECL_HANDLER(get_window_property)
2726 struct unicode_str name = get_req_unicode_str();
2727 struct window *win = get_window( req->window );
2729 if (win)
2731 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2732 if (atom) reply->data = get_property( win, atom );
2737 /* get the list of properties of a window */
2738 DECL_HANDLER(get_window_properties)
2740 property_data_t *data;
2741 int i, count, max = get_reply_max_size() / sizeof(*data);
2742 struct window *win = get_window( req->window );
2744 reply->total = 0;
2745 if (!win) return;
2747 for (i = count = 0; i < win->prop_inuse; i++)
2748 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2749 reply->total = count;
2751 if (count > max) count = max;
2752 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2754 for (i = 0; i < win->prop_inuse && count; i++)
2756 if (win->properties[i].type == PROP_TYPE_FREE) continue;
2757 data->atom = win->properties[i].atom;
2758 data->string = (win->properties[i].type == PROP_TYPE_STRING);
2759 data->data = win->properties[i].data;
2760 data++;
2761 count--;
2766 /* get the new window pointer for a global window, checking permissions */
2767 /* helper for set_global_windows request */
2768 static int get_new_global_window( struct window **win, user_handle_t handle )
2770 if (!handle)
2772 *win = NULL;
2773 return 1;
2775 else if (*win)
2777 set_error( STATUS_ACCESS_DENIED );
2778 return 0;
2780 *win = get_window( handle );
2781 return (*win != NULL);
2784 /* Set/get the global windows */
2785 DECL_HANDLER(set_global_windows)
2787 struct window *new_shell_window = shell_window;
2788 struct window *new_shell_listview = shell_listview;
2789 struct window *new_progman_window = progman_window;
2790 struct window *new_taskman_window = taskman_window;
2792 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2793 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2794 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2795 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2797 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2799 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2800 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2802 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2804 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2806 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2808 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2810 shell_window = new_shell_window;
2811 shell_listview = new_shell_listview;
2812 progman_window = new_progman_window;
2813 taskman_window = new_taskman_window;
2816 /* retrieve layered info for a window */
2817 DECL_HANDLER(get_window_layered_info)
2819 struct window *win = get_window( req->handle );
2821 if (!win) return;
2823 if (win->is_layered)
2825 reply->color_key = win->color_key;
2826 reply->alpha = win->alpha;
2827 reply->flags = win->layered_flags;
2829 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2833 /* set layered info for a window */
2834 DECL_HANDLER(set_window_layered_info)
2836 struct window *win = get_window( req->handle );
2838 if (!win) return;
2840 if (win->ex_style & WS_EX_LAYERED)
2842 int was_layered = win->is_layered;
2844 if (req->flags & LWA_ALPHA) win->alpha = req->alpha;
2845 else if (!win->is_layered) win->alpha = 0; /* alpha init value is 0 */
2847 win->color_key = req->color_key;
2848 win->layered_flags = req->flags;
2849 win->is_layered = 1;
2850 /* repaint since we know now it's not going to use UpdateLayeredWindow */
2851 if (!was_layered) redraw_window( win, 0, 1, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_ERASE | RDW_FRAME );
2853 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );