Sync msvcmaker with make_ctests to generate valid code.
[wine.git] / server / window.c
blob20775f2745465de481d7d8431c90f1d718b20ea4
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 "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
32 #include "object.h"
33 #include "request.h"
34 #include "thread.h"
35 #include "process.h"
36 #include "user.h"
37 #include "unicode.h"
39 /* a window property */
40 struct property
42 unsigned short type; /* property type (see below) */
43 atom_t atom; /* property atom */
44 obj_handle_t handle; /* property handle (user-defined storage) */
47 enum property_type
49 PROP_TYPE_FREE, /* free entry */
50 PROP_TYPE_STRING, /* atom that was originally a string */
51 PROP_TYPE_ATOM /* plain atom */
55 struct window
57 struct window *parent; /* parent window */
58 user_handle_t owner; /* owner of this window */
59 struct list children; /* list of children in Z-order */
60 struct list unlinked; /* list of children not linked in the Z-order list */
61 struct list entry; /* entry in parent's children list */
62 user_handle_t handle; /* full handle for this window */
63 struct thread *thread; /* thread owning the window */
64 struct window_class *class; /* window class */
65 atom_t atom; /* class atom */
66 user_handle_t last_active; /* last active popup */
67 rectangle_t window_rect; /* window rectangle (relative to parent client area) */
68 rectangle_t visible_rect; /* visible part of window rect (relative to parent client area) */
69 rectangle_t client_rect; /* client rectangle (relative to parent client area) */
70 struct region *win_region; /* region for shaped windows (relative to window rect) */
71 struct region *update_region; /* update region (relative to window rect) */
72 unsigned int style; /* window style */
73 unsigned int ex_style; /* window extended style */
74 unsigned int id; /* window id */
75 void* instance; /* creator instance */
76 void* user_data; /* user-specific data */
77 WCHAR *text; /* window caption text */
78 unsigned int paint_flags; /* various painting flags */
79 int prop_inuse; /* number of in-use window properties */
80 int prop_alloc; /* number of allocated window properties */
81 struct property *properties; /* window properties array */
82 int nb_extra_bytes; /* number of extra bytes */
83 char extra_bytes[1]; /* extra bytes storage */
86 #define PAINT_INTERNAL 0x01 /* internal WM_PAINT pending */
87 #define PAINT_ERASE 0x02 /* needs WM_ERASEBKGND */
88 #define PAINT_NONCLIENT 0x04 /* needs WM_NCPAINT */
90 /* growable array of user handles */
91 struct user_handle_array
93 user_handle_t *handles;
94 int count;
95 int total;
98 static struct window *top_window; /* top-level (desktop) window */
100 /* global window pointers */
101 static struct window *shell_window;
102 static struct window *shell_listview;
103 static struct window *progman_window;
104 static struct window *taskman_window;
106 /* retrieve a pointer to a window from its handle */
107 inline static struct window *get_window( user_handle_t handle )
109 struct window *ret = get_user_object( handle, USER_WINDOW );
110 if (!ret) set_error( STATUS_INVALID_HANDLE );
111 return ret;
114 /* change the parent of a window (or unlink the window if the new parent is NULL) */
115 static int set_parent_window( struct window *win, struct window *parent )
117 struct window *ptr;
119 /* make sure parent is not a child of window */
120 for (ptr = parent; ptr; ptr = ptr->parent)
122 if (ptr == win)
124 set_error( STATUS_INVALID_PARAMETER );
125 return 0;
129 list_remove( &win->entry ); /* unlink it from the previous location */
131 if (parent)
133 win->parent = parent;
134 list_add_head( &parent->children, &win->entry );
136 /* if parent belongs to a different thread, attach the two threads */
137 if (parent->thread && parent->thread != win->thread)
138 attach_thread_input( win->thread, parent->thread );
140 else /* move it to parent unlinked list */
142 list_add_head( &win->parent->unlinked, &win->entry );
144 return 1;
147 /* get next window in Z-order list */
148 static inline struct window *get_next_window( struct window *win )
150 struct list *ptr = list_next( &win->parent->children, &win->entry );
151 if (ptr == &win->parent->unlinked) ptr = NULL;
152 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
155 /* get previous window in Z-order list */
156 static inline struct window *get_prev_window( struct window *win )
158 struct list *ptr = list_prev( &win->parent->children, &win->entry );
159 if (ptr == &win->parent->unlinked) ptr = NULL;
160 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
163 /* get first child in Z-order list */
164 static inline struct window *get_first_child( struct window *win )
166 struct list *ptr = list_head( &win->children );
167 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
170 /* get last child in Z-order list */
171 static inline struct window *get_last_child( struct window *win )
173 struct list *ptr = list_tail( &win->children );
174 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
177 /* append a user handle to a handle array */
178 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
180 if (array->count >= array->total)
182 int new_total = max( array->total * 2, 32 );
183 user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
184 if (!new_array)
186 free( array->handles );
187 set_error( STATUS_NO_MEMORY );
188 return 0;
190 array->handles = new_array;
191 array->total = new_total;
193 array->handles[array->count++] = handle;
194 return 1;
197 /* set a window property */
198 static void set_property( struct window *win, atom_t atom, obj_handle_t handle,
199 enum property_type type )
201 int i, free = -1;
202 struct property *new_props;
204 /* check if it exists already */
205 for (i = 0; i < win->prop_inuse; i++)
207 if (win->properties[i].type == PROP_TYPE_FREE)
209 free = i;
210 continue;
212 if (win->properties[i].atom == atom)
214 win->properties[i].type = type;
215 win->properties[i].handle = handle;
216 return;
220 /* need to add an entry */
221 if (!grab_global_atom( atom )) return;
222 if (free == -1)
224 /* no free entry */
225 if (win->prop_inuse >= win->prop_alloc)
227 /* need to grow the array */
228 if (!(new_props = realloc( win->properties,
229 sizeof(*new_props) * (win->prop_alloc + 16) )))
231 set_error( STATUS_NO_MEMORY );
232 release_global_atom( atom );
233 return;
235 win->prop_alloc += 16;
236 win->properties = new_props;
238 free = win->prop_inuse++;
240 win->properties[free].atom = atom;
241 win->properties[free].type = type;
242 win->properties[free].handle = handle;
245 /* remove a window property */
246 static obj_handle_t remove_property( struct window *win, atom_t atom )
248 int i;
250 for (i = 0; i < win->prop_inuse; i++)
252 if (win->properties[i].type == PROP_TYPE_FREE) continue;
253 if (win->properties[i].atom == atom)
255 release_global_atom( atom );
256 win->properties[i].type = PROP_TYPE_FREE;
257 return win->properties[i].handle;
260 /* FIXME: last error? */
261 return 0;
264 /* find a window property */
265 static obj_handle_t get_property( struct window *win, atom_t atom )
267 int i;
269 for (i = 0; i < win->prop_inuse; i++)
271 if (win->properties[i].type == PROP_TYPE_FREE) continue;
272 if (win->properties[i].atom == atom) return win->properties[i].handle;
274 /* FIXME: last error? */
275 return 0;
278 /* destroy all properties of a window */
279 inline static void destroy_properties( struct window *win )
281 int i;
283 if (!win->properties) return;
284 for (i = 0; i < win->prop_inuse; i++)
286 if (win->properties[i].type == PROP_TYPE_FREE) continue;
287 release_global_atom( win->properties[i].atom );
289 free( win->properties );
292 /* destroy a window */
293 static void destroy_window( struct window *win )
295 assert( win != top_window );
297 /* destroy all children */
298 while (!list_empty(&win->children))
299 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
300 while (!list_empty(&win->unlinked))
301 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
303 if (win->thread->queue)
305 if (win->update_region) inc_queue_paint_count( win->thread, -1 );
306 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( win->thread, -1 );
307 queue_cleanup_window( win->thread, win->handle );
309 /* reset global window pointers, if the corresponding window is destroyed */
310 if (win == shell_window) shell_window = NULL;
311 if (win == shell_listview) shell_listview = NULL;
312 if (win == progman_window) progman_window = NULL;
313 if (win == taskman_window) taskman_window = NULL;
314 free_user_handle( win->handle );
315 destroy_properties( win );
316 list_remove( &win->entry );
317 if (win->win_region) free_region( win->win_region );
318 if (win->update_region) free_region( win->update_region );
319 release_class( win->class );
320 if (win->text) free( win->text );
321 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
322 free( win );
325 /* create a new window structure (note: the window is not linked in the window tree) */
326 static struct window *create_window( struct window *parent, struct window *owner,
327 atom_t atom, void *instance )
329 int extra_bytes;
330 struct window *win;
331 struct window_class *class = grab_class( current->process, atom, instance, &extra_bytes );
333 if (!class) return NULL;
335 win = mem_alloc( sizeof(*win) + extra_bytes - 1 );
336 if (!win)
338 release_class( class );
339 return NULL;
341 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
343 win->parent = parent;
344 win->owner = owner ? owner->handle : 0;
345 win->thread = current;
346 win->class = class;
347 win->atom = atom;
348 win->last_active = win->handle;
349 win->win_region = NULL;
350 win->update_region = NULL;
351 win->style = 0;
352 win->ex_style = 0;
353 win->id = 0;
354 win->instance = NULL;
355 win->user_data = NULL;
356 win->text = NULL;
357 win->paint_flags = 0;
358 win->prop_inuse = 0;
359 win->prop_alloc = 0;
360 win->properties = NULL;
361 win->nb_extra_bytes = extra_bytes;
362 memset( win->extra_bytes, 0, extra_bytes );
363 list_init( &win->children );
364 list_init( &win->unlinked );
366 /* if parent belongs to a different thread, attach the two threads */
367 if (parent && parent->thread && parent->thread != current)
369 if (!attach_thread_input( current, parent->thread )) goto failed;
371 else /* otherwise just make sure that the thread has a message queue */
373 if (!current->queue && !init_thread_queue( current )) goto failed;
376 /* put it on parent unlinked list */
377 if (parent) list_add_head( &parent->unlinked, &win->entry );
379 return win;
381 failed:
382 if (win->handle) free_user_handle( win->handle );
383 release_class( class );
384 free( win );
385 return NULL;
388 /* destroy all windows belonging to a given thread */
389 void destroy_thread_windows( struct thread *thread )
391 user_handle_t handle = 0;
392 struct window *win;
394 while ((win = next_user_handle( &handle, USER_WINDOW )))
396 if (win->thread != thread) continue;
397 destroy_window( win );
401 /* check whether child is a descendant of parent */
402 int is_child_window( user_handle_t parent, user_handle_t child )
404 struct window *child_ptr = get_user_object( child, USER_WINDOW );
405 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
407 if (!child_ptr || !parent_ptr) return 0;
408 while (child_ptr->parent)
410 if (child_ptr->parent == parent_ptr) return 1;
411 child_ptr = child_ptr->parent;
413 return 0;
416 /* check whether window is a top-level window */
417 int is_top_level_window( user_handle_t window )
419 struct window *win = get_user_object( window, USER_WINDOW );
420 return (win && win->parent == top_window);
423 /* make a window active if possible */
424 int make_window_active( user_handle_t window )
426 struct window *owner, *win = get_window( window );
428 if (!win) return 0;
430 /* set last active for window and its owner */
431 win->last_active = win->handle;
432 if ((owner = get_user_object( win->owner, USER_WINDOW ))) owner->last_active = win->handle;
433 return 1;
436 /* increment (or decrement) the window paint count */
437 static inline void inc_window_paint_count( struct window *win, int incr )
439 if (win->thread) inc_queue_paint_count( win->thread, incr );
442 /* check if window and all its ancestors are visible */
443 static int is_visible( const struct window *win )
445 while (win && win != top_window)
447 if (!(win->style & WS_VISIBLE)) return 0;
448 win = win->parent;
449 /* if parent is minimized children are not visible */
450 if (win && (win->style & WS_MINIMIZE)) return 0;
452 return 1;
455 /* same as is_visible but takes a window handle */
456 int is_window_visible( user_handle_t window )
458 struct window *win = get_user_object( window, USER_WINDOW );
459 if (!win) return 0;
460 return is_visible( win );
463 /* check if point is inside the window */
464 static inline int is_point_in_window( struct window *win, int x, int y )
466 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
467 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
468 return 0; /* disabled child */
469 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
470 return 0; /* transparent */
471 if (x < win->visible_rect.left || x >= win->visible_rect.right ||
472 y < win->visible_rect.top || y >= win->visible_rect.bottom)
473 return 0; /* not in window */
474 if (win->win_region &&
475 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
476 return 0; /* not in window region */
477 return 1;
480 /* find child of 'parent' that contains the given point (in parent-relative coords) */
481 static struct window *child_window_from_point( struct window *parent, int x, int y )
483 struct window *ptr;
485 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
487 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
489 /* if window is minimized or disabled, return at once */
490 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
492 /* if point is not in client area, return at once */
493 if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
494 y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
495 return ptr;
497 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
499 return parent; /* not found any child */
502 /* find all children of 'parent' that contain the given point */
503 static int get_window_children_from_point( struct window *parent, int x, int y,
504 struct user_handle_array *array )
506 struct window *ptr;
508 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
510 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
512 /* if point is in client area, and window is not minimized or disabled, check children */
513 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
514 x >= ptr->client_rect.left && x < ptr->client_rect.right &&
515 y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
517 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
518 y - ptr->client_rect.top, array ))
519 return 0;
522 /* now add window to the array */
523 if (!add_handle_to_array( array, ptr->handle )) return 0;
525 return 1;
528 /* find window containing point (in absolute coords) */
529 user_handle_t window_from_point( int x, int y )
531 struct window *ret;
533 if (!top_window) return 0;
534 ret = child_window_from_point( top_window, x, y );
535 return ret->handle;
538 /* return list of all windows containing point (in absolute coords) */
539 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
541 struct window *ptr;
543 /* make point relative to top window */
544 for (ptr = top->parent; ptr && ptr != top_window; ptr = ptr->parent)
546 x -= ptr->client_rect.left;
547 y -= ptr->client_rect.top;
550 if (!is_point_in_window( top, x, y )) return 1;
552 /* if point is in client area, and window is not minimized or disabled, check children */
553 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
554 x >= top->client_rect.left && x < top->client_rect.right &&
555 y >= top->client_rect.top && y < top->client_rect.bottom)
557 if (!get_window_children_from_point( top, x - top->client_rect.left,
558 y - top->client_rect.top, array ))
559 return 0;
561 /* now add window to the array */
562 if (!add_handle_to_array( array, top->handle )) return 0;
563 return 1;
567 /* return the thread owning a window */
568 struct thread *get_window_thread( user_handle_t handle )
570 struct window *win = get_user_object( handle, USER_WINDOW );
571 if (!win || !win->thread) return NULL;
572 return (struct thread *)grab_object( win->thread );
576 /* check if any area of a window needs repainting */
577 static inline int win_needs_repaint( struct window *win )
579 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
583 /* find a child of the specified window that needs repainting */
584 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
586 struct window *ptr, *ret = NULL;
588 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
590 if (!(ptr->style & WS_VISIBLE)) continue;
591 if (ptr->thread == thread && win_needs_repaint( ptr ))
592 ret = ptr;
593 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
594 ret = find_child_to_repaint( ptr, thread );
595 if (ret) break;
598 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
600 /* transparent window, check for non-transparent sibling to paint first */
601 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
603 if (!(ptr->style & WS_VISIBLE)) continue;
604 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
605 if (ptr->thread != thread) continue;
606 if (win_needs_repaint( ptr )) return ptr;
609 return ret;
613 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
614 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
616 struct window *ptr, *win = find_child_to_repaint( top_window, thread );
618 if (win && parent)
620 /* check that it is a child of the specified parent */
621 for (ptr = win; ptr; ptr = ptr->parent)
622 if (ptr->handle == parent) break;
623 /* otherwise don't return any window, we don't repaint a child before its parent */
624 if (!ptr) win = NULL;
626 if (!win) return 0;
627 win->paint_flags &= ~PAINT_INTERNAL;
628 return win->handle;
632 /* intersect the window region with the specified region, relative to the window parent */
633 static struct region *intersect_window_region( struct region *region, struct window *win )
635 /* make region relative to window rect */
636 offset_region( region, -win->window_rect.left, -win->window_rect.top );
637 if (!intersect_region( region, region, win->win_region )) return NULL;
638 /* make region relative to parent again */
639 offset_region( region, win->window_rect.left, win->window_rect.top );
640 return region;
644 /* convert coordinates from client to screen coords */
645 static inline void client_to_screen( struct window *win, int *x, int *y )
647 for ( ; win; win = win->parent)
649 *x += win->client_rect.left;
650 *y += win->client_rect.top;
654 /* map the region from window to screen coordinates */
655 static inline void map_win_region_to_screen( struct window *win, struct region *region )
657 int x = win->window_rect.left;
658 int y = win->window_rect.top;
659 client_to_screen( win->parent, &x, &y );
660 offset_region( region, x, y );
664 /* clip all children of a given window out of the visible region */
665 static struct region *clip_children( struct window *parent, struct window *last,
666 struct region *region, int offset_x, int offset_y )
668 struct window *ptr;
669 struct region *tmp = create_empty_region();
671 if (!tmp) return NULL;
672 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
674 if (ptr == last) break;
675 if (!(ptr->style & WS_VISIBLE)) continue;
676 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
677 set_region_rect( tmp, &ptr->visible_rect );
678 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
680 free_region( tmp );
681 return NULL;
683 offset_region( tmp, offset_x, offset_y );
684 if (!(region = subtract_region( region, region, tmp ))) break;
685 if (is_region_empty( region )) break;
687 free_region( tmp );
688 return region;
692 /* compute the intersection of two rectangles; return 0 if the result is empty */
693 static inline int intersect_rect( rectangle_t *dst, const rectangle_t *src1, const rectangle_t *src2 )
695 dst->left = max( src1->left, src2->left );
696 dst->top = max( src1->top, src2->top );
697 dst->right = min( src1->right, src2->right );
698 dst->bottom = min( src1->bottom, src2->bottom );
699 return (dst->left < dst->right && dst->top < dst->bottom);
703 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
704 static void set_region_client_rect( struct region *region, struct window *win )
706 rectangle_t rect;
708 intersect_rect( &rect, &win->window_rect, &win->client_rect );
709 set_region_rect( region, &rect );
713 /* get the top-level window to clip against for a given window */
714 static inline struct window *get_top_clipping_window( struct window *win )
716 while (win->parent && win->parent != top_window) win = win->parent;
717 return win;
721 /* compute the visible region of a window, in window coordinates */
722 static struct region *get_visible_region( struct window *win, struct window *top, unsigned int flags )
724 struct region *tmp = NULL, *region;
725 int offset_x, offset_y;
727 if (!(region = create_empty_region())) return NULL;
729 /* first check if all ancestors are visible */
731 if (!is_visible( win )) return region; /* empty region */
733 /* create a region relative to the window itself */
735 if ((flags & DCX_PARENTCLIP) && win != top && win->parent)
737 set_region_client_rect( region, win->parent );
738 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
740 else if (flags & DCX_WINDOW)
742 set_region_rect( region, &win->visible_rect );
743 if (win->win_region && !intersect_window_region( region, win )) goto error;
745 else
747 set_region_client_rect( region, win );
748 if (win->win_region && !intersect_window_region( region, win )) goto error;
750 offset_x = win->window_rect.left;
751 offset_y = win->window_rect.top;
753 /* clip children */
755 if (flags & DCX_CLIPCHILDREN)
757 if (!clip_children( win, NULL, region, win->client_rect.left, win->client_rect.top ))
758 goto error;
761 /* clip siblings of ancestors */
763 if (top && top != win && (tmp = create_empty_region()) != NULL)
765 while (win != top && win->parent)
767 if (win->style & WS_CLIPSIBLINGS)
769 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
770 if (is_region_empty( region )) break;
772 /* clip to parent client area */
773 win = win->parent;
774 offset_x += win->client_rect.left;
775 offset_y += win->client_rect.top;
776 offset_region( region, win->client_rect.left, win->client_rect.top );
777 set_region_client_rect( tmp, win );
778 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
779 if (!intersect_region( region, region, tmp )) goto error;
780 if (is_region_empty( region )) break;
782 free_region( tmp );
784 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
785 return region;
787 error:
788 if (tmp) free_region( tmp );
789 free_region( region );
790 return NULL;
794 /* get the window class of a window */
795 struct window_class* get_window_class( user_handle_t window )
797 struct window *win;
798 if (!(win = get_window( window ))) return NULL;
799 return win->class;
802 /* return a copy of the specified region cropped to the window client or frame rectangle, */
803 /* and converted from client to window coordinates. Helper for (in)validate_window. */
804 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
806 struct region *tmp = create_empty_region();
808 if (!tmp) return NULL;
810 /* get bounding rect in client coords */
811 if (frame) set_region_rect( tmp, &win->window_rect );
812 else set_region_client_rect( tmp, win );
813 offset_region( tmp, -win->client_rect.left, -win->client_rect.top );
815 /* intersect specified region with bounding rect */
816 if (region && !intersect_region( tmp, region, tmp )) goto done;
817 if (is_region_empty( tmp )) goto done;
819 /* map it to window coords */
820 offset_region( tmp, win->client_rect.left - win->window_rect.left,
821 win->client_rect.top - win->window_rect.top );
822 return tmp;
824 done:
825 free_region( tmp );
826 return NULL;
830 /* set a region as new update region for the window */
831 static void set_update_region( struct window *win, struct region *region )
833 if (region && !is_region_empty( region ))
835 if (!win->update_region) inc_window_paint_count( win, 1 );
836 else free_region( win->update_region );
837 win->update_region = region;
839 else
841 if (win->update_region)
843 inc_window_paint_count( win, -1 );
844 free_region( win->update_region );
846 win->paint_flags &= ~(PAINT_ERASE | PAINT_NONCLIENT);
847 win->update_region = NULL;
848 if (region) free_region( region );
853 /* add a region to the update region; the passed region is freed or reused */
854 static int add_update_region( struct window *win, struct region *region )
856 if (win->update_region && !union_region( region, win->update_region, region ))
858 free_region( region );
859 return 0;
861 set_update_region( win, region );
862 return 1;
866 /* validate the non client area of a window */
867 static void validate_non_client( struct window *win )
869 struct region *tmp;
870 rectangle_t rect;
872 if (!win->update_region) return; /* nothing to do */
874 /* get client rect in window coords */
875 rect.left = win->client_rect.left - win->window_rect.left;
876 rect.top = win->client_rect.top - win->window_rect.top;
877 rect.right = win->client_rect.right - win->window_rect.left;
878 rect.bottom = win->client_rect.bottom - win->window_rect.top;
880 if ((tmp = create_empty_region()))
882 set_region_rect( tmp, &rect );
883 if (intersect_region( tmp, win->update_region, tmp ))
884 set_update_region( win, tmp );
885 else
886 free_region( tmp );
888 win->paint_flags &= ~PAINT_NONCLIENT;
892 /* validate a window completely so that we don't get any further paint messages for it */
893 static void validate_whole_window( struct window *win )
895 set_update_region( win, NULL );
897 if (win->paint_flags & PAINT_INTERNAL)
899 win->paint_flags &= ~PAINT_INTERNAL;
900 inc_window_paint_count( win, -1 );
905 /* validate the update region of a window on all parents; helper for redraw_window */
906 static void validate_parents( struct window *child )
908 int offset_x = 0, offset_y = 0;
909 struct window *win = child;
910 struct region *tmp = NULL;
912 if (!child->update_region) return;
914 while (win->parent && win->parent != top_window)
916 /* map to parent client coords */
917 offset_x += win->window_rect.left;
918 offset_y += win->window_rect.top;
920 win = win->parent;
922 /* and now map to window coords */
923 offset_x += win->client_rect.left - win->window_rect.left;
924 offset_y += win->client_rect.top - win->window_rect.top;
926 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
928 if (!tmp && !(tmp = create_empty_region())) return;
929 offset_region( child->update_region, offset_x, offset_y );
930 if (subtract_region( tmp, win->update_region, child->update_region ))
932 set_update_region( win, tmp );
933 tmp = NULL;
935 /* restore child coords */
936 offset_region( child->update_region, -offset_x, -offset_y );
939 if (tmp) free_region( tmp );
943 /* add/subtract a region (in client coordinates) to the update region of the window */
944 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
946 struct region *tmp;
947 struct window *child;
949 if (flags & RDW_INVALIDATE)
951 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
953 if (!add_update_region( win, tmp )) return;
955 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
956 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
958 else if (flags & RDW_VALIDATE)
960 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
962 set_update_region( win, NULL );
964 else if (win->update_region)
966 if ((tmp = crop_region_to_win_rect( win, region, frame )))
968 if (!subtract_region( tmp, win->update_region, tmp ))
970 free_region( tmp );
971 return;
973 set_update_region( win, tmp );
975 if (flags & RDW_NOFRAME) validate_non_client( win );
976 if (flags & RDW_NOERASE) win->paint_flags &= ~PAINT_ERASE;
980 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
982 win->paint_flags |= PAINT_INTERNAL;
983 inc_window_paint_count( win, 1 );
985 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
987 win->paint_flags &= ~PAINT_INTERNAL;
988 inc_window_paint_count( win, -1 );
991 if (flags & RDW_UPDATENOW)
993 validate_parents( win );
994 flags &= ~RDW_UPDATENOW;
997 /* now process children recursively */
999 if (flags & RDW_NOCHILDREN) return;
1000 if (win->style & WS_MINIMIZE) return;
1001 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1003 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1005 /* map to client coordinates */
1006 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1007 win->window_rect.top - win->client_rect.top );
1009 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1011 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1013 if (!(child->style & WS_VISIBLE)) continue;
1014 if (!rect_in_region( tmp, &child->window_rect )) continue;
1015 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1016 redraw_window( child, tmp, 1, flags );
1017 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1019 free_region( tmp );
1023 /* retrieve the update flags for a window depending on the state of the update region */
1024 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1026 unsigned int ret = 0;
1028 if (flags & UPDATE_NONCLIENT)
1030 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1032 if (flags & UPDATE_ERASE)
1034 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1036 if (flags & UPDATE_PAINT)
1038 if (win->update_region) ret |= UPDATE_PAINT;
1040 if (flags & UPDATE_INTERNALPAINT)
1042 if (win->paint_flags & PAINT_INTERNAL) ret |= UPDATE_INTERNALPAINT;
1044 return ret;
1048 /* iterate through the children of the given window until we find one with some update flags */
1049 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1050 unsigned int flags, struct window **child )
1052 struct window *ptr;
1053 unsigned int ret = 0;
1055 /* first make sure we want to iterate children at all */
1057 if (win->style & WS_MINIMIZE) return 0;
1059 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1060 * here we only want to repaint children of windows that clip them, others
1061 * need to wait for WM_PAINT to be done in the parent first.
1063 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1065 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1067 if (from_child) /* skip all children until from_child is found */
1069 if (ptr == from_child) from_child = NULL;
1070 continue;
1072 if (!(ptr->style & WS_VISIBLE)) continue;
1073 if ((ret = get_update_flags( ptr, flags )) != 0)
1075 *child = ptr;
1076 break;
1078 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1080 return ret;
1083 /* iterate through children and siblings of the given window until we find one with some update flags */
1084 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1085 unsigned int flags, struct window **child )
1087 unsigned int ret;
1088 struct window *ptr, *from_sibling = NULL;
1090 /* if some parent is not visible start from the next sibling */
1092 if (!is_visible( win )) return 0;
1093 for (ptr = from_child; ptr && ptr != top_window; ptr = ptr->parent)
1095 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1096 if (ptr == win) break;
1099 /* non-client painting must be delayed if one of the parents is going to
1100 * be repainted and doesn't clip children */
1102 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1104 for (ptr = win->parent; ptr && ptr != top_window; ptr = ptr->parent)
1106 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1107 return 0;
1109 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1111 for (ptr = from_sibling ? from_sibling : from_child;
1112 ptr && ptr != top_window; ptr = ptr->parent)
1114 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1115 if (ptr == win) break;
1121 /* check window itself (only if not restarting from a child) */
1123 if (!from_child)
1125 if ((ret = get_update_flags( win, flags )))
1127 *child = win;
1128 return ret;
1130 from_child = win;
1133 /* now check children */
1135 if (flags & UPDATE_NOCHILDREN) return 0;
1136 if (!from_sibling)
1138 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1139 from_sibling = from_child;
1142 /* then check siblings and parent siblings */
1144 while (from_sibling->parent && from_sibling != win)
1146 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1147 return ret;
1148 from_sibling = from_sibling->parent;
1150 return 0;
1154 /* expose a region of a window, looking for the top most parent that needs to be exposed */
1155 /* the region is in window coordinates */
1156 static void expose_window( struct window *win, struct window *top, struct region *region )
1158 struct window *parent, *ptr;
1159 int offset_x, offset_y;
1161 /* find the top most parent that doesn't clip either siblings or children */
1162 for (parent = ptr = win; ptr != top; ptr = ptr->parent)
1164 if (!(ptr->style & WS_CLIPCHILDREN)) parent = ptr;
1165 if (!(ptr->style & WS_CLIPSIBLINGS)) parent = ptr->parent;
1167 if (parent == win && parent != top && win->parent)
1168 parent = win->parent; /* always go up at least one level if possible */
1170 offset_x = win->window_rect.left - win->client_rect.left;
1171 offset_y = win->window_rect.top - win->client_rect.top;
1172 for (ptr = win; ptr != parent; ptr = ptr->parent)
1174 offset_x += ptr->client_rect.left;
1175 offset_y += ptr->client_rect.top;
1177 offset_region( region, offset_x, offset_y );
1178 redraw_window( parent, region, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1179 offset_region( region, -offset_x, -offset_y );
1183 /* set the window and client rectangles, updating the update region if necessary */
1184 static void set_window_pos( struct window *win, struct window *previous,
1185 unsigned int swp_flags, const rectangle_t *window_rect,
1186 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1187 const rectangle_t *valid_rects )
1189 struct region *old_vis_rgn = NULL, *new_vis_rgn;
1190 const rectangle_t old_window_rect = win->window_rect;
1191 const rectangle_t old_visible_rect = win->visible_rect;
1192 const rectangle_t old_client_rect = win->client_rect;
1193 struct window *top = get_top_clipping_window( win );
1194 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1196 if (win->parent && !is_visible( win->parent )) visible = 0;
1198 if (visible && !(old_vis_rgn = get_visible_region( win, top, DCX_WINDOW ))) return;
1200 /* set the new window info before invalidating anything */
1202 win->window_rect = *window_rect;
1203 win->visible_rect = *visible_rect;
1204 win->client_rect = *client_rect;
1205 if (!(swp_flags & SWP_NOZORDER) && win->parent)
1207 list_remove( &win->entry ); /* unlink it from the previous location */
1208 if (previous) list_add_after( &previous->entry, &win->entry );
1209 else list_add_head( &win->parent->children, &win->entry );
1211 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1212 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1214 /* if the window is not visible, everything is easy */
1215 if (!visible) return;
1217 if (!(new_vis_rgn = get_visible_region( win, top, DCX_WINDOW )))
1219 free_region( old_vis_rgn );
1220 clear_error(); /* ignore error since the window info has been modified already */
1221 return;
1224 /* expose anything revealed by the change */
1226 if (!(swp_flags & SWP_NOREDRAW))
1228 offset_region( old_vis_rgn, old_window_rect.left - window_rect->left,
1229 old_window_rect.top - window_rect->top );
1230 if (xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1231 expose_window( win, top, new_vis_rgn );
1233 free_region( old_vis_rgn );
1235 if (!(win->style & WS_VISIBLE))
1237 /* clear the update region since the window is no longer visible */
1238 validate_whole_window( win );
1239 goto done;
1242 /* crop update region to the new window rect */
1244 if (win->update_region &&
1245 (window_rect->right - window_rect->left < old_window_rect.right - old_window_rect.left ||
1246 window_rect->bottom - window_rect->top < old_window_rect.bottom - old_window_rect.top))
1248 struct region *tmp = create_empty_region();
1249 if (tmp)
1251 set_region_rect( tmp, window_rect );
1252 offset_region( tmp, -window_rect->left, -window_rect->top );
1253 if (intersect_region( tmp, win->update_region, tmp ))
1254 set_update_region( win, tmp );
1255 else
1256 free_region( tmp );
1260 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1262 /* expose the whole non-client area if it changed in any way */
1264 if ((swp_flags & SWP_FRAMECHANGED) ||
1265 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1266 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ) ||
1267 memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1269 struct region *tmp = create_empty_region();
1271 if (tmp)
1273 /* subtract the valid portion of client rect from the total region */
1274 if (!memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1275 set_region_rect( tmp, client_rect );
1276 else if (valid_rects)
1277 set_region_rect( tmp, &valid_rects[0] );
1279 set_region_rect( new_vis_rgn, window_rect );
1280 if (subtract_region( tmp, new_vis_rgn, tmp ))
1282 offset_region( tmp, -client_rect->left, -client_rect->top );
1283 redraw_window( win, tmp, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1285 free_region( tmp );
1289 done:
1290 free_region( new_vis_rgn );
1291 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1295 /* create a window */
1296 DECL_HANDLER(create_window)
1298 struct window *win;
1300 reply->handle = 0;
1301 if (!req->parent) /* return desktop window */
1303 if (!top_window)
1305 if (!(top_window = create_window( NULL, NULL, req->atom, req->instance ))) return;
1306 top_window->thread = NULL; /* no thread owns the desktop */
1307 top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
1309 win = top_window;
1311 else
1313 struct window *parent, *owner = NULL;
1315 if (!(parent = get_window( req->parent ))) return;
1316 if (req->owner && !(owner = get_window( req->owner ))) return;
1317 if (owner == top_window) owner = NULL;
1318 else if (owner && parent != top_window)
1320 /* an owned window must be created as top-level */
1321 set_error( STATUS_ACCESS_DENIED );
1322 return;
1324 if (!(win = create_window( parent, owner, req->atom, req->instance ))) return;
1326 reply->handle = win->handle;
1327 reply->extra = win->nb_extra_bytes;
1328 reply->class_ptr = get_class_client_ptr( win->class );
1332 /* set the parent of a window */
1333 DECL_HANDLER(set_parent)
1335 struct window *win, *parent = NULL;
1337 if (!(win = get_window( req->handle ))) return;
1338 if (req->parent && !(parent = get_window( req->parent ))) return;
1340 if (win == top_window)
1342 set_error( STATUS_INVALID_PARAMETER );
1343 return;
1345 reply->old_parent = win->parent->handle;
1346 reply->full_parent = parent ? parent->handle : 0;
1347 set_parent_window( win, parent );
1351 /* destroy a window */
1352 DECL_HANDLER(destroy_window)
1354 struct window *win = get_window( req->handle );
1355 if (win)
1357 if (win != top_window) destroy_window( win );
1358 else set_error( STATUS_ACCESS_DENIED );
1363 /* set a window owner */
1364 DECL_HANDLER(set_window_owner)
1366 struct window *win = get_window( req->handle );
1367 struct window *owner = NULL;
1369 if (!win) return;
1370 if (req->owner && !(owner = get_window( req->owner ))) return;
1371 if (win == top_window)
1373 set_error( STATUS_ACCESS_DENIED );
1374 return;
1376 reply->prev_owner = win->owner;
1377 reply->full_owner = win->owner = owner ? owner->handle : 0;
1381 /* get information from a window handle */
1382 DECL_HANDLER(get_window_info)
1384 struct window *win = get_window( req->handle );
1386 reply->full_handle = 0;
1387 reply->tid = reply->pid = 0;
1388 if (win)
1390 reply->full_handle = win->handle;
1391 reply->last_active = win->handle;
1392 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
1393 if (win->thread)
1395 reply->tid = get_thread_id( win->thread );
1396 reply->pid = get_process_id( win->thread->process );
1397 reply->atom = get_class_atom( win->class );
1403 /* set some information in a window */
1404 DECL_HANDLER(set_window_info)
1406 struct window *win = get_window( req->handle );
1408 if (!win) return;
1409 if (req->flags && win == top_window)
1411 set_error( STATUS_ACCESS_DENIED );
1412 return;
1414 if (req->extra_size > sizeof(req->extra_value) ||
1415 req->extra_offset < -1 ||
1416 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
1418 set_win32_error( ERROR_INVALID_INDEX );
1419 return;
1421 if (req->extra_offset != -1)
1423 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
1425 else if (req->flags & SET_WIN_EXTRA)
1427 set_win32_error( ERROR_INVALID_INDEX );
1428 return;
1430 reply->old_style = win->style;
1431 reply->old_ex_style = win->ex_style;
1432 reply->old_id = win->id;
1433 reply->old_instance = win->instance;
1434 reply->old_user_data = win->user_data;
1435 if (req->flags & SET_WIN_STYLE) win->style = req->style;
1436 if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
1437 if (req->flags & SET_WIN_ID) win->id = req->id;
1438 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
1439 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
1440 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
1441 &req->extra_value, req->extra_size );
1443 /* changing window style triggers a non-client paint */
1444 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
1448 /* get a list of the window parents, up to the root of the tree */
1449 DECL_HANDLER(get_window_parents)
1451 struct window *ptr, *win = get_window( req->handle );
1452 int total = 0;
1453 user_handle_t *data;
1454 size_t len;
1456 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
1458 reply->count = total;
1459 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1460 if (len && ((data = set_reply_data_size( len ))))
1462 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
1463 *data++ = ptr->handle;
1468 /* get a list of the window children */
1469 DECL_HANDLER(get_window_children)
1471 struct window *ptr, *parent = get_window( req->parent );
1472 int total = 0;
1473 user_handle_t *data;
1474 size_t len;
1476 if (parent)
1478 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
1480 if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1481 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1482 total++;
1485 reply->count = total;
1486 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1487 if (len && ((data = set_reply_data_size( len ))))
1489 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
1491 if (len < sizeof(*data)) break;
1492 if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1493 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1494 *data++ = ptr->handle;
1495 len -= sizeof(*data);
1501 /* get a list of the window children that contain a given point */
1502 DECL_HANDLER(get_window_children_from_point)
1504 struct user_handle_array array;
1505 struct window *parent = get_window( req->parent );
1506 size_t len;
1508 if (!parent) return;
1510 array.handles = NULL;
1511 array.count = 0;
1512 array.total = 0;
1513 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
1515 reply->count = array.count;
1516 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
1517 if (len) set_reply_data_ptr( array.handles, len );
1518 else free( array.handles );
1522 /* get window tree information from a window handle */
1523 DECL_HANDLER(get_window_tree)
1525 struct window *ptr, *win = get_window( req->handle );
1527 if (!win) return;
1529 reply->parent = 0;
1530 reply->owner = 0;
1531 reply->next_sibling = 0;
1532 reply->prev_sibling = 0;
1533 reply->first_sibling = 0;
1534 reply->last_sibling = 0;
1535 reply->first_child = 0;
1536 reply->last_child = 0;
1538 if (win->parent)
1540 struct window *parent = win->parent;
1541 reply->parent = parent->handle;
1542 reply->owner = win->owner;
1543 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
1544 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
1545 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
1546 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
1548 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
1549 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
1553 /* set the position and Z order of a window */
1554 DECL_HANDLER(set_window_pos)
1556 const rectangle_t *visible_rect = NULL, *valid_rects = NULL;
1557 struct window *previous = NULL;
1558 struct window *win = get_window( req->handle );
1559 unsigned int flags = req->flags;
1561 if (!win) return;
1562 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
1564 if (!(flags & SWP_NOZORDER))
1566 if (!req->previous) /* special case: HWND_TOP */
1568 if (get_first_child(win->parent) == win) flags |= SWP_NOZORDER;
1570 else if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
1572 previous = get_last_child( win->parent );
1574 else
1576 if (!(previous = get_window( req->previous ))) return;
1577 /* previous must be a sibling */
1578 if (previous->parent != win->parent)
1580 set_error( STATUS_INVALID_PARAMETER );
1581 return;
1584 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
1587 /* window rectangle must be ordered properly */
1588 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
1590 set_error( STATUS_INVALID_PARAMETER );
1591 return;
1594 if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = get_req_data();
1595 if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rects = visible_rect + 1;
1597 if (!visible_rect) visible_rect = &req->window;
1598 set_window_pos( win, previous, flags, &req->window, &req->client, visible_rect, valid_rects );
1599 reply->new_style = win->style;
1603 /* get the window and client rectangles of a window */
1604 DECL_HANDLER(get_window_rectangles)
1606 struct window *win = get_window( req->handle );
1608 if (win)
1610 reply->window = win->window_rect;
1611 reply->visible = win->visible_rect;
1612 reply->client = win->client_rect;
1617 /* get the window text */
1618 DECL_HANDLER(get_window_text)
1620 struct window *win = get_window( req->handle );
1622 if (win && win->text)
1624 size_t len = strlenW( win->text ) * sizeof(WCHAR);
1625 if (len > get_reply_max_size()) len = get_reply_max_size();
1626 set_reply_data( win->text, len );
1631 /* set the window text */
1632 DECL_HANDLER(set_window_text)
1634 struct window *win = get_window( req->handle );
1636 if (win)
1638 WCHAR *text = NULL;
1639 size_t len = get_req_data_size() / sizeof(WCHAR);
1640 if (len)
1642 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
1643 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
1644 text[len] = 0;
1646 if (win->text) free( win->text );
1647 win->text = text;
1652 /* get the coordinates offset between two windows */
1653 DECL_HANDLER(get_windows_offset)
1655 struct window *win;
1657 reply->x = reply->y = 0;
1658 if (req->from)
1660 if (!(win = get_window( req->from ))) return;
1661 while (win)
1663 reply->x += win->client_rect.left;
1664 reply->y += win->client_rect.top;
1665 win = win->parent;
1668 if (req->to)
1670 if (!(win = get_window( req->to ))) return;
1671 while (win)
1673 reply->x -= win->client_rect.left;
1674 reply->y -= win->client_rect.top;
1675 win = win->parent;
1681 /* get the visible region of a window */
1682 DECL_HANDLER(get_visible_region)
1684 struct region *region;
1685 struct window *top, *win = get_window( req->window );
1687 if (!win) return;
1689 top = get_top_clipping_window( win );
1690 if ((region = get_visible_region( win, top, req->flags )))
1692 rectangle_t *data;
1693 map_win_region_to_screen( win, region );
1694 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
1695 if (data) set_reply_data_ptr( data, reply->total_size );
1697 reply->top_win = top->handle;
1698 reply->top_org_x = top->visible_rect.left;
1699 reply->top_org_y = top->visible_rect.top;
1700 reply->win_org_x = (req->flags & DCX_WINDOW) ? win->window_rect.left : win->client_rect.left;
1701 reply->win_org_y = (req->flags & DCX_WINDOW) ? win->window_rect.top : win->client_rect.top;
1702 client_to_screen( top->parent, &reply->top_org_x, &reply->top_org_y );
1703 client_to_screen( win->parent, &reply->win_org_x, &reply->win_org_y );
1707 /* get the window region */
1708 DECL_HANDLER(get_window_region)
1710 struct window *win = get_window( req->window );
1712 if (!win) return;
1714 if (win->win_region)
1716 rectangle_t *data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
1717 if (data) set_reply_data_ptr( data, reply->total_size );
1722 /* set the window region */
1723 DECL_HANDLER(set_window_region)
1725 struct region *region = NULL;
1726 struct window *win = get_window( req->window );
1728 if (!win) return;
1730 if (get_req_data_size()) /* no data means remove the region completely */
1732 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1733 return;
1735 if (win->win_region) free_region( win->win_region );
1736 win->win_region = region;
1740 /* get a window update region */
1741 DECL_HANDLER(get_update_region)
1743 rectangle_t *data;
1744 unsigned int flags = req->flags;
1745 struct window *from_child = NULL;
1746 struct window *win = get_window( req->window );
1748 reply->flags = 0;
1749 if (!win) return;
1751 if (req->from_child)
1753 struct window *ptr;
1755 if (!(from_child = get_window( req->from_child ))) return;
1757 /* make sure from_child is a child of win */
1758 ptr = from_child;
1759 while (ptr && ptr != win) ptr = ptr->parent;
1760 if (!ptr)
1762 set_error( STATUS_INVALID_PARAMETER );
1763 return;
1767 reply->flags = get_window_update_flags( win, from_child, flags, &win );
1768 reply->child = win->handle;
1770 if (flags & UPDATE_NOREGION) return;
1772 if (win->update_region)
1774 /* convert update region to screen coordinates */
1775 struct region *region = create_empty_region();
1777 if (!region) return;
1778 if (!copy_region( region, win->update_region ))
1780 free_region( region );
1781 return;
1783 map_win_region_to_screen( win, region );
1784 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
1785 &reply->total_size ))) return;
1786 set_reply_data_ptr( data, reply->total_size );
1789 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
1791 validate_whole_window( win );
1793 else
1795 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
1796 if (reply->flags & UPDATE_ERASE)
1798 win->paint_flags &= ~PAINT_ERASE;
1799 /* desktop window only gets erased, not repainted */
1800 if (win == top_window) validate_whole_window( win );
1806 /* update the z order of a window so that a given rectangle is fully visible */
1807 DECL_HANDLER(update_window_zorder)
1809 rectangle_t tmp;
1810 struct window *ptr, *win = get_window( req->window );
1812 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
1814 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
1816 if (ptr == win) break;
1817 if (!(ptr->style & WS_VISIBLE)) continue;
1818 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1819 if (!intersect_rect( &tmp, &ptr->visible_rect, &req->rect )) continue;
1820 if (ptr->win_region && !rect_in_region( ptr->win_region, &req->rect )) continue;
1821 /* found a window obscuring the rectangle, now move win above this one */
1822 list_remove( &win->entry );
1823 list_add_before( &ptr->entry, &win->entry );
1824 break;
1829 /* mark parts of a window as needing a redraw */
1830 DECL_HANDLER(redraw_window)
1832 struct region *region = NULL;
1833 struct window *win = get_window( req->window );
1835 if (!win) return;
1836 if (!is_visible( win )) return; /* nothing to do */
1838 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
1840 if (get_req_data_size()) /* no data means whole rectangle */
1842 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1843 return;
1847 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
1848 req->flags );
1849 if (region) free_region( region );
1853 /* set a window property */
1854 DECL_HANDLER(set_window_property)
1856 struct window *win = get_window( req->window );
1858 if (!win) return;
1860 if (get_req_data_size())
1862 atom_t atom = add_global_atom( get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1863 if (atom)
1865 set_property( win, atom, req->handle, PROP_TYPE_STRING );
1866 release_global_atom( atom );
1869 else set_property( win, req->atom, req->handle, PROP_TYPE_ATOM );
1873 /* remove a window property */
1874 DECL_HANDLER(remove_window_property)
1876 struct window *win = get_window( req->window );
1877 reply->handle = 0;
1878 if (win)
1880 atom_t atom = req->atom;
1881 if (get_req_data_size()) atom = find_global_atom( get_req_data(),
1882 get_req_data_size() / sizeof(WCHAR) );
1883 if (atom) reply->handle = remove_property( win, atom );
1888 /* get a window property */
1889 DECL_HANDLER(get_window_property)
1891 struct window *win = get_window( req->window );
1892 reply->handle = 0;
1893 if (win)
1895 atom_t atom = req->atom;
1896 if (get_req_data_size()) atom = find_global_atom( get_req_data(),
1897 get_req_data_size() / sizeof(WCHAR) );
1898 if (atom) reply->handle = get_property( win, atom );
1903 /* get the list of properties of a window */
1904 DECL_HANDLER(get_window_properties)
1906 property_data_t *data;
1907 int i, count, max = get_reply_max_size() / sizeof(*data);
1908 struct window *win = get_window( req->window );
1910 reply->total = 0;
1911 if (!win) return;
1913 for (i = count = 0; i < win->prop_inuse; i++)
1914 if (win->properties[i].type != PROP_TYPE_FREE) count++;
1915 reply->total = count;
1917 if (count > max) count = max;
1918 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
1920 for (i = 0; i < win->prop_inuse && count; i++)
1922 if (win->properties[i].type == PROP_TYPE_FREE) continue;
1923 data->atom = win->properties[i].atom;
1924 data->string = (win->properties[i].type == PROP_TYPE_STRING);
1925 data->handle = win->properties[i].handle;
1926 data++;
1927 count--;
1932 /* get the new window pointer for a global window, checking permissions */
1933 /* helper for set_global_windows request */
1934 static int get_new_global_window( struct window **win, user_handle_t handle )
1936 if (!handle)
1938 *win = NULL;
1939 return 1;
1941 else if (*win)
1943 set_error( STATUS_ACCESS_DENIED );
1944 return 0;
1946 *win = get_window( handle );
1947 return (*win != NULL);
1950 /* Set/get the global windows */
1951 DECL_HANDLER(set_global_windows)
1953 struct window *new_shell_window = shell_window;
1954 struct window *new_shell_listview = shell_listview;
1955 struct window *new_progman_window = progman_window;
1956 struct window *new_taskman_window = taskman_window;
1958 reply->old_shell_window = shell_window ? shell_window->handle : 0;
1959 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
1960 reply->old_progman_window = progman_window ? progman_window->handle : 0;
1961 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
1963 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
1965 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
1966 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
1968 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
1970 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
1972 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
1974 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
1976 shell_window = new_shell_window;
1977 shell_listview = new_shell_listview;
1978 progman_window = new_progman_window;
1979 taskman_window = new_taskman_window;