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 <memory>
9 #include <vector>
10
11 #include "base/compiler_specific.h"
12 #include "base/component_export.h"
13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "build/build_config.h"
17 #include "ui/base/ime/ime_input_context_handler_interface.h"
18 #include "ui/base/ime/input_method.h"
19 #include "ui/events/event_dispatcher.h"
20
21 namespace gfx {
22 class Rect;
23 } // namespace gfx
24
25 namespace ui {
26
27 class IMEEngineHandlerInterface;
28 class InputMethodObserver;
29 class KeyEvent;
30 class TextInputClient;
31
32 // A helper class providing functionalities shared among ui::InputMethod
33 // implementations.
COMPONENT_EXPORT(UI_BASE_IME)34 class COMPONENT_EXPORT(UI_BASE_IME) InputMethodBase
35 : public InputMethod,
36 public base::SupportsWeakPtr<InputMethodBase>,
37 public IMEInputContextHandlerInterface {
38 public:
39 ~InputMethodBase() override;
40
41 // Overriden from InputMethod.
42 void SetDelegate(internal::InputMethodDelegate* delegate) override;
43 void OnFocus() override;
44 void OnBlur() override;
45
46 #if defined(OS_WIN)
47 bool OnUntranslatedIMEMessage(const MSG event,
48 NativeEventResult* result) override;
49 #endif
50
51 void SetFocusedTextInputClient(TextInputClient* client) override;
52 void DetachTextInputClient(TextInputClient* client) override;
53 TextInputClient* GetTextInputClient() const override;
54 void SetOnScreenKeyboardBounds(const gfx::Rect& new_bounds) override;
55
56 // If a derived class overrides this method, it should call parent's
57 // implementation.
58 void OnTextInputTypeChanged(const TextInputClient* client) override;
59 void OnInputLocaleChanged() override;
60 bool IsInputLocaleCJK() const override;
61
62 TextInputType GetTextInputType() const override;
63 TextInputMode GetTextInputMode() const override;
64 int GetTextInputFlags() const override;
65 bool CanComposeInline() const override;
66 bool GetClientShouldDoLearning() override;
67 void ShowVirtualKeyboardIfEnabled() override;
68
69 void AddObserver(InputMethodObserver* observer) override;
70 void RemoveObserver(InputMethodObserver* observer) override;
71
72 InputMethodKeyboardController* GetInputMethodKeyboardController() override;
73
74 protected:
75 explicit InputMethodBase(internal::InputMethodDelegate* delegate);
76 InputMethodBase(internal::InputMethodDelegate* delegate,
77 std::unique_ptr<InputMethodKeyboardController> controller);
78
79 virtual void OnWillChangeFocusedClient(TextInputClient* focused_before,
80 TextInputClient* focused) {}
81 virtual void OnDidChangeFocusedClient(TextInputClient* focused_before,
82 TextInputClient* focused) {}
83
84 // IMEInputContextHandlerInterface:
85 void CommitText(const std::string& text) override;
86 void UpdateCompositionText(const CompositionText& text,
87 uint32_t cursor_pos,
88 bool visible) override;
89
90 #if defined(OS_CHROMEOS)
91 bool SetCompositionRange(
92 uint32_t before,
93 uint32_t after,
94 const std::vector<ui::ImeTextSpan>& text_spans) override;
95 bool SetSelectionRange(uint32_t start, uint32_t end) override;
96 #endif
97
98 void DeleteSurroundingText(int32_t offset, uint32_t length) override;
99 SurroundingTextInfo GetSurroundingTextInfo() override;
100 void SendKeyEvent(KeyEvent* event) override;
101 InputMethod* GetInputMethod() override;
102 void ConfirmCompositionText(bool reset_engine, bool keep_selection) override;
103 bool HasCompositionText() override;
104
105 // Sends a fake key event for IME composing without physical key events.
106 // Returns true if the faked key event is stopped propagation.
107 bool SendFakeProcessKeyEvent(bool pressed) const;
108
109 // Returns true if |client| is currently focused.
110 bool IsTextInputClientFocused(const TextInputClient* client);
111
112 // Checks if the focused text input client's text input type is
113 // TEXT_INPUT_TYPE_NONE. Also returns true if there is no focused text
114 // input client.
115 bool IsTextInputTypeNone() const;
116
117 // Convenience method to call the focused text input client's
118 // OnInputMethodChanged() method. It'll only take effect if the current text
119 // input type is not TEXT_INPUT_TYPE_NONE.
120 void OnInputMethodChanged() const;
121
122 virtual ui::EventDispatchDetails DispatchKeyEventPostIME(
123 ui::KeyEvent* event) const WARN_UNUSED_RESULT;
124
125 // Convenience method to notify all observers of TextInputClient changes.
126 void NotifyTextInputStateChanged(const TextInputClient* client);
127
128 // Convenience method to notify all observers of CaretBounds changes on
129 // |client| which is the text input client with focus.
130 void NotifyTextInputCaretBoundsChanged(const TextInputClient* client);
131
132 // Gets the bounds of the composition text or cursor in |client|.
133 std::vector<gfx::Rect> GetCompositionBounds(const TextInputClient* client);
134
135 internal::InputMethodDelegate* delegate() const { return delegate_; }
136
137 static IMEEngineHandlerInterface* GetEngine();
138
139 private:
140 internal::InputMethodDelegate* delegate_;
141
142 // InputMethod:
143 const std::vector<std::unique_ptr<ui::KeyEvent>>& GetKeyEventsForTesting()
144 override;
145
146 void SetFocusedTextInputClientInternal(TextInputClient* client);
147
148 TextInputClient* text_input_client_ = nullptr;
149
150 base::ObserverList<InputMethodObserver>::Unchecked observer_list_;
151
152 std::vector<std::unique_ptr<ui::KeyEvent>> key_events_for_testing_;
153
154 // Screen bounds of a on-screen keyboard.
155 gfx::Rect keyboard_bounds_;
156
157 std::unique_ptr<InputMethodKeyboardController> const keyboard_controller_;
158
159 DISALLOW_COPY_AND_ASSIGN(InputMethodBase);
160 };
161
162 } // namespace ui
163
164 #endif // UI_BASE_IME_INPUT_METHOD_BASE_H_
165