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
22 #include "wine/port.h"
28 #define WIN32_NO_STATUS
42 /* a window 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) */
52 PROP_TYPE_FREE
, /* free entry */
53 PROP_TYPE_STRING
, /* atom that was originally a string */
54 PROP_TYPE_ATOM
/* plain atom */
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 */
95 /* growable array of user handles */
96 struct user_handle_array
98 user_handle_t
*handles
;
103 /* global window pointers */
104 static struct window
*shell_window
;
105 static struct window
*shell_listview
;
106 static struct window
*progman_window
;
107 static struct window
*taskman_window
;
109 /* retrieve a pointer to a window from its handle */
110 inline static struct window
*get_window( user_handle_t handle
)
112 struct window
*ret
= get_user_object( handle
, USER_WINDOW
);
113 if (!ret
) set_win32_error( ERROR_INVALID_WINDOW_HANDLE
);
117 /* check if window is the desktop */
118 static inline int is_desktop_window( const struct window
*win
)
120 return !win
->parent
; /* only desktop windows have no parent */
123 /* change the parent of a window (or unlink the window if the new parent is NULL) */
124 static int set_parent_window( struct window
*win
, struct window
*parent
)
128 /* make sure parent is not a child of window */
129 for (ptr
= parent
; ptr
; ptr
= ptr
->parent
)
133 set_error( STATUS_INVALID_PARAMETER
);
138 list_remove( &win
->entry
); /* unlink it from the previous location */
142 win
->parent
= parent
;
143 list_add_head( &parent
->children
, &win
->entry
);
145 /* if parent belongs to a different thread and the window isn't */
146 /* top-level, attach the two threads */
147 if (parent
->thread
&& parent
->thread
!= win
->thread
&& !is_desktop_window(parent
))
148 attach_thread_input( win
->thread
, parent
->thread
);
150 else /* move it to parent unlinked list */
152 list_add_head( &win
->parent
->unlinked
, &win
->entry
);
157 /* get next window in Z-order list */
158 static inline struct window
*get_next_window( struct window
*win
)
160 struct list
*ptr
= list_next( &win
->parent
->children
, &win
->entry
);
161 if (ptr
== &win
->parent
->unlinked
) ptr
= NULL
;
162 return ptr
? LIST_ENTRY( ptr
, struct window
, entry
) : NULL
;
165 /* get previous window in Z-order list */
166 static inline struct window
*get_prev_window( struct window
*win
)
168 struct list
*ptr
= list_prev( &win
->parent
->children
, &win
->entry
);
169 if (ptr
== &win
->parent
->unlinked
) ptr
= NULL
;
170 return ptr
? LIST_ENTRY( ptr
, struct window
, entry
) : NULL
;
173 /* get first child in Z-order list */
174 static inline struct window
*get_first_child( struct window
*win
)
176 struct list
*ptr
= list_head( &win
->children
);
177 return ptr
? LIST_ENTRY( ptr
, struct window
, entry
) : NULL
;
180 /* get last child in Z-order list */
181 static inline struct window
*get_last_child( struct window
*win
)
183 struct list
*ptr
= list_tail( &win
->children
);
184 return ptr
? LIST_ENTRY( ptr
, struct window
, entry
) : NULL
;
187 /* append a user handle to a handle array */
188 static int add_handle_to_array( struct user_handle_array
*array
, user_handle_t handle
)
190 if (array
->count
>= array
->total
)
192 int new_total
= max( array
->total
* 2, 32 );
193 user_handle_t
*new_array
= realloc( array
->handles
, new_total
* sizeof(*new_array
) );
196 free( array
->handles
);
197 set_error( STATUS_NO_MEMORY
);
200 array
->handles
= new_array
;
201 array
->total
= new_total
;
203 array
->handles
[array
->count
++] = handle
;
207 /* set a window property */
208 static void set_property( struct window
*win
, atom_t atom
, obj_handle_t handle
, enum property_type type
)
211 struct property
*new_props
;
213 /* check if it exists already */
214 for (i
= 0; i
< win
->prop_inuse
; i
++)
216 if (win
->properties
[i
].type
== PROP_TYPE_FREE
)
221 if (win
->properties
[i
].atom
== atom
)
223 win
->properties
[i
].type
= type
;
224 win
->properties
[i
].handle
= handle
;
229 /* need to add an entry */
230 if (!grab_global_atom( NULL
, atom
)) return;
234 if (win
->prop_inuse
>= win
->prop_alloc
)
236 /* need to grow the array */
237 if (!(new_props
= realloc( win
->properties
,
238 sizeof(*new_props
) * (win
->prop_alloc
+ 16) )))
240 set_error( STATUS_NO_MEMORY
);
241 release_global_atom( NULL
, atom
);
244 win
->prop_alloc
+= 16;
245 win
->properties
= new_props
;
247 free
= win
->prop_inuse
++;
249 win
->properties
[free
].atom
= atom
;
250 win
->properties
[free
].type
= type
;
251 win
->properties
[free
].handle
= handle
;
254 /* remove a window property */
255 static obj_handle_t
remove_property( struct window
*win
, atom_t atom
)
259 for (i
= 0; i
< win
->prop_inuse
; i
++)
261 if (win
->properties
[i
].type
== PROP_TYPE_FREE
) continue;
262 if (win
->properties
[i
].atom
== atom
)
264 release_global_atom( NULL
, atom
);
265 win
->properties
[i
].type
= PROP_TYPE_FREE
;
266 return win
->properties
[i
].handle
;
269 /* FIXME: last error? */
273 /* find a window property */
274 static obj_handle_t
get_property( struct window
*win
, atom_t atom
)
278 for (i
= 0; i
< win
->prop_inuse
; i
++)
280 if (win
->properties
[i
].type
== PROP_TYPE_FREE
) continue;
281 if (win
->properties
[i
].atom
== atom
) return win
->properties
[i
].handle
;
283 /* FIXME: last error? */
287 /* destroy all properties of a window */
288 inline static void destroy_properties( struct window
*win
)
292 if (!win
->properties
) return;
293 for (i
= 0; i
< win
->prop_inuse
; i
++)
295 if (win
->properties
[i
].type
== PROP_TYPE_FREE
) continue;
296 release_global_atom( NULL
, win
->properties
[i
].atom
);
298 free( win
->properties
);
301 /* detach a window from its owner thread but keep the window around */
302 static void detach_window_thread( struct window
*win
)
304 struct thread
*thread
= win
->thread
;
309 if (win
->update_region
) inc_queue_paint_count( thread
, -1 );
310 if (win
->paint_flags
& PAINT_INTERNAL
) inc_queue_paint_count( thread
, -1 );
311 queue_cleanup_window( thread
, win
->handle
);
313 assert( thread
->desktop_users
> 0 );
314 thread
->desktop_users
--;
315 release_class( win
->class );
318 /* don't hold a reference to the desktop so that the desktop window can be */
319 /* destroyed when the desktop ref count reaches zero */
320 release_object( win
->desktop
);
324 /* destroy a window */
325 void destroy_window( struct window
*win
)
327 /* destroy all children */
328 while (!list_empty(&win
->children
))
329 destroy_window( LIST_ENTRY( list_head(&win
->children
), struct window
, entry
));
330 while (!list_empty(&win
->unlinked
))
331 destroy_window( LIST_ENTRY( list_head(&win
->unlinked
), struct window
, entry
));
333 /* reset global window pointers, if the corresponding window is destroyed */
334 if (win
== shell_window
) shell_window
= NULL
;
335 if (win
== shell_listview
) shell_listview
= NULL
;
336 if (win
== progman_window
) progman_window
= NULL
;
337 if (win
== taskman_window
) taskman_window
= NULL
;
338 free_user_handle( win
->handle
);
339 destroy_properties( win
);
340 list_remove( &win
->entry
);
341 if (is_desktop_window(win
))
343 assert( win
->desktop
->top_window
== win
);
344 win
->desktop
->top_window
= NULL
;
346 detach_window_thread( win
);
347 if (win
->win_region
) free_region( win
->win_region
);
348 if (win
->update_region
) free_region( win
->update_region
);
349 if (win
->class) release_class( win
->class );
351 memset( win
, 0x55, sizeof(*win
) + win
->nb_extra_bytes
- 1 );
355 /* get the process owning the top window of a given desktop */
356 struct process
*get_top_window_owner( struct desktop
*desktop
)
358 struct window
*win
= desktop
->top_window
;
359 if (!win
|| !win
->thread
) return NULL
;
360 return win
->thread
->process
;
363 /* attempt to close the desktop window when the last process using it is gone */
364 void close_desktop_window( struct desktop
*desktop
)
366 struct window
*win
= desktop
->top_window
;
367 if (win
&& win
->thread
) post_message( win
->handle
, WM_CLOSE
, 0, 0 );
370 /* create a new window structure (note: the window is not linked in the window tree) */
371 static struct window
*create_window( struct window
*parent
, struct window
*owner
,
372 atom_t atom
, void *instance
)
376 struct desktop
*desktop
;
377 struct window_class
*class;
379 if (!(desktop
= get_thread_desktop( current
, DESKTOP_CREATEWINDOW
))) return NULL
;
381 if (!(class = grab_class( current
->process
, atom
, instance
, &extra_bytes
)))
383 release_object( desktop
);
387 if (!(win
= mem_alloc( sizeof(*win
) + extra_bytes
- 1 ))) goto failed
;
388 if (!(win
->handle
= alloc_user_handle( win
, USER_WINDOW
))) goto failed
;
390 win
->parent
= parent
;
391 win
->owner
= owner
? owner
->handle
: 0;
392 win
->thread
= current
;
393 win
->desktop
= desktop
;
396 win
->last_active
= win
->handle
;
397 win
->win_region
= NULL
;
398 win
->update_region
= NULL
;
402 win
->instance
= NULL
;
406 win
->paint_flags
= 0;
409 win
->properties
= NULL
;
410 win
->nb_extra_bytes
= extra_bytes
;
411 memset( win
->extra_bytes
, 0, extra_bytes
);
412 list_init( &win
->children
);
413 list_init( &win
->unlinked
);
415 /* parent must be on the same desktop */
416 if (parent
&& parent
->desktop
!= desktop
)
418 set_error( STATUS_ACCESS_DENIED
);
422 /* if no parent, class must be the desktop */
423 if (!parent
&& !is_desktop_class( class ))
425 set_error( STATUS_ACCESS_DENIED
);
429 /* if parent belongs to a different thread and the window isn't */
430 /* top-level, attach the two threads */
431 if (parent
&& parent
->thread
&& parent
->thread
!= current
&& !is_desktop_window(parent
))
433 if (!attach_thread_input( current
, parent
->thread
)) goto failed
;
435 else /* otherwise just make sure that the thread has a message queue */
437 if (!current
->queue
&& !init_thread_queue( current
)) goto failed
;
440 /* put it on parent unlinked list */
441 if (parent
) list_add_head( &parent
->unlinked
, &win
->entry
);
444 list_init( &win
->entry
);
445 assert( !desktop
->top_window
);
446 desktop
->top_window
= win
;
447 set_process_default_desktop( current
->process
, desktop
, current
->desktop
);
450 current
->desktop_users
++;
456 if (win
->handle
) free_user_handle( win
->handle
);
459 release_object( desktop
);
460 release_class( class );
464 /* destroy all windows belonging to a given thread */
465 void destroy_thread_windows( struct thread
*thread
)
467 user_handle_t handle
= 0;
470 while ((win
= next_user_handle( &handle
, USER_WINDOW
)))
472 if (win
->thread
!= thread
) continue;
473 if (is_desktop_window( win
)) detach_window_thread( win
);
474 else destroy_window( win
);
478 /* get the desktop window */
479 static struct window
*get_desktop_window( struct thread
*thread
, int create
)
481 struct window
*top_window
;
482 struct desktop
*desktop
= get_thread_desktop( thread
, 0 );
484 if (!desktop
) return NULL
;
486 if (!(top_window
= desktop
->top_window
) && create
)
488 if ((top_window
= create_window( NULL
, NULL
, DESKTOP_ATOM
, 0 )))
490 detach_window_thread( top_window
);
491 top_window
->style
= WS_POPUP
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
;
494 release_object( desktop
);
498 /* check whether child is a descendant of parent */
499 int is_child_window( user_handle_t parent
, user_handle_t child
)
501 struct window
*child_ptr
= get_user_object( child
, USER_WINDOW
);
502 struct window
*parent_ptr
= get_user_object( parent
, USER_WINDOW
);
504 if (!child_ptr
|| !parent_ptr
) return 0;
505 while (child_ptr
->parent
)
507 if (child_ptr
->parent
== parent_ptr
) return 1;
508 child_ptr
= child_ptr
->parent
;
513 /* check whether window is a top-level window */
514 int is_top_level_window( user_handle_t window
)
516 struct window
*win
= get_user_object( window
, USER_WINDOW
);
517 return (win
&& win
->parent
&& is_desktop_window(win
->parent
));
520 /* make a window active if possible */
521 int make_window_active( user_handle_t window
)
523 struct window
*owner
, *win
= get_window( window
);
527 /* set last active for window and its owner */
528 win
->last_active
= win
->handle
;
529 if ((owner
= get_user_object( win
->owner
, USER_WINDOW
))) owner
->last_active
= win
->handle
;
533 /* increment (or decrement) the window paint count */
534 static inline void inc_window_paint_count( struct window
*win
, int incr
)
536 if (win
->thread
) inc_queue_paint_count( win
->thread
, incr
);
539 /* check if window and all its ancestors are visible */
540 static int is_visible( const struct window
*win
)
542 while (win
&& win
->parent
)
544 if (!(win
->style
& WS_VISIBLE
)) return 0;
546 /* if parent is minimized children are not visible */
547 if (win
&& (win
->style
& WS_MINIMIZE
)) return 0;
552 /* same as is_visible but takes a window handle */
553 int is_window_visible( user_handle_t window
)
555 struct window
*win
= get_user_object( window
, USER_WINDOW
);
557 return is_visible( win
);
560 /* check if point is inside the window */
561 static inline int is_point_in_window( struct window
*win
, int x
, int y
)
563 if (!(win
->style
& WS_VISIBLE
)) return 0; /* not visible */
564 if ((win
->style
& (WS_POPUP
|WS_CHILD
|WS_DISABLED
)) == (WS_CHILD
|WS_DISABLED
))
565 return 0; /* disabled child */
566 if ((win
->ex_style
& (WS_EX_LAYERED
|WS_EX_TRANSPARENT
)) == (WS_EX_LAYERED
|WS_EX_TRANSPARENT
))
567 return 0; /* transparent */
568 if (x
< win
->visible_rect
.left
|| x
>= win
->visible_rect
.right
||
569 y
< win
->visible_rect
.top
|| y
>= win
->visible_rect
.bottom
)
570 return 0; /* not in window */
571 if (win
->win_region
&&
572 !point_in_region( win
->win_region
, x
- win
->window_rect
.left
, y
- win
->window_rect
.top
))
573 return 0; /* not in window region */
577 /* find child of 'parent' that contains the given point (in parent-relative coords) */
578 static struct window
*child_window_from_point( struct window
*parent
, int x
, int y
)
582 LIST_FOR_EACH_ENTRY( ptr
, &parent
->children
, struct window
, entry
)
584 if (!is_point_in_window( ptr
, x
, y
)) continue; /* skip it */
586 /* if window is minimized or disabled, return at once */
587 if (ptr
->style
& (WS_MINIMIZE
|WS_DISABLED
)) return ptr
;
589 /* if point is not in client area, return at once */
590 if (x
< ptr
->client_rect
.left
|| x
>= ptr
->client_rect
.right
||
591 y
< ptr
->client_rect
.top
|| y
>= ptr
->client_rect
.bottom
)
594 return child_window_from_point( ptr
, x
- ptr
->client_rect
.left
, y
- ptr
->client_rect
.top
);
596 return parent
; /* not found any child */
599 /* find all children of 'parent' that contain the given point */
600 static int get_window_children_from_point( struct window
*parent
, int x
, int y
,
601 struct user_handle_array
*array
)
605 LIST_FOR_EACH_ENTRY( ptr
, &parent
->children
, struct window
, entry
)
607 if (!is_point_in_window( ptr
, x
, y
)) continue; /* skip it */
609 /* if point is in client area, and window is not minimized or disabled, check children */
610 if (!(ptr
->style
& (WS_MINIMIZE
|WS_DISABLED
)) &&
611 x
>= ptr
->client_rect
.left
&& x
< ptr
->client_rect
.right
&&
612 y
>= ptr
->client_rect
.top
&& y
< ptr
->client_rect
.bottom
)
614 if (!get_window_children_from_point( ptr
, x
- ptr
->client_rect
.left
,
615 y
- ptr
->client_rect
.top
, array
))
619 /* now add window to the array */
620 if (!add_handle_to_array( array
, ptr
->handle
)) return 0;
625 /* find window containing point (in absolute coords) */
626 user_handle_t
window_from_point( struct desktop
*desktop
, int x
, int y
)
630 if (!desktop
->top_window
) return 0;
631 ret
= child_window_from_point( desktop
->top_window
, x
, y
);
635 /* return list of all windows containing point (in absolute coords) */
636 static int all_windows_from_point( struct window
*top
, int x
, int y
, struct user_handle_array
*array
)
640 /* make point relative to top window */
641 for (ptr
= top
->parent
; ptr
&& !is_desktop_window(ptr
); ptr
= ptr
->parent
)
643 x
-= ptr
->client_rect
.left
;
644 y
-= ptr
->client_rect
.top
;
647 if (!is_point_in_window( top
, x
, y
)) return 1;
649 /* if point is in client area, and window is not minimized or disabled, check children */
650 if (!(top
->style
& (WS_MINIMIZE
|WS_DISABLED
)) &&
651 x
>= top
->client_rect
.left
&& x
< top
->client_rect
.right
&&
652 y
>= top
->client_rect
.top
&& y
< top
->client_rect
.bottom
)
654 if (!is_desktop_window(top
))
656 x
-= top
->client_rect
.left
;
657 y
-= top
->client_rect
.top
;
659 if (!get_window_children_from_point( top
, x
, y
, array
)) return 0;
661 /* now add window to the array */
662 if (!add_handle_to_array( array
, top
->handle
)) return 0;
667 /* return the thread owning a window */
668 struct thread
*get_window_thread( user_handle_t handle
)
670 struct window
*win
= get_user_object( handle
, USER_WINDOW
);
671 if (!win
|| !win
->thread
) return NULL
;
672 return (struct thread
*)grab_object( win
->thread
);
676 /* check if any area of a window needs repainting */
677 static inline int win_needs_repaint( struct window
*win
)
679 return win
->update_region
|| (win
->paint_flags
& PAINT_INTERNAL
);
683 /* find a child of the specified window that needs repainting */
684 static struct window
*find_child_to_repaint( struct window
*parent
, struct thread
*thread
)
686 struct window
*ptr
, *ret
= NULL
;
688 LIST_FOR_EACH_ENTRY( ptr
, &parent
->children
, struct window
, entry
)
690 if (!(ptr
->style
& WS_VISIBLE
)) continue;
691 if (ptr
->thread
== thread
&& win_needs_repaint( ptr
))
693 else if (!(ptr
->style
& WS_MINIMIZE
)) /* explore its children */
694 ret
= find_child_to_repaint( ptr
, thread
);
698 if (ret
&& (ret
->ex_style
& WS_EX_TRANSPARENT
))
700 /* transparent window, check for non-transparent sibling to paint first */
701 for (ptr
= get_next_window(ret
); ptr
; ptr
= get_next_window(ptr
))
703 if (!(ptr
->style
& WS_VISIBLE
)) continue;
704 if (ptr
->ex_style
& WS_EX_TRANSPARENT
) continue;
705 if (ptr
->thread
!= thread
) continue;
706 if (win_needs_repaint( ptr
)) return ptr
;
713 /* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
714 user_handle_t
find_window_to_repaint( user_handle_t parent
, struct thread
*thread
)
716 struct window
*ptr
, *win
, *top_window
= get_desktop_window( thread
, 0 );
718 if (!top_window
) return 0;
720 if (top_window
->thread
== thread
&& win_needs_repaint( top_window
)) win
= top_window
;
721 else win
= find_child_to_repaint( top_window
, thread
);
725 /* check that it is a child of the specified parent */
726 for (ptr
= win
; ptr
; ptr
= ptr
->parent
)
727 if (ptr
->handle
== parent
) break;
728 /* otherwise don't return any window, we don't repaint a child before its parent */
729 if (!ptr
) win
= NULL
;
732 win
->paint_flags
&= ~PAINT_INTERNAL
;
737 /* intersect the window region with the specified region, relative to the window parent */
738 static struct region
*intersect_window_region( struct region
*region
, struct window
*win
)
740 /* make region relative to window rect */
741 offset_region( region
, -win
->window_rect
.left
, -win
->window_rect
.top
);
742 if (!intersect_region( region
, region
, win
->win_region
)) return NULL
;
743 /* make region relative to parent again */
744 offset_region( region
, win
->window_rect
.left
, win
->window_rect
.top
);
749 /* convert coordinates from client to screen coords */
750 static inline void client_to_screen( struct window
*win
, int *x
, int *y
)
752 for ( ; win
&& !is_desktop_window(win
); win
= win
->parent
)
754 *x
+= win
->client_rect
.left
;
755 *y
+= win
->client_rect
.top
;
759 /* convert coordinates from client to screen coords */
760 static inline void client_to_screen_rect( struct window
*win
, rectangle_t
*rect
)
762 for ( ; win
&& !is_desktop_window(win
); win
= win
->parent
)
764 rect
->left
+= win
->client_rect
.left
;
765 rect
->right
+= win
->client_rect
.left
;
766 rect
->top
+= win
->client_rect
.top
;
767 rect
->bottom
+= win
->client_rect
.top
;
771 /* map the region from window to screen coordinates */
772 static inline void map_win_region_to_screen( struct window
*win
, struct region
*region
)
774 if (!is_desktop_window(win
))
776 int x
= win
->window_rect
.left
;
777 int y
= win
->window_rect
.top
;
778 client_to_screen( win
->parent
, &x
, &y
);
779 offset_region( region
, x
, y
);
784 /* clip all children of a given window out of the visible region */
785 static struct region
*clip_children( struct window
*parent
, struct window
*last
,
786 struct region
*region
, int offset_x
, int offset_y
)
789 struct region
*tmp
= create_empty_region();
791 if (!tmp
) return NULL
;
792 LIST_FOR_EACH_ENTRY( ptr
, &parent
->children
, struct window
, entry
)
794 if (ptr
== last
) break;
795 if (!(ptr
->style
& WS_VISIBLE
)) continue;
796 if (ptr
->ex_style
& WS_EX_TRANSPARENT
) continue;
797 set_region_rect( tmp
, &ptr
->visible_rect
);
798 if (ptr
->win_region
&& !intersect_window_region( tmp
, ptr
))
803 offset_region( tmp
, offset_x
, offset_y
);
804 if (!(region
= subtract_region( region
, region
, tmp
))) break;
805 if (is_region_empty( region
)) break;
812 /* compute the intersection of two rectangles; return 0 if the result is empty */
813 static inline int intersect_rect( rectangle_t
*dst
, const rectangle_t
*src1
, const rectangle_t
*src2
)
815 dst
->left
= max( src1
->left
, src2
->left
);
816 dst
->top
= max( src1
->top
, src2
->top
);
817 dst
->right
= min( src1
->right
, src2
->right
);
818 dst
->bottom
= min( src1
->bottom
, src2
->bottom
);
819 return (dst
->left
< dst
->right
&& dst
->top
< dst
->bottom
);
823 /* set the region to the client rect clipped by the window rect, in parent-relative coordinates */
824 static void set_region_client_rect( struct region
*region
, struct window
*win
)
828 intersect_rect( &rect
, &win
->window_rect
, &win
->client_rect
);
829 set_region_rect( region
, &rect
);
833 /* get the top-level window to clip against for a given window */
834 static inline struct window
*get_top_clipping_window( struct window
*win
)
836 while (win
->parent
&& !is_desktop_window(win
->parent
)) win
= win
->parent
;
841 /* compute the visible region of a window, in window coordinates */
842 static struct region
*get_visible_region( struct window
*win
, struct window
*top
, unsigned int flags
)
844 struct region
*tmp
= NULL
, *region
;
845 int offset_x
, offset_y
;
847 if (!(region
= create_empty_region())) return NULL
;
849 /* first check if all ancestors are visible */
851 if (!is_visible( win
)) return region
; /* empty region */
853 /* create a region relative to the window itself */
855 if ((flags
& DCX_PARENTCLIP
) && win
!= top
&& win
->parent
)
857 set_region_client_rect( region
, win
->parent
);
858 offset_region( region
, -win
->parent
->client_rect
.left
, -win
->parent
->client_rect
.top
);
860 else if (flags
& DCX_WINDOW
)
862 set_region_rect( region
, &win
->visible_rect
);
863 if (win
->win_region
&& !intersect_window_region( region
, win
)) goto error
;
867 set_region_client_rect( region
, win
);
868 if (win
->win_region
&& !intersect_window_region( region
, win
)) goto error
;
873 if (flags
& DCX_CLIPCHILDREN
)
875 if (is_desktop_window(win
)) offset_x
= offset_y
= 0;
878 offset_x
= win
->client_rect
.left
;
879 offset_y
= win
->client_rect
.top
;
881 if (!clip_children( win
, NULL
, region
, offset_x
, offset_y
)) goto error
;
884 /* clip siblings of ancestors */
886 if (is_desktop_window(win
)) offset_x
= offset_y
= 0;
889 offset_x
= win
->window_rect
.left
;
890 offset_y
= win
->window_rect
.top
;
893 if (top
&& top
!= win
&& (tmp
= create_empty_region()) != NULL
)
895 while (win
!= top
&& win
->parent
)
897 if (win
->style
& WS_CLIPSIBLINGS
)
899 if (!clip_children( win
->parent
, win
, region
, 0, 0 )) goto error
;
900 if (is_region_empty( region
)) break;
902 /* clip to parent client area */
904 if (!is_desktop_window(win
))
906 offset_x
+= win
->client_rect
.left
;
907 offset_y
+= win
->client_rect
.top
;
908 offset_region( region
, win
->client_rect
.left
, win
->client_rect
.top
);
910 set_region_client_rect( tmp
, win
);
911 if (win
->win_region
&& !intersect_window_region( tmp
, win
)) goto error
;
912 if (!intersect_region( region
, region
, tmp
)) goto error
;
913 if (is_region_empty( region
)) break;
917 offset_region( region
, -offset_x
, -offset_y
); /* make it relative to target window */
921 if (tmp
) free_region( tmp
);
922 free_region( region
);
927 /* get the window class of a window */
928 struct window_class
* get_window_class( user_handle_t window
)
931 if (!(win
= get_window( window
))) return NULL
;
932 if (!win
->class) set_error( STATUS_ACCESS_DENIED
);
936 /* return a copy of the specified region cropped to the window client or frame rectangle, */
937 /* and converted from client to window coordinates. Helper for (in)validate_window. */
938 static struct region
*crop_region_to_win_rect( struct window
*win
, struct region
*region
, int frame
)
940 struct region
*tmp
= create_empty_region();
942 if (!tmp
) return NULL
;
944 /* get bounding rect in client coords */
945 if (frame
) set_region_rect( tmp
, &win
->window_rect
);
946 else set_region_client_rect( tmp
, win
);
947 if (!is_desktop_window(win
))
948 offset_region( tmp
, -win
->client_rect
.left
, -win
->client_rect
.top
);
950 /* intersect specified region with bounding rect */
951 if (region
&& !intersect_region( tmp
, region
, tmp
)) goto done
;
952 if (is_region_empty( tmp
)) goto done
;
954 /* map it to window coords */
955 offset_region( tmp
, win
->client_rect
.left
- win
->window_rect
.left
,
956 win
->client_rect
.top
- win
->window_rect
.top
);
965 /* set a region as new update region for the window */
966 static void set_update_region( struct window
*win
, struct region
*region
)
968 if (region
&& !is_region_empty( region
))
970 if (!win
->update_region
) inc_window_paint_count( win
, 1 );
971 else free_region( win
->update_region
);
972 win
->update_region
= region
;
976 if (win
->update_region
)
978 inc_window_paint_count( win
, -1 );
979 free_region( win
->update_region
);
981 win
->paint_flags
&= ~(PAINT_ERASE
| PAINT_NONCLIENT
);
982 win
->update_region
= NULL
;
983 if (region
) free_region( region
);
988 /* add a region to the update region; the passed region is freed or reused */
989 static int add_update_region( struct window
*win
, struct region
*region
)
991 if (win
->update_region
&& !union_region( region
, win
->update_region
, region
))
993 free_region( region
);
996 set_update_region( win
, region
);
1001 /* validate the non client area of a window */
1002 static void validate_non_client( struct window
*win
)
1007 if (!win
->update_region
) return; /* nothing to do */
1009 /* get client rect in window coords */
1010 rect
.left
= win
->client_rect
.left
- win
->window_rect
.left
;
1011 rect
.top
= win
->client_rect
.top
- win
->window_rect
.top
;
1012 rect
.right
= win
->client_rect
.right
- win
->window_rect
.left
;
1013 rect
.bottom
= win
->client_rect
.bottom
- win
->window_rect
.top
;
1015 if ((tmp
= create_empty_region()))
1017 set_region_rect( tmp
, &rect
);
1018 if (intersect_region( tmp
, win
->update_region
, tmp
))
1019 set_update_region( win
, tmp
);
1023 win
->paint_flags
&= ~PAINT_NONCLIENT
;
1027 /* validate a window completely so that we don't get any further paint messages for it */
1028 static void validate_whole_window( struct window
*win
)
1030 set_update_region( win
, NULL
);
1032 if (win
->paint_flags
& PAINT_INTERNAL
)
1034 win
->paint_flags
&= ~PAINT_INTERNAL
;
1035 inc_window_paint_count( win
, -1 );
1040 /* validate the update region of a window on all parents; helper for redraw_window */
1041 static void validate_parents( struct window
*child
)
1043 int offset_x
= 0, offset_y
= 0;
1044 struct window
*win
= child
;
1045 struct region
*tmp
= NULL
;
1047 if (!child
->update_region
) return;
1051 /* map to parent client coords */
1052 offset_x
+= win
->window_rect
.left
;
1053 offset_y
+= win
->window_rect
.top
;
1057 /* and now map to window coords */
1058 offset_x
+= win
->client_rect
.left
- win
->window_rect
.left
;
1059 offset_y
+= win
->client_rect
.top
- win
->window_rect
.top
;
1061 if (win
->update_region
&& !(win
->style
& WS_CLIPCHILDREN
))
1063 if (!tmp
&& !(tmp
= create_empty_region())) return;
1064 offset_region( child
->update_region
, offset_x
, offset_y
);
1065 if (subtract_region( tmp
, win
->update_region
, child
->update_region
))
1067 set_update_region( win
, tmp
);
1070 /* restore child coords */
1071 offset_region( child
->update_region
, -offset_x
, -offset_y
);
1074 if (tmp
) free_region( tmp
);
1078 /* add/subtract a region (in client coordinates) to the update region of the window */
1079 static void redraw_window( struct window
*win
, struct region
*region
, int frame
, unsigned int flags
)
1082 struct window
*child
;
1084 if (flags
& RDW_INVALIDATE
)
1086 if (!(tmp
= crop_region_to_win_rect( win
, region
, frame
))) return;
1088 if (!add_update_region( win
, tmp
)) return;
1090 if (flags
& RDW_FRAME
) win
->paint_flags
|= PAINT_NONCLIENT
;
1091 if (flags
& RDW_ERASE
) win
->paint_flags
|= PAINT_ERASE
;
1093 else if (flags
& RDW_VALIDATE
)
1095 if (!region
&& (flags
& RDW_NOFRAME
)) /* shortcut: validate everything */
1097 set_update_region( win
, NULL
);
1099 else if (win
->update_region
)
1101 if ((tmp
= crop_region_to_win_rect( win
, region
, frame
)))
1103 if (!subtract_region( tmp
, win
->update_region
, tmp
))
1108 set_update_region( win
, tmp
);
1110 if (flags
& RDW_NOFRAME
) validate_non_client( win
);
1111 if (flags
& RDW_NOERASE
) win
->paint_flags
&= ~PAINT_ERASE
;
1115 if ((flags
& RDW_INTERNALPAINT
) && !(win
->paint_flags
& PAINT_INTERNAL
))
1117 win
->paint_flags
|= PAINT_INTERNAL
;
1118 inc_window_paint_count( win
, 1 );
1120 else if ((flags
& RDW_NOINTERNALPAINT
) && (win
->paint_flags
& PAINT_INTERNAL
))
1122 win
->paint_flags
&= ~PAINT_INTERNAL
;
1123 inc_window_paint_count( win
, -1 );
1126 if (flags
& RDW_UPDATENOW
)
1128 validate_parents( win
);
1129 flags
&= ~RDW_UPDATENOW
;
1132 /* now process children recursively */
1134 if (flags
& RDW_NOCHILDREN
) return;
1135 if (win
->style
& WS_MINIMIZE
) return;
1136 if ((win
->style
& WS_CLIPCHILDREN
) && !(flags
& RDW_ALLCHILDREN
)) return;
1138 if (!(tmp
= crop_region_to_win_rect( win
, region
, 0 ))) return;
1140 /* map to client coordinates */
1141 offset_region( tmp
, win
->window_rect
.left
- win
->client_rect
.left
,
1142 win
->window_rect
.top
- win
->client_rect
.top
);
1144 if (flags
& RDW_INVALIDATE
) flags
|= RDW_FRAME
| RDW_ERASE
;
1146 LIST_FOR_EACH_ENTRY( child
, &win
->children
, struct window
, entry
)
1148 if (!(child
->style
& WS_VISIBLE
)) continue;
1149 if (!rect_in_region( tmp
, &child
->window_rect
)) continue;
1150 offset_region( tmp
, -child
->client_rect
.left
, -child
->client_rect
.top
);
1151 redraw_window( child
, tmp
, 1, flags
);
1152 offset_region( tmp
, child
->client_rect
.left
, child
->client_rect
.top
);
1158 /* retrieve the update flags for a window depending on the state of the update region */
1159 static unsigned int get_update_flags( struct window
*win
, unsigned int flags
)
1161 unsigned int ret
= 0;
1163 if (flags
& UPDATE_NONCLIENT
)
1165 if ((win
->paint_flags
& PAINT_NONCLIENT
) && win
->update_region
) ret
|= UPDATE_NONCLIENT
;
1167 if (flags
& UPDATE_ERASE
)
1169 if ((win
->paint_flags
& PAINT_ERASE
) && win
->update_region
) ret
|= UPDATE_ERASE
;
1171 if (flags
& UPDATE_PAINT
)
1173 if (win
->update_region
) ret
|= UPDATE_PAINT
;
1175 if (flags
& UPDATE_INTERNALPAINT
)
1177 if (win
->paint_flags
& PAINT_INTERNAL
) ret
|= UPDATE_INTERNALPAINT
;
1183 /* iterate through the children of the given window until we find one with some update flags */
1184 static unsigned int get_child_update_flags( struct window
*win
, struct window
*from_child
,
1185 unsigned int flags
, struct window
**child
)
1188 unsigned int ret
= 0;
1190 /* first make sure we want to iterate children at all */
1192 if (win
->style
& WS_MINIMIZE
) return 0;
1194 /* note: the WS_CLIPCHILDREN test is the opposite of the invalidation case,
1195 * here we only want to repaint children of windows that clip them, others
1196 * need to wait for WM_PAINT to be done in the parent first.
1198 if (!(flags
& UPDATE_ALLCHILDREN
) && !(win
->style
& WS_CLIPCHILDREN
)) return 0;
1200 LIST_FOR_EACH_ENTRY( ptr
, &win
->children
, struct window
, entry
)
1202 if (from_child
) /* skip all children until from_child is found */
1204 if (ptr
== from_child
) from_child
= NULL
;
1207 if (!(ptr
->style
& WS_VISIBLE
)) continue;
1208 if ((ret
= get_update_flags( ptr
, flags
)) != 0)
1213 if ((ret
= get_child_update_flags( ptr
, NULL
, flags
, child
))) break;
1218 /* iterate through children and siblings of the given window until we find one with some update flags */
1219 static unsigned int get_window_update_flags( struct window
*win
, struct window
*from_child
,
1220 unsigned int flags
, struct window
**child
)
1223 struct window
*ptr
, *from_sibling
= NULL
;
1225 /* if some parent is not visible start from the next sibling */
1227 if (!is_visible( win
)) return 0;
1228 for (ptr
= from_child
; ptr
; ptr
= ptr
->parent
)
1230 if (!(ptr
->style
& WS_VISIBLE
) || (ptr
->style
& WS_MINIMIZE
)) from_sibling
= ptr
;
1231 if (ptr
== win
) break;
1234 /* non-client painting must be delayed if one of the parents is going to
1235 * be repainted and doesn't clip children */
1237 if ((flags
& UPDATE_NONCLIENT
) && !(flags
& (UPDATE_PAINT
|UPDATE_INTERNALPAINT
)))
1239 for (ptr
= win
->parent
; ptr
; ptr
= ptr
->parent
)
1241 if (!(ptr
->style
& WS_CLIPCHILDREN
) && win_needs_repaint( ptr
))
1244 if (from_child
&& !(flags
& UPDATE_ALLCHILDREN
))
1246 for (ptr
= from_sibling
? from_sibling
: from_child
; ptr
; ptr
= ptr
->parent
)
1248 if (!(ptr
->style
& WS_CLIPCHILDREN
) && win_needs_repaint( ptr
)) from_sibling
= ptr
;
1249 if (ptr
== win
) break;
1255 /* check window itself (only if not restarting from a child) */
1259 if ((ret
= get_update_flags( win
, flags
)))
1267 /* now check children */
1269 if (flags
& UPDATE_NOCHILDREN
) return 0;
1272 if ((ret
= get_child_update_flags( from_child
, NULL
, flags
, child
))) return ret
;
1273 from_sibling
= from_child
;
1276 /* then check siblings and parent siblings */
1278 while (from_sibling
->parent
&& from_sibling
!= win
)
1280 if ((ret
= get_child_update_flags( from_sibling
->parent
, from_sibling
, flags
, child
)))
1282 from_sibling
= from_sibling
->parent
;
1288 /* expose a region of a window, looking for the top most parent that needs to be exposed */
1289 /* the region is in window coordinates */
1290 static void expose_window( struct window
*win
, struct window
*top
, struct region
*region
)
1292 struct window
*parent
, *ptr
;
1293 int offset_x
, offset_y
;
1295 /* find the top most parent that doesn't clip either siblings or children */
1296 for (parent
= ptr
= win
; ptr
!= top
; ptr
= ptr
->parent
)
1298 if (!(ptr
->style
& WS_CLIPCHILDREN
)) parent
= ptr
;
1299 if (!(ptr
->style
& WS_CLIPSIBLINGS
)) parent
= ptr
->parent
;
1301 if (parent
== win
&& parent
!= top
&& win
->parent
)
1302 parent
= win
->parent
; /* always go up at least one level if possible */
1304 offset_x
= win
->window_rect
.left
- win
->client_rect
.left
;
1305 offset_y
= win
->window_rect
.top
- win
->client_rect
.top
;
1306 for (ptr
= win
; ptr
!= parent
&& !is_desktop_window(ptr
); ptr
= ptr
->parent
)
1308 offset_x
+= ptr
->client_rect
.left
;
1309 offset_y
+= ptr
->client_rect
.top
;
1311 offset_region( region
, offset_x
, offset_y
);
1312 redraw_window( parent
, region
, 0, RDW_INVALIDATE
| RDW_ERASE
| RDW_ALLCHILDREN
);
1313 offset_region( region
, -offset_x
, -offset_y
);
1317 /* set the window and client rectangles, updating the update region if necessary */
1318 static void set_window_pos( struct window
*win
, struct window
*previous
,
1319 unsigned int swp_flags
, const rectangle_t
*window_rect
,
1320 const rectangle_t
*client_rect
, const rectangle_t
*visible_rect
,
1321 const rectangle_t
*valid_rects
)
1323 struct region
*old_vis_rgn
= NULL
, *new_vis_rgn
;
1324 const rectangle_t old_window_rect
= win
->window_rect
;
1325 const rectangle_t old_visible_rect
= win
->visible_rect
;
1326 const rectangle_t old_client_rect
= win
->client_rect
;
1327 struct window
*top
= get_top_clipping_window( win
);
1328 int visible
= (win
->style
& WS_VISIBLE
) || (swp_flags
& SWP_SHOWWINDOW
);
1330 if (win
->parent
&& !is_visible( win
->parent
)) visible
= 0;
1332 if (visible
&& !(old_vis_rgn
= get_visible_region( win
, top
, DCX_WINDOW
))) return;
1334 /* set the new window info before invalidating anything */
1336 win
->window_rect
= *window_rect
;
1337 win
->visible_rect
= *visible_rect
;
1338 win
->client_rect
= *client_rect
;
1339 if (!(swp_flags
& SWP_NOZORDER
) && win
->parent
)
1341 list_remove( &win
->entry
); /* unlink it from the previous location */
1342 if (previous
) list_add_after( &previous
->entry
, &win
->entry
);
1343 else list_add_head( &win
->parent
->children
, &win
->entry
);
1345 if (swp_flags
& SWP_SHOWWINDOW
) win
->style
|= WS_VISIBLE
;
1346 else if (swp_flags
& SWP_HIDEWINDOW
) win
->style
&= ~WS_VISIBLE
;
1348 /* if the window is not visible, everything is easy */
1349 if (!visible
) return;
1351 if (!(new_vis_rgn
= get_visible_region( win
, top
, DCX_WINDOW
)))
1353 free_region( old_vis_rgn
);
1354 clear_error(); /* ignore error since the window info has been modified already */
1358 /* expose anything revealed by the change */
1360 if (!(swp_flags
& SWP_NOREDRAW
))
1362 offset_region( old_vis_rgn
, old_window_rect
.left
- window_rect
->left
,
1363 old_window_rect
.top
- window_rect
->top
);
1364 if (xor_region( new_vis_rgn
, old_vis_rgn
, new_vis_rgn
))
1365 expose_window( win
, top
, new_vis_rgn
);
1367 free_region( old_vis_rgn
);
1369 if (!(win
->style
& WS_VISIBLE
))
1371 /* clear the update region since the window is no longer visible */
1372 validate_whole_window( win
);
1376 /* crop update region to the new window rect */
1378 if (win
->update_region
&&
1379 (window_rect
->right
- window_rect
->left
< old_window_rect
.right
- old_window_rect
.left
||
1380 window_rect
->bottom
- window_rect
->top
< old_window_rect
.bottom
- old_window_rect
.top
))
1382 struct region
*tmp
= create_empty_region();
1385 set_region_rect( tmp
, window_rect
);
1386 if (!is_desktop_window(win
))
1387 offset_region( tmp
, -window_rect
->left
, -window_rect
->top
);
1388 if (intersect_region( tmp
, win
->update_region
, tmp
))
1389 set_update_region( win
, tmp
);
1395 if (swp_flags
& SWP_NOREDRAW
) goto done
; /* do not repaint anything */
1397 /* expose the whole non-client area if it changed in any way */
1399 if ((swp_flags
& SWP_FRAMECHANGED
) ||
1400 memcmp( window_rect
, &old_window_rect
, sizeof(old_window_rect
) ) ||
1401 memcmp( visible_rect
, &old_visible_rect
, sizeof(old_visible_rect
) ) ||
1402 memcmp( client_rect
, &old_client_rect
, sizeof(old_client_rect
) ))
1404 struct region
*tmp
= create_empty_region();
1408 /* subtract the valid portion of client rect from the total region */
1409 if (!memcmp( client_rect
, &old_client_rect
, sizeof(old_client_rect
) ))
1410 set_region_rect( tmp
, client_rect
);
1411 else if (valid_rects
)
1412 set_region_rect( tmp
, &valid_rects
[0] );
1414 set_region_rect( new_vis_rgn
, window_rect
);
1415 if (subtract_region( tmp
, new_vis_rgn
, tmp
))
1417 if (!is_desktop_window(win
))
1418 offset_region( tmp
, -client_rect
->left
, -client_rect
->top
);
1419 redraw_window( win
, tmp
, 1, RDW_INVALIDATE
| RDW_ERASE
| RDW_FRAME
| RDW_ALLCHILDREN
);
1426 free_region( new_vis_rgn
);
1427 clear_error(); /* we ignore out of memory errors once the new rects have been set */
1431 /* create a window */
1432 DECL_HANDLER(create_window
)
1434 struct window
*win
, *parent
, *owner
= NULL
;
1438 if (!req
->parent
) parent
= get_desktop_window( current
, 0 );
1439 else if (!(parent
= get_window( req
->parent
))) return;
1443 if (!(owner
= get_window( req
->owner
))) return;
1444 if (is_desktop_window(owner
)) owner
= NULL
;
1445 else if (!is_desktop_window(parent
))
1447 /* an owned window must be created as top-level */
1448 set_error( STATUS_ACCESS_DENIED
);
1451 else /* owner must be a top-level window */
1452 while (!is_desktop_window(owner
->parent
)) owner
= owner
->parent
;
1454 if (!(win
= create_window( parent
, owner
, req
->atom
, req
->instance
))) return;
1456 reply
->handle
= win
->handle
;
1457 reply
->parent
= win
->parent
? win
->parent
->handle
: 0;
1458 reply
->owner
= win
->owner
;
1459 reply
->extra
= win
->nb_extra_bytes
;
1460 reply
->class_ptr
= get_class_client_ptr( win
->class );
1464 /* set the parent of a window */
1465 DECL_HANDLER(set_parent
)
1467 struct window
*win
, *parent
= NULL
;
1469 if (!(win
= get_window( req
->handle
))) return;
1470 if (req
->parent
&& !(parent
= get_window( req
->parent
))) return;
1472 if (is_desktop_window(win
))
1474 set_error( STATUS_INVALID_PARAMETER
);
1477 reply
->old_parent
= win
->parent
->handle
;
1478 reply
->full_parent
= parent
? parent
->handle
: 0;
1479 set_parent_window( win
, parent
);
1483 /* destroy a window */
1484 DECL_HANDLER(destroy_window
)
1486 struct window
*win
= get_window( req
->handle
);
1489 if (!is_desktop_window(win
)) destroy_window( win
);
1490 else if (win
->thread
== current
) detach_window_thread( win
);
1491 else set_error( STATUS_ACCESS_DENIED
);
1496 /* retrieve the desktop window for the current thread */
1497 DECL_HANDLER(get_desktop_window
)
1499 struct window
*win
= get_desktop_window( current
, req
->force
);
1501 if (win
) reply
->handle
= win
->handle
;
1505 /* set a window owner */
1506 DECL_HANDLER(set_window_owner
)
1508 struct window
*win
= get_window( req
->handle
);
1509 struct window
*owner
= NULL
;
1512 if (req
->owner
&& !(owner
= get_window( req
->owner
))) return;
1513 if (is_desktop_window(win
))
1515 set_error( STATUS_ACCESS_DENIED
);
1518 reply
->prev_owner
= win
->owner
;
1519 reply
->full_owner
= win
->owner
= owner
? owner
->handle
: 0;
1523 /* get information from a window handle */
1524 DECL_HANDLER(get_window_info
)
1526 struct window
*win
= get_window( req
->handle
);
1528 reply
->full_handle
= 0;
1529 reply
->tid
= reply
->pid
= 0;
1532 reply
->full_handle
= win
->handle
;
1533 reply
->last_active
= win
->handle
;
1534 reply
->is_unicode
= win
->is_unicode
;
1535 if (get_user_object( win
->last_active
, USER_WINDOW
)) reply
->last_active
= win
->last_active
;
1538 reply
->tid
= get_thread_id( win
->thread
);
1539 reply
->pid
= get_process_id( win
->thread
->process
);
1540 reply
->atom
= win
->class ? get_class_atom( win
->class ) : DESKTOP_ATOM
;
1546 /* set some information in a window */
1547 DECL_HANDLER(set_window_info
)
1549 struct window
*win
= get_window( req
->handle
);
1552 if (req
->flags
&& is_desktop_window(win
) && win
->thread
!= current
)
1554 set_error( STATUS_ACCESS_DENIED
);
1557 if (req
->extra_size
> sizeof(req
->extra_value
) ||
1558 req
->extra_offset
< -1 ||
1559 req
->extra_offset
> win
->nb_extra_bytes
- (int)req
->extra_size
)
1561 set_win32_error( ERROR_INVALID_INDEX
);
1564 if (req
->extra_offset
!= -1)
1566 memcpy( &reply
->old_extra_value
, win
->extra_bytes
+ req
->extra_offset
, req
->extra_size
);
1568 else if (req
->flags
& SET_WIN_EXTRA
)
1570 set_win32_error( ERROR_INVALID_INDEX
);
1573 reply
->old_style
= win
->style
;
1574 reply
->old_ex_style
= win
->ex_style
;
1575 reply
->old_id
= win
->id
;
1576 reply
->old_instance
= win
->instance
;
1577 reply
->old_user_data
= win
->user_data
;
1578 if (req
->flags
& SET_WIN_STYLE
) win
->style
= req
->style
;
1579 if (req
->flags
& SET_WIN_EXSTYLE
) win
->ex_style
= req
->ex_style
;
1580 if (req
->flags
& SET_WIN_ID
) win
->id
= req
->id
;
1581 if (req
->flags
& SET_WIN_INSTANCE
) win
->instance
= req
->instance
;
1582 if (req
->flags
& SET_WIN_UNICODE
) win
->is_unicode
= req
->is_unicode
;
1583 if (req
->flags
& SET_WIN_USERDATA
) win
->user_data
= req
->user_data
;
1584 if (req
->flags
& SET_WIN_EXTRA
) memcpy( win
->extra_bytes
+ req
->extra_offset
,
1585 &req
->extra_value
, req
->extra_size
);
1587 /* changing window style triggers a non-client paint */
1588 if (req
->flags
& SET_WIN_STYLE
) win
->paint_flags
|= PAINT_NONCLIENT
;
1592 /* get a list of the window parents, up to the root of the tree */
1593 DECL_HANDLER(get_window_parents
)
1595 struct window
*ptr
, *win
= get_window( req
->handle
);
1597 user_handle_t
*data
;
1600 if (win
) for (ptr
= win
->parent
; ptr
; ptr
= ptr
->parent
) total
++;
1602 reply
->count
= total
;
1603 len
= min( get_reply_max_size(), total
* sizeof(user_handle_t
) );
1604 if (len
&& ((data
= set_reply_data_size( len
))))
1606 for (ptr
= win
->parent
; ptr
&& len
; ptr
= ptr
->parent
, len
-= sizeof(*data
))
1607 *data
++ = ptr
->handle
;
1612 /* get a list of the window children */
1613 DECL_HANDLER(get_window_children
)
1615 struct window
*ptr
, *parent
= get_window( req
->parent
);
1617 user_handle_t
*data
;
1622 LIST_FOR_EACH_ENTRY( ptr
, &parent
->children
, struct window
, entry
)
1624 if (req
->atom
&& get_class_atom(ptr
->class) != req
->atom
) continue;
1625 if (req
->tid
&& get_thread_id(ptr
->thread
) != req
->tid
) continue;
1629 reply
->count
= total
;
1630 len
= min( get_reply_max_size(), total
* sizeof(user_handle_t
) );
1631 if (len
&& ((data
= set_reply_data_size( len
))))
1633 LIST_FOR_EACH_ENTRY( ptr
, &parent
->children
, struct window
, entry
)
1635 if (len
< sizeof(*data
)) break;
1636 if (req
->atom
&& get_class_atom(ptr
->class) != req
->atom
) continue;
1637 if (req
->tid
&& get_thread_id(ptr
->thread
) != req
->tid
) continue;
1638 *data
++ = ptr
->handle
;
1639 len
-= sizeof(*data
);
1645 /* get a list of the window children that contain a given point */
1646 DECL_HANDLER(get_window_children_from_point
)
1648 struct user_handle_array array
;
1649 struct window
*parent
= get_window( req
->parent
);
1652 if (!parent
) return;
1654 array
.handles
= NULL
;
1657 if (!all_windows_from_point( parent
, req
->x
, req
->y
, &array
)) return;
1659 reply
->count
= array
.count
;
1660 len
= min( get_reply_max_size(), array
.count
* sizeof(user_handle_t
) );
1661 if (len
) set_reply_data_ptr( array
.handles
, len
);
1662 else free( array
.handles
);
1666 /* get window tree information from a window handle */
1667 DECL_HANDLER(get_window_tree
)
1669 struct window
*ptr
, *win
= get_window( req
->handle
);
1675 reply
->next_sibling
= 0;
1676 reply
->prev_sibling
= 0;
1677 reply
->first_sibling
= 0;
1678 reply
->last_sibling
= 0;
1679 reply
->first_child
= 0;
1680 reply
->last_child
= 0;
1684 struct window
*parent
= win
->parent
;
1685 reply
->parent
= parent
->handle
;
1686 reply
->owner
= win
->owner
;
1687 if ((ptr
= get_next_window( win
))) reply
->next_sibling
= ptr
->handle
;
1688 if ((ptr
= get_prev_window( win
))) reply
->prev_sibling
= ptr
->handle
;
1689 if ((ptr
= get_first_child( parent
))) reply
->first_sibling
= ptr
->handle
;
1690 if ((ptr
= get_last_child( parent
))) reply
->last_sibling
= ptr
->handle
;
1692 if ((ptr
= get_first_child( win
))) reply
->first_child
= ptr
->handle
;
1693 if ((ptr
= get_last_child( win
))) reply
->last_child
= ptr
->handle
;
1697 /* set the position and Z order of a window */
1698 DECL_HANDLER(set_window_pos
)
1700 const rectangle_t
*visible_rect
= NULL
, *valid_rects
= NULL
;
1701 struct window
*previous
= NULL
;
1702 struct window
*win
= get_window( req
->handle
);
1703 unsigned int flags
= req
->flags
;
1706 if (!win
->parent
) flags
|= SWP_NOZORDER
; /* no Z order for the desktop */
1708 if (!(flags
& SWP_NOZORDER
))
1710 if (!req
->previous
) /* special case: HWND_TOP */
1712 if (get_first_child(win
->parent
) == win
) flags
|= SWP_NOZORDER
;
1714 else if (req
->previous
== (user_handle_t
)1) /* special case: HWND_BOTTOM */
1716 previous
= get_last_child( win
->parent
);
1720 if (!(previous
= get_window( req
->previous
))) return;
1721 /* previous must be a sibling */
1722 if (previous
->parent
!= win
->parent
)
1724 set_error( STATUS_INVALID_PARAMETER
);
1728 if (previous
== win
) flags
|= SWP_NOZORDER
; /* nothing to do */
1731 /* window rectangle must be ordered properly */
1732 if (req
->window
.right
< req
->window
.left
|| req
->window
.bottom
< req
->window
.top
)
1734 set_error( STATUS_INVALID_PARAMETER
);
1738 if (get_req_data_size() >= sizeof(rectangle_t
)) visible_rect
= get_req_data();
1739 if (get_req_data_size() >= 3 * sizeof(rectangle_t
)) valid_rects
= visible_rect
+ 1;
1741 if (!visible_rect
) visible_rect
= &req
->window
;
1742 set_window_pos( win
, previous
, flags
, &req
->window
, &req
->client
, visible_rect
, valid_rects
);
1743 reply
->new_style
= win
->style
;
1747 /* get the window and client rectangles of a window */
1748 DECL_HANDLER(get_window_rectangles
)
1750 struct window
*win
= get_window( req
->handle
);
1754 reply
->window
= win
->window_rect
;
1755 reply
->visible
= win
->visible_rect
;
1756 reply
->client
= win
->client_rect
;
1761 /* get the window text */
1762 DECL_HANDLER(get_window_text
)
1764 struct window
*win
= get_window( req
->handle
);
1766 if (win
&& win
->text
)
1768 data_size_t len
= strlenW( win
->text
) * sizeof(WCHAR
);
1769 if (len
> get_reply_max_size()) len
= get_reply_max_size();
1770 set_reply_data( win
->text
, len
);
1775 /* set the window text */
1776 DECL_HANDLER(set_window_text
)
1778 struct window
*win
= get_window( req
->handle
);
1783 data_size_t len
= get_req_data_size() / sizeof(WCHAR
);
1786 if (!(text
= mem_alloc( (len
+1) * sizeof(WCHAR
) ))) return;
1787 memcpy( text
, get_req_data(), len
* sizeof(WCHAR
) );
1796 /* get the coordinates offset between two windows */
1797 DECL_HANDLER(get_windows_offset
)
1801 reply
->x
= reply
->y
= 0;
1804 if (!(win
= get_window( req
->from
))) return;
1805 while (win
&& !is_desktop_window(win
))
1807 reply
->x
+= win
->client_rect
.left
;
1808 reply
->y
+= win
->client_rect
.top
;
1814 if (!(win
= get_window( req
->to
))) return;
1815 while (win
&& !is_desktop_window(win
))
1817 reply
->x
-= win
->client_rect
.left
;
1818 reply
->y
-= win
->client_rect
.top
;
1825 /* get the visible region of a window */
1826 DECL_HANDLER(get_visible_region
)
1828 struct region
*region
;
1829 struct window
*top
, *win
= get_window( req
->window
);
1833 top
= get_top_clipping_window( win
);
1834 if ((region
= get_visible_region( win
, top
, req
->flags
)))
1837 map_win_region_to_screen( win
, region
);
1838 data
= get_region_data_and_free( region
, get_reply_max_size(), &reply
->total_size
);
1839 if (data
) set_reply_data_ptr( data
, reply
->total_size
);
1841 reply
->top_win
= top
->handle
;
1842 reply
->top_rect
= top
->visible_rect
;
1844 if (!is_desktop_window(win
))
1846 reply
->win_rect
= (req
->flags
& DCX_WINDOW
) ? win
->window_rect
: win
->client_rect
;
1847 client_to_screen_rect( top
->parent
, &reply
->top_rect
);
1848 client_to_screen_rect( win
->parent
, &reply
->win_rect
);
1852 reply
->win_rect
.left
= 0;
1853 reply
->win_rect
.top
= 0;
1854 reply
->win_rect
.right
= win
->client_rect
.right
- win
->client_rect
.left
;
1855 reply
->win_rect
.bottom
= win
->client_rect
.bottom
- win
->client_rect
.top
;
1860 /* get the window region */
1861 DECL_HANDLER(get_window_region
)
1863 struct window
*win
= get_window( req
->window
);
1867 if (win
->win_region
)
1869 rectangle_t
*data
= get_region_data( win
->win_region
, get_reply_max_size(), &reply
->total_size
);
1870 if (data
) set_reply_data_ptr( data
, reply
->total_size
);
1875 /* set the window region */
1876 DECL_HANDLER(set_window_region
)
1878 struct region
*region
= NULL
;
1879 struct window
*win
= get_window( req
->window
);
1883 if (get_req_data_size()) /* no data means remove the region completely */
1885 if (!(region
= create_region_from_req_data( get_req_data(), get_req_data_size() )))
1888 if (win
->win_region
) free_region( win
->win_region
);
1889 win
->win_region
= region
;
1893 /* get a window update region */
1894 DECL_HANDLER(get_update_region
)
1897 unsigned int flags
= req
->flags
;
1898 struct window
*from_child
= NULL
;
1899 struct window
*win
= get_window( req
->window
);
1904 if (req
->from_child
)
1908 if (!(from_child
= get_window( req
->from_child
))) return;
1910 /* make sure from_child is a child of win */
1912 while (ptr
&& ptr
!= win
) ptr
= ptr
->parent
;
1915 set_error( STATUS_INVALID_PARAMETER
);
1920 reply
->flags
= get_window_update_flags( win
, from_child
, flags
, &win
);
1921 reply
->child
= win
->handle
;
1923 if (flags
& UPDATE_NOREGION
) return;
1925 if (win
->update_region
)
1927 /* convert update region to screen coordinates */
1928 struct region
*region
= create_empty_region();
1930 if (!region
) return;
1931 if (!copy_region( region
, win
->update_region
))
1933 free_region( region
);
1936 map_win_region_to_screen( win
, region
);
1937 if (!(data
= get_region_data_and_free( region
, get_reply_max_size(),
1938 &reply
->total_size
))) return;
1939 set_reply_data_ptr( data
, reply
->total_size
);
1942 if (reply
->flags
& (UPDATE_PAINT
|UPDATE_INTERNALPAINT
)) /* validate everything */
1944 validate_whole_window( win
);
1948 if (reply
->flags
& UPDATE_NONCLIENT
) validate_non_client( win
);
1949 if (reply
->flags
& UPDATE_ERASE
)
1951 win
->paint_flags
&= ~PAINT_ERASE
;
1952 /* desktop window only gets erased, not repainted */
1953 if (is_desktop_window(win
)) validate_whole_window( win
);
1959 /* update the z order of a window so that a given rectangle is fully visible */
1960 DECL_HANDLER(update_window_zorder
)
1963 struct window
*ptr
, *win
= get_window( req
->window
);
1965 if (!win
|| !win
->parent
|| !is_visible( win
)) return; /* nothing to do */
1967 LIST_FOR_EACH_ENTRY( ptr
, &win
->parent
->children
, struct window
, entry
)
1969 if (ptr
== win
) break;
1970 if (!(ptr
->style
& WS_VISIBLE
)) continue;
1971 if (ptr
->ex_style
& WS_EX_TRANSPARENT
) continue;
1972 if (!intersect_rect( &tmp
, &ptr
->visible_rect
, &req
->rect
)) continue;
1973 if (ptr
->win_region
&& !rect_in_region( ptr
->win_region
, &req
->rect
)) continue;
1974 /* found a window obscuring the rectangle, now move win above this one */
1975 list_remove( &win
->entry
);
1976 list_add_before( &ptr
->entry
, &win
->entry
);
1982 /* mark parts of a window as needing a redraw */
1983 DECL_HANDLER(redraw_window
)
1985 struct region
*region
= NULL
;
1986 struct window
*win
= get_window( req
->window
);
1989 if (!is_visible( win
)) return; /* nothing to do */
1991 if (req
->flags
& (RDW_VALIDATE
|RDW_INVALIDATE
))
1993 if (get_req_data_size()) /* no data means whole rectangle */
1995 if (!(region
= create_region_from_req_data( get_req_data(), get_req_data_size() )))
2000 redraw_window( win
, region
, (req
->flags
& RDW_INVALIDATE
) && (req
->flags
& RDW_FRAME
),
2002 if (region
) free_region( region
);
2006 /* set a window property */
2007 DECL_HANDLER(set_window_property
)
2009 struct window
*win
= get_window( req
->window
);
2013 if (get_req_data_size())
2015 atom_t atom
= add_global_atom( NULL
, get_req_data(), get_req_data_size() / sizeof(WCHAR
) );
2018 set_property( win
, atom
, req
->handle
, PROP_TYPE_STRING
);
2019 release_global_atom( NULL
, atom
);
2022 else set_property( win
, req
->atom
, req
->handle
, PROP_TYPE_ATOM
);
2026 /* remove a window property */
2027 DECL_HANDLER(remove_window_property
)
2029 struct window
*win
= get_window( req
->window
);
2033 atom_t atom
= req
->atom
;
2034 if (get_req_data_size()) atom
= find_global_atom( NULL
, get_req_data(),
2035 get_req_data_size() / sizeof(WCHAR
) );
2036 if (atom
) reply
->handle
= remove_property( win
, atom
);
2041 /* get a window property */
2042 DECL_HANDLER(get_window_property
)
2044 struct window
*win
= get_window( req
->window
);
2048 atom_t atom
= req
->atom
;
2049 if (get_req_data_size()) atom
= find_global_atom( NULL
, get_req_data(),
2050 get_req_data_size() / sizeof(WCHAR
) );
2051 if (atom
) reply
->handle
= get_property( win
, atom
);
2056 /* get the list of properties of a window */
2057 DECL_HANDLER(get_window_properties
)
2059 property_data_t
*data
;
2060 int i
, count
, max
= get_reply_max_size() / sizeof(*data
);
2061 struct window
*win
= get_window( req
->window
);
2066 for (i
= count
= 0; i
< win
->prop_inuse
; i
++)
2067 if (win
->properties
[i
].type
!= PROP_TYPE_FREE
) count
++;
2068 reply
->total
= count
;
2070 if (count
> max
) count
= max
;
2071 if (!count
|| !(data
= set_reply_data_size( count
* sizeof(*data
) ))) return;
2073 for (i
= 0; i
< win
->prop_inuse
&& count
; i
++)
2075 if (win
->properties
[i
].type
== PROP_TYPE_FREE
) continue;
2076 data
->atom
= win
->properties
[i
].atom
;
2077 data
->string
= (win
->properties
[i
].type
== PROP_TYPE_STRING
);
2078 data
->handle
= win
->properties
[i
].handle
;
2085 /* get the new window pointer for a global window, checking permissions */
2086 /* helper for set_global_windows request */
2087 static int get_new_global_window( struct window
**win
, user_handle_t handle
)
2096 set_error( STATUS_ACCESS_DENIED
);
2099 *win
= get_window( handle
);
2100 return (*win
!= NULL
);
2103 /* Set/get the global windows */
2104 DECL_HANDLER(set_global_windows
)
2106 struct window
*new_shell_window
= shell_window
;
2107 struct window
*new_shell_listview
= shell_listview
;
2108 struct window
*new_progman_window
= progman_window
;
2109 struct window
*new_taskman_window
= taskman_window
;
2111 reply
->old_shell_window
= shell_window
? shell_window
->handle
: 0;
2112 reply
->old_shell_listview
= shell_listview
? shell_listview
->handle
: 0;
2113 reply
->old_progman_window
= progman_window
? progman_window
->handle
: 0;
2114 reply
->old_taskman_window
= taskman_window
? taskman_window
->handle
: 0;
2116 if (req
->flags
& SET_GLOBAL_SHELL_WINDOWS
)
2118 if (!get_new_global_window( &new_shell_window
, req
->shell_window
)) return;
2119 if (!get_new_global_window( &new_shell_listview
, req
->shell_listview
)) return;
2121 if (req
->flags
& SET_GLOBAL_PROGMAN_WINDOW
)
2123 if (!get_new_global_window( &new_progman_window
, req
->progman_window
)) return;
2125 if (req
->flags
& SET_GLOBAL_TASKMAN_WINDOW
)
2127 if (!get_new_global_window( &new_taskman_window
, req
->taskman_window
)) return;
2129 shell_window
= new_shell_window
;
2130 shell_listview
= new_shell_listview
;
2131 progman_window
= new_progman_window
;
2132 taskman_window
= new_taskman_window
;