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/views/ime/input_method_base.h"
7 #include "base/logging.h"
8 #include "ui/base/ime/text_input_client.h"
9 #include "ui/events/event.h"
10 #include "ui/views/view.h"
11 #include "ui/views/widget/widget.h"
15 InputMethodBase::InputMethodBase() : delegate_(NULL
), widget_(NULL
) {}
17 InputMethodBase::~InputMethodBase() {
21 void InputMethodBase::SetDelegate(internal::InputMethodDelegate
* delegate
) {
26 void InputMethodBase::Init(Widget
* widget
) {
28 DCHECK(widget
->GetFocusManager());
29 DCHECK(!widget_
) << "The input method is already initialized.";
32 // Alert the InputMethod of the Widget's currently focused view.
33 View
* focused
= widget
->GetFocusManager()->GetFocusedView();
35 OnWillChangeFocus(NULL
, focused
);
36 widget
->GetFocusManager()->AddFocusChangeListener(this);
39 views::View
* InputMethodBase::GetFocusedView() const {
40 return widget_
? widget_
->GetFocusManager()->GetFocusedView() : NULL
;
43 void InputMethodBase::OnTextInputTypeChanged(View
* view
) {}
45 ui::TextInputClient
* InputMethodBase::GetTextInputClient() const {
46 return (widget_
&& widget_
->IsActive() && GetFocusedView()) ?
47 GetFocusedView()->GetTextInputClient() : NULL
;
50 ui::TextInputType
InputMethodBase::GetTextInputType() const {
51 ui::TextInputClient
* client
= GetTextInputClient();
52 return client
? client
->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE
;
55 bool InputMethodBase::IsMock() const {
59 void InputMethodBase::OnWillChangeFocus(View
* focused_before
, View
* focused
) {}
61 void InputMethodBase::OnDidChangeFocus(View
* focused_before
, View
* focused
) {}
63 bool InputMethodBase::IsViewFocused(View
* view
) const {
64 return widget_
&& widget_
->IsActive() && view
&& GetFocusedView() == view
;
67 bool InputMethodBase::IsTextInputTypeNone() const {
68 return GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE
;
71 void InputMethodBase::OnInputMethodChanged() const {
72 ui::TextInputClient
* client
= GetTextInputClient();
73 if (client
&& client
->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE
)
74 client
->OnInputMethodChanged();
77 void InputMethodBase::DispatchKeyEventPostIME(const ui::KeyEvent
& key
) const {
79 delegate_
->DispatchKeyEventPostIME(key
);
82 bool InputMethodBase::GetCaretBoundsInWidget(gfx::Rect
* rect
) const {
84 ui::TextInputClient
* client
= GetTextInputClient();
85 if (!client
|| client
->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE
)
88 gfx::Rect caret_bounds
= client
->GetCaretBounds();
89 gfx::Point caret_origin
= caret_bounds
.origin();
90 View::ConvertPointFromScreen(GetFocusedView(), &caret_origin
);
91 caret_bounds
.set_origin(caret_origin
);
92 *rect
= GetFocusedView()->ConvertRectToWidget(caret_bounds
);
94 // Convert coordinates if the focused view is inside a child Widget.
95 if (GetFocusedView()->GetWidget() != widget_
)
96 return Widget::ConvertRect(GetFocusedView()->GetWidget(), widget_
, rect
);
100 void InputMethodBase::DetachFromWidget() {
104 widget_
->GetFocusManager()->RemoveFocusChangeListener(this);