/* $Id: Unwm.c,v 1.1 1997/04/15 10:09:52 archer Exp $
 *	UNWM.C - Handle windows that are unmanaged by WM
 *
 */
#include <Widget/UnWMP.h>


/**
 ** X11_NOWM_CREATE
 **	Call upon creation
 **/
void x11__nowm_create(X11_widget wdg, XEvent *dummy)
{
XSetWindowAttributes	attr;
int			w, h;
/*
 * Nothing to do
 */
	if (wdg->id)
		return;
/*
 * Ok, create X11 window
 */
	attr.background_pixel  = wdg->background;
	attr.border_pixel      = wdg->border_color;
	attr.event_mask        = wdg->event_mask;
	attr.override_redirect = True;
	attr.save_under        = True;
	w = wdg->w - wdg->border2;
	if (w <= 0)
		w = 1;
	h = wdg->h - wdg->border2;
	if (h <= 0)
		h = 1;
	wdg->id = XCreateWindow(x11__screen, x11__root_id,
				wdg->x, wdg->y, w, h, wdg->border2/2,
				CopyFromParent, CopyFromParent, CopyFromParent,
				(CWBackPixel|CWBorderPixel|CWOverrideRedirect|
				 CWSaveUnder|CWEventMask), &attr);
	if (wdg->use_cursor)
		XDefineCursor(x11__screen, wdg->id, wdg->use_cursor);
	if (!wdg->unmapped)
		XMapWindow(x11__screen, wdg->id);
}


/**
 ** X11_WIDGET_ROOT_POSITION
 **	Find the position of a widget over the root window
 **	(not the window position, the position of 0,0 inside - see border)
 **/
void X11_WidgetRootLocation(X11_widget wdg, int *x, int *y)
{
XWindowAttributes	attr;
Window			w;
Window			root, parent;
Window			*children;
unsigned int		trash;
int			at_x, at_y;
/*
 * Find out where is the widget mapped
 */
	at_x = 0;
	at_y = 0;
	w    = wdg->id;
	while (1) {
		XGetWindowAttributes(x11__screen, w, &attr);
		at_x += attr.x + attr.border_width;
		at_y += attr.y + attr.border_width;
/*
 * Which is the parent?
 */
/*** HACK ***/
		XQueryTree(x11__screen, w, &root, &parent, &children, &trash);
		if (children)
			XFree(children);
		if (root == parent)
			break;
		w = parent;
	}
/*
 * Return results
 */
	if (x)
		*x = at_x;
	if (y)
		*y = at_y;
}
