/*  Project Tec3270

	 SUBSYSTEM:    Tec3270.exe (Tec3270 application)
	 FILE:         alffmdlg.cpp
	 AUTHOR:       Alcindo Ferreira Filho

	 OVERVIEW:
	 ALFFSessSelect,  ALFFAutoConnect, ALFFChooseFontDialog,
    ALFFFontOptionsDialog, and ALFFSecurityOptions
    Classes Implementation, main Tec3270 application options
    dialogs.

    COPYRIGHT:    © 1998-2007
                  Alcindo Ferreira Filho
                  <alcindo@inttec.com.br>

    LICENSE:      Tec3270_License (docs/Tec3270_License.txt)
*/
#include "alffnall.h"

//{{ALFFMDIClient Dialogs Implementation}}

//////////////////////////////////////////////////////////
// ALFFSessSelect  Dialog
// ======================
//
// Define Dialog Response Table
//
DEFINE_RESPONSE_TABLE1(ALFFSessSelect,ALFFDialog)
  EV_BN_CLICKED(IDOK,CmOk),
  EV_BN_CLICKED(IDHELP,CmHelp),
  EV_CHILD_NOTIFY_ALL_CODES(IDC_SESSLIST,NotifyList),
  EV_WM_PAINT,
END_RESPONSE_TABLE;

////////////////////////////////////////////////////////////
// Construction/Destruction handling.
ALFFSessSelect::ALFFSessSelect(TWindow* Parent)
									  :ALFFDialog(Parent,IDD_SESSSELECT)
{
 keyList = new TListBox(this,IDC_SESSLIST,Parent->GetModule());
 sessList = new char[MAXSESSIONS*(SESSNAMELEN+1)];
 sessIndex = new int[MAXSESSIONS];
 pMsgs = new ALFFMsgs(Parent);

 /* Get the Application IniFile Pointer  */
 Tec3270App *theApp = TYPESAFE_DOWNCAST(GetApplication(),Tec3270App);
 iniFile = theApp->GetIniFile();
}

ALFFSessSelect::~ALFFSessSelect()
{
 delete keyList;
 delete [] sessList;
 delete [] sessIndex;
 delete pMsgs;
}

void
ALFFSessSelect::SetupWindow()
{
 ALFFDialog::SetupWindow();

 /* Set Dialog Title */
 char dlgTitle[STRINGMSGLEN];
 pMsgs->LoadString(IDS_ALFFAUTOSELSESSTIT, dlgTitle, STRINGMSGLEN);
 SetCaption(dlgTitle);

 int i;
 char* pList;
 char iniSection[21] = "CONNECTIONS";
 /* Initialize connections name list to nulls for identifying list end */
 memset(sessList,NULL,MAXSESSIONS*(SESSNAMELEN+1));
 for (i = 0; i < MAXSESSIONS; i++)
  {
	sessIndex[i] = -1;
  }

 keyList->ClearList();
 keyList->SetRedraw(false);

 /********************************************************
  * Getting All Sessions That Must Be Defined Before     *
  ********************************************************/
 int rc = iniFile->GetInt(iniSection,"QUANTITY",0);
 if (!rc)
  {
	 sessExist=false;
	 keyList->SetRedraw(true);
	 return;
  }
 rc = iniFile->GetString(iniSection,NULL,"    ",
								 sessList,MAXSESSIONS*(SESSNAMELEN+1));
 if (rc)
  {
	 /* Process all connections that was read and build an access index */
	 i = 0;
	 pList = sessList;
	 while (strlen(pList))
	  {
		 if (strcmp(pList,"QUANTITY"))
			sessIndex[i++] = (int)(pList-sessList);
		 pList += strlen(pList)+1;
	  }
	 if (i > 0)
	  {
		for (int j = 0; j < i; j++)
		{
		 pList = sessList+sessIndex[j];
		 if (keyList->AddString(pList) < 0)
		  TXOwl(IDS_SESSLISTADDERR );
		}
		keyList->SetSelIndex(0);
		selIndex = 0;
		sessExist=true;
	  }
	  else sessExist = false;
  }
 keyList->SetRedraw(true);
}

char*
ALFFSessSelect::ExecuteSel()
{
 if (keyList)
  {
	if (Execute() == IDOK )
	  return sessList;
	else return NULL;
  }
  else return NULL;
}

void
ALFFSessSelect::CmOk()
{
 if (keyList->GetSelString(sessList,SESSNAMELEN) <= 1)
  {
	selIndex = -1;
	return;
  }
  else
	{
	 /*
	  * Remember that GetSelIndex return an index relative to
	  * 0 (first element has index 0) so we must add 1 to get
	  * correct command identifier selected.
	  */
	 selIndex = keyList->GetSelIndex();
    CloseWindow(IDOK);
	}
}

void
ALFFSessSelect::CmHelp()
{
 /* Get the Application IniFile Pointer  */
 Tec3270App *theApp = TYPESAFE_DOWNCAST(GetApplication(),Tec3270App);
 ALFFHelp* helpFile = theApp->GetHelpFile();
 HWND helpWin = helpFile->ShowHelp(HH_HELP_CONTEXT, IDH_HELP_SESSION_SELECT, IDH_POPUP_WINDOW);
 if (!helpWin)
   pMsgs->Message(IDS_ALFFHELPNOTAVAIL, IDS_ALFFSESSTITLE);
}
                                           
void
ALFFSessSelect::EvPaint()
{
 /*
  * This is a little tweak to fix a problem with dialogs, when it is
  * moved, because combolist will not disappear in its old location.
  */
 ALFFDialog::EvPaint();
}

void
ALFFSessSelect::NotifyList(UINT code)
{
 if (code == LBN_DBLCLK)
  CmOk();
}


//////////////////////////////////////////////////////////
// ALFFAutoConnect  Dialog
// =======================
//
ALFFAutoConnect::ALFFAutoConnect (TWindow* parent,
											 TResId idDlg) :
											 ALFFDialog(parent, idDlg)
{
 acParent = parent;

 pMsgs  = new ALFFMsgs(this);

 /* Automation Number  Control Object       */
 autoListBox  = new TListBox(this, IDC_ALFFAUTOLIST);

 /* Act/Inact Automations Control Object */
 checkInact   = new TCheckBox(this, IDC_ALFFAUTOINACT);

 /* Get the Application IniFile Pointer  */
 Tec3270App *theApp = TYPESAFE_DOWNCAST(GetApplication(),
													 Tec3270App);
 iniFile = theApp->GetIniFile();

 /* Automated Sessions List Controls .............*/
 autoList = new char[(MAXSESSIONS*(SESSNAMELEN+1))+2];
 autoSelIndex = 0;
 lastAutoNumber = 0;
}

ALFFAutoConnect::~ALFFAutoConnect()
{
 /* Current Session Number  */
 delete autoListBox;
 delete checkInact;
 delete [] autoList;
 delete pMsgs;
}

void
ALFFAutoConnect::SetupWindow()
{
 /* Call Base Class Setup Window */
 ALFFDialog::SetupWindow();
 /*
  * Load Automated Connections List Box:
  */
 if (LoadAutomations())
  {
	autoSelIndex = 0;
	autoExist = true;
  }
  else autoExist = false;
 autoListBox->SetRedraw(true);

 /* Get Automations Status */
 char iniSection[24] = "AUTOMATIC CONNECTIONS";
 autoInact = iniFile->GetInt(iniSection,"STATUS",0);
 if (autoInact)
  {
	checkInact->Check();
	autoListBox->EnableWindow(false);
	autoListBox->SetSelIndex(-1);
  }
 else
  {
	checkInact->Uncheck();
	autoListBox->EnableWindow(true);
	autoListBox->SetSelIndex(0);
  }
}

bool
ALFFAutoConnect::LoadAutomations()
{
 int j;
 char* pList;
 /*
  * Load Connection List Box:
  */
 char iniSection[24] = "AUTOMATIC CONNECTIONS";
 memset(autoList,0xff,(MAXSESSIONS*(SESSNAMELEN+1)));
 /******************************************************
  * Getting All Automated Connections already defined  *
  ******************************************************/
 int rc = iniFile->GetInt(iniSection,"STATUS",255);
 if (rc == 255)  // section not found
  {
	lastAutoNumber = 0;
	return false;
  }
 int sizeAutoList = (MAXSESSIONS*(SESSNAMELEN+1));
 rc = iniFile->GetString(iniSection,NULL," ",
								(char* far)autoList,
								sizeAutoList);
 if (rc)
  {
	 /* Process all connections that was read and build an access index */
	 char autoName[2*SESSNAMELEN];
	 char sessName[SESSNAMELEN+1];

	 pList = autoList;
	 j = 0;
	 while (strlen(pList))
	  {
		 if (strcmp(pList,"STATUS"))
		  {
			iniFile->GetString(iniSection,pList,"---",sessName,SESSNAMELEN);
			BuildAutoName(autoName,j+1,sessName);
			if (autoListBox->AddString(autoName) < 0)
				TXOwl(IDS_SESSLISTADDERR );
			j++;
		  }
		 pList += strlen(pList)+1;
	  }
	 if (j>0)
	  {
		lastAutoNumber = j;
		return true;
	  }
	 else
	  {
		lastAutoNumber = 0;
		return false;
	  }
  }
 lastAutoNumber = 0;
 return false;
}

bool
ALFFAutoConnect::RenumAutomations()
{
 char* pList;
 char* pSess;

 char iniSection[24] = "AUTOMATIC CONNECTIONS";
 /******************************************************
  * Getting All Automated Connections already defined  *
  ******************************************************/
 if (iniFile->GetString(iniSection,NULL," ",
								 autoList,MAXSESSIONS*(SESSNAMELEN+1)) )
  {
	/* Creating Automated Sessions Name List */
	char* sessList = new char[MAXSESSIONS*(SESSNAMELEN+1)];
	memset(sessList,NULL,MAXSESSIONS*(SESSNAMELEN+1));

	pSess = sessList;
	pList = autoList;
	while (strlen(pList))
	 {
	  if (strcmp(pList,"STATUS"))
		{
		 /* Get Automated Session */
		 iniFile->GetString(iniSection,pList,"   ",pSess,SESSNAMELEN);
		 /* Delete Session from Ini File */
		 iniFile->WriteString(iniSection,pList,NULL);
		 pSess+=strlen(pSess)+1;
		}
	  pList+=strlen(pList)+1;
	 }

	/* Create All Automated Sessions with new sequence Id */
	pSess = sessList;
	char autoId[16];
	char autoName[21];
	pMsgs->LoadString(IDS_ALFFAUTOSESSION,autoId,STRINGMSGLEN);
	int j = 1;
	while (strlen(pSess))
	 {
	  /* Write Automation to Ini File */
	  wsprintf(autoName,autoId,j++);
	  iniFile->WriteString(iniSection,autoName,pSess);
	  pSess+=strlen(pSess)+1;
	 }

   delete [] sessList;

	return true;
  }
 return false;
}

void
ALFFAutoConnect::BuildAutoName(char* autoName, const UINT sessNum,
										 const char* sessName)
{
 char autoId[STRINGMSGLEN+1];
 char autoStr[STRINGMSGLEN+1];
 pMsgs->LoadString(IDS_ALFFAUTOSESSION,autoStr,STRINGMSGLEN);
 wsprintf(autoId,autoStr,sessNum);
 strcpy(autoName,autoId);
 strcat(autoName," = ");
 strcat(autoName,sessName);
}


bool
ALFFAutoConnect::NewAutomation(char* autoName)
{
 bool done;
 char connSection[24] = "AUTOMATIC CONNECTIONS";
 char autoId[2*SESSNAMELEN];
 char autoStr[STRINGMSGLEN+1];
 char sessName[SESSNAMELEN+1];
 char* newAuto;
 pMsgs->LoadString(IDS_ALFFAUTOSESSION,autoStr,STRINGMSGLEN);
 wsprintf(autoId,autoStr,++lastAutoNumber);
 ALFFSessSelect*  sessSelect = new ALFFSessSelect(this);
 newAuto = sessSelect->ExecuteSel();
 if (newAuto)
  {
	strcpy(sessName,newAuto);
	if (iniFile->WriteString(connSection,autoId,sessName))
	 {
	  BuildAutoName(autoName,lastAutoNumber,sessName);
	  done = true;
	 }
	 else done = false;
  }
 else done = false;

 delete sessSelect;
 return done;
}

bool
ALFFAutoConnect::DelAutomation(const UINT sessNum)
{
 char connSection[24] = "AUTOMATIC CONNECTIONS";
 if (sessNum)
  {
	char sessionId[21];
	char session[STRINGMSGLEN+1];
	pMsgs->LoadString(IDS_ALFFAUTOSESSION,session,STRINGMSGLEN);
	wsprintf(sessionId,session,sessNum);
	if (iniFile->WriteString(connSection,sessionId,NULL))
	 {
		autoListBox->ClearList();
		autoExist = false;
		if (RenumAutomations())
		 {
		  if (LoadAutomations())
			{
			 if (sessNum > 2)
				autoListBox->SetSelIndex(sessNum-2);
			 else autoListBox->SetSelIndex(0);
			 autoExist = true;
			}
		 }
		autoListBox->SetRedraw(true);
		return true;
	 }
	 else return false;
  }
  else return false;
}

bool
ALFFAutoConnect::ChangeAutomation(const UINT sessNum, const char* sessName)
{
 char connSection[24] = "AUTOMATIC CONNECTIONS";
 char autoName[2*SESSNAMELEN];
 if (sessNum && strlen(sessName))
  {
	char sessionId[21];
	char session[STRINGMSGLEN+1];
	pMsgs->LoadString(IDS_ALFFAUTOSESSION,session,STRINGMSGLEN);
	wsprintf(sessionId,session,sessNum);
	if (iniFile->WriteString(connSection,sessionId,sessName))
	 {
	  autoListBox->DeleteString(sessNum-1);
	  BuildAutoName(autoName,sessNum,sessName);
	  autoListBox->AddString(autoName);
	  autoListBox->SetSelIndex(autoSelIndex);
	  return true;
	 }
	 else return false;
  }
  else return false;
}

void
ALFFAutoConnect::CmOk()
{
 /* Get Automations Status */
 char iniSection[24] = "AUTOMATIC CONNECTIONS";
 iniFile->WriteInt(iniSection,"STATUS",autoInact);
 ALFFDialog::CmOk();
}

void
ALFFAutoConnect::CmHelp()
{
 /* Get the Application IniFile Pointer  */
 Tec3270App *theApp = TYPESAFE_DOWNCAST(GetApplication(),Tec3270App);
 ALFFHelp* helpFile = theApp->GetHelpFile();

 HWND helpWin = helpFile->ShowHelp(HH_HELP_CONTEXT, IDH_HELP_AUTOMAT_SESSIONS, IDH_POPUP_WINDOW);
 if (!helpWin)
   pMsgs->Message(IDS_ALFFHELPNOTAVAIL, IDS_TEC3270_HELP_ERR);
}

LRESULT
ALFFAutoConnect::EvCommand(uint controlId, HWND , uint notifyCode)
{
 ALFFSessSelect* sessSelect;
 char sessName[SESSNAMELEN+1];
 char autoName[2*SESSNAMELEN];
 char* newSess;
 /*
  * controlId is the control identifier that is related to some
  * option on Connect Dialog.
  */
 switch (controlId)
  {
	/* Connect to PushButton Pressed */
	case			        IDOK: CmOk();
									  break;

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

	/* Create an new Automatic Sess Connection on Ini File */
	case 	   IDC_ALFFAUTONEW: if (!autoInact)
										{
										 if (NewAutomation(&autoName[0]))
										  {
											autoSelIndex = autoListBox->AddString(autoName);
											autoListBox->SetSelIndex(autoSelIndex);
											autoExist = true;
										  }
										}
										else pMsgs->Message(IDS_AUTOINACTISON,IDS_ALFFAUTOTITLE);
									  break;

	/* Delete this Automatic Connection Configuration from Ini File */
	case 	   IDC_ALFFAUTODEL: if (autoExist)
										{
										 if (!autoInact)
										  {
											if (pMsgs->Message(IDS_AUTOSUREDEL,IDS_ALFFAUTOTITLE, true)
													== IDOK)
											  {
												/* Delete the connection and take it out from the list box*/
												if (!DelAutomation(autoSelIndex+1))
												  pMsgs->Message(IDS_AUTODLGERROR,IDS_ALFFAUTOTITLE);
												else autoSelIndex = autoListBox->GetSelIndex();
											  }
										  }
										  else pMsgs->Message(IDS_AUTOINACTISON,IDS_ALFFAUTOTITLE);
										}
										else
										  pMsgs->Message(IDS_AUTODONOTEXIST,IDS_ALFFAUTOTITLE);
									  break;

	/* Modify Selected Automatic Sess Connection on Ini File */
	case 	IDC_ALFFAUTOCHANGE: if (autoExist)
										{
										 if (!autoInact)
										  {
											sessSelect = new ALFFSessSelect(this);
											newSess = sessSelect->ExecuteSel();
											delete sessSelect;
											sessSelect = NULL;
											if (newSess)
											 {
											  strcpy(sessName,newSess);
											  if (strlen(sessName))
												 ChangeAutomation(autoSelIndex+1,sessName);
											 }
										  }
										 else pMsgs->Message(IDS_AUTOINACTISON,IDS_ALFFAUTOTITLE);
										}
									  else pMsgs->Message(IDS_AUTODONOTEXIST,IDS_ALFFAUTOTITLE);
									  break;

	/* Select or Insert New Automatic Connection Parms */
	case    IDC_ALFFAUTOLIST: switch (notifyCode)
										{
										 /* Sess Combo box notification codes */
										 case   CBN_ERRSPACE: pMsgs->Message(IDS_AUTODLGERROR,
																						 IDS_ALFFAUTOTITLE);
																	 break;

										 case  CBN_SELCHANGE: autoSelIndex = autoListBox->GetSelIndex();
																	 autoListBox->GetString(autoName,autoSelIndex);
																	 autoExist=true;
																	 break;
										 case  CBN_KILLFOCUS: autoSelIndex = autoListBox->GetSelIndex();
																	 break;
										}
									  break;

	/* Inact All Automations Defined             */
	case   IDC_ALFFAUTOINACT: if (autoInact)
										{
										 autoListBox->EnableWindow(true);
										 autoListBox->SetSelIndex(autoSelIndex);
										 autoInact = false;
										 checkInact->Uncheck();
										}
									  else
										{
										 autoListBox->EnableWindow(false);
										 autoListBox->SetSelIndex(-1);
										 autoInact = true;
										 checkInact->Check();
										}
									  break;

	default             : char msgString[STRINGMSGLEN];
								 char msgError[STRINGMSGLEN];
								 pMsgs->LoadString(IDS_AUTOUNKNOWCOMMAND,msgString,STRINGMSGLEN);
								 sprintf(msgError,msgString,controlId, notifyCode);
								 pMsgs->StrMessage(msgError,IDS_AUTODLGERROR,IDS_ALFFAUTOTITLE);
								 break;
  }

 return 1L;
}

//////////////////////////////////////////////////////////
// ALFFFontOpionsDialog
// ====================
//
ALFFFontOptionsDialog::ALFFFontOptionsDialog (TWindow* parent,
							 ALFFChooseFontDialog::TData& fData):
							 ALFFDialog(parent, IDD_ALFFFONTOPTIONS),
							 Data(fData)
{
 /* Automatic Window Sizing to Fit Font Metrics */
 windowSizing = new TCheckBox(this, IDC_ALFFWINDOWSIZING);

 /* Automatic Font Sizing to Fit On Full Screen */
 fontSizing = new TCheckBox(this, IDC_ALFFFONTSIZING);
}

ALFFFontOptionsDialog::~ALFFFontOptionsDialog()
{
 if (windowSizing)
	delete windowSizing;
 if (fontSizing)
	delete fontSizing;
}

void
ALFFFontOptionsDialog::SetupWindow()
{
 ALFFDialog::SetupWindow();
 /* Set Controls that Tec3270 has included */
 if (Data.AutomaticWindowSizing)
	  windowSizing->Check();
 else windowSizing->Uncheck();

 if (Data.AutomaticFontSizing)
	  fontSizing->Check();
 else fontSizing->Uncheck();
}

void
ALFFFontOptionsDialog::CmOk()
{
 /* Do Standard Dialog Ending ...*/
 ALFFDialog::CmOk();
}

void
ALFFFontOptionsDialog::CmHelp()
{
 /* Get the Application IniFile Pointer  */
 Tec3270App *theApp = TYPESAFE_DOWNCAST(GetApplication(),Tec3270App);
 ALFFHelp* helpFile = theApp->GetHelpFile();

 HWND helpWin = helpFile->ShowHelp(HH_HELP_CONTEXT, IDH_HELP_TERMINAL_FONT_OPTIONS, IDH_POPUP_WINDOW);
 if (!helpWin)
  {
   ALFFMsgs pMsgs(this);
   pMsgs.Message(IDS_ALFFHELPNOTAVAIL, IDS_TEC3270_HELP_ERR);
  }
}

LRESULT
ALFFFontOptionsDialog::EvCommand(uint controlId, HWND, uint)
{
 switch (controlId)
  {
	/* OK PushButton Pressed */
	case			        IDOK: CmOk();
									  break;

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

	/* Cancel the Dialog */
	case            IDCANCEL: ALFFDialog::CmCancel();
									  break;

	/* Process Tec3270 Window and Font Automatic Sizing Options */
	case IDC_ALFFWINDOWSIZING: if (Data.AutomaticWindowSizing)
												{
												 Data.AutomaticWindowSizing = false;
												 windowSizing->Uncheck();
												}
											else
												{
												 Data.AutomaticWindowSizing = true;
												 windowSizing->Check();
												}
											break;
	case   IDC_ALFFFONTSIZING: if (Data.AutomaticFontSizing)
												{
												 Data.AutomaticFontSizing = false;
												 fontSizing->Uncheck();
												}
											else
												{
												 Data.AutomaticFontSizing = true;
												 fontSizing->Check();
												}
											break;
	default : break;
  }
 return 1L;
}

//////////////////////////////////////////////////////////
// ALFFChooseFontDialog
// ====================
//
ALFFChooseFontDialog::ALFFChooseFontDialog (TWindow* parent,
							 ALFFChooseFontDialog::TData& fData,
							 const char far* title,
							 bool showOptions,
							 TModule* module):
							 TChooseFontDialog(parent, fData, 0,
													 title, module),
							 Data(fData)
{
 fontOptions = showOptions;
 cf.Flags   |= CF_SHOWHELP	;  // Force Help button
 if (fontOptions)
  {
	cf.Flags |= CF_APPLY;
	optionsButton = new TButton(this, 1026); // Apply Button Changed !!
  }
 else optionsButton = NULL;

}

ALFFChooseFontDialog::~ALFFChooseFontDialog()
{
 if (optionsButton)
	delete optionsButton;
}

void
ALFFChooseFontDialog::SetupWindow()
{
 TChooseFontDialog::SetupWindow();

 char* optButtonTit = new char[STRINGMSGLEN];
 ALFFMsgs* pMsgs = new ALFFMsgs(this);
 pMsgs->LoadString(IDS_ALFFFONTOPTTIT,optButtonTit,STRINGMSGLEN-1);
 delete pMsgs;

 /* Centering the Dialog on Screen */
 TRect dialogRect = GetWindowRect();
 int screenWidth = GetSystemMetrics(SM_CXSCREEN);
 int screenHeight = GetSystemMetrics(SM_CYSCREEN);
 TPoint topLeft((screenWidth - dialogRect.Width()) / 2,
					 (screenHeight - dialogRect.Height()) / 2);
 TRect newPosition(topLeft,dialogRect.Size());
 MoveWindow(newPosition);
 if (optionsButton)
	optionsButton->SetCaption(optButtonTit);
 delete [] optButtonTit;
}

void
ALFFChooseFontDialog::CmHelp()
{
 /* Get the Application IniFile Pointer  */
 Tec3270App *theApp = TYPESAFE_DOWNCAST(GetApplication(),Tec3270App);
 ALFFHelp* helpFile = theApp->GetHelpFile();

 int helpItem;
 if (optionsButton)
   helpItem = IDH_HELP_TERMINAL_FONT;
 else helpItem = IDH_HELP_PRINTER_FONT;
 HWND helpWin = helpFile->ShowHelp(HH_HELP_CONTEXT, helpItem, IDH_POPUP_WINDOW);
 if (!helpWin)
  {
   ALFFMsgs pMsgs(this);
   pMsgs.Message(IDS_ALFFHELPNOTAVAIL, IDS_TEC3270_HELP_ERR);
  }
}

LRESULT
ALFFChooseFontDialog::EvCommand(uint controlId, HWND hwnd, uint notifyCode)
{
 ALFFFontOptionsDialog* fontOptionsDlg;
 switch (controlId)
  {
   case 1038: CmHelp();
                break;

	/* Process Tec3270 Window and Font Automatic Sizing Options */
	case 1026: fontOptionsDlg = new ALFFFontOptionsDialog(this,Data);
					 fontOptionsDlg->Execute();
					 delete fontOptionsDlg;
					 break;
	default : return TChooseFontDialog::EvCommand(controlId, hwnd, notifyCode);
  }
 return 1L;
}


// ALFFSecurityOptions Dialog Box Implementation
ALFFSecurityOptions::ALFFSecurityOptions (TWindow* parent, TResId idDlg):
                     ALFFDialog(parent, idDlg)
{
  verifyServerCertCheck = new   	TCheckBox(this, IDC_ALFFSECVERIFYSERVER);
  verifyRevokedCheck = new   	TCheckBox(this, IDC_ALFFSECVERIFYREVOKED);
  telnetTLSMethodGroup = new   	TGroupBox(this, IDC_ALFFSECTLSMETHODGRP);
  sslv3Radio = new   	TRadioButton(this, IDC_ALFFSECTLSMETHODSSLV3, telnetTLSMethodGroup);
  tlsv1Radio = new   	TRadioButton(this, IDC_ALFFSECTLSMETHODTLSV1, telnetTLSMethodGroup);

  /* Get the Application IniFile Pointer  */
  Tec3270App *theApp = TYPESAFE_DOWNCAST(GetApplication(),Tec3270App);
  iniFile = theApp->GetIniFile();

  pMsgs = new ALFFMsgs(this);
}


ALFFSecurityOptions::~ALFFSecurityOptions()
{
 delete pMsgs;
 delete verifyServerCertCheck;
 delete verifyRevokedCheck;
 delete telnetTLSMethodGroup;
 delete sslv3Radio;
 delete tlsv1Radio;
}

void
ALFFSecurityOptions::SetupWindow()
{
 char iniSection[24] = "SECURITY OPTIONS";

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

 /******************************************************
  * Getting All Security Options previous defined      *
  ******************************************************/
 telnetTLSMethod  = iniFile->GetInt(iniSection,"TLSMETHOD",ALFFSSLComm::ALFFSSLV3);
 verifyServerCert = iniFile->GetInt(iniSection,"VERIFYSERVERCERT",true);
 verifyRevoked = iniFile->GetInt(iniSection,"VERIFYREVOKED",false);

  if (telnetTLSMethod == ALFFSSLComm::ALFFSSLV3)
   sslv3Radio->Check();
 else if (telnetTLSMethod == ALFFSSLComm::ALFFTLSV1)
   tlsv1Radio->Check();

 if (verifyServerCert)
  {
   verifyServerCertCheck->Check();
   if (verifyRevoked)
      verifyRevokedCheck->Check();
   else verifyRevokedCheck->Uncheck();
  }
 else
  {
   verifyServerCertCheck->Uncheck();
   verifyRevoked = false;
   verifyRevokedCheck->Uncheck();
   verifyRevokedCheck->EnableWindow(false);
  }


 sslv3Radio->EnableWindow(true);
 tlsv1Radio->EnableWindow(true);
 verifyServerCertCheck->EnableWindow(true);
}

void
ALFFSecurityOptions::CmOk()
{
 char iniSection[24] = "SECURITY OPTIONS";
 iniFile->WriteInt(iniSection,"TLSMETHOD",telnetTLSMethod);
 iniFile->WriteInt(iniSection,"VERIFYSERVERCERT",verifyServerCert);
 iniFile->WriteInt(iniSection,"VERIFYREVOKED",verifyRevoked);
 ALFFDialog::CmOk();
}

void
ALFFSecurityOptions::CmHelp()
{
 /* Get the Application IniFile Pointer  */
 Tec3270App *theApp = TYPESAFE_DOWNCAST(GetApplication(),Tec3270App);
 ALFFHelp* helpFile = theApp->GetHelpFile();

 HWND helpWin = helpFile->ShowHelp(HH_HELP_CONTEXT, IDH_HELP_SECURITY_OPTIONS, IDH_POPUP_WINDOW);
 if (!helpWin)
  {
   ALFFMsgs pMsgs(this);
   pMsgs.Message(IDS_ALFFHELPNOTAVAIL, IDS_TEC3270_HELP_ERR);
  }
}

LRESULT
ALFFSecurityOptions::EvCommand(uint controlId, HWND , uint notifyCode)
{
 switch (controlId)
  {
	/* OK PushButton Pressed */
	case	IDOK:
          CmOk();
			 break;

	/* Cancel the Dialog */
	case  IDCANCEL:
          ALFFDialog::CmCancel();
			 break;

	/* OK PushButton Pressed */
	case	IDHELP:
          CmHelp();
			 break;

	case  IDC_ALFFSECVERIFYSERVER:
          if (verifyServerCert)
           {
            verifyServerCert = false;
            verifyServerCertCheck->Uncheck();
            verifyRevoked = false;
            verifyRevokedCheck->Uncheck();
			   verifyRevokedCheck->EnableWindow(false);
           }
          else
           {
            verifyServerCert = true;
            verifyServerCertCheck->Check();
			   verifyRevokedCheck->EnableWindow(true);
           }
          break;

   case  IDC_ALFFSECVERIFYREVOKED:
          if (verifyRevoked)
           {
            verifyRevoked = false;
            verifyRevokedCheck->Uncheck();
           }
          else
           {
            verifyRevoked = true;
            verifyRevokedCheck->Check();
           }
          break;

   case  IDC_ALFFSECTLSMETHODGRP:
          break;

   case  IDC_ALFFSECTLSMETHODSSLV3:
          if (notifyCode == BN_CLICKED)
             telnetTLSMethod = ALFFSSLComm::ALFFSSLV3;
          break;

   case  IDC_ALFFSECTLSMETHODTLSV1:
          if (notifyCode == BN_CLICKED)
             telnetTLSMethod = ALFFSSLComm::ALFFTLSV1;
          break;

	default :
          char msgString[STRINGMSGLEN];
			 char msgError[STRINGMSGLEN];
			 pMsgs->LoadString(IDS_AUTOUNKNOWCOMMAND,msgString,STRINGMSGLEN);
			 sprintf(msgError,msgString,controlId, notifyCode);
			 pMsgs->StrMessage(msgError,IDS_ALFFSECURITYOPTIONSDLGERR,IDS_ALFFSECURITYOPTIONSDLGTIT);
          break;
  }
 return 1L;
}


