Back to index

im-sdk  12.3.91
iiimqccontext.cpp
Go to the documentation of this file.
00001 /*
00002   iiimqccontext.cpp
00003   Copyright (C) 2003 Free Standards Group
00004 
00005   Permission is hereby granted, free of charge, to any person obtaining a
00006   copy of this software and associated documentation files (the
00007   "Software"), to deal in the Software without restriction, including
00008   without limitation the rights to use, copy, modify, merge, publish,
00009   distribute, sublicense, and/or sell copies of the Software, and to
00010   permit persons to whom the Software is furnished to do so, subject to
00011   the following conditions: The above copyright notice and this
00012   permission notice shall be included in all copies or substantial
00013   portions of the Software.
00014 
00015   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018   IN NO EVENT SHALL OPENI18N WG OR FREE STANDARDS GROUP. BE LIABLE
00019   FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020   CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021   THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022   ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024   Except as contained in this notice, the names of OpenI18N WG and/or
00025   Free Standards Group shall not be used in advertising or otherwise to
00026   promote the sale, use or other dealings in this Software without prior
00027   written authorization from OpenI18N WG and/or Free Standards Group
00028   as applicable.
00029 
00030   Author: ILGYOUNG PARK <Karl.Park@Sun.COM>
00031          TuBo <Gavin.Tu@Sun.COM>
00032 
00033 */
00034 
00035 #include <qwidget.h>
00036 #include <qinputcontext.h>
00037 #include <qapplication.h>
00038 #include <qvaluelist.h>
00039 #include <qtextcodec.h>
00040 #include <iiimcf.h>
00041 
00042 #include "iiimqccontext.h"
00043 #include "keymapper.h"
00044 #include "lookup.h"
00045 #include "debug.h"
00046 
00047 
00048 #include "imaux.h"
00049 
00050 
00051 class IIIMInputContextPrivate
00052 {
00053 public:
00054   typedef IIIMInputContext::IIIMICLIST IIIMICLIST;
00055   typedef IIIMInputContext::IIIMICLISTITR IIIMICLISTITR;
00056   typedef IIIMInputContext::IIIMInputContextID IIIMInputContextID;
00057   IIIMInputContextPrivate (IIIMInputContext *iiimqc);
00058   bool getLookupChoiceItems (QStringList &slist,
00059                           int *psize,
00060                           int *pfirst_index,
00061                           int *plast_index,
00062                           int *pcurrent_index);
00063   bool getLookupTitle(QString &title);
00064 
00065 private:
00066   IIIMInputContext *_iiimqc;
00067 };
00068 
00069 IIIMInputContextPrivate::IIIMInputContextPrivate (IIIMInputContext *iiimqc)
00070 {
00071   if (iiimqc)
00072     _iiimqc = iiimqc;
00073   else
00074     _iiimqc = 0;
00075 }
00076 
00077 
00078 bool IIIMInputContextPrivate::getLookupChoiceItems (QStringList &qstrlist,
00079                                           int *psize,
00080                                           int *pfirst_index,
00081                                           int *plast_index,
00082                                           int *pcurrent_index
00083                                           )
00084 {
00085   IIIMCF_context context;
00086   IIIMCF_lookup_choice lookup_choice;
00087   IIIMF_status st;
00088   QString *qlabel;
00089   QString *qcandidate;
00090  
00091   int size = 0;
00092   int index_of_first_candidate = 0;
00093   int index_of_last_candidate = 0;
00094   int index_of_current_candidate = 0;
00095 
00096   IIIMCF_text acandidate;
00097   IIIMCF_text alabel;
00098   int flag;
00099   IIIMICLISTITR itr = _iiimqc->_curic;
00100   context = (*itr)._c;
00101  
00102   if(!context)
00103        return FALSE;
00104  
00105   st = iiimcf_get_lookup_choice (context, &lookup_choice);
00106   if(st != IIIMF_STATUS_SUCCESS)
00107   {
00108        DEBUG_DO(qDebug("**IIIMQCF: iiimcf_get_lookup_choice(): error\n"));    
00109        return FALSE;
00110   }    
00111   
00112   if(!lookup_choice)
00113   {
00114        DEBUG_DO(qDebug("**IIIMQCF: iiimcf_get_lookup_choice(): error\n"));    
00115        return FALSE;
00116   }
00117      
00118   st = iiimcf_get_lookup_choice_size (lookup_choice,
00119                              &size,
00120                              &index_of_first_candidate,
00121                              &index_of_last_candidate,
00122                              &index_of_current_candidate);
00123   if( st != IIIMF_STATUS_SUCCESS)
00124   {
00125        DEBUG_DO(qDebug("**IIIMQCF: iiimcf_get_lookup_choice_size(): error\n"));
00126        return FALSE;
00127   }     
00128   
00129   if (psize)
00130     *psize = size;
00131   if (pfirst_index)
00132     *pfirst_index = index_of_first_candidate;
00133   if (plast_index)
00134     *plast_index = index_of_last_candidate;
00135   if (pcurrent_index)
00136     *pcurrent_index = index_of_current_candidate;
00137   
00138   for (int idx = 0; idx < size; idx++){
00139     const IIIMP_card16 *u16text;
00140     const IIIMP_card16 *u16label;
00141     int thelength; int labellength;
00142 
00143     st = iiimcf_get_lookup_choice_item (lookup_choice, idx,
00144                                &acandidate, &alabel,
00145                                &flag);
00146     if( st != IIIMF_STATUS_SUCCESS )
00147     {
00148        DEBUG_DO(qDebug("**IIIMQCF: iiimcf_get_lookup_choice_item(): error, idx is %d\n", idx));
00149        continue;
00150     }  
00151 
00152     st = iiimcf_get_text_utf16string (acandidate, &u16text);
00153     if (st != IIIMF_STATUS_SUCCESS)
00154       continue;
00155   
00156     st = iiimcf_get_text_length (acandidate, &thelength);
00157     if (st != IIIMF_STATUS_SUCCESS)
00158       continue;
00159 
00160         
00161     st = iiimcf_get_text_utf16string (alabel, &u16label);
00162     if (st != IIIMF_STATUS_SUCCESS)
00163       continue;
00164   
00165     st = iiimcf_get_text_length (alabel, &labellength);
00166     if (st != IIIMF_STATUS_SUCCESS)
00167       continue;
00168   
00169     qlabel = new QString((const QChar *)u16label, labellength);
00170     qcandidate = new QString((const QChar *)u16text, thelength);
00171     
00172     DEBUG_DO(qDebug("**IIIMQCF: label: %s.\n", (const char*)qlabel->utf8())); 
00173     DEBUG_DO(qDebug ("**IIIMQCF: candidate:%s.\n", (const char *)qcandidate->utf8()));
00174     if(qlabel->isEmpty() || *qlabel == " ")
00175        qstrlist.append(*qcandidate);
00176      else
00177        qstrlist.append (*qlabel + " " +  *qcandidate);
00178    
00179     delete qlabel;
00180     delete qcandidate;                                  
00181   }
00182 
00183   return TRUE;
00184 }
00185 
00186 bool IIIMInputContextPrivate::getLookupTitle (QString &title)
00187 {
00188   IIIMF_status st;
00189   IIIMCF_text text;
00190   const IIIMP_card16 *u16text;
00191   int thelength;
00192   IIIMCF_lookup_choice lookup_choice;
00193   IIIMCF_context context;
00194   IIIMICLISTITR itr = _iiimqc->_curic;
00195  
00196   title = QString::null;
00197 
00198   context = (*itr)._c;
00199   if(!context)
00200        return FALSE;
00201 
00202   st = iiimcf_get_lookup_choice (context, &lookup_choice);
00203   if(st != IIIMF_STATUS_SUCCESS)
00204   {
00205         DEBUG_DO(qDebug("**IIIMQCF: iiimcf_get_lookup_choice(): error\n"));
00206         return FALSE;
00207   }
00208 
00209   if(!lookup_choice)
00210   {
00211         DEBUG_DO(qDebug("**IIIMQCF: iiimcf_get_lookup_choice(): error\n"));
00212         return FALSE;
00213   }
00214   
00215 
00216   st = iiimcf_get_lookup_choice_title (lookup_choice, &text);
00217   if (st != IIIMF_STATUS_SUCCESS)
00218   {
00219       DEBUG_DO(qDebug("**IIIMQCF: Failed to get lookup choice title"));
00220       return FALSE;
00221   }
00222   
00223   st = iiimcf_get_text_utf16string (text, &u16text);
00224   if (st != IIIMF_STATUS_SUCCESS)
00225      return FALSE;
00226 
00227   st = iiimcf_get_text_length (text, &thelength);
00228   if (st != IIIMF_STATUS_SUCCESS)
00229      return FALSE;
00230  
00231    QString *tmp = new QString((const QChar *)u16text, thelength);
00232    title.append(*tmp);
00233    delete tmp;
00234  
00235     return TRUE;
00236 }
00237 
00238 
00239 
00240 
00241 IIIMInputContext::IIIMInputContext ()
00242 {
00243   DEBUG_DO(qDebug("**IIIMQCF: IIIMInputcontext()"));
00244   d = new IIIMInputContextPrivate (this);
00245   iiimcfIsInitialized = FALSE;
00246   _h = 0;
00247   keymapper = new KeyMapper();
00248 
00249   iiimlookup = new IIIMQCFLookup(0, this);
00250   if (!iiimqcf_init_iiim ()){
00251     DEBUG_DO(qDebug ("**IIIQCF: iiimcf libarary initialization failed!!!\n"));
00252   }
00253 }
00254 
00255 IIIMInputContext::~IIIMInputContext ()
00256 {
00257   DEBUG_DO(qDebug("**IIIMQCF: ~IIIMInputcontext()"));
00258   if (iiimlookup)
00259     delete iiimlookup;
00260   if(keymapper)
00261     delete keymapper;
00262   if(d)
00263     delete d;
00264 }
00265 
00266 
00267 QString IIIMInputContext::identifierName ()
00268 {
00269   return "iiimqcf";
00270 }
00271 
00272 
00273 QString IIIMInputContext::language ()
00274 {
00275        QString locale = QTextCodec::locale();
00276        QString language;
00277         if ( locale.startsWith( "zh" ) ) {
00278             // Chinese language should be formed as "zh_CN", "zh_TW", "zh_HK"
00279             language = locale.left( 5 );
00280         } else {
00281             // other languages should be two-letter ISO 639 language code
00282             language = locale.left( 2 );
00283         }
00284        return language;
00285 
00286 }
00287 
00288 
00289 void IIIMInputContext::reset ()
00290 {
00291    QInputContext::reset();
00292 }
00293 
00294 
00295 
00296 
00301 void IIIMInputContext::setFocus ()
00302 {
00303   /* Intentionally not calling QInputContext::focusWidget()
00304    in accordance with the commenet in qinputcontext.h
00305   */
00306   IIIMICLISTITR itr;
00307   int pos;
00308   QString qpreedit;
00309   IIIMF_status st; 
00310   IIIMCF_event ev;  
00311 
00312  
00313   QWidget *keywidget = qApp->focusWidget();
00314   
00315   if(!keywidget)     
00316        return;
00317 
00318   DEBUG_DO(qDebug ("**IIIMQCF: setFocus() for widget(name: %s, WId : %d)",
00319          qApp->focusWidget ()->name (),
00320          (int)qApp->focusWidget ()->winId ()
00321          ));
00322 
00323   itr = get_iiimcf_session_context (keywidget);
00324   set_current_iiimcf_session_context (keywidget);
00325  
00326   cur_context = (*itr)._c; 
00327  
00328   st = iiimcf_create_seticfocus_event (&ev);
00329   if (st != IIIMF_STATUS_SUCCESS)
00330   { 
00331      DEBUG_DO(qDebug("**IIIMQCF: iiimcf_create_seticfocus_event(): error"));
00332      return;
00333    }
00334 
00335   if (forwardEvent (keywidget, ev, &st))
00336   {
00337     DEBUG_DO(qDebug("**IIIMQCF: forward iiim_seticfocus event, and return sucessfully"));
00338     iiimqcf_event_dispatch (keywidget);
00339    }
00340   
00341   if (get_preedit_data (keywidget, qpreedit, &pos)){
00342     if(!qpreedit)
00343        return;
00344     DEBUG_DO(qDebug("**IIIMQCF: setFocus() preedit string is  %s\n", qpreedit.ascii()));
00345     sendIMEvent (QEvent::IMCompose, qpreedit, pos, qpreedit.length());    
00346   }
00347 
00348    iiimlookup->showLookupWindow (keywidget);
00349    IIim_aux_set_icfocus(this);
00350 }
00351 
00352 
00359 void IIIMInputContext::unsetFocus ()
00360 {
00361   QWidget *w = qApp->focusWidget ();
00362   if (w)
00363     DEBUG_DO(qDebug ("**IIIMQCF: unsetFocus() for widiget \"%s\"",
00364            qApp->focusWidget()->name()));
00365 
00366   iiimlookup->hideLookupWindow ();
00367   IIim_aux_unset_icfocus(this);
00368 }
00369 
00370 
00371 void IIIMInputContext::setMicroFocus( int x, int y, int w, int h, QFont *f)
00372 {
00373   if(!f)
00374        DEBUG_DO(qDebug("**IIIMQCF: setMicroFocus(): font is null"));
00375   DEBUG_DO(qDebug ("**IIIMQCF: setMicroFocus():Cursor position (%d, %d), width is %d, height is %d\n", x, y, w, h));
00376   
00377   cursor_h = h;
00378 
00379   QWidget *widget = qApp->focusWidget();
00380   if ( widget ) {
00381         QPoint p( x, y );
00382         p = widget->mapFromGlobal( p );
00383        cursor_x = p.x();
00384        cursor_y = p.y();
00385        DEBUG_DO(qDebug ("**IIIMQCF: setMicroFocus():local  Cursor position (%d, %d), \
00386               width is %d, height is %d\n", cursor_x, cursor_y, w, h));
00387   }
00388   else
00389   {
00390        cursor_x = 0;
00391         cursor_y = 0;
00392   }
00393 
00394   iiimlookup->layoutWindow(x, y, w, h);
00395 }
00396 
00397 
00398 void IIIMInputContext::mouseHandler( int x, QEvent::Type type,
00399                             Qt::ButtonState button, Qt::ButtonState state )
00400 {
00401   DEBUG_DO(qDebug("**IIIMQCF: mouseHandler(): x is %d, type is %d, button state is %d,%d\n",\
00402                              x, type, button, state));
00403  /* if ( type == QEvent::MouseButtonPress ||
00404        type == QEvent::MouseButtonDblClick )
00405     reset();
00406 */
00407 }
00408 
00409 
00410 bool IIIMInputContext::iiimqcf_event_dispatch (QWidget *w)
00411 {
00412   IIIMCF_context c;
00413   IIIMF_status st;
00414   IIIMCF_event ev;
00415   IIIMCF_event_type et;
00416   int conversion_mode = FALSE;
00417   
00418   QString lookup_title;
00419   QKeyEvent *pqkey;
00420   QString qpreedit;
00421   QString qcommitted;
00422   QStringList candidates;
00423   int size, first_index, last_index, current_index;
00424  
00425   if (_curic == 0)
00426     return FALSE;
00427   
00428 
00429   c = (*_curic)._c; 
00430 
00431   if(!c) 
00432   {
00433      DEBUG_DO(qDebug("**IIIMQCF: iiimqcf_event_dispatch: get current IC error\n")); 
00434      return FALSE ;
00435   }
00436  
00437   while ( (st = iiimcf_get_next_event (c, &ev)) == IIIMF_STATUS_SUCCESS){
00438     st = iiimcf_get_event_type (ev, &et);
00439     if (st != IIIMF_STATUS_SUCCESS)
00440       continue;
00441 
00442     switch (et){
00443     case IIIMCF_EVENT_TYPE_KEYEVENT:
00444       DEBUG_DO(qDebug ("**IIIMQCF: iiimqcf_event_dispatch():IIIMCF_EVENT_TYPE_KEYEVENT "));
00445       return FALSE;
00446       /*break;*/
00447       
00448     case IIIMCF_EVENT_TYPE_TRIGGER_NOTIFY:
00449       st = iiimcf_get_current_conversion_mode (c, &conversion_mode);
00450 
00451       if (conversion_mode){
00452        DEBUG_DO(qDebug ("**IIIMQCF: iiimqcf_event_dispatch() : trigger on"));
00453        sendIMEvent (QEvent::IMStart); 
00454       } else {
00455        DEBUG_DO(qDebug ("**IIIMQCF: iiimqcf_event_dispatch() : trigger off"));
00456        iiimlookup->clearLookupChoices ();
00457        iiimlookup->hideLookupWindow ();
00458        
00459        sendIMEvent (QEvent::IMEnd);
00460       }
00461 
00462       break;
00463       
00464     case IIIMCF_EVENT_TYPE_UI_PREEDIT_START:
00465       DEBUG_DO(qDebug ("**IIIMQCF: iiimqcf_event_dispatch() : preedit start"));
00466       sendIMEvent (QEvent::IMStart); 
00467       break;
00468       
00469     case IIIMCF_EVENT_TYPE_UI_PREEDIT_CHANGE:
00470       int pos;
00471 
00472       DEBUG_DO(qDebug ("**IIIMQCF: iiimqcf_event_dispatch():IIIMCF_EVENT_TYPE_UI_PREEDIT_CHANGE"));
00473       
00474       if (!get_preedit_data (w, qpreedit, &pos))
00475       {
00476         sendIMEvent(QEvent::IMEnd);
00477        break;
00478       }
00479       if(!qpreedit)
00480         break;
00481 
00482       if( !this->isComposing()  )
00483          sendIMEvent (QEvent::IMStart); 
00484       sendIMEvent (QEvent::IMCompose, qpreedit, pos, qpreedit.length());
00485       qpreedit = QString::null;
00486       break;
00487 
00488     case IIIMCF_EVENT_TYPE_UI_PREEDIT_DONE:
00489       DEBUG_DO(qDebug ("**IIIMQCF: iiimqcf_event_dispatch():IIIMCF_EVENT_TYPE_UI_PREEDIT_DONE"));
00490       get_committed_text (w, qcommitted);
00491       sendIMEvent(QEvent::IMEnd);
00492       break;
00493       
00494     case IIIMCF_EVENT_TYPE_UI_COMMIT:
00495       DEBUG_DO(qDebug ("**IIIMQCF: iiimqcf_event_dispatch():IIIMCF_EVENT_TYPE_UI_COMMIT"));
00496       if (!get_committed_text (w, qcommitted))
00497        break;
00498      
00499       if( !this->isComposing()  )
00500          sendIMEvent (QEvent::IMStart); /*for user-defined preedit/lookup */ 
00501       sendIMEvent (QEvent::IMEnd, qcommitted,qcommitted.length ());
00502 
00503       break;
00504 
00505       
00506     case IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_START:
00507       DEBUG_DO(qDebug ("**IIIMQCF: iiimqcf_event_dispatch():IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_START"));
00508       d->getLookupChoiceItems (candidates,
00509                             &size, &first_index,
00510                             &last_index, &current_index);
00511      
00512       if(d->getLookupTitle(lookup_title))
00513           DEBUG_DO(qDebug("**IIIMQCF: lookup_title is %s\n", lookup_title.ascii())); 
00514       else
00515       {       
00516           DEBUG_DO(qDebug("**IIIMQCF: can not get lookup_title\n"));
00517           lookup_title.append("Input method");
00518       }
00519       iiimlookup->setLookupTitle(lookup_title);
00520       iiimlookup->setLookupChoices (candidates,
00521                                 size, first_index,
00522                                 last_index, current_index);
00523       DEBUG_DO(qDebug ("**IIIMQCF: To OPEN LOOKUP WINDOW FROM %s", (*_curic)._w->name()));
00524       
00525       size = 0;
00526       first_index = 0;
00527       last_index = 0;
00528       current_index = 0;
00529       candidates.clear();
00530  
00531       iiimlookup->showLookupWindow ((*_curic)._w);
00532       
00533       break;
00534       
00535     case IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_CHANGE:
00536       DEBUG_DO(qDebug ("**IIIMQCF: iiimqcf_event_dispatch():IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_CHANGE"));
00537 
00538       d->getLookupChoiceItems (candidates,
00539                             &size, &first_index,
00540                             &last_index, &current_index);
00541       
00542       iiimlookup->setLookupChoices (candidates,
00543                                 size, first_index,
00544                                 last_index, current_index);
00545       iiimlookup->showLookupWindow ((*_curic)._w);
00546       DEBUG_DO(qDebug("**IIIQCF: IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_CHANGE: end\n "));
00547       break;
00548       
00549     case IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_DONE:
00550       DEBUG_DO(qDebug ("IIIMQCF: iiimqcf_event_dispatch():IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_DONE"));
00551       iiimlookup->clearLookupChoices ();
00552       iiimlookup->hideLookupWindow ();
00553       /*sendIMEvent(QEvent::IMEnd);*/
00554       break;
00555       
00556     case IIIMCF_EVENT_TYPE_AUX_START:
00557          DEBUG_DO (qDebug ("aux_start"));
00558          iiim_aux_start (this, ev);
00559          break;
00560        case IIIMCF_EVENT_TYPE_AUX_DRAW:
00561          DEBUG_DO (qDebug ("aux_draw"));
00562          iiim_aux_draw (this, ev);
00563          break;
00564        case IIIMCF_EVENT_TYPE_AUX_DONE:
00565          DEBUG_DO (qDebug ("aux_done"));
00566          iiim_aux_done (this, ev);
00567          break;
00568        case IIIMCF_EVENT_TYPE_AUX_GETVALUES:
00569          DEBUG_DO (qDebug ("aux_getvalues_reply"));
00570          iiim_aux_getvalues_reply (this, ev);
00571          break;
00572     default:
00573       DEBUG_DO(qDebug ("**IIIMQCF: iiimqcf_event_dispatch():event type 0x%x", et));
00574       break;
00575     }
00576     iiimcf_dispatch_event (c, ev);
00577     iiimcf_ignore_event (ev);    
00578   }
00579   return TRUE;
00580 }
00581 
00582 
00583 bool IIIMInputContext::get_preedit_data (QWidget *w, QString &text, int *cursor)
00584 {
00585   IIIMCF_context c;
00586   IIIMCF_text preedit_text;
00587   IIIMF_status st;
00588   QString *qtext;
00589   
00590   const IIIMP_card16 *u16text;
00591   int length;
00592 
00593   if(!w || !cursor)
00594        return FALSE;
00595 
00596   c = (*_curic)._c;
00597   st = iiimcf_get_preedit_text (c, &preedit_text, cursor);
00598   
00599   if (st != IIIMF_STATUS_SUCCESS)
00600   {
00601     DEBUG_DO(qDebug("**IIIMQCF: iiimcf_get_preedit_text(): error\n"));
00602     return FALSE;
00603   }
00604  
00605 
00606   st = iiimcf_get_text_utf16string (preedit_text, &u16text);
00607   if (st != IIIMF_STATUS_SUCCESS)
00608   {
00609     DEBUG_DO(qDebug("**IIIMQCF: iiimcf_get_text_utf16string(): error\n"));
00610     return FALSE;
00611   }
00612   
00613   iiimcf_get_text_length (preedit_text, &length);
00614   if (st != IIIMF_STATUS_SUCCESS)
00615   {
00616     DEBUG_DO(qDebug("**IIIMQCF: iiimcf_get_text_length(): error\n"));
00617     return FALSE;
00618    }
00619 
00620   if(length == 0)
00621   {
00622        DEBUG_DO(qDebug("**IIIMQCF: get_preedit_data(): length is zero\n "));
00623         return FALSE;
00624   }
00625   qtext = new QString ((const QChar *)u16text, length);
00626   text = *qtext;
00627  
00628   delete qtext;
00629  
00630   DEBUG_DO(qDebug("**IIIMQCF: iiimcf_get_preedit_text(): preedit_text is  %s\n", text.ascii()));  
00631   return TRUE;
00632 }
00633 
00634 bool IIIMInputContext::get_committed_text (QWidget *w, QString &text)
00635 {
00636   IIIMCF_context c;
00637   IIIMCF_text committed_text;
00638   IIIMF_status st;
00639   QString *qtext;
00640   
00641   const IIIMP_card16 *u16text;
00642   int length;
00643 
00644   if(!w)
00645        return FALSE;
00646   
00647   c = (*_curic)._c;
00648   st = iiimcf_get_committed_text (c, &committed_text);
00649   
00650   if (st != IIIMF_STATUS_SUCCESS)
00651     return FALSE;
00652 
00653   st = iiimcf_get_text_utf16string (committed_text, &u16text);
00654   if (st != IIIMF_STATUS_SUCCESS)
00655     return FALSE;
00656   
00657   iiimcf_get_text_length (committed_text, &length);
00658   if (st != IIIMF_STATUS_SUCCESS)
00659     return FALSE;
00660   qtext = new QString ((const QChar *)u16text, length);
00661   text = *qtext;
00662   
00663   return TRUE;
00664 }
00665 
00666 
00667 bool IIIMInputContext::filterEvent (const QEvent *event)
00668 {
00669   IIIMCF_keyevent *pkev;
00670   IIIMCF_event ev;
00671   IIIMF_status st;
00672   QKeyEvent *qkev;
00673   QWidget *keywidget;
00674 
00675   int code;
00676   char ascii;
00677   Qt::ButtonState state;
00678   
00679   /* We are only interested in key event..*/
00680     if ((event->type() != QEvent::KeyRelease) &&
00681       (event->type() != QEvent::KeyPress))
00682     return FALSE;
00683   
00684   /* But for now, let's not care about key release event */
00685   if (event->type() == QEvent::KeyRelease){
00686     /* qDebug ("filterEvent=KeyRelease Event ignoring"); */
00687     return FALSE;
00688   }
00689 
00690 #if 0
00691   /*
00692     It is not likely this code will evaluate to true, becuase
00693     this checking is already done within qapplication_x11.cpp.
00694     see QApplication::x11ProcessEvent( XEvent* event )
00695   */
00696   if (!keywidget->isEnabled () || !keywidget->isInputMethodEnabled ()){
00697     qDebug ("IIIMInputContext(%d) : not yet composing, thus ignoring",
00698              id);
00699     return FALSE;
00700   }
00701 #endif
00702   
00703   qkev = (QKeyEvent *)event;
00704   code = qkev->key();
00705   ascii = qkev->ascii();
00706   state = qkev->state();
00707   
00708   /*  if current status is not Composing, then return FALSE
00709   if (isNotComposing ())
00710   return FALSE;
00711 
00712    ugly test code
00713    see the qnamespace.h in qt library.
00714    I need a mapping table from QT's value to IIIM
00715   */
00716 
00717   if (state == Qt::ControlButton){
00718     if (code == ' ')
00719       DEBUG_DO(qDebug ("**IIIMQCF: control space pressed"));
00720   }
00721   
00722   pkev = get_iiimcf_keyevent_from (qkev);
00723   if (!pkev){
00724     DEBUG_DO(qDebug ("**IIIMQCF: Error creating IIIMCF_keyevent object"));
00725     goto commit_this_event; 
00726   }
00727   
00728   st = iiimcf_create_keyevent (pkev, &ev);
00729   if (st != IIIMF_STATUS_SUCCESS){
00730     DEBUG_DO(qDebug ("**IIIMQCF: Error creating IIIMCF_keyevent object"));
00731     goto commit_this_event;
00732   }
00733   /* delete pkev */
00734   delete pkev;
00735   
00736   /* FIX ME */
00737   keywidget = qApp->focusWidget();
00738   
00739   if(!keywidget)
00740        return FALSE;
00741   DEBUG_DO(qDebug("**IIIMQCF: filterEvent(), get focusWidget's name is %s, id is %d",\
00742                keywidget->name(), (int)keywidget->winId()));  
00743 
00744   if (forwardEvent (keywidget, ev, &st)){/*send messages to IIIMSF */
00745     if(iiimqcf_event_dispatch (keywidget))
00746        return TRUE;
00747     else
00748         return FALSE; 
00749   }
00750   if(st != IIIMF_STATUS_EVENT_NOT_FORWARDED && st != IIIMF_STATUS_IC_INVALID)
00751   {
00752         
00753         DEBUG_DO(qDebug("**IIIMQCF: status is not IIIMF_STATUS_EVENT_NOT_FORWARDED"));
00754        return FALSE;
00755    }
00756   
00757   
00758   DEBUG_DO(qDebug("**IIIMQCF: status is  IIIMF_STATUS_EVENT_NOT_FORWARDED"));
00759   commit_this_event:
00760        /* TODO */
00761        return FALSE;
00762 
00763 }
00764 
00765 
00766 IIIMCF_handle IIIMInputContext::iiimqcf_init_iiim (void)
00767 {
00768   IIIMF_status st;
00769   IIIMCF_attr attr;
00770 
00771   if (iiimcfIsInitialized){
00772     DEBUG_DO(qDebug ("**IIIMQCF: iiimqcf_init_iiim : Handle alread exists, returning"));
00773     return _h;
00774   }
00775   
00776   st = iiimcf_initialize (IIIMCF_ATTR_NULL);
00777   if (st != IIIMF_STATUS_SUCCESS){
00778     return 0;
00779   }
00780 
00781   st = iiimcf_create_attr (&attr);
00782   if (st != IIIMF_STATUS_SUCCESS){
00783     return 0;
00784   }
00785   st = iiimcf_attr_put_string_value (attr,
00786                                  IIIMCF_ATTR_CLIENT_TYPE,
00787                                  "QT IIIMCF Module");
00788   if (st != IIIMF_STATUS_SUCCESS){
00789     return 0;
00790   }
00791   
00792   DEBUG_DO(qDebug ("**IIIMQCF: iiimqcf_init_iiim : Creating IIIMCF handle"));
00793 #if 0
00794   int i = 1;
00795   while (i);
00796 #endif
00797   st = iiimcf_create_handle (attr, &_h);
00798   
00799   if (st != IIIMF_STATUS_SUCCESS){
00800     return 0;
00801   }
00802   
00803   iiim_setup_aux_object(_h);
00804   aux = NULL;
00805  
00806   st = iiimcf_destroy_attr (attr);
00807   iiimcfIsInitialized = TRUE;
00808   return _h;
00809 }
00810 
00811 
00812 IIIMCF_keyevent * IIIMInputContext::get_iiimcf_keyevent_from (QKeyEvent *kev)
00813 {
00814   IIIMCF_keyevent *pkev = new IIIMCF_keyevent;
00815   
00816   if (pkev == 0){
00817     DEBUG_DO(qDebug ("**IIIMQCF: get_iiimcf_keyevent_from() failed"));
00818     return 0;
00819   }
00820   
00821   pkev->keycode = keymapper->q2i(kev->key());
00822   pkev->keychar = kev->ascii();
00823   pkev->modifier = keymapper->modifier (kev->state());
00824   
00825   DEBUG_DO(qDebug("**IIIMQCF: get_iiimcf_keyevent_from(): keychar is %c, keycode is %d, modifier is %d\n", \
00826                 pkev->keychar, pkev->keycode, pkev->modifier)); 
00827   return pkev;
00828 }
00829 
00830 void IIIMInputContext::im_context_aux_set_values (IIIMCF_context context, IIIMCF_event ev)
00831 {
00832   IIIMF_status st;  
00833 
00834   QWidget *keywidget = qApp->focusWidget();
00835   if(!keywidget)
00836        return;
00837 
00838   DEBUG_DO(qDebug ("**IIIMQCF: setFocus() for widget(name: %s, WId : %d)",
00839          qApp->focusWidget ()->name (),
00840          (int)qApp->focusWidget ()->winId ()
00841          ));
00842  
00843   if (forwardEvent (keywidget, ev, &st))
00844   {
00845     DEBUG_DO(qDebug("**IIIMQCF: forward im_context_aux_set_values, and return sucessfully"));
00846     iiimqcf_event_dispatch (keywidget);
00847    }
00848   
00849 }
00850 
00851 void IIIMInputContext::im_context_change_conversion_mode(IIIMCF_context context, char *conv_mode)
00852 {
00853   QWidget *keywidget = qApp->focusWidget();
00854   IIIMF_status st;
00855 
00856   if(!keywidget)
00857        return;
00858   DEBUG_DO(qDebug ("**IIIMQCF: setFocus() for widget(name: %s, WId : %d)",
00859          qApp->focusWidget ()->name (),
00860          (int)qApp->focusWidget ()->winId ()
00861          ));
00862 
00863   IIIMCF_event event;
00864   if (conv_mode && !strcmp (conv_mode, "on"))
00865     {
00866       st = iiimcf_create_trigger_notify_event (TRUE, &event);
00867       if (st != IIIMF_STATUS_SUCCESS)
00868        return;
00869     }
00870   else if (conv_mode && !strcmp (conv_mode, "off"))
00871     {
00872       IIIMF_status st;
00873       st = iiimcf_create_trigger_notify_event (FALSE, &event);
00874       if (st != IIIMF_STATUS_SUCCESS)
00875        return;
00876     }
00877   if (forwardEvent (keywidget, event, &st))
00878   {
00879     DEBUG_DO(qDebug("**IIIMQCF: forward , im_context_change_conversion_mode() and return sucessfully"));
00880     iiimqcf_event_dispatch (keywidget);
00881    }
00882 }
00883 
00884 
00885 QWidget* IIIMInputContext::get_focus_widget()
00886 {
00887   return (qApp->focusWidget());
00888 }
00889 
00890 
00891 bool IIIMInputContext::forwardEvent (QWidget *keywidget, IIIMCF_event ev, IIIMF_status *st_ret)
00892 {
00893   IIIMCF_context c;
00894   IIIMICLISTITR itr;
00895   IIIMF_status st;
00896 
00897   if(!keywidget)
00898        return FALSE;
00899     
00900   DEBUG_DO(qDebug ("**IIIMQCF: forwardEvent() on widget %s", keywidget->name ()));
00901 
00902   itr = get_iiimcf_session_context (keywidget);
00903 
00904   c = (*itr)._c;
00905   
00906   if(!c)
00907   {
00908     DEBUG_DO(qDebug("**IIIMQCF: forwardEvent(): get ic error\n"));
00909     if(st_ret)
00910        *st_ret = IIIMF_STATUS_EVENT_NOT_FORWARDED;
00911     return FALSE;
00912   }
00913 
00914   /* Send messages to IIIMSF */
00915   st = iiimcf_forward_event (c, ev);
00916   if(st_ret)
00917      *st_ret = st;
00918 
00919   switch (st){
00920    
00921   case IIIMF_STATUS_SUCCESS:
00922     DEBUG_DO(qDebug("**IIIMQCF: iiimcf_forward_event:IIIMF_STATUS_SUCCESS\n "));
00923     break;
00924   case IIIMF_STATUS_IC_INVALID:
00925     DEBUG_DO(qDebug("**IIIMQCF: iiimcf_forward_event:IIIMF_STATUS_IC_INVALID\n"));
00926   case IIIMF_STATUS_EVENT_NOT_FORWARDED:
00927     DEBUG_DO(qDebug("**IIIMQCF: iiimcf_forward_event: IIIMF_STATUS_EVENT_NOT_FORWARDED\n"));
00928     break;
00929   case IIIMF_STATUS_STREAM_SEND:
00930     DEBUG_DO(qDebug("**IIIMQCF: iiimcf_forward_event:IIIMF_STATUS_STREAM_SEND\n"));
00931   case IIIMF_STATUS_STREAM_RECEIVE:
00932     DEBUG_DO(qDebug("**IIIMQCF: iiimcf_forward_event:IIIMF_STATUS_STREAM_RECEIVE\n"));
00933   case IIIMF_STATUS_CONNECTION_CLOSED:
00934     DEBUG_DO(qDebug("**IIIMQCF: iiimcf_forward_event:IIIMF_STATUS_CONNECTION_CLOSED\n"));
00935     /*et_error_message (context_iiim);*/
00936     iiimcf_ignore_event (ev);
00937     break;
00938   default:
00939     iiimcf_ignore_event (ev);
00940     /*status_window_set_text (context_iiim->status_window, "");*/
00941     break;
00942   }
00943 
00944   return (st == IIIMF_STATUS_SUCCESS);
00945 }
00946 
00947 bool IIIMInputContext::set_current_iiimcf_session_context (QWidget *widget)
00948 {
00949   if(!widget)
00950        return FALSE;
00951   IIIMICLISTITR it = get_iiimcf_session_context (widget);
00952   _curic = it;
00953   if (it != _iclist.end ()){
00954     _curic = it;
00955     return TRUE;
00956   } else
00957     return FALSE;
00958 }
00959 
00960 
00961 IIIMCF_handle IIIMInputContext::get_iiimcf_handle()
00962 {
00963        if(_h)
00964               return _h;
00965        else
00966               return NULL;
00967 
00968 }
00969 
00970 
00971 IIIMInputContext::IIIMICLISTITR
00972 IIIMInputContext::search_iiimiclist_for (QWidget *widget)
00973 {
00974   if(!widget)
00975        return NULL;
00976 
00977   IIIMICLISTITR it = _iclist.begin ();
00978 
00979   DEBUG_DO(qDebug ("**IIIMQCF: Searcing widget %s", widget->name()));
00980   DEBUG_DO(qDebug("**IIIMQCF: length of iclist is %d", _iclist.count()));
00981 
00982   /* Currently, onley keep one IC for one standalone QT application, will support multiple IC within on QT application in
00983      latter. So mark code below currently. 
00984   */
00985   /*while (it != _iclist.end ()){
00986     DEBUG_DO(qDebug ("**IIIMQCF: search_iiimiclist_for ():%s\n", (*it)._w->name()));
00987     if ((*it)._w == widget)
00988       break;
00989     else
00990       it++;
00991   }
00992   if (it == _iclist.end ())
00993     DEBUG_DO(qDebug ("**IIIMQCF: Search Failed"));
00994   else
00995     DEBUG_DO(qDebug ("**IIIMQCF: Search Success"));
00996   */
00997 
00998   return it; // it == _iclist.end ();
00999 }
01000 
01001 
01002 IIIMInputContext::IIIMICLISTITR
01003 IIIMInputContext::create_iiimcf_session_context (QWidget *widget)
01004 {
01005   if(!widget)
01006        return NULL;
01007 
01008   IIIMICLISTITR it = search_iiimiclist_for (widget);
01009   IIIMInputContextID iiimic;
01010   
01011   if ( (it == _iclist.end ()) ||
01012        !(*it)._c ){
01013     IIIMCF_attr attr;
01014     IIIMF_status st;
01015     st = iiimcf_create_attr (&attr);
01016     st = iiimcf_create_context (_h, NULL, &iiimic._c);
01017 
01018     DEBUG_DO(qDebug ("**IIIMQCF: create_iiimcf_session_context():creating IIIMCF_context"));
01019 
01020     iiimic._w = widget;
01021     it = _iclist.prepend (iiimic);
01022     iiimcf_destroy_attr (attr);
01023     
01024   }
01025   return it;
01026 }
01027 
01028 IIIMInputContext::IIIMICLISTITR
01029 IIIMInputContext::get_iiimcf_session_context (QWidget *widget, bool doCreate)
01030 {
01031   if(!widget)
01032        return NULL;
01033 
01034   IIIMICLISTITR it = search_iiimiclist_for (widget);
01035   
01036   if ( (it == _iclist.end () || !(*it)._c) &&  doCreate){
01037     DEBUG_DO(qDebug ("**IIIMQCF: get_iiimcf_session_context() : need to create new IIIMCF_context on %s", widget->name()));
01038     return create_iiimcf_session_context (widget);
01039   }
01040   return it;
01041 }
01042 
01043 
01044 
01045 bool IIIMInputContext::isPreeditRelocationEnabled()
01046 {
01047     return ( language() == "ja" ); 
01048 }
01049 
01050 
01051 bool IIIMInputContext::isPreeditPreservationEnabled()
01052 {
01053      return ( language() == "ja" );
01054 }
01055