Back to index

im-sdk  12.3.91
ctim_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 "ctim_messages.h"
00009 
00010 ImePropertyRec ctim_options[] = {
00011     { KEYBYKEY_MODE_ID,   ENCODE_UTF8, "/input_style",    KEYBYKEY_MODE_STR, NULL, ImeProperty_Toggle, {0, 0}, 0},
00012     { HELPINFO_MODE_ID,   ENCODE_UTF8, "/codehinting",    HELPINFO_MODE_STR,   NULL, ImeProperty_Toggle, {0, 0}, 0},
00013     { AUTOSELECT_MODE_ID, ENCODE_UTF8, "/singleselect",   AUTOSELECT_MODE_STR, NULL, ImeProperty_Toggle, {0, 0}, 1},
00014     { KEYPROMPT_MODE_ID,  ENCODE_UTF8, "/keyprompt",      KEYPROMPT_MODE_STR, NULL, ImeProperty_Toggle, {0, 0}, 0},
00015     { KEYMAPPING_MODE_ID, ENCODE_UTF8, "/keymapping",     KEYMAPPING_MODE_STR, NULL, ImeProperty_Selection, {0, 0}, 0},
00016 };
00017 
00018 ImePropertyListRec ctim_optionlist = {
00019     sizeof(ctim_options)/sizeof(ImePropertyRec),
00020     ctim_options
00021 };
00022 
00023 extern ImmServices imm_services;
00024 
00025 char *ctim_get_keymapping_string(CodeTableStruct *ctHeader)
00026 {
00027        int i;
00028         char *keymapping_str = NULL;
00029 
00030         if (ctHeader == NULL || ctHeader->keyprompt == NULL)
00031               return NULL;
00032 
00033        if (GETBIT(ctHeader->bSectionsFlag, KEYPROMPT_SECTION)) {
00034               int buf_len = 0;
00035               int item_num = 0;
00036               char ch;
00037 
00038               char *prefix = "keymapping:/default";
00039 
00040               for (i = 0; i < MAX_USEDCODES_NUM; i ++) {
00041                      ch = ctHeader->UsedCodes[i];
00042 
00043                      if (ch == 0) break;
00044 
00045                      if (ctHeader->keyprompt[ch].prompt[0] != 0) {
00046                             item_num ++;
00047                             buf_len += strlen (ctHeader->keyprompt[ch].prompt);
00048                             DEBUG_printf("%c: %s\n", ch, ctHeader->keyprompt[ch].prompt);
00049                      }
00050               }
00051 
00052               if (buf_len > 0) {
00053                      buf_len += (item_num * 2 + strlen (prefix)) + 3;
00054 
00055                      keymapping_str = (char *)calloc (1, buf_len);
00056                      snprintf(keymapping_str, buf_len, "%s", prefix);
00057 
00058                      for (i = 0; i < MAX_USEDCODES_NUM; i ++) {
00059                             char key_str[256];
00060 
00061                             ch = ctHeader->UsedCodes[i];
00062                             if (ch == 0) break;
00063 
00064                             if (ctHeader->keyprompt[ch].prompt[0] != 0) {
00065                                    char *format_str = "|%c%s";
00066                                    if (ch == '/' || ch == '|') {
00067                                           format_str = "|\\%c%s";
00068                                    }
00069                                    snprintf(key_str, 256, format_str,
00070                                            ch, ctHeader->keyprompt[ch].prompt);
00071                                    strncat(keymapping_str, key_str, buf_len);
00072                             }
00073                      }
00074                      DEBUG_printf("keymapping_str: %s\n", keymapping_str);
00075                 }
00076        }
00077 
00078        return keymapping_str;
00079 }
00080 
00081 ImeResult ctim_Init_Ime_Properties(ImeInfoRec *ctim_info, CodeTableStruct *ctHeader)
00082 {
00083        int i;
00084        ImePropertyListRec *pl;
00085 
00086        if (ctim_info == NULL || ctHeader == NULL)
00087               return (IME_FAIL);
00088 
00089        DEBUG_printf("ctim_Init_Ime_Properties\n");
00090 
00091        pl = (ImePropertyListRec *)calloc(1, sizeof (ImePropertyListRec));
00092        if (pl == NULL)
00093               return (IME_FAIL);
00094 
00095        pl->properties = (ImePropertyRec *)calloc (CTIM_ARGS_NUM, sizeof(ImePropertyRec));
00096        if (pl->properties == NULL) {
00097               free ((char *) pl);
00098               return (IME_FAIL);
00099        }
00100 
00101        pl->count = CTIM_ARGS_NUM;
00102        for (i = 0; i < CTIM_ARGS_NUM; i ++) {
00103               ImePropertyRec *pi = &(pl->properties[i]);
00104 
00105               pi->id = ctim_options[i].id;
00106               pi->encode = ctim_options[i].encode;
00107               pi->key = ctim_options[i].key;
00108               pi->name = ctim_options[i].name;
00109               pi->tip = ctim_options[i].tip;
00110               pi->type = ctim_options[i].type;
00111               pi->range = ctim_options[i].range;
00112        }
00113 
00114        pl->properties[KEYBYKEY_MODE_ID].value.int_value = ctHeader->nKeyByKeyMode;
00115        pl->properties[HELPINFO_MODE_ID].value.int_value = ctHeader->nHelpInfoMode;
00116        pl->properties[AUTOSELECT_MODE_ID].value.int_value = ctHeader->nAutoSelectMode;
00117        pl->properties[KEYPROMPT_MODE_ID].value.int_value = ctHeader->nKeyPromptMode;
00118 
00119        pl->properties[KEYMAPPING_MODE_ID].encode = ctHeader->Encode;
00120        pl->properties[KEYMAPPING_MODE_ID].name = (char *)ctim_get_keymapping_string(ctHeader);
00121        pl->properties[KEYMAPPING_MODE_ID].value.int_value = 0;
00122 
00123        ctim_info->pl = pl;
00124 
00125        return (IME_OK);
00126 }
00127 
00128 ImeResult ctim_Destroy_Ime_Properties(ImeInfoRec *ctim_info)
00129 {
00130        ImePropertyListRec *pl;
00131 
00132        if (ctim_info == NULL)
00133               return (IME_FAIL);
00134 
00135        DEBUG_printf("ctim_Destroy_Ime_Properties\n");
00136 
00137        pl = ctim_info->pl;
00138        if (pl) {
00139               if (pl->properties) {
00140                      char *name = (char *)pl->properties[KEYMAPPING_MODE_ID].name;
00141                      if (name) free ((char *)name);
00142 
00143                      free ((char *) pl->properties);
00144               }
00145               free ((char *) pl);
00146        }
00147 
00148        return (IME_OK);
00149 }
00150 
00151 ImeResult ctim_Set_Ime_Properties(ImeInputContext ic, CodeTableStruct *ctHeader)
00152 {
00153        int i;
00154        ImePropertyListRec *pl;
00155        ImePropertyRec *pi;
00156 
00157        DEBUG_printf("ctim_Set_Ime_Properties\n");
00158 
00159        if (ctHeader == NULL)
00160               return (IME_FAIL);
00161 
00162         pl = imm_services->ImmGetPropertyList(ic);
00163        if (pl != NULL && pl->count > 0 && pl->properties != NULL) {
00164               for (i = 0; i < pl->count; i++) {
00165                      pi = &(pl->properties[i]);
00166                      switch (pi->id) {
00167                      case KEYBYKEY_MODE_ID:
00168                             ctHeader->nKeyByKeyMode =  pi->value.int_value;
00169                             break;
00170                      case HELPINFO_MODE_ID:
00171                             ctHeader->nHelpInfoMode = pi->value.int_value;
00172                             break;
00173                      case AUTOSELECT_MODE_ID:
00174                             ctHeader->nAutoSelectMode = pi->value.int_value;
00175                             break;
00176                      case KEYPROMPT_MODE_ID:
00177                             ctHeader->nKeyPromptMode = pi->value.int_value;
00178                             break;
00179                      }
00180               }
00181        }
00182 
00183        return (IME_OK);
00184 }