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