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

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

	 OVERVIEW:

    ALFFCursorSelect and ALFFVirtualTerm classes definition. They are used,
    respectively, to change 3270 cursor format and 3270 terminal screen
    simulation.

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

    LICENSE:      Tec3270_License (docs/Tec3270_License.txt)
*/
#include <owl\owlpch.h>
#include "alffmsgs.h"
#include "alffkeyb.h"
#include "alff3270.h"          // Definition for 3270 Terminal
#include "ALFFNvt.h"           // Definition for TELNET NVT
#include "alffnaux.h"          // Utilities Classes for Tec3270
#include "alffnspt.h"          // Script File Definitions
#include "tec3270.rh"          // Definition of all resources.

/* Protected Variables For Internal Methods Control */
enum nCursorType {nCursorBLOCK, nCursorUNDER, nCursorVERT};

//{{ALFFDialog = ALFFCursorSelect}}
class ALFFCursorSelect : public ALFFDialog
{
 public:
  ALFFCursorSelect (TWindow* parent, TResId idDlg,
						  nCursorType& nCursor, nCursorType& iCursor,
                    bool& horizLine, bool& vertLine);
  ~ALFFCursorSelect();
  char far* GetClassName() {return "ALFFCursorSelect";}
 protected:
	 void 		SetupWindow();
	 void       CmOk();
	 void       CmHelp();
	 LRESULT    EvCommand(uint controlId, HWND , uint notifyCode);
	 TWindow*       	scParent;
	 ALFFMsgs*        pMsgs;
	 nCursorType&     normCursor;
	 nCursorType&     insCursor;
    nCursorType      wNormCursor, wInsCursor;
    bool&            horizEffect;
    bool&            vertEffect;

	 // Normal Mode Cursor Selection Options
	 TGroupBox*    normalCursorGrp;
	 TRadioButton* normalUnder;
	 TRadioButton* normalBlock;
	 TRadioButton* normalVert;

	 // Insertion Mode Cursor Selection Options
	 TGroupBox*    insCursorGrp;
	 TRadioButton* insUnder;
	 TRadioButton* insBlock;
	 TRadioButton* insVert;

	 // Special Effects  Cursor Selection Options
	 TGroupBox*    specCursorGrp;
    TCheckBox*    specHorizLine;
    TCheckBox*    specVertLine;
};

//{{TWindow = ALFFVirtualTerm}}
class ALFFVirtualTerm : public TWindow
{
 public:
	 ALFFVirtualTerm(TWindow* parent = 0,
						const char far* text = 0,
						TModule* module = 0);
	 virtual ~ALFFVirtualTerm ();
    char far* GetClassName() {return "ALFFVirtualTerm";}
	 /*
	  * Functions included for text painting
	  */
	 Field* GetTextField(int pos);
	 void PaintScreen(bool erase=false);
	 void UnlockKeyboard();
	 void LockKeyboard(char* err3270=NULL);
	 bool ReLoadKeyboard(int keybId);
	 void WriteNVTLine(int resourceId, char* errText=NULL);
	 void WriteNVTLine(const char* line);
	 bool ProcessCommand(int command, bool ignoreKbLock=false);
	 int  GetNumberOfTextItens();

	 void SetScrLineSize(const int defLSize, const int altLSize);
	 void SetScrNumLines(const int defNLines, const int altNLines);
	 int  GetScrLineSize() {return scrLineSize;}
	 int  GetScrNumLines() {return scrNumLines;}
	 bool SetScreen(const int scrType);

	 unsigned int GetCursorPos();
	 void SetCursorPos(const TPoint newCursorPos, bool setCursor=true);
	 void SetCursorPos(const int newCursorPos, bool setCursor=true);
  	 void UpdateCursor(bool removeSel=true);
	 bool IsKeyboardLocked();
	 void SetMode3270(bool mode);
	 bool IsMode3270() {return mode3270;};
	 void InitTerminal(TWindow* parent, ALFF3270* p3270,
							 ALFFNvt* pNVT, ALFFConnData* cnnData);
	 void SetScreenFont(bool redraw = false);
	 void SetColors(ALFFConnData* newCnnData);
	 void ResetScrColor();
	 void ClearScr(COLORREF bkgdColor);
	 bool GetInsertMode() {return insertMode;}
	 bool UpdateScreen(const int fdBegin, const int option,
							 const int subOption=CHARUPDATED);
	 void RemoveSelection(bool resetBuf=false, bool up=false, bool updateCursor=true);
	 void SetSelection(TPoint newPoint);
	 void UpdateScrSel(const uint fromBufPos, const uint toBufPos);
	 bool IsTextSelected() {return blkMarked;}
	 void ChangeCursor();
	 void SetScroll();
	 bool IsConnected() {return connected;}
    void SetEchoMode(bool echo);
	 void Connect(bool mode3270, bool justConnect=false);
	 void Disconnect();
	 bool IsTraceOn() {return traceOn;}
	 void SetTraceOn(bool status) {traceOn = status;}
    void SetMenuTraceStat();
	 bool IsLogOn() {return logOn;}
	 void SetLogOn(bool status);

 protected:
	 TWindow*      tParent;
	 ALFFKeyboard* pKeyboard;
	 ALFF3270*     p3270Buf;
	 ALFFNvt*      pNVTBuf;
	 ALFFConnData* connData;
	 ALFFMsgs*     pMsgs;

	 /* Trace File (Stream) */
	 ofstream   scrtrace;

	 /* Internal Procedures */
	 void SetConnected(bool connStatus) {connected = connStatus;}
	 void DisplayStatusLine(TDC& dc);
	 void DisplayField(const Field* field, TDC& dc,
							 const COLORREF bgColor,
							 const int lineSize,
							 const int scrBegLine,
							 const int scrEndLine,
							 const int fCharWidth,
							 const int fCharHeight,
							 bool  eraseLine=false);
#ifdef TRACEKEYBOARD
	 void LogEv(char* evName, UINT key, BYTE vKey, BYTE SCode,
					 bool shift,bool ctrl, bool alt, UINT command=0);
#endif
	 void ResetTimer();
	 void BuildKeyboard();
	 void UpdateScroll(TPoint& winPos, bool convertToScrolled);
	 void BuildCursor(TPoint cursorPos);
	 void SetCursor();
	 void KillCursor();
	 void WriteCursorPos();
	 bool NextLine(int& line, int begLine, int endLine, int offset=1);
	 bool NextCol(int& col, int lineSize, int offset=1);
	 bool PrevLine(int& line, int begLine, int endLine, int offset=1);
	 bool PrevCol(int& col, int lineSize, int offset=1);
	 bool DelChar();
	 void Paint (TDC& dc, bool erase, TRect& rect);
	 void PaintTerminal(TDC& dc, TRect& pRect, bool erase=false);
	 bool WithinRange(const TPoint chkPoint,const TPoint topPoint,const TPoint botPoint);
	 void InvertScrColor(TDC& dc, const TPoint fromPoint, const TPoint toPoint,
								bool  convertToScreen=true);
	 void UpdateScrSel(const TPoint fromPoint, const TPoint toPoint, bool charCoordinates=false);

	 /* Protected Variables For Internal Methods Control */
	 nCursorType 	nCursor, nNormCursor, nInsCursor;
    bool          horizEffect;
    bool          vertEffect;
    bool          firstCursor;

	 /* Cursor Position and Selected Text Range Controls */
	 /* Cursor Position Control */
	 TPoint    screenPos;
	 TPoint	  currentPos;
	 TPoint	  cursorPos;
	 TPoint	  nvtCursorPos;
	 bool      cursorVisible;
    bool      lockedCursor;

	 /* Text Selection */
	 TPoint    startSel; // Point where Selected Text Begins (Cursor Position reference)
	 TPoint    endSel;   // Point where Selected Text Ends   (Cursor Position reference)
	 bool      blkMarked;
	 bool      blkInverse;
	 bool      markNoBlk;

	 /* Tec3270 Configuration */
    bool      terminalInited;
	 bool      traceOn;
    bool      logOn;
    bool      connected;
    bool      hasFocus;
	 bool      keyboardLocked;
	 bool      mode3270;
	 bool      lineBehavior;
	 bool      insertMode;
    bool      remoteEcho;
	 int       fieldCount;    // Number of Fields on Screen
	 Field     field;         // Screen Field
	 Screen    screenBlink;
    bool      blink;
    int       scrType;
	 int       scrLineSize;
	 int       scrNumLines;
	 int       defScrLineSize;
	 int       defScrNumLines;
	 int       altScrLineSize;
	 int       altScrNumLines;
	 TFont*    scrFont;
	 int       fontCharWidth;
	 int       fontCharHeight;
	 int       statusLinePos;
	 int       timePos;
    int       xSystemPos;
	 char*     charSystem;
    time_t    oldTimer;

//{{ALFFVirtualTermVIRTUAL_BEGIN}}
 public:
	 virtual void SetupWindow();
    virtual bool IdleAction(long idleCount);
//{{ALFFVirtualTermVIRTUAL_END}}

//{{ALFFVirtualTermRSP_TBL_BEGIN}}
protected:
	 void EvVScroll(uint scrollCode, uint thumbPos, HWND hWndCtl);
	 void EvHScroll(uint scrollCode, uint thumbPos, HWND hWndCtl);
	 void EvSize(uint sizeType, TSize& size);
	 void EvSetFocus(HWND);
	 void EvKillFocus(HWND);
	 void EvGetMinMaxInfo (MINMAXINFO far& minmaxinfo);
	 void EvChar(UINT key, UINT repeatCount, UINT flags);
	 void EvKeyDown(UINT key, UINT repeatCount, UINT flags);
	 void EvKeyUp(UINT key, UINT repeatCount, UINT flags);
	 void EvSysKeyDown(UINT key, UINT repeatCount, UINT flags);
	 void EvLButtonDown(uint modKeys, TPoint& point);
	 void EvLButtonDblClk(uint modKeys, TPoint& point);
	 void EvRButtonDown(uint modKeys, TPoint& point);
	 void EvMouseMove(uint modKeys, TPoint& point);
//{{ALFFVirtualTermRSP_TBL_END}}
DECLARE_RESPONSE_TABLE(ALFFVirtualTerm);
};    //{{ALFFVirtualTerm}}
#endif                                      // __alffntsc_h sent	ry.
