summaryrefslogtreecommitdiffstats
path: root/plugins/src/inputmethods/imsw-multi/qmultiinputcontext.cpp
blob: 42791a4edb8095871bda9bf53c8e79e8a2106998 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/****************************************************************************
** $Id$
**
** Implementation of QMultiInputContext 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.
**
**********************************************************************/

#ifndef QT_NO_IM
#include "qmultiinputcontext.h"
#include <qinputcontextfactory.h>
#include <qstringlist.h>
#include <qpopupmenu.h>
#ifndef QT_NO_IM_EXTENSIONS
#include <qsettings.h>
#endif

#include <cstdlib>

#define QT_NO_IM_QMULTIINPUTCONTEXT_IMINDEX

QMultiInputContext::QMultiInputContext()
    : QInputContext(), _slave( 0 ), imIndex( 0 ), cachedFocus( FALSE ),
      cachedFocusWidget( 0 ), cachedHolderWidget( 0 ),
      beIndirectlyConnected( FALSE ), popup( NULL ), currentIMKey( QString::null )
{
    keyDict.setAutoDelete( true );
    keyDict.clear();

    if ( getenv( "QT_IM_MODULE" ) ) {
        currentIMKey = getenv( "QT_IM_MODULE" );
    } else {
#ifndef QT_NO_IM_EXTENSIONS
	QSettings settings;
        currentIMKey = settings.readEntry( "/qt/DefaultInputMethod", "xim" );
#else
	currentIMKey = "xim";
#endif
    }
}

QMultiInputContext::~QMultiInputContext()
{
    keyDict.clear();
}


QString QMultiInputContext::identifierName()
{
    return ( slave() ) ? slave()->identifierName() : "";
}

QString QMultiInputContext::language()
{
    return ( slave() ) ? slave()->language() : "";
}


#if defined(Q_WS_X11)
bool QMultiInputContext::x11FilterEvent( QWidget *keywidget, XEvent *event )
{
    return ( slave() ) ? slave()->x11FilterEvent( keywidget, event ) : FALSE;
}
#endif // Q_WS_X11


bool QMultiInputContext::filterEvent( const QEvent *event )
{
#if !defined(QT_NO_IM_QMULTIINPUTCONTEXT_IMINDEX)
    if ( event->type() == QEvent::KeyPress ) {
	QKeyEvent *keyevent = (QKeyEvent *)event;

	// filter selection key
	// Control+Alt+Key_Down: change to next input method
	// Control+Alt+Key_Up:   change to previous input method
	if ( ( keyevent->state() & Qt::ControlButton ) &&
	     ( keyevent->state() & Qt::AltButton ) ) {
	    if ( keyevent->key() == Qt::Key_Up ) {
		changeInputMethod( --imIndex );
		return TRUE;
	    } else if ( keyevent->key() == Qt::Key_Down ) {
		changeInputMethod( ++imIndex );
		return TRUE;
	    }
	}
    }
#endif

    return ( slave() ) ? slave()->filterEvent( event ) : FALSE;
}

void QMultiInputContext::reset()
{
    if ( slave() )
	slave()->reset();
}


void QMultiInputContext::setFocus()
{
    cachedFocus = TRUE;
    if ( slave() )
	slave()->setFocus();
}

void QMultiInputContext::unsetFocus()
{
    cachedFocus = FALSE;
    if ( slave() )
	slave()->unsetFocus();
}

void QMultiInputContext::setMicroFocus( int x, int y, int w, int h, QFont *f )
{
    if ( slave() )
	slave()->setMicroFocus( x, y, w, h, f );
}

void QMultiInputContext::mouseHandler( int x, QEvent::Type type,
				       Qt::ButtonState button,
				       Qt::ButtonState state )
{
    if ( slave() )
	slave()->mouseHandler( x, type, button, state );
}

QFont QMultiInputContext::font() const
{
    return ( slave() ) ? slave()->font() : QInputContext::font();
}

void QMultiInputContext::destroyInputContext()
{
    if ( _slave ) {
	// _slave->reset() may not properly work in the case, so we
	// manually resets the composing state of text widget
	if ( _slave->focusWidget() ) {
	    QIMEvent *terminator = new QIMEvent( QEvent::IMEnd, QString::null, -1 );
	    emit imEventGenerated( _slave->focusWidget(), terminator );
	}
	_slave->deleteLater();
	_slave = 0;
    }
}


/*!
    This function is a placeholder for future experiment or extension
    such as commit string snooping. set beIndirectlyConnected = TRUE
    to activate this virtual function.
*/
void QMultiInputContext::postIMEvent( QObject *receiver, QIMEvent *event )
{
    emit imEventGenerated( receiver, event );
}


#if defined(Q_WS_X11)
QWidget *QMultiInputContext::focusWidget() const
{
    return ( slave() ) ? slave()->focusWidget() : 0;
}

QWidget *QMultiInputContext::holderWidget() const
{
    return ( slave() ) ? slave()->holderWidget() : 0;
}


void QMultiInputContext::setFocusWidget( QWidget *w )
{
    cachedFocusWidget = w;
    if ( slave() )
	slave()->setFocusWidget( w );
}

void QMultiInputContext::setHolderWidget( QWidget *w )
{
    cachedHolderWidget = w;
    if ( slave() )
	slave()->setHolderWidget( w );
}

void QMultiInputContext::releaseComposingWidget( QWidget *w )
{
    if ( slave() )
	slave()->releaseComposingWidget( w );
}

#endif

bool QMultiInputContext::isComposing() const
{
    return ( slave() ) ? slave()->isComposing() : FALSE;
}

bool QMultiInputContext::isPreeditRelocationEnabled()
{
    return ( slave() ) ? slave()->isPreeditRelocationEnabled() : FALSE;
}

QInputContext *QMultiInputContext::slave()
{
    if ( ! _slave ) {
#if !defined(QT_NO_IM_QMULTIINPUTCONTEXT_IMINDEX)
	changeInputMethod( imIndex );
#else
	changeInputMethod( currentIMKey );
#endif
    }

    return _slave;
}

const QInputContext *QMultiInputContext::slave() const
{
    return _slave;
}

void QMultiInputContext::changeInputMethod( int newIndex )
{
#if !defined(QT_NO_IM_QMULTIINPUTCONTEXT_IMINDEX)
    QStringList keys = QInputContextFactory::keys();
    if ( keys.size() == 0 )
	return;

    if ( newIndex >= (int)keys.size() ) {
	imIndex = 0;
    } else if ( newIndex < 0 ) {
	imIndex = keys.size() - 1;
    } else {
	imIndex = newIndex;
    }

    changeInputMethod( keys[imIndex] );
#endif
}

void QMultiInputContext::changeInputMethod( QString key )
{
    QStringList keys = QInputContextFactory::keys();
    if ( keys.size() == 0 )
	return;

    if ( key.isEmpty() )
	key = keys[0];

    if ( _slave ) {
	_slave->reset();
	delete _slave;
    }

    _slave = QInputContextFactory::create( key, cachedHolderWidget );
    if ( _slave ) {
	insertChild( _slave );

	const char *method;
	if ( beIndirectlyConnected ) {
	    method = SLOT(imEventReceived(QObject *,QIMEvent *));
	} else {
	    method = SIGNAL(imEventGenerated(QObject *,QIMEvent *));
	}
	connect( _slave, SIGNAL(imEventGenerated(QObject *,QIMEvent *)),
		 this, method );
	connect( _slave, SIGNAL(deletionRequested()),
		 this, SLOT(destroyInputContext()) );

	if ( cachedFocus ) {
	    _slave->setFocus();
	    _slave->setFocusWidget( cachedFocusWidget );
	}

        currentIMKey = key;

	//qDebug( "QMultiInputContext::changeInputMethod(): index=%d, slave=%s",
	//	imIndex, (const char *)_slave->identifierName() );
    }
}

QPtrList<QInputContextMenu> *QMultiInputContext::menus()
{
    QInputContextMenu *imSelMenu = new QInputContextMenu;
    imSelMenu->title = tr( "Select Input &Method" );
    imSelMenu->popup = createImSelPopup();

    QPtrList<QInputContextMenu> *result = new QPtrList<QInputContextMenu>;
    result->append( imSelMenu );

    QPtrList<QInputContextMenu> *slaveMenus = ( slave() ) ? slave()->menus() : 0;
    if ( slaveMenus ) {
	for ( QPtrList<QInputContextMenu>::Iterator it = slaveMenus->begin();
	      it != slaveMenus->end();
	      ++it ) {
	    QInputContextMenu *slaveMenu = *it;
	    result->append( slaveMenu );
	}
	delete slaveMenus;
    }

    return result;
}

QPopupMenu *QMultiInputContext::createImSelPopup()
{
    if ( popup )
        delete popup;

    popup = new QPopupMenu();
    keyDict.clear();

    QStringList keys = QInputContextFactory::keys();
    for ( uint i=0; i < keys.size(); i++ ) {
	QString idName = keys[i];
	bool isIMSwitcher = idName.startsWith( "imsw-" );

	if ( ! isIMSwitcher ) {
	    QString dispName = QInputContextFactory::displayName( idName );
	    if ( dispName.isEmpty() )
		dispName = idName;

	    int id = popup->insertItem( dispName );
	    keyDict.insert( (long)id, new QString( idName ) );

	    if ( idName == currentIMKey )
		popup->setItemChecked( id, true );

            QString descriptionStr = QInputContextFactory::description( idName );
            if ( ! descriptionStr.isEmpty() )
                popup->setWhatsThis( id, descriptionStr );
	}
    }

    QObject::connect( popup, SIGNAL(activated(int)),
                      this, SLOT(changeInputMethodWithMenuId(int)) );

    return popup;
}

void QMultiInputContext::changeInputMethodWithMenuId( int menuid )
{
    QString *key = keyDict.find( (long)menuid );
    changeInputMethod( (*key) );
}

#endif