Chromium Code Reviews| 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 if (widget()) |
|
sky
2013/03/27 15:10:23
Is this needed? You don't null check widget any wh
msw
2013/03/27 17:36:25
Done; I removed the check, but will restore it if
| |
| 19 widget()->GetFocusManager()->RemoveFocusChangeListener(this); | |
| 20 widget_ = NULL; | |
| 20 } | 21 } |
| 21 | 22 |
| 22 InputMethodBase::~InputMethodBase() { | 23 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); | 24 DCHECK(delegate); |
| 31 delegate_ = delegate; | 25 delegate_ = delegate; |
| 32 } | 26 } |
| 33 | 27 |
| 34 void InputMethodBase::Init(Widget* widget) { | 28 void InputMethodBase::Init(Widget* widget) { |
| 35 DCHECK(widget); | 29 DCHECK(widget); |
| 36 DCHECK(widget->GetFocusManager()); | 30 DCHECK(widget->GetFocusManager()); |
| 37 | 31 DCHECK(!widget_) << "The input method is already initialized."; |
| 38 if (widget_) { | |
| 39 NOTREACHED() << "The input method is already initialized."; | |
| 40 return; | |
| 41 } | |
| 42 | 32 |
| 43 widget_ = widget; | 33 widget_ = widget; |
| 44 // The InputMethod is lazily created, so we need to tell it the currently | 34 // Alert the InputMethod of the Widget's currently focused view. |
| 45 // focused view. | |
| 46 View* focused = widget->GetFocusManager()->GetFocusedView(); | 35 View* focused = widget->GetFocusManager()->GetFocusedView(); |
| 47 if (focused) | 36 if (focused) |
| 48 OnWillChangeFocus(NULL, focused); | 37 OnWillChangeFocus(NULL, focused); |
| 49 widget->GetFocusManager()->AddFocusChangeListener(this); | 38 widget->GetFocusManager()->AddFocusChangeListener(this); |
| 50 } | 39 } |
| 51 | 40 |
| 52 void InputMethodBase::OnFocus() { | 41 views::View* InputMethodBase::GetFocusedView() const { |
| 53 widget_focused_ = true; | 42 return widget()->GetFocusManager()->GetFocusedView(); |
| 54 } | 43 } |
| 55 | 44 |
| 56 void InputMethodBase::OnBlur() { | 45 void InputMethodBase::OnTextInputTypeChanged(View* view) {} |
| 57 widget_focused_ = false; | |
| 58 } | |
| 59 | |
| 60 views::View* InputMethodBase::GetFocusedView() const { | |
| 61 return widget_->GetFocusManager()->GetFocusedView(); | |
| 62 } | |
| 63 | |
| 64 void InputMethodBase::OnTextInputTypeChanged(View* view) { | |
| 65 } | |
| 66 | 46 |
| 67 ui::TextInputClient* InputMethodBase::GetTextInputClient() const { | 47 ui::TextInputClient* InputMethodBase::GetTextInputClient() const { |
| 68 return (widget_focused_ && GetFocusedView()) ? | 48 return (widget()->IsActive() && GetFocusedView()) ? |
| 69 GetFocusedView()->GetTextInputClient() : NULL; | 49 GetFocusedView()->GetTextInputClient() : NULL; |
| 70 } | 50 } |
| 71 | 51 |
| 72 ui::TextInputType InputMethodBase::GetTextInputType() const { | 52 ui::TextInputType InputMethodBase::GetTextInputType() const { |
| 73 ui::TextInputClient* client = GetTextInputClient(); | 53 ui::TextInputClient* client = GetTextInputClient(); |
| 74 return client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; | 54 return client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; |
| 75 } | 55 } |
| 76 | 56 |
| 77 bool InputMethodBase::IsMock() const { | 57 bool InputMethodBase::IsMock() const { |
| 78 return false; | 58 return false; |
| 79 } | 59 } |
| 80 | 60 |
| 81 void InputMethodBase::OnWillChangeFocus(View* focused_before, View* focused) { | 61 void InputMethodBase::OnWillChangeFocus(View* focused_before, View* focused) {} |
| 82 } | |
| 83 | 62 |
| 84 void InputMethodBase::OnDidChangeFocus(View* focused_before, View* focused) { | 63 void InputMethodBase::OnDidChangeFocus(View* focused_before, View* focused) {} |
| 85 } | |
| 86 | 64 |
| 87 bool InputMethodBase::IsViewFocused(View* view) const { | 65 bool InputMethodBase::IsViewFocused(View* view) const { |
| 88 return widget_focused_ && view && GetFocusedView() == view; | 66 return widget()->IsActive() && view && GetFocusedView() == view; |
| 89 } | 67 } |
| 90 | 68 |
| 91 bool InputMethodBase::IsTextInputTypeNone() const { | 69 bool InputMethodBase::IsTextInputTypeNone() const { |
| 92 return GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE; | 70 return GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE; |
| 93 } | 71 } |
| 94 | 72 |
| 95 void InputMethodBase::OnInputMethodChanged() const { | 73 void InputMethodBase::OnInputMethodChanged() const { |
| 96 ui::TextInputClient* client = GetTextInputClient(); | 74 ui::TextInputClient* client = GetTextInputClient(); |
| 97 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) | 75 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) |
| 98 client->OnInputMethodChanged(); | 76 client->OnInputMethodChanged(); |
| 99 } | 77 } |
| 100 | 78 |
| 101 void InputMethodBase::DispatchKeyEventPostIME(const ui::KeyEvent& key) const { | 79 void InputMethodBase::DispatchKeyEventPostIME(const ui::KeyEvent& key) const { |
| 102 if (delegate_) | 80 if (delegate_) |
| 103 delegate_->DispatchKeyEventPostIME(key); | 81 delegate_->DispatchKeyEventPostIME(key); |
| 104 } | 82 } |
| 105 | 83 |
| 106 bool InputMethodBase::GetCaretBoundsInWidget(gfx::Rect* rect) const { | 84 bool InputMethodBase::GetCaretBoundsInWidget(gfx::Rect* rect) const { |
| 107 DCHECK(rect); | 85 DCHECK(rect); |
| 108 ui::TextInputClient* client = GetTextInputClient(); | 86 ui::TextInputClient* client = GetTextInputClient(); |
| 109 if (!client || client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) | 87 if (!client || client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) |
| 110 return false; | 88 return false; |
| 111 | 89 |
| 112 *rect = GetFocusedView()->ConvertRectToWidget(client->GetCaretBounds()); | 90 *rect = GetFocusedView()->ConvertRectToWidget(client->GetCaretBounds()); |
| 113 | 91 |
| 114 // We need to do coordinate conversion if the focused view is inside a child | 92 // Convert coordinates if the focused view is inside a child Widget. |
| 115 // Widget. | 93 if (GetFocusedView()->GetWidget() != widget()) |
| 116 if (GetFocusedView()->GetWidget() != widget_) | 94 return Widget::ConvertRect(GetFocusedView()->GetWidget(), widget(), rect); |
| 117 return Widget::ConvertRect(GetFocusedView()->GetWidget(), widget_, rect); | |
| 118 return true; | 95 return true; |
| 119 } | 96 } |
| 120 | 97 |
| 121 } // namespace views | 98 } // namespace views |
| OLD | NEW |