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