Back to index

im-sdk  12.3.91
IIIMLineEdit.cpp
Go to the documentation of this file.
00001 #include <qdatetime.h>
00002 #include <qapplication.h>
00003 #include "IIIMQKeyEvent.hh"
00004 #include "IIIMPreedit.hh"
00005 #include "IIIMLineEdit.hh"
00006 
00007 IIIMPreeditWidget*
00008 IIIMLineEdit::get_preedit()
00009 {
00010     if (ppw) return ppw;
00011     ppw = new IIIMPreeditWidget(get_context(), this);
00012     return ppw;
00013 }
00014 
00015 IIIMStatusWidget*
00016 IIIMLineEdit::get_status()
00017 {
00018     if (psw) return psw;
00019 
00020     psw = new IIIMStatusWidget(get_context(),
00021                             NULL, NULL,
00022                             Qt::WStyle_Customize
00023                             | Qt::WStyle_NoBorder
00024                             | Qt::WX11BypassWM
00025                             | Qt::WStyle_StaysOnTop);
00026     psw->place(this, IIIMStatusWidget::BOTTOM_RIGHT);
00027     connect(this, SIGNAL(ref_changed()), psw, SLOT(ref_change()));
00028     return psw;
00029 }
00030 
00031 void
00032 IIIMLineEdit::resizeEvent(
00033     QResizeEvent *pre
00034 )
00035 {
00036     emit ref_changed();
00037     QLineEdit::resizeEvent(pre);
00038     return;
00039 }
00040 
00041 void
00042 IIIMLineEdit::moveEvent(
00043     QMoveEvent *pme
00044 )
00045 {
00046     emit ref_changed();
00047     QLineEdit::moveEvent(pme);
00048     return;
00049 }
00050 
00051 IIIMLookupChoiceWidget*
00052 IIIMLineEdit::get_lookup_choice()
00053 {
00054     if (plw) return plw;
00055 
00056 #if 1
00057     plw = new IIIMLookupChoiceWidget(get_context(), NULL, NULL,
00058                                  Qt::WStyle_Customize
00059                                  | Qt::WX11BypassWM
00060     );
00061 #else
00062     plw = new IIIMLookupChoiceWidget(get_context(), NULL, NULL);
00063 #endif
00064 
00065     return plw;
00066 }
00067 
00068 IIIMCF_language
00069 IIIMLineEdit::get_language(
00070     const char *id
00071 )
00072 {
00073     IIIMF_status st;
00074     IIIMCF_language *plangs;
00075     const char *id2;
00076     int i, n;
00077     st = iiimcf_get_supported_languages(handle, &n, &plangs) ;
00078     if (st != IIIMF_STATUS_SUCCESS) return NULL;
00079     for (i = 0; i < n; i++) {
00080        iiimcf_get_language_id(plangs[i], &id2);
00081        if (strcmp(id2, id) == 0) return plangs[i];
00082     }
00083     return NULL;
00084 }
00085 
00086 void
00087 IIIMLineEdit::toggle_im(
00088     IIIMCF_context c,
00089     bool flag
00090 )
00091 {
00092     IIIMF_status st;
00093     IIIMCF_event ev;
00094 
00095     st = iiimcf_create_trigger_notify_event(flag, &ev);
00096     if (st != IIIMF_STATUS_SUCCESS) return;
00097     iiimcf_forward_event(c, ev);
00098     update_UI();
00099 }
00100 
00101 IIIMCF_context
00102 IIIMLineEdit::get_context()
00103 {
00104     if (!iiimcf_flag) {
00105        IIIMF_status st;
00106        IIIMCF_language lang;
00107        IIIMCF_attr attr = IIIMCF_ATTR_NULL;
00108 
00109        ctx = NULL;
00110        lang = get_language("ja");
00111        if (lang) {
00112            if (iiimcf_create_attr(&attr) != IIIMF_STATUS_SUCCESS) return ctx;
00113            if (iiimcf_attr_put_ptr_value(attr, IIIMCF_ATTR_INPUT_LANGUAGE, lang)
00114               != IIIMF_STATUS_SUCCESS) return ctx;
00115            st = iiimcf_create_context(handle, attr, &ctx);
00116            iiimcf_destroy_attr(attr);
00117        } else {
00118            st = iiimcf_create_context(handle, IIIMCF_ATTR_NULL, &ctx);
00119        }
00120 
00121        if (st == IIIMF_STATUS_SUCCESS) {
00122            iiimcf_flag = 1;
00123            toggle_im(ctx, true);
00124        } else {
00125            ctx = NULL;
00126        }
00127     }
00128     return ctx;
00129 }
00130 
00131 void
00132 IIIMLineEdit::commit_string()
00133 {
00134     IIIMF_status st;
00135     const IIIMP_card16 *pu16;
00136     IIIMCF_text text;
00137     
00138     st = iiimcf_get_committed_text(get_context(), &text);
00139     if (st != IIIMF_STATUS_SUCCESS) return;
00140     st = iiimcf_get_text_utf16string(text, &pu16);
00141     if (st != IIIMF_STATUS_SUCCESS) return;
00142     QString str = QString::fromUcs2(pu16);
00143     insert(str);
00144 }
00145 
00146 void
00147 IIIMLineEdit::deal_with_iiimcf_event()
00148 {
00149     IIIMF_status st;
00150     IIIMCF_event ev;
00151     IIIMCF_event_type et;
00152 
00153     while ((st = iiimcf_get_next_event(get_context(), &ev))
00154           == IIIMF_STATUS_SUCCESS) {
00155        if (iiimcf_get_event_type(ev, &et) == IIIMF_STATUS_SUCCESS) {
00156            if (et == IIIMCF_EVENT_TYPE_KEYEVENT) {
00157               IIIMCF_keyevent kev;
00158               QKeyEvent *pqe;
00159 
00160               if (iiimcf_get_keyevent_value(ev, &kev)
00161                   != IIIMF_STATUS_SUCCESS)
00162                   continue;
00163               if (convert_IIIMCF_keyevent_to_QKeyEvent(&kev, 1, &pqe)
00164                   != IIIMF_STATUS_SUCCESS)
00165                   continue;
00166               QLineEdit::keyPressEvent(pqe);
00167               delete pqe;
00168               if (convert_IIIMCF_keyevent_to_QKeyEvent(&kev, 0, &pqe)
00169                   != IIIMF_STATUS_SUCCESS)
00170                   continue;
00171               QLineEdit::keyReleaseEvent(pqe);
00172               delete pqe;
00173            }
00174        }
00175        iiimcf_ignore_event(ev);
00176     }
00177 }
00178 
00179 void
00180 IIIMLineEdit::update_UI()
00181 {
00182     int flag;
00183     if (iiimcf_is_UIstate_changed(get_context(), &flag)
00184        != IIIMF_STATUS_SUCCESS)
00185        return;
00186 
00187     if (flag & IIIMCF_STATE_PREEDIT_CHANGED) {
00188        int w;
00189        QRect cr = contentsRect();
00190        QFontMetrics fm = fontMetrics();
00191 
00192        w = fm.width(displayText().left(cursorPosition()));
00193        cr.setWidth(w);
00194        get_preedit()->move(cr.topRight());
00195        get_preedit()->update_im();
00196     }
00197     if (flag & IIIMCF_STATE_STATUS_CHANGED) {
00198        get_status()->update_im();
00199     }
00200     if (flag & IIIMCF_STATE_LOOKUP_CHOICE_CHANGED) {
00201        get_lookup_choice()->update_im();
00202     }
00203     if (flag & IIIMCF_STATE_COMMIT_REQUIRED) {
00204        commit_string();
00205     }
00206     deal_with_iiimcf_event();
00207 }
00208 
00209 void
00210 IIIMLineEdit::keyPressEvent(
00211     QKeyEvent *e
00212 )
00213 {
00214     IIIMCF_event ev;
00215     IIIMCF_keyevent kev;
00216 
00217     if (convert_QKeyEvent_to_IIIMCF_keyevent(e, &kev)
00218        != IIIMF_STATUS_SUCCESS)
00219        return QLineEdit::keyPressEvent(e);
00220 
00221     if (iiimcf_create_keyevent(&kev, &ev) != IIIMF_STATUS_SUCCESS)
00222        return;
00223 
00224     if (iiimcf_forward_event(get_context(), ev) != IIIMF_STATUS_SUCCESS)
00225        return QLineEdit::keyPressEvent(e);
00226 
00227     update_UI();
00228 
00229     return;
00230 }
00231 
00232 IIIMLineEdit::IIIMLineEdit(
00233     IIIMCF_handle h,
00234     QWidget *parent,
00235     const char *name
00236 )
00237     : QLineEdit(parent, name)
00238 {
00239     handle = h;
00240     QString text = QString::fromUtf8("漢字");
00241     setText(text);
00242     ppw = NULL;
00243     psw = NULL;
00244     plw = NULL;
00245     iiimcf_flag = 0;
00246 }
00247 
00248 IIIMLineEdit::~IIIMLineEdit()
00249 {
00250     if (ppw) delete ppw;
00251     if (psw) delete psw;
00252     if (plw) delete plw;
00253     if (iiimcf_flag) iiimcf_destroy_context(ctx);
00254 }
00255 
00256 int
00257 main(
00258     int argc,
00259     char **argv
00260 )
00261 {
00262     IIIMCF_attr attr;
00263     IIIMCF_handle h;
00264     int r;
00265     QApplication app(argc, argv);
00266     iiimcf_initialize(IIIMCF_ATTR_NULL);
00267     iiimcf_create_attr(&attr);
00268     iiimcf_attr_put_string_value(attr, IIIMCF_ATTR_CLIENT_TYPE,
00269                              "Qt based sample IIIM client");
00270     if (iiimcf_create_handle(attr, &h) != IIIMF_STATUS_SUCCESS) {
00271        return 255;
00272     }
00273     iiimcf_destroy_attr(attr);
00274 
00275     IIIMLineEdit *lineedit = new IIIMLineEdit(h);
00276     app.setMainWidget(lineedit);
00277     lineedit->show();
00278     r = app.exec();
00279     delete lineedit;
00280     iiimcf_destroy_handle(h);
00281     iiimcf_finalize();
00282     return r;
00283 }
00284 
00285 /* Local Variables: */
00286 /* c-file-style: "iiim-project" */
00287 /* End: */