gdi: better implementation for GetCharABCWidthsFloat{A,W}.
[wine/multimedia.git] / server / window.c
blobbc6c20874334d571a2a3c9b08e62233fc6a80a6a
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 /* destroy a window */
301 void destroy_window( struct window *win )
303 struct thread *thread = win->thread;
305 /* destroy all children */
306 while (!list_empty(&win->children))
307 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
308 while (!list_empty(&win->unlinked))
309 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
311 if (thread && thread->queue)
313 if (win->update_region) inc_queue_paint_count( thread, -1 );
314 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
315 queue_cleanup_window( thread, win->handle );
318 /* reset global window pointers, if the corresponding window is destroyed */
319 if (win == shell_window) shell_window = NULL;
320 if (win == shell_listview) shell_listview = NULL;
321 if (win == progman_window) progman_window = NULL;
322 if (win == taskman_window) taskman_window = NULL;
323 free_user_handle( win->handle );
324 destroy_properties( win );
325 list_remove( &win->entry );
326 if (win->win_region) free_region( win->win_region );
327 if (win->update_region) free_region( win->update_region );
328 release_class( win->class );
329 if (win->text) free( win->text );
330 if (!is_desktop_window(win))
332 assert( thread->desktop_users > 0 );
333 thread->desktop_users--;
334 release_object( win->desktop );
336 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
337 free( win );
340 /* create a new window structure (note: the window is not linked in the window tree) */
341 static struct window *create_window( struct window *parent, struct window *owner,
342 atom_t atom, void *instance )
344 int extra_bytes;
345 struct window *win;
346 struct desktop *desktop;
347 struct window_class *class;
349 if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
351 if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
353 release_object( desktop );
354 return NULL;
357 win = mem_alloc( sizeof(*win) + extra_bytes - 1 );
358 if (!win)
360 release_object( desktop );
361 release_class( class );
362 return NULL;
364 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
366 win->parent = parent;
367 win->owner = owner ? owner->handle : 0;
368 win->thread = current;
369 win->desktop = desktop;
370 win->class = class;
371 win->atom = atom;
372 win->last_active = win->handle;
373 win->win_region = NULL;
374 win->update_region = NULL;
375 win->style = 0;
376 win->ex_style = 0;
377 win->id = 0;
378 win->instance = NULL;
379 win->is_unicode = 1;
380 win->user_data = NULL;
381 win->text = NULL;
382 win->paint_flags = 0;
383 win->prop_inuse = 0;
384 win->prop_alloc = 0;
385 win->properties = NULL;
386 win->nb_extra_bytes = extra_bytes;
387 memset( win->extra_bytes, 0, extra_bytes );
388 list_init( &win->children );
389 list_init( &win->unlinked );
391 /* parent must be on the same desktop */
392 if (parent && parent->desktop != desktop)
394 set_error( STATUS_ACCESS_DENIED );
395 goto failed;
398 /* if parent belongs to a different thread, attach the two threads */
399 if (parent && parent->thread && parent->thread != current)
401 if (!attach_thread_input( current, parent->thread )) goto failed;
403 else /* otherwise just make sure that the thread has a message queue */
405 if (!current->queue && !init_thread_queue( current )) goto failed;
408 /* put it on parent unlinked list */
409 if (parent) list_add_head( &parent->unlinked, &win->entry );
410 else list_init( &win->entry );
412 current->desktop_users++;
413 return win;
415 failed:
416 if (win->handle) free_user_handle( win->handle );
417 release_object( desktop );
418 release_class( class );
419 free( win );
420 return NULL;
423 /* destroy all windows belonging to a given thread */
424 void destroy_thread_windows( struct thread *thread )
426 user_handle_t handle = 0;
427 struct window *win;
429 while ((win = next_user_handle( &handle, USER_WINDOW )))
431 if (win->thread != thread) continue;
432 destroy_window( win );
436 /* get the desktop window */
437 static struct window *get_desktop_window( struct thread *thread, int create )
439 struct window *top_window;
440 struct desktop *desktop = get_thread_desktop( thread, 0 );
442 if (!desktop) return NULL;
444 if (!(top_window = desktop->top_window) && create)
446 if ((top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
448 current->desktop_users--;
449 top_window->thread = NULL; /* no thread owns the desktop */
450 top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
451 desktop->top_window = top_window;
452 /* don't hold a reference to the desktop so that the desktop window can be */
453 /* destroyed when the desktop ref count reaches zero */
454 release_object( top_window->desktop );
457 release_object( desktop );
458 return top_window;
461 /* check whether child is a descendant of parent */
462 int is_child_window( user_handle_t parent, user_handle_t child )
464 struct window *child_ptr = get_user_object( child, USER_WINDOW );
465 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
467 if (!child_ptr || !parent_ptr) return 0;
468 while (child_ptr->parent)
470 if (child_ptr->parent == parent_ptr) return 1;
471 child_ptr = child_ptr->parent;
473 return 0;
476 /* check whether window is a top-level window */
477 int is_top_level_window( user_handle_t window )
479 struct window *win = get_user_object( window, USER_WINDOW );
480 return (win && win->parent && is_desktop_window(win->parent));
483 /* make a window active if possible */
484 int make_window_active( user_handle_t window )
486 struct window *owner, *win = get_window( window );
488 if (!win) return 0;
490 /* set last active for window and its owner */
491 win->last_active = win->handle;
492 if ((owner = get_user_object( win->owner, USER_WINDOW ))) owner->last_active = win->handle;
493 return 1;
496 /* increment (or decrement) the window paint count */
497 static inline void inc_window_paint_count( struct window *win, int incr )
499 if (win->thread) inc_queue_paint_count( win->thread, incr );
502 /* check if window and all its ancestors are visible */
503 static int is_visible( const struct window *win )
505 while (win && win->parent)
507 if (!(win->style & WS_VISIBLE)) return 0;
508 win = win->parent;
509 /* if parent is minimized children are not visible */
510 if (win && (win->style & WS_MINIMIZE)) return 0;
512 return 1;
515 /* same as is_visible but takes a window handle */
516 int is_window_visible( user_handle_t window )
518 struct window *win = get_user_object( window, USER_WINDOW );
519 if (!win) return 0;
520 return is_visible( win );
523 /* check if point is inside the window */
524 static inline int is_point_in_window( struct window *win, int x, int y )
526 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
527 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
528 return 0; /* disabled child */
529 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
530 return 0; /* transparent */
531 if (x < win->visible_rect.left || x >= win->visible_rect.right ||
532 y < win->visible_rect.top || y >= win->visible_rect.bottom)
533 return 0; /* not in window */
534 if (win->win_region &&
535 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
536 return 0; /* not in window region */
537 return 1;
540 /* find child of 'parent' that contains the given point (in parent-relative coords) */
541 static struct window *child_window_from_point( struct window *parent, int x, int y )
543 struct window *ptr;
545 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
547 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
549 /* if window is minimized or disabled, return at once */
550 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
552 /* if point is not in client area, return at once */
553 if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
554 y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
555 return ptr;
557 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
559 return parent; /* not found any child */
562 /* find all children of 'parent' that contain the given point */
563 static int get_window_children_from_point( struct window *parent, int x, int y,
564 struct user_handle_array *array )
566 struct window *ptr;
568 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
570 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
572 /* if point is in client area, and window is not minimized or disabled, check children */
573 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
574 x >= ptr->client_rect.left && x < ptr->client_rect.right &&
575 y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
577 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
578 y - ptr->client_rect.top, array ))
579 return 0;
582 /* now add window to the array */
583 if (!add_handle_to_array( array, ptr->handle )) return 0;
585 return 1;
588 /* find window containing point (in absolute coords) */
589 user_handle_t window_from_point( struct desktop *desktop, int x, int y )
591 struct window *ret;
593 if (!desktop->top_window) return 0;
594 ret = child_window_from_point( desktop->top_window, x, y );
595 return ret->handle;
598 /* return list of all windows containing point (in absolute coords) */
599 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
601 struct window *ptr;
603 /* make point relative to top window */
604 for (ptr = top->parent; ptr; ptr = ptr->parent)
606 x -= ptr->client_rect.left;
607 y -= ptr->client_rect.top;
610 if (!is_point_in_window( top, x, y )) return 1;
612 /* if point is in client area, and window is not minimized or disabled, check children */
613 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
614 x >= top->client_rect.left && x < top->client_rect.right &&
615 y >= top->client_rect.top && y < top->client_rect.bottom)
617 if (!get_window_children_from_point( top, x - top->client_rect.left,
618 y - top->client_rect.top, array ))
619 return 0;
621 /* now add window to the array */
622 if (!add_handle_to_array( array, top->handle )) return 0;
623 return 1;
627 /* return the thread owning a window */
628 struct thread *get_window_thread( user_handle_t handle )
630 struct window *win = get_user_object( handle, USER_WINDOW );
631 if (!win || !win->thread) return NULL;
632 return (struct thread *)grab_object( win->thread );
636 /* check if any area of a window needs repainting */
637 static inline int win_needs_repaint( struct window *win )
639 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
643 /* find a child of the specified window that needs repainting */
644 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
646 struct window *ptr, *ret = NULL;
648 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
650 if (!(ptr->style & WS_VISIBLE)) continue;
651 if (ptr->thread == thread && win_needs_repaint( ptr ))
652 ret = ptr;
653 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
654 ret = find_child_to_repaint( ptr, thread );
655 if (ret) break;
658 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
660 /* transparent window, check for non-transparent sibling to paint first */
661 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
663 if (!(ptr->style & WS_VISIBLE)) continue;
664 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
665 if (ptr->thread != thread) continue;
666 if (win_needs_repaint( ptr )) return ptr;
669 return ret;
673 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
674 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
676 struct window *ptr, *win, *top_window = get_desktop_window( thread, 0 );
678 if (!top_window) return 0;
680 win = find_child_to_repaint( top_window, thread );
681 if (win && parent)
683 /* check that it is a child of the specified parent */
684 for (ptr = win; ptr; ptr = ptr->parent)
685 if (ptr->handle == parent) break;
686 /* otherwise don't return any window, we don't repaint a child before its parent */
687 if (!ptr) win = NULL;
689 if (!win) return 0;
690 win->paint_flags &= ~PAINT_INTERNAL;
691 return win->handle;
695 /* intersect the window region with the specified region, relative to the window parent */
696 static struct region *intersect_window_region( struct region *region, struct window *win )
698 /* make region relative to window rect */
699 offset_region( region, -win->window_rect.left, -win->window_rect.top );
700 if (!intersect_region( region, region, win->win_region )) return NULL;
701 /* make region relative to parent again */
702 offset_region( region, win->window_rect.left, win->window_rect.top );
703 return region;
707 /* convert coordinates from client to screen coords */
708 static inline void client_to_screen( struct window *win, int *x, int *y )
710 for ( ; win; win = win->parent)
712 *x += win->client_rect.left;
713 *y += win->client_rect.top;
717 /* map the region from window to screen coordinates */
718 static inline void map_win_region_to_screen( struct window *win, struct region *region )
720 int x = win->window_rect.left;
721 int y = win->window_rect.top;
722 client_to_screen( win->parent, &x, &y );
723 offset_region( region, x, y );
727 /* clip all children of a given window out of the visible region */
728 static struct region *clip_children( struct window *parent, struct window *last,
729 struct region *region, int offset_x, int offset_y )
731 struct window *ptr;
732 struct region *tmp = create_empty_region();
734 if (!tmp) return NULL;
735 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
737 if (ptr == last) break;
738 if (!(ptr->style & WS_VISIBLE)) continue;
739 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
740 set_region_rect( tmp, &ptr->visible_rect );
741 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
743 free_region( tmp );
744 return NULL;
746 offset_region( tmp, offset_x, offset_y );
747 if (!(region = subtract_region( region, region, tmp ))) break;
748 if (is_region_empty( region )) break;
750 free_region( tmp );
751 return region;
755 /* compute the intersection of two rectangles; return 0 if the result is empty */
756 static inline int intersect_rect( rectangle_t *dst, const rectangle_t *src1, const rectangle_t *src2 )
758 dst->left = max( src1->left, src2->left );
759 dst->top = max( src1->top, src2->top );
760 dst->right = min( src1->right, src2->right );
761 dst->bottom = min( src1->bottom, src2->bottom );
762 return (dst->left < dst->right && dst->top < dst->bottom);
766 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
767 static void set_region_client_rect( struct region *region, struct window *win )
769 rectangle_t rect;
771 intersect_rect( &rect, &win->window_rect, &win->client_rect );
772 set_region_rect( region, &rect );
776 /* get the top-level window to clip against for a given window */
777 static inline struct window *get_top_clipping_window( struct window *win )
779 while (win->parent && !is_desktop_window(win->parent)) win = win->parent;
780 return win;
784 /* compute the visible region of a window, in window coordinates */
785 static struct region *get_visible_region( struct window *win, struct window *top, unsigned int flags )
787 struct region *tmp = NULL, *region;
788 int offset_x, offset_y;
790 if (!(region = create_empty_region())) return NULL;
792 /* first check if all ancestors are visible */
794 if (!is_visible( win )) return region; /* empty region */
796 /* create a region relative to the window itself */
798 if ((flags & DCX_PARENTCLIP) && win != top && win->parent)
800 set_region_client_rect( region, win->parent );
801 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
803 else if (flags & DCX_WINDOW)
805 set_region_rect( region, &win->visible_rect );
806 if (win->win_region && !intersect_window_region( region, win )) goto error;
808 else
810 set_region_client_rect( region, win );
811 if (win->win_region && !intersect_window_region( region, win )) goto error;
813 offset_x = win->window_rect.left;
814 offset_y = win->window_rect.top;
816 /* clip children */
818 if (flags & DCX_CLIPCHILDREN)
820 if (!clip_children( win, NULL, region, win->client_rect.left, win->client_rect.top ))
821 goto error;
824 /* clip siblings of ancestors */
826 if (top && top != win && (tmp = create_empty_region()) != NULL)
828 while (win != top && win->parent)
830 if (win->style & WS_CLIPSIBLINGS)
832 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
833 if (is_region_empty( region )) break;
835 /* clip to parent client area */
836 win = win->parent;
837 offset_x += win->client_rect.left;
838 offset_y += win->client_rect.top;
839 offset_region( region, win->client_rect.left, win->client_rect.top );
840 set_region_client_rect( tmp, win );
841 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
842 if (!intersect_region( region, region, tmp )) goto error;
843 if (is_region_empty( region )) break;
845 free_region( tmp );
847 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
848 return region;
850 error:
851 if (tmp) free_region( tmp );
852 free_region( region );
853 return NULL;
857 /* get the window class of a window */
858 struct window_class* get_window_class( user_handle_t window )
860 struct window *win;
861 if (!(win = get_window( window ))) return NULL;
862 return win->class;
865 /* return a copy of the specified region cropped to the window client or frame rectangle, */
866 /* and converted from client to window coordinates. Helper for (in)validate_window. */
867 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
869 struct region *tmp = create_empty_region();
871 if (!tmp) return NULL;
873 /* get bounding rect in client coords */
874 if (frame) set_region_rect( tmp, &win->window_rect );
875 else set_region_client_rect( tmp, win );
876 offset_region( tmp, -win->client_rect.left, -win->client_rect.top );
878 /* intersect specified region with bounding rect */
879 if (region && !intersect_region( tmp, region, tmp )) goto done;
880 if (is_region_empty( tmp )) goto done;
882 /* map it to window coords */
883 offset_region( tmp, win->client_rect.left - win->window_rect.left,
884 win->client_rect.top - win->window_rect.top );
885 return tmp;
887 done:
888 free_region( tmp );
889 return NULL;
893 /* set a region as new update region for the window */
894 static void set_update_region( struct window *win, struct region *region )
896 if (region && !is_region_empty( region ))
898 if (!win->update_region) inc_window_paint_count( win, 1 );
899 else free_region( win->update_region );
900 win->update_region = region;
902 else
904 if (win->update_region)
906 inc_window_paint_count( win, -1 );
907 free_region( win->update_region );
909 win->paint_flags &= ~(PAINT_ERASE | PAINT_NONCLIENT);
910 win->update_region = NULL;
911 if (region) free_region( region );
916 /* add a region to the update region; the passed region is freed or reused */
917 static int add_update_region( struct window *win, struct region *region )
919 if (win->update_region && !union_region( region, win->update_region, region ))
921 free_region( region );
922 return 0;
924 set_update_region( win, region );
925 return 1;
929 /* validate the non client area of a window */
930 static void validate_non_client( struct window *win )
932 struct region *tmp;
933 rectangle_t rect;
935 if (!win->update_region) return; /* nothing to do */
937 /* get client rect in window coords */
938 rect.left = win->client_rect.left - win->window_rect.left;
939 rect.top = win->client_rect.top - win->window_rect.top;
940 rect.right = win->client_rect.right - win->window_rect.left;
941 rect.bottom = win->client_rect.bottom - win->window_rect.top;
943 if ((tmp = create_empty_region()))
945 set_region_rect( tmp, &rect );
946 if (intersect_region( tmp, win->update_region, tmp ))
947 set_update_region( win, tmp );
948 else
949 free_region( tmp );
951 win->paint_flags &= ~PAINT_NONCLIENT;
955 /* validate a window completely so that we don't get any further paint messages for it */
956 static void validate_whole_window( struct window *win )
958 set_update_region( win, NULL );
960 if (win->paint_flags & PAINT_INTERNAL)
962 win->paint_flags &= ~PAINT_INTERNAL;
963 inc_window_paint_count( win, -1 );
968 /* validate the update region of a window on all parents; helper for redraw_window */
969 static void validate_parents( struct window *child )
971 int offset_x = 0, offset_y = 0;
972 struct window *win = child;
973 struct region *tmp = NULL;
975 if (!child->update_region) return;
977 while (win->parent)
979 /* map to parent client coords */
980 offset_x += win->window_rect.left;
981 offset_y += win->window_rect.top;
983 win = win->parent;
985 /* and now map to window coords */
986 offset_x += win->client_rect.left - win->window_rect.left;
987 offset_y += win->client_rect.top - win->window_rect.top;
989 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
991 if (!tmp && !(tmp = create_empty_region())) return;
992 offset_region( child->update_region, offset_x, offset_y );
993 if (subtract_region( tmp, win->update_region, child->update_region ))
995 set_update_region( win, tmp );
996 tmp = NULL;
998 /* restore child coords */
999 offset_region( child->update_region, -offset_x, -offset_y );
1002 if (tmp) free_region( tmp );
1006 /* add/subtract a region (in client coordinates) to the update region of the window */
1007 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1009 struct region *tmp;
1010 struct window *child;
1012 if (flags & RDW_INVALIDATE)
1014 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1016 if (!add_update_region( win, tmp )) return;
1018 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1019 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1021 else if (flags & RDW_VALIDATE)
1023 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1025 set_update_region( win, NULL );
1027 else if (win->update_region)
1029 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1031 if (!subtract_region( tmp, win->update_region, tmp ))
1033 free_region( tmp );
1034 return;
1036 set_update_region( win, tmp );
1038 if (flags & RDW_NOFRAME) validate_non_client( win );
1039 if (flags & RDW_NOERASE) win->paint_flags &= ~PAINT_ERASE;
1043 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1045 win->paint_flags |= PAINT_INTERNAL;
1046 inc_window_paint_count( win, 1 );
1048 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1050 win->paint_flags &= ~PAINT_INTERNAL;
1051 inc_window_paint_count( win, -1 );
1054 if (flags & RDW_UPDATENOW)
1056 validate_parents( win );
1057 flags &= ~RDW_UPDATENOW;
1060 /* now process children recursively */
1062 if (flags & RDW_NOCHILDREN) return;
1063 if (win->style & WS_MINIMIZE) return;
1064 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1066 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1068 /* map to client coordinates */
1069 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1070 win->window_rect.top - win->client_rect.top );
1072 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1074 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1076 if (!(child->style & WS_VISIBLE)) continue;
1077 if (!rect_in_region( tmp, &child->window_rect )) continue;
1078 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1079 redraw_window( child, tmp, 1, flags );
1080 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1082 free_region( tmp );
1086 /* retrieve the update flags for a window depending on the state of the update region */
1087 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1089 unsigned int ret = 0;
1091 if (flags & UPDATE_NONCLIENT)
1093 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1095 if (flags & UPDATE_ERASE)
1097 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1099 if (flags & UPDATE_PAINT)
1101 if (win->update_region) ret |= UPDATE_PAINT;
1103 if (flags & UPDATE_INTERNALPAINT)
1105 if (win->paint_flags & PAINT_INTERNAL) ret |= UPDATE_INTERNALPAINT;
1107 return ret;
1111 /* iterate through the children of the given window until we find one with some update flags */
1112 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1113 unsigned int flags, struct window **child )
1115 struct window *ptr;
1116 unsigned int ret = 0;
1118 /* first make sure we want to iterate children at all */
1120 if (win->style & WS_MINIMIZE) return 0;
1122 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1123 * here we only want to repaint children of windows that clip them, others
1124 * need to wait for WM_PAINT to be done in the parent first.
1126 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1128 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1130 if (from_child) /* skip all children until from_child is found */
1132 if (ptr == from_child) from_child = NULL;
1133 continue;
1135 if (!(ptr->style & WS_VISIBLE)) continue;
1136 if ((ret = get_update_flags( ptr, flags )) != 0)
1138 *child = ptr;
1139 break;
1141 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1143 return ret;
1146 /* iterate through children and siblings of the given window until we find one with some update flags */
1147 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1148 unsigned int flags, struct window **child )
1150 unsigned int ret;
1151 struct window *ptr, *from_sibling = NULL;
1153 /* if some parent is not visible start from the next sibling */
1155 if (!is_visible( win )) return 0;
1156 for (ptr = from_child; ptr; ptr = ptr->parent)
1158 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1159 if (ptr == win) break;
1162 /* non-client painting must be delayed if one of the parents is going to
1163 * be repainted and doesn't clip children */
1165 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1167 for (ptr = win->parent; ptr; ptr = ptr->parent)
1169 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1170 return 0;
1172 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1174 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1176 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1177 if (ptr == win) break;
1183 /* check window itself (only if not restarting from a child) */
1185 if (!from_child)
1187 if ((ret = get_update_flags( win, flags )))
1189 *child = win;
1190 return ret;
1192 from_child = win;
1195 /* now check children */
1197 if (flags & UPDATE_NOCHILDREN) return 0;
1198 if (!from_sibling)
1200 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1201 from_sibling = from_child;
1204 /* then check siblings and parent siblings */
1206 while (from_sibling->parent && from_sibling != win)
1208 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1209 return ret;
1210 from_sibling = from_sibling->parent;
1212 return 0;
1216 /* expose a region of a window, looking for the top most parent that needs to be exposed */
1217 /* the region is in window coordinates */
1218 static void expose_window( struct window *win, struct window *top, struct region *region )
1220 struct window *parent, *ptr;
1221 int offset_x, offset_y;
1223 /* find the top most parent that doesn't clip either siblings or children */
1224 for (parent = ptr = win; ptr != top; ptr = ptr->parent)
1226 if (!(ptr->style & WS_CLIPCHILDREN)) parent = ptr;
1227 if (!(ptr->style & WS_CLIPSIBLINGS)) parent = ptr->parent;
1229 if (parent == win && parent != top && win->parent)
1230 parent = win->parent; /* always go up at least one level if possible */
1232 offset_x = win->window_rect.left - win->client_rect.left;
1233 offset_y = win->window_rect.top - win->client_rect.top;
1234 for (ptr = win; ptr != parent; ptr = ptr->parent)
1236 offset_x += ptr->client_rect.left;
1237 offset_y += ptr->client_rect.top;
1239 offset_region( region, offset_x, offset_y );
1240 redraw_window( parent, region, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1241 offset_region( region, -offset_x, -offset_y );
1245 /* set the window and client rectangles, updating the update region if necessary */
1246 static void set_window_pos( struct window *win, struct window *previous,
1247 unsigned int swp_flags, const rectangle_t *window_rect,
1248 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1249 const rectangle_t *valid_rects )
1251 struct region *old_vis_rgn = NULL, *new_vis_rgn;
1252 const rectangle_t old_window_rect = win->window_rect;
1253 const rectangle_t old_visible_rect = win->visible_rect;
1254 const rectangle_t old_client_rect = win->client_rect;
1255 struct window *top = get_top_clipping_window( win );
1256 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1258 if (win->parent && !is_visible( win->parent )) visible = 0;
1260 if (visible && !(old_vis_rgn = get_visible_region( win, top, DCX_WINDOW ))) return;
1262 /* set the new window info before invalidating anything */
1264 win->window_rect = *window_rect;
1265 win->visible_rect = *visible_rect;
1266 win->client_rect = *client_rect;
1267 if (!(swp_flags & SWP_NOZORDER) && win->parent)
1269 list_remove( &win->entry ); /* unlink it from the previous location */
1270 if (previous) list_add_after( &previous->entry, &win->entry );
1271 else list_add_head( &win->parent->children, &win->entry );
1273 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1274 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1276 /* if the window is not visible, everything is easy */
1277 if (!visible) return;
1279 if (!(new_vis_rgn = get_visible_region( win, top, DCX_WINDOW )))
1281 free_region( old_vis_rgn );
1282 clear_error(); /* ignore error since the window info has been modified already */
1283 return;
1286 /* expose anything revealed by the change */
1288 if (!(swp_flags & SWP_NOREDRAW))
1290 offset_region( old_vis_rgn, old_window_rect.left - window_rect->left,
1291 old_window_rect.top - window_rect->top );
1292 if (xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1293 expose_window( win, top, new_vis_rgn );
1295 free_region( old_vis_rgn );
1297 if (!(win->style & WS_VISIBLE))
1299 /* clear the update region since the window is no longer visible */
1300 validate_whole_window( win );
1301 goto done;
1304 /* crop update region to the new window rect */
1306 if (win->update_region &&
1307 (window_rect->right - window_rect->left < old_window_rect.right - old_window_rect.left ||
1308 window_rect->bottom - window_rect->top < old_window_rect.bottom - old_window_rect.top))
1310 struct region *tmp = create_empty_region();
1311 if (tmp)
1313 set_region_rect( tmp, window_rect );
1314 offset_region( tmp, -window_rect->left, -window_rect->top );
1315 if (intersect_region( tmp, win->update_region, tmp ))
1316 set_update_region( win, tmp );
1317 else
1318 free_region( tmp );
1322 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1324 /* expose the whole non-client area if it changed in any way */
1326 if ((swp_flags & SWP_FRAMECHANGED) ||
1327 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1328 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ) ||
1329 memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1331 struct region *tmp = create_empty_region();
1333 if (tmp)
1335 /* subtract the valid portion of client rect from the total region */
1336 if (!memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1337 set_region_rect( tmp, client_rect );
1338 else if (valid_rects)
1339 set_region_rect( tmp, &valid_rects[0] );
1341 set_region_rect( new_vis_rgn, window_rect );
1342 if (subtract_region( tmp, new_vis_rgn, tmp ))
1344 offset_region( tmp, -client_rect->left, -client_rect->top );
1345 redraw_window( win, tmp, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1347 free_region( tmp );
1351 done:
1352 free_region( new_vis_rgn );
1353 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1357 /* create a window */
1358 DECL_HANDLER(create_window)
1360 struct window *win, *parent, *owner = NULL;
1362 reply->handle = 0;
1364 if (!(parent = get_window( req->parent ))) return;
1365 if (req->owner)
1367 if (!(owner = get_window( req->owner ))) return;
1368 if (is_desktop_window(owner)) owner = NULL;
1369 else if (!is_desktop_window(parent))
1371 /* an owned window must be created as top-level */
1372 set_error( STATUS_ACCESS_DENIED );
1373 return;
1376 if (!(win = create_window( parent, owner, req->atom, req->instance ))) return;
1378 reply->handle = win->handle;
1379 reply->extra = win->nb_extra_bytes;
1380 reply->class_ptr = get_class_client_ptr( win->class );
1384 /* set the parent of a window */
1385 DECL_HANDLER(set_parent)
1387 struct window *win, *parent = NULL;
1389 if (!(win = get_window( req->handle ))) return;
1390 if (req->parent && !(parent = get_window( req->parent ))) return;
1392 if (is_desktop_window(win))
1394 set_error( STATUS_INVALID_PARAMETER );
1395 return;
1397 reply->old_parent = win->parent->handle;
1398 reply->full_parent = parent ? parent->handle : 0;
1399 set_parent_window( win, parent );
1403 /* destroy a window */
1404 DECL_HANDLER(destroy_window)
1406 struct window *win = get_window( req->handle );
1407 if (win)
1409 if (!is_desktop_window(win)) destroy_window( win );
1410 else set_error( STATUS_ACCESS_DENIED );
1415 /* retrieve the desktop window for the current thread */
1416 DECL_HANDLER(get_desktop_window)
1418 struct window *win = get_desktop_window( current, 1 );
1420 if (win) reply->handle = win->handle;
1424 /* set a window owner */
1425 DECL_HANDLER(set_window_owner)
1427 struct window *win = get_window( req->handle );
1428 struct window *owner = NULL;
1430 if (!win) return;
1431 if (req->owner && !(owner = get_window( req->owner ))) return;
1432 if (is_desktop_window(win))
1434 set_error( STATUS_ACCESS_DENIED );
1435 return;
1437 reply->prev_owner = win->owner;
1438 reply->full_owner = win->owner = owner ? owner->handle : 0;
1442 /* get information from a window handle */
1443 DECL_HANDLER(get_window_info)
1445 struct window *win = get_window( req->handle );
1447 reply->full_handle = 0;
1448 reply->tid = reply->pid = 0;
1449 if (win)
1451 reply->full_handle = win->handle;
1452 reply->last_active = win->handle;
1453 reply->is_unicode = win->is_unicode;
1454 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
1455 if (win->thread)
1457 reply->tid = get_thread_id( win->thread );
1458 reply->pid = get_process_id( win->thread->process );
1459 reply->atom = get_class_atom( win->class );
1465 /* set some information in a window */
1466 DECL_HANDLER(set_window_info)
1468 struct window *win = get_window( req->handle );
1470 if (!win) return;
1471 if (req->flags && is_desktop_window(win))
1473 set_error( STATUS_ACCESS_DENIED );
1474 return;
1476 if (req->extra_size > sizeof(req->extra_value) ||
1477 req->extra_offset < -1 ||
1478 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
1480 set_win32_error( ERROR_INVALID_INDEX );
1481 return;
1483 if (req->extra_offset != -1)
1485 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
1487 else if (req->flags & SET_WIN_EXTRA)
1489 set_win32_error( ERROR_INVALID_INDEX );
1490 return;
1492 reply->old_style = win->style;
1493 reply->old_ex_style = win->ex_style;
1494 reply->old_id = win->id;
1495 reply->old_instance = win->instance;
1496 reply->old_user_data = win->user_data;
1497 if (req->flags & SET_WIN_STYLE) win->style = req->style;
1498 if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
1499 if (req->flags & SET_WIN_ID) win->id = req->id;
1500 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
1501 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
1502 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
1503 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
1504 &req->extra_value, req->extra_size );
1506 /* changing window style triggers a non-client paint */
1507 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
1511 /* get a list of the window parents, up to the root of the tree */
1512 DECL_HANDLER(get_window_parents)
1514 struct window *ptr, *win = get_window( req->handle );
1515 int total = 0;
1516 user_handle_t *data;
1517 size_t len;
1519 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
1521 reply->count = total;
1522 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1523 if (len && ((data = set_reply_data_size( len ))))
1525 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
1526 *data++ = ptr->handle;
1531 /* get a list of the window children */
1532 DECL_HANDLER(get_window_children)
1534 struct window *ptr, *parent = get_window( req->parent );
1535 int total = 0;
1536 user_handle_t *data;
1537 size_t len;
1539 if (parent)
1541 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
1543 if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1544 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1545 total++;
1548 reply->count = total;
1549 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1550 if (len && ((data = set_reply_data_size( len ))))
1552 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
1554 if (len < sizeof(*data)) break;
1555 if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1556 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1557 *data++ = ptr->handle;
1558 len -= sizeof(*data);
1564 /* get a list of the window children that contain a given point */
1565 DECL_HANDLER(get_window_children_from_point)
1567 struct user_handle_array array;
1568 struct window *parent = get_window( req->parent );
1569 size_t len;
1571 if (!parent) return;
1573 array.handles = NULL;
1574 array.count = 0;
1575 array.total = 0;
1576 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
1578 reply->count = array.count;
1579 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
1580 if (len) set_reply_data_ptr( array.handles, len );
1581 else free( array.handles );
1585 /* get window tree information from a window handle */
1586 DECL_HANDLER(get_window_tree)
1588 struct window *ptr, *win = get_window( req->handle );
1590 if (!win) return;
1592 reply->parent = 0;
1593 reply->owner = 0;
1594 reply->next_sibling = 0;
1595 reply->prev_sibling = 0;
1596 reply->first_sibling = 0;
1597 reply->last_sibling = 0;
1598 reply->first_child = 0;
1599 reply->last_child = 0;
1601 if (win->parent)
1603 struct window *parent = win->parent;
1604 reply->parent = parent->handle;
1605 reply->owner = win->owner;
1606 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
1607 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
1608 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
1609 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
1611 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
1612 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
1616 /* set the position and Z order of a window */
1617 DECL_HANDLER(set_window_pos)
1619 const rectangle_t *visible_rect = NULL, *valid_rects = NULL;
1620 struct window *previous = NULL;
1621 struct window *win = get_window( req->handle );
1622 unsigned int flags = req->flags;
1624 if (!win) return;
1625 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
1627 if (!(flags & SWP_NOZORDER))
1629 if (!req->previous) /* special case: HWND_TOP */
1631 if (get_first_child(win->parent) == win) flags |= SWP_NOZORDER;
1633 else if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
1635 previous = get_last_child( win->parent );
1637 else
1639 if (!(previous = get_window( req->previous ))) return;
1640 /* previous must be a sibling */
1641 if (previous->parent != win->parent)
1643 set_error( STATUS_INVALID_PARAMETER );
1644 return;
1647 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
1650 /* window rectangle must be ordered properly */
1651 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
1653 set_error( STATUS_INVALID_PARAMETER );
1654 return;
1657 if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = get_req_data();
1658 if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rects = visible_rect + 1;
1660 if (!visible_rect) visible_rect = &req->window;
1661 set_window_pos( win, previous, flags, &req->window, &req->client, visible_rect, valid_rects );
1662 reply->new_style = win->style;
1666 /* get the window and client rectangles of a window */
1667 DECL_HANDLER(get_window_rectangles)
1669 struct window *win = get_window( req->handle );
1671 if (win)
1673 reply->window = win->window_rect;
1674 reply->visible = win->visible_rect;
1675 reply->client = win->client_rect;
1680 /* get the window text */
1681 DECL_HANDLER(get_window_text)
1683 struct window *win = get_window( req->handle );
1685 if (win && win->text)
1687 size_t len = strlenW( win->text ) * sizeof(WCHAR);
1688 if (len > get_reply_max_size()) len = get_reply_max_size();
1689 set_reply_data( win->text, len );
1694 /* set the window text */
1695 DECL_HANDLER(set_window_text)
1697 struct window *win = get_window( req->handle );
1699 if (win)
1701 WCHAR *text = NULL;
1702 size_t len = get_req_data_size() / sizeof(WCHAR);
1703 if (len)
1705 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
1706 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
1707 text[len] = 0;
1709 if (win->text) free( win->text );
1710 win->text = text;
1715 /* get the coordinates offset between two windows */
1716 DECL_HANDLER(get_windows_offset)
1718 struct window *win;
1720 reply->x = reply->y = 0;
1721 if (req->from)
1723 if (!(win = get_window( req->from ))) return;
1724 while (win)
1726 reply->x += win->client_rect.left;
1727 reply->y += win->client_rect.top;
1728 win = win->parent;
1731 if (req->to)
1733 if (!(win = get_window( req->to ))) return;
1734 while (win)
1736 reply->x -= win->client_rect.left;
1737 reply->y -= win->client_rect.top;
1738 win = win->parent;
1744 /* get the visible region of a window */
1745 DECL_HANDLER(get_visible_region)
1747 struct region *region;
1748 struct window *top, *win = get_window( req->window );
1750 if (!win) return;
1752 top = get_top_clipping_window( win );
1753 if ((region = get_visible_region( win, top, req->flags )))
1755 rectangle_t *data;
1756 map_win_region_to_screen( win, region );
1757 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
1758 if (data) set_reply_data_ptr( data, reply->total_size );
1760 reply->top_win = top->handle;
1761 reply->top_org_x = top->visible_rect.left;
1762 reply->top_org_y = top->visible_rect.top;
1763 reply->win_org_x = (req->flags & DCX_WINDOW) ? win->window_rect.left : win->client_rect.left;
1764 reply->win_org_y = (req->flags & DCX_WINDOW) ? win->window_rect.top : win->client_rect.top;
1765 client_to_screen( top->parent, &reply->top_org_x, &reply->top_org_y );
1766 client_to_screen( win->parent, &reply->win_org_x, &reply->win_org_y );
1770 /* get the window region */
1771 DECL_HANDLER(get_window_region)
1773 struct window *win = get_window( req->window );
1775 if (!win) return;
1777 if (win->win_region)
1779 rectangle_t *data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
1780 if (data) set_reply_data_ptr( data, reply->total_size );
1785 /* set the window region */
1786 DECL_HANDLER(set_window_region)
1788 struct region *region = NULL;
1789 struct window *win = get_window( req->window );
1791 if (!win) return;
1793 if (get_req_data_size()) /* no data means remove the region completely */
1795 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1796 return;
1798 if (win->win_region) free_region( win->win_region );
1799 win->win_region = region;
1803 /* get a window update region */
1804 DECL_HANDLER(get_update_region)
1806 rectangle_t *data;
1807 unsigned int flags = req->flags;
1808 struct window *from_child = NULL;
1809 struct window *win = get_window( req->window );
1811 reply->flags = 0;
1812 if (!win) return;
1814 if (req->from_child)
1816 struct window *ptr;
1818 if (!(from_child = get_window( req->from_child ))) return;
1820 /* make sure from_child is a child of win */
1821 ptr = from_child;
1822 while (ptr && ptr != win) ptr = ptr->parent;
1823 if (!ptr)
1825 set_error( STATUS_INVALID_PARAMETER );
1826 return;
1830 reply->flags = get_window_update_flags( win, from_child, flags, &win );
1831 reply->child = win->handle;
1833 if (flags & UPDATE_NOREGION) return;
1835 if (win->update_region)
1837 /* convert update region to screen coordinates */
1838 struct region *region = create_empty_region();
1840 if (!region) return;
1841 if (!copy_region( region, win->update_region ))
1843 free_region( region );
1844 return;
1846 map_win_region_to_screen( win, region );
1847 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
1848 &reply->total_size ))) return;
1849 set_reply_data_ptr( data, reply->total_size );
1852 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
1854 validate_whole_window( win );
1856 else
1858 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
1859 if (reply->flags & UPDATE_ERASE)
1861 win->paint_flags &= ~PAINT_ERASE;
1862 /* desktop window only gets erased, not repainted */
1863 if (is_desktop_window(win)) validate_whole_window( win );
1869 /* update the z order of a window so that a given rectangle is fully visible */
1870 DECL_HANDLER(update_window_zorder)
1872 rectangle_t tmp;
1873 struct window *ptr, *win = get_window( req->window );
1875 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
1877 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
1879 if (ptr == win) break;
1880 if (!(ptr->style & WS_VISIBLE)) continue;
1881 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1882 if (!intersect_rect( &tmp, &ptr->visible_rect, &req->rect )) continue;
1883 if (ptr->win_region && !rect_in_region( ptr->win_region, &req->rect )) continue;
1884 /* found a window obscuring the rectangle, now move win above this one */
1885 list_remove( &win->entry );
1886 list_add_before( &ptr->entry, &win->entry );
1887 break;
1892 /* mark parts of a window as needing a redraw */
1893 DECL_HANDLER(redraw_window)
1895 struct region *region = NULL;
1896 struct window *win = get_window( req->window );
1898 if (!win) return;
1899 if (!is_visible( win )) return; /* nothing to do */
1901 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
1903 if (get_req_data_size()) /* no data means whole rectangle */
1905 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1906 return;
1910 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
1911 req->flags );
1912 if (region) free_region( region );
1916 /* set a window property */
1917 DECL_HANDLER(set_window_property)
1919 struct window *win = get_window( req->window );
1921 if (!win) return;
1923 if (get_req_data_size())
1925 atom_t atom = add_global_atom( win->desktop->winstation,
1926 get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1927 if (atom)
1929 set_property( win, atom, req->handle, PROP_TYPE_STRING );
1930 release_global_atom( win->desktop->winstation, atom );
1933 else set_property( win, req->atom, req->handle, PROP_TYPE_ATOM );
1937 /* remove a window property */
1938 DECL_HANDLER(remove_window_property)
1940 struct window *win = get_window( req->window );
1942 if (win)
1944 atom_t atom = req->atom;
1945 if (get_req_data_size()) atom = find_global_atom( win->desktop->winstation, get_req_data(),
1946 get_req_data_size() / sizeof(WCHAR) );
1947 if (atom) reply->handle = remove_property( win, atom );
1952 /* get a window property */
1953 DECL_HANDLER(get_window_property)
1955 struct window *win = get_window( req->window );
1957 if (win)
1959 atom_t atom = req->atom;
1960 if (get_req_data_size()) atom = find_global_atom( win->desktop->winstation, get_req_data(),
1961 get_req_data_size() / sizeof(WCHAR) );
1962 if (atom) reply->handle = get_property( win, atom );
1967 /* get the list of properties of a window */
1968 DECL_HANDLER(get_window_properties)
1970 property_data_t *data;
1971 int i, count, max = get_reply_max_size() / sizeof(*data);
1972 struct window *win = get_window( req->window );
1974 reply->total = 0;
1975 if (!win) return;
1977 for (i = count = 0; i < win->prop_inuse; i++)
1978 if (win->properties[i].type != PROP_TYPE_FREE) count++;
1979 reply->total = count;
1981 if (count > max) count = max;
1982 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
1984 for (i = 0; i < win->prop_inuse && count; i++)
1986 if (win->properties[i].type == PROP_TYPE_FREE) continue;
1987 data->atom = win->properties[i].atom;
1988 data->string = (win->properties[i].type == PROP_TYPE_STRING);
1989 data->handle = win->properties[i].handle;
1990 data++;
1991 count--;
1996 /* get the new window pointer for a global window, checking permissions */
1997 /* helper for set_global_windows request */
1998 static int get_new_global_window( struct window **win, user_handle_t handle )
2000 if (!handle)
2002 *win = NULL;
2003 return 1;
2005 else if (*win)
2007 set_error( STATUS_ACCESS_DENIED );
2008 return 0;
2010 *win = get_window( handle );
2011 return (*win != NULL);
2014 /* Set/get the global windows */
2015 DECL_HANDLER(set_global_windows)
2017 struct window *new_shell_window = shell_window;
2018 struct window *new_shell_listview = shell_listview;
2019 struct window *new_progman_window = progman_window;
2020 struct window *new_taskman_window = taskman_window;
2022 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2023 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2024 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2025 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2027 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2029 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2030 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2032 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2034 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2036 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2038 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2040 shell_window = new_shell_window;
2041 shell_listview = new_shell_listview;
2042 progman_window = new_progman_window;
2043 taskman_window = new_taskman_window;