Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / views / ime / input_method_base.cc
blob6eb517952dec9161070c5fc1296f9660e25e481c
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 void InputMethodBase::OnWillChangeFocus(View* focused_before, View* focused) {}
57 void InputMethodBase::OnDidChangeFocus(View* focused_before, View* focused) {}
59 bool InputMethodBase::IsViewFocused(View* view) const {
60 return widget_ && widget_->IsActive() && view && GetFocusedView() == view;
63 bool InputMethodBase::IsTextInputTypeNone() const {
64 return GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE;
67 void InputMethodBase::OnInputMethodChanged() const {
68 ui::TextInputClient* client = GetTextInputClient();
69 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE)
70 client->OnInputMethodChanged();
73 void InputMethodBase::DispatchKeyEventPostIME(const ui::KeyEvent& key) const {
74 if (delegate_)
75 delegate_->DispatchKeyEventPostIME(key);
78 bool InputMethodBase::GetCaretBoundsInWidget(gfx::Rect* rect) const {
79 DCHECK(rect);
80 ui::TextInputClient* client = GetTextInputClient();
81 if (!client || client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE)
82 return false;
84 gfx::Rect caret_bounds = client->GetCaretBounds();
85 gfx::Point caret_origin = caret_bounds.origin();
86 View::ConvertPointFromScreen(GetFocusedView(), &caret_origin);
87 caret_bounds.set_origin(caret_origin);
88 *rect = GetFocusedView()->ConvertRectToWidget(caret_bounds);
90 // Convert coordinates if the focused view is inside a child Widget.
91 if (GetFocusedView()->GetWidget() != widget_)
92 return Widget::ConvertRect(GetFocusedView()->GetWidget(), widget_, rect);
93 return true;
96 void InputMethodBase::DetachFromWidget() {
97 if (!widget_)
98 return;
100 widget_->GetFocusManager()->RemoveFocusChangeListener(this);
101 widget_ = NULL;
104 } // namespace views