- EM_CHARFROMPOS should return the closest char (clicking on the first

half of a character should return current char and clicking on the
  second half should return next char)
- EM_POSFROMCHAR should return -1 if character index is greater or
  equal to text length
This commit is contained in:
Lauri Tulmin 2005-06-14 12:24:48 +00:00 committed by Alexandre Julliard
parent 8a985c59da
commit f124878918
2 changed files with 159 additions and 11 deletions

View File

@ -747,7 +747,9 @@ static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
break;
case EM_POSFROMCHAR:
result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
result = strlenW(es->text);
if ((INT)wParam >= result) result = -1;
else result = EDIT_EM_PosFromChar(es, (INT)wParam, FALSE);
break;
case EM_CHARFROMPOS:
@ -1410,6 +1412,7 @@ static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
INT index;
HDC dc;
HFONT old_font = 0;
INT x_high = 0, x_low = 0;
if (es->style & ES_MULTILINE) {
INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
@ -1439,14 +1442,23 @@ static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
dc = GetDC(es->hwndSelf);
if (es->font)
old_font = SelectObject(dc, es->font);
low = line_index + 1;
low = line_index;
high = line_index + line_def->net_length + 1;
while (low < high - 1)
{
INT mid = (low + high) / 2;
if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid;
else low = mid;
INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
if (x_now > x) {
high = mid;
x_high = x_now;
} else {
low = mid;
x_low = x_now;
}
}
if (abs(x_high - x) <= abs(x_low - x) + 1)
index = high;
else
index = low;
if (after_wrap)
@ -1483,9 +1495,17 @@ static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
INT mid = (low + high) / 2;
GetTextExtentPoint32W( dc, text + mid,
es->x_offset - mid, &size );
if (size.cx > -x) low = mid;
else high = mid;
if (size.cx > -x) {
low = mid;
x_low = size.cx;
} else {
high = mid;
x_high = size.cx;
}
}
if (abs(x_high + x) <= abs(x_low + x) + 1)
index = high;
else
index = low;
}
else
@ -1497,9 +1517,17 @@ static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
INT mid = (low + high) / 2;
GetTextExtentPoint32W( dc, text + es->x_offset,
mid - es->x_offset, &size );
if (size.cx > x) high = mid;
else low = mid;
if (size.cx > x) {
high = mid;
x_high = size.cx;
} else {
low = mid;
x_low = size.cx;
}
}
if (abs(x_high - x) <= abs(x_low - x) + 1)
index = high;
else
index = low;
}
if (es->style & ES_PASSWORD)

View File

@ -797,6 +797,125 @@ static void test_edit_control_3(void)
DestroyWindow(hWnd);
}
/* Test EM_CHARFROMPOS and EM_POSFROMCHAR
*/
static void test_edit_control_4(void)
{
HWND hwEdit;
int lo, hi, mid;
int ret;
int i;
trace("EDIT: Test EM_CHARFROMPOS and EM_POSFROMCHAR\n");
hwEdit = create_editcontrol(0, 0);
SendMessage(hwEdit, WM_SETTEXT, 0, (LPARAM) "aa");
lo = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 0, 0));
hi = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 1, 0));
mid = lo + (hi - lo) / 2;
for (i = lo; i < mid; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(0 == ret, "expected 0 got %d\n", ret);
}
for (i = mid; i <= hi; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(1 == ret, "expected 1 got %d\n", ret);
}
ret = SendMessage(hwEdit, EM_POSFROMCHAR, 2, 0);
ok(-1 == ret, "expected -1 got %d\n", ret);
DestroyWindow(hwEdit);
hwEdit = create_editcontrol(ES_RIGHT, 0);
SendMessage(hwEdit, WM_SETTEXT, 0, (LPARAM) "aa");
lo = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 0, 0));
hi = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 1, 0));
mid = lo + (hi - lo) / 2;
for (i = lo; i < mid; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(0 == ret, "expected 0 got %d\n", ret);
}
for (i = mid; i <= hi; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(1 == ret, "expected 1 got %d\n", ret);
}
ret = SendMessage(hwEdit, EM_POSFROMCHAR, 2, 0);
ok(-1 == ret, "expected -1 got %d\n", ret);
DestroyWindow(hwEdit);
hwEdit = create_editcontrol(ES_CENTER, 0);
SendMessage(hwEdit, WM_SETTEXT, 0, (LPARAM) "aa");
lo = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 0, 0));
hi = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 1, 0));
mid = lo + (hi - lo) / 2;
for (i = lo; i < mid; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(0 == ret, "expected 0 got %d\n", ret);
}
for (i = mid; i <= hi; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(1 == ret, "expected 1 got %d\n", ret);
}
ret = SendMessage(hwEdit, EM_POSFROMCHAR, 2, 0);
ok(-1 == ret, "expected -1 got %d\n", ret);
DestroyWindow(hwEdit);
hwEdit = create_editcontrol(ES_MULTILINE, 0);
SendMessage(hwEdit, WM_SETTEXT, 0, (LPARAM) "aa");
lo = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 0, 0));
hi = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 1, 0));
mid = lo + (hi - lo) / 2;
for (i = lo; i < mid; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(0 == ret, "expected 0 got %d\n", ret);
}
for (i = mid; i <= hi; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(1 == ret, "expected 1 got %d\n", ret);
}
ret = SendMessage(hwEdit, EM_POSFROMCHAR, 2, 0);
ok(-1 == ret, "expected -1 got %d\n", ret);
DestroyWindow(hwEdit);
hwEdit = create_editcontrol(ES_MULTILINE | ES_RIGHT, 0);
SendMessage(hwEdit, WM_SETTEXT, 0, (LPARAM) "aa");
lo = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 0, 0));
hi = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 1, 0));
mid = lo + (hi - lo) / 2;
for (i = lo; i < mid; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(0 == ret, "expected 0 got %d\n", ret);
}
for (i = mid; i <= hi; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(1 == ret, "expected 1 got %d\n", ret);
}
ret = SendMessage(hwEdit, EM_POSFROMCHAR, 2, 0);
ok(-1 == ret, "expected -1 got %d\n", ret);
DestroyWindow(hwEdit);
hwEdit = create_editcontrol(ES_MULTILINE | ES_CENTER, 0);
SendMessage(hwEdit, WM_SETTEXT, 0, (LPARAM) "aa");
lo = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 0, 0));
hi = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 1, 0));
mid = lo + (hi - lo) / 2;
for (i = lo; i < mid; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(0 == ret, "expected 0 got %d\n", ret);
}
for (i = mid; i <= hi; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(1 == ret, "expected 1 got %d\n", ret);
}
ret = SendMessage(hwEdit, EM_POSFROMCHAR, 2, 0);
ok(-1 == ret, "expected -1 got %d\n", ret);
DestroyWindow(hwEdit);
}
START_TEST(edit)
{
hinst = GetModuleHandleA (NULL);
@ -806,4 +925,5 @@ START_TEST(edit)
test_edit_control_1();
test_edit_control_2();
test_edit_control_3();
test_edit_control_4();
}