author | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
commit | 15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff) | |
tree | c2fa0399a2c47fda8e2cd0092c73a809d17f68eb /core/launcher/inputmethods.cpp | |
download | opie-15318cad33835e4e2dc620d033e43cd930676cdd.zip opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2 |
Initial revision
Diffstat (limited to 'core/launcher/inputmethods.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/launcher/inputmethods.cpp | 297 |
1 files changed, 297 insertions, 0 deletions
diff --git a/core/launcher/inputmethods.cpp b/core/launcher/inputmethods.cpp new file mode 100644 index 0000000..003dc77 --- a/dev/null +++ b/core/launcher/inputmethods.cpp | |||
@@ -0,0 +1,297 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | #include "inputmethods.h" | ||
22 | |||
23 | #include <qpe/config.h> | ||
24 | #include <qpe/qpeapplication.h> | ||
25 | #include <qpe/inputmethodinterface.h> | ||
26 | #include <qpe/qlibrary.h> | ||
27 | |||
28 | #include <qpopupmenu.h> | ||
29 | #include <qpushbutton.h> | ||
30 | #include <qtoolbutton.h> | ||
31 | #include <qwidget.h> | ||
32 | #include <qlayout.h> | ||
33 | #include <qtimer.h> | ||
34 | #include <qdir.h> | ||
35 | #include <stdlib.h> | ||
36 | #include <qtranslator.h> | ||
37 | |||
38 | #ifdef Q_WS_QWS | ||
39 | #include <qwindowsystem_qws.h> | ||
40 | #include <qwsevent_qws.h> | ||
41 | #endif | ||
42 | |||
43 | #ifdef SINGLE_APP | ||
44 | #include "handwritingimpl.h" | ||
45 | #include "keyboardimpl.h" | ||
46 | #include "pickboardimpl.h" | ||
47 | #endif | ||
48 | |||
49 | |||
50 | /* XPM */ | ||
51 | static const char * tri_xpm[]={ | ||
52 | "9 9 2 1", | ||
53 | "a c #000000", | ||
54 | ". c None", | ||
55 | ".........", | ||
56 | ".........", | ||
57 | ".........", | ||
58 | "....a....", | ||
59 | "...aaa...", | ||
60 | "..aaaaa..", | ||
61 | ".aaaaaaa.", | ||
62 | ".........", | ||
63 | "........."}; | ||
64 | |||
65 | static const int inputWidgetStyle = QWidget::WStyle_Customize | | ||
66 | QWidget::WStyle_Tool | | ||
67 | QWidget::WStyle_StaysOnTop | | ||
68 | QWidget::WGroupLeader; | ||
69 | |||
70 | InputMethods::InputMethods( QWidget *parent ) : | ||
71 | QWidget( parent, "InputMethods", WStyle_Tool | WStyle_Customize ) | ||
72 | { | ||
73 | method = NULL; | ||
74 | |||
75 | QHBoxLayout *hbox = new QHBoxLayout( this ); | ||
76 | |||
77 | kbdButton = new QToolButton( this ); | ||
78 | kbdButton->setFocusPolicy(NoFocus); | ||
79 | kbdButton->setToggleButton( TRUE ); | ||
80 | kbdButton->setFixedHeight( 17 ); | ||
81 | kbdButton->setFixedWidth( 32 ); | ||
82 | kbdButton->setAutoRaise( TRUE ); | ||
83 | kbdButton->setUsesBigPixmap( TRUE ); | ||
84 | hbox->addWidget( kbdButton ); | ||
85 | connect( kbdButton, SIGNAL(toggled(bool)), this, SLOT(showKbd(bool)) ); | ||
86 | |||
87 | kbdChoice = new QToolButton( this ); | ||
88 | kbdChoice->setFocusPolicy(NoFocus); | ||
89 | kbdChoice->setPixmap( QPixmap( (const char **)tri_xpm ) ); | ||
90 | kbdChoice->setFixedHeight( 17 ); | ||
91 | kbdChoice->setFixedWidth( 12 ); | ||
92 | kbdChoice->setAutoRaise( TRUE ); | ||
93 | hbox->addWidget( kbdChoice ); | ||
94 | connect( kbdChoice, SIGNAL(clicked()), this, SLOT(chooseKbd()) ); | ||
95 | |||
96 | connect( (QPEApplication*)qApp, SIGNAL(clientMoused()), | ||
97 | this, SLOT(resetStates()) ); | ||
98 | |||
99 | loadInputMethods(); | ||
100 | } | ||
101 | |||
102 | InputMethods::~InputMethods() | ||
103 | { | ||
104 | #ifndef SINGLE_APP | ||
105 | QValueList<InputMethod>::Iterator mit; | ||
106 | for ( mit = inputMethodList.begin(); mit != inputMethodList.end(); ++mit ) { | ||
107 | int i = (*mit).interface->release(); | ||
108 | (*mit).library->unload(); | ||
109 | delete (*mit).library; | ||
110 | } | ||
111 | #endif | ||
112 | } | ||
113 | |||
114 | void InputMethods::hideInputMethod() | ||
115 | { | ||
116 | kbdButton->setOn( FALSE ); | ||
117 | } | ||
118 | |||
119 | void InputMethods::showInputMethod() | ||
120 | { | ||
121 | kbdButton->setOn( TRUE ); | ||
122 | } | ||
123 | |||
124 | void InputMethods::showInputMethod(const QString& name) | ||
125 | { | ||
126 | int i = 0; | ||
127 | QValueList<InputMethod>::Iterator it; | ||
128 | InputMethod *im = 0; | ||
129 | for ( it = inputMethodList.begin(); it != inputMethodList.end(); ++it, i++ ) { | ||
130 | if ( (*it).interface->name() == name ) { | ||
131 | im = &(*it); | ||
132 | break; | ||
133 | } | ||
134 | } | ||
135 | if ( im ) | ||
136 | chooseMethod(im); | ||
137 | } | ||
138 | |||
139 | void InputMethods::resetStates() | ||
140 | { | ||
141 | if ( method ) | ||
142 | method->interface->resetState(); | ||
143 | } | ||
144 | |||
145 | QRect InputMethods::inputRect() const | ||
146 | { | ||
147 | if ( !method || !method->widget->isVisible() ) | ||
148 | return QRect(); | ||
149 | else | ||
150 | return method->widget->geometry(); | ||
151 | } | ||
152 | |||
153 | void InputMethods::loadInputMethods() | ||
154 | { | ||
155 | #ifndef SINGLE_APP | ||
156 | hideInputMethod(); | ||
157 | method = 0; | ||
158 | |||
159 | QValueList<InputMethod>::Iterator mit; | ||
160 | for ( mit = inputMethodList.begin(); mit != inputMethodList.end(); ++mit ) { | ||
161 | (*mit).interface->release(); | ||
162 | (*mit).library->unload(); | ||
163 | delete (*mit).library; | ||
164 | } | ||
165 | inputMethodList.clear(); | ||
166 | |||
167 | QString path = QPEApplication::qpeDir() + "/plugins/inputmethods"; | ||
168 | QDir dir( path, "lib*.so" ); | ||
169 | QStringList list = dir.entryList(); | ||
170 | QStringList::Iterator it; | ||
171 | for ( it = list.begin(); it != list.end(); ++it ) { | ||
172 | InputMethodInterface *iface = 0; | ||
173 | QLibrary *lib = new QLibrary( path + "/" + *it ); | ||
174 | if ( lib->queryInterface( IID_InputMethod, (QUnknownInterface**)&iface ) == QS_OK ) { | ||
175 | InputMethod input; | ||
176 | input.library = lib; | ||
177 | input.interface = iface; | ||
178 | input.widget = input.interface->inputMethod( 0, inputWidgetStyle ); | ||
179 | input.interface->onKeyPress( this, SLOT(sendKey(ushort,ushort,ushort,bool,bool)) ); | ||
180 | inputMethodList.append( input ); | ||
181 | QString lang = getenv( "LANG" ); | ||
182 | QTranslator * trans = new QTranslator(qApp); | ||
183 | QString type = (*it).left( (*it).find(".") ); | ||
184 | QString tfn = QPEApplication::qpeDir()+"/i18n/"+lang+"/"+type+".qm"; | ||
185 | qDebug("tr for inputmethod: %s", tfn.latin1() ); | ||
186 | if ( trans->load( tfn )) | ||
187 | qApp->installTranslator( trans ); | ||
188 | else | ||
189 | delete trans; | ||
190 | } else { | ||
191 | delete lib; | ||
192 | } | ||
193 | } | ||
194 | #else | ||
195 | InputMethod input; | ||
196 | input.interface = new HandwritingImpl(); | ||
197 | input.widget = input.interface->inputMethod( 0, inputWidgetStyle ); | ||
198 | input.interface->onKeyPress( this, SLOT(sendKey(ushort,ushort,ushort,bool,bool)) ); | ||
199 | inputMethodList.append( input ); | ||
200 | input.interface = new KeyboardImpl(); | ||
201 | input.widget = input.interface->inputMethod( 0, inputWidgetStyle ); | ||
202 | input.interface->onKeyPress( this, SLOT(sendKey(ushort,ushort,ushort,bool,bool)) ); | ||
203 | inputMethodList.append( input ); | ||
204 | input.interface = new PickboardImpl(); | ||
205 | input.widget = input.interface->inputMethod( 0, inputWidgetStyle ); | ||
206 | input.interface->onKeyPress( this, SLOT(sendKey(ushort,ushort,ushort,bool,bool)) ); | ||
207 | inputMethodList.append( input ); | ||
208 | #endif | ||
209 | if ( !inputMethodList.isEmpty() ) { | ||
210 | method = &inputMethodList[0]; | ||
211 | kbdButton->setPixmap( *method->interface->icon() ); | ||
212 | } | ||
213 | if ( !inputMethodList.isEmpty() ) | ||
214 | kbdButton->show(); | ||
215 | else | ||
216 | kbdButton->hide(); | ||
217 | if ( inputMethodList.count() > 1 ) | ||
218 | kbdChoice->show(); | ||
219 | else | ||
220 | kbdChoice->hide(); | ||
221 | } | ||
222 | |||
223 | void InputMethods::chooseKbd() | ||
224 | { | ||
225 | QPopupMenu pop( this ); | ||
226 | |||
227 | int i = 0; | ||
228 | QValueList<InputMethod>::Iterator it; | ||
229 | for ( it = inputMethodList.begin(); it != inputMethodList.end(); ++it, i++ ) { | ||
230 | pop.insertItem( (*it).interface->name(), i ); | ||
231 | if ( method == &(*it) ) | ||
232 | pop.setItemChecked( i, TRUE ); | ||
233 | } | ||
234 | |||
235 | QPoint pt = mapToGlobal(kbdChoice->geometry().topRight()); | ||
236 | QSize s = pop.sizeHint(); | ||
237 | pt.ry() -= s.height(); | ||
238 | pt.rx() -= s.width(); | ||
239 | i = pop.exec( pt ); | ||
240 | if ( i == -1 ) | ||
241 | return; | ||
242 | InputMethod *im = &inputMethodList[i]; | ||
243 | chooseMethod(im); | ||
244 | } | ||
245 | |||
246 | void InputMethods::chooseMethod(InputMethod* im) | ||
247 | { | ||
248 | if ( im != method ) { | ||
249 | if ( method && method->widget->isVisible() ) | ||
250 | method->widget->hide(); | ||
251 | method = im; | ||
252 | kbdButton->setPixmap( *method->interface->icon() ); | ||
253 | } | ||
254 | if ( !kbdButton->isOn() ) | ||
255 | kbdButton->setOn( TRUE ); | ||
256 | else | ||
257 | showKbd( TRUE ); | ||
258 | } | ||
259 | |||
260 | |||
261 | void InputMethods::showKbd( bool on ) | ||
262 | { | ||
263 | if ( !method ) | ||
264 | return; | ||
265 | |||
266 | if ( on ) { | ||
267 | method->interface->resetState(); | ||
268 | // HACK... Make the texteditor fit with all input methods | ||
269 | // Input methods should also never use more than about 40% of the screen | ||
270 | int height = QMIN( method->widget->sizeHint().height(), 134 ); | ||
271 | method->widget->resize( qApp->desktop()->width(), height ); | ||
272 | method->widget->move( 0, mapToGlobal( QPoint() ).y() - height ); | ||
273 | method->widget->show(); | ||
274 | } else { | ||
275 | method->widget->hide(); | ||
276 | } | ||
277 | |||
278 | emit inputToggled( on ); | ||
279 | } | ||
280 | |||
281 | bool InputMethods::shown() const | ||
282 | { | ||
283 | return method && method->widget->isVisible(); | ||
284 | } | ||
285 | |||
286 | QString InputMethods::currentShown() const | ||
287 | { | ||
288 | return method && method->widget->isVisible() | ||
289 | ? method->interface->name() : QString::null; | ||
290 | } | ||
291 | |||
292 | void InputMethods::sendKey( ushort unicode, ushort scancode, ushort mod, bool press, bool repeat ) | ||
293 | { | ||
294 | #if defined(Q_WS_QWS) | ||
295 | QWSServer::sendKeyEvent( unicode, scancode, mod, press, repeat ); | ||
296 | #endif | ||
297 | } | ||