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