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