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