The meaning of the rewrap flag got inverted (MEPF_REWRAP instead of
[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 /* 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 /* compute the visible region of a window */
662 static struct region *get_visible_region( struct window *win, struct window *top,
663                                           unsigned int flags )
664 {
665     struct region *tmp, *region;
666     int offset_x, offset_y;
667
668     if (!(region = create_empty_region())) return NULL;
669
670     /* first check if all ancestors are visible */
671
672     if (!is_visible( win )) return region;  /* empty region */
673
674     /* create a region relative to the window itself */
675
676     if ((flags & DCX_PARENTCLIP) && win != top && win->parent)
677     {
678         set_region_client_rect( region, win->parent );
679         offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
680         offset_x = win->client_rect.left;
681         offset_y = win->client_rect.top;
682     }
683     else if (flags & DCX_WINDOW)
684     {
685         set_region_rect( region, &win->window_rect );
686         if (win->win_region && !intersect_window_region( region, win )) goto error;
687         offset_x = win->window_rect.left;
688         offset_y = win->window_rect.top;
689     }
690     else
691     {
692         set_region_client_rect( region, win );
693         if (win->win_region && !intersect_window_region( region, win )) goto error;
694         offset_x = win->client_rect.left;
695         offset_y = win->client_rect.top;
696     }
697     offset_region( region, -offset_x, -offset_y );
698
699     /* clip children */
700
701     if (flags & DCX_CLIPCHILDREN)
702     {
703         if (!clip_children( win, NULL, region,
704                             offset_x - win->client_rect.left,
705                             offset_y - win->client_rect.top )) goto error;
706     }
707
708     /* clip siblings of ancestors */
709
710     if (top && top != win && (tmp = create_empty_region()) != NULL)
711     {
712         offset_region( region, offset_x, offset_y );  /* make it relative to parent */
713         while (win != top && win->parent)
714         {
715             if (win->style & WS_CLIPSIBLINGS)
716             {
717                 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
718                 if (is_region_empty( region )) break;
719             }
720             /* clip to parent client area */
721             win = win->parent;
722             offset_x += win->client_rect.left;
723             offset_y += win->client_rect.top;
724             offset_region( region, win->client_rect.left, win->client_rect.top );
725             set_region_client_rect( tmp, win );
726             if (win->win_region && !intersect_window_region( tmp, win ))
727             {
728                 free_region( tmp );
729                 goto error;
730             }
731             if (!intersect_region( region, region, tmp ))
732             {
733                 free_region( tmp );
734                 goto error;
735             }
736             if (is_region_empty( region )) break;
737         }
738         offset_region( region, -offset_x, -offset_y );  /* make it relative to target window again */
739         free_region( tmp );
740     }
741     return region;
742
743 error:
744     free_region( region );
745     return NULL;
746 }
747
748
749 /* get the window class of a window */
750 struct window_class* get_window_class( user_handle_t window )
751 {
752     struct window *win;
753     if (!(win = get_window( window ))) return NULL;
754     return win->class;
755 }
756
757 /* return a copy of the specified region cropped to the window client or frame rectangle, */
758 /* and converted from client to window coordinates. Helper for (in)validate_window. */
759 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
760 {
761     struct region *tmp = create_empty_region();
762
763     if (!tmp) return NULL;
764
765     /* get bounding rect in client coords */
766     if (frame) set_region_rect( tmp, &win->window_rect );
767     else set_region_client_rect( tmp, win );
768     offset_region( tmp, -win->client_rect.left, -win->client_rect.top );
769
770     /* intersect specified region with bounding rect */
771     if (region && !intersect_region( tmp, region, tmp )) goto done;
772     if (is_region_empty( tmp )) goto done;
773
774     /* map it to window coords */
775     offset_region( tmp, win->client_rect.left - win->window_rect.left,
776                    win->client_rect.top - win->window_rect.top );
777     return tmp;
778
779 done:
780     free_region( tmp );
781     return NULL;
782 }
783
784
785 /* set a region as new update region for the window */
786 static void set_update_region( struct window *win, struct region *region )
787 {
788     if (region && !is_region_empty( region ))
789     {
790         if (!win->update_region) inc_window_paint_count( win, 1 );
791         else free_region( win->update_region );
792         win->update_region = region;
793     }
794     else
795     {
796         if (win->update_region) inc_window_paint_count( win, -1 );
797         win->paint_flags &= ~(PAINT_ERASE | PAINT_NONCLIENT);
798         win->update_region = NULL;
799         if (region) free_region( region );
800     }
801 }
802
803
804 /* add a region to the update region; the passed region is freed or reused */
805 static int add_update_region( struct window *win, struct region *region )
806 {
807     if (win->update_region && !union_region( region, win->update_region, region ))
808     {
809         free_region( region );
810         return 0;
811     }
812     set_update_region( win, region );
813     return 1;
814 }
815
816
817 /* validate the non client area of a window */
818 static void validate_non_client( struct window *win )
819 {
820     struct region *tmp;
821     rectangle_t rect;
822
823     if (!win->update_region) return;  /* nothing to do */
824
825     /* get client rect in window coords */
826     rect.left   = win->client_rect.left - win->window_rect.left;
827     rect.top    = win->client_rect.top - win->window_rect.top;
828     rect.right  = win->client_rect.right - win->window_rect.left;
829     rect.bottom = win->client_rect.bottom - win->window_rect.top;
830
831     if ((tmp = create_empty_region()))
832     {
833         set_region_rect( tmp, &rect );
834         if (intersect_region( tmp, win->update_region, tmp ))
835             set_update_region( win, tmp );
836         else
837             free_region( tmp );
838     }
839     win->paint_flags &= ~PAINT_NONCLIENT;
840 }
841
842
843 /* validate a window completely so that we don't get any further paint messages for it */
844 static void validate_whole_window( struct window *win )
845 {
846     set_update_region( win, NULL );
847
848     if (win->paint_flags & PAINT_INTERNAL)
849     {
850         win->paint_flags &= ~PAINT_INTERNAL;
851         inc_window_paint_count( win, -1 );
852     }
853 }
854
855
856 /* validate the update region of a window on all parents; helper for redraw_window */
857 static void validate_parents( struct window *child )
858 {
859     int offset_x = 0, offset_y = 0;
860     struct window *win = child;
861     struct region *tmp = NULL;
862
863     if (!child->update_region) return;
864
865     while (win->parent && win->parent != top_window)
866     {
867         /* map to parent client coords */
868         offset_x += win->window_rect.left;
869         offset_y += win->window_rect.top;
870
871         win = win->parent;
872
873         /* and now map to window coords */
874         offset_x += win->client_rect.left - win->window_rect.left;
875         offset_y += win->client_rect.top - win->window_rect.top;
876
877         if (win->update_region && !(win->style & WS_CLIPCHILDREN))
878         {
879             if (!tmp && !(tmp = create_empty_region())) return;
880             offset_region( child->update_region, offset_x, offset_y );
881             if (subtract_region( tmp, win->update_region, child->update_region ))
882             {
883                 set_update_region( win, tmp );
884                 tmp = NULL;
885             }
886             /* restore child coords */
887             offset_region( child->update_region, -offset_x, -offset_y );
888         }
889     }
890     if (tmp) free_region( tmp );
891 }
892
893
894 /* add/subtract a region (in client coordinates) to the update region of the window */
895 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
896 {
897     struct region *tmp;
898     struct window *child;
899
900     if (flags & RDW_INVALIDATE)
901     {
902         if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
903
904         if (!add_update_region( win, tmp )) return;
905
906         if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
907         if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
908     }
909     else if (flags & RDW_VALIDATE)
910     {
911         if (!region && (flags & RDW_NOFRAME))  /* shortcut: validate everything */
912         {
913             set_update_region( win, NULL );
914         }
915         else if (win->update_region)
916         {
917             if ((tmp = crop_region_to_win_rect( win, region, frame )))
918             {
919                 if (!subtract_region( tmp, win->update_region, tmp ))
920                 {
921                     free_region( tmp );
922                     return;
923                 }
924                 set_update_region( win, tmp );
925             }
926             if (flags & RDW_NOFRAME) validate_non_client( win );
927             if (flags & RDW_NOERASE) win->paint_flags &= ~PAINT_ERASE;
928         }
929     }
930
931     if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
932     {
933         win->paint_flags |= PAINT_INTERNAL;
934         inc_window_paint_count( win, 1 );
935     }
936     else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
937     {
938         win->paint_flags &= ~PAINT_INTERNAL;
939         inc_window_paint_count( win, -1 );
940     }
941
942     if (flags & RDW_UPDATENOW)
943     {
944         validate_parents( win );
945         flags &= ~RDW_UPDATENOW;
946     }
947
948     /* now process children recursively */
949
950     if (flags & RDW_NOCHILDREN) return;
951     if (win->style & WS_MINIMIZE) return;
952     if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
953
954     if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
955
956     /* map to client coordinates */
957     offset_region( tmp, win->window_rect.left - win->client_rect.left,
958                    win->window_rect.top - win->client_rect.top );
959
960     if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
961
962     for (child = win->first_child; child; child = child->next)
963     {
964         if (!(child->style & WS_VISIBLE)) continue;
965         if (!rect_in_region( tmp, &child->window_rect )) continue;
966         offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
967         redraw_window( child, tmp, 1, flags );
968         offset_region( tmp, child->client_rect.left, child->client_rect.top );
969     }
970     free_region( tmp );
971 }
972
973
974 /* retrieve the update flags for a window depending on the state of the update region */
975 static unsigned int get_update_flags( struct window *win, unsigned int flags )
976 {
977     unsigned int ret = 0;
978
979     if (flags & UPDATE_NONCLIENT)
980     {
981         if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
982     }
983     if (flags & UPDATE_ERASE)
984     {
985         if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
986     }
987     if (flags & UPDATE_PAINT)
988     {
989         if (win->update_region) ret |= UPDATE_PAINT;
990     }
991     if (flags & UPDATE_INTERNALPAINT)
992     {
993         if (win->paint_flags & PAINT_INTERNAL) ret |= UPDATE_INTERNALPAINT;
994     }
995     return ret;
996 }
997
998
999 /* iterate through the children of the given window until we find one with some update flags */
1000 static unsigned int get_child_update_flags( struct window *win, unsigned int flags,
1001                                             struct window **child )
1002 {
1003     struct window *ptr;
1004     unsigned int ret = 0;
1005
1006     for (ptr = win->first_child; ptr && !ret; ptr = ptr->next)
1007     {
1008         if (!(ptr->style & WS_VISIBLE)) continue;
1009         if ((ret = get_update_flags( ptr, flags )) != 0)
1010         {
1011             *child = ptr;
1012             break;
1013         }
1014         if (ptr->style & WS_MINIMIZE) continue;
1015
1016         /* Note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1017          * here we only want to repaint children of windows that clip them, others
1018          * need to wait for WM_PAINT to be done in the parent first.
1019          */
1020         if (!(flags & UPDATE_NOCHILDREN) &&
1021             ((flags & UPDATE_ALLCHILDREN) || (ptr->style & WS_CLIPCHILDREN)))
1022             ret = get_child_update_flags( ptr, flags, child );
1023     }
1024     return ret;
1025 }
1026
1027
1028 /* expose a region of a window, looking for the top most parent that needs to be exposed */
1029 /* the region is in window coordinates */
1030 static void expose_window( struct window *win, struct window *top, struct region *region )
1031 {
1032     struct window *parent, *ptr;
1033     int offset_x, offset_y;
1034
1035     /* find the top most parent that doesn't clip either siblings or children */
1036     for (parent = ptr = win; ptr != top; ptr = ptr->parent)
1037     {
1038         if (!(ptr->style & WS_CLIPCHILDREN)) parent = ptr;
1039         if (!(ptr->style & WS_CLIPSIBLINGS)) parent = ptr->parent;
1040     }
1041     if (parent == win && parent != top && win->parent)
1042         parent = win->parent;  /* always go up at least one level if possible */
1043
1044     offset_x = win->window_rect.left - win->client_rect.left;
1045     offset_y = win->window_rect.top - win->client_rect.top;
1046     for (ptr = win; ptr != parent; ptr = ptr->parent)
1047     {
1048         offset_x += ptr->client_rect.left;
1049         offset_y += ptr->client_rect.top;
1050     }
1051     offset_region( region, offset_x, offset_y );
1052     redraw_window( parent, region, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1053     offset_region( region, -offset_x, -offset_y );
1054 }
1055
1056
1057 /* set the window and client rectangles, updating the update region if necessary */
1058 static void set_window_pos( struct window *win, struct window *top, struct window *previous,
1059                             unsigned int swp_flags, const rectangle_t *window_rect,
1060                             const rectangle_t *client_rect, const rectangle_t *valid_rects )
1061 {
1062     struct region *old_vis_rgn, *new_vis_rgn;
1063     const rectangle_t old_window_rect = win->window_rect;
1064     const rectangle_t old_client_rect = win->client_rect;
1065
1066     /* if the window is not visible, everything is easy */
1067
1068     if ((win->parent && !is_visible( win->parent )) ||
1069         (!(win->style & WS_VISIBLE) && !(swp_flags & SWP_SHOWWINDOW)))
1070     {
1071         win->window_rect = *window_rect;
1072         win->client_rect = *client_rect;
1073         if (!(swp_flags & SWP_NOZORDER)) link_window( win, win->parent, previous );
1074         if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1075         else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1076         return;
1077     }
1078
1079     if (!(old_vis_rgn = get_visible_region( win, top, DCX_WINDOW ))) return;
1080
1081     /* set the new window info before invalidating anything */
1082
1083     win->window_rect = *window_rect;
1084     win->client_rect = *client_rect;
1085     if (!(swp_flags & SWP_NOZORDER)) link_window( win, win->parent, previous );
1086     if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1087     else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1088
1089     if (!(new_vis_rgn = get_visible_region( win, top, DCX_WINDOW )))
1090     {
1091         free_region( old_vis_rgn );
1092         clear_error();  /* ignore error since the window info has been modified already */
1093         return;
1094     }
1095
1096     /* expose anything revealed by the change */
1097
1098     if (!(swp_flags & SWP_NOREDRAW))
1099     {
1100         offset_region( old_vis_rgn, old_window_rect.left - window_rect->left,
1101                        old_window_rect.top - window_rect->top );
1102         if (xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1103             expose_window( win, top, new_vis_rgn );
1104     }
1105     free_region( old_vis_rgn );
1106
1107     if (!(win->style & WS_VISIBLE))
1108     {
1109         /* clear the update region since the window is no longer visible */
1110         validate_whole_window( win );
1111         goto done;
1112     }
1113
1114     if (swp_flags & SWP_NOREDRAW) goto done;  /* do not repaint anything */
1115
1116     /* expose the whole non-client area if it changed in any way */
1117
1118     if ((swp_flags & SWP_FRAMECHANGED) ||
1119         memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1120         memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1121     {
1122         struct region *tmp = create_empty_region();
1123
1124         if (tmp)
1125         {
1126             /* subtract the valid portion of client rect from the total region */
1127             if (!memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1128                 set_region_rect( tmp, client_rect );
1129             else if (valid_rects)
1130                 set_region_rect( tmp, &valid_rects[0] );
1131
1132             set_region_rect( new_vis_rgn, window_rect );
1133             if (subtract_region( tmp, new_vis_rgn, tmp ))
1134             {
1135                 offset_region( tmp, -client_rect->left, -client_rect->top );
1136                 redraw_window( win, tmp, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1137             }
1138             free_region( tmp );
1139         }
1140     }
1141
1142 done:
1143     free_region( new_vis_rgn );
1144     clear_error();  /* we ignore out of memory errors once the new rects have been set */
1145 }
1146
1147
1148 /* create a window */
1149 DECL_HANDLER(create_window)
1150 {
1151     struct window *win;
1152
1153     reply->handle = 0;
1154     if (!req->parent)  /* return desktop window */
1155     {
1156         if (!top_window)
1157         {
1158             if (!(top_window = create_window( NULL, NULL, req->atom, req->instance ))) return;
1159             top_window->thread = NULL;  /* no thread owns the desktop */
1160             top_window->style  = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1161         }
1162         win = top_window;
1163     }
1164     else
1165     {
1166         struct window *parent, *owner = NULL;
1167
1168         if (!(parent = get_window( req->parent ))) return;
1169         if (req->owner && !(owner = get_window( req->owner ))) return;
1170         if (owner == top_window) owner = NULL;
1171         else if (owner && parent != top_window)
1172         {
1173             /* an owned window must be created as top-level */
1174             set_error( STATUS_ACCESS_DENIED );
1175             return;
1176         }
1177         if (!(win = create_window( parent, owner, req->atom, req->instance ))) return;
1178     }
1179     reply->handle    = win->handle;
1180     reply->extra     = win->nb_extra_bytes;
1181     reply->class_ptr = get_class_client_ptr( win->class );
1182 }
1183
1184
1185 /* link a window into the tree */
1186 DECL_HANDLER(link_window)
1187 {
1188     struct window *win, *parent = NULL, *previous = NULL;
1189
1190     if (!(win = get_window( req->handle ))) return;
1191     if (req->parent && !(parent = get_window( req->parent ))) return;
1192
1193     if (win == top_window)
1194     {
1195         set_error( STATUS_INVALID_PARAMETER );
1196         return;
1197     }
1198     reply->full_parent = parent ? parent->handle : 0;
1199     if (parent && req->previous)
1200     {
1201         if (req->previous == (user_handle_t)1)  /* special case: HWND_BOTTOM */
1202         {
1203             previous = parent->last_child;
1204             if (previous == win) return;  /* nothing to do */
1205         }
1206         else
1207         {
1208             if (!(previous = get_window( req->previous ))) return;
1209             /* previous must be a child of parent, and not win itself */
1210             if (previous->parent != parent || previous == win)
1211             {
1212                 set_error( STATUS_INVALID_PARAMETER );
1213                 return;
1214             }
1215         }
1216     }
1217     link_window( win, parent, previous );
1218 }
1219
1220
1221 /* destroy a window */
1222 DECL_HANDLER(destroy_window)
1223 {
1224     struct window *win = get_window( req->handle );
1225     if (win)
1226     {
1227         if (win != top_window) destroy_window( win );
1228         else set_error( STATUS_ACCESS_DENIED );
1229     }
1230 }
1231
1232
1233 /* set a window owner */
1234 DECL_HANDLER(set_window_owner)
1235 {
1236     struct window *win = get_window( req->handle );
1237     struct window *owner = NULL;
1238
1239     if (!win) return;
1240     if (req->owner && !(owner = get_window( req->owner ))) return;
1241     if (win == top_window)
1242     {
1243         set_error( STATUS_ACCESS_DENIED );
1244         return;
1245     }
1246     reply->prev_owner = win->owner;
1247     reply->full_owner = win->owner = owner ? owner->handle : 0;
1248 }
1249
1250
1251 /* get information from a window handle */
1252 DECL_HANDLER(get_window_info)
1253 {
1254     struct window *win = get_window( req->handle );
1255
1256     reply->full_handle = 0;
1257     reply->tid = reply->pid = 0;
1258     if (win)
1259     {
1260         reply->full_handle = win->handle;
1261         reply->last_active = win->handle;
1262         if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
1263         if (win->thread)
1264         {
1265             reply->tid  = get_thread_id( win->thread );
1266             reply->pid  = get_process_id( win->thread->process );
1267             reply->atom = get_class_atom( win->class );
1268         }
1269     }
1270 }
1271
1272
1273 /* set some information in a window */
1274 DECL_HANDLER(set_window_info)
1275 {
1276     struct window *win = get_window( req->handle );
1277
1278     if (!win) return;
1279     if (req->flags && win == top_window)
1280     {
1281         set_error( STATUS_ACCESS_DENIED );
1282         return;
1283     }
1284     if (req->extra_size > sizeof(req->extra_value) ||
1285         req->extra_offset < -1 ||
1286         req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
1287     {
1288         set_win32_error( ERROR_INVALID_INDEX );
1289         return;
1290     }
1291     if (req->extra_offset != -1)
1292     {
1293         memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
1294     }
1295     else if (req->flags & SET_WIN_EXTRA)
1296     {
1297         set_win32_error( ERROR_INVALID_INDEX );
1298         return;
1299     }
1300     reply->old_style     = win->style;
1301     reply->old_ex_style  = win->ex_style;
1302     reply->old_id        = win->id;
1303     reply->old_instance  = win->instance;
1304     reply->old_user_data = win->user_data;
1305     if (req->flags & SET_WIN_STYLE) win->style = req->style;
1306     if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
1307     if (req->flags & SET_WIN_ID) win->id = req->id;
1308     if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
1309     if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
1310     if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
1311                                             &req->extra_value, req->extra_size );
1312
1313     /* changing window style triggers a non-client paint */
1314     if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
1315 }
1316
1317
1318 /* get a list of the window parents, up to the root of the tree */
1319 DECL_HANDLER(get_window_parents)
1320 {
1321     struct window *ptr, *win = get_window( req->handle );
1322     int total = 0;
1323     user_handle_t *data;
1324     size_t len;
1325
1326     if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
1327
1328     reply->count = total;
1329     len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1330     if (len && ((data = set_reply_data_size( len ))))
1331     {
1332         for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
1333             *data++ = ptr->handle;
1334     }
1335 }
1336
1337
1338 /* get a list of the window children */
1339 DECL_HANDLER(get_window_children)
1340 {
1341     struct window *ptr, *parent = get_window( req->parent );
1342     int total = 0;
1343     user_handle_t *data;
1344     size_t len;
1345
1346     if (parent)
1347         for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
1348         {
1349             if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1350             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1351             total++;
1352         }
1353
1354     reply->count = total;
1355     len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1356     if (len && ((data = set_reply_data_size( len ))))
1357     {
1358         for (ptr = parent->first_child; ptr && len; ptr = ptr->next)
1359         {
1360             if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1361             if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1362             *data++ = ptr->handle;
1363             len -= sizeof(*data);
1364         }
1365     }
1366 }
1367
1368
1369 /* get a list of the window children that contain a given point */
1370 DECL_HANDLER(get_window_children_from_point)
1371 {
1372     struct user_handle_array array;
1373     struct window *parent = get_window( req->parent );
1374     size_t len;
1375
1376     if (!parent) return;
1377
1378     array.handles = NULL;
1379     array.count = 0;
1380     array.total = 0;
1381     if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
1382
1383     reply->count = array.count;
1384     len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
1385     if (len) set_reply_data_ptr( array.handles, len );
1386     else free( array.handles );
1387 }
1388
1389
1390 /* get window tree information from a window handle */
1391 DECL_HANDLER(get_window_tree)
1392 {
1393     struct window *win = get_window( req->handle );
1394
1395     if (!win) return;
1396
1397     if (win->parent)
1398     {
1399         struct window *parent = win->parent;
1400         reply->parent        = parent->handle;
1401         reply->owner         = win->owner;
1402         reply->next_sibling  = win->next ? win->next->handle : 0;
1403         reply->prev_sibling  = win->prev ? win->prev->handle : 0;
1404         reply->first_sibling = parent->first_child ? parent->first_child->handle : 0;
1405         reply->last_sibling  = parent->last_child ? parent->last_child->handle : 0;
1406     }
1407     else
1408     {
1409         reply->parent        = 0;
1410         reply->owner         = 0;
1411         reply->next_sibling  = 0;
1412         reply->prev_sibling  = 0;
1413         reply->first_sibling = 0;
1414         reply->last_sibling  = 0;
1415     }
1416     reply->first_child = win->first_child ? win->first_child->handle : 0;
1417     reply->last_child  = win->last_child ? win->last_child->handle : 0;
1418 }
1419
1420
1421 /* set the position and Z order of a window */
1422 DECL_HANDLER(set_window_pos)
1423 {
1424     const rectangle_t *valid_rects = NULL;
1425     struct window *previous = NULL;
1426     struct window *top = top_window;
1427     struct window *win = get_window( req->handle );
1428     unsigned int flags = req->flags;
1429
1430     if (!win) return;
1431     if (!win->parent) flags |= SWP_NOZORDER;  /* no Z order for the desktop */
1432
1433     if (req->top_win)
1434     {
1435         if (!(top = get_window( req->top_win ))) return;
1436     }
1437
1438     if (!(flags & SWP_NOZORDER))
1439     {
1440         if (!req->previous)  /* special case: HWND_TOP */
1441         {
1442             if (win->parent->first_child == win) flags |= SWP_NOZORDER;
1443         }
1444         else if (req->previous == (user_handle_t)1)  /* special case: HWND_BOTTOM */
1445         {
1446             previous = win->parent->last_child;
1447         }
1448         else
1449         {
1450             if (!(previous = get_window( req->previous ))) return;
1451             /* previous must be a sibling */
1452             if (previous->parent != win->parent)
1453             {
1454                 set_error( STATUS_INVALID_PARAMETER );
1455                 return;
1456             }
1457         }
1458         if (previous == win) flags |= SWP_NOZORDER;  /* nothing to do */
1459     }
1460
1461     /* window rectangle must be ordered properly */
1462     if (req->window.right < req->window.left || req->window.bottom < req->window.top)
1463     {
1464         set_error( STATUS_INVALID_PARAMETER );
1465         return;
1466     }
1467
1468     if (get_req_data_size() >= 2 * sizeof(rectangle_t)) valid_rects = get_req_data();
1469
1470     set_window_pos( win, top, previous, flags, &req->window, &req->client, valid_rects );
1471     reply->new_style = win->style;
1472 }
1473
1474
1475 /* get the window and client rectangles of a window */
1476 DECL_HANDLER(get_window_rectangles)
1477 {
1478     struct window *win = get_window( req->handle );
1479
1480     if (win)
1481     {
1482         reply->window = win->window_rect;
1483         reply->client = win->client_rect;
1484     }
1485 }
1486
1487
1488 /* get the window text */
1489 DECL_HANDLER(get_window_text)
1490 {
1491     struct window *win = get_window( req->handle );
1492
1493     if (win && win->text)
1494     {
1495         size_t len = strlenW( win->text ) * sizeof(WCHAR);
1496         if (len > get_reply_max_size()) len = get_reply_max_size();
1497         set_reply_data( win->text, len );
1498     }
1499 }
1500
1501
1502 /* set the window text */
1503 DECL_HANDLER(set_window_text)
1504 {
1505     struct window *win = get_window( req->handle );
1506
1507     if (win)
1508     {
1509         WCHAR *text = NULL;
1510         size_t len = get_req_data_size() / sizeof(WCHAR);
1511         if (len)
1512         {
1513             if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
1514             memcpy( text, get_req_data(), len * sizeof(WCHAR) );
1515             text[len] = 0;
1516         }
1517         if (win->text) free( win->text );
1518         win->text = text;
1519     }
1520 }
1521
1522
1523 /* get the coordinates offset between two windows */
1524 DECL_HANDLER(get_windows_offset)
1525 {
1526     struct window *win;
1527
1528     reply->x = reply->y = 0;
1529     if (req->from)
1530     {
1531         if (!(win = get_window( req->from ))) return;
1532         while (win)
1533         {
1534             reply->x += win->client_rect.left;
1535             reply->y += win->client_rect.top;
1536             win = win->parent;
1537         }
1538     }
1539     if (req->to)
1540     {
1541         if (!(win = get_window( req->to ))) return;
1542         while (win)
1543         {
1544             reply->x -= win->client_rect.left;
1545             reply->y -= win->client_rect.top;
1546             win = win->parent;
1547         }
1548     }
1549 }
1550
1551
1552 /* get the visible region of a window */
1553 DECL_HANDLER(get_visible_region)
1554 {
1555     struct region *region;
1556     struct window *win = get_window( req->window );
1557     struct window *top = NULL;
1558
1559     if (!win) return;
1560     if (req->top_win && !(top = get_window( req->top_win ))) return;
1561
1562     if ((region = get_visible_region( win, top, req->flags )))
1563     {
1564         rectangle_t *data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
1565         if (data) set_reply_data_ptr( data, reply->total_size );
1566     }
1567 }
1568
1569
1570 /* get the window region */
1571 DECL_HANDLER(get_window_region)
1572 {
1573     struct window *win = get_window( req->window );
1574
1575     if (!win) return;
1576
1577     if (win->win_region)
1578     {
1579         rectangle_t *data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
1580         if (data) set_reply_data_ptr( data, reply->total_size );
1581     }
1582 }
1583
1584
1585 /* set the window region */
1586 DECL_HANDLER(set_window_region)
1587 {
1588     struct region *region = NULL;
1589     struct window *win = get_window( req->window );
1590
1591     if (!win) return;
1592
1593     if (get_req_data_size())  /* no data means remove the region completely */
1594     {
1595         if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1596             return;
1597     }
1598     if (win->win_region) free_region( win->win_region );
1599     win->win_region = region;
1600 }
1601
1602
1603 /* get a window update region */
1604 DECL_HANDLER(get_update_region)
1605 {
1606     rectangle_t *data;
1607     unsigned int flags = req->flags;
1608     struct window *win = get_window( req->window );
1609
1610     reply->flags = 0;
1611     if (!win || !is_visible( win )) return;
1612
1613     if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1614     {
1615         /* non-client painting must be delayed if one of the parents is going to
1616          * be repainted and doesn't clip children */
1617         struct window *ptr;
1618
1619         for (ptr = win->parent; ptr && ptr != top_window; ptr = ptr->parent)
1620         {
1621             if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1622                 return;
1623         }
1624     }
1625
1626     if (!(reply->flags = get_update_flags( win, flags )))
1627     {
1628         /* if window doesn't need any repaint, check the children */
1629         if (!(flags & UPDATE_NOCHILDREN) &&
1630             ((flags & UPDATE_ALLCHILDREN) || (win->style & WS_CLIPCHILDREN)))
1631         {
1632             reply->flags = get_child_update_flags( win, flags, &win );
1633         }
1634     }
1635
1636     reply->child = win->handle;
1637
1638     if (flags & UPDATE_NOREGION) return;
1639
1640     if (win->update_region)
1641     {
1642         if (!(data = get_region_data( win->update_region, get_reply_max_size(),
1643                                       &reply->total_size ))) return;
1644         set_reply_data_ptr( data, reply->total_size );
1645     }
1646
1647     if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
1648     {
1649         validate_whole_window( win );
1650     }
1651     else
1652     {
1653         if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
1654         if (reply->flags & UPDATE_ERASE)
1655         {
1656             win->paint_flags &= ~PAINT_ERASE;
1657             /* desktop window only gets erased, not repainted */
1658             if (win == top_window) validate_whole_window( win );
1659         }
1660     }
1661 }
1662
1663
1664 /* mark parts of a window as needing a redraw */
1665 DECL_HANDLER(redraw_window)
1666 {
1667     struct region *region = NULL;
1668     struct window *win = get_window( req->window );
1669
1670     if (!win) return;
1671     if (!is_visible( win )) return;  /* nothing to do */
1672
1673     if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
1674     {
1675         if (get_req_data_size())  /* no data means whole rectangle */
1676         {
1677             if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1678                 return;
1679         }
1680     }
1681
1682     redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
1683                    req->flags );
1684     if (region) free_region( region );
1685 }
1686
1687
1688 /* set a window property */
1689 DECL_HANDLER(set_window_property)
1690 {
1691     struct window *win = get_window( req->window );
1692
1693     if (win) set_property( win, req->atom, req->handle,
1694                            req->string ? PROP_TYPE_STRING : PROP_TYPE_ATOM );
1695 }
1696
1697
1698 /* remove a window property */
1699 DECL_HANDLER(remove_window_property)
1700 {
1701     struct window *win = get_window( req->window );
1702     reply->handle = 0;
1703     if (win) reply->handle = remove_property( win, req->atom );
1704 }
1705
1706
1707 /* get a window property */
1708 DECL_HANDLER(get_window_property)
1709 {
1710     struct window *win = get_window( req->window );
1711     reply->handle = 0;
1712     if (win) reply->handle = get_property( win, req->atom );
1713 }
1714
1715
1716 /* get the list of properties of a window */
1717 DECL_HANDLER(get_window_properties)
1718 {
1719     property_data_t *data;
1720     int i, count, max = get_reply_max_size() / sizeof(*data);
1721     struct window *win = get_window( req->window );
1722
1723     reply->total = 0;
1724     if (!win) return;
1725
1726     for (i = count = 0; i < win->prop_inuse; i++)
1727         if (win->properties[i].type != PROP_TYPE_FREE) count++;
1728     reply->total = count;
1729
1730     if (count > max) count = max;
1731     if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
1732
1733     for (i = 0; i < win->prop_inuse && count; i++)
1734     {
1735         if (win->properties[i].type == PROP_TYPE_FREE) continue;
1736         data->atom   = win->properties[i].atom;
1737         data->string = (win->properties[i].type == PROP_TYPE_STRING);
1738         data->handle = win->properties[i].handle;
1739         data++;
1740         count--;
1741     }
1742 }
1743
1744
1745 /* get the new window pointer for a global window, checking permissions */
1746 /* helper for set_global_windows request */
1747 static int get_new_global_window( struct window **win, user_handle_t handle )
1748 {
1749     if (!handle)
1750     {
1751         *win = NULL;
1752         return 1;
1753     }
1754     else if (*win)
1755     {
1756         set_error( STATUS_ACCESS_DENIED );
1757         return 0;
1758     }
1759     *win = get_window( handle );
1760     return (*win != NULL);
1761 }
1762
1763 /* Set/get the global windows */
1764 DECL_HANDLER(set_global_windows)
1765 {
1766     struct window *new_shell_window   = shell_window;
1767     struct window *new_shell_listview = shell_listview;
1768     struct window *new_progman_window = progman_window;
1769     struct window *new_taskman_window = taskman_window;
1770
1771     reply->old_shell_window   = shell_window ? shell_window->handle : 0;
1772     reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
1773     reply->old_progman_window = progman_window ? progman_window->handle : 0;
1774     reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
1775
1776     if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
1777     {
1778         if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
1779         if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
1780     }
1781     if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
1782     {
1783         if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
1784     }
1785     if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
1786     {
1787         if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
1788     }
1789     shell_window   = new_shell_window;
1790     shell_listview = new_shell_listview;
1791     progman_window = new_progman_window;
1792     taskman_window = new_taskman_window;
1793 }