/******************************************************************
** charmap.c : Affiche la table ascii d'une police de caractère  **
**             Écrit (en partie) par T.Pierron                   **
**             11-Déc-1999                                       **
**---------------------------------------------------------------**
** Special Requirements: WorkBench 2.0, version 36 or above      **
******************************************************************/

#include <Intuition/Intuition.H>
#include <Intuition/IntuitionBase.H>
#include <Graphics/RastPort.H>
#include <Graphics/Text.H>
#include <Libraries/Gadtools.H>
#include <Intuition/Screens.H>

#include "cmap.h"
#define  CATCOMP_NUMBERS		/* We will need the string id */
#define  CATCOMP_STRINGS		/* and the english string corresponding to the id */
#include "cmap_strings.h"

/* Shared libraries we'll need to open */
struct IntuitionBase *IntuitionBase = NULL;
struct GfxBase *GfxBase       = NULL;
struct Library *GadToolsBase  = NULL;
struct Library *AslBase       = NULL;
struct Library *DiskfontBase  = NULL;
struct Library *CxBase			= NULL;
struct Library *IconBase		= NULL;
struct Library *KeymapBase		= NULL;
struct Library *LocaleBase    = NULL;
void           *clipdev			= NULL;

/* Graphics area: */
struct RastPort *RP,RPT;
struct TextFont *font,*newfont=0;

struct Menu   *menu   = NULL;				/* Menu attached to the window */
struct Screen *screen = NULL;				/* Screen where window will be opened */
struct Window *window = NULL;				/* Main window ptr */
struct Gadget *glist  = NULL;  		   /* Gadget list pointer */
struct Gadget *gad;							/* our running Gadget pointer */
struct Gadget *copy_gad, *paste_gad;	/* Copy and paste gadget (for menu action) */
void   *vi = NULL;							/* VisualInfo pointer */

WORD  ZoomData[4];							/* Position of iconified window */
WORD  xdeb,ydeb,xfin,yfin,X,Y;			/* Position of ASCII table */
UBYTE CharWid[256],Char[]=" ";			/* Data of ASCII table */
UBYTE Font_height;							/* Font information */
UBYTE GadDisable=0;							/* If a ressource is unavailable */
WORD  Wid[4],len;								/* Width of various gadgets */

/* For the "version" command: */
const UBYTE VERSION[]= "$VER:CharMap v" CHARMAP_VERSION " " CHARMAP_DATE "\n";

/* Tag list for openning the main window: */
ULONG WinTags[] = {
	WA_Zoom,(ULONG)ZoomData,
	WA_NewLookMenus,TRUE,
	TAG_DONE
};

/* Set the color of the menu to Newlook (only 3.0+): */
ULONG MenuTags[] = {
	GTMN_FrontPen, 1,
	TAG_DONE
};

ULONG BoxTags[] = {
	GT_VisualInfo,0,
	TAG_DONE,FALSE,
	TAG_DONE
};

/* Our gadtools gadgets text: */
UBYTE *GadTxt[7+NUM_CHARSET+5]={
	"<=",
	MSG_NEWFONT_STR,
	NM_BARLABEL,
	MSG_COPY_STR,
	MSG_PASTE_STR,
	MSG_STRGAD_STR
};

#define	CBLen		Wid[0]
#define	ScrLen	Wid[1]
#define	StdLen	Wid[2]
#define	ChrLen	Wid[3]

/* Tags associated with the charset cycle gadget: */
ULONG CycleTags[]={
	GTCY_Labels, (ULONG) &GadTxt[6],
	GTCY_Active, 1,
	TAG_DONE
};

/* Compute size of nth string in GadTxt table: */
BYTE MeasStr[]={4,GT+1,8,2};

/* The ones associated with the font type: */
ULONG FontTags[]={
	GTCY_Labels, (ULONG) &GadTxt[GT+1],
	GTCY_Active, 0,
	TAG_DONE
};

/* here we declare the information for our new window - notice that GadTools
 * provides some constants which describe which IDCMP events the Window will
 * need to handle the GadTools Gadgets properly. Just OR them into your
 * normal IDCMPFlags field.
 */

ULONG sigwin=0;
struct NewWindow new_window = {
	50, 20, 510, 150,
	-1, -1,						/* DetailPen, BlockPen */
	CLOSEWINDOW | REFRESHWINDOW | MOUSEBUTTONS | MOUSEMOVE | RAWKEY | MENUPICK | MENUVERIFY | GADGETUP,
	ACTIVATE | WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE | SMART_REFRESH | REPORTMOUSE,
	NULL,							/* FirstGadget */
	NULL,							/* CheckMark */
	NULL,							/* Title */
	NULL,							/* Screen */
	NULL,							/* BitMap */
	0, 0, 0, 0,					/* min/max sizes */
	CUSTOMSCREEN
};

/* Error messages: */
UBYTE *Errors[]= {
	MSG_BADOS_STR,
	MSG_SCREENLOCK_STR,
	MSG_ERRLAYOUT_STR,
	MSG_FONTREALLYBIG_STR,
	MSG_ERRGADGET_STR,
	MSG_ERRNOMEM_STR,
	MSG_NOASL_STR,
	MSG_NEXTSCR_STR			/* When commo is not present */
};

struct EasyStruct Request= {
	sizeof(struct EasyStruct),0,
	MSG_ABOUT_STR,
	MSG_ABOUTMSG_STR,
	MSG_CONTINUE_STR
};

/**********************************************************
*****					MENU DATA MANAGMENT:						*****
**********************************************************/

/* NewMenu menu declaration */
struct NewMenu newmenu[] =
{
	{NM_TITLE, MSG_MENUTITLE_STR,0 , 0, 0, 0},
	{  NM_ITEM, MSG_FONTTYPE_STR, 0, 0, 0, 0},
	{   NM_SUB, MSG_MENUFONTSCREEN_STR,"S", CHECKIT|CHECKED, ~1, (APTR) 111},
	{   NM_SUB, MSG_MENUTEXTFONT_STR,  "T", CHECKIT, ~2, (APTR)112},
	{   NM_SUB, MSG_MENUCUSTOMFONT_STR,"F", CHECKIT, ~4, (APTR)113},
	{  NM_ITEM, MSG_MENUCHARSET_STR,   0, 0, 0, 0},
	{   NM_SUB,   MSG_MENUASCII8_STR,    "1", CHECKIT, ~1, (APTR)141},
	{   NM_SUB,   MSG_MENUISOLATIN1_STR, "2", CHECKIT, ~2, (APTR)142},
	{   NM_SUB,   MSG_MENUSTDAMIGA_STR,  "3", CHECKIT, ~4, (APTR)143},
	{  NM_ITEM, NM_BARLABEL,  0, 0, 0, 0},
	{  NM_ITEM, MSG_MENUABOUT_STR,       "?", 0, 0, (APTR)15},
	{  NM_ITEM, NM_BARLABEL,  0, 0,  0, 0},
	{  NM_ITEM, MSG_MENUICONOFY_STR,     "I", 0, 0, (APTR)16},
	{  NM_ITEM, MSG_MENUHIDE_STR,        "H", 0, 0, (APTR)17},
	{  NM_ITEM, MSG_MENUQUIT_STR,        "Q", 0, 0, (APTR)18},

	{NM_TITLE, MSG_EDITMENU_STR ,0, 0, 0, 0},
	{  NM_ITEM, MSG_CUT_STR,             "X", 0, 0, (APTR)21},
	{  NM_ITEM, NM_BARLABEL,             "C", 0, 0, (APTR)22},
	{  NM_ITEM, NM_BARLABEL,             "V", 0, 0, (APTR)23},
	{  NM_ITEM, NM_BARLABEL,  0, 0,  0, 0},
	{  NM_ITEM, MSG_MENUCLEARTXT_STR,  "Del", NM_COMMANDSTRING, 0, (APTR)24},
/*	{  NM_ITEM, NM_BARLABEL,  0, 0,  0, 0},
	{  NM_ITEM, MSG_SAVESETTINGS,          0, 0,  0, (APTR)26},
*/	{NM_END, 0, 0, 0, 0, 0},
},*checked=&newmenu[NM_FONTTYPE],
  *checkset;

/* Different width of box for each charset: */
UBYTE MaxWid,NumChars,CharsetNum=0;

/* Colors pens: */
UWORD fillpen=3,txtpen=1,poppen=2,filltxt=2;

/* Position of normal and minimized window: */
WORD Offset[]={0x7fff,0x7fff,0x7fff,0x7fff};

/* Function prototypes */
BYTE setup();										/* Open window, ressources, font... */
void cleanup(UBYTE *,WORD);					/* Quit properly */
void create_gadgets(void);						/* Create the gadget using gadtools lib */
void handle_input(void);						/* Handle intuition events */
void Init_helpwin();								/* Alloc memory for help window */
void Open_helpwin(UBYTE, WORD, WORD);		/* Show the help window */
void Free_helpwin();								/* Free all allocated memory */
void Close_helpwin();							/* Cleanup the display */

/**** Definition of the main loop: ****/
int main(int argc, char *argv[])
{
	extern BOOL PopWin;									/* True if the window is poped up at start */

	/* If no locale, use english as default: */
	if(LocaleBase = (struct Library *)OpenLibrary("locale.library", 38))
		Translate_srings();

	/* These libraries are located in ROM. If it failed to open, it's   **
	** because we are attempting to run this programme on an old system */
	if( !(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", MIN_VERSION)) ||
	    !(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",MIN_VERSION)) ||
	    !(GadToolsBase = (struct Library *)OpenLibrary("gadtools.library",MIN_VERSION)) ||
	    !(KeymapBase = (struct Library *)OpenLibrary("keymap.library",37)) )
		cleanup(ErrMsg(MSG_BADOS),30);

	/* Open ze libraries */
	if( (CxBase   = (void *) OpenLibrary("commodities.library", 37L)) &&
		 (IconBase = (void *) OpenLibrary("icon.library",        36L)) )
		/* If the icon and commodities.library are present, and the	**
		** programm failed to init as a commodity, it's probably		**
		** because it is already running, and user want to quit:		*/
	{	if( Init_commodity(argc,argv)==FALSE )
			cleanup(0,0);
	} else {
		/* If commo. is not present, change menu "Hide" by "Next screen": */
		newmenu[13].nm_Label = ErrMsg(MSG_NEXTSCR);
		newmenu[13].nm_CommKey = "J";
		newmenu[13].nm_UserData = (APTR)25;
	}

	/* Same as gadget or menu text: */
	newmenu[17].nm_Label=GadTxt[3];
	newmenu[18].nm_Label=GadTxt[4];
	GadTxt[2]=newmenu[NM_CHARSET-1].nm_Label;
	/* Init cycle font-type gadget text list and charset cycle gad: */
	for(GadTxt[GT]=GadTxt[1], checkset=&newmenu[NM_CHARSET], X=6, GadTxt[1]=0;
	    X<7+NUM_CHARSET+4; X++,checkset++)
	{
		if(X==GT-1) X+=2,checkset=&newmenu[2];
		GadTxt[ X ]=checkset->nm_Label;
	}

	/* No ASL or diskfont library, no font requester */
	if( !(DiskfontBase = (struct Library *)OpenLibrary("diskfont.library",0)) ||
	    !(AslBase = (struct Library *)OpenLibrary("asl.library",MIN_VERSION)) ) {
		newmenu[NM_FONTTYPE+2].nm_Flags |= NM_ITEMDISABLED;
		GadTxt[GT+3] = 0;
	}

	/* For temporary font measurement: */
	InitRastPort( &RPT );

	/* Get access to the frontmost screen */
	screen = (struct Screen *) IntuitionBase->ActiveScreen;
	font = screen->RastPort.Font;

	/* Initial charset displayed: */
	if(CharsetNum<1 || CharsetNum>NUM_CHARSET) Init_charset(font);
	checkset=&newmenu[CharsetNum+NM_CHARSET-1];
	checkset->nm_Flags |= CHECKED;

	if(PopWin)
		/* If setup failed at start (font too big): */
		if(setup()) Handle_menu(113L);

	handle_input();
}

/**** Setup the window, return 1 if init failed: ****/
BYTE setup()
{
	struct DrawInfo *di;

	/* get the screen's visual information data */
	if( !(vi = (void *) GetVisualInfoA( screen,NULL )) )
		cleanup(ErrMsg(MSG_SCREENLOCK),30);

	BoxTags[1]=(ULONG)vi;

	/* Adjust the pen number to what the screen uses: */
	if( di = (void *)GetScreenDrawInfo(screen))
	{
		/* Get a copy of the correct pens for the screen: */
		fillpen = di->dri_Pens[FILLPEN];
		txtpen  = di->dri_Pens[TEXTPEN];
		filltxt = poppen = di->dri_Pens[FILLTEXTPEN];
		if(poppen==txtpen) poppen=fillpen;

		/* This one is only available on system V39+: */
		if(di->dri_Version>=2) MenuTags[1] = di->dri_Pens[BARDETAILPEN];
		FreeScreenDrawInfo(screen,di);
	}

	/* Build the menu-strip and compute menu items size: */
	if( !(menu = (void *) CreateMenusA(newmenu, MenuTags)) ||
	    !LayoutMenusA(menu, vi, NULL) )
	   cleanup(ErrMsg(MSG_ERRLAYOUT),30);

	SetFont(&RPT,font);
	/* Read the size of the 256 characters of the current font, **
	** and compute the maximal width of the box chars:          */
	NumChars = CharsetNum==1 ? 32 : CharsetNum == 2 ? 28 : 24;
	{
		register WORD I;
		register UBYTE *W;
		for(I=0,W=CharWid,MaxWid=0; I<256; I++,W++) {
			*Char = I;
			*W = TextLength(&RPT,Char,1);

			/* Compute the width of each box: */
			switch( CharsetNum )
			{
				case 3: if(I>=127 && I<160) break;
				case 2: if(I<32) break;
				case 1: if(MaxWid < *W+6) MaxWid=*W+6;
			}
		}
	}

	/* Some important font size-depending values: */
	Font_height = font->tf_YSize+4;
	Init_helpwin();
	SetFont(&RPT,screen->RastPort.Font);

	/* Measure lenght of strings: */
	for(X=0; X<sizeof(MeasStr)/sizeof(MeasStr[0]); X++)
	{
		static char Len[]={15,30,30,20};
		register BYTE *str = GadTxt[ MeasStr[X] ];
		Wid[ X ] = TextLength(&RPT,str,strlen(str))+ Len[X];
	}

	for(X=1;;) {
		/* Computes the width of our window: */
		new_window.Width = xfin = MaxWid*NumChars+20;

		/* Does the width of the window fit in the screen ? */
		if(xfin > screen->Width)
		{
			/* No! force the window width: */
			MaxWid = (screen->Width-20) / NumChars;
			/* For the extremely rare case, when charmap text GUI in larger than screen: */
			X=0; continue;
		}

		/* Computes the widths of the bottom row of gadgets: */
		len = (xfin - 30 - ChrLen) / 3;
		if(len<StdLen)	{ len = xfin - 30 - ChrLen - StdLen; if(len&1) StdLen++; len/=2; }
		else				StdLen = len;

		/* Reduces string gadget if cycle is too short: */
		if(ScrLen<len)	ScrLen=len;

		/* Is the width of the window enough to place gadgets? */
		if(X && len < CBLen) MaxWid += 1+(CBLen-len)/NumChars;
		else break;
	}

	/* Setup normal and minimized position of main window: */
	new_window.TopEdge  = Adjust_pos(Offset[1],screen->Height,
	new_window.Height   = Font_height*8+3*screen->Font->ta_YSize+35 );
	new_window.LeftEdge = Adjust_pos(Offset[0],screen->Width,xfin);

	/* Title of the window is the name of our font: */
	new_window.Title    = font->tf_Message.mn_Node.ln_Name;
	new_window.Screen   = screen;

	/* Init the position and the size of the minimized window: */
	ZoomData[0] = Adjust_pos(Offset[2],screen->Width,
	ZoomData[2] = TextLength(&screen->RastPort,new_window.Title,strlen(new_window.Title))+100);
	ZoomData[1] = Adjust_pos(Offset[3],screen->Height,
	ZoomData[3] = screen->BarHeight+1);

	/* If this call failed, the font is VERY too much big! */
	if( !(window = (void *) OpenWindowTagList(&new_window,WinTags)) )
		/* If ASL is avaible, show a requester to change the font: */
		if( avert(ErrMsg(MSG_FONTREALLYBIG)) && AslBase )
			return TRUE;
		else cleanup(0,0);	/* Game over! */

	/* Signal bit, to process events from the window: */
	sigwin = 1 << window->UserPort->mp_SigBit;
	SetMenuStrip(window, menu);	/* Attach the menu to the window */
	create_gadgets();					/* Creates all gadgets */
	return FALSE;
}

/**** Desallocate all ressources: ****/
void cleanup(UBYTE *msg,WORD err)
{
	extern APTR FR;
	extern void *catalog;

	if (msg)			puts(msg);
	if (window)	{
		/* Save the positions of the window before to close it: */
		CopyMem(&window->LeftEdge,Offset,2*sizeof(WORD));
		CopyMem(ZoomData,Offset+2,2*sizeof(WORD));
		/* Now we can restore it's place if it will be reopen */
		ClearMenuStrip(window);
		CloseWindow(window);
		window=NULL;
	}

	/* These two functions can take a NULL, but let's stay consistent */
	if (glist) FreeGadgets(glist);	glist=0;
	if (vi) FreeVisualInfo(vi);		vi=0;
	if (menu) FreeMenus(menu);			menu=0;
	Free_helpwin();

	if (err<0) return;
	Free_commodity();
	if (FR)					FreeAslRequest(FR);
	if (newfont)			CloseFont(newfont);
	if (catalog)			CloseCatalog(catalog);
	if (clipdev)			CBClose(clipdev);
	if (CxBase)				CloseLibrary(CxBase);
	if (AslBase)			CloseLibrary(AslBase);
	if (IconBase)			CloseLibrary(IconBase);
	if (KeymapBase)		CloseLibrary(KeymapBase);
	if (LocaleBase)		CloseLibrary(LocaleBase);
	if (DiskfontBase)		CloseLibrary(DiskfontBase);
	if (GadToolsBase)		CloseLibrary(GadToolsBase);
	if (IntuitionBase)	CloseLibrary(IntuitionBase);
	if (GfxBase)			CloseLibrary(GfxBase);
	exit(err);
}

/**** Draw the table of characters: ****/
void Draw_ASCIIChart()
{
	register WORD i,x,y;

	/* Initial bevel box, which will be copied 256 times: */
	DrawBevelBoxA(RP,xdeb,ydeb,MaxWid,Font_height,BoxTags);

	/* Exponential copy (very fast :-) */
	for(x=MaxWid,i=xdeb+x,y=window->Width-10; i<xfin; ) {
		ClipBlit(RP,xdeb,ydeb,RP,i,ydeb,x,Font_height,0xC0);
		i+=x; x<<=1; if(i+x>y) x=y-i;
	}

	for(x=Font_height,i=ydeb+x; i<yfin; i+=x,x<<=1)
		ClipBlit(RP,xdeb,ydeb,RP,xdeb,i,y,x,0xC0);

	/* Character in each box: */
	SetAPen(RP,txtpen);
	for(i=CharsetNum==1?0:32,x=xdeb; i<256; i++,y+=Font_height)
	{
		/* In Amiga Standard charset, the 127th character isn't displayed: */
		if(CharsetNum==3 && i==127) { i=160; goto NL; }
		if( (i&7)==0 ) { NL: y=ydeb; x+=MaxWid; }
		*Char = i;
		Move(RP,x-(MaxWid+CharWid[i]>>1),y+font->tf_Baseline+2);
		Text(RP,Char,1);
	}
}

/**** Create the gadgets associated with the window: ****/
void create_gadgets()
{
#	define tmpgad		((struct Gadget *)RP)
	struct NewGadget ng={65,5,0,14,0,0,5,PLACETEXT_LEFT | NG_HIGHLABEL,0,0};
	extern UBYTE  *StrBuf;
	UWORD  top,I;

	/* «Constant» variables: */
	top = window->BorderTop + 1;
	gad = (void *) CreateContext(&glist);
	RP  = &screen->RastPort;

	ng.ng_TextAttr = screen->Font;
	ng.ng_VisualInfo = vi;

	/*	the NewGadget stucture is not modified by any Gadget creation call,
		so you'll only need to change information that actually needs to be
		changed */
	ng.ng_GadgetText = GadTxt[5];
	if( ChrLen-20 < (ng.ng_LeftEdge = TextLength(RP,GadTxt[5],strlen(GadTxt[5]))+20) )
		ng.ng_LeftEdge = ChrLen;
 	ng.ng_Width = window->Width - 20 - ScrLen - ng.ng_LeftEdge -
	(Wid[0] = ng.ng_Height = screen->Font->ta_YSize+6);
	ng.ng_TopEdge += top;

	tmpgad = gad = (void *) CreateGadget(STRING_KIND, gad, &ng, GTST_MaxChars,50,TAG_DONE);
	*(StrBuf = sti(gad)->Buffer)=0;

	/* Name of the charset displayed in the cycle gad: */
	CycleTags[3]=CharsetNum-1;

	/* Create the 5 gadgets shorcut: */
	ng.ng_LeftEdge -= 10;
	for(I=0; I<5; I++)
	{
		ng.ng_LeftEdge+=ng.ng_Width+10;
		ng.ng_Width = I<3 ? Wid[I] : len;
		if(I==2) { ng.ng_Flags = PLACETEXT_LEFT | NG_HIGHLABEL; ng.ng_Height -= 2;
		           ng.ng_LeftEdge = ChrLen; top = (ng.ng_TopEdge += ng.ng_Height+6); }
		else     { ng.ng_Flags = PLACETEXT_IN; }
		ng.ng_GadgetID   = I;
		ng.ng_GadgetText = GadTxt[I];
		tmpgad = (void *) CreateGadgetA(I && I<=2 ? CYCLE_KIND:BUTTON_KIND, tmpgad, &ng,
								I==2 ? CycleTags : I==1 ? FontTags:NULL);

		if(I==3) copy_gad=tmpgad; if(I==4) paste_gad=tmpgad;

		/* If a ressource isn't available, disable the corresponding gadget: */
		if(GadDisable & (1<<I)) tmpgad->Flags |= GADGDISABLED;
	}

	/*	Now we're ready to add the Gadget list. Quit if the final Gadget
		pointer is NULL - this means that one of the Gadget allocations failed.
		If it exists, add it to the window and refresh, then add the new
		GT_RefreshWindow() call. */

	if (!tmpgad)
		cleanup(ErrMsg(MSG_ERRGADGET),30);

	AddGList(window, glist, (UWORD)-1, (UWORD)-1, NULL);
	RefreshGList(glist, window, NULL, (UWORD)-1);
	GT_RefreshWindow(window, NULL);
	RP=window->RPort;
	SetFont(RP,font);
	SetDrMd(RP,JAM1);

	/* Draw the ASCII table: */
	xdeb = 10;			ydeb = top+ng.ng_Height+5;
	xfin -= 10;			yfin  = ydeb+Font_height*8;
	Draw_ASCIIChart();
}
