#include "inc.h"
#include "defs.h"
#include <signal.h>

#define GadgetList1 Gadget1

struct TRStructure TxReq = { 0,0,0,0,0,0,NULL,0xFFFF,0,0,0,0,0,0 };
struct Window *win, *win1, *win2, *win3, *win4, *win5, *win6,*win7;
struct RastPort 	*rp, *rp1, *rp2, *rp3, *rp4, *rp5, *rp6, *rp7;


/* win.c */
void Iconify(void);
void Make_Screen(void);
void Make_Window(void);
void Make_SingleReqWindow(int xp, int yp);
void Make_MultiReqWindow(int xp, int yp);
void Make_SingleWindow(char *string);
void Make_MultiWindow(char *string);
void Make_PropWindow(int hoff);
void Make_EnvWindow(void);
void Print(struct Window *win, long x, long y, char *string);
void ColPrint(struct Window *win, long x, long y, char *string);
void MyCenter(struct Window *win, long y, char *string);
void Line(struct Window *win, long x1, long y1, long x2, long y2);
void About(void);
short BoolRequest(char *mytext, char *yestext, char *notext);
void SetTitle(char *string);
int MyReq(char *text, char *postext, char *negtext);

extern struct Gadget Gadget1;

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

void Iconify(void)
{
	int OLDSCREEN;

	OLDSCREEN=SCREEN;

	if (SCREEN != 0) 
	{	
		SCREEN=0;
		CloseWindow(win);
		CloseScreen(scr);
		Make_Window();	
	}
	
	SizeWindow(win,-(win->Width)+200,-(win->Height)+11);
	MODWIN
Loop:	WaitPort(win->UserPort);

	while(mesg=(struct IntuiMessage *) GetMsg(win->UserPort))
	{
		if (mesg->Class != MOUSEBUTTONS && mesg->Code != SELECTUP)
		{
			ReplyMsg((struct Message *) mesg);
			goto Loop;
		}
		ReplyMsg((struct Message *) mesg);
	}
	Delay(10);

Loop1:	WaitPort(win->UserPort);
	mesg=(struct IntuiMessage *) GetMsg(win->UserPort);

	if (mesg->Class == CLOSEWINDOW) 
	{
		ReplyMsg((struct Message *) mesg);
		Quit();
		goto Loop1;
	}

	if (mesg->Class == ACTIVEWINDOW)
	{
		ReplyMsg((struct Message *) mesg);
		SetColours();
		goto Loop1;
	}

	if (mesg->Class ==INACTIVEWINDOW)
	{
		ReplyMsg((struct Message *) mesg);
		SetOldColours();
		goto Loop1;
	}

	if (mesg->Class != MOUSEBUTTONS && mesg->Code != MENUDOWN)
	{
		ReplyMsg((struct Message *) mesg);
		goto Loop1;
	}

	ReplyMsg((struct Message *) mesg);
	
	SCREEN=OLDSCREEN;

	if (SCREEN==0)
	{
		SetColours();
		MoveWindow(win,-(win->LeftEdge),-(win->TopEdge));
		SizeWindow(win,BREITE-win->Width-5,HOEHE-win->Height-1);
		Delay(20);
	}
	else
	{
		SetOldColours();
		CloseWindow(win);
		Make_Screen();
		Make_Window();
	}	
	Display();
}

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

void Make_Screen(void)
{
	struct NewScreen ns;

	if (textfont==NULL)
	{
		textattr.ta_Name = (STRPTR)"topaz.font";      
		textattr.ta_YSize = TOPAZ_EIGHTY;              
		textattr.ta_Style = FS_NORMAL;              
		textattr.ta_Flags = FPF_ROMFONT;
		textfont = (struct TextFont *) OpenFont(&textattr);
	}

	ns.LeftEdge = 0;
	ns.TopEdge = 0;
	ns.Width = BREITE-1;
	ns.Height = HOEHE+1;
	ns.Depth = 2;
	ns.DetailPen = 0;
	ns.BlockPen = 1;
	ns.ViewModes = HIRES|(WBLACE != 0 ? LACE : 0);
	ns.Type = CUSTOMSCREEN;
	ns.Font = &textattr;
	ns.DefaultTitle =(char *)NULL; 
	ns.Gadgets = NULL;
	ns.CustomBitMap = NULL;

	scr=(struct Screen *) OpenScreen(&ns);
	if (scr==NULL) Error("Can't open screen");

	if (scr != NULL)
	{
		FILE *fp;

#ifndef _DCC
		FILE *fopen();
#endif
		long a,b,c,d;

		fp=fopen("DEVS:K1-LibColours","r");
		if (fp!=NULL)
		{
			fscanf(fp, "%ld%ld%ld%ld", &a, &b, &c, &d);
			fclose(fp);
			SetRGB4(&scr->ViewPort, 0, (a & 0xf00) >> 8, (a & 0x0f0) >> 4, a & 0x00f );
			SetRGB4(&scr->ViewPort, 1, (b & 0xf00) >> 8, (b & 0x0f0) >> 4, b & 0x00f );
			SetRGB4(&scr->ViewPort, 2, (c & 0xf00) >> 8, (c & 0x0f0) >> 4, c & 0x00f );
			SetRGB4(&scr->ViewPort, 3, (d & 0xf00) >> 8, (d & 0x0f0) >> 4, d & 0x00f );
		}
	}
}

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

void Make_Window(void)
{
	struct NewWindow nw;
	
	Gadget1.LeftEdge=625-(SCREEN==1 ? 2 :0);

	nw.LeftEdge = 0;
	nw.TopEdge = (SCREEN != 0 ? 1 : 0);
	nw.Width = BREITE-1;
	nw.Height = HOEHE-1;
	nw.DetailPen = 0;
	nw.BlockPen = 1;
	nw.Title =(UBYTE *)"KAWAI K1-II Librarian V "VERSION"  Written by Andreas Jung in 1990-93";
	nw.Flags = RMBTRAP|ACTIVATE|WINDOWCLOSE|REPORTMOUSE|SMART_REFRESH|(SCREEN == 0 ? WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH : NULL) ;
	nw.IDCMPFlags =NEWSIZE|CLOSEWINDOW|MOUSEBUTTONS|GADGETDOWN|GADGETUP;
	nw.Type = (SCREEN != 0 ? CUSTOMSCREEN : WBENCHSCREEN);
	nw.FirstGadget = &GadgetList1;
	nw.CheckMark = NULL;
	nw.Screen = (SCREEN != 0 ? scr :NULL);
	nw.BitMap = NULL;
	nw.MinWidth = 140;
	nw.MinHeight = 10;
	nw.MaxWidth = nw.Width;
	nw.MaxHeight = nw.Height;

	win=(struct Window *) OpenWindow(&nw);
	if (win==NULL) Error("Can't open main window");
	rp=win->RPort;

	if (textfont==NULL)
	{
		textattr.ta_Name = (STRPTR)"topaz.font";      
		textattr.ta_YSize = 8;              
		textattr.ta_Style = 0;              
		textattr.ta_Flags = 0;
		textfont = (struct TextFont *) OpenFont(&textattr);
		if (textfont != NULL) SetFont(rp,textfont);              
	}

	myprocess = (struct Process *)FindTask((char *)0);
	olderrorwindow = myprocess->pr_WindowPtr;
	myprocess->pr_WindowPtr = (APTR)win;
}

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

void Make_SingleReqWindow(int xp,int yp)
{
	struct NewWindow nw;

	nw.LeftEdge = xp+win->LeftEdge;
	nw.TopEdge = yp+win->TopEdge-10;
	nw.Width = 130;
	nw.Height = 73;
	nw.DetailPen = 0;
	nw.BlockPen = 2;
	nw.Title =NULL;
	nw.Flags = ACTIVATE|REPORTMOUSE|SMART_REFRESH ;
	nw.IDCMPFlags = MOUSEBUTTONS;
	nw.Type = (SCREEN != 0 ? CUSTOMSCREEN : WBENCHSCREEN);
	nw.FirstGadget = NULL;
	nw.CheckMark = NULL;
	nw.Screen = (SCREEN != 0 ? scr :NULL);
	nw.BitMap = NULL;
	nw.MinWidth = 0;
	nw.MinHeight = 0;
	nw.MaxWidth = 0;
	nw.MaxHeight = 0;

	win2=(struct Window *) OpenWindow(&nw);
	if (win2==NULL) Error("Can't open single requester");
	rp2=win2->RPort;
	SetFont(rp2,textfont);
}

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

void Make_MultiReqWindow(int xp,int yp)
{
	struct NewWindow nw;

	nw.LeftEdge = xp+win->LeftEdge;
	nw.TopEdge = yp+win->TopEdge-10;
	nw.Width = 130;
	nw.Height = 53;
	nw.DetailPen = 0;
	nw.BlockPen = 2;
	nw.Title =NULL;
	nw.Flags = ACTIVATE|REPORTMOUSE|SMART_REFRESH ;
	nw.IDCMPFlags = MOUSEBUTTONS;
	nw.Type = (SCREEN != 0 ? CUSTOMSCREEN : WBENCHSCREEN);
	nw.FirstGadget = NULL;
	nw.CheckMark = NULL;
	nw.Screen = (SCREEN != 0 ? scr :NULL);
	nw.BitMap = NULL;
	nw.MinWidth = 0;
	nw.MinHeight = 0;
	nw.MaxWidth = 0;
	nw.MaxHeight = 0;

	win6=(struct Window *) OpenWindow(&nw);
	if (win6==NULL) Error("Can't open multi requester");
	rp6=win6->RPort;
	SetFont(rp6,textfont);
}

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

void Make_SingleWindow(char *string)
{
	struct NewWindow nw;

	nw.LeftEdge = 150;
	nw.TopEdge = 0;
	nw.Width = 430;
	nw.Height = 180;
	nw.DetailPen = 0;
	nw.BlockPen = 1;
	nw.Title =(UBYTE *)string;
	nw.Flags = RMBTRAP|ACTIVATE|WINDOWCLOSE|REPORTMOUSE|SMART_REFRESH|(SCREEN == 0 ? WINDOWDRAG|WINDOWDEPTH : NULL) ;
	nw.IDCMPFlags = NEWSIZE|CLOSEWINDOW|MOUSEBUTTONS;
	nw.Type = (SCREEN != 0 ? CUSTOMSCREEN : WBENCHSCREEN);
	nw.FirstGadget = NULL;
	nw.CheckMark = NULL;
	nw.Screen = (SCREEN != 0 ? scr :NULL);
	nw.BitMap = NULL;
	nw.MinWidth = 140;
	nw.MinHeight = 20;
	nw.MaxWidth = nw.Width;
	nw.MaxHeight = nw.Height;

	Center(&nw,0,0);	
	win3=(struct Window *) OpenWindow(&nw);
	if (win3==NULL) Error("Can't open single window");
	rp3=win3->RPort;
	SetFont(rp3,textfont);
}

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

void Make_PropWindow(int hoff)
{
	struct NewWindow nw;

	nw.LeftEdge = 150;
	nw.TopEdge = 0;
	nw.Width = 140;
	nw.Height = 33;
	nw.DetailPen = 0;
	nw.BlockPen = 1;
	nw.Title =(char *)NULL;
	nw.Flags = RMBTRAP|ACTIVATE|REPORTMOUSE|SMART_REFRESH;
	nw.IDCMPFlags =NEWSIZE|CLOSEWINDOW|MOUSEBUTTONS|GADGETUP|GADGETDOWN;
	nw.Type = (SCREEN != 0 ? CUSTOMSCREEN : WBENCHSCREEN);
	nw.FirstGadget = NULL;
	nw.CheckMark = NULL;
	nw.Screen = (SCREEN != 0 ? scr :NULL);
	nw.BitMap = NULL;
	nw.MinWidth = 140;
	nw.MinHeight = 20;
	nw.MaxWidth = nw.Width;
	nw.MaxHeight = nw.Height;

	Center(&nw,hoff,0);	
	win4=(struct Window *) OpenWindow(&nw);
	if (win4==NULL) Error("Can't open prop-window");
	rp4=win4->RPort;
	SetFont(rp4,textfont);
}

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

void Make_EnvWindow(void)
{
	struct NewWindow nw;

	nw.LeftEdge = 260;
	nw.TopEdge = 21;
	nw.Width = 196;
	nw.Height = 55;
	nw.DetailPen = 0;
	nw.BlockPen = 1;
	nw.Title =(char *)NULL;
	nw.Flags =NOCAREREFRESH;
	nw.IDCMPFlags =MOUSEBUTTONS;
	nw.Type = (SCREEN != 0 ? CUSTOMSCREEN : WBENCHSCREEN);
	nw.FirstGadget = NULL;
	nw.CheckMark = NULL;
	nw.Screen = (SCREEN != 0 ? scr :NULL);
	nw.BitMap = NULL;
	nw.MinWidth = 0;
	nw.MinHeight = 0;
	nw.MaxWidth = 0;
	nw.MaxHeight = 0;

	win5=(struct Window *) OpenWindow(&nw);
	if (win5==NULL) Error("Can't open env-window");
	rp5=win5->RPort;
	SetFont(rp5,textfont);
}

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

void Make_MultiWindow(char *string)
{
	struct NewWindow nw;

	nw.LeftEdge = 150;
	nw.TopEdge = 0;
	nw.Width = 230;
	nw.Height = 180;
	nw.DetailPen = 0;
	nw.BlockPen = 1;
	nw.Title =(UBYTE *)string;
	nw.Flags = RMBTRAP|ACTIVATE|WINDOWCLOSE|REPORTMOUSE|SMART_REFRESH|(SCREEN == 0 ? WINDOWDRAG|WINDOWDEPTH : NULL) ;
	nw.IDCMPFlags = NEWSIZE|CLOSEWINDOW|MOUSEBUTTONS;
	nw.Type = (SCREEN != 0 ? CUSTOMSCREEN : WBENCHSCREEN);
	nw.FirstGadget = NULL;
	nw.CheckMark = NULL;
	nw.Screen = (SCREEN != 0 ? scr :NULL);
	nw.BitMap = NULL;
	nw.MinWidth = 140;
	nw.MinHeight = 20;
	nw.MaxWidth = nw.Width;
	nw.MaxHeight = nw.Height;

	Center(&nw,0,0);	
	win7=(struct Window *) OpenWindow(&nw);
	if (win7==NULL) Error("Can't open multi window");
	rp7=win7->RPort;
	SetFont(rp7,textfont);
}


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

void Print(struct Window *win,long x,long y,char *string)
{
	SetAPen(win->RPort,1);
	SetBPen(win->RPort,0);
	Move(win->RPort,x,y);
	Text(win->RPort,string,strlen(string));
}

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

void ColPrint(struct Window *win,long x,long y,char *string)
{
	SetAPen(win->RPort,2);
	SetBPen(win->RPort,0);
	Move(win->RPort,x,y);
	Text(win->RPort,string,1);
	if (strlen(string)>1)
	{
		SetAPen(win->RPort,1);	
		Move(win->RPort,x+8,y);
		Text(win->RPort,&string[1],strlen(string)-1);
	}
}

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

void MyCenter(struct Window *win,long y,char *string)
{
	SetAPen(win->RPort,1);
	SetDrMd(win->RPort,JAM2);
	Move(win->RPort,(win->Width)/2-strlen(string)*4,y);
	Text(win->RPort,string,strlen(string));
}

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

void Line(struct Window *win,long x1,long y1,long x2,long y2)
{
	Move(win->RPort,x1,y1);
	Draw(win->RPort,x2,y2);
}

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

void About(void)
{
	MyReq("\
                Kawai K1-II Librarian Version "VERSION"\n"
	"                       Release: "__DATE__"\n\n"
	"                       Written in 1990-93 by \n"
	"                            Andreas Jung\n"
	"                          Klosterstraße 21\n"
	"                          D-6602 Dudweiler\n"
	"                    Federal Republic of Germany\n\n"
	"  This program is freeware and may be freely distributed  \n"
	"  for noncommercial use. Commercial distribution requieres \n"
	"  the written permission of the author. All rights reserved !\n\n"
	"  The K1-Librarian uses the Midi-library and the Req-library.\n"
	"  Midi-library is (C) 1987, 1988 by Pregnant Badger Music.\n"
	"  Req-library is (C) 1989 by Colin Fox and Bruce Dawson.\n\n"
	"  Sleepy mouse pointer written by Timm Martin.\n\n"
	"  Input handler written by Paris E. Bingham Jr.\n\n"
	"  Special thanks to Michael Balzer for his famous K1-editor,\n"
	"  the sources and the Req-library documentation. Thank you.\n",NULL," Ok ");
}

/**********************************************************************************/
/***	                Stinknormaler Boolrequester                             ***/
/**********************************************************************************/

BOOL BoolRequest(char *mytext,char *yestext,char *notext)
{
	struct IntuiText text=
	{ 0,1,JAM1,17,10,NULL,NULL,NULL };
	struct IntuiText postext=
	{ 0,1,JAM1,7,4,NULL,NULL,NULL };
	struct IntuiText negtext=
	{ 0,1,JAM1,7,4,NULL,NULL,NULL };

	text.IText 	 =(UBYTE *)	mytext;
	postext.IText=(UBYTE *)	yestext;
	negtext.IText=(UBYTE *)	notext;
	
	return(AutoRequest(NULL,&text,&postext,&negtext,NULL,NULL,strlen(mytext)*9+30,55));
}

/**********************************************************************************/
/***	              	  Setzen des Fenstertitels                            	***/
/**********************************************************************************/

void SetTitle(char *string)
{
	SetWindowTitles(win,string,(char *)-1);
}

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

int MyReq(char *text,char *postext,char *negtext) 
{
	TxReq.Text			= text;
	TxReq.NegativeText	= negtext;
	TxReq.PositiveText	= postext;
	TxReq.MiddleText 	= NULL;
	TxReq.Title			= NULL;
	TxReq.versionnumber = REQVERSION;
	TxReq.Timeout		= 25;
	TxReq.Window		= win;

	return TextRequest(&TxReq);
}

