# HG changeset patch
# User Dan Pascu <[EMAIL PROTECTED]>
# Date 1146106098 25200
# Branch wm_0_92
# Node ID 3f2715bc97c1838f813417deae01a0cb5324d192
# Parent ca5197e5c8973e15eae8eedb6ed392600ed11be1
Enhanced the cursor positioning/selecting text using the mouse in a textfield.
This also fixed an endless loop that could be entered by the previous code
in certain situations, after the patch to fix navigation/selection in a
textfiled with UTF8 chars was applied.
(transplanted from 84685abca352053e487b793c42832fbb4b6a0bf1)
diff -r ca5197e5c897 -r 3f2715bc97c1 WINGs/Tests/wtest.c
--- a/WINGs/Tests/wtest.c Wed Apr 26 13:28:24 2006 -0700
+++ b/WINGs/Tests/wtest.c Wed Apr 26 19:48:18 2006 -0700
@@ -572,6 +572,7 @@
field = WMCreateTextField(win);
WMResizeWidget(field, 200, 20);
WMMoveWidget(field, 20, 20);
+ WMSetTextFieldText(field, "the little \xc2\xa9 sign");
field2 = WMCreateTextField(win);
WMResizeWidget(field2, 200, 20);
diff -r ca5197e5c897 -r 3f2715bc97c1 WINGs/wtextfield.c
--- a/WINGs/wtextfield.c Wed Apr 26 13:28:24 2006 -0700
+++ b/WINGs/wtextfield.c Wed Apr 26 19:48:18 2006 -0700
@@ -1375,35 +1375,53 @@
static int
pointToCursorPosition(TextField *tPtr, int x)
{
- int a, b, mid;
- int tw;
+ int a, b, pos, prev, tw;
if (tPtr->flags.bordered)
x -= 2;
if (WMWidthOfString(tPtr->font, &(tPtr->text[tPtr->viewPosition]),
- tPtr->textLen - tPtr->viewPosition) < x)
+ tPtr->textLen - tPtr->viewPosition) <= x)
return tPtr->textLen;
a = tPtr->viewPosition;
b = tPtr->textLen;
- while (a < b) {
- mid = (a+b)/2;
- mid += seekUTF8CharStart(&tPtr->text[mid], mid - a);
+ /* we halve the text until we get into a 10 byte vicinity of x */
+ while (b - a > 10) {
+ pos = (a+b)/2;
+ pos += seekUTF8CharStart(&tPtr->text[pos], pos - a);
tw = WMWidthOfString(tPtr->font, &(tPtr->text[tPtr->viewPosition]),
- mid - tPtr->viewPosition + 1);
+ pos - tPtr->viewPosition);
if (tw > x) {
- b = mid;
+ b = pos;
} else if (tw < x) {
- if (a == mid)
- a += oneUTF8CharForward(&tPtr->text[mid], b - a);
- else
- a = mid;
+ a = pos;
} else {
- return mid;
+ return pos;
}
}
+
+ /* at this point x can be positioned on any glyph between 'a' and 'b-1'
+ * inclusive, with the exception of the left border of the 'a' glyph and
+ * the right border or the 'b-1' glyph
+ *
+ * ( <--- range for x's position ---> )
+ * a a+1 .......................... b-1 b
+ */
+ pos = prev = a;
+ while (pos <= b) {
+ tw = WMWidthOfString(tPtr->font, &(tPtr->text[tPtr->viewPosition]),
+ pos - tPtr->viewPosition);
+ if (tw > x) {
+ return prev;
+ } else if (pos == b) {
+ break;
+ }
+ prev = pos;
+ pos += oneUTF8CharForward(&tPtr->text[pos], b - pos);
+ }
+
return b;
}
--
To unsubscribe, send mail to [EMAIL PROTECTED]