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