Chromium Code Reviews| 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 "views/ime/input_method_base.h" | |
| 6 #include "views/view.h" | |
| 7 #include "views/widget/widget.h" | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 | |
| 11 namespace views { | |
| 12 | |
| 13 InputMethodBase::InputMethodBase() | |
| 14 : delegate_(NULL), | |
| 15 widget_(NULL), | |
| 16 focused_view_(NULL), | |
| 17 widget_focused_(false) { | |
| 18 } | |
| 19 | |
| 20 InputMethodBase::~InputMethodBase() { | |
| 21 if (widget_) { | |
| 22 widget_->GetFocusManager()->RemoveFocusChangeListener(this); | |
| 23 widget_ = NULL; | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 void InputMethodBase::set_delegate(internal::InputMethodDelegate* delegate) { | |
| 28 DCHECK(delegate); | |
| 29 delegate_ = delegate; | |
| 30 } | |
| 31 | |
| 32 void InputMethodBase::Init(Widget* widget) { | |
| 33 DCHECK(widget); | |
| 34 DCHECK(widget->GetFocusManager()); | |
| 35 | |
| 36 if (widget_) { | |
| 37 NOTREACHED() << "The input method is already initialized."; | |
| 38 return; | |
| 39 } | |
| 40 | |
| 41 widget_ = widget; | |
| 42 widget->GetFocusManager()->AddFocusChangeListener(this); | |
|
Peng
2011/03/30 18:41:35
Maybe it is better to move those code to Widget?
James Su
2011/03/30 19:44:22
The intention to keep the change to Widget/NativeW
| |
| 43 } | |
| 44 | |
| 45 void InputMethodBase::OnFocusIn() { | |
| 46 widget_focused_ = true; | |
| 47 } | |
| 48 | |
| 49 void InputMethodBase::OnFocusOut() { | |
| 50 widget_focused_ = false; | |
| 51 } | |
| 52 | |
| 53 TextInputClient* InputMethodBase::GetTextInputClient() const { | |
| 54 return (widget_focused_ && focused_view_) ? | |
| 55 focused_view_->GetTextInputClient() : NULL; | |
| 56 } | |
| 57 | |
| 58 ui::TextInputType InputMethodBase::GetTextInputType() const { | |
| 59 TextInputClient* client = GetTextInputClient(); | |
| 60 return client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; | |
| 61 } | |
| 62 | |
| 63 void InputMethodBase::FocusWillChange(View* focused_before, View* focused) { | |
| 64 DCHECK_EQ(focused_view_, focused_before); | |
| 65 FocusedViewWillChange(); | |
| 66 focused_view_ = focused; | |
| 67 FocusedViewDidChange(); | |
| 68 } | |
| 69 | |
| 70 bool InputMethodBase::IsViewFocused(View* view) const { | |
| 71 return widget_focused_ && view && focused_view_ == view; | |
| 72 } | |
| 73 | |
| 74 bool InputMethodBase::IsTextInputTypeNone() const { | |
| 75 return GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE; | |
| 76 } | |
| 77 | |
| 78 void InputMethodBase::OnInputMethodChanged() const { | |
| 79 TextInputClient* client = GetTextInputClient(); | |
| 80 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) | |
| 81 client->OnInputMethodChanged(); | |
| 82 } | |
| 83 | |
| 84 void InputMethodBase::DispatchKeyEventPostIME(const KeyEvent& key) const { | |
| 85 if (delegate_) | |
| 86 delegate_->DispatchKeyEventPostIME(key); | |
| 87 } | |
| 88 | |
| 89 bool InputMethodBase::GetCaretBoundsInWidget(gfx::Rect* rect) const { | |
| 90 DCHECK(rect); | |
| 91 TextInputClient* client = GetTextInputClient(); | |
| 92 if (!client || client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) | |
| 93 return false; | |
| 94 | |
| 95 *rect = client->GetCaretBounds(); | |
| 96 gfx::Point origin = rect->origin(); | |
| 97 gfx::Point end = gfx::Point(rect->right(), rect->bottom()); | |
| 98 | |
| 99 View::ConvertPointToWidget(focused_view_, &origin); | |
| 100 View::ConvertPointToWidget(focused_view_, &end); | |
| 101 rect->SetRect(origin.x(), origin.y(), | |
| 102 end.x() - origin.x(), end.y() - origin.y()); | |
| 103 | |
| 104 // We need to do coordinate conversion if the focused view is inside a child | |
| 105 // Widget. | |
| 106 if (focused_view_->GetWidget() != widget_) | |
| 107 return Widget::ConvertRect(focused_view_->GetWidget(), widget_, rect); | |
| 108 return true; | |
| 109 } | |
| 110 | |
| 111 void InputMethodBase::FocusedViewWillChange() { | |
| 112 } | |
| 113 | |
| 114 void InputMethodBase::FocusedViewDidChange() { | |
| 115 } | |
| 116 | |
| 117 } // namespace views | |
| OLD | NEW |