usp10: Add some tests to ScriptGetFontProperties.
[wine/multimedia.git] / server / window.c
blob5ceab4972ac9b02c15b2695a7bdaec7c936c0ff2
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( NULL, 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( NULL, 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( NULL, 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( NULL, win->properties[i].atom );
297 free( win->properties );
300 /* detach a window from its owner thread but keep the window around */
301 static void detach_window_thread( struct window *win )
303 struct thread *thread = win->thread;
305 if (!thread) return;
306 if (thread->queue)
308 if (win->update_region) inc_queue_paint_count( thread, -1 );
309 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
310 queue_cleanup_window( thread, win->handle );
312 assert( thread->desktop_users > 0 );
313 thread->desktop_users--;
314 release_class( win->class );
315 win->class = NULL;
317 /* don't hold a reference to the desktop so that the desktop window can be */
318 /* destroyed when the desktop ref count reaches zero */
319 release_object( win->desktop );
320 win->thread = NULL;
323 /* destroy a window */
324 void destroy_window( struct window *win )
326 /* destroy all children */
327 while (!list_empty(&win->children))
328 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
329 while (!list_empty(&win->unlinked))
330 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
332 /* reset global window pointers, if the corresponding window is destroyed */
333 if (win == shell_window) shell_window = NULL;
334 if (win == shell_listview) shell_listview = NULL;
335 if (win == progman_window) progman_window = NULL;
336 if (win == taskman_window) taskman_window = NULL;
337 free_user_handle( win->handle );
338 destroy_properties( win );
339 list_remove( &win->entry );
340 if (is_desktop_window(win))
342 assert( win->desktop->top_window == win );
343 win->desktop->top_window = NULL;
345 detach_window_thread( win );
346 if (win->win_region) free_region( win->win_region );
347 if (win->update_region) free_region( win->update_region );
348 if (win->class) release_class( win->class );
349 if (win->text) free( win->text );
350 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
351 free( win );
354 /* get the process owning the top window of a given desktop */
355 struct process *get_top_window_owner( struct desktop *desktop )
357 struct window *win = desktop->top_window;
358 if (!win || !win->thread) return NULL;
359 return win->thread->process;
362 /* attempt to close the desktop window when the last process using it is gone */
363 void close_desktop_window( struct desktop *desktop )
365 struct window *win = desktop->top_window;
366 if (win && win->thread) post_message( win->handle, WM_CLOSE, 0, 0 );
369 /* create a new window structure (note: the window is not linked in the window tree) */
370 static struct window *create_window( struct window *parent, struct window *owner,
371 atom_t atom, void *instance )
373 int extra_bytes;
374 struct window *win;
375 struct desktop *desktop;
376 struct window_class *class;
378 if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
380 if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
382 release_object( desktop );
383 return NULL;
386 if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
387 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
389 win->parent = parent;
390 win->owner = owner ? owner->handle : 0;
391 win->thread = current;
392 win->desktop = desktop;
393 win->class = class;
394 win->atom = atom;
395 win->last_active = win->handle;
396 win->win_region = NULL;
397 win->update_region = NULL;
398 win->style = 0;
399 win->ex_style = 0;
400 win->id = 0;
401 win->instance = NULL;
402 win->is_unicode = 1;
403 win->user_data = NULL;
404 win->text = NULL;
405 win->paint_flags = 0;
406 win->prop_inuse = 0;
407 win->prop_alloc = 0;
408 win->properties = NULL;
409 win->nb_extra_bytes = extra_bytes;
410 memset( win->extra_bytes, 0, extra_bytes );
411 list_init( &win->children );
412 list_init( &win->unlinked );
414 /* parent must be on the same desktop */
415 if (parent && parent->desktop != desktop)
417 set_error( STATUS_ACCESS_DENIED );
418 goto failed;
421 /* if no parent, class must be the desktop */
422 if (!parent && !is_desktop_class( class ))
424 set_error( STATUS_ACCESS_DENIED );
425 goto failed;
428 /* if parent belongs to a different thread, attach the two threads */
429 if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
431 if (!attach_thread_input( current, parent->thread )) goto failed;
433 else /* otherwise just make sure that the thread has a message queue */
435 if (!current->queue && !init_thread_queue( current )) goto failed;
438 /* put it on parent unlinked list */
439 if (parent) list_add_head( &parent->unlinked, &win->entry );
440 else
442 list_init( &win->entry );
443 assert( !desktop->top_window );
444 desktop->top_window = win;
445 set_process_default_desktop( current->process, desktop, current->desktop );
448 current->desktop_users++;
449 return win;
451 failed:
452 if (win)
454 if (win->handle) free_user_handle( win->handle );
455 free( win );
457 release_object( desktop );
458 release_class( class );
459 return NULL;
462 /* destroy all windows belonging to a given thread */
463 void destroy_thread_windows( struct thread *thread )
465 user_handle_t handle = 0;
466 struct window *win;
468 while ((win = next_user_handle( &handle, USER_WINDOW )))
470 if (win->thread != thread) continue;
471 if (is_desktop_window( win )) detach_window_thread( win );
472 else destroy_window( win );
476 /* get the desktop window */
477 static struct window *get_desktop_window( struct thread *thread, int create )
479 struct window *top_window;
480 struct desktop *desktop = get_thread_desktop( thread, 0 );
482 if (!desktop) return NULL;
484 if (!(top_window = desktop->top_window) && create)
486 if ((top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
488 detach_window_thread( top_window );
489 top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
492 release_object( desktop );
493 return top_window;
496 /* check whether child is a descendant of parent */
497 int is_child_window( user_handle_t parent, user_handle_t child )
499 struct window *child_ptr = get_user_object( child, USER_WINDOW );
500 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
502 if (!child_ptr || !parent_ptr) return 0;
503 while (child_ptr->parent)
505 if (child_ptr->parent == parent_ptr) return 1;
506 child_ptr = child_ptr->parent;
508 return 0;
511 /* check whether window is a top-level window */
512 int is_top_level_window( user_handle_t window )
514 struct window *win = get_user_object( window, USER_WINDOW );
515 return (win && win->parent && is_desktop_window(win->parent));
518 /* make a window active if possible */
519 int make_window_active( user_handle_t window )
521 struct window *owner, *win = get_window( window );
523 if (!win) return 0;
525 /* set last active for window and its owner */
526 win->last_active = win->handle;
527 if ((owner = get_user_object( win->owner, USER_WINDOW ))) owner->last_active = win->handle;
528 return 1;
531 /* increment (or decrement) the window paint count */
532 static inline void inc_window_paint_count( struct window *win, int incr )
534 if (win->thread) inc_queue_paint_count( win->thread, incr );
537 /* check if window and all its ancestors are visible */
538 static int is_visible( const struct window *win )
540 while (win && win->parent)
542 if (!(win->style & WS_VISIBLE)) return 0;
543 win = win->parent;
544 /* if parent is minimized children are not visible */
545 if (win && (win->style & WS_MINIMIZE)) return 0;
547 return 1;
550 /* same as is_visible but takes a window handle */
551 int is_window_visible( user_handle_t window )
553 struct window *win = get_user_object( window, USER_WINDOW );
554 if (!win) return 0;
555 return is_visible( win );
558 /* check if point is inside the window */
559 static inline int is_point_in_window( struct window *win, int x, int y )
561 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
562 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
563 return 0; /* disabled child */
564 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
565 return 0; /* transparent */
566 if (x < win->visible_rect.left || x >= win->visible_rect.right ||
567 y < win->visible_rect.top || y >= win->visible_rect.bottom)
568 return 0; /* not in window */
569 if (win->win_region &&
570 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
571 return 0; /* not in window region */
572 return 1;
575 /* find child of 'parent' that contains the given point (in parent-relative coords) */
576 static struct window *child_window_from_point( struct window *parent, int x, int y )
578 struct window *ptr;
580 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
582 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
584 /* if window is minimized or disabled, return at once */
585 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
587 /* if point is not in client area, return at once */
588 if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
589 y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
590 return ptr;
592 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
594 return parent; /* not found any child */
597 /* find all children of 'parent' that contain the given point */
598 static int get_window_children_from_point( struct window *parent, int x, int y,
599 struct user_handle_array *array )
601 struct window *ptr;
603 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
605 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
607 /* if point is in client area, and window is not minimized or disabled, check children */
608 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
609 x >= ptr->client_rect.left && x < ptr->client_rect.right &&
610 y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
612 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
613 y - ptr->client_rect.top, array ))
614 return 0;
617 /* now add window to the array */
618 if (!add_handle_to_array( array, ptr->handle )) return 0;
620 return 1;
623 /* find window containing point (in absolute coords) */
624 user_handle_t window_from_point( struct desktop *desktop, int x, int y )
626 struct window *ret;
628 if (!desktop->top_window) return 0;
629 ret = child_window_from_point( desktop->top_window, x, y );
630 return ret->handle;
633 /* return list of all windows containing point (in absolute coords) */
634 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
636 struct window *ptr;
638 /* make point relative to top window */
639 for (ptr = top->parent; ptr; ptr = ptr->parent)
641 x -= ptr->client_rect.left;
642 y -= ptr->client_rect.top;
645 if (!is_point_in_window( top, x, y )) return 1;
647 /* if point is in client area, and window is not minimized or disabled, check children */
648 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
649 x >= top->client_rect.left && x < top->client_rect.right &&
650 y >= top->client_rect.top && y < top->client_rect.bottom)
652 if (!get_window_children_from_point( top, x - top->client_rect.left,
653 y - top->client_rect.top, array ))
654 return 0;
656 /* now add window to the array */
657 if (!add_handle_to_array( array, top->handle )) return 0;
658 return 1;
662 /* return the thread owning a window */
663 struct thread *get_window_thread( user_handle_t handle )
665 struct window *win = get_user_object( handle, USER_WINDOW );
666 if (!win || !win->thread) return NULL;
667 return (struct thread *)grab_object( win->thread );
671 /* check if any area of a window needs repainting */
672 static inline int win_needs_repaint( struct window *win )
674 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
678 /* find a child of the specified window that needs repainting */
679 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
681 struct window *ptr, *ret = NULL;
683 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
685 if (!(ptr->style & WS_VISIBLE)) continue;
686 if (ptr->thread == thread && win_needs_repaint( ptr ))
687 ret = ptr;
688 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
689 ret = find_child_to_repaint( ptr, thread );
690 if (ret) break;
693 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
695 /* transparent window, check for non-transparent sibling to paint first */
696 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
698 if (!(ptr->style & WS_VISIBLE)) continue;
699 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
700 if (ptr->thread != thread) continue;
701 if (win_needs_repaint( ptr )) return ptr;
704 return ret;
708 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
709 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
711 struct window *ptr, *win, *top_window = get_desktop_window( thread, 0 );
713 if (!top_window) return 0;
715 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
716 else win = find_child_to_repaint( top_window, thread );
718 if (win && parent)
720 /* check that it is a child of the specified parent */
721 for (ptr = win; ptr; ptr = ptr->parent)
722 if (ptr->handle == parent) break;
723 /* otherwise don't return any window, we don't repaint a child before its parent */
724 if (!ptr) win = NULL;
726 if (!win) return 0;
727 win->paint_flags &= ~PAINT_INTERNAL;
728 return win->handle;
732 /* intersect the window region with the specified region, relative to the window parent */
733 static struct region *intersect_window_region( struct region *region, struct window *win )
735 /* make region relative to window rect */
736 offset_region( region, -win->window_rect.left, -win->window_rect.top );
737 if (!intersect_region( region, region, win->win_region )) return NULL;
738 /* make region relative to parent again */
739 offset_region( region, win->window_rect.left, win->window_rect.top );
740 return region;
744 /* convert coordinates from client to screen coords */
745 static inline void client_to_screen( struct window *win, int *x, int *y )
747 for ( ; win; win = win->parent)
749 *x += win->client_rect.left;
750 *y += win->client_rect.top;
754 /* map the region from window to screen coordinates */
755 static inline void map_win_region_to_screen( struct window *win, struct region *region )
757 int x = win->window_rect.left;
758 int y = win->window_rect.top;
759 client_to_screen( win->parent, &x, &y );
760 offset_region( region, x, y );
764 /* clip all children of a given window out of the visible region */
765 static struct region *clip_children( struct window *parent, struct window *last,
766 struct region *region, int offset_x, int offset_y )
768 struct window *ptr;
769 struct region *tmp = create_empty_region();
771 if (!tmp) return NULL;
772 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
774 if (ptr == last) break;
775 if (!(ptr->style & WS_VISIBLE)) continue;
776 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
777 set_region_rect( tmp, &ptr->visible_rect );
778 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
780 free_region( tmp );
781 return NULL;
783 offset_region( tmp, offset_x, offset_y );
784 if (!(region = subtract_region( region, region, tmp ))) break;
785 if (is_region_empty( region )) break;
787 free_region( tmp );
788 return region;
792 /* compute the intersection of two rectangles; return 0 if the result is empty */
793 static inline int intersect_rect( rectangle_t *dst, const rectangle_t *src1, const rectangle_t *src2 )
795 dst->left = max( src1->left, src2->left );
796 dst->top = max( src1->top, src2->top );
797 dst->right = min( src1->right, src2->right );
798 dst->bottom = min( src1->bottom, src2->bottom );
799 return (dst->left < dst->right && dst->top < dst->bottom);
803 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
804 static void set_region_client_rect( struct region *region, struct window *win )
806 rectangle_t rect;
808 intersect_rect( &rect, &win->window_rect, &win->client_rect );
809 set_region_rect( region, &rect );
813 /* get the top-level window to clip against for a given window */
814 static inline struct window *get_top_clipping_window( struct window *win )
816 while (win->parent && !is_desktop_window(win->parent)) win = win->parent;
817 return win;
821 /* compute the visible region of a window, in window coordinates */
822 static struct region *get_visible_region( struct window *win, struct window *top, unsigned int flags )
824 struct region *tmp = NULL, *region;
825 int offset_x, offset_y;
827 if (!(region = create_empty_region())) return NULL;
829 /* first check if all ancestors are visible */
831 if (!is_visible( win )) return region; /* empty region */
833 /* create a region relative to the window itself */
835 if ((flags & DCX_PARENTCLIP) && win != top && win->parent)
837 set_region_client_rect( region, win->parent );
838 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
840 else if (flags & DCX_WINDOW)
842 set_region_rect( region, &win->visible_rect );
843 if (win->win_region && !intersect_window_region( region, win )) goto error;
845 else
847 set_region_client_rect( region, win );
848 if (win->win_region && !intersect_window_region( region, win )) goto error;
850 offset_x = win->window_rect.left;
851 offset_y = win->window_rect.top;
853 /* clip children */
855 if (flags & DCX_CLIPCHILDREN)
857 if (!clip_children( win, NULL, region, win->client_rect.left, win->client_rect.top ))
858 goto error;
861 /* clip siblings of ancestors */
863 if (top && top != win && (tmp = create_empty_region()) != NULL)
865 while (win != top && win->parent)
867 if (win->style & WS_CLIPSIBLINGS)
869 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
870 if (is_region_empty( region )) break;
872 /* clip to parent client area */
873 win = win->parent;
874 offset_x += win->client_rect.left;
875 offset_y += win->client_rect.top;
876 offset_region( region, win->client_rect.left, win->client_rect.top );
877 set_region_client_rect( tmp, win );
878 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
879 if (!intersect_region( region, region, tmp )) goto error;
880 if (is_region_empty( region )) break;
882 free_region( tmp );
884 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
885 return region;
887 error:
888 if (tmp) free_region( tmp );
889 free_region( region );
890 return NULL;
894 /* get the window class of a window */
895 struct window_class* get_window_class( user_handle_t window )
897 struct window *win;
898 if (!(win = get_window( window ))) return NULL;
899 if (!win->class) set_error( STATUS_ACCESS_DENIED );
900 return win->class;
903 /* return a copy of the specified region cropped to the window client or frame rectangle, */
904 /* and converted from client to window coordinates. Helper for (in)validate_window. */
905 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
907 struct region *tmp = create_empty_region();
909 if (!tmp) return NULL;
911 /* get bounding rect in client coords */
912 if (frame) set_region_rect( tmp, &win->window_rect );
913 else set_region_client_rect( tmp, win );
914 offset_region( tmp, -win->client_rect.left, -win->client_rect.top );
916 /* intersect specified region with bounding rect */
917 if (region && !intersect_region( tmp, region, tmp )) goto done;
918 if (is_region_empty( tmp )) goto done;
920 /* map it to window coords */
921 offset_region( tmp, win->client_rect.left - win->window_rect.left,
922 win->client_rect.top - win->window_rect.top );
923 return tmp;
925 done:
926 free_region( tmp );
927 return NULL;
931 /* set a region as new update region for the window */
932 static void set_update_region( struct window *win, struct region *region )
934 if (region && !is_region_empty( region ))
936 if (!win->update_region) inc_window_paint_count( win, 1 );
937 else free_region( win->update_region );
938 win->update_region = region;
940 else
942 if (win->update_region)
944 inc_window_paint_count( win, -1 );
945 free_region( win->update_region );
947 win->paint_flags &= ~(PAINT_ERASE | PAINT_NONCLIENT);
948 win->update_region = NULL;
949 if (region) free_region( region );
954 /* add a region to the update region; the passed region is freed or reused */
955 static int add_update_region( struct window *win, struct region *region )
957 if (win->update_region && !union_region( region, win->update_region, region ))
959 free_region( region );
960 return 0;
962 set_update_region( win, region );
963 return 1;
967 /* validate the non client area of a window */
968 static void validate_non_client( struct window *win )
970 struct region *tmp;
971 rectangle_t rect;
973 if (!win->update_region) return; /* nothing to do */
975 /* get client rect in window coords */
976 rect.left = win->client_rect.left - win->window_rect.left;
977 rect.top = win->client_rect.top - win->window_rect.top;
978 rect.right = win->client_rect.right - win->window_rect.left;
979 rect.bottom = win->client_rect.bottom - win->window_rect.top;
981 if ((tmp = create_empty_region()))
983 set_region_rect( tmp, &rect );
984 if (intersect_region( tmp, win->update_region, tmp ))
985 set_update_region( win, tmp );
986 else
987 free_region( tmp );
989 win->paint_flags &= ~PAINT_NONCLIENT;
993 /* validate a window completely so that we don't get any further paint messages for it */
994 static void validate_whole_window( struct window *win )
996 set_update_region( win, NULL );
998 if (win->paint_flags & PAINT_INTERNAL)
1000 win->paint_flags &= ~PAINT_INTERNAL;
1001 inc_window_paint_count( win, -1 );
1006 /* validate the update region of a window on all parents; helper for redraw_window */
1007 static void validate_parents( struct window *child )
1009 int offset_x = 0, offset_y = 0;
1010 struct window *win = child;
1011 struct region *tmp = NULL;
1013 if (!child->update_region) return;
1015 while (win->parent)
1017 /* map to parent client coords */
1018 offset_x += win->window_rect.left;
1019 offset_y += win->window_rect.top;
1021 win = win->parent;
1023 /* and now map to window coords */
1024 offset_x += win->client_rect.left - win->window_rect.left;
1025 offset_y += win->client_rect.top - win->window_rect.top;
1027 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1029 if (!tmp && !(tmp = create_empty_region())) return;
1030 offset_region( child->update_region, offset_x, offset_y );
1031 if (subtract_region( tmp, win->update_region, child->update_region ))
1033 set_update_region( win, tmp );
1034 tmp = NULL;
1036 /* restore child coords */
1037 offset_region( child->update_region, -offset_x, -offset_y );
1040 if (tmp) free_region( tmp );
1044 /* add/subtract a region (in client coordinates) to the update region of the window */
1045 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1047 struct region *tmp;
1048 struct window *child;
1050 if (flags & RDW_INVALIDATE)
1052 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1054 if (!add_update_region( win, tmp )) return;
1056 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1057 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1059 else if (flags & RDW_VALIDATE)
1061 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1063 set_update_region( win, NULL );
1065 else if (win->update_region)
1067 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1069 if (!subtract_region( tmp, win->update_region, tmp ))
1071 free_region( tmp );
1072 return;
1074 set_update_region( win, tmp );
1076 if (flags & RDW_NOFRAME) validate_non_client( win );
1077 if (flags & RDW_NOERASE) win->paint_flags &= ~PAINT_ERASE;
1081 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1083 win->paint_flags |= PAINT_INTERNAL;
1084 inc_window_paint_count( win, 1 );
1086 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1088 win->paint_flags &= ~PAINT_INTERNAL;
1089 inc_window_paint_count( win, -1 );
1092 if (flags & RDW_UPDATENOW)
1094 validate_parents( win );
1095 flags &= ~RDW_UPDATENOW;
1098 /* now process children recursively */
1100 if (flags & RDW_NOCHILDREN) return;
1101 if (win->style & WS_MINIMIZE) return;
1102 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1104 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1106 /* map to client coordinates */
1107 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1108 win->window_rect.top - win->client_rect.top );
1110 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1112 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1114 if (!(child->style & WS_VISIBLE)) continue;
1115 if (!rect_in_region( tmp, &child->window_rect )) continue;
1116 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1117 redraw_window( child, tmp, 1, flags );
1118 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1120 free_region( tmp );
1124 /* retrieve the update flags for a window depending on the state of the update region */
1125 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1127 unsigned int ret = 0;
1129 if (flags & UPDATE_NONCLIENT)
1131 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1133 if (flags & UPDATE_ERASE)
1135 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1137 if (flags & UPDATE_PAINT)
1139 if (win->update_region) ret |= UPDATE_PAINT;
1141 if (flags & UPDATE_INTERNALPAINT)
1143 if (win->paint_flags & PAINT_INTERNAL) ret |= UPDATE_INTERNALPAINT;
1145 return ret;
1149 /* iterate through the children of the given window until we find one with some update flags */
1150 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1151 unsigned int flags, struct window **child )
1153 struct window *ptr;
1154 unsigned int ret = 0;
1156 /* first make sure we want to iterate children at all */
1158 if (win->style & WS_MINIMIZE) return 0;
1160 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1161 * here we only want to repaint children of windows that clip them, others
1162 * need to wait for WM_PAINT to be done in the parent first.
1164 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1166 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1168 if (from_child) /* skip all children until from_child is found */
1170 if (ptr == from_child) from_child = NULL;
1171 continue;
1173 if (!(ptr->style & WS_VISIBLE)) continue;
1174 if ((ret = get_update_flags( ptr, flags )) != 0)
1176 *child = ptr;
1177 break;
1179 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1181 return ret;
1184 /* iterate through children and siblings of the given window until we find one with some update flags */
1185 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1186 unsigned int flags, struct window **child )
1188 unsigned int ret;
1189 struct window *ptr, *from_sibling = NULL;
1191 /* if some parent is not visible start from the next sibling */
1193 if (!is_visible( win )) return 0;
1194 for (ptr = from_child; ptr; ptr = ptr->parent)
1196 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1197 if (ptr == win) break;
1200 /* non-client painting must be delayed if one of the parents is going to
1201 * be repainted and doesn't clip children */
1203 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1205 for (ptr = win->parent; ptr; ptr = ptr->parent)
1207 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1208 return 0;
1210 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1212 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1214 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1215 if (ptr == win) break;
1221 /* check window itself (only if not restarting from a child) */
1223 if (!from_child)
1225 if ((ret = get_update_flags( win, flags )))
1227 *child = win;
1228 return ret;
1230 from_child = win;
1233 /* now check children */
1235 if (flags & UPDATE_NOCHILDREN) return 0;
1236 if (!from_sibling)
1238 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1239 from_sibling = from_child;
1242 /* then check siblings and parent siblings */
1244 while (from_sibling->parent && from_sibling != win)
1246 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1247 return ret;
1248 from_sibling = from_sibling->parent;
1250 return 0;
1254 /* expose a region of a window, looking for the top most parent that needs to be exposed */
1255 /* the region is in window coordinates */
1256 static void expose_window( struct window *win, struct window *top, struct region *region )
1258 struct window *parent, *ptr;
1259 int offset_x, offset_y;
1261 /* find the top most parent that doesn't clip either siblings or children */
1262 for (parent = ptr = win; ptr != top; ptr = ptr->parent)
1264 if (!(ptr->style & WS_CLIPCHILDREN)) parent = ptr;
1265 if (!(ptr->style & WS_CLIPSIBLINGS)) parent = ptr->parent;
1267 if (parent == win && parent != top && win->parent)
1268 parent = win->parent; /* always go up at least one level if possible */
1270 offset_x = win->window_rect.left - win->client_rect.left;
1271 offset_y = win->window_rect.top - win->client_rect.top;
1272 for (ptr = win; ptr != parent; ptr = ptr->parent)
1274 offset_x += ptr->client_rect.left;
1275 offset_y += ptr->client_rect.top;
1277 offset_region( region, offset_x, offset_y );
1278 redraw_window( parent, region, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1279 offset_region( region, -offset_x, -offset_y );
1283 /* set the window and client rectangles, updating the update region if necessary */
1284 static void set_window_pos( struct window *win, struct window *previous,
1285 unsigned int swp_flags, const rectangle_t *window_rect,
1286 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1287 const rectangle_t *valid_rects )
1289 struct region *old_vis_rgn = NULL, *new_vis_rgn;
1290 const rectangle_t old_window_rect = win->window_rect;
1291 const rectangle_t old_visible_rect = win->visible_rect;
1292 const rectangle_t old_client_rect = win->client_rect;
1293 struct window *top = get_top_clipping_window( win );
1294 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1296 if (win->parent && !is_visible( win->parent )) visible = 0;
1298 if (visible && !(old_vis_rgn = get_visible_region( win, top, DCX_WINDOW ))) return;
1300 /* set the new window info before invalidating anything */
1302 win->window_rect = *window_rect;
1303 win->visible_rect = *visible_rect;
1304 win->client_rect = *client_rect;
1305 if (!(swp_flags & SWP_NOZORDER) && win->parent)
1307 list_remove( &win->entry ); /* unlink it from the previous location */
1308 if (previous) list_add_after( &previous->entry, &win->entry );
1309 else list_add_head( &win->parent->children, &win->entry );
1311 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1312 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1314 /* if the window is not visible, everything is easy */
1315 if (!visible) return;
1317 if (!(new_vis_rgn = get_visible_region( win, top, DCX_WINDOW )))
1319 free_region( old_vis_rgn );
1320 clear_error(); /* ignore error since the window info has been modified already */
1321 return;
1324 /* expose anything revealed by the change */
1326 if (!(swp_flags & SWP_NOREDRAW))
1328 offset_region( old_vis_rgn, old_window_rect.left - window_rect->left,
1329 old_window_rect.top - window_rect->top );
1330 if (xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1331 expose_window( win, top, new_vis_rgn );
1333 free_region( old_vis_rgn );
1335 if (!(win->style & WS_VISIBLE))
1337 /* clear the update region since the window is no longer visible */
1338 validate_whole_window( win );
1339 goto done;
1342 /* crop update region to the new window rect */
1344 if (win->update_region &&
1345 (window_rect->right - window_rect->left < old_window_rect.right - old_window_rect.left ||
1346 window_rect->bottom - window_rect->top < old_window_rect.bottom - old_window_rect.top))
1348 struct region *tmp = create_empty_region();
1349 if (tmp)
1351 set_region_rect( tmp, window_rect );
1352 offset_region( tmp, -window_rect->left, -window_rect->top );
1353 if (intersect_region( tmp, win->update_region, tmp ))
1354 set_update_region( win, tmp );
1355 else
1356 free_region( tmp );
1360 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1362 /* expose the whole non-client area if it changed in any way */
1364 if ((swp_flags & SWP_FRAMECHANGED) ||
1365 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1366 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ) ||
1367 memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1369 struct region *tmp = create_empty_region();
1371 if (tmp)
1373 /* subtract the valid portion of client rect from the total region */
1374 if (!memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1375 set_region_rect( tmp, client_rect );
1376 else if (valid_rects)
1377 set_region_rect( tmp, &valid_rects[0] );
1379 set_region_rect( new_vis_rgn, window_rect );
1380 if (subtract_region( tmp, new_vis_rgn, tmp ))
1382 offset_region( tmp, -client_rect->left, -client_rect->top );
1383 redraw_window( win, tmp, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1385 free_region( tmp );
1389 done:
1390 free_region( new_vis_rgn );
1391 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1395 /* create a window */
1396 DECL_HANDLER(create_window)
1398 struct window *win, *parent, *owner = NULL;
1400 reply->handle = 0;
1402 if (!req->parent) parent = get_desktop_window( current, 0 );
1403 else if (!(parent = get_window( req->parent ))) return;
1405 if (req->owner)
1407 if (!(owner = get_window( req->owner ))) return;
1408 if (is_desktop_window(owner)) owner = NULL;
1409 else if (!is_desktop_window(parent))
1411 /* an owned window must be created as top-level */
1412 set_error( STATUS_ACCESS_DENIED );
1413 return;
1415 else /* owner must be a top-level window */
1416 while (!is_desktop_window(owner->parent)) owner = owner->parent;
1418 if (!(win = create_window( parent, owner, req->atom, req->instance ))) return;
1420 reply->handle = win->handle;
1421 reply->parent = win->parent ? win->parent->handle : 0;
1422 reply->owner = win->owner;
1423 reply->extra = win->nb_extra_bytes;
1424 reply->class_ptr = get_class_client_ptr( win->class );
1428 /* set the parent of a window */
1429 DECL_HANDLER(set_parent)
1431 struct window *win, *parent = NULL;
1433 if (!(win = get_window( req->handle ))) return;
1434 if (req->parent && !(parent = get_window( req->parent ))) return;
1436 if (is_desktop_window(win))
1438 set_error( STATUS_INVALID_PARAMETER );
1439 return;
1441 reply->old_parent = win->parent->handle;
1442 reply->full_parent = parent ? parent->handle : 0;
1443 set_parent_window( win, parent );
1447 /* destroy a window */
1448 DECL_HANDLER(destroy_window)
1450 struct window *win = get_window( req->handle );
1451 if (win)
1453 if (!is_desktop_window(win)) destroy_window( win );
1454 else if (win->thread == current) detach_window_thread( win );
1455 else set_error( STATUS_ACCESS_DENIED );
1460 /* retrieve the desktop window for the current thread */
1461 DECL_HANDLER(get_desktop_window)
1463 struct window *win = get_desktop_window( current, req->force );
1465 if (win) reply->handle = win->handle;
1469 /* set a window owner */
1470 DECL_HANDLER(set_window_owner)
1472 struct window *win = get_window( req->handle );
1473 struct window *owner = NULL;
1475 if (!win) return;
1476 if (req->owner && !(owner = get_window( req->owner ))) return;
1477 if (is_desktop_window(win))
1479 set_error( STATUS_ACCESS_DENIED );
1480 return;
1482 reply->prev_owner = win->owner;
1483 reply->full_owner = win->owner = owner ? owner->handle : 0;
1487 /* get information from a window handle */
1488 DECL_HANDLER(get_window_info)
1490 struct window *win = get_window( req->handle );
1492 reply->full_handle = 0;
1493 reply->tid = reply->pid = 0;
1494 if (win)
1496 reply->full_handle = win->handle;
1497 reply->last_active = win->handle;
1498 reply->is_unicode = win->is_unicode;
1499 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
1500 if (win->thread)
1502 reply->tid = get_thread_id( win->thread );
1503 reply->pid = get_process_id( win->thread->process );
1504 reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
1510 /* set some information in a window */
1511 DECL_HANDLER(set_window_info)
1513 struct window *win = get_window( req->handle );
1515 if (!win) return;
1516 if (req->flags && is_desktop_window(win) && win->thread != current)
1518 set_error( STATUS_ACCESS_DENIED );
1519 return;
1521 if (req->extra_size > sizeof(req->extra_value) ||
1522 req->extra_offset < -1 ||
1523 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
1525 set_win32_error( ERROR_INVALID_INDEX );
1526 return;
1528 if (req->extra_offset != -1)
1530 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
1532 else if (req->flags & SET_WIN_EXTRA)
1534 set_win32_error( ERROR_INVALID_INDEX );
1535 return;
1537 reply->old_style = win->style;
1538 reply->old_ex_style = win->ex_style;
1539 reply->old_id = win->id;
1540 reply->old_instance = win->instance;
1541 reply->old_user_data = win->user_data;
1542 if (req->flags & SET_WIN_STYLE) win->style = req->style;
1543 if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
1544 if (req->flags & SET_WIN_ID) win->id = req->id;
1545 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
1546 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
1547 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
1548 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
1549 &req->extra_value, req->extra_size );
1551 /* changing window style triggers a non-client paint */
1552 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
1556 /* get a list of the window parents, up to the root of the tree */
1557 DECL_HANDLER(get_window_parents)
1559 struct window *ptr, *win = get_window( req->handle );
1560 int total = 0;
1561 user_handle_t *data;
1562 size_t len;
1564 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
1566 reply->count = total;
1567 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1568 if (len && ((data = set_reply_data_size( len ))))
1570 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
1571 *data++ = ptr->handle;
1576 /* get a list of the window children */
1577 DECL_HANDLER(get_window_children)
1579 struct window *ptr, *parent = get_window( req->parent );
1580 int total = 0;
1581 user_handle_t *data;
1582 size_t len;
1584 if (parent)
1586 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
1588 if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1589 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1590 total++;
1593 reply->count = total;
1594 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1595 if (len && ((data = set_reply_data_size( len ))))
1597 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
1599 if (len < sizeof(*data)) break;
1600 if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1601 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1602 *data++ = ptr->handle;
1603 len -= sizeof(*data);
1609 /* get a list of the window children that contain a given point */
1610 DECL_HANDLER(get_window_children_from_point)
1612 struct user_handle_array array;
1613 struct window *parent = get_window( req->parent );
1614 size_t len;
1616 if (!parent) return;
1618 array.handles = NULL;
1619 array.count = 0;
1620 array.total = 0;
1621 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
1623 reply->count = array.count;
1624 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
1625 if (len) set_reply_data_ptr( array.handles, len );
1626 else free( array.handles );
1630 /* get window tree information from a window handle */
1631 DECL_HANDLER(get_window_tree)
1633 struct window *ptr, *win = get_window( req->handle );
1635 if (!win) return;
1637 reply->parent = 0;
1638 reply->owner = 0;
1639 reply->next_sibling = 0;
1640 reply->prev_sibling = 0;
1641 reply->first_sibling = 0;
1642 reply->last_sibling = 0;
1643 reply->first_child = 0;
1644 reply->last_child = 0;
1646 if (win->parent)
1648 struct window *parent = win->parent;
1649 reply->parent = parent->handle;
1650 reply->owner = win->owner;
1651 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
1652 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
1653 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
1654 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
1656 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
1657 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
1661 /* set the position and Z order of a window */
1662 DECL_HANDLER(set_window_pos)
1664 const rectangle_t *visible_rect = NULL, *valid_rects = NULL;
1665 struct window *previous = NULL;
1666 struct window *win = get_window( req->handle );
1667 unsigned int flags = req->flags;
1669 if (!win) return;
1670 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
1672 if (!(flags & SWP_NOZORDER))
1674 if (!req->previous) /* special case: HWND_TOP */
1676 if (get_first_child(win->parent) == win) flags |= SWP_NOZORDER;
1678 else if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
1680 previous = get_last_child( win->parent );
1682 else
1684 if (!(previous = get_window( req->previous ))) return;
1685 /* previous must be a sibling */
1686 if (previous->parent != win->parent)
1688 set_error( STATUS_INVALID_PARAMETER );
1689 return;
1692 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
1695 /* window rectangle must be ordered properly */
1696 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
1698 set_error( STATUS_INVALID_PARAMETER );
1699 return;
1702 if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = get_req_data();
1703 if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rects = visible_rect + 1;
1705 if (!visible_rect) visible_rect = &req->window;
1706 set_window_pos( win, previous, flags, &req->window, &req->client, visible_rect, valid_rects );
1707 reply->new_style = win->style;
1711 /* get the window and client rectangles of a window */
1712 DECL_HANDLER(get_window_rectangles)
1714 struct window *win = get_window( req->handle );
1716 if (win)
1718 reply->window = win->window_rect;
1719 reply->visible = win->visible_rect;
1720 reply->client = win->client_rect;
1725 /* get the window text */
1726 DECL_HANDLER(get_window_text)
1728 struct window *win = get_window( req->handle );
1730 if (win && win->text)
1732 size_t len = strlenW( win->text ) * sizeof(WCHAR);
1733 if (len > get_reply_max_size()) len = get_reply_max_size();
1734 set_reply_data( win->text, len );
1739 /* set the window text */
1740 DECL_HANDLER(set_window_text)
1742 struct window *win = get_window( req->handle );
1744 if (win)
1746 WCHAR *text = NULL;
1747 size_t len = get_req_data_size() / sizeof(WCHAR);
1748 if (len)
1750 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
1751 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
1752 text[len] = 0;
1754 if (win->text) free( win->text );
1755 win->text = text;
1760 /* get the coordinates offset between two windows */
1761 DECL_HANDLER(get_windows_offset)
1763 struct window *win;
1765 reply->x = reply->y = 0;
1766 if (req->from)
1768 if (!(win = get_window( req->from ))) return;
1769 while (win)
1771 reply->x += win->client_rect.left;
1772 reply->y += win->client_rect.top;
1773 win = win->parent;
1776 if (req->to)
1778 if (!(win = get_window( req->to ))) return;
1779 while (win)
1781 reply->x -= win->client_rect.left;
1782 reply->y -= win->client_rect.top;
1783 win = win->parent;
1789 /* get the visible region of a window */
1790 DECL_HANDLER(get_visible_region)
1792 struct region *region;
1793 struct window *top, *win = get_window( req->window );
1795 if (!win) return;
1797 top = get_top_clipping_window( win );
1798 if ((region = get_visible_region( win, top, req->flags )))
1800 rectangle_t *data;
1801 map_win_region_to_screen( win, region );
1802 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
1803 if (data) set_reply_data_ptr( data, reply->total_size );
1805 reply->top_win = top->handle;
1806 reply->top_org_x = top->visible_rect.left;
1807 reply->top_org_y = top->visible_rect.top;
1808 reply->win_org_x = (req->flags & DCX_WINDOW) ? win->window_rect.left : win->client_rect.left;
1809 reply->win_org_y = (req->flags & DCX_WINDOW) ? win->window_rect.top : win->client_rect.top;
1810 client_to_screen( top->parent, &reply->top_org_x, &reply->top_org_y );
1811 client_to_screen( win->parent, &reply->win_org_x, &reply->win_org_y );
1815 /* get the window region */
1816 DECL_HANDLER(get_window_region)
1818 struct window *win = get_window( req->window );
1820 if (!win) return;
1822 if (win->win_region)
1824 rectangle_t *data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
1825 if (data) set_reply_data_ptr( data, reply->total_size );
1830 /* set the window region */
1831 DECL_HANDLER(set_window_region)
1833 struct region *region = NULL;
1834 struct window *win = get_window( req->window );
1836 if (!win) return;
1838 if (get_req_data_size()) /* no data means remove the region completely */
1840 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1841 return;
1843 if (win->win_region) free_region( win->win_region );
1844 win->win_region = region;
1848 /* get a window update region */
1849 DECL_HANDLER(get_update_region)
1851 rectangle_t *data;
1852 unsigned int flags = req->flags;
1853 struct window *from_child = NULL;
1854 struct window *win = get_window( req->window );
1856 reply->flags = 0;
1857 if (!win) return;
1859 if (req->from_child)
1861 struct window *ptr;
1863 if (!(from_child = get_window( req->from_child ))) return;
1865 /* make sure from_child is a child of win */
1866 ptr = from_child;
1867 while (ptr && ptr != win) ptr = ptr->parent;
1868 if (!ptr)
1870 set_error( STATUS_INVALID_PARAMETER );
1871 return;
1875 reply->flags = get_window_update_flags( win, from_child, flags, &win );
1876 reply->child = win->handle;
1878 if (flags & UPDATE_NOREGION) return;
1880 if (win->update_region)
1882 /* convert update region to screen coordinates */
1883 struct region *region = create_empty_region();
1885 if (!region) return;
1886 if (!copy_region( region, win->update_region ))
1888 free_region( region );
1889 return;
1891 map_win_region_to_screen( win, region );
1892 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
1893 &reply->total_size ))) return;
1894 set_reply_data_ptr( data, reply->total_size );
1897 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
1899 validate_whole_window( win );
1901 else
1903 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
1904 if (reply->flags & UPDATE_ERASE)
1906 win->paint_flags &= ~PAINT_ERASE;
1907 /* desktop window only gets erased, not repainted */
1908 if (is_desktop_window(win)) validate_whole_window( win );
1914 /* update the z order of a window so that a given rectangle is fully visible */
1915 DECL_HANDLER(update_window_zorder)
1917 rectangle_t tmp;
1918 struct window *ptr, *win = get_window( req->window );
1920 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
1922 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
1924 if (ptr == win) break;
1925 if (!(ptr->style & WS_VISIBLE)) continue;
1926 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1927 if (!intersect_rect( &tmp, &ptr->visible_rect, &req->rect )) continue;
1928 if (ptr->win_region && !rect_in_region( ptr->win_region, &req->rect )) continue;
1929 /* found a window obscuring the rectangle, now move win above this one */
1930 list_remove( &win->entry );
1931 list_add_before( &ptr->entry, &win->entry );
1932 break;
1937 /* mark parts of a window as needing a redraw */
1938 DECL_HANDLER(redraw_window)
1940 struct region *region = NULL;
1941 struct window *win = get_window( req->window );
1943 if (!win) return;
1944 if (!is_visible( win )) return; /* nothing to do */
1946 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
1948 if (get_req_data_size()) /* no data means whole rectangle */
1950 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1951 return;
1955 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
1956 req->flags );
1957 if (region) free_region( region );
1961 /* set a window property */
1962 DECL_HANDLER(set_window_property)
1964 struct window *win = get_window( req->window );
1966 if (!win) return;
1968 if (get_req_data_size())
1970 atom_t atom = add_global_atom( NULL, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1971 if (atom)
1973 set_property( win, atom, req->handle, PROP_TYPE_STRING );
1974 release_global_atom( NULL, atom );
1977 else set_property( win, req->atom, req->handle, PROP_TYPE_ATOM );
1981 /* remove a window property */
1982 DECL_HANDLER(remove_window_property)
1984 struct window *win = get_window( req->window );
1986 if (win)
1988 atom_t atom = req->atom;
1989 if (get_req_data_size()) atom = find_global_atom( NULL, get_req_data(),
1990 get_req_data_size() / sizeof(WCHAR) );
1991 if (atom) reply->handle = remove_property( win, atom );
1996 /* get a window property */
1997 DECL_HANDLER(get_window_property)
1999 struct window *win = get_window( req->window );
2001 if (win)
2003 atom_t atom = req->atom;
2004 if (get_req_data_size()) atom = find_global_atom( NULL, get_req_data(),
2005 get_req_data_size() / sizeof(WCHAR) );
2006 if (atom) reply->handle = get_property( win, atom );
2011 /* get the list of properties of a window */
2012 DECL_HANDLER(get_window_properties)
2014 property_data_t *data;
2015 int i, count, max = get_reply_max_size() / sizeof(*data);
2016 struct window *win = get_window( req->window );
2018 reply->total = 0;
2019 if (!win) return;
2021 for (i = count = 0; i < win->prop_inuse; i++)
2022 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2023 reply->total = count;
2025 if (count > max) count = max;
2026 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2028 for (i = 0; i < win->prop_inuse && count; i++)
2030 if (win->properties[i].type == PROP_TYPE_FREE) continue;
2031 data->atom = win->properties[i].atom;
2032 data->string = (win->properties[i].type == PROP_TYPE_STRING);
2033 data->handle = win->properties[i].handle;
2034 data++;
2035 count--;
2040 /* get the new window pointer for a global window, checking permissions */
2041 /* helper for set_global_windows request */
2042 static int get_new_global_window( struct window **win, user_handle_t handle )
2044 if (!handle)
2046 *win = NULL;
2047 return 1;
2049 else if (*win)
2051 set_error( STATUS_ACCESS_DENIED );
2052 return 0;
2054 *win = get_window( handle );
2055 return (*win != NULL);
2058 /* Set/get the global windows */
2059 DECL_HANDLER(set_global_windows)
2061 struct window *new_shell_window = shell_window;
2062 struct window *new_shell_listview = shell_listview;
2063 struct window *new_progman_window = progman_window;
2064 struct window *new_taskman_window = taskman_window;
2066 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2067 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2068 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2069 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2071 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2073 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2074 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2076 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2078 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2080 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2082 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2084 shell_window = new_shell_window;
2085 shell_listview = new_shell_listview;
2086 progman_window = new_progman_window;
2087 taskman_window = new_taskman_window;