Fixed 'break NN' command (using dbghelp.SymEnumLines).
[wine/multimedia.git] / server / window.c
blobea19663b001b885f59387e27c525ffeea30ea483
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 desktop *desktop; /* desktop that the window belongs to */
65 struct window_class *class; /* window class */
66 atom_t atom; /* class atom */
67 user_handle_t last_active; /* last active popup */
68 rectangle_t window_rect; /* window rectangle (relative to parent client area) */
69 rectangle_t visible_rect; /* visible part of window rect (relative to parent client area) */
70 rectangle_t client_rect; /* client rectangle (relative to parent client area) */
71 struct region *win_region; /* region for shaped windows (relative to window rect) */
72 struct region *update_region; /* update region (relative to window rect) */
73 unsigned int style; /* window style */
74 unsigned int ex_style; /* window extended style */
75 unsigned int id; /* window id */
76 void* instance; /* creator instance */
77 int is_unicode; /* ANSI or unicode */
78 void* user_data; /* user-specific data */
79 WCHAR *text; /* window caption text */
80 unsigned int paint_flags; /* various painting flags */
81 int prop_inuse; /* number of in-use window properties */
82 int prop_alloc; /* number of allocated window properties */
83 struct property *properties; /* window properties array */
84 int nb_extra_bytes; /* number of extra bytes */
85 char extra_bytes[1]; /* extra bytes storage */
88 #define PAINT_INTERNAL 0x01 /* internal WM_PAINT pending */
89 #define PAINT_ERASE 0x02 /* needs WM_ERASEBKGND */
90 #define PAINT_NONCLIENT 0x04 /* needs WM_NCPAINT */
92 /* growable array of user handles */
93 struct user_handle_array
95 user_handle_t *handles;
96 int count;
97 int total;
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 /* check if window is the desktop */
178 static inline int is_desktop_window( const struct window *win )
180 return !win->parent; /* only desktop windows have no parent */
183 /* append a user handle to a handle array */
184 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
186 if (array->count >= array->total)
188 int new_total = max( array->total * 2, 32 );
189 user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
190 if (!new_array)
192 free( array->handles );
193 set_error( STATUS_NO_MEMORY );
194 return 0;
196 array->handles = new_array;
197 array->total = new_total;
199 array->handles[array->count++] = handle;
200 return 1;
203 /* set a window property */
204 static void set_property( struct window *win, atom_t atom, obj_handle_t handle, enum property_type type )
206 int i, free = -1;
207 struct property *new_props;
209 /* check if it exists already */
210 for (i = 0; i < win->prop_inuse; i++)
212 if (win->properties[i].type == PROP_TYPE_FREE)
214 free = i;
215 continue;
217 if (win->properties[i].atom == atom)
219 win->properties[i].type = type;
220 win->properties[i].handle = handle;
221 return;
225 /* need to add an entry */
226 if (!grab_global_atom( win->desktop->winstation, atom )) return;
227 if (free == -1)
229 /* no free entry */
230 if (win->prop_inuse >= win->prop_alloc)
232 /* need to grow the array */
233 if (!(new_props = realloc( win->properties,
234 sizeof(*new_props) * (win->prop_alloc + 16) )))
236 set_error( STATUS_NO_MEMORY );
237 release_global_atom( win->desktop->winstation, atom );
238 return;
240 win->prop_alloc += 16;
241 win->properties = new_props;
243 free = win->prop_inuse++;
245 win->properties[free].atom = atom;
246 win->properties[free].type = type;
247 win->properties[free].handle = handle;
250 /* remove a window property */
251 static obj_handle_t remove_property( struct window *win, atom_t atom )
253 int i;
255 for (i = 0; i < win->prop_inuse; i++)
257 if (win->properties[i].type == PROP_TYPE_FREE) continue;
258 if (win->properties[i].atom == atom)
260 release_global_atom( win->desktop->winstation, atom );
261 win->properties[i].type = PROP_TYPE_FREE;
262 return win->properties[i].handle;
265 /* FIXME: last error? */
266 return 0;
269 /* find a window property */
270 static obj_handle_t get_property( struct window *win, atom_t atom )
272 int i;
274 for (i = 0; i < win->prop_inuse; i++)
276 if (win->properties[i].type == PROP_TYPE_FREE) continue;
277 if (win->properties[i].atom == atom) return win->properties[i].handle;
279 /* FIXME: last error? */
280 return 0;
283 /* destroy all properties of a window */
284 inline static void destroy_properties( struct window *win )
286 int i;
288 if (!win->properties) return;
289 for (i = 0; i < win->prop_inuse; i++)
291 if (win->properties[i].type == PROP_TYPE_FREE) continue;
292 release_global_atom( win->desktop->winstation, win->properties[i].atom );
294 free( win->properties );
297 /* destroy a window */
298 void destroy_window( struct window *win )
300 struct thread *thread = win->thread;
302 /* destroy all children */
303 while (!list_empty(&win->children))
304 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
305 while (!list_empty(&win->unlinked))
306 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
308 if (thread && thread->queue)
310 if (win->update_region) inc_queue_paint_count( thread, -1 );
311 if (win->paint_flags & PAINT_INTERNAL) inc_queue_paint_count( thread, -1 );
312 queue_cleanup_window( thread, win->handle );
315 /* reset global window pointers, if the corresponding window is destroyed */
316 if (win == shell_window) shell_window = NULL;
317 if (win == shell_listview) shell_listview = NULL;
318 if (win == progman_window) progman_window = NULL;
319 if (win == taskman_window) taskman_window = NULL;
320 free_user_handle( win->handle );
321 destroy_properties( win );
322 list_remove( &win->entry );
323 if (win->win_region) free_region( win->win_region );
324 if (win->update_region) free_region( win->update_region );
325 release_class( win->class );
326 if (win->text) free( win->text );
327 if (!is_desktop_window(win))
329 assert( thread->desktop_users > 0 );
330 thread->desktop_users--;
331 release_object( win->desktop );
333 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
334 free( win );
337 /* create a new window structure (note: the window is not linked in the window tree) */
338 static struct window *create_window( struct window *parent, struct window *owner,
339 atom_t atom, void *instance )
341 int extra_bytes;
342 struct window *win;
343 struct desktop *desktop;
344 struct window_class *class;
346 if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
348 if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
350 release_object( desktop );
351 return NULL;
354 win = mem_alloc( sizeof(*win) + extra_bytes - 1 );
355 if (!win)
357 release_object( desktop );
358 release_class( class );
359 return NULL;
361 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
363 win->parent = parent;
364 win->owner = owner ? owner->handle : 0;
365 win->thread = current;
366 win->desktop = desktop;
367 win->class = class;
368 win->atom = atom;
369 win->last_active = win->handle;
370 win->win_region = NULL;
371 win->update_region = NULL;
372 win->style = 0;
373 win->ex_style = 0;
374 win->id = 0;
375 win->instance = NULL;
376 win->is_unicode = 1;
377 win->user_data = NULL;
378 win->text = NULL;
379 win->paint_flags = 0;
380 win->prop_inuse = 0;
381 win->prop_alloc = 0;
382 win->properties = NULL;
383 win->nb_extra_bytes = extra_bytes;
384 memset( win->extra_bytes, 0, extra_bytes );
385 list_init( &win->children );
386 list_init( &win->unlinked );
388 /* parent must be on the same desktop */
389 if (parent && parent->desktop != desktop)
391 set_error( STATUS_ACCESS_DENIED );
392 goto failed;
395 /* if parent belongs to a different thread, attach the two threads */
396 if (parent && parent->thread && parent->thread != current)
398 if (!attach_thread_input( current, parent->thread )) goto failed;
400 else /* otherwise just make sure that the thread has a message queue */
402 if (!current->queue && !init_thread_queue( current )) goto failed;
405 /* put it on parent unlinked list */
406 if (parent) list_add_head( &parent->unlinked, &win->entry );
407 else list_init( &win->entry );
409 current->desktop_users++;
410 return win;
412 failed:
413 if (win->handle) free_user_handle( win->handle );
414 release_object( desktop );
415 release_class( class );
416 free( win );
417 return NULL;
420 /* destroy all windows belonging to a given thread */
421 void destroy_thread_windows( struct thread *thread )
423 user_handle_t handle = 0;
424 struct window *win;
426 while ((win = next_user_handle( &handle, USER_WINDOW )))
428 if (win->thread != thread) continue;
429 destroy_window( win );
433 /* get the desktop window */
434 static struct window *get_desktop_window( struct thread *thread, int create )
436 struct window *top_window;
437 struct desktop *desktop = get_thread_desktop( thread, 0 );
439 if (!desktop) return NULL;
441 if (!(top_window = desktop->top_window) && create)
443 if ((top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
445 current->desktop_users--;
446 top_window->thread = NULL; /* no thread owns the desktop */
447 top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
448 desktop->top_window = top_window;
449 /* don't hold a reference to the desktop so that the desktop window can be */
450 /* destroyed when the desktop ref count reaches zero */
451 release_object( top_window->desktop );
454 release_object( desktop );
455 return top_window;
458 /* check whether child is a descendant of parent */
459 int is_child_window( user_handle_t parent, user_handle_t child )
461 struct window *child_ptr = get_user_object( child, USER_WINDOW );
462 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
464 if (!child_ptr || !parent_ptr) return 0;
465 while (child_ptr->parent)
467 if (child_ptr->parent == parent_ptr) return 1;
468 child_ptr = child_ptr->parent;
470 return 0;
473 /* check whether window is a top-level window */
474 int is_top_level_window( user_handle_t window )
476 struct window *win = get_user_object( window, USER_WINDOW );
477 return (win && win->parent && is_desktop_window(win->parent));
480 /* make a window active if possible */
481 int make_window_active( user_handle_t window )
483 struct window *owner, *win = get_window( window );
485 if (!win) return 0;
487 /* set last active for window and its owner */
488 win->last_active = win->handle;
489 if ((owner = get_user_object( win->owner, USER_WINDOW ))) owner->last_active = win->handle;
490 return 1;
493 /* increment (or decrement) the window paint count */
494 static inline void inc_window_paint_count( struct window *win, int incr )
496 if (win->thread) inc_queue_paint_count( win->thread, incr );
499 /* check if window and all its ancestors are visible */
500 static int is_visible( const struct window *win )
502 while (win && win->parent)
504 if (!(win->style & WS_VISIBLE)) return 0;
505 win = win->parent;
506 /* if parent is minimized children are not visible */
507 if (win && (win->style & WS_MINIMIZE)) return 0;
509 return 1;
512 /* same as is_visible but takes a window handle */
513 int is_window_visible( user_handle_t window )
515 struct window *win = get_user_object( window, USER_WINDOW );
516 if (!win) return 0;
517 return is_visible( win );
520 /* check if point is inside the window */
521 static inline int is_point_in_window( struct window *win, int x, int y )
523 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
524 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
525 return 0; /* disabled child */
526 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
527 return 0; /* transparent */
528 if (x < win->visible_rect.left || x >= win->visible_rect.right ||
529 y < win->visible_rect.top || y >= win->visible_rect.bottom)
530 return 0; /* not in window */
531 if (win->win_region &&
532 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
533 return 0; /* not in window region */
534 return 1;
537 /* find child of 'parent' that contains the given point (in parent-relative coords) */
538 static struct window *child_window_from_point( struct window *parent, int x, int y )
540 struct window *ptr;
542 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
544 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
546 /* if window is minimized or disabled, return at once */
547 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
549 /* if point is not in client area, return at once */
550 if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
551 y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
552 return ptr;
554 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
556 return parent; /* not found any child */
559 /* find all children of 'parent' that contain the given point */
560 static int get_window_children_from_point( struct window *parent, int x, int y,
561 struct user_handle_array *array )
563 struct window *ptr;
565 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
567 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
569 /* if point is in client area, and window is not minimized or disabled, check children */
570 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
571 x >= ptr->client_rect.left && x < ptr->client_rect.right &&
572 y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
574 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
575 y - ptr->client_rect.top, array ))
576 return 0;
579 /* now add window to the array */
580 if (!add_handle_to_array( array, ptr->handle )) return 0;
582 return 1;
585 /* find window containing point (in absolute coords) */
586 user_handle_t window_from_point( struct desktop *desktop, int x, int y )
588 struct window *ret;
590 if (!desktop->top_window) return 0;
591 ret = child_window_from_point( desktop->top_window, x, y );
592 return ret->handle;
595 /* return list of all windows containing point (in absolute coords) */
596 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
598 struct window *ptr;
600 /* make point relative to top window */
601 for (ptr = top->parent; ptr; ptr = ptr->parent)
603 x -= ptr->client_rect.left;
604 y -= ptr->client_rect.top;
607 if (!is_point_in_window( top, x, y )) return 1;
609 /* if point is in client area, and window is not minimized or disabled, check children */
610 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
611 x >= top->client_rect.left && x < top->client_rect.right &&
612 y >= top->client_rect.top && y < top->client_rect.bottom)
614 if (!get_window_children_from_point( top, x - top->client_rect.left,
615 y - top->client_rect.top, array ))
616 return 0;
618 /* now add window to the array */
619 if (!add_handle_to_array( array, top->handle )) return 0;
620 return 1;
624 /* return the thread owning a window */
625 struct thread *get_window_thread( user_handle_t handle )
627 struct window *win = get_user_object( handle, USER_WINDOW );
628 if (!win || !win->thread) return NULL;
629 return (struct thread *)grab_object( win->thread );
633 /* check if any area of a window needs repainting */
634 static inline int win_needs_repaint( struct window *win )
636 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
640 /* find a child of the specified window that needs repainting */
641 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
643 struct window *ptr, *ret = NULL;
645 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
647 if (!(ptr->style & WS_VISIBLE)) continue;
648 if (ptr->thread == thread && win_needs_repaint( ptr ))
649 ret = ptr;
650 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
651 ret = find_child_to_repaint( ptr, thread );
652 if (ret) break;
655 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
657 /* transparent window, check for non-transparent sibling to paint first */
658 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
660 if (!(ptr->style & WS_VISIBLE)) continue;
661 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
662 if (ptr->thread != thread) continue;
663 if (win_needs_repaint( ptr )) return ptr;
666 return ret;
670 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
671 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
673 struct window *ptr, *win, *top_window = get_desktop_window( thread, 0 );
675 if (!top_window) return 0;
677 win = find_child_to_repaint( top_window, thread );
678 if (win && parent)
680 /* check that it is a child of the specified parent */
681 for (ptr = win; ptr; ptr = ptr->parent)
682 if (ptr->handle == parent) break;
683 /* otherwise don't return any window, we don't repaint a child before its parent */
684 if (!ptr) win = NULL;
686 if (!win) return 0;
687 win->paint_flags &= ~PAINT_INTERNAL;
688 return win->handle;
692 /* intersect the window region with the specified region, relative to the window parent */
693 static struct region *intersect_window_region( struct region *region, struct window *win )
695 /* make region relative to window rect */
696 offset_region( region, -win->window_rect.left, -win->window_rect.top );
697 if (!intersect_region( region, region, win->win_region )) return NULL;
698 /* make region relative to parent again */
699 offset_region( region, win->window_rect.left, win->window_rect.top );
700 return region;
704 /* convert coordinates from client to screen coords */
705 static inline void client_to_screen( struct window *win, int *x, int *y )
707 for ( ; win; win = win->parent)
709 *x += win->client_rect.left;
710 *y += win->client_rect.top;
714 /* map the region from window to screen coordinates */
715 static inline void map_win_region_to_screen( struct window *win, struct region *region )
717 int x = win->window_rect.left;
718 int y = win->window_rect.top;
719 client_to_screen( win->parent, &x, &y );
720 offset_region( region, x, y );
724 /* clip all children of a given window out of the visible region */
725 static struct region *clip_children( struct window *parent, struct window *last,
726 struct region *region, int offset_x, int offset_y )
728 struct window *ptr;
729 struct region *tmp = create_empty_region();
731 if (!tmp) return NULL;
732 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
734 if (ptr == last) break;
735 if (!(ptr->style & WS_VISIBLE)) continue;
736 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
737 set_region_rect( tmp, &ptr->visible_rect );
738 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
740 free_region( tmp );
741 return NULL;
743 offset_region( tmp, offset_x, offset_y );
744 if (!(region = subtract_region( region, region, tmp ))) break;
745 if (is_region_empty( region )) break;
747 free_region( tmp );
748 return region;
752 /* compute the intersection of two rectangles; return 0 if the result is empty */
753 static inline int intersect_rect( rectangle_t *dst, const rectangle_t *src1, const rectangle_t *src2 )
755 dst->left = max( src1->left, src2->left );
756 dst->top = max( src1->top, src2->top );
757 dst->right = min( src1->right, src2->right );
758 dst->bottom = min( src1->bottom, src2->bottom );
759 return (dst->left < dst->right && dst->top < dst->bottom);
763 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
764 static void set_region_client_rect( struct region *region, struct window *win )
766 rectangle_t rect;
768 intersect_rect( &rect, &win->window_rect, &win->client_rect );
769 set_region_rect( region, &rect );
773 /* get the top-level window to clip against for a given window */
774 static inline struct window *get_top_clipping_window( struct window *win )
776 while (win->parent && !is_desktop_window(win->parent)) win = win->parent;
777 return win;
781 /* compute the visible region of a window, in window coordinates */
782 static struct region *get_visible_region( struct window *win, struct window *top, unsigned int flags )
784 struct region *tmp = NULL, *region;
785 int offset_x, offset_y;
787 if (!(region = create_empty_region())) return NULL;
789 /* first check if all ancestors are visible */
791 if (!is_visible( win )) return region; /* empty region */
793 /* create a region relative to the window itself */
795 if ((flags & DCX_PARENTCLIP) && win != top && win->parent)
797 set_region_client_rect( region, win->parent );
798 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
800 else if (flags & DCX_WINDOW)
802 set_region_rect( region, &win->visible_rect );
803 if (win->win_region && !intersect_window_region( region, win )) goto error;
805 else
807 set_region_client_rect( region, win );
808 if (win->win_region && !intersect_window_region( region, win )) goto error;
810 offset_x = win->window_rect.left;
811 offset_y = win->window_rect.top;
813 /* clip children */
815 if (flags & DCX_CLIPCHILDREN)
817 if (!clip_children( win, NULL, region, win->client_rect.left, win->client_rect.top ))
818 goto error;
821 /* clip siblings of ancestors */
823 if (top && top != win && (tmp = create_empty_region()) != NULL)
825 while (win != top && win->parent)
827 if (win->style & WS_CLIPSIBLINGS)
829 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
830 if (is_region_empty( region )) break;
832 /* clip to parent client area */
833 win = win->parent;
834 offset_x += win->client_rect.left;
835 offset_y += win->client_rect.top;
836 offset_region( region, win->client_rect.left, win->client_rect.top );
837 set_region_client_rect( tmp, win );
838 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
839 if (!intersect_region( region, region, tmp )) goto error;
840 if (is_region_empty( region )) break;
842 free_region( tmp );
844 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
845 return region;
847 error:
848 if (tmp) free_region( tmp );
849 free_region( region );
850 return NULL;
854 /* get the window class of a window */
855 struct window_class* get_window_class( user_handle_t window )
857 struct window *win;
858 if (!(win = get_window( window ))) return NULL;
859 return win->class;
862 /* return a copy of the specified region cropped to the window client or frame rectangle, */
863 /* and converted from client to window coordinates. Helper for (in)validate_window. */
864 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
866 struct region *tmp = create_empty_region();
868 if (!tmp) return NULL;
870 /* get bounding rect in client coords */
871 if (frame) set_region_rect( tmp, &win->window_rect );
872 else set_region_client_rect( tmp, win );
873 offset_region( tmp, -win->client_rect.left, -win->client_rect.top );
875 /* intersect specified region with bounding rect */
876 if (region && !intersect_region( tmp, region, tmp )) goto done;
877 if (is_region_empty( tmp )) goto done;
879 /* map it to window coords */
880 offset_region( tmp, win->client_rect.left - win->window_rect.left,
881 win->client_rect.top - win->window_rect.top );
882 return tmp;
884 done:
885 free_region( tmp );
886 return NULL;
890 /* set a region as new update region for the window */
891 static void set_update_region( struct window *win, struct region *region )
893 if (region && !is_region_empty( region ))
895 if (!win->update_region) inc_window_paint_count( win, 1 );
896 else free_region( win->update_region );
897 win->update_region = region;
899 else
901 if (win->update_region)
903 inc_window_paint_count( win, -1 );
904 free_region( win->update_region );
906 win->paint_flags &= ~(PAINT_ERASE | PAINT_NONCLIENT);
907 win->update_region = NULL;
908 if (region) free_region( region );
913 /* add a region to the update region; the passed region is freed or reused */
914 static int add_update_region( struct window *win, struct region *region )
916 if (win->update_region && !union_region( region, win->update_region, region ))
918 free_region( region );
919 return 0;
921 set_update_region( win, region );
922 return 1;
926 /* validate the non client area of a window */
927 static void validate_non_client( struct window *win )
929 struct region *tmp;
930 rectangle_t rect;
932 if (!win->update_region) return; /* nothing to do */
934 /* get client rect in window coords */
935 rect.left = win->client_rect.left - win->window_rect.left;
936 rect.top = win->client_rect.top - win->window_rect.top;
937 rect.right = win->client_rect.right - win->window_rect.left;
938 rect.bottom = win->client_rect.bottom - win->window_rect.top;
940 if ((tmp = create_empty_region()))
942 set_region_rect( tmp, &rect );
943 if (intersect_region( tmp, win->update_region, tmp ))
944 set_update_region( win, tmp );
945 else
946 free_region( tmp );
948 win->paint_flags &= ~PAINT_NONCLIENT;
952 /* validate a window completely so that we don't get any further paint messages for it */
953 static void validate_whole_window( struct window *win )
955 set_update_region( win, NULL );
957 if (win->paint_flags & PAINT_INTERNAL)
959 win->paint_flags &= ~PAINT_INTERNAL;
960 inc_window_paint_count( win, -1 );
965 /* validate the update region of a window on all parents; helper for redraw_window */
966 static void validate_parents( struct window *child )
968 int offset_x = 0, offset_y = 0;
969 struct window *win = child;
970 struct region *tmp = NULL;
972 if (!child->update_region) return;
974 while (win->parent)
976 /* map to parent client coords */
977 offset_x += win->window_rect.left;
978 offset_y += win->window_rect.top;
980 win = win->parent;
982 /* and now map to window coords */
983 offset_x += win->client_rect.left - win->window_rect.left;
984 offset_y += win->client_rect.top - win->window_rect.top;
986 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
988 if (!tmp && !(tmp = create_empty_region())) return;
989 offset_region( child->update_region, offset_x, offset_y );
990 if (subtract_region( tmp, win->update_region, child->update_region ))
992 set_update_region( win, tmp );
993 tmp = NULL;
995 /* restore child coords */
996 offset_region( child->update_region, -offset_x, -offset_y );
999 if (tmp) free_region( tmp );
1003 /* add/subtract a region (in client coordinates) to the update region of the window */
1004 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1006 struct region *tmp;
1007 struct window *child;
1009 if (flags & RDW_INVALIDATE)
1011 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1013 if (!add_update_region( win, tmp )) return;
1015 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1016 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1018 else if (flags & RDW_VALIDATE)
1020 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1022 set_update_region( win, NULL );
1024 else if (win->update_region)
1026 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1028 if (!subtract_region( tmp, win->update_region, tmp ))
1030 free_region( tmp );
1031 return;
1033 set_update_region( win, tmp );
1035 if (flags & RDW_NOFRAME) validate_non_client( win );
1036 if (flags & RDW_NOERASE) win->paint_flags &= ~PAINT_ERASE;
1040 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1042 win->paint_flags |= PAINT_INTERNAL;
1043 inc_window_paint_count( win, 1 );
1045 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1047 win->paint_flags &= ~PAINT_INTERNAL;
1048 inc_window_paint_count( win, -1 );
1051 if (flags & RDW_UPDATENOW)
1053 validate_parents( win );
1054 flags &= ~RDW_UPDATENOW;
1057 /* now process children recursively */
1059 if (flags & RDW_NOCHILDREN) return;
1060 if (win->style & WS_MINIMIZE) return;
1061 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1063 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1065 /* map to client coordinates */
1066 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1067 win->window_rect.top - win->client_rect.top );
1069 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1071 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1073 if (!(child->style & WS_VISIBLE)) continue;
1074 if (!rect_in_region( tmp, &child->window_rect )) continue;
1075 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1076 redraw_window( child, tmp, 1, flags );
1077 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1079 free_region( tmp );
1083 /* retrieve the update flags for a window depending on the state of the update region */
1084 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1086 unsigned int ret = 0;
1088 if (flags & UPDATE_NONCLIENT)
1090 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1092 if (flags & UPDATE_ERASE)
1094 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1096 if (flags & UPDATE_PAINT)
1098 if (win->update_region) ret |= UPDATE_PAINT;
1100 if (flags & UPDATE_INTERNALPAINT)
1102 if (win->paint_flags & PAINT_INTERNAL) ret |= UPDATE_INTERNALPAINT;
1104 return ret;
1108 /* iterate through the children of the given window until we find one with some update flags */
1109 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1110 unsigned int flags, struct window **child )
1112 struct window *ptr;
1113 unsigned int ret = 0;
1115 /* first make sure we want to iterate children at all */
1117 if (win->style & WS_MINIMIZE) return 0;
1119 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1120 * here we only want to repaint children of windows that clip them, others
1121 * need to wait for WM_PAINT to be done in the parent first.
1123 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1125 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1127 if (from_child) /* skip all children until from_child is found */
1129 if (ptr == from_child) from_child = NULL;
1130 continue;
1132 if (!(ptr->style & WS_VISIBLE)) continue;
1133 if ((ret = get_update_flags( ptr, flags )) != 0)
1135 *child = ptr;
1136 break;
1138 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1140 return ret;
1143 /* iterate through children and siblings of the given window until we find one with some update flags */
1144 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1145 unsigned int flags, struct window **child )
1147 unsigned int ret;
1148 struct window *ptr, *from_sibling = NULL;
1150 /* if some parent is not visible start from the next sibling */
1152 if (!is_visible( win )) return 0;
1153 for (ptr = from_child; ptr; ptr = ptr->parent)
1155 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1156 if (ptr == win) break;
1159 /* non-client painting must be delayed if one of the parents is going to
1160 * be repainted and doesn't clip children */
1162 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1164 for (ptr = win->parent; ptr; ptr = ptr->parent)
1166 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1167 return 0;
1169 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1171 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1173 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1174 if (ptr == win) break;
1180 /* check window itself (only if not restarting from a child) */
1182 if (!from_child)
1184 if ((ret = get_update_flags( win, flags )))
1186 *child = win;
1187 return ret;
1189 from_child = win;
1192 /* now check children */
1194 if (flags & UPDATE_NOCHILDREN) return 0;
1195 if (!from_sibling)
1197 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1198 from_sibling = from_child;
1201 /* then check siblings and parent siblings */
1203 while (from_sibling->parent && from_sibling != win)
1205 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1206 return ret;
1207 from_sibling = from_sibling->parent;
1209 return 0;
1213 /* expose a region of a window, looking for the top most parent that needs to be exposed */
1214 /* the region is in window coordinates */
1215 static void expose_window( struct window *win, struct window *top, struct region *region )
1217 struct window *parent, *ptr;
1218 int offset_x, offset_y;
1220 /* find the top most parent that doesn't clip either siblings or children */
1221 for (parent = ptr = win; ptr != top; ptr = ptr->parent)
1223 if (!(ptr->style & WS_CLIPCHILDREN)) parent = ptr;
1224 if (!(ptr->style & WS_CLIPSIBLINGS)) parent = ptr->parent;
1226 if (parent == win && parent != top && win->parent)
1227 parent = win->parent; /* always go up at least one level if possible */
1229 offset_x = win->window_rect.left - win->client_rect.left;
1230 offset_y = win->window_rect.top - win->client_rect.top;
1231 for (ptr = win; ptr != parent; ptr = ptr->parent)
1233 offset_x += ptr->client_rect.left;
1234 offset_y += ptr->client_rect.top;
1236 offset_region( region, offset_x, offset_y );
1237 redraw_window( parent, region, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1238 offset_region( region, -offset_x, -offset_y );
1242 /* set the window and client rectangles, updating the update region if necessary */
1243 static void set_window_pos( struct window *win, struct window *previous,
1244 unsigned int swp_flags, const rectangle_t *window_rect,
1245 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1246 const rectangle_t *valid_rects )
1248 struct region *old_vis_rgn = NULL, *new_vis_rgn;
1249 const rectangle_t old_window_rect = win->window_rect;
1250 const rectangle_t old_visible_rect = win->visible_rect;
1251 const rectangle_t old_client_rect = win->client_rect;
1252 struct window *top = get_top_clipping_window( win );
1253 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1255 if (win->parent && !is_visible( win->parent )) visible = 0;
1257 if (visible && !(old_vis_rgn = get_visible_region( win, top, DCX_WINDOW ))) return;
1259 /* set the new window info before invalidating anything */
1261 win->window_rect = *window_rect;
1262 win->visible_rect = *visible_rect;
1263 win->client_rect = *client_rect;
1264 if (!(swp_flags & SWP_NOZORDER) && win->parent)
1266 list_remove( &win->entry ); /* unlink it from the previous location */
1267 if (previous) list_add_after( &previous->entry, &win->entry );
1268 else list_add_head( &win->parent->children, &win->entry );
1270 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1271 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1273 /* if the window is not visible, everything is easy */
1274 if (!visible) return;
1276 if (!(new_vis_rgn = get_visible_region( win, top, DCX_WINDOW )))
1278 free_region( old_vis_rgn );
1279 clear_error(); /* ignore error since the window info has been modified already */
1280 return;
1283 /* expose anything revealed by the change */
1285 if (!(swp_flags & SWP_NOREDRAW))
1287 offset_region( old_vis_rgn, old_window_rect.left - window_rect->left,
1288 old_window_rect.top - window_rect->top );
1289 if (xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1290 expose_window( win, top, new_vis_rgn );
1292 free_region( old_vis_rgn );
1294 if (!(win->style & WS_VISIBLE))
1296 /* clear the update region since the window is no longer visible */
1297 validate_whole_window( win );
1298 goto done;
1301 /* crop update region to the new window rect */
1303 if (win->update_region &&
1304 (window_rect->right - window_rect->left < old_window_rect.right - old_window_rect.left ||
1305 window_rect->bottom - window_rect->top < old_window_rect.bottom - old_window_rect.top))
1307 struct region *tmp = create_empty_region();
1308 if (tmp)
1310 set_region_rect( tmp, window_rect );
1311 offset_region( tmp, -window_rect->left, -window_rect->top );
1312 if (intersect_region( tmp, win->update_region, tmp ))
1313 set_update_region( win, tmp );
1314 else
1315 free_region( tmp );
1319 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1321 /* expose the whole non-client area if it changed in any way */
1323 if ((swp_flags & SWP_FRAMECHANGED) ||
1324 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1325 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ) ||
1326 memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1328 struct region *tmp = create_empty_region();
1330 if (tmp)
1332 /* subtract the valid portion of client rect from the total region */
1333 if (!memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1334 set_region_rect( tmp, client_rect );
1335 else if (valid_rects)
1336 set_region_rect( tmp, &valid_rects[0] );
1338 set_region_rect( new_vis_rgn, window_rect );
1339 if (subtract_region( tmp, new_vis_rgn, tmp ))
1341 offset_region( tmp, -client_rect->left, -client_rect->top );
1342 redraw_window( win, tmp, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1344 free_region( tmp );
1348 done:
1349 free_region( new_vis_rgn );
1350 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1354 /* create a window */
1355 DECL_HANDLER(create_window)
1357 struct window *win, *parent, *owner = NULL;
1359 reply->handle = 0;
1361 if (!(parent = get_window( req->parent ))) return;
1362 if (req->owner)
1364 if (!(owner = get_window( req->owner ))) return;
1365 if (is_desktop_window(owner)) owner = NULL;
1366 else if (!is_desktop_window(parent))
1368 /* an owned window must be created as top-level */
1369 set_error( STATUS_ACCESS_DENIED );
1370 return;
1373 if (!(win = create_window( parent, owner, req->atom, req->instance ))) return;
1375 reply->handle = win->handle;
1376 reply->extra = win->nb_extra_bytes;
1377 reply->class_ptr = get_class_client_ptr( win->class );
1381 /* set the parent of a window */
1382 DECL_HANDLER(set_parent)
1384 struct window *win, *parent = NULL;
1386 if (!(win = get_window( req->handle ))) return;
1387 if (req->parent && !(parent = get_window( req->parent ))) return;
1389 if (is_desktop_window(win))
1391 set_error( STATUS_INVALID_PARAMETER );
1392 return;
1394 reply->old_parent = win->parent->handle;
1395 reply->full_parent = parent ? parent->handle : 0;
1396 set_parent_window( win, parent );
1400 /* destroy a window */
1401 DECL_HANDLER(destroy_window)
1403 struct window *win = get_window( req->handle );
1404 if (win)
1406 if (!is_desktop_window(win)) destroy_window( win );
1407 else set_error( STATUS_ACCESS_DENIED );
1412 /* retrieve the desktop window for the current thread */
1413 DECL_HANDLER(get_desktop_window)
1415 struct window *win = get_desktop_window( current, 1 );
1417 if (win) reply->handle = win->handle;
1421 /* set a window owner */
1422 DECL_HANDLER(set_window_owner)
1424 struct window *win = get_window( req->handle );
1425 struct window *owner = NULL;
1427 if (!win) return;
1428 if (req->owner && !(owner = get_window( req->owner ))) return;
1429 if (is_desktop_window(win))
1431 set_error( STATUS_ACCESS_DENIED );
1432 return;
1434 reply->prev_owner = win->owner;
1435 reply->full_owner = win->owner = owner ? owner->handle : 0;
1439 /* get information from a window handle */
1440 DECL_HANDLER(get_window_info)
1442 struct window *win = get_window( req->handle );
1444 reply->full_handle = 0;
1445 reply->tid = reply->pid = 0;
1446 if (win)
1448 reply->full_handle = win->handle;
1449 reply->last_active = win->handle;
1450 reply->is_unicode = win->is_unicode;
1451 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
1452 if (win->thread)
1454 reply->tid = get_thread_id( win->thread );
1455 reply->pid = get_process_id( win->thread->process );
1456 reply->atom = get_class_atom( win->class );
1462 /* set some information in a window */
1463 DECL_HANDLER(set_window_info)
1465 struct window *win = get_window( req->handle );
1467 if (!win) return;
1468 if (req->flags && is_desktop_window(win))
1470 set_error( STATUS_ACCESS_DENIED );
1471 return;
1473 if (req->extra_size > sizeof(req->extra_value) ||
1474 req->extra_offset < -1 ||
1475 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
1477 set_win32_error( ERROR_INVALID_INDEX );
1478 return;
1480 if (req->extra_offset != -1)
1482 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
1484 else if (req->flags & SET_WIN_EXTRA)
1486 set_win32_error( ERROR_INVALID_INDEX );
1487 return;
1489 reply->old_style = win->style;
1490 reply->old_ex_style = win->ex_style;
1491 reply->old_id = win->id;
1492 reply->old_instance = win->instance;
1493 reply->old_user_data = win->user_data;
1494 if (req->flags & SET_WIN_STYLE) win->style = req->style;
1495 if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
1496 if (req->flags & SET_WIN_ID) win->id = req->id;
1497 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
1498 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
1499 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
1500 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
1501 &req->extra_value, req->extra_size );
1503 /* changing window style triggers a non-client paint */
1504 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
1508 /* get a list of the window parents, up to the root of the tree */
1509 DECL_HANDLER(get_window_parents)
1511 struct window *ptr, *win = get_window( req->handle );
1512 int total = 0;
1513 user_handle_t *data;
1514 size_t len;
1516 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
1518 reply->count = total;
1519 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1520 if (len && ((data = set_reply_data_size( len ))))
1522 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
1523 *data++ = ptr->handle;
1528 /* get a list of the window children */
1529 DECL_HANDLER(get_window_children)
1531 struct window *ptr, *parent = get_window( req->parent );
1532 int total = 0;
1533 user_handle_t *data;
1534 size_t len;
1536 if (parent)
1538 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
1540 if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1541 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1542 total++;
1545 reply->count = total;
1546 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1547 if (len && ((data = set_reply_data_size( len ))))
1549 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
1551 if (len < sizeof(*data)) break;
1552 if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1553 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1554 *data++ = ptr->handle;
1555 len -= sizeof(*data);
1561 /* get a list of the window children that contain a given point */
1562 DECL_HANDLER(get_window_children_from_point)
1564 struct user_handle_array array;
1565 struct window *parent = get_window( req->parent );
1566 size_t len;
1568 if (!parent) return;
1570 array.handles = NULL;
1571 array.count = 0;
1572 array.total = 0;
1573 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
1575 reply->count = array.count;
1576 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
1577 if (len) set_reply_data_ptr( array.handles, len );
1578 else free( array.handles );
1582 /* get window tree information from a window handle */
1583 DECL_HANDLER(get_window_tree)
1585 struct window *ptr, *win = get_window( req->handle );
1587 if (!win) return;
1589 reply->parent = 0;
1590 reply->owner = 0;
1591 reply->next_sibling = 0;
1592 reply->prev_sibling = 0;
1593 reply->first_sibling = 0;
1594 reply->last_sibling = 0;
1595 reply->first_child = 0;
1596 reply->last_child = 0;
1598 if (win->parent)
1600 struct window *parent = win->parent;
1601 reply->parent = parent->handle;
1602 reply->owner = win->owner;
1603 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
1604 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
1605 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
1606 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
1608 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
1609 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
1613 /* set the position and Z order of a window */
1614 DECL_HANDLER(set_window_pos)
1616 const rectangle_t *visible_rect = NULL, *valid_rects = NULL;
1617 struct window *previous = NULL;
1618 struct window *win = get_window( req->handle );
1619 unsigned int flags = req->flags;
1621 if (!win) return;
1622 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
1624 if (!(flags & SWP_NOZORDER))
1626 if (!req->previous) /* special case: HWND_TOP */
1628 if (get_first_child(win->parent) == win) flags |= SWP_NOZORDER;
1630 else if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
1632 previous = get_last_child( win->parent );
1634 else
1636 if (!(previous = get_window( req->previous ))) return;
1637 /* previous must be a sibling */
1638 if (previous->parent != win->parent)
1640 set_error( STATUS_INVALID_PARAMETER );
1641 return;
1644 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
1647 /* window rectangle must be ordered properly */
1648 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
1650 set_error( STATUS_INVALID_PARAMETER );
1651 return;
1654 if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = get_req_data();
1655 if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rects = visible_rect + 1;
1657 if (!visible_rect) visible_rect = &req->window;
1658 set_window_pos( win, previous, flags, &req->window, &req->client, visible_rect, valid_rects );
1659 reply->new_style = win->style;
1663 /* get the window and client rectangles of a window */
1664 DECL_HANDLER(get_window_rectangles)
1666 struct window *win = get_window( req->handle );
1668 if (win)
1670 reply->window = win->window_rect;
1671 reply->visible = win->visible_rect;
1672 reply->client = win->client_rect;
1677 /* get the window text */
1678 DECL_HANDLER(get_window_text)
1680 struct window *win = get_window( req->handle );
1682 if (win && win->text)
1684 size_t len = strlenW( win->text ) * sizeof(WCHAR);
1685 if (len > get_reply_max_size()) len = get_reply_max_size();
1686 set_reply_data( win->text, len );
1691 /* set the window text */
1692 DECL_HANDLER(set_window_text)
1694 struct window *win = get_window( req->handle );
1696 if (win)
1698 WCHAR *text = NULL;
1699 size_t len = get_req_data_size() / sizeof(WCHAR);
1700 if (len)
1702 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
1703 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
1704 text[len] = 0;
1706 if (win->text) free( win->text );
1707 win->text = text;
1712 /* get the coordinates offset between two windows */
1713 DECL_HANDLER(get_windows_offset)
1715 struct window *win;
1717 reply->x = reply->y = 0;
1718 if (req->from)
1720 if (!(win = get_window( req->from ))) return;
1721 while (win)
1723 reply->x += win->client_rect.left;
1724 reply->y += win->client_rect.top;
1725 win = win->parent;
1728 if (req->to)
1730 if (!(win = get_window( req->to ))) return;
1731 while (win)
1733 reply->x -= win->client_rect.left;
1734 reply->y -= win->client_rect.top;
1735 win = win->parent;
1741 /* get the visible region of a window */
1742 DECL_HANDLER(get_visible_region)
1744 struct region *region;
1745 struct window *top, *win = get_window( req->window );
1747 if (!win) return;
1749 top = get_top_clipping_window( win );
1750 if ((region = get_visible_region( win, top, req->flags )))
1752 rectangle_t *data;
1753 map_win_region_to_screen( win, region );
1754 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
1755 if (data) set_reply_data_ptr( data, reply->total_size );
1757 reply->top_win = top->handle;
1758 reply->top_org_x = top->visible_rect.left;
1759 reply->top_org_y = top->visible_rect.top;
1760 reply->win_org_x = (req->flags & DCX_WINDOW) ? win->window_rect.left : win->client_rect.left;
1761 reply->win_org_y = (req->flags & DCX_WINDOW) ? win->window_rect.top : win->client_rect.top;
1762 client_to_screen( top->parent, &reply->top_org_x, &reply->top_org_y );
1763 client_to_screen( win->parent, &reply->win_org_x, &reply->win_org_y );
1767 /* get the window region */
1768 DECL_HANDLER(get_window_region)
1770 struct window *win = get_window( req->window );
1772 if (!win) return;
1774 if (win->win_region)
1776 rectangle_t *data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
1777 if (data) set_reply_data_ptr( data, reply->total_size );
1782 /* set the window region */
1783 DECL_HANDLER(set_window_region)
1785 struct region *region = NULL;
1786 struct window *win = get_window( req->window );
1788 if (!win) return;
1790 if (get_req_data_size()) /* no data means remove the region completely */
1792 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1793 return;
1795 if (win->win_region) free_region( win->win_region );
1796 win->win_region = region;
1800 /* get a window update region */
1801 DECL_HANDLER(get_update_region)
1803 rectangle_t *data;
1804 unsigned int flags = req->flags;
1805 struct window *from_child = NULL;
1806 struct window *win = get_window( req->window );
1808 reply->flags = 0;
1809 if (!win) return;
1811 if (req->from_child)
1813 struct window *ptr;
1815 if (!(from_child = get_window( req->from_child ))) return;
1817 /* make sure from_child is a child of win */
1818 ptr = from_child;
1819 while (ptr && ptr != win) ptr = ptr->parent;
1820 if (!ptr)
1822 set_error( STATUS_INVALID_PARAMETER );
1823 return;
1827 reply->flags = get_window_update_flags( win, from_child, flags, &win );
1828 reply->child = win->handle;
1830 if (flags & UPDATE_NOREGION) return;
1832 if (win->update_region)
1834 /* convert update region to screen coordinates */
1835 struct region *region = create_empty_region();
1837 if (!region) return;
1838 if (!copy_region( region, win->update_region ))
1840 free_region( region );
1841 return;
1843 map_win_region_to_screen( win, region );
1844 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
1845 &reply->total_size ))) return;
1846 set_reply_data_ptr( data, reply->total_size );
1849 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
1851 validate_whole_window( win );
1853 else
1855 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
1856 if (reply->flags & UPDATE_ERASE)
1858 win->paint_flags &= ~PAINT_ERASE;
1859 /* desktop window only gets erased, not repainted */
1860 if (is_desktop_window(win)) validate_whole_window( win );
1866 /* update the z order of a window so that a given rectangle is fully visible */
1867 DECL_HANDLER(update_window_zorder)
1869 rectangle_t tmp;
1870 struct window *ptr, *win = get_window( req->window );
1872 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
1874 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
1876 if (ptr == win) break;
1877 if (!(ptr->style & WS_VISIBLE)) continue;
1878 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
1879 if (!intersect_rect( &tmp, &ptr->visible_rect, &req->rect )) continue;
1880 if (ptr->win_region && !rect_in_region( ptr->win_region, &req->rect )) continue;
1881 /* found a window obscuring the rectangle, now move win above this one */
1882 list_remove( &win->entry );
1883 list_add_before( &ptr->entry, &win->entry );
1884 break;
1889 /* mark parts of a window as needing a redraw */
1890 DECL_HANDLER(redraw_window)
1892 struct region *region = NULL;
1893 struct window *win = get_window( req->window );
1895 if (!win) return;
1896 if (!is_visible( win )) return; /* nothing to do */
1898 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
1900 if (get_req_data_size()) /* no data means whole rectangle */
1902 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1903 return;
1907 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
1908 req->flags );
1909 if (region) free_region( region );
1913 /* set a window property */
1914 DECL_HANDLER(set_window_property)
1916 struct window *win = get_window( req->window );
1918 if (!win) return;
1920 if (get_req_data_size())
1922 atom_t atom = add_global_atom( win->desktop->winstation,
1923 get_req_data(), get_req_data_size() / sizeof(WCHAR) );
1924 if (atom)
1926 set_property( win, atom, req->handle, PROP_TYPE_STRING );
1927 release_global_atom( win->desktop->winstation, atom );
1930 else set_property( win, req->atom, req->handle, PROP_TYPE_ATOM );
1934 /* remove a window property */
1935 DECL_HANDLER(remove_window_property)
1937 struct window *win = get_window( req->window );
1939 if (win)
1941 atom_t atom = req->atom;
1942 if (get_req_data_size()) atom = find_global_atom( win->desktop->winstation, get_req_data(),
1943 get_req_data_size() / sizeof(WCHAR) );
1944 if (atom) reply->handle = remove_property( win, atom );
1949 /* get a window property */
1950 DECL_HANDLER(get_window_property)
1952 struct window *win = get_window( req->window );
1954 if (win)
1956 atom_t atom = req->atom;
1957 if (get_req_data_size()) atom = find_global_atom( win->desktop->winstation, get_req_data(),
1958 get_req_data_size() / sizeof(WCHAR) );
1959 if (atom) reply->handle = get_property( win, atom );
1964 /* get the list of properties of a window */
1965 DECL_HANDLER(get_window_properties)
1967 property_data_t *data;
1968 int i, count, max = get_reply_max_size() / sizeof(*data);
1969 struct window *win = get_window( req->window );
1971 reply->total = 0;
1972 if (!win) return;
1974 for (i = count = 0; i < win->prop_inuse; i++)
1975 if (win->properties[i].type != PROP_TYPE_FREE) count++;
1976 reply->total = count;
1978 if (count > max) count = max;
1979 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
1981 for (i = 0; i < win->prop_inuse && count; i++)
1983 if (win->properties[i].type == PROP_TYPE_FREE) continue;
1984 data->atom = win->properties[i].atom;
1985 data->string = (win->properties[i].type == PROP_TYPE_STRING);
1986 data->handle = win->properties[i].handle;
1987 data++;
1988 count--;
1993 /* get the new window pointer for a global window, checking permissions */
1994 /* helper for set_global_windows request */
1995 static int get_new_global_window( struct window **win, user_handle_t handle )
1997 if (!handle)
1999 *win = NULL;
2000 return 1;
2002 else if (*win)
2004 set_error( STATUS_ACCESS_DENIED );
2005 return 0;
2007 *win = get_window( handle );
2008 return (*win != NULL);
2011 /* Set/get the global windows */
2012 DECL_HANDLER(set_global_windows)
2014 struct window *new_shell_window = shell_window;
2015 struct window *new_shell_listview = shell_listview;
2016 struct window *new_progman_window = progman_window;
2017 struct window *new_taskman_window = taskman_window;
2019 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2020 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2021 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2022 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2024 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2026 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2027 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2029 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2031 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2033 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2035 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2037 shell_window = new_shell_window;
2038 shell_listview = new_shell_listview;
2039 progman_window = new_progman_window;
2040 taskman_window = new_taskman_window;