/*
 *	SetColors.c - Copyright © 1990 by Devil's child.
 *
 *	Created:	23 Nov 1990  14:15:11
 *	Modified:	28 Nov 1990  17:57:19
 *
 *	Make>> cc -qf -ps -sb -so -wu -wd -wp -hi ram:small.pre <file>.c
 *	Make>> ln -m <file>.o -larps -lreq
 */

#include <dos_functions.h>
#include <arpdos_pragmas.h>

#define NUMGADGETS 4

extern int _argc;
extern char **_argv;
extern struct WBStartup *WBenchMsg;
extern struct ExecBase *SysBase;
extern struct ArpBase *ArpBase;
struct ReqLib *ReqBase;
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Window *Win;
struct Screen *S;
struct ColorMap *ColorMap;
struct Preferences Prefs;
struct FileReq FileReq;
char Dir[REQ_DSIZE+1];
char File[REQ_FCHARS+1];
char Path[REQ_DSIZE+REQ_FCHARS+2];
struct GadgetBlock GadgetArray[NUMGADGETS];
BOOL WorkBench;

char *GadgetText[] = {
	" Palette ",
	"Load File",
	"Save File",
	"Save Pref"
};

struct NewWindow NWS = {
	0, 0,					/* LeftEdge, TopEdge */
	87, 69,					/* Width, Height */
	3, 2,					/* DetailPen, BlockPen */
	GADGETUP|CLOSEWINDOW,	/* IDCMPFlags */
	SMART_REFRESH|NOCAREREFRESH|ACTIVATE|WINDOWCLOSE|WINDOWDRAG,		/* Flags */
	NULL,					/* FirstGadget */
	NULL,					/* CheckMark */
	(UBYTE *)"Colors ",		/* Title */
	NULL,					/* Screen */
	NULL,					/* BitMap */
	0, 0,					/* MinWidth, MinHeight */
	-1, -1,					/* MaxWidth, MaxHeight */
	CUSTOMSCREEN			/* Type (of screen) */
};


void SetPref(void)
{
	USHORT *ColorPtr;
	short i;

	if (WorkBench) {
		GetPrefs(&Prefs, 232);
		ColorPtr = &Prefs.color0;
		for( i=0 ; i<4 ; i++ )
			*ColorPtr++ = GetRGB4(ColorMap, i);
		SetPrefs(&Prefs, 232, TRUE);
	}
}


void LoadColors(char *file)
{
	BPTR fh;
	USHORT RGB[32]; 	/* 32 color registers max */
	short nc, count;

	if (fh = Open(file, MODE_OLDFILE)) {
		nc = Read(fh, (char *)RGB, 2 << S->BitMap.Depth);
		Close(fh);
	}
	else {
		SimpleRequest("Colors Request", "Couldn't open '%s'", file);
		return;
	}
	if (nc < 0) {
		SimpleRequest("Colors Request", "Error reading '%s'\nError code %ld", file, IoErr());
		return;
	}
	if (nc > 0)	/* less than 2^depth colors have been read */
		count = nc >> 1;
	else
		count = 1 << S->BitMap.Depth;
	LoadRGB4(&S->ViewPort, RGB, count);
	SetPref();
}


void SaveColors(char *file)
{
	BPTR fh;
	USHORT RGB[32]; 	/* 32 color registers max */
	short i;

	for( i=0 ; i < (1 << S->BitMap.Depth) ; i++ )
		RGB[i] = GetRGB4(ColorMap, i);
	if (fh = Open(file, MODE_NEWFILE)) {
		Write(fh, (char *)RGB, 2 << S->BitMap.Depth);
		Close(fh);
	}
	else
		SimpleRequest("Colors Request", "Couldn't open '%s'", file);
}


void SavePref(void)
{
	BPTR fh;

	GetPrefs(&Prefs, 232);
	if (fh = Open("devs:system-configuration", MODE_NEWFILE)) {
		Write(fh, (char *)&Prefs, 232);
		Close(fh);
	}
	else
		SimpleRequest("Colors Request", "Couldn't open 'devs:system-configuration'");
}


void main(short argc, char *argv[])
{
	struct Process *MyProcess;
	APTR OldWindowPtr;
	struct IntuiMessage *Message;
	ULONG Class;
	SHORT ID, y, i;
	BOOL quit = FALSE;
	char file[256];

	if (!(ReqBase = (struct ReqLib *)ArpOpenLibrary("req.library", 1L))) {
		if (!WBenchMsg) Puts("No req.library!");
		ArpExit(20, 0);
	}
	GfxBase = (struct GfxBase *)ArpBase->GfxBase;
	IntuitionBase = (struct IntuitionBase *)ArpBase->IntuiBase;
	if (argc>0 && argv[1])
		Delay(LMult(Atol(argv[1]),50));
	NWS.Screen = S = IntuitionBase->ActiveScreen;
	ColorMap = S->ViewPort.ColorMap;
	WorkBench = (S->Flags & 0x000E) ? FALSE : TRUE;

	for( i=0, y=14 ; i < NUMGADGETS ; i++ ) {
		LinkGadget(&GadgetArray[i], GadgetText[i], &NWS, 8, y);
		GadgetArray[i].Gadget.GadgetID = i;
		y += 14;
	}
	if (!WorkBench)
		GadgetArray[3].Gadget.Flags |= GADGDISABLED;
	Center(&NWS, 0, 0);

	if (argc>0 && argv[0]) {
		if (BaseName(argv[0]) == argv[0])
			SPrintf(file, "devs:%s.clr", argv[0]);
		else
			SPrintf(file, "%s.clr", argv[0]);
		LoadColors(file);
		return;
	}

	if (!(Win = OpenWindow(&NWS)))
		ArpExit(20, 0);
	MyProcess = (struct Process *)SysBase->ThisTask;
	OldWindowPtr = MyProcess->pr_WindowPtr;
	MyProcess->pr_WindowPtr = (APTR)Win;

	FileReq.Dir = Dir;
	FileReq.File = File;
	FileReq.PathName = Path;
	FileReq.dirnamescolor = 3;
	FileReq.devicenamescolor = 3;
	FileReq.stringnamecolor = 2;
	FileReq.stringgadgetcolor = 2;
	FileReq.boxbordercolor = 2;
	FileReq.gadgetboxcolor = 2;
	strcpy(FileReq.Show, "*.clr");
	strcpy(Dir, "devs:");

	while (!quit) {
		WaitPort(Win->UserPort);
		while (Message = (struct IntuiMessage *)GetMsg(Win->UserPort)) {
			Class = Message->Class;
			ID = ((struct Gadget *)Message->IAddress)->GadgetID;
			ReplyMsg((struct Message *) Message);
			switch (Class) {
			case CLOSEWINDOW:
				quit = TRUE;
				break;
			case GADGETUP:
				switch(ID) {
				case 0:
					if (ColorRequester(1) != 0xFFFF)
						SetPref();
					break;
				case 1:
					FileReq.Title = "Load Colors";
					FileReq.Flags = FRQCACHINGM|FRQLOADINGM;
					if (FileRequester(&FileReq))
						LoadColors(Path);
					break;
				case 2:
					FileReq.Title = "Save Colors";
					FileReq.Flags = FRQCACHINGM|FRQSAVINGM;
					if (FileRequester(&FileReq))
						SaveColors(Path);
					break;
				case 3:
					if (TwoGadRequest("Colors Request", "Really update system-configuration ?"))
						SavePref();
					break;
				}
				break;
			}
		}
	}
	PurgeFiles(&FileReq);
	MyProcess->pr_WindowPtr = OldWindowPtr;
	CloseWindowSafely(Win, NULL);
}


void _cli_parse(struct Process *pp, long alen, char *aptr)
{
	_argv = ArpAlloc(12);
	_argc = (int)GADS(aptr, alen, "Usage: SetColors [ColorFile] [DELAY secs]", _argv, "ColorFile,DELAY/k");
	if (_argc < 0) {
		Puts(_argv[0]);
		ArpExit(20L, ERROR_LINE_TOO_LONG);
	}
}

