Index: views/ime/input_method_win.cc |
diff --git a/views/ime/input_method_win.cc b/views/ime/input_method_win.cc |
index 4ab1a187378d389ba70bf8dc6d3131b107afa847..e9eeb070d4cb41665e0a5eb60c356bdce0ac8219 100644 |
--- a/views/ime/input_method_win.cc |
+++ b/views/ime/input_method_win.cc |
@@ -10,6 +10,8 @@ |
#include "ui/base/keycodes/keyboard_codes.h" |
#include "views/events/event.h" |
+static const size_t kExtraNumberOfChars = 100; |
James Su
2011/10/28 04:20:45
I think 10~20 should be enough.
And how about to a
Peng
2011/10/28 15:42:19
Done.
|
+ |
namespace views { |
InputMethodWin::InputMethodWin(internal::InputMethodDelegate* delegate) |
@@ -115,6 +117,9 @@ LRESULT InputMethodWin::OnImeMessages( |
case WM_IME_ENDCOMPOSITION: |
result = OnImeEndComposition(message, w_param, l_param, handled); |
break; |
+ case WM_IME_REQUEST: |
+ result = OnImeRequest(message, w_param, l_param, handled); |
+ break; |
case WM_CHAR: |
case WM_SYSCHAR: |
result = OnChar(message, w_param, l_param, handled); |
@@ -219,6 +224,29 @@ LRESULT InputMethodWin::OnImeEndComposition( |
return 0; |
} |
+LRESULT InputMethodWin::OnImeRequest( |
+ UINT message, WPARAM wparam, LPARAM lparam, BOOL* handled) { |
+ *handled = FALSE; |
+ |
+ // Should not receive WM_IME_REQUEST message, if IME is disabled. |
+ const ui::TextInputType type = GetTextInputType(); |
+ if (type == ui::TEXT_INPUT_TYPE_NONE || |
+ type == ui::TEXT_INPUT_TYPE_PASSWORD) { |
+ return 0; |
+ } |
+ |
+ switch (wparam) { |
+ case IMR_RECONVERTSTRING: |
+ *handled = TRUE; |
+ return OnReconvertString(reinterpret_cast<RECONVERTSTRING*>(lparam)); |
+ case IMR_DOCUMENTFEED: |
+ *handled = TRUE; |
+ return OnDocumentFeed(reinterpret_cast<RECONVERTSTRING*>(lparam)); |
+ default: |
+ return 0; |
+ } |
+} |
+ |
LRESULT InputMethodWin::OnChar( |
UINT message, WPARAM wparam, LPARAM lparam, BOOL* handled) { |
*handled = TRUE; |
@@ -254,6 +282,113 @@ LRESULT InputMethodWin::OnDeadChar( |
return 0; |
} |
+LRESULT InputMethodWin::OnDocumentFeed(RECONVERTSTRING *reconv) { |
James Su
2011/10/28 04:20:45
Per our discussion, feel free to remove this metho
|
+ TextInputClient* client = GetTextInputClient(); |
+ if (!client) |
+ return 0; |
+ |
+ ui::Range text_range; |
+ if (!client->GetTextRange(&text_range) || text_range.is_empty()) |
+ return 0; |
+ |
+ bool result = false; |
+ ui::Range target_range; |
+ if (client->HasCompositionText()) |
+ result = client->GetCompositionTextRange(&target_range); |
+ |
+ if (!result || target_range.is_empty()) { |
+ if (!client->GetSelectionRange(&target_range) || |
+ !target_range.IsValid()) { |
+ return 0; |
+ } |
+ } |
+ |
+ if (!text_range.Contains(target_range)) |
+ return 0; |
+ |
+ if (target_range.GetMin() - text_range.start() > kExtraNumberOfChars) |
+ text_range.set_start(target_range.GetMin() - kExtraNumberOfChars); |
+ |
+ if (text_range.end() - target_range.GetMax() > kExtraNumberOfChars) |
+ text_range.set_end(target_range.GetMax() + kExtraNumberOfChars); |
+ |
+ size_t len = text_range.length(); |
+ size_t need_size = sizeof(RECONVERTSTRING) + len * sizeof(WCHAR); |
+ |
+ if (!reconv) |
+ return need_size; |
+ |
+ if (reconv->dwSize < need_size) |
+ return 0; |
+ |
+ string16 text; |
+ if (!GetTextInputClient()->GetTextFromRange(text_range, &text)) |
+ return 0; |
+ |
+ reconv->dwVersion = 0; |
+ reconv->dwStrLen = len; |
+ reconv->dwStrOffset = sizeof(RECONVERTSTRING); |
+ reconv->dwCompStrLen = |
+ client->HasCompositionText() ? target_range.length() : 0; |
+ reconv->dwCompStrOffset = |
+ (target_range.GetMin() - text_range.start()) * sizeof(WCHAR); |
+ reconv->dwTargetStrLen = target_range.length(); |
+ reconv->dwTargetStrOffset = reconv->dwCompStrOffset; |
+ |
+ memcpy((char*)reconv + sizeof(RECONVERTSTRING), |
+ text.c_str(), len * sizeof(WCHAR)); |
+ |
+ return reinterpret_cast<LRESULT>(reconv); |
+} |
+ |
+LRESULT InputMethodWin::OnReconvertString(RECONVERTSTRING *reconv) { |
+ TextInputClient* client = GetTextInputClient(); |
+ if (!client) |
+ return 0; |
+ |
+ // If there is a composition string already, we don't allow reconversion. |
+ if (client->HasCompositionText()) |
+ return 0; |
+ |
+ ui::Range text_range; |
+ if (!client->GetTextRange(&text_range) || text_range.is_empty()) |
+ return 0; |
+ |
+ ui::Range selection_range; |
+ if (!client->GetSelectionRange(&selection_range) || |
+ selection_range.is_empty()) { |
+ return 0; |
+ } |
+ |
+ DCHECK(text_range.Contains(selection_range)); |
+ |
+ size_t len = selection_range.length(); |
James Su
2011/10/28 04:20:45
If you really don't want to return extra surroundi
Peng
2011/10/28 15:42:19
Done.
|
+ size_t need_size = sizeof(RECONVERTSTRING) + len * sizeof(WCHAR); |
+ |
+ if (!reconv) |
+ return need_size; |
+ |
+ if (reconv->dwSize < need_size) |
+ return 0; |
+ |
+ string16 text; |
+ if (!GetTextInputClient()->GetTextFromRange(selection_range, &text)) |
James Su
2011/10/28 04:20:45
nit: check the text length in case it's shorter th
Peng
2011/10/28 15:42:19
The API guarantee the text has right size with giv
|
+ return 0; |
+ |
+ reconv->dwVersion = 0; |
+ reconv->dwStrLen = len; |
+ reconv->dwStrOffset = sizeof(RECONVERTSTRING); |
+ reconv->dwCompStrLen = len; |
+ reconv->dwCompStrOffset = 0; |
+ reconv->dwTargetStrLen = len; |
+ reconv->dwTargetStrOffset = 0; |
+ |
+ memcpy(reinterpret_cast<char*>(reconv) + sizeof(RECONVERTSTRING), |
+ text.c_str(), len * sizeof(WCHAR)); |
+ |
+ return reinterpret_cast<LRESULT>(reconv); |
James Su
2011/10/28 04:20:45
Though msdn says that this method should return th
Peng
2011/10/28 15:42:19
I am not sure. Most IME just tests the return valu
|
+} |
+ |
void InputMethodWin::ConfirmCompositionText() { |
if (!IsTextInputTypeNone()) { |
ime_input_.CleanupComposition(hwnd()); |