Update ooo320-m1
[ooovba.git] / vcl / unx / source / app / i18n_cb.cxx
blob49d2c85a7ca49042f61d5dabb6838c842ca4d8e6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: i18n_cb.cxx,v $
10 * $Revision: 1.32 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
34 #include <stdio.h>
35 #include <string.h>
36 #include <sal/alloca.h>
37 #include <tools/prex.h>
38 #include <X11/Xlocale.h>
39 #include <X11/Xlib.h>
40 #include <tools/postx.h>
42 #include <salunx.h>
43 #include <XIM.h>
44 #include <i18n_cb.hxx>
45 #include <i18n_status.hxx>
46 #include "i18n_ic.hxx"
47 #include "i18n_im.hxx"
48 #ifndef _OSL_THREAD_H
49 #include <osl/thread.h>
50 #endif
51 #include <vcl/salframe.hxx>
53 // -------------------------------------------------------------------------
55 // i. preedit start callback
57 // -------------------------------------------------------------------------
59 int
60 PreeditStartCallback ( XIC, XPointer client_data, XPointer )
62 preedit_data_t* pPreeditData = (preedit_data_t*)client_data;
63 if ( pPreeditData->eState == ePreeditStatusActivationRequired )
65 pPreeditData->eState = ePreeditStatusActive;
66 pPreeditData->aText.nCursorPos = 0;
67 pPreeditData->aText.nLength = 0;
70 return -1;
73 // -------------------------------------------------------------------------
75 // ii. preedit done callback
77 // -------------------------------------------------------------------------
79 void
80 PreeditDoneCallback ( XIC, XPointer client_data, XPointer )
82 preedit_data_t* pPreeditData = (preedit_data_t*)client_data;
83 if (pPreeditData->eState == ePreeditStatusActive )
85 if( pPreeditData->pFrame )
86 pPreeditData->pFrame->CallCallback( SALEVENT_ENDEXTTEXTINPUT, (void*)NULL );
88 pPreeditData->eState = ePreeditStatusStartPending;
91 // -------------------------------------------------------------------------
93 // iii. preedit draw callback
95 // -------------------------------------------------------------------------
98 // Handle deletion of text in a preedit_draw_callback
99 // from and howmuch are guaranteed to be nonnegative
102 void
103 Preedit_DeleteText(preedit_text_t *ptext, int from, int howmuch)
105 // If we've been asked to delete no text then just set
106 // nLength correctly and return
107 if (ptext->nLength == 0)
109 ptext->nLength = from;
110 return;
113 int to = from + howmuch;
115 if (to == (int)ptext->nLength)
117 // delete from the end of the text
118 ptext->nLength = from;
120 else
121 if (to < (int)ptext->nLength)
123 // cut out of the middle of the text
124 memmove( (void*)(ptext->pUnicodeBuffer + from),
125 (void*)(ptext->pUnicodeBuffer + to),
126 (ptext->nLength - to) * sizeof(sal_Unicode));
127 memmove( (void*)(ptext->pCharStyle + from),
128 (void*)(ptext->pCharStyle + to),
129 (ptext->nLength - to) * sizeof(XIMFeedback));
130 ptext->nLength -= howmuch;
132 else
133 // if ( to > pText->nLength )
135 // XXX this indicates an error, are we out of sync ?
136 fprintf(stderr, "Preedit_DeleteText( from=%i to=%i length=%i )\n",
137 from, to, ptext->nLength );
138 fprintf (stderr, "\t XXX internal error, out of sync XXX\n");
140 ptext->nLength = from;
143 // NULL-terminate the string
144 ptext->pUnicodeBuffer[ptext->nLength] = (sal_Unicode)0;
147 // reallocate the textbuffer with sufficiently large size 2^x
148 // nnewlimit is presupposed to be larger than ptext->size
149 void
150 enlarge_buffer ( preedit_text_t *ptext, int nnewlimit )
152 size_t nnewsize = ptext->nSize;
154 while ( nnewsize <= (size_t)nnewlimit )
155 nnewsize *= 2;
157 ptext->nSize = nnewsize;
158 ptext->pUnicodeBuffer = (sal_Unicode*)realloc((void*)ptext->pUnicodeBuffer,
159 nnewsize * sizeof(sal_Unicode));
160 ptext->pCharStyle = (XIMFeedback*)realloc((void*)ptext->pCharStyle,
161 nnewsize * sizeof(XIMFeedback));
165 // Handle insertion of text in a preedit_draw_callback
166 // string field of XIMText struct is guaranteed to be != NULL
169 void
170 Preedit_InsertText(preedit_text_t *pText, XIMText *pInsertText, int where,
171 Bool isMultilingual)
173 sal_Unicode *pInsertTextString;
174 int nInsertTextLength = 0;
175 XIMFeedback *pInsertTextCharStyle = pInsertText->feedback;
177 nInsertTextLength = pInsertText->length;
179 if (isMultilingual)
181 XIMUnicodeText *pUniText = (XIMUnicodeText*)pInsertText;
182 pInsertTextString = pUniText->string.utf16_char;
184 else
186 // can't handle wchar_t strings, so convert to multibyte chars first
187 char *pMBString;
188 size_t nMBLength;
189 if (pInsertText->encoding_is_wchar)
191 wchar_t *pWCString = pInsertText->string.wide_char;
192 size_t nBytes = wcstombs ( NULL, pWCString, 1024 /* dont care */);
193 pMBString = (char*)alloca( nBytes + 1 );
194 nMBLength = wcstombs ( pMBString, pWCString, nBytes + 1);
196 else
198 pMBString = pInsertText->string.multi_byte;
199 nMBLength = strlen(pMBString); // xxx
202 // convert multibyte chars to unicode
203 rtl_TextEncoding nEncoding = osl_getThreadTextEncoding();
205 if (nEncoding != RTL_TEXTENCODING_UNICODE)
207 rtl_TextToUnicodeConverter aConverter =
208 rtl_createTextToUnicodeConverter( nEncoding );
209 rtl_TextToUnicodeContext aContext =
210 rtl_createTextToUnicodeContext(aConverter);
212 sal_Size nBufferSize = nInsertTextLength * 2;
214 pInsertTextString = (sal_Unicode*)alloca(nBufferSize);
216 sal_uInt32 nConversionInfo;
217 sal_Size nConvertedChars;
219 rtl_convertTextToUnicode( aConverter, aContext,
220 pMBString, nMBLength,
221 pInsertTextString, nBufferSize,
222 RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_IGNORE
223 | RTL_TEXTTOUNICODE_FLAGS_INVALID_IGNORE,
224 &nConversionInfo, &nConvertedChars );
226 rtl_destroyTextToUnicodeContext(aConverter, aContext);
227 rtl_destroyTextToUnicodeConverter(aConverter);
230 else
232 pInsertTextString = (sal_Unicode*)pMBString;
236 // enlarge target text-buffer if necessary
237 if (pText->nSize <= (pText->nLength + nInsertTextLength))
238 enlarge_buffer(pText, pText->nLength + nInsertTextLength);
240 // insert text: displace old mem and put new bytes in
241 int from = where;
242 int to = where + nInsertTextLength;
243 int howmany = pText->nLength - where;
245 memmove((void*)(pText->pUnicodeBuffer + to),
246 (void*)(pText->pUnicodeBuffer + from),
247 howmany * sizeof(sal_Unicode));
248 memmove((void*)(pText->pCharStyle + to),
249 (void*)(pText->pCharStyle + from),
250 howmany * sizeof(XIMFeedback));
252 to = from;
253 howmany = nInsertTextLength;
255 memcpy((void*)(pText->pUnicodeBuffer + to), (void*)pInsertTextString,
256 howmany * sizeof(sal_Unicode));
257 memcpy((void*)(pText->pCharStyle + to), (void*)pInsertTextCharStyle,
258 howmany * sizeof(XIMFeedback));
260 pText->nLength += howmany;
262 // NULL-terminate the string
263 pText->pUnicodeBuffer[pText->nLength] = (sal_Unicode)0;
267 // Handle the change of attributes in a preedit_draw_callback
269 void
270 Preedit_UpdateAttributes ( preedit_text_t* ptext, XIMFeedback* feedback,
271 int from, int amount )
273 if ( (from + amount) > (int)ptext->nLength )
275 // XXX this indicates an error, are we out of sync ?
276 fprintf (stderr, "Preedit_UpdateAttributes( %i + %i > %i )\n",
277 from, amount, ptext->nLength );
278 fprintf (stderr, "\t XXX internal error, out of sync XXX\n");
280 return;
283 memcpy ( ptext->pCharStyle + from,
284 feedback, amount * sizeof(XIMFeedback) );
287 // Convert the XIM feedback values into appropriate VCL
288 // SAL_EXTTEXTINPUT_ATTR values
289 // returns an allocate list of attributes, which must be freed by caller
290 USHORT*
291 Preedit_FeedbackToSAL ( XIMFeedback* pfeedback, int nlength, std::vector<USHORT>& rSalAttr )
293 USHORT *psalattr;
294 USHORT nval;
295 USHORT noldval = 0;
296 XIMFeedback nfeedback;
298 // only work with reasonable length
299 if (nlength > 0 && nlength > sal::static_int_cast<int>(rSalAttr.size()) )
301 rSalAttr.reserve( nlength );
302 psalattr = &rSalAttr[0];
304 else
305 return (USHORT*)NULL;
307 for (int npos = 0; npos < nlength; npos++)
309 nval = 0;
310 nfeedback = pfeedback[npos];
312 // means to use the feedback of the previous char
313 if (nfeedback == 0)
315 nval = noldval;
317 // convert feedback to attributes
318 else
320 if (nfeedback & XIMReverse)
321 nval |= SAL_EXTTEXTINPUT_ATTR_HIGHLIGHT;
322 if (nfeedback & XIMUnderline)
323 nval |= SAL_EXTTEXTINPUT_ATTR_UNDERLINE;
324 if (nfeedback & XIMHighlight)
325 nval |= SAL_EXTTEXTINPUT_ATTR_HIGHLIGHT;
326 if (nfeedback & XIMPrimary)
327 nval |= SAL_EXTTEXTINPUT_ATTR_DOTTEDUNDERLINE;
328 if (nfeedback & XIMSecondary)
329 nval |= SAL_EXTTEXTINPUT_ATTR_DASHDOTUNDERLINE;
330 if (nfeedback & XIMTertiary) // same as 2ery
331 nval |= SAL_EXTTEXTINPUT_ATTR_DASHDOTUNDERLINE;
334 // visibility feedback not supported now
335 if ( (nfeedback & XIMVisibleToForward)
336 || (nfeedback & XIMVisibleToBackward)
337 || (nfeedback & XIMVisibleCenter) )
341 // copy in list
342 psalattr[npos] = nval;
343 noldval = nval;
345 // return list of sal attributes
346 return psalattr;
349 void
350 PreeditDrawCallback(XIC ic, XPointer client_data,
351 XIMPreeditDrawCallbackStruct *call_data)
353 preedit_data_t* pPreeditData = (preedit_data_t*)client_data;
355 // if there's nothing to change then change nothing
356 if ( ( (call_data->text == NULL) && (call_data->chg_length == 0) )
357 || pPreeditData->pFrame == NULL )
358 return;
360 // #88564# Solaris 7 deletes the preedit buffer after commit
361 // since the next call to preeditstart will have the same effect just skip this.
362 // if (pPreeditData->eState == ePreeditStatusStartPending && call_data->text == NULL)
363 // return;
365 if ( pPreeditData->eState == ePreeditStatusStartPending )
366 pPreeditData->eState = ePreeditStatusActivationRequired;
367 PreeditStartCallback( ic, client_data, NULL );
369 // Edit the internal textbuffer as indicated by the call_data,
370 // chg_first and chg_length are guaranteed to be nonnegative
372 // handle text deletion
373 if (call_data->text == NULL)
375 Preedit_DeleteText(&(pPreeditData->aText),
376 call_data->chg_first, call_data->chg_length );
378 else
380 // handle text insertion
381 if ( (call_data->chg_length == 0)
382 && (call_data->text->string.wide_char != NULL))
384 Preedit_InsertText(&(pPreeditData->aText), call_data->text,
385 call_data->chg_first, pPreeditData->bIsMultilingual);
387 else
388 // handle text replacement by deletion and insertion of text,
389 // not smart, just good enough
390 if ( (call_data->chg_length != 0)
391 && (call_data->text->string.wide_char != NULL))
393 Preedit_DeleteText(&(pPreeditData->aText),
394 call_data->chg_first, call_data->chg_length);
395 Preedit_InsertText(&(pPreeditData->aText), call_data->text,
396 call_data->chg_first, pPreeditData->bIsMultilingual);
398 else
399 // not really a text update, only attributes are concerned
400 if ( (call_data->chg_length != 0)
401 && (call_data->text->string.wide_char == NULL))
403 Preedit_UpdateAttributes(&(pPreeditData->aText),
404 call_data->text->feedback,
405 call_data->chg_first, call_data->chg_length);
410 // build the SalExtTextInputEvent and send it up
412 pPreeditData->aInputEv.mnTime = 0;
413 pPreeditData->aInputEv.mpTextAttr = Preedit_FeedbackToSAL(
414 pPreeditData->aText.pCharStyle, pPreeditData->aText.nLength, pPreeditData->aInputFlags);
415 pPreeditData->aInputEv.mnCursorPos = call_data->caret;
416 pPreeditData->aInputEv.maText = String (pPreeditData->aText.pUnicodeBuffer,
417 pPreeditData->aText.nLength);
418 pPreeditData->aInputEv.mnCursorFlags = 0; // default: make cursor visible
419 pPreeditData->aInputEv.mnDeltaStart = 0; // call_data->chg_first;
420 pPreeditData->aInputEv.mbOnlyCursor = False;
422 if ( pPreeditData->eState == ePreeditStatusActive && pPreeditData->pFrame )
423 pPreeditData->pFrame->CallCallback(SALEVENT_EXTTEXTINPUT, (void*)&pPreeditData->aInputEv);
424 if (pPreeditData->aText.nLength == 0 && pPreeditData->pFrame )
425 pPreeditData->pFrame->CallCallback( SALEVENT_ENDEXTTEXTINPUT, (void*)NULL );
427 if (pPreeditData->aText.nLength == 0)
428 pPreeditData->eState = ePreeditStatusStartPending;
430 GetPreeditSpotLocation(ic, (XPointer)pPreeditData);
433 void
434 GetPreeditSpotLocation(XIC ic, XPointer client_data)
437 // Send SalEventExtTextInputPos event to get spotlocation
439 SalExtTextInputPosEvent mPosEvent;
440 preedit_data_t* pPreeditData = (preedit_data_t*)client_data;
442 if( pPreeditData->pFrame )
443 pPreeditData->pFrame->CallCallback(SALEVENT_EXTTEXTINPUTPOS, (void*)&mPosEvent);
445 XPoint point;
446 point.x = mPosEvent.mnX + mPosEvent.mnWidth;
447 point.y = mPosEvent.mnY + mPosEvent.mnHeight;
449 XVaNestedList preedit_attr;
450 preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &point, NULL);
451 XSetICValues(ic, XNPreeditAttributes, preedit_attr, NULL);
452 XFree(preedit_attr);
454 return;
457 // -------------------------------------------------------------------------
459 // iv. preedit caret callback
461 // -------------------------------------------------------------------------
463 #if OSL_DEBUG_LEVEL > 1
464 void
465 PreeditCaretCallback ( XIC ic, XPointer client_data,
466 XIMPreeditCaretCallbackStruct *call_data )
467 #else
468 void
469 PreeditCaretCallback ( XIC, XPointer,XIMPreeditCaretCallbackStruct* )
470 #endif
472 #if OSL_DEBUG_LEVEL > 1
473 // XXX PreeditCaretCallback is pure debug code for now
474 const char *direction = "?";
475 const char *style = "?";
477 switch ( call_data->style )
479 case XIMIsInvisible: style = "Invisible"; break;
480 case XIMIsPrimary: style = "Primary"; break;
481 case XIMIsSecondary: style = "Secondary"; break;
483 switch ( call_data->direction )
485 case XIMForwardChar: direction = "Forward char"; break;
486 case XIMBackwardChar: direction = "Backward char"; break;
487 case XIMForwardWord: direction = "Forward word"; break;
488 case XIMBackwardWord: direction = "Backward word"; break;
489 case XIMCaretUp: direction = "Caret up"; break;
490 case XIMCaretDown: direction = "Caret down"; break;
491 case XIMNextLine: direction = "Next line"; break;
492 case XIMPreviousLine: direction = "Previous line"; break;
493 case XIMLineStart: direction = "Line start"; break;
494 case XIMLineEnd: direction = "Line end"; break;
495 case XIMAbsolutePosition: direction = "Absolute"; break;
496 case XIMDontChange: direction = "Dont change"; break;
499 fprintf (stderr, "PreeditCaretCallback( ic=%p, client=%p,\n",
500 ic, client_data );
501 fprintf (stderr, "\t position=%i, direction=\"%s\", style=\"%s\" )\n",
502 call_data->position, direction, style );
503 #endif
506 // -----------------------------------------------------------------------
508 // v. commit string callback: convert an extended text input (iiimp ... )
509 // into an ordinary key-event
511 // -----------------------------------------------------------------------
513 Bool
514 IsControlCode(sal_Unicode nChar)
516 if ( nChar <= 0x1F // C0 controls
517 /* || (0x80 <= nChar && nChar <= 0x9F) C1 controls */ )
518 return True;
519 else
520 return False;
524 CommitStringCallback( XIC ic, XPointer client_data, XPointer call_data )
526 preedit_data_t* pPreeditData = (preedit_data_t*)client_data;
528 XIMUnicodeText *cbtext = (XIMUnicodeText *)call_data;
529 sal_Unicode *p_unicode_data = (sal_Unicode*)cbtext->string.utf16_char;
531 // #86964# filter unexpected pure control events
532 if (cbtext->length == 1 && IsControlCode(p_unicode_data[0]) )
534 if( pPreeditData->pFrame )
536 pPreeditData->pFrame->CallCallback( SALEVENT_ENDEXTTEXTINPUT, (void*)NULL );
539 else
541 if( pPreeditData->pFrame )
543 pPreeditData->aInputEv.mnTime = 0;
544 pPreeditData->aInputEv.mpTextAttr = 0;
545 pPreeditData->aInputEv.mnCursorPos = cbtext->length;
546 pPreeditData->aInputEv.maText = UniString(p_unicode_data, cbtext->length);
547 pPreeditData->aInputEv.mnCursorFlags = 0; // default: make cursor visible
548 pPreeditData->aInputEv.mnDeltaStart = 0;
549 pPreeditData->aInputEv.mbOnlyCursor = False;
551 pPreeditData->pFrame->CallCallback( SALEVENT_EXTTEXTINPUT, (void*)&pPreeditData->aInputEv);
552 pPreeditData->pFrame->CallCallback( SALEVENT_ENDEXTTEXTINPUT, (void*)NULL );
555 pPreeditData->eState = ePreeditStatusStartPending;
557 GetPreeditSpotLocation(ic, (XPointer)pPreeditData);
559 return 0;
562 // ----------------------------------------------------------------------------------
564 // vi. status callbacks: for now these are empty, they are just needed for turbo linux
566 // ----------------------------------------------------------------------------------
568 void
569 StatusStartCallback (XIC, XPointer, XPointer)
571 return;
574 void
575 StatusDoneCallback (XIC, XPointer, XPointer)
577 return;
580 void
581 StatusDrawCallback (XIC ic, XPointer client_data, XIMStatusDrawCallbackStruct *call_data)
583 preedit_data_t* pPreeditData = (preedit_data_t*)client_data;
584 if( pPreeditData->bIsMultilingual )
586 // IIIMP
587 XIMUnicodeText *cbtext = (XIMUnicodeText *)call_data->data.text;
588 ::vcl::I18NStatus::get().setStatusText( String( cbtext->string.utf16_char, call_data->data.text->length ) );
589 XIMUnicodeCharacterSubset* pSubset = NULL;
590 if( ! XGetICValues( ic,
591 XNUnicodeCharacterSubset, & pSubset,
592 NULL )
593 && pSubset )
595 ::vcl::I18NStatus::get().changeIM( String( ByteString( pSubset->name ), RTL_TEXTENCODING_UTF8 ) );
596 #if OSL_DEBUG_LEVEL > 1
597 fprintf( stderr, "got XNUnicodeCharacterSubset\n %d\n %d\n %s\n %d\n", pSubset->index, pSubset->subset_id, pSubset->name, pSubset->is_active );
598 #endif
601 else if( call_data->type == XIMTextType )
603 String aText;
604 if( call_data->data.text )
606 // XIM with text
607 sal_Char* pMBString = NULL;
608 size_t nLength = 0;
609 if( call_data->data.text->encoding_is_wchar )
611 if( call_data->data.text->string.wide_char )
613 wchar_t* pWString = call_data->data.text->string.wide_char;
614 size_t nBytes = wcstombs( NULL, pWString, 1024 );
615 pMBString = (sal_Char*)alloca( nBytes+1 );
616 nLength = wcstombs( pMBString, pWString, nBytes+1 );
619 else
621 if( call_data->data.text->string.multi_byte )
623 pMBString = call_data->data.text->string.multi_byte;
624 nLength = strlen( pMBString );
627 if( nLength )
628 aText = String( pMBString, nLength, gsl_getSystemTextEncoding() );
630 ::vcl::I18NStatus::get().setStatusText( aText );
632 #if OSL_DEBUG_LEVEL > 1
633 else
634 fprintf( stderr, "XIMStatusDataType %s not supported\n",
635 call_data->type == XIMBitmapType ? "XIMBitmapType" : ByteString::CreateFromInt32( call_data->type ).GetBuffer() );
636 #endif
637 return;
640 void
641 SwitchIMCallback (XIC, XPointer, XPointer call_data)
643 XIMSwitchIMNotifyCallbackStruct* pCallData = (XIMSwitchIMNotifyCallbackStruct*)call_data;
644 ::vcl::I18NStatus::get().changeIM( String( ByteString( pCallData->to->name ), RTL_TEXTENCODING_UTF8 ) );
647 // ----------------------------------------------------------------------------------
649 // vii. destroy callbacks: internally disable all IC/IM calls
651 // ----------------------------------------------------------------------------------
653 void
654 IC_IMDestroyCallback (XIM, XPointer client_data, XPointer)
656 SalI18N_InputContext *pContext = (SalI18N_InputContext*)client_data;
657 if (pContext != NULL)
658 pContext->HandleDestroyIM();
661 void
662 IM_IMDestroyCallback (XIM, XPointer client_data, XPointer)
664 SalI18N_InputMethod *pMethod = (SalI18N_InputMethod*)client_data;
665 if (pMethod != NULL)
666 pMethod->HandleDestroyIM();