summaryrefslogtreecommitdiffstats
path: root/plugins/src/inputmethods/simple/qsimpleinputcontext.cpp
blob: 2e8398fb00e5eccfd30b250b73bfbfa43632dfb8 (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 TQSimpleInputContext class
**
** Copyright (C) 2004 immodule for TQt Project.  All rights reserved.
**
** This file is written to contribute to Trolltech AS under their own
** licence. You may use this file under your TQt license. Following
** description is copied from their original file headers. Contact
** [email protected] if any conditions of this licensing are
** not clear to you.
**
**
** This file is part of the input method module of the TQt 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.TQPL 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 TQt Enterprise Edition or TQt Professional Edition
** licenses may use this file in accordance with the TQt 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 [email protected] for
**   information about TQt Commercial License Agreements.
** See http://www.trolltech.com/qpl/ for TQPL licensing information.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact [email protected] 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[] = {
    TQt::Key_Shift,
    TQt::Key_Control,
    TQt::Key_Meta,
    TQt::Key_Alt,
    TQt::Key_CapsLock,
    TQt::Key_Super_L,
    TQt::Key_Super_R,
    TQt::Key_Hyper_L,
    TQt::Key_Hyper_R,
    TQt::Key_Mode_switch
};

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

class Cmp
{
public:
    bool operator () (const TQComposeTableElement &lhs, const TQComposeTableElement &rhs) const {
        for ( size_t i=0; i < QT_KEYSETQUENCE_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 TQComposeTableElement &lhs, const uint rhs[QT_KEYSETQUENCE_MAX_LEN]) const {
        for ( size_t i=0; i < QT_KEYSETQUENCE_MAX_LEN; i++ ) {
            if ( lhs.keys[i] < rhs[i] ) return TRUE;
            else
            if ( lhs.keys[i] > rhs[i] ) return FALSE;
        }
        return FALSE;
    }
};

TQSimpleInputContext::TQSimpleInputContext()
    : TQInputContext()
{
    clearComposeBuffer();
}

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

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

    TQKeyEvent *keyevent = (TQKeyEvent *)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 {
        TQString text = keyevent->text();
        if ( text.isEmpty() )
            return FALSE;
        
        // If not composing keys are pressed, use the character's unicode value
        // NOTE : The contents of TQKeyEvent::text() is restricted to
        // only one character. See description of
        // TQInputContext::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_KEYSETQUENCE_MAX_LEN )
        nCompose++;

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

    composeBuffer[nCompose] = val;

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

    return FALSE;
}

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

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

void TQSimpleInputContext::setMicroFocus( int x, int y, int w, int h, TQFont *f )
{
}

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


void TQSimpleInputContext::reset()
{
    clearComposeBuffer();
    TQInputContext::reset();
}

TQString TQSimpleInputContext::identifierName()
{
    return "simple";
}

TQString TQSimpleInputContext::language()
{
    return "";  // FIXME
}

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

    return FALSE;
}

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

    return FALSE;
}

bool TQSimpleInputContext::checkComposeTable( uint* composeBuffer, const TQComposeTable *composeTable )
{
    const TQComposeTableElement *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_KEYSETQUENCE_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 TQSimpleInputContext::commitChar( uint c )
{
    sendIMEvent( TQEvent::IMStart );
    sendIMEvent( TQEvent::IMEnd, TQString(TQChar(c)) );
}

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