| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "ui/base/ime/text_input_client.h" | |
| 6 #include "views/ime/input_method_base.h" | |
| 7 #include "views/ime/text_input_type_tracker.h" | |
| 8 #include "views/view.h" | |
| 9 #include "views/widget/widget.h" | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 | |
| 13 namespace views { | |
| 14 | |
| 15 InputMethodBase::InputMethodBase() | |
| 16 : delegate_(NULL), | |
| 17 widget_(NULL), | |
| 18 widget_focused_(false) { | |
| 19 } | |
| 20 | |
| 21 InputMethodBase::~InputMethodBase() { | |
| 22 if (widget_) { | |
| 23 widget_->GetFocusManager()->RemoveFocusChangeListener(this); | |
| 24 widget_ = NULL; | |
| 25 } | |
| 26 } | |
| 27 | |
| 28 void InputMethodBase::set_delegate(internal::InputMethodDelegate* delegate) { | |
| 29 DCHECK(delegate); | |
| 30 delegate_ = delegate; | |
| 31 } | |
| 32 | |
| 33 void InputMethodBase::Init(Widget* widget) { | |
| 34 DCHECK(widget); | |
| 35 DCHECK(widget->GetFocusManager()); | |
| 36 | |
| 37 if (widget_) { | |
| 38 NOTREACHED() << "The input method is already initialized."; | |
| 39 return; | |
| 40 } | |
| 41 | |
| 42 widget_ = widget; | |
| 43 // The InputMethod is lazily created, so we need to tell it the currently | |
| 44 // focused view. | |
| 45 View* focused = widget->GetFocusManager()->GetFocusedView(); | |
| 46 if (focused) | |
| 47 OnWillChangeFocus(NULL, focused); | |
| 48 widget->GetFocusManager()->AddFocusChangeListener(this); | |
| 49 } | |
| 50 | |
| 51 void InputMethodBase::OnFocus() { | |
| 52 widget_focused_ = true; | |
| 53 TextInputTypeTracker::GetInstance()->OnTextInputTypeChanged( | |
| 54 GetTextInputType(), widget_); | |
| 55 } | |
| 56 | |
| 57 void InputMethodBase::OnBlur() { | |
| 58 widget_focused_ = false; | |
| 59 TextInputTypeTracker::GetInstance()->OnTextInputTypeChanged( | |
| 60 GetTextInputType(), widget_); | |
| 61 } | |
| 62 | |
| 63 views::View* InputMethodBase::GetFocusedView() const { | |
| 64 return widget_->GetFocusManager()->GetFocusedView(); | |
| 65 } | |
| 66 | |
| 67 void InputMethodBase::OnTextInputTypeChanged(View* view) { | |
| 68 if (IsViewFocused(view)) { | |
| 69 TextInputTypeTracker::GetInstance()->OnTextInputTypeChanged( | |
| 70 GetTextInputType(), widget_); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 ui::TextInputClient* InputMethodBase::GetTextInputClient() const { | |
| 75 return (widget_focused_ && GetFocusedView()) ? | |
| 76 GetFocusedView()->GetTextInputClient() : NULL; | |
| 77 } | |
| 78 | |
| 79 ui::TextInputType InputMethodBase::GetTextInputType() const { | |
| 80 ui::TextInputClient* client = GetTextInputClient(); | |
| 81 return client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; | |
| 82 } | |
| 83 | |
| 84 bool InputMethodBase::IsMock() const { | |
| 85 return false; | |
| 86 } | |
| 87 | |
| 88 void InputMethodBase::OnWillChangeFocus(View* focused_before, View* focused) { | |
| 89 } | |
| 90 | |
| 91 void InputMethodBase::OnDidChangeFocus(View* focused_before, View* focused) { | |
| 92 if (widget_focused_) { | |
| 93 TextInputTypeTracker::GetInstance()->OnTextInputTypeChanged( | |
| 94 GetTextInputType(), widget_); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 bool InputMethodBase::IsViewFocused(View* view) const { | |
| 99 return widget_focused_ && view && GetFocusedView() == view; | |
| 100 } | |
| 101 | |
| 102 bool InputMethodBase::IsTextInputTypeNone() const { | |
| 103 return GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE; | |
| 104 } | |
| 105 | |
| 106 void InputMethodBase::OnInputMethodChanged() const { | |
| 107 ui::TextInputClient* client = GetTextInputClient(); | |
| 108 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) | |
| 109 client->OnInputMethodChanged(); | |
| 110 } | |
| 111 | |
| 112 void InputMethodBase::DispatchKeyEventPostIME(const KeyEvent& key) const { | |
| 113 if (delegate_) | |
| 114 delegate_->DispatchKeyEventPostIME(key); | |
| 115 } | |
| 116 | |
| 117 bool InputMethodBase::GetCaretBoundsInWidget(gfx::Rect* rect) const { | |
| 118 DCHECK(rect); | |
| 119 ui::TextInputClient* client = GetTextInputClient(); | |
| 120 if (!client || client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) | |
| 121 return false; | |
| 122 | |
| 123 *rect = GetFocusedView()->ConvertRectToWidget(client->GetCaretBounds()); | |
| 124 | |
| 125 // We need to do coordinate conversion if the focused view is inside a child | |
| 126 // Widget. | |
| 127 if (GetFocusedView()->GetWidget() != widget_) | |
| 128 return Widget::ConvertRect(GetFocusedView()->GetWidget(), widget_, rect); | |
| 129 return true; | |
| 130 } | |
| 131 } // namespace views | |
| OLD | NEW |