wbemprox: Add support for uncommitted instances in IWbemClassObject::Get.
[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     lparam_t       data;     /* property data (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     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 */
94 };
95
96 /* flags that can be set by the client */
97 #define PAINT_HAS_SURFACE        SET_WINPOS_PAINT_SURFACE
98 #define PAINT_HAS_PIXEL_FORMAT   SET_WINPOS_PIXEL_FORMAT
99 #define PAINT_CLIENT_FLAGS       (PAINT_HAS_SURFACE | PAINT_HAS_PIXEL_FORMAT)
100 /* flags only manipulated by the server */
101 #define PAINT_INTERNAL           0x0010  /* internal WM_PAINT pending */
102 #define PAINT_ERASE              0x0020  /* needs WM_ERASEBKGND */
103 #define PAINT_NONCLIENT          0x0040  /* needs WM_NCPAINT */
104 #define PAINT_DELAYED_ERASE      0x0080  /* still needs erase after WM_ERASEBKGND */
105 #define PAINT_PIXEL_FORMAT_CHILD 0x0100  /* at least one child has a custom pixel format */
106
107 /* growable array of user handles */
108 struct user_handle_array
109 {
110     user_handle_t *handles;
111     int            count;
112     int            total;
113 };
114
115 /* global window pointers */
116 static struct window *shell_window;
117 static struct window *shell_listview;
118 static struct window *progman_window;
119 static struct window *taskman_window;
120
121 /* magic HWND_TOP etc. pointers */
122 #define WINPTR_TOP       ((struct window *)1L)
123 #define WINPTR_BOTTOM    ((struct window *)2L)
124 #define WINPTR_TOPMOST   ((struct window *)3L)
125 #define WINPTR_NOTOPMOST ((struct window *)4L)
126
127 /* retrieve a pointer to a window from its handle */
128 static inline struct window *get_window( user_handle_t handle )
129 {
130     struct window *ret = get_user_object( handle, USER_WINDOW );
131     if (!ret) set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
132     return ret;
133 }
134
135 /* check if window is the desktop */
136 static inline int is_desktop_window( const struct window *win )
137 {
138     return !win->parent;  /* only desktop windows have no parent */
139 }
140
141 /* get next window in Z-order list */
142 static inline struct window *get_next_window( struct window *win )
143 {
144     struct list *ptr = list_next( &win->parent->children, &win->entry );
145     return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
146 }
147
148 /* get previous window in Z-order list */
149 static inline struct window *get_prev_window( struct window *win )
150 {
151     struct list *ptr = list_prev( &win->parent->children, &win->entry );
152     return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
153 }
154
155 /* get first child in Z-order list */
156 static inline struct window *get_first_child( struct window *win )
157 {
158     struct list *ptr = list_head( &win->children );
159     return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
160 }
161
162 /* get last child in Z-order list */
163 static inline struct window *get_last_child( struct window *win )
164 {
165     struct list *ptr = list_tail( &win->children );
166     return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
167 }
168
169 /* set the PAINT_PIXEL_FORMAT_CHILD flag on all the parents */
170 /* note: we never reset the flag, it's just a heuristic */
171 static inline void update_pixel_format_flags( struct window *win )
172 {
173     for (win = win->parent; win && win->parent; win = win->parent)
174         win->paint_flags |= PAINT_PIXEL_FORMAT_CHILD;
175 }
176
177 /* link a window at the right place in the siblings list */
178 static void link_window( struct window *win, struct window *previous )
179 {
180     if (previous == WINPTR_NOTOPMOST)
181     {
182         if (!(win->ex_style & WS_EX_TOPMOST) && win->is_linked) return;  /* nothing to do */
183         win->ex_style &= ~WS_EX_TOPMOST;
184         previous = WINPTR_TOP;  /* fallback to the HWND_TOP case */
185     }
186
187     list_remove( &win->entry );  /* unlink it from the previous location */
188
189     if (previous == WINPTR_BOTTOM)
190     {
191         list_add_tail( &win->parent->children, &win->entry );
192         win->ex_style &= ~WS_EX_TOPMOST;
193     }
194     else if (previous == WINPTR_TOPMOST)
195     {
196         list_add_head( &win->parent->children, &win->entry );
197         win->ex_style |= WS_EX_TOPMOST;
198     }
199     else if (previous == WINPTR_TOP)
200     {
201         struct list *entry = win->parent->children.next;
202         if (!(win->ex_style & WS_EX_TOPMOST))  /* put it above the first non-topmost window */
203         {
204             while (entry != &win->parent->children)
205             {
206                 struct window *next = LIST_ENTRY( entry, struct window, entry );
207                 if (!(next->ex_style & WS_EX_TOPMOST)) break;
208                 if (next->handle == win->owner)  /* keep it above owner */
209                 {
210                     win->ex_style |= WS_EX_TOPMOST;
211                     break;
212                 }
213                 entry = entry->next;
214             }
215         }
216         list_add_before( entry, &win->entry );
217     }
218     else
219     {
220         list_add_after( &previous->entry, &win->entry );
221         if (!(previous->ex_style & WS_EX_TOPMOST)) win->ex_style &= ~WS_EX_TOPMOST;
222         else
223         {
224             struct window *next = get_next_window( win );
225             if (next && (next->ex_style & WS_EX_TOPMOST)) win->ex_style |= WS_EX_TOPMOST;
226         }
227     }
228
229     win->is_linked = 1;
230 }
231
232 /* change the parent of a window (or unlink the window if the new parent is NULL) */
233 static int set_parent_window( struct window *win, struct window *parent )
234 {
235     struct window *ptr;
236
237     /* make sure parent is not a child of window */
238     for (ptr = parent; ptr; ptr = ptr->parent)
239     {
240         if (ptr == win)
241         {
242             set_error( STATUS_INVALID_PARAMETER );
243             return 0;
244         }
245     }
246
247     if (parent)
248     {
249         win->parent = parent;
250         link_window( win, WINPTR_TOP );
251
252         /* if parent belongs to a different thread and the window isn't */
253         /* top-level, attach the two threads */
254         if (parent->thread && parent->thread != win->thread && !is_desktop_window(parent))
255             attach_thread_input( win->thread, parent->thread );
256
257         if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
258     }
259     else  /* move it to parent unlinked list */
260     {
261         list_remove( &win->entry );  /* unlink it from the previous location */
262         list_add_head( &win->parent->unlinked, &win->entry );
263         win->is_linked = 0;
264     }
265     return 1;
266 }
267
268 /* append a user handle to a handle array */
269 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
270 {
271     if (array->count >= array->total)
272     {
273         int new_total = max( array->total * 2, 32 );
274         user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
275         if (!new_array)
276         {
277             free( array->handles );
278             set_error( STATUS_NO_MEMORY );
279             return 0;
280         }
281         array->handles = new_array;
282         array->total = new_total;
283     }
284     array->handles[array->count++] = handle;
285     return 1;
286 }
287
288 /* set a window property */
289 static void set_property( struct window *win, atom_t atom, lparam_t data, enum property_type type )
290 {
291     int i, free = -1;
292     struct property *new_props;
293
294     /* check if it exists already */
295     for (i = 0; i < win->prop_inuse; i++)
296     {
297         if (win->properties[i].type == PROP_TYPE_FREE)
298         {
299             free = i;
300             continue;
301         }
302         if (win->properties[i].atom == atom)
303         {
304             win->properties[i].type = type;
305             win->properties[i].data = data;
306             return;
307         }
308     }
309
310     /* need to add an entry */
311     if (!grab_global_atom( NULL, atom )) return;
312     if (free == -1)
313     {
314         /* no free entry */
315         if (win->prop_inuse >= win->prop_alloc)
316         {
317             /* need to grow the array */
318             if (!(new_props = realloc( win->properties,
319                                        sizeof(*new_props) * (win->prop_alloc + 16) )))
320             {
321                 set_error( STATUS_NO_MEMORY );
322                 release_global_atom( NULL, atom );
323                 return;
324             }
325             win->prop_alloc += 16;
326             win->properties = new_props;
327         }
328         free = win->prop_inuse++;
329     }
330     win->properties[free].atom = atom;
331     win->properties[free].type = type;
332     win->properties[free].data = data;
333 }
334
335 /* remove a window property */
336 static lparam_t remove_property( struct window *win, atom_t atom )
337 {
338     int i;
339
340     for (i = 0; i < win->prop_inuse; i++)
341     {
342         if (win->properties[i].type == PROP_TYPE_FREE) continue;
343         if (win->properties[i].atom == atom)
344         {
345             release_global_atom( NULL, atom );
346             win->properties[i].type = PROP_TYPE_FREE;
347             return win->properties[i].data;
348         }
349     }
350     /* FIXME: last error? */
351     return 0;
352 }
353
354 /* find a window property */
355 static lparam_t get_property( struct window *win, atom_t atom )
356 {
357     int i;
358
359     for (i = 0; i < win->prop_inuse; i++)
360     {
361         if (win->properties[i].type == PROP_TYPE_FREE) continue;
362         if (win->properties[i].atom == atom) return win->properties[i].data;
363     }
364     /* FIXME: last error? */
365     return 0;
366 }
367
368 /* destroy all properties of a window */
369 static inline void destroy_properties( struct window *win )
370 {
371     int i;
372
373     if (!win->properties) return;
374     for (i = 0; i < win->prop_inuse; i++)
375     {
376         if (win->properties[i].type == PROP_TYPE_FREE) continue;
377         release_global_atom( NULL, win->properties[i].atom );
378     }
379     free( win->properties );
380 }
381
382 /* detach a window from its owner thread but keep the window around */
383 static void detach_window_thread( struct window *win )
384 {
385     struct thread *thread = win->thread;
386
387     if (!thread) return;
388     if (thread->queue)
389     {
390         if (win->update_region) inc_queue_paint_count( thread, -1 );
391         if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
392         queue_cleanup_window( thread, win->handle );
393     }
394     assert( thread->desktop_users > 0 );
395     thread->desktop_users--;
396     release_class( win->class );
397     win->class = NULL;
398
399     /* don't hold a reference to the desktop so that the desktop window can be */
400     /* destroyed when the desktop ref count reaches zero */
401     release_object( win->desktop );
402     win->thread = NULL;
403 }
404
405 /* get the process owning the top window of a given desktop */
406 struct process *get_top_window_owner( struct desktop *desktop )
407 {
408     struct window *win = desktop->top_window;
409     if (!win || !win->thread) return NULL;
410     return win->thread->process;
411 }
412
413 /* get the top window size of a given desktop */
414 void get_top_window_rectangle( struct desktop *desktop, rectangle_t *rect )
415 {
416     struct window *win = desktop->top_window;
417     if (!win) rect->left = rect->top = rect->right = rect->bottom = 0;
418     else *rect = win->window_rect;
419 }
420
421 /* post a message to the desktop window */
422 void post_desktop_message( struct desktop *desktop, unsigned int message,
423                            lparam_t wparam, lparam_t lparam )
424 {
425     struct window *win = desktop->top_window;
426     if (win && win->thread) post_message( win->handle, message, wparam, lparam );
427 }
428
429 /* create a new window structure (note: the window is not linked in the window tree) */
430 static struct window *create_window( struct window *parent, struct window *owner,
431                                      atom_t atom, mod_handle_t instance )
432 {
433     static const rectangle_t empty_rect;
434     int extra_bytes;
435     struct window *win = NULL;
436     struct desktop *desktop;
437     struct window_class *class;
438
439     if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
440
441     if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
442     {
443         release_object( desktop );
444         return NULL;
445     }
446
447     if (!parent)  /* null parent is only allowed for desktop or HWND_MESSAGE top window */
448     {
449         if (is_desktop_class( class ))
450             parent = desktop->top_window;  /* use existing desktop if any */
451         else if (is_hwnd_message_class( class ))
452             /* use desktop window if message window is already created */
453             parent = desktop->msg_window ? desktop->top_window : NULL;
454         else if (!(parent = desktop->top_window))  /* must already have a desktop then */
455         {
456             set_error( STATUS_ACCESS_DENIED );
457             goto failed;
458         }
459     }
460
461     /* parent must be on the same desktop */
462     if (parent && parent->desktop != desktop)
463     {
464         set_error( STATUS_ACCESS_DENIED );
465         goto failed;
466     }
467
468     if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
469     if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
470
471     win->parent         = parent;
472     win->owner          = owner ? owner->handle : 0;
473     win->thread         = current;
474     win->desktop        = desktop;
475     win->class          = class;
476     win->atom           = atom;
477     win->last_active    = win->handle;
478     win->win_region     = NULL;
479     win->update_region  = NULL;
480     win->style          = 0;
481     win->ex_style       = 0;
482     win->id             = 0;
483     win->instance       = 0;
484     win->is_unicode     = 1;
485     win->is_linked      = 0;
486     win->is_layered     = 0;
487     win->user_data      = 0;
488     win->text           = NULL;
489     win->paint_flags    = 0;
490     win->prop_inuse     = 0;
491     win->prop_alloc     = 0;
492     win->properties     = NULL;
493     win->nb_extra_bytes = extra_bytes;
494     win->window_rect = win->visible_rect = win->client_rect = empty_rect;
495     memset( win->extra_bytes, 0, extra_bytes );
496     list_init( &win->children );
497     list_init( &win->unlinked );
498
499     /* if parent belongs to a different thread and the window isn't */
500     /* top-level, attach the two threads */
501     if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
502     {
503         if (!attach_thread_input( current, parent->thread )) goto failed;
504     }
505     else  /* otherwise just make sure that the thread has a message queue */
506     {
507         if (!current->queue && !init_thread_queue( current )) goto failed;
508     }
509
510     /* put it on parent unlinked list */
511     if (parent) list_add_head( &parent->unlinked, &win->entry );
512     else
513     {
514         list_init( &win->entry );
515         if (is_desktop_class( class ))
516         {
517             assert( !desktop->top_window );
518             desktop->top_window = win;
519             set_process_default_desktop( current->process, desktop, current->desktop );
520         }
521         else
522         {
523             assert( !desktop->msg_window );
524             desktop->msg_window = win;
525         }
526     }
527
528     current->desktop_users++;
529     return win;
530
531 failed:
532     if (win)
533     {
534         if (win->handle) free_user_handle( win->handle );
535         free( win );
536     }
537     release_object( desktop );
538     release_class( class );
539     return NULL;
540 }
541
542 /* destroy all windows belonging to a given thread */
543 void destroy_thread_windows( struct thread *thread )
544 {
545     user_handle_t handle = 0;
546     struct window *win;
547
548     while ((win = next_user_handle( &handle, USER_WINDOW )))
549     {
550         if (win->thread != thread) continue;
551         if (is_desktop_window( win )) detach_window_thread( win );
552         else destroy_window( win );
553     }
554 }
555
556 /* get the desktop window */
557 static struct window *get_desktop_window( struct thread *thread )
558 {
559     struct window *top_window;
560     struct desktop *desktop = get_thread_desktop( thread, 0 );
561
562     if (!desktop) return NULL;
563     top_window = desktop->top_window;
564     release_object( desktop );
565     return top_window;
566 }
567
568 /* check whether child is a descendant of parent */
569 int is_child_window( user_handle_t parent, user_handle_t child )
570 {
571     struct window *child_ptr = get_user_object( child, USER_WINDOW );
572     struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
573
574     if (!child_ptr || !parent_ptr) return 0;
575     while (child_ptr->parent)
576     {
577         if (child_ptr->parent == parent_ptr) return 1;
578         child_ptr = child_ptr->parent;
579     }
580     return 0;
581 }
582
583 /* check whether window is a top-level window */
584 int is_top_level_window( user_handle_t window )
585 {
586     struct window *win = get_user_object( window, USER_WINDOW );
587     return (win && (is_desktop_window(win) || is_desktop_window(win->parent)));
588 }
589
590 /* make a window active if possible */
591 int make_window_active( user_handle_t window )
592 {
593     struct window *owner, *win = get_window( window );
594
595     if (!win) return 0;
596
597     /* set last active for window and its owners */
598     owner = win;
599     while (owner)
600     {
601         owner->last_active = win->handle;
602         owner = get_user_object( owner->owner, USER_WINDOW );
603     }
604     return 1;
605 }
606
607 /* increment (or decrement) the window paint count */
608 static inline void inc_window_paint_count( struct window *win, int incr )
609 {
610     if (win->thread) inc_queue_paint_count( win->thread, incr );
611 }
612
613 /* check if window and all its ancestors are visible */
614 static int is_visible( const struct window *win )
615 {
616     while (win)
617     {
618         if (!(win->style & WS_VISIBLE)) return 0;
619         win = win->parent;
620         /* if parent is minimized children are not visible */
621         if (win && (win->style & WS_MINIMIZE)) return 0;
622     }
623     return 1;
624 }
625
626 /* same as is_visible but takes a window handle */
627 int is_window_visible( user_handle_t window )
628 {
629     struct window *win = get_user_object( window, USER_WINDOW );
630     if (!win) return 0;
631     return is_visible( win );
632 }
633
634 int is_window_transparent( user_handle_t window )
635 {
636     struct window *win = get_user_object( window, USER_WINDOW );
637     if (!win) return 0;
638     return (win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT);
639 }
640
641 /* check if point is inside the window */
642 static inline int is_point_in_window( struct window *win, int x, int y )
643 {
644     if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
645     if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
646         return 0;  /* disabled child */
647     if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
648         return 0;  /* transparent */
649     if (x < win->visible_rect.left || x >= win->visible_rect.right ||
650         y < win->visible_rect.top || y >= win->visible_rect.bottom)
651         return 0;  /* not in window */
652     if (win->win_region &&
653         !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
654         return 0;  /* not in window region */
655     return 1;
656 }
657
658 /* fill an array with the handles of the children of a specified window */
659 static unsigned int get_children_windows( struct window *parent, atom_t atom, thread_id_t tid,
660                                           user_handle_t *handles, unsigned int max_count )
661 {
662     struct window *ptr;
663     unsigned int count = 0;
664
665     if (!parent) return 0;
666
667     LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
668     {
669         if (atom && get_class_atom(ptr->class) != atom) continue;
670         if (tid && get_thread_id(ptr->thread) != tid) continue;
671         if (handles)
672         {
673             if (count >= max_count) break;
674             handles[count] = ptr->handle;
675         }
676         count++;
677     }
678     return count;
679 }
680
681 /* find child of 'parent' that contains the given point (in parent-relative coords) */
682 static struct window *child_window_from_point( struct window *parent, int x, int y )
683 {
684     struct window *ptr;
685
686     LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
687     {
688         if (!is_point_in_window( ptr, x, y )) continue;  /* skip it */
689
690         /* if window is minimized or disabled, return at once */
691         if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
692
693         /* if point is not in client area, return at once */
694         if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
695             y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
696             return ptr;
697
698         return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
699     }
700     return parent;  /* not found any child */
701 }
702
703 /* find all children of 'parent' that contain the given point */
704 static int get_window_children_from_point( struct window *parent, int x, int y,
705                                            struct user_handle_array *array )
706 {
707     struct window *ptr;
708
709     LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
710     {
711         if (!is_point_in_window( ptr, x, y )) continue;  /* skip it */
712
713         /* if point is in client area, and window is not minimized or disabled, check children */
714         if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
715             x >= ptr->client_rect.left && x < ptr->client_rect.right &&
716             y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
717         {
718             if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
719                                                  y - ptr->client_rect.top, array ))
720                 return 0;
721         }
722
723         /* now add window to the array */
724         if (!add_handle_to_array( array, ptr->handle )) return 0;
725     }
726     return 1;
727 }
728
729 /* find window containing point (in absolute coords) */
730 user_handle_t window_from_point( struct desktop *desktop, int x, int y )
731 {
732     struct window *ret;
733
734     if (!desktop->top_window) return 0;
735     ret = child_window_from_point( desktop->top_window, x, y );
736     return ret->handle;
737 }
738
739 /* return list of all windows containing point (in absolute coords) */
740 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
741 {
742     struct window *ptr;
743
744     /* make point relative to top window */
745     for (ptr = top->parent; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
746     {
747         x -= ptr->client_rect.left;
748         y -= ptr->client_rect.top;
749     }
750
751     if (!is_point_in_window( top, x, y )) return 1;
752
753     /* if point is in client area, and window is not minimized or disabled, check children */
754     if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
755         x >= top->client_rect.left && x < top->client_rect.right &&
756         y >= top->client_rect.top && y < top->client_rect.bottom)
757     {
758         if (!is_desktop_window(top))
759         {
760             x -= top->client_rect.left;
761             y -= top->client_rect.top;
762         }
763         if (!get_window_children_from_point( top, x, y, array )) return 0;
764     }
765     /* now add window to the array */
766     if (!add_handle_to_array( array, top->handle )) return 0;
767     return 1;
768 }
769
770
771 /* return the thread owning a window */
772 struct thread *get_window_thread( user_handle_t handle )
773 {
774     struct window *win = get_user_object( handle, USER_WINDOW );
775     if (!win || !win->thread) return NULL;
776     return (struct thread *)grab_object( win->thread );
777 }
778
779
780 /* check if any area of a window needs repainting */
781 static inline int win_needs_repaint( struct window *win )
782 {
783     return win->update_region || (win->paint_flags & PAINT_INTERNAL);
784 }
785
786
787 /* find a child of the specified window that needs repainting */
788 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
789 {
790     struct window *ptr, *ret = NULL;
791
792     LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
793     {
794         if (!(ptr->style & WS_VISIBLE)) continue;
795         if (ptr->thread == thread && win_needs_repaint( ptr ))
796             ret = ptr;
797         else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
798             ret = find_child_to_repaint( ptr, thread );
799         if (ret) break;
800     }
801
802     if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
803     {
804         /* transparent window, check for non-transparent sibling to paint first */
805         for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
806         {
807             if (!(ptr->style & WS_VISIBLE)) continue;
808             if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
809             if (ptr->thread != thread) continue;
810             if (win_needs_repaint( ptr )) return ptr;
811         }
812     }
813     return ret;
814 }
815
816
817 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
818 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
819 {
820     struct window *ptr, *win, *top_window = get_desktop_window( thread );
821
822     if (!top_window) return 0;
823
824     if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
825     else win = find_child_to_repaint( top_window, thread );
826
827     if (win && parent)
828     {
829         /* check that it is a child of the specified parent */
830         for (ptr = win; ptr; ptr = ptr->parent)
831             if (ptr->handle == parent) break;
832         /* otherwise don't return any window, we don't repaint a child before its parent */
833         if (!ptr) win = NULL;
834     }
835     if (!win) return 0;
836     win->paint_flags &= ~PAINT_INTERNAL;
837     return win->handle;
838 }
839
840
841 /* intersect the window region with the specified region, relative to the window parent */
842 static struct region *intersect_window_region( struct region *region, struct window *win )
843 {
844     /* make region relative to window rect */
845     offset_region( region, -win->window_rect.left, -win->window_rect.top );
846     if (!intersect_region( region, region, win->win_region )) return NULL;
847     /* make region relative to parent again */
848     offset_region( region, win->window_rect.left, win->window_rect.top );
849     return region;
850 }
851
852
853 /* convert coordinates from client to screen coords */
854 static inline void client_to_screen( struct window *win, int *x, int *y )
855 {
856     for ( ; win && !is_desktop_window(win); win = win->parent)
857     {
858         *x += win->client_rect.left;
859         *y += win->client_rect.top;
860     }
861 }
862
863 /* convert coordinates from client to screen coords */
864 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
865 {
866     for ( ; win && !is_desktop_window(win); win = win->parent)
867     {
868         rect->left   += win->client_rect.left;
869         rect->right  += win->client_rect.left;
870         rect->top    += win->client_rect.top;
871         rect->bottom += win->client_rect.top;
872     }
873 }
874
875 /* map the region from window to screen coordinates */
876 static inline void map_win_region_to_screen( struct window *win, struct region *region )
877 {
878     if (!is_desktop_window(win))
879     {
880         int x = win->window_rect.left;
881         int y = win->window_rect.top;
882         client_to_screen( win->parent, &x, &y );
883         offset_region( region, x, y );
884     }
885 }
886
887
888 /* clip all children of a given window out of the visible region */
889 static struct region *clip_children( struct window *parent, struct window *last,
890                                      struct region *region, int offset_x, int offset_y )
891 {
892     struct window *ptr;
893     struct region *tmp = create_empty_region();
894
895     if (!tmp) return NULL;
896     LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
897     {
898         if (ptr == last) break;
899         if (!(ptr->style & WS_VISIBLE)) continue;
900         if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
901         set_region_rect( tmp, &ptr->visible_rect );
902         if (ptr->win_region && !intersect_window_region( tmp, ptr ))
903         {
904             free_region( tmp );
905             return NULL;
906         }
907         offset_region( tmp, offset_x, offset_y );
908         if (!(region = subtract_region( region, region, tmp ))) break;
909         if (is_region_empty( region )) break;
910     }
911     free_region( tmp );
912     return region;
913 }
914
915
916 /* offset the coordinates of a rectangle */
917 static inline void offset_rect( rectangle_t *rect, int offset_x, int offset_y )
918 {
919     rect->left   += offset_x;
920     rect->top    += offset_y;
921     rect->right  += offset_x;
922     rect->bottom += offset_y;
923 }
924
925
926 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
927 static void set_region_client_rect( struct region *region, struct window *win )
928 {
929     rectangle_t rect;
930
931     intersect_rect( &rect, &win->window_rect, &win->client_rect );
932     set_region_rect( region, &rect );
933 }
934
935
936 /* get the top-level window to clip against for a given window */
937 static inline struct window *get_top_clipping_window( struct window *win )
938 {
939     while (!(win->paint_flags & PAINT_HAS_SURFACE) && win->parent && !is_desktop_window(win->parent))
940         win = win->parent;
941     return win;
942 }
943
944
945 /* compute the visible region of a window, in window coordinates */
946 static struct region *get_visible_region( struct window *win, unsigned int flags )
947 {
948     struct region *tmp = NULL, *region;
949     int offset_x, offset_y;
950
951     if (!(region = create_empty_region())) return NULL;
952
953     /* first check if all ancestors are visible */
954
955     if (!is_visible( win )) return region;  /* empty region */
956
957     /* create a region relative to the window itself */
958
959     if ((flags & DCX_PARENTCLIP) && win->parent && !is_desktop_window(win->parent))
960     {
961         set_region_client_rect( region, win->parent );
962         offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
963     }
964     else if (flags & DCX_WINDOW)
965     {
966         set_region_rect( region, &win->visible_rect );
967         if (win->win_region && !intersect_window_region( region, win )) goto error;
968     }
969     else
970     {
971         set_region_client_rect( region, win );
972         if (win->win_region && !intersect_window_region( region, win )) goto error;
973     }
974
975     /* clip children */
976
977     if (flags & DCX_CLIPCHILDREN)
978     {
979         if (is_desktop_window(win)) offset_x = offset_y = 0;
980         else
981         {
982             offset_x = win->client_rect.left;
983             offset_y = win->client_rect.top;
984         }
985         if (!clip_children( win, NULL, region, offset_x, offset_y )) goto error;
986     }
987
988     /* clip siblings of ancestors */
989
990     if (is_desktop_window(win)) offset_x = offset_y = 0;
991     else
992     {
993         offset_x = win->window_rect.left;
994         offset_y = win->window_rect.top;
995     }
996
997     if ((tmp = create_empty_region()) != NULL)
998     {
999         while (win->parent)
1000         {
1001             /* we don't clip out top-level siblings as that's up to the native windowing system */
1002             if ((win->style & WS_CLIPSIBLINGS) && !is_desktop_window( win->parent ))
1003             {
1004                 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
1005                 if (is_region_empty( region )) break;
1006             }
1007             /* clip to parent client area */
1008             win = win->parent;
1009             if (!is_desktop_window(win))
1010             {
1011                 offset_x += win->client_rect.left;
1012                 offset_y += win->client_rect.top;
1013                 offset_region( region, win->client_rect.left, win->client_rect.top );
1014             }
1015             set_region_client_rect( tmp, win );
1016             if (win->win_region && !intersect_window_region( tmp, win )) goto error;
1017             if (!intersect_region( region, region, tmp )) goto error;
1018             if (is_region_empty( region )) break;
1019         }
1020         free_region( tmp );
1021     }
1022     offset_region( region, -offset_x, -offset_y );  /* make it relative to target window */
1023     return region;
1024
1025 error:
1026     if (tmp) free_region( tmp );
1027     free_region( region );
1028     return NULL;
1029 }
1030
1031
1032 /* clip all children with a custom pixel format out of the visible region */
1033 static struct region *clip_pixel_format_children( struct window *parent, struct region *parent_clip,
1034                                                   struct region *region, int offset_x, int offset_y )
1035 {
1036     struct window *ptr;
1037     struct region *clip = create_empty_region();
1038
1039     if (!clip) return NULL;
1040
1041     LIST_FOR_EACH_ENTRY_REV( ptr, &parent->children, struct window, entry )
1042     {
1043         if (!(ptr->style & WS_VISIBLE)) continue;
1044         if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1045
1046         /* add the visible rect */
1047         set_region_rect( clip, &ptr->visible_rect );
1048         if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1049         offset_region( clip, offset_x, offset_y );
1050         if (!intersect_region( clip, clip, parent_clip )) break;
1051         if (!union_region( region, region, clip )) break;
1052         if (!(ptr->paint_flags & (PAINT_HAS_PIXEL_FORMAT | PAINT_PIXEL_FORMAT_CHILD))) continue;
1053
1054         /* subtract the client rect if it uses a custom pixel format */
1055         set_region_rect( clip, &ptr->client_rect );
1056         if (ptr->win_region && !intersect_window_region( clip, ptr )) break;
1057         offset_region( clip, offset_x, offset_y );
1058         if (!intersect_region( clip, clip, parent_clip )) break;
1059         if ((ptr->paint_flags & PAINT_HAS_PIXEL_FORMAT) && !subtract_region( region, region, clip ))
1060             break;
1061
1062         if (!clip_pixel_format_children( ptr, clip, region, offset_x + ptr->client_rect.left,
1063                                          offset_y + ptr->client_rect.top ))
1064             break;
1065     }
1066     free_region( clip );
1067     return region;
1068 }
1069
1070
1071 /* compute the visible surface region of a window, in parent coordinates */
1072 static struct region *get_surface_region( struct window *win )
1073 {
1074     struct region *region, *clip;
1075     int offset_x, offset_y;
1076
1077     /* create a region relative to the window itself */
1078
1079     if (!(region = create_empty_region())) return NULL;
1080     if (!(clip = create_empty_region())) goto error;
1081     set_region_rect( region, &win->visible_rect );
1082     if (win->win_region && !intersect_window_region( region, win )) goto error;
1083     set_region_rect( clip, &win->client_rect );
1084     if (win->win_region && !intersect_window_region( clip, win )) goto error;
1085
1086     /* clip children */
1087
1088     if (!is_desktop_window(win))
1089     {
1090         offset_x = win->client_rect.left;
1091         offset_y = win->client_rect.top;
1092     }
1093     else offset_x = offset_y = 0;
1094
1095     if (!clip_pixel_format_children( win, clip, region, offset_x, offset_y )) goto error;
1096
1097     free_region( clip );
1098     return region;
1099
1100 error:
1101     if (clip) free_region( clip );
1102     free_region( region );
1103     return NULL;
1104 }
1105
1106
1107 /* get the window class of a window */
1108 struct window_class* get_window_class( user_handle_t window )
1109 {
1110     struct window *win;
1111     if (!(win = get_window( window ))) return NULL;
1112     if (!win->class) set_error( STATUS_ACCESS_DENIED );
1113     return win->class;
1114 }
1115
1116 /* determine the window visible rectangle, i.e. window or client rect cropped by parent rects */
1117 /* the returned rectangle is in window coordinates; return 0 if rectangle is empty */
1118 static int get_window_visible_rect( struct window *win, rectangle_t *rect, int frame )
1119 {
1120     int offset_x = 0, offset_y = 0;
1121
1122     if (!(win->style & WS_VISIBLE)) return 0;
1123
1124     *rect = frame ? win->window_rect : win->client_rect;
1125     if (!is_desktop_window(win))
1126     {
1127         offset_x = win->window_rect.left;
1128         offset_y = win->window_rect.top;
1129     }
1130
1131     while (win->parent)
1132     {
1133         win = win->parent;
1134         if (!(win->style & WS_VISIBLE) || win->style & WS_MINIMIZE) return 0;
1135         if (!is_desktop_window(win))
1136         {
1137             offset_x += win->client_rect.left;
1138             offset_y += win->client_rect.top;
1139             offset_rect( rect, win->client_rect.left, win->client_rect.top );
1140         }
1141         if (!intersect_rect( rect, rect, &win->client_rect )) return 0;
1142         if (!intersect_rect( rect, rect, &win->window_rect )) return 0;
1143     }
1144     offset_rect( rect, -offset_x, -offset_y );
1145     return 1;
1146 }
1147
1148 /* return a copy of the specified region cropped to the window client or frame rectangle, */
1149 /* and converted from client to window coordinates. Helper for (in)validate_window. */
1150 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
1151 {
1152     rectangle_t rect;
1153     struct region *tmp;
1154
1155     if (!get_window_visible_rect( win, &rect, frame )) return NULL;
1156     if (!(tmp = create_empty_region())) return NULL;
1157     set_region_rect( tmp, &rect );
1158
1159     if (region)
1160     {
1161         /* map it to client coords */
1162         offset_region( tmp, win->window_rect.left - win->client_rect.left,
1163                        win->window_rect.top - win->client_rect.top );
1164
1165         /* intersect specified region with bounding rect */
1166         if (!intersect_region( tmp, region, tmp )) goto done;
1167         if (is_region_empty( tmp )) goto done;
1168
1169         /* map it back to window coords */
1170         offset_region( tmp, win->client_rect.left - win->window_rect.left,
1171                        win->client_rect.top - win->window_rect.top );
1172     }
1173     return tmp;
1174
1175 done:
1176     free_region( tmp );
1177     return NULL;
1178 }
1179
1180
1181 /* set a region as new update region for the window */
1182 static void set_update_region( struct window *win, struct region *region )
1183 {
1184     if (region && !is_region_empty( region ))
1185     {
1186         if (!win->update_region) inc_window_paint_count( win, 1 );
1187         else free_region( win->update_region );
1188         win->update_region = region;
1189     }
1190     else
1191     {
1192         if (win->update_region)
1193         {
1194             inc_window_paint_count( win, -1 );
1195             free_region( win->update_region );
1196         }
1197         win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
1198         win->update_region = NULL;
1199         if (region) free_region( region );
1200     }
1201 }
1202
1203
1204 /* add a region to the update region; the passed region is freed or reused */
1205 static int add_update_region( struct window *win, struct region *region )
1206 {
1207     if (win->update_region && !union_region( region, win->update_region, region ))
1208     {
1209         free_region( region );
1210         return 0;
1211     }
1212     set_update_region( win, region );
1213     return 1;
1214 }
1215
1216
1217 /* crop the update region of children to the specified rectangle, in client coords */
1218 static void crop_children_update_region( struct window *win, rectangle_t *rect )
1219 {
1220     struct window *child;
1221     struct region *tmp;
1222     rectangle_t child_rect;
1223
1224     LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1225     {
1226         if (!(child->style & WS_VISIBLE)) continue;
1227         if (!rect)  /* crop everything out */
1228         {
1229             crop_children_update_region( child, NULL );
1230             set_update_region( child, NULL );
1231             continue;
1232         }
1233
1234         /* nothing to do if child is completely inside rect */
1235         if (child->window_rect.left >= rect->left &&
1236             child->window_rect.top >= rect->top &&
1237             child->window_rect.right <= rect->right &&
1238             child->window_rect.bottom <= rect->bottom) continue;
1239
1240         /* map to child client coords and crop grand-children */
1241         child_rect = *rect;
1242         offset_rect( &child_rect, -child->client_rect.left, -child->client_rect.top );
1243         crop_children_update_region( child, &child_rect );
1244
1245         /* now crop the child itself */
1246         if (!child->update_region) continue;
1247         if (!(tmp = create_empty_region())) continue;
1248         set_region_rect( tmp, rect );
1249         offset_region( tmp, -child->window_rect.left, -child->window_rect.top );
1250         if (intersect_region( tmp, child->update_region, tmp )) set_update_region( child, tmp );
1251         else free_region( tmp );
1252     }
1253 }
1254
1255
1256 /* validate the non client area of a window */
1257 static void validate_non_client( struct window *win )
1258 {
1259     struct region *tmp;
1260     rectangle_t rect;
1261
1262     if (!win->update_region) return;  /* nothing to do */
1263
1264     /* get client rect in window coords */
1265     rect.left   = win->client_rect.left - win->window_rect.left;
1266     rect.top    = win->client_rect.top - win->window_rect.top;
1267     rect.right  = win->client_rect.right - win->window_rect.left;
1268     rect.bottom = win->client_rect.bottom - win->window_rect.top;
1269
1270     if ((tmp = create_empty_region()))
1271     {
1272         set_region_rect( tmp, &rect );
1273         if (intersect_region( tmp, win->update_region, tmp ))
1274             set_update_region( win, tmp );
1275         else
1276             free_region( tmp );
1277     }
1278     win->paint_flags &= ~PAINT_NONCLIENT;
1279 }
1280
1281
1282 /* validate a window completely so that we don't get any further paint messages for it */
1283 static void validate_whole_window( struct window *win )
1284 {
1285     set_update_region( win, NULL );
1286
1287     if (win->paint_flags & PAINT_INTERNAL)
1288     {
1289         win->paint_flags &= ~PAINT_INTERNAL;
1290         inc_window_paint_count( win, -1 );
1291     }
1292 }
1293
1294
1295 /* validate a window's children so that we don't get any further paint messages for it */
1296 static void validate_children( struct window *win )
1297 {
1298     struct window *child;
1299
1300     LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1301     {
1302         if (!(child->style & WS_VISIBLE)) continue;
1303         validate_children(child);
1304         validate_whole_window(child);
1305     }
1306 }
1307
1308
1309 /* validate the update region of a window on all parents; helper for get_update_region */
1310 static void validate_parents( struct window *child )
1311 {
1312     int offset_x = 0, offset_y = 0;
1313     struct window *win = child;
1314     struct region *tmp = NULL;
1315
1316     if (!child->update_region) return;
1317
1318     while (win->parent)
1319     {
1320         /* map to parent client coords */
1321         offset_x += win->window_rect.left;
1322         offset_y += win->window_rect.top;
1323
1324         win = win->parent;
1325
1326         /* and now map to window coords */
1327         offset_x += win->client_rect.left - win->window_rect.left;
1328         offset_y += win->client_rect.top - win->window_rect.top;
1329
1330         if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1331         {
1332             if (!tmp && !(tmp = create_empty_region())) return;
1333             offset_region( child->update_region, offset_x, offset_y );
1334             if (subtract_region( tmp, win->update_region, child->update_region ))
1335             {
1336                 set_update_region( win, tmp );
1337                 tmp = NULL;
1338             }
1339             /* restore child coords */
1340             offset_region( child->update_region, -offset_x, -offset_y );
1341         }
1342     }
1343     if (tmp) free_region( tmp );
1344 }
1345
1346
1347 /* add/subtract a region (in client coordinates) to the update region of the window */
1348 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1349 {
1350     struct region *tmp;
1351     struct window *child;
1352
1353     if (flags & RDW_INVALIDATE)
1354     {
1355         if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1356
1357         if (!add_update_region( win, tmp )) return;
1358
1359         if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1360         if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1361     }
1362     else if (flags & RDW_VALIDATE)
1363     {
1364         if (!region && (flags & RDW_NOFRAME))  /* shortcut: validate everything */
1365         {
1366             set_update_region( win, NULL );
1367         }
1368         else if (win->update_region)
1369         {
1370             if ((tmp = crop_region_to_win_rect( win, region, frame )))
1371             {
1372                 if (!subtract_region( tmp, win->update_region, tmp ))
1373                 {
1374                     free_region( tmp );
1375                     return;
1376                 }
1377                 set_update_region( win, tmp );
1378             }
1379             if (flags & RDW_NOFRAME) validate_non_client( win );
1380             if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1381         }
1382     }
1383
1384     if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1385     {
1386         win->paint_flags |= PAINT_INTERNAL;
1387         inc_window_paint_count( win, 1 );
1388     }
1389     else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1390     {
1391         win->paint_flags &= ~PAINT_INTERNAL;
1392         inc_window_paint_count( win, -1 );
1393     }
1394
1395     /* now process children recursively */
1396
1397     if (flags & RDW_NOCHILDREN) return;
1398     if (win->style & WS_MINIMIZE) return;
1399     if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1400
1401     if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1402
1403     /* map to client coordinates */
1404     offset_region( tmp, win->window_rect.left - win->client_rect.left,
1405                    win->window_rect.top - win->client_rect.top );
1406
1407     if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1408
1409     LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1410     {
1411         if (!(child->style & WS_VISIBLE)) continue;
1412         if (!rect_in_region( tmp, &child->window_rect )) continue;
1413         offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1414         redraw_window( child, tmp, 1, flags );
1415         offset_region( tmp, child->client_rect.left, child->client_rect.top );
1416     }
1417     free_region( tmp );
1418 }
1419
1420
1421 /* retrieve the update flags for a window depending on the state of the update region */
1422 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1423 {
1424     unsigned int ret = 0;
1425
1426     if (flags & UPDATE_NONCLIENT)
1427     {
1428         if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1429     }
1430     if (flags & UPDATE_ERASE)
1431     {
1432         if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1433     }
1434     if (flags & UPDATE_PAINT)
1435     {
1436         if (win->update_region)
1437         {
1438             if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1439             ret |= UPDATE_PAINT;
1440         }
1441     }
1442     if (flags & UPDATE_INTERNALPAINT)
1443     {
1444         if (win->paint_flags & PAINT_INTERNAL)
1445         {
1446             ret |= UPDATE_INTERNALPAINT;
1447             if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1448         }
1449     }
1450     return ret;
1451 }
1452
1453
1454 /* iterate through the children of the given window until we find one with some update flags */
1455 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1456                                             unsigned int flags, struct window **child )
1457 {
1458     struct window *ptr;
1459     unsigned int ret = 0;
1460
1461     /* first make sure we want to iterate children at all */
1462
1463     if (win->style & WS_MINIMIZE) return 0;
1464
1465     /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1466      * here we only want to repaint children of windows that clip them, others
1467      * need to wait for WM_PAINT to be done in the parent first.
1468      */
1469     if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1470
1471     LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1472     {
1473         if (from_child)  /* skip all children until from_child is found */
1474         {
1475             if (ptr == from_child) from_child = NULL;
1476             continue;
1477         }
1478         if (!(ptr->style & WS_VISIBLE)) continue;
1479         if ((ret = get_update_flags( ptr, flags )) != 0)
1480         {
1481             *child = ptr;
1482             break;
1483         }
1484         if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1485     }
1486     return ret;
1487 }
1488
1489 /* iterate through children and siblings of the given window until we find one with some update flags */
1490 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1491                                              unsigned int flags, struct window **child )
1492 {
1493     unsigned int ret;
1494     struct window *ptr, *from_sibling = NULL;
1495
1496     /* if some parent is not visible start from the next sibling */
1497
1498     if (!is_visible( win )) return 0;
1499     for (ptr = from_child; ptr; ptr = ptr->parent)
1500     {
1501         if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1502         if (ptr == win) break;
1503     }
1504
1505     /* non-client painting must be delayed if one of the parents is going to
1506      * be repainted and doesn't clip children */
1507
1508     if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1509     {
1510         for (ptr = win->parent; ptr; ptr = ptr->parent)
1511         {
1512             if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1513                 return 0;
1514         }
1515         if (from_child && !(flags & UPDATE_ALLCHILDREN))
1516         {
1517             for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1518             {
1519                 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1520                 if (ptr == win) break;
1521             }
1522         }
1523     }
1524
1525
1526     /* check window itself (only if not restarting from a child) */
1527
1528     if (!from_child)
1529     {
1530         if ((ret = get_update_flags( win, flags )))
1531         {
1532             *child = win;
1533             return ret;
1534         }
1535         from_child = win;
1536     }
1537
1538     /* now check children */
1539
1540     if (flags & UPDATE_NOCHILDREN) return 0;
1541     if (!from_sibling)
1542     {
1543         if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1544         from_sibling = from_child;
1545     }
1546
1547     /* then check siblings and parent siblings */
1548
1549     while (from_sibling->parent && from_sibling != win)
1550     {
1551         if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1552             return ret;
1553         from_sibling = from_sibling->parent;
1554     }
1555     return 0;
1556 }
1557
1558
1559 /* expose the areas revealed by a vis region change on the window parent */
1560 /* returns the region exposed on the window itself (in client coordinates) */
1561 static struct region *expose_window( struct window *win, const rectangle_t *old_window_rect,
1562                                      struct region *old_vis_rgn )
1563 {
1564     struct region *new_vis_rgn, *exposed_rgn;
1565
1566     if (!(new_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return NULL;
1567
1568     if ((exposed_rgn = create_empty_region()))
1569     {
1570         if (subtract_region( exposed_rgn, new_vis_rgn, old_vis_rgn ) && !is_region_empty( exposed_rgn ))
1571         {
1572             /* make it relative to the new client area */
1573             offset_region( exposed_rgn, win->window_rect.left - win->client_rect.left,
1574                            win->window_rect.top - win->client_rect.top );
1575         }
1576         else
1577         {
1578             free_region( exposed_rgn );
1579             exposed_rgn = NULL;
1580         }
1581     }
1582
1583     if (win->parent)
1584     {
1585         /* make it relative to the old window pos for subtracting */
1586         offset_region( new_vis_rgn, win->window_rect.left - old_window_rect->left,
1587                        win->window_rect.top - old_window_rect->top  );
1588
1589         if ((win->parent->style & WS_CLIPCHILDREN) ?
1590             subtract_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ) :
1591             xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1592         {
1593             if (!is_region_empty( new_vis_rgn ))
1594             {
1595                 /* make it relative to parent */
1596                 offset_region( new_vis_rgn, old_window_rect->left, old_window_rect->top );
1597                 redraw_window( win->parent, new_vis_rgn, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1598             }
1599         }
1600     }
1601     free_region( new_vis_rgn );
1602     return exposed_rgn;
1603 }
1604
1605
1606 /* set the window and client rectangles, updating the update region if necessary */
1607 static void set_window_pos( struct window *win, struct window *previous,
1608                             unsigned int swp_flags, const rectangle_t *window_rect,
1609                             const rectangle_t *client_rect, const rectangle_t *visible_rect,
1610                             const rectangle_t *valid_rects )
1611 {
1612     struct region *old_vis_rgn = NULL, *exposed_rgn = NULL;
1613     const rectangle_t old_window_rect = win->window_rect;
1614     const rectangle_t old_visible_rect = win->visible_rect;
1615     const rectangle_t old_client_rect = win->client_rect;
1616     rectangle_t rect;
1617     int client_changed, frame_changed;
1618     int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1619
1620     if (win->parent && !is_visible( win->parent )) visible = 0;
1621
1622     if (visible && !(old_vis_rgn = get_visible_region( win, DCX_WINDOW ))) return;
1623
1624     /* set the new window info before invalidating anything */
1625
1626     win->window_rect  = *window_rect;
1627     win->visible_rect = *visible_rect;
1628     win->client_rect  = *client_rect;
1629     if (!(swp_flags & SWP_NOZORDER) && win->parent) link_window( win, previous );
1630     if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1631     else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1632
1633     /* keep children at the same position relative to top right corner when the parent is mirrored */
1634     if (win->ex_style & WS_EX_LAYOUTRTL)
1635     {
1636         struct window *child;
1637         int old_size = old_client_rect.right - old_client_rect.left;
1638         int new_size = win->client_rect.right - win->client_rect.left;
1639
1640         if (old_size != new_size) LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1641         {
1642             offset_rect( &child->window_rect, new_size - old_size, 0 );
1643             offset_rect( &child->visible_rect, new_size - old_size, 0 );
1644             offset_rect( &child->client_rect, new_size - old_size, 0 );
1645         }
1646     }
1647
1648     /* reset cursor clip rectangle when the desktop changes size */
1649     if (win == win->desktop->top_window) win->desktop->cursor.clip = *window_rect;
1650
1651     /* if the window is not visible, everything is easy */
1652     if (!visible) return;
1653
1654     /* expose anything revealed by the change */
1655
1656     if (!(swp_flags & SWP_NOREDRAW))
1657         exposed_rgn = expose_window( win, &old_window_rect, old_vis_rgn );
1658
1659     if (!(win->style & WS_VISIBLE))
1660     {
1661         /* clear the update region since the window is no longer visible */
1662         validate_whole_window( win );
1663         validate_children( win );
1664         goto done;
1665     }
1666
1667     /* crop update region to the new window rect */
1668
1669     if (win->update_region)
1670     {
1671         if (get_window_visible_rect( win, &rect, 1 ))
1672         {
1673             struct region *tmp = create_empty_region();
1674             if (tmp)
1675             {
1676                 set_region_rect( tmp, &rect );
1677                 if (intersect_region( tmp, win->update_region, tmp ))
1678                     set_update_region( win, tmp );
1679                 else
1680                     free_region( tmp );
1681             }
1682         }
1683         else set_update_region( win, NULL ); /* visible rect is empty */
1684     }
1685
1686     /* crop children regions to the new window rect */
1687
1688     if (get_window_visible_rect( win, &rect, 0 ))
1689     {
1690         /* map to client coords */
1691         offset_rect( &rect, win->window_rect.left - win->client_rect.left,
1692                      win->window_rect.top - win->client_rect.top );
1693         crop_children_update_region( win, &rect );
1694     }
1695     else crop_children_update_region( win, NULL );
1696
1697     if (swp_flags & SWP_NOREDRAW) goto done;  /* do not repaint anything */
1698
1699     /* expose the whole non-client area if it changed in any way */
1700
1701     if (swp_flags & SWP_NOCOPYBITS)
1702     {
1703         frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1704                          memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1705                          memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ));
1706         client_changed = memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) );
1707     }
1708     else
1709     {
1710         /* assume the bits have been moved to follow the window rect */
1711         int x_offset = window_rect->left - old_window_rect.left;
1712         int y_offset = window_rect->top - old_window_rect.top;
1713         frame_changed = ((swp_flags & SWP_FRAMECHANGED) ||
1714                          window_rect->right  - old_window_rect.right != x_offset ||
1715                          window_rect->bottom - old_window_rect.bottom != y_offset ||
1716                          visible_rect->left   - old_visible_rect.left   != x_offset ||
1717                          visible_rect->right  - old_visible_rect.right  != x_offset ||
1718                          visible_rect->top    - old_visible_rect.top    != y_offset ||
1719                          visible_rect->bottom - old_visible_rect.bottom != y_offset);
1720         client_changed = (client_rect->left   - old_client_rect.left   != x_offset ||
1721                           client_rect->right  - old_client_rect.right  != x_offset ||
1722                           client_rect->top    - old_client_rect.top    != y_offset ||
1723                           client_rect->bottom - old_client_rect.bottom != y_offset ||
1724                           !valid_rects ||
1725                           memcmp( &valid_rects[0], client_rect, sizeof(*client_rect) ));
1726         /* if part of the non-client area was exposed, consider it changed */
1727         if (exposed_rgn && !frame_changed)
1728         {
1729             get_region_extents( exposed_rgn, &rect );
1730             offset_rect( &rect, client_rect->left, client_rect->top );
1731             frame_changed = (rect.left < client_rect->left || rect.top < client_rect->top ||
1732                              rect.right > client_rect->right || rect.bottom > client_rect->bottom);
1733         }
1734     }
1735
1736     if (frame_changed || client_changed)
1737     {
1738         struct region *win_rgn = old_vis_rgn;  /* reuse previous region */
1739
1740         set_region_rect( win_rgn, window_rect );
1741         if (valid_rects)
1742         {
1743             /* subtract the valid portion of client rect from the total region */
1744             struct region *tmp = create_empty_region();
1745             if (tmp)
1746             {
1747                 set_region_rect( tmp, &valid_rects[0] );
1748                 /* subtract update region since invalid parts of the valid rect won't be copied */
1749                 if (win->update_region)
1750                 {
1751                     offset_region( tmp, -window_rect->left, -window_rect->top );
1752                     subtract_region( tmp, tmp, win->update_region );
1753                     offset_region( tmp, window_rect->left, window_rect->top );
1754                 }
1755                 if (subtract_region( tmp, win_rgn, tmp )) win_rgn = tmp;
1756                 else free_region( tmp );
1757             }
1758         }
1759         if (!is_desktop_window(win))
1760             offset_region( win_rgn, -client_rect->left, -client_rect->top );
1761         if (exposed_rgn)
1762         {
1763             union_region( exposed_rgn, exposed_rgn, win_rgn );
1764             if (win_rgn != old_vis_rgn) free_region( win_rgn );
1765         }
1766         else
1767         {
1768             exposed_rgn = win_rgn;
1769             if (win_rgn == old_vis_rgn) old_vis_rgn = NULL;
1770         }
1771     }
1772
1773     if (exposed_rgn)
1774         redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1775
1776 done:
1777     if (old_vis_rgn) free_region( old_vis_rgn );
1778     if (exposed_rgn) free_region( exposed_rgn );
1779     clear_error();  /* we ignore out of memory errors once the new rects have been set */
1780 }
1781
1782
1783 /* set the window region, updating the update region if necessary */
1784 static void set_window_region( struct window *win, struct region *region, int redraw )
1785 {
1786     struct region *old_vis_rgn = NULL, *exposed_rgn;
1787
1788     /* no need to redraw if window is not visible */
1789     if (redraw && !is_visible( win )) redraw = 0;
1790
1791     if (redraw) old_vis_rgn = get_visible_region( win, DCX_WINDOW );
1792
1793     if (win->win_region) free_region( win->win_region );
1794     win->win_region = region;
1795
1796     /* expose anything revealed by the change */
1797     if (old_vis_rgn && ((exposed_rgn = expose_window( win, &win->window_rect, old_vis_rgn ))))
1798     {
1799         redraw_window( win, exposed_rgn, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1800         free_region( exposed_rgn );
1801     }
1802
1803     if (old_vis_rgn) free_region( old_vis_rgn );
1804     clear_error();  /* we ignore out of memory errors since the region has been set */
1805 }
1806
1807
1808 /* destroy a window */
1809 void destroy_window( struct window *win )
1810 {
1811     /* hide the window */
1812     if (is_visible(win))
1813     {
1814         struct region *vis_rgn = get_visible_region( win, DCX_WINDOW );
1815         win->style &= ~WS_VISIBLE;
1816         if (vis_rgn)
1817         {
1818             struct region *exposed_rgn = expose_window( win, &win->window_rect, vis_rgn );
1819             if (exposed_rgn) free_region( exposed_rgn );
1820             free_region( vis_rgn );
1821         }
1822         validate_whole_window( win );
1823         validate_children( win );
1824     }
1825
1826     /* destroy all children */
1827     while (!list_empty(&win->children))
1828         destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
1829     while (!list_empty(&win->unlinked))
1830         destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
1831
1832     /* reset global window pointers, if the corresponding window is destroyed */
1833     if (win == shell_window) shell_window = NULL;
1834     if (win == shell_listview) shell_listview = NULL;
1835     if (win == progman_window) progman_window = NULL;
1836     if (win == taskman_window) taskman_window = NULL;
1837     free_hotkeys( win->desktop, win->handle );
1838     free_user_handle( win->handle );
1839     destroy_properties( win );
1840     list_remove( &win->entry );
1841     if (is_desktop_window(win))
1842     {
1843         struct desktop *desktop = win->desktop;
1844         assert( desktop->top_window == win || desktop->msg_window == win );
1845         if (desktop->top_window == win) desktop->top_window = NULL;
1846         else desktop->msg_window = NULL;
1847     }
1848     detach_window_thread( win );
1849     if (win->win_region) free_region( win->win_region );
1850     if (win->update_region) free_region( win->update_region );
1851     if (win->class) release_class( win->class );
1852     free( win->text );
1853     memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
1854     free( win );
1855 }
1856
1857
1858 /* create a window */
1859 DECL_HANDLER(create_window)
1860 {
1861     struct window *win, *parent = NULL, *owner = NULL;
1862     struct unicode_str cls_name;
1863     atom_t atom;
1864
1865     reply->handle = 0;
1866     if (req->parent && !(parent = get_window( req->parent ))) return;
1867
1868     if (req->owner)
1869     {
1870         if (!(owner = get_window( req->owner ))) return;
1871         if (is_desktop_window(owner)) owner = NULL;
1872         else if (parent && !is_desktop_window(parent))
1873         {
1874             /* an owned window must be created as top-level */
1875             set_error( STATUS_ACCESS_DENIED );
1876             return;
1877         }
1878         else /* owner must be a top-level window */
1879             while (!is_desktop_window(owner->parent)) owner = owner->parent;
1880     }
1881
1882     get_req_unicode_str( &cls_name );
1883     atom = cls_name.len ? find_global_atom( NULL, &cls_name ) : req->atom;
1884
1885     if (!(win = create_window( parent, owner, atom, req->instance ))) return;
1886
1887     reply->handle    = win->handle;
1888     reply->parent    = win->parent ? win->parent->handle : 0;
1889     reply->owner     = win->owner;
1890     reply->extra     = win->nb_extra_bytes;
1891     reply->class_ptr = get_class_client_ptr( win->class );
1892 }
1893
1894
1895 /* set the parent of a window */
1896 DECL_HANDLER(set_parent)
1897 {
1898     struct window *win, *parent = NULL;
1899
1900     if (!(win = get_window( req->handle ))) return;
1901     if (req->parent && !(parent = get_window( req->parent ))) return;
1902
1903     if (is_desktop_window(win))
1904     {
1905         set_error( STATUS_INVALID_PARAMETER );
1906         return;
1907     }
1908     reply->old_parent  = win->parent->handle;
1909     reply->full_parent = parent ? parent->handle : 0;
1910     set_parent_window( win, parent );
1911 }
1912
1913
1914 /* destroy a window */
1915 DECL_HANDLER(destroy_window)
1916 {
1917     struct window *win = get_window( req->handle );
1918     if (win)
1919     {
1920         if (!is_desktop_window(win)) destroy_window( win );
1921         else if (win->thread == current) detach_window_thread( win );
1922         else set_error( STATUS_ACCESS_DENIED );
1923     }
1924 }
1925
1926
1927 /* retrieve the desktop window for the current thread */
1928 DECL_HANDLER(get_desktop_window)
1929 {
1930     struct desktop *desktop = get_thread_desktop( current, 0 );
1931
1932     if (!desktop) return;
1933
1934     if (!desktop->top_window && req->force)  /* create it */
1935     {
1936         if ((desktop->top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
1937         {
1938             detach_window_thread( desktop->top_window );
1939             desktop->top_window->style  = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1940         }
1941     }
1942
1943     if (!desktop->msg_window && req->force)  /* create it */
1944     {
1945         static const WCHAR messageW[] = {'M','e','s','s','a','g','e'};
1946         static const struct unicode_str name = { messageW, sizeof(messageW) };
1947         atom_t atom = add_global_atom( NULL, &name );
1948         if (atom && (desktop->msg_window = create_window( NULL, NULL, atom, 0 )))
1949         {
1950             detach_window_thread( desktop->msg_window );
1951             desktop->msg_window->style = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1952         }
1953     }
1954
1955     reply->top_window = desktop->top_window ? desktop->top_window->handle : 0;
1956     reply->msg_window = desktop->msg_window ? desktop->msg_window->handle : 0;
1957     release_object( desktop );
1958 }
1959
1960
1961 /* set a window owner */
1962 DECL_HANDLER(set_window_owner)
1963 {
1964     struct window *win = get_window( req->handle );
1965     struct window *owner = NULL, *ptr;
1966
1967     if (!win) return;
1968     if (req->owner && !(owner = get_window( req->owner ))) return;
1969     if (is_desktop_window(win))
1970     {
1971         set_error( STATUS_ACCESS_DENIED );
1972         return;
1973     }
1974
1975     /* make sure owner is not a successor of window */
1976     for (ptr = owner; ptr; ptr = ptr->owner ? get_window( ptr->owner ) : NULL)
1977     {
1978         if (ptr == win)
1979         {
1980             set_error( STATUS_INVALID_PARAMETER );
1981             return;
1982         }
1983     }
1984
1985     reply->prev_owner = win->owner;
1986     reply->full_owner = win->owner = owner ? owner->handle : 0;
1987 }
1988
1989
1990 /* get information from a window handle */
1991 DECL_HANDLER(get_window_info)
1992 {
1993     struct window *win = get_window( req->handle );
1994
1995     reply->full_handle = 0;
1996     reply->tid = reply->pid = 0;
1997     if (win)
1998     {
1999         reply->full_handle = win->handle;
2000         reply->last_active = win->handle;
2001         reply->is_unicode  = win->is_unicode;
2002         if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
2003         if (win->thread)
2004         {
2005             reply->tid  = get_thread_id( win->thread );
2006             reply->pid  = get_process_id( win->thread->process );
2007             reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
2008         }
2009     }
2010 }
2011
2012
2013 /* set some information in a window */
2014 DECL_HANDLER(set_window_info)
2015 {
2016     struct window *win = get_window( req->handle );
2017
2018     if (!win) return;
2019     if (req->flags && is_desktop_window(win) && win->thread != current)
2020     {
2021         set_error( STATUS_ACCESS_DENIED );
2022         return;
2023     }
2024     if (req->extra_size > sizeof(req->extra_value) ||
2025         req->extra_offset < -1 ||
2026         req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
2027     {
2028         set_win32_error( ERROR_INVALID_INDEX );
2029         return;
2030     }
2031     if (req->extra_offset != -1)
2032     {
2033         memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
2034     }
2035     else if (req->flags & SET_WIN_EXTRA)
2036     {
2037         set_win32_error( ERROR_INVALID_INDEX );
2038         return;
2039     }
2040     reply->old_style     = win->style;
2041     reply->old_ex_style  = win->ex_style;
2042     reply->old_id        = win->id;
2043     reply->old_instance  = win->instance;
2044     reply->old_user_data = win->user_data;
2045     if (req->flags & SET_WIN_STYLE) win->style = req->style;
2046     if (req->flags & SET_WIN_EXSTYLE)
2047     {
2048         /* WS_EX_TOPMOST can only be changed for unlinked windows */
2049         if (!win->is_linked) win->ex_style = req->ex_style;
2050         else win->ex_style = (req->ex_style & ~WS_EX_TOPMOST) | (win->ex_style & WS_EX_TOPMOST);
2051         if (!(win->ex_style & WS_EX_LAYERED)) win->is_layered = 0;
2052     }
2053     if (req->flags & SET_WIN_ID) win->id = req->id;
2054     if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
2055     if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
2056     if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
2057     if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
2058                                             &req->extra_value, req->extra_size );
2059
2060     /* changing window style triggers a non-client paint */
2061     if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
2062 }
2063
2064
2065 /* get a list of the window parents, up to the root of the tree */
2066 DECL_HANDLER(get_window_parents)
2067 {
2068     struct window *ptr, *win = get_window( req->handle );
2069     int total = 0;
2070     user_handle_t *data;
2071     data_size_t len;
2072
2073     if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
2074
2075     reply->count = total;
2076     len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2077     if (len && ((data = set_reply_data_size( len ))))
2078     {
2079         for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
2080             *data++ = ptr->handle;
2081     }
2082 }
2083
2084
2085 /* get a list of the window children */
2086 DECL_HANDLER(get_window_children)
2087 {
2088     struct window *parent = NULL;
2089     unsigned int total;
2090     user_handle_t *data;
2091     data_size_t len;
2092     struct unicode_str cls_name;
2093     atom_t atom = req->atom;
2094     struct desktop *desktop = NULL;
2095
2096     get_req_unicode_str( &cls_name );
2097     if (cls_name.len && !(atom = find_global_atom( NULL, &cls_name ))) return;
2098
2099     if (req->desktop)
2100     {
2101         if (!(desktop = get_desktop_obj( current->process, req->desktop, DESKTOP_ENUMERATE ))) return;
2102         parent = desktop->top_window;
2103     }
2104     else
2105     {
2106         if (req->parent && !(parent = get_window( req->parent ))) return;
2107         if (!parent && !(desktop = get_thread_desktop( current, 0 ))) return;
2108     }
2109
2110     if (parent)
2111         total = get_children_windows( parent, atom, req->tid, NULL, 0 );
2112     else
2113         total = get_children_windows( desktop->top_window, atom, req->tid, NULL, 0 ) +
2114                 get_children_windows( desktop->msg_window, atom, req->tid, NULL, 0 );
2115
2116     reply->count = total;
2117     len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
2118     if (len && ((data = set_reply_data_size( len ))))
2119     {
2120         if (parent) get_children_windows( parent, atom, req->tid, data, len / sizeof(user_handle_t) );
2121         else
2122         {
2123             total = get_children_windows( desktop->top_window, atom, req->tid,
2124                                           data, len / sizeof(user_handle_t) );
2125             data += total;
2126             len -= total * sizeof(user_handle_t);
2127             if (len >= sizeof(user_handle_t))
2128                 get_children_windows( desktop->msg_window, atom, req->tid,
2129                                       data, len / sizeof(user_handle_t) );
2130         }
2131     }
2132     if (desktop) release_object( desktop );
2133 }
2134
2135
2136 /* get a list of the window children that contain a given point */
2137 DECL_HANDLER(get_window_children_from_point)
2138 {
2139     struct user_handle_array array;
2140     struct window *parent = get_window( req->parent );
2141     data_size_t len;
2142
2143     if (!parent) return;
2144
2145     array.handles = NULL;
2146     array.count = 0;
2147     array.total = 0;
2148     if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
2149
2150     reply->count = array.count;
2151     len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
2152     if (len) set_reply_data_ptr( array.handles, len );
2153     else free( array.handles );
2154 }
2155
2156
2157 /* get window tree information from a window handle */
2158 DECL_HANDLER(get_window_tree)
2159 {
2160     struct window *ptr, *win = get_window( req->handle );
2161
2162     if (!win) return;
2163
2164     reply->parent        = 0;
2165     reply->owner         = 0;
2166     reply->next_sibling  = 0;
2167     reply->prev_sibling  = 0;
2168     reply->first_sibling = 0;
2169     reply->last_sibling  = 0;
2170     reply->first_child   = 0;
2171     reply->last_child    = 0;
2172
2173     if (win->parent)
2174     {
2175         struct window *parent = win->parent;
2176         reply->parent = parent->handle;
2177         reply->owner  = win->owner;
2178         if (win->is_linked)
2179         {
2180             if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
2181             if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
2182         }
2183         if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
2184         if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
2185     }
2186     if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
2187     if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
2188 }
2189
2190
2191 /* set the position and Z order of a window */
2192 DECL_HANDLER(set_window_pos)
2193 {
2194     rectangle_t window_rect, client_rect, visible_rect;
2195     struct window *previous = NULL;
2196     struct window *top, *win = get_window( req->handle );
2197     unsigned int flags = req->swp_flags;
2198
2199     if (!win) return;
2200     if (!win->parent) flags |= SWP_NOZORDER;  /* no Z order for the desktop */
2201
2202     if (!(flags & SWP_NOZORDER))
2203     {
2204         switch ((int)req->previous)
2205         {
2206         case 0:   /* HWND_TOP */
2207             previous = WINPTR_TOP;
2208             break;
2209         case 1:   /* HWND_BOTTOM */
2210             previous = WINPTR_BOTTOM;
2211             break;
2212         case -1:  /* HWND_TOPMOST */
2213             previous = WINPTR_TOPMOST;
2214             break;
2215         case -2:  /* HWND_NOTOPMOST */
2216             previous = WINPTR_NOTOPMOST;
2217             break;
2218         default:
2219             if (!(previous = get_window( req->previous ))) return;
2220             /* previous must be a sibling */
2221             if (previous->parent != win->parent)
2222             {
2223                 set_error( STATUS_INVALID_PARAMETER );
2224                 return;
2225             }
2226             break;
2227         }
2228         if (previous == win) flags |= SWP_NOZORDER;  /* nothing to do */
2229     }
2230
2231     /* windows that use UpdateLayeredWindow don't trigger repaints */
2232     if ((win->ex_style & WS_EX_LAYERED) && !win->is_layered) flags |= SWP_NOREDRAW;
2233
2234     /* window rectangle must be ordered properly */
2235     if (req->window.right < req->window.left || req->window.bottom < req->window.top)
2236     {
2237         set_error( STATUS_INVALID_PARAMETER );
2238         return;
2239     }
2240
2241     window_rect = visible_rect = req->window;
2242     client_rect = req->client;
2243     if (get_req_data_size() >= sizeof(rectangle_t))
2244         memcpy( &visible_rect, get_req_data(), sizeof(rectangle_t) );
2245     if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2246     {
2247         mirror_rect( &win->parent->client_rect, &window_rect );
2248         mirror_rect( &win->parent->client_rect, &visible_rect );
2249         mirror_rect( &win->parent->client_rect, &client_rect );
2250     }
2251
2252     win->paint_flags = (win->paint_flags & ~PAINT_CLIENT_FLAGS) | (req->paint_flags & PAINT_CLIENT_FLAGS);
2253     if (win->paint_flags & PAINT_HAS_PIXEL_FORMAT) update_pixel_format_flags( win );
2254
2255     if (get_req_data_size() >= 3 * sizeof(rectangle_t))
2256     {
2257         rectangle_t valid_rects[2];
2258         memcpy( valid_rects, (const rectangle_t *)get_req_data() + 1, 2 * sizeof(rectangle_t) );
2259         if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2260         {
2261             mirror_rect( &win->parent->client_rect, &valid_rects[0] );
2262             mirror_rect( &win->parent->client_rect, &valid_rects[1] );
2263         }
2264         set_window_pos( win, previous, flags, &window_rect, &client_rect, &visible_rect, valid_rects );
2265     }
2266     else set_window_pos( win, previous, flags, &window_rect, &client_rect, &visible_rect, NULL );
2267
2268     reply->new_style = win->style;
2269     reply->new_ex_style = win->ex_style;
2270
2271     top = get_top_clipping_window( win );
2272     if (is_visible( top ) &&
2273         (top->paint_flags & PAINT_HAS_SURFACE) &&
2274         (top->paint_flags & PAINT_PIXEL_FORMAT_CHILD))
2275         reply->surface_win = top->handle;
2276 }
2277
2278
2279 /* get the window and client rectangles of a window */
2280 DECL_HANDLER(get_window_rectangles)
2281 {
2282     struct window *win = get_window( req->handle );
2283
2284     if (!win) return;
2285
2286     reply->window  = win->window_rect;
2287     reply->visible = win->visible_rect;
2288     reply->client  = win->client_rect;
2289
2290     switch (req->relative)
2291     {
2292     case COORDS_CLIENT:
2293         offset_rect( &reply->window, -win->client_rect.left, -win->client_rect.top );
2294         offset_rect( &reply->visible, -win->client_rect.left, -win->client_rect.top );
2295         offset_rect( &reply->client, -win->client_rect.left, -win->client_rect.top );
2296         if (win->ex_style & WS_EX_LAYOUTRTL)
2297         {
2298             mirror_rect( &win->client_rect, &reply->window );
2299             mirror_rect( &win->client_rect, &reply->visible );
2300         }
2301         break;
2302     case COORDS_WINDOW:
2303         offset_rect( &reply->window, -win->window_rect.left, -win->window_rect.top );
2304         offset_rect( &reply->visible, -win->window_rect.left, -win->window_rect.top );
2305         offset_rect( &reply->client, -win->window_rect.left, -win->window_rect.top );
2306         if (win->ex_style & WS_EX_LAYOUTRTL)
2307         {
2308             mirror_rect( &win->window_rect, &reply->visible );
2309             mirror_rect( &win->window_rect, &reply->client );
2310         }
2311         break;
2312     case COORDS_PARENT:
2313         if (win->parent && win->parent->ex_style & WS_EX_LAYOUTRTL)
2314         {
2315             mirror_rect( &win->parent->client_rect, &reply->window );
2316             mirror_rect( &win->parent->client_rect, &reply->visible );
2317             mirror_rect( &win->parent->client_rect, &reply->client );
2318         }
2319         break;
2320     case COORDS_SCREEN:
2321         client_to_screen_rect( win->parent, &reply->window );
2322         client_to_screen_rect( win->parent, &reply->visible );
2323         client_to_screen_rect( win->parent, &reply->client );
2324         break;
2325     default:
2326         set_error( STATUS_INVALID_PARAMETER );
2327         break;
2328     }
2329 }
2330
2331
2332 /* get the window text */
2333 DECL_HANDLER(get_window_text)
2334 {
2335     struct window *win = get_window( req->handle );
2336
2337     if (win && win->text)
2338     {
2339         data_size_t len = strlenW( win->text ) * sizeof(WCHAR);
2340         if (len > get_reply_max_size()) len = get_reply_max_size();
2341         set_reply_data( win->text, len );
2342     }
2343 }
2344
2345
2346 /* set the window text */
2347 DECL_HANDLER(set_window_text)
2348 {
2349     struct window *win = get_window( req->handle );
2350
2351     if (win)
2352     {
2353         WCHAR *text = NULL;
2354         data_size_t len = get_req_data_size() / sizeof(WCHAR);
2355         if (len)
2356         {
2357             if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
2358             memcpy( text, get_req_data(), len * sizeof(WCHAR) );
2359             text[len] = 0;
2360         }
2361         free( win->text );
2362         win->text = text;
2363     }
2364 }
2365
2366
2367 /* get the coordinates offset between two windows */
2368 DECL_HANDLER(get_windows_offset)
2369 {
2370     struct window *win;
2371     int mirror_from = 0, mirror_to = 0;
2372
2373     reply->x = reply->y = 0;
2374     if (req->from)
2375     {
2376         if (!(win = get_window( req->from ))) return;
2377         if (win->ex_style & WS_EX_LAYOUTRTL)
2378         {
2379             mirror_from = 1;
2380             reply->x += win->client_rect.right - win->client_rect.left;
2381         }
2382         while (win && !is_desktop_window(win))
2383         {
2384             reply->x += win->client_rect.left;
2385             reply->y += win->client_rect.top;
2386             win = win->parent;
2387         }
2388     }
2389     if (req->to)
2390     {
2391         if (!(win = get_window( req->to ))) return;
2392         if (win->ex_style & WS_EX_LAYOUTRTL)
2393         {
2394             mirror_to = 1;
2395             reply->x -= win->client_rect.right - win->client_rect.left;
2396         }
2397         while (win && !is_desktop_window(win))
2398         {
2399             reply->x -= win->client_rect.left;
2400             reply->y -= win->client_rect.top;
2401             win = win->parent;
2402         }
2403     }
2404     if (mirror_from) reply->x = -reply->x;
2405     reply->mirror = mirror_from ^ mirror_to;
2406 }
2407
2408
2409 /* get the visible region of a window */
2410 DECL_HANDLER(get_visible_region)
2411 {
2412     struct region *region;
2413     struct window *top, *win = get_window( req->window );
2414
2415     if (!win) return;
2416
2417     top = get_top_clipping_window( win );
2418     if ((region = get_visible_region( win, req->flags )))
2419     {
2420         rectangle_t *data;
2421         map_win_region_to_screen( win, region );
2422         data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2423         if (data) set_reply_data_ptr( data, reply->total_size );
2424     }
2425     reply->top_win  = top->handle;
2426     reply->top_rect = top->visible_rect;
2427
2428     if (!is_desktop_window(win))
2429     {
2430         reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
2431         client_to_screen_rect( top->parent, &reply->top_rect );
2432         client_to_screen_rect( win->parent, &reply->win_rect );
2433     }
2434     else
2435     {
2436         reply->win_rect.left   = 0;
2437         reply->win_rect.top    = 0;
2438         reply->win_rect.right  = win->client_rect.right - win->client_rect.left;
2439         reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
2440     }
2441 }
2442
2443
2444 /* get the surface visible region of a window */
2445 DECL_HANDLER(get_surface_region)
2446 {
2447     struct region *region;
2448     struct window *win = get_window( req->window );
2449
2450     if (!win || !is_visible( win )) return;
2451
2452     if ((region = get_surface_region( win )))
2453     {
2454         rectangle_t *data;
2455         if (win->parent) map_win_region_to_screen( win->parent, region );
2456         data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2457         if (data) set_reply_data_ptr( data, reply->total_size );
2458     }
2459     reply->visible_rect = win->visible_rect;
2460     if (win->parent) client_to_screen_rect( win->parent, &reply->visible_rect );
2461 }
2462
2463
2464 /* get the window region */
2465 DECL_HANDLER(get_window_region)
2466 {
2467     rectangle_t *data;
2468     struct window *win = get_window( req->window );
2469
2470     if (!win) return;
2471     if (!win->win_region) return;
2472
2473     if (win->ex_style & WS_EX_LAYOUTRTL)
2474     {
2475         struct region *region = create_empty_region();
2476
2477         if (!region) return;
2478         if (!copy_region( region, win->win_region ))
2479         {
2480             free_region( region );
2481             return;
2482         }
2483         mirror_region( &win->window_rect, region );
2484         data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
2485     }
2486     else data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
2487
2488     if (data) set_reply_data_ptr( data, reply->total_size );
2489 }
2490
2491
2492 /* set the window region */
2493 DECL_HANDLER(set_window_region)
2494 {
2495     struct region *region = NULL;
2496     struct window *win = get_window( req->window );
2497
2498     if (!win) return;
2499
2500     if (get_req_data_size())  /* no data means remove the region completely */
2501     {
2502         if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2503             return;
2504         if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->window_rect, region );
2505     }
2506     set_window_region( win, region, req->redraw );
2507 }
2508
2509
2510 /* get a window update region */
2511 DECL_HANDLER(get_update_region)
2512 {
2513     rectangle_t *data;
2514     unsigned int flags = req->flags;
2515     struct window *from_child = NULL;
2516     struct window *win = get_window( req->window );
2517
2518     reply->flags = 0;
2519     if (!win) return;
2520
2521     if (req->from_child)
2522     {
2523         struct window *ptr;
2524
2525         if (!(from_child = get_window( req->from_child ))) return;
2526
2527         /* make sure from_child is a child of win */
2528         ptr = from_child;
2529         while (ptr && ptr != win) ptr = ptr->parent;
2530         if (!ptr)
2531         {
2532             set_error( STATUS_INVALID_PARAMETER );
2533             return;
2534         }
2535     }
2536
2537     if (flags & UPDATE_DELAYED_ERASE)  /* this means that the previous call didn't erase */
2538     {
2539         if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
2540         else win->paint_flags |= PAINT_DELAYED_ERASE;
2541     }
2542
2543     reply->flags = get_window_update_flags( win, from_child, flags, &win );
2544     reply->child = win->handle;
2545
2546     if (flags & UPDATE_NOREGION) return;
2547
2548     if (win->update_region)
2549     {
2550         /* convert update region to screen coordinates */
2551         struct region *region = create_empty_region();
2552
2553         if (!region) return;
2554         if (!copy_region( region, win->update_region ))
2555         {
2556             free_region( region );
2557             return;
2558         }
2559         map_win_region_to_screen( win, region );
2560         if (!(data = get_region_data_and_free( region, get_reply_max_size(),
2561                                                &reply->total_size ))) return;
2562         set_reply_data_ptr( data, reply->total_size );
2563     }
2564
2565     if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
2566     {
2567         validate_parents( win );
2568         validate_whole_window( win );
2569     }
2570     else
2571     {
2572         if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
2573         if (reply->flags & UPDATE_ERASE)
2574         {
2575             win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
2576             /* desktop window only gets erased, not repainted */
2577             if (is_desktop_window(win)) validate_whole_window( win );
2578         }
2579     }
2580 }
2581
2582
2583 /* update the z order of a window so that a given rectangle is fully visible */
2584 DECL_HANDLER(update_window_zorder)
2585 {
2586     rectangle_t tmp, rect = req->rect;
2587     struct window *ptr, *win = get_window( req->window );
2588
2589     if (!win || !win->parent || !is_visible( win )) return;  /* nothing to do */
2590     if (win->ex_style & WS_EX_LAYOUTRTL) mirror_rect( &win->client_rect, &rect );
2591     offset_rect( &rect, win->client_rect.left, win->client_rect.top );
2592
2593     LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2594     {
2595         if (ptr == win) break;
2596         if (!(ptr->style & WS_VISIBLE)) continue;
2597         if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2598         if (ptr->is_layered && (ptr->layered_flags & LWA_COLORKEY)) continue;
2599         if (!intersect_rect( &tmp, &ptr->visible_rect, &rect )) continue;
2600         if (ptr->win_region)
2601         {
2602             tmp = rect;
2603             offset_rect( &tmp, -ptr->window_rect.left, -ptr->window_rect.top );
2604             if (!rect_in_region( ptr->win_region, &tmp )) continue;
2605         }
2606         /* found a window obscuring the rectangle, now move win above this one */
2607         /* making sure to not violate the topmost rule */
2608         if (!(ptr->ex_style & WS_EX_TOPMOST) || (win->ex_style & WS_EX_TOPMOST))
2609         {
2610             list_remove( &win->entry );
2611             list_add_before( &ptr->entry, &win->entry );
2612         }
2613         break;
2614     }
2615 }
2616
2617
2618 /* mark parts of a window as needing a redraw */
2619 DECL_HANDLER(redraw_window)
2620 {
2621     struct region *region = NULL;
2622     struct window *win = get_window( req->window );
2623
2624     if (!win) return;
2625     if (!is_visible( win )) return;  /* nothing to do */
2626
2627     if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
2628     {
2629         if (get_req_data_size())  /* no data means whole rectangle */
2630         {
2631             if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2632                 return;
2633             if (win->ex_style & WS_EX_LAYOUTRTL) mirror_region( &win->client_rect, region );
2634         }
2635     }
2636
2637     redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
2638                    req->flags );
2639     if (region) free_region( region );
2640 }
2641
2642
2643 /* set a window property */
2644 DECL_HANDLER(set_window_property)
2645 {
2646     struct unicode_str name;
2647     struct window *win = get_window( req->window );
2648
2649     if (!win) return;
2650
2651     get_req_unicode_str( &name );
2652     if (name.len)
2653     {
2654         atom_t atom = add_global_atom( NULL, &name );
2655         if (atom)
2656         {
2657             set_property( win, atom, req->data, PROP_TYPE_STRING );
2658             release_global_atom( NULL, atom );
2659         }
2660     }
2661     else set_property( win, req->atom, req->data, PROP_TYPE_ATOM );
2662 }
2663
2664
2665 /* remove a window property */
2666 DECL_HANDLER(remove_window_property)
2667 {
2668     struct unicode_str name;
2669     struct window *win = get_window( req->window );
2670
2671     get_req_unicode_str( &name );
2672     if (win)
2673     {
2674         atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2675         if (atom) reply->data = remove_property( win, atom );
2676     }
2677 }
2678
2679
2680 /* get a window property */
2681 DECL_HANDLER(get_window_property)
2682 {
2683     struct unicode_str name;
2684     struct window *win = get_window( req->window );
2685
2686     get_req_unicode_str( &name );
2687     if (win)
2688     {
2689         atom_t atom = name.len ? find_global_atom( NULL, &name ) : req->atom;
2690         if (atom) reply->data = get_property( win, atom );
2691     }
2692 }
2693
2694
2695 /* get the list of properties of a window */
2696 DECL_HANDLER(get_window_properties)
2697 {
2698     property_data_t *data;
2699     int i, count, max = get_reply_max_size() / sizeof(*data);
2700     struct window *win = get_window( req->window );
2701
2702     reply->total = 0;
2703     if (!win) return;
2704
2705     for (i = count = 0; i < win->prop_inuse; i++)
2706         if (win->properties[i].type != PROP_TYPE_FREE) count++;
2707     reply->total = count;
2708
2709     if (count > max) count = max;
2710     if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2711
2712     for (i = 0; i < win->prop_inuse && count; i++)
2713     {
2714         if (win->properties[i].type == PROP_TYPE_FREE) continue;
2715         data->atom   = win->properties[i].atom;
2716         data->string = (win->properties[i].type == PROP_TYPE_STRING);
2717         data->data   = win->properties[i].data;
2718         data++;
2719         count--;
2720     }
2721 }
2722
2723
2724 /* get the new window pointer for a global window, checking permissions */
2725 /* helper for set_global_windows request */
2726 static int get_new_global_window( struct window **win, user_handle_t handle )
2727 {
2728     if (!handle)
2729     {
2730         *win = NULL;
2731         return 1;
2732     }
2733     else if (*win)
2734     {
2735         set_error( STATUS_ACCESS_DENIED );
2736         return 0;
2737     }
2738     *win = get_window( handle );
2739     return (*win != NULL);
2740 }
2741
2742 /* Set/get the global windows */
2743 DECL_HANDLER(set_global_windows)
2744 {
2745     struct window *new_shell_window   = shell_window;
2746     struct window *new_shell_listview = shell_listview;
2747     struct window *new_progman_window = progman_window;
2748     struct window *new_taskman_window = taskman_window;
2749
2750     reply->old_shell_window   = shell_window ? shell_window->handle : 0;
2751     reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2752     reply->old_progman_window = progman_window ? progman_window->handle : 0;
2753     reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2754
2755     if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2756     {
2757         if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2758         if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2759     }
2760     if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2761     {
2762         if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2763     }
2764     if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2765     {
2766         if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2767     }
2768     shell_window   = new_shell_window;
2769     shell_listview = new_shell_listview;
2770     progman_window = new_progman_window;
2771     taskman_window = new_taskman_window;
2772 }
2773
2774 /* retrieve layered info for a window */
2775 DECL_HANDLER(get_window_layered_info)
2776 {
2777     struct window *win = get_window( req->handle );
2778
2779     if (!win) return;
2780
2781     if (win->is_layered)
2782     {
2783         reply->color_key = win->color_key;
2784         reply->alpha     = win->alpha;
2785         reply->flags     = win->layered_flags;
2786     }
2787     else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2788 }
2789
2790
2791 /* set layered info for a window */
2792 DECL_HANDLER(set_window_layered_info)
2793 {
2794     struct window *win = get_window( req->handle );
2795
2796     if (!win) return;
2797
2798     if (win->ex_style & WS_EX_LAYERED)
2799     {
2800         if (req->flags & LWA_ALPHA) win->alpha = req->alpha;
2801         else if (!win->is_layered) win->alpha = 0;  /* alpha init value is 0 */
2802
2803         win->color_key     = req->color_key;
2804         win->layered_flags = req->flags;
2805         win->is_layered    = 1;
2806     }
2807     else set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
2808 }