

#include "WINGsP.h"



typedef struct W_Window {
    W_Class widgetClass;
    W_View *view;
    
    char *caption;
    char *wclass;
    char *wname;

    struct {
	unsigned int configured:1;
    } flags;
} _Window;



struct W_ViewProcedureTable _WindowViewProcedures = {
    NULL,
	NULL,
	NULL,
	NULL
};


#define DEFAULT_WIDTH	400
#define DEFAULT_HEIGHT	180
#define DEFAULT_TITLE	"Untitled"


static void destroyWindow(_Window *win);


static void
handleEvents(XEvent *event, void *clientData)
{
    _Window *win = (_Window*)clientData;
    
    
    switch (event->type) {
     case DestroyNotify:
	destroyWindow(win);
	break;
    }
}


WMWindow*
WMCreateWindow(WMScreen *screen)
{
    _Window *win;
    
    win = wmalloc(sizeof(_Window));
    memset(win, 0, sizeof(_Window));
	   
    win->widgetClass = WC_Window;

    win->view = W_CreateTopView(screen);
    if (!win->view) {
	free(win);
	return NULL;
    }

    WMCreateEventHandler(win->view, ExposureMask|StructureNotifyMask
			 |FocusChangeMask, handleEvents, win);

    W_ResizeView(win->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
    
    return win;
}



void
WMSetWindowTitle(WMWindow *win, char *title)
{
    if (win->caption!=NULL)
	free(win->caption);
    if (title!=NULL)
	win->caption = wstrdup(title);
    else
	win->caption = NULL;
    
    if (win->view->flags.mapped) {
	XStoreName(win->view->screen->display, win->view->window, title);
    }
}


static void
destroyWindow(_Window *win)
{

    if (win->caption)
	free(win->caption);
    
    if (win->wclass)
	free(win->wclass);
    
    if (win->wname)
	free(win->wname);

    free(win);
}


