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

Programma ........ Window Color Modifier

Versione ......... 1.0, 30 giugno 1989

Autore ........... Flavio Stanchina

Scopo ............ Modifica colori di finestra Workbench

Software ......... Lattice C V5.04 || Aztec C V5.0a

Sintassi ......... Vedi documentazione su EnigmA Amiga Disk 23

Make Lattice ..... LC -vbr -Lntv -rr -O -ms Wcm.c

Make Aztec C ..... CC -sb -so Wcm.c, LN wcm -lc

Note ............. Linkabile con modulo personalizzato da F.S.

                   Adattamenti marginale per Aztec C di Luigi Callegari

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

#include <libraries/dosextens.h>
#include <intuition/intuition.h>
#include <workbench/workbench.h>
#include <string.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>

#ifdef LATTICE
#  include <proto/all.h>
#else
#  include <functions.h>
#endif

#define LENBUF 80
#define VERS 33L

struct DosLibrary *DOSBase;
struct IntuitionBase *IntuitionBase;
struct Library *IconBase;
struct Window *win;
struct IntuiMessage *msg;

/* Le tre stringhe necessarie per contenere il nome dei file di input e
 * i valori per le due penne.
 */
char file[LENBUF];
char detpen[3] = "-1";
char blkpen[3] = "-1";

/* La seguente struttura serve per garantire l' uso del topaz 8 ed evitare
 * malfunzionamenti.
 */
struct TextAttr top80 = {
	(STRPTR)"topaz.font",
	TOPAZ_EIGHTY,
	NULL,
	NULL
};

/* Gli array con la struttura dei bordi dei gadget e subito dopo le strut-
 * ture Border per ogni gadget. I bordi per le due penne sono uguali quin-
 * di viene usata la stessa struttura Border.
 */
SHORT border_file[] = { 0,0, 275,0, 275,11, 0,11, 0,0 };
SHORT border_pens[] = { 0,0, 27,0, 27,11, 0,11, 0,0 };

struct Border file_border = {
	-2, -2,		/* Origine relativa */
	3, 0,		/* Penne (Fore/Back) */
	JAM1,		/* Modo */
	5,		/* Numero coppie di coord. */
	border_file,	/* Matrice di coord. */
	NULL		/* Prossima struttura */
};

struct Border pens_border = {
	-2, -2,
	3, 0,
	JAM1,
	5,
	border_pens,
	NULL
};

/* Le definizioni per i gadget stringa.
 */
struct StringInfo file_sinfo = {
	file,
	NULL,
	0,
	LENBUF,
	0,
	0, 0, 0, 0, 0,
	0,
	0,
	NULL
};

struct StringInfo detpen_sinfo = {
	detpen,
	NULL,
	0,
	3,
	0,
	0, 0, 0, 0, 0,
	0,
	-1,
	NULL
};

struct StringInfo blkpen_sinfo = {
	blkpen,
	NULL,
	0,
	3,
	0,
	0, 0, 0, 0, 0,
	0,
	-1,
	NULL
};

struct IntuiText go_itext = {
	0, 1,
	JAM2,
	8, 2,
	&top80,
	"Modify",
	NULL
};

struct Image go_image = {
	0, 0,	/* Offset */
	64, 12,	/* Dimensions */
	2,	/* Depth */
	NULL,	/* Data */
	0,	/* PlanePick */
	0x1,	/* PlaneOnOff */
	NULL	/* Next */
};
	
/* Le definizioni vere e proprie dei gadget.
 */
struct Gadget go_gad = {
	NULL,
	-67, 24,
	64, 12,
	GADGIMAGE | GADGHCOMP | GRELRIGHT,
	RELVERIFY,
	BOOLGADGET,
	(APTR)&go_image,
	NULL,
	&go_itext,
	NULL,
	NULL,
	NULL,
	NULL
};

struct Gadget blkpen_gad = {
	&go_gad,
	188, 26,
	24, 8,
	NULL,
	RELVERIFY | STRINGRIGHT | LONGINT,
	STRGADGET,
	(APTR)&pens_border,
	NULL,
	NULL,
	NULL,
	(APTR)&blkpen_sinfo,
	NULL,
	NULL
};

struct Gadget detpen_gad = {
	&blkpen_gad,
	82, 26,
	24, 8,
	NULL,
	RELVERIFY | STRINGRIGHT | LONGINT,
	STRGADGET,
	(APTR)&pens_border,
	NULL,
	NULL,
	NULL,
	(APTR)&detpen_sinfo,
	NULL,
	NULL
};

struct Gadget file_gad = {
	&detpen_gad,
	42, 13,
	272, 8,
	NULL,
	RELVERIFY,
	STRGADGET,
	(APTR)&file_border,
	NULL,
	NULL,
	NULL,
	(APTR)&file_sinfo,
	NULL,
	NULL
};

/* Le strutture IntuiText con i nomi dei gadget.
 */
struct IntuiText blkpen_itext = {
	1, 0,
	JAM1,
	120, 26,
	&top80,
	(UBYTE*)"BlockPen",
	NULL
};

struct IntuiText detpen_itext = {
	1, 0,
	JAM1,
	6, 26,
	&top80,
	(UBYTE*)"DetailPen",
	&blkpen_itext
};

struct IntuiText file_itext = {
	1, 0,
	JAM1,
	6, 13,
	&top80,
	(UBYTE*)"File",
	&detpen_itext
};

/* La definizione della finestra.
 */
struct NewWindow wcm_win = {
	100, 100,
	320, 47,
	-1, -1,
	GADGETDOWN | GADGETUP | CLOSEWINDOW,
	WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE | ACTIVATE | NOCAREREFRESH,
	&file_gad,
	NULL,
	(UBYTE*)"WCM v1.0 by Flavio Stanchina",
	NULL,
	NULL,
	0, 0,
	0, 0,
	WBENCHSCREEN
};

/* La funzione main si limita a chiamare tutte le altre. E` stata messa al
 * primo posto per poter eliminare le routine di startup col Lattice 5.0.
 * Infatti in mancanza delle suddette routine la prima routine che compare
 * e` la prima ad essere eseguita.
 */
void main( void )
{
	void err(char *);
	BOOL wbhandle( void );

	register struct DiskObject *icon;
	register long status;

	if(!(IntuitionBase = (struct IntuitionBase *)
	  OpenLibrary("intuition.library", VERS)))
		return;

	if(!(IconBase = (struct Library *)
	  OpenLibrary("icon.library", VERS)))
		goto end2;

	if(!(win = OpenWindow(&wcm_win)))
		goto end1;

	PrintIText(win->RPort, &file_itext, 0, 0);
	ActivateGadget(&file_gad, win, NULL);

	while( wbhandle() )
	{
		if(!(icon = (struct DiskObject *)GetDiskObject(file)))
		{
			err("Error reading icon file         ");
			goto loop;
		}

		if(icon->do_Type != WBDISK &&
		   icon->do_Type != WBDRAWER &&
		   icon->do_Type != WBGARBAGE)
		{
			err("Not type DISK, DRAWER or GARBAGE");
			goto loop;
		}

		icon->do_DrawerData->dd_NewWindow.DetailPen =
		  (UBYTE)detpen_sinfo.LongInt;
		icon->do_DrawerData->dd_NewWindow.BlockPen =
		  (UBYTE)blkpen_sinfo.LongInt;

		status = PutDiskObject(file, icon);
		FreeDiskObject(icon);
	
		if(status == 0) err("Error Writing icon file         ");
		else err("Icon modified                   ");

loop: ;
	}
	
	CloseWindow(win);
end1:	CloseLibrary((struct Library *)IconBase);
end2:	CloseLibrary((struct Library *)IntuitionBase);
	return;
}

void err(char *s)
{
	struct IntuiText err_itext = {
		1, 0,
		JAM2,
		6, 37,
		&top80,
		NULL,
		NULL
	};

	err_itext.IText = s;
	PrintIText(win->RPort, &err_itext, 0, 0);

	return;
}

BOOL wbhandle( void )
{
	register struct Gadget *addr;
	register ULONG class;

	for( ;; )
	{
		Wait( 1L << win->UserPort->mp_SigBit );

		while(msg = (struct IntuiMessage *)GetMsg(win->UserPort))
		{
			class = msg->Class;
			addr = (struct Gadget *)msg->IAddress;
			ReplyMsg((struct Message *)msg);

			switch(class)
			{
			case CLOSEWINDOW:
			return(FALSE);

			case GADGETUP:
			case GADGETDOWN:
			if(addr == &go_gad)
				if(!file[0] ||
				  !detpen[0] ||
				  !blkpen[0])
					err("Input file and pens required    ");

				else if(detpen_sinfo.LongInt > 3 ||
				  detpen_sinfo.LongInt < -1 ||
				  blkpen_sinfo.LongInt > 3 ||
				  blkpen_sinfo.LongInt < -1)
					err("Pens must be from -1 to 3        ");

				else return(TRUE);
			}
		}
	}
}

