1
0
mirror of https://github.com/gryf/wmaker.git synced 2026-01-26 01:15:52 +01:00

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.
This commit is contained in:
dan
2006-04-27 02:48:18 +00:00
parent 577b3ee949
commit ecd5217633
2 changed files with 32 additions and 13 deletions

View File

@@ -572,6 +572,7 @@ testTextField(WMScreen *scr)
field = WMCreateTextField(win); field = WMCreateTextField(win);
WMResizeWidget(field, 200, 20); WMResizeWidget(field, 200, 20);
WMMoveWidget(field, 20, 20); WMMoveWidget(field, 20, 20);
WMSetTextFieldText(field, "the little \xc2\xa9 sign");
field2 = WMCreateTextField(win); field2 = WMCreateTextField(win);
WMResizeWidget(field2, 200, 20); WMResizeWidget(field2, 200, 20);

View File

@@ -1375,35 +1375,53 @@ handleTextFieldKeyPress(TextField *tPtr, XEvent *event)
static int static int
pointToCursorPosition(TextField *tPtr, int x) pointToCursorPosition(TextField *tPtr, int x)
{ {
int a, b, mid; int a, b, pos, prev, tw;
int tw;
if (tPtr->flags.bordered) if (tPtr->flags.bordered)
x -= 2; x -= 2;
if (WMWidthOfString(tPtr->font, &(tPtr->text[tPtr->viewPosition]), if (WMWidthOfString(tPtr->font, &(tPtr->text[tPtr->viewPosition]),
tPtr->textLen - tPtr->viewPosition) < x) tPtr->textLen - tPtr->viewPosition) <= x)
return tPtr->textLen; return tPtr->textLen;
a = tPtr->viewPosition; a = tPtr->viewPosition;
b = tPtr->textLen; b = tPtr->textLen;
while (a < b) { /* we halve the text until we get into a 10 byte vicinity of x */
mid = (a+b)/2; while (b - a > 10) {
mid += seekUTF8CharStart(&tPtr->text[mid], mid - a); pos = (a+b)/2;
pos += seekUTF8CharStart(&tPtr->text[pos], pos - a);
tw = WMWidthOfString(tPtr->font, &(tPtr->text[tPtr->viewPosition]), tw = WMWidthOfString(tPtr->font, &(tPtr->text[tPtr->viewPosition]),
mid - tPtr->viewPosition + 1); pos - tPtr->viewPosition);
if (tw > x) { if (tw > x) {
b = mid; b = pos;
} else if (tw < x) { } else if (tw < x) {
if (a == mid) a = pos;
a += oneUTF8CharForward(&tPtr->text[mid], b - a);
else
a = mid;
} else { } 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; return b;
} }