/*
***************************************************************************
*
* Datei:
*	  RSysTextFormatter.c
*
* Inhalt:
*
*      --- Globale Routinen ---
*
*    int RSysFormatOutput ( struct Window *wind , char *format );
*
*      --- Lokale  Routinen ---
*
*    static int BlankLeftCGadClicked ( void );
*    static int HandleFormatterIDCMP ( void );
*    static int NoFormatGadClicked ( void );
*    static int NoHeaderCGadClicked ( void );
*    static int OpenFormatterWindow ( void );
*    static int PostFixSGadClicked ( void );
*    static int PrefixSGadClicked ( void );
*    static int QuoteCGadClicked ( void );
*    static int UseGadClicked ( void );
*    static void preview ( void );
*
* Bemerkungen:
*	  Formatierung der Ausgabelisten von RSys.
*
* Erstellungsdatum:
*	  07-Jul-93 	Rolf Böhme
*
* Änderungen:
*	  07-Jul-93 	Rolf Böhme	  Erstellung
*
***************************************************************************
*/

#include "RSys.h"

static struct _fmt {
	int	f_blanks,
			f_quote;
	int	f_noheader;
	int	f_noformat;
	char	f_prefix[100],
			f_postfix[100];
	char	f_format[MAXWRITESIZE];
} TFormat, TFormatDef = {
	FALSE, FALSE, FALSE, TRUE,"","","%s"
};

static struct Window *FormatterWnd = NULL;
static struct Gadget *FormatterGList = NULL;
static struct IntuiMessage FormatterMsg;
static struct Gadget *FormatterGadgets[Formatter_CNT];
static UWORD FormatterLeft = 47;
static UWORD FormatterTop = 52;
static UWORD FormatterWidth = 610;
static UWORD FormatterHeight = 74;
static UBYTE *FormatterWdt = (UBYTE *) NAME " " VERSION " - Output Formatter";

static UWORD FormatterGTypes[]=
{
	STRING_KIND,
	STRING_KIND,
	CHECKBOX_KIND,
	CHECKBOX_KIND,
	CHECKBOX_KIND,
	TEXT_KIND,
	BUTTON_KIND,
	BUTTON_KIND
};

int PrefixSGadClicked( void );
int PostFixSGadClicked( void );
int BlankLeftCGadClicked( void );
int QuoteCGadClicked( void );
int NoHeaderCGadClicked( void );
int UseGadClicked( void );
int NoFormatGadClicked( void );

static struct NewGadget FormatterNGad[]=
{
	14, 17, 211, 13, (UBYTE *)"Prefix", NULL, GD_PrefixSGad, PLACETEXT_ABOVE, NULL, (APTR)PrefixSGadClicked,
	384, 17, 211, 13, (UBYTE *)"Postfix", NULL, GD_PostFixSGad, PLACETEXT_ABOVE, NULL, (APTR)PostFixSGadClicked,
	14, 33, 26, 11, (UBYTE *)"Blanks between texts", NULL, GD_BlankLeftCGad, PLACETEXT_RIGHT, NULL, (APTR)BlankLeftCGadClicked,
	234, 33, 26, 11, (UBYTE *)"Quote-in text", NULL, GD_QuoteCGad, PLACETEXT_RIGHT, NULL, (APTR)QuoteCGadClicked,
	419, 33, 26, 11, (UBYTE *)"No header in file", NULL, GD_NoHeaderCGad, PLACETEXT_RIGHT, NULL, (APTR)NoHeaderCGadClicked,
	233, 17, 143, 13, (UBYTE *)"Text to save", NULL, GD_TextTGad, PLACETEXT_ABOVE, NULL, NULL,
	119, 53, 147, 13, (UBYTE *)"Use Format", NULL, GD_UseGad, PLACETEXT_IN, NULL, (APTR)UseGadClicked,
	343, 53, 147, 13, (UBYTE *)"No Format", NULL, GD_NoFormatGad, PLACETEXT_IN, NULL, (APTR)NoFormatGadClicked
};

static ULONG *FormatterGTags[]=
{
	(ULONG *) (GTST_MaxChars), (ULONG *) 100, (ULONG *) (TAG_DONE),
	(ULONG *) (GTST_MaxChars), (ULONG *) 100, (ULONG *) (TAG_DONE),
	(ULONG *) (TAG_DONE),
	(ULONG *) (TAG_DONE),
	(ULONG *) (TAG_DONE),
	(ULONG *) (GTTX_Text), (ULONG *) "This is text", (ULONG *) (GTTX_Border), (ULONG *) TRUE, (ULONG *) (TAG_DONE),
	(ULONG *) (TAG_DONE),
	(ULONG *) (TAG_DONE)
};

static int
UseGadClicked(void)
{
	TFormat.f_noformat = FALSE;

	strncpy(TFormat.f_prefix,gadgetbuff(FormatterGadgets[GD_PrefixSGad - GD_PrefixSGad]),100);
	strncpy(TFormat.f_postfix,gadgetbuff(FormatterGadgets[GD_PostFixSGad - GD_PrefixSGad]),100);

	sprintf(TFormat.f_format,"%s%s%s%s%s%s%s",
			  TFormat.f_prefix,
			  TFormat.f_blanks ? " " : "",
			  TFormat.f_quote  ? "\"" : "",
			  "\%s",
			  TFormat.f_quote  ? "\"" : "",
			  TFormat.f_blanks ? " " : "",
			  TFormat.f_postfix
			  );

	return(FALSE);
}

static void
preview(void)
{
	char text[MAXWRITESIZE];

	DPOS;

	sprintf(text,"%s%s%s%s%s",
			  TFormat.f_blanks ? " " : "",
			  TFormat.f_quote  ? "\"" : "",
			  "This is text",
			  TFormat.f_quote  ? "\"" : "",
			  TFormat.f_blanks ? " " : "");

	GT_SetGadgetAttrs(FormatterGadgets[GD_TextTGad - GD_PrefixSGad], FormatterWnd,
							NULL,
							GTTX_Text, text,
							TAG_DONE);

	(void)UseGadClicked();

	return;
}

static int
PrefixSGadClicked(void)
{
	DPOS;

	strncpy(TFormat.f_prefix,gadgetbuff(FormatterGadgets[GD_PrefixSGad - GD_PrefixSGad]),100);

	preview();

	return TRUE;
}

static int
PostFixSGadClicked(void)
{
	DPOS;

	strncpy(TFormat.f_postfix,gadgetbuff(FormatterGadgets[GD_PostFixSGad - GD_PrefixSGad]),100);

	preview();

	return TRUE;
}

static int
BlankLeftCGadClicked(void)
{
	DPOS;

	TFormat.f_blanks = (TFormat.f_blanks ? FALSE : TRUE);

	preview();

	return TRUE;
}

static int
QuoteCGadClicked(void)
{
	DPOS;

	TFormat.f_quote = (TFormat.f_quote ? FALSE : TRUE);

	preview();

	return TRUE;
}

static int
NoHeaderCGadClicked(void)
{
	DPOS;

	TFormat.f_noheader = (TFormat.f_noheader ? FALSE : TRUE);

	return TRUE;
}

static int
NoFormatGadClicked(void)
{
	DPOS;

	TFormat.f_noformat = TRUE;
	TFormat.f_noheader = FALSE;

	return FALSE;
}

static int
OpenFormatterWindow(void)
{
	struct NewGadget ng;
	struct Gadget *g;
	UWORD lc,
			tc;
	UWORD wleft = FormatterLeft,
			wtop = FormatterTop,
			ww,
			wh;
	int gl[] = { GD_PrefixSGad - GD_PrefixSGad,
                GD_PostFixSGad - GD_PrefixSGad,
                GD_TextTGad - GD_PrefixSGad };

	DPOS;

   AdjustWindowDimensions(Scr, FormatterLeft, FormatterTop, FormatterWidth, FormatterHeight,
                               &wleft, &wtop, &ww, &wh);

	if (!(g = CreateContext(&FormatterGList))) return 1L;

	for (lc = 0, tc = 0; lc < Formatter_CNT; lc++)
	{
		CopyMem((char *)&FormatterNGad[lc], (char *)&ng, (long)sizeof(struct NewGadget));

		ng.ng_VisualInfo = VisualInfo;
		ng.ng_TextAttr = Font;
		ng.ng_LeftEdge = OffX + ComputeX(ng.ng_LeftEdge);
		ng.ng_TopEdge = OffY + ComputeY(ng.ng_TopEdge);
		ng.ng_Width = ComputeX(ng.ng_Width);
		ng.ng_Height = ComputeY(ng.ng_Height);

		FormatterGadgets[lc] = g = CreateGadgetA((ULONG) FormatterGTypes[lc], g, &ng, (struct TagItem *) & FormatterGTags[tc]);

		makelabelvisible(FormatterGadgets[lc]);

		while (FormatterGTags[tc]) tc += 2;
		tc++;

		if (NOT g) return 2L;
	}

	if (!(FormatterWnd = OpenWindowTags(NULL,
													WA_Left, wleft,
													WA_Top, wtop,
													WA_Width, ww,
													WA_Height, wh,
													WA_IDCMP, STRINGIDCMP | CHECKBOXIDCMP |
													TEXTIDCMP | BUTTONIDCMP | IDCMP_CLOSEWINDOW |
													IDCMP_REFRESHWINDOW | IDCMP_VANILLAKEY,
													WA_Flags, WFLG_DRAGBAR | WFLG_DEPTHGADGET |
													WFLG_CLOSEGADGET | WFLG_SMART_REFRESH |
													WFLG_ACTIVATE | WFLG_RMBTRAP,
													WA_Title, FormatterWdt,
													WA_PubScreenFallBack,TRUE,
													WA_PubScreen, Scr,
													TAG_DONE)))
		return 4L;

	RefreshRastPort(FormatterWnd,FormatterGadgets,gl, 3, FALSE, FormatterGList);

	return NULL;
}

static int
HandleFormatterIDCMP(void)
{
	struct IntuiMessage *m;
	int	(*func) (void), ID;
	BOOL	running = TRUE;

	DPOS;

	while (m = GT_GetIMsg(FormatterWnd->UserPort))
	{
		CopyMem((char *)m, (char *)&FormatterMsg, (long)sizeof(struct IntuiMessage));

		GT_ReplyIMsg(m);

		switch (FormatterMsg.Class)
		{
			case IDCMP_REFRESHWINDOW:
				GT_BeginRefresh(FormatterWnd);
				GT_EndRefresh(FormatterWnd, TRUE);
				break;

			case IDCMP_VANILLAKEY:
				if ((char)FormatterMsg.Code == ESC) running = FALSE;
				break;

			case IDCMP_CLOSEWINDOW:
				running = NoFormatGadClicked();
				break;

			case IDCMP_GADGETUP:
            ID = ((struct Gadget *) FormatterMsg.IAddress)->GadgetID;

            HandleHelp((enum RSysNumbers)ID);

				func = (void *)((struct Gadget *) FormatterMsg.IAddress)->UserData;
				running = func();
				break;
		}
	}

	return running;
}

int
RSysFormatOutput(struct Window *wind, char *format)
{
   APTR req = NULL;

	DPOS;

   if(!Flags.textformat)
   {
      strcpy(format, "%s");
      return TFormat.f_noheader;
   }

	CopyMem(&TFormatDef, &TFormat, sizeof(struct _fmt));

	if (OpenASysWindow(OpenFormatterWindow, NO_KILL))
	{
		PrintInfo("Format output strings", SPEAK, 0);

		if(wind) req = LockWindow(wind);

		while(HandleFormatterIDCMP()) ;

		CloseASysWindow(&FormatterWnd, &FormatterGList, NULL);

		if(TFormat.f_noformat == FALSE) strcpy(format,TFormat.f_format);
		else
			strcpy(format,"%s");

      UnlockWindow(req);
	}

	return TFormat.f_noheader;
}
