Upstream version 7.35.139.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 "base/containers/hash_tables.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/strings/string16.h"
11 #include "ui/base/glib/glib_integers.h"
12 #include "ui/base/glib/glib_signal.h"
13 #include "ui/base/ime/linux/linux_input_method_context.h"
14 #include "ui/gfx/rect.h"
15
16 typedef struct _GdkDrawable GdkWindow;
17 typedef struct _GtkIMContext GtkIMContext;
18
19 namespace libgtk2ui {
20
21 // An implementation of LinuxInputMethodContext which is based on X11 event loop
22 // and uses GtkIMContext(gtk-immodule) as a bridge from/to underlying IMEs.
23 class X11InputMethodContextImplGtk2 : public ui::LinuxInputMethodContext {
24  public:
25   explicit X11InputMethodContextImplGtk2(
26       ui::LinuxInputMethodContextDelegate* delegate);
27   virtual ~X11InputMethodContextImplGtk2();
28
29   // Overriden from ui::LinuxInputMethodContext
30   virtual bool DispatchKeyEvent(const ui::KeyEvent& key_event) OVERRIDE;
31   virtual void Reset() OVERRIDE;
32   virtual void OnTextInputTypeChanged(ui::TextInputType text_input_type)
33       OVERRIDE;
34   virtual void OnCaretBoundsChanged(const gfx::Rect& caret_bounds) OVERRIDE;
35
36  private:
37   // Returns true if the hardware |keycode| is assigned to a modifier key.
38   bool IsKeycodeModifierKey(unsigned int keycode) const;
39
40   // GtkIMContext event handlers.  They are shared among |gtk_context_simple_|
41   // and |gtk_multicontext_|.
42   CHROMEG_CALLBACK_1(X11InputMethodContextImplGtk2, void, OnCommit,
43                      GtkIMContext*, gchar*);
44   CHROMEG_CALLBACK_0(X11InputMethodContextImplGtk2, void, OnPreeditChanged,
45                      GtkIMContext*);
46   CHROMEG_CALLBACK_0(X11InputMethodContextImplGtk2, void, OnPreeditEnd,
47                      GtkIMContext*);
48   CHROMEG_CALLBACK_0(X11InputMethodContextImplGtk2, void, OnPreeditStart,
49                      GtkIMContext*);
50
51   // A set of callback functions.  Must not be NULL.
52   ui::LinuxInputMethodContextDelegate* delegate_;
53
54   // IME's input context used for TEXT_INPUT_TYPE_NONE and
55   // TEXT_INPUT_TYPE_PASSWORD.
56   GtkIMContext* gtk_context_simple_;
57   // IME's input context used for the other text input types.
58   GtkIMContext* gtk_multicontext_;
59
60   // An alias to |gtk_context_simple_| or |gtk_multicontext_| depending on the
61   // text input type.  Can be NULL when it's not focused.
62   GtkIMContext* gtk_context_;
63
64   // Last set client window.
65   GdkWindow* gdk_last_set_client_window_;
66
67   // Last known caret bounds relative to the screen coordinates.
68   gfx::Rect last_caret_bounds_;
69
70   // A set of hardware keycodes of modifier keys.
71   base::hash_set<unsigned int> modifier_keycodes_;
72
73   // The helper class to trap GTK+'s "commit" signal for direct input key
74   // events.
75   //
76   // gtk_im_context_filter_keypress() emits "commit" signal in order to insert
77   // a character which is not actually processed by a IME.  This behavior seems,
78   // in Javascript world, that a keydown event with keycode = VKEY_PROCESSKEY
79   // (= 229) is fired.  So we have to trap such "commit" signal for direct input
80   // key events.  This class helps to trap such events.
81   class GtkCommitSignalTrap {
82    public:
83     GtkCommitSignalTrap();
84
85     // Enables the trap which monitors a direct input key event of |keyval|.
86     void StartTrap(guint keyval);
87
88     // Disables the trap.
89     void StopTrap();
90
91     // Checks if the committed |text| has come from a direct input key event,
92     // and returns true in that case.  Once it's trapped, IsSignalCaught()
93     // returns true.
94     // Must be called at most once between StartTrap() and StopTrap().
95     bool Trap(const base::string16& text);
96
97     // Returns true if a direct input key event is detected.
98     bool IsSignalCaught() const { return is_signal_caught_; }
99
100    private:
101     bool is_trap_enabled_;
102     guint gdk_event_key_keyval_;
103     bool is_signal_caught_;
104
105     DISALLOW_COPY_AND_ASSIGN(GtkCommitSignalTrap);
106   };
107
108   GtkCommitSignalTrap commit_signal_trap_;
109
110   FRIEND_TEST_ALL_PREFIXES(X11InputMethodContextImplGtk2FriendTest,
111                            GtkCommitSignalTrap);
112
113   DISALLOW_COPY_AND_ASSIGN(X11InputMethodContextImplGtk2);
114 };
115
116 }  // namespace libgtk2ui
117
118 #endif  // CHROME_BROWSER_UI_LIBGTK2UI_X11_INPUT_METHOD_CONTEXT_IMPL_GTK2_H_