Back to index

im-sdk  12.3.91
cangjie_filter.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "ctim.h"
00003 
00004 #define LOOKUP_CHOICE_NUM   6
00005 
00006 #define IME_Status          ime_buffer->return_status
00007 #define Input_Buf           ime_buffer->input_buf
00008 #define Input_Len           ime_buffer->input_len
00009 #define Preedit_Buf         ime_buffer->preedit_buf
00010 #define Preedit_Len         ime_buffer->preedit_len
00011 #define Preedit_CaretPos    ime_buffer->preedit_caretpos
00012 #define Commit_Buf          ime_buffer->commit_buf
00013 #define Commit_Len          ime_buffer->commit_len
00014 #define Status_Buf          ime_buffer->status_buf
00015 #define Status_Len          ime_buffer->status_len
00016 #define Lookups_Num         ime_buffer->num_candidates
00017 #define Lookup_Pos          ime_buffer->cur_lookup_pos
00018 #define Candidates          ime_buffer->candidates
00019 #define Comments            ime_buffer->comments
00020 #define Lookups                    ime_buffer->lookups
00021 #define Page_State          ime_buffer->page_state
00022 
00023 int Is_UsedCodes_Key(CodeTableStruct *hztbl, int key)
00024 {
00025        if (index(hztbl->UsedCodes, key))
00026               return(1);
00027        else
00028               return(0);
00029 }
00030 
00031 int get_select_num(CodeTableStruct *hztbl, int key)
00032 {
00033        int ret = -1;
00034        switch(hztbl->nSelectKeyMode) {
00035               case NUMBER0_MODE:
00036                      if (key >= '0' && key <= '9')
00037                             ret = (key - '0') % 10;
00038                      break;
00039 
00040               case NUMBER_MODE:
00041                      if (key >= '0' && key <= '9')
00042                             ret = (key - '0' + 9) % 10;
00043                      break;
00044 
00045               case LOWER_MODE:
00046                      if (key >= 'a' && key <= 'a' + 10)
00047                             ret = key - 'a';
00048                      break;
00049 
00050               case UPPER_MODE:
00051                      if (key >= 'A' && key <= 'A' + 10)
00052                             ret = key - 'A';
00053                      break;
00054        }
00055        return(ret);
00056 }
00057 
00058 int Is_Select_Key(CodeTableStruct *hztbl, int key)
00059 {
00060        int ret = get_select_num(hztbl, key);
00061        if (ret == -1)
00062               return(0);
00063        else
00064               return(1);
00065 }
00066 
00067 int Is_Space_Key(CodeTableStruct *hztbl, int key)
00068 {
00069        if (key == IME_FILTERED_KEY_SPACEBAR)
00070               return(1);
00071        else
00072               return(0);
00073 }
00074 
00075 int Is_BackSpace_Key(CodeTableStruct *hztbl, int key)
00076 {
00077        int i;
00078        unsigned char *keylist = hztbl->functionkey[BACKSPACE_KEY_ID].keylist;
00079 
00080        if (key == IME_FILTERED_KEY_BACKSPACE || key == IME_FILTERED_KEY_DELETE)
00081               return(1);
00082 
00083        if (keylist[0]) {
00084               for (i=0; i<MAX_FUNCTIONKEY_LEN; i++)
00085                      if (keylist[i] == key)
00086                             return(1);
00087        }
00088 
00089        return(0);
00090 }
00091 
00092 int Is_ClearAll_Key(CodeTableStruct *hztbl, int key)
00093 {
00094        int i;
00095        unsigned char *keylist = hztbl->functionkey[CLEARALL_KEY_ID].keylist;
00096 
00097        if (key == IME_FILTERED_KEY_ESCAPE)
00098               return(1);
00099 
00100        if (keylist[0]) {
00101               for (i=0; i<MAX_FUNCTIONKEY_LEN; i++)
00102                      if (keylist[i] == key)
00103                             return(1);
00104        }
00105        return(0);
00106 }
00107 
00108 int Is_NextPage_Key(CodeTableStruct *hztbl, int key)
00109 {
00110        int i;
00111        unsigned char *keylist = hztbl->functionkey[PAGEDOWN_KEY_ID].keylist;
00112 
00113        if (key == IME_FILTERED_KEY_PAGEDOWN)
00114               return(1);
00115 
00116        if (keylist[0]) {
00117               for (i=0; i<MAX_FUNCTIONKEY_LEN; i++)
00118                      if (keylist[i] == key)
00119                             return(1);
00120        }
00121        return(0);
00122 }
00123 
00124 int Is_PrevPage_Key(CodeTableStruct *hztbl, int key)
00125 {
00126        int i;
00127        unsigned char *keylist = hztbl->functionkey[PAGEUP_KEY_ID].keylist;
00128 
00129        if (key == IME_FILTERED_KEY_PAGEUP)
00130               return(1);
00131        
00132        if (keylist[0]) {
00133               for (i=0; i<MAX_FUNCTIONKEY_LEN; i++)
00134                      if (keylist[i] == key)
00135                             return(1);
00136        }
00137        return(0);
00138 }
00139 
00140 int get_lookup_result(CodeTableStruct *hztbl, ImeBufferRec *ime_buffer, 
00141 int nHelpInfoMode, int pos, int max_lookup_num)
00142 {
00143        int i, lookup_num;
00144 
00145        lookup_num = codetable_search(hztbl, Input_Buf, Candidates, Comments, pos, max_lookup_num + 1); 
00146        if (lookup_num > 0) {
00147               Page_State = 0;
00148               if (pos == 0)
00149                      Page_State |= ImeCandidatesFirstPage;
00150               if (lookup_num < max_lookup_num + 1)
00151                      Page_State |= ImeCandidatesLastPage;
00152               if (lookup_num == max_lookup_num + 1)
00153                      lookup_num = max_lookup_num;
00154 
00155               for (i=0; i<lookup_num; i++) {
00156                      DEBUG_printf("%d: %s %s\n", i, Candidates[i], Comments[i]);
00157                      if (nHelpInfoMode)
00158                             snprintf((char *)Lookups[i], MAX_CANDIDATE_CHAR_NUM, "%s %s",
00159                                     Candidates[i], Comments[i]);
00160                      else
00161                             strcpy(Lookups[i], Candidates[i]);
00162               }
00163        }
00164 
00165        return(lookup_num);
00166 
00167 }
00168 
00169 int Commit_And_Clean_All_Area(ImeBufferRec *ime_buffer)
00170 {
00171        Input_Len = 0;
00172        Preedit_Len = 0;
00173        Lookups_Num = 0;
00174        Preedit_CaretPos = 0;
00175        IME_Status = IME_COMMIT | IME_PREEDIT_AREA | IME_LOOKUP_AREA;
00176 }
00177 
00178 int Clean_All_Area(ImeBufferRec *ime_buffer)
00179 {
00180        Input_Len = 0;
00181        Preedit_Len = 0;
00182        Lookups_Num = 0;
00183        Preedit_CaretPos = 0;
00184        IME_Status = IME_PREEDIT_AREA | IME_LOOKUP_AREA;
00185 }
00186 
00187 int commit_candidate(ImeBufferRec *ime_buffer, int id)
00188 {
00189        if (id >= Lookups_Num)
00190               return(-1);
00191 
00192        strcpy((char *)Commit_Buf, (char *)Candidates[id]);
00193        Commit_Len = strlen((char *)Commit_Buf);
00194        Commit_And_Clean_All_Area(ime_buffer);
00195 
00196        return(0);
00197 }
00198 
00199 void warning_bell(ImeBufferRec *ime_buffer)
00200 {
00201        IME_Status |= IME_BEEP;
00202 }
00203 
00204 /* return value:  IME_UNUSED_KEY:  if IME not use this key, return this key to systerm directly */
00205 /*                IME_OK:      if IME has used this key */
00206 int ctim_filter(CodeTableStruct *hztbl, unsigned char key, ImeBufferRec *ime_buffer)
00207 {
00208        char    ch;
00209        int     i, k, hznum;
00210        int     pos, select_num;
00211 
00212        int    Max_Lookups_Num = LOOKUP_CHOICE_NUM;
00213        int    nKeyByKeyMode = hztbl->nKeyByKeyMode;
00214        int    nHelpInfoMode = hztbl->nHelpInfoMode;
00215        int    nAutoSelectMode = hztbl->nAutoSelectMode;
00216        int    nKeyPromptMode = hztbl->nKeyPromptMode;
00217 
00218        ime_buffer->encoding = hztbl->Encode;
00219 
00220        IME_Status = 0;
00221 
00222 /*
00223        switch(hztbl->nSelectKeyMode) {
00224               case NUMBER0_MODE:
00225                      ime_buffer->lookup_label_type = NUMERIC0_LABEL;
00226                      break;
00227               case NUMBER_MODE:
00228                      ime_buffer->lookup_label_type = NUMERIC_LABEL;
00229                      break;
00230               case LOWER_MODE:
00231                      ime_buffer->lookup_label_type = LOWER_LABEL;
00232                      break;
00233               case UPPER_MODE:
00234                      ime_buffer->lookup_label_type = UPPER_LABEL;
00235                      break;
00236               default:
00237                      ime_buffer->lookup_label_type = NUMERIC_LABEL;
00238        }
00239 */
00240        if(Is_Select_Key(hztbl, key)) {
00241               if (Lookups_Num > 0) {
00242                      select_num = get_select_num(hztbl, key);
00243                      if (select_num >= 0 && select_num < Lookups_Num) {
00244                             commit_candidate(ime_buffer, select_num);
00245                      } else {
00246                             warning_bell(ime_buffer);
00247                      }
00248                      return(IME_OK);
00249               }
00250        }
00251 
00252        if((Is_UsedCodes_Key(hztbl, key)) || (Is_Wildchar_Key(hztbl, key))) { 
00253               /* if inputted keys length has over the limit */
00254               if (Input_Len >= hztbl->MaxCodes) {
00255                      return(IME_OK);
00256               }
00257 
00258               if (Lookups_Num > 0 && !nKeyByKeyMode) {
00259                      strcpy((char *)Commit_Buf, (char *)Candidates[0]);
00260                         Commit_Len = strlen((char *)Commit_Buf);
00261                         Commit_And_Clean_All_Area(ime_buffer);
00262               }
00263 
00264               Input_Buf[Input_Len] = key;
00265               Input_Len ++;
00266               Input_Buf[Input_Len] = '\0';
00267               
00268               if (nKeyPromptMode) {
00269                      strcpy(Preedit_Buf+Preedit_Len, hztbl->keyprompt[key].prompt);
00270                      Preedit_Len += strlen(hztbl->keyprompt[key].prompt);
00271                      Preedit_Buf[Preedit_Len] = '\0';
00272               } else {
00273                      Preedit_Buf[Preedit_Len] = key;
00274                      Preedit_Len += 1;
00275                      Preedit_Buf[Preedit_Len] = '\0';
00276               }
00277               Preedit_CaretPos = Preedit_Len;
00278               IME_Status |= IME_PREEDIT_AREA;
00279 
00280               Lookups_Num = 0;
00281               if (nKeyByKeyMode /* || Input_Len == hztbl->MaxCodes */) {
00282                      Lookup_Pos = 0;
00283                      Lookups_Num = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, Lookup_Pos, Max_Lookups_Num); 
00284                      /* if Input_Len is MaxCodes, no matter the KeyByKey mode, need do checking */
00285                      if (Input_Len == hztbl->MaxCodes) {
00286                             if (Lookups_Num == 1 && nAutoSelectMode) {
00287                                    commit_candidate(ime_buffer, 0);
00288                                    return(IME_OK);
00289                             }
00290                      }
00291               }
00292               IME_Status |= IME_LOOKUP_AREA;
00293 
00294               return(IME_OK);
00295        }
00296 
00297        if(Is_ClearAll_Key(hztbl, key)) { 
00298               /* Esc , clear preedit area, lookup choice area */
00299 
00300               /*  if no inputted key, directly return this key */
00301               if (Input_Len==0) return(IME_UNUSED_KEY);
00302 
00303               Clean_All_Area(ime_buffer);
00304 
00305               return (IME_OK);
00306        }
00307 
00308        if (Is_BackSpace_Key(hztbl, key)) { /* Back Space & Delete */
00309               /*  if no inputted key, directly return this key */
00310               if (Input_Len==0) return(IME_UNUSED_KEY);
00311 
00312               /* Delete characters in pre-edit region */
00313               Input_Len --;
00314               if (nKeyPromptMode) {
00315                      ch = Input_Buf[Input_Len];
00316                      Preedit_Len -= strlen(hztbl->keyprompt[ch].prompt);
00317                      Preedit_Buf[Preedit_Len] = '\0';
00318               } else {
00319                      Preedit_Len -= 1;
00320                      Preedit_Buf[Preedit_Len] = '\0';
00321               }
00322 
00323               Input_Buf[Input_Len] = '\0';
00324               Preedit_CaretPos = Preedit_Len;
00325               IME_Status = IME_PREEDIT_AREA;
00326 
00327               Lookups_Num = 0;
00328               if ((Input_Len > 0) && nKeyByKeyMode) {
00329                      Lookup_Pos = 0;
00330                      Lookups_Num = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, Lookup_Pos, Max_Lookups_Num); 
00331               }
00332               IME_Status |= IME_LOOKUP_AREA;
00333 
00334               return(IME_OK);
00335        }
00336 
00337        if(Is_NextPage_Key(hztbl, key)) {
00338               /*  if no inputted key, directly return this key */
00339               if (Input_Len==0) return(IME_UNUSED_KEY);
00340 
00341               /* if any preedit key and not in Lookup status */
00342               if (Lookups_Num==0) return(IME_OK);
00343 
00344               pos = Lookup_Pos + Max_Lookups_Num;
00345               hznum = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, pos, Max_Lookups_Num); 
00346               if (hznum > 0) {
00347                      Lookups_Num = hznum;
00348                      Lookup_Pos = pos;
00349                      IME_Status = IME_LOOKUP_AREA;
00350               } else {
00351                             warning_bell(ime_buffer);
00352               }
00353 
00354               return(IME_OK);
00355        }
00356 
00357        if(Is_PrevPage_Key(hztbl, key)) {
00358               /*  if no inputted key, directly return this key */
00359               if (Input_Len==0) return(IME_UNUSED_KEY);
00360 
00361               /* if any preedit key and not in Lookup status */
00362               if (Lookups_Num==0) return(IME_OK);
00363 
00364               /* if in beginning pos */
00365               if (Lookup_Pos<=0) {
00366                             warning_bell(ime_buffer);
00367                      return(IME_OK);
00368               }
00369 
00370               pos = Lookup_Pos - Max_Lookups_Num;
00371               hznum = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, pos, Max_Lookups_Num); 
00372               if (hznum > 0) {
00373                      Lookups_Num = hznum;
00374                      Lookup_Pos = pos;
00375                      IME_Status = IME_LOOKUP_AREA;
00376               } else {
00377                             warning_bell(ime_buffer);
00378               }
00379 
00380               return(IME_OK);
00381        }
00382 
00383        if(Is_Space_Key(hztbl, key)) {
00384               /* if no input keys */
00385               if (Input_Len==0) 
00386                      return(IME_UNUSED_KEY);
00387 
00388               if (Lookups_Num==0) {
00389                      /* Search from beginning */
00390                      Lookup_Pos = 0;
00391                      Lookups_Num = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, Lookup_Pos, Max_Lookups_Num);
00392                      if (Lookups_Num == 1 && nAutoSelectMode) {
00393                             commit_candidate(ime_buffer, 0);
00394                      } else if (Lookups_Num == 0) {
00395                             warning_bell(ime_buffer);
00396                      }
00397                      IME_Status |= IME_LOOKUP_AREA;
00398                      return(IME_OK);
00399               }
00400 
00401               /* Space key as scroll key */
00402               /* if Have several candidates, need search the next page */
00403               pos = Lookup_Pos + Max_Lookups_Num;
00404               hznum = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, pos, Max_Lookups_Num); 
00405               if (hznum == 0) {
00406                      /* if no candidates for the next page */
00407                      if (Lookup_Pos = 0) {
00408                             /* if is the first page, need not do anything */
00409                             return(IME_OK);
00410                      } else {
00411                             /* if not the first page, need scroll to the first page */
00412                             pos = 0;
00413                             hznum = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, pos, Max_Lookups_Num); 
00414                      }
00415               }
00416 
00417               Lookup_Pos = pos;
00418               Lookups_Num = hznum;
00419               IME_Status = IME_LOOKUP_AREA;
00420 
00421               return(IME_OK);
00422        }
00423 
00424        /* for unnormal keys */
00425        if (Lookups_Num > 0) {
00426               /* need commit the first candidate */
00427               strcpy((char *)Commit_Buf, (char *)Candidates[0]);
00428               Commit_Len = strlen((char *)Commit_Buf);
00429               Commit_And_Clean_All_Area(ime_buffer);
00430        }
00431 
00432         return(IME_UNUSED_KEY);
00433 }