2 * Server-side window handling
4 * Copyright (C) 2001 Alexandre Julliard
20 /* a window property */
23 unsigned short type; /* property type (see below) */
24 atom_t atom; /* property atom */
25 handle_t handle; /* property handle (user-defined storage) */
30 PROP_TYPE_FREE, /* free entry */
31 PROP_TYPE_STRING, /* atom that was originally a string */
32 PROP_TYPE_ATOM /* plain atom */
38 struct window *parent; /* parent window */
39 struct window *owner; /* owner of this window */
40 struct window *first_child; /* first child in Z-order */
41 struct window *last_child; /* last child in Z-order */
42 struct window *first_unlinked; /* first child not linked in the Z-order list */
43 struct window *next; /* next window in Z-order */
44 struct window *prev; /* prev window in Z-order */
45 user_handle_t handle; /* full handle for this window */
46 struct thread *thread; /* thread owning the window */
47 atom_t atom; /* class atom */
48 rectangle_t window_rect; /* window rectangle */
49 rectangle_t client_rect; /* client rectangle */
50 unsigned int style; /* window style */
51 unsigned int ex_style; /* window extended style */
52 unsigned int id; /* window id */
53 void* instance; /* creator instance */
54 void* user_data; /* user-specific data */
55 WCHAR *text; /* window caption text */
56 int paint_count; /* count of pending paints for this window */
57 int prop_inuse; /* number of in-use window properties */
58 int prop_alloc; /* number of allocated window properties */
59 struct property *properties; /* window properties array */
62 static struct window *top_window; /* top-level (desktop) window */
65 /* retrieve a pointer to a window from its handle */
66 inline static struct window *get_window( user_handle_t handle )
68 struct window *ret = get_user_object( handle, USER_WINDOW );
69 if (!ret) set_error( STATUS_INVALID_HANDLE );
73 /* unlink a window from the tree */
74 static void unlink_window( struct window *win )
76 struct window *parent = win->parent;
80 if (win->next) win->next->prev = win->prev;
81 else if (parent->last_child == win) parent->last_child = win->prev;
83 if (win->prev) win->prev->next = win->next;
84 else if (parent->first_child == win) parent->first_child = win->next;
85 else if (parent->first_unlinked == win) parent->first_unlinked = win->next;
89 /* link a window into the tree (or unlink it if the new parent is NULL) */
90 static void link_window( struct window *win, struct window *parent, struct window *previous )
92 unlink_window( win ); /* unlink it from the previous location */
96 if (win->parent != parent)
98 win->owner = NULL; /* reset owner if changing parent */
101 if ((win->prev = previous))
103 if ((win->next = previous->next)) win->next->prev = win;
104 else if (win->parent->last_child == previous) win->parent->last_child = win;
105 win->prev->next = win;
109 if ((win->next = parent->first_child)) win->next->prev = win;
110 else win->parent->last_child = win;
111 parent->first_child = win;
114 else /* move it to parent unlinked list */
116 parent = win->parent;
117 if ((win->next = parent->first_unlinked)) win->next->prev = win;
119 parent->first_unlinked = win;
123 /* set a window property */
124 static void set_property( struct window *win, atom_t atom, handle_t handle,
125 enum property_type type )
128 struct property *new_props;
130 /* check if it exists already */
131 for (i = 0; i < win->prop_inuse; i++)
133 if (win->properties[i].type == PROP_TYPE_FREE)
138 if (win->properties[i].atom == atom)
140 win->properties[i].type = type;
141 win->properties[i].handle = handle;
146 /* need to add an entry */
147 if (!grab_global_atom( atom )) return;
151 if (win->prop_inuse >= win->prop_alloc)
153 /* need to grow the array */
154 if (!(new_props = realloc( win->properties,
155 sizeof(*new_props) * (win->prop_alloc + 16) )))
157 set_error( STATUS_NO_MEMORY );
158 release_global_atom( atom );
161 win->prop_alloc += 16;
162 win->properties = new_props;
164 free = win->prop_inuse++;
166 win->properties[free].atom = atom;
167 win->properties[free].type = type;
168 win->properties[free].handle = handle;
171 /* remove a window property */
172 static handle_t remove_property( struct window *win, atom_t atom )
176 for (i = 0; i < win->prop_inuse; i++)
178 if (win->properties[i].type == PROP_TYPE_FREE) continue;
179 if (win->properties[i].atom == atom)
181 release_global_atom( atom );
182 win->properties[i].type = PROP_TYPE_FREE;
183 return win->properties[i].handle;
186 /* FIXME: last error? */
190 /* find a window property */
191 static handle_t get_property( struct window *win, atom_t atom )
195 for (i = 0; i < win->prop_inuse; i++)
197 if (win->properties[i].type == PROP_TYPE_FREE) continue;
198 if (win->properties[i].atom == atom) return win->properties[i].handle;
200 /* FIXME: last error? */
204 /* destroy all properties of a window */
205 inline static void destroy_properties( struct window *win )
209 if (!win->properties) return;
210 for (i = 0; i < win->prop_inuse; i++)
212 if (win->properties[i].type == PROP_TYPE_FREE) continue;
213 release_global_atom( win->properties[i].atom );
215 free( win->properties );
218 /* destroy a window */
219 static void destroy_window( struct window *win )
221 assert( win != top_window );
223 /* destroy all children */
224 while (win->first_child) destroy_window( win->first_child );
225 while (win->first_unlinked) destroy_window( win->first_unlinked );
227 /* reset siblings owner */
231 for (ptr = win->parent->first_child; ptr; ptr = ptr->next)
232 if (ptr->owner == win) ptr->owner = NULL;
233 for (ptr = win->parent->first_unlinked; ptr; ptr = ptr->next)
234 if (ptr->owner == win) ptr->owner = NULL;
237 if (win->thread->queue)
239 if (win->paint_count) inc_queue_paint_count( win->thread, -win->paint_count );
240 queue_cleanup_window( win->thread, win->handle );
242 free_user_handle( win->handle );
243 destroy_properties( win );
244 unlink_window( win );
245 if (win->text) free( win->text );
246 memset( win, 0x55, sizeof(*win) );
250 /* create a new window structure (note: the window is not linked in the window tree) */
251 static struct window *create_window( struct window *parent, struct window *owner, atom_t atom )
253 struct window *win = mem_alloc( sizeof(*win) );
254 if (!win) return NULL;
256 if (!(win->handle = alloc_user_handle( win, USER_WINDOW )))
261 win->parent = parent;
263 win->first_child = NULL;
264 win->last_child = NULL;
265 win->first_unlinked = NULL;
266 win->thread = current;
271 win->instance = NULL;
272 win->user_data = NULL;
274 win->paint_count = 0;
277 win->properties = NULL;
279 if (parent) /* put it on parent unlinked list */
281 if ((win->next = parent->first_unlinked)) win->next->prev = win;
283 parent->first_unlinked = win;
285 else win->next = win->prev = NULL;
290 /* destroy all windows belonging to a given thread */
291 void destroy_thread_windows( struct thread *thread )
293 user_handle_t handle = 0;
296 while ((win = next_user_handle( &handle, USER_WINDOW )))
298 if (win->thread != thread) continue;
299 destroy_window( win );
303 /* check whether child is a descendant of parent */
304 int is_child_window( user_handle_t parent, user_handle_t child )
306 struct window *child_ptr = get_user_object( child, USER_WINDOW );
307 struct window *parent_ptr = get_user_object( parent, USER_WINDOW );
309 if (!child_ptr || !parent_ptr) return 0;
310 while (child_ptr->parent)
312 if (child_ptr->parent == parent_ptr) return 1;
313 child_ptr = child_ptr->parent;
319 /* find a child of the specified window that needs repainting */
320 static struct window *find_child_to_repaint( struct window *parent, struct thread *thread )
322 struct window *ptr, *ret = NULL;
324 for (ptr = parent->first_child; ptr && !ret; ptr = ptr->next)
326 if (!(ptr->style & WS_VISIBLE)) continue;
327 if (ptr->paint_count && ptr->thread == thread)
329 else /* explore its children */
330 ret = find_child_to_repaint( ptr, thread );
333 if (ret && (ret->ex_style & WS_EX_TRANSPARENT))
335 /* transparent window, check for non-transparent sibling to paint first */
336 for (ptr = ret->next; ptr; ptr = ptr->next)
338 if (!(ptr->style & WS_VISIBLE)) continue;
339 if (ptr->ex_style & WS_EX_TRANSPARENT) continue;
340 if (ptr->paint_count && ptr->thread == thread) return ptr;
347 /* find a window that needs repainting */
348 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
350 struct window *win = parent ? get_window( parent ) : top_window;
352 if (!win || !(win->style & WS_VISIBLE)) return 0;
353 if (!win->paint_count || win->thread != thread)
354 win = find_child_to_repaint( win, thread );
355 return win ? win->handle : 0;
359 /* create a window */
360 DECL_HANDLER(create_window)
363 if (!req->parent) /* return desktop window */
367 if (!(top_window = create_window( NULL, NULL, req->atom ))) return;
368 top_window->thread = NULL; /* no thread owns the desktop */
370 reply->handle = top_window->handle;
374 struct window *win, *parent, *owner = NULL;
376 if (!(parent = get_window( req->parent ))) return;
377 if (req->owner && !(owner = get_window( req->owner ))) return;
378 if (owner == top_window) owner = NULL;
379 else if (owner && owner->parent != parent)
381 /* owner must be a sibling of the new window */
382 set_error( STATUS_ACCESS_DENIED );
385 if (!(win = create_window( parent, owner, req->atom ))) return;
386 reply->handle = win->handle;
391 /* link a window into the tree */
392 DECL_HANDLER(link_window)
394 struct window *win, *parent = NULL, *previous = NULL;
396 if (!(win = get_window( req->handle ))) return;
397 if (req->parent && !(parent = get_window( req->parent ))) return;
399 if (win == top_window)
401 set_error( STATUS_INVALID_PARAMETER );
404 reply->full_parent = parent ? parent->handle : 0;
405 if (parent && req->previous)
407 if (req->previous == (user_handle_t)1) /* special case: HWND_BOTTOM */
409 previous = parent->last_child;
410 if (previous == win) return; /* nothing to do */
414 if (!(previous = get_window( req->previous ))) return;
415 /* previous must be a child of parent, and not win itself */
416 if (previous->parent != parent || previous == win)
418 set_error( STATUS_INVALID_PARAMETER );
423 link_window( win, parent, previous );
427 /* destroy a window */
428 DECL_HANDLER(destroy_window)
430 struct window *win = get_window( req->handle );
433 if (win != top_window) destroy_window( win );
434 else set_error( STATUS_ACCESS_DENIED );
439 /* set a window owner */
440 DECL_HANDLER(set_window_owner)
442 struct window *win = get_window( req->handle );
443 struct window *owner = get_window( req->owner );
445 if (!win || !owner) return;
446 if (owner->parent != win->parent)
448 /* owner has to be a sibling of window */
449 set_error( STATUS_ACCESS_DENIED );
453 reply->full_owner = owner->handle;
457 /* get information from a window handle */
458 DECL_HANDLER(get_window_info)
460 struct window *win = get_window( req->handle );
462 reply->full_handle = 0;
463 reply->tid = reply->pid = 0;
466 reply->full_handle = win->handle;
469 reply->tid = get_thread_id( win->thread );
470 reply->pid = get_process_id( win->thread->process );
471 reply->atom = win->atom;
477 /* set some information in a window */
478 DECL_HANDLER(set_window_info)
480 struct window *win = get_window( req->handle );
482 reply->old_style = win->style;
483 reply->old_ex_style = win->ex_style;
484 reply->old_id = win->id;
485 reply->old_instance = win->instance;
486 reply->old_user_data = win->user_data;
487 if (req->flags & SET_WIN_STYLE) win->style = req->style;
488 if (req->flags & SET_WIN_EXSTYLE) win->ex_style = req->ex_style;
489 if (req->flags & SET_WIN_ID) win->id = req->id;
490 if (req->flags & SET_WIN_INSTANCE) win->instance = req->instance;
491 if (req->flags & SET_WIN_USERDATA) win->user_data = req->user_data;
495 /* get a list of the window parents, up to the root of the tree */
496 DECL_HANDLER(get_window_parents)
498 struct window *ptr, *win = get_window( req->handle );
503 if (win) for (ptr = win->parent; ptr; ptr = ptr->parent) total++;
505 reply->count = total;
506 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
507 if (len && ((data = set_reply_data_size( len ))))
509 for (ptr = win->parent; ptr && len; ptr = ptr->parent, len -= sizeof(*data))
510 *data++ = ptr->handle;
515 /* get a list of the window children */
516 DECL_HANDLER(get_window_children)
518 struct window *ptr, *parent = get_window( req->parent );
524 for (ptr = parent->first_child, total = 0; ptr; ptr = ptr->next)
526 if (req->atom && ptr->atom != req->atom) continue;
527 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
531 reply->count = total;
532 len = min( get_reply_max_size(), total * sizeof(user_handle_t) );
533 if (len && ((data = set_reply_data_size( len ))))
535 for (ptr = parent->first_child; ptr && len; ptr = ptr->next, len -= sizeof(*data))
537 if (req->atom && ptr->atom != req->atom) continue;
538 if (req->tid && get_thread_id(ptr->thread) != req->tid) continue;
539 *data++ = ptr->handle;
545 /* get window tree information from a window handle */
546 DECL_HANDLER(get_window_tree)
548 struct window *win = get_window( req->handle );
554 struct window *parent = win->parent;
555 reply->parent = parent->handle;
556 reply->owner = win->owner ? win->owner->handle : 0;
557 reply->next_sibling = win->next ? win->next->handle : 0;
558 reply->prev_sibling = win->prev ? win->prev->handle : 0;
559 reply->first_sibling = parent->first_child ? parent->first_child->handle : 0;
560 reply->last_sibling = parent->last_child ? parent->last_child->handle : 0;
566 reply->next_sibling = 0;
567 reply->prev_sibling = 0;
568 reply->first_sibling = 0;
569 reply->last_sibling = 0;
571 reply->first_child = win->first_child ? win->first_child->handle : 0;
572 reply->last_child = win->last_child ? win->last_child->handle : 0;
576 /* set the window and client rectangles of a window */
577 DECL_HANDLER(set_window_rectangles)
579 struct window *win = get_window( req->handle );
583 win->window_rect = req->window;
584 win->client_rect = req->client;
589 /* get the window and client rectangles of a window */
590 DECL_HANDLER(get_window_rectangles)
592 struct window *win = get_window( req->handle );
596 reply->window = win->window_rect;
597 reply->client = win->client_rect;
602 /* get the window text */
603 DECL_HANDLER(get_window_text)
605 struct window *win = get_window( req->handle );
607 if (win && win->text)
609 size_t len = strlenW( win->text ) * sizeof(WCHAR);
610 if (len > get_reply_max_size()) len = get_reply_max_size();
611 set_reply_data( win->text, len );
616 /* set the window text */
617 DECL_HANDLER(set_window_text)
619 struct window *win = get_window( req->handle );
624 size_t len = get_req_data_size() / sizeof(WCHAR);
627 if (!(text = mem_alloc( (len+1) * sizeof(WCHAR) ))) return;
628 memcpy( text, get_req_data(), len * sizeof(WCHAR) );
631 if (win->text) free( win->text );
637 /* increment the window paint count */
638 DECL_HANDLER(inc_window_paint_count)
640 struct window *win = get_window( req->handle );
642 if (win && win->thread)
644 int old = win->paint_count;
645 if ((win->paint_count += req->incr) < 0) win->paint_count = 0;
646 inc_queue_paint_count( win->thread, win->paint_count - old );
651 /* get the coordinates offset between two windows */
652 DECL_HANDLER(get_windows_offset)
656 reply->x = reply->y = 0;
659 if (!(win = get_window( req->from ))) return;
662 reply->x += win->client_rect.left;
663 reply->y += win->client_rect.top;
669 if (!(win = get_window( req->to ))) return;
672 reply->x -= win->client_rect.left;
673 reply->y -= win->client_rect.top;
680 /* set a window property */
681 DECL_HANDLER(set_window_property)
683 struct window *win = get_window( req->window );
685 if (win) set_property( win, req->atom, req->handle,
686 req->string ? PROP_TYPE_STRING : PROP_TYPE_ATOM );
690 /* remove a window property */
691 DECL_HANDLER(remove_window_property)
693 struct window *win = get_window( req->window );
695 if (win) reply->handle = remove_property( win, req->atom );
699 /* get a window property */
700 DECL_HANDLER(get_window_property)
702 struct window *win = get_window( req->window );
704 if (win) reply->handle = get_property( win, req->atom );
708 /* get the list of properties of a window */
709 DECL_HANDLER(get_window_properties)
711 property_data_t *data;
712 int i, count, max = get_reply_max_size() / sizeof(*data);
713 struct window *win = get_window( req->window );
718 for (i = count = 0; i < win->prop_inuse; i++)
719 if (win->properties[i].type != PROP_TYPE_FREE) count++;
720 reply->total = count;
722 if (count > max) count = max;
723 if (!count || !(data = set_reply_data_size( count * sizeof(*data) ))) return;
725 for (i = 0; i < win->prop_inuse && count; i++)
727 if (win->properties[i].type == PROP_TYPE_FREE) continue;
728 data->atom = win->properties[i].atom;
729 data->string = (win->properties[i].type == PROP_TYPE_STRING);
730 data->handle = win->properties[i].handle;