ddraw: Hack to draw to the desktop window.
[wine/hacks.git] / server / window.c
blob7415f1fd52031ce5fbd96901ca422adb33fddd42
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 lparam_t user_data; /* user-specific data */
87 WCHAR *text; /* window caption text */
88 unsigned int paint_flags; /* various painting flags */
89 int prop_inuse; /* number of in-use window properties */
90 int prop_alloc; /* number of allocated window properties */
91 struct property *properties; /* window properties array */
92 int nb_extra_bytes; /* number of extra bytes */
93 char extra_bytes[1]; /* extra bytes storage */
96 #define PAINT_INTERNAL 0x01 /* internal WM_PAINT pending */
97 #define PAINT_ERASE 0x02 /* needs WM_ERASEBKGND */
98 #define PAINT_NONCLIENT 0x04 /* needs WM_NCPAINT */
99 #define PAINT_DELAYED_ERASE 0x08 /* still needs erase after WM_ERASEBKGND */
101 /* growable array of user handles */
102 struct user_handle_array
104 user_handle_t *handles;
105 int count;
106 int total;
109 /* global window pointers */
110 static struct window *shell_window;
111 static struct window *shell_listview;
112 static struct window *progman_window;
113 static struct window *taskman_window;
115 /* magic HWND_TOP etc. pointers */
116 #define WINPTR_TOP ((struct window *)1L)
117 #define WINPTR_BOTTOM ((struct window *)2L)
118 #define WINPTR_TOPMOST ((struct window *)3L)
119 #define WINPTR_NOTOPMOST ((struct window *)4L)
121 /* retrieve a pointer to a window from its handle */
122 static inline struct window *get_window( user_handle_t handle )
124 struct window *ret = get_user_object( handle, USER_WINDOW );
125 if (!ret) set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
126 return ret;
129 /* check if window is the desktop */
130 static inline int is_desktop_window( const struct window *win )
132 return !win->parent; /* only desktop windows have no parent */
135 /* get next window in Z-order list */
136 static inline struct window *get_next_window( struct window *win )
138 struct list *ptr = list_next( &win->parent->children, &win->entry );
139 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
142 /* get previous window in Z-order list */
143 static inline struct window *get_prev_window( struct window *win )
145 struct list *ptr = list_prev( &win->parent->children, &win->entry );
146 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
149 /* get first child in Z-order list */
150 static inline struct window *get_first_child( struct window *win )
152 struct list *ptr = list_head( &win->children );
153 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
156 /* get last child in Z-order list */
157 static inline struct window *get_last_child( struct window *win )
159 struct list *ptr = list_tail( &win->children );
160 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
163 /* link a window at the right place in the siblings list */
164 static void link_window( struct window *win, struct window *previous )
166 if (previous == WINPTR_NOTOPMOST)
168 if (!(win->ex_style & WS_EX_TOPMOST) && win->is_linked) return; /* nothing to do */
169 win->ex_style &= ~WS_EX_TOPMOST;
170 previous = WINPTR_TOP; /* fallback to the HWND_TOP case */
173 list_remove( &win->entry ); /* unlink it from the previous location */
175 if (previous == WINPTR_BOTTOM)
177 list_add_tail( &win->parent->children, &win->entry );
178 win->ex_style &= ~WS_EX_TOPMOST;
180 else if (previous == WINPTR_TOPMOST)
182 list_add_head( &win->parent->children, &win->entry );
183 win->ex_style |= WS_EX_TOPMOST;
185 else if (previous == WINPTR_TOP)
187 struct list *entry = win->parent->children.next;
188 if (!(win->ex_style & WS_EX_TOPMOST)) /* put it above the first non-topmost window */
190 while (entry != &win->parent->children)
192 struct window *next = LIST_ENTRY( entry, struct window, entry );
193 if (!(next->ex_style & WS_EX_TOPMOST)) break;
194 if (next->handle == win->owner) /* keep it above owner */
196 win->ex_style |= WS_EX_TOPMOST;
197 break;
199 entry = entry->next;
202 list_add_before( entry, &win->entry );
204 else
206 list_add_after( &previous->entry, &win->entry );
207 if (!(previous->ex_style & WS_EX_TOPMOST)) win->ex_style &= ~WS_EX_TOPMOST;
208 else
210 struct window *next = get_next_window( win );
211 if (next && (next->ex_style & WS_EX_TOPMOST)) win->ex_style |= WS_EX_TOPMOST;
215 win->is_linked = 1;
218 /* change the parent of a window (or unlink the window if the new parent is NULL) */
219 static int set_parent_window( struct window *win, struct window *parent )
221 struct window *ptr;
223 /* make sure parent is not a child of window */
224 for (ptr = parent; ptr; ptr = ptr->parent)
226 if (ptr == win)
228 set_error( STATUS_INVALID_PARAMETER );
229 return 0;
233 if (parent)
235 win->parent = parent;
236 link_window( win, WINPTR_TOP );
238 /* if parent belongs to a different thread and the window isn't */
239 /* top-level, attach the two threads */
240 if (parent->thread && parent->thread != win->thread && !is_desktop_window(parent))
241 attach_thread_input( win->thread, parent->thread );
243 else /* move it to parent unlinked list */
245 list_remove( &win->entry ); /* unlink it from the previous location */
246 list_add_head( &win->parent->unlinked, &win->entry );
247 win->is_linked = 0;
249 return 1;
252 /* append a user handle to a handle array */
253 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
255 if (array->count >= array->total)
257 int new_total = max( array->total * 2, 32 );
258 user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
259 if (!new_array)
261 free( array->handles );
262 set_error( STATUS_NO_MEMORY );
263 return 0;
265 array->handles = new_array;
266 array->total = new_total;
268 array->handles[array->count++] = handle;
269 return 1;
272 /* set a window property */
273 static void set_property( struct window *win, atom_t atom, lparam_t data, enum property_type type )
275 int i, free = -1;
276 struct property *new_props;
278 /* check if it exists already */
279 for (i = 0; i < win->prop_inuse; i++)
281 if (win->properties[i].type == PROP_TYPE_FREE)
283 free = i;
284 continue;
286 if (win->properties[i].atom == atom)
288 win->properties[i].type = type;
289 win->properties[i].data = data;
290 return;
294 /* need to add an entry */
295 if (!grab_global_atom( NULL, atom )) return;
296 if (free == -1)
298 /* no free entry */
299 if (win->prop_inuse >= win->prop_alloc)
301 /* need to grow the array */
302 if (!(new_props = realloc( win->properties,
303 sizeof(*new_props) * (win->prop_alloc + 16) )))
305 set_error( STATUS_NO_MEMORY );
306 release_global_atom( NULL, atom );
307 return;
309 win->prop_alloc += 16;
310 win->properties = new_props;
312 free = win->prop_inuse++;
314 win->properties[free].atom = atom;
315 win->properties[free].type = type;
316 win->properties[free].data = data;
319 /* remove a window property */
320 static lparam_t remove_property( struct window *win, atom_t atom )
322 int i;
324 for (i = 0; i < win->prop_inuse; i++)
326 if (win->properties[i].type == PROP_TYPE_FREE) continue;
327 if (win->properties[i].atom == atom)
329 release_global_atom( NULL, atom );
330 win->properties[i].type = PROP_TYPE_FREE;
331 return win->properties[i].data;
334 /* FIXME: last error? */
335 return 0;
338 /* find a window property */
339 static lparam_t get_property( struct window *win, atom_t atom )
341 int i;
343 for (i = 0; i < win->prop_inuse; i++)
345 if (win->properties[i].type == PROP_TYPE_FREE) continue;
346 if (win->properties[i].atom == atom) return win->properties[i].data;
348 /* FIXME: last error? */
349 return 0;
352 /* destroy all properties of a window */
353 static inline void destroy_properties( struct window *win )
355 int i;
357 if (!win->properties) return;
358 for (i = 0; i < win->prop_inuse; i++)
360 if (win->properties[i].type == PROP_TYPE_FREE) continue;
361 release_global_atom( NULL, win->properties[i].atom );
363 free( win->properties );
366 /* detach a window from its owner thread but keep the window around */
367 static void detach_window_thread( struct window *win )
369 struct thread *thread = win->thread;
371 if (!thread) return;
372 if (thread->queue)
374 if (win->update_region) inc_queue_paint_count( thread, -1 );
375 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
376 queue_cleanup_window( thread, win->handle );
378 assert( thread->desktop_users > 0 );
379 thread->desktop_users--;
380 release_class( win->class );
381 win->class = NULL;
383 /* don't hold a reference to the desktop so that the desktop window can be */
384 /* destroyed when the desktop ref count reaches zero */
385 release_object( win->desktop );
386 win->thread = NULL;
389 /* get the process owning the top window of a given desktop */
390 struct process *get_top_window_owner( struct desktop *desktop )
392 struct window *win = desktop->top_window;
393 if (!win || !win->thread) return NULL;
394 return win->thread->process;
397 /* attempt to close the desktop window when the last process using it is gone */
398 void close_desktop_window( struct desktop *desktop )
400 struct window *win = desktop->top_window;
401 if (win && win->thread) post_message( win->handle, WM_CLOSE, 0, 0 );
404 /* create a new window structure (note: the window is not linked in the window tree) */
405 static struct window *create_window( struct window *parent, struct window *owner,
406 atom_t atom, mod_handle_t instance )
408 static const rectangle_t empty_rect;
409 int extra_bytes;
410 struct window *win = NULL;
411 struct desktop *desktop;
412 struct window_class *class;
414 if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
416 if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
418 release_object( desktop );
419 return NULL;
422 if (!parent) /* null parent is only allowed for desktop or HWND_MESSAGE top window */
424 if (is_desktop_class( class ))
425 parent = desktop->top_window; /* use existing desktop if any */
426 else if (is_hwnd_message_class( class ))
427 /* use desktop window if message window is already created */
428 parent = desktop->msg_window ? desktop->top_window : NULL;
429 else if (!(parent = desktop->top_window)) /* must already have a desktop then */
431 set_error( STATUS_ACCESS_DENIED );
432 goto failed;
436 /* parent must be on the same desktop */
437 if (parent && parent->desktop != desktop)
439 set_error( STATUS_ACCESS_DENIED );
440 goto failed;
443 if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
444 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
446 win->parent = parent;
447 win->owner = owner ? owner->handle : 0;
448 win->thread = current;
449 win->desktop = desktop;
450 win->class = class;
451 win->atom = atom;
452 win->last_active = win->handle;
453 win->win_region = NULL;
454 win->update_region = NULL;
455 win->style = 0;
456 win->ex_style = 0;
457 win->id = 0;
458 win->instance = 0;
459 win->is_unicode = 1;
460 win->is_linked = 0;
461 win->is_layered = 0;
462 win->user_data = 0;
463 win->text = NULL;
464 win->paint_flags = 0;
465 win->prop_inuse = 0;
466 win->prop_alloc = 0;
467 win->properties = NULL;
468 win->nb_extra_bytes = extra_bytes;
469 win->window_rect = win->visible_rect = win->client_rect = empty_rect;
470 memset( win->extra_bytes, 0, extra_bytes );
471 list_init( &win->children );
472 list_init( &win->unlinked );
474 /* if parent belongs to a different thread and the window isn't */
475 /* top-level, attach the two threads */
476 if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
478 if (!attach_thread_input( current, parent->thread )) goto failed;
480 else /* otherwise just make sure that the thread has a message queue */
482 if (!current->queue && !init_thread_queue( current )) goto failed;
485 /* put it on parent unlinked list */
486 if (parent) list_add_head( &parent->unlinked, &win->entry );
487 else
489 list_init( &win->entry );
490 if (is_desktop_class( class ))
492 assert( !desktop->top_window );
493 desktop->top_window = win;
494 set_process_default_desktop( current->process, desktop, current->desktop );
496 else
498 assert( !desktop->msg_window );
499 desktop->msg_window = win;
503 current->desktop_users++;
504 return win;
506 failed:
507 if (win)
509 if (win->handle) free_user_handle( win->handle );
510 free( win );
512 release_object( desktop );
513 release_class( class );
514 return NULL;
517 /* destroy all windows belonging to a given thread */
518 void destroy_thread_windows( struct thread *thread )
520 user_handle_t handle = 0;
521 struct window *win;
523 while ((win = next_user_handle( &handle, USER_WINDOW )))
525 if (win->thread != thread) continue;
526 if (is_desktop_window( win )) detach_window_thread( win );
527 else destroy_window( win );
531 /* get the desktop window */
532 static struct window *get_desktop_window( struct thread *thread )
534 struct window *top_window;
535 struct desktop *desktop = get_thread_desktop( thread, 0 );
537 if (!desktop) return NULL;
538 top_window = desktop->top_window;
539 release_object( desktop );
540 return top_window;
543 /* check whether child is a descendant of parent */
544 int is_child_window( user_handle_t parent, user_handle_t child )
546 struct window *child_ptr = get_user_object( child, USER_WINDOW );
547 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
549 if (!child_ptr || !parent_ptr) return 0;
550 while (child_ptr->parent)
552 if (child_ptr->parent == parent_ptr) return 1;
553 child_ptr = child_ptr->parent;
555 return 0;
558 /* check whether window is a top-level window */
559 int is_top_level_window( user_handle_t window )
561 struct window *win = get_user_object( window, USER_WINDOW );
562 #ifndef NOFOCUSLOSS
563 /* set to 1 to not loose focus (e.g. when playing a game
564 * in virtual desktop mode) */
565 #define NOFOCUSLOSS 0
566 #endif
567 if(NOFOCUSLOSS)
568 return (win && win->parent && is_desktop_window(win->parent));
569 else
570 return (win && (is_desktop_window(win) || is_desktop_window(win->parent)));
573 /* make a window active if possible */
574 int make_window_active( user_handle_t window )
576 struct window *owner, *win = get_window( window );
578 if (!win) return 0;
580 /* set last active for window and its owner */
581 win->last_active = win->handle;
582 if ((owner = get_user_object( win->owner, USER_WINDOW ))) owner->last_active = win->handle;
583 return 1;
586 /* increment (or decrement) the window paint count */
587 static inline void inc_window_paint_count( struct window *win, int incr )
589 if (win->thread) inc_queue_paint_count( win->thread, incr );
592 /* check if window and all its ancestors are visible */
593 static int is_visible( const struct window *win )
595 while (win)
597 if (!(win->style & WS_VISIBLE)) return 0;
598 win = win->parent;
599 /* if parent is minimized children are not visible */
600 if (win && (win->style & WS_MINIMIZE)) return 0;
602 return 1;
605 /* same as is_visible but takes a window handle */
606 int is_window_visible( user_handle_t window )
608 struct window *win = get_user_object( window, USER_WINDOW );
609 if (!win) return 0;
610 return is_visible( win );
613 int is_window_transparent( user_handle_t window )
615 struct window *win = get_user_object( window, USER_WINDOW );
616 if (!win) return 0;
617 return (win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT);
620 /* check if point is inside the window */
621 static inline int is_point_in_window( struct window *win, int x, int y )
623 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
624 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
625 return 0; /* disabled child */
626 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
627 return 0; /* transparent */
628 if (x < win->visible_rect.left || x >= win->visible_rect.right ||
629 y < win->visible_rect.top || y >= win->visible_rect.bottom)
630 return 0; /* not in window */
631 if (win->win_region &&
632 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
633 return 0; /* not in window region */
634 return 1;
637 /* fill an array with the handles of the children of a specified window */
638 static unsigned int get_children_windows( struct window *parent, atom_t atom, thread_id_t tid,
639 user_handle_t *handles, unsigned int max_count )
641 struct window *ptr;
642 unsigned int count = 0;
644 if (!parent) return 0;
646 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
648 if (atom && get_class_atom(ptr->class) != atom) continue;
649 if (tid && get_thread_id(ptr->thread) != tid) continue;
650 if (handles)
652 if (count >= max_count) break;
653 handles[count] = ptr->handle;
655 count++;
657 return count;
660 /* find child of 'parent' that contains the given point (in parent-relative coords) */
661 static struct window *child_window_from_point( struct window *parent, int x, int y )
663 struct window *ptr;
665 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
667 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
669 /* if window is minimized or disabled, return at once */
670 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
672 /* if point is not in client area, return at once */
673 if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
674 y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
675 return ptr;
677 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
679 return parent; /* not found any child */
682 /* find all children of 'parent' that contain the given point */
683 static int get_window_children_from_point( struct window *parent, int x, int y,
684 struct user_handle_array *array )
686 struct window *ptr;
688 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
690 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
692 /* if point is in client area, and window is not minimized or disabled, check children */
693 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
694 x >= ptr->client_rect.left && x < ptr->client_rect.right &&
695 y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
697 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
698 y - ptr->client_rect.top, array ))
699 return 0;
702 /* now add window to the array */
703 if (!add_handle_to_array( array, ptr->handle )) return 0;
705 return 1;
708 /* find window containing point (in absolute coords) */
709 user_handle_t window_from_point( struct desktop *desktop, int x, int y )
711 struct window *ret;
713 if (!desktop->top_window) return 0;
714 ret = child_window_from_point( desktop->top_window, x, y );
715 return ret->handle;
718 /* return list of all windows containing point (in absolute coords) */
719 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
721 struct window *ptr;
723 /* make point relative to top window */
724 for (ptr = top->parent; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
726 x -= ptr->client_rect.left;
727 y -= ptr->client_rect.top;
730 if (!is_point_in_window( top, x, y )) return 1;
732 /* if point is in client area, and window is not minimized or disabled, check children */
733 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
734 x >= top->client_rect.left && x < top->client_rect.right &&
735 y >= top->client_rect.top && y < top->client_rect.bottom)
737 if (!is_desktop_window(top))
739 x -= top->client_rect.left;
740 y -= top->client_rect.top;
742 if (!get_window_children_from_point( top, x, y, array )) return 0;
744 /* now add window to the array */
745 if (!add_handle_to_array( array, top->handle )) return 0;
746 return 1;
750 /* return the thread owning a window */
751 struct thread *get_window_thread( user_handle_t handle )
753 struct window *win = get_user_object( handle, USER_WINDOW );
754 if (!win || !win->thread) return NULL;
755 return (struct thread *)grab_object( win->thread );
759 /* check if any area of a window needs repainting */
760 static inline int win_needs_repaint( struct window *win )
762 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
766 /* find a child of the specified window that needs repainting */
767 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
769 struct window *ptr, *ret = NULL;
771 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
773 if (!(ptr->style & WS_VISIBLE)) continue;
774 if (ptr->thread == thread && win_needs_repaint( ptr ))
775 ret = ptr;
776 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
777 ret = find_child_to_repaint( ptr, thread );
778 if (ret) break;
781 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
783 /* transparent window, check for non-transparent sibling to paint first */
784 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
786 if (!(ptr->style & WS_VISIBLE)) continue;
787 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
788 if (ptr->thread != thread) continue;
789 if (win_needs_repaint( ptr )) return ptr;
792 return ret;
796 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
797 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
799 struct window *ptr, *win, *top_window = get_desktop_window( thread );
801 if (!top_window) return 0;
803 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
804 else win = find_child_to_repaint( top_window, thread );
806 if (win && parent)
808 /* check that it is a child of the specified parent */
809 for (ptr = win; ptr; ptr = ptr->parent)
810 if (ptr->handle == parent) break;
811 /* otherwise don't return any window, we don't repaint a child before its parent */
812 if (!ptr) win = NULL;
814 if (!win) return 0;
815 win->paint_flags &= ~PAINT_INTERNAL;
816 return win->handle;
820 /* intersect the window region with the specified region, relative to the window parent */
821 static struct region *intersect_window_region( struct region *region, struct window *win )
823 /* make region relative to window rect */
824 offset_region( region, -win->window_rect.left, -win->window_rect.top );
825 if (!intersect_region( region, region, win->win_region )) return NULL;
826 /* make region relative to parent again */
827 offset_region( region, win->window_rect.left, win->window_rect.top );
828 return region;
832 /* convert coordinates from client to screen coords */
833 static inline void client_to_screen( struct window *win, int *x, int *y )
835 for ( ; win && !is_desktop_window(win); win = win->parent)
837 *x += win->client_rect.left;
838 *y += win->client_rect.top;
842 /* convert coordinates from client to screen coords */
843 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
845 for ( ; win && !is_desktop_window(win); win = win->parent)
847 rect->left += win->client_rect.left;
848 rect->right += win->client_rect.left;
849 rect->top += win->client_rect.top;
850 rect->bottom += win->client_rect.top;
854 /* map the region from window to screen coordinates */
855 static inline void map_win_region_to_screen( struct window *win, struct region *region )
857 if (!is_desktop_window(win))
859 int x = win->window_rect.left;
860 int y = win->window_rect.top;
861 client_to_screen( win->parent, &x, &y );
862 offset_region( region, x, y );
867 /* clip all children of a given window out of the visible region */
868 static struct region *clip_children( struct window *parent, struct window *last,
869 struct region *region, int offset_x, int offset_y )
871 struct window *ptr;
872 struct region *tmp = create_empty_region();
874 if (!tmp) return NULL;
875 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
877 if (ptr == last) break;
878 if (!(ptr->style & WS_VISIBLE)) continue;
879 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
880 set_region_rect( tmp, &ptr->visible_rect );
881 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
883 free_region( tmp );
884 return NULL;
886 offset_region( tmp, offset_x, offset_y );
887 if (!(region = subtract_region( region, region, tmp ))) break;
888 if (is_region_empty( region )) break;
890 free_region( tmp );
891 return region;
895 /* compute the intersection of two rectangles; return 0 if the result is empty */
896 static inline int intersect_rect( rectangle_t *dst, const rectangle_t *src1, const rectangle_t *src2 )
898 dst->left = max( src1->left, src2->left );
899 dst->top = max( src1->top, src2->top );
900 dst->right = min( src1->right, src2->right );
901 dst->bottom = min( src1->bottom, src2->bottom );
902 return (dst->left < dst->right && dst->top < dst->bottom);
906 /* offset the coordinates of a rectangle */
907 static inline void offset_rect( rectangle_t *rect, int offset_x, int offset_y )
909 rect->left += offset_x;
910 rect->top += offset_y;
911 rect->right += offset_x;
912 rect->bottom += offset_y;
916 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
917 static void set_region_client_rect( struct region *region, struct window *win )
919 rectangle_t rect;
921 intersect_rect( &rect, &win->window_rect, &win->client_rect );
922 set_region_rect( region, &rect );
926 /* get the top-level window to clip against for a given window */
927 static inline struct window *get_top_clipping_window( struct window *win )
929 while (win->parent && !is_desktop_window(win->parent)) win = win->parent;
930 return win;
934 /* compute the visible region of a window, in window coordinates */
935 static struct region *get_visible_region( struct window *win, unsigned int flags )
937 struct region *tmp = NULL, *region;
938 int offset_x, offset_y;
940 if (!(region = create_empty_region())) return NULL;
942 /* first check if all ancestors are visible */
944 if (!is_visible( win )) return region; /* empty region */
946 /* create a region relative to the window itself */
948 if ((flags & DCX_PARENTCLIP) && win->parent && !is_desktop_window(win->parent))
950 set_region_client_rect( region, win->parent );
951 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
953 else if (flags & DCX_WINDOW)
955 set_region_rect( region, &win->visible_rect );
956 if (win->win_region && !intersect_window_region( region, win )) goto error;
958 else
960 set_region_client_rect( region, win );
961 if (win->win_region && !intersect_window_region( region, win )) goto error;
964 /* clip children */
966 if (flags & DCX_CLIPCHILDREN)
968 if (is_desktop_window(win)) offset_x = offset_y = 0;
969 else
971 offset_x = win->client_rect.left;
972 offset_y = win->client_rect.top;
974 if (!clip_children( win, NULL, region, offset_x, offset_y )) goto error;
977 /* clip siblings of ancestors */
979 if (is_desktop_window(win)) offset_x = offset_y = 0;
980 else
982 offset_x = win->window_rect.left;
983 offset_y = win->window_rect.top;
986 if ((tmp = create_empty_region()) != NULL)
988 while (win->parent)
990 /* we don't clip out top-level siblings as that's up to the native windowing system */
991 if ((win->style & WS_CLIPSIBLINGS) && !is_desktop_window( win->parent ))
993 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
994 if (is_region_empty( region )) break;
996 /* clip to parent client area */
997 win = win->parent;
998 if (!is_desktop_window(win))
1000 offset_x += win->client_rect.left;
1001 offset_y += win->client_rect.top;
1002 offset_region( region, win->client_rect.left, win->client_rect.top );
1004 set_region_client_rect( tmp, win );
1005 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
1006 if (!intersect_region( region, region, tmp )) goto error;
1007 if (is_region_empty( region )) break;
1009 free_region( tmp );
1011 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
1012 return region;
1014 error:
1015 if (tmp) free_region( tmp );
1016 free_region( region );
1017 return NULL;
1021 /* get the window class of a window */
1022 struct window_class* get_window_class( user_handle_t window )
1024 struct window *win;
1025 if (!(win = get_window( window ))) return NULL;
1026 if (!win->class) set_error( STATUS_ACCESS_DENIED );
1027 return win->class;
1030 /* determine the window visible rectangle, i.e. window or client rect cropped by parent rects */
1031 /* the returned rectangle is in window coordinates; return 0 if rectangle is empty */
1032 static int get_window_visible_rect( struct window *win, rectangle_t *rect, int frame )
1034 int offset_x = 0, offset_y = 0;
1036 if (!(win->style & WS_VISIBLE)) return 0;
1038 *rect = frame ? win->window_rect : win->client_rect;
1039 if (!is_desktop_window(win))
1041 offset_x = win->window_rect.left;
1042 offset_y = win->window_rect.top;
1045 while (win->parent)
1047 win = win->parent;
1048 if (!(win->style & WS_VISIBLE) || win->style & WS_MINIMIZE) return 0;
1049 if (!is_desktop_window(win))
1051 offset_x += win->client_rect.left;
1052 offset_y += win->client_rect.top;
1053 offset_rect( rect, win->client_rect.left, win->client_rect.top );
1055 if (!intersect_rect( rect, rect, &win->client_rect )) return 0;
1056 if (!intersect_rect( rect, rect, &win->window_rect )) return 0;
1058 offset_rect( rect, -offset_x, -offset_y );
1059 return 1;
1062 /* return a copy of the specified region cropped to the window client or frame rectangle, */
1063 /* and converted from client to window coordinates. Helper for (in)validate_window. */
1064 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
1066 rectangle_t rect;
1067 struct region *tmp;
1069 if (!get_window_visible_rect( win, &rect, frame )) return NULL;
1070 if (!(tmp = create_empty_region())) return NULL;
1071 set_region_rect( tmp, &rect );
1073 if (region)
1075 /* map it to client coords */
1076 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1077 win->window_rect.top - win->client_rect.top );
1079 /* intersect specified region with bounding rect */
1080 if (!intersect_region( tmp, region, tmp )) goto done;
1081 if (is_region_empty( tmp )) goto done;
1083 /* map it back to window coords */
1084 offset_region( tmp, win->client_rect.left - win->window_rect.left,
1085 win->client_rect.top - win->window_rect.top );
1087 return tmp;
1089 done:
1090 free_region( tmp );
1091 return NULL;
1095 /* set a region as new update region for the window */
1096 static void set_update_region( struct window *win, struct region *region )
1098 if (region && !is_region_empty( region ))
1100 if (!win->update_region) inc_window_paint_count( win, 1 );
1101 else free_region( win->update_region );
1102 win->update_region = region;
1104 else
1106 if (win->update_region)
1108 inc_window_paint_count( win, -1 );
1109 free_region( win->update_region );
1111 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
1112 win->update_region = NULL;
1113 if (region) free_region( region );
1118 /* add a region to the update region; the passed region is freed or reused */
1119 static int add_update_region( struct window *win, struct region *region )
1121 if (win->update_region && !union_region( region, win->update_region, region ))
1123 free_region( region );
1124 return 0;
1126 set_update_region( win, region );
1127 return 1;
1131 /* crop the update region of children to the specified rectangle, in client coords */
1132 static void crop_children_update_region( struct window *win, rectangle_t *rect )
1134 struct window *child;
1135 struct region *tmp;
1136 rectangle_t child_rect;
1138 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1140 if (!(child->style & WS_VISIBLE)) continue;
1141 if (!rect) /* crop everything out */
1143 crop_children_update_region( child, NULL );
1144 set_update_region( child, NULL );
1145 continue;
1148 /* nothing to do if child is completely inside rect */
1149 if (child->window_rect.left >= rect->left &&
1150 child->window_rect.top >= rect->top &&
1151 child->window_rect.right <= rect->right &&
1152 child->window_rect.bottom <= rect->bottom) continue;
1154 /* map to child client coords and crop grand-children */
1155 child_rect = *rect;
1156 offset_rect( &child_rect, -child->client_rect.left, -child->client_rect.top );
1157 crop_children_update_region( child, &child_rect );
1159 /* now crop the child itself */
1160 if (!child->update_region) continue;
1161 if (!(tmp = create_empty_region())) continue;
1162 set_region_rect( tmp, rect );
1163 offset_region( tmp, -child->window_rect.left, -child->window_rect.top );
1164 if (intersect_region( tmp, child->update_region, tmp )) set_update_region( child, tmp );
1165 else free_region( tmp );
1170 /* validate the non client area of a window */
1171 static void validate_non_client( struct window *win )
1173 struct region *tmp;
1174 rectangle_t rect;
1176 if (!win->update_region) return; /* nothing to do */
1178 /* get client rect in window coords */
1179 rect.left = win->client_rect.left - win->window_rect.left;
1180 rect.top = win->client_rect.top - win->window_rect.top;
1181 rect.right = win->client_rect.right - win->window_rect.left;
1182 rect.bottom = win->client_rect.bottom - win->window_rect.top;
1184 if ((tmp = create_empty_region()))
1186 set_region_rect( tmp, &rect );
1187 if (intersect_region( tmp, win->update_region, tmp ))
1188 set_update_region( win, tmp );
1189 else
1190 free_region( tmp );
1192 win->paint_flags &= ~PAINT_NONCLIENT;
1196 /* validate a window completely so that we don't get any further paint messages for it */
1197 static void validate_whole_window( struct window *win )
1199 set_update_region( win, NULL );
1201 if (win->paint_flags & PAINT_INTERNAL)
1203 win->paint_flags &= ~PAINT_INTERNAL;
1204 inc_window_paint_count( win, -1 );
1209 /* validate a window's children so that we don't get any further paint messages for it */
1210 static void validate_children( struct window *win )
1212 struct window *child;
1214 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1216 if (!(child->style & WS_VISIBLE)) continue;
1217 validate_children(child);
1218 validate_whole_window(child);
1223 /* validate the update region of a window on all parents; helper for get_update_region */
1224 static void validate_parents( struct window *child )
1226 int offset_x = 0, offset_y = 0;
1227 struct window *win = child;
1228 struct region *tmp = NULL;
1230 if (!child->update_region) return;
1232 while (win->parent)
1234 /* map to parent client coords */
1235 offset_x += win->window_rect.left;
1236 offset_y += win->window_rect.top;
1238 win = win->parent;
1240 /* and now map to window coords */
1241 offset_x += win->client_rect.left - win->window_rect.left;
1242 offset_y += win->client_rect.top - win->window_rect.top;
1244 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1246 if (!tmp && !(tmp = create_empty_region())) return;
1247 offset_region( child->update_region, offset_x, offset_y );
1248 if (subtract_region( tmp, win->update_region, child->update_region ))
1250 set_update_region( win, tmp );
1251 tmp = NULL;
1253 /* restore child coords */
1254 offset_region( child->update_region, -offset_x, -offset_y );
1257 if (tmp) free_region( tmp );
1261 /* add/subtract a region (in client coordinates) to the update region of the window */
1262 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1264 struct region *tmp;
1265 struct window *child;
1267 if (flags & RDW_INVALIDATE)
1269 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1271 if (!add_update_region( win, tmp )) return;
1273 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1274 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1276 else if (flags & RDW_VALIDATE)
1278 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1280 set_update_region( win, NULL );
1282 else if (win->update_region)
1284 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1286 if (!subtract_region( tmp, win->update_region, tmp ))
1288 free_region( tmp );
1289 return;
1291 set_update_region( win, tmp );
1293 if (flags & RDW_NOFRAME) validate_non_client( win );
1294 if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1298 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1300 win->paint_flags |= PAINT_INTERNAL;
1301 inc_window_paint_count( win, 1 );
1303 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1305 win->paint_flags &= ~PAINT_INTERNAL;
1306 inc_window_paint_count( win, -1 );
1309 /* now process children recursively */
1311 if (flags & RDW_NOCHILDREN) return;
1312 if (win->style & WS_MINIMIZE) return;
1313 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1315 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1317 /* map to client coordinates */
1318 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1319 win->window_rect.top - win->client_rect.top );
1321 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1323 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1325 if (!(child->style & WS_VISIBLE)) continue;
1326 if (!rect_in_region( tmp, &child->window_rect )) continue;
1327 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1328 redraw_window( child, tmp, 1, flags );
1329 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1331 free_region( tmp );
1335 /* retrieve the update flags for a window depending on the state of the update region */
1336 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1338 unsigned int ret = 0;
1340 if (flags & UPDATE_NONCLIENT)
1342 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1344 if (flags & UPDATE_ERASE)
1346 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1348 if (flags & UPDATE_PAINT)
1350 if (win->update_region)
1352 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1353 ret |= UPDATE_PAINT;
1356 if (flags & UPDATE_INTERNALPAINT)
1358 if (win->paint_flags & PAINT_INTERNAL)
1360 ret |= UPDATE_INTERNALPAINT;
1361 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1364 return ret;
1368 /* iterate through the children of the given window until we find one with some update flags */
1369 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1370 unsigned int flags, struct window **child )
1372 struct window *ptr;
1373 unsigned int ret = 0;
1375 /* first make sure we want to iterate children at all */
1377 if (win->style & WS_MINIMIZE) return 0;
1379 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1380 * here we only want to repaint children of windows that clip them, others
1381 * need to wait for WM_PAINT to be done in the parent first.
1383 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1385 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1387 if (from_child) /* skip all children until from_child is found */
1389 if (ptr == from_child) from_child = NULL;
1390 continue;
1392 if (!(ptr->style & WS_VISIBLE)) continue;
1393 if ((ret = get_update_flags( ptr, flags )) != 0)
1395 *child = ptr;
1396 break;
1398 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1400 return ret;
1403 /* iterate through children and siblings of the given window until we find one with some update flags */
1404 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1405 unsigned int flags, struct window **child )
1407 unsigned int ret;
1408 struct window *ptr, *from_sibling = NULL;
1410 /* if some parent is not visible start from the next sibling */
1412 if (!is_visible( win )) return 0;
1413 for (ptr = from_child; ptr; ptr = ptr->parent)
1415 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1416 if (ptr == win) break;
1419 /* non-client painting must be delayed if one of the parents is going to
1420 * be repainted and doesn't clip children */
1422 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1424 for (ptr = win->parent; ptr; ptr = ptr->parent)
1426 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1427 return 0;
1429 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1431 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1433 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1434 if (ptr == win) break;
1440 /* check window itself (only if not restarting from a child) */
1442 if (!from_child)
1444 if ((ret = get_update_flags( win, flags )))
1446 *child = win;
1447 return ret;
1449 from_child = win;
1452 /* now check children */
1454 if (flags & UPDATE_NOCHILDREN) return 0;
1455 if (!from_sibling)
1457 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1458 from_sibling = from_child;
1461 /* then check siblings and parent siblings */
1463 while (from_sibling->parent && from_sibling != win)
1465 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1466 return ret;
1467 from_sibling = from_sibling->parent;
1469 return 0;
1473 /* expose the areas revealed by a vis region change on the window parent */
1474 /* returns the region exposed on the window itself (in client coordinates) */
1475 static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
1476 struct region *old_vis_rgn )
1478 struct region *new_vis_rgn, *exposed_rgn;
1480 if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
1482 if ((exposed_rgn = create_empty_region()))
1484 if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
1486 /* make it relative to the new client area */
1487 offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
1488 win->window_rect.top - win->client_rect.top );
1490 else
1492 free_region( exposed_rgn );
1493 exposed_rgn = NULL;
1497 if (win->parent)
1499 /* make it relative to the old window pos for subtracting */
1500 offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
1501 win->window_rect.top - old_window_rect->top );
1503 if ((win->parent->style & WS_CLIPCHILDREN) ?
1504 subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) :
1505 xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1507 if (!is_region_empty( new_vis_rgn ))
1509 /* make it relative to parent */
1510 offset_region( new_vis_rgn, old_window_rect->left, old_window_rect->top );
1511 redraw_window( win->parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1515 free_region( new_vis_rgn );
1516 return exposed_rgn;
1520 /* set the window and client rectangles, updating the update region if necessary */
1521 static void set_window_pos( struct window *win, struct window *previous,
1522 unsigned int swp_flags, const rectangle_t *window_rect,
1523 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1524 const rectangle_t *valid_rects )
1526 struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
1527 const rectangle_t old_window_rect = win->window_rect;
1528 const rectangle_t old_visible_rect = win->visible_rect;
1529 const rectangle_t old_client_rect = win->client_rect;
1530 rectangle_t rect;
1531 int client_changed, frame_changed;
1532 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1534 if (win->parent && !is_visible( win->parent )) visible = 0;
1536 if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
1538 /* set the new window info before invalidating anything */
1540 win->window_rect = *window_rect;
1541 win->visible_rect = *visible_rect;
1542 win->client_rect = *client_rect;
1543 if (!(swp_flags & SWP_NOZORDER) && win->parent) link_window( win, previous );
1544 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1545 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1547 /* if the window is not visible, everything is easy */
1548 if (!visible) return;
1550 /* expose anything revealed by the change */
1552 if (!(swp_flags & SWP_NOREDRAW))
1553 exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
1555 if (!(win->style & WS_VISIBLE))
1557 /* clear the update region since the window is no longer visible */
1558 validate_whole_window( win );
1559 validate_children( win );
1560 goto done;
1563 /* crop update region to the new window rect */
1565 if (win->update_region)
1567 if (get_window_visible_rect( win, &rect, 1 ))
1569 struct region *tmp = create_empty_region();
1570 if (tmp)
1572 set_region_rect( tmp, &rect );
1573 if (intersect_region( tmp, win->update_region, tmp ))
1574 set_update_region( win, tmp );
1575 else
1576 free_region( tmp );
1579 else set_update_region( win, NULL ); /* visible rect is empty */
1582 /* crop children regions to the new window rect */
1584 if (get_window_visible_rect( win, &rect, 0 ))
1586 /* map to client coords */
1587 offset_rect( &rect, win->window_rect.left - win->client_rect.left,
1588 win->window_rect.top - win->client_rect.top );
1589 crop_children_update_region( win, &rect );
1591 else crop_children_update_region( win, NULL );
1593 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1595 /* expose the whole non-client area if it changed in any way */
1597 if (swp_flags & SWP_NOCOPYBITS)
1599 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1600 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1601 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
1602 client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
1604 else
1606 /* assume the bits have been moved to follow the window rect */
1607 int x_offset = window_rect->left - old_window_rect.left;
1608 int y_offset = window_rect->top - old_window_rect.top;
1609 frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1610 window_rect->right - old_window_rect.right != x_offset ||
1611 window_rect->bottom - old_window_rect.bottom != y_offset ||
1612 visible_rect->left - old_visible_rect.left != x_offset ||
1613 visible_rect->right - old_visible_rect.right != x_offset ||
1614 visible_rect->top - old_visible_rect.top != y_offset ||
1615 visible_rect->bottom - old_visible_rect.bottom != y_offset);
1616 client_changed = (client_rect->left - old_client_rect.left != x_offset ||
1617 client_rect->right - old_client_rect.right != x_offset ||
1618 client_rect->top - old_client_rect.top != y_offset ||
1619 client_rect->bottom - old_client_rect.bottom != y_offset ||
1620 !valid_rects ||
1621 memcmp( &valid_rects[0], client_rect, sizeof(*client_rect) ));
1624 if (frame_changed || client_changed)
1626 struct region *win_rgn = old_vis_rgn; /* reuse previous region */
1628 set_region_rect( win_rgn, window_rect );
1629 if (valid_rects)
1631 /* subtract the valid portion of client rect from the total region */
1632 struct region *tmp = create_empty_region();
1633 if (tmp)
1635 set_region_rect( tmp, &valid_rects[0] );
1636 if (subtract_region( tmp, win_rgn, tmp )) win_rgn = tmp;
1637 else free_region( tmp );
1640 if (!is_desktop_window(win))
1641 offset_region( win_rgn, -client_rect->left, -client_rect->top );
1642 if (exposed_rgn)
1644 union_region( exposed_rgn, exposed_rgn, win_rgn );
1645 if (win_rgn != old_vis_rgn) free_region( win_rgn );
1647 else
1649 exposed_rgn = win_rgn;
1650 if (win_rgn == old_vis_rgn) old_vis_rgn = NULL;
1654 if (exposed_rgn)
1655 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1657 done:
1658 if (old_vis_rgn) free_region( old_vis_rgn );
1659 if (exposed_rgn) free_region( exposed_rgn );
1660 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1664 /* set the window region, updating the update region if necessary */
1665 static void set_window_region( struct window *win, struct region *region, int redraw )
1667 struct region *old_vis_rgn = NULL, *exposed_rgn;
1669 /* no need to redraw if window is not visible */
1670 if (redraw && !is_visible( win )) redraw = 0;
1672 if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
1674 if (win->win_region) free_region( win->win_region );
1675 win->win_region = region;
1677 /* expose anything revealed by the change */
1678 if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
1680 redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1681 free_region( exposed_rgn );
1684 if (old_vis_rgn) free_region( old_vis_rgn );
1685 clear_error(); /* we ignore out of memory errors since the region has been set */
1689 /* destroy a window */
1690 void destroy_window( struct window *win )
1692 /* hide the window */
1693 if (is_visible(win))
1695 struct region *vis_rgn = get_visible_region( win, DCX_WINDOW );
1696 win->style &= ~WS_VISIBLE;
1697 if (vis_rgn)
1699 struct region *exposed_rgn = expose_window( win, &win->window_rect, vis_rgn );
1700 if (exposed_rgn) free_region( exposed_rgn );
1701 free_region( vis_rgn );
1703 validate_whole_window( win );
1704 validate_children( win );
1707 /* destroy all children */
1708 while (!list_empty(&win->children))
1709 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
1710 while (!list_empty(&win->unlinked))
1711 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
1713 /* reset global window pointers, if the corresponding window is destroyed */
1714 if (win == shell_window) shell_window = NULL;
1715 if (win == shell_listview) shell_listview = NULL;
1716 if (win == progman_window) progman_window = NULL;
1717 if (win == taskman_window) taskman_window = NULL;
1718 free_user_handle( win->handle );
1719 destroy_properties( win );
1720 list_remove( &win->entry );
1721 if (is_desktop_window(win))
1723 struct desktop *desktop = win->desktop;
1724 assert( desktop->top_window == win || desktop->msg_window == win );
1725 if (desktop->top_window == win) desktop->top_window = NULL;
1726 else desktop->msg_window = NULL;
1728 detach_window_thread( win );
1729 if (win->win_region) free_region( win->win_region );
1730 if (win->update_region) free_region( win->update_region );
1731 if (win->class) release_class( win->class );
1732 free( win->text );
1733 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
1734 free( win );
1738 /* create a window */
1739 DECL_HANDLER(create_window)
1741 struct window *win, *parent = NULL, *owner = NULL;
1742 struct unicode_str cls_name;
1743 atom_t atom;
1745 reply->handle = 0;
1746 if (req->parent && !(parent = get_window( req->parent ))) return;
1748 if (req->owner)
1750 if (!(owner = get_window( req->owner ))) return;
1751 if (is_desktop_window(owner)) owner = NULL;
1752 else if (parent && !is_desktop_window(parent))
1754 /* an owned window must be created as top-level */
1755 set_error( STATUS_ACCESS_DENIED );
1756 return;
1758 else /* owner must be a top-level window */
1759 while (!is_desktop_window(owner->parent)) owner = owner->parent;
1762 get_req_unicode_str( &cls_name );
1763 atom = cls_name.len ? find_global_atom( NULL, &cls_name ) : req->atom;
1765 if (!(win = create_window( parent, owner, atom, req->instance ))) return;
1767 reply->handle = win->handle;
1768 reply->parent = win->parent ? win->parent->handle : 0;
1769 reply->owner = win->owner;
1770 reply->extra = win->nb_extra_bytes;
1771 reply->class_ptr = get_class_client_ptr( win->class );
1775 /* set the parent of a window */
1776 DECL_HANDLER(set_parent)
1778 struct window *win, *parent = NULL;
1780 if (!(win = get_window( req->handle ))) return;
1781 if (req->parent && !(parent = get_window( req->parent ))) return;
1783 if (is_desktop_window(win))
1785 set_error( STATUS_INVALID_PARAMETER );
1786 return;
1788 reply->old_parent = win->parent->handle;
1789 reply->full_parent = parent ? parent->handle : 0;
1790 set_parent_window( win, parent );
1794 /* destroy a window */
1795 DECL_HANDLER(destroy_window)
1797 struct window *win = get_window( req->handle );
1798 if (win)
1800 if (!is_desktop_window(win)) destroy_window( win );
1801 else if (win->thread == current) detach_window_thread( win );
1802 else set_error( STATUS_ACCESS_DENIED );
1807 /* retrieve the desktop window for the current thread */
1808 DECL_HANDLER(get_desktop_window)
1810 struct desktop *desktop = get_thread_desktop( current, 0 );
1812 if (!desktop) return;
1814 if (!desktop->top_window && req->force) /* create it */
1816 if ((desktop->top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
1818 detach_window_thread( desktop->top_window );
1819 desktop->top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1823 if (!desktop->msg_window && req->force) /* create it */
1825 static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
1826 static const struct unicode_str name = { messageW, sizeof(messageW) };
1827 atom_t atom = add_global_atom( NULL, &name );
1828 if (atom && (desktop->msg_window = create_window( NULL, NULL, atom, 0 )))
1830 detach_window_thread( desktop->msg_window );
1831 desktop->msg_window->style = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1835 reply->top_window = desktop->top_window ? desktop->top_window->handle : 0;
1836 reply->msg_window = desktop->msg_window ? desktop->msg_window->handle : 0;
1837 release_object( desktop );
1841 /* set a window owner */
1842 DECL_HANDLER(set_window_owner)
1844 struct window *win = get_window( req->handle );
1845 struct window *owner = NULL;
1847 if (!win) return;
1848 if (req->owner && !(owner = get_window( req->owner ))) return;
1849 if (is_desktop_window(win))
1851 set_error( STATUS_ACCESS_DENIED );
1852 return;
1854 reply->prev_owner = win->owner;
1855 reply->full_owner = win->owner = owner ? owner->handle : 0;
1859 /* get information from a window handle */
1860 DECL_HANDLER(get_window_info)
1862 struct window *win = get_window( req->handle );
1864 reply->full_handle = 0;
1865 reply->tid = reply->pid = 0;
1866 if (win)
1868 reply->full_handle = win->handle;
1869 reply->last_active = win->handle;
1870 reply->is_unicode = win->is_unicode;
1871 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
1872 if (win->thread)
1874 reply->tid = get_thread_id( win->thread );
1875 reply->pid = get_process_id( win->thread->process );
1876 reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
1882 /* set some information in a window */
1883 DECL_HANDLER(set_window_info)
1885 struct window *win = get_window( req->handle );
1887 if (!win) return;
1888 if (req->flags && is_desktop_window(win) && win->thread != current)
1890 set_error( STATUS_ACCESS_DENIED );
1891 return;
1893 if (req->extra_size > sizeof(req->extra_value) ||
1894 req->extra_offset < -1 ||
1895 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
1897 set_win32_error( ERROR_INVALID_INDEX );
1898 return;
1900 if (req->extra_offset != -1)
1902 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
1904 else if (req->flags & SET_WIN_EXTRA)
1906 set_win32_error( ERROR_INVALID_INDEX );
1907 return;
1909 reply->old_style = win->style;
1910 reply->old_ex_style = win->ex_style;
1911 reply->old_id = win->id;
1912 reply->old_instance = win->instance;
1913 reply->old_user_data = win->user_data;
1914 if (req->flags & SET_WIN_STYLE) win->style = req->style;
1915 if (req->flags & SET_WIN_EXSTYLE)
1917 /* WS_EX_TOPMOST can only be changed for unlinked windows */
1918 if (!win->is_linked) win->ex_style = req->ex_style;
1919 else win->ex_style = (req->ex_style & ~WS_EX_TOPMOST) | (win->ex_style & WS_EX_TOPMOST);
1920 if (!(win->ex_style & WS_EX_LAYERED)) win->is_layered = 0;
1922 if (req->flags & SET_WIN_ID) win->id = req->id;
1923 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
1924 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
1925 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
1926 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
1927 &req->extra_value, req->extra_size );
1929 /* changing window style triggers a non-client paint */
1930 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
1934 /* get a list of the window parents, up to the root of the tree */
1935 DECL_HANDLER(get_window_parents)
1937 struct window *ptr, *win = get_window( req->handle );
1938 int total = 0;
1939 user_handle_t *data;
1940 data_size_t len;
1942 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
1944 reply->count = total;
1945 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1946 if (len && ((data = set_reply_data_size( len ))))
1948 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
1949 *data++ = ptr->handle;
1954 /* get a list of the window children */
1955 DECL_HANDLER(get_window_children)
1957 struct window *parent = NULL;
1958 unsigned int total;
1959 user_handle_t *data;
1960 data_size_t len;
1961 struct unicode_str cls_name;
1962 atom_t atom = req->atom;
1963 struct desktop *desktop = NULL;
1965 get_req_unicode_str( &cls_name );
1966 if (cls_name.len && !(atom = find_global_atom( NULL, &cls_name ))) return;
1968 if (req->desktop)
1970 if (!(desktop = get_desktop_obj( current->process, req->desktop, DESKTOP_ENUMERATE ))) return;
1971 parent = desktop->top_window;
1973 else
1975 if (req->parent && !(parent = get_window( req->parent ))) return;
1976 if (!parent && !(desktop = get_thread_desktop( current, 0 ))) return;
1979 if (parent)
1980 total = get_children_windows( parent, atom, req->tid, NULL, 0 );
1981 else
1982 total = get_children_windows( desktop->top_window, atom, req->tid, NULL, 0 ) +
1983 get_children_windows( desktop->msg_window, atom, req->tid, NULL, 0 );
1985 reply->count = total;
1986 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1987 if (len && ((data = set_reply_data_size( len ))))
1989 if (parent) get_children_windows( parent, atom, req->tid, data, len / sizeof(user_handle_t) );
1990 else
1992 total = get_children_windows( desktop->top_window, atom, req->tid,
1993 data, len / sizeof(user_handle_t) );
1994 data += total;
1995 len -= total * sizeof(user_handle_t);
1996 if (len >= sizeof(user_handle_t))
1997 get_children_windows( desktop->msg_window, atom, req->tid,
1998 data, len / sizeof(user_handle_t) );
2001 if (desktop) release_object( desktop );
2005 /* get a list of the window children that contain a given point */
2006 DECL_HANDLER(get_window_children_from_point)
2008 struct user_handle_array array;
2009 struct window *parent = get_window( req->parent );
2010 data_size_t len;
2012 if (!parent) return;
2014 array.handles = NULL;
2015 array.count = 0;
2016 array.total = 0;
2017 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
2019 reply->count = array.count;
2020 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
2021 if (len) set_reply_data_ptr( array.handles, len );
2022 else free( array.handles );
2026 /* get window tree information from a window handle */
2027 DECL_HANDLER(get_window_tree)
2029 struct window *ptr, *win = get_window( req->handle );
2031 if (!win) return;
2033 reply->parent = 0;
2034 reply->owner = 0;
2035 reply->next_sibling = 0;
2036 reply->prev_sibling = 0;
2037 reply->first_sibling = 0;
2038 reply->last_sibling = 0;
2039 reply->first_child = 0;
2040 reply->last_child = 0;
2042 if (win->parent)
2044 struct window *parent = win->parent;
2045 reply->parent = parent->handle;
2046 reply->owner = win->owner;
2047 if (win->is_linked)
2049 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
2050 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
2052 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
2053 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
2055 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
2056 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
2060 /* set the position and Z order of a window */
2061 DECL_HANDLER(set_window_pos)
2063 const rectangle_t *visible_rect = NULL, *valid_rects = NULL;
2064 struct window *previous = NULL;
2065 struct window *win = get_window( req->handle );
2066 unsigned int flags = req->flags;
2068 if (!win) return;
2069 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
2071 if (!(flags & SWP_NOZORDER))
2073 switch ((int)req->previous)
2075 case 0: /* HWND_TOP */
2076 previous = WINPTR_TOP;
2077 break;
2078 case 1: /* HWND_BOTTOM */
2079 previous = WINPTR_BOTTOM;
2080 break;
2081 case -1: /* HWND_TOPMOST */
2082 previous = WINPTR_TOPMOST;
2083 break;
2084 case -2: /* HWND_NOTOPMOST */
2085 previous = WINPTR_NOTOPMOST;
2086 break;
2087 default:
2088 if (!(previous = get_window( req->previous ))) return;
2089 /* previous must be a sibling */
2090 if (previous->parent != win->parent)
2092 set_error( STATUS_INVALID_PARAMETER );
2093 return;
2095 break;
2097 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
2100 /* window rectangle must be ordered properly */
2101 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
2103 set_error( STATUS_INVALID_PARAMETER );
2104 return;
2107 if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = get_req_data();
2108 if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rects = visible_rect + 1;
2110 if (!visible_rect) visible_rect = &req->window;
2111 set_window_pos( win, previous, flags, &req->window, &req->client, visible_rect, valid_rects );
2112 reply->new_style = win->style;
2113 reply->new_ex_style = win->ex_style;
2117 /* get the window and client rectangles of a window */
2118 DECL_HANDLER(get_window_rectangles)
2120 struct window *win = get_window( req->handle );
2122 if (win)
2124 reply->window = win->window_rect;
2125 reply->visible = win->visible_rect;
2126 reply->client = win->client_rect;
2131 /* get the window text */
2132 DECL_HANDLER(get_window_text)
2134 struct window *win = get_window( req->handle );
2136 if (win && win->text)
2138 data_size_t len = strlenW( win->text ) * sizeof(WCHAR);
2139 if (len > get_reply_max_size()) len = get_reply_max_size();
2140 set_reply_data( win->text, len );
2145 /* set the window text */
2146 DECL_HANDLER(set_window_text)
2148 struct window *win = get_window( req->handle );
2150 if (win)
2152 WCHAR *text = NULL;
2153 data_size_t len = get_req_data_size() / sizeof(WCHAR);
2154 if (len)
2156 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
2157 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
2158 text[len] = 0;
2160 free( win->text );
2161 win->text = text;
2166 /* get the coordinates offset between two windows */
2167 DECL_HANDLER(get_windows_offset)
2169 struct window *win;
2171 reply->x = reply->y = 0;
2172 if (req->from)
2174 if (!(win = get_window( req->from ))) return;
2175 while (win && !is_desktop_window(win))
2177 reply->x += win->client_rect.left;
2178 reply->y += win->client_rect.top;
2179 win = win->parent;
2182 if (req->to)
2184 if (!(win = get_window( req->to ))) return;
2185 while (win && !is_desktop_window(win))
2187 reply->x -= win->client_rect.left;
2188 reply->y -= win->client_rect.top;
2189 win = win->parent;
2195 /* get the visible region of a window */
2196 DECL_HANDLER(get_visible_region)
2198 struct region *region;
2199 struct window *top, *win = get_window( req->window );
2201 if (!win) return;
2203 top = get_top_clipping_window( win );
2204 if ((region = get_visible_region( win, req->flags )))
2206 rectangle_t *data;
2207 map_win_region_to_screen( win, region );
2208 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2209 if (data) set_reply_data_ptr( data, reply->total_size );
2211 reply->top_win = top->handle;
2212 reply->top_rect = (top == win && (req->flags & DCX_WINDOW)) ? top->visible_rect : top->client_rect;
2214 if (!is_desktop_window(win))
2216 reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
2217 client_to_screen_rect( top->parent, &reply->top_rect );
2218 client_to_screen_rect( win->parent, &reply->win_rect );
2220 else
2222 reply->win_rect.left = 0;
2223 reply->win_rect.top = 0;
2224 reply->win_rect.right = win->client_rect.right - win->client_rect.left;
2225 reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
2230 /* get the window region */
2231 DECL_HANDLER(get_window_region)
2233 struct window *win = get_window( req->window );
2235 if (!win) return;
2237 if (win->win_region)
2239 rectangle_t *data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
2240 if (data) set_reply_data_ptr( data, reply->total_size );
2245 /* set the window region */
2246 DECL_HANDLER(set_window_region)
2248 struct region *region = NULL;
2249 struct window *win = get_window( req->window );
2251 if (!win) return;
2253 if (get_req_data_size()) /* no data means remove the region completely */
2255 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2256 return;
2258 set_window_region( win, region, req->redraw );
2262 /* get a window update region */
2263 DECL_HANDLER(get_update_region)
2265 rectangle_t *data;
2266 unsigned int flags = req->flags;
2267 struct window *from_child = NULL;
2268 struct window *win = get_window( req->window );
2270 reply->flags = 0;
2271 if (!win) return;
2273 if (req->from_child)
2275 struct window *ptr;
2277 if (!(from_child = get_window( req->from_child ))) return;
2279 /* make sure from_child is a child of win */
2280 ptr = from_child;
2281 while (ptr && ptr != win) ptr = ptr->parent;
2282 if (!ptr)
2284 set_error( STATUS_INVALID_PARAMETER );
2285 return;
2289 if (flags & UPDATE_DELAYED_ERASE) /* this means that the previous call didn't erase */
2291 if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
2292 else win->paint_flags |= PAINT_DELAYED_ERASE;
2295 reply->flags = get_window_update_flags( win, from_child, flags, &win );
2296 reply->child = win->handle;
2298 if (flags & UPDATE_NOREGION) return;
2300 if (win->update_region)
2302 /* convert update region to screen coordinates */
2303 struct region *region = create_empty_region();
2305 if (!region) return;
2306 if (!copy_region( region, win->update_region ))
2308 free_region( region );
2309 return;
2311 map_win_region_to_screen( win, region );
2312 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
2313 &reply->total_size ))) return;
2314 set_reply_data_ptr( data, reply->total_size );
2317 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
2319 validate_parents( win );
2320 validate_whole_window( win );
2322 else
2324 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
2325 if (reply->flags & UPDATE_ERASE)
2327 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
2328 /* desktop window only gets erased, not repainted */
2329 if (is_desktop_window(win)) validate_whole_window( win );
2335 /* update the z order of a window so that a given rectangle is fully visible */
2336 DECL_HANDLER(update_window_zorder)
2338 rectangle_t tmp;
2339 struct window *ptr, *win = get_window( req->window );
2341 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
2343 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2345 if (ptr == win) break;
2346 if (!(ptr->style & WS_VISIBLE)) continue;
2347 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2348 if (!intersect_rect( &tmp, &ptr->visible_rect, &req->rect )) continue;
2349 if (ptr->win_region && !rect_in_region( ptr->win_region, &req->rect )) continue;
2350 /* found a window obscuring the rectangle, now move win above this one */
2351 /* making sure to not violate the topmost rule */
2352 if (!(ptr->ex_style & WS_EX_TOPMOST) || (win->ex_style & WS_EX_TOPMOST))
2354 list_remove( &win->entry );
2355 list_add_before( &ptr->entry, &win->entry );
2357 break;
2362 /* mark parts of a window as needing a redraw */
2363 DECL_HANDLER(redraw_window)
2365 struct region *region = NULL;
2366 struct window *win = get_window( req->window );
2368 if (!win) return;
2369 if (!is_visible( win )) return; /* nothing to do */
2371 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
2373 if (get_req_data_size()) /* no data means whole rectangle */
2375 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2376 return;
2380 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
2381 req->flags );
2382 if (region) free_region( region );
2386 /* set a window property */
2387 DECL_HANDLER(set_window_property)
2389 struct unicode_str name;
2390 struct window *win = get_window( req->window );
2392 if (!win) return;
2394 get_req_unicode_str( &name );
2395 if (name.len)
2397 atom_t atom = add_global_atom( NULL, &name );
2398 if (atom)
2400 set_property( win, atom, req->data, PROP_TYPE_STRING );
2401 release_global_atom( NULL, atom );
2404 else set_property( win, req->atom, req->data, PROP_TYPE_ATOM );
2408 /* remove a window property */
2409 DECL_HANDLER(remove_window_property)
2411 struct unicode_str name;
2412 struct window *win = get_window( req->window );
2414 get_req_unicode_str( &name );
2415 if (win)
2417 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2418 if (atom) reply->data = remove_property( win, atom );
2423 /* get a window property */
2424 DECL_HANDLER(get_window_property)
2426 struct unicode_str name;
2427 struct window *win = get_window( req->window );
2429 get_req_unicode_str( &name );
2430 if (win)
2432 atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2433 if (atom) reply->data = get_property( win, atom );
2438 /* get the list of properties of a window */
2439 DECL_HANDLER(get_window_properties)
2441 property_data_t *data;
2442 int i, count, max = get_reply_max_size() / sizeof(*data);
2443 struct window *win = get_window( req->window );
2445 reply->total = 0;
2446 if (!win) return;
2448 for (i = count = 0; i < win->prop_inuse; i++)
2449 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2450 reply->total = count;
2452 if (count > max) count = max;
2453 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2455 for (i = 0; i < win->prop_inuse && count; i++)
2457 if (win->properties[i].type == PROP_TYPE_FREE) continue;
2458 data->atom = win->properties[i].atom;
2459 data->string = (win->properties[i].type == PROP_TYPE_STRING);
2460 data->data = win->properties[i].data;
2461 data++;
2462 count--;
2467 /* get the new window pointer for a global window, checking permissions */
2468 /* helper for set_global_windows request */
2469 static int get_new_global_window( struct window **win, user_handle_t handle )
2471 if (!handle)
2473 *win = NULL;
2474 return 1;
2476 else if (*win)
2478 set_error( STATUS_ACCESS_DENIED );
2479 return 0;
2481 *win = get_window( handle );
2482 return (*win != NULL);
2485 /* Set/get the global windows */
2486 DECL_HANDLER(set_global_windows)
2488 struct window *new_shell_window = shell_window;
2489 struct window *new_shell_listview = shell_listview;
2490 struct window *new_progman_window = progman_window;
2491 struct window *new_taskman_window = taskman_window;
2493 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2494 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2495 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2496 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2498 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2500 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2501 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2503 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2505 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2507 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2509 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2511 shell_window = new_shell_window;
2512 shell_listview = new_shell_listview;
2513 progman_window = new_progman_window;
2514 taskman_window = new_taskman_window;
2517 /* retrieve layered info for a window */
2518 DECL_HANDLER(get_window_layered_info)
2520 struct window *win = get_window( req->handle );
2522 if (!win) return;
2524 if (win->is_layered)
2526 reply->color_key = win->color_key;
2527 reply->alpha = win->alpha;
2528 reply->flags = win->layered_flags;
2530 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2534 /* set layered info for a window */
2535 DECL_HANDLER(set_window_layered_info)
2537 struct window *win = get_window( req->handle );
2539 if (!win) return;
2541 if (win->ex_style & WS_EX_LAYERED)
2543 if (req->flags & LWA_ALPHA) win->alpha = req->alpha;
2544 else if (!win->is_layered) win->alpha = 0; /* alpha init value is 0 */
2546 win->color_key = req->color_key;
2547 win->layered_flags = req->flags;
2548 win->is_layered = 1;
2550 else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );