cmd.exe: Add basic support for FOR /F parsing launched programs output.
[wine/multimedia.git] / server / window.c
blob14d646852e9544ea9b17449f206a96f94c850d32
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 unsigned long 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 */
94 #define PAINT_DELAYED_ERASE 0x08 /* still needs erase after WM_ERASEBKGND */
96 /* growable array of user handles */
97 struct user_handle_array
99 user_handle_t *handles;
100 int count;
101 int total;
104 /* global window pointers */
105 static struct window *shell_window;
106 static struct window *shell_listview;
107 static struct window *progman_window;
108 static struct window *taskman_window;
110 /* retrieve a pointer to a window from its handle */
111 static inline struct window *get_window( user_handle_t handle )
113 struct window *ret = get_user_object( handle, USER_WINDOW );
114 if (!ret) set_win32_error( ERROR_INVALID_WINDOW_HANDLE );
115 return ret;
118 /* check if window is the desktop */
119 static inline int is_desktop_window( const struct window *win )
121 return !win->parent; /* only desktop windows have no parent */
124 /* change the parent of a window (or unlink the window if the new parent is NULL) */
125 static int set_parent_window( struct window *win, struct window *parent )
127 struct window *ptr;
129 /* make sure parent is not a child of window */
130 for (ptr = parent; ptr; ptr = ptr->parent)
132 if (ptr == win)
134 set_error( STATUS_INVALID_PARAMETER );
135 return 0;
139 list_remove( &win->entry ); /* unlink it from the previous location */
141 if (parent)
143 win->parent = parent;
144 list_add_head( &parent->children, &win->entry );
146 /* if parent belongs to a different thread and the window isn't */
147 /* top-level, attach the two threads */
148 if (parent->thread && parent->thread != win->thread && !is_desktop_window(parent))
149 attach_thread_input( win->thread, parent->thread );
151 else /* move it to parent unlinked list */
153 list_add_head( &win->parent->unlinked, &win->entry );
155 return 1;
158 /* get next window in Z-order list */
159 static inline struct window *get_next_window( struct window *win )
161 struct list *ptr = list_next( &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 previous window in Z-order list */
167 static inline struct window *get_prev_window( struct window *win )
169 struct list *ptr = list_prev( &win->parent->children, &win->entry );
170 if (ptr == &win->parent->unlinked) ptr = NULL;
171 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
174 /* get first child in Z-order list */
175 static inline struct window *get_first_child( struct window *win )
177 struct list *ptr = list_head( &win->children );
178 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
181 /* get last child in Z-order list */
182 static inline struct window *get_last_child( struct window *win )
184 struct list *ptr = list_tail( &win->children );
185 return ptr ? LIST_ENTRY( ptr, struct window, entry ) : NULL;
188 /* append a user handle to a handle array */
189 static int add_handle_to_array( struct user_handle_array *array, user_handle_t handle )
191 if (array->count >= array->total)
193 int new_total = max( array->total * 2, 32 );
194 user_handle_t *new_array = realloc( array->handles, new_total * sizeof(*new_array) );
195 if (!new_array)
197 free( array->handles );
198 set_error( STATUS_NO_MEMORY );
199 return 0;
201 array->handles = new_array;
202 array->total = new_total;
204 array->handles[array->count++] = handle;
205 return 1;
208 /* set a window property */
209 static void set_property( struct window *win, atom_t atom, obj_handle_t handle, enum property_type type )
211 int i, free = -1;
212 struct property *new_props;
214 /* check if it exists already */
215 for (i = 0; i < win->prop_inuse; i++)
217 if (win->properties[i].type == PROP_TYPE_FREE)
219 free = i;
220 continue;
222 if (win->properties[i].atom == atom)
224 win->properties[i].type = type;
225 win->properties[i].handle = handle;
226 return;
230 /* need to add an entry */
231 if (!grab_global_atom( NULL, atom )) return;
232 if (free == -1)
234 /* no free entry */
235 if (win->prop_inuse >= win->prop_alloc)
237 /* need to grow the array */
238 if (!(new_props = realloc( win->properties,
239 sizeof(*new_props) * (win->prop_alloc + 16) )))
241 set_error( STATUS_NO_MEMORY );
242 release_global_atom( NULL, atom );
243 return;
245 win->prop_alloc += 16;
246 win->properties = new_props;
248 free = win->prop_inuse++;
250 win->properties[free].atom = atom;
251 win->properties[free].type = type;
252 win->properties[free].handle = handle;
255 /* remove a window property */
256 static obj_handle_t remove_property( struct window *win, atom_t atom )
258 int i;
260 for (i = 0; i < win->prop_inuse; i++)
262 if (win->properties[i].type == PROP_TYPE_FREE) continue;
263 if (win->properties[i].atom == atom)
265 release_global_atom( NULL, atom );
266 win->properties[i].type = PROP_TYPE_FREE;
267 return win->properties[i].handle;
270 /* FIXME: last error? */
271 return 0;
274 /* find a window property */
275 static obj_handle_t get_property( struct window *win, atom_t atom )
277 int i;
279 for (i = 0; i < win->prop_inuse; i++)
281 if (win->properties[i].type == PROP_TYPE_FREE) continue;
282 if (win->properties[i].atom == atom) return win->properties[i].handle;
284 /* FIXME: last error? */
285 return 0;
288 /* destroy all properties of a window */
289 static inline void destroy_properties( struct window *win )
291 int i;
293 if (!win->properties) return;
294 for (i = 0; i < win->prop_inuse; i++)
296 if (win->properties[i].type == PROP_TYPE_FREE) continue;
297 release_global_atom( NULL, win->properties[i].atom );
299 free( win->properties );
302 /* detach a window from its owner thread but keep the window around */
303 static void detach_window_thread( struct window *win )
305 struct thread *thread = win->thread;
307 if (!thread) return;
308 if (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 );
314 assert( thread->desktop_users > 0 );
315 thread->desktop_users--;
316 release_class( win->class );
317 win->class = NULL;
319 /* don't hold a reference to the desktop so that the desktop window can be */
320 /* destroyed when the desktop ref count reaches zero */
321 release_object( win->desktop );
322 win->thread = NULL;
325 /* destroy a window */
326 void destroy_window( struct window *win )
328 /* destroy all children */
329 while (!list_empty(&win->children))
330 destroy_window( LIST_ENTRY( list_head(&win->children), struct window, entry ));
331 while (!list_empty(&win->unlinked))
332 destroy_window( LIST_ENTRY( list_head(&win->unlinked), struct window, entry ));
334 /* reset global window pointers, if the corresponding window is destroyed */
335 if (win == shell_window) shell_window = NULL;
336 if (win == shell_listview) shell_listview = NULL;
337 if (win == progman_window) progman_window = NULL;
338 if (win == taskman_window) taskman_window = NULL;
339 free_user_handle( win->handle );
340 destroy_properties( win );
341 list_remove( &win->entry );
342 if (is_desktop_window(win))
344 assert( win->desktop->top_window == win );
345 win->desktop->top_window = NULL;
347 detach_window_thread( win );
348 if (win->win_region) free_region( win->win_region );
349 if (win->update_region) free_region( win->update_region );
350 if (win->class) release_class( win->class );
351 free( win->text );
352 memset( win, 0x55, sizeof(*win) + win->nb_extra_bytes - 1 );
353 free( win );
356 /* get the process owning the top window of a given desktop */
357 struct process *get_top_window_owner( struct desktop *desktop )
359 struct window *win = desktop->top_window;
360 if (!win || !win->thread) return NULL;
361 return win->thread->process;
364 /* attempt to close the desktop window when the last process using it is gone */
365 void close_desktop_window( struct desktop *desktop )
367 struct window *win = desktop->top_window;
368 if (win && win->thread) post_message( win->handle, WM_CLOSE, 0, 0 );
371 /* create a new window structure (note: the window is not linked in the window tree) */
372 static struct window *create_window( struct window *parent, struct window *owner,
373 atom_t atom, void *instance )
375 int extra_bytes;
376 struct window *win;
377 struct desktop *desktop;
378 struct window_class *class;
380 if (!(desktop = get_thread_desktop( current, DESKTOP_CREATEWINDOW ))) return NULL;
382 if (!(class = grab_class( current->process, atom, instance, &extra_bytes )))
384 release_object( desktop );
385 return NULL;
388 if (!(win = mem_alloc( sizeof(*win) + extra_bytes - 1 ))) goto failed;
389 if (!(win->handle = alloc_user_handle( win, USER_WINDOW ))) goto failed;
391 win->parent = parent;
392 win->owner = owner ? owner->handle : 0;
393 win->thread = current;
394 win->desktop = desktop;
395 win->class = class;
396 win->atom = atom;
397 win->last_active = win->handle;
398 win->win_region = NULL;
399 win->update_region = NULL;
400 win->style = 0;
401 win->ex_style = 0;
402 win->id = 0;
403 win->instance = NULL;
404 win->is_unicode = 1;
405 win->user_data = 0;
406 win->text = NULL;
407 win->paint_flags = 0;
408 win->prop_inuse = 0;
409 win->prop_alloc = 0;
410 win->properties = NULL;
411 win->nb_extra_bytes = extra_bytes;
412 memset( win->extra_bytes, 0, extra_bytes );
413 list_init( &win->children );
414 list_init( &win->unlinked );
416 /* parent must be on the same desktop */
417 if (parent && parent->desktop != desktop)
419 set_error( STATUS_ACCESS_DENIED );
420 goto failed;
423 /* if no parent, class must be the desktop */
424 if (!parent && !is_desktop_class( class ))
426 set_error( STATUS_ACCESS_DENIED );
427 goto failed;
430 /* if parent belongs to a different thread and the window isn't */
431 /* top-level, attach the two threads */
432 if (parent && parent->thread && parent->thread != current && !is_desktop_window(parent))
434 if (!attach_thread_input( current, parent->thread )) goto failed;
436 else /* otherwise just make sure that the thread has a message queue */
438 if (!current->queue && !init_thread_queue( current )) goto failed;
441 /* put it on parent unlinked list */
442 if (parent) list_add_head( &parent->unlinked, &win->entry );
443 else
445 list_init( &win->entry );
446 assert( !desktop->top_window );
447 desktop->top_window = win;
448 set_process_default_desktop( current->process, desktop, current->desktop );
451 current->desktop_users++;
452 return win;
454 failed:
455 if (win)
457 if (win->handle) free_user_handle( win->handle );
458 free( win );
460 release_object( desktop );
461 release_class( class );
462 return NULL;
465 /* destroy all windows belonging to a given thread */
466 void destroy_thread_windows( struct thread *thread )
468 user_handle_t handle = 0;
469 struct window *win;
471 while ((win = next_user_handle( &handle, USER_WINDOW )))
473 if (win->thread != thread) continue;
474 if (is_desktop_window( win )) detach_window_thread( win );
475 else destroy_window( win );
479 /* get the desktop window */
480 static struct window *get_desktop_window( struct thread *thread, int create )
482 struct window *top_window;
483 struct desktop *desktop = get_thread_desktop( thread, 0 );
485 if (!desktop) return NULL;
487 if (!(top_window = desktop->top_window) && create)
489 if ((top_window = create_window( NULL, NULL, DESKTOP_ATOM, 0 )))
491 detach_window_thread( top_window );
492 top_window->style = WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
495 release_object( desktop );
496 return top_window;
499 /* check whether child is a descendant of parent */
500 int is_child_window( user_handle_t parent, user_handle_t child )
502 struct window *child_ptr = get_user_object( child, USER_WINDOW );
503 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
505 if (!child_ptr || !parent_ptr) return 0;
506 while (child_ptr->parent)
508 if (child_ptr->parent == parent_ptr) return 1;
509 child_ptr = child_ptr->parent;
511 return 0;
514 /* check whether window is a top-level window */
515 int is_top_level_window( user_handle_t window )
517 struct window *win = get_user_object( window, USER_WINDOW );
518 return (win && (is_desktop_window(win) || is_desktop_window(win->parent)));
521 /* make a window active if possible */
522 int make_window_active( user_handle_t window )
524 struct window *owner, *win = get_window( window );
526 if (!win) return 0;
528 /* set last active for window and its owner */
529 win->last_active = win->handle;
530 if ((owner = get_user_object( win->owner, USER_WINDOW ))) owner->last_active = win->handle;
531 return 1;
534 /* increment (or decrement) the window paint count */
535 static inline void inc_window_paint_count( struct window *win, int incr )
537 if (win->thread) inc_queue_paint_count( win->thread, incr );
540 /* check if window and all its ancestors are visible */
541 static int is_visible( const struct window *win )
543 while (win && win->parent)
545 if (!(win->style & WS_VISIBLE)) return 0;
546 win = win->parent;
547 /* if parent is minimized children are not visible */
548 if (win && (win->style & WS_MINIMIZE)) return 0;
550 return 1;
553 /* same as is_visible but takes a window handle */
554 int is_window_visible( user_handle_t window )
556 struct window *win = get_user_object( window, USER_WINDOW );
557 if (!win) return 0;
558 return is_visible( win );
561 /* check if point is inside the window */
562 static inline int is_point_in_window( struct window *win, int x, int y )
564 if (!(win->style & WS_VISIBLE)) return 0; /* not visible */
565 if ((win->style & (WS_POPUP|WS_CHILD|WS_DISABLED)) == (WS_CHILD|WS_DISABLED))
566 return 0; /* disabled child */
567 if ((win->ex_style & (WS_EX_LAYERED|WS_EX_TRANSPARENT)) == (WS_EX_LAYERED|WS_EX_TRANSPARENT))
568 return 0; /* transparent */
569 if (x < win->visible_rect.left || x >= win->visible_rect.right ||
570 y < win->visible_rect.top || y >= win->visible_rect.bottom)
571 return 0; /* not in window */
572 if (win->win_region &&
573 !point_in_region( win->win_region, x - win->window_rect.left, y - win->window_rect.top ))
574 return 0; /* not in window region */
575 return 1;
578 /* find child of 'parent' that contains the given point (in parent-relative coords) */
579 static struct window *child_window_from_point( struct window *parent, int x, int y )
581 struct window *ptr;
583 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
585 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
587 /* if window is minimized or disabled, return at once */
588 if (ptr->style & (WS_MINIMIZE|WS_DISABLED)) return ptr;
590 /* if point is not in client area, return at once */
591 if (x < ptr->client_rect.left || x >= ptr->client_rect.right ||
592 y < ptr->client_rect.top || y >= ptr->client_rect.bottom)
593 return ptr;
595 return child_window_from_point( ptr, x - ptr->client_rect.left, y - ptr->client_rect.top );
597 return parent; /* not found any child */
600 /* find all children of 'parent' that contain the given point */
601 static int get_window_children_from_point( struct window *parent, int x, int y,
602 struct user_handle_array *array )
604 struct window *ptr;
606 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
608 if (!is_point_in_window( ptr, x, y )) continue; /* skip it */
610 /* if point is in client area, and window is not minimized or disabled, check children */
611 if (!(ptr->style & (WS_MINIMIZE|WS_DISABLED)) &&
612 x >= ptr->client_rect.left && x < ptr->client_rect.right &&
613 y >= ptr->client_rect.top && y < ptr->client_rect.bottom)
615 if (!get_window_children_from_point( ptr, x - ptr->client_rect.left,
616 y - ptr->client_rect.top, array ))
617 return 0;
620 /* now add window to the array */
621 if (!add_handle_to_array( array, ptr->handle )) return 0;
623 return 1;
626 /* find window containing point (in absolute coords) */
627 user_handle_t window_from_point( struct desktop *desktop, int x, int y )
629 struct window *ret;
631 if (!desktop->top_window) return 0;
632 ret = child_window_from_point( desktop->top_window, x, y );
633 return ret->handle;
636 /* return list of all windows containing point (in absolute coords) */
637 static int all_windows_from_point( struct window *top, int x, int y, struct user_handle_array *array )
639 struct window *ptr;
641 /* make point relative to top window */
642 for (ptr = top->parent; ptr && !is_desktop_window(ptr); ptr = ptr->parent)
644 x -= ptr->client_rect.left;
645 y -= ptr->client_rect.top;
648 if (!is_point_in_window( top, x, y )) return 1;
650 /* if point is in client area, and window is not minimized or disabled, check children */
651 if (!(top->style & (WS_MINIMIZE|WS_DISABLED)) &&
652 x >= top->client_rect.left && x < top->client_rect.right &&
653 y >= top->client_rect.top && y < top->client_rect.bottom)
655 if (!is_desktop_window(top))
657 x -= top->client_rect.left;
658 y -= top->client_rect.top;
660 if (!get_window_children_from_point( top, x, y, array )) return 0;
662 /* now add window to the array */
663 if (!add_handle_to_array( array, top->handle )) return 0;
664 return 1;
668 /* return the thread owning a window */
669 struct thread *get_window_thread( user_handle_t handle )
671 struct window *win = get_user_object( handle, USER_WINDOW );
672 if (!win || !win->thread) return NULL;
673 return (struct thread *)grab_object( win->thread );
677 /* check if any area of a window needs repainting */
678 static inline int win_needs_repaint( struct window *win )
680 return win->update_region || (win->paint_flags & PAINT_INTERNAL);
684 /* find a child of the specified window that needs repainting */
685 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
687 struct window *ptr, *ret = NULL;
689 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
691 if (!(ptr->style & WS_VISIBLE)) continue;
692 if (ptr->thread == thread && win_needs_repaint( ptr ))
693 ret = ptr;
694 else if (!(ptr->style & WS_MINIMIZE)) /* explore its children */
695 ret = find_child_to_repaint( ptr, thread );
696 if (ret) break;
699 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
701 /* transparent window, check for non-transparent sibling to paint first */
702 for (ptr = get_next_window(ret); ptr; ptr = get_next_window(ptr))
704 if (!(ptr->style & WS_VISIBLE)) continue;
705 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
706 if (ptr->thread != thread) continue;
707 if (win_needs_repaint( ptr )) return ptr;
710 return ret;
714 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
715 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
717 struct window *ptr, *win, *top_window = get_desktop_window( thread, 0 );
719 if (!top_window) return 0;
721 if (top_window->thread == thread && win_needs_repaint( top_window )) win = top_window;
722 else win = find_child_to_repaint( top_window, thread );
724 if (win && parent)
726 /* check that it is a child of the specified parent */
727 for (ptr = win; ptr; ptr = ptr->parent)
728 if (ptr->handle == parent) break;
729 /* otherwise don't return any window, we don't repaint a child before its parent */
730 if (!ptr) win = NULL;
732 if (!win) return 0;
733 win->paint_flags &= ~PAINT_INTERNAL;
734 return win->handle;
738 /* intersect the window region with the specified region, relative to the window parent */
739 static struct region *intersect_window_region( struct region *region, struct window *win )
741 /* make region relative to window rect */
742 offset_region( region, -win->window_rect.left, -win->window_rect.top );
743 if (!intersect_region( region, region, win->win_region )) return NULL;
744 /* make region relative to parent again */
745 offset_region( region, win->window_rect.left, win->window_rect.top );
746 return region;
750 /* convert coordinates from client to screen coords */
751 static inline void client_to_screen( struct window *win, int *x, int *y )
753 for ( ; win && !is_desktop_window(win); win = win->parent)
755 *x += win->client_rect.left;
756 *y += win->client_rect.top;
760 /* convert coordinates from client to screen coords */
761 static inline void client_to_screen_rect( struct window *win, rectangle_t *rect )
763 for ( ; win && !is_desktop_window(win); win = win->parent)
765 rect->left += win->client_rect.left;
766 rect->right += win->client_rect.left;
767 rect->top += win->client_rect.top;
768 rect->bottom += win->client_rect.top;
772 /* map the region from window to screen coordinates */
773 static inline void map_win_region_to_screen( struct window *win, struct region *region )
775 if (!is_desktop_window(win))
777 int x = win->window_rect.left;
778 int y = win->window_rect.top;
779 client_to_screen( win->parent, &x, &y );
780 offset_region( region, x, y );
785 /* clip all children of a given window out of the visible region */
786 static struct region *clip_children( struct window *parent, struct window *last,
787 struct region *region, int offset_x, int offset_y )
789 struct window *ptr;
790 struct region *tmp = create_empty_region();
792 if (!tmp) return NULL;
793 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
795 if (ptr == last) break;
796 if (!(ptr->style & WS_VISIBLE)) continue;
797 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
798 set_region_rect( tmp, &ptr->visible_rect );
799 if (ptr->win_region && !intersect_window_region( tmp, ptr ))
801 free_region( tmp );
802 return NULL;
804 offset_region( tmp, offset_x, offset_y );
805 if (!(region = subtract_region( region, region, tmp ))) break;
806 if (is_region_empty( region )) break;
808 free_region( tmp );
809 return region;
813 /* compute the intersection of two rectangles; return 0 if the result is empty */
814 static inline int intersect_rect( rectangle_t *dst, const rectangle_t *src1, const rectangle_t *src2 )
816 dst->left = max( src1->left, src2->left );
817 dst->top = max( src1->top, src2->top );
818 dst->right = min( src1->right, src2->right );
819 dst->bottom = min( src1->bottom, src2->bottom );
820 return (dst->left < dst->right && dst->top < dst->bottom);
824 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
825 static void set_region_client_rect( struct region *region, struct window *win )
827 rectangle_t rect;
829 intersect_rect( &rect, &win->window_rect, &win->client_rect );
830 set_region_rect( region, &rect );
834 /* get the top-level window to clip against for a given window */
835 static inline struct window *get_top_clipping_window( struct window *win )
837 while (win->parent && !is_desktop_window(win->parent)) win = win->parent;
838 return win;
842 /* compute the visible region of a window, in window coordinates */
843 static struct region *get_visible_region( struct window *win, struct window *top, unsigned int flags )
845 struct region *tmp = NULL, *region;
846 int offset_x, offset_y;
848 if (!(region = create_empty_region())) return NULL;
850 /* first check if all ancestors are visible */
852 if (!is_visible( win )) return region; /* empty region */
854 /* create a region relative to the window itself */
856 if ((flags & DCX_PARENTCLIP) && win != top && win->parent)
858 set_region_client_rect( region, win->parent );
859 offset_region( region, -win->parent->client_rect.left, -win->parent->client_rect.top );
861 else if (flags & DCX_WINDOW)
863 set_region_rect( region, &win->visible_rect );
864 if (win->win_region && !intersect_window_region( region, win )) goto error;
866 else
868 set_region_client_rect( region, win );
869 if (win->win_region && !intersect_window_region( region, win )) goto error;
872 /* clip children */
874 if (flags & DCX_CLIPCHILDREN)
876 if (is_desktop_window(win)) offset_x = offset_y = 0;
877 else
879 offset_x = win->client_rect.left;
880 offset_y = win->client_rect.top;
882 if (!clip_children( win, NULL, region, offset_x, offset_y )) goto error;
885 /* clip siblings of ancestors */
887 if (is_desktop_window(win)) offset_x = offset_y = 0;
888 else
890 offset_x = win->window_rect.left;
891 offset_y = win->window_rect.top;
894 if (top && top != win && (tmp = create_empty_region()) != NULL)
896 while (win != top && win->parent)
898 if (win->style & WS_CLIPSIBLINGS)
900 if (!clip_children( win->parent, win, region, 0, 0 )) goto error;
901 if (is_region_empty( region )) break;
903 /* clip to parent client area */
904 win = win->parent;
905 if (!is_desktop_window(win))
907 offset_x += win->client_rect.left;
908 offset_y += win->client_rect.top;
909 offset_region( region, win->client_rect.left, win->client_rect.top );
911 set_region_client_rect( tmp, win );
912 if (win->win_region && !intersect_window_region( tmp, win )) goto error;
913 if (!intersect_region( region, region, tmp )) goto error;
914 if (is_region_empty( region )) break;
916 free_region( tmp );
918 offset_region( region, -offset_x, -offset_y ); /* make it relative to target window */
919 return region;
921 error:
922 if (tmp) free_region( tmp );
923 free_region( region );
924 return NULL;
928 /* get the window class of a window */
929 struct window_class* get_window_class( user_handle_t window )
931 struct window *win;
932 if (!(win = get_window( window ))) return NULL;
933 if (!win->class) set_error( STATUS_ACCESS_DENIED );
934 return win->class;
937 /* return a copy of the specified region cropped to the window client or frame rectangle, */
938 /* and converted from client to window coordinates. Helper for (in)validate_window. */
939 static struct region *crop_region_to_win_rect( struct window *win, struct region *region, int frame )
941 struct region *tmp = create_empty_region();
943 if (!tmp) return NULL;
945 /* get bounding rect in client coords */
946 if (frame) set_region_rect( tmp, &win->window_rect );
947 else set_region_client_rect( tmp, win );
948 if (!is_desktop_window(win))
949 offset_region( tmp, -win->client_rect.left, -win->client_rect.top );
951 /* intersect specified region with bounding rect */
952 if (region && !intersect_region( tmp, region, tmp )) goto done;
953 if (is_region_empty( tmp )) goto done;
955 /* map it to window coords */
956 offset_region( tmp, win->client_rect.left - win->window_rect.left,
957 win->client_rect.top - win->window_rect.top );
958 return tmp;
960 done:
961 free_region( tmp );
962 return NULL;
966 /* set a region as new update region for the window */
967 static void set_update_region( struct window *win, struct region *region )
969 if (region && !is_region_empty( region ))
971 if (!win->update_region) inc_window_paint_count( win, 1 );
972 else free_region( win->update_region );
973 win->update_region = region;
975 else
977 if (win->update_region)
979 inc_window_paint_count( win, -1 );
980 free_region( win->update_region );
982 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE | PAINT_NONCLIENT);
983 win->update_region = NULL;
984 if (region) free_region( region );
989 /* add a region to the update region; the passed region is freed or reused */
990 static int add_update_region( struct window *win, struct region *region )
992 if (win->update_region && !union_region( region, win->update_region, region ))
994 free_region( region );
995 return 0;
997 set_update_region( win, region );
998 return 1;
1002 /* validate the non client area of a window */
1003 static void validate_non_client( struct window *win )
1005 struct region *tmp;
1006 rectangle_t rect;
1008 if (!win->update_region) return; /* nothing to do */
1010 /* get client rect in window coords */
1011 rect.left = win->client_rect.left - win->window_rect.left;
1012 rect.top = win->client_rect.top - win->window_rect.top;
1013 rect.right = win->client_rect.right - win->window_rect.left;
1014 rect.bottom = win->client_rect.bottom - win->window_rect.top;
1016 if ((tmp = create_empty_region()))
1018 set_region_rect( tmp, &rect );
1019 if (intersect_region( tmp, win->update_region, tmp ))
1020 set_update_region( win, tmp );
1021 else
1022 free_region( tmp );
1024 win->paint_flags &= ~PAINT_NONCLIENT;
1028 /* validate a window completely so that we don't get any further paint messages for it */
1029 static void validate_whole_window( struct window *win )
1031 set_update_region( win, NULL );
1033 if (win->paint_flags & PAINT_INTERNAL)
1035 win->paint_flags &= ~PAINT_INTERNAL;
1036 inc_window_paint_count( win, -1 );
1041 /* validate a window's children so that we don't get any further paint messages for it */
1042 static void validate_children( struct window *win )
1044 struct window *child;
1046 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1048 if (!(child->style & WS_VISIBLE)) continue;
1049 validate_children(child);
1050 validate_whole_window(child);
1055 /* validate the update region of a window on all parents; helper for get_update_region */
1056 static void validate_parents( struct window *child )
1058 int offset_x = 0, offset_y = 0;
1059 struct window *win = child;
1060 struct region *tmp = NULL;
1062 if (!child->update_region) return;
1064 while (win->parent)
1066 /* map to parent client coords */
1067 offset_x += win->window_rect.left;
1068 offset_y += win->window_rect.top;
1070 win = win->parent;
1072 /* and now map to window coords */
1073 offset_x += win->client_rect.left - win->window_rect.left;
1074 offset_y += win->client_rect.top - win->window_rect.top;
1076 if (win->update_region && !(win->style & WS_CLIPCHILDREN))
1078 if (!tmp && !(tmp = create_empty_region())) return;
1079 offset_region( child->update_region, offset_x, offset_y );
1080 if (subtract_region( tmp, win->update_region, child->update_region ))
1082 set_update_region( win, tmp );
1083 tmp = NULL;
1085 /* restore child coords */
1086 offset_region( child->update_region, -offset_x, -offset_y );
1089 if (tmp) free_region( tmp );
1093 /* add/subtract a region (in client coordinates) to the update region of the window */
1094 static void redraw_window( struct window *win, struct region *region, int frame, unsigned int flags )
1096 struct region *tmp;
1097 struct window *child;
1099 if (flags & RDW_INVALIDATE)
1101 if (!(tmp = crop_region_to_win_rect( win, region, frame ))) return;
1103 if (!add_update_region( win, tmp )) return;
1105 if (flags & RDW_FRAME) win->paint_flags |= PAINT_NONCLIENT;
1106 if (flags & RDW_ERASE) win->paint_flags |= PAINT_ERASE;
1108 else if (flags & RDW_VALIDATE)
1110 if (!region && (flags & RDW_NOFRAME)) /* shortcut: validate everything */
1112 set_update_region( win, NULL );
1114 else if (win->update_region)
1116 if ((tmp = crop_region_to_win_rect( win, region, frame )))
1118 if (!subtract_region( tmp, win->update_region, tmp ))
1120 free_region( tmp );
1121 return;
1123 set_update_region( win, tmp );
1125 if (flags & RDW_NOFRAME) validate_non_client( win );
1126 if (flags & RDW_NOERASE) win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1130 if ((flags & RDW_INTERNALPAINT) && !(win->paint_flags & PAINT_INTERNAL))
1132 win->paint_flags |= PAINT_INTERNAL;
1133 inc_window_paint_count( win, 1 );
1135 else if ((flags & RDW_NOINTERNALPAINT) && (win->paint_flags & PAINT_INTERNAL))
1137 win->paint_flags &= ~PAINT_INTERNAL;
1138 inc_window_paint_count( win, -1 );
1141 /* now process children recursively */
1143 if (flags & RDW_NOCHILDREN) return;
1144 if (win->style & WS_MINIMIZE) return;
1145 if ((win->style & WS_CLIPCHILDREN) && !(flags & RDW_ALLCHILDREN)) return;
1147 if (!(tmp = crop_region_to_win_rect( win, region, 0 ))) return;
1149 /* map to client coordinates */
1150 offset_region( tmp, win->window_rect.left - win->client_rect.left,
1151 win->window_rect.top - win->client_rect.top );
1153 if (flags & RDW_INVALIDATE) flags |= RDW_FRAME | RDW_ERASE;
1155 LIST_FOR_EACH_ENTRY( child, &win->children, struct window, entry )
1157 if (!(child->style & WS_VISIBLE)) continue;
1158 if (!rect_in_region( tmp, &child->window_rect )) continue;
1159 offset_region( tmp, -child->client_rect.left, -child->client_rect.top );
1160 redraw_window( child, tmp, 1, flags );
1161 offset_region( tmp, child->client_rect.left, child->client_rect.top );
1163 free_region( tmp );
1167 /* retrieve the update flags for a window depending on the state of the update region */
1168 static unsigned int get_update_flags( struct window *win, unsigned int flags )
1170 unsigned int ret = 0;
1172 if (flags & UPDATE_NONCLIENT)
1174 if ((win->paint_flags & PAINT_NONCLIENT) && win->update_region) ret |= UPDATE_NONCLIENT;
1176 if (flags & UPDATE_ERASE)
1178 if ((win->paint_flags & PAINT_ERASE) && win->update_region) ret |= UPDATE_ERASE;
1180 if (flags & UPDATE_PAINT)
1182 if (win->update_region)
1184 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1185 ret |= UPDATE_PAINT;
1188 if (flags & UPDATE_INTERNALPAINT)
1190 if (win->paint_flags & PAINT_INTERNAL)
1192 ret |= UPDATE_INTERNALPAINT;
1193 if (win->paint_flags & PAINT_DELAYED_ERASE) ret |= UPDATE_DELAYED_ERASE;
1196 return ret;
1200 /* iterate through the children of the given window until we find one with some update flags */
1201 static unsigned int get_child_update_flags( struct window *win, struct window *from_child,
1202 unsigned int flags, struct window **child )
1204 struct window *ptr;
1205 unsigned int ret = 0;
1207 /* first make sure we want to iterate children at all */
1209 if (win->style & WS_MINIMIZE) return 0;
1211 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1212 * here we only want to repaint children of windows that clip them, others
1213 * need to wait for WM_PAINT to be done in the parent first.
1215 if (!(flags & UPDATE_ALLCHILDREN) && !(win->style & WS_CLIPCHILDREN)) return 0;
1217 LIST_FOR_EACH_ENTRY( ptr, &win->children, struct window, entry )
1219 if (from_child) /* skip all children until from_child is found */
1221 if (ptr == from_child) from_child = NULL;
1222 continue;
1224 if (!(ptr->style & WS_VISIBLE)) continue;
1225 if ((ret = get_update_flags( ptr, flags )) != 0)
1227 *child = ptr;
1228 break;
1230 if ((ret = get_child_update_flags( ptr, NULL, flags, child ))) break;
1232 return ret;
1235 /* iterate through children and siblings of the given window until we find one with some update flags */
1236 static unsigned int get_window_update_flags( struct window *win, struct window *from_child,
1237 unsigned int flags, struct window **child )
1239 unsigned int ret;
1240 struct window *ptr, *from_sibling = NULL;
1242 /* if some parent is not visible start from the next sibling */
1244 if (!is_visible( win )) return 0;
1245 for (ptr = from_child; ptr; ptr = ptr->parent)
1247 if (!(ptr->style & WS_VISIBLE) || (ptr->style & WS_MINIMIZE)) from_sibling = ptr;
1248 if (ptr == win) break;
1251 /* non-client painting must be delayed if one of the parents is going to
1252 * be repainted and doesn't clip children */
1254 if ((flags & UPDATE_NONCLIENT) && !(flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)))
1256 for (ptr = win->parent; ptr; ptr = ptr->parent)
1258 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr ))
1259 return 0;
1261 if (from_child && !(flags & UPDATE_ALLCHILDREN))
1263 for (ptr = from_sibling ? from_sibling : from_child; ptr; ptr = ptr->parent)
1265 if (!(ptr->style & WS_CLIPCHILDREN) && win_needs_repaint( ptr )) from_sibling = ptr;
1266 if (ptr == win) break;
1272 /* check window itself (only if not restarting from a child) */
1274 if (!from_child)
1276 if ((ret = get_update_flags( win, flags )))
1278 *child = win;
1279 return ret;
1281 from_child = win;
1284 /* now check children */
1286 if (flags & UPDATE_NOCHILDREN) return 0;
1287 if (!from_sibling)
1289 if ((ret = get_child_update_flags( from_child, NULL, flags, child ))) return ret;
1290 from_sibling = from_child;
1293 /* then check siblings and parent siblings */
1295 while (from_sibling->parent && from_sibling != win)
1297 if ((ret = get_child_update_flags( from_sibling->parent, from_sibling, flags, child )))
1298 return ret;
1299 from_sibling = from_sibling->parent;
1301 return 0;
1305 /* expose a region of a window on its parent */
1306 /* the region is in window coordinates */
1307 static void expose_window( struct window *win, struct region *region )
1309 struct window *parent = win;
1310 int offset_x = win->window_rect.left - win->client_rect.left;
1311 int offset_y = win->window_rect.top - win->client_rect.top;
1313 if (win->parent && !is_desktop_window(win->parent))
1315 offset_x += win->client_rect.left;
1316 offset_y += win->client_rect.top;
1317 parent = win->parent;
1319 offset_region( region, offset_x, offset_y );
1320 redraw_window( parent, region, 0, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1321 offset_region( region, -offset_x, -offset_y );
1325 /* set the window and client rectangles, updating the update region if necessary */
1326 static void set_window_pos( struct window *win, struct window *previous,
1327 unsigned int swp_flags, const rectangle_t *window_rect,
1328 const rectangle_t *client_rect, const rectangle_t *visible_rect,
1329 const rectangle_t *valid_rects )
1331 struct region *old_vis_rgn = NULL, *new_vis_rgn;
1332 const rectangle_t old_window_rect = win->window_rect;
1333 const rectangle_t old_visible_rect = win->visible_rect;
1334 const rectangle_t old_client_rect = win->client_rect;
1335 struct window *top = get_top_clipping_window( win );
1336 int visible = (win->style & WS_VISIBLE) || (swp_flags & SWP_SHOWWINDOW);
1338 if (win->parent && !is_visible( win->parent )) visible = 0;
1340 if (visible && !(old_vis_rgn = get_visible_region( win, top, DCX_WINDOW ))) return;
1342 /* set the new window info before invalidating anything */
1344 win->window_rect = *window_rect;
1345 win->visible_rect = *visible_rect;
1346 win->client_rect = *client_rect;
1347 if (!(swp_flags & SWP_NOZORDER) && win->parent)
1349 list_remove( &win->entry ); /* unlink it from the previous location */
1350 if (previous) list_add_after( &previous->entry, &win->entry );
1351 else list_add_head( &win->parent->children, &win->entry );
1353 if (swp_flags & SWP_SHOWWINDOW) win->style |= WS_VISIBLE;
1354 else if (swp_flags & SWP_HIDEWINDOW) win->style &= ~WS_VISIBLE;
1356 /* if the window is not visible, everything is easy */
1357 if (!visible) return;
1359 if (!(new_vis_rgn = get_visible_region( win, top, DCX_WINDOW )))
1361 free_region( old_vis_rgn );
1362 clear_error(); /* ignore error since the window info has been modified already */
1363 return;
1366 /* expose anything revealed by the change */
1368 if (!(swp_flags & SWP_NOREDRAW))
1370 offset_region( old_vis_rgn, old_window_rect.left - window_rect->left,
1371 old_window_rect.top - window_rect->top );
1372 if (xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1373 expose_window( win, new_vis_rgn );
1375 free_region( old_vis_rgn );
1377 if (!(win->style & WS_VISIBLE))
1379 /* clear the update region since the window is no longer visible */
1380 validate_whole_window( win );
1381 validate_children( win );
1382 goto done;
1385 /* crop update region to the new window rect */
1387 if (win->update_region &&
1388 (window_rect->right - window_rect->left < old_window_rect.right - old_window_rect.left ||
1389 window_rect->bottom - window_rect->top < old_window_rect.bottom - old_window_rect.top))
1391 struct region *tmp = create_empty_region();
1392 if (tmp)
1394 set_region_rect( tmp, window_rect );
1395 if (!is_desktop_window(win))
1396 offset_region( tmp, -window_rect->left, -window_rect->top );
1397 if (intersect_region( tmp, win->update_region, tmp ))
1398 set_update_region( win, tmp );
1399 else
1400 free_region( tmp );
1404 if (swp_flags & SWP_NOREDRAW) goto done; /* do not repaint anything */
1406 /* expose the whole non-client area if it changed in any way */
1408 if ((swp_flags & SWP_FRAMECHANGED) ||
1409 memcmp( window_rect, &old_window_rect, sizeof(old_window_rect) ) ||
1410 memcmp( visible_rect, &old_visible_rect, sizeof(old_visible_rect) ) ||
1411 memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1413 struct region *tmp = create_empty_region();
1415 if (tmp)
1417 /* subtract the valid portion of client rect from the total region */
1418 if (!memcmp( client_rect, &old_client_rect, sizeof(old_client_rect) ))
1419 set_region_rect( tmp, client_rect );
1420 else if (valid_rects)
1421 set_region_rect( tmp, &valid_rects[0] );
1423 set_region_rect( new_vis_rgn, window_rect );
1424 if (subtract_region( tmp, new_vis_rgn, tmp ))
1426 if (!is_desktop_window(win))
1427 offset_region( tmp, -client_rect->left, -client_rect->top );
1428 redraw_window( win, tmp, 1, RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
1430 free_region( tmp );
1434 done:
1435 free_region( new_vis_rgn );
1436 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1440 /* set the window region, updating the update region if necessary */
1441 static void set_window_region( struct window *win, struct region *region, int redraw )
1443 struct region *old_vis_rgn = NULL, *new_vis_rgn;
1444 struct window *top = get_top_clipping_window( win );
1446 /* no need to redraw if window is not visible */
1447 if (redraw && !is_visible( win )) redraw = 0;
1449 if (redraw) old_vis_rgn = get_visible_region( win, top, DCX_WINDOW );
1451 if (win->win_region) free_region( win->win_region );
1452 win->win_region = region;
1454 if (old_vis_rgn && (new_vis_rgn = get_visible_region( win, top, DCX_WINDOW )))
1456 /* expose anything revealed by the change */
1457 if (xor_region( new_vis_rgn, old_vis_rgn, new_vis_rgn ))
1458 expose_window( win, new_vis_rgn );
1459 free_region( new_vis_rgn );
1462 if (old_vis_rgn) free_region( old_vis_rgn );
1463 clear_error(); /* we ignore out of memory errors since the region has been set */
1467 /* create a window */
1468 DECL_HANDLER(create_window)
1470 struct window *win, *parent, *owner = NULL;
1472 reply->handle = 0;
1474 if (!req->parent) parent = get_desktop_window( current, 0 );
1475 else if (!(parent = get_window( req->parent ))) return;
1477 if (req->owner)
1479 if (!(owner = get_window( req->owner ))) return;
1480 if (is_desktop_window(owner)) owner = NULL;
1481 else if (!is_desktop_window(parent))
1483 /* an owned window must be created as top-level */
1484 set_error( STATUS_ACCESS_DENIED );
1485 return;
1487 else /* owner must be a top-level window */
1488 while (!is_desktop_window(owner->parent)) owner = owner->parent;
1490 if (!(win = create_window( parent, owner, req->atom, req->instance ))) return;
1492 reply->handle = win->handle;
1493 reply->parent = win->parent ? win->parent->handle : 0;
1494 reply->owner = win->owner;
1495 reply->extra = win->nb_extra_bytes;
1496 reply->class_ptr = get_class_client_ptr( win->class );
1500 /* set the parent of a window */
1501 DECL_HANDLER(set_parent)
1503 struct window *win, *parent = NULL;
1505 if (!(win = get_window( req->handle ))) return;
1506 if (req->parent && !(parent = get_window( req->parent ))) return;
1508 if (is_desktop_window(win))
1510 set_error( STATUS_INVALID_PARAMETER );
1511 return;
1513 reply->old_parent = win->parent->handle;
1514 reply->full_parent = parent ? parent->handle : 0;
1515 set_parent_window( win, parent );
1519 /* destroy a window */
1520 DECL_HANDLER(destroy_window)
1522 struct window *win = get_window( req->handle );
1523 if (win)
1525 if (!is_desktop_window(win)) destroy_window( win );
1526 else if (win->thread == current) detach_window_thread( win );
1527 else set_error( STATUS_ACCESS_DENIED );
1532 /* retrieve the desktop window for the current thread */
1533 DECL_HANDLER(get_desktop_window)
1535 struct window *win = get_desktop_window( current, req->force );
1537 if (win) reply->handle = win->handle;
1541 /* set a window owner */
1542 DECL_HANDLER(set_window_owner)
1544 struct window *win = get_window( req->handle );
1545 struct window *owner = NULL;
1547 if (!win) return;
1548 if (req->owner && !(owner = get_window( req->owner ))) return;
1549 if (is_desktop_window(win))
1551 set_error( STATUS_ACCESS_DENIED );
1552 return;
1554 reply->prev_owner = win->owner;
1555 reply->full_owner = win->owner = owner ? owner->handle : 0;
1559 /* get information from a window handle */
1560 DECL_HANDLER(get_window_info)
1562 struct window *win = get_window( req->handle );
1564 reply->full_handle = 0;
1565 reply->tid = reply->pid = 0;
1566 if (win)
1568 reply->full_handle = win->handle;
1569 reply->last_active = win->handle;
1570 reply->is_unicode = win->is_unicode;
1571 if (get_user_object( win->last_active, USER_WINDOW )) reply->last_active = win->last_active;
1572 if (win->thread)
1574 reply->tid = get_thread_id( win->thread );
1575 reply->pid = get_process_id( win->thread->process );
1576 reply->atom = win->class ? get_class_atom( win->class ) : DESKTOP_ATOM;
1582 /* set some information in a window */
1583 DECL_HANDLER(set_window_info)
1585 struct window *win = get_window( req->handle );
1587 if (!win) return;
1588 if (req->flags && is_desktop_window(win) && win->thread != current)
1590 set_error( STATUS_ACCESS_DENIED );
1591 return;
1593 if (req->extra_size > sizeof(req->extra_value) ||
1594 req->extra_offset < -1 ||
1595 req->extra_offset > win->nb_extra_bytes - (int)req->extra_size)
1597 set_win32_error( ERROR_INVALID_INDEX );
1598 return;
1600 if (req->extra_offset != -1)
1602 memcpy( &reply->old_extra_value, win->extra_bytes + req->extra_offset, req->extra_size );
1604 else if (req->flags & SET_WIN_EXTRA)
1606 set_win32_error( ERROR_INVALID_INDEX );
1607 return;
1609 reply->old_style = win->style;
1610 reply->old_ex_style = win->ex_style;
1611 reply->old_id = win->id;
1612 reply->old_instance = win->instance;
1613 reply->old_user_data = win->user_data;
1614 if (req->flags & SET_WIN_STYLE) win->style = req->style;
1615 if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
1616 if (req->flags & SET_WIN_ID) win->id = req->id;
1617 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
1618 if (req->flags & SET_WIN_UNICODE) win->is_unicode = req->is_unicode;
1619 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
1620 if (req->flags & SET_WIN_EXTRA) memcpy( win->extra_bytes + req->extra_offset,
1621 &req->extra_value, req->extra_size );
1623 /* changing window style triggers a non-client paint */
1624 if (req->flags & SET_WIN_STYLE) win->paint_flags |= PAINT_NONCLIENT;
1628 /* get a list of the window parents, up to the root of the tree */
1629 DECL_HANDLER(get_window_parents)
1631 struct window *ptr, *win = get_window( req->handle );
1632 int total = 0;
1633 user_handle_t *data;
1634 data_size_t len;
1636 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
1638 reply->count = total;
1639 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1640 if (len && ((data = set_reply_data_size( len ))))
1642 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
1643 *data++ = ptr->handle;
1648 /* get a list of the window children */
1649 DECL_HANDLER(get_window_children)
1651 struct window *ptr, *parent = get_window( req->parent );
1652 int total = 0;
1653 user_handle_t *data;
1654 data_size_t len;
1656 if (parent)
1658 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
1660 if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1661 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1662 total++;
1665 reply->count = total;
1666 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
1667 if (len && ((data = set_reply_data_size( len ))))
1669 LIST_FOR_EACH_ENTRY( ptr, &parent->children, struct window, entry )
1671 if (len < sizeof(*data)) break;
1672 if (req->atom && get_class_atom(ptr->class) != req->atom) continue;
1673 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
1674 *data++ = ptr->handle;
1675 len -= sizeof(*data);
1681 /* get a list of the window children that contain a given point */
1682 DECL_HANDLER(get_window_children_from_point)
1684 struct user_handle_array array;
1685 struct window *parent = get_window( req->parent );
1686 data_size_t len;
1688 if (!parent) return;
1690 array.handles = NULL;
1691 array.count = 0;
1692 array.total = 0;
1693 if (!all_windows_from_point( parent, req->x, req->y, &array )) return;
1695 reply->count = array.count;
1696 len = min( get_reply_max_size(), array.count * sizeof(user_handle_t) );
1697 if (len) set_reply_data_ptr( array.handles, len );
1698 else free( array.handles );
1702 /* get window tree information from a window handle */
1703 DECL_HANDLER(get_window_tree)
1705 struct window *ptr, *win = get_window( req->handle );
1707 if (!win) return;
1709 reply->parent = 0;
1710 reply->owner = 0;
1711 reply->next_sibling = 0;
1712 reply->prev_sibling = 0;
1713 reply->first_sibling = 0;
1714 reply->last_sibling = 0;
1715 reply->first_child = 0;
1716 reply->last_child = 0;
1718 if (win->parent)
1720 struct window *parent = win->parent;
1721 reply->parent = parent->handle;
1722 reply->owner = win->owner;
1723 if ((ptr = get_next_window( win ))) reply->next_sibling = ptr->handle;
1724 if ((ptr = get_prev_window( win ))) reply->prev_sibling = ptr->handle;
1725 if ((ptr = get_first_child( parent ))) reply->first_sibling = ptr->handle;
1726 if ((ptr = get_last_child( parent ))) reply->last_sibling = ptr->handle;
1728 if ((ptr = get_first_child( win ))) reply->first_child = ptr->handle;
1729 if ((ptr = get_last_child( win ))) reply->last_child = ptr->handle;
1733 /* set the position and Z order of a window */
1734 DECL_HANDLER(set_window_pos)
1736 const rectangle_t *visible_rect = NULL, *valid_rects = NULL;
1737 struct window *previous = NULL;
1738 struct window *win = get_window( req->handle );
1739 unsigned int flags = req->flags;
1741 if (!win) return;
1742 if (!win->parent) flags |= SWP_NOZORDER; /* no Z order for the desktop */
1744 if (!(flags & SWP_NOZORDER))
1746 if (!req->previous) /* special case: HWND_TOP */
1748 if (get_first_child(win->parent) == win) flags |= SWP_NOZORDER;
1750 else if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
1752 previous = get_last_child( win->parent );
1754 else
1756 if (!(previous = get_window( req->previous ))) return;
1757 /* previous must be a sibling */
1758 if (previous->parent != win->parent)
1760 set_error( STATUS_INVALID_PARAMETER );
1761 return;
1764 if (previous == win) flags |= SWP_NOZORDER; /* nothing to do */
1767 /* window rectangle must be ordered properly */
1768 if (req->window.right < req->window.left || req->window.bottom < req->window.top)
1770 set_error( STATUS_INVALID_PARAMETER );
1771 return;
1774 if (get_req_data_size() >= sizeof(rectangle_t)) visible_rect = get_req_data();
1775 if (get_req_data_size() >= 3 * sizeof(rectangle_t)) valid_rects = visible_rect + 1;
1777 if (!visible_rect) visible_rect = &req->window;
1778 set_window_pos( win, previous, flags, &req->window, &req->client, visible_rect, valid_rects );
1779 reply->new_style = win->style;
1783 /* get the window and client rectangles of a window */
1784 DECL_HANDLER(get_window_rectangles)
1786 struct window *win = get_window( req->handle );
1788 if (win)
1790 reply->window = win->window_rect;
1791 reply->visible = win->visible_rect;
1792 reply->client = win->client_rect;
1797 /* get the window text */
1798 DECL_HANDLER(get_window_text)
1800 struct window *win = get_window( req->handle );
1802 if (win && win->text)
1804 data_size_t len = strlenW( win->text ) * sizeof(WCHAR);
1805 if (len > get_reply_max_size()) len = get_reply_max_size();
1806 set_reply_data( win->text, len );
1811 /* set the window text */
1812 DECL_HANDLER(set_window_text)
1814 struct window *win = get_window( req->handle );
1816 if (win)
1818 WCHAR *text = NULL;
1819 data_size_t len = get_req_data_size() / sizeof(WCHAR);
1820 if (len)
1822 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
1823 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
1824 text[len] = 0;
1826 free( win->text );
1827 win->text = text;
1832 /* get the coordinates offset between two windows */
1833 DECL_HANDLER(get_windows_offset)
1835 struct window *win;
1837 reply->x = reply->y = 0;
1838 if (req->from)
1840 if (!(win = get_window( req->from ))) return;
1841 while (win && !is_desktop_window(win))
1843 reply->x += win->client_rect.left;
1844 reply->y += win->client_rect.top;
1845 win = win->parent;
1848 if (req->to)
1850 if (!(win = get_window( req->to ))) return;
1851 while (win && !is_desktop_window(win))
1853 reply->x -= win->client_rect.left;
1854 reply->y -= win->client_rect.top;
1855 win = win->parent;
1861 /* get the visible region of a window */
1862 DECL_HANDLER(get_visible_region)
1864 struct region *region;
1865 struct window *top, *win = get_window( req->window );
1867 if (!win) return;
1869 top = get_top_clipping_window( win );
1870 if ((region = get_visible_region( win, top, req->flags )))
1872 rectangle_t *data;
1873 map_win_region_to_screen( win, region );
1874 data = get_region_data_and_free( region, get_reply_max_size(), &reply->total_size );
1875 if (data) set_reply_data_ptr( data, reply->total_size );
1877 reply->top_win = top->handle;
1878 reply->top_rect = top->visible_rect;
1880 if (!is_desktop_window(win))
1882 reply->win_rect = (req->flags & DCX_WINDOW) ? win->window_rect : win->client_rect;
1883 client_to_screen_rect( top->parent, &reply->top_rect );
1884 client_to_screen_rect( win->parent, &reply->win_rect );
1886 else
1888 reply->win_rect.left = 0;
1889 reply->win_rect.top = 0;
1890 reply->win_rect.right = win->client_rect.right - win->client_rect.left;
1891 reply->win_rect.bottom = win->client_rect.bottom - win->client_rect.top;
1896 /* get the window region */
1897 DECL_HANDLER(get_window_region)
1899 struct window *win = get_window( req->window );
1901 if (!win) return;
1903 if (win->win_region)
1905 rectangle_t *data = get_region_data( win->win_region, get_reply_max_size(), &reply->total_size );
1906 if (data) set_reply_data_ptr( data, reply->total_size );
1911 /* set the window region */
1912 DECL_HANDLER(set_window_region)
1914 struct region *region = NULL;
1915 struct window *win = get_window( req->window );
1917 if (!win) return;
1919 if (get_req_data_size()) /* no data means remove the region completely */
1921 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
1922 return;
1924 set_window_region( win, region, req->redraw );
1928 /* get a window update region */
1929 DECL_HANDLER(get_update_region)
1931 rectangle_t *data;
1932 unsigned int flags = req->flags;
1933 struct window *from_child = NULL;
1934 struct window *win = get_window( req->window );
1936 reply->flags = 0;
1937 if (!win) return;
1939 if (req->from_child)
1941 struct window *ptr;
1943 if (!(from_child = get_window( req->from_child ))) return;
1945 /* make sure from_child is a child of win */
1946 ptr = from_child;
1947 while (ptr && ptr != win) ptr = ptr->parent;
1948 if (!ptr)
1950 set_error( STATUS_INVALID_PARAMETER );
1951 return;
1955 if (flags & UPDATE_DELAYED_ERASE) /* this means that the previous call didn't erase */
1957 if (from_child) from_child->paint_flags |= PAINT_DELAYED_ERASE;
1958 else win->paint_flags |= PAINT_DELAYED_ERASE;
1961 reply->flags = get_window_update_flags( win, from_child, flags, &win );
1962 reply->child = win->handle;
1964 if (flags & UPDATE_NOREGION) return;
1966 if (win->update_region)
1968 /* convert update region to screen coordinates */
1969 struct region *region = create_empty_region();
1971 if (!region) return;
1972 if (!copy_region( region, win->update_region ))
1974 free_region( region );
1975 return;
1977 map_win_region_to_screen( win, region );
1978 if (!(data = get_region_data_and_free( region, get_reply_max_size(),
1979 &reply->total_size ))) return;
1980 set_reply_data_ptr( data, reply->total_size );
1983 if (reply->flags & (UPDATE_PAINT|UPDATE_INTERNALPAINT)) /* validate everything */
1985 validate_parents( win );
1986 validate_whole_window( win );
1988 else
1990 if (reply->flags & UPDATE_NONCLIENT) validate_non_client( win );
1991 if (reply->flags & UPDATE_ERASE)
1993 win->paint_flags &= ~(PAINT_ERASE | PAINT_DELAYED_ERASE);
1994 /* desktop window only gets erased, not repainted */
1995 if (is_desktop_window(win)) validate_whole_window( win );
2001 /* update the z order of a window so that a given rectangle is fully visible */
2002 DECL_HANDLER(update_window_zorder)
2004 rectangle_t tmp;
2005 struct window *ptr, *win = get_window( req->window );
2007 if (!win || !win->parent || !is_visible( win )) return; /* nothing to do */
2009 LIST_FOR_EACH_ENTRY( ptr, &win->parent->children, struct window, entry )
2011 if (ptr == win) break;
2012 if (!(ptr->style & WS_VISIBLE)) continue;
2013 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
2014 if (!intersect_rect( &tmp, &ptr->visible_rect, &req->rect )) continue;
2015 if (ptr->win_region && !rect_in_region( ptr->win_region, &req->rect )) continue;
2016 /* found a window obscuring the rectangle, now move win above this one */
2017 list_remove( &win->entry );
2018 list_add_before( &ptr->entry, &win->entry );
2019 break;
2024 /* mark parts of a window as needing a redraw */
2025 DECL_HANDLER(redraw_window)
2027 struct region *region = NULL;
2028 struct window *win = get_window( req->window );
2030 if (!win) return;
2031 if (!is_visible( win )) return; /* nothing to do */
2033 if (req->flags & (RDW_VALIDATE|RDW_INVALIDATE))
2035 if (get_req_data_size()) /* no data means whole rectangle */
2037 if (!(region = create_region_from_req_data( get_req_data(), get_req_data_size() )))
2038 return;
2042 redraw_window( win, region, (req->flags & RDW_INVALIDATE) && (req->flags & RDW_FRAME),
2043 req->flags );
2044 if (region) free_region( region );
2048 /* set a window property */
2049 DECL_HANDLER(set_window_property)
2051 struct window *win = get_window( req->window );
2053 if (!win) return;
2055 if (get_req_data_size())
2057 atom_t atom = add_global_atom( NULL, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
2058 if (atom)
2060 set_property( win, atom, req->handle, PROP_TYPE_STRING );
2061 release_global_atom( NULL, atom );
2064 else set_property( win, req->atom, req->handle, PROP_TYPE_ATOM );
2068 /* remove a window property */
2069 DECL_HANDLER(remove_window_property)
2071 struct window *win = get_window( req->window );
2073 if (win)
2075 atom_t atom = req->atom;
2076 if (get_req_data_size()) atom = find_global_atom( NULL, get_req_data(),
2077 get_req_data_size() / sizeof(WCHAR) );
2078 if (atom) reply->handle = remove_property( win, atom );
2083 /* get a window property */
2084 DECL_HANDLER(get_window_property)
2086 struct window *win = get_window( req->window );
2088 if (win)
2090 atom_t atom = req->atom;
2091 if (get_req_data_size()) atom = find_global_atom( NULL, get_req_data(),
2092 get_req_data_size() / sizeof(WCHAR) );
2093 if (atom) reply->handle = get_property( win, atom );
2098 /* get the list of properties of a window */
2099 DECL_HANDLER(get_window_properties)
2101 property_data_t *data;
2102 int i, count, max = get_reply_max_size() / sizeof(*data);
2103 struct window *win = get_window( req->window );
2105 reply->total = 0;
2106 if (!win) return;
2108 for (i = count = 0; i < win->prop_inuse; i++)
2109 if (win->properties[i].type != PROP_TYPE_FREE) count++;
2110 reply->total = count;
2112 if (count > max) count = max;
2113 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
2115 for (i = 0; i < win->prop_inuse && count; i++)
2117 if (win->properties[i].type == PROP_TYPE_FREE) continue;
2118 data->atom = win->properties[i].atom;
2119 data->string = (win->properties[i].type == PROP_TYPE_STRING);
2120 data->handle = win->properties[i].handle;
2121 data++;
2122 count--;
2127 /* get the new window pointer for a global window, checking permissions */
2128 /* helper for set_global_windows request */
2129 static int get_new_global_window( struct window **win, user_handle_t handle )
2131 if (!handle)
2133 *win = NULL;
2134 return 1;
2136 else if (*win)
2138 set_error( STATUS_ACCESS_DENIED );
2139 return 0;
2141 *win = get_window( handle );
2142 return (*win != NULL);
2145 /* Set/get the global windows */
2146 DECL_HANDLER(set_global_windows)
2148 struct window *new_shell_window = shell_window;
2149 struct window *new_shell_listview = shell_listview;
2150 struct window *new_progman_window = progman_window;
2151 struct window *new_taskman_window = taskman_window;
2153 reply->old_shell_window = shell_window ? shell_window->handle : 0;
2154 reply->old_shell_listview = shell_listview ? shell_listview->handle : 0;
2155 reply->old_progman_window = progman_window ? progman_window->handle : 0;
2156 reply->old_taskman_window = taskman_window ? taskman_window->handle : 0;
2158 if (req->flags & SET_GLOBAL_SHELL_WINDOWS)
2160 if (!get_new_global_window( &new_shell_window, req->shell_window )) return;
2161 if (!get_new_global_window( &new_shell_listview, req->shell_listview )) return;
2163 if (req->flags & SET_GLOBAL_PROGMAN_WINDOW)
2165 if (!get_new_global_window( &new_progman_window, req->progman_window )) return;
2167 if (req->flags & SET_GLOBAL_TASKMAN_WINDOW)
2169 if (!get_new_global_window( &new_taskman_window, req->taskman_window )) return;
2171 shell_window = new_shell_window;
2172 shell_listview = new_shell_listview;
2173 progman_window = new_progman_window;
2174 taskman_window = new_taskman_window;