Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / libgtk2ui / x11_input_method_context_impl_gtk2.h
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 CHROME_BROWSER_UI_LIBGTK2UI_X11_INPUT_METHOD_CONTEXT_IMPL_GTK2_H_
6 #define CHROME_BROWSER_UI_LIBGTK2UI_X11_INPUT_METHOD_CONTEXT_IMPL_GTK2_H_
7
8 #include <vector>
9
10 #include "base/containers/hash_tables.h"
11 #include "base/event_types.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/strings/string16.h"
14 #include "ui/base/glib/glib_integers.h"
15 #include "ui/base/glib/glib_signal.h"
16 #include "ui/base/ime/linux/linux_input_method_context.h"
17 #include "ui/gfx/rect.h"
18
19 typedef union _GdkEvent GdkEvent;
20 typedef struct _GdkDrawable GdkWindow;
21 typedef struct _GtkIMContext GtkIMContext;
22
23 namespace libgtk2ui {
24
25 // An implementation of LinuxInputMethodContext which is based on X11 event loop
26 // and uses GtkIMContext(gtk-immodule) as a bridge from/to underlying IMEs.
27 class X11InputMethodContextImplGtk2 : public ui::LinuxInputMethodContext {
28  public:
29   explicit X11InputMethodContextImplGtk2(
30       ui::LinuxInputMethodContextDelegate* delegate);
31   ~X11InputMethodContextImplGtk2() override;
32
33   // Overriden from ui::LinuxInputMethodContext
34   bool DispatchKeyEvent(const ui::KeyEvent& key_event) override;
35   void Reset() override;
36   void OnTextInputTypeChanged(ui::TextInputType text_input_type) override;
37   void OnCaretBoundsChanged(const gfx::Rect& caret_bounds) override;
38
39  private:
40   // Resets the cache of X modifier keycodes.
41   // TODO(yukishiino): We should call this method whenever X keyboard mapping
42   // changes, for example when a user switched to another keyboard layout.
43   void ResetXModifierKeycodesCache();
44
45   // Constructs a GdkEventKey from a XKeyEvent and returns it.  Otherwise,
46   // returns NULL.  The returned GdkEvent must be freed by gdk_event_free.
47   GdkEvent* GdkEventFromNativeEvent(const base::NativeEvent& native_event);
48
49   // Returns true if the hardware |keycode| is assigned to a modifier key.
50   bool IsKeycodeModifierKey(unsigned int keycode) const;
51
52   // Returns true if one of |keycodes| is pressed.  |keybits| is a bit vector
53   // returned by XQueryKeymap, and |num_keys| is the number of keys in
54   // |keybits|.
55   bool IsAnyOfKeycodesPressed(const std::vector<int>& keycodes,
56                               const char* keybits,
57                               int num_keys) const;
58
59   // GtkIMContext event handlers.  They are shared among |gtk_context_simple_|
60   // and |gtk_multicontext_|.
61   CHROMEG_CALLBACK_1(X11InputMethodContextImplGtk2, void, OnCommit,
62                      GtkIMContext*, gchar*);
63   CHROMEG_CALLBACK_0(X11InputMethodContextImplGtk2, void, OnPreeditChanged,
64                      GtkIMContext*);
65   CHROMEG_CALLBACK_0(X11InputMethodContextImplGtk2, void, OnPreeditEnd,
66                      GtkIMContext*);
67   CHROMEG_CALLBACK_0(X11InputMethodContextImplGtk2, void, OnPreeditStart,
68                      GtkIMContext*);
69
70   // A set of callback functions.  Must not be NULL.
71   ui::LinuxInputMethodContextDelegate* delegate_;
72
73   // IME's input context used for TEXT_INPUT_TYPE_NONE and
74   // TEXT_INPUT_TYPE_PASSWORD.
75   GtkIMContext* gtk_context_simple_;
76   // IME's input context used for the other text input types.
77   GtkIMContext* gtk_multicontext_;
78
79   // An alias to |gtk_context_simple_| or |gtk_multicontext_| depending on the
80   // text input type.  Can be NULL when it's not focused.
81   GtkIMContext* gtk_context_;
82
83   // Last set client window.
84   GdkWindow* gdk_last_set_client_window_;
85
86   // Last known caret bounds relative to the screen coordinates.
87   gfx::Rect last_caret_bounds_;
88
89   // A set of hardware keycodes of modifier keys.
90   base::hash_set<unsigned int> modifier_keycodes_;
91
92   // A list of keycodes of each modifier key.
93   std::vector<int> meta_keycodes_;
94   std::vector<int> super_keycodes_;
95   std::vector<int> hyper_keycodes_;
96
97   // The helper class to trap GTK+'s "commit" signal for direct input key
98   // events.
99   //
100   // gtk_im_context_filter_keypress() emits "commit" signal in order to insert
101   // a character which is not actually processed by a IME.  This behavior seems,
102   // in Javascript world, that a keydown event with keycode = VKEY_PROCESSKEY
103   // (= 229) is fired.  So we have to trap such "commit" signal for direct input
104   // key events.  This class helps to trap such events.
105   class GtkCommitSignalTrap {
106    public:
107     GtkCommitSignalTrap();
108
109     // Enables the trap which monitors a direct input key event of |keyval|.
110     void StartTrap(guint keyval);
111
112     // Disables the trap.
113     void StopTrap();
114
115     // Checks if the committed |text| has come from a direct input key event,
116     // and returns true in that case.  Once it's trapped, IsSignalCaught()
117     // returns true.
118     // Must be called at most once between StartTrap() and StopTrap().
119     bool Trap(const base::string16& text);
120
121     // Returns true if a direct input key event is detected.
122     bool IsSignalCaught() const { return is_signal_caught_; }
123
124    private:
125     bool is_trap_enabled_;
126     guint gdk_event_key_keyval_;
127     bool is_signal_caught_;
128
129     DISALLOW_COPY_AND_ASSIGN(GtkCommitSignalTrap);
130   };
131
132   GtkCommitSignalTrap commit_signal_trap_;
133
134   FRIEND_TEST_ALL_PREFIXES(X11InputMethodContextImplGtk2FriendTest,
135                            GtkCommitSignalTrap);
136
137   DISALLOW_COPY_AND_ASSIGN(X11InputMethodContextImplGtk2);
138 };
139
140 }  // namespace libgtk2ui
141
142 #endif  // CHROME_BROWSER_UI_LIBGTK2UI_X11_INPUT_METHOD_CONTEXT_IMPL_GTK2_H_