OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ui/views/ime/input_method_base.h" | |
6 | |
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" | |
12 | |
13 namespace views { | |
14 | |
15 InputMethodBase::InputMethodBase() : delegate_(NULL), widget_(NULL) {} | |
16 | |
17 InputMethodBase::~InputMethodBase() { | |
18 DetachFromWidget(); | |
19 } | |
20 | |
21 void InputMethodBase::SetDelegate(internal::InputMethodDelegate* delegate) { | |
22 DCHECK(delegate); | |
23 delegate_ = delegate; | |
24 } | |
25 | |
26 void InputMethodBase::Init(Widget* widget) { | |
27 DCHECK(widget); | |
28 DCHECK(widget->GetFocusManager()); | |
29 DCHECK(!widget_) << "The input method is already initialized."; | |
30 | |
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); | |
37 } | |
38 | |
39 views::View* InputMethodBase::GetFocusedView() const { | |
40 return widget_ ? widget_->GetFocusManager()->GetFocusedView() : NULL; | |
41 } | |
42 | |
43 void InputMethodBase::OnTextInputTypeChanged(View* view) {} | |
44 | |
45 ui::TextInputClient* InputMethodBase::GetTextInputClient() const { | |
46 return (widget_ && widget_->IsActive() && GetFocusedView()) ? | |
47 GetFocusedView()->GetTextInputClient() : NULL; | |
48 } | |
49 | |
50 ui::TextInputType InputMethodBase::GetTextInputType() const { | |
51 ui::TextInputClient* client = GetTextInputClient(); | |
52 return client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; | |
53 } | |
54 | |
55 void InputMethodBase::OnWillChangeFocus(View* focused_before, View* focused) {} | |
56 | |
57 void InputMethodBase::OnDidChangeFocus(View* focused_before, View* focused) {} | |
58 | |
59 bool InputMethodBase::IsViewFocused(View* view) const { | |
60 return widget_ && widget_->IsActive() && view && GetFocusedView() == view; | |
61 } | |
62 | |
63 bool InputMethodBase::IsTextInputTypeNone() const { | |
64 return GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE; | |
65 } | |
66 | |
67 void InputMethodBase::OnInputMethodChanged() const { | |
68 ui::TextInputClient* client = GetTextInputClient(); | |
69 if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE) | |
70 client->OnInputMethodChanged(); | |
71 } | |
72 | |
73 void InputMethodBase::DispatchKeyEventPostIME(const ui::KeyEvent& key) const { | |
74 if (delegate_) | |
75 delegate_->DispatchKeyEventPostIME(key); | |
76 } | |
77 | |
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; | |
83 | |
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); | |
89 | |
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; | |
94 } | |
95 | |
96 void InputMethodBase::DetachFromWidget() { | |
97 if (!widget_) | |
98 return; | |
99 | |
100 widget_->GetFocusManager()->RemoveFocusChangeListener(this); | |
101 widget_ = NULL; | |
102 } | |
103 | |
104 } // namespace views | |
OLD | NEW |