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