/*  Project Tec3270

	 Copyright © 2005. All Rights Reserved.

	 SUBSYSTEM:    alfflibtst.exe Application
	 FILE:         alfflibtst.cpp
	 AUTHOR:       Alcindo Ferreira Filho


	 OVERVIEW
	 ========
	 Source file for implementation of TecWHllTest application
	 (Dialog Main Window that verifies ALFFLib classes)
*/


//#define __TRACE
//#define __WARN

#include <owl\wsksockm.h>
#include <owl\wskhostm.h>
#include "whlltest.h"
#include "whllapi.h"
#include "whllapi.rh"

/* TecWHllAbout Dialog:  TecWHllTest About */
TecWHllAbout::TecWHllAbout(TWindow* Parent, TResId idDlg):
					ALFFDialog(Parent, idDlg)
{
  dateCompiled = new TStatic(this,IDC_DATECOMPILED);
}

TecWHllAbout::~TecWHllAbout()
{
  delete dateCompiled;
}

void
TecWHllAbout::SetupWindow()
{
 ALFFDialog::SetupWindow();
#if (PORTUGUES == 1)
 dateCompiled->SetText("Versão 3.2.0 - 9 de Maio de 2006");
#elif (ENGLISH == 1)
 dateCompiled->SetText("Version 3.2.0 - May 9, 2006");
#else
#error (WHLLTEST.CPP) - ENGLISH or PORTUGUESE must be defined for using localized resources.
#endif 
}


// Dialog Main Window Implementation
DEFINE_RESPONSE_TABLE1(TecWHllTestDlg, ALFFDialog)
END_RESPONSE_TABLE;

//{{ALFFDialog = TecWHllTestDlg}}
// Constructor
TecWHllTestDlg::TecWHllTestDlg(TWindow* parent, TModule* module, TResId idDlg):
          ALFFDialog(parent, idDlg, module)  // center dialog on screen
{
 // WHLLAPI Commands Combobox & Parms to send
 wHllCommands = new TComboBox(this,IDC_WHLLCOMMANDS);
 wHllParmToSend = new TEdit(this,IDC_WHLLSENDPARM);
 wHllParmToSendLength = new TEdit(this,IDC_WHLLPARMLEN);
 wHllPSPosition = new TEdit(this,IDC_WHLLPSPOSITION);
 wHllPSPosition->SetValidator(new TFilterValidator("0-9"));

 // WHLLAPI command response controls
 wHllReturnedParm = new TEdit(this,IDC_WHLLRETURNEDPARM);
 wHllReturnedParmLen = new TEdit(this,IDC_WHLLRETURNEDPARMLEN);
 wHllReturnedCode = new TEdit(this,IDC_WHLLRETCODE);

 // Buttons
 executeComm   = new TButton(this, IDOK);
 aboutTecWHll  = new TButton(this, IDC_TECWHLLABOUT);

 // Messages Class
 pMsgs = new ALFFMsgs(this);

 // WHLLAPI Command Parms
 wHllCommand = -1;
 wHllParm = NULL;
 wHllParmLen = 0;
 wHllRetCode = 0;
}

// Destructor
TecWHllTestDlg::~TecWHllTestDlg()
{
 if (!WinHLLAPICleanup())
	// WHLLAPI not ended Ok, show message
	//pMsgs->Message(IDS_WHLLAPIENDEDOK, IDS_TECWHLLTESTTITLE);
 //else
	pMsgs->Message(IDS_WHLLAPIENDEDNOTOK, IDS_TECWHLLTESTTITLE);

 // WHLLAPI Commands Combobox & Parms to send
 delete wHllCommands;
 delete wHllParmToSend;
 delete wHllParmToSendLength;
 delete wHllPSPosition;

 // WHLLAPI command response controls
 delete wHllReturnedParm;
 delete wHllReturnedParmLen;
 delete wHllReturnedCode;

 // Buttons
 delete executeComm;
 delete aboutTecWHll;

 // TecWHllTest protected objects
 if (pMsgs != NULL)
      delete pMsgs;
}

void
TecWHllTestDlg::SetupWindow()
{
 /* Call Base Class Setup Window */
 ALFFDialog::SetupWindow();

 char tempChar[STRINGMSGLEN+1];
 int  commLoc;

 // WHLLAPI Commands ComboBox Loading
 for (int i=WHLLAPICMDBASE; i < (LASTWHLLCMD+1); i++)
  {
   int len = pMsgs->LoadString(i,tempChar,STRINGMSGLEN);
   if (len)
    {
      commLoc = wHllCommands->AddString(tempChar);
      // Set WHLLAPI command id
      wHllCommands->SetItemData(commLoc, i-WHLLAPICMDBASE);
    }
  }

 wHllCommands->SetSelIndex(0);
 wHllCommands->EnableWindow(true);

 // Other Controls
 wHllParmToSend->SetText("");
 wHllParmToSendLength->SetText("0");
 wHllPSPosition->SetText("");

 // WHLLAPI command response controls
 wHllReturnedParm->SetText("");
 wHllReturnedParmLen->SetText("0");
 wHllReturnedCode->Clear();
 wHllReturnedCode->SetText("0");

 // Buttons
 executeComm->EnableWindow(false);
 aboutTecWHll->EnableWindow(true);

 // Initialize WHLLAPI Interface
 WHLLAPIDATA wHllApiData;

 WORD   version = 0x0001;
 int rc = WinHLLAPIStartup(version, &wHllApiData);
 if (!rc)
  {
	// initialized Ok, show description
	pMsgs->StrMessage(wHllApiData.szDescription, IDS_WHLLAPIOK, IDS_TECWHLLTESTTITLE);
  }
 else
  {
   char tempChar[128];
   pMsgs->LoadString(WHLLAPIRETCODEBASE+rc, tempChar, STRINGMSGLEN);
   pMsgs->StrMessage(tempChar, IDS_WHLLAPINOTOK, IDS_TECWHLLTESTTITLE);
   wHllReturnedCode->SetText(tempChar);
  }
}

void
TecWHllTestDlg::CmAbout()
{
 TecWHllAbout TecWHllAboutDlg(this);
 TecWHllAboutDlg.Execute();
}

void
TecWHllTestDlg::CmCancel()
{
 ALFFDialog::CmCancel();
}

void
TecWHllTestDlg::CmHelp()
{
 return;
 //ALFFDialog::CmCancel();
}

void
TecWHllTestDlg::CmExecute()
{
  char tempMsg[STRINGMSGLEN+1];
  char tempChar[256];

  // Recover WinHLLAPI Parms from dialog

  // Parm and ParmLen
  wHllParmLen = wHllParmToSend->GetText(tempChar,255);
  if (wHllParm != NULL)
     delete wHllParm;
  wHllParm = new char[wHllParmLen+1];
  strcpy(wHllParm,tempChar);

  // PS Position:
  wHllPSPosition->GetText(tempChar,255);
  wHllRetCode = atoi(tempChar);

  WinHLLAPI (&wHllCommand, wHllParm, &wHllParmLen, &wHllRetCode);
  pMsgs->LoadString(WHLLAPIRETCODEBASE+wHllRetCode,tempMsg,STRINGMSGLEN);
  if ( (wHllRetCode == WHLLOK)
	  ||(wHllRetCode == WHLLTRUNCATED)
	  ||(wHllRetCode == WHLLPSBUSY)
	  ||(wHllRetCode == WHLLINHIBITED)
	  ||(wHllRetCode == WHLLPARAMETERERROR) )
	{
	 if (wHllParmLen)
	  {
		 if (wHllCommand != SETSESSIONPARAMETERS)
		  {
			memset(wHllParm+wHllParmLen,NULL,1);
			wHllReturnedParm->SetText(wHllParm);
		  }
		 itoa(wHllParmLen, tempChar, 10);
		 wHllReturnedParmLen->SetText(tempChar);
	  }
	 else
	  {
		wHllReturnedParm->SetText("");
		wHllReturnedParmLen->SetText("");
	  }
	}
  else if (wHllRetCode == WHLLSYSERROR)
	{
	 int extendedError = WinHLLAPIGetExtendedError();
	 itoa(extendedError, tempChar, 10);
	 strcat(tempMsg," (");
	 strcat(tempMsg, tempChar);
	 strcat(tempMsg, ")");
	}
  else
   {
	 wHllReturnedParm->SetText("");
	 wHllReturnedParmLen->SetText("");
	}

  wHllReturnedCode->SetText(tempMsg);
}

// Menu Help ShowWINSOCK Data
void
TecWHllTestDlg::CmInfo()
{
  THostInfoManager host;
  TSocketManager sock;
  char computerName[128];
  char computerAddress[64];

  if (pMsgs != NULL)
   {
    char tempChar[256];
    char szOutput[512];
	 char rscString[STRINGMSGLEN];
	 pMsgs->LoadString(IDS_SOCKETINFO,rscString,STRINGMSGLEN-1);
	 wsprintf(szOutput,rscString,sock.GetDescription(),
                                sock.GetMajorVersion(),
										  sock.GetMinorVersion(),
										  sock.GetMaxSocketsAvailable(),
										  sock.GetSystemStatus());
	 pMsgs->LoadString(IDS_COMPUTERINFO,rscString,STRINGMSGLEN-1);
    host.GetHostName(computerName, 127);
    host.GetHostAddress(computerAddress, computerName);
    
	 wsprintf(tempChar,rscString, computerName, computerAddress);
    strcat(szOutput,"\r\n\r\n");
    strcat(szOutput,tempChar);
	 pMsgs->StrMessage(szOutput,0,IDS_SKTINFOTIT);
   }

}

LRESULT
TecWHllTestDlg::EvCommand(uint controlId, HWND , uint notifyCode)
{
 char msgCntrlId[20];
 //struct ffblk fileInfo;
 char tempChar[256];
 string tempString;
 int index;

 /*
  * controlId is the control identifier that is related to some
  * option on TecWHllTestDlg Dialog.
  */
 switch (controlId)
  {

	/* Enviar comando WHLLAPI  */
	case			        IDOK: CmExecute();
									  break;

	/* Help to this Dialog Fields */
	case              IDHELP: CmHelp();
									  break;

   /* Cancel Button Pressed */
	case            IDCANCEL: CmCancel();
									  break;

   /* About button pressed  */
   case             IDC_TECWHLLABOUT: CmAbout();
                             break;
   /* Info button pressed  */
   case            IDC_COMPUTERINFO:  CmInfo();
                             break;
   /* This is the WHLLAPI to be executed */
   case IDC_WHLLCOMMANDS:    if (notifyCode == CBN_SELCHANGE)
									   {
									    index = wHllCommands->GetSelIndex();
                               wHllCommand = wHllCommands->GetItemData(index);
                               wHllParmToSend->SetText("");
										 wHllParmLen = 0;
										 wHllReturnedParm->SetText("");
										 wHllReturnedParmLen->SetText("");
                               wHllReturnedCode->SetText("");
										 if ( (wHllCommand == DISCONNECTPS)
                                  ||(wHllCommand == QUERYCURSORLOC)
											 ||(wHllCommand == RESETSYSTEM)
                                  ||(wHllCommand == WAIT)
											 ||(wHllCommand == RELEASE)
											 ||(wHllCommand == RESERVE) )
											executeComm->EnableWindow(true);
										}
                             break;

   case IDC_WHLLSENDPARM:    if (notifyCode == EN_KILLFOCUS)
									   {
									    wHllParmLen = wHllParmToSend->GetText(tempChar,255);
                               if (wHllParm != NULL)
                                 delete wHllParm;
                               wHllParm = new char[wHllParmLen+1];
                               strcpy(wHllParm,tempChar);
									   }
                             else if (notifyCode == EN_CHANGE)
                              {
                               wHllParmLen = wHllParmToSend->GetText(tempChar,255);
                               itoa(wHllParmLen, tempChar, 10);
                               wHllParmToSendLength->SetText(tempChar);
                               if ((wHllParmLen)
                                 &&(wHllCommand != SETCURSOR)
                                 &&(wHllCommand != COPYSTRTOPS)
                                 &&(wHllCommand != COPYPSTOSTR)
                                 &&(wHllCommand != QUERYCLIENTCERTIFICATE)
                                 &&(wHllCommand != QUERYSERVERCERTIFICATE))
											executeComm->EnableWindow(true);
                               else executeComm->EnableWindow(false);
                              }
									  break;

   case IDC_WHLLPARMLEN:
                             break;

   case IDC_WHLLPSPOSITION:  if (notifyCode == EN_KILLFOCUS)
									   {
									    wHllPSPosition->GetText(tempChar,255);
                               wHllRetCode = atoi(tempChar);
									   }
                             else if (notifyCode == EN_CHANGE)
                              {
                               int textLen = wHllPSPosition->GetText(tempChar,255);
                               wHllRetCode = atoi(tempChar);
                               if (textLen)
                                {
                                 if (wHllCommand == SETCURSOR)
											   executeComm->EnableWindow(true);
                                 else if ((wHllParmLen)
                                       &&((wHllCommand == COPYSTRTOPS)
                                        ||(wHllCommand == COPYPSTOSTR)
                                        ||(wHllCommand == QUERYCLIENTCERTIFICATE)
                                        ||(wHllCommand == QUERYSERVERCERTIFICATE)))
                                    executeComm->EnableWindow(true);
                                 else executeComm->EnableWindow(false);
                                }
                              }
                             break;

   case IDC_WHLLRETURNEDPARM:
                             break;

   case IDC_WHLLRETURNEDPARMLEN:
                             break;

   case IDC_WHLLRETCODE:
                             break;
   

	default      : itoa(controlId,msgCntrlId,10);
                  pMsgs->StrMessage(msgCntrlId,IDS_TECWHLLDLGERROR,IDS_TECWHLLTESTTITLE);
						break;
  }
 return 0L;
}

// Main window (TFrameWindow)
DEFINE_RESPONSE_TABLE1(TecWHllFrameWindow,TFrameWindow)
END_RESPONSE_TABLE;
TecWHllFrameWindow::TecWHllFrameWindow(TWindow* parent,
                   const char far *title,
                   TWindow* clientWnd,
                   bool shrinkToClient,
                   TModule* module):
                   TFrameWindow(parent, title, clientWnd, shrinkToClient,module)
{
}

TecWHllFrameWindow::~TecWHllFrameWindow()
{
}

/////////////////////////////////////////////////////////////
// Main Application Implementation
class TecWHllTest : public TApplication
{
	public:
     TecWHllTest(char *title);
     ~TecWHllTest();

   void InitMainWindow()
    {
      tecWHllTestDlg = new TecWHllTestDlg(0, this);
      tecWHllFrameWindow = new TecWHllFrameWindow(0, "Tec3270 WHLLAPI 32 Bits Tester",
                                                  tecWHllTestDlg, true);

      //x509FrameWindow->SetClientWindow(x509Test);

      SetMainWindow(tecWHllFrameWindow);
      tecWHllFrameWindow->SetIcon(this,IDI_TECWHLLTESTICON);
      MainWindow->Attr.Style &= ~WS_THICKFRAME & ~WS_MAXIMIZEBOX;
      EnableCtl3d();
    }

   protected:
      TecWHllTestDlg*     tecWHllTestDlg;
      TecWHllFrameWindow* tecWHllFrameWindow;

};

TecWHllTest::TecWHllTest(char* title):TApplication(title)
{
}

TecWHllTest::~TecWHllTest()
{
}

int OwlMain(int, char*[])
 {
  try {
   TecWHllTest tecWHllTest("WHLLAPI32 Tester");

   return tecWHllTest.Run();
  }
  catch (xmsg& x) {
		  ::MessageBox(0, x.why().c_str(),
							"Tec3270 WHLLAPI Tester: Unexpected Exception",
							MB_OK);
	 }

	 return -1;
 }