exdisp.idl: Added missing IE6 methods to DWebBrowserEvents2.
[wine/hacks.git] / server / window.c
blob6b876af3c55fd19b52bd70dd159d391fb1af8332
1 /*
2 * Server-side window handling
4 * Copyright (C) 2001 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
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"
35 #include "object.h"
36 #include "request.h"
37 #include "thread.h"
38 #include "process.h"
39 #include "user.h"
40 #include "unicode.h"
42 /* a window property */
43 struct property
45 unsigned short type; /* property type (see below) */
46 atom_t atom; /* property atom */
47 obj_handle_t handle; /* property handle (user-defined storage) */
50 enum property_type
52 PROP_TYPE_FREE, /* free entry */
53 PROP_TYPE_STRING, /* atom that was originally a string */
54 PROP_TYPE_ATOM /* plain atom */
58 struct window
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 */
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 */
95 /* growable array of user handles */
96 struct user_handle_array
98 user_handle_t *handles;
99 int count;
100 int total;
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;
109 /* retrieve a pointer to a window from its handle */
110 inline static struct window *get_window( user_handle_t handle )
112 struct window *ret = get_user_object( handle, USER_WINDOW );
113 if (!ret) set_error( STATUS_INVALID_HANDLE );
114 return ret;
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 )
120 struct window *ptr;
122 /* make sure parent is not a child of window */
123 for (ptr = parent; ptr; ptr = ptr->parent)
125 if (ptr == win)
127 set_error( STATUS_INVALID_PARAMETER );
128 return 0;
132 list_remove( &win->entry ); /* unlink it from the previous location */
134 if (parent)
136 win->parent = parent;
137 list_add_head( &parent->children, &win->entry );
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 );
143 else /* move it to parent unlinked list */
145 list_add_head( &win->parent->unlinked, &win->entry );
147 return 1;
150 /* get next window in Z-order list */
151 static inline struct window *get_next_window( struct window *win )
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;
158 /* get previous window in Z-order list */
159 static inline struct window *get_prev_window( struct window *win )
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;
166 /* get first child in Z-order list */
167 static inline struct window *get_first_child( struct window *win )
169 struct list *ptr = list_head( &win->children );
170 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
173 /* get last child in Z-order list */
174 static inline struct window *get_last_child( struct window *win )
176 struct list *ptr = list_tail( &win->children );
177 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
180 /* check if window is the desktop */
181 static inline int is_desktop_window( const struct window *win )
183 return !win->parent; /* only desktop windows have no parent */
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 )
189 if (array->count >= array->total)
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)
195 free( array->handles );
196 set_error( STATUS_NO_MEMORY );
197 return 0;
199 array->handles = new_array;
200 array->total = new_total;
202 array->handles[array->count++] = handle;
203 return 1;
206 /* set a window property */
207 static void set_property( struct window *win, atom_t atom, obj_handle_t handle, enum property_type type )
209 int i, free = -1;
210 struct property *new_props;
212 /* check if it exists already */
213 for (i = 0; i < win->prop_inuse; i++)
215 if (win->properties[i].type == PROP_TYPE_FREE)
217 free = i;
218 continue;
220 if (win->properties[i].atom == atom)
222 win->properties[i].type = type;
223 win->properties[i].handle = handle;
224 return;
228 /* need to add an entry */
229 if (!grab_global_atom( win->desktop->winstation, atom )) return;
230 if (free == -1)
232 /* no free entry */
233 if (win->prop_inuse >= win->prop_alloc)
235 /* need to grow the array */
236 if (!(new_props = realloc( win->properties,
237 sizeof(*new_props) * (win->prop_alloc + 16) )))
239 set_error( STATUS_NO_MEMORY );
240 release_global_atom( win->desktop->winstation, atom );
241 return;
243 win->prop_alloc += 16;
244 win->properties = new_props;
246 free = win->prop_inuse++;
248 win->properties[free].atom = atom;
249 win->properties[free].type = type;
250 win->properties[free].handle = handle;
253 /* remove a window property */
254 static obj_handle_t remove_property( struct window *win, atom_t atom )
256 int i;
258 for (i = 0; i < win->prop_inuse; i++)
260 if (win->properties[i].type == PROP_TYPE_FREE) continue;
261 if (win->properties[i].atom == atom)
263 release_global_atom( win->desktop->winstation, atom );
264 win->properties[i].type = PROP_TYPE_FREE;
265 return win->properties[i].handle;
268 /* FIXME: last error? */
269 return 0;
272 /* find a window property */
273 static obj_handle_t get_property( struct window *win, atom_t atom )
275 int i;
277 for (i = 0; i < win->prop_inuse; i++)
279 if (win->properties[i].type == PROP_TYPE_FREE) continue;
280 if (win->properties[i].atom == atom) return win->properties[i].handle;
282 /* FIXME: last error? */
283 return 0;
286 /* destroy all properties of a window */
287 inline static void destroy_properties( struct window *win )
289 int i;
291 if (!win->properties) return;
292 for (i = 0; i < win->prop_inuse; i++)
294 if (win->properties[i].type == PROP_TYPE_FREE) continue;
295 release_global_atom( win->desktop->winstation, win->properties[i].atom );
297 free( win->properties );
300 /* detach a window from its owner thread but keep the window around */
301 static void detach_window_thread( struct window *win )
303 struct thread *thread = win->thread;
305 if (!thread) return;
306 if (thread->queue)
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 );
312 assert( thread->desktop_users > 0 );
313 thread->desktop_users--;
314 release_class( win->class );
315 win->class = NULL;
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;
323 /* destroy a window */
324 void destroy_window( struct window *win )
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 ));
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))
342 assert( win->desktop->top_window == win );
343 win->desktop->top_window = NULL;
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 );
354 /* get the process owning the top window of a given desktop */
355 struct process *get_top_window_owner( struct desktop *desktop )
357 struct window *win = desktop->top_window;
358 if (!win || !win->thread) return NULL;
359 return win->thread->process;
362 /* attempt to close the desktop window when the last process using it is gone */
363 void close_desktop_window( struct desktop *desktop )
365 struct window *win = desktop->top_window;
366 if (win && win->thread) post_message( win->handle, WM_SYSCOMMAND, SC_CLOSE, 0 );
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 )
373 int extra_bytes;
374 struct window *win;
375 struct desktop *desktop;
376 struct window_class *class;
378 if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
380 if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
382 release_object( desktop );
383 return NULL;
386 if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
387 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
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 );
414 /* parent must be on the same desktop */
415 if (parent && parent->desktop != desktop)
417 set_error( STATUS_ACCESS_DENIED );
418 goto failed;
421 /* if no parent, class must be the desktop */
422 if (!parent && !is_desktop_class( class ))
424 set_error( STATUS_ACCESS_DENIED );
425 goto failed;
428 /* if parent belongs to a different thread, attach the two threads */
429 if (parent && parent->thread && parent->thread != current)
431 if (!attach_thread_input( current, parent->thread )) goto failed;
433 else /* otherwise just make sure that the thread has a message queue */
435 if (!current->queue && !init_thread_queue( current )) goto failed;
438 /* put it on parent unlinked list */
439 if (parent) list_add_head( &parent->unlinked, &win->entry );
440 else
442 list_init( &win->entry );
443 assert( !desktop->top_window );
444 desktop->top_window = win;
447 current->desktop_users++;
448 return win;
450 failed:
451 if (win)
453 if (win->handle) free_user_handle( win->handle );
454 free( win );
456 release_object( desktop );
457 release_class( class );
458 return NULL;
461 /* destroy all windows belonging to a given thread */
462 void destroy_thread_windows( struct thread *thread )
464 user_handle_t handle = 0;
465 struct window *win;
467 while ((win = next_user_handle( &handle, USER_WINDOW )))
469 if (win->thread != thread) continue;
470 if (is_desktop_window( win )) detach_window_thread( win );
471 else destroy_window( win );
475 /* get the desktop window */
476 static struct window *get_desktop_window( struct thread *thread, int create )
478 struct window *top_window;
479 struct desktop *desktop = get_thread_desktop( thread, 0 );
481 if (!desktop) return NULL;
483 if (!(top_window = desktop->top_window) && create)
485 if ((top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
487 detach_window_thread( top_window );
488 top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
491 release_object( desktop );
492 return top_window;
495 /* check whether child is a descendant of parent */
496 int is_child_window( user_handle_t parent, user_handle_t child )
498 struct window *child_ptr = get_user_object( child, USER_WINDOW );
499 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
501 if (!child_ptr || !parent_ptr) return 0;
502 while (child_ptr->parent)
504 if (child_ptr->parent == parent_ptr) return 1;
505 child_ptr = child_ptr->parent;
507 return 0;
510 /* check whether window is a top-level window */
511 int is_top_level_window( user_handle_t window )
513 struct window *win = get_user_object( window, USER_WINDOW );
514 return (win && win->parent && is_desktop_window(win->parent));
517 /* make a window active if possible */
518 int make_window_active( user_handle_t window )
520 struct window *owner, *win = get_window( window );
522 if (!win) return 0;
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;
530 /* increment (or decrement) the window paint count */
531 static inline void inc_window_paint_count( struct window *win, int incr )
533 if (win->thread) inc_queue_paint_count( win->thread, incr );
536 /* check if window and all its ancestors are visible */
537 static int is_visible( const struct window *win )
539 while (win && win->parent)
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;
546 return 1;
549 /* same as is_visible but takes a window handle */
550 int is_window_visible( user_handle_t window )
552 struct window *win = get_user_object( window, USER_WINDOW );
553 if (!win) return 0;
554 return is_visible( win );
557 /* check if point is inside the window */
558 static inline int is_point_in_window( struct window *win, int x, int y )
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;
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 )
577 struct window *ptr;
579 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
581 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
583 /* if window is minimized or disabled, return at once */
584 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
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;
591 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
593 return parent; /* not found any child */
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 )
600 struct window *ptr;
602 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
604 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
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)
611 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
612 y - ptr->client_rect.top, array ))
613 return 0;
616 /* now add window to the array */
617 if (!add_handle_to_array( array, ptr->handle )) return 0;
619 return 1;
622 /* find window containing point (in absolute coords) */
623 user_handle_t window_from_point( struct desktop *desktop, int x, int y )
625 struct window *ret;
627 if (!desktop->top_window) return 0;
628 ret = child_window_from_point( desktop->top_window, x, y );
629 return ret->handle;
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 )
635 struct window *ptr;
637 /* make point relative to top window */
638 for (ptr = top->parent; ptr; ptr = ptr->parent)
640 x -= ptr->client_rect.left;
641 y -= ptr->client_rect.top;
644 if (!is_point_in_window( top, x, y )) return 1;
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)
651 if (!get_window_children_from_point( top, x - top->client_rect.left,
652 y - top->client_rect.top, array ))
653 return 0;
655 /* now add window to the array */
656 if (!add_handle_to_array( array, top->handle )) return 0;
657 return 1;
661 /* return the thread owning a window */
662 struct thread *get_window_thread( user_handle_t handle )
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 );
670 /* check if any area of a window needs repainting */
671 static inline int win_needs_repaint( struct window *win )
673 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
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 )
680 struct window *ptr, *ret = NULL;
682 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
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;
692 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
694 /* transparent window, check for non-transparent sibling to paint first */
695 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
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;
703 return ret;
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 )
710 struct window *ptr, *win, *top_window = get_desktop_window( thread, 0 );
712 if (!top_window) return 0;
714 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
715 else win = find_child_to_repaint( top_window, thread );
717 if (win && parent)
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;
725 if (!win) return 0;
726 win->paint_flags &= ~PAINT_INTERNAL;
727 return win->handle;
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 )
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;
743 /* convert coordinates from client to screen coords */
744 static inline void client_to_screen( struct window *win, int *x, int *y )
746 for ( ; win; win = win->parent)
748 *x += win->client_rect.left;
749 *y += win->client_rect.top;
753 /* map the region from window to screen coordinates */
754 static inline void map_win_region_to_screen( struct window *win, struct region *region )
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 );
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 )
767 struct window *ptr;
768 struct region *tmp = create_empty_region();
770 if (!tmp) return NULL;
771 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
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 ))
779 free_region( tmp );
780 return NULL;
782 offset_region( tmp, offset_x, offset_y );
783 if (!(region = subtract_region( region, region, tmp ))) break;
784 if (is_region_empty( region )) break;
786 free_region( tmp );
787 return region;
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 )
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);
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 )
805 rectangle_t rect;
807 intersect_rect( &rect, &win->window_rect, &win->client_rect );
808 set_region_rect( region, &rect );
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 )
815 while (win->parent && !is_desktop_window(win->parent)) win = win->parent;
816 return win;
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 )
823 struct region *tmp = NULL, *region;
824 int offset_x, offset_y;
826 if (!(region = create_empty_region())) return NULL;
828 /* first check if all ancestors are visible */
830 if (!is_visible( win )) return region; /* empty region */
832 /* create a region relative to the window itself */
834 if ((flags & DCX_PARENTCLIP) && win != top && win->parent)
836 set_region_client_rect( region, win->parent );
837 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
839 else if (flags & DCX_WINDOW)
841 set_region_rect( region, &win->visible_rect );
842 if (win->win_region && !intersect_window_region( region, win )) goto error;
844 else
846 set_region_client_rect( region, win );
847 if (win->win_region && !intersect_window_region( region, win )) goto error;
849 offset_x = win->window_rect.left;
850 offset_y = win->window_rect.top;
852 /* clip children */
854 if (flags & DCX_CLIPCHILDREN)
856 if (!clip_children( win, NULL, region, win->client_rect.left, win->client_rect.top ))
857 goto error;
860 /* clip siblings of ancestors */
862 if (top && top != win && (tmp = create_empty_region()) != NULL)
864 while (win != top && win->parent)
866 if (win->style & WS_CLIPSIBLINGS)
868 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
869 if (is_region_empty( region )) break;
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;
881 free_region( tmp );
883 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
884 return region;
886 error:
887 if (tmp) free_region( tmp );
888 free_region( region );
889 return NULL;
893 /* get the window class of a window */
894 struct window_class* get_window_class( user_handle_t window )
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;
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 )
906 struct region *tmp = create_empty_region();
908 if (!tmp) return NULL;
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 );
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;
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;
924 done:
925 free_region( tmp );
926 return NULL;
930 /* set a region as new update region for the window */
931 static void set_update_region( struct window *win, struct region *region )
933 if (region && !is_region_empty( region ))
935 if (!win->update_region) inc_window_paint_count( win, 1 );
936 else free_region( win->update_region );
937 win->update_region = region;
939 else
941 if (win->update_region)
943 inc_window_paint_count( win, -1 );
944 free_region( win->update_region );
946 win->paint_flags &= ~(PAINT_ERASE | PAINT_NONCLIENT);
947 win->update_region = NULL;
948 if (region) free_region( region );
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 )
956 if (win->update_region && !union_region( region, win->update_region, region ))
958 free_region( region );
959 return 0;
961 set_update_region( win, region );
962 return 1;
966 /* validate the non client area of a window */
967 static void validate_non_client( struct window *win )
969 struct region *tmp;
970 rectangle_t rect;
972 if (!win->update_region) return; /* nothing to do */
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;
980 if ((tmp = create_empty_region()))
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 );
988 win->paint_flags &= ~PAINT_NONCLIENT;
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 )
995 set_update_region( win, NULL );
997 if (win->paint_flags & PAINT_INTERNAL)
999 win->paint_flags &= ~PAINT_INTERNAL;
1000 inc_window_paint_count( win, -1 );
1005 /* validate the update region of a window on all parents; helper for redraw_window */
1006 static void validate_parents( struct window *child )
1008 int offset_x = 0, offset_y = 0;
1009 struct window *win = child;
1010 struct region *tmp = NULL;
1012 if (!child->update_region) return;
1014 while (win->parent)
1016 /* map to parent client coords */
1017 offset_x += win->window_rect.left;
1018 offset_y += win->window_rect.top;
1020 win = win->parent;
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;
1026 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
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 ))
1032 set_update_region( win, tmp );
1033 tmp = NULL;
1035 /* restore child coords */
1036 offset_region( child->update_region, -offset_x, -offset_y );
1039 if (tmp) free_region( tmp );
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 )
1046 struct region *tmp;
1047 struct window *child;
1049 if (flags & RDW_INVALIDATE)
1051 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1053 if (!add_update_region( win, tmp )) return;
1055 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1056 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1058 else if (flags & RDW_VALIDATE)
1060 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1062 set_update_region( win, NULL );
1064 else if (win->update_region)
1066 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1068 if (!subtract_region( tmp, win->update_region, tmp ))
1070 free_region( tmp );
1071 return;
1073 set_update_region( win, tmp );
1075 if (flags & RDW_NOFRAME) validate_non_client( win );
1076 if (flags & RDW_NOERASE) win->paint_flags &= ~PAINT_ERASE;
1080 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1082 win->paint_flags |= PAINT_INTERNAL;
1083 inc_window_paint_count( win, 1 );
1085 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1087 win->paint_flags &= ~PAINT_INTERNAL;
1088 inc_window_paint_count( win, -1 );
1091 if (flags & RDW_UPDATENOW)
1093 validate_parents( win );
1094 flags &= ~RDW_UPDATENOW;
1097 /* now process children recursively */
1099 if (flags & RDW_NOCHILDREN) return;
1100 if (win->style & WS_MINIMIZE) return;
1101 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1103 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
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 );
1109 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1111 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
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 );
1119 free_region( tmp );
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 )
1126 unsigned int ret = 0;
1128 if (flags & UPDATE_NONCLIENT)
1130 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1132 if (flags & UPDATE_ERASE)
1134 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1136 if (flags & UPDATE_PAINT)
1138 if (win->update_region) ret |= UPDATE_PAINT;
1140 if (flags & UPDATE_INTERNALPAINT)
1142 if (win->paint_flags & PAINT_INTERNAL) ret |= UPDATE_INTERNALPAINT;
1144 return ret;
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 )
1152 struct window *ptr;
1153 unsigned int ret = 0;
1155 /* first make sure we want to iterate children at all */
1157 if (win->style & WS_MINIMIZE) return 0;
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.
1163 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1165 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1167 if (from_child) /* skip all children until from_child is found */
1169 if (ptr == from_child) from_child = NULL;
1170 continue;
1172 if (!(ptr->style & WS_VISIBLE)) continue;
1173 if ((ret = get_update_flags( ptr, flags )) != 0)
1175 *child = ptr;
1176 break;
1178 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1180 return ret;
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 )
1187 unsigned int ret;
1188 struct window *ptr, *from_sibling = NULL;
1190 /* if some parent is not visible start from the next sibling */
1192 if (!is_visible( win )) return 0;
1193 for (ptr = from_child; ptr; ptr = ptr->parent)
1195 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1196 if (ptr == win) break;
1199 /* non-client painting must be delayed if one of the parents is going to
1200 * be repainted and doesn't clip children */
1202 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1204 for (ptr = win->parent; ptr; ptr = ptr->parent)
1206 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1207 return 0;
1209 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1211 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1213 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1214 if (ptr == win) break;
1220 /* check window itself (only if not restarting from a child) */
1222 if (!from_child)
1224 if ((ret = get_update_flags( win, flags )))
1226 *child = win;
1227 return ret;
1229 from_child = win;
1232 /* now check children */
1234 if (flags & UPDATE_NOCHILDREN) return 0;
1235 if (!from_sibling)
1237 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1238 from_sibling = from_child;
1241 /* then check siblings and parent siblings */
1243 while (from_sibling->parent && from_sibling != win)
1245 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1246 return ret;
1247 from_sibling = from_sibling->parent;
1249 return 0;
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 )
1257 struct window *parent, *ptr;
1258 int offset_x, offset_y;
1260 /* find the top most parent that doesn't clip either siblings or children */
1261 for (parent = ptr = win; ptr != top; ptr = ptr->parent)
1263 if (!(ptr->style & WS_CLIPCHILDREN)) parent = ptr;
1264 if (!(ptr->style & WS_CLIPSIBLINGS)) parent = ptr->parent;
1266 if (parent == win && parent != top && win->parent)
1267 parent = win->parent; /* always go up at least one level if possible */
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)
1273 offset_x += ptr->client_rect.left;
1274 offset_y += ptr->client_rect.top;
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 );
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 )
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);
1295 if (win->parent && !is_visible( win->parent )) visible = 0;
1297 if (visible && !(old_vis_rgn = get_visible_region( win, top, DCX_WINDOW ))) return;
1299 /* set the new window info before invalidating anything */
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)
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 );
1310 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1311 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1313 /* if the window is not visible, everything is easy */
1314 if (!visible) return;
1316 if (!(new_vis_rgn = get_visible_region( win, top, DCX_WINDOW )))
1318 free_region( old_vis_rgn );
1319 clear_error(); /* ignore error since the window info has been modified already */
1320 return;
1323 /* expose anything revealed by the change */
1325 if (!(swp_flags & SWP_NOREDRAW))
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 );
1332 free_region( old_vis_rgn );
1334 if (!(win->style & WS_VISIBLE))
1336 /* clear the update region since the window is no longer visible */
1337 validate_whole_window( win );
1338 goto done;
1341 /* crop update region to the new window rect */
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))
1347 struct region *tmp = create_empty_region();
1348 if (tmp)
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 );
1359 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1361 /* expose the whole non-client area if it changed in any way */
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) ))
1368 struct region *tmp = create_empty_region();
1370 if (tmp)
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] );
1378 set_region_rect( new_vis_rgn, window_rect );
1379 if (subtract_region( tmp, new_vis_rgn, tmp ))
1381 offset_region( tmp, -client_rect->left, -client_rect->top );
1382 redraw_window( win, tmp, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1384 free_region( tmp );
1388 done:
1389 free_region( new_vis_rgn );
1390 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1394 /* create a window */
1395 DECL_HANDLER(create_window)
1397 struct window *win, *parent, *owner = NULL;
1399 reply->handle = 0;
1401 if (!req->parent) parent = get_desktop_window( current, 0 );
1402 else if (!(parent = get_window( req->parent ))) return;
1404 if (req->owner)
1406 if (!(owner = get_window( req->owner ))) return;
1407 if (is_desktop_window(owner)) owner = NULL;
1408 else if (!is_desktop_window(parent))
1410 /* an owned window must be created as top-level */
1411 set_error( STATUS_ACCESS_DENIED );
1412 return;
1414 else /* owner must be a top-level window */
1415 while (!is_desktop_window(owner->parent)) owner = owner->parent;
1417 if (!(win = create_window( parent, owner, req->atom, req->instance ))) return;
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 );
1427 /* set the parent of a window */
1428 DECL_HANDLER(set_parent)
1430 struct window *win, *parent = NULL;
1432 if (!(win = get_window( req->handle ))) return;
1433 if (req->parent && !(parent = get_window( req->parent ))) return;
1435 if (is_desktop_window(win))
1437 set_error( STATUS_INVALID_PARAMETER );
1438 return;
1440 reply->old_parent = win->parent->handle;
1441 reply->full_parent = parent ? parent->handle : 0;
1442 set_parent_window( win, parent );
1446 /* destroy a window */
1447 DECL_HANDLER(destroy_window)
1449 struct window *win = get_window( req->handle );
1450 if (win)
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 );
1459 /* retrieve the desktop window for the current thread */
1460 DECL_HANDLER(get_desktop_window)
1462 struct window *win = get_desktop_window( current, req->force );
1464 if (win) reply->handle = win->handle;
1468 /* set a window owner */
1469 DECL_HANDLER(set_window_owner)
1471 struct window *win = get_window( req->handle );
1472 struct window *owner = NULL;
1474 if (!win) return;
1475 if (req->owner && !(owner = get_window( req->owner ))) return;
1476 if (is_desktop_window(win))
1478 set_error( STATUS_ACCESS_DENIED );
1479 return;
1481 reply->prev_owner = win->owner;
1482 reply->full_owner = win->owner = owner ? owner->handle : 0;
1486 /* get information from a window handle */
1487 DECL_HANDLER(get_window_info)
1489 struct window *win = get_window( req->handle );
1491 reply->full_handle = 0;
1492 reply->tid = reply->pid = 0;
1493 if (win)
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)
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;
1509 /* set some information in a window */
1510 DECL_HANDLER(set_window_info)
1512 struct window *win = get_window( req->handle );
1514 if (!win) return;
1515 if (req->flags && is_desktop_window(win) && win->thread != current)
1517 set_error( STATUS_ACCESS_DENIED );
1518 return;
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)
1524 set_win32_error( ERROR_INVALID_INDEX );
1525 return;
1527 if (req->extra_offset != -1)
1529 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
1531 else if (req->flags & SET_WIN_EXTRA)
1533 set_win32_error( ERROR_INVALID_INDEX );
1534 return;
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 );
1550 /* changing window style triggers a non-client paint */
1551 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
1555 /* get a list of the window parents, up to the root of the tree */
1556 DECL_HANDLER(get_window_parents)
1558 struct window *ptr, *win = get_window( req->handle );
1559 int total = 0;
1560 user_handle_t *data;
1561 size_t len;
1563 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
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 ))))
1569 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
1570 *data++ = ptr->handle;
1575 /* get a list of the window children */
1576 DECL_HANDLER(get_window_children)
1578 struct window *ptr, *parent = get_window( req->parent );
1579 int total = 0;
1580 user_handle_t *data;
1581 size_t len;
1583 if (parent)
1585 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
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++;
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 ))))
1596 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
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);
1608 /* get a list of the window children that contain a given point */
1609 DECL_HANDLER(get_window_children_from_point)
1611 struct user_handle_array array;
1612 struct window *parent = get_window( req->parent );
1613 size_t len;
1615 if (!parent) return;
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;
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 );
1629 /* get window tree information from a window handle */
1630 DECL_HANDLER(get_window_tree)
1632 struct window *ptr, *win = get_window( req->handle );
1634 if (!win) return;
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;
1645 if (win->parent)
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;
1655 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
1656 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
1660 /* set the position and Z order of a window */
1661 DECL_HANDLER(set_window_pos)
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;
1668 if (!win) return;
1669 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
1671 if (!(flags & SWP_NOZORDER))
1673 if (!req->previous) /* special case: HWND_TOP */
1675 if (get_first_child(win->parent) == win) flags |= SWP_NOZORDER;
1677 else if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
1679 previous = get_last_child( win->parent );
1681 else
1683 if (!(previous = get_window( req->previous ))) return;
1684 /* previous must be a sibling */
1685 if (previous->parent != win->parent)
1687 set_error( STATUS_INVALID_PARAMETER );
1688 return;
1691 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
1694 /* window rectangle must be ordered properly */
1695 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
1697 set_error( STATUS_INVALID_PARAMETER );
1698 return;
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;
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;
1710 /* get the window and client rectangles of a window */
1711 DECL_HANDLER(get_window_rectangles)
1713 struct window *win = get_window( req->handle );
1715 if (win)
1717 reply->window = win->window_rect;
1718 reply->visible = win->visible_rect;
1719 reply->client = win->client_rect;
1724 /* get the window text */
1725 DECL_HANDLER(get_window_text)
1727 struct window *win = get_window( req->handle );
1729 if (win && win->text)
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 );
1738 /* set the window text */
1739 DECL_HANDLER(set_window_text)
1741 struct window *win = get_window( req->handle );
1743 if (win)
1745 WCHAR *text = NULL;
1746 size_t len = get_req_data_size() / sizeof(WCHAR);
1747 if (len)
1749 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
1750 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
1751 text[len] = 0;
1753 if (win->text) free( win->text );
1754 win->text = text;
1759 /* get the coordinates offset between two windows */
1760 DECL_HANDLER(get_windows_offset)
1762 struct window *win;
1764 reply->x = reply->y = 0;
1765 if (req->from)
1767 if (!(win = get_window( req->from ))) return;
1768 while (win)
1770 reply->x += win->client_rect.left;
1771 reply->y += win->client_rect.top;
1772 win = win->parent;
1775 if (req->to)
1777 if (!(win = get_window( req->to ))) return;
1778 while (win)
1780 reply->x -= win->client_rect.left;
1781 reply->y -= win->client_rect.top;
1782 win = win->parent;
1788 /* get the visible region of a window */
1789 DECL_HANDLER(get_visible_region)
1791 struct region *region;
1792 struct window *top, *win = get_window( req->window );
1794 if (!win) return;
1796 top = get_top_clipping_window( win );
1797 if ((region = get_visible_region( win, top, req->flags )))
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 );
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 );
1814 /* get the window region */
1815 DECL_HANDLER(get_window_region)
1817 struct window *win = get_window( req->window );
1819 if (!win) return;
1821 if (win->win_region)
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 );
1829 /* set the window region */
1830 DECL_HANDLER(set_window_region)
1832 struct region *region = NULL;
1833 struct window *win = get_window( req->window );
1835 if (!win) return;
1837 if (get_req_data_size()) /* no data means remove the region completely */
1839 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1840 return;
1842 if (win->win_region) free_region( win->win_region );
1843 win->win_region = region;
1847 /* get a window update region */
1848 DECL_HANDLER(get_update_region)
1850 rectangle_t *data;
1851 unsigned int flags = req->flags;
1852 struct window *from_child = NULL;
1853 struct window *win = get_window( req->window );
1855 reply->flags = 0;
1856 if (!win) return;
1858 if (req->from_child)
1860 struct window *ptr;
1862 if (!(from_child = get_window( req->from_child ))) return;
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)
1869 set_error( STATUS_INVALID_PARAMETER );
1870 return;
1874 reply->flags = get_window_update_flags( win, from_child, flags, &win );
1875 reply->child = win->handle;
1877 if (flags & UPDATE_NOREGION) return;
1879 if (win->update_region)
1881 /* convert update region to screen coordinates */
1882 struct region *region = create_empty_region();
1884 if (!region) return;
1885 if (!copy_region( region, win->update_region ))
1887 free_region( region );
1888 return;
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 );
1896 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
1898 validate_whole_window( win );
1900 else
1902 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
1903 if (reply->flags & UPDATE_ERASE)
1905 win->paint_flags &= ~PAINT_ERASE;
1906 /* desktop window only gets erased, not repainted */
1907 if (is_desktop_window(win)) validate_whole_window( win );
1913 /* update the z order of a window so that a given rectangle is fully visible */
1914 DECL_HANDLER(update_window_zorder)
1916 rectangle_t tmp;
1917 struct window *ptr, *win = get_window( req->window );
1919 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
1921 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
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;
1936 /* mark parts of a window as needing a redraw */
1937 DECL_HANDLER(redraw_window)
1939 struct region *region = NULL;
1940 struct window *win = get_window( req->window );
1942 if (!win) return;
1943 if (!is_visible( win )) return; /* nothing to do */
1945 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
1947 if (get_req_data_size()) /* no data means whole rectangle */
1949 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1950 return;
1954 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
1955 req->flags );
1956 if (region) free_region( region );
1960 /* set a window property */
1961 DECL_HANDLER(set_window_property)
1963 struct window *win = get_window( req->window );
1965 if (!win) return;
1967 if (get_req_data_size())
1969 atom_t atom = add_global_atom( win->desktop->winstation,
1970 get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1971 if (atom)
1973 set_property( win, atom, req->handle, PROP_TYPE_STRING );
1974 release_global_atom( win->desktop->winstation, atom );
1977 else set_property( win, req->atom, req->handle, PROP_TYPE_ATOM );
1981 /* remove a window property */
1982 DECL_HANDLER(remove_window_property)
1984 struct window *win = get_window( req->window );
1986 if (win)
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 );
1996 /* get a window property */
1997 DECL_HANDLER(get_window_property)
1999 struct window *win = get_window( req->window );
2001 if (win)
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 );
2011 /* get the list of properties of a window */
2012 DECL_HANDLER(get_window_properties)
2014 property_data_t *data;
2015 int i, count, max = get_reply_max_size() / sizeof(*data);
2016 struct window *win = get_window( req->window );
2018 reply->total = 0;
2019 if (!win) return;
2021 for (i = count = 0; i < win->prop_inuse; i++)
2022 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2023 reply->total = count;
2025 if (count > max) count = max;
2026 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2028 for (i = 0; i < win->prop_inuse && count; i++)
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--;
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 )
2044 if (!handle)
2046 *win = NULL;
2047 return 1;
2049 else if (*win)
2051 set_error( STATUS_ACCESS_DENIED );
2052 return 0;
2054 *win = get_window( handle );
2055 return (*win != NULL);
2058 /* Set/get the global windows */
2059 DECL_HANDLER(set_global_windows)
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;
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;
2071 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
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;
2076 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2078 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2080 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2082 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2084 shell_window = new_shell_window;
2085 shell_listview = new_shell_listview;
2086 progman_window = new_progman_window;
2087 taskman_window = new_taskman_window;