summaryrefslogtreecommitdiffstats
path: root/views/ime/input_method_base.cc
blob: a062f6bd2d4840f397df8f0e2549148981809f67 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2011 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 "views/ime/input_method_base.h"
#include "views/view.h"
#include "views/widget/widget.h"

#include "base/logging.h"

namespace views {

InputMethodBase::InputMethodBase()
    : delegate_(NULL),
      widget_(NULL),
      focused_view_(NULL),
      widget_focused_(false) {
}

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

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

void InputMethodBase::Init(Widget* widget) {
  DCHECK(widget);
  DCHECK(widget->GetFocusManager());

  if (widget_) {
    NOTREACHED() << "The input method is already initialized.";
    return;
  }

  widget_ = widget;
  widget->GetFocusManager()->AddFocusChangeListener(this);
}

void InputMethodBase::OnFocus() {
  widget_focused_ = true;
}

void InputMethodBase::OnBlur() {
  widget_focused_ = false;
}

TextInputClient* InputMethodBase::GetTextInputClient() const {
  return (widget_focused_ && focused_view_) ?
      focused_view_->GetTextInputClient() : NULL;
}

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

void InputMethodBase::FocusWillChange(View* focused_before, View* focused) {
  DCHECK_EQ(focused_view_, focused_before);
  FocusedViewWillChange();
  focused_view_ = focused;
  FocusedViewDidChange();
}

bool InputMethodBase::IsViewFocused(View* view) const {
  return widget_focused_ && view && focused_view_ == view;
}

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

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

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

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

  *rect = client->GetCaretBounds();
  gfx::Point origin = rect->origin();
  gfx::Point end = gfx::Point(rect->right(), rect->bottom());

  View::ConvertPointToWidget(focused_view_, &origin);
  View::ConvertPointToWidget(focused_view_, &end);
  rect->SetRect(origin.x(), origin.y(),
                end.x() - origin.x(), end.y() - origin.y());

  // We need to do coordinate conversion if the focused view is inside a child
  // Widget.
  if (focused_view_->GetWidget() != widget_)
    return Widget::ConvertRect(focused_view_->GetWidget(), widget_, rect);
  return true;
}

void InputMethodBase::FocusedViewWillChange() {
}

void InputMethodBase::FocusedViewDidChange() {
}

}  // namespace views