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

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

	 OVERVIEW:

    ALFF3270 class definition. It is used to manage the 3270 terminal screen
    buffer and 3270 printer buffer.

    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"         // ALFF3270 specific defines
#include "tec3270.rh"          // Tec3270 defines

//{{ALFF3270}}
class ALFF3270
{
  public:
	 ALFF3270(ALFFConnData* connData, bool printer=false);
	 virtual ~ALFF3270();
    char far* GetClassName() {return "ALFF3270";}

	 /*  3270 Process Public Function Members     */
	 void   InitBuf(const short defNumLines, const short defLineSize,
						 const short altNumLines, const short altLineSize,
						 const bool colorTerm, const bool extendedAttr);
	 void   ClearBuf();
	 void   ResetAll();
	 int    DataFromNetwork(LPALFFBUF pNetInBuf);
    unsigned char Get3270SenseCode(int retCode);
	 int    WriteBuffer(char** stream3270, int& streamSize);
	 int    DataToNetwork(LPALFFBUF, int mode);
    int    SendQueryReply(char** netBufPtr, int* netCount,
                          const int netOutMax, const int qCode);
	 Field* DataToScreen(int& fromPos, const int option,
								const int suboption=TOENDOFFIELD,
								const int code=ASCII,
                        bool noAttrb=false, bool eabXlat=true,
                        bool noDisplay=true);
    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();
	 TPoint EraseAllUnprot();
	 bool   IsTerminalOn();
	 int    Get3270BufSize();
	 TPoint GetCursorPos(UINT pos=0, bool convertToPoint=false);
    UINT   GetKeyCount() {return keyCount;}
	 TPoint GetNextUnprotField(const int  fromPos);
	 TPoint GetPrevWord(const int  fromPos);
	 TPoint GetNextWord(const int  fromPos);
  	 TPoint GetEndOfWord(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);
	 char*  GetTabEbcAsc();
	 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*  GetLastStream3270() {return lastStream3270;}
	 int    GetLastStreamSize() {return lastStreamSize;}
    unsigned char ConvertToAscii(unsigned char pos);

    /* Printer Operations Support Methods */
    bool   IsWCCLineFormatted() {return wccLineSize;}
    int    GetStreamLineSize()  {return thisStreamLineSize;}
    const char*  GetQueryReplyList()  {return queryReplyList;}

  protected:
	 ALFFConnData* connData;
    bool          isPrinter;
    bool				wccLineSize;
    int			   thisStreamLineSize;
	 /*
	  * << 3270 16 to 12 Bits Address Converter Tables >>
	  */
	 char* convTab1216;
	 char* convTab1612;

	 /*
	  * << EBCDIC to ASCII & Vice-Versa Conversion Tables >>
	  */
	 char* tabEBCtoASC;       /* CodePage 037 - 1140 (037 with Euro symbol) */
	 char* tabASCtoEBC;

	 /*
	  * 3270 Terminal Controls
	  */
	 Field         scrField;
	 unsigned char defaultHighLight;
	 unsigned char defaultCharSet;
	 unsigned char defaultCharColor;
	 unsigned char defaultBkgdColor;
	 unsigned char defaultTransparency;
	 unsigned char defaultValidation;
	 unsigned char defaultOutLining;
	 bool          colorTerminal;
    bool          extendedAttribute;
	 unsigned char lastAID;
	 unsigned int  cursorPos;
  	 unsigned int  keyCount;
	 unsigned int  currentBufPos;
	 unsigned int  lastCharUpdated;
	 unsigned int    size3270Buf;
	 unsigned short  scrLineSize;
	 unsigned short  defScrLineSize;
	 unsigned short  altScrLineSize;
	 unsigned short  defScrNumLines;
	 unsigned short  altScrNumLines;
    unsigned char   replyMode;
    unsigned char*  charModeAttrList;
    bool          alternateScreen;
	 bool          bufferInitialized;
	 bool          keyboardRestore;
	 bool          printBuffer;
    bool          bufferUserModified;

	 /* Query Reply List:
		 Byte 01 - Request Type B'00' = QCODE List
										B'01' = QCODE List + Equivalent
										B'10' = All
										B'11' = Reserved
		 Bytes 02-nn Query Reply Codes List (ended with 0xFFFF)
	 */
	 char*         queryReplyList;

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

	 /*
	  * Internal Procedures
	  */
	 unsigned char ConvertToEBCDIC(unsigned char pos);
	 bool         	AutomaticSkip(int  pos);
	 int           GetPrevAttrPos(const int  fromPos);
	 void          GetDefaultCharAttr(ScreenPos& pAttribute,
												 int  fdBegin=0,
												 unsigned char fdAttr=ATTRDEFAULT);
	 unsigned int	NextFieldPos(const unsigned int fromPos, bool unProt=true,bool resetPos=false);
	 COLORREF		SetTextColor(unsigned char fdAttr);
	 bool   			IsWordDelimiter(unsigned char Char);
	 int    			NextBufPos(int  bufPos);
	 int    			PrevBufPos(int  bufPos);
	 COLORREF   	GetColor(int  color3270);
	 void   			SoundAlarm();
	 bool   			IsModifiedField(int  cursorPos);
	 void   			SetMDTBit(int  cursorPos);
	 void   			ResetMDTBits();
	 void   			ProcessWCC(char wcc);
    bool          IsCharAttrEqual(const int  thisPos,const int  compPos);
	 bool          SetExtendedAttr(Field& field, ScreenPos& pos, const int  code);
    bool          SetIgnoreAttr(Field& field, int fieldBeginAt);
	 short  			ProcessAttr(ScreenPos* pAttribute, char* buffer, int  countPairs, int  order);
	 bool   			IsUnprotField(const unsigned int  pos, bool checkSF=false);
	 void   			Addr16To12Bits(const unsigned short* addr16Bits, char* addr12Bits);
	 unsigned short Addr12To16Bits(const char* addr12Bits);
	 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 {n3270_IDLE,n3270_WRITE,n3270_WRITESF,
					  n3270_ERASEWRITE,n3270_WRITINGBUFFER,
					  n3270_ERASEWRITEALT,n3270_ERASEALLUNPROT} n3270State;

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

	 /*
	  * This is the terminal control buffer implemented as an container
	  */
	 ScreenBuffer screenBuffer;
	 ScreenPos    screenPos;
	 ScreenPos    currCharAttr;
	 ScreenPos*   pScreenBuffer;

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

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

};    //{{ALFF3270}}

typedef ALFF3270*	 LPALFFTERM;

#endif               // __ALFF3270_h sentry.
