Home | History | Annotate | Download | only in ime
      1 // Copyright 2014 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_CHROMEOS_H_
      6 #define UI_BASE_IME_INPUT_METHOD_CHROMEOS_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "ui/base/ime/chromeos/character_composer.h"
     16 #include "ui/base/ime/chromeos/ime_bridge.h"
     17 #include "ui/base/ime/composition_text.h"
     18 #include "ui/base/ime/input_method_base.h"
     19 
     20 namespace ui {
     21 
     22 // A ui::InputMethod implementation based on IBus.
     23 class UI_BASE_EXPORT InputMethodChromeOS
     24     : public InputMethodBase,
     25       public chromeos::IMEInputContextHandlerInterface {
     26  public:
     27   explicit InputMethodChromeOS(internal::InputMethodDelegate* delegate);
     28   virtual ~InputMethodChromeOS();
     29 
     30   // Overridden from InputMethod:
     31   virtual void OnFocus() OVERRIDE;
     32   virtual void OnBlur() OVERRIDE;
     33   virtual bool OnUntranslatedIMEMessage(const base::NativeEvent& event,
     34                                         NativeEventResult* result) OVERRIDE;
     35   virtual bool DispatchKeyEvent(const ui::KeyEvent& event) OVERRIDE;
     36   virtual void OnTextInputTypeChanged(const TextInputClient* client) OVERRIDE;
     37   virtual void OnCaretBoundsChanged(const TextInputClient* client) OVERRIDE;
     38   virtual void CancelComposition(const TextInputClient* client) OVERRIDE;
     39   virtual void OnInputLocaleChanged() OVERRIDE;
     40   virtual std::string GetInputLocale() OVERRIDE;
     41   virtual bool IsActive() OVERRIDE;
     42   virtual bool IsCandidatePopupOpen() const OVERRIDE;
     43 
     44  protected:
     45   // Converts |text| into CompositionText.
     46   void ExtractCompositionText(const chromeos::CompositionText& text,
     47                               uint32 cursor_position,
     48                               CompositionText* out_composition) const;
     49 
     50   // Process a key returned from the input method.
     51   virtual void ProcessKeyEventPostIME(const ui::KeyEvent& event,
     52                                       bool handled);
     53 
     54   // Resets context and abandon all pending results and key events.
     55   void ResetContext();
     56 
     57  private:
     58   class PendingKeyEvent;
     59 
     60   // Overridden from InputMethodBase:
     61   virtual void OnWillChangeFocusedClient(TextInputClient* focused_before,
     62                                          TextInputClient* focused) OVERRIDE;
     63   virtual void OnDidChangeFocusedClient(TextInputClient* focused_before,
     64                                         TextInputClient* focused) OVERRIDE;
     65 
     66   // Asks the client to confirm current composition text.
     67   void ConfirmCompositionText();
     68 
     69   // Checks the availability of focused text input client and update focus
     70   // state.
     71   void UpdateContextFocusState();
     72 
     73   // Processes a key event that was already filtered by the input method.
     74   // A VKEY_PROCESSKEY may be dispatched to the focused View.
     75   void ProcessFilteredKeyPressEvent(const ui::KeyEvent& event);
     76 
     77   // Processes a key event that was not filtered by the input method.
     78   void ProcessUnfilteredKeyPressEvent(const ui::KeyEvent& event);
     79 
     80   // Sends input method result caused by the given key event to the focused text
     81   // input client.
     82   void ProcessInputMethodResult(const ui::KeyEvent& event, bool filtered);
     83 
     84   // Checks if the pending input method result needs inserting into the focused
     85   // text input client as a single character.
     86   bool NeedInsertChar() const;
     87 
     88   // Checks if there is pending input method result.
     89   bool HasInputMethodResult() const;
     90 
     91   // Sends a fake key event for IME composing without physical key events.
     92   void SendFakeProcessKeyEvent(bool pressed) const;
     93 
     94   // Abandons all pending key events. It usually happends when we lose keyboard
     95   // focus, the text input type is changed or we are destroyed.
     96   void AbandonAllPendingKeyEvents();
     97 
     98   // Passes keyevent and executes character composition if necessary. Returns
     99   // true if character composer comsumes key event.
    100   bool ExecuteCharacterComposer(const ui::KeyEvent& event);
    101 
    102   // chromeos::IMEInputContextHandlerInterface overrides:
    103   virtual void CommitText(const std::string& text) OVERRIDE;
    104   virtual void UpdateCompositionText(const chromeos::CompositionText& text,
    105                                      uint32 cursor_pos,
    106                                      bool visible) OVERRIDE;
    107   virtual void DeleteSurroundingText(int32 offset, uint32 length) OVERRIDE;
    108 
    109   // Hides the composition text.
    110   void HidePreeditText();
    111 
    112   // Callback function for IMEEngineHandlerInterface::ProcessKeyEvent.
    113   void ProcessKeyEventDone(uint32 id, ui::KeyEvent* event, bool is_handled);
    114 
    115   // Returns whether an input field is focused. Note that password field is not
    116   // considered as an input field.
    117   bool IsInputFieldFocused();
    118 
    119   // All pending key events. Note: we do not own these object, we just save
    120   // pointers to these object so that we can abandon them when necessary.
    121   // They will be deleted in ProcessKeyEventDone().
    122   std::set<uint32> pending_key_events_;
    123 
    124   // Pending composition text generated by the current pending key event.
    125   // It'll be sent to the focused text input client as soon as we receive the
    126   // processing result of the pending key event.
    127   CompositionText composition_;
    128 
    129   // Pending result text generated by the current pending key event.
    130   // It'll be sent to the focused text input client as soon as we receive the
    131   // processing result of the pending key event.
    132   base::string16 result_text_;
    133 
    134   base::string16 previous_surrounding_text_;
    135   gfx::Range previous_selection_range_;
    136 
    137   // Indicates if there is an ongoing composition text.
    138   bool composing_text_;
    139 
    140   // Indicates if the composition text is changed or deleted.
    141   bool composition_changed_;
    142 
    143   // The latest id of key event.
    144   uint32 current_keyevent_id_;
    145 
    146   // An object to compose a character from a sequence of key presses
    147   // including dead key etc.
    148   CharacterComposer character_composer_;
    149 
    150   // Used for making callbacks.
    151   base::WeakPtrFactory<InputMethodChromeOS> weak_ptr_factory_;
    152 
    153   DISALLOW_COPY_AND_ASSIGN(InputMethodChromeOS);
    154 };
    155 
    156 }  // namespace ui
    157 
    158 #endif  // UI_BASE_IME_INPUT_METHOD_CHROMEOS_H_
    159