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