1 /*
2 
3   Copyright (c) 2004-2005 Kazuki Ohta <mover@hct.zaq.ne.jp>
4   Copyright (c) 2005-2013 uim Project https://github.com/uim/uim
5 
6   All rights reserved.
7 
8   Redistribution and use in source and binary forms, with or without
9   modification, are permitted provided that the following conditions
10   are met:
11 
12   1. Redistributions of source code must retain the above copyright
13      notice, this list of conditions and the following disclaimer.
14   2. Redistributions in binary form must reproduce the above copyright
15      notice, this list of conditions and the following disclaimer in the
16      documentation and/or other materials provided with the distribution.
17   3. Neither the name of authors nor the names of its contributors
18      may be used to endorse or promote products derived from this software
19      without specific prior written permission.
20 
21   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
22   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24   ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
25   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31   SUCH DAMAGE.
32 
33 */
34 
35 #ifndef UIM_QT4_IMMODULE_QUIMINPUTCONTEXT_H
36 #define UIM_QT4_IMMODULE_QUIMINPUTCONTEXT_H
37 
38 #include <QtCore/QtGlobal>
39 #include <QtGui/QInputContext>
40 
41 #ifdef Q_WS_X11
42 #define UIM_QT_USE_JAPANESE_KANA_KEYBOARD_HACK 1
43 #endif
44 
45 #include <uim/uim.h>
46 #include <uim/uim-helper.h>
47 
48 #include "util.h" // for WORKAROUND_BROKEN_RESET_IN_QT4
49 
50 class QEvent;
51 
52 class CandidateWindowProxy;
53 class CaretStateIndicator;
54 class QUimHelperManager;
55 class QUimTextUtil;
56 #ifdef Q_WS_X11
57 typedef struct _DefTree DefTree;
58 class Compose;
59 #endif
60 
61 struct PreeditSegment
62 {
PreeditSegmentPreeditSegment63     PreeditSegment( int attr, const QString &str )
64     {
65         this->attr = attr;
66         this->str = str;
67     }
68 
69     int attr;
70     QString str;
71 };
72 
73 class QUimInputContext : public QInputContext
74 {
75     Q_OBJECT
76 public:
77     explicit QUimInputContext( const char *imname = 0 );
78     ~QUimInputContext();
79 
80     virtual QString identifierName();
81     virtual QString language();
82 
83 #ifdef Q_WS_X11
84     virtual bool x11FilterEvent( QWidget *keywidget, XEvent *event );
85 #endif
86     virtual bool filterEvent( const QEvent *event );
87     virtual void reset();
88     virtual void update();
89     virtual void mouseHandler( int x, QMouseEvent *event );
isComposing()90     virtual bool isComposing() const { return m_isComposing; }
91     virtual void setFocusWidget( QWidget *w );
92 
uimContext()93     uim_context uimContext() { return m_uc; }
94 
95     static QUimInputContext *focusedIC();
96 
97     void commitString( const QString& str );
98 
99     void updatePosition();
100     void updateStyle();
101 
textUtil()102     QUimTextUtil *textUtil() { return mTextUtil; }
103 
104     QString getPreeditString();
105     int getPreeditCursorPosition();
106 
107     void saveContext();
108     void restoreContext();
109 
110     void updateIndicator( const QString &str );
111 
setCandwinActive()112     void setCandwinActive() { candwinIsActive = true; }
113 
114 protected:
115     uim_context createUimContext( const char *imname );
116     void createCandidateWindow();
117     bool isPreeditPreservationEnabled();  // not a QInputContext func
118     virtual void setFocus();    // not a QInputContext func
119     virtual void unsetFocus();  // not a QInputContext func
120 
121 private:
122     int getPreeditSelectionLength();
123     QList<QInputMethodEvent::Attribute> getPreeditAttrs();
124 #ifdef WORKAROUND_BROKEN_RESET_IN_QT4
125     void savePreedit();
126     void restorePreedit();
127 #endif
128 
129     /* callbacks for uim */
130     static void commit_cb( void *ptr, const char *str );
131     //preedit
132     static void clear_cb( void *ptr );
133     static void pushback_cb( void *ptr, int attr, const char *str );
134     static void update_cb( void *ptr );
135     //candidate
136     static void cand_activate_cb( void *ptr, int nr, int displayLimit );
137     static void cand_select_cb( void *ptr, int index );
138     static void cand_shift_page_cb( void* ptr, int index );
139     static void cand_deactivate_cb( void *ptr );
140     //imsw
141     static void switch_app_global_im_cb( void *ptr, const char *str );
142     static void switch_system_global_im_cb( void *ptr, const char *str );
143     //delay
144     static void cand_activate_with_delay_cb( void *ptr, int delay );
145     /* real functions for callbacks (correspond order) */
146     //preedit
147     void clearPreedit();
148     void pushbackPreeditString( int attr, const QString& str );
149     void updatePreedit();
150     //imsw
151     void switch_app_global_im( const char *str );
152     void switch_system_global_im( const char *str );
153 
154 #ifdef Q_WS_X11
155     // for X11 Compose
156     static DefTree *mTreeTop;
157     static void create_compose_tree( void );
158     static int get_compose_filename( char *filename, size_t len );
159     static int TransFileName( char *transname, const char *name, size_t len );
160     static void ParseComposeStringFile( FILE *fp );
161     static void FreeComposeTree( DefTree *top );
162     static int parse_compose_line( FILE *fp, char **tokenbuf, size_t *buflen );
163     static int get_mb_string( char *buf, unsigned int ks );
164     static const char *get_encoding( void );
165     static int get_lang_region( char *lang_region, size_t len );
166 
167     Compose *mCompose;
168 #endif
169     QUimTextUtil *mTextUtil;
170 
171     CaretStateIndicator *m_indicator;
172 
173 protected:
174     bool candwinIsActive;
175     bool m_isComposing;
176 
177     uim_context m_uc;
178     QList<PreeditSegment> psegs;
179     CandidateWindowProxy *proxy;
180 
181 #ifdef WORKAROUND_BROKEN_RESET_IN_QT4
182     QHash<QWidget*, uim_context> m_ucHash;
183     QHash<QWidget*, QList<PreeditSegment> > psegsHash;
184     QHash<QWidget*, CandidateWindowProxy*> proxyHash;
185     QHash<QWidget*, bool> visibleHash;
186 
187     QWidget *focusedWidget;
188 #endif
189 
190     static QUimHelperManager *m_HelperManager;
191 };
192 
193 #endif /* Not def: UIM_QT4_IMMODULE_QUIMINPUTCONTEXT_H */
194