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#ifndef UI_BASE_IME_INPUT_METHOD_BASE_H_
6#define UI_BASE_IME_INPUT_METHOD_BASE_H_
7
8#include "base/basictypes.h"
9#include "base/compiler_specific.h"
10#include "base/memory/weak_ptr.h"
11#include "base/observer_list.h"
12#include "ui/base/ime/input_method.h"
13#include "ui/base/ui_export.h"
14
15namespace gfx {
16class Rect;
17}  // namespace gfx
18
19namespace ui {
20
21class InputMethodObserver;
22class KeyEvent;
23class TextInputClient;
24
25// A helper class providing functionalities shared among ui::InputMethod
26// implementations.
27class UI_EXPORT InputMethodBase
28   : NON_EXPORTED_BASE(public InputMethod),
29     public base::SupportsWeakPtr<InputMethodBase> {
30 public:
31  InputMethodBase();
32  virtual ~InputMethodBase();
33
34  // Overriden from InputMethod.
35  virtual void SetDelegate(internal::InputMethodDelegate* delegate) OVERRIDE;
36  virtual void Init(bool focused) OVERRIDE;
37  // If a derived class overrides OnFocus()/OnBlur(), it should call parent's
38  // implementation first, to make sure |system_toplevel_window_focused_| flag
39  // can be updated correctly.
40  virtual void OnFocus() OVERRIDE;
41  virtual void OnBlur() OVERRIDE;
42  virtual void SetFocusedTextInputClient(TextInputClient* client) OVERRIDE;
43  virtual void DetachTextInputClient(TextInputClient* client) OVERRIDE;
44  virtual TextInputClient* GetTextInputClient() const OVERRIDE;
45
46  // If a derived class overrides this method, it should call parent's
47  // implementation.
48  virtual void OnTextInputTypeChanged(const TextInputClient* client) OVERRIDE;
49
50  virtual TextInputType GetTextInputType() const OVERRIDE;
51  virtual TextInputMode GetTextInputMode() const OVERRIDE;
52  virtual bool CanComposeInline() const OVERRIDE;
53
54  virtual void AddObserver(InputMethodObserver* observer) OVERRIDE;
55  virtual void RemoveObserver(InputMethodObserver* observer) OVERRIDE;
56
57 protected:
58  virtual void OnWillChangeFocusedClient(TextInputClient* focused_before,
59                                         TextInputClient* focused) {}
60  virtual void OnDidChangeFocusedClient(TextInputClient* focused_before,
61                                        TextInputClient* focused) {}
62
63  // Returns true if |client| is currently focused.
64  bool IsTextInputClientFocused(const TextInputClient* client);
65
66  // Checks if the focused text input client's text input type is
67  // TEXT_INPUT_TYPE_NONE. Also returns true if there is no focused text
68  // input client.
69  bool IsTextInputTypeNone() const;
70
71  // Convenience method to call the focused text input client's
72  // OnInputMethodChanged() method. It'll only take effect if the current text
73  // input type is not TEXT_INPUT_TYPE_NONE.
74  void OnInputMethodChanged() const;
75
76  // Convenience method to call delegate_->DispatchKeyEventPostIME().
77  // Returns true if the event was processed
78  bool DispatchKeyEventPostIME(const ui::KeyEvent& event) const;
79
80  // Convenience method to notify all observers of TextInputClient changes.
81  void NotifyTextInputStateChanged(const TextInputClient* client);
82
83  // Interface for for signalling candidate window events.
84  // See also *Callback functions below. To avoid reentrancy issue that
85  // TextInputClient manipulates IME state during even handling, these methods
86  // defer sending actual signals to renderer.
87  void OnCandidateWindowShown();
88  void OnCandidateWindowUpdated();
89  void OnCandidateWindowHidden();
90
91  bool system_toplevel_window_focused() const {
92    return system_toplevel_window_focused_;
93  }
94
95 private:
96  void SetFocusedTextInputClientInternal(TextInputClient* client);
97
98  // Deferred callbacks for signalling TextInputClient about candidate window
99  // appearance changes.
100  void CandidateWindowShownCallback();
101  void CandidateWindowUpdatedCallback();
102  void CandidateWindowHiddenCallback();
103
104  internal::InputMethodDelegate* delegate_;
105  TextInputClient* text_input_client_;
106
107  ObserverList<InputMethodObserver> observer_list_;
108
109  bool system_toplevel_window_focused_;
110
111  DISALLOW_COPY_AND_ASSIGN(InputMethodBase);
112};
113
114}  // namespace ui
115
116#endif  // UI_BASE_IME_INPUT_METHOD_BASE_H_
117