summaryrefslogtreecommitdiffstats
path: root/ui/views/ime/input_method_base.cc
blob: e72c05421f1ea84c9f008060af16a2b05ee796bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/ime/input_method_base.h"

#include "base/logging.h"
#include "ui/base/events/event.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace views {

InputMethodBase::InputMethodBase() : delegate_(NULL), widget_(NULL) {}

InputMethodBase::~InputMethodBase() {
  widget_->GetFocusManager()->RemoveFocusChangeListener(this);
  widget_ = NULL;
}

void InputMethodBase::SetDelegate(internal::InputMethodDelegate* delegate) {
  DCHECK(delegate);
  delegate_ = delegate;
}

void InputMethodBase::Init(Widget* widget) {
  DCHECK(widget);
  DCHECK(widget->GetFocusManager());
  DCHECK(!widget_) << "The input method is already initialized.";

  widget_ = widget;
  // Alert the InputMethod of the Widget's currently focused view.
  View* focused = widget->GetFocusManager()->GetFocusedView();
  if (focused)
    OnWillChangeFocus(NULL, focused);
  widget->GetFocusManager()->AddFocusChangeListener(this);
}

views::View* InputMethodBase::GetFocusedView() const {
  return widget_->GetFocusManager()->GetFocusedView();
}

void InputMethodBase::OnTextInputTypeChanged(View* view) {}

ui::TextInputClient* InputMethodBase::GetTextInputClient() const {
  return (widget_->IsActive() && GetFocusedView()) ?
      GetFocusedView()->GetTextInputClient() : NULL;
}

ui::TextInputType InputMethodBase::GetTextInputType() const {
  ui::TextInputClient* client = GetTextInputClient();
  return client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
}

bool InputMethodBase::IsMock() const {
  return false;
}

void InputMethodBase::OnWillChangeFocus(View* focused_before, View* focused) {}

void InputMethodBase::OnDidChangeFocus(View* focused_before, View* focused) {}

bool InputMethodBase::IsViewFocused(View* view) const {
  return widget_->IsActive() && view && GetFocusedView() == view;
}

bool InputMethodBase::IsTextInputTypeNone() const {
  return GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE;
}

void InputMethodBase::OnInputMethodChanged() const {
  ui::TextInputClient* client = GetTextInputClient();
  if (client && client->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE)
    client->OnInputMethodChanged();
}

void InputMethodBase::DispatchKeyEventPostIME(const ui::KeyEvent& key) const {
  if (delegate_)
    delegate_->DispatchKeyEventPostIME(key);
}

bool InputMethodBase::GetCaretBoundsInWidget(gfx::Rect* rect) const {
  DCHECK(rect);
  ui::TextInputClient* client = GetTextInputClient();
  if (!client || client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE)
    return false;

  gfx::Rect caret_bounds = client->GetCaretBounds();
  gfx::Point caret_origin = caret_bounds.origin();
  View::ConvertPointFromScreen(GetFocusedView(), &caret_origin);
  caret_bounds.set_origin(caret_origin);
  *rect = GetFocusedView()->ConvertRectToWidget(caret_bounds);

  // Convert coordinates if the focused view is inside a child Widget.
  if (GetFocusedView()->GetWidget() != widget_)
    return Widget::ConvertRect(GetFocusedView()->GetWidget(), widget_, rect);
  return true;
}

}  // namespace views