Source

gdt / SpriteEd / imgdim.cpp

#include <OWL\Dialog.H> /* TDialog */
#include <OWL\Edit.H> /* TEdit */
#include "Stds\DLStdInc.H" /* Daybo Logic standard inclusions */
#pragma hdrstop

#include "Headers\PCMath.H" // for powi
#include "SpriteEd.RH" // Resource defines
#include "ImgDim.H" // Our header
//---------------------------------------------------------------------------
TImgDimDlg::TImgDimDlg(TWindow* parent, TModule* module)
:	TSelfCenteringDialog(parent, MAKEINTRESOURCE(IDD_IMAGEDIMS), module),
	_BPP(8),
   _width(20),
   _height(20), /* Defaults for when user first creates an image without visiting this dialog */
   _noEditChangeEvents(0) /* Changes fires event by default */
{
	WidthEdit = new TEdit(this, IDC_WIDTHEDIT, 17+1);
   HeightEdit = new TEdit(this, IDC_HEIGHTEDIT, 17+1);
   BPPEdit = new TEdit(this, IDC_BPPEDIT, 2+1);
   SizeResult = new TStatic(this, IDC_SIZERESULT);
}
//---------------------------------------------------------------------------
void TImgDimDlg::SetupWindow()
{
	TSelfCenteringDialog::SetupWindow(); // Call next base up the chain

   MoveToWindowCentre(Parent->HWindow); // Move to centre of parent
}
//---------------------------------------------------------------------------
void TImgDimDlg::EvValuesChanged()
{
	/* Called when any of the three edit boxes values are changed */

   if (!_noEditChangeEvents) /* Provided nobody objects... */
   {
   	// locals
   	char buff[100];
   	unsigned long rcolors, imgsize, temp;

   	rcolors = powli(2, GetBPPBox());
   	temp = ((GetWidthBox() * GetHeightBox()) * GetBPPBox());
   	imgsize = temp /8; /* Bits to bytes */
   	if (temp % 8 > 0) /* Remainder in conversion from bits to bytes? */ imgsize++; /* One more byte then */
   	sprintf(buff, "Resulting colors: %lu, image size: %lu bytes", rcolors, imgsize);
   	SizeResult->SetText(buff);
   }
}
//---------------------------------------------------------------------------
unsigned long TImgDimDlg::GetBPPBox()
{
	char far* buffer = BPPEdit->LockBuffer();
   unsigned long r = (unsigned long)atol(buffer);
   BPPEdit->UnlockBuffer(buffer);
   return r;
}
//---------------------------------------------------------------------------
unsigned long TImgDimDlg::GetWidthBox()
{
	char far* buffer = WidthEdit->LockBuffer();
	unsigned long r = (unsigned long)atol(buffer);
   WidthEdit->UnlockBuffer(buffer);
   return r;
}
//---------------------------------------------------------------------------
unsigned long TImgDimDlg::GetHeightBox()
{
	char far* buffer = HeightEdit->LockBuffer();
	unsigned long r = (unsigned long)atol(buffer);
   HeightEdit->UnlockBuffer(buffer);
   return r;
}
//---------------------------------------------------------------------------
void TImgDimDlg::SetBPPBox(unsigned long newval)
{
	char far* buffer = BPPEdit->LockBuffer();
   sprintf(buffer, "%lu", newval);
   BPPEdit->UnlockBuffer(buffer, (NATIVEBOOLTYPE)TRUE);
}
//---------------------------------------------------------------------------
void TImgDimDlg::SetWidthBox(unsigned long newval)
{
	char far* buffer = WidthEdit->LockBuffer();
   sprintf(buffer, "%lu", newval);
   WidthEdit->UnlockBuffer(buffer, (NATIVEBOOLTYPE)TRUE);
}
//---------------------------------------------------------------------------
void TImgDimDlg::SetHeightBox(unsigned long newval)
{
	char far* buffer = HeightEdit->LockBuffer();
   sprintf(buffer, "%lu", newval);
   HeightEdit->UnlockBuffer(buffer, (NATIVEBOOLTYPE)TRUE);
}
//---------------------------------------------------------------------------
void TImgDimDlg::CmOk() // OK button clicked
{
	BOOL Problems = FALSE; /* Set to TRUE if you encounter a problem */

	if (GetHeightBox() < 1) /* Must have a height! */
   {
   	::MessageBox(HWindow, "Height must be greater than 0", NULL, MB_OK + MB_ICONEXCLAMATION);
      Problems = TRUE; /* Had a problem */
   }
   if (GetWidthBox() < 1) /* Must have a width */
   {
   	::MessageBox(HWindow, "Width must be greater than 0", NULL, MB_OK + MB_ICONEXCLAMATION);
      Problems = TRUE; /* Had a problem */
   }
   if (GetBPPBox() < 1) /* Must sprcify bits per pixel */
   {
   	::MessageBox(HWindow, "Must specify number of bits to use per pixel!", NULL, MB_OK + MB_ICONEXCLAMATION);
      Problems = TRUE; /* Had a problem */
   }

   if (!Problems) /* No problems? */
   {
   	EditBoxesToMembers(); // Saves stuff from form

      SendMessage(WM_CLOSE); /* Time to close */
   }
}
//---------------------------------------------------------------------------
void TImgDimDlg::EvShowWindow(NATIVEBOOLTYPE /*show*/, uint /*stat*/)
{
	_noEditChangeEvents++; // Don't update now!
	MembersToEditBoxes(); // Reads stuff onto the form
   _noEditChangeEvents--; // Fine
}
//---------------------------------------------------------------------------
void TImgDimDlg::EditBoxesToMembers()
{
	_BPP = GetBPPBox();
   _height = GetHeightBox();
   _width = GetWidthBox();
}
//---------------------------------------------------------------------------
void TImgDimDlg::MembersToEditBoxes()
{
	_noEditChangeEvents++; /* Don't bother updating until we are through! */

	SetBPPBox(_BPP);
   SetHeightBox(_height);

   _noEditChangeEvents--; /* Update on last one, we saved two futile refreshes */

   SetWidthBox(_height);
}
//---------------------------------------------------------------------------