2004-08-26 14:29:25 +00:00
|
|
|
/*
|
2004-10-01 19:53:55 +00:00
|
|
|
* gtkimcontextime.c
|
2004-08-26 14:29:25 +00:00
|
|
|
* Copyright (C) 2003 Takuro Ashie
|
|
|
|
* Copyright (C) 2003-2004 Kazuki IWAMOTO
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2004-08-26 14:29:25 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Please see the following site for the detail of Windows IME API.
|
|
|
|
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/appendix/hh/appendix/imeimes2_35ph.asp
|
|
|
|
*/
|
|
|
|
|
2011-01-11 15:13:34 +00:00
|
|
|
#ifdef GTK_DISABLE_DEPRECATED
|
|
|
|
#undef GTK_DISABLE_DEPRECATED
|
|
|
|
#endif
|
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
#include "gtkimcontextime.h"
|
2018-03-09 07:13:13 +00:00
|
|
|
#include "gtkimmoduleprivate.h"
|
2019-02-23 21:52:23 +00:00
|
|
|
#include "gtkroot.h"
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
#include "imm-extra.h"
|
|
|
|
|
2017-04-18 17:31:27 +00:00
|
|
|
#include "gdk/gdkkeysyms.h"
|
2004-08-30 04:53:23 +00:00
|
|
|
#include "gdk/win32/gdkwin32.h"
|
2018-03-08 00:26:39 +00:00
|
|
|
#include "gtk/gtkimmodule.h"
|
2020-01-27 23:27:36 +00:00
|
|
|
#include "gtk/gtkstylecontextprivate.h"
|
|
|
|
#include "gtk/gtkcssstyleprivate.h"
|
2004-10-01 19:53:55 +00:00
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
/* avoid warning */
|
|
|
|
#ifdef STRICT
|
|
|
|
# undef STRICT
|
|
|
|
# include <pango/pangowin32.h>
|
|
|
|
# ifndef STRICT
|
|
|
|
# define STRICT 1
|
|
|
|
# endif
|
|
|
|
#else /* STRICT */
|
|
|
|
# include <pango/pangowin32.h>
|
|
|
|
#endif /* STRICT */
|
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
/* Determines what happens when focus is lost while preedit is in process. */
|
|
|
|
typedef enum {
|
|
|
|
/* Preedit is committed. */
|
|
|
|
GTK_WIN32_IME_FOCUS_BEHAVIOR_COMMIT,
|
|
|
|
/* Preedit is discarded. */
|
|
|
|
GTK_WIN32_IME_FOCUS_BEHAVIOR_DISCARD,
|
|
|
|
/* Preedit follows the cursor (that means it will appear in the widget
|
|
|
|
* that receives the focus) */
|
|
|
|
GTK_WIN32_IME_FOCUS_BEHAVIOR_FOLLOW,
|
|
|
|
} GtkWin32IMEFocusBehavior;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2013-07-26 15:45:19 +00:00
|
|
|
#define IS_DEAD_KEY(k) \
|
2017-04-18 17:31:27 +00:00
|
|
|
((k) >= GDK_KEY_dead_grave && (k) <= (GDK_KEY_dead_dasia+1))
|
2013-07-26 15:45:19 +00:00
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
struct _GtkIMContextIMEPrivate
|
|
|
|
{
|
2020-09-07 11:04:47 +00:00
|
|
|
/* When pretend_empty_preedit is set to TRUE,
|
|
|
|
* gtk_im_context_ime_get_preedit_string() will return an empty string
|
|
|
|
* instead of the actual content of ImmGetCompositionStringW().
|
|
|
|
*
|
|
|
|
* This is necessary because GtkEntry expects the preedit buffer to be
|
|
|
|
* cleared before commit() is called, otherwise it leads to an assertion
|
|
|
|
* failure in Pango. However, since we emit the commit() signal while
|
|
|
|
* handling the WM_IME_COMPOSITION message, the IME buffer will be non-empty,
|
|
|
|
* so we temporarily set this flag while emmiting the appropriate signals.
|
|
|
|
*
|
|
|
|
* See also:
|
|
|
|
* https://bugzilla.gnome.org/show_bug.cgi?id=787142
|
|
|
|
* https://gitlab.gnome.org/GNOME/gtk/commit/c255ba68fc2c918dd84da48a472e7973d3c00b03
|
|
|
|
*/
|
|
|
|
gboolean pretend_empty_preedit;
|
2013-07-26 15:45:19 +00:00
|
|
|
guint32 dead_key_keyval;
|
2020-09-07 11:04:47 +00:00
|
|
|
GtkWin32IMEFocusBehavior focus_behavior;
|
2004-08-26 14:29:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* GObject class methods */
|
|
|
|
static void gtk_im_context_ime_dispose (GObject *obj);
|
|
|
|
static void gtk_im_context_ime_finalize (GObject *obj);
|
|
|
|
|
|
|
|
static void gtk_im_context_ime_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
static void gtk_im_context_ime_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
|
|
|
|
/* GtkIMContext's virtual functions */
|
2017-05-27 02:38:19 +00:00
|
|
|
static void gtk_im_context_ime_set_client_widget (GtkIMContext *context,
|
|
|
|
GtkWidget *widget);
|
2004-08-26 14:29:25 +00:00
|
|
|
static gboolean gtk_im_context_ime_filter_keypress (GtkIMContext *context,
|
2020-02-21 02:21:08 +00:00
|
|
|
GdkEvent *event);
|
2004-08-26 14:29:25 +00:00
|
|
|
static void gtk_im_context_ime_reset (GtkIMContext *context);
|
|
|
|
static void gtk_im_context_ime_get_preedit_string (GtkIMContext *context,
|
2020-07-24 18:40:36 +00:00
|
|
|
char **str,
|
2004-08-26 14:29:25 +00:00
|
|
|
PangoAttrList **attrs,
|
2020-07-24 13:54:49 +00:00
|
|
|
int *cursor_pos);
|
2004-08-26 14:29:25 +00:00
|
|
|
static void gtk_im_context_ime_focus_in (GtkIMContext *context);
|
|
|
|
static void gtk_im_context_ime_focus_out (GtkIMContext *context);
|
|
|
|
static void gtk_im_context_ime_set_cursor_location (GtkIMContext *context,
|
|
|
|
GdkRectangle *area);
|
|
|
|
static void gtk_im_context_ime_set_use_preedit (GtkIMContext *context,
|
|
|
|
gboolean use_preedit);
|
|
|
|
|
|
|
|
/* GtkIMContextIME's private functions */
|
2004-10-01 19:53:55 +00:00
|
|
|
static void gtk_im_context_ime_set_preedit_font (GtkIMContext *context);
|
|
|
|
|
2018-03-24 16:39:13 +00:00
|
|
|
static GdkWin32MessageFilterReturn
|
|
|
|
gtk_im_context_ime_message_filter (GdkWin32Display *display,
|
|
|
|
MSG *msg,
|
2020-07-24 13:54:49 +00:00
|
|
|
int *ret_valp,
|
2004-08-26 14:29:25 +00:00
|
|
|
gpointer data);
|
|
|
|
|
2018-02-23 19:59:49 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkIMContextIME, gtk_im_context_ime, GTK_TYPE_IM_CONTEXT,
|
2018-03-09 07:13:13 +00:00
|
|
|
gtk_im_module_ensure_extension_point ();
|
2018-02-23 19:59:49 +00:00
|
|
|
g_io_extension_point_implement (GTK_IM_MODULE_EXTENSION_POINT_NAME,
|
|
|
|
g_define_type_id,
|
|
|
|
"ime",
|
2019-01-20 23:47:30 +00:00
|
|
|
0))
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_ime_class_init (GtkIMContextIMEClass *class)
|
|
|
|
{
|
|
|
|
GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (class);
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
|
|
|
|
gobject_class->finalize = gtk_im_context_ime_finalize;
|
|
|
|
gobject_class->dispose = gtk_im_context_ime_dispose;
|
|
|
|
gobject_class->set_property = gtk_im_context_ime_set_property;
|
|
|
|
gobject_class->get_property = gtk_im_context_ime_get_property;
|
|
|
|
|
2017-05-27 02:38:19 +00:00
|
|
|
im_context_class->set_client_widget = gtk_im_context_ime_set_client_widget;
|
2004-08-26 14:29:25 +00:00
|
|
|
im_context_class->filter_keypress = gtk_im_context_ime_filter_keypress;
|
|
|
|
im_context_class->reset = gtk_im_context_ime_reset;
|
|
|
|
im_context_class->get_preedit_string = gtk_im_context_ime_get_preedit_string;
|
|
|
|
im_context_class->focus_in = gtk_im_context_ime_focus_in;
|
|
|
|
im_context_class->focus_out = gtk_im_context_ime_focus_out;
|
|
|
|
im_context_class->set_cursor_location = gtk_im_context_ime_set_cursor_location;
|
|
|
|
im_context_class->set_use_preedit = gtk_im_context_ime_set_use_preedit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_ime_init (GtkIMContextIME *context_ime)
|
|
|
|
{
|
2022-02-21 11:39:52 +00:00
|
|
|
context_ime->client_widget = NULL;
|
2018-03-24 19:33:14 +00:00
|
|
|
context_ime->client_surface = NULL;
|
2004-08-26 14:29:25 +00:00
|
|
|
context_ime->use_preedit = TRUE;
|
|
|
|
context_ime->preediting = FALSE;
|
|
|
|
context_ime->opened = FALSE;
|
|
|
|
context_ime->focus = FALSE;
|
|
|
|
context_ime->cursor_location.x = 0;
|
|
|
|
context_ime->cursor_location.y = 0;
|
|
|
|
context_ime->cursor_location.width = 0;
|
|
|
|
context_ime->cursor_location.height = 0;
|
input/IME: Defer the emit of the "commit" signal
On Windows, when IME is used, each keystroke results in the
WM_IME_COMPOSITION event being sent first. This means that in our case
when one decides on to accept the input that is in the preedit buffer,
we first get from Windows the WM_IME_COMPOSITION event
(where we emit the commit signal), followed by the WM_IME_ENDCOMPOSITION
event (where we emit the pair of preedit-changed and preedit-end
signals).
Since commit f11f989 (GtkEntry: Remove recompute idle), we do the input
recomputation directly, this will cause a pair of "Pango-WARNING:
Assertion failed: (index >= 0 && index <= layout->length)" being shown,
as gtkentry.c's priv->preedit_length and priv->preedit_cursor was unable
to be reset to 0 in time as a result of the recomputation triggered by
the commit being done before the reset of priv->preedit_length and
priv->preedit_cursor (which are no longer valid as we essentially say
that we are done with the preedit buffer).
As we could only acquire the final string that was entered in this
preedit session when we handle the WM_IME_COMPOSITION event, fix this by
saving up the final string we acquire from Windows IME in UTF-8 when we
handle the WM_IME_COMPOSITION event from Windows, and emit the commit
signal with that string after we emit the preedit-changed and
preedit-end signals when we handle the WM_IME_ENDCOMPOSITION event from
Windows, which comes afterwards.
Also fix the formatting of the code around the parts of the files that
was changed.
https://bugzilla.gnome.org/show_bug.cgi?id=787142
2017-08-31 10:43:07 +00:00
|
|
|
context_ime->commit_string = NULL;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
context_ime->priv = g_malloc0 (sizeof (GtkIMContextIMEPrivate));
|
2020-09-07 11:04:47 +00:00
|
|
|
context_ime->priv->focus_behavior = GTK_WIN32_IME_FOCUS_BEHAVIOR_COMMIT;
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_ime_dispose (GObject *obj)
|
|
|
|
{
|
|
|
|
GtkIMContext *context = GTK_IM_CONTEXT (obj);
|
|
|
|
GtkIMContextIME *context_ime = GTK_IM_CONTEXT_IME (obj);
|
|
|
|
|
2018-03-24 19:33:14 +00:00
|
|
|
if (context_ime->client_surface)
|
2017-05-27 02:38:19 +00:00
|
|
|
gtk_im_context_ime_set_client_widget (context, NULL);
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2018-03-08 00:26:39 +00:00
|
|
|
G_OBJECT_CLASS (gtk_im_context_ime_parent_class)->dispose (obj);
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_ime_finalize (GObject *obj)
|
|
|
|
{
|
|
|
|
GtkIMContextIME *context_ime = GTK_IM_CONTEXT_IME (obj);
|
|
|
|
|
|
|
|
g_free (context_ime->priv);
|
|
|
|
context_ime->priv = NULL;
|
|
|
|
|
2018-03-08 00:26:39 +00:00
|
|
|
G_OBJECT_CLASS (gtk_im_context_ime_parent_class)->finalize (obj);
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_ime_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkIMContextIME *context_ime = GTK_IM_CONTEXT_IME (object);
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_IM_CONTEXT_IME (context_ime));
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_ime_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkIMContextIME *context_ime = GTK_IM_CONTEXT_IME (object);
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_IM_CONTEXT_IME (context_ime));
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GtkIMContext *
|
|
|
|
gtk_im_context_ime_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_IM_CONTEXT_IME, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-27 02:38:19 +00:00
|
|
|
gtk_im_context_ime_set_client_widget (GtkIMContext *context,
|
|
|
|
GtkWidget *widget)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
|
|
|
GtkIMContextIME *context_ime;
|
2022-02-21 11:40:54 +00:00
|
|
|
GdkSurface *surface = NULL;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_IM_CONTEXT_IME (context));
|
|
|
|
context_ime = GTK_IM_CONTEXT_IME (context);
|
2019-05-21 21:29:38 +00:00
|
|
|
|
|
|
|
if (widget)
|
2022-02-21 11:40:54 +00:00
|
|
|
surface = gtk_native_get_surface (gtk_widget_get_native (widget));
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2022-02-21 11:40:54 +00:00
|
|
|
if (surface != NULL)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
2022-08-23 12:31:07 +00:00
|
|
|
HWND hwnd = gdk_win32_surface_get_handle (surface);
|
2020-09-07 11:04:47 +00:00
|
|
|
HIMC himc = ImmGetContext (hwnd);
|
2004-10-01 19:53:55 +00:00
|
|
|
if (himc)
|
2020-09-07 11:04:47 +00:00
|
|
|
{
|
|
|
|
context_ime->opened = ImmGetOpenStatus (himc);
|
|
|
|
ImmReleaseContext (hwnd, himc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context_ime->opened = FALSE;
|
|
|
|
}
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
|
|
|
else if (context_ime->focus)
|
|
|
|
{
|
|
|
|
gtk_im_context_ime_focus_out (context);
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:39:52 +00:00
|
|
|
context_ime->client_widget = widget;
|
2022-02-21 11:40:54 +00:00
|
|
|
context_ime->client_surface = surface;
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 15:45:19 +00:00
|
|
|
static gunichar
|
|
|
|
_gtk_im_context_ime_dead_key_unichar (guint keyval,
|
|
|
|
gboolean spacing)
|
|
|
|
{
|
|
|
|
switch (keyval)
|
|
|
|
{
|
|
|
|
#define CASE(keysym, unicode, spacing_unicode) \
|
2017-04-18 17:31:27 +00:00
|
|
|
case GDK_KEY_dead_##keysym: return (spacing) ? spacing_unicode : unicode;
|
2013-07-26 15:45:19 +00:00
|
|
|
|
|
|
|
CASE (grave, 0x0300, 0x0060);
|
|
|
|
CASE (acute, 0x0301, 0x00b4);
|
|
|
|
CASE (circumflex, 0x0302, 0x005e);
|
|
|
|
CASE (tilde, 0x0303, 0x007e); /* Also used with perispomeni, 0x342. */
|
|
|
|
CASE (macron, 0x0304, 0x00af);
|
|
|
|
CASE (breve, 0x0306, 0x02d8);
|
|
|
|
CASE (abovedot, 0x0307, 0x02d9);
|
|
|
|
CASE (diaeresis, 0x0308, 0x00a8);
|
|
|
|
CASE (hook, 0x0309, 0);
|
|
|
|
CASE (abovering, 0x030A, 0x02da);
|
|
|
|
CASE (doubleacute, 0x030B, 0x2dd);
|
|
|
|
CASE (caron, 0x030C, 0x02c7);
|
|
|
|
CASE (abovecomma, 0x0313, 0); /* Equivalent to psili */
|
|
|
|
CASE (abovereversedcomma, 0x0314, 0); /* Equivalent to dasia */
|
|
|
|
CASE (horn, 0x031B, 0); /* Legacy use for psili, 0x313 (or 0x343). */
|
|
|
|
CASE (belowdot, 0x0323, 0);
|
|
|
|
CASE (cedilla, 0x0327, 0x00b8);
|
|
|
|
CASE (ogonek, 0x0328, 0); /* Legacy use for dasia, 0x314.*/
|
|
|
|
CASE (iota, 0x0345, 0);
|
|
|
|
|
|
|
|
#undef CASE
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gtk_im_context_ime_commit_unichar (GtkIMContextIME *context_ime,
|
|
|
|
gunichar c)
|
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
char utf8[10];
|
2013-07-26 15:45:19 +00:00
|
|
|
int len;
|
|
|
|
|
|
|
|
if (context_ime->priv->dead_key_keyval != 0)
|
|
|
|
{
|
|
|
|
gunichar combining;
|
|
|
|
|
|
|
|
combining =
|
|
|
|
_gtk_im_context_ime_dead_key_unichar (context_ime->priv->dead_key_keyval,
|
|
|
|
FALSE);
|
|
|
|
g_unichar_compose (c, combining, &c);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = g_unichar_to_utf8 (c, utf8);
|
|
|
|
utf8[len] = 0;
|
|
|
|
|
|
|
|
g_signal_emit_by_name (context_ime, "commit", utf8);
|
|
|
|
context_ime->priv->dead_key_keyval = 0;
|
|
|
|
}
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_im_context_ime_filter_keypress (GtkIMContext *context,
|
2020-02-21 02:21:08 +00:00
|
|
|
GdkEvent *event)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
|
|
|
GtkIMContextIME *context_ime;
|
|
|
|
gboolean retval = FALSE;
|
2004-10-01 19:53:55 +00:00
|
|
|
guint32 c;
|
2022-01-13 12:43:23 +00:00
|
|
|
GdkModifierType state, consumed_modifiers, no_text_input_mask;
|
2017-10-24 08:06:32 +00:00
|
|
|
guint keyval;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_IM_CONTEXT_IME (context), FALSE);
|
|
|
|
g_return_val_if_fail (event, FALSE);
|
|
|
|
|
2017-10-24 08:06:32 +00:00
|
|
|
if (gdk_event_get_event_type ((GdkEvent *) event) == GDK_KEY_RELEASE)
|
2004-10-01 19:53:55 +00:00
|
|
|
return FALSE;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2022-01-13 12:43:23 +00:00
|
|
|
no_text_input_mask = GDK_ALT_MASK|GDK_CONTROL_MASK;
|
|
|
|
|
2020-02-18 05:38:55 +00:00
|
|
|
state = gdk_event_get_modifier_state ((GdkEvent *) event);
|
2022-01-13 12:43:23 +00:00
|
|
|
consumed_modifiers = gdk_key_event_get_consumed_modifiers (event);
|
2017-10-24 08:06:32 +00:00
|
|
|
|
2022-01-13 12:43:23 +00:00
|
|
|
if (state & no_text_input_mask & ~consumed_modifiers)
|
2004-12-06 05:21:39 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
context_ime = GTK_IM_CONTEXT_IME (context);
|
2004-10-01 19:53:55 +00:00
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
if (!context_ime->focus)
|
|
|
|
return FALSE;
|
2004-10-01 19:53:55 +00:00
|
|
|
|
2018-03-21 10:49:14 +00:00
|
|
|
if (!GDK_IS_SURFACE (context_ime->client_surface))
|
2004-08-26 14:29:25 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2020-02-18 05:38:55 +00:00
|
|
|
keyval = gdk_key_event_get_keyval ((GdkEvent *) event);
|
2017-10-24 08:06:32 +00:00
|
|
|
|
|
|
|
if (keyval == GDK_KEY_space &&
|
2013-07-26 15:45:19 +00:00
|
|
|
context_ime->priv->dead_key_keyval != 0)
|
|
|
|
{
|
|
|
|
c = _gtk_im_context_ime_dead_key_unichar (context_ime->priv->dead_key_keyval, TRUE);
|
|
|
|
context_ime->priv->dead_key_keyval = 0;
|
|
|
|
_gtk_im_context_ime_commit_unichar (context_ime, c);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-10-24 08:06:32 +00:00
|
|
|
c = gdk_keyval_to_unicode (keyval);
|
2013-07-26 15:45:19 +00:00
|
|
|
|
2022-01-17 10:56:48 +00:00
|
|
|
if (c && !g_unichar_iscntrl(c))
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
2013-07-26 15:45:19 +00:00
|
|
|
_gtk_im_context_ime_commit_unichar (context_ime, c);
|
2004-08-26 14:29:25 +00:00
|
|
|
retval = TRUE;
|
|
|
|
}
|
2017-10-24 08:06:32 +00:00
|
|
|
else if (IS_DEAD_KEY (keyval))
|
2013-07-26 15:45:19 +00:00
|
|
|
{
|
|
|
|
gunichar dead_key;
|
|
|
|
|
2017-10-24 08:06:32 +00:00
|
|
|
dead_key = _gtk_im_context_ime_dead_key_unichar (keyval, FALSE);
|
2013-07-26 15:45:19 +00:00
|
|
|
|
|
|
|
/* Emulate double input of dead keys */
|
2017-10-24 08:06:32 +00:00
|
|
|
if (dead_key && keyval == context_ime->priv->dead_key_keyval)
|
2013-07-26 15:45:19 +00:00
|
|
|
{
|
|
|
|
c = _gtk_im_context_ime_dead_key_unichar (context_ime->priv->dead_key_keyval, TRUE);
|
|
|
|
context_ime->priv->dead_key_keyval = 0;
|
|
|
|
_gtk_im_context_ime_commit_unichar (context_ime, c);
|
|
|
|
_gtk_im_context_ime_commit_unichar (context_ime, c);
|
|
|
|
}
|
|
|
|
else
|
2017-10-24 08:06:32 +00:00
|
|
|
context_ime->priv->dead_key_keyval = keyval;
|
2013-07-26 15:45:19 +00:00
|
|
|
}
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_ime_reset (GtkIMContext *context)
|
|
|
|
{
|
|
|
|
GtkIMContextIME *context_ime = GTK_IM_CONTEXT_IME (context);
|
|
|
|
HWND hwnd;
|
|
|
|
HIMC himc;
|
|
|
|
|
2018-03-21 10:49:14 +00:00
|
|
|
if (!context_ime->client_surface)
|
2011-06-06 23:33:23 +00:00
|
|
|
return;
|
|
|
|
|
2022-08-23 12:31:07 +00:00
|
|
|
hwnd = gdk_win32_surface_get_handle (context_ime->client_surface);
|
2004-08-26 14:29:25 +00:00
|
|
|
himc = ImmGetContext (hwnd);
|
|
|
|
if (!himc)
|
|
|
|
return;
|
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
ImmNotifyIME (himc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
|
|
|
|
|
2012-11-19 14:29:51 +00:00
|
|
|
if (context_ime->preediting)
|
|
|
|
{
|
|
|
|
context_ime->preediting = FALSE;
|
|
|
|
g_signal_emit_by_name (context, "preedit-changed");
|
|
|
|
}
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
ImmReleaseContext (hwnd, himc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
static char *
|
2020-09-07 11:04:47 +00:00
|
|
|
get_utf8_preedit_string (GtkIMContextIME *context_ime,
|
|
|
|
int kind,
|
|
|
|
int *pos_ret)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
2020-09-07 11:04:47 +00:00
|
|
|
gunichar2 *utf16str = NULL;
|
|
|
|
glong size;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *utf8str = NULL;
|
2004-08-26 14:29:25 +00:00
|
|
|
HWND hwnd;
|
|
|
|
HIMC himc;
|
2020-07-24 13:54:49 +00:00
|
|
|
int pos = 0;
|
2020-09-07 11:04:47 +00:00
|
|
|
GError *error = NULL;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
if (pos_ret)
|
|
|
|
*pos_ret = 0;
|
|
|
|
|
2018-03-21 10:49:14 +00:00
|
|
|
if (!context_ime->client_surface)
|
2012-08-29 06:03:46 +00:00
|
|
|
return g_strdup ("");
|
2022-08-23 12:31:07 +00:00
|
|
|
hwnd = gdk_win32_surface_get_handle (context_ime->client_surface);
|
2004-08-26 14:29:25 +00:00
|
|
|
himc = ImmGetContext (hwnd);
|
2004-10-01 19:53:55 +00:00
|
|
|
if (!himc)
|
|
|
|
return g_strdup ("");
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
size = ImmGetCompositionStringW (himc, kind, NULL, 0);
|
|
|
|
|
|
|
|
if (size > 0)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
2020-09-07 11:04:47 +00:00
|
|
|
utf16str = g_malloc (size);
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
ImmGetCompositionStringW (himc, kind, utf16str, size);
|
|
|
|
utf8str = g_utf16_to_utf8 (utf16str, size / sizeof (gunichar2),
|
|
|
|
NULL, NULL, &error);
|
|
|
|
if (error)
|
2018-10-09 09:06:39 +00:00
|
|
|
{
|
2020-09-07 11:04:47 +00:00
|
|
|
g_warning ("%s", error->message);
|
|
|
|
g_error_free (error);
|
2018-10-09 09:06:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
if (pos_ret)
|
2018-10-09 09:06:39 +00:00
|
|
|
{
|
2020-09-07 11:04:47 +00:00
|
|
|
pos = ImmGetCompositionStringW (himc, GCS_CURSORPOS, NULL, 0);
|
|
|
|
if (pos < 0 || size < pos)
|
2018-10-09 09:06:39 +00:00
|
|
|
{
|
2020-09-07 11:04:47 +00:00
|
|
|
g_warning ("ImmGetCompositionString: "
|
|
|
|
"Invalid cursor position!");
|
|
|
|
pos = 0;
|
2018-10-09 09:06:39 +00:00
|
|
|
}
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!utf8str)
|
|
|
|
{
|
|
|
|
utf8str = g_strdup ("");
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos_ret)
|
|
|
|
*pos_ret = pos;
|
|
|
|
|
|
|
|
ImmReleaseContext (hwnd, himc);
|
2020-09-07 11:04:47 +00:00
|
|
|
g_free (utf16str);
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
return utf8str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PangoAttrList *
|
2020-07-24 18:40:36 +00:00
|
|
|
get_pango_attr_list (GtkIMContextIME *context_ime, const char *utf8str)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
|
|
|
PangoAttrList *attrs = pango_attr_list_new ();
|
|
|
|
HWND hwnd;
|
|
|
|
HIMC himc;
|
2020-09-07 11:04:47 +00:00
|
|
|
guint8 *buf = NULL;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2018-03-21 10:49:14 +00:00
|
|
|
if (!context_ime->client_surface)
|
2012-08-29 06:03:46 +00:00
|
|
|
return attrs;
|
2022-08-23 12:31:07 +00:00
|
|
|
hwnd = gdk_win32_surface_get_handle (context_ime->client_surface);
|
2004-08-26 14:29:25 +00:00
|
|
|
himc = ImmGetContext (hwnd);
|
2004-10-01 19:53:55 +00:00
|
|
|
if (!himc)
|
|
|
|
return attrs;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
if (context_ime->preediting)
|
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *schr = utf8str, *echr;
|
2004-08-26 14:29:25 +00:00
|
|
|
guint16 f_red, f_green, f_blue, b_red, b_green, b_blue;
|
|
|
|
glong len, spos = 0, epos, sidx = 0, eidx;
|
|
|
|
PangoAttribute *attr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get attributes list of IME.
|
|
|
|
*/
|
2005-02-03 20:51:48 +00:00
|
|
|
len = ImmGetCompositionStringW (himc, GCS_COMPATTR, NULL, 0);
|
2020-09-07 11:04:47 +00:00
|
|
|
buf = g_malloc (len);
|
2005-02-03 20:51:48 +00:00
|
|
|
ImmGetCompositionStringW (himc, GCS_COMPATTR, buf, len);
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* schr and echr are pointer in utf8str.
|
|
|
|
*/
|
|
|
|
for (echr = g_utf8_next_char (utf8str); *schr != '\0';
|
|
|
|
echr = g_utf8_next_char (echr))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* spos and epos are buf(attributes list of IME) position by
|
|
|
|
* bytes.
|
2004-10-01 19:53:55 +00:00
|
|
|
* Using the wide-char API, this value is same with UTF-8 offset.
|
2004-08-26 14:29:25 +00:00
|
|
|
*/
|
2004-10-01 19:53:55 +00:00
|
|
|
epos = g_utf8_pointer_to_offset (utf8str, echr);
|
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
/*
|
|
|
|
* sidx and eidx are positions in utf8str by bytes.
|
|
|
|
*/
|
|
|
|
eidx = echr - utf8str;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* convert attributes list to PangoAttriute.
|
|
|
|
*/
|
|
|
|
if (*echr == '\0' || buf[spos] != buf[epos])
|
|
|
|
{
|
|
|
|
switch (buf[spos])
|
|
|
|
{
|
|
|
|
case ATTR_TARGET_CONVERTED:
|
|
|
|
attr = pango_attr_underline_new (PANGO_UNDERLINE_DOUBLE);
|
|
|
|
attr->start_index = sidx;
|
|
|
|
attr->end_index = eidx;
|
|
|
|
pango_attr_list_change (attrs, attr);
|
|
|
|
f_red = f_green = f_blue = 0;
|
|
|
|
b_red = b_green = b_blue = 0xffff;
|
|
|
|
break;
|
|
|
|
case ATTR_TARGET_NOTCONVERTED:
|
|
|
|
f_red = f_green = f_blue = 0xffff;
|
|
|
|
b_red = b_green = b_blue = 0;
|
|
|
|
break;
|
|
|
|
case ATTR_INPUT_ERROR:
|
|
|
|
f_red = f_green = f_blue = 0;
|
|
|
|
b_red = b_green = b_blue = 0x7fff;
|
|
|
|
break;
|
|
|
|
default: /* ATTR_INPUT,ATTR_CONVERTED,ATTR_FIXEDCONVERTED */
|
|
|
|
attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
|
|
|
|
attr->start_index = sidx;
|
|
|
|
attr->end_index = eidx;
|
|
|
|
pango_attr_list_change (attrs, attr);
|
|
|
|
f_red = f_green = f_blue = 0;
|
|
|
|
b_red = b_green = b_blue = 0xffff;
|
|
|
|
}
|
|
|
|
attr = pango_attr_foreground_new (f_red, f_green, f_blue);
|
|
|
|
attr->start_index = sidx;
|
|
|
|
attr->end_index = eidx;
|
|
|
|
pango_attr_list_change (attrs, attr);
|
|
|
|
attr = pango_attr_background_new (b_red, b_green, b_blue);
|
|
|
|
attr->start_index = sidx;
|
|
|
|
attr->end_index = eidx;
|
|
|
|
pango_attr_list_change (attrs, attr);
|
|
|
|
|
|
|
|
schr = echr;
|
|
|
|
spos = epos;
|
|
|
|
sidx = eidx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImmReleaseContext (hwnd, himc);
|
2020-09-07 11:04:47 +00:00
|
|
|
g_free (buf);
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_ime_get_preedit_string (GtkIMContext *context,
|
2020-07-24 18:40:36 +00:00
|
|
|
char **str,
|
2004-08-26 14:29:25 +00:00
|
|
|
PangoAttrList **attrs,
|
2020-07-24 13:54:49 +00:00
|
|
|
int *cursor_pos)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
char *utf8str = NULL;
|
2020-07-24 13:54:49 +00:00
|
|
|
int pos = 0;
|
2004-08-26 14:29:25 +00:00
|
|
|
GtkIMContextIME *context_ime;
|
|
|
|
|
|
|
|
context_ime = GTK_IM_CONTEXT_IME (context);
|
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
if (!context_ime->focus || context_ime->priv->pretend_empty_preedit)
|
|
|
|
utf8str = g_strdup ("");
|
|
|
|
else
|
|
|
|
utf8str = get_utf8_preedit_string (context_ime, GCS_COMPSTR, &pos);
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
if (attrs)
|
|
|
|
*attrs = get_pango_attr_list (context_ime, utf8str);
|
|
|
|
|
|
|
|
if (str)
|
2020-09-07 11:04:47 +00:00
|
|
|
*str = utf8str;
|
2004-08-26 14:29:25 +00:00
|
|
|
else
|
2020-09-07 11:04:47 +00:00
|
|
|
utf8str = NULL;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
if (cursor_pos)
|
|
|
|
*cursor_pos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_ime_focus_in (GtkIMContext *context)
|
|
|
|
{
|
|
|
|
GtkIMContextIME *context_ime = GTK_IM_CONTEXT_IME (context);
|
2018-03-20 10:40:08 +00:00
|
|
|
GdkSurface *toplevel;
|
2014-08-13 15:51:43 +00:00
|
|
|
HWND hwnd;
|
2004-08-26 14:29:25 +00:00
|
|
|
HIMC himc;
|
|
|
|
|
2018-03-21 10:49:14 +00:00
|
|
|
if (!GDK_IS_SURFACE (context_ime->client_surface))
|
2004-08-26 14:29:25 +00:00
|
|
|
return;
|
|
|
|
|
2020-08-21 12:41:13 +00:00
|
|
|
/* switch current context */
|
2004-10-01 19:53:55 +00:00
|
|
|
context_ime->focus = TRUE;
|
|
|
|
|
2019-05-19 03:09:05 +00:00
|
|
|
toplevel = context_ime->client_surface;
|
2020-09-07 11:04:47 +00:00
|
|
|
if (!GDK_IS_SURFACE (toplevel))
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
|
|
|
g_warning ("gtk_im_context_ime_focus_in(): "
|
|
|
|
"cannot find toplevel window.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-23 12:31:07 +00:00
|
|
|
hwnd = gdk_win32_surface_get_handle (toplevel);
|
2020-09-07 11:04:47 +00:00
|
|
|
himc = ImmGetContext (hwnd);
|
|
|
|
if (!himc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gdk_win32_display_add_filter (GDK_WIN32_DISPLAY (gdk_surface_get_display (toplevel)),
|
|
|
|
gtk_im_context_ime_message_filter, context_ime);
|
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
/* restore preedit context */
|
2020-09-07 11:04:47 +00:00
|
|
|
context_ime->opened = ImmGetOpenStatus (himc);
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
switch (context_ime->priv->focus_behavior)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
2020-09-07 11:04:47 +00:00
|
|
|
case GTK_WIN32_IME_FOCUS_BEHAVIOR_COMMIT:
|
|
|
|
case GTK_WIN32_IME_FOCUS_BEHAVIOR_DISCARD:
|
|
|
|
gtk_im_context_ime_reset (context);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GTK_WIN32_IME_FOCUS_BEHAVIOR_FOLLOW:
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
2020-09-07 11:04:47 +00:00
|
|
|
gchar *utf8str = get_utf8_preedit_string (context_ime, GCS_COMPSTR, NULL);
|
|
|
|
if (utf8str != NULL && strlen(utf8str) > 0)
|
|
|
|
{
|
|
|
|
context_ime->preediting = TRUE;
|
|
|
|
gtk_im_context_ime_set_cursor_location (context, NULL);
|
|
|
|
g_signal_emit_by_name (context, "preedit-start");
|
|
|
|
g_signal_emit_by_name (context, "preedit-changed");
|
|
|
|
}
|
|
|
|
g_free (utf8str);
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
2020-09-07 11:04:47 +00:00
|
|
|
break;
|
2021-09-24 21:18:15 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clean */
|
|
|
|
ImmReleaseContext (hwnd, himc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_ime_focus_out (GtkIMContext *context)
|
|
|
|
{
|
|
|
|
GtkIMContextIME *context_ime = GTK_IM_CONTEXT_IME (context);
|
2020-09-07 11:04:47 +00:00
|
|
|
gboolean was_preediting;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2018-03-21 10:49:14 +00:00
|
|
|
if (!GDK_IS_SURFACE (context_ime->client_surface))
|
2004-08-26 14:29:25 +00:00
|
|
|
return;
|
|
|
|
|
2020-08-21 12:41:13 +00:00
|
|
|
/* switch current context */
|
2020-09-07 11:04:47 +00:00
|
|
|
was_preediting = context_ime->preediting;
|
|
|
|
context_ime->opened = FALSE;
|
|
|
|
context_ime->preediting = FALSE;
|
2004-10-01 19:53:55 +00:00
|
|
|
context_ime->focus = FALSE;
|
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
switch (context_ime->priv->focus_behavior)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
2020-09-07 11:04:47 +00:00
|
|
|
case GTK_WIN32_IME_FOCUS_BEHAVIOR_COMMIT:
|
|
|
|
if (was_preediting)
|
|
|
|
{
|
|
|
|
gchar *utf8str = get_utf8_preedit_string (context_ime, GCS_COMPSTR, NULL);
|
|
|
|
|
|
|
|
context_ime->priv->pretend_empty_preedit = TRUE;
|
|
|
|
g_signal_emit_by_name (context, "preedit-changed");
|
|
|
|
g_signal_emit_by_name (context, "preedit-end");
|
|
|
|
g_signal_emit_by_name (context, "commit", utf8str);
|
|
|
|
g_signal_emit_by_name (context, "preedit-start");
|
|
|
|
g_signal_emit_by_name (context, "preedit-changed");
|
|
|
|
context_ime->priv->pretend_empty_preedit = FALSE;
|
|
|
|
g_free (utf8str);
|
|
|
|
}
|
2021-08-26 03:14:18 +00:00
|
|
|
G_GNUC_FALLTHROUGH;
|
2020-09-07 11:04:47 +00:00
|
|
|
case GTK_WIN32_IME_FOCUS_BEHAVIOR_DISCARD:
|
|
|
|
gtk_im_context_ime_reset (context);
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
/* Callbacks triggered by im_context_ime_reset() could set the focus back to our
|
|
|
|
context. In that case, we want to exit here. */
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
if (context_ime->focus)
|
|
|
|
return;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GTK_WIN32_IME_FOCUS_BEHAVIOR_FOLLOW:
|
|
|
|
break;
|
2022-08-23 12:42:15 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 12:41:13 +00:00
|
|
|
/* remove event filter */
|
2020-09-07 11:04:47 +00:00
|
|
|
if (GDK_IS_SURFACE (context_ime->client_surface))
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
2020-09-07 11:04:47 +00:00
|
|
|
gdk_win32_display_remove_filter (GDK_WIN32_DISPLAY (gdk_surface_get_display (context_ime->client_surface)),
|
2018-03-24 16:39:13 +00:00
|
|
|
gtk_im_context_ime_message_filter,
|
|
|
|
context_ime);
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
2020-09-07 11:04:47 +00:00
|
|
|
|
|
|
|
if (was_preediting)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
|
|
|
g_warning ("gtk_im_context_ime_focus_out(): "
|
|
|
|
"cannot find toplevel window.");
|
2020-09-07 11:04:47 +00:00
|
|
|
g_signal_emit_by_name (context, "preedit-changed");
|
|
|
|
g_signal_emit_by_name (context, "preedit-end");
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_ime_set_cursor_location (GtkIMContext *context,
|
|
|
|
GdkRectangle *area)
|
|
|
|
{
|
|
|
|
GtkIMContextIME *context_ime;
|
|
|
|
COMPOSITIONFORM cf;
|
|
|
|
HWND hwnd;
|
|
|
|
HIMC himc;
|
2022-02-19 17:31:28 +00:00
|
|
|
int scale;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_IM_CONTEXT_IME (context));
|
|
|
|
|
|
|
|
context_ime = GTK_IM_CONTEXT_IME (context);
|
|
|
|
if (area)
|
|
|
|
context_ime->cursor_location = *area;
|
|
|
|
|
2018-03-21 10:49:14 +00:00
|
|
|
if (!context_ime->client_surface)
|
2004-08-26 14:29:25 +00:00
|
|
|
return;
|
|
|
|
|
2022-08-23 12:31:07 +00:00
|
|
|
hwnd = gdk_win32_surface_get_handle (context_ime->client_surface);
|
2004-08-26 14:29:25 +00:00
|
|
|
himc = ImmGetContext (hwnd);
|
|
|
|
if (!himc)
|
|
|
|
return;
|
|
|
|
|
2022-02-19 17:31:28 +00:00
|
|
|
scale = gdk_surface_get_scale_factor (context_ime->client_surface);
|
2022-02-21 11:32:20 +00:00
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
cf.dwStyle = CFS_POINT;
|
2022-02-21 11:32:20 +00:00
|
|
|
cf.ptCurrentPos.x = context_ime->cursor_location.x * scale;
|
|
|
|
cf.ptCurrentPos.y = context_ime->cursor_location.y * scale;
|
2004-08-26 14:29:25 +00:00
|
|
|
ImmSetCompositionWindow (himc, &cf);
|
|
|
|
|
|
|
|
ImmReleaseContext (hwnd, himc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_ime_set_use_preedit (GtkIMContext *context,
|
|
|
|
gboolean use_preedit)
|
|
|
|
{
|
|
|
|
GtkIMContextIME *context_ime;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_IM_CONTEXT_IME (context));
|
|
|
|
context_ime = GTK_IM_CONTEXT_IME (context);
|
|
|
|
|
|
|
|
context_ime->use_preedit = use_preedit;
|
|
|
|
if (context_ime->preediting)
|
|
|
|
{
|
|
|
|
HWND hwnd;
|
|
|
|
HIMC himc;
|
|
|
|
|
2022-08-23 12:31:07 +00:00
|
|
|
hwnd = gdk_win32_surface_get_handle (context_ime->client_surface);
|
2004-08-26 14:29:25 +00:00
|
|
|
himc = ImmGetContext (hwnd);
|
|
|
|
if (!himc)
|
|
|
|
return;
|
2004-10-01 19:53:55 +00:00
|
|
|
|
|
|
|
/* FIXME: What to do? */
|
|
|
|
|
|
|
|
ImmReleaseContext (hwnd, himc);
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2004-10-01 19:53:55 +00:00
|
|
|
gtk_im_context_ime_set_preedit_font (GtkIMContext *context)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
|
|
|
GtkIMContextIME *context_ime;
|
|
|
|
HWND hwnd;
|
|
|
|
HIMC himc;
|
2004-10-01 19:53:55 +00:00
|
|
|
HKL ime = GetKeyboardLayout (0);
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *lang;
|
2004-10-01 19:53:55 +00:00
|
|
|
gunichar wc;
|
2004-08-26 14:29:25 +00:00
|
|
|
PangoContext *pango_context;
|
2004-10-01 19:53:55 +00:00
|
|
|
PangoFont *font;
|
2010-06-27 20:41:50 +00:00
|
|
|
LOGFONT *logfont;
|
2013-07-31 03:42:36 +00:00
|
|
|
PangoFontDescription *font_desc;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_IM_CONTEXT_IME (context));
|
|
|
|
|
|
|
|
context_ime = GTK_IM_CONTEXT_IME (context);
|
|
|
|
|
2022-02-21 11:39:52 +00:00
|
|
|
if (!(context_ime->client_widget && context_ime->client_surface))
|
2019-05-21 05:18:50 +00:00
|
|
|
return;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2022-08-23 12:31:07 +00:00
|
|
|
hwnd = gdk_win32_surface_get_handle (context_ime->client_surface);
|
2004-08-26 14:29:25 +00:00
|
|
|
himc = ImmGetContext (hwnd);
|
|
|
|
if (!himc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* set font */
|
2022-02-21 11:39:52 +00:00
|
|
|
pango_context = gtk_widget_get_pango_context (context_ime->client_widget);
|
2004-08-26 14:29:25 +00:00
|
|
|
if (!pango_context)
|
|
|
|
goto ERROR_OUT;
|
|
|
|
|
2004-10-01 19:53:55 +00:00
|
|
|
/* Try to make sure we use a font that actually can show the
|
|
|
|
* language in question.
|
2018-02-23 19:59:49 +00:00
|
|
|
*/
|
2004-10-01 19:53:55 +00:00
|
|
|
|
|
|
|
switch (PRIMARYLANGID (LOWORD (ime)))
|
|
|
|
{
|
|
|
|
case LANG_JAPANESE:
|
|
|
|
lang = "ja"; break;
|
|
|
|
case LANG_KOREAN:
|
|
|
|
lang = "ko"; break;
|
|
|
|
case LANG_CHINESE:
|
|
|
|
switch (SUBLANGID (LOWORD (ime)))
|
|
|
|
{
|
|
|
|
case SUBLANG_CHINESE_TRADITIONAL:
|
|
|
|
lang = "zh_TW"; break;
|
|
|
|
case SUBLANG_CHINESE_SIMPLIFIED:
|
|
|
|
lang = "zh_CN"; break;
|
|
|
|
case SUBLANG_CHINESE_HONGKONG:
|
|
|
|
lang = "zh_HK"; break;
|
|
|
|
case SUBLANG_CHINESE_SINGAPORE:
|
|
|
|
lang = "zh_SG"; break;
|
|
|
|
case SUBLANG_CHINESE_MACAU:
|
|
|
|
lang = "zh_MO"; break;
|
|
|
|
default:
|
|
|
|
lang = "zh"; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lang = ""; break;
|
|
|
|
}
|
2013-07-31 03:42:36 +00:00
|
|
|
|
2022-02-21 11:39:52 +00:00
|
|
|
font_desc = gtk_css_style_get_pango_font (gtk_style_context_lookup_style (gtk_widget_get_style_context (context_ime->client_widget)));
|
2018-02-23 19:59:49 +00:00
|
|
|
|
2004-10-01 19:53:55 +00:00
|
|
|
if (lang[0])
|
|
|
|
{
|
|
|
|
/* We know what language it is. Look for a character, any
|
|
|
|
* character, that language needs.
|
|
|
|
*/
|
|
|
|
PangoLanguage *pango_lang = pango_language_from_string (lang);
|
|
|
|
PangoFontset *fontset =
|
2013-07-31 03:42:36 +00:00
|
|
|
pango_context_load_fontset (pango_context,
|
|
|
|
font_desc,
|
|
|
|
pango_lang);
|
2004-10-01 19:53:55 +00:00
|
|
|
gunichar *sample =
|
|
|
|
g_utf8_to_ucs4 (pango_language_get_sample_string (pango_lang),
|
|
|
|
-1, NULL, NULL, NULL);
|
|
|
|
wc = 0x4E00; /* In all CJK languages? */
|
|
|
|
if (sample != NULL)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; sample[i]; i++)
|
|
|
|
if (g_unichar_iswide (sample[i]))
|
|
|
|
{
|
|
|
|
wc = sample[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
g_free (sample);
|
|
|
|
}
|
|
|
|
font = pango_fontset_get_font (fontset, wc);
|
|
|
|
g_object_unref (fontset);
|
|
|
|
}
|
|
|
|
else
|
2013-07-31 03:42:36 +00:00
|
|
|
font = pango_context_load_font (pango_context, font_desc);
|
2004-10-01 19:53:55 +00:00
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
if (!font)
|
|
|
|
goto ERROR_OUT;
|
|
|
|
|
2010-06-27 20:41:50 +00:00
|
|
|
logfont = pango_win32_font_logfont (font);
|
2004-08-26 14:29:25 +00:00
|
|
|
if (logfont)
|
2010-06-27 20:41:50 +00:00
|
|
|
ImmSetCompositionFont (himc, logfont);
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2004-10-01 19:53:55 +00:00
|
|
|
g_object_unref (font);
|
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
ERROR_OUT:
|
|
|
|
/* clean */
|
|
|
|
ImmReleaseContext (hwnd, himc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-24 16:39:13 +00:00
|
|
|
static GdkWin32MessageFilterReturn
|
|
|
|
gtk_im_context_ime_message_filter (GdkWin32Display *display,
|
|
|
|
MSG *msg,
|
2020-07-24 13:54:49 +00:00
|
|
|
int *ret_valp,
|
2018-03-24 16:39:13 +00:00
|
|
|
gpointer data)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
|
|
|
GtkIMContext *context;
|
|
|
|
GtkIMContextIME *context_ime;
|
|
|
|
HWND hwnd;
|
|
|
|
HIMC himc;
|
2019-02-23 05:31:10 +00:00
|
|
|
GdkSurface *toplevel;
|
2018-03-24 16:39:13 +00:00
|
|
|
GdkWin32MessageFilterReturn retval = GDK_WIN32_MESSAGE_FILTER_CONTINUE;
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_IM_CONTEXT_IME (data), retval);
|
|
|
|
|
|
|
|
context = GTK_IM_CONTEXT (data);
|
|
|
|
context_ime = GTK_IM_CONTEXT_IME (data);
|
|
|
|
if (!context_ime->focus)
|
|
|
|
return retval;
|
|
|
|
|
2019-05-19 03:09:05 +00:00
|
|
|
toplevel = context_ime->client_surface;
|
2022-08-23 12:31:07 +00:00
|
|
|
if (gdk_win32_surface_get_handle (toplevel) != msg->hwnd)
|
2018-03-24 16:39:13 +00:00
|
|
|
return retval;
|
|
|
|
|
2022-08-23 12:31:07 +00:00
|
|
|
hwnd = gdk_win32_surface_get_handle (context_ime->client_surface);
|
2004-08-26 14:29:25 +00:00
|
|
|
himc = ImmGetContext (hwnd);
|
|
|
|
if (!himc)
|
|
|
|
return retval;
|
|
|
|
|
2018-03-24 16:39:13 +00:00
|
|
|
*ret_valp = 0;
|
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
switch (msg->message)
|
|
|
|
{
|
|
|
|
case WM_IME_COMPOSITION:
|
|
|
|
{
|
|
|
|
CANDIDATEFORM cf;
|
2022-02-21 11:53:02 +00:00
|
|
|
int wx = 0;
|
|
|
|
int wy = 0;
|
|
|
|
int scale = 1;
|
|
|
|
|
|
|
|
if (context_ime->client_surface && context_ime->client_widget)
|
|
|
|
{
|
|
|
|
GtkNative *native = gtk_native_get_for_surface (context_ime->client_surface);
|
|
|
|
if G_LIKELY (native)
|
|
|
|
{
|
|
|
|
double x = 0.0;
|
|
|
|
double y = 0.0;
|
|
|
|
double decor_x = 0.0;
|
|
|
|
double decor_y = 0.0;
|
|
|
|
|
|
|
|
gtk_widget_translate_coordinates (context_ime->client_widget,
|
|
|
|
GTK_WIDGET (native),
|
|
|
|
0.0, 0.0, &x, &y);
|
|
|
|
|
|
|
|
gtk_native_get_surface_transform (native, &decor_x, &decor_y);
|
|
|
|
x += decor_x;
|
|
|
|
y += decor_y;
|
|
|
|
|
|
|
|
wx = (int) x;
|
|
|
|
wy = (int) y;
|
|
|
|
}
|
|
|
|
|
|
|
|
scale = gtk_widget_get_scale_factor (context_ime->client_widget);
|
|
|
|
}
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
cf.dwIndex = 0;
|
2022-02-21 11:53:02 +00:00
|
|
|
cf.dwStyle = CFS_EXCLUDE;
|
|
|
|
cf.ptCurrentPos.x = (wx + context_ime->cursor_location.x) * scale;
|
|
|
|
cf.ptCurrentPos.y = (wy + context_ime->cursor_location.y) * scale;
|
|
|
|
cf.rcArea.left = cf.ptCurrentPos.x;
|
|
|
|
cf.rcArea.right = cf.rcArea.left + context_ime->cursor_location.width * scale;
|
|
|
|
cf.rcArea.top = cf.ptCurrentPos.y;
|
|
|
|
cf.rcArea.bottom = cf.rcArea.top + context_ime->cursor_location.height * scale;
|
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
ImmSetCandidateWindow (himc, &cf);
|
|
|
|
|
|
|
|
if ((msg->lParam & GCS_COMPSTR))
|
2008-08-11 14:55:31 +00:00
|
|
|
g_signal_emit_by_name (context, "preedit-changed");
|
2004-08-26 14:29:25 +00:00
|
|
|
|
|
|
|
if (msg->lParam & GCS_RESULTSTR)
|
|
|
|
{
|
2020-09-07 11:04:47 +00:00
|
|
|
gchar *utf8str = get_utf8_preedit_string (context_ime, GCS_RESULTSTR, NULL);
|
2004-08-26 14:29:25 +00:00
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
if (utf8str)
|
2004-08-26 14:29:25 +00:00
|
|
|
{
|
2020-09-07 11:04:47 +00:00
|
|
|
context_ime->priv->pretend_empty_preedit = TRUE;
|
|
|
|
g_signal_emit_by_name (context, "preedit-changed");
|
|
|
|
g_signal_emit_by_name (context, "preedit-end");
|
|
|
|
|
|
|
|
g_signal_emit_by_name (context, "commit", utf8str);
|
|
|
|
|
|
|
|
g_signal_emit_by_name (context, "preedit-start");
|
|
|
|
g_signal_emit_by_name (context, "preedit-changed");
|
|
|
|
context_ime->priv->pretend_empty_preedit = FALSE;
|
|
|
|
|
|
|
|
retval = TRUE;
|
2018-10-09 09:06:39 +00:00
|
|
|
}
|
2020-09-07 11:04:47 +00:00
|
|
|
|
|
|
|
g_free (utf8str);
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (context_ime->use_preedit)
|
2018-03-24 16:39:13 +00:00
|
|
|
retval = GDK_WIN32_MESSAGE_FILTER_REMOVE;
|
2004-08-26 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 11:04:47 +00:00
|
|
|
break;
|
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
case WM_IME_STARTCOMPOSITION:
|
|
|
|
context_ime->preediting = TRUE;
|
|
|
|
gtk_im_context_ime_set_cursor_location (context, NULL);
|
2008-08-11 14:55:31 +00:00
|
|
|
g_signal_emit_by_name (context, "preedit-start");
|
2004-08-26 14:29:25 +00:00
|
|
|
if (context_ime->use_preedit)
|
2018-03-24 16:39:13 +00:00
|
|
|
retval = GDK_WIN32_MESSAGE_FILTER_REMOVE;
|
2004-08-26 14:29:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_IME_ENDCOMPOSITION:
|
|
|
|
context_ime->preediting = FALSE;
|
2008-08-11 14:55:31 +00:00
|
|
|
g_signal_emit_by_name (context, "preedit-changed");
|
|
|
|
g_signal_emit_by_name (context, "preedit-end");
|
input/IME: Defer the emit of the "commit" signal
On Windows, when IME is used, each keystroke results in the
WM_IME_COMPOSITION event being sent first. This means that in our case
when one decides on to accept the input that is in the preedit buffer,
we first get from Windows the WM_IME_COMPOSITION event
(where we emit the commit signal), followed by the WM_IME_ENDCOMPOSITION
event (where we emit the pair of preedit-changed and preedit-end
signals).
Since commit f11f989 (GtkEntry: Remove recompute idle), we do the input
recomputation directly, this will cause a pair of "Pango-WARNING:
Assertion failed: (index >= 0 && index <= layout->length)" being shown,
as gtkentry.c's priv->preedit_length and priv->preedit_cursor was unable
to be reset to 0 in time as a result of the recomputation triggered by
the commit being done before the reset of priv->preedit_length and
priv->preedit_cursor (which are no longer valid as we essentially say
that we are done with the preedit buffer).
As we could only acquire the final string that was entered in this
preedit session when we handle the WM_IME_COMPOSITION event, fix this by
saving up the final string we acquire from Windows IME in UTF-8 when we
handle the WM_IME_COMPOSITION event from Windows, and emit the commit
signal with that string after we emit the preedit-changed and
preedit-end signals when we handle the WM_IME_ENDCOMPOSITION event from
Windows, which comes afterwards.
Also fix the formatting of the code around the parts of the files that
was changed.
https://bugzilla.gnome.org/show_bug.cgi?id=787142
2017-08-31 10:43:07 +00:00
|
|
|
|
2004-08-26 14:29:25 +00:00
|
|
|
if (context_ime->use_preedit)
|
2018-03-24 16:39:13 +00:00
|
|
|
retval = GDK_WIN32_MESSAGE_FILTER_REMOVE;
|
2004-08-26 14:29:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_IME_NOTIFY:
|
|
|
|
switch (msg->wParam)
|
|
|
|
{
|
|
|
|
case IMN_SETOPENSTATUS:
|
|
|
|
context_ime->opened = ImmGetOpenStatus (himc);
|
2004-10-01 19:53:55 +00:00
|
|
|
gtk_im_context_ime_set_preedit_font (context);
|
2004-08-26 14:29:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-08-26 03:14:18 +00:00
|
|
|
break;
|
2004-08-26 14:29:25 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImmReleaseContext (hwnd, himc);
|
|
|
|
return retval;
|
|
|
|
}
|