Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <stdio.h>
21 : #include <string.h>
22 :
23 : #include <sal/alloca.h>
24 : #include <osl/thread.h>
25 :
26 : #include <prex.h>
27 : #include <X11/Xlocale.h>
28 : #include <X11/Xlib.h>
29 : #include <postx.h>
30 :
31 : #include "vcl/cmdevt.hxx"
32 : #include "unx/salunx.h"
33 : #include "unx/XIM.h"
34 : #include "unx/i18n_cb.hxx"
35 : #include "unx/i18n_status.hxx"
36 : #include "unx/i18n_ic.hxx"
37 : #include "unx/i18n_im.hxx"
38 : #include "salframe.hxx"
39 :
40 : // i. preedit start callback
41 :
42 : int
43 0 : PreeditStartCallback ( XIC, XPointer client_data, XPointer )
44 : {
45 0 : preedit_data_t* pPreeditData = reinterpret_cast<preedit_data_t*>(client_data);
46 0 : if ( pPreeditData->eState == ePreeditStatusActivationRequired )
47 : {
48 0 : pPreeditData->eState = ePreeditStatusActive;
49 0 : pPreeditData->aText.nCursorPos = 0;
50 0 : pPreeditData->aText.nLength = 0;
51 : }
52 :
53 0 : return -1;
54 : }
55 :
56 : // ii. preedit done callback
57 :
58 : void
59 0 : PreeditDoneCallback ( XIC, XPointer client_data, XPointer )
60 : {
61 0 : preedit_data_t* pPreeditData = reinterpret_cast<preedit_data_t*>(client_data);
62 0 : if (pPreeditData->eState == ePreeditStatusActive )
63 : {
64 0 : if( pPreeditData->pFrame )
65 0 : pPreeditData->pFrame->CallCallback( SALEVENT_ENDEXTTEXTINPUT, nullptr );
66 : }
67 0 : pPreeditData->eState = ePreeditStatusStartPending;
68 0 : }
69 :
70 : // iii. preedit draw callback
71 :
72 : // Handle deletion of text in a preedit_draw_callback
73 : // from and howmuch are guaranteed to be nonnegative
74 :
75 : void
76 0 : Preedit_DeleteText(preedit_text_t *ptext, int from, int howmuch)
77 : {
78 : // If we've been asked to delete no text then just set
79 : // nLength correctly and return
80 0 : if (ptext->nLength == 0)
81 : {
82 0 : ptext->nLength = from;
83 0 : return;
84 : }
85 :
86 0 : int to = from + howmuch;
87 :
88 0 : if (to == (int)ptext->nLength)
89 : {
90 : // delete from the end of the text
91 0 : ptext->nLength = from;
92 : }
93 : else
94 0 : if (to < (int)ptext->nLength)
95 : {
96 : // cut out of the middle of the text
97 0 : memmove( static_cast<void*>(ptext->pUnicodeBuffer + from),
98 : static_cast<void*>(ptext->pUnicodeBuffer + to),
99 0 : (ptext->nLength - to) * sizeof(sal_Unicode));
100 0 : memmove( static_cast<void*>(ptext->pCharStyle + from),
101 : static_cast<void*>(ptext->pCharStyle + to),
102 0 : (ptext->nLength - to) * sizeof(XIMFeedback));
103 0 : ptext->nLength -= howmuch;
104 : }
105 : else
106 : {
107 : // XXX this indicates an error, are we out of sync ?
108 : fprintf(stderr, "Preedit_DeleteText( from=%i to=%i length=%i )\n",
109 0 : from, to, ptext->nLength );
110 0 : fprintf (stderr, "\t XXX internal error, out of sync XXX\n");
111 :
112 0 : ptext->nLength = from;
113 : }
114 :
115 : // NULL-terminate the string
116 0 : ptext->pUnicodeBuffer[ptext->nLength] = (sal_Unicode)0;
117 : }
118 :
119 : // reallocate the textbuffer with sufficiently large size 2^x
120 : // nnewlimit is presupposed to be larger than ptext->size
121 : void
122 0 : enlarge_buffer ( preedit_text_t *ptext, int nnewlimit )
123 : {
124 0 : size_t nnewsize = ptext->nSize;
125 :
126 0 : while ( nnewsize <= (size_t)nnewlimit )
127 0 : nnewsize *= 2;
128 :
129 0 : ptext->nSize = nnewsize;
130 : ptext->pUnicodeBuffer = static_cast<sal_Unicode*>(realloc(static_cast<void*>(ptext->pUnicodeBuffer),
131 0 : nnewsize * sizeof(sal_Unicode)));
132 : ptext->pCharStyle = static_cast<XIMFeedback*>(realloc(static_cast<void*>(ptext->pCharStyle),
133 0 : nnewsize * sizeof(XIMFeedback)));
134 0 : }
135 :
136 : // Handle insertion of text in a preedit_draw_callback
137 : // string field of XIMText struct is guaranteed to be != NULL
138 :
139 : void
140 0 : Preedit_InsertText(preedit_text_t *pText, XIMText *pInsertText, int where)
141 : {
142 : sal_Unicode *pInsertTextString;
143 0 : int nInsertTextLength = 0;
144 0 : XIMFeedback *pInsertTextCharStyle = pInsertText->feedback;
145 :
146 0 : nInsertTextLength = pInsertText->length;
147 :
148 : // can't handle wchar_t strings, so convert to multibyte chars first
149 : char *pMBString;
150 : size_t nMBLength;
151 0 : if (pInsertText->encoding_is_wchar)
152 : {
153 0 : wchar_t *pWCString = pInsertText->string.wide_char;
154 0 : size_t nBytes = wcstombs ( NULL, pWCString, 1024 /* dont care */);
155 0 : pMBString = static_cast<char*>(alloca( nBytes + 1 ));
156 0 : nMBLength = wcstombs ( pMBString, pWCString, nBytes + 1);
157 : }
158 : else
159 : {
160 0 : pMBString = pInsertText->string.multi_byte;
161 0 : nMBLength = strlen(pMBString); // xxx
162 : }
163 :
164 : // convert multibyte chars to unicode
165 0 : rtl_TextEncoding nEncoding = osl_getThreadTextEncoding();
166 :
167 0 : if (nEncoding != RTL_TEXTENCODING_UNICODE)
168 : {
169 : rtl_TextToUnicodeConverter aConverter =
170 0 : rtl_createTextToUnicodeConverter( nEncoding );
171 : rtl_TextToUnicodeContext aContext =
172 0 : rtl_createTextToUnicodeContext(aConverter);
173 :
174 0 : sal_Size nBufferSize = nInsertTextLength * 2;
175 :
176 0 : pInsertTextString = static_cast<sal_Unicode*>(alloca(nBufferSize));
177 :
178 : sal_uInt32 nConversionInfo;
179 : sal_Size nConvertedChars;
180 :
181 : rtl_convertTextToUnicode( aConverter, aContext,
182 : pMBString, nMBLength,
183 : pInsertTextString, nBufferSize,
184 : RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_IGNORE
185 : | RTL_TEXTTOUNICODE_FLAGS_INVALID_IGNORE,
186 0 : &nConversionInfo, &nConvertedChars );
187 :
188 0 : rtl_destroyTextToUnicodeContext(aConverter, aContext);
189 0 : rtl_destroyTextToUnicodeConverter(aConverter);
190 :
191 : }
192 : else
193 : {
194 0 : pInsertTextString = reinterpret_cast<sal_Unicode*>(pMBString);
195 : }
196 :
197 : // enlarge target text-buffer if necessary
198 0 : if (pText->nSize <= (pText->nLength + nInsertTextLength))
199 0 : enlarge_buffer(pText, pText->nLength + nInsertTextLength);
200 :
201 : // insert text: displace old mem and put new bytes in
202 0 : int from = where;
203 0 : int to = where + nInsertTextLength;
204 0 : int howmany = pText->nLength - where;
205 :
206 0 : memmove(static_cast<void*>(pText->pUnicodeBuffer + to),
207 : static_cast<void*>(pText->pUnicodeBuffer + from),
208 0 : howmany * sizeof(sal_Unicode));
209 0 : memmove(static_cast<void*>(pText->pCharStyle + to),
210 : static_cast<void*>(pText->pCharStyle + from),
211 0 : howmany * sizeof(XIMFeedback));
212 :
213 0 : to = from;
214 0 : howmany = nInsertTextLength;
215 :
216 0 : memcpy(static_cast<void*>(pText->pUnicodeBuffer + to), static_cast<void*>(pInsertTextString),
217 0 : howmany * sizeof(sal_Unicode));
218 0 : memcpy(static_cast<void*>(pText->pCharStyle + to), static_cast<void*>(pInsertTextCharStyle),
219 0 : howmany * sizeof(XIMFeedback));
220 :
221 0 : pText->nLength += howmany;
222 :
223 : // NULL-terminate the string
224 0 : pText->pUnicodeBuffer[pText->nLength] = (sal_Unicode)0;
225 0 : }
226 :
227 : // Handle the change of attributes in a preedit_draw_callback
228 :
229 : void
230 0 : Preedit_UpdateAttributes ( preedit_text_t* ptext, XIMFeedback* feedback,
231 : int from, int amount )
232 : {
233 0 : if ( (from + amount) > (int)ptext->nLength )
234 : {
235 : // XXX this indicates an error, are we out of sync ?
236 : fprintf (stderr, "Preedit_UpdateAttributes( %i + %i > %i )\n",
237 0 : from, amount, ptext->nLength );
238 0 : fprintf (stderr, "\t XXX internal error, out of sync XXX\n");
239 :
240 0 : return;
241 : }
242 :
243 0 : memcpy ( ptext->pCharStyle + from,
244 0 : feedback, amount * sizeof(XIMFeedback) );
245 : }
246 :
247 : // Convert the XIM feedback values into appropriate VCL
248 : // EXTTEXTINPUT_ATTR values
249 : // returns an allocate list of attributes, which must be freed by caller
250 : sal_uInt16*
251 0 : Preedit_FeedbackToSAL ( XIMFeedback* pfeedback, int nlength, std::vector<sal_uInt16>& rSalAttr )
252 : {
253 : sal_uInt16 *psalattr;
254 : sal_uInt16 nval;
255 0 : sal_uInt16 noldval = 0;
256 : XIMFeedback nfeedback;
257 :
258 : // only work with reasonable length
259 0 : if (nlength > 0 && nlength > sal::static_int_cast<int>(rSalAttr.size()) )
260 : {
261 0 : rSalAttr.reserve( nlength );
262 0 : psalattr = &rSalAttr[0];
263 : }
264 : else
265 0 : return nullptr;
266 :
267 0 : for (int npos = 0; npos < nlength; npos++)
268 : {
269 0 : nval = 0;
270 0 : nfeedback = pfeedback[npos];
271 :
272 : // means to use the feedback of the previous char
273 0 : if (nfeedback == 0)
274 : {
275 0 : nval = noldval;
276 : }
277 : // convert feedback to attributes
278 : else
279 : {
280 0 : if (nfeedback & XIMReverse)
281 0 : nval |= EXTTEXTINPUT_ATTR_HIGHLIGHT;
282 0 : if (nfeedback & XIMUnderline)
283 0 : nval |= EXTTEXTINPUT_ATTR_UNDERLINE;
284 0 : if (nfeedback & XIMHighlight)
285 0 : nval |= EXTTEXTINPUT_ATTR_HIGHLIGHT;
286 0 : if (nfeedback & XIMPrimary)
287 0 : nval |= EXTTEXTINPUT_ATTR_DOTTEDUNDERLINE;
288 0 : if (nfeedback & XIMSecondary)
289 0 : nval |= EXTTEXTINPUT_ATTR_DASHDOTUNDERLINE;
290 0 : if (nfeedback & XIMTertiary) // same as 2ery
291 0 : nval |= EXTTEXTINPUT_ATTR_DASHDOTUNDERLINE;
292 :
293 : }
294 : // copy in list
295 0 : psalattr[npos] = nval;
296 0 : noldval = nval;
297 : }
298 : // return list of sal attributes
299 0 : return psalattr;
300 : }
301 :
302 : void
303 0 : PreeditDrawCallback(XIC ic, XPointer client_data,
304 : XIMPreeditDrawCallbackStruct *call_data)
305 : {
306 0 : preedit_data_t* pPreeditData = reinterpret_cast<preedit_data_t*>(client_data);
307 :
308 : // if there's nothing to change then change nothing
309 0 : if ( ( (call_data->text == NULL) && (call_data->chg_length == 0) )
310 0 : || pPreeditData->pFrame == NULL )
311 0 : return;
312 :
313 : // Solaris 7 deletes the preedit buffer after commit
314 : // since the next call to preeditstart will have the same effect just skip this.
315 : // if (pPreeditData->eState == ePreeditStatusStartPending && call_data->text == NULL)
316 : // return;
317 :
318 0 : if ( pPreeditData->eState == ePreeditStatusStartPending )
319 0 : pPreeditData->eState = ePreeditStatusActivationRequired;
320 0 : PreeditStartCallback( ic, client_data, NULL );
321 :
322 : // Edit the internal textbuffer as indicated by the call_data,
323 : // chg_first and chg_length are guaranteed to be nonnegative
324 :
325 : // handle text deletion
326 0 : if (call_data->text == NULL)
327 : {
328 : Preedit_DeleteText(&(pPreeditData->aText),
329 0 : call_data->chg_first, call_data->chg_length );
330 : }
331 : else
332 : {
333 : // handle text insertion
334 0 : if ( (call_data->chg_length == 0)
335 0 : && (call_data->text->string.wide_char != NULL))
336 : {
337 : Preedit_InsertText(&(pPreeditData->aText), call_data->text,
338 0 : call_data->chg_first);
339 : }
340 0 : else if ( (call_data->chg_length != 0)
341 0 : && (call_data->text->string.wide_char != NULL))
342 : {
343 : // handle text replacement by deletion and insertion of text,
344 : // not smart, just good enough
345 :
346 : Preedit_DeleteText(&(pPreeditData->aText),
347 0 : call_data->chg_first, call_data->chg_length);
348 : Preedit_InsertText(&(pPreeditData->aText), call_data->text,
349 0 : call_data->chg_first);
350 : }
351 0 : else if ( (call_data->chg_length != 0)
352 0 : && (call_data->text->string.wide_char == NULL))
353 : {
354 : // not really a text update, only attributes are concerned
355 : Preedit_UpdateAttributes(&(pPreeditData->aText),
356 : call_data->text->feedback,
357 0 : call_data->chg_first, call_data->chg_length);
358 : }
359 : }
360 :
361 : // build the SalExtTextInputEvent and send it up
362 :
363 0 : pPreeditData->aInputEv.mnTime = 0;
364 : pPreeditData->aInputEv.mpTextAttr = Preedit_FeedbackToSAL(
365 0 : pPreeditData->aText.pCharStyle, pPreeditData->aText.nLength, pPreeditData->aInputFlags);
366 0 : pPreeditData->aInputEv.mnCursorPos = call_data->caret;
367 0 : pPreeditData->aInputEv.maText = OUString(pPreeditData->aText.pUnicodeBuffer,
368 0 : pPreeditData->aText.nLength);
369 0 : pPreeditData->aInputEv.mnCursorFlags = 0; // default: make cursor visible
370 0 : pPreeditData->aInputEv.mbOnlyCursor = False;
371 :
372 0 : if ( pPreeditData->eState == ePreeditStatusActive && pPreeditData->pFrame )
373 0 : pPreeditData->pFrame->CallCallback(SALEVENT_EXTTEXTINPUT, static_cast<void*>(&pPreeditData->aInputEv));
374 0 : if (pPreeditData->aText.nLength == 0 && pPreeditData->pFrame )
375 0 : pPreeditData->pFrame->CallCallback( SALEVENT_ENDEXTTEXTINPUT, nullptr );
376 :
377 0 : if (pPreeditData->aText.nLength == 0)
378 0 : pPreeditData->eState = ePreeditStatusStartPending;
379 :
380 0 : GetPreeditSpotLocation(ic, reinterpret_cast<XPointer>(pPreeditData));
381 : }
382 :
383 : void
384 0 : GetPreeditSpotLocation(XIC ic, XPointer client_data)
385 : {
386 :
387 : // Send SalEventExtTextInputPos event to get spotlocation
388 :
389 0 : SalExtTextInputPosEvent mPosEvent;
390 0 : preedit_data_t* pPreeditData = reinterpret_cast<preedit_data_t*>(client_data);
391 :
392 0 : if( pPreeditData->pFrame )
393 0 : pPreeditData->pFrame->CallCallback(SALEVENT_EXTTEXTINPUTPOS, static_cast<void*>(&mPosEvent));
394 :
395 : XPoint point;
396 0 : point.x = mPosEvent.mnX + mPosEvent.mnWidth;
397 0 : point.y = mPosEvent.mnY + mPosEvent.mnHeight;
398 :
399 : XVaNestedList preedit_attr;
400 0 : preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &point, NULL);
401 0 : XSetICValues(ic, XNPreeditAttributes, preedit_attr, NULL);
402 0 : XFree(preedit_attr);
403 :
404 0 : return;
405 : }
406 :
407 : // iv. preedit caret callback
408 :
409 : #if OSL_DEBUG_LEVEL > 1
410 : void
411 : PreeditCaretCallback ( XIC ic, XPointer client_data,
412 : XIMPreeditCaretCallbackStruct *call_data )
413 : #else
414 : void
415 0 : PreeditCaretCallback ( XIC, XPointer,XIMPreeditCaretCallbackStruct* )
416 : #endif
417 : {
418 : #if OSL_DEBUG_LEVEL > 1
419 : // XXX PreeditCaretCallback is pure debug code for now
420 : const char *direction = "?";
421 : const char *style = "?";
422 :
423 : switch ( call_data->style )
424 : {
425 : case XIMIsInvisible: style = "Invisible"; break;
426 : case XIMIsPrimary: style = "Primary"; break;
427 : case XIMIsSecondary: style = "Secondary"; break;
428 : }
429 : switch ( call_data->direction )
430 : {
431 : case XIMForwardChar: direction = "Forward char"; break;
432 : case XIMBackwardChar: direction = "Backward char"; break;
433 : case XIMForwardWord: direction = "Forward word"; break;
434 : case XIMBackwardWord: direction = "Backward word"; break;
435 : case XIMCaretUp: direction = "Caret up"; break;
436 : case XIMCaretDown: direction = "Caret down"; break;
437 : case XIMNextLine: direction = "Next line"; break;
438 : case XIMPreviousLine: direction = "Previous line"; break;
439 : case XIMLineStart: direction = "Line start"; break;
440 : case XIMLineEnd: direction = "Line end"; break;
441 : case XIMAbsolutePosition: direction = "Absolute"; break;
442 : case XIMDontChange: direction = "Dont change"; break;
443 : }
444 :
445 : fprintf (stderr, "PreeditCaretCallback( ic=%p, client=%p,\n",
446 : ic, client_data );
447 : fprintf (stderr, "\t position=%i, direction=\"%s\", style=\"%s\" )\n",
448 : call_data->position, direction, style );
449 : #endif
450 0 : }
451 :
452 : // v. commit string callback: convert an extended text input (iiimp ... )
453 : // into an ordinary key-event
454 :
455 : Bool
456 0 : IsControlCode(sal_Unicode nChar)
457 : {
458 0 : if ( nChar <= 0x1F /* C0 controls */ )
459 0 : return True;
460 : else
461 0 : return False;
462 : }
463 :
464 : // vi. status callbacks: for now these are empty, they are just needed for turbo linux
465 :
466 : void
467 0 : StatusStartCallback (XIC, XPointer, XPointer)
468 : {
469 0 : return;
470 : }
471 :
472 : void
473 0 : StatusDoneCallback (XIC, XPointer, XPointer)
474 : {
475 0 : return;
476 : }
477 :
478 : void
479 0 : StatusDrawCallback (XIC, XPointer, XIMStatusDrawCallbackStruct *call_data)
480 : {
481 0 : if( call_data->type == XIMTextType )
482 : {
483 0 : OUString aText;
484 0 : if( call_data->data.text )
485 : {
486 : // XIM with text
487 0 : sal_Char* pMBString = NULL;
488 0 : size_t nLength = 0;
489 0 : if( call_data->data.text->encoding_is_wchar )
490 : {
491 0 : if( call_data->data.text->string.wide_char )
492 : {
493 0 : wchar_t* pWString = call_data->data.text->string.wide_char;
494 0 : size_t nBytes = wcstombs( NULL, pWString, 1024 );
495 0 : pMBString = static_cast<sal_Char*>(alloca( nBytes+1 ));
496 0 : nLength = wcstombs( pMBString, pWString, nBytes+1 );
497 : }
498 : }
499 : else
500 : {
501 0 : if( call_data->data.text->string.multi_byte )
502 : {
503 0 : pMBString = call_data->data.text->string.multi_byte;
504 0 : nLength = strlen( pMBString );
505 : }
506 : }
507 0 : if( nLength )
508 0 : aText = OUString( pMBString, nLength, osl_getThreadTextEncoding() );
509 : }
510 0 : vcl::I18NStatus::get().setStatusText( aText );
511 : }
512 : #if OSL_DEBUG_LEVEL > 1
513 : else
514 : {
515 : fprintf( stderr, "XIMStatusDataType %s not supported\n",
516 : call_data->type == XIMBitmapType ? "XIMBitmapType" : OString::number(call_data->type).getStr() );
517 : }
518 : #endif
519 0 : return;
520 : }
521 :
522 : void
523 0 : SwitchIMCallback (XIC, XPointer, XPointer call_data)
524 : {
525 0 : XIMSwitchIMNotifyCallbackStruct* pCallData = reinterpret_cast<XIMSwitchIMNotifyCallbackStruct*>(call_data);
526 0 : vcl::I18NStatus::get().changeIM( OStringToOUString(pCallData->to->name, RTL_TEXTENCODING_UTF8) );
527 0 : }
528 :
529 : // vii. destroy callbacks: internally disable all IC/IM calls
530 :
531 : void
532 0 : IC_IMDestroyCallback (XIM, XPointer client_data, XPointer)
533 : {
534 0 : SalI18N_InputContext *pContext = reinterpret_cast<SalI18N_InputContext*>(client_data);
535 0 : if (pContext != NULL)
536 0 : pContext->HandleDestroyIM();
537 0 : }
538 :
539 : void
540 0 : IM_IMDestroyCallback (XIM, XPointer client_data, XPointer)
541 : {
542 0 : SalI18N_InputMethod *pMethod = reinterpret_cast<SalI18N_InputMethod*>(client_data);
543 0 : if (pMethod != NULL)
544 0 : pMethod->HandleDestroyIM();
545 0 : }
546 :
547 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|