#include <stdlib.h>
#include <string.h>

#include "wimpt.h"
#include "wimp.h"
#include "baricon.h"
#include "event.h"
#include "dbox.h"
#include "menu.h"
#include "res.h"
#include "resspr.h"
#include "template.h"
#include "win.h"
#include "werr.h"


#define WozMenuInfo 1
#define WozMenuQuit 2

static wimp_i im;
static BOOL WozDboxOpen= FALSE;
static dbox WozDbox;


#define ICONDRIVE1 16
#define ICONDRIVE2 19
#define ICONDISKBOX 22

static void WozEventHandler(dbox d, void *handle)
{
dbox_field df;

df= dbox_get(d);
switch(df)
	{
	static char pathname[80]= "filer_opendir ";
	case dbox_CLOSE:
		dbox_hide(d);
		WozDboxOpen= FALSE;
		break;
	case ICONDRIVE1+2:
	case ICONDRIVE2+2:
		wimp_set_icon_state(dbox_syshandle(d),(wimp_i) df-2,
			wimp_ISELECTED, 0);
		dbox_setnumeric(d, df-1, FALSE);
		dbox_fadefield(d, df-1);
		dbox_fadefield(d, df);
		dbox_setfield(d, df-2,"Empty");
		break;
	case ICONDISKBOX:
		strcpy(pathname+14, getenv("Woz$Dir"));
		system(strcat(pathname,".dBox"));
		break;
	}
}

static BOOL WozRawEventHandler(dbox d,void *event, void *handle)
{
wimp_eventstr *e= (wimp_eventstr *) event;
handle= handle;
switch(e->e)
	{
	wimp_i icon;
	case wimp_ESEND:
	case wimp_ESENDWANTACK:
		icon= e->data.msg.data.dataload.i;
		if((icon== ICONDRIVE1) || (icon== ICONDRIVE2))
			{
			int i;
			char *pstr;
			wimp_set_icon_state(dbox_syshandle(WozDbox),icon,
				wimp_ISELECTED,wimp_ISELECTED);
			dbox_unfadefield(d, icon+1);
			dbox_unfadefield(d, icon+2);
			pstr= e->data.msg.data.dataload.name;
			for(i= strlen(pstr); pstr[i-1]!= '.'; i--);
			dbox_setfield(d, icon, pstr+i); 
/*			werr(FALSE,"File %s",pstr); */
			if(e->e == wimp_ESENDWANTACK)
				{
				e->data.msg.hdr.your_ref= e->data.msg.hdr.my_ref;
				e->data.msg.hdr.action= wimp_MDATALOADOK;
				wimp_sendmessage(wimp_ESEND,&(e->data.msg),
					e->data.msg.hdr.task);
				}
			return(TRUE);
			}
		else
			return(FALSE);
	default:
		return(FALSE);
	}
}

static void WozIconClick(wimp_i icon)
{
if(!WozDboxOpen)
	{
	icon= icon;
	dbox_showstatic(WozDbox);
	WozDboxOpen= TRUE;
	}
} 

static void WozCreateDbox(char *name)
{
WozDbox= dbox_new(name);
dbox_fadefield(WozDbox, 8);
dbox_fadefield(WozDbox, 17);
dbox_fadefield(WozDbox, 18);
dbox_fadefield(WozDbox, 20);
dbox_fadefield(WozDbox, 21);
dbox_setfield(WozDbox, ICONDRIVE1, "Empty");
dbox_setfield(WozDbox, ICONDRIVE2, "Empty");
}


static void affiche_info(void)
{
dbox d= dbox_new("ProgInfo");
if(d)
	{
	dbox_show(d);
	dbox_fillin(d);
	dbox_dispose(&d);
	}
}

static void menuproc(void *handle, char *hit)
{
switch(hit[0])
	{
	case WozMenuInfo:
		affiche_info();
		break;
	case WozMenuQuit:
		handle= handle;
		exit(0);
	}
}

int main()
{
menu mon_menu;

int vers;

vers= wimpt_init("Woz");
res_init("Woz");
resspr_init();
template_init();
dbox_init();

mon_menu= menu_new("Woz",">Info,Quit");

WozCreateDbox("MainWindow");
im= baricon_textandsprite("!woz","Woz",3,(int) resspr_area(),
	WozIconClick);

dbox_eventhandler(WozDbox,WozEventHandler, (void *) 1);

dbox_raw_eventhandler(WozDbox,WozRawEventHandler, (void *) 1);

event_attachmenu(win_ICONBAR, mon_menu,menuproc,0);
event_setmask(wimp_ENULL | wimp_EPTRENTER | wimp_EPTRLEAVE);
while(TRUE) event_process();
}
