summaryrefslogtreecommitdiffstats
path: root/plugins/src/inputmethods/simple/qsimpleinputcontext.cpp
blob: 07d69285d17557ab8efaa0c66499bddb56a2b606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/****************************************************************************
** $Id$
**
** Implementation of QSimpleInputContext class
**
** Copyright (C) 2004 immodule for Qt Project.  All rights reserved.
**
** This file is written to contribute to Trolltech AS under their own
** licence. You may use this file under your Qt license. Following
** description is copied from their original file headers. Contact
** immodule-qt@freedesktop.org if any conditions of this licensing are
** not clear to you.
**
**
** This file is part of the input method module of the Qt GUI Toolkit.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.QPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
** licenses may use this file in accordance with the Qt Commercial License
** Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
**   information about Qt Commercial License Agreements.
** See http://www.trolltech.com/qpl/ for QPL licensing information.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "qsimpleinputcontext.h"

#include <qnamespace.h>
#include <qevent.h>
#include <qglobal.h>
#include <algorithm>

static const int ignoreKeys[] = {
    Qt::Key_Shift,
    Qt::Key_Control,
    Qt::Key_Meta,
    Qt::Key_Alt,
    Qt::Key_CapsLock,
    Qt::Key_Super_L,
    Qt::Key_Super_R,
    Qt::Key_Hyper_L,
    Qt::Key_Hyper_R,
    Qt::Key_Mode_switch
};

static const int composingKeys[] = {
    Qt::Key_Multi_key,
    Qt::Key_Dead_Grave,
    Qt::Key_Dead_Acute,
    Qt::Key_Dead_Circumflex,
    Qt::Key_Dead_Tilde,
    Qt::Key_Dead_Macron,
    Qt::Key_Dead_Breve,
    Qt::Key_Dead_Abovedot,
    Qt::Key_Dead_Diaeresis,
    Qt::Key_Dead_Abovering,
    Qt::Key_Dead_Doubleacute,
    Qt::Key_Dead_Caron,
    Qt::Key_Dead_Cedilla,
    Qt::Key_Dead_Ogonek,
    Qt::Key_Dead_Iota,
    Qt::Key_Dead_Voiced_Sound,
    Qt::Key_Dead_Semivoiced_Sound,
    Qt::Key_Dead_Belowdot,
    Qt::Key_Dead_Hook,
    Qt::Key_Dead_Horn
};

class Cmp
{
public:
    bool operator () (const QComposeTableElement &lhs, const QComposeTableElement &rhs) const {
        for ( size_t i=0; i < QT_KEYSEQUENCE_MAX_LEN; i++ ) {
            if ( lhs.keys[i] < rhs.keys[i] ) return TRUE;
            else
            if ( lhs.keys[i] > rhs.keys[i] ) return FALSE;
        }
        return FALSE;
    }

    bool operator () (const QComposeTableElement &lhs, const uint rhs[QT_KEYSEQUENCE_MAX_LEN]) const {
        for ( size_t i=0; i < QT_KEYSEQUENCE_MAX_LEN; i++ ) {
            if ( lhs.keys[i] < rhs[i] ) return TRUE;
            else
            if ( lhs.keys[i] > rhs[i] ) return FALSE;
        }
        return FALSE;
    }
};

QSimpleInputContext::QSimpleInputContext()
    : QInputContext()
{
    clearComposeBuffer();
}

QSimpleInputContext::~QSimpleInputContext()
{
    clearComposeBuffer();
}

bool QSimpleInputContext::filterEvent( const QEvent *event )
{
    if ( event->type() != QEvent::KeyPress )
        return FALSE;

    QKeyEvent *keyevent = (QKeyEvent *)event;
    int keyval = keyevent->key();
    int val = 0;

    if ( isIgnoreKeys( keyval ) )
        return FALSE;

    if ( isComposingKeys( keyval ) ) {
        // If composing keys are pressed, use keyval directly
        val = UNITIZE( keyval );
    } else {
        QString text = keyevent->text();
        if ( text.isEmpty() )
            return FALSE;
        
        // If not composing keys are pressed, use the character's unicode value
        // NOTE : The contents of QKeyEvent::text() is restricted to
        // only one character. See description of
        // QInputContext::filterEvent() about key compression.
        val = text[0].unicode();
       // qDebug( "str = %s", (const char*)keyevent->text().local8Bit() );
    }

    // Store value
    int nCompose = 0;
    while ( composeBuffer[nCompose] != 0 && nCompose < QT_KEYSEQUENCE_MAX_LEN )
        nCompose++;

    if ( nCompose == QT_KEYSEQUENCE_MAX_LEN ) {
        clearComposeBuffer();
        nCompose = 0;
    }

    composeBuffer[nCompose] = val;

    // check sequence
    if( checkComposeTable( composeBuffer, &defaultComposeTable ) )
        return TRUE;

    return FALSE;
}

void QSimpleInputContext::setFocus()
{
    //qDebug( "QSimpleInputContext: %p->setFocus(), focusWidget()=%p",
            //this, focusWidget() );
}

void QSimpleInputContext::unsetFocus()
{
    //qDebug( "QSimpleInputContext: %p->unsetFocus(), focusWidget()=%p",
            //this, focusWidget() );
    reset();
}

void QSimpleInputContext::setMicroFocus( int x, int y, int w, int h, QFont *f )
{
}

void QSimpleInputContext::mouseHandler( int x, QEvent::Type type,
				     Qt::ButtonState button,
				     Qt::ButtonState state )
{
    switch ( type ) {
    case QEvent::MouseButtonPress:
    case QEvent::MouseButtonRelease:
    case QEvent::MouseButtonDblClick:
    case QEvent::MouseMove:
       // qDebug( "QSimpleInputContext::mouseHandler: "
               // "x=%d, type=%d, button=%d, state=%d", x, type, button, state );
        break;
    default:
        break;
    }
}


void QSimpleInputContext::reset()
{
    clearComposeBuffer();
    QInputContext::reset();
}

QString QSimpleInputContext::identifierName()
{
    return "simple";
}

QString QSimpleInputContext::language()
{
    return "";  // FIXME
}

bool QSimpleInputContext::isIgnoreKeys( int keyval )
{
    for ( uint i = 0; i < (sizeof(ignoreKeys)/sizeof(ignoreKeys[0])); i++ )
        if ( keyval == ignoreKeys[i] )
            return TRUE;

    return FALSE;
}

bool QSimpleInputContext::isComposingKeys( int keyval )
{
    for ( uint i = 0; i < (sizeof(composingKeys)/sizeof(composingKeys[0])); i++ )
        if ( keyval == composingKeys[i] )
            return TRUE;

    return FALSE;
}

bool QSimpleInputContext::checkComposeTable( uint* composeBuffer, const QComposeTable *composeTable )
{
    const QComposeTableElement *p = std::lower_bound( composeTable->data,
						      composeTable->data + composeTable->size,
						      composeBuffer,
						      Cmp() );

    // no entries were found
    if ( p == composeTable->data + composeTable->size ) {
       // qDebug( "no match" );
        clearComposeBuffer();
        return FALSE;
    }

    // check if compose buffer is matched
    for ( int i=0; i < QT_KEYSEQUENCE_MAX_LEN; i++ ) {

        // check if partial match
        if ( composeBuffer[i] == 0 && p->keys[i] ) {
            // qDebug("partial match");
            return TRUE;
        }

        if ( composeBuffer[i] != p->keys[i] ) {
          // qDebug("different entry");
            clearComposeBuffer();
            return i!=0;
        }
    }

   // qDebug("match exactly");

    // match exactly
    commitChar( p->value );
    clearComposeBuffer();

    return TRUE;
}

void QSimpleInputContext::commitChar( uint c )
{
    sendIMEvent( QEvent::IMStart );
    sendIMEvent( QEvent::IMEnd, QString(QChar(c)) );
}

void QSimpleInputContext::clearComposeBuffer(void)
{
    for ( uint i=0; i < (sizeof(composeBuffer)/sizeof(int)); i++ )
        composeBuffer[i] = 0;
}