Commit f1248789 authored by Lauri Tulmin's avatar Lauri Tulmin Committed by Alexandre Julliard

- 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
parent 8a985c59
...@@ -747,7 +747,9 @@ static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg, ...@@ -747,7 +747,9 @@ static LRESULT WINAPI EditWndProc_common( HWND hwnd, UINT msg,
break; break;
case EM_POSFROMCHAR: 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; break;
case EM_CHARFROMPOS: case EM_CHARFROMPOS:
...@@ -1410,6 +1412,7 @@ static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap) ...@@ -1410,6 +1412,7 @@ static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
INT index; INT index;
HDC dc; HDC dc;
HFONT old_font = 0; HFONT old_font = 0;
INT x_high = 0, x_low = 0;
if (es->style & ES_MULTILINE) { if (es->style & ES_MULTILINE) {
INT line = (y - es->format_rect.top) / es->line_height + es->y_offset; INT line = (y - es->format_rect.top) / es->line_height + es->y_offset;
...@@ -1439,15 +1442,24 @@ static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap) ...@@ -1439,15 +1442,24 @@ static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
dc = GetDC(es->hwndSelf); dc = GetDC(es->hwndSelf);
if (es->font) if (es->font)
old_font = SelectObject(dc, es->font); old_font = SelectObject(dc, es->font);
low = line_index + 1; low = line_index;
high = line_index + line_def->net_length + 1; high = line_index + line_def->net_length + 1;
while (low < high - 1) while (low < high - 1)
{ {
INT mid = (low + high) / 2; INT mid = (low + high) / 2;
if (LOWORD(GetTabbedTextExtentW(dc, es->text + line_index,mid - line_index, es->tabs_count, es->tabs)) > x) high = mid; INT x_now = LOWORD(GetTabbedTextExtentW(dc, es->text + line_index, mid - line_index, es->tabs_count, es->tabs));
else low = mid; if (x_now > x) {
high = mid;
x_high = x_now;
} else {
low = mid;
x_low = x_now;
}
} }
index = low; if (abs(x_high - x) <= abs(x_low - x) + 1)
index = high;
else
index = low;
if (after_wrap) if (after_wrap)
*after_wrap = ((index == line_index + line_def->net_length) && *after_wrap = ((index == line_index + line_def->net_length) &&
...@@ -1483,10 +1495,18 @@ static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap) ...@@ -1483,10 +1495,18 @@ static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
INT mid = (low + high) / 2; INT mid = (low + high) / 2;
GetTextExtentPoint32W( dc, text + mid, GetTextExtentPoint32W( dc, text + mid,
es->x_offset - mid, &size ); es->x_offset - mid, &size );
if (size.cx > -x) low = mid; if (size.cx > -x) {
else high = mid; low = mid;
x_low = size.cx;
} else {
high = mid;
x_high = size.cx;
}
} }
index = low; if (abs(x_high + x) <= abs(x_low + x) + 1)
index = high;
else
index = low;
} }
else else
{ {
...@@ -1497,10 +1517,18 @@ static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap) ...@@ -1497,10 +1517,18 @@ static INT EDIT_CharFromPos(EDITSTATE *es, INT x, INT y, LPBOOL after_wrap)
INT mid = (low + high) / 2; INT mid = (low + high) / 2;
GetTextExtentPoint32W( dc, text + es->x_offset, GetTextExtentPoint32W( dc, text + es->x_offset,
mid - es->x_offset, &size ); mid - es->x_offset, &size );
if (size.cx > x) high = mid; if (size.cx > x) {
else low = mid; high = mid;
x_high = size.cx;
} else {
low = mid;
x_low = size.cx;
}
} }
index = low; if (abs(x_high - x) <= abs(x_low - x) + 1)
index = high;
else
index = low;
} }
if (es->style & ES_PASSWORD) if (es->style & ES_PASSWORD)
HeapFree(GetProcessHeap(), 0, text); HeapFree(GetProcessHeap(), 0, text);
......
...@@ -797,6 +797,125 @@ static void test_edit_control_3(void) ...@@ -797,6 +797,125 @@ static void test_edit_control_3(void)
DestroyWindow(hWnd); 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) START_TEST(edit)
{ {
hinst = GetModuleHandleA (NULL); hinst = GetModuleHandleA (NULL);
...@@ -806,4 +925,5 @@ START_TEST(edit) ...@@ -806,4 +925,5 @@ START_TEST(edit)
test_edit_control_1(); test_edit_control_1();
test_edit_control_2(); test_edit_control_2();
test_edit_control_3(); test_edit_control_3();
test_edit_control_4();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment