summaryrefslogtreecommitdiffstats
path: root/tqtinterface/qt4/plugins/src/inputmethods/imsw-multi/tqmultiinputcontext.cpp
blob: a3caede803197e14083f186468fe7169f198e1a7 (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 TQMultiInputContext 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
** 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 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 sales@trolltech.com 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 info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#ifndef TQT_NO_IM
#include "tqmultiinputcontext.h"
#include <tqinputcontextfactory.h>
#include <tqstringlist.h>
#include <tqpopupmenu.h>
#ifndef TQT_NO_IM_EXTENSIONS
#include <tqsettings.h>
#endif

#include <cstdlib>

#define TQT_NO_IM_TQMULTIINPUTCONTEXT_IMINDEX

TQMultiInputContext::TQMultiInputContext()
    : TQInputContext(), _slave( 0 ), imIndex( 0 ), cachedFocus( FALSE ),
      cachedFocusWidget( 0 ), cachedHolderWidget( 0 ),
      beIndirectlyConnected( FALSE ), popup( NULL ), currentIMKey( TQString() )
{
    keyDict.setAutoDelete( true );
    keyDict.clear();

    if ( getenv( "TQT_IM_MODULE" ) ) {
        currentIMKey = getenv( "TQT_IM_MODULE" );
    } else {
#ifndef TQT_NO_IM_EXTENSIONS
	TQSettings settings;
        currentIMKey = settings.readEntry( "/qt/DefaultInputMethod", "xim" );
#else
	currentIMKey = "xim";
#endif
    }
}

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


TQString TQMultiInputContext::identifierName()
{
    return ( slave() ) ? slave()->identifierName() : "";
}

TQString TQMultiInputContext::language()
{
    return ( slave() ) ? slave()->language() : "";
}


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


bool TQMultiInputContext::filterEvent( const TQEvent *event )
{
#if !defined(TQT_NO_IM_TQMULTIINPUTCONTEXT_IMINDEX)
    if ( event->type() == TQEvent::KeyPress ) {
	TQKeyEvent *keyevent = (TQKeyEvent *)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() & TQt::ControlButton ) &&
	     ( keyevent->state() & TQt::AltButton ) ) {
	    if ( keyevent->key() == TQt::Key_Up ) {
		changeInputMethod( --imIndex );
		return TRUE;
	    } else if ( keyevent->key() == TQt::Key_Down ) {
		changeInputMethod( ++imIndex );
		return TRUE;
	    }
	}
    }
#endif

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

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


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

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

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

void TQMultiInputContext::mouseHandler( int x, TQEvent::Type type,
				       TQt::ButtonState button,
				       TQt::ButtonState state )
{
    if ( slave() )
	slave()->mouseHandler( x, type, button, state );
}

TQFont TQMultiInputContext::font() const
{
    return ( slave() ) ? slave()->font() : TQInputContext::font();
}

void TQMultiInputContext::destroyInputContext()
{
    if ( _slave ) {
	// _slave->reset() may not properly work in the case, so we
	// manually resets the composing state of text widget
	if ( _slave->tqfocusWidget() ) {
	    TQIMEvent *terminator = new TQIMEvent( TQEvent::IMEnd, TQString(), -1 );
	    emit imEventGenerated( _slave->tqfocusWidget(), 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 TQMultiInputContext::postIMEvent( TQObject *receiver, TQIMEvent *event )
{
    emit imEventGenerated( receiver, event );
}


#if defined(TQ_WS_X11)
TQWidget *TQMultiInputContext::tqfocusWidget() const
{
    return ( slave() ) ? slave()->tqfocusWidget() : 0;
}

TQWidget *TQMultiInputContext::holderWidget() const
{
    return ( slave() ) ? slave()->holderWidget() : 0;
}


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

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

void TQMultiInputContext::releaseComposingWidget( TQWidget *w )
{
    if ( slave() )
	slave()->releaseComposingWidget( w );
}

#endif

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

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

TQInputContext *TQMultiInputContext::slave()
{
    if ( ! _slave ) {
#if !defined(TQT_NO_IM_TQMULTIINPUTCONTEXT_IMINDEX)
	changeInputMethod( imIndex );
#else
	changeInputMethod( currentIMKey );
#endif
    }

    return _slave;
}

const TQInputContext *TQMultiInputContext::slave() const
{
    return _slave;
}

void TQMultiInputContext::changeInputMethod( int newIndex )
{
#if !defined(TQT_NO_IM_TQMULTIINPUTCONTEXT_IMINDEX)
    TQStringList keys = TQInputContextFactory::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 TQMultiInputContext::changeInputMethod( TQString key )
{
    TQStringList keys = TQInputContextFactory::keys();
    if ( keys.size() == 0 )
	return;

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

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

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

	const char *method;
	if ( beIndirectlyConnected ) {
	    method = TQT_SLOT(imEventReceived(TQObject *,TQIMEvent *));
	} else {
	    method = TQT_SIGNAL(imEventGenerated(TQObject *,TQIMEvent *));
	}
	connect( _slave, TQT_SIGNAL(imEventGenerated(TQObject *,TQIMEvent *)),
		 this, method );
	connect( _slave, TQT_SIGNAL(deletionRequested()),
		 this, TQT_SLOT(destroyInputContext()) );

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

        currentIMKey = key;

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

TQPtrList<TQInputContextMenu> *TQMultiInputContext::menus()
{
    TQInputContextMenu *imSelMenu = new TQInputContextMenu;
    imSelMenu->title = tr( "Select Input &Method" );
    imSelMenu->popup = createImSelPopup();

    TQPtrList<TQInputContextMenu> *result = new TQPtrList<TQInputContextMenu>;
    result->append( imSelMenu );

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

    return result;
}

TQPopupMenu *TQMultiInputContext::createImSelPopup()
{
    if ( popup )
        delete popup;

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

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

	if ( ! isIMSwitcher ) {
	    TQString dispName = TQInputContextFactory::displayName( idName );
	    if ( dispName.isEmpty() )
		dispName = idName;

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

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

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

    TQObject::connect( popup, TQT_SIGNAL(activated(int)),
                      this, TQT_SLOT(changeInputMethodWithMenuId(int)) );

    return popup;
}

void TQMultiInputContext::changeInputMethodWithMenuId( int menuid )
{
    TQString *key = keyDict.tqfind( (long)menuid );
    changeInputMethod( (*key) );
}

#endif