2 * Server-side window handling
4 * Copyright (C) 2001 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 /* a window property */
37 unsigned short type; /* property type (see below) */
38 atom_t atom; /* property atom */
39 handle_t handle; /* property handle (user-defined storage) */
44 PROP_TYPE_FREE, /* free entry */
45 PROP_TYPE_STRING, /* atom that was originally a string */
46 PROP_TYPE_ATOM /* plain atom */
52 struct window *parent; /* parent window */
53 struct window *owner; /* owner of this window */
54 struct window *first_child; /* first child in Z-order */
55 struct window *last_child; /* last child in Z-order */
56 struct window *first_unlinked; /* first child not linked in the Z-order list */
57 struct window *next; /* next window in Z-order */
58 struct window *prev; /* prev window in Z-order */
59 user_handle_t handle; /* full handle for this window */
60 struct thread *thread; /* thread owning the window */
61 atom_t atom; /* class atom */
62 rectangle_t window_rect; /* window rectangle */
63 rectangle_t client_rect; /* client rectangle */
64 unsigned int style; /* window style */
65 unsigned int ex_style; /* window extended style */
66 unsigned int id; /* window id */
67 void* instance; /* creator instance */
68 void* user_data; /* user-specific data */
69 WCHAR *text; /* window caption text */
70 int paint_count; /* count of pending paints for this window */
71 int prop_inuse; /* number of in-use window properties */
72 int prop_alloc; /* number of allocated window properties */
73 struct property *properties; /* window properties array */
76 static struct window *top_window; /* top-level (desktop) window */
79 /* retrieve a pointer to a window from its handle */
80 inline static struct window *get_window( user_handle_t handle )
82 struct window *ret = get_user_object( handle, USER_WINDOW );
83 if (!ret) set_error( STATUS_INVALID_HANDLE );
87 /* unlink a window from the tree */
88 static void unlink_window( struct window *win )
90 struct window *parent = win->parent;
94 if (win->next) win->next->prev = win->prev;
95 else if (parent->last_child == win) parent->last_child = win->prev;
97 if (win->prev) win->prev->next = win->next;
98 else if (parent->first_child == win) parent->first_child = win->next;
99 else if (parent->first_unlinked == win) parent->first_unlinked = win->next;
103 /* link a window into the tree (or unlink it if the new parent is NULL) */
104 static void link_window( struct window *win, struct window *parent, struct window *previous )
106 unlink_window( win ); /* unlink it from the previous location */
110 if (win->parent != parent)
112 win->owner = NULL; /* reset owner if changing parent */
113 win->parent = parent;
115 if ((win->prev = previous))
117 if ((win->next = previous->next)) win->next->prev = win;
118 else if (win->parent->last_child == previous) win->parent->last_child = win;
119 win->prev->next = win;
123 if ((win->next = parent->first_child)) win->next->prev = win;
124 else win->parent->last_child = win;
125 parent->first_child = win;
128 else /* move it to parent unlinked list */
130 parent = win->parent;
131 if ((win->next = parent->first_unlinked)) win->next->prev = win;
133 parent->first_unlinked = win;
137 /* set a window property */
138 static void set_property( struct window *win, atom_t atom, handle_t handle,
139 enum property_type type )
142 struct property *new_props;
144 /* check if it exists already */
145 for (i = 0; i < win->prop_inuse; i++)
147 if (win->properties[i].type == PROP_TYPE_FREE)
152 if (win->properties[i].atom == atom)
154 win->properties[i].type = type;
155 win->properties[i].handle = handle;
160 /* need to add an entry */
161 if (!grab_global_atom( atom )) return;
165 if (win->prop_inuse >= win->prop_alloc)
167 /* need to grow the array */
168 if (!(new_props = realloc( win->properties,
169 sizeof(*new_props) * (win->prop_alloc + 16) )))
171 set_error( STATUS_NO_MEMORY );
172 release_global_atom( atom );
175 win->prop_alloc += 16;
176 win->properties = new_props;
178 free = win->prop_inuse++;
180 win->properties[free].atom = atom;
181 win->properties[free].type = type;
182 win->properties[free].handle = handle;
185 /* remove a window property */
186 static handle_t remove_property( struct window *win, atom_t atom )
190 for (i = 0; i < win->prop_inuse; i++)
192 if (win->properties[i].type == PROP_TYPE_FREE) continue;
193 if (win->properties[i].atom == atom)
195 release_global_atom( atom );
196 win->properties[i].type = PROP_TYPE_FREE;
197 return win->properties[i].handle;
200 /* FIXME: last error? */
204 /* find a window property */
205 static handle_t get_property( struct window *win, atom_t atom )
209 for (i = 0; i < win->prop_inuse; i++)
211 if (win->properties[i].type == PROP_TYPE_FREE) continue;
212 if (win->properties[i].atom == atom) return win->properties[i].handle;
214 /* FIXME: last error? */
218 /* destroy all properties of a window */
219 inline static void destroy_properties( struct window *win )
223 if (!win->properties) return;
224 for (i = 0; i < win->prop_inuse; i++)
226 if (win->properties[i].type == PROP_TYPE_FREE) continue;
227 release_global_atom( win->properties[i].atom );
229 free( win->properties );
232 /* destroy a window */
233 static void destroy_window( struct window *win )
235 assert( win != top_window );
237 /* destroy all children */
238 while (win->first_child) destroy_window( win->first_child );
239 while (win->first_unlinked) destroy_window( win->first_unlinked );
241 /* reset siblings owner */
245 for (ptr = win->parent->first_child; ptr; ptr = ptr->next)
246 if (ptr->owner == win) ptr->owner = NULL;
247 for (ptr = win->parent->first_unlinked; ptr; ptr = ptr->next)
248 if (ptr->owner == win) ptr->owner = NULL;
251 if (win->thread->queue)
253 if (win->paint_count) inc_queue_paint_count( win->thread, -win->paint_count );
254 queue_cleanup_window( win->thread, win->handle );
256 free_user_handle( win->handle );
257 destroy_properties( win );
258 unlink_window( win );
259 if (win->text) free( win->text );
260 memset( win, 0x55, sizeof(*win) );
264 /* create a new window structure (note: the window is not linked in the window tree) */
265 static struct window *create_window( struct window *parent, struct window *owner, atom_t atom )
267 struct window *win = mem_alloc( sizeof(*win) );
268 if (!win) return NULL;
270 if (!(win->handle = alloc_user_handle( win, USER_WINDOW )))
275 win->parent = parent;
277 win->first_child = NULL;
278 win->last_child = NULL;
279 win->first_unlinked = NULL;
280 win->thread = current;
285 win->instance = NULL;
286 win->user_data = NULL;
288 win->paint_count = 0;
291 win->properties = NULL;
293 if (parent) /* put it on parent unlinked list */
295 if ((win->next = parent->first_unlinked)) win->next->prev = win;
297 parent->first_unlinked = win;
299 else win->next = win->prev = NULL;
304 /* destroy all windows belonging to a given thread */
305 void destroy_thread_windows( struct thread *thread )
307 user_handle_t handle = 0;
310 while ((win = next_user_handle( &handle, USER_WINDOW )))
312 if (win->thread != thread) continue;
313 destroy_window( win );
317 /* check whether child is a descendant of parent */
318 int is_child_window( user_handle_t parent, user_handle_t child )
320 struct window *child_ptr = get_user_object( child, USER_WINDOW );
321 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
323 if (!child_ptr || !parent_ptr) return 0;
324 while (child_ptr->parent)
326 if (child_ptr->parent == parent_ptr) return 1;
327 child_ptr = child_ptr->parent;
333 /* find a child of the specified window that needs repainting */
334 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
336 struct window *ptr, *ret = NULL;
338 for (ptr = parent->first_child; ptr && !ret; ptr = ptr->next)
340 if (!(ptr->style & WS_VISIBLE)) continue;
341 if (ptr->paint_count && ptr->thread == thread)
343 else /* explore its children */
344 ret = find_child_to_repaint( ptr, thread );
347 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
349 /* transparent window, check for non-transparent sibling to paint first */
350 for (ptr = ret->next; ptr; ptr = ptr->next)
352 if (!(ptr->style & WS_VISIBLE)) continue;
353 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
354 if (ptr->paint_count && ptr->thread == thread) return ptr;
361 /* find a window that needs repainting */
362 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
364 struct window *win = parent ? get_window( parent ) : top_window;
366 if (!win || !(win->style & WS_VISIBLE)) return 0;
367 if (!win->paint_count || win->thread != thread)
368 win = find_child_to_repaint( win, thread );
369 return win ? win->handle : 0;
373 /* create a window */
374 DECL_HANDLER(create_window)
377 if (!req->parent) /* return desktop window */
381 if (!(top_window = create_window( NULL, NULL, req->atom ))) return;
382 top_window->thread = NULL; /* no thread owns the desktop */
384 reply->handle = top_window->handle;
388 struct window *win, *parent, *owner = NULL;
390 if (!(parent = get_window( req->parent ))) return;
391 if (req->owner && !(owner = get_window( req->owner ))) return;
392 if (owner == top_window) owner = NULL;
393 else if (owner && owner->parent != parent)
395 /* owner must be a sibling of the new window */
396 set_error( STATUS_ACCESS_DENIED );
399 if (!(win = create_window( parent, owner, req->atom ))) return;
400 reply->handle = win->handle;
405 /* link a window into the tree */
406 DECL_HANDLER(link_window)
408 struct window *win, *parent = NULL, *previous = NULL;
410 if (!(win = get_window( req->handle ))) return;
411 if (req->parent && !(parent = get_window( req->parent ))) return;
413 if (win == top_window)
415 set_error( STATUS_INVALID_PARAMETER );
418 reply->full_parent = parent ? parent->handle : 0;
419 if (parent && req->previous)
421 if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
423 previous = parent->last_child;
424 if (previous == win) return; /* nothing to do */
428 if (!(previous = get_window( req->previous ))) return;
429 /* previous must be a child of parent, and not win itself */
430 if (previous->parent != parent || previous == win)
432 set_error( STATUS_INVALID_PARAMETER );
437 link_window( win, parent, previous );
441 /* destroy a window */
442 DECL_HANDLER(destroy_window)
444 struct window *win = get_window( req->handle );
447 if (win != top_window) destroy_window( win );
448 else set_error( STATUS_ACCESS_DENIED );
453 /* set a window owner */
454 DECL_HANDLER(set_window_owner)
456 struct window *win = get_window( req->handle );
457 struct window *owner = get_window( req->owner );
459 if (!win || !owner) return;
460 if (owner->parent != win->parent)
462 /* owner has to be a sibling of window */
463 set_error( STATUS_ACCESS_DENIED );
467 reply->full_owner = owner->handle;
471 /* get information from a window handle */
472 DECL_HANDLER(get_window_info)
474 struct window *win = get_window( req->handle );
476 reply->full_handle = 0;
477 reply->tid = reply->pid = 0;
480 reply->full_handle = win->handle;
483 reply->tid = get_thread_id( win->thread );
484 reply->pid = get_process_id( win->thread->process );
485 reply->atom = win->atom;
491 /* set some information in a window */
492 DECL_HANDLER(set_window_info)
494 struct window *win = get_window( req->handle );
496 reply->old_style = win->style;
497 reply->old_ex_style = win->ex_style;
498 reply->old_id = win->id;
499 reply->old_instance = win->instance;
500 reply->old_user_data = win->user_data;
501 if (req->flags & SET_WIN_STYLE) win->style = req->style;
502 if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
503 if (req->flags & SET_WIN_ID) win->id = req->id;
504 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
505 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
509 /* get a list of the window parents, up to the root of the tree */
510 DECL_HANDLER(get_window_parents)
512 struct window *ptr, *win = get_window( req->handle );
517 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
519 reply->count = total;
520 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
521 if (len && ((data = set_reply_data_size( len ))))
523 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
524 *data++ = ptr->handle;
529 /* get a list of the window children */
530 DECL_HANDLER(get_window_children)
532 struct window *ptr, *parent = get_window( req->parent );
538 for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
540 if (req->atom && ptr->atom != req->atom) continue;
541 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
545 reply->count = total;
546 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
547 if (len && ((data = set_reply_data_size( len ))))
549 for (ptr = parent->first_child; ptr && len; ptr = ptr->next, len -= sizeof(*data))
551 if (req->atom && ptr->atom != req->atom) continue;
552 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
553 *data++ = ptr->handle;
559 /* get window tree information from a window handle */
560 DECL_HANDLER(get_window_tree)
562 struct window *win = get_window( req->handle );
568 struct window *parent = win->parent;
569 reply->parent = parent->handle;
570 reply->owner = win->owner ? win->owner->handle : 0;
571 reply->next_sibling = win->next ? win->next->handle : 0;
572 reply->prev_sibling = win->prev ? win->prev->handle : 0;
573 reply->first_sibling = parent->first_child ? parent->first_child->handle : 0;
574 reply->last_sibling = parent->last_child ? parent->last_child->handle : 0;
580 reply->next_sibling = 0;
581 reply->prev_sibling = 0;
582 reply->first_sibling = 0;
583 reply->last_sibling = 0;
585 reply->first_child = win->first_child ? win->first_child->handle : 0;
586 reply->last_child = win->last_child ? win->last_child->handle : 0;
590 /* set the window and client rectangles of a window */
591 DECL_HANDLER(set_window_rectangles)
593 struct window *win = get_window( req->handle );
597 win->window_rect = req->window;
598 win->client_rect = req->client;
603 /* get the window and client rectangles of a window */
604 DECL_HANDLER(get_window_rectangles)
606 struct window *win = get_window( req->handle );
610 reply->window = win->window_rect;
611 reply->client = win->client_rect;
616 /* get the window text */
617 DECL_HANDLER(get_window_text)
619 struct window *win = get_window( req->handle );
621 if (win && win->text)
623 size_t len = strlenW( win->text ) * sizeof(WCHAR);
624 if (len > get_reply_max_size()) len = get_reply_max_size();
625 set_reply_data( win->text, len );
630 /* set the window text */
631 DECL_HANDLER(set_window_text)
633 struct window *win = get_window( req->handle );
638 size_t len = get_req_data_size() / sizeof(WCHAR);
641 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
642 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
645 if (win->text) free( win->text );
651 /* increment the window paint count */
652 DECL_HANDLER(inc_window_paint_count)
654 struct window *win = get_window( req->handle );
656 if (win && win->thread)
658 int old = win->paint_count;
659 if ((win->paint_count += req->incr) < 0) win->paint_count = 0;
660 inc_queue_paint_count( win->thread, win->paint_count - old );
665 /* get the coordinates offset between two windows */
666 DECL_HANDLER(get_windows_offset)
670 reply->x = reply->y = 0;
673 if (!(win = get_window( req->from ))) return;
676 reply->x += win->client_rect.left;
677 reply->y += win->client_rect.top;
683 if (!(win = get_window( req->to ))) return;
686 reply->x -= win->client_rect.left;
687 reply->y -= win->client_rect.top;
694 /* set a window property */
695 DECL_HANDLER(set_window_property)
697 struct window *win = get_window( req->window );
699 if (win) set_property( win, req->atom, req->handle,
700 req->string ? PROP_TYPE_STRING : PROP_TYPE_ATOM );
704 /* remove a window property */
705 DECL_HANDLER(remove_window_property)
707 struct window *win = get_window( req->window );
709 if (win) reply->handle = remove_property( win, req->atom );
713 /* get a window property */
714 DECL_HANDLER(get_window_property)
716 struct window *win = get_window( req->window );
718 if (win) reply->handle = get_property( win, req->atom );
722 /* get the list of properties of a window */
723 DECL_HANDLER(get_window_properties)
725 property_data_t *data;
726 int i, count, max = get_reply_max_size() / sizeof(*data);
727 struct window *win = get_window( req->window );
732 for (i = count = 0; i < win->prop_inuse; i++)
733 if (win->properties[i].type != PROP_TYPE_FREE) count++;
734 reply->total = count;
736 if (count > max) count = max;
737 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
739 for (i = 0; i < win->prop_inuse && count; i++)
741 if (win->properties[i].type == PROP_TYPE_FREE) continue;
742 data->atom = win->properties[i].atom;
743 data->string = (win->properties[i].type == PROP_TYPE_STRING);
744 data->handle = win->properties[i].handle;