riched20: Fix test crash on WinXP-SP2.
[wine] / server / window.c
1 /*
2  * Server-side window handling
3  *
4  * Copyright (C) 2001 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdarg.h>
26
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"
34
35 #include "object.h"
36 #include "request.h"
37 #include "thread.h"
38 #include "process.h"
39 #include "user.h"
40 #include "unicode.h"
41
42 /* a window property */
43 struct property
44 {
45     unsigned short type;     /* property type (see below) */
46     atom_t         atom;     /* property atom */
47     obj_handle_t   handle;   /* property handle (user-defined storage) */
48 };
49
50 enum property_type
51 {
52     PROP_TYPE_FREE,   /* free entry */
53     PROP_TYPE_STRING, /* atom that was originally a string */
54     PROP_TYPE_ATOM    /* plain atom */
55 };
56
57
58 struct window
59 {
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     void*            instance;        /* creator instance */
80     int              is_unicode;      /* ANSI or unicode */
81     unsigned long    user_data;       /* user-specific data */
82     WCHAR           *text;            /* window caption text */
83     unsigned int     paint_flags;     /* various painting flags */
84     int              prop_inuse;      /* number of in-use window properties */
85     int              prop_alloc;      /* number of allocated window properties */
86     struct property *properties;      /* window properties array */
87     int              nb_extra_bytes;  /* number of extra bytes */
88     char             extra_bytes[1];  /* extra bytes storage */
89 };
90
91 #define PAINT_INTERNAL      0x01  /* internal WM_PAINT pending */
92 #define PAINT_ERASE         0x02  /* needs WM_ERASEBKGND */
93 #define PAINT_NONCLIENT     0x04  /* needs WM_NCPAINT */
94 #define PAINT_DELAYED_ERASE 0x08  /* still needs erase after WM_ERASEBKGND */
95
96 /* growable array of user handles */
97 struct user_handle_array
98 {
99     user_handle_t *handles;
100     int            count;
101     int            total;
102 };
103
104 /* global window pointers */
105 static struct window *shell_window;
106 static struct window *shell_listview;
107 static struct window *progman_window;
108 static struct window *taskman_window;
109
110 /* retrieve a pointer to a window from its handle */
111 static inline struct window *get_window( user_handle_t handle )
112 {
113     struct window *ret = get_user_object( handle, USER_WINDOW );
114     if (!ret) set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
115     return ret;
116 }
117
118 /* check if window is the desktop */
119 static inline int is_desktop_window( const struct window *win )
120 {
121     return !win->parent;  /* only desktop windows have no parent */
122 }
123
124 /* change the parent of a window (or unlink the window if the new parent is NULL) */
125 static int set_parent_window( struct window *win, struct window *parent )
126 {
127     struct window *ptr;
128
129     /* make sure parent is not a child of window */
130     for (ptr = parent; ptr; ptr = ptr->parent)
131     {
132         if (ptr == win)
133         {
134             set_error( STATUS_INVALID_PARAMETER );
135             return 0;
136         }
137     }
138
139     list_remove( &win->entry );  /* unlink it from the previous location */
140
141     if (parent)
142     {
143         win->parent = parent;
144         list_add_head( &parent->children, &win->entry );
145
146         /* if parent belongs to a different thread and the window isn't */
147         /* top-level, attach the two threads */
148         if (parent->thread && parent->thread != win->thread && !is_desktop_window(parent))
149             attach_thread_input( win->thread, parent->thread );
150     }
151     else  /* move it to parent unlinked list */
152     {
153         list_add_head( &win->parent->unlinked, &win->entry );
154     }
155     return 1;
156 }
157
158 /* get next window in Z-order list */
159 static inline struct window *get_next_window( struct window *win )
160 {
161     struct list *ptr = list_next( &win->parent->children, &win->entry );
162     if (ptr == &win->parent->unlinked) ptr = NULL;
163     return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
164 }
165
166 /* get previous window in Z-order list */
167 static inline struct window *get_prev_window( struct window *win )
168 {
169     struct list *ptr = list_prev( &win->parent->children, &win->entry );
170     if (ptr == &win->parent->unlinked) ptr = NULL;
171     return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
172 }
173
174 /* get first child in Z-order list */
175 static inline struct window *get_first_child( struct window *win )
176 {
177     struct list *ptr = list_head( &win->children );
178     return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
179 }
180
181 /* get last child in Z-order list */
182 static inline struct window *get_last_child( struct window *win )
183 {
184     struct list *ptr = list_tail( &win->children );
185     return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
186 }
187
188 /* append a user handle to a handle array */
189 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
190 {
191     if (array->count >= array->total)
192     {
193         int new_total = max( array->total * 2, 32 );
194         user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
195         if (!new_array)
196         {
197             free( array->handles );
198             set_error( STATUS_NO_MEMORY );
199             return 0;
200         }
201         array->handles = new_array;
202         array->total = new_total;
203     }
204     array->handles[array->count++] = handle;
205     return 1;
206 }
207
208 /* set a window property */
209 static void set_property( struct window *win, atom_t atom, obj_handle_t handle, enum property_type type )
210 {
211     int i, free = -1;
212     struct property *new_props;
213
214     /* check if it exists already */
215     for (i = 0; i < win->prop_inuse; i++)
216     {
217         if (win->properties[i].type == PROP_TYPE_FREE)
218         {
219             free = i;
220             continue;
221         }
222         if (win->properties[i].atom == atom)
223         {
224             win->properties[i].type = type;
225             win->properties[i].handle = handle;
226             return;
227         }
228     }
229
230     /* need to add an entry */
231     if (!grab_global_atom( NULL, atom )) return;
232     if (free == -1)
233     {
234         /* no free entry */
235         if (win->prop_inuse >= win->prop_alloc)
236         {
237             /* need to grow the array */
238             if (!(new_props = realloc( win->properties,
239                                        sizeof(*new_props) * (win->prop_alloc + 16) )))
240             {
241                 set_error( STATUS_NO_MEMORY );
242                 release_global_atom( NULL, atom );
243                 return;
244             }
245             win->prop_alloc += 16;
246             win->properties = new_props;
247         }
248         free = win->prop_inuse++;
249     }
250     win->properties[free].atom   = atom;
251     win->properties[free].type   = type;
252     win->properties[free].handle = handle;
253 }
254
255 /* remove a window property */
256 static obj_handle_t remove_property( struct window *win, atom_t atom )
257 {
258     int i;
259
260     for (i = 0; i < win->prop_inuse; i++)
261     {
262         if (win->properties[i].type == PROP_TYPE_FREE) continue;
263         if (win->properties[i].atom == atom)
264         {
265             release_global_atom( NULL, atom );
266             win->properties[i].type = PROP_TYPE_FREE;
267             return win->properties[i].handle;
268         }
269     }
270     /* FIXME: last error? */
271     return 0;
272 }
273
274 /* find a window property */
275 static obj_handle_t get_property( struct window *win, atom_t atom )
276 {
277     int i;
278
279     for (i = 0; i < win->prop_inuse; i++)
280     {
281         if (win->properties[i].type == PROP_TYPE_FREE) continue;
282         if (win->properties[i].atom == atom) return win->properties[i].handle;
283     }
284     /* FIXME: last error? */
285     return 0;
286 }
287
288 /* destroy all properties of a window */
289 static inline void destroy_properties( struct window *win )
290 {
291     int i;
292
293     if (!win->properties) return;
294     for (i = 0; i < win->prop_inuse; i++)
295     {
296         if (win->properties[i].type == PROP_TYPE_FREE) continue;
297         release_global_atom( NULL, win->properties[i].atom );
298     }
299     free( win->properties );
300 }
301
302 /* detach a window from its owner thread but keep the window around */
303 static void detach_window_thread( struct window *win )
304 {
305     struct thread *thread = win->thread;
306
307     if (!thread) return;
308     if (thread->queue)
309     {
310         if (win->update_region) inc_queue_paint_count( thread, -1 );
311         if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
312         queue_cleanup_window( thread, win->handle );
313     }
314     assert( thread->desktop_users > 0 );
315     thread->desktop_users--;
316     release_class( win->class );
317     win->class = NULL;
318
319     /* don't hold a reference to the desktop so that the desktop window can be */
320     /* destroyed when the desktop ref count reaches zero */
321     release_object( win->desktop );
322     win->thread = NULL;
323 }
324
325 /* destroy a window */
326 void destroy_window( struct window *win )
327 {
328     /* destroy all children */
329     while (!list_empty(&win->children))
330         destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
331     while (!list_empty(&win->unlinked))
332         destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
333
334     /* reset global window pointers, if the corresponding window is destroyed */
335     if (win == shell_window) shell_window = NULL;
336     if (win == shell_listview) shell_listview = NULL;
337     if (win == progman_window) progman_window = NULL;
338     if (win == taskman_window) taskman_window = NULL;
339     free_user_handle( win->handle );
340     destroy_properties( win );
341     list_remove( &win->entry );
342     if (is_desktop_window(win))
343     {
344         assert( win->desktop->top_window == win );
345         win->desktop->top_window = NULL;
346     }
347     detach_window_thread( win );
348     if (win->win_region) free_region( win->win_region );
349     if (win->update_region) free_region( win->update_region );
350     if (win->class) release_class( win->class );
351     free( win->text );
352     memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
353     free( win );
354 }
355
356 /* get the process owning the top window of a given desktop */
357 struct process *get_top_window_owner( struct desktop *desktop )
358 {
359     struct window *win = desktop->top_window;
360     if (!win || !win->thread) return NULL;
361     return win->thread->process;
362 }
363
364 /* attempt to close the desktop window when the last process using it is gone */
365 void close_desktop_window( struct desktop *desktop )
366 {
367     struct window *win = desktop->top_window;
368     if (win && win->thread) post_message( win->handle, WM_CLOSE, 0, 0 );
369 }
370
371 /* create a new window structure (note: the window is not linked in the window tree) */
372 static struct window *create_window( struct window *parent, struct window *owner,
373                                      atom_t atom, void *instance )
374 {
375     int extra_bytes;
376     struct window *win;
377     struct desktop *desktop;
378     struct window_class *class;
379
380     if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
381
382     if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
383     {
384         release_object( desktop );
385         return NULL;
386     }
387
388     if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
389     if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
390
391     win->parent         = parent;
392     win->owner          = owner ? owner->handle : 0;
393     win->thread         = current;
394     win->desktop        = desktop;
395     win->class          = class;
396     win->atom           = atom;
397     win->last_active    = win->handle;
398     win->win_region     = NULL;
399     win->update_region  = NULL;
400     win->style          = 0;
401     win->ex_style       = 0;
402     win->id             = 0;
403     win->instance       = NULL;
404     win->is_unicode     = 1;
405     win->user_data      = 0;
406     win->text           = NULL;
407     win->paint_flags    = 0;
408     win->prop_inuse     = 0;
409     win->prop_alloc     = 0;
410     win->properties     = NULL;
411     win->nb_extra_bytes = extra_bytes;
412     memset( win->extra_bytes, 0, extra_bytes );
413     list_init( &win->children );
414     list_init( &win->unlinked );
415
416     /* parent must be on the same desktop */
417     if (parent && parent->desktop != desktop)
418     {
419         set_error( STATUS_ACCESS_DENIED );
420         goto failed;
421     }
422
423     /* if no parent, class must be the desktop */
424     if (!parent && !is_desktop_class( class ))
425     {
426         set_error( STATUS_ACCESS_DENIED );
427         goto failed;
428     }
429
430     /* if parent belongs to a different thread and the window isn't */
431     /* top-level, attach the two threads */
432     if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
433     {
434         if (!attach_thread_input( current, parent->thread )) goto failed;
435     }
436     else  /* otherwise just make sure that the thread has a message queue */
437     {
438         if (!current->queue && !init_thread_queue( current )) goto failed;
439     }
440
441     /* put it on parent unlinked list */
442     if (parent) list_add_head( &parent->unlinked, &win->entry );
443     else
444     {
445         list_init( &win->entry );
446         assert( !desktop->top_window );
447         desktop->top_window = win;
448         set_process_default_desktop( current->process, desktop, current->desktop );
449     }
450
451     current->desktop_users++;
452     return win;
453
454 failed:
455     if (win)
456     {
457         if (win->handle) free_user_handle( win->handle );
458         free( win );
459     }
460     release_object( desktop );
461     release_class( class );
462     return NULL;
463 }
464
465 /* destroy all windows belonging to a given thread */
466 void destroy_thread_windows( struct thread *thread )
467 {
468     user_handle_t handle = 0;
469     struct window *win;
470
471     while ((win = next_user_handle( &handle, USER_WINDOW )))
472     {
473         if (win->thread != thread) continue;
474         if (is_desktop_window( win )) detach_window_thread( win );
475         else destroy_window( win );
476     }
477 }
478
479 /* get the desktop window */
480 static struct window *get_desktop_window( struct thread *thread, int create )
481 {
482     struct window *top_window;
483     struct desktop *desktop = get_thread_desktop( thread, 0 );
484
485     if (!desktop) return NULL;
486
487     if (!(top_window = desktop->top_window) && create)
488     {
489         if ((top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
490         {
491             detach_window_thread( top_window );
492             top_window->style  = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
493         }
494     }
495     release_object( desktop );
496     return top_window;
497 }
498
499 /* check whether child is a descendant of parent */
500 int is_child_window( user_handle_t parent, user_handle_t child )
501 {
502     struct window *child_ptr = get_user_object( child, USER_WINDOW );
503     struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
504
505     if (!child_ptr || !parent_ptr) return 0;
506     while (child_ptr->parent)
507     {
508         if (child_ptr->parent == parent_ptr) return 1;
509         child_ptr = child_ptr->parent;
510     }
511     return 0;
512 }
513
514 /* check whether window is a top-level window */
515 int is_top_level_window( user_handle_t window )
516 {
517     struct window *win = get_user_object( window, USER_WINDOW );
518     return (win && (is_desktop_window(win) || is_desktop_window(win->parent)));
519 }
520
521 /* make a window active if possible */
522 int make_window_active( user_handle_t window )
523 {
524     struct window *owner, *win = get_window( window );
525
526     if (!win) return 0;
527
528     /* set last active for window and its owner */
529     win->last_active = win->handle;
530     if ((owner = get_user_object( win->owner, USER_WINDOW ))) owner->last_active = win->handle;
531     return 1;
532 }
533
534 /* increment (or decrement) the window paint count */
535 static inline void inc_window_paint_count( struct window *win, int incr )
536 {
537     if (win->thread) inc_queue_paint_count( win->thread, incr );
538 }
539
540 /* check if window and all its ancestors are visible */
541 static int is_visible( const struct window *win )
542 {
543     while (win && win->parent)
544     {
545         if (!(win->style & WS_VISIBLE)) return 0;
546         win = win->parent;
547         /* if parent is minimized children are not visible */
548         if (win && (win->style & WS_MINIMIZE)) return 0;
549     }
550     return 1;
551 }
552
553 /* same as is_visible but takes a window handle */
554 int is_window_visible( user_handle_t window )
555 {
556     struct window *win = get_user_object( window, USER_WINDOW );
557     if (!win) return 0;
558     return is_visible( win );
559 }
560
561 /* check if point is inside the window */
562 static inline int is_point_in_window( struct window *win, int x, int y )
563 {
564     if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
565     if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
566         return 0;  /* disabled child */
567     if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
568         return 0;  /* transparent */
569     if (x < win->visible_rect.left || x >= win->visible_rect.right ||
570         y < win->visible_rect.top || y >= win->visible_rect.bottom)
571         return 0;  /* not in window */
572     if (win->win_region &&
573         !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
574         return 0;  /* not in window region */
575     return 1;
576 }
577
578 /* find child of 'parent' that contains the given point (in parent-relative coords) */
579 static struct window *child_window_from_point( struct window *parent, int x, int y )
580 {
581     struct window *ptr;
582
583     LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
584     {
585         if (!is_point_in_window( ptr, x, y )) continue;  /* skip it */
586
587         /* if window is minimized or disabled, return at once */
588         if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
589
590         /* if point is not in client area, return at once */
591         if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
592             y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
593             return ptr;
594
595         return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
596     }
597     return parent;  /* not found any child */
598 }
599
600 /* find all children of 'parent' that contain the given point */
601 static int get_window_children_from_point( struct window *parent, int x, int y,
602                                            struct user_handle_array *array )
603 {
604     struct window *ptr;
605
606     LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
607     {
608         if (!is_point_in_window( ptr, x, y )) continue;  /* skip it */
609
610         /* if point is in client area, and window is not minimized or disabled, check children */
611         if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
612             x >= ptr->client_rect.left && x < ptr->client_rect.right &&
613             y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
614         {
615             if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
616                                                  y - ptr->client_rect.top, array ))
617                 return 0;
618         }
619
620         /* now add window to the array */
621         if (!add_handle_to_array( array, ptr->handle )) return 0;
622     }
623     return 1;
624 }
625
626 /* find window containing point (in absolute coords) */
627 user_handle_t window_from_point( struct desktop *desktop, int x, int y )
628 {
629     struct window *ret;
630
631     if (!desktop->top_window) return 0;
632     ret = child_window_from_point( desktop->top_window, x, y );
633     return ret->handle;
634 }
635
636 /* return list of all windows containing point (in absolute coords) */
637 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
638 {
639     struct window *ptr;
640
641     /* make point relative to top window */
642     for (ptr = top->parent; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
643     {
644         x -= ptr->client_rect.left;
645         y -= ptr->client_rect.top;
646     }
647
648     if (!is_point_in_window( top, x, y )) return 1;
649
650     /* if point is in client area, and window is not minimized or disabled, check children */
651     if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
652         x >= top->client_rect.left && x < top->client_rect.right &&
653         y >= top->client_rect.top && y < top->client_rect.bottom)
654     {
655         if (!is_desktop_window(top))
656         {
657             x -= top->client_rect.left;
658             y -= top->client_rect.top;
659         }
660         if (!get_window_children_from_point( top, x, y, array )) return 0;
661     }
662     /* now add window to the array */
663     if (!add_handle_to_array( array, top->handle )) return 0;
664     return 1;
665 }
666
667
668 /* return the thread owning a window */
669 struct thread *get_window_thread( user_handle_t handle )
670 {
671     struct window *win = get_user_object( handle, USER_WINDOW );
672     if (!win || !win->thread) return NULL;
673     return (struct thread *)grab_object( win->thread );
674 }
675
676
677 /* check if any area of a window needs repainting */
678 static inline int win_needs_repaint( struct window *win )
679 {
680     return win->update_region || (win->paint_flags & PAINT_INTERNAL);
681 }
682
683
684 /* find a child of the specified window that needs repainting */
685 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
686 {
687     struct window *ptr, *ret = NULL;
688
689     LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
690     {
691         if (!(ptr->style & WS_VISIBLE)) continue;
692         if (ptr->thread == thread && win_needs_repaint( ptr ))
693             ret = ptr;
694         else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
695             ret = find_child_to_repaint( ptr, thread );
696         if (ret) break;
697     }
698
699     if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
700     {
701         /* transparent window, check for non-transparent sibling to paint first */
702         for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
703         {
704             if (!(ptr->style & WS_VISIBLE)) continue;
705             if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
706             if (ptr->thread != thread) continue;
707             if (win_needs_repaint( ptr )) return ptr;
708         }
709     }
710     return ret;
711 }
712
713
714 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
715 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
716 {
717     struct window *ptr, *win, *top_window = get_desktop_window( thread, 0 );
718
719     if (!top_window) return 0;
720
721     if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
722     else win = find_child_to_repaint( top_window, thread );
723
724     if (win && parent)
725     {
726         /* check that it is a child of the specified parent */
727         for (ptr = win; ptr; ptr = ptr->parent)
728             if (ptr->handle == parent) break;
729         /* otherwise don't return any window, we don't repaint a child before its parent */
730         if (!ptr) win = NULL;
731     }
732     if (!win) return 0;
733     win->paint_flags &= ~PAINT_INTERNAL;
734     return win->handle;
735 }
736
737
738 /* intersect the window region with the specified region, relative to the window parent */
739 static struct region *intersect_window_region( struct region *region, struct window *win )
740 {
741     /* make region relative to window rect */
742     offset_region( region, -win->window_rect.left, -win->window_rect.top );
743     if (!intersect_region( region, region, win->win_region )) return NULL;
744     /* make region relative to parent again */
745     offset_region( region, win->window_rect.left, win->window_rect.top );
746     return region;
747 }
748
749
750 /* convert coordinates from client to screen coords */
751 static inline void client_to_screen( struct window *win, int *x, int *y )
752 {
753     for ( ; win && !is_desktop_window(win); win = win->parent)
754     {
755         *x += win->client_rect.left;
756         *y += win->client_rect.top;
757     }
758 }
759
760 /* convert coordinates from client to screen coords */
761 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
762 {
763     for ( ; win && !is_desktop_window(win); win = win->parent)
764     {
765         rect->left   += win->client_rect.left;
766         rect->right  += win->client_rect.left;
767         rect->top    += win->client_rect.top;
768         rect->bottom += win->client_rect.top;
769     }
770 }
771
772 /* map the region from window to screen coordinates */
773 static inline void map_win_region_to_screen( struct window *win, struct region *region )
774 {
775     if (!is_desktop_window(win))
776     {
777         int x = win->window_rect.left;
778         int y = win->window_rect.top;
779         client_to_screen( win->parent, &x, &y );
780         offset_region( region, x, y );
781     }
782 }
783
784
785 /* clip all children of a given window out of the visible region */
786 static struct region *clip_children( struct window *parent, struct window *last,
787                                      struct region *region, int offset_x, int offset_y )
788 {
789     struct window *ptr;
790     struct region *tmp = create_empty_region();
791
792     if (!tmp) return NULL;
793     LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
794     {
795         if (ptr == last) break;
796         if (!(ptr->style & WS_VISIBLE)) continue;
797         if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
798         set_region_rect( tmp, &ptr->visible_rect );
799         if (ptr->win_region && !intersect_window_region( tmp, ptr ))
800         {
801             free_region( tmp );
802             return NULL;
803         }
804         offset_region( tmp, offset_x, offset_y );
805         if (!(region = subtract_region( region, region, tmp ))) break;
806         if (is_region_empty( region )) break;
807     }
808     free_region( tmp );
809     return region;
810 }
811
812
813 /* compute the intersection of two rectangles; return 0 if the result is empty */
814 static inline int intersect_rect( rectangle_t *dst, const rectangle_t *src1, const rectangle_t *src2 )
815 {
816     dst->left   = max( src1->left, src2->left );
817     dst->top    = max( src1->top, src2->top );
818     dst->right  = min( src1->right, src2->right );
819     dst->bottom = min( src1->bottom, src2->bottom );
820     return (dst->left < dst->right && dst->top < dst->bottom);
821 }
822
823
824 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
825 static void set_region_client_rect( struct region *region, struct window *win )
826 {
827     rectangle_t rect;
828
829     intersect_rect( &rect, &win->window_rect, &win->client_rect );
830     set_region_rect( region, &rect );
831 }
832
833
834 /* get the top-level window to clip against for a given window */
835 static inline struct window *get_top_clipping_window( struct window *win )
836 {
837     while (win->parent && !is_desktop_window(win->parent)) win = win->parent;
838     return win;
839 }
840
841
842 /* compute the visible region of a window, in window coordinates */
843 static struct region *get_visible_region( struct window *win, unsigned int flags )
844 {
845     struct region *tmp = NULL, *region;
846     int offset_x, offset_y;
847
848     if (!(region = create_empty_region())) return NULL;
849
850     /* first check if all ancestors are visible */
851
852     if (!is_visible( win )) return region;  /* empty region */
853
854     /* create a region relative to the window itself */
855
856     if ((flags & DCX_PARENTCLIP) && win->parent && !is_desktop_window(win->parent))
857     {
858         set_region_client_rect( region, win->parent );
859         offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
860     }
861     else if (flags & DCX_WINDOW)
862     {
863         set_region_rect( region, &win->visible_rect );
864         if (win->win_region && !intersect_window_region( region, win )) goto error;
865     }
866     else
867     {
868         set_region_client_rect( region, win );
869         if (win->win_region && !intersect_window_region( region, win )) goto error;
870     }
871
872     /* clip children */
873
874     if (flags & DCX_CLIPCHILDREN)
875     {
876         if (is_desktop_window(win)) offset_x = offset_y = 0;
877         else
878         {
879             offset_x = win->client_rect.left;
880             offset_y = win->client_rect.top;
881         }
882         if (!clip_children( win, NULL, region, offset_x, offset_y )) goto error;
883     }
884
885     /* clip siblings of ancestors */
886
887     if (is_desktop_window(win)) offset_x = offset_y = 0;
888     else
889     {
890         offset_x = win->window_rect.left;
891         offset_y = win->window_rect.top;
892     }
893
894     if ((tmp = create_empty_region()) != NULL)
895     {
896         while (win->parent)
897         {
898             /* we don't clip out top-level siblings as that's up to the native windowing system */
899             if ((win->style & WS_CLIPSIBLINGS) && !is_desktop_window( win->parent ))
900             {
901                 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
902                 if (is_region_empty( region )) break;
903             }
904             /* clip to parent client area */
905             win = win->parent;
906             if (!is_desktop_window(win))
907             {
908                 offset_x += win->client_rect.left;
909                 offset_y += win->client_rect.top;
910                 offset_region( region, win->client_rect.left, win->client_rect.top );
911             }
912             set_region_client_rect( tmp, win );
913             if (win->win_region && !intersect_window_region( tmp, win )) goto error;
914             if (!intersect_region( region, region, tmp )) goto error;
915             if (is_region_empty( region )) break;
916         }
917         free_region( tmp );
918     }
919     offset_region( region, -offset_x, -offset_y );  /* make it relative to target window */
920     return region;
921
922 error:
923     if (tmp) free_region( tmp );
924     free_region( region );
925     return NULL;
926 }
927
928
929 /* get the window class of a window */
930 struct window_class* get_window_class( user_handle_t window )
931 {
932     struct window *win;
933     if (!(win = get_window( window ))) return NULL;
934     if (!win->class) set_error( STATUS_ACCESS_DENIED );
935     return win->class;
936 }
937
938 /* return a copy of the specified region cropped to the window client or frame rectangle, */
939 /* and converted from client to window coordinates. Helper for (in)validate_window. */
940 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
941 {
942     struct region *tmp = create_empty_region();
943
944     if (!tmp) return NULL;
945
946     /* get bounding rect in client coords */
947     if (frame) set_region_rect( tmp, &win->window_rect );
948     else set_region_client_rect( tmp, win );
949     if (!is_desktop_window(win))
950         offset_region( tmp, -win->client_rect.left, -win->client_rect.top );
951
952     /* intersect specified region with bounding rect */
953     if (region && !intersect_region( tmp, region, tmp )) goto done;
954     if (is_region_empty( tmp )) goto done;
955
956     /* map it to window coords */
957     offset_region( tmp, win->client_rect.left - win->window_rect.left,
958                    win->client_rect.top - win->window_rect.top );
959     return tmp;
960
961 done:
962     free_region( tmp );
963     return NULL;
964 }
965
966
967 /* set a region as new update region for the window */
968 static void set_update_region( struct window *win, struct region *region )
969 {
970     if (region && !is_region_empty( region ))
971     {
972         if (!win->update_region) inc_window_paint_count( win, 1 );
973         else free_region( win->update_region );
974         win->update_region = region;
975     }
976     else
977     {
978         if (win->update_region)
979         {
980             inc_window_paint_count( win, -1 );
981             free_region( win->update_region );
982         }
983         win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
984         win->update_region = NULL;
985         if (region) free_region( region );
986     }
987 }
988
989
990 /* add a region to the update region; the passed region is freed or reused */
991 static int add_update_region( struct window *win, struct region *region )
992 {
993     if (win->update_region && !union_region( region, win->update_region, region ))
994     {
995         free_region( region );
996         return 0;
997     }
998     set_update_region( win, region );
999     return 1;
1000 }
1001
1002
1003 /* validate the non client area of a window */
1004 static void validate_non_client( struct window *win )
1005 {
1006     struct region *tmp;
1007     rectangle_t rect;
1008
1009     if (!win->update_region) return;  /* nothing to do */
1010
1011     /* get client rect in window coords */
1012     rect.left   = win->client_rect.left - win->window_rect.left;
1013     rect.top    = win->client_rect.top - win->window_rect.top;
1014     rect.right  = win->client_rect.right - win->window_rect.left;
1015     rect.bottom = win->client_rect.bottom - win->window_rect.top;
1016
1017     if ((tmp = create_empty_region()))
1018     {
1019         set_region_rect( tmp, &rect );
1020         if (intersect_region( tmp, win->update_region, tmp ))
1021             set_update_region( win, tmp );
1022         else
1023             free_region( tmp );
1024     }
1025     win->paint_flags &= ~PAINT_NONCLIENT;
1026 }
1027
1028
1029 /* validate a window completely so that we don't get any further paint messages for it */
1030 static void validate_whole_window( struct window *win )
1031 {
1032     set_update_region( win, NULL );
1033
1034     if (win->paint_flags & PAINT_INTERNAL)
1035     {
1036         win->paint_flags &= ~PAINT_INTERNAL;
1037         inc_window_paint_count( win, -1 );
1038     }
1039 }
1040
1041
1042 /* validate a window's children so that we don't get any further paint messages for it */
1043 static void validate_children( struct window *win )
1044 {
1045     struct window *child;
1046
1047     LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1048     {
1049         if (!(child->style & WS_VISIBLE)) continue;
1050         validate_children(child);
1051         validate_whole_window(child);
1052     }
1053 }
1054
1055
1056 /* validate the update region of a window on all parents; helper for get_update_region */
1057 static void validate_parents( struct window *child )
1058 {
1059     int offset_x = 0, offset_y = 0;
1060     struct window *win = child;
1061     struct region *tmp = NULL;
1062
1063     if (!child->update_region) return;
1064
1065     while (win->parent)
1066     {
1067         /* map to parent client coords */
1068         offset_x += win->window_rect.left;
1069         offset_y += win->window_rect.top;
1070
1071         win = win->parent;
1072
1073         /* and now map to window coords */
1074         offset_x += win->client_rect.left - win->window_rect.left;
1075         offset_y += win->client_rect.top - win->window_rect.top;
1076
1077         if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1078         {
1079             if (!tmp && !(tmp = create_empty_region())) return;
1080             offset_region( child->update_region, offset_x, offset_y );
1081             if (subtract_region( tmp, win->update_region, child->update_region ))
1082             {
1083                 set_update_region( win, tmp );
1084                 tmp = NULL;
1085             }
1086             /* restore child coords */
1087             offset_region( child->update_region, -offset_x, -offset_y );
1088         }
1089     }
1090     if (tmp) free_region( tmp );
1091 }
1092
1093
1094 /* add/subtract a region (in client coordinates) to the update region of the window */
1095 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1096 {
1097     struct region *tmp;
1098     struct window *child;
1099
1100     if (flags & RDW_INVALIDATE)
1101     {
1102         if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1103
1104         if (!add_update_region( win, tmp )) return;
1105
1106         if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1107         if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1108     }
1109     else if (flags & RDW_VALIDATE)
1110     {
1111         if (!region && (flags & RDW_NOFRAME))  /* shortcut: validate everything */
1112         {
1113             set_update_region( win, NULL );
1114         }
1115         else if (win->update_region)
1116         {
1117             if ((tmp = crop_region_to_win_rect( win, region, frame )))
1118             {
1119                 if (!subtract_region( tmp, win->update_region, tmp ))
1120                 {
1121                     free_region( tmp );
1122                     return;
1123                 }
1124                 set_update_region( win, tmp );
1125             }
1126             if (flags & RDW_NOFRAME) validate_non_client( win );
1127             if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1128         }
1129     }
1130
1131     if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1132     {
1133         win->paint_flags |= PAINT_INTERNAL;
1134         inc_window_paint_count( win, 1 );
1135     }
1136     else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1137     {
1138         win->paint_flags &= ~PAINT_INTERNAL;
1139         inc_window_paint_count( win, -1 );
1140     }
1141
1142     /* now process children recursively */
1143
1144     if (flags & RDW_NOCHILDREN) return;
1145     if (win->style & WS_MINIMIZE) return;
1146     if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1147
1148     if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1149
1150     /* map to client coordinates */
1151     offset_region( tmp, win->window_rect.left - win->client_rect.left,
1152                    win->window_rect.top - win->client_rect.top );
1153
1154     if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1155
1156     LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1157     {
1158         if (!(child->style & WS_VISIBLE)) continue;
1159         if (!rect_in_region( tmp, &child->window_rect )) continue;
1160         offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1161         redraw_window( child, tmp, 1, flags );
1162         offset_region( tmp, child->client_rect.left, child->client_rect.top );
1163     }
1164     free_region( tmp );
1165 }
1166
1167
1168 /* retrieve the update flags for a window depending on the state of the update region */
1169 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1170 {
1171     unsigned int ret = 0;
1172
1173     if (flags & UPDATE_NONCLIENT)
1174     {
1175         if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1176     }
1177     if (flags & UPDATE_ERASE)
1178     {
1179         if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1180     }
1181     if (flags & UPDATE_PAINT)
1182     {
1183         if (win->update_region)
1184         {
1185             if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1186             ret |= UPDATE_PAINT;
1187         }
1188     }
1189     if (flags & UPDATE_INTERNALPAINT)
1190     {
1191         if (win->paint_flags & PAINT_INTERNAL)
1192         {
1193             ret |= UPDATE_INTERNALPAINT;
1194             if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1195         }
1196     }
1197     return ret;
1198 }
1199
1200
1201 /* iterate through the children of the given window until we find one with some update flags */
1202 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1203                                             unsigned int flags, struct window **child )
1204 {
1205     struct window *ptr;
1206     unsigned int ret = 0;
1207
1208     /* first make sure we want to iterate children at all */
1209
1210     if (win->style & WS_MINIMIZE) return 0;
1211
1212     /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1213      * here we only want to repaint children of windows that clip them, others
1214      * need to wait for WM_PAINT to be done in the parent first.
1215      */
1216     if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1217
1218     LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1219     {
1220         if (from_child)  /* skip all children until from_child is found */
1221         {
1222             if (ptr == from_child) from_child = NULL;
1223             continue;
1224         }
1225         if (!(ptr->style & WS_VISIBLE)) continue;
1226         if ((ret = get_update_flags( ptr, flags )) != 0)
1227         {
1228             *child = ptr;
1229             break;
1230         }
1231         if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1232     }
1233     return ret;
1234 }
1235
1236 /* iterate through children and siblings of the given window until we find one with some update flags */
1237 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1238                                              unsigned int flags, struct window **child )
1239 {
1240     unsigned int ret;
1241     struct window *ptr, *from_sibling = NULL;
1242
1243     /* if some parent is not visible start from the next sibling */
1244
1245     if (!is_visible( win )) return 0;
1246     for (ptr = from_child; ptr; ptr = ptr->parent)
1247     {
1248         if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1249         if (ptr == win) break;
1250     }
1251
1252     /* non-client painting must be delayed if one of the parents is going to
1253      * be repainted and doesn't clip children */
1254
1255     if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1256     {
1257         for (ptr = win->parent; ptr; ptr = ptr->parent)
1258         {
1259             if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1260                 return 0;
1261         }
1262         if (from_child && !(flags & UPDATE_ALLCHILDREN))
1263         {
1264             for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1265             {
1266                 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1267                 if (ptr == win) break;
1268             }
1269         }
1270     }
1271
1272
1273     /* check window itself (only if not restarting from a child) */
1274
1275     if (!from_child)
1276     {
1277         if ((ret = get_update_flags( win, flags )))
1278         {
1279             *child = win;
1280             return ret;
1281         }
1282         from_child = win;
1283     }
1284
1285     /* now check children */
1286
1287     if (flags & UPDATE_NOCHILDREN) return 0;
1288     if (!from_sibling)
1289     {
1290         if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1291         from_sibling = from_child;
1292     }
1293
1294     /* then check siblings and parent siblings */
1295
1296     while (from_sibling->parent && from_sibling != win)
1297     {
1298         if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1299             return ret;
1300         from_sibling = from_sibling->parent;
1301     }
1302     return 0;
1303 }
1304
1305
1306 /* expose the areas revealed by a vis region change on the window parent */
1307 /* returns the region exposed on the window itself (in client coordinates) */
1308 static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
1309                                      struct region *old_vis_rgn )
1310 {
1311     struct window *parent = win;
1312     struct region *new_vis_rgn, *exposed_rgn;
1313
1314     if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
1315
1316     if ((exposed_rgn = create_empty_region()))
1317     {
1318         if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
1319         {
1320             /* make it relative to the new client area */
1321             offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
1322                            win->window_rect.top - win->client_rect.top );
1323         }
1324         else
1325         {
1326             free_region( exposed_rgn );
1327             exposed_rgn = NULL;
1328         }
1329     }
1330
1331     /* make it relative to the old window pos for subtracting */
1332     offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
1333                    win->window_rect.top - old_window_rect->top  );
1334
1335     if (subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) && !is_region_empty( new_vis_rgn ))
1336     {
1337         /* make it relative to new client rect again */
1338         int offset_x = old_window_rect->left - win->client_rect.left;
1339         int offset_y = old_window_rect->top - win->client_rect.top;
1340         if (win->parent && !is_desktop_window(win->parent))
1341         {
1342             offset_x += win->client_rect.left;
1343             offset_y += win->client_rect.top;
1344             parent = win->parent;
1345         }
1346         offset_region( new_vis_rgn, offset_x, offset_y );
1347         redraw_window( parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1348     }
1349     free_region( new_vis_rgn );
1350     return exposed_rgn;
1351 }
1352
1353
1354 /* set the window and client rectangles, updating the update region if necessary */
1355 static void set_window_pos( struct window *win, struct window *previous,
1356                             unsigned int swp_flags, const rectangle_t *window_rect,
1357                             const rectangle_t *client_rect, const rectangle_t *visible_rect,
1358                             const rectangle_t *valid_rects )
1359 {
1360     struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
1361     const rectangle_t old_window_rect = win->window_rect;
1362     const rectangle_t old_visible_rect = win->visible_rect;
1363     const rectangle_t old_client_rect = win->client_rect;
1364     int client_changed, frame_changed;
1365     int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1366
1367     if (win->parent && !is_visible( win->parent )) visible = 0;
1368
1369     if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
1370
1371     /* set the new window info before invalidating anything */
1372
1373     win->window_rect  = *window_rect;
1374     win->visible_rect = *visible_rect;
1375     win->client_rect  = *client_rect;
1376     if (!(swp_flags & SWP_NOZORDER) && win->parent)
1377     {
1378         list_remove( &win->entry );  /* unlink it from the previous location */
1379         if (previous) list_add_after( &previous->entry, &win->entry );
1380         else list_add_head( &win->parent->children, &win->entry );
1381     }
1382     if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1383     else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1384
1385     /* if the window is not visible, everything is easy */
1386     if (!visible) return;
1387
1388     /* expose anything revealed by the change */
1389
1390     if (!(swp_flags & SWP_NOREDRAW))
1391         exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
1392
1393     if (!(win->style & WS_VISIBLE))
1394     {
1395         /* clear the update region since the window is no longer visible */
1396         validate_whole_window( win );
1397         validate_children( win );
1398         goto done;
1399     }
1400
1401     /* crop update region to the new window rect */
1402
1403     if (win->update_region &&
1404         (window_rect->right - window_rect->left < old_window_rect.right - old_window_rect.left ||
1405          window_rect->bottom - window_rect->top < old_window_rect.bottom - old_window_rect.top))
1406     {
1407         struct region *tmp = create_empty_region();
1408         if (tmp)
1409         {
1410             set_region_rect( tmp, window_rect );
1411             if (!is_desktop_window(win))
1412                 offset_region( tmp, -window_rect->left, -window_rect->top );
1413             if (intersect_region( tmp, win->update_region, tmp ))
1414                 set_update_region( win, tmp );
1415             else
1416                 free_region( tmp );
1417         }
1418     }
1419
1420     if (swp_flags & SWP_NOREDRAW) goto done;  /* do not repaint anything */
1421
1422     /* expose the whole non-client area if it changed in any way */
1423
1424     if (swp_flags & SWP_NOCOPYBITS)
1425     {
1426         frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1427                          memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1428                          memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
1429         client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
1430     }
1431     else
1432     {
1433         /* assume the bits have been moved to follow the window rect */
1434         int x_offset = window_rect->left - old_window_rect.left;
1435         int y_offset = window_rect->top - old_window_rect.top;
1436         frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1437                          window_rect->right  - old_window_rect.right != x_offset ||
1438                          window_rect->bottom - old_window_rect.bottom != y_offset ||
1439                          visible_rect->left   - old_visible_rect.left   != x_offset ||
1440                          visible_rect->right  - old_visible_rect.right  != x_offset ||
1441                          visible_rect->top    - old_visible_rect.top    != y_offset ||
1442                          visible_rect->bottom - old_visible_rect.bottom != y_offset);
1443         client_changed = (client_rect->left   - old_client_rect.left   != x_offset ||
1444                           client_rect->right  - old_client_rect.right  != x_offset ||
1445                           client_rect->top    - old_client_rect.top    != y_offset ||
1446                           client_rect->bottom - old_client_rect.bottom != y_offset);
1447     }
1448
1449     if (frame_changed || client_changed)
1450     {
1451         struct region *tmp = create_empty_region();
1452
1453         if (tmp)
1454         {
1455             /* subtract the valid portion of client rect from the total region */
1456             if (!client_changed)
1457                 set_region_rect( tmp, client_rect );
1458             else if (valid_rects)
1459                 set_region_rect( tmp, &valid_rects[0] );
1460
1461             set_region_rect( old_vis_rgn, window_rect );
1462             if (!subtract_region( tmp, old_vis_rgn, tmp )) free_region( tmp );
1463             else
1464             {
1465                 if (!is_desktop_window(win))
1466                     offset_region( tmp, -client_rect->left, -client_rect->top );
1467                 if (exposed_rgn)
1468                 {
1469                     union_region( exposed_rgn, exposed_rgn, tmp );
1470                     free_region( tmp );
1471                 }
1472                 else exposed_rgn = tmp;
1473             }
1474         }
1475     }
1476
1477     if (exposed_rgn)
1478         redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1479
1480 done:
1481     free_region( old_vis_rgn );
1482     if (exposed_rgn) free_region( exposed_rgn );
1483     clear_error();  /* we ignore out of memory errors once the new rects have been set */
1484 }
1485
1486
1487 /* set the window region, updating the update region if necessary */
1488 static void set_window_region( struct window *win, struct region *region, int redraw )
1489 {
1490     struct region *old_vis_rgn = NULL, *exposed_rgn;
1491
1492     /* no need to redraw if window is not visible */
1493     if (redraw && !is_visible( win )) redraw = 0;
1494
1495     if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
1496
1497     if (win->win_region) free_region( win->win_region );
1498     win->win_region = region;
1499
1500     /* expose anything revealed by the change */
1501     if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
1502     {
1503         redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1504         free_region( exposed_rgn );
1505     }
1506
1507     if (old_vis_rgn) free_region( old_vis_rgn );
1508     clear_error();  /* we ignore out of memory errors since the region has been set */
1509 }
1510
1511
1512 /* create a window */
1513 DECL_HANDLER(create_window)
1514 {
1515     struct window *win, *parent, *owner = NULL;
1516
1517     reply->handle = 0;
1518
1519     if (!req->parent) parent = get_desktop_window( current, 0 );
1520     else if (!(parent = get_window( req->parent ))) return;
1521
1522     if (req->owner)
1523     {
1524         if (!(owner = get_window( req->owner ))) return;
1525         if (is_desktop_window(owner)) owner = NULL;
1526         else if (!is_desktop_window(parent))
1527         {
1528             /* an owned window must be created as top-level */
1529             set_error( STATUS_ACCESS_DENIED );
1530             return;
1531         }
1532         else /* owner must be a top-level window */
1533             while (!is_desktop_window(owner->parent)) owner = owner->parent;
1534     }
1535     if (!(win = create_window( parent, owner, req->atom, req->instance ))) return;
1536
1537     reply->handle    = win->handle;
1538     reply->parent    = win->parent ? win->parent->handle : 0;
1539     reply->owner     = win->owner;
1540     reply->extra     = win->nb_extra_bytes;
1541     reply->class_ptr = get_class_client_ptr( win->class );
1542 }
1543
1544
1545 /* set the parent of a window */
1546 DECL_HANDLER(set_parent)
1547 {
1548     struct window *win, *parent = NULL;
1549
1550     if (!(win = get_window( req->handle ))) return;
1551     if (req->parent && !(parent = get_window( req->parent ))) return;
1552
1553     if (is_desktop_window(win))
1554     {
1555         set_error( STATUS_INVALID_PARAMETER );
1556         return;
1557     }
1558     reply->old_parent  = win->parent->handle;
1559     reply->full_parent = parent ? parent->handle : 0;
1560     set_parent_window( win, parent );
1561 }
1562
1563
1564 /* destroy a window */
1565 DECL_HANDLER(destroy_window)
1566 {
1567     struct window *win = get_window( req->handle );
1568     if (win)
1569     {
1570         if (!is_desktop_window(win)) destroy_window( win );
1571         else if (win->thread == current) detach_window_thread( win );
1572         else set_error( STATUS_ACCESS_DENIED );
1573     }
1574 }
1575
1576
1577 /* retrieve the desktop window for the current thread */
1578 DECL_HANDLER(get_desktop_window)
1579 {
1580     struct window *win = get_desktop_window( current, req->force );
1581
1582     if (win) reply->handle = win->handle;
1583 }
1584
1585
1586 /* set a window owner */
1587 DECL_HANDLER(set_window_owner)
1588 {
1589     struct window *win = get_window( req->handle );
1590     struct window *owner = NULL;
1591
1592     if (!win) return;
1593     if (req->owner && !(owner = get_window( req->owner ))) return;
1594     if (is_desktop_window(win))
1595     {
1596         set_error( STATUS_ACCESS_DENIED );
1597         return;
1598     }
1599     reply->prev_owner = win->owner;
1600     reply->full_owner = win->owner = owner ? owner->handle : 0;
1601 }
1602
1603
1604 /* get information from a window handle */
1605 DECL_HANDLER(get_window_info)
1606 {
1607     struct window *win = get_window( req->handle );
1608
1609     reply->full_handle = 0;
1610     reply->tid = reply->pid = 0;
1611     if (win)
1612     {
1613         reply->full_handle = win->handle;
1614         reply->last_active = win->handle;
1615         reply->is_unicode  = win->is_unicode;
1616         if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
1617         if (win->thread)
1618         {
1619             reply->tid  = get_thread_id( win->thread );
1620             reply->pid  = get_process_id( win->thread->process );
1621             reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
1622         }
1623     }
1624 }
1625
1626
1627 /* set some information in a window */
1628 DECL_HANDLER(set_window_info)
1629 {
1630     struct window *win = get_window( req->handle );
1631
1632     if (!win) return;
1633     if (req->flags && is_desktop_window(win) && win->thread != current)
1634     {
1635         set_error( STATUS_ACCESS_DENIED );
1636         return;
1637     }
1638     if (req->extra_size > sizeof(req->extra_value) ||
1639         req->extra_offset < -1 ||
1640         req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
1641     {
1642         set_win32_error( ERROR_INVALID_INDEX );
1643         return;
1644     }
1645     if (req->extra_offset != -1)
1646     {
1647         memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
1648     }
1649     else if (req->flags & SET_WIN_EXTRA)
1650     {
1651         set_win32_error( ERROR_INVALID_INDEX );
1652         return;
1653     }
1654     reply->old_style     = win->style;
1655     reply->old_ex_style  = win->ex_style;
1656     reply->old_id        = win->id;
1657     reply->old_instance  = win->instance;
1658     reply->old_user_data = win->user_data;
1659     if (req->flags & SET_WIN_STYLE) win->style = req->style;
1660     if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
1661     if (req->flags & SET_WIN_ID) win->id = req->id;
1662     if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
1663     if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
1664     if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
1665     if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
1666                                             &req->extra_value, req->extra_size );
1667
1668     /* changing window style triggers a non-client paint */
1669     if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
1670 }
1671
1672
1673 /* get a list of the window parents, up to the root of the tree */
1674 DECL_HANDLER(get_window_parents)
1675 {
1676     struct window *ptr, *win = get_window( req->handle );
1677     int total = 0;
1678     user_handle_t *data;
1679     data_size_t len;
1680
1681     if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
1682
1683     reply->count = total;
1684     len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1685     if (len && ((data = set_reply_data_size( len ))))
1686     {
1687         for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
1688             *data++ = ptr->handle;
1689     }
1690 }
1691
1692
1693 /* get a list of the window children */
1694 DECL_HANDLER(get_window_children)
1695 {
1696     struct window *ptr, *parent = get_window( req->parent );
1697     int total = 0;
1698     user_handle_t *data;
1699     data_size_t len;
1700
1701     if (parent)
1702     {
1703         LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
1704         {
1705             if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1706             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1707             total++;
1708         }
1709     }
1710     reply->count = total;
1711     len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1712     if (len && ((data = set_reply_data_size( len ))))
1713     {
1714         LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
1715         {
1716             if (len < sizeof(*data)) break;
1717             if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1718             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1719             *data++ = ptr->handle;
1720             len -= sizeof(*data);
1721         }
1722     }
1723 }
1724
1725
1726 /* get a list of the window children that contain a given point */
1727 DECL_HANDLER(get_window_children_from_point)
1728 {
1729     struct user_handle_array array;
1730     struct window *parent = get_window( req->parent );
1731     data_size_t len;
1732
1733     if (!parent) return;
1734
1735     array.handles = NULL;
1736     array.count = 0;
1737     array.total = 0;
1738     if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
1739
1740     reply->count = array.count;
1741     len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
1742     if (len) set_reply_data_ptr( array.handles, len );
1743     else free( array.handles );
1744 }
1745
1746
1747 /* get window tree information from a window handle */
1748 DECL_HANDLER(get_window_tree)
1749 {
1750     struct window *ptr, *win = get_window( req->handle );
1751
1752     if (!win) return;
1753
1754     reply->parent        = 0;
1755     reply->owner         = 0;
1756     reply->next_sibling  = 0;
1757     reply->prev_sibling  = 0;
1758     reply->first_sibling = 0;
1759     reply->last_sibling  = 0;
1760     reply->first_child   = 0;
1761     reply->last_child    = 0;
1762
1763     if (win->parent)
1764     {
1765         struct window *parent = win->parent;
1766         reply->parent = parent->handle;
1767         reply->owner  = win->owner;
1768         if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
1769         if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
1770         if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
1771         if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
1772     }
1773     if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
1774     if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
1775 }
1776
1777
1778 /* set the position and Z order of a window */
1779 DECL_HANDLER(set_window_pos)
1780 {
1781     const rectangle_t *visible_rect = NULL, *valid_rects = NULL;
1782     struct window *previous = NULL;
1783     struct window *win = get_window( req->handle );
1784     unsigned int flags = req->flags;
1785
1786     if (!win) return;
1787     if (!win->parent) flags |= SWP_NOZORDER;  /* no Z order for the desktop */
1788
1789     if (!(flags & SWP_NOZORDER))
1790     {
1791         if (!req->previous)  /* special case: HWND_TOP */
1792         {
1793             if (get_first_child(win->parent) == win) flags |= SWP_NOZORDER;
1794         }
1795         else if (req->previous == (user_handle_t)1)  /* special case: HWND_BOTTOM */
1796         {
1797             previous = get_last_child( win->parent );
1798         }
1799         else
1800         {
1801             if (!(previous = get_window( req->previous ))) return;
1802             /* previous must be a sibling */
1803             if (previous->parent != win->parent)
1804             {
1805                 set_error( STATUS_INVALID_PARAMETER );
1806                 return;
1807             }
1808         }
1809         if (previous == win) flags |= SWP_NOZORDER;  /* nothing to do */
1810     }
1811
1812     /* window rectangle must be ordered properly */
1813     if (req->window.right < req->window.left || req->window.bottom < req->window.top)
1814     {
1815         set_error( STATUS_INVALID_PARAMETER );
1816         return;
1817     }
1818
1819     if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = get_req_data();
1820     if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rects = visible_rect + 1;
1821
1822     if (!visible_rect) visible_rect = &req->window;
1823     set_window_pos( win, previous, flags, &req->window, &req->client, visible_rect, valid_rects );
1824     reply->new_style = win->style;
1825 }
1826
1827
1828 /* get the window and client rectangles of a window */
1829 DECL_HANDLER(get_window_rectangles)
1830 {
1831     struct window *win = get_window( req->handle );
1832
1833     if (win)
1834     {
1835         reply->window  = win->window_rect;
1836         reply->visible = win->visible_rect;
1837         reply->client  = win->client_rect;
1838     }
1839 }
1840
1841
1842 /* get the window text */
1843 DECL_HANDLER(get_window_text)
1844 {
1845     struct window *win = get_window( req->handle );
1846
1847     if (win && win->text)
1848     {
1849         data_size_t len = strlenW( win->text ) * sizeof(WCHAR);
1850         if (len > get_reply_max_size()) len = get_reply_max_size();
1851         set_reply_data( win->text, len );
1852     }
1853 }
1854
1855
1856 /* set the window text */
1857 DECL_HANDLER(set_window_text)
1858 {
1859     struct window *win = get_window( req->handle );
1860
1861     if (win)
1862     {
1863         WCHAR *text = NULL;
1864         data_size_t len = get_req_data_size() / sizeof(WCHAR);
1865         if (len)
1866         {
1867             if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
1868             memcpy( text, get_req_data(), len * sizeof(WCHAR) );
1869             text[len] = 0;
1870         }
1871         free( win->text );
1872         win->text = text;
1873     }
1874 }
1875
1876
1877 /* get the coordinates offset between two windows */
1878 DECL_HANDLER(get_windows_offset)
1879 {
1880     struct window *win;
1881
1882     reply->x = reply->y = 0;
1883     if (req->from)
1884     {
1885         if (!(win = get_window( req->from ))) return;
1886         while (win && !is_desktop_window(win))
1887         {
1888             reply->x += win->client_rect.left;
1889             reply->y += win->client_rect.top;
1890             win = win->parent;
1891         }
1892     }
1893     if (req->to)
1894     {
1895         if (!(win = get_window( req->to ))) return;
1896         while (win && !is_desktop_window(win))
1897         {
1898             reply->x -= win->client_rect.left;
1899             reply->y -= win->client_rect.top;
1900             win = win->parent;
1901         }
1902     }
1903 }
1904
1905
1906 /* get the visible region of a window */
1907 DECL_HANDLER(get_visible_region)
1908 {
1909     struct region *region;
1910     struct window *top, *win = get_window( req->window );
1911
1912     if (!win) return;
1913
1914     top = get_top_clipping_window( win );
1915     if ((region = get_visible_region( win, req->flags )))
1916     {
1917         rectangle_t *data;
1918         map_win_region_to_screen( win, region );
1919         data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
1920         if (data) set_reply_data_ptr( data, reply->total_size );
1921     }
1922     reply->top_win  = top->handle;
1923     reply->top_rect = top->visible_rect;
1924
1925     if (!is_desktop_window(win))
1926     {
1927         reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
1928         client_to_screen_rect( top->parent, &reply->top_rect );
1929         client_to_screen_rect( win->parent, &reply->win_rect );
1930     }
1931     else
1932     {
1933         reply->win_rect.left   = 0;
1934         reply->win_rect.top    = 0;
1935         reply->win_rect.right  = win->client_rect.right - win->client_rect.left;
1936         reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
1937     }
1938 }
1939
1940
1941 /* get the window region */
1942 DECL_HANDLER(get_window_region)
1943 {
1944     struct window *win = get_window( req->window );
1945
1946     if (!win) return;
1947
1948     if (win->win_region)
1949     {
1950         rectangle_t *data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
1951         if (data) set_reply_data_ptr( data, reply->total_size );
1952     }
1953 }
1954
1955
1956 /* set the window region */
1957 DECL_HANDLER(set_window_region)
1958 {
1959     struct region *region = NULL;
1960     struct window *win = get_window( req->window );
1961
1962     if (!win) return;
1963
1964     if (get_req_data_size())  /* no data means remove the region completely */
1965     {
1966         if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1967             return;
1968     }
1969     set_window_region( win, region, req->redraw );
1970 }
1971
1972
1973 /* get a window update region */
1974 DECL_HANDLER(get_update_region)
1975 {
1976     rectangle_t *data;
1977     unsigned int flags = req->flags;
1978     struct window *from_child = NULL;
1979     struct window *win = get_window( req->window );
1980
1981     reply->flags = 0;
1982     if (!win) return;
1983
1984     if (req->from_child)
1985     {
1986         struct window *ptr;
1987
1988         if (!(from_child = get_window( req->from_child ))) return;
1989
1990         /* make sure from_child is a child of win */
1991         ptr = from_child;
1992         while (ptr && ptr != win) ptr = ptr->parent;
1993         if (!ptr)
1994         {
1995             set_error( STATUS_INVALID_PARAMETER );
1996             return;
1997         }
1998     }
1999
2000     if (flags & UPDATE_DELAYED_ERASE)  /* this means that the previous call didn't erase */
2001     {
2002         if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
2003         else win->paint_flags |= PAINT_DELAYED_ERASE;
2004     }
2005
2006     reply->flags = get_window_update_flags( win, from_child, flags, &win );
2007     reply->child = win->handle;
2008
2009     if (flags & UPDATE_NOREGION) return;
2010
2011     if (win->update_region)
2012     {
2013         /* convert update region to screen coordinates */
2014         struct region *region = create_empty_region();
2015
2016         if (!region) return;
2017         if (!copy_region( region, win->update_region ))
2018         {
2019             free_region( region );
2020             return;
2021         }
2022         map_win_region_to_screen( win, region );
2023         if (!(data = get_region_data_and_free( region, get_reply_max_size(),
2024                                                &reply->total_size ))) return;
2025         set_reply_data_ptr( data, reply->total_size );
2026     }
2027
2028     if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
2029     {
2030         validate_parents( win );
2031         validate_whole_window( win );
2032     }
2033     else
2034     {
2035         if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
2036         if (reply->flags & UPDATE_ERASE)
2037         {
2038             win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
2039             /* desktop window only gets erased, not repainted */
2040             if (is_desktop_window(win)) validate_whole_window( win );
2041         }
2042     }
2043 }
2044
2045
2046 /* update the z order of a window so that a given rectangle is fully visible */
2047 DECL_HANDLER(update_window_zorder)
2048 {
2049     rectangle_t tmp;
2050     struct window *ptr, *win = get_window( req->window );
2051
2052     if (!win || !win->parent || !is_visible( win )) return;  /* nothing to do */
2053
2054     LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2055     {
2056         if (ptr == win) break;
2057         if (!(ptr->style & WS_VISIBLE)) continue;
2058         if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2059         if (!intersect_rect( &tmp, &ptr->visible_rect, &req->rect )) continue;
2060         if (ptr->win_region && !rect_in_region( ptr->win_region, &req->rect )) continue;
2061         /* found a window obscuring the rectangle, now move win above this one */
2062         list_remove( &win->entry );
2063         list_add_before( &ptr->entry, &win->entry );
2064         break;
2065     }
2066 }
2067
2068
2069 /* mark parts of a window as needing a redraw */
2070 DECL_HANDLER(redraw_window)
2071 {
2072     struct region *region = NULL;
2073     struct window *win = get_window( req->window );
2074
2075     if (!win) return;
2076     if (!is_visible( win )) return;  /* nothing to do */
2077
2078     if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
2079     {
2080         if (get_req_data_size())  /* no data means whole rectangle */
2081         {
2082             if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2083                 return;
2084         }
2085     }
2086
2087     redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
2088                    req->flags );
2089     if (region) free_region( region );
2090 }
2091
2092
2093 /* set a window property */
2094 DECL_HANDLER(set_window_property)
2095 {
2096     struct window *win = get_window( req->window );
2097
2098     if (!win) return;
2099
2100     if (get_req_data_size())
2101     {
2102         atom_t atom = add_global_atom( NULL, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
2103         if (atom)
2104         {
2105             set_property( win, atom, req->handle, PROP_TYPE_STRING );
2106             release_global_atom( NULL, atom );
2107         }
2108     }
2109     else set_property( win, req->atom, req->handle, PROP_TYPE_ATOM );
2110 }
2111
2112
2113 /* remove a window property */
2114 DECL_HANDLER(remove_window_property)
2115 {
2116     struct window *win = get_window( req->window );
2117
2118     if (win)
2119     {
2120         atom_t atom = req->atom;
2121         if (get_req_data_size()) atom = find_global_atom( NULL, get_req_data(),
2122                                                           get_req_data_size() / sizeof(WCHAR) );
2123         if (atom) reply->handle = remove_property( win, atom );
2124     }
2125 }
2126
2127
2128 /* get a window property */
2129 DECL_HANDLER(get_window_property)
2130 {
2131     struct window *win = get_window( req->window );
2132
2133     if (win)
2134     {
2135         atom_t atom = req->atom;
2136         if (get_req_data_size()) atom = find_global_atom( NULL, get_req_data(),
2137                                                           get_req_data_size() / sizeof(WCHAR) );
2138         if (atom) reply->handle = get_property( win, atom );
2139     }
2140 }
2141
2142
2143 /* get the list of properties of a window */
2144 DECL_HANDLER(get_window_properties)
2145 {
2146     property_data_t *data;
2147     int i, count, max = get_reply_max_size() / sizeof(*data);
2148     struct window *win = get_window( req->window );
2149
2150     reply->total = 0;
2151     if (!win) return;
2152
2153     for (i = count = 0; i < win->prop_inuse; i++)
2154         if (win->properties[i].type != PROP_TYPE_FREE) count++;
2155     reply->total = count;
2156
2157     if (count > max) count = max;
2158     if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2159
2160     for (i = 0; i < win->prop_inuse && count; i++)
2161     {
2162         if (win->properties[i].type == PROP_TYPE_FREE) continue;
2163         data->atom   = win->properties[i].atom;
2164         data->string = (win->properties[i].type == PROP_TYPE_STRING);
2165         data->handle = win->properties[i].handle;
2166         data++;
2167         count--;
2168     }
2169 }
2170
2171
2172 /* get the new window pointer for a global window, checking permissions */
2173 /* helper for set_global_windows request */
2174 static int get_new_global_window( struct window **win, user_handle_t handle )
2175 {
2176     if (!handle)
2177     {
2178         *win = NULL;
2179         return 1;
2180     }
2181     else if (*win)
2182     {
2183         set_error( STATUS_ACCESS_DENIED );
2184         return 0;
2185     }
2186     *win = get_window( handle );
2187     return (*win != NULL);
2188 }
2189
2190 /* Set/get the global windows */
2191 DECL_HANDLER(set_global_windows)
2192 {
2193     struct window *new_shell_window   = shell_window;
2194     struct window *new_shell_listview = shell_listview;
2195     struct window *new_progman_window = progman_window;
2196     struct window *new_taskman_window = taskman_window;
2197
2198     reply->old_shell_window   = shell_window ? shell_window->handle : 0;
2199     reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2200     reply->old_progman_window = progman_window ? progman_window->handle : 0;
2201     reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2202
2203     if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2204     {
2205         if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2206         if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2207     }
2208     if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2209     {
2210         if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2211     }
2212     if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2213     {
2214         if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2215     }
2216     shell_window   = new_shell_window;
2217     shell_listview = new_shell_listview;
2218     progman_window = new_progman_window;
2219     taskman_window = new_taskman_window;
2220 }