#if !defined(__alffkeyb_h) // Sentry, use file only if it's not already included.
#define __alffkeyb_h
/*  Project Biblio-Tec

	 SUBSYSTEM:    TEC30r.dll Library (TecLib)
	 FILE:         alffkeyb.h
	 AUTHOR:       Alcindo Ferreira Filho

	 OVERVIEW:
	 Key, ALFFKeySelect, ALFFKeyGetTxt, ALFFKeyDialog and ALFFKeyboard
    Classes definition. They define an complete and self configurable
    Keyboard using English and Brazilian Keyboars layout. Every
    keystroke should be decoded by this class as they represents the
    entire keyboard and respective keys configuration.

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

    LICENSE:      Biblio-Tec license (BiblTec.txt)
*/
#include <owl\owlall.h>
#include "alffdlg.h"
#include "alfffile.h"
#include "alffhelp.h"
#include "teclib.rh"


/////////////////////////////////////////////////////////////
//
//  Key : A class that describes an keyboard key mapping.
//        ( A Keyboard is mapped in an container of Keys)
//
class _TECLIBCLASS Key : public ALFFObject
{
 public:
  Key();
  Key(const Key& kCopy);
  ~Key();
  char far* GetClassName() {return "ALFFKey";}
  Key& operator = (const Key& equals);
  int operator == (const Key& kTest) const;
  unsigned char  nKeyFlags;
  unsigned short nKeyIndex;
  char*          nKeyStr;
  unsigned char  sKeyFlags;
  unsigned short sKeyIndex;
  char*          sKeyStr;
  unsigned char  cKeyFlags;
  unsigned short cKeyIndex;
  char*          cKeyStr;
  unsigned char  aKeyFlags;
  unsigned short aKeyIndex;
  char*          aKeyStr;
  unsigned char  agKeyFlags;
  unsigned short agKeyIndex;
  char*          agKeyStr;
};

/*
 * Define a Type for Keyboard Mapping
 */
typedef TVectorImp<Key> Keyboard;

/*
 * Define a Type for Keyboard Key Iterator
 */
typedef TVectorIteratorImp<Key> KeyIterator;


/*
 * Struct for Keyboard Defaults Loading
 */
struct _TECLIBCLASS KeybDefaults
 {
  unsigned short keyIdentifier;
  unsigned short nkeyType;
  unsigned short nkeyValue;
  unsigned short skeyType;
  unsigned short skeyValue;
  unsigned short ckeyType;
  unsigned short ckeyValue;
  unsigned short akeyType;
  unsigned short akeyValue;
  unsigned short agkeyType;
  unsigned short agkeyValue;
 };

/*
 * Dialog for selecting key value (char or command)
 *
 * Should be executed using "ExecuteSel()" function that returns
 * the value of the ASCII Char selected or the "int" identifier
 * of the Command selected, depending upon the value of "listType"
 * that may be "CHARKEY" or "COMMKEY". Any other value received
 * will return INVALIDLISTTYPE on Execute() function call.
 *
 */
//{{ALFFKeySelect}}
class _TECLIBCLASS ALFFKeySelect : public ALFFDialog
{
 public:
  ALFFKeySelect(TWindow* Parent, const UINT listType, const int kbId=0);
  virtual ~ALFFKeySelect();
  char far* GetClassName() {return "ALFFKeySelect";}

  UINT ExecuteSel();

 protected:
  void SetupWindow();
  void CmOk();
  void CmHelp();
  void EvPaint();
  void NotifyList(UINT code);
  TListBox* keyList;
  UINT keyListType;
  UINT selValue;
  int  keybId;
  ALFFHelp* helpFile;
  TWindow*  selParent;
  
 DECLARE_RESPONSE_TABLE(ALFFKeySelect);
};

/*
 * Dialog for getting text for associating to a key
 *
 * Should be executed using "ExecuteGet()" function that returns
 * the pointer (char*) to text string informed.
 * If no text string was informed, or Cancel Button was clicked,
 * it will return "NULL".
 *
 */
class _TECLIBCLASS ALFFKeyGetTxt : public ALFFDialog
{
 public:
  ALFFKeyGetTxt(TWindow* Parent);
  virtual ~ALFFKeyGetTxt();
  char far* GetClassName() {return "ALFFKeyGetTxt";}

  char* ExecuteGet();

 protected:
  void SetupWindow();
  void CmOk();
  void CmHelp();
  void CmCancel();
  TEdit* keyText;
  char*  textGet;
  ALFFHelp* helpFile;
  TWindow* txtParent;

 DECLARE_RESPONSE_TABLE(ALFFKeyGetTxt);
};


//{{ALFFKeyDialog}}
class _TECLIBCLASS ALFFKeyDialog : public ALFFDialog
{
 public:
  ALFFKeyDialog(TWindow* Parent, char* title, Keyboard* pKeyb,
					 int keyboardId = IDD_KEYBBR,
					 int keybDefId = IDT_KEYBOARDBRPC);
  virtual ~ALFFKeyDialog();
  char far* GetClassName() {return "ALFFKeyDialog";}

 protected:
  void SetupWindow();
  void CmHelp();
  void KDLoadString(const int id, char* destString, const int destLen);
  int  KDError(const int errorId, bool cancel=false);
  bool ResetKeyboard();
  void LoadKeyText(const int keyId);
  bool ModifyKey(const int subKey);
  void ChangeKey(const int subKey);
  void SetSubKey(const int subKey, const unsigned char keyFlag, const ushort selection);
  void SetSubKeyChar(const int subKey, const unsigned char keyFlag,const char* selection);
  void FormatKeyText(char* textDest, const char* textSrc, const int destLen);
  void EvPaint();
  LRESULT EvCommand(uint id, HWND hWndCtl, uint notifyCode);

  TOpenSaveDialog::TData fileData;
  TWindow*       kdParent;
  TStatic*       keyName;

  TGroupBox*     changeGroup;
  TRadioButton*  chgNormal;
  TRadioButton*  chgShift;
  TRadioButton*  chgCtrl;
  TRadioButton*  chgAlt;
  TRadioButton*  chgAGr;
  TStatic*       nkeyText;
  TStatic*       skeyText;
  TStatic*       ckeyText;
  TStatic*       akeyText;
  TStatic*       agkeyText;
  int            currentKey;

  TGroupBox*     selGroup;
  TRadioButton*  selChar;
  TRadioButton*  selComm;
  TRadioButton*  selText;
  TRadioButton*  selScript;
  int            currentSel;

  Keyboard*      pKeyboard;
  int            keybDefaultId;
  char*			  dlgTitle;
  ALFFHelp*      helpFile;

 DECLARE_RESPONSE_TABLE(ALFFKeyDialog);
};

class _TECLIBCLASS ALFFKeyboard : public ALFFObject
{
	public:
	 ALFFKeyboard(TWindow* Parent, const int keyboardId,

	              bool remap = false);

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

	 int  RemapKeyboard();
	 bool LoadKeybFile();
	 bool IsKeyboardAvail();
	 int  GetKeyboardId();
	 uint GetCommand(UINT vkey, bool shift, bool ctrl,
						  bool alt, bool altgr);
	 char* GetKeyText(UINT vkey,  bool shift,
							bool ctrl, bool alt, bool altgr);

  protected:
	 void      KBLoadString(const int id, char* destString,
									const int destLen);
	 bool      KeyboardFileExist();
	 void 	  SaveKeyboard();
	 int       KBError(const int errorId, bool cancel=false);
	 uint      DecodeKey(int flags, unsigned short keyIndex);

	 Keyboard  keyBoard;
	 Key       key;
	 int       keybId;
    int       keybDlg;
    int       keybDefaultId;
	 ALFFKeyDialog*   pRemapDialog;
	 char*     kBoardFileName;
    char*     keyboardTitle;
	 ALFFFile* fileKeyb;
	 TWindow*  keyParent;

    ALFFHelp* helpFile;

};

//{{ALFFKeyboard}}
#endif
