• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
InputMethodBase()15 InputMethodBase::InputMethodBase() : delegate_(NULL), widget_(NULL) {}
16 
~InputMethodBase()17 InputMethodBase::~InputMethodBase() {
18   DetachFromWidget();
19 }
20 
SetDelegate(internal::InputMethodDelegate * delegate)21 void InputMethodBase::SetDelegate(internal::InputMethodDelegate* delegate) {
22   DCHECK(delegate);
23   delegate_ = delegate;
24 }
25 
Init(Widget * widget)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 
GetFocusedView() const39 views::View* InputMethodBase::GetFocusedView() const {
40   return widget_ ? widget_->GetFocusManager()->GetFocusedView() : NULL;
41 }
42 
OnTextInputTypeChanged(View * view)43 void InputMethodBase::OnTextInputTypeChanged(View* view) {}
44 
GetTextInputClient() const45 ui::TextInputClient* InputMethodBase::GetTextInputClient() const {
46   return (widget_ && widget_->IsActive() && GetFocusedView()) ?
47       GetFocusedView()->GetTextInputClient() : NULL;
48 }
49 
GetTextInputType() const50 ui::TextInputType InputMethodBase::GetTextInputType() const {
51   ui::TextInputClient* client = GetTextInputClient();
52   return client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
53 }
54 
IsMock() const55 bool InputMethodBase::IsMock() const {
56   return false;
57 }
58 
OnWillChangeFocus(View * focused_before,View * focused)59 void InputMethodBase::OnWillChangeFocus(View* focused_before, View* focused) {}
60 
OnDidChangeFocus(View * focused_before,View * focused)61 void InputMethodBase::OnDidChangeFocus(View* focused_before, View* focused) {}
62 
IsViewFocused(View * view) const63 bool InputMethodBase::IsViewFocused(View* view) const {
64   return widget_ && widget_->IsActive() && view && GetFocusedView() == view;
65 }
66 
IsTextInputTypeNone() const67 bool InputMethodBase::IsTextInputTypeNone() const {
68   return GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE;
69 }
70 
OnInputMethodChanged() const71 void InputMethodBase::OnInputMethodChanged() const {
72   ui::TextInputClient* client = GetTextInputClient();
73   if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE)
74     client->OnInputMethodChanged();
75 }
76 
DispatchKeyEventPostIME(const ui::KeyEvent & key) const77 void InputMethodBase::DispatchKeyEventPostIME(const ui::KeyEvent& key) const {
78   if (delegate_)
79     delegate_->DispatchKeyEventPostIME(key);
80 }
81 
GetCaretBoundsInWidget(gfx::Rect * rect) const82 bool InputMethodBase::GetCaretBoundsInWidget(gfx::Rect* rect) const {
83   DCHECK(rect);
84   ui::TextInputClient* client = GetTextInputClient();
85   if (!client || client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE)
86     return false;
87 
88   gfx::Rect caret_bounds = client->GetCaretBounds();
89   gfx::Point caret_origin = caret_bounds.origin();
90   View::ConvertPointFromScreen(GetFocusedView(), &caret_origin);
91   caret_bounds.set_origin(caret_origin);
92   *rect = GetFocusedView()->ConvertRectToWidget(caret_bounds);
93 
94   // Convert coordinates if the focused view is inside a child Widget.
95   if (GetFocusedView()->GetWidget() != widget_)
96     return Widget::ConvertRect(GetFocusedView()->GetWidget(), widget_, rect);
97   return true;
98 }
99 
DetachFromWidget()100 void InputMethodBase::DetachFromWidget() {
101   if (!widget_)
102     return;
103 
104   widget_->GetFocusManager()->RemoveFocusChangeListener(this);
105   widget_ = NULL;
106 }
107 
108 }  // namespace views
109