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], IMUnderline, -1,
00157                                         -1, -1);
00158         }
00159     }
00160 
00161     return text;
00162 }
00163 
00164 /* Send a native 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, int highlight_from, int highlight_to)
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     for (i = highlight_from; i < highlight_to; i ++) {
00290         le_iml_set_feedback(&(im_text->feedback[i]), IMReverse);
00291     }
00292 
00293     lp = (iml_inst *) s->If->m->iml_make_preedit_draw_inst(s, im_text);
00294     s->If->m->iml_link_inst_tail(&rrv, lp);
00295 
00296     if (caret_pos != -1) {
00297         lp = (iml_inst *) s->If->m->iml_make_preedit_caret_inst(s,
00298                                                                 caret_pos);
00299         s->If->m->iml_link_inst_tail(&rrv, lp);
00300     }
00301     s->If->m->iml_execute(s, &rrv);
00302 }
00303 
00304 void le_iml_lookup_start(iml_session_t * s, LayoutInfo * layout)
00305 {
00306     iml_inst *lp;
00307     IMLookupStartCallbackStruct *start;
00308 
00309     start =
00310         (IMLookupStartCallbackStruct *) s->If->m->iml_new(s,
00311                                                           sizeof
00312                                                           (IMLookupStartCallbackStruct));
00313     memset(start, 0, sizeof(IMLookupStartCallbackStruct));
00314 
00315     start->IMPreference =
00316         (LayoutInfo *) s->If->m->iml_new(s, sizeof(LayoutInfo));
00317     memset(start->IMPreference, 0, sizeof(LayoutInfo));
00318 
00319     start->IMPreference->choice_per_window = layout->choice_per_window;
00320     start->IMPreference->ncolumns = layout->ncolumns;
00321     start->IMPreference->nrows = layout->nrows;
00322     start->IMPreference->drawUpDirection = layout->drawUpDirection;
00323     start->IMPreference->whoOwnsLabel = IMOwnsLabel;
00324 
00325     start->CBPreference = NULL;
00326     start->whoIsMaster = IMIsMaster;
00327 
00328     lp = (iml_inst *) s->If->m->iml_make_lookup_start_inst(s, start);
00329     s->If->m->iml_execute(s, &lp);
00330 }
00331 
00332 /* Destroy Candidate Selection Area */
00333 void le_iml_lookup_enddraw(iml_session_t * s)
00334 {
00335     iml_inst *lp;
00336 
00337     lp = (iml_inst *) s->If->m->iml_make_lookup_done_inst(s);
00338     s->If->m->iml_execute(s, &lp);
00339 }
00340 
00341 /* Draw Candidate Selection Area */
00342 void le_iml_lookup_draw(iml_session_t * s, int lookup_num,
00343                         UTFCHAR ** lookup_strs,
00344                         IMFeedbackList ** lookup_feedbacks,
00345                         UTFCHAR ** label_strs,
00346                         IMFeedbackList ** label_feedbacks,
00347                         UTFCHAR * title_str,
00348                         IMFeedbackList * title_feedbacks,
00349                         int current_candidate_id, int vertical)
00350 {
00351     int i, char_len;
00352     char begin_char;
00353     int max_len = 0;
00354     iml_inst *lp;
00355     IMText **candidates, **labels;
00356     IMLookupDrawCallbackStruct *draw;
00357     IMFeedbackList *feedback = NULL;
00358 
00359     IMLookupStartCallbackStruct *start;
00360 
00361     if (lookup_num <= 0)
00362         return;
00363     if (lookup_strs == NULL)
00364         return;
00365 
00366     start =
00367         (IMLookupStartCallbackStruct *) s->If->m->iml_new(s,
00368                                                           sizeof
00369                                                           (IMLookupStartCallbackStruct));
00370 
00371     start->whoIsMaster = IMIsMaster;
00372     start->IMPreference =
00373         (LayoutInfo *) s->If->m->iml_new(s, sizeof(LayoutInfo));
00374 
00375     start->IMPreference->choice_per_window = lookup_num;
00376     start->IMPreference->ncolumns = (vertical) ? 1 : lookup_num;
00377     start->IMPreference->nrows = (vertical) ? lookup_num : 1;
00378     start->IMPreference->drawUpDirection = vertical;
00379     start->IMPreference->whoOwnsLabel = IMOwnsLabel;
00380 
00381     lp = s->If->m->iml_make_lookup_start_inst(s, start);
00382     s->If->m->iml_execute(s, &lp);
00383 
00384 
00385     /* Set candidate data */
00386     candidates =
00387         (IMText **) s->If->m->iml_new(s, lookup_num * sizeof(IMText *));
00388     if (candidates == NULL)
00389         return;
00390     memset(candidates, 0, lookup_num * sizeof(IMText *));
00391     for (i = 0; i < lookup_num; i++) {
00392         feedback = NULL;
00393         if (lookup_feedbacks != NULL)
00394             feedback = lookup_feedbacks[i];
00395 
00396         if (lookup_strs[i])
00397             candidates[i] = (IMText *) le_iml_make_imtext(s, lookup_strs[i], feedback);
00398         else
00399             candidates[i] = (IMText *) NULL;
00400     }
00401 
00402     labels =
00403         (IMText **) s->If->m->iml_new(s, lookup_num * sizeof(IMText *));
00404     if (labels == NULL)
00405         return;
00406 
00407     memset(labels, 0, lookup_num * sizeof(IMText *));
00408     for (i = 0; i < lookup_num; i++) {
00409         feedback = NULL;
00410         if (label_feedbacks != NULL)
00411             feedback = label_feedbacks[i];
00412 
00413         if (label_strs && label_strs[i])
00414             labels[i] =
00415                 (IMText *) le_iml_make_imtext(s, label_strs[i], feedback);
00416         else
00417             labels[i] = (IMText *) NULL;
00418     }
00419 
00420     draw =
00421         (IMLookupDrawCallbackStruct *) s->If->m->iml_new(s,
00422                                                          sizeof
00423                                                          (IMLookupDrawCallbackStruct));
00424     memset(draw, 0, sizeof(IMLookupDrawCallbackStruct));
00425 
00426     if (title_str == NULL)
00427         draw->title = NULL;
00428     else
00429         draw->title =
00430             (IMText *) le_iml_make_imtext(s, title_str, title_feedbacks);
00431 
00432     draw->n_choices = lookup_num;
00433     draw->index_of_first_candidate = 0;
00434     draw->index_of_last_candidate = lookup_num - 1;
00435     draw->index_of_current_candidate = current_candidate_id;
00436 
00437     /* Set choices data */
00438     draw->choices =
00439         (IMChoiceObject *) s->If->m->iml_new(s,
00440                                              lookup_num *
00441                                              sizeof(IMChoiceObject));
00442     memset(draw->choices, 0, lookup_num * sizeof(IMChoiceObject));
00443 
00444     for (i = 0; i < lookup_num; i++) {
00445         IMText *vt;         /* for value */
00446         IMText *lt;         /* for label */
00447 
00448         vt = draw->choices[i].value = candidates[i];
00449         lt = draw->choices[i].label = labels[i];
00450 
00451         if (vt && max_len < vt->char_length)
00452             max_len = vt->char_length;
00453     }
00454 
00455     draw->max_len = max_len;
00456 
00457     lp = (iml_inst *) s->If->m->iml_make_lookup_draw_inst(s, draw);
00458     s->If->m->iml_execute(s, &lp);
00459 }
00460 
00461 /* Start An Auxiliary Window */
00462 void le_iml_aux_start(iml_session_t * s, char *classname)
00463 {
00464     iml_inst *lp;
00465     IMAuxStartCallbackStruct *aux;
00466 
00467     aux =
00468         (IMAuxStartCallbackStruct *) s->If->m->iml_new(s,
00469                                                        sizeof
00470                                                        (IMAuxStartCallbackStruct));
00471     memset(aux, 0, sizeof(IMAuxStartCallbackStruct));
00472     aux->aux_name = classname;
00473 
00474     lp = (iml_inst *) s->If->m->iml_make_aux_start_inst(s, aux);
00475     s->If->m->iml_execute(s, &lp);
00476 
00477     DEBUG_printf("iml_aux_start ----- auxwin name :%s \n", classname);
00478 }
00479 
00480 /* Close the Auxiliary Window */
00481 void le_iml_aux_done(iml_session_t * s, char *classname)
00482 {
00483     iml_inst *lp;
00484     IMAuxDoneCallbackStruct *aux;
00485 
00486     aux =
00487         (IMAuxDoneCallbackStruct *) s->If->m->iml_new(s,
00488                                                       sizeof
00489                                                       (IMAuxDoneCallbackStruct));
00490     memset(aux, 0, sizeof(IMAuxDoneCallbackStruct));
00491     aux->aux_name = classname;
00492 
00493     lp = (iml_inst *) s->If->m->iml_make_aux_done_inst(s, aux);
00494     s->If->m->iml_execute(s, &lp);
00495 
00496     DEBUG_printf("iml_aux_done -------------------------------- end \n");
00497 }
00498 
00499 
00500 /* Draw the Auxiliary Window Items */
00501 void le_iml_aux_draw(iml_session_t * s, char *classname,
00502                      int count_integers, int *integers,
00503                      int count_strings, int *string_char_lens,
00504                      UTFCHAR **strings)
00505 {
00506     iml_inst *lp;
00507     IMText *lt;
00508     IMAuxDrawCallbackStruct *aux;
00509     int i, len;
00510 
00511     aux = (IMAuxDrawCallbackStruct *) s->If->m->iml_new(s, sizeof(IMAuxDrawCallbackStruct));
00512     memset(aux, 0, sizeof(IMAuxDrawCallbackStruct));
00513     aux->aux_name = classname;
00514 
00515     aux->count_integer_values = count_integers;
00516     if (count_integers) {
00517         aux->integer_values = (int *)s->If->m->iml_new(s, sizeof(int) * count_integers);
00518         memset(aux->integer_values, 0, sizeof(int) * count_integers);
00519         for (i = 0; i < count_integers; i++) {
00520             aux->integer_values[i] = integers[i];
00521         }
00522     }
00523 
00524     aux->count_string_values = count_strings;
00525     if (count_strings) {
00526         aux->string_values = (IMText *) s->If->m->iml_new(s, sizeof(IMText) * count_strings);
00527         memset(aux->string_values, 0, sizeof(IMText) * count_strings);
00528 
00529         aux->string_values->encoding = UTF16_CODESET;
00530         for (i = 0, lt = aux->string_values; i < count_strings; i++, lt++) {
00531             len = (strings[i])?(UTFCHARLen(strings[i])):(0);
00532             lt->text.utf_chars = (UTFCHAR *)s->If->m->iml_new(s, (len+1)*sizeof(UTFCHAR));
00533             memset(lt->text.utf_chars, 0,(len+1)*sizeof(UTFCHAR));
00534             lt->char_length = len+1;
00535             if (strings[i])
00536                 memcpy(lt->text.utf_chars, strings[i], len*sizeof(UTFCHAR));
00537         }
00538     }
00539 
00540     lp = (iml_inst *) s->If->m->iml_make_aux_draw_inst(s, aux);
00541     s->If->m->iml_execute(s, &lp);
00542 
00543     DEBUG_printf("iml_aux_draw -------------------------------- end \n");
00544 }
00545 
00546 static char* dummy="";
00547 
00548 /* Draw the Auxiliary Window Items */
00549 void le_iml_aux_draw_native(iml_session_t * s, char *classname,
00550                              int count_integers, int *integers,
00551                              int encoding_id, int count_strings,  char **strings)
00552 {
00553     int i, from_len, to_left;
00554     char* src, *tmp_ptr;
00555 
00556     if (encoding_id != UTF16_CODESET) {
00557         UTFCHAR * utf16strings[count_strings+1]; // to avoid count_strings == 0
00558         for (i=0; i < count_strings; ++i) {
00559             src = (strings[i])?(strings[i]):(dummy);
00560             from_len = strlen(src) + 1;
00561 
00562             utf16strings[i] = (UTFCHAR*)calloc(from_len+1, sizeof(UTFCHAR));
00563             utf16strings[i][0] = 0;
00564             utf16strings[i][from_len-1] = 0;
00565             utf16strings[i][from_len] = 0;
00566 
00567             tmp_ptr = (char*)utf16strings[i];
00568             to_left = (from_len)*sizeof(UTFCHAR);
00569             Convert_Native_To_UTF16(encoding_id, strings[i], from_len, (char **)&tmp_ptr, (size_t *) & to_left);
00570         }
00571 
00572         le_iml_aux_draw(s, classname, count_integers, integers, count_strings, NULL, utf16strings);
00573 
00574         for (i=0; i < count_strings; ++i)
00575             free(utf16strings[i]);
00576     } else {
00577         le_iml_aux_draw(s, classname, count_integers, integers, count_strings, NULL, (UTFCHAR**)strings);
00578     }
00579 }