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 #include "ui/base/ime/input_method_base.h" 6 7 #include "base/logging.h" 8 #include "ui/base/ime/input_method_delegate.h" 9 #include "ui/base/ime/input_method_observer.h" 10 #include "ui/base/ime/text_input_client.h" 11 12 namespace ui { 13 14 InputMethodBase::InputMethodBase() 15 : delegate_(NULL), 16 text_input_client_(NULL), 17 system_toplevel_window_focused_(false) { 18 } 19 20 InputMethodBase::~InputMethodBase() { 21 FOR_EACH_OBSERVER(InputMethodObserver, 22 observer_list_, 23 OnInputMethodDestroyed(this)); 24 } 25 26 void InputMethodBase::SetDelegate(internal::InputMethodDelegate* delegate) { 27 delegate_ = delegate; 28 } 29 30 void InputMethodBase::Init(bool focused) { 31 if (focused) 32 OnFocus(); 33 } 34 35 void InputMethodBase::OnFocus() { 36 DCHECK(!system_toplevel_window_focused_); 37 system_toplevel_window_focused_ = true; 38 } 39 40 void InputMethodBase::OnBlur() { 41 DCHECK(system_toplevel_window_focused_); 42 system_toplevel_window_focused_ = false; 43 } 44 45 void InputMethodBase::SetFocusedTextInputClient(TextInputClient* client) { 46 TextInputClient* old = text_input_client_; 47 OnWillChangeFocusedClient(old, client); 48 text_input_client_ = client; // NULL allowed. 49 OnDidChangeFocusedClient(old, client); 50 51 if (old != text_input_client_) 52 NotifyTextInputStateChanged(text_input_client_); 53 } 54 55 TextInputClient* InputMethodBase::GetTextInputClient() const { 56 return system_toplevel_window_focused_ ? text_input_client_ : NULL; 57 } 58 59 void InputMethodBase::OnTextInputTypeChanged(const TextInputClient* client) { 60 if (!IsTextInputClientFocused(client)) 61 return; 62 NotifyTextInputStateChanged(client); 63 } 64 65 TextInputType InputMethodBase::GetTextInputType() const { 66 TextInputClient* client = GetTextInputClient(); 67 return client ? client->GetTextInputType() : TEXT_INPUT_TYPE_NONE; 68 } 69 70 bool InputMethodBase::CanComposeInline() const { 71 TextInputClient* client = GetTextInputClient(); 72 return client ? client->CanComposeInline() : true; 73 } 74 75 void InputMethodBase::AddObserver(InputMethodObserver* observer) { 76 observer_list_.AddObserver(observer); 77 } 78 79 void InputMethodBase::RemoveObserver(InputMethodObserver* observer) { 80 observer_list_.RemoveObserver(observer); 81 } 82 83 bool InputMethodBase::IsTextInputClientFocused(const TextInputClient* client) { 84 return client && (client == GetTextInputClient()); 85 } 86 87 bool InputMethodBase::IsTextInputTypeNone() const { 88 return GetTextInputType() == TEXT_INPUT_TYPE_NONE; 89 } 90 91 void InputMethodBase::OnInputMethodChanged() const { 92 TextInputClient* client = GetTextInputClient(); 93 if (!IsTextInputTypeNone()) 94 client->OnInputMethodChanged(); 95 } 96 97 bool InputMethodBase::DispatchKeyEventPostIME( 98 const base::NativeEvent& native_event) const { 99 return delegate_ ? delegate_->DispatchKeyEventPostIME(native_event) : false; 100 } 101 102 bool InputMethodBase::DispatchFabricatedKeyEventPostIME(EventType type, 103 KeyboardCode key_code, 104 int flags) const { 105 return delegate_ ? delegate_->DispatchFabricatedKeyEventPostIME 106 (type, key_code, flags) : false; 107 } 108 109 void InputMethodBase::NotifyTextInputStateChanged( 110 const TextInputClient* client) { 111 FOR_EACH_OBSERVER(InputMethodObserver, 112 observer_list_, 113 OnTextInputStateChanged(client)); 114 } 115 116 } // namespace ui 117