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#ifndef UI_VIEWS_IME_INPUT_METHOD_BASE_H_
6#define UI_VIEWS_IME_INPUT_METHOD_BASE_H_
7
8#include "base/basictypes.h"
9#include "base/compiler_specific.h"
10#include "ui/views/focus/focus_manager.h"
11#include "ui/views/ime/input_method.h"
12#include "ui/views/ime/input_method_delegate.h"
13
14namespace gfx {
15class Rect;
16}
17
18namespace ui {
19class KeyEvent;
20}
21
22namespace views {
23
24// A helper that provides functionality shared by InputMethod implementations.
25class VIEWS_EXPORT InputMethodBase : public InputMethod,
26                                     public FocusChangeListener {
27 public:
28  InputMethodBase();
29  virtual ~InputMethodBase();
30
31  // Overridden from InputMethod.
32  virtual void SetDelegate(internal::InputMethodDelegate* delegate) OVERRIDE;
33  virtual void Init(Widget* widget) OVERRIDE;
34  virtual void OnTextInputTypeChanged(View* view) OVERRIDE;
35  virtual ui::TextInputClient* GetTextInputClient() const OVERRIDE;
36  virtual ui::TextInputType GetTextInputType() const OVERRIDE;
37  virtual bool IsMock() const OVERRIDE;
38
39  // Overridden from FocusChangeListener.
40  virtual void OnWillChangeFocus(View* focused_before, View* focused) OVERRIDE;
41  virtual void OnDidChangeFocus(View* focused_before, View* focused) OVERRIDE;
42
43 protected:
44  internal::InputMethodDelegate* delegate() const { return delegate_; }
45  Widget* widget() const { return widget_; }
46  View* GetFocusedView() const;
47
48  // Returns true only if the View is focused and its Widget is active.
49  bool IsViewFocused(View* view) const;
50
51  // Returns true if there is no focused text input client or its type is none.
52  bool IsTextInputTypeNone() const;
53
54  // Calls the focused text input client's OnInputMethodChanged() method.
55  // This has no effect if the text input type is ui::TEXT_INPUT_TYPE_NONE.
56  void OnInputMethodChanged() const;
57
58  // Convenience method to call delegate_->DispatchKeyEventPostIME().
59  void DispatchKeyEventPostIME(const ui::KeyEvent& key) const;
60
61  // Gets the current text input client's caret bounds in Widget's coordinates.
62  // Returns false if the current text input client doesn't support text input.
63  bool GetCaretBoundsInWidget(gfx::Rect* rect) const;
64
65  // Removes any state installed on |widget_| and NULLs it out. Use if the
66  // widget is in a state such that it should no longer be used (such as when
67  // this is in its destructor).
68  void DetachFromWidget();
69
70 private:
71  internal::InputMethodDelegate* delegate_;
72  Widget* widget_;
73
74  DISALLOW_COPY_AND_ASSIGN(InputMethodBase);
75};
76
77}  // namespace views
78
79#endif  // UI_VIEWS_IME_INPUT_METHOD_BASE_H_
80