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