// **************************************************************************
// **************************************************************************
// **************************************************************************
// **                                                                      **
// ** POPUP.C                                                       MODULE **
// **                                                                      **
// ** Functions for a popup message box with user input.                   **
// **                                                                      **
// ** Last modified : 26 Mar 1998 by John Brandwood                        **
// **                                                                      **
// **************************************************************************
// **************************************************************************
// **************************************************************************

// Game's N64 header files.

#include "generic.h"
#include "gameinc.h"

// John's N64 header files.

#include "shellres.h"

#include "shell.h"
#include "shellgfx.h"

#include "states.h"

#include "popup.h"

//
// DEFINITIONS
//

#define	NUM_POPUP_CELS      32

#define	POPUP_TXT           0
#define	POPUP_BTN           (NUM_POPUP_CELS - 5)
#define	POPUP_TTL           (NUM_POPUP_CELS - 1)

#define	BOX_XBORDER         4
#define	BOX_YBORDER         2
#define	BOX_XSPACE          12
#define	BOX_YSPACE          7

#define	BOX_YTITLE          (13 - BOX_YBORDER)

#define	BTN_XBORDER         8
#define	BTN_YBORDER         3
#define	BTN_XSPACE          16

//
// GLOBAL VARIABLES
//

//
// STATIC FUNCTION PROTOTYPES
//

static	void                ParseMessage            (char *);
static	void                AutoSizeBox             (void);

//
// STATIC VARIABLES
//

static	UB                  ub___sPopupInitialized;

static	UB                  ub___sPopupF;			// Font
static	UB                  ub___sPopupP;			// Palette
static	UD                  ud___sPopupM;			// Mode

static	TXTCEL_T **         ppcl_sPopupCels;
static	SW *                psw__sPopupXPos;
static	SW *                psw__sPopupYPos;
static	UB *                pub__sPopupColr;

static	SI                  si___sPopupCels;
static	SI                  si___sPopupBtns;
static	SI                  si___sPopupSiz;
static	SI                  si___sPopupDef;
static	SI                  si___sPopupEsc;
static	SI                  si___sPopupCur;

static	SI                  si___sPopupBoxW;
static	SI                  si___sPopupBoxH;
static	SI                  si___sPopupBtnW;
static	SI                  si___sPopupBtnH;

static	SI                  si___sTabCount;
static	UB                  aub__sTabType [8];
static	SW                  asw__sTabPosn [8];

static	UB                  aub__sMsgBoxColours [] =
	{
	RGB_MESSAGE_BOXTL,		// BoxTL
	RGB_MESSAGE_BOXTR,		// BoxTR
	RGB_MESSAGE_BOXBL,		// BoxBL
	RGB_MESSAGE_BOXBR,		// BoxBR
	RGB_MESSAGE_EDGE, 		// Edge
	RGB_MESSAGE_BUTTON, 	// Button (normal)
	RGB_MESSAGE_HILITE 		// Button (hilite)
	};

static	int                 asi__sTabFlag [4] =
	{
	FFLG_XL,
	FFLG_XC,
	FFLG_XR,
	0
	};

static	UB                  aub__sSprPopup [] =
	{
	SPR_SMALLBTN,

	SPR_HELPTL,		// green corners
	SPR_HELPTR,
	SPR_HELPBL,
	SPR_HELPBR,

	SPR_HELPTL1,	// blue corners
	SPR_HELPTR1,
	SPR_HELPBL1,
	SPR_HELPBR1,

	0
	};

//
//
//



// **************************************************************************
// **************************************************************************
// **************************************************************************
//	GLOBAL FUNCTIONS
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// *                                                                        *
// * [AS]         Allow shoulder button                                     *
// *                                                                        *
// * [DEF]        Default button, followed by text                          *
// * [ESC]        Escape button, followed by text                           *
// * [BTN]        Other button, followed by text                            *
// *                                                                        *
// * [FTn]        Text font                                                 *
// * [FPn]        Text palette                                              *
// * [FS]         Text shadow on/off flip                                   *
// *                                                                        *
// * [TABtxtx..]  Set tab stops, followed by T-pos and X-pos pairs          *
// *                                                                        *
// *              T-pos = MTAB_XL, MTAB_XC, MTAB_XR                         *
// *              X-pos = 0..254 : 0..508 from LHS of box                   *
// *                         255 : center                                   *
// *                                                                        *
// **************************************************************************



// **************************************************************************
// * IsPopupShown ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 IsPopupShown            (void)

	{
	// Are we initialized ?

	if (ub___sPopupInitialized == 0)
		{
		return (0);
		}

	// All done.

	return (1);
	}



// **************************************************************************
// * IsPopupFullSize ()                                                     *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 IsPopupFullSize         (void)

	{
	// Are we initialized ?

	if (ub___sPopupInitialized == 0)
		{
		return (0);
		}

	if (si___sPopupSiz != 256)
		{
		return (0);
		}

	// All done.

	return (1);
	}



// **************************************************************************
// * InitPopup ()                                                           *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 InitPopup               (
								char *              pcz__Title,
								char *              pcz__Message,
								UB *                pub__Colours)

	{
	// Local Variables.

	int                 i;

	TXTCEL_T *          pcl__Txt;

	// Load up the sprites for this screen.

	LoadSpriteList(aub__sSprPopup);

	// Are we initialized ?

	if (ub___sPopupInitialized == 0)
		{
		// No, we're starting from scratch.

		si___sPopupSiz = 0;
		si___sPopupCur = 0;
		}
	else
		{
		// Yes, we're changing the current popup.

		ExitPopup();
		}

	// Allocate buffer for text cel array.

	ppcl_sPopupCels = MemCalloc(NUM_POPUP_CELS * (sizeof(TXTCEL_T *) + 4), MEM_ANY);

	psw__sPopupXPos = ((SW *) (ppcl_sPopupCels + NUM_POPUP_CELS)) + 0;
	psw__sPopupYPos = ((SW *) (ppcl_sPopupCels + NUM_POPUP_CELS)) + NUM_POPUP_CELS;

	// Process the title.

	if (pcz__Title != 0)
		{
//		pcl__Txt = InitTxtCel(NULL, 0, FONT_BLKBT22, FPAL_BLACK, FFLG_XC|FFLG_YT);
		pcl__Txt = InitTxtCel(NULL, 0, FONT_BLKBT18, FPAL_BLACK, FFLG_XC|FFLG_YT);	// only font with buttons
		WriteTxtCel(pcl__Txt, (char *) pcz__Title);

		ppcl_sPopupCels[POPUP_TTL] = pcl__Txt;
		}

	// Process the message.

	ParseMessage(pcz__Message);

	AutoSizeBox();

	// Process the colours.

	pub__sPopupColr =
		(pub__Colours != NULL) ? pub__Colours : aub__sMsgBoxColours;

	// All Done.

	return (ub___sPopupInitialized = 1);
	}



// **************************************************************************
// * ExitPopup ()                                                           *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 ExitPopup               (void)

	{
	// Local Variables.

	int                 i;

	// Are we initialized ?

	if (ub___sPopupInitialized == 0)
		{
		return (0);
		}

	// Free up the text cels.

	for (i = 0; i < NUM_POPUP_CELS; i += 1)
		{
		if (ppcl_sPopupCels[i] != NULL)
			{
			FreeTxtCel(ppcl_sPopupCels[i]);
			MemFree(ppcl_sPopupCels[i]);
			}
		}

	MemFree(ppcl_sPopupCels);

	ppcl_sPopupCels = NULL;
	psw__sPopupXPos = NULL;
	psw__sPopupYPos = NULL;

	si___sPopupCels = 0;
	si___sPopupBtns = 0;

	// Free up the sprites for this screen.

	FreeSpriteList(aub__sSprPopup);

	// All Done.

	return (ub___sPopupInitialized = 0);
	}



// **************************************************************************
// * ProcPopup ()                                                           *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	int                 ProcPopup               (void)

	{
	// Local Variables.

	int                 si___Sound = -1;

	// Are we initialized ?

	if (ub___sPopupInitialized == 0)
		{
		return (0);
		}

	// Have we already finished ?

	if (si___sPopupCur < 0)
		{
		goto allDone;
		}

	// Expand the box onto the screen.

	if (si___sPopupSiz != 256)
		{
		if (ui___gFrameDrawn != 0)
			{
			if ((si___sPopupSiz += 48) > 256) si___sPopupSiz = 256;
			}
		goto allDone;
		}

	// Are there any buttons to press ?

	if (si___sPopupBtns == 0)
		{
		if (asi__gTeamBtn[EVERYBODY] &
				(CTRL_TRIGGER |
				 CTRL_START | CTRL_FIRE_A | CTRL_FIRE_B |
				 CTRL_PAD_UP | CTRL_PAD_DOWN | CTRL_PAD_LEFT | CTRL_PAD_RIGHT |
				 CTRL_C_UP | CTRL_C_DOWN | CTRL_C_LEFT | CTRL_C_RIGHT |
				 CTRL_SHOULDER_L | CTRL_SHOULDER_R))
			{
			si___Sound = SFX_SHELL_NOMV;
			}
		goto allDone;
		}

	// Allow the shoulder buttons to exit a help screen.

	if (fl___gShowingHelp)
		{
//		if (asi__gTeamBtn[EVERYBODY] & (CTRL_SHOULDER_L | CTRL_SHOULDER_R))
		if (asi__gTeamBtn[EVERYBODY] & (CTRL_SHOULDER_R))
			{
			asi__gTeamBtn[EVERYBODY] |= CTRL_FIRE_B;
			}
		}

	// Exit button ?

	if (asi__gTeamBtn[EVERYBODY] & (CTRL_START))
		{
		si___sPopupCur = 0 - si___sPopupCur;
		si___Sound     = SFX_SHELL_STRT;
		goto allDone;
		}

	if (asi__gTeamBtn[EVERYBODY] & (CTRL_FIRE_A))
		{
		si___sPopupCur = 0 - si___sPopupCur;
		si___Sound     = SFX_SHELL_BTNA;
		goto allDone;
		}

	if (asi__gTeamBtn[EVERYBODY] & (CTRL_FIRE_B))
		{
		if (si___sPopupEsc == 0)
			{
			si___Sound = SFX_SHELL_NOMV;
			}
		else
			{
			si___sPopupCur = 0 - (si___sPopupBtns + 1);
			si___Sound     = SFX_SHELL_BTNB;
			}
		goto allDone;
		}

	// Move left or right ?

	if (si___sPopupBtns > 1)
		{
		if (asi__gTeamBtn[EVERYBODY] & (CTRL_PAD_LEFT | CTRL_PAD_RIGHT))
			{
			if (asi__gTeamBtn[EVERYBODY] & (CTRL_PAD_LEFT))
				{
				if (--si___sPopupCur < 1) si___sPopupCur = si___sPopupBtns;
				}
			else
				{
				if (++si___sPopupCur > si___sPopupBtns) si___sPopupCur = 1;
				}
			si___Sound = SFX_SHELL_CHNG;
			goto allDone;
			}
		}

	// Anything else ?

	if (asi__gTeamBtn[EVERYBODY] &
		(CTRL_TRIGGER |
		 CTRL_PAD_UP | CTRL_PAD_DOWN | CTRL_PAD_LEFT | CTRL_PAD_RIGHT |
		 CTRL_C_UP | CTRL_C_DOWN | CTRL_C_LEFT | CTRL_C_RIGHT |
		 CTRL_SHOULDER_L | CTRL_SHOULDER_R))
		{
		si___Sound = SFX_SHELL_NOMV;
		}

	// All done.

	allDone:

	if (si___Sound >= 0)
	{
		AudioPlaySfx(si___Sound, 11025, 255, 7);
	}

	if ((si___sPopupCur < 0) && (si___sPopupSiz != 0))
	{
		if (ui___gFrameDrawn != 0)
		{
			if ((si___sPopupSiz -= 48) < 0) si___sPopupSiz = 0;
		}
		return (si___sPopupCur - 0);
	}

	return (0 - si___sPopupCur);
}



// **************************************************************************
// * StopPopup ()                                                           *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	void                StopPopup               (void)

	{
	// Are we initialized ?

	if (ub___sPopupInitialized == 0)
		{
		return;
		}

	// Flag the popup as finished.

	if (si___sPopupCur > 0)
		{
		si___sPopupCur = 0 - si___sPopupCur;
		}

	// All done.

	return;
	}



// **************************************************************************
// * DrawPopup ()                                                           *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  dyn *   ?                                                      *
// *         int     ?                                                      *
// *         Gfx **  ?                                                      *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

global	Gfx *               DrawPopup               (
								Gfx *               gp)

	{
	// Local Variables.

	int                 i;
	int                 j;

	int                 x;
	int                 y;
	int                 w;
	int                 h;

	TXTCEL_T *          pcl__Txt;
	UB *                pub__Pal;

	// Are we initialized ?

	if (ub___sPopupInitialized == 0)
		{
		return (gp);
		}

	// Make sure that we're using the shell's drawing modes.

	gp = ResetRenderStatus(gp);

	// Reset controller-button sprites.

	InitFontBtn();

	// Draw the background.

	i = (ppcl_sPopupCels[POPUP_TTL] == NULL) ? 0 : 1;

	gp = RenderMessageBox(gp, i, 256, 120,
			si___sPopupBoxW, si___sPopupBoxH, si___sPopupSiz, pub__sPopupColr);

	// Don't draw anything else if we're not fully expanded.

	if (si___sPopupSiz != 256)
		{
		return (gp);
		}

	// Draw the titlebar.

	pcl__Txt = ppcl_sPopupCels[POPUP_TTL];

	if (pcl__Txt != NULL)
		{
		DrawTxtCel(pcl__Txt,
			256 + psw__sPopupXPos[POPUP_TTL],
			120 + psw__sPopupYPos[POPUP_TTL]);
		}

	// Draw the message text.

	for (i = 0; i < si___sPopupCels; i += 1)
		{
		pcl__Txt = ppcl_sPopupCels[POPUP_TXT + i];

		DrawTxtCel(pcl__Txt,
			256 + psw__sPopupXPos[POPUP_TXT + i],
			120 + psw__sPopupYPos[POPUP_TXT + i]);
		}

	// Draw the button text.

	for (i = 0; i < si___sPopupBtns; i += 1)
		{
		pcl__Txt = ppcl_sPopupCels[POPUP_BTN + i];

		x = psw__sPopupXPos[POPUP_BTN + i] + 256;
		y = psw__sPopupYPos[POPUP_BTN + i] + 120;
		w = si___sPopupBtnW,
		h = si___sPopupBtnH,

		pcl__Txt->ui___tcFlg &= ~FFLG_HILITE;

		if (si___sPopupCur == (i + 1))
			{
			pcl__Txt->ui___tcFlg |= FFLG_HILITE;

			pub__Pal = &pub__sPopupColr[(RGB_BOXHILITE*4)];

			gp = RenderTSHD(gp, x    , y + h, w, 3);
			gp = RenderESHD(gp,	x + w, y + h, 6, 3);
			gp = RenderLSHD(gp, x + w, y    , 6, h);
			}
		else
			{
			pub__Pal = &pub__sPopupColr[(RGB_BOXBUTTON*4)];
			}

		gp = RenderTBOX(gp, x, y,
			si___sPopupBtnW,
			si___sPopupBtnH,
			pub__Pal[0], pub__Pal[1], pub__Pal[2], pub__Pal[3]);

		DrawTxtCel(pcl__Txt,
			x + (si___sPopupBtnW >> 1),	y + BTN_YBORDER);
		}

//	pcl__Txt->ui___tcPal = FPAL_YELLOW;

	// Finally draw characters.

	gp = DrawChrList(gp);

	// Draw controller-button sprites.

	gp = DrawFontBtn(gp);

	// All done.

	return (gp);

	}



// **************************************************************************
// **************************************************************************
// **************************************************************************
//	STATIC FUNCTIONS
// **************************************************************************
// **************************************************************************
// **************************************************************************



// **************************************************************************
// * ParseMessage ()                                                        *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                ParseMessage            (
								char *              pcz__Message)

	{
	// Local Variables.

	int                 i;
	char                c;

	int                 si___Col;
	int                 si___Row;

	TXTCEL_T *          pcl__Txt;
	FNTHDR_T *          pcl__Fnt;

	int                 fl___Btn;
	int                 fl___Ret;

	char *              pcz__String;
	char                acz__String [128];

	// Reset popup status variables.

	si___sPopupCels  = 0;
	si___sPopupBtns  = 0;
	si___sPopupDef   = 0;
	si___sPopupEsc   = 0;

	ub___sPopupF     = FONT_BLKBT18;
	ub___sPopupP     = FPAL_WHITE;
	ud___sPopupM     = FFLG_SHADOW;

	si___sTabCount   =  1;
	aub__sTabType[0] =  1;
	asw__sTabPosn[0] = -1;

	// Process the message.

	si___Col = 0;
	si___Row = 0;
	fl___Btn = 0;
	fl___Ret = 0;

	while (*pcz__Message != 0)
		{
		// Is it a command ?

		if (*pcz__Message == '[')
			{
			switch (pcz__Message[1])
				{
				case 'B':
					{
					fl___Btn = 1;
					pcz__Message  += 5;
					break;
					}
				case 'D':
					{
					si___sPopupDef = si___sPopupBtns + 1;
					fl___Btn = 1;
					pcz__Message  += 5;
					break;
					}
				case 'E':
					{
					si___sPopupEsc = 1;
					pcz__Message  += 5;
					break;
					}
				case 'F':
					{
					switch (pcz__Message[2])
						{
						case 'T': ub___sPopupF  = pcz__Message[3]; pcz__Message += 5; break;
						case 'P': ub___sPopupP  = pcz__Message[3]; pcz__Message += 5; break;
						case 'S': ud___sPopupM ^= FFLG_SHADOW;     pcz__Message += 4; break;
						}
					break;
					}
				case 'T':
					{
					pcz__Message += 4;
					si___sTabCount = 0;
					while (*pcz__Message != ']')
						{
						aub__sTabType[si___sTabCount] = ((UB *) pcz__Message)[0];
						asw__sTabPosn[si___sTabCount] = ((UB *) pcz__Message)[1];
						if (asw__sTabPosn[si___sTabCount] == 255)
							{
							asw__sTabPosn[si___sTabCount] = -1;
							}
						else
							{
							asw__sTabPosn[si___sTabCount] <<= 1;
							}
						si___sTabCount += 1;
						pcz__Message   += 2;
						}
					if (si___sTabCount == 0)
						{
						si___sTabCount   =  1;
						aub__sTabType[0] =  1;
						asw__sTabPosn[0] = -1;
						}
					pcz__Message += 1;
					break;
					}
				default:
					{
					pcz__Message += 1;
					break;
					}
				}

			// Command processed, continue.

			continue;
			}

		// Handle TAB characters.

		if (*pcz__Message == '\t')
			{
			if (si___Col < si___sTabCount)
				{
				si___Col += 1;
				}

			pcz__Message += 1;
			continue;
			}

		// Handle RET characters.

		if (*pcz__Message == '\n')
			{
			pcl__Fnt = apcl_gFontHdr[ub___sPopupF];

			if (fl___Ret == 0)
				{
				si___Row +=
					pcl__Fnt->sb___fntYTop +
					pcl__Fnt->sb___fntYBtm +
					pcl__Fnt->sb___fntYSpc;
				}
			else
				{
				si___Row += 3;
				}

			si___Col = 0;
			fl___Ret = 1;

			pcz__Message += 1;
			continue;
			}

		// Copy the string to a temporary buffer.

		fl___Ret = 0;

		pcz__String = acz__String;

		while ((c = *pcz__Message) != 0)
			{
			if ((c == '[') || (c == '\t') || (c == '\n')) break;

			*pcz__String++ = (char) *pcz__Message++;
			}

		// Was there a string ?

		if (pcz__String == acz__String)
			{
			continue;
			}

		// Allocate a text cel.

		*pcz__String++ = 0;

		if (fl___Btn)
			{
			// Button Text.

			pcl__Txt = InitTxtCel(NULL, 32, ub___sPopupF, ub___sPopupP, FFLG_XC|FFLG_YT);

			ppcl_sPopupCels[POPUP_BTN + si___sPopupBtns] = pcl__Txt;

			si___sPopupBtns++;
			}
		else
			{
			// Normal Text.

			pcl__Txt =
				InitTxtCel(NULL, 80, ub___sPopupF, ub___sPopupP,
					ud___sPopupM | asi__sTabFlag[aub__sTabType[si___Col]]| FFLG_YL);

			ppcl_sPopupCels[POPUP_TXT + si___sPopupCels] = pcl__Txt;
			psw__sPopupXPos[POPUP_TXT + si___sPopupCels] = asw__sTabPosn[si___Col];
			psw__sPopupYPos[POPUP_TXT + si___sPopupCels] = si___Row;

			si___sPopupCels++;
			}

		// Write the string to the text cel.

		WriteTxtCel(pcl__Txt, acz__String);

		// Reset next-string flags, and loop around to process the next part
		// of the message string.

		fl___Btn = 0;
		}

	// Initialize the

	if (si___sPopupDef != 0)
		{
		si___sPopupCur = si___sPopupDef;
		}

	if (si___sPopupCur == 0)
		{
		si___sPopupCur = 1;
		}

	// All Done.

	return;
	}



// **************************************************************************
// * AutoSizeBox ()                                                         *
// **************************************************************************
// *                                                                        *
// **************************************************************************
// * Inputs  -                                                              *
// *                                                                        *
// * Output  -                                                              *
// **************************************************************************

static	void                AutoSizeBox             (void)

	{
	// Local Variables.

	int                 i;
	char                c;

	TXTCEL_T *          pcl__Txt;
	FNTHDR_T *          pcl__Fnt;

	int                 si___ChgX;
	int                 si___ChgY;

	int                 si___TmpL;
	int                 si___TmpR;
	int                 si___TmpT;
	int                 si___TmpB;

	int                 si___TxtL;
	int                 si___TxtR;
	int                 si___TxtT;
	int                 si___TxtB;

	int                 si___TxtW;
	int                 si___TxtH;

	int                 si___BtnW;
	int                 si___BtnH;

	int                 si___BoxW;
	int                 si___BoxH;

	int                 si___BtnA;

	// Reset popup status variables.

	si___TxtL = 0;
	si___TxtR = 0;
	si___TxtT = 0;
	si___TxtB = 0;

	si___TxtW = 0;
	si___TxtH = 0;

	si___BtnW = 0;
	si___BtnH = 0;

	si___BoxW = 0;
	si___BoxH = 0;

	// Search through the message text and find the boundaries.

	for (i = 0; i < si___sPopupCels; i += 1)
		{
		// Locate the text cel and the font used.

		pcl__Txt = ppcl_sPopupCels[POPUP_TXT + i];
		pcl__Fnt = apcl_gFontHdr[pcl__Txt->ui___tcFnt];

		// Does this string expand the box width ?

		si___TmpL = psw__sPopupXPos[POPUP_TXT + i];

		if (si___TmpL < 0)
			{
			// Text is centered in box.

			if (si___TxtW < pcl__Txt->ui___tcW) si___TxtW = pcl__Txt->ui___tcW;
			}
		else
			{
			// Text has absolute position.

			si___TmpL = si___TmpL + pcl__Txt->si___tcX;
			si___TmpR = si___TmpL + pcl__Txt->ui___tcW;

			if (si___TxtL > si___TmpL) si___TxtL = si___TmpL;
			if (si___TxtR < si___TmpR) si___TxtR = si___TmpR;
			}

		// Does this string expand the box height ?

		si___TmpB = psw__sPopupYPos[POPUP_TXT + i];

		si___TmpT = si___TmpB - pcl__Fnt->sb___fntYCap;

		if (pcl__Txt->ui___tcFlg & FFLG_SHADOW)
			{
			si___TmpB += 1;
			}

		if (si___TxtT > si___TmpT) si___TxtT = si___TmpT;
		if (si___TxtB < si___TmpB) si___TxtB = si___TmpB;
		}

	// Convert top and bottom to take into account the YL justification mode.

	si___TxtT += 1;
	si___TxtB += 1;

	// Save size of the message text.

	if (si___TxtW < (si___TxtR - si___TxtL))
		{
		si___TxtW = (si___TxtR - si___TxtL);
		}

	si___TxtH = (si___TxtB - si___TxtT);

	si___BoxW = si___TxtW;
	si___BoxH = si___TxtH;

	// Are there any buttons ?

	if (si___sPopupBtns)
		{
		// Search through the button text and find the boundaries.

		for (i = 0; i < si___sPopupBtns; i += 1)
			{
			// Locate the text cel and the font used.

			pcl__Txt = ppcl_sPopupCels[POPUP_BTN + i];
			pcl__Fnt = apcl_gFontHdr[pcl__Txt->ui___tcFnt];

			// Does this string expand the btn size ?

			if (si___BtnW < pcl__Txt->ui___tcW)     si___BtnW = pcl__Txt->ui___tcW;
			if (si___BtnH < pcl__Fnt->sb___fntYCap) si___BtnH = pcl__Fnt->sb___fntYCap;
			}

		// Now expand each button, and calculate their total width.

		si___BtnW += (BTN_XBORDER * 2);
		si___BtnH += (BTN_YBORDER * 2);

		si___BtnA = ((si___BtnW + BTN_XSPACE) * si___sPopupBtns) - BTN_XSPACE;

		// Do the buttons expand the box width ?

		if (si___BoxW < si___BtnA)
			{
			si___BoxW = si___BtnA;
			}

		// Expand the box height.

		si___BoxH += BOX_YSPACE + si___BtnH;
		}

	// Calculate the full box width and height.

	si___BoxW += (BOX_XBORDER * 2) + (BOX_XSPACE * 2);
	si___BoxH += (BOX_YBORDER * 2) + (BOX_YSPACE * 2);

	// Find out how much to adjust the message coordinates by.

	si___ChgX = BOX_XBORDER + BOX_XSPACE - si___TxtL;
	si___ChgY = BOX_YBORDER + BOX_YSPACE - si___TxtT;

	// Take the titlebar into account.

	if (ppcl_sPopupCels[POPUP_TTL])
		{
		si___BoxH += BOX_YTITLE;
		si___ChgY += BOX_YTITLE;
		}

	// Then adjust again for centered coordinates.

	si___ChgX -= (si___BoxW >> 1);
	si___ChgY -= (si___BoxH >> 1);

	// Adjust each message's position.

	for (i = 0; i < si___sPopupCels; i += 1)
		{
		if (psw__sPopupXPos[POPUP_TXT + i] < 0)
			{
			psw__sPopupXPos[POPUP_TXT + i] = 0;
			}
		else
			{
			psw__sPopupXPos[POPUP_TXT + i] += si___ChgX;
			}

		psw__sPopupYPos[POPUP_TXT + i] += si___ChgY;
		}

	// Find out how much to adjust the button coordinates by.

	si___ChgX = (si___BoxW - si___BtnA) >> 1;
	si___ChgY = si___BoxH - (BOX_YBORDER + BOX_YSPACE + si___BtnH);

	// Then adjust again for centered coordinates.

	si___ChgX -= (si___BoxW >> 1);
	si___ChgY -= (si___BoxH >> 1);

	// Adjust each button's position.

	for (i = 0; i < si___sPopupBtns; i += 1)
		{
		psw__sPopupXPos[POPUP_BTN + i] = si___ChgX;
		psw__sPopupYPos[POPUP_BTN + i] = si___ChgY;

		si___ChgX += (si___BtnW + BTN_XSPACE);
		}

	// Find out how much to adjust the titlebar coordinates by.

	si___ChgX = (si___BoxW >> 1);
	si___ChgY = 3;

	// Then adjust again for centered coordinates.

	si___ChgX -= (si___BoxW >> 1);
	si___ChgY -= (si___BoxH >> 1);

	// Adjust each button's position.

	psw__sPopupXPos[POPUP_TTL] = si___ChgX;
	psw__sPopupYPos[POPUP_TTL] = si___ChgY;

	// Finally, save out the results.

	si___sPopupBoxW = si___BoxW;
	si___sPopupBoxH = si___BoxH;

	si___sPopupBtnW = si___BtnW;
	si___sPopupBtnH = si___BtnH;

	// All Done.

	return;
	}



// **************************************************************************
// **************************************************************************
// **************************************************************************
//	END OF POPUP.C
// **************************************************************************
// **************************************************************************
// **************************************************************************

