• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_base_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_BASE_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.
composed_character()33   const base::string16& composed_character() const {
34     return composed_character_;
35   }
36 
37   // Returns the preedit string.
preedit_string()38   const base::string16& preedit_string() const { return preedit_string_; }
39 
40  private:
41   friend class CharacterComposerTest;
42 
43   // An enum to describe composition mode.
44   enum CompositionMode {
45     // This is the initial state.
46     // Composite a character with dead-keys and compose-key.
47     KEY_SEQUENCE_MODE,
48     // Composite a character with a hexadecimal unicode sequence.
49     HEX_MODE,
50   };
51 
52   // Filters keypress using IBus defined value.
53   // Returns true if the keypress is recognized as a part of composition
54   // sequence.
55   // |keyval| must be a GDK_KEY_* constant.
56   // |keycode| must be a X key code.
57   // |flags| must be a combination of ui::EF_* flags.
58   //
59   // composed_character() returns non empty string when there is a character
60   // composed after this method returns true.
61   // preedit_string() returns non empty string when there is a preedit string
62   // after this method returns true.
63   // Return values of preedit_string() is empty after this method returns false.
64   // composed_character() may have some characters which are consumed in this
65   // composing session.
66   //
67   //
68   // TODO(nona): Actually a X KeySym is passed to |keyval|, so we should use
69   // XK_* rather than GDK_KEY_*.
70   bool FilterKeyPressInternal(unsigned int keyval, unsigned int keycode,
71                               int flags);
72 
73   // Filters keypress in key sequence mode.
74   bool FilterKeyPressSequenceMode(unsigned int keyval, int flags);
75 
76   // Filters keypress in hexadecimal mode.
77   bool FilterKeyPressHexMode(unsigned int keyval, unsigned int keycode,
78                              int flags);
79 
80   // Commit a character composed from hexadecimal uncode sequence
81   void CommitHex();
82 
83   // Updates preedit string in hexadecimal mode.
84   void UpdatePreeditStringHexMode();
85 
86   // Remembers keypresses previously filtered.
87   std::vector<unsigned int> compose_buffer_;
88 
89   // A string representing the composed character.
90   base::string16 composed_character_;
91 
92   // Preedit string.
93   base::string16 preedit_string_;
94 
95   // Composition mode which this instance is in.
96   CompositionMode composition_mode_;
97 
98   DISALLOW_COPY_AND_ASSIGN(CharacterComposer);
99 };
100 
101 }  // namespace ui
102 
103 #endif  // UI_BASE_IME_CHROMEOS_CHARACTER_COMPOSER_H_
104