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