?? hzinput.c
字號:
/*** $Id: hzinput.c,v 1.61.10.1 2005/02/16 06:23:31 weiym Exp $**** hzinput.c: The GB2312 IME window.**** Orignally by KANG Xiaoning, some code from cce by He Rui.**** Copyright (C) 1998, 1999 He Rui (herui@cs.duke.edu)** Copyright (C) 1999 Kang Xiaoning.** Copyright (C) 2000 Zheng Xiang.** Copyright (C) 2000, 2001, 2002 Wei Yongming.** Copyright (C) 2003 Feynman Software.**** Current maintainer: Wei Yongming** Create date: 1999/04/01**** Used abbreviations:** ime: input method** punc: punctuate mark***/ /*** 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//*** Modify records:**** Who When Where For What**-----------------------------------------------------------------------------** WEI Yongming 1999/06/14 Tsinghua Message dispatching** GONG Xiaorui 1999/07/01 HongShiFang Soft keyboard** WEI Yongming 1999/07/17 Tsinghua Add some local functions** WEI Yongming 1999/08/24 Tsinghua Remove soft keyboard.** WEI Yongming 1999/08/29 Tsinghua Handle some special keys.** Wei Yongming 1999/09/01 Tsinghua Translat punc marks.** Wei Yongming 1999/09/01 Tsinghua Translat punc marks.** Wei Yongming 2000/11/06 BluePoint Merge pinyin module by Zheng Xiang.**** TODO:*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include "common.h" #include "minigui.h" #include "gdi.h" #include "window.h" #include "control.h" #include "endianrw.h" #include "misc.h"#include "hzinput.h"#ifdef _IME_GB2312_PINYIN#include "pinyin.h"#endif/************ private functions *******************/static BOOL hz_input_init(void);static void hz_input_done(void);static void unload_input_table(int i);static BOOL hz_filter(unsigned char key, LPARAM lParam);static int toggle_input_method(void);static void toggle_half_full(void);static void set_active_input_method(int);static void refresh_input_method_area(HWND hwnd, HDC hDC);static void DispSelection(HDC hDC);static void FindMatchKey(void);static void FillMatchChars(int j);static void FillAssociateChars(int index);static void FindAssociateKey(int index);static hz_input_table* IntCode_Init(void);static void IntCode_FindMatchKey(void);static void IntCode_FillMatchChars(int index);static void intcode_hz_filter(unsigned char key,LPARAM lParam);/*************************************************************************** * variable defines * ***************************************************************************/#define VERSION_STRING "Chinese Input"static BOOL bTwoLines;static char ime_tab_path [MAX_PATH + 1];static const unsigned char fullchar[] = " !”#$%&’()*+,-./0123456789:;<=>?" "@ABCDEFGHIJKLMNOPQRSTUVWXYZ〔\〕^□" "‘abcdefghijklmnopqrstuvwxyz{|}~ ";#define CPUNCTYPE_1TO1 0#define CPUNCTYPE_1TO2 1#define CPUNCTYPE_CYCLE 2#define CTRLBTN_WIDTH 16#define CTRLBTN_HEIGHT 16typedef struct PuncMark{ unsigned char epunc; int type; int currone; // only for cycle unsigned char* cpunc;}PUNCMARK;static PUNCMARK puncmark[] = { {'`', 0, 0, "·"}, {'~', 0, 0, "~"}, {'!', 0, 0, "!"}, {'@', 0, 0, "℃"}, {'#', 0, 0, "#"}, {'$', 0, 0, "¥"}, {'%', 0, 0, "%"}, {'^', CPUNCTYPE_1TO2, 0, "……"}, {'&', 0, 0, "§"}, {'*', 0, 0, "×"}, {'_', CPUNCTYPE_1TO2, 0, "——"}, {'(', CPUNCTYPE_CYCLE, 0, "()"}, {')', CPUNCTYPE_CYCLE, 0, "〔〕"}, {'{', CPUNCTYPE_CYCLE, 0, "《》"}, {'}', CPUNCTYPE_CYCLE, 0, "〈〉"}, {'[', CPUNCTYPE_CYCLE, 0, "〖〗"}, {']', CPUNCTYPE_CYCLE, 0, "【】"}, {'\\', 0, 0, "÷"}, {'|', 0, 0, "※"}, {';', 0, 0, ";"}, {':', 0, 0, ":"}, {'\'', CPUNCTYPE_CYCLE, 0, "‘’"}, {'\"', CPUNCTYPE_CYCLE, 0, "“”"}, {',', 0, 0, ","}, {'.', 0, 0, "。"}, {'<', CPUNCTYPE_CYCLE, 0, "「」"}, {'>', CPUNCTYPE_CYCLE, 0, "『』"}, {'?', 0, 0, "?"}, {'/', 0, 0, "、"}};static int IsOpened; /* 1 for opened, 0 for closed */static int IsHanziInput; /* 0 for ascii, 1 for hanzi input */static int IsFullChar; /* 0 for half char, 1 for full char */static int IsFullPunc; /* 0 for half punctuate marks, 1 for full marks */ static int CurIME;static int nIMENr; /* effetive IME method number */static HWND sg_hTargetWnd = HWND_DESKTOP; // target window of ime.static hz_input_table *input_table[NR_INPUTMETHOD],*cur_table = NULL;static char seltab[16][MAX_PHRASE_LENGTH];static int CurSelNum=0; /* Current Total Selection Number */static unsigned long InpKey[MAX_INPUT_LENGTH],save_InpKey[MAX_INPUT_LENGTH]; /* Input key buffer */static int InputCount,InputMatch, StartKey,EndKey;static int save_StartKey,save_EndKey, save_MultiPageMode, save_NextPageIndex, save_CurrentPageIndex;static int NextPageIndex,CurrentPageIndex,MultiPageMode;/* When enter MultiPageMode: StartKey .... CurrentPageIndex .... NextPageIndex .... EndKey */static unsigned long val1, val2,key1,key2;static int IsAssociateMode;static int CharIndex[15]; // starting index of matched char/phrasesstatic const BITMAP* sg_bmpIME; // the bmp of the IME control buttonstatic int UseAssociateMode = 0;/* 6 bit a key mask */static const unsigned long mask[]={ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x3F000000, 0x3FFC0000, 0x3FFFF000, 0x3FFFFFC0, 0x3FFFFFFF, 0x3FFFFFFF, 0x3FFFFFFF, 0x3FFFFFFF, 0x3FFFFFFF, 0x3FFFFFFF, 0x3FFFFFFF, 0x3FFFFFFF};/*************************************************************************** * some local functions * ***************************************************************************/static hz_input_table* load_input_method(char *filename){#if MGUI_BYTEORDER == MGUI_BIG_ENDIAN int i;#endif int nread; FILE *fd; char phrase_filename[100],assoc_filename[100]; hz_input_table *table; table = calloc (1, sizeof(hz_input_table)); if (table == NULL) return NULL; fd = fopen(filename, "r"); if (fd == NULL) { free (table); return NULL; } nread = fread(table, sizeof(hz_input_table),1,fd); if (nread != 1) { goto fail; }#if MGUI_BYTEORDER == MGUI_BIG_ENDIAN table->TotalKey = ArchSwap32(table->TotalKey); table->MaxPress = ArchSwap32(table->MaxPress); table->MaxDupSel= ArchSwap32(table->MaxDupSel); table->TotalChar= ArchSwap32(table->TotalChar); table->PhraseNum= ArchSwap32(table->PhraseNum); for (i = 0; i < 64; i++) { table->KeyIndex [i] = ArchSwap16(table->KeyIndex[i]); }#endif if( strcmp(MAGIC_NUMBER, table->magic_number) ) { goto fail; } table->item = (ITEM *)malloc (sizeof(ITEM) * table->TotalChar); if ( table->item == NULL ) { goto fail; } if (fread (table->item, sizeof(ITEM), table->TotalChar, fd) < table->TotalChar) goto fail;#if MGUI_BYTEORDER == MGUI_BIG_ENDIAN for (i = 0; i < table->TotalChar; i++) { table->item[i].key1 = ArchSwap32(table->item[i].key1); table->item[i].key2 = ArchSwap32(table->item[i].key2); table->item[i].frequency = ArchSwap16(table->item[i].frequency); }#endif fclose (fd); if (table->PhraseNum > 0) { strcpy( phrase_filename, filename ); strcat( phrase_filename, ".phr" ); strcpy( assoc_filename, filename ); strcat( assoc_filename, ".lx"); table->PhraseFile = fopen( phrase_filename, "r" ); table->AssocFile = fopen( assoc_filename, "r"); if (table->PhraseFile == NULL || table->AssocFile == NULL) { printf("Load Phrase/Assoc File error!\n"); free(table->item); free(table); return NULL; } } else { table->PhraseFile = table->AssocFile = NULL; } return table;fail: free (table->item); free (table); fclose (fd); return NULL;}static void free_input_method(hz_input_table *table){ free(table->item); free(table);}static void ClrIn(void){ bzero( InpKey, sizeof( InpKey ) ); bzero( seltab, sizeof( seltab ) ); MultiPageMode = NextPageIndex = CurrentPageIndex = 0; CurSelNum = InputCount = InputMatch = 0; IsAssociateMode = 0; /* lian xiang */}static void FindAssociateKey(int index){ FILE *fp = cur_table->AssocFile; int ofs[2],offset; if (index < 0xB0A1) { StartKey = EndKey = 0; return; /* no match */ } offset = (index / 256 - 0xB0) * 94 + index % 256 - 0xA1; fseek(fp, offset * sizeof(int), SEEK_SET); fread(ofs,sizeof(int),2,fp);#if MGUI_BYTEORDER == MGUI_BIG_ENDIAN ofs[0] = ArchSwap32(ofs[0]); ofs[1] = ArchSwap32(ofs[1]);#endif StartKey = 72 * 94 + 1 + ofs[0]; EndKey = 72 * 94 + 1 + ofs[1];}static void load_phrase( int phrno, char *tt ){ FILE *fp = cur_table->PhraseFile; int ofs[2], len; fseek( fp, ( phrno + 1 ) << 2, SEEK_SET ); fread( ofs, sizeof(int), 2, fp );#if MGUI_BYTEORDER == MGUI_BIG_ENDIAN ofs[0] = ArchSwap32(ofs[0]); ofs[1] = ArchSwap32(ofs[1]);#endif len = ofs[1] - ofs[0]; if ( len > 128 || len <= 0 ) { strcpy( tt, "error" ); return; } ofs[0] += ( cur_table->PhraseNum + 1 ) << 2; /* Add the index area length */ fseek( fp, ofs[0], SEEK_SET ); fread( tt, 1, len, fp ); tt[len] = 0;}static void putstr (unsigned char *p ,LPARAM lParam){ int index,len = strlen(p); __mg_ime_writemsg(p, len, lParam, TRUE); if (InputCount <= InputMatch) /* All Match */ { index = (int)p[len-2] * 256 + p[len-1]; ClrIn(); if (UseAssociateMode) { FindAssociateKey(index); CurrentPageIndex = StartKey; MultiPageMode = 0; FillAssociateChars(StartKey); if (CurSelNum > 0) { IsAssociateMode = 1; } } } else { int nCount = InputCount - InputMatch,nMatch = InputMatch,i; MultiPageMode = NextPageIndex = CurrentPageIndex = 0; InputCount = InputMatch = 0; for(i = 0; i < nCount; i++) save_InpKey[i] = InpKey[nMatch+i]; bzero(InpKey, sizeof(InpKey)); for(i = 1; i <= nCount; i++) /* feed the additional keys */ { InpKey[InputCount] = save_InpKey[InputCount]; InputCount++; if (InputCount <= InputMatch+1) { FindMatchKey(); MultiPageMode = 0; CurrentPageIndex = StartKey; FillMatchChars(StartKey); } } if (InputMatch == 0) /* left key has no match, delete */ { ClrIn(); return; } }}static int epunc2cpunc (unsigned char epunc, unsigned char* cpunc){ int i; for (i = 0; i < 29; i++) { if (puncmark [i].epunc == epunc) { switch (puncmark [i].type) { case CPUNCTYPE_1TO1: memcpy (cpunc, puncmark [i].cpunc, 2); return 2; case CPUNCTYPE_1TO2: memcpy (cpunc, puncmark [i].cpunc, 4); return 4; case CPUNCTYPE_CYCLE: memcpy (cpunc, puncmark[i].cpunc + puncmark[i].currone, 2); if (puncmark[i].currone == 0) puncmark[i].currone = 2; else puncmark[i].currone = 0; return 2; default: return 0; } } } return 0;}/* After add/delete a char, search the matched char/phrase, update the StartKey/EndKey key, save the related keys at first, if no match is found, we may restore its original value*/static void FindMatchKey(void){ save_StartKey = StartKey; save_EndKey = EndKey; save_MultiPageMode = MultiPageMode; save_NextPageIndex = NextPageIndex; save_CurrentPageIndex = CurrentPageIndex; val1 = InpKey[4] | (InpKey[3]<<6) | (InpKey[2]<<12) | (InpKey[1]<<18) | (InpKey[0]<<24); val2 = InpKey[9] | (InpKey[8]<<6) | (InpKey[7]<<12) | (InpKey[6]<<18) | (InpKey[5]<<24);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -