Back to index

im-sdk  12.3.91
le_iml.c
Go to the documentation of this file.
00001 #include "SunIM.h"
00002 
00003 #define MAXSTRINGLEN 1024
00004 
00005 /* general functions */
00006 IMFeedbackList *le_iml_create_feedback_list(iml_session_t * s, int size)
00007 {
00008     int i;
00009     IMFeedbackList *feedback, *fbl;
00010 
00011     if (s) {
00012         feedback =
00013             (IMFeedbackList *) s->If->m->iml_new(s,
00014                                                  sizeof(IMFeedbackList) *
00015                                                  size);
00016         memset(feedback, 0, sizeof(IMFeedbackList) * size);
00017     } else {
00018         feedback =
00019             (IMFeedbackList *) calloc(1, sizeof(IMFeedbackList) * size);
00020     }
00021     for (i = 0; i < size; i++) {
00022         fbl = &feedback[i];
00023         fbl->count_feedbacks = 1;
00024         if (s) {
00025             fbl->feedbacks =
00026                 (IMFeedback *) s->If->m->iml_new(s,
00027                                                  4 * sizeof(IMFeedback));
00028             memset(fbl->feedbacks, 0, 4 * sizeof(IMFeedback));
00029         } else {
00030             fbl->feedbacks = (IMFeedback *) calloc(4, sizeof(IMFeedback));
00031         }
00032     }
00033     return feedback;
00034 }
00035 
00036 void le_iml_free_feedback_list(iml_session_t * s,
00037                                IMFeedbackList * feedback, int size)
00038 {
00039     int i;
00040     IMFeedbackList *fbl;
00041 
00042     if ((s != NULL) || (feedback == NULL))
00043         return;
00044 
00045     for (i = 0; i < size; i++) {
00046         fbl = &feedback[i];
00047         if (fbl->feedbacks != NULL)
00048             free(fbl->feedbacks);
00049     }
00050     free(feedback);
00051 }
00052 
00053 int le_iml_get_feedback(IMFeedbackList * fbl)
00054 {
00055     IMFeedback *fb = &fbl->feedbacks[0];
00056     return IM_FEEDBACK_VALUE(fb);
00057 }
00058 
00059 void le_iml_set_feedback(IMFeedbackList * fbl, int value)
00060 {
00061     IMFeedback *fb = &fbl->feedbacks[0];
00062     IM_FEEDBACK_TYPE(fb) = IM_DECORATION_FEEDBACK;
00063     IM_FEEDBACK_VALUE(fb) = value;
00064 }
00065 
00066 void le_iml_set_feedback_private(IMFeedbackList * fbl, int normalfeedback,
00067                                  int fg, int bg, int underline)
00068 {
00069     int count = 0;
00070     IMFeedback *fb;
00071 
00072     fb = &fbl->feedbacks[count];
00073     IM_FEEDBACK_TYPE(fb) = IM_DECORATION_FEEDBACK;
00074     IM_FEEDBACK_VALUE(fb) = normalfeedback;
00075     count++;
00076 
00077     if (fg != -1) {
00078         fb = &fbl->feedbacks[count];
00079         IM_FEEDBACK_TYPE(fb) = IM_FOREGROUND_RGB_FEEDBACK;
00080         IM_FEEDBACK_VALUE(fb) = fg;
00081         count++;
00082     }
00083     if (bg != -1) {
00084         fb = &fbl->feedbacks[count];
00085         IM_FEEDBACK_TYPE(fb) = IM_BACKGROUND_RGB_FEEDBACK;
00086         IM_FEEDBACK_VALUE(fb) = bg;
00087         count++;
00088     }
00089     if (underline != -1) {
00090         fb = &fbl->feedbacks[count];
00091         IM_FEEDBACK_TYPE(fb) = IM_UNDERLINE_RGB_FEEDBACK;
00092         IM_FEEDBACK_VALUE(fb) = underline;
00093         count++;
00094     }
00095     IM_FEEDBACK_COUNT(fbl) = count;
00096 }
00097 
00098 /* UTF16 string handle functions */
00099 UTFCHARCat(UTFCHAR * dest, UTFCHAR * str1, UTFCHAR * str2)
00100 {
00101     int i;
00102     for (i = 0; *str1; i++) {
00103         *dest++ = *str1++;
00104     }
00105     for (i = 0; *str2; i++) {
00106         *dest++ = *str2++;
00107     }
00108     *dest = 0;
00109     return i;
00110 }
00111 
00112 int UTFCHARCpy(UTFCHAR * dest, UTFCHAR * original)
00113 {
00114     int i;
00115     for (i = 0; *original; i++) {
00116         *dest++ = *original++;
00117     }
00118     *dest = 0;
00119     return i;
00120 }
00121 
00122 int UTFCHARLen(UTFCHAR * p)
00123 {
00124     int i;
00125     for (i = 0; *p; i++)
00126         p++;
00127     return i;
00128 }
00129 
00130 IMText *le_iml_make_imtext(iml_session_t * s, UTFCHAR * p,
00131                            IMFeedbackList * feedbacks)
00132 {
00133     int len;
00134     IMText *text = (IMText *) s->If->m->iml_new(s, sizeof(IMText));
00135 
00136     memset(text, 0, sizeof(IMText));
00137 
00138     len = UTFCHARLen(p);
00139 
00140     text->encoding = UTF16_CODESET;
00141     text->count_annotations = 0;
00142     text->annotations = NULL;
00143 
00144     text->text.utf_chars =
00145         (UTFCHAR *) s->If->m->iml_new(s, sizeof(UTFCHAR) * (len + 1));
00146     UTFCHARCpy(text->text.utf_chars, p);
00147     text->char_length = len;
00148 
00149     if (feedbacks != NULL) {
00150         text->feedback = feedbacks;
00151     } else {
00152         int i;
00153 
00154         text->feedback = le_iml_create_feedback_list(s, len);
00155         for (i = 0; i < len; i++) {
00156             le_iml_set_feedback_private(&text->feedback[i], IMReverse, -1,
00157                                         -1, -1);
00158         }
00159     }
00160 
00161     return text;
00162 }
00163 
00164 /* Send a Chinese string to system */
00165 void le_iml_commit(iml_session_t * s, UTFCHAR * commit_buf)
00166 {
00167     int len;
00168     iml_inst *lp;
00169     IMText *im_text;
00170 
00171     len = UTFCHARLen(commit_buf);
00172     if (len == 0)
00173         return;
00174 
00175     im_text = le_iml_make_imtext(s, commit_buf, NULL);
00176     lp = (iml_inst *) s->If->m->iml_make_commit_inst(s, im_text);
00177     s->If->m->iml_execute(s, &lp);
00178 }
00179 
00180 /* Send a KeyPress Event to system */
00181 void le_iml_sendback_key(iml_session_t * s, IMKeyEventStruct * key)
00182 {
00183     iml_inst *lp;
00184 
00185     lp = (iml_inst *) s->If->m->iml_make_keypress_inst(s, key);
00186     s->If->m->iml_execute(s, &lp);
00187 }
00188 
00189 /* Begin catch the system Key Event */
00190 void le_iml_conversion_on(iml_session_t * s)
00191 {
00192     iml_inst *lp;
00193 
00194     lp = (iml_inst *) s->If->m->iml_make_start_conversion_inst(s);
00195     lp = s->If->m->iml_execute(s, &lp);
00196 }
00197 
00198 /* Do not catch the system Key Event */
00199 void le_iml_conversion_off(iml_session_t * s)
00200 {
00201     iml_inst *lp;
00202 
00203     lp = (iml_inst *) s->If->m->iml_make_end_conversion_inst(s);
00204     s->If->m->iml_execute(s, &lp);
00205 }
00206 
00207 /* Start Status Area Information */
00208 void le_iml_status_start(iml_session_t * s)
00209 {
00210     iml_inst *lp;
00211     iml_inst *rrv = NULL;
00212 
00213     lp = (iml_inst *) s->If->m->iml_make_status_start_inst(s);
00214     s->If->m->iml_link_inst_tail(&rrv, lp);
00215 }
00216 
00217 /* Destroy Status Area Information */
00218 void le_iml_status_enddraw(iml_session_t * s)
00219 {
00220     iml_inst *lp;
00221 
00222     lp = (iml_inst *) s->If->m->iml_make_status_done_inst(s);
00223     s->If->m->iml_execute(s, &lp);
00224 }
00225 
00226 /* Draw Status line information */
00227 void le_iml_status_draw(iml_session_t * s, UTFCHAR * status_str)
00228 {
00229     int len;
00230     iml_inst *lp;
00231     iml_inst *rrv = NULL;
00232     IMText *im_text;
00233 
00234     le_iml_status_start(s);
00235 
00236     im_text = le_iml_make_imtext(s, status_str, NULL);
00237     lp = (iml_inst *) s->If->m->iml_make_status_draw_inst(s, im_text);
00238     s->If->m->iml_link_inst_tail(&rrv, lp);
00239     s->If->m->iml_execute(s, &rrv);
00240 }
00241 
00242 void le_iml_preedit_start(iml_session_t * s)
00243 {
00244     iml_inst *lp;
00245     iml_inst *rrv = NULL;
00246 
00247     lp = (iml_inst *) s->If->m->iml_make_preedit_start_inst(s);
00248     s->If->m->iml_link_inst_tail(&rrv, lp);
00249 }
00250 
00251 /* Destroy Pre_edit Area Information */
00252 void le_iml_preedit_enddraw(iml_session_t * s)
00253 {
00254     iml_inst *lp;
00255     iml_inst *rrv = NULL;
00256 
00257     lp = (iml_inst *) s->If->m->iml_make_preedit_erase_inst(s);
00258     s->If->m->iml_link_inst_tail(&rrv, lp);
00259     lp = (iml_inst *) s->If->m->iml_make_preedit_done_inst(s);
00260     s->If->m->iml_link_inst_tail(&rrv, lp);
00261 
00262     s->If->m->iml_execute(s, &rrv);
00263 }
00264 
00265 /* Draw Pre_edit Area Information */
00266 void le_iml_preedit_draw(iml_session_t * s, UTFCHAR * preedit_buf,
00267                          int caret_pos)
00268 {
00269     int i, len;
00270     iml_inst *lp;
00271     iml_inst *rrv = NULL;
00272     IMText *im_text;
00273     IMText Preedit_Buf;
00274 
00275     le_iml_preedit_start(s);
00276 
00277     len = UTFCHARLen(preedit_buf);
00278     DEBUG_printf("len:%d, caret_pos:%d\n", len, caret_pos);
00279 
00280     if (len == 0) {
00281         lp = (iml_inst *) s->If->m->iml_make_preedit_erase_inst(s);
00282         s->If->m->iml_link_inst_tail(&rrv, lp);
00283         s->If->m->iml_execute(s, &rrv);
00284         return;
00285     }
00286 
00287     im_text = le_iml_make_imtext(s, preedit_buf, NULL);
00288 
00289 /*
00290         if (caret_pos>len) caret_pos = len;
00291         for(i = 0; i < caret_pos; i ++) {
00292                 le_iml_set_feedback(&(im_text->feedback[i]), IMReverse);
00293         }
00294         for(i = caret_pos; i < len; i ++) {
00295                 le_iml_set_feedback(&(im_text->feedback[i]), IMUnderline);
00296         }
00297         le_iml_set_feedback(&(im_text->feedback[caret_pos - 1]), IMNormal);
00298 */
00299 
00300     lp = (iml_inst *) s->If->m->iml_make_preedit_draw_inst(s, im_text);
00301     s->If->m->iml_link_inst_tail(&rrv, lp);
00302 
00303     if (caret_pos != -1) {
00304         lp = (iml_inst *) s->If->m->iml_make_preedit_caret_inst(s,
00305                                                                 caret_pos);
00306         s->If->m->iml_link_inst_tail(&rrv, lp);
00307     }
00308     s->If->m->iml_execute(s, &rrv);
00309 }
00310 
00311 void le_iml_lookup_start(iml_session_t * s, LayoutInfo * layout)
00312 {
00313     iml_inst *lp;
00314     IMLookupStartCallbackStruct *start;
00315 
00316     start =
00317         (IMLookupStartCallbackStruct *) s->If->m->iml_new(s,
00318                                                           sizeof
00319                                                           (IMLookupStartCallbackStruct));
00320     memset(start, 0, sizeof(IMLookupStartCallbackStruct));
00321 
00322     start->IMPreference =
00323         (LayoutInfo *) s->If->m->iml_new(s, sizeof(LayoutInfo));
00324     memset(start->IMPreference, 0, sizeof(LayoutInfo));
00325 
00326     start->IMPreference->choice_per_window = layout->choice_per_window;
00327     start->IMPreference->ncolumns = layout->ncolumns;
00328     start->IMPreference->nrows = layout->nrows;
00329     start->IMPreference->drawUpDirection = layout->drawUpDirection;
00330     start->IMPreference->whoOwnsLabel = IMOwnsLabel;
00331 
00332     start->CBPreference = NULL;
00333     start->whoIsMaster = IMIsMaster;
00334 
00335     lp = (iml_inst *) s->If->m->iml_make_lookup_start_inst(s, start);
00336     s->If->m->iml_execute(s, &lp);
00337 }
00338 
00339 /* Destroy Candidate Selection Area */
00340 void le_iml_lookup_enddraw(iml_session_t * s)
00341 {
00342     iml_inst *lp;
00343 
00344     lp = (iml_inst *) s->If->m->iml_make_lookup_done_inst(s);
00345     s->If->m->iml_execute(s, &lp);
00346 }
00347 
00348 /* Draw Candidate Selection Area */
00349 void le_iml_lookup_draw(iml_session_t * s, int lookup_num,
00350                         UTFCHAR ** lookup_strs,
00351                         IMFeedbackList ** lookup_feedbacks,
00352                         UTFCHAR ** label_strs,
00353                         IMFeedbackList ** label_feedbacks,
00354                         UTFCHAR * title_str,
00355                         IMFeedbackList * title_feedbacks,
00356                         int current_candidate_id, int vertical)
00357 {
00358     int i, char_len;
00359     char begin_char;
00360     int max_len = 0;
00361     iml_inst *lp;
00362     IMText **candidates, **labels;
00363     IMLookupDrawCallbackStruct *draw;
00364     IMFeedbackList *feedback = NULL;
00365 
00366     IMLookupStartCallbackStruct *start;
00367 
00368     if (lookup_num <= 0)
00369         return;
00370     if (lookup_strs == NULL)
00371         return;
00372     if (label_strs == NULL)
00373         return;
00374 
00375     start =
00376         (IMLookupStartCallbackStruct *) s->If->m->iml_new(s,
00377                                                           sizeof
00378                                                           (IMLookupStartCallbackStruct));
00379 
00380     start->whoIsMaster = IMIsMaster;
00381     start->IMPreference =
00382         (LayoutInfo *) s->If->m->iml_new(s, sizeof(LayoutInfo));
00383 
00384     start->IMPreference->choice_per_window = lookup_num;
00385     start->IMPreference->ncolumns = (vertical) ? 1 : lookup_num;
00386     start->IMPreference->nrows = (vertical) ? lookup_num : 1;
00387     start->IMPreference->drawUpDirection = vertical;
00388     start->IMPreference->whoOwnsLabel = IMOwnsLabel;
00389 
00390     lp = s->If->m->iml_make_lookup_start_inst(s, start);
00391     s->If->m->iml_execute(s, &lp);
00392 
00393 
00394     /* Set candidate data */
00395     candidates =
00396         (IMText **) s->If->m->iml_new(s, lookup_num * sizeof(IMText *));
00397     if (candidates == NULL)
00398         return;
00399     memset(candidates, 0, lookup_num * sizeof(IMText *));
00400     for (i = 0; i < lookup_num; i++) {
00401         feedback = NULL;
00402         if (lookup_feedbacks != NULL)
00403             feedback = lookup_feedbacks[i];
00404 
00405         if (lookup_strs[i])
00406             candidates[i] = (IMText *) le_iml_make_imtext(s, lookup_strs[i], feedback);
00407         else
00408             candidates[i] = (IMText *) NULL;
00409     }
00410 
00411     labels =
00412         (IMText **) s->If->m->iml_new(s, lookup_num * sizeof(IMText *));
00413     if (labels == NULL)
00414         return;
00415     memset(labels, 0, lookup_num * sizeof(IMText *));
00416     for (i = 0; i < lookup_num; i++) {
00417         feedback = NULL;
00418         if (label_feedbacks != NULL)
00419             feedback = label_feedbacks[i];
00420 
00421         if (label_strs[i])
00422             labels[i] =
00423                 (IMText *) le_iml_make_imtext(s, label_strs[i], feedback);
00424         else
00425             labels[i] = (IMText *) NULL;
00426     }
00427 
00428     draw =
00429         (IMLookupDrawCallbackStruct *) s->If->m->iml_new(s,
00430                                                          sizeof
00431                                                          (IMLookupDrawCallbackStruct));
00432     memset(draw, 0, sizeof(IMLookupDrawCallbackStruct));
00433 
00434     if (title_str == NULL)
00435         draw->title = NULL;
00436     else
00437         draw->title =
00438             (IMText *) le_iml_make_imtext(s, title_str, title_feedbacks);
00439 
00440     draw->n_choices = lookup_num;
00441     draw->index_of_first_candidate = 0;
00442     draw->index_of_last_candidate = lookup_num - 1;
00443     draw->index_of_current_candidate = current_candidate_id;
00444 
00445     /* Set choices data */
00446     draw->choices =
00447         (IMChoiceObject *) s->If->m->iml_new(s,
00448                                              lookup_num *
00449                                              sizeof(IMChoiceObject));
00450     memset(draw->choices, 0, lookup_num * sizeof(IMChoiceObject));
00451 
00452     for (i = 0; i < lookup_num; i++) {
00453         IMText *vt;         /* for value */
00454         IMText *lt;         /* for label */
00455 
00456         vt = draw->choices[i].value = candidates[i];
00457         lt = draw->choices[i].label = labels[i];
00458 
00459         if (vt && max_len < vt->char_length)
00460             max_len = vt->char_length;
00461     }
00462 
00463     draw->max_len = max_len;
00464 
00465     lp = (iml_inst *) s->If->m->iml_make_lookup_draw_inst(s, draw);
00466     s->If->m->iml_execute(s, &lp);
00467 }
00468 
00469 /* Start An Auxiliary Window */
00470 void le_iml_aux_start(iml_session_t * s, char *classname)
00471 {
00472     iml_inst *lp;
00473     IMAuxStartCallbackStruct *aux;
00474 
00475     aux =
00476         (IMAuxStartCallbackStruct *) s->If->m->iml_new(s,
00477                                                        sizeof
00478                                                        (IMAuxStartCallbackStruct));
00479     memset(aux, 0, sizeof(IMAuxStartCallbackStruct));
00480     aux->aux_name = classname;
00481 
00482     lp = (iml_inst *) s->If->m->iml_make_aux_start_inst(s, aux);
00483     s->If->m->iml_execute(s, &lp);
00484 
00485     DEBUG_printf("iml_aux_start ----- auxwin name :%s \n", classname);
00486 }
00487 
00488 /* Close the Auxiliary Window */
00489 void le_iml_aux_done(iml_session_t * s, char *classname)
00490 {
00491     iml_inst *lp;
00492     IMAuxDoneCallbackStruct *aux;
00493 
00494     aux =
00495         (IMAuxDoneCallbackStruct *) s->If->m->iml_new(s,
00496                                                       sizeof
00497                                                       (IMAuxDoneCallbackStruct));
00498     memset(aux, 0, sizeof(IMAuxDoneCallbackStruct));
00499     aux->aux_name = classname;
00500 
00501     lp = (iml_inst *) s->If->m->iml_make_aux_done_inst(s, aux);
00502     s->If->m->iml_execute(s, &lp);
00503 
00504     DEBUG_printf("iml_aux_done -------------------------------- end \n");
00505 }
00506 
00507 
00508 /* Draw the Auxiliary Window Items */
00509 void le_iml_aux_draw(iml_session_t * s, char *classname,
00510                      int count_integers, int *integers,
00511                      int count_strings, int *string_char_lens,
00512                      UTFCHAR **strings)
00513 {
00514     iml_inst *lp;
00515     IMText *lt;
00516     IMAuxDrawCallbackStruct *aux;
00517     int i, len;
00518 
00519     aux = (IMAuxDrawCallbackStruct *) s->If->m->iml_new(s, sizeof(IMAuxDrawCallbackStruct));
00520     memset(aux, 0, sizeof(IMAuxDrawCallbackStruct));
00521     aux->aux_name = classname;
00522 
00523     aux->count_integer_values = count_integers;
00524     if (count_integers) {
00525         aux->integer_values = (int *)s->If->m->iml_new(s, sizeof(int) * count_integers);
00526         memset(aux->integer_values, 0, sizeof(int) * count_integers);
00527         for (i = 0; i < count_integers; i++) {
00528             aux->integer_values[i] = integers[i];
00529         }
00530     }
00531 
00532     aux->count_string_values = count_strings;
00533     if (count_strings) {
00534         aux->string_values = (IMText *) s->If->m->iml_new(s, sizeof(IMText) * count_strings);
00535         memset(aux->string_values, 0, sizeof(IMText) * count_strings);
00536 
00537         aux->string_values->encoding = UTF16_CODESET;
00538         for (i = 0, lt = aux->string_values; i < count_strings; i++, lt++) {
00539             len = (strings[i])?(UTFCHARLen(strings[i])):(0);
00540             lt->text.utf_chars = (UTFCHAR *)s->If->m->iml_new(s, (len+1)*sizeof(UTFCHAR));
00541             memset(lt->text.utf_chars, 0,(len+1)*sizeof(UTFCHAR));
00542             lt->char_length = len+1;
00543             if (strings[i])
00544                 memcpy(lt->text.utf_chars, strings[i], len*sizeof(UTFCHAR));
00545         }
00546     }
00547 
00548     lp = (iml_inst *) s->If->m->iml_make_aux_draw_inst(s, aux);
00549     s->If->m->iml_execute(s, &lp);
00550 
00551     DEBUG_printf("iml_aux_draw -------------------------------- end \n");
00552 }
00553 
00554 static char* dummy="";
00555 
00556 /* Draw the Auxiliary Window Items */
00557 void le_iml_aux_draw_native(iml_session_t * s, char *classname,
00558                              int count_integers, int *integers,
00559                              int encoding_id, int count_strings,  char **strings)
00560 {
00561     int i, from_len, to_left;
00562     char* src, *tmp_ptr;
00563 
00564     if (encoding_id != UTF16_CODESET) {
00565         UTFCHAR * utf16strings[count_strings+1]; // to avoid count_strings == 0
00566         for (i=0; i < count_strings; ++i) {
00567             src = (strings[i])?(strings[i]):(dummy);
00568             from_len = strlen(src) + 1;
00569 
00570             utf16strings[i] = (UTFCHAR*)malloc((from_len+1)*sizeof(UTFCHAR));
00571             utf16strings[i][0] = 0;
00572             utf16strings[i][from_len-1] = 0;
00573             utf16strings[i][from_len] = 0;
00574 
00575             tmp_ptr = (char*)utf16strings[i];
00576             to_left = (from_len)*sizeof(UTFCHAR);
00577             Convert_Native_To_UTF16(encoding_id, strings[i], from_len, (char **)&tmp_ptr, (size_t *) & to_left);
00578         }
00579 
00580         le_iml_aux_draw(s, classname, count_integers, integers, count_strings, NULL, utf16strings);
00581 
00582         for (i=0; i < count_strings; ++i)
00583             free(utf16strings[i]);
00584     } else {
00585         le_iml_aux_draw(s, classname, count_integers, integers, count_strings, NULL, (UTFCHAR**)strings);
00586     }
00587 }