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_GTK_INPUT_METHOD_CONTEXT_IMPL_GTK_H_
6 #define UI_GTK_INPUT_METHOD_CONTEXT_IMPL_GTK_H_
7 
8 #include "base/macros.h"
9 #include "base/strings/string16.h"
10 #include "ui/base/glib/glib_integers.h"
11 #include "ui/base/glib/glib_signal.h"
12 #include "ui/base/ime/linux/linux_input_method_context.h"
13 #include "ui/gfx/geometry/rect.h"
14 
15 typedef struct _GdkWindow GdkWindow;
16 typedef struct _GtkIMContext GtkIMContext;
17 
18 namespace gtk {
19 
20 // An implementation of LinuxInputMethodContext which uses GtkIMContext
21 // (gtk-immodule) as a bridge from/to underlying IMEs.
22 class InputMethodContextImplGtk : public ui::LinuxInputMethodContext {
23  public:
24   InputMethodContextImplGtk(ui::LinuxInputMethodContextDelegate* delegate,
25                             bool is_simple);
26   ~InputMethodContextImplGtk() override;
27 
28   // Overridden from ui::LinuxInputMethodContext
29   bool DispatchKeyEvent(const ui::KeyEvent& key_event) override;
30   void SetCursorLocation(const gfx::Rect& rect) override;
31   void Reset() override;
32   void Focus() override;
33   void Blur() override;
34   void SetSurroundingText(const base::string16& text,
35                           const gfx::Range& selection_range) override;
36 
37  private:
38   // GtkIMContext event handlers.  They are shared among |gtk_context_simple_|
39   // and |gtk_multicontext_|.
40   CHROMEG_CALLBACK_1(InputMethodContextImplGtk,
41                      void,
42                      OnCommit,
43                      GtkIMContext*,
44                      gchar*);
45   CHROMEG_CALLBACK_0(InputMethodContextImplGtk,
46                      void,
47                      OnPreeditChanged,
48                      GtkIMContext*);
49   CHROMEG_CALLBACK_0(InputMethodContextImplGtk,
50                      void,
51                      OnPreeditEnd,
52                      GtkIMContext*);
53   CHROMEG_CALLBACK_0(InputMethodContextImplGtk,
54                      void,
55                      OnPreeditStart,
56                      GtkIMContext*);
57 
58   void SetContextClientWindow(GdkWindow* window);
59 
60   // A set of callback functions.  Must not be nullptr.
61   ui::LinuxInputMethodContextDelegate* delegate_;
62 
63   // Input method context type flag.
64   //   - true if it supports table-based input methods
65   //   - false if it supports multiple, loadable input methods
66   bool is_simple_;
67 
68   // Keeps track of current focus state.
69   bool has_focus_;
70 
71   // IME's input GTK context.
72   GtkIMContext* gtk_context_;
73 
74   gpointer gdk_last_set_client_window_;
75 
76   // Last known caret bounds relative to the screen coordinates.
77   gfx::Rect last_caret_bounds_;
78 
79   DISALLOW_COPY_AND_ASSIGN(InputMethodContextImplGtk);
80 };
81 
82 }  // namespace gtk
83 
84 #endif  // UI_GTK_INPUT_METHOD_CONTEXT_IMPL_GTK_H_
85