Back to index

im-sdk  12.3.91
zhuyin_properties.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include "config.h"
00005 #endif
00006 
00007 #include "ctim.h"
00008 #include "zhuyin_data.h"
00009 #include "zhuyin_messages.h"
00010 
00011 char **KeymappingOptions = NULL;
00012 
00013 ImePropertyRec zhuyin_options[] = {
00014     { ZHUYIN_KEYMAP_MODE_ID,     ENCODE_UTF8, "/keymapping",     ZHUYIN_KEYMAP_MODE_STR, NULL, ImeProperty_Selection, {0, 0}, 0},
00015     { ZHUYIN_AUTOSELECT_MODE_ID, ENCODE_UTF8, "/singleselect",   ZHUYIN_AUTOSELECT_MODE_STR, NULL, ImeProperty_Toggle, {0, 0}, 1},
00016 };
00017 
00018 ImePropertyListRec zhuyin_optionlist = {
00019     sizeof(zhuyin_options)/sizeof(ImePropertyRec),
00020     zhuyin_options
00021 };
00022 
00023 extern ImmServices imm_services;
00024 
00025 char *zhuyin_get_keymapping_string(TZhuyinKeymap *pZhuyinKeymap)
00026 {
00027        int idx;
00028        char ch;
00029        int buf_len = 0;
00030         char *keymapping_str = NULL;
00031 
00032         if (pZhuyinKeymap == NULL ||
00033            pZhuyinKeymap->pName == NULL ||
00034            pZhuyinKeymap->pKeymap == NULL)
00035               return NULL;
00036 
00037        buf_len = strlen (pZhuyinKeymap->pName);
00038        for (idx = 1; idx <= NUM_OF_ZHUYIN_SYMBOL; idx++) {
00039               ch = pZhuyinKeymap->pKeymap[idx];
00040                      if (ch != 0) {
00041                      buf_len += strlen((char *)ZhuyinIndexToZhuyinSymbol(idx));
00042                      }
00043               }
00044 
00045        buf_len += (NUM_OF_ZHUYIN_SYMBOL * 2 + 3);
00046 
00047        keymapping_str = (char *)calloc (buf_len, sizeof (char));
00048        if (keymapping_str == NULL)
00049               return NULL;
00050 
00051        snprintf(keymapping_str, buf_len, "%s", pZhuyinKeymap->pName);
00052 
00053        for (idx = 1; idx <= NUM_OF_ZHUYIN_SYMBOL; idx++) {
00054               char key_str[256];
00055               ch = pZhuyinKeymap->pKeymap[idx];
00056                      if (ch != 0) {
00057                      char *format_str = "|%c%s";
00058                      if (ch == '/' || ch == '|') {
00059                             format_str = "|\\%c%s";
00060                      }
00061                      snprintf(key_str, 256, format_str,
00062                              ch, ZhuyinIndexToZhuyinSymbol(idx));
00063                      strncat(keymapping_str, key_str, buf_len);
00064                      }
00065               }
00066 
00067        DEBUG_printf("keymapping_str: %s\n", keymapping_str);
00068 
00069        return keymapping_str;
00070 }
00071 
00072 ImeResult zhuyin_Init_Ime_Properties(ImeInfoRec *zhuyin_info,  TZhuyinData *pZhuyinData)
00073 {
00074        int i;
00075        int num_keymappings;
00076 
00077        if (zhuyin_info == NULL || pZhuyinData == NULL)
00078               return (IME_FAIL);
00079 
00080        DEBUG_printf("zhuyin_Init_Ime_Properties\n");
00081 
00082        if (KeymappingOptions == NULL &&
00083            pZhuyinData->pZhuyinKeymapList &&
00084            pZhuyinData->pZhuyinKeymapList->nNum_Keymaps > 0){
00085               int idx;
00086               num_keymappings = pZhuyinData->pZhuyinKeymapList->nNum_Keymaps;
00087               KeymappingOptions = (char **)calloc (num_keymappings + 1, 
00088                                                sizeof (char *));
00089               idx = 0;
00090               for (i = 0; i < num_keymappings; i++) {
00091                      char *keymapping_str;
00092                      keymapping_str = (char *)zhuyin_get_keymapping_string(pZhuyinData->pZhuyinKeymapList->pKeymaps[i]);
00093 
00094                      if (keymapping_str)
00095                             KeymappingOptions[idx++] = keymapping_str;
00096               }
00097        }
00098 
00099        zhuyin_options[ZHUYIN_KEYMAP_MODE_ID].range.multiString_range = KeymappingOptions;
00100        zhuyin_options[ZHUYIN_KEYMAP_MODE_ID].value.int_value = 0;
00101        zhuyin_options[ZHUYIN_AUTOSELECT_MODE_ID].value.int_value = 1;
00102 
00103        zhuyin_info->pl = (ImePropertyListRec *)(&zhuyin_optionlist);
00104 
00105        return (IME_OK);
00106 }
00107 
00108 ImeResult zhuyin_Destroy_Ime_Properties(ImeInfoRec *zhuyin_info)
00109 {
00110        int i;
00111 
00112        if (KeymappingOptions != NULL) {
00113               for (i = 0; KeymappingOptions[i]; i++) {
00114                      free ((char *) KeymappingOptions[i]);
00115               }
00116               free ((char *)KeymappingOptions);
00117        }
00118 
00119        return (IME_OK);
00120 }
00121 
00122 ImeResult zhuyin_Set_Ime_Properties(ImeInputContext ic, TZhuyinData *pZhuyinData)
00123 {
00124        int i;
00125        CodeTableStruct *ctHeader;
00126        ImePropertyListRec *pl;
00127        ImePropertyRec *pi;
00128 
00129        DEBUG_printf("zhuyin_Set_Ime_Properties\n");
00130 
00131        if (pZhuyinData == NULL || pZhuyinData->pCodetableHeader == NULL)
00132               return (IME_FAIL);
00133 
00134        pZhuyinData->nCurrentKeymapID = 0;
00135 
00136        /* ctHeader's argument setting are always coveried by user defined data */
00137        ctHeader = pZhuyinData->pCodetableHeader;
00138        ctHeader->nKeyByKeyMode = 0;
00139        ctHeader->nHelpInfoMode = 0;
00140        ctHeader->nAutoSelectMode = 1;
00141        ctHeader->nKeyPromptMode = 1;
00142 
00143         pl = imm_services->ImmGetPropertyList(ic);
00144        if (pl != NULL && pl->count > 0 && pl->properties != NULL) {
00145               for (i = 0; i < pl->count; i++) {
00146                      pi = &(pl->properties[i]);
00147                      switch (pi->id) {
00148                      case ZHUYIN_KEYMAP_MODE_ID:
00149                             pZhuyinData->nCurrentKeymapID  =  pi->value.int_value;
00150                             break;
00151                      case ZHUYIN_AUTOSELECT_MODE_ID:
00152                             ctHeader->nAutoSelectMode = pi->value.int_value;
00153                             break;
00154                      }
00155               }
00156        }
00157 
00158        return (IME_OK);
00159 }