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.
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_
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/geometry/rect.h"
19 typedef union _GdkEvent GdkEvent
;
20 typedef struct _GdkDrawable GdkWindow
;
21 typedef struct _GtkIMContext GtkIMContext
;
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
{
29 X11InputMethodContextImplGtk2(ui::LinuxInputMethodContextDelegate
* delegate
,
31 ~X11InputMethodContextImplGtk2() override
;
33 // Overriden from ui::LinuxInputMethodContext
34 bool DispatchKeyEvent(const ui::KeyEvent
& key_event
) override
;
35 void SetCursorLocation(const gfx::Rect
& rect
) override
;
36 void Reset() override
;
37 void Focus() override
;
41 // Resets the cache of X modifier keycodes.
42 // TODO(yukishiino): We should call this method whenever X keyboard mapping
43 // changes, for example when a user switched to another keyboard layout.
44 void ResetXModifierKeycodesCache();
46 // Constructs a GdkEventKey from a XKeyEvent and returns it. Otherwise,
47 // returns NULL. The returned GdkEvent must be freed by gdk_event_free.
48 GdkEvent
* GdkEventFromNativeEvent(const base::NativeEvent
& native_event
);
50 // Returns true if the hardware |keycode| is assigned to a modifier key.
51 bool IsKeycodeModifierKey(unsigned int keycode
) const;
53 // Returns true if one of |keycodes| is pressed. |keybits| is a bit vector
54 // returned by XQueryKeymap, and |num_keys| is the number of keys in
56 bool IsAnyOfKeycodesPressed(const std::vector
<int>& keycodes
,
60 // GtkIMContext event handlers. They are shared among |gtk_context_simple_|
61 // and |gtk_multicontext_|.
62 CHROMEG_CALLBACK_1(X11InputMethodContextImplGtk2
, void, OnCommit
,
63 GtkIMContext
*, gchar
*);
64 CHROMEG_CALLBACK_0(X11InputMethodContextImplGtk2
, void, OnPreeditChanged
,
66 CHROMEG_CALLBACK_0(X11InputMethodContextImplGtk2
, void, OnPreeditEnd
,
68 CHROMEG_CALLBACK_0(X11InputMethodContextImplGtk2
, void, OnPreeditStart
,
71 // A set of callback functions. Must not be NULL.
72 ui::LinuxInputMethodContextDelegate
* delegate_
;
74 // IME's input GTK context.
75 GtkIMContext
* gtk_context_
;
77 GdkWindow
* gdk_last_set_client_window_
;
79 // Last known caret bounds relative to the screen coordinates.
80 gfx::Rect last_caret_bounds_
;
82 // A set of hardware keycodes of modifier keys.
83 base::hash_set
<unsigned int> modifier_keycodes_
;
85 // A list of keycodes of each modifier key.
86 std::vector
<int> meta_keycodes_
;
87 std::vector
<int> super_keycodes_
;
88 std::vector
<int> hyper_keycodes_
;
90 DISALLOW_COPY_AND_ASSIGN(X11InputMethodContextImplGtk2
);
93 } // namespace libgtk2ui
95 #endif // CHROME_BROWSER_UI_LIBGTK2UI_X11_INPUT_METHOD_CONTEXT_IMPL_GTK2_H_