/*
 *
 * UNICON - The Console Chinese & I18N
 * Copyright (c) 1999-2002
 *
 * This file is part of UNICON, a console Chinese & I18N
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * See the file COPYING directory of this archive
 * Author: see CREDITS
 */


//
// hzinput.h -- Definitions used in Input Methods
//

#ifndef HZINPUT_H
#define HZINPUT_H
#include <xl_sysphrase.h>

// Why?
#define MAX_INPUT_LENGTH        17
#define MAGIC_NUMBER            "CCEGB"
#define CIN_ENAME_LENGTH        24
#define CIN_CNAME_LENGTH        16
#define MAX_PHRASE_LENGTH       20 
#define SELECT_KEY_LENGTH       16 
#define END_KEY_LENGTH          16

#define InputAreaX              8
// 0-7 input method name, 8 input chars
#define IntCode_SelectionX      13
#define SelectionX             (InputAreaX + MAX_INPUT_LENGTH + 1) 
// 26
// 8-23  16 max input chars

/* #define SelectionXMax           78  */
extern int SelectionXMax; 

/* key of toggle input method */

#define NR_INPUTMETHOD 9
 /* 0-9, 0 PinYin, 9 IntCode */

/* define type of ITEM */
#define   IMM_DOUBLECHAR           0
#define   IMM_PHRASE               1
#define   IMM_DEL_PHRASE           0xff

typedef struct 
{
	// key1, key2 : Char Code to input the char.
	// Each char can have code less than or equal to 10 (hard coded)
	// The first five, is bitwise shifted and stored in key1.
	// And the rest for key2.
    u_long	key1;
    u_long	key2;

	// Index of the char. in the array of SysPhrase (char in tl_sysphrase.bin)
	// This limit the len of the array of SysPhrase.pPhrase to 2^32
    u_long	nPhrase;

    u_char	type; 
}
ITEM;

typedef struct
{
    u_short	total;
	// An array of pointers, which are pointing to elements of ITEM array
	// In other words, each is pointing to a ITEM element.
    ITEM	**pPhrase;
}
AssociatePhrase;

typedef struct
{
	// MIME Type ID for .tab file
	char	magic_number[sizeof(MAGIC_NUMBER)];
	
	// Name in English, and Chinese
	char	ename[CIN_ENAME_LENGTH];
	char	cname[CIN_CNAME_LENGTH];

	// Key (char) usued in selection; In Ascii; From .cin
	char	selkey[SELECT_KEY_LENGTH];

	// Flag: <Space> to complete input? Default: 1; From .cin
	// It should always 1 in TL_hzinput.so
	char	last_full;

	// Binary digit: Max. No. of selection in each time; From .cin
	int		MaxDupSel;

	// Binary digit: Max. No. of key used in input; From .cin
	int		TotalKey;
	
	// Binary digit: Max. No. of input code in the input method; From .cin
	// Max limit is 10; Hard coded in cin2tab
	int		MaxPress;

	// Total of character read from .cin
	int		TotalChar;

	// Each cell represent a value in Ascii. The value of it is the index in 
	// KeyName. Hence possible value for every cell is 0~63
	u_char	KeyMap[128];

	// Symbolic Name of each input key. From .cin
	u_char	KeyName[64][3];

	// Index for searching chars
	u_short	KeyIndex[64];

	// Total of char defined.
	int		TotalPhrase;
	// Array of ITEM structure. Each store the information for one char.
	ITEM	*item;

	// Total of AssociatePhrase defined.
	int		TotalAssociatePhrase;
	// Array of AssociatePhrase, size = (2^16-1), i.e. 65535
	// For a phrase starting with 0x80 0x45, the array pAssociatePhrase[8045]
	// will be the record for this chars.
	AssociatePhrase *pAssociatePhrase;
}
hz_input_table;

typedef struct
{
	int		IsHanziInput;
	int		IsFullChar;
	int		IsFullComma;
	int		IsSysMenu;
	int		IsHelpMenu;
	int		current_method;

	hz_input_table *cur_table;

	char	seltab [16] [MAX_PHRASE_LENGTH];
	long	seltab_phrase [16];

	int		CurSelNum;
	u_long	InpKey[MAX_INPUT_LENGTH];
	u_long	save_InpKey[MAX_INPUT_LENGTH];

	int		InputCount;
	int		InputMatch;
	int		StartKey;
	int		EndKey;

	int		save_StartKey;
	int		save_EndKey;
	int		save_MultiPageMode;
	int		save_NextPageIndex;
	int		save_CurrentPageIndex;

	int		NextPageIndex;
	int		CurrentPageIndex;
	int		MultiPageMode;

	u_long	val1, val2;
	u_long	key1, key2;
	int		IsAssociateMode;
	int		CharIndex[15];
	int		UseAssociateMode;
	int		IsFullCommaInChinese;
	int		IsFullCommaInEnglish;
	int		IsFullCharBackup; 
	int		IsHanziInputBackup;
	// The function for these two var is unknown
	// As long as there is no reference for these two var in the TL modules.
	int		MaxSelectLen;
	int		MAX_SEL_LENGTH;

	/* Output Sorting Support */
	u_char	bIsFinishSorting;
	long	TotalOutputPhrase;
	long	OutputPhrase[32000];

	/* System Flush Support */
	char	szTabFileName[256];
	char	szSysPhraseFile[256];

	TL_SysPhrase_T *pSysPhrase;
}
HzInputTable_T;

extern void TL_InputInit (HzInputTable_T *p);
extern hz_input_table* TL_LoadMethod (char *szName);
extern void TL_UnloadMethod (hz_input_table *p);
extern int TL_KeyFilter (HzInputTable_T *pClient, u_char key, char *buf, int *len);
extern char* TL_GetSelItem (HzInputTable_T *pClient, int idx);
extern int TL_ConfigureInputArea (HzInputTable_T *p, int MaxSelectLen);
extern int TL_GetInputDisplay (HzInputTable_T *p, char *buf, long buflen);
extern int TL_GetSelectDisplay (HzInputTable_T *p, char *buffer);


#endif
