Attachment "tk-unixkey-getstring.patch" to
ticket [1986818f]
added by
jenglish
2008-06-07 02:51:21.
From abde44c53b7a36b24bee4a4a75a4a4780408d4f8 Mon Sep 17 00:00:00 2001
From: Joe English <[email protected]>
Date: Fri, 6 Jun 2008 12:39:09 -0700
Subject: [PATCH] unix/tkUnixKey.c: TkpGetString fixes.
+ Use Xutf8LookupString() if available instead of XmbLookupString
[Fixes #1908443]
+ Factored out calls to XLookupString into TkpGetChar.
+ Don't use system encoding to translate results of XLookupString,
it always returns ISO Latin 1 characters.
+ Clarified comments: dsPtr must point to an initialized Tcl_DString.
commit 410c704f84057f774d26458321b2f33c8cdd6095
Author: jenglish <jenglish>
Date: Fri May 23 17:58:05 2008 +0000
---
unix/tkUnixKey.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 82 insertions(+), 13 deletions(-)
diff --git a/unix/tkUnixKey.c b/unix/tkUnixKey.c
index 4d422cf..6118332 100644
--- a/unix/tkUnixKey.c
+++ b/unix/tkUnixKey.c
@@ -78,6 +78,48 @@ Tk_SetCaretPos(
/*
*----------------------------------------------------------------------
*
+ * TkpGetChar --
+ *
+ * Convert a keyboard event to a UTF-8 string using XLookupString.
+ *
+ * This is used as a fallback instead of Xutf8LookupString
+ * or XmbLookupString if input methods are turned off
+ * and for KeyRelease events.
+ *
+ * Notes:
+ * XLookupString() normally returns a single ISO Latin 1
+ * or ASCII control character.
+ *
+ *----------------------------------------------------------------------
+ */
+static char *
+TkpGetChar(
+ XEvent *eventPtr, /* KeyPress or KeyRelease event */
+ Tcl_DString *dsPtr) /* Initialized, empty string to hold result. */
+{
+ int len;
+ char buf[TCL_DSTRING_STATIC_SIZE];
+
+ len = XLookupString(&eventPtr->xkey, buf, TCL_DSTRING_STATIC_SIZE, 0, 0);
+ buf[len] = '\0';
+
+ if (len == 1) {
+ len = Tcl_UniCharToUtf((unsigned char)buf[0], Tcl_DStringValue(dsPtr));
+ Tcl_DStringSetLength(dsPtr, len);
+ } else {
+ /*
+ * len > 1 should only happen if someone has called XRebindKeysym().
+ * Assume UTF-8.
+ */
+ Tcl_DStringSetLength(dsPtr, len);
+ strncpy(Tcl_DStringValue(dsPtr), buf, len);
+ }
+ return Tcl_DStringValue(dsPtr);
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
* TkpGetString --
*
* Retrieve the UTF string associated with a keyboard event.
@@ -95,11 +137,36 @@ Tk_SetCaretPos(
char *
TkpGetString(
- TkWindow *winPtr, /* Window where event occurred: needed to get
- * input context. */
+ TkWindow *winPtr, /* Window where event occurred */
XEvent *eventPtr, /* X keyboard event. */
- Tcl_DString *dsPtr) /* Uninitialized or empty string to hold
- * result. */
+ Tcl_DString *dsPtr) /* Initialized, empty string to hold result. */
+#ifdef TK_USE_INPUT_METHODS
+#if X_HAVE_UTF8_STRING
+{
+ if ((winPtr->dispPtr->flags & TK_DISPLAY_USE_IM)
+ && (winPtr->inputContext != NULL)
+ && (eventPtr->type == KeyPress))
+ {
+ int len = TCL_DSTRING_STATIC_SIZE - 1;
+ Status status = XBufferOverflow;
+
+ while (status == XBufferOverflow) {
+ len = Xutf8LookupString(winPtr->inputContext, &eventPtr->xkey,
+ Tcl_DStringValue(dsPtr), len, NULL, &status);
+ Tcl_DStringSetLength(dsPtr, len);
+ }
+ if ((status != XLookupChars) && (status != XLookupBoth)) {
+ Tcl_DStringSetLength(dsPtr, 0);
+ len = 0;
+ }
+ Tcl_DStringSetLength(dsPtr, len);
+
+ return Tcl_DStringValue(dsPtr);
+ } else {
+ return TkpGetChar(eventPtr, dsPtr);
+ }
+}
+#else /* !X_HAVE_UTF8_STRING */
{
int len;
Tcl_DString buf;
@@ -112,7 +179,6 @@ TkpGetString(
Tcl_DStringInit(&buf);
Tcl_DStringSetLength(&buf, TCL_DSTRING_STATIC_SIZE-1);
-#ifdef TK_USE_INPUT_METHODS
if ((winPtr->dispPtr->flags & TK_DISPLAY_USE_IM)
&& (winPtr->inputContext != NULL)
&& (eventPtr->type == KeyPress)) {
@@ -120,6 +186,7 @@ TkpGetString(
len = XmbLookupString(winPtr->inputContext, &eventPtr->xkey,
Tcl_DStringValue(&buf), Tcl_DStringLength(&buf), NULL,
&status);
+
/*
* If the buffer wasn't big enough, grow the buffer and try again.
*/
@@ -132,22 +199,24 @@ TkpGetString(
if ((status != XLookupChars) && (status != XLookupBoth)) {
len = 0;
}
-
} else {
- len = XLookupString(&eventPtr->xkey, Tcl_DStringValue(&buf),
- Tcl_DStringLength(&buf), NULL, NULL);
+ return TkpGetChar(eventPtr, dsPtr);
}
-#else /* TK_USE_INPUT_METHODS */
- len = XLookupString(&eventPtr->xkey, Tcl_DStringValue(&buf),
- Tcl_DStringLength(&buf), NULL, NULL);
-#endif /* TK_USE_INPUT_METHODS */
- Tcl_DStringSetLength(&buf, len);
+ Tcl_DStringSetLength(&buf, len);
Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&buf), len, dsPtr);
Tcl_DStringFree(&buf);
return Tcl_DStringValue(dsPtr);
}
+
+#endif /* X_HAVE_UTF8_STRING */
+#else /* !TK_USE_INPUT_METHODS */
+{
+ return TkpGetChar(eventPtr, dsPtr);
+}
+#endif
+
/*
* When mapping from a keysym to a keycode, need information about the
--
1.5.0.2