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