Home | History | Annotate | Download | only in chromeos
      1 // Copyright 2013 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_CHROMEOS_CHARACTER_COMPOSER_H_
      6 #define UI_BASE_IME_CHROMEOS_CHARACTER_COMPOSER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/strings/string_util.h"
     11 #include "ui/base/ui_export.h"
     12 
     13 namespace ui {
     14 class KeyEvent;
     15 
     16 // A class to recognize compose and dead key sequence.
     17 // Outputs composed character.
     18 class UI_EXPORT CharacterComposer {
     19  public:
     20   CharacterComposer();
     21   ~CharacterComposer();
     22 
     23   void Reset();
     24 
     25   // Filters keypress.
     26   // Returns true if the keypress is recognized as a part of composition
     27   // sequence.
     28   // Fabricated events which don't have the native event, are not supported.
     29   bool FilterKeyPress(const ui::KeyEvent& event);
     30 
     31   // Returns a string consisting of composed character.
     32   // Empty string is returned when there is no composition result.
     33   const string16& composed_character() const { return composed_character_; }
     34 
     35   // Returns the preedit string.
     36   const string16& preedit_string() const { return preedit_string_; }
     37 
     38  private:
     39   friend class CharacterComposerTest;
     40 
     41   // An enum to describe composition mode.
     42   enum CompositionMode {
     43     // This is the initial state.
     44     // Composite a character with dead-keys and compose-key.
     45     KEY_SEQUENCE_MODE,
     46     // Composite a character with a hexadecimal unicode sequence.
     47     HEX_MODE,
     48   };
     49 
     50   // Filters keypress using IBus defined value.
     51   // Returns true if the keypress is recognized as a part of composition
     52   // sequence.
     53   // |keyval| must be a GDK_KEY_* constant.
     54   // |keycode| must be a X key code.
     55   // |flags| must be a combination of ui::EF_* flags.
     56   //
     57   // composed_character() returns non empty string when there is a character
     58   // composed after this method returns true.
     59   // preedit_string() returns non empty string when there is a preedit string
     60   // after this method returns true.
     61   // Return values of preedit_string() is empty after this method returns false.
     62   // composed_character() may have some characters which are consumed in this
     63   // composing session.
     64   //
     65   //
     66   // TODO(nona): Actually a X KeySym is passed to |keyval|, so we should use
     67   // XK_* rather than GDK_KEY_*.
     68   bool FilterKeyPressInternal(unsigned int keyval, unsigned int keycode,
     69                               int flags);
     70 
     71   // Filters keypress in key sequence mode.
     72   bool FilterKeyPressSequenceMode(unsigned int keyval, int flags);
     73 
     74   // Filters keypress in hexadecimal mode.
     75   bool FilterKeyPressHexMode(unsigned int keyval, unsigned int keycode,
     76                              int flags);
     77 
     78   // Commit a character composed from hexadecimal uncode sequence
     79   void CommitHex();
     80 
     81   // Updates preedit string in hexadecimal mode.
     82   void UpdatePreeditStringHexMode();
     83 
     84   // Remembers keypresses previously filtered.
     85   std::vector<unsigned int> compose_buffer_;
     86 
     87   // A string representing the composed character.
     88   string16 composed_character_;
     89 
     90   // Preedit string.
     91   string16 preedit_string_;
     92 
     93   // Composition mode which this instance is in.
     94   CompositionMode composition_mode_;
     95 
     96   DISALLOW_COPY_AND_ASSIGN(CharacterComposer);
     97 };
     98 
     99 }  // namespace ui
    100 
    101 #endif  // UI_BASE_IME_CHROMEOS_CHARACTER_COMPOSER_H_
    102