Add include.
[chromium-blink-merge.git] / ui / views / ime / input_method_base.cc
blob784746b7e371eec0601b0162814450d647b3a56b
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"
13 namespace views {
15 InputMethodBase::InputMethodBase() : delegate_(NULL), widget_(NULL) {}
17 InputMethodBase::~InputMethodBase() {
18 DetachFromWidget();
21 void InputMethodBase::SetDelegate(internal::InputMethodDelegate* delegate) {
22 DCHECK(delegate);
23 delegate_ = delegate;
26 void InputMethodBase::Init(Widget* widget) {
27 DCHECK(widget);
28 DCHECK(widget->GetFocusManager());
29 DCHECK(!widget_) << "The input method is already initialized.";
31 widget_ = widget;
32 // Alert the InputMethod of the Widget's currently focused view.
33 View* focused = widget->GetFocusManager()->GetFocusedView();
34 if (focused)
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 {
56 return false;
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 {
78 if (delegate_)
79 delegate_->DispatchKeyEventPostIME(key);
82 bool InputMethodBase::GetCaretBoundsInWidget(gfx::Rect* rect) const {
83 DCHECK(rect);
84 ui::TextInputClient* client = GetTextInputClient();
85 if (!client || client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE)
86 return false;
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);
97 return true;
100 void InputMethodBase::DetachFromWidget() {
101 if (!widget_)
102 return;
104 widget_->GetFocusManager()->RemoveFocusChangeListener(this);
105 widget_ = NULL;
108 } // namespace views