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

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

	 OVERVIEW:

    ALFFNvt class definition. It is used to manage the basic terminal
    (Network Virtual Terminal - NVT) buffer and screen.

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

    LICENSE:      Tec3270_License (docs/Tec3270_License.txt)
*/
//
#include <owl\owlpch.h>
#include "alffnaux.h"          // Utilities Classes for Tec3270
#include "alffbuff.h"          // TecLib Ring Buffer Header
#include "alff3270.rh"         // TecLib Ring Buffer Header
#include "tec3270.rh"          // Tec3270 defines

//{{ALFFNvt}}
class ALFFNvt
{
  public:
	 ALFFNvt(ALFFConnData* connData, bool printer);
	 virtual ~ALFFNvt();
    char far* GetClassName() {return "ALFFNvt";}
	 /*  NVT Process Public Function Members     */
	 void   InitBuf(const int defNumLines, const int defLineSize,
						 const int altNumLines, const int altLineSize,
						 const bool colorTerm=false, const bool extendedAttr=false);
	 void   ClearBuf();
	 void   Reset();
	 int    DataFromNetwork(LPALFFBUF pNetInBuf);
	 int    WriteBuffer(char** streamNVT, int& streamSize);
	 int    DataToNetwork(LPALFFBUF, int mode);
	 Field* DataToScreen(int& fromPos, const int option,
								const int suboption=TOENDOFFIELD,
								const int code=ASCII);
    int    DataToPrinter(ALFFPrinterBuffer*  printerBuf);                        
	 int    DataFromKeyboard(const char* buffer, int count, bool insertMode);
	 bool   IsModified() { return bufferUserModified; }
	 int    DelBufferChar(const int option=DELCHAR, const int pos=-1);
	 int    DelWord();
	 void   FlushAllData();
	 void   TermNVT_Type();
	 void   FinishNVT();
	 bool   IsTerminalOn();
	 int    GetNVTBufSize();
	 TPoint GetCursorPos(UINT pos=0, bool convertToPoint=false);
	 TPoint GetNextUnprotField(const int fromPos);
	 TPoint GetPrevWord(const int fromPos);
	 TPoint GetNextWord(const int fromPos);
	 TPoint GetPrevUnprotField(const int fromPos);
	 TPoint GetBeginOfField(const int fromPos);
	 TPoint GetEndOfField(const int fromPos);
	 bool   IsEndOfField();
	 bool   IsWithinField(const int initPos, const int increment);
	 void   SetCursorPos(unsigned int pos);
	 void   SetCursorPos(TPoint* pos);
	 void   CommandKey(HWND hWindow, int command);
	 unsigned char GetAID(int command);

	 /* Find/Replace Support Methods */
	 int     Replace(const char far* str);
	 int     Search(uint startPos, const char far* text,
						 bool caseSensitive=false, bool wholeWord=false,
						 bool up=false, bool select=true);
	 int     GetSelection(uint& startPos, uint& endPos) const;
	 bool    SetSelection(const int startPos, const int endPos=0);
	 bool    SetSelection(const TPoint startPos, const TPoint endPos);
	 int     ExecuteUndo();
	 char*   GetLastStreamNVT() {return lastStreamNVT;}
	 int     GetLastStreamSize() {return lastStreamSize;}

  protected:
	 ALFFConnData* connData;
    bool          isPrinter;
    bool          endOfMessage;

	 /*
	  * NVT Terminal Controls
	  */
	 Field         scrField;
	 unsigned char defaultHighLight;
	 unsigned char defaultCharSet;
	 COLORREF      defaultCharColor;
	 COLORREF      defaultBkgdColor;
	 unsigned char defaultTransparency;
	 unsigned char defaultValidation;
	 unsigned char defaultOutLining;
	 bool          colorTerminal;
	 bool          extendedAttribute;
	 unsigned char lastAID;
	 unsigned int  cursorPos;
	 unsigned int  currentBufPos;
	 unsigned int  lastBufPosRecv;
	 unsigned int  unprotBeginPos;
    unsigned int  unprotEndPos;
	 int           lastBufPosMod;
	 int           lastCharUpdated;
	 unsigned int  sizeNVTBuf;
	 unsigned int  currScrBegin;
	 unsigned int  beginKeybModPos;
    unsigned int  endKeybModPos;
	 unsigned int  scrLineSize;
	 unsigned int  scrNumLines;
	 unsigned int  defScrLineSize;
	 unsigned int  altScrLineSize;
	 unsigned int  defScrNumLines;
	 unsigned int  altScrNumLines;
	 unsigned char replyMode;
	 bool          alternateScreen;
	 bool          bufferInitialized;
	 bool          keyboardRestore;
	 bool          printBuffer;
	 bool          bufferUserModified;

	 /* Controls For Internal Errors Debugging Facility */
	 char*         lastStreamNVT;
	 int           lastStreamSize;

	 /*
	  * Internal Procedures
	  */
	 bool         	AutomaticSkip(int pos);
	 unsigned int 	NextFieldPos(const unsigned int fromPos, bool unProt=true,bool resetPos=false);
	 TColor 			SetTextColor(unsigned char fdAttr);
	 bool   			IsWordDelimiter(unsigned char Char);
	 int    			NextBufPos(int bufPos);
	 int    			PrevBufPos(int bufPos);
	 TColor 			GetColor(int colorNVT);
	 void   			SoundAlarm();
	 bool   			IsModifiedField(int cursorPos);
	 void   			SetMDTBit(int cursorPos);
	 void   			ResetMDTBits();
	 UINT          GetLineBegin(const uint bufPos);
	 void          IncCurrBufPos(const uint incPos, const int oper);
	 bool          SetExtendedAttr(Field& field);
	 bool   			IsUnprotField(const unsigned int pos);
	 UINT   			EndOfField(const unsigned int fromPos);
	 bool   			FindNulls(const int initPos, int& firstNull, int& lastNull,
									 const int count);
	 bool   			AddUndo(const int option, const int subOption=TOENDOFFIELD);
	 Field* 			GetUndoField();
	 bool   			RemoveUndoField();
	 void   			ResetUndo();

  private:
	 enum nState {nNVT_IDLE,nNVT_WRITE,nNVT_WRITESF,
					  nNVT_ERASEWRITE,nNVT_WRITINGBUFFER,
					  nNVT_ERASEWRITEALT,nNVT_ERASEALLUNPROT} nNVTState;

	 /* This is the terminal character buffer implemented as an array */
	 unsigned char* charBuffer;
	 unsigned char  currChar;

	 /*
	  * This the NVT UNDO Buffer (Fields that were modified)
	  */
	 Screen       undoBuffer;
	 uint         numUndo;

	 /* Controls to Implement Text Selection on Buffer */
	 uint 		  selStartPos;
	 uint         selEndPos;

};    //{{ALFFNvt}}

typedef ALFFNvt*	 LPALFFNvt;

#endif               // __ALFFNvt_h sentry.
