#ifndef CODE_TOOLS_CXX
#define CODE_TOOLS_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: tools.cxx,v 1.1.1.1 1998/07/16 19:29:27 cvs Exp $)
 * 
 * Copyright (C) 1998 Jürgen Schmitz
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *-----------------------------------------------------------------------
 */

#include <string.h>
#include "types.h"
#include <Xclasses/tools.h>
#include <Xclasses/Xdisplay.h>
#include <Xclasses/appwindow.h>
#include <Xclasses/Xwindows.h>
#include <Xclasses/dlist.h>

void GetPositionOnWindow(Display *d,Window win,Window root,int &x,int &y)
{
#ifdef OLDWAY
 	int s;
 	Window r,p,*c;
 	unsigned int n;
	XWindowAttributes xwa;

	x=0;
	y=0;

 	do
 	{
 		XGetWindowAttributes(d,win,&xwa);
 		x+=xwa.x+xwa.border_width;
 		y+=xwa.y+xwa.border_width;
 
 		s=XQueryTree(d,win,&r,&p,&c,&n);
 		if (s)
 		{
 			win=p;
 			XFree(c);
 		}
 	}
 	while (s && p!=root);
#else
	XWindowAttributes xwa;
	Window w;

	x=0;
	y=0;
	XGetWindowAttributes(d,win,&xwa);
	XTranslateCoordinates(d,win,root,xwa.border_width,xwa.border_width,&x,&y,&w);
#endif
}
void GetPositionOnRoot(Display *d,Window win,int &x,int &y)
{
	GetPositionOnWindow(d,win,DefaultRootWindow(d),x,y);
}
Window GetParentWindow(Display *d,Window win)
{
	int s;
	Window r,p,*c;
	unsigned int n;

	s=XQueryTree(d,win,&r,&p,&c,&n);
	if (s)
	{
		if (c) XFree(c);
	}
	else
	{
		p=0;
	}
	return p;
}
int IsParentWindow(Display *d,Window win,Window win2)
{
	if (win==0 || win2==0) return 0;
	if (win==win2) return 1;
	Window w=win;
	do
	{
		w=GetParentWindow(d,w);
	}
	while (w && w!=win2);
	return (w==win2);
}
void DisplaySize(Display *d,int &x,int &y)
{
	XWindowAttributes xwa;
	XGetWindowAttributes(d,DefaultRootWindow(d),&xwa);
	x=xwa.width;
	y=xwa.height;
}
appwindow *GetAppwindowOf(Display *d,Window win)
{
	struct node *n;
	n=allapps.tail;
	while (n->prev)
	{
		if (win==((appwindow*)n->text)->window()) return (appwindow*)n->text;
		n=n->prev;
	}
	n=allapps.tail;
	while (n->prev)
	{
		if (IsParentWindow(d,win,((appwindow*)n->text)->window())) return (appwindow*)n->text;
		n=n->prev;
	}
	return NULL;
}
Xwindows *GetXwindowsOf(Display *d,Window win)
{
	struct node *n;
	n=allXwins.tail;
	while (n->prev)
	{
		if (win==((Xwindows*)n->text)->window()) return (Xwindows*)n->text;
		n=n->prev;
	}
	n=allXwins.tail;
	while (n->prev)
	{
		if (IsParentWindow(d,win,((Xwindows*)n->text)->window())) return (Xwindows*)n->text;
		n=n->prev;
	}
	return NULL;
}
void WMPositionTranslate(Display *d,Window win,int &wx,int &wy)
{
 	int s=0,x,y;
 	Window r,p,*c;
 	unsigned int n;
	XWindowAttributes xwa;

	x=0;
	y=0;

 	do
 	{
 		XGetWindowAttributes(d,win,&xwa);
 		x+=xwa.x+xwa.border_width;
 		y+=xwa.y+xwa.border_width;
 		if (!s && xwa.x==wx && xwa.y==wy)
 		{ // IRIX 4Dwm workaround
// klappt nicht: Probleme
 			continue;
 		}
 
 		s=XQueryTree(d,win,&r,&p,&c,&n);
 		if (s)
 		{
 			win=p;
 			XFree(c);
 		}
 	}
 	while (s && p!=DefaultRootWindow(d));
	x-=xwa.x+xwa.border_width;
	y-=xwa.y+xwa.border_width;

	wx-=x;
	wy-=y;
}

void WMPosition(Display *d,Window win,int &wx,int &wy)
{
 	int s=0;
 	Window r,p,*c;
 	unsigned int n;

	wx=0;
	wy=0;

 	do
 	{
 		s=XQueryTree(d,win,&r,&p,&c,&n);
		if (p==DefaultRootWindow(d))
		{
			XWindowAttributes xwa;
	 		XGetWindowAttributes(d,win,&xwa);
 			wx=xwa.x+xwa.border_width;
	 		wy=xwa.y+xwa.border_width;
		}
 		if (s)
 		{
 			win=p;
 			XFree(c);
 		}
 	}
 	while (s && p!=DefaultRootWindow(d));
}

static Window FindWindowOnServer(Display *d,char *name,Window base,XClassHint *xch)
{
	int s;
	Window r,p,*c,ret=0;
	unsigned int n;

	s=XQueryTree(d,base,&r,&p,&c,&n);
	if (s)
	{
		if (n>0)
		{
			for(int i=0;i<n && !ret;i++)
			{
				if (XGetClassHint(d,c[i],xch))
				{
					if (!strcmp(name,xch->res_class))
					{
						ret=c[i];
					}
					XFree(xch->res_name);
					XFree(xch->res_class);
				}
				ret=ret | FindWindowOnServer(d,name,c[i],xch);
			}
		}
		if (c) XFree(c);
	}
	return ret;
}

Window FindWindowOnServer(Display *d,char *wname)
{
	Window ret=0;
	XClassHint *xch=XAllocClassHint();
	if (xch)
	{
		ret=FindWindowOnServer(d,wname,DefaultRootWindow(d),xch);
		XFree(xch);
	}
	return ret;
}

#endif
