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