input_method_context_impl_gtk.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifndef UI_GTK_INPUT_METHOD_CONTEXT_IMPL_GTK_H_
  5. #define UI_GTK_INPUT_METHOD_CONTEXT_IMPL_GTK_H_
  6. #include <string>
  7. #include "base/memory/raw_ptr.h"
  8. #include "ui/base/glib/glib_integers.h"
  9. #include "ui/base/glib/glib_signal.h"
  10. #include "ui/base/ime/linux/linux_input_method_context.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. using GtkIMContext = struct _GtkIMContext;
  13. using GdkWindow = struct _GdkWindow;
  14. namespace gtk {
  15. // An implementation of LinuxInputMethodContext which uses GtkIMContext
  16. // (gtk-immodule) as a bridge from/to underlying IMEs.
  17. class InputMethodContextImplGtk : public ui::LinuxInputMethodContext {
  18. public:
  19. explicit InputMethodContextImplGtk(
  20. ui::LinuxInputMethodContextDelegate* delegate);
  21. InputMethodContextImplGtk(const InputMethodContextImplGtk&) = delete;
  22. InputMethodContextImplGtk& operator=(const InputMethodContextImplGtk&) =
  23. delete;
  24. ~InputMethodContextImplGtk() override;
  25. // Overridden from ui::LinuxInputMethodContext
  26. bool DispatchKeyEvent(const ui::KeyEvent& key_event) override;
  27. bool IsPeekKeyEvent(const ui::KeyEvent& key_event) override;
  28. void SetCursorLocation(const gfx::Rect& rect) override;
  29. void Reset() override;
  30. void UpdateFocus(bool has_client,
  31. ui::TextInputType old_type,
  32. ui::TextInputType new_type) override;
  33. void SetSurroundingText(const std::u16string& text,
  34. const gfx::Range& selection_range) override;
  35. void SetContentType(ui::TextInputType type,
  36. ui::TextInputMode mode,
  37. uint32_t flags,
  38. bool should_do_learning) override;
  39. void SetGrammarFragmentAtCursor(
  40. const ui::GrammarFragment& fragment) override {}
  41. void SetAutocorrectInfo(const gfx::Range& autocorrect_range,
  42. const gfx::Rect& autocorrect_bounds) override {}
  43. ui::VirtualKeyboardController* GetVirtualKeyboardController() override;
  44. private:
  45. // GtkIMContext event handlers. They are shared among |gtk_context_simple_|
  46. // and |gtk_multicontext_|.
  47. CHROMEG_CALLBACK_1(InputMethodContextImplGtk,
  48. void,
  49. OnCommit,
  50. GtkIMContext*,
  51. gchar*);
  52. CHROMEG_CALLBACK_0(InputMethodContextImplGtk,
  53. void,
  54. OnPreeditChanged,
  55. GtkIMContext*);
  56. CHROMEG_CALLBACK_0(InputMethodContextImplGtk,
  57. void,
  58. OnPreeditEnd,
  59. GtkIMContext*);
  60. CHROMEG_CALLBACK_0(InputMethodContextImplGtk,
  61. void,
  62. OnPreeditStart,
  63. GtkIMContext*);
  64. // Only used on GTK3.
  65. void SetContextClientWindow(GdkWindow* window, GtkIMContext* gtk_context);
  66. // Returns the IMContext depending on the currently connected input field
  67. // type.
  68. GtkIMContext* GetIMContext();
  69. // A set of callback functions. Must not be nullptr.
  70. const raw_ptr<ui::LinuxInputMethodContextDelegate> delegate_;
  71. // Tracks the input field type.
  72. ui::TextInputType type_ = ui::TEXT_INPUT_TYPE_NONE;
  73. // IME's input GTK context.
  74. raw_ptr<GtkIMContext> gtk_context_ = nullptr;
  75. raw_ptr<GtkIMContext> gtk_simple_context_ = nullptr;
  76. // Only used on GTK3.
  77. gpointer gdk_last_set_client_window_ = nullptr;
  78. gpointer gdk_last_set_client_window_for_simple_ = nullptr;
  79. // Last known caret bounds relative to the screen coordinates, in DIPs.
  80. // Effective only on non-simple context.
  81. gfx::Rect last_caret_bounds_;
  82. };
  83. } // namespace gtk
  84. #endif // UI_GTK_INPUT_METHOD_CONTEXT_IMPL_GTK_H_