#if !defined(__alffnmd1_h)    // Sentry, use file only if it's not already included.
#define __alffnmd1_h
/*  Project Tec3270

	 SUBSYSTEM:    Tec3270.exe (Tec3270 application)
	 FILE:         alffnmd1.h
	 AUTHOR:       Alcindo Ferreira Filho

	 OVERVIEW:

    ALFFMDIChild Class Definition, the Tec3270 session class.
    Sessions are childs of class ALFFMDIClient, the MDI manager.

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

    LICENSE:      Tec3270_License (docs/Tec3270_License.txt)
*/
#include "alffnaux.h"
#include "alffmsgs.h"   /* TecLib Message Object Include */
#include "alffinif.h"   /* TecLib Ini File Object Include */
#include "alffnmdc.h"
#include "alfflogf.h"   /* TecLib Log File Object Include */
#include "alffnttl.h"
#include "ALFFNvt.h"
#include "alff3270.h"
#include "alffntsc.h"
#include "alffsocket.h"
#include "alffnprt.h"   /* Tec3270 Remote Printer */

// Script File Debug On/Off switch
//#define TRACESCRIPT    1

//{{TMDIChild = ALFFMDIChild}}
class ALFFMDIChild : public TMDIChild
{
 public:
	 ALFFMDIChild (TMDIClient &parent,
							const char far *title,
                     ALFFDate::DateFormat fmt,
							TWindow *clientWnd,
							int childCount,
							bool shrinkToClient = false,
							TModule* module = 0);

	 virtual ~ALFFMDIChild ();
    char far* GetClassName() {return "ALFFMDIChild";}

	 bool Connect(char* sessName=NULL, bool isWHLL=false, char* certPath=NULL, char* certPwd=NULL);
	 bool Disconnect(bool checkExitOnClose=false);
	 void ReceiveData(bool urgent=false);
	 void SendData(bool urgent=false);
    bool IsWaitingHostResponse() {return waitingHost;}
    void SetWaitingHostResponse(bool isWaiting) { waitingHost = isWaiting; }
    //
	 ALFFNvt*         GetNVTBufPtr()     			{return pNVTBuf;}
	 ALFF3270*        Get3270BufPtr()    			{return p3270Buf;}
	 ALFFVirtualTerm* GetTerminalPtr()   			{return pTerminal;}
    ALFFTelnet*      GetTelnetPtr()    			{return pTelnet;}
	 ALFFConnData*    GetChildConnData()         {return connData;}
    ALFFCommLine*    GetLinePtr()               {return pLineComm;}
    //
    ALFFRemotePrinter* GetRemotePrinter() {return pRemotePrt;}
    bool IsIdle()              {return (nLineState == nIdle);}
	 bool IsConnected()         {return (nLineState == nConnected);}
	 bool IsConnecting()        {return (nLineState == nConnecting);}
	 //bool IsSSLInitializing()   {return (nLineState == nSSLInitializing); }
	 bool IsSSLConnecting()     {return (nLineState == nSSLConnecting); }
    bool IsSSLConnected()      {return (nLineState == nSSLConnected); }
    bool IsSSLDisconnecting()  {return (nLineState == nSSLDisconnecting); }
    bool IsSecureConnection()  {return (connData->securityMethod != ALFFSSLComm::ALFFNONE); }
    void ShowCertificate();
    // WHLLAPI
    void SetWHLLSession(bool isWHLL) {wHLLSession = isWHLL;}
    bool IsWHLLSession()       { return wHLLSession; }
    bool IsWHLLPSChanged() { return sessionPSChangeType != PSNOTCHANGED; }
    unsigned char GetWHLLPSChangeType()    { return sessionPSChangeType; }
    void SetWHLLPSChanged(unsigned char changeType)    { sessionPSChangeType = changeType; }

    void SetWHLLHostNotification(unsigned char notifyPSChanges) { sessionNotifyPSChanges = notifyPSChanges; }
    bool IsWHLLHostNotifying() { return sessionNotifyPSChanges; }
    short CopyPSToString(unsigned char** byString, short byStringLen, short psPosition,
                         bool noAttrb, bool noEAB, bool eabXlat, bool noDisplay);
    short CopyStringToPS(unsigned char* byString, short byStringLen, short psPosition);
    short SendKeyStrokes(unsigned char* byString, short& byStringLen,
                         bool autoReset, char escapePFKChar);
    string QueryCertInfo(bool isClient, int queryType);
    bool   IsWHLLClosing() { return isWHLLClosing; }
    void   SetWHLLClosing(bool isClosing) { isWHLLClosing = isClosing; }
    //
 protected:
	 /* Objects that are needed to establish any 3270 session to host     */
    ALFFDate::DateFormat	dateFormat;		  /* Current localization           */
	 ALFFMDIClient* 	   	MDIParent;       /* MDI Client Parent              */
	 ALFFConnData*     		connData;        /* Connection Struct Data         */
	 ALFFLogFile*      		pTrcFile;        /* Line Recv/Send Trace Data      */
	 ALFFLogFile*      		pConnLog;        /* Session Messages Log File      */
	 ALFFVirtualTerm*     	pTerminal;       /* Virtual Terminal (Kbd/Scr)     */
	 ALFFCommLine*  	   	pLineComm;       /* Communications (Winsock)       */
	 ALFFTelnet*    		   pTelnet;         /* TELNET Encoder/Decoder         */
	 ALFFNvt*          		pNVTBuf;         /* TELNET NVT Buffer Manager      */
	 ALFF3270*      		   p3270Buf;        /* 3270 Buffer Manager            */
	 ALFFMsgs*  		 		pMsgs;           /* Messages/Strings Manager       */
    ALFFScript*    	    	pScript;         /* Tec3270 Script File Manager    */
    ALFFRemotePrinter* 	   pRemotePrt;      /* Tec3270 TN3270E Remote Printer */

	 /* Session Buffers (Berkeley Ring Buffers) */
	 LPALFFBUF     pLineInBuf;
	 LPALFFBUF     pLineOutBuf;
	 LPALFFBUF     pNetInBuf;
	 LPALFFBUF     pNetOutBuf;
	 LPALFFBUF     pOOBData;

	 /* Session Status Controls */
	 int           connNumber;
	 int           connShift;
	 bool          closeConn;
	 bool          autoConnect;
	 long          connTimeout;
	 bool          timerOn;
	 int           timerType;
	 int           recvQueue;
	 bool          receiving;
	 int           sendQueue;
	 bool          sending;
	 bool          termBufferInitialized;
	 bool          traceON;
	 bool          scrLogOn;
	 int           numReconnTries;
    bool          scriptProcessing;
    bool          disconnectedByHost;
	 int           netInputQueue;
	 bool          netInputProcessing;
    WPARAM*       dataTypeQueue;
    bool          waitingHost;
    int           connectionError;
    
    // WHLL controls
    bool          wHLLSession;
    bool          isWHLLClosing;
    unsigned char sessionNotifyPSChanges;
    unsigned char sessionPSChangeType;
    char*         hllCertPath;
    char*         hllCertPwd;

	 /* Session Window Title Composition:
	  * connTitle = connStatusTitle + connTextTitle + connTraceTitle */
	 string        connInitialTitle;
	 string        connTitle;
	 string        connStatusTitle;
    string        connSSLStatus;
	 string        connTextTitle;
	 string        connTraceTitle;

	 string        connHostAddress;
	 string        connPortAddress;

	 /*        Line Connection State Information            */
	 enum      nState  {nIdle, nConnecting, nConnected, //nSSLInitializing,
                       nSSLConnecting, nSSLConnected, nSSLDisconnecting /*,
                       nSSLWaitingForRead, nSSLWaitingForWrite*/} nLineState;
	 bool      dataSent;

    /* Session SSL Controls           */
    bool                      connectPendingForReadWrite;
    bool                      receiveDataPendingForReadWrite;
    bool                      sendDataPendingForReadWrite;
    bool                      disconnectPendingForReadWrite;

	 /* Find/Replace Controls & Method */
	 TFindReplaceDialog::TData SearchData;
	 TFindReplaceDialog*       SearchDialog;  // Find or replace dialog
	 uint                      SearchCmd;     // command set that opened dialog
	 void                      CannotFind();
	 bool                      DoSearch();
	 bool                      strReplaced;
	 bool                      strFound;
	 uint32                    lastOper;

	 /* Session Connect Disconnect Menu Commands */
	 void CmConnect();
	 void CmConnectEnable(TCommandEnabler &ce);
	 void CmDisconnect();
	 void CmDisconnectEnable(TCommandEnabler &ce);

	 /* Undo, Cut, Copy, Paste, Clear and Delete Selection Methods */
	 /*
	  * Command Response functions
	  */
	 void    CmEditCut();  //{Cut();}                  // CM_EDITCUT
	 void    CmEditCopy(); //{Copy();}                // CM_EDITCOPY
	 void    CmEditPaste(); //{Paste();}              // CM_EDITPASTE
	 void    CmEditDelete(); //{DeleteSelection();}   // CM_EDITDELETE
	 void    CmEditClear(); //{Clear();}              // CM_EDITCLEAR
	 void    CmEditUndo(); //{Undo();}                // CM_EDITUNDO
	 /*
	  * Command enabler functions
	  */
	 void CmSelectEnable(TCommandEnabler& commandHandler);
	 void CmPasteEnable(TCommandEnabler& commandHandler);
	 void CmCharsEnable(TCommandEnabler& commandHandler);
	 void CmModEnable(TCommandEnabler& commandHandler);

	 /*
	  * Child Id notification handled at the child
	  */
	 void    ENErrSpace();  // EN_ERRSPACE


	 /* Internal Methods/Procedures */
	 void SetupWindow();
	 void SetWindowTitle();
	 bool InitTerminalBuffer(bool nvt=false);
	 bool CloseTerminal();
	 void TimeMessage();
	 void SessionMessage(const int errorId, bool fromPrinter=false);
    void SessionMessage(const char* errorMsg, bool fromPrinter=false);
	 void ResetTimer(bool forceSet=false);
	 void SetIdleState();
	 void SetConnectingState()        {nLineState = nConnecting;}
	 void SetConnectedState();
    void SetSSLConnectingState()     {nLineState = nSSLConnecting;}
    void SetSSLConnectedState();
    void SetSSLDisconnectingState()  {nLineState = nSSLDisconnecting;}
	 bool CanClose();
	 void SetLogScreenMenu();
	 void SetScrLogOn(bool status){scrLogOn = status;}
	 bool GetScrLogOn() {return scrLogOn;}
	 void ExecuteScript();
    void LineError(int cError);
    void ActivateConnection();
    int  ConnectSecurity();
    void DisconnectedByHost();
    void ReceiveDataEnqueued(bool urgent=false);
    int  ProcessScript(ScriptCommand* comando);
#ifdef TRACESCRIPT
    void TraceScript(ScriptCommand* comando);
#endif
    void WriteScriptText(char* text);
    bool BuildTN3270EResponse(LPALFFBUF pNetOutBuf,
                              int printerStatus,
                              int retCode,
                              unsigned char* requestFlag,
                              unsigned char* seqNumber);

//{{ALFFMDIChildRSP_TBL_BEGIN}}
	 LRESULT DoClusterNotification(WPARAM wParam, LPARAM lParam);
	 LRESULT DoLineNotification(WPARAM, LPARAM lParam);
    LRESULT EvStartTLS(WPARAM, LPARAM);
	 LRESULT DoNetInputProcess(WPARAM, LPARAM);
	 LRESULT DoNetOutputProcess(WPARAM, LPARAM);
	 LRESULT DoNetOutputEcho(WPARAM, LPARAM);
	 LRESULT UpdateFont(WPARAM wparam, LPARAM lparam);
    LRESULT UpdateSecurityOptions(WPARAM wparam, LPARAM lparam);
	 LRESULT UpdateColors(WPARAM wparam, LPARAM lparam);
	 LRESULT UpdateKeyboard(WPARAM wparam, LPARAM lparam);
	 LRESULT EvResetTimer(WPARAM, LPARAM);
  	 LRESULT EvInitTerminal(WPARAM, LPARAM);
    LRESULT EvRemPrtNotify(WPARAM wParam, LPARAM lParam);
    LRESULT EvStartPrinter(WPARAM wParam, LPARAM lParam);
	 void    EvSize(uint sizeType, TSize& size);
	 void    EvTimer(uint timerId);
	 void    EvKillFocus(HWND hWndGetFocus);
    LRESULT EvOIAChanged(WPARAM wParam, LPARAM lParam);
    LRESULT EvLogMessage(WPARAM wParam, LPARAM msgId);

	 /* Find/Replace menu command handlers */
	 void          CmEditFind();               // CM_EDITFIND
	 void          CmEditReplace();            // CM_EDITREPLACE
	 void          CeEditFindReplace(TCommandEnabler& ce);
	 void          CmEditFindNext();           // CM_EDITFINDNEXT
	 void          CeEditFindNext(TCommandEnabler& ce);
	 LRESULT       EvFindMsg(WPARAM, LPARAM);  // Registered commdlg message

	 /* Options menu command handlers */
	 void    CmTraceSession();
	 void    CmToolsLogScreen();
	 void    CmChooseCursor();
	 void    CmChangeSession();
	 void    CmTraceSessionEnable(TCommandEnabler &ce);
	 void    CmSessionEnable(TCommandEnabler &ce);
    void    CmToolsLogScreenEnable(TCommandEnabler &ce);

    /* Security menu command handlers  */
    void    CmShowCertificate();
    void    CmShowCertificateEnable(TCommandEnabler &ce);
    void    CmShowSecurityInfo();

//{{ALFFMDIClientRSP_TBL_END}}
DECLARE_RESPONSE_TABLE(ALFFMDIChild);
};    //{{ALFFMDIChild}}
#endif                                      // __alffnmd1_h sentry.
