Add ICU message format support
[chromium-blink-merge.git] / ui / base / ime / win / tsf_input_scope.cc
bloba1ba89a6c23dbba19805619bbf57fd3b921c8c1d
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/base/ime/win/tsf_input_scope.h"
7 #include <algorithm>
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/win/windows_version.h"
14 namespace ui {
15 namespace tsf_inputscope {
16 namespace {
18 void AppendNonTrivialInputScope(std::vector<InputScope>* input_scopes,
19 InputScope input_scope) {
20 DCHECK(input_scopes);
22 if (input_scope == IS_DEFAULT)
23 return;
25 if (std::find(input_scopes->begin(), input_scopes->end(), input_scope) !=
26 input_scopes->end())
27 return;
29 input_scopes->push_back(input_scope);
32 class TSFInputScope final : public ITfInputScope {
33 public:
34 explicit TSFInputScope(const std::vector<InputScope>& input_scopes)
35 : input_scopes_(input_scopes),
36 ref_count_(0) {}
38 // ITfInputScope:
39 STDMETHOD_(ULONG, AddRef)() override {
40 return InterlockedIncrement(&ref_count_);
43 STDMETHOD_(ULONG, Release)() override {
44 const LONG count = InterlockedDecrement(&ref_count_);
45 if (!count) {
46 delete this;
47 return 0;
49 return static_cast<ULONG>(count);
52 STDMETHOD(QueryInterface)(REFIID iid, void** result) override {
53 if (!result)
54 return E_INVALIDARG;
55 if (iid == IID_IUnknown || iid == IID_ITfInputScope) {
56 *result = static_cast<ITfInputScope*>(this);
57 } else {
58 *result = NULL;
59 return E_NOINTERFACE;
61 AddRef();
62 return S_OK;
65 STDMETHOD(GetInputScopes)(InputScope** input_scopes, UINT* count) override {
66 if (!count || !input_scopes)
67 return E_INVALIDARG;
68 *input_scopes = static_cast<InputScope*>(CoTaskMemAlloc(
69 sizeof(InputScope) * input_scopes_.size()));
70 if (!input_scopes) {
71 *count = 0;
72 return E_OUTOFMEMORY;
75 for (size_t i = 0; i < input_scopes_.size(); ++i)
76 (*input_scopes)[i] = input_scopes_[i];
77 *count = input_scopes_.size();
78 return S_OK;
81 STDMETHOD(GetPhrase)(BSTR** phrases, UINT* count) override {
82 return E_NOTIMPL;
85 STDMETHOD(GetRegularExpression)(BSTR* regexp) override {
86 return E_NOTIMPL;
89 STDMETHOD(GetSRGS)(BSTR* srgs) override {
90 return E_NOTIMPL;
93 STDMETHOD(GetXML)(BSTR* xml) override {
94 return E_NOTIMPL;
97 private:
98 // The corresponding text input types.
99 std::vector<InputScope> input_scopes_;
101 // The refrence count of this instance.
102 volatile LONG ref_count_;
104 DISALLOW_COPY_AND_ASSIGN(TSFInputScope);
107 typedef HRESULT (WINAPI *SetInputScopesFunc)(HWND window_handle,
108 const InputScope* input_scope_list,
109 UINT num_input_scopes,
110 WCHAR**, /* unused */
111 UINT, /* unused */
112 WCHAR*, /* unused */
113 WCHAR* /* unused */);
115 SetInputScopesFunc g_set_input_scopes = NULL;
116 bool g_get_proc_done = false;
118 InputScope ConvertTextInputTypeToInputScope(TextInputType text_input_type) {
119 // Following mapping is based in IE10 on Windows 8.
120 switch (text_input_type) {
121 case TEXT_INPUT_TYPE_PASSWORD:
122 return IS_PASSWORD;
123 case TEXT_INPUT_TYPE_SEARCH:
124 return IS_SEARCH;
125 case TEXT_INPUT_TYPE_EMAIL:
126 return IS_EMAIL_SMTPEMAILADDRESS;
127 case TEXT_INPUT_TYPE_NUMBER:
128 return IS_NUMBER;
129 case TEXT_INPUT_TYPE_TELEPHONE:
130 return IS_TELEPHONE_FULLTELEPHONENUMBER;
131 case TEXT_INPUT_TYPE_URL:
132 return IS_URL;
133 default:
134 return IS_DEFAULT;
138 InputScope ConvertTextInputModeToInputScope(TextInputMode text_input_mode) {
139 switch (text_input_mode) {
140 case TEXT_INPUT_MODE_FULL_WIDTH_LATIN:
141 return IS_ALPHANUMERIC_FULLWIDTH;
142 case TEXT_INPUT_MODE_KANA:
143 return IS_HIRAGANA;
144 case TEXT_INPUT_MODE_KATAKANA:
145 return IS_KATAKANA_FULLWIDTH;
146 case TEXT_INPUT_MODE_NUMERIC:
147 return IS_NUMBER;
148 case TEXT_INPUT_MODE_TEL:
149 return IS_TELEPHONE_FULLTELEPHONENUMBER;
150 case TEXT_INPUT_MODE_EMAIL:
151 return IS_EMAIL_SMTPEMAILADDRESS;
152 case TEXT_INPUT_MODE_URL:
153 return IS_URL;
154 default:
155 return IS_DEFAULT;
159 } // namespace
161 void InitializeTsfForInputScopes() {
162 DCHECK(base::MessageLoopForUI::IsCurrent());
163 // Thread safety is not required because this function is under UI thread.
164 if (!g_get_proc_done) {
165 g_get_proc_done = true;
167 // For stability reasons, we do not support Windows XP.
168 if (base::win::GetVersion() < base::win::VERSION_VISTA)
169 return;
171 HMODULE module = NULL;
172 if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, L"msctf.dll",
173 &module)) {
174 g_set_input_scopes = reinterpret_cast<SetInputScopesFunc>(
175 GetProcAddress(module, "SetInputScopes"));
180 std::vector<InputScope> GetInputScopes(TextInputType text_input_type,
181 TextInputMode text_input_mode) {
182 std::vector<InputScope> input_scopes;
184 AppendNonTrivialInputScope(&input_scopes,
185 ConvertTextInputTypeToInputScope(text_input_type));
186 AppendNonTrivialInputScope(&input_scopes,
187 ConvertTextInputModeToInputScope(text_input_mode));
189 if (input_scopes.empty())
190 input_scopes.push_back(IS_DEFAULT);
192 return input_scopes;
195 ITfInputScope* CreateInputScope(TextInputType text_input_type,
196 TextInputMode text_input_mode) {
197 return new TSFInputScope(GetInputScopes(text_input_type, text_input_mode));
200 void SetInputScopeForTsfUnawareWindow(HWND window_handle,
201 TextInputType text_input_type,
202 TextInputMode text_input_mode) {
203 if (!g_set_input_scopes)
204 return;
206 std::vector<InputScope> input_scopes = GetInputScopes(text_input_type,
207 text_input_mode);
208 g_set_input_scopes(window_handle, &input_scopes[0], input_scopes.size(),
209 NULL, 0, NULL, NULL);
212 } // namespace tsf_inputscope
213 } // namespace ui