1 // Copyright 2013 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 "win8/metro_driver/ime/input_scope.h"
10 #include "base/logging.h"
11 #include "ui/base/win/atl_module.h"
13 namespace metro_driver
{
16 // An implementation of ITfInputScope interface.
17 // This implementation only covers ITfInputScope::GetInputScopes since built-in
18 // on-screen keyboard on Windows 8+ changes its layout depending on the returned
19 // value of this method.
20 // Although other advanced features of ITfInputScope such as phase list or
21 // regex support might be useful for IMEs or on-screen keyboards in future,
22 // no IME seems to be utilizing such features as of Windows 8.1.
23 class ATL_NO_VTABLE InputScopeImpl
24 : public CComObjectRootEx
<CComMultiThreadModel
>,
25 public ITfInputScope
{
29 BEGIN_COM_MAP(InputScopeImpl
)
30 COM_INTERFACE_ENTRY(ITfInputScope
)
33 void Initialize(const std::vector
<InputScope
>& input_scopes
) {
34 input_scopes_
= input_scopes
;
38 // ITfInputScope overrides:
39 STDMETHOD(GetInputScopes
)(InputScope
** input_scopes
, UINT
* count
) OVERRIDE
{
40 if (!count
|| !input_scopes
)
42 *input_scopes
= static_cast<InputScope
*>(
43 CoTaskMemAlloc(sizeof(InputScope
) * input_scopes_
.size()));
48 std::copy(input_scopes_
.begin(), input_scopes_
.end(), *input_scopes
);
49 *count
= static_cast<UINT
>(input_scopes_
.size());
52 STDMETHOD(GetPhrase
)(BSTR
** phrases
, UINT
* count
) OVERRIDE
{
55 STDMETHOD(GetRegularExpression
)(BSTR
* regexp
) OVERRIDE
{
58 STDMETHOD(GetSRGS
)(BSTR
* srgs
) OVERRIDE
{
61 STDMETHOD(GetXML
)(BSTR
* xml
) OVERRIDE
{
65 // Data which ITfInputScope::GetInputScopes should return.
66 std::vector
<InputScope
> input_scopes_
;
68 DISALLOW_COPY_AND_ASSIGN(InputScopeImpl
);
73 base::win::ScopedComPtr
<ITfInputScope
>
74 CreteInputScope(const std::vector
<InputScope
>& input_scopes
) {
75 ui::win::CreateATLModuleIfNeeded();
76 CComObject
<InputScopeImpl
>* object
= NULL
;
77 HRESULT hr
= CComObject
<InputScopeImpl
>::CreateInstance(&object
);
79 LOG(ERROR
) << "CComObject<InputScopeImpl>::CreateInstance failed. hr = "
81 return base::win::ScopedComPtr
<ITfInputScope
>();
83 object
->Initialize(input_scopes
);
84 return base::win::ScopedComPtr
<ITfInputScope
>(object
);
87 } // namespace metro_driver