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/observer_list.h" 11 #include "ui/base/events/event_constants.h" 12 #include "ui/base/ime/input_method.h" 13 #include "ui/base/ui_export.h" 14 15 namespace gfx { 16 class Rect; 17 } // namespace gfx 18 19 namespace ui { 20 21 class InputMethodObserver; 22 class TextInputClient; 23 24 // A helper class providing functionalities shared among ui::InputMethod 25 // implementations. 26 class UI_EXPORT InputMethodBase : NON_EXPORTED_BASE(public InputMethod) { 27 public: 28 InputMethodBase(); 29 virtual ~InputMethodBase(); 30 31 // Overriden from InputMethod. 32 virtual void SetDelegate(internal::InputMethodDelegate* delegate) OVERRIDE; 33 virtual void Init(bool focused) OVERRIDE; 34 // If a derived class overrides OnFocus()/OnBlur(), it should call parent's 35 // implementation first, to make sure |system_toplevel_window_focused_| flag 36 // can be updated correctly. 37 virtual void OnFocus() OVERRIDE; 38 virtual void OnBlur() OVERRIDE; 39 virtual void SetFocusedTextInputClient(TextInputClient* client) OVERRIDE; 40 virtual TextInputClient* GetTextInputClient() const OVERRIDE; 41 42 // If a derived class overrides this method, it should call parent's 43 // implementation. 44 virtual void OnTextInputTypeChanged(const TextInputClient* client) OVERRIDE; 45 46 virtual TextInputType GetTextInputType() const OVERRIDE; 47 virtual bool CanComposeInline() const OVERRIDE; 48 49 virtual void AddObserver(InputMethodObserver* observer) OVERRIDE; 50 virtual void RemoveObserver(InputMethodObserver* observer) OVERRIDE; 51 52 protected: 53 virtual void OnWillChangeFocusedClient(TextInputClient* focused_before, 54 TextInputClient* focused) {} 55 virtual void OnDidChangeFocusedClient(TextInputClient* focused_before, 56 TextInputClient* focused) {} 57 58 // Returns true if |client| is currently focused. 59 bool IsTextInputClientFocused(const TextInputClient* client); 60 61 // Checks if the focused text input client's text input type is 62 // TEXT_INPUT_TYPE_NONE. Also returns true if there is no focused text 63 // input client. 64 bool IsTextInputTypeNone() const; 65 66 // Convenience method to call the focused text input client's 67 // OnInputMethodChanged() method. It'll only take effect if the current text 68 // input type is not TEXT_INPUT_TYPE_NONE. 69 void OnInputMethodChanged() const; 70 71 // Convenience method to call delegate_->DispatchKeyEventPostIME(). 72 // Returns true if the event was processed 73 bool DispatchKeyEventPostIME(const base::NativeEvent& native_event) const; 74 75 // Convenience method to call delegate_->DispatchFabricatedKeyEventPostIME(). 76 // Returns true if the event was processed 77 bool DispatchFabricatedKeyEventPostIME(EventType type, 78 KeyboardCode key_code, 79 int flags) const; 80 81 // Convenience method to notify all observers of TextInputClient changes. 82 void NotifyTextInputStateChanged(const TextInputClient* client); 83 84 bool system_toplevel_window_focused() const { 85 return system_toplevel_window_focused_; 86 } 87 88 private: 89 internal::InputMethodDelegate* delegate_; 90 TextInputClient* text_input_client_; 91 92 ObserverList<InputMethodObserver> observer_list_; 93 94 bool system_toplevel_window_focused_; 95 96 DISALLOW_COPY_AND_ASSIGN(InputMethodBase); 97 }; 98 99 } // namespace ui 100 101 #endif // UI_BASE_IME_INPUT_METHOD_BASE_H_ 102