/**********************************************************************
 
	Copyright (C) 2003 Hirohisa MORI <joshua@nichibun.ac.jp>
 
	This program is free software; you can redistribute it 
	and/or modify it under the terms of the GLOBALBASE 
	Library General Public License (G-LGPL) as published by 

	http://www.globalbase.org/
 
	This program 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.

**********************************************************************/


#include	<stdlib.h>
#include	<X11/Xatom.h>
#include	"memory_debug.h"
#include	"v.h"


int
_v_open_win(V_OPEN_WIN_S * v)
{
VWINDOW * win;
XSetWindowAttributes attr;
unsigned long vmask;
XStandardColormap cmap;
int er;
 long valuemask;

VDISPLAY * d;
int x;
int y;
int w;
int h;
int ret,count;
XStandardColormap std_colmaps;

	d = v->d;
	x = v->x;
	y = v->y;
	w = v->w;
	h = v->h;

	win = d_alloc(sizeof(*win),156);
	win->d = d;

	memset(&attr,0,sizeof(attr));

	valuemask = CWBackPixel|CWBorderPixel;

	attr.background_pixel = 0;
	attr.border_pixel = 0;

	if ( d->visual->class == DirectColor  ) {
		attr.colormap = d->cmap;
		valuemask |= CWColormap;
	}

	win->w = XCreateWindow(
		d->d,
		d->r,
		x,y,
		w,h,
		0,
		d->depth,
		InputOutput,
		d->visual,
		valuemask,
		&attr);


	win->gc = XCreateGC(d->d,win->w,0,0);	
	XSelectInput(d->d,win->w,
		ExposureMask|
		ButtonPressMask|
		ButtonReleaseMask|
		ButtonMotionMask|
		PointerMotionMask);
	if ( v->map )
		XMapWindow(d->d,win->w);
	XFlush(d->d);
	win->next = d->win_list;
	d->win_list = win;
	win->obj_list = 0;
	v->err->err1 = E_OK;
	v->ret = win;
	return 0;
}

VWINDOW *
v_open_win(VERROR * err,VDISPLAY * d,int x,int y,int w,int h)
{
V_OPEN_WIN_S v;
	v.err = err;
	v.d = d;
	v.x = x;
	v.y = y;
	v.w = w;
	v.h = h;
	v.ret = 0;
	v.map = 1;
	ms_do(_v_open_win,&v,"v_open_win");
	return v.ret;
}


typedef struct v_get_window_attributes_s {
	VERROR * err;
	VWINDOW_ATTRIBUTES * attr;
	VWINDOW * win;
} V_GET_WINDOW_ATTRIBUTES_S;

int
_v_get_window_attributes(V_GET_WINDOW_ATTRIBUTES_S * v)
{
XWindowAttributes xattr;
VWINDOW_ATTRIBUTES * attr;
VWINDOW * win;

	attr = v->attr;
	win = v->win;

	XGetWindowAttributes(
		win->d->d,
		win->w,
		&xattr);
	attr->w = xattr.width;
	attr->h = xattr.height;
	attr->x = xattr.x;
	attr->y = xattr.y;
	v->err->err1 = E_OK;
	return 0;
}


void
v_get_window_attributes(
	VERROR * err,
	VWINDOW_ATTRIBUTES * attr,
	VWINDOW * win)
{
V_GET_WINDOW_ATTRIBUTES_S v;
	v.err = err;
	v.attr = attr;
	v.win = win;
	ms_do(_v_get_window_attributes,&v,"v_get_window_attribute");
}



typedef struct v_clear_window_content_s {
        VERROR * err;
        VWINDOW * win;
} V_CLEAR_WINDOW_CONTENT_S;


int
_v_clear_window_content(V_CLEAR_WINDOW_CONTENT_S *v)
{
Window w = v->win->w;
Display *d = v->win->d->d;
        XSetWindowBackground(d,w,BlackPixel(d,0));
        XClearWindow(d,w);
        v->err->err1 = E_OK;
        return 0;
}


void
v_clear_window_content(VERROR * err, VWINDOW *win)
{
V_CLEAR_WINDOW_CONTENT_S v;
        v.err = err;
        v.win = win;
        ms_do((int(*)())_v_clear_window_content,&v,"v_clear_window_content");
}

