1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
| | /* Pure Gtk+-3 communication module. -*- coding: utf-8 -*-
Copyright (C) 1989, 1993-1994, 2005-2006, 2008-2018 Free Software
Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
/* This should be the first include, as it may set up #defines affecting
interpretation of even the system includes. */
#include <config.h>
#include <stdio.h>
#include "pgtkterm.h"
#include "keyboard.h"
#include "gtkconfig.h"
#include "pgtkim.h"
void
im_context_commit_cb (GtkIMContext *imc, gchar *str, gpointer user_data)
{
struct pgtk_display_info *dpyinfo = user_data;
struct frame *f = dpyinfo->im.focused_frame;
if (dpyinfo->im.context == NULL)
return;
if (f == NULL)
return;
pgtk_enqueue_string (f, str);
}
gboolean
im_context_retrieve_surrounding_cb (GtkIMContext *imc, gpointer user_data)
{
gtk_im_context_set_surrounding (imc, "", -1, 0);
return TRUE;
}
gboolean
im_context_delete_surrounding_cb (GtkIMContext *imc, int offset, int n_chars,
gpointer user_data)
{
return TRUE;
}
static Lisp_Object
make_color_string (PangoAttrColor *pac)
{
char buf[256];
sprintf (buf, "#%02x%02x%02x", pac->color.red >> 8, pac->color.green >> 8,
pac->color.blue >> 8);
return build_string (buf);
}
void
im_context_preedit_changed_cb (GtkIMContext *imc, gpointer user_data)
{
struct pgtk_display_info *dpyinfo = user_data;
struct frame *f = dpyinfo->im.focused_frame;
char *str;
PangoAttrList *attrs;
int pos;
if (dpyinfo->im.context == NULL)
return;
if (f == NULL)
return;
gtk_im_context_get_preedit_string (imc, &str, &attrs, &pos);
/*
* (
* (TEXT (ul . COLOR) (bg . COLOR) (fg . COLOR))
* ...
* )
*/
Lisp_Object list = Qnil;
PangoAttrIterator *iter;
iter = pango_attr_list_get_iterator (attrs);
do
{
int st, ed;
int has_underline = 0;
Lisp_Object part = Qnil;
pango_attr_iterator_range (iter, &st, &ed);
if (ed > strlen (str))
ed = strlen (str);
if (st >= ed)
continue;
Lisp_Object text = make_string (str + st, ed - st);
part = Fcons (text, part);
PangoAttrInt *ul
= (PangoAttrInt *) pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
if (ul != NULL)
{
if (ul->value != PANGO_UNDERLINE_NONE)
has_underline = 1;
}
PangoAttrColor *pac;
if (has_underline)
{
pac = (PangoAttrColor *)
pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE_COLOR);
if (pac != NULL)
part = Fcons (Fcons (Qul, make_color_string (pac)), part);
else
part = Fcons (Fcons (Qul, Qt), part);
}
pac = (PangoAttrColor *) pango_attr_iterator_get (iter,
PANGO_ATTR_FOREGROUND);
if (pac != NULL)
part = Fcons (Fcons (Qfg, make_color_string (pac)), part);
pac = (PangoAttrColor *) pango_attr_iterator_get (iter,
PANGO_ATTR_BACKGROUND);
if (pac != NULL)
part = Fcons (Fcons (Qbg, make_color_string (pac)), part);
part = Fnreverse (part);
list = Fcons (part, list);
}
while (pango_attr_iterator_next (iter));
list = Fnreverse (list);
pgtk_enqueue_preedit (f, list);
g_free (str);
pango_attr_list_unref (attrs);
}
void
im_context_preedit_end_cb (GtkIMContext *imc, gpointer user_data)
{
struct pgtk_display_info *dpyinfo = user_data;
struct frame *f = dpyinfo->im.focused_frame;
if (dpyinfo->im.context == NULL)
return;
if (f == NULL)
return;
pgtk_enqueue_preedit (f, Qnil);
}
void
im_context_preedit_start_cb (GtkIMContext *imc, gpointer user_data)
{
struct pgtk_display_info *dpyinfo = user_data;
struct frame *f = dpyinfo->im.focused_frame;
if (dpyinfo->im.context != NULL)
{
gdouble x, y;
x = XWINDOW (f->selected_window)->pixel_left
+ XWINDOW (f->selected_window)->phys_cursor.x
+ FRAME_COLUMN_WIDTH (f)
+ FRAME_LEFT_FRINGE_WIDTH (f)
+ WINDOW_LEFT_MARGIN_WIDTH (XWINDOW (f->selected_window));
y = XWINDOW (f->selected_window)->pixel_top
+ XWINDOW (f->selected_window)->phys_cursor.y;
GdkRectangle rect;
rect.x = (int) x;
rect.y = (int) y;
rect.width = FRAME_CURSOR_WIDTH (f);
rect.height = FRAME_LINE_HEIGHT (f);
gtk_im_context_set_cursor_location (dpyinfo->im.context, &rect);
}
}
void
pgtk_im_focus_in (struct frame *f)
{
struct pgtk_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
if (dpyinfo->im.context != NULL)
{
gtk_im_context_reset (dpyinfo->im.context);
#ifndef HAVE_GTK4
gtk_im_context_set_client_window
#else
gtk_im_context_set_client_widget
#endif
(dpyinfo->im.context,
#ifndef HAVE_GTK4
gtk_widget_get_window (FRAME_GTK_WIDGET (f))
#else
FRAME_GTK_WIDGET (f)
#endif
);
gtk_im_context_focus_in (dpyinfo->im.context);
gdouble x, y;
x = XWINDOW (f->selected_window)->pixel_left
+ XWINDOW (f->selected_window)->phys_cursor.x
+ FRAME_LEFT_FRINGE_WIDTH (f)
+ WINDOW_LEFT_MARGIN_WIDTH (XWINDOW (f->selected_window));
y = XWINDOW (f->selected_window)->pixel_top
+ XWINDOW (f->selected_window)->phys_cursor.y;
GdkRectangle rect;
rect.x = (int) x;
rect.y = (int) y;
rect.width = FRAME_CURSOR_WIDTH (f);
rect.height = FRAME_LINE_HEIGHT (f);
gtk_im_context_set_cursor_location (dpyinfo->im.context, &rect);
}
dpyinfo->im.focused_frame = f;
}
void
pgtk_im_focus_out (struct frame *f)
{
struct pgtk_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
if (dpyinfo->im.focused_frame == f)
{
if (dpyinfo->im.context != NULL)
{
gtk_im_context_reset (dpyinfo->im.context);
gtk_im_context_focus_out (dpyinfo->im.context);
#ifndef HAVE_GTK4
gtk_im_context_set_client_window (dpyinfo->im.context, NULL);
#else
gtk_im_context_set_client_widget (dpyinfo->im.context, NULL);
#endif
}
dpyinfo->im.focused_frame = NULL;
}
}
#ifndef HAVE_GTK4
bool
pgtk_im_filter_keypress (struct frame *f, GdkEventKey *ev)
#else
bool
pgtk_im_filter_keypress (struct frame *f, GdkEvent *ev)
#endif
{
struct pgtk_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
if (dpyinfo->im.context != NULL)
{
gdouble x, y;
x = XWINDOW (f->selected_window)->pixel_left
+ XWINDOW (f->selected_window)->phys_cursor.x
+ FRAME_LEFT_FRINGE_WIDTH (f)
+ WINDOW_LEFT_MARGIN_WIDTH (XWINDOW (f->selected_window));
y = XWINDOW (f->selected_window)->pixel_top
+ XWINDOW (f->selected_window)->phys_cursor.y;
GdkRectangle rect;
rect.x = (int) x;
rect.y = (int) y;
rect.width = FRAME_CURSOR_WIDTH (f);
rect.height = FRAME_LINE_HEIGHT (f);
gtk_im_context_set_cursor_location (dpyinfo->im.context, &rect);
if (gtk_im_context_filter_keypress (dpyinfo->im.context, ev))
return true;
}
return false;
}
void
pgtk_im_init (struct pgtk_display_info *dpyinfo)
{
dpyinfo->im.context = NULL;
}
void
pgtk_im_finish (struct pgtk_display_info *dpyinfo)
{
if (dpyinfo->im.context != NULL)
g_object_unref (dpyinfo->im.context);
dpyinfo->im.context = NULL;
}
DEFUN ("pgtk-use-im-context", Fpgtk_use_im_context, Spgtk_use_im_context, 1, 2,
0, doc: /* Set whether GTK input methods will be used. */)
(Lisp_Object use_p, Lisp_Object terminal)
{
struct pgtk_display_info *dpyinfo = check_pgtk_display_info (terminal);
if (NILP (use_p))
{
if (dpyinfo->im.context != NULL)
{
gtk_im_context_reset (dpyinfo->im.context);
gtk_im_context_focus_out (dpyinfo->im.context);
#ifndef HAVE_GTK4
gtk_im_context_set_client_window (dpyinfo->im.context, NULL);
#else
gtk_im_context_set_client_widget (dpyinfo->im.context, NULL);
#endif
}
}
else
{
if (dpyinfo->im.context == NULL)
{
dpyinfo->im.context = gtk_im_multicontext_new ();
g_signal_connect (dpyinfo->im.context, "commit",
G_CALLBACK (im_context_commit_cb), dpyinfo);
g_signal_connect (dpyinfo->im.context, "retrieve-surrounding",
G_CALLBACK (im_context_retrieve_surrounding_cb),
dpyinfo);
g_signal_connect (dpyinfo->im.context, "delete-surrounding",
G_CALLBACK (im_context_delete_surrounding_cb),
dpyinfo);
g_signal_connect (dpyinfo->im.context, "preedit-changed",
G_CALLBACK (im_context_preedit_changed_cb),
dpyinfo);
g_signal_connect (dpyinfo->im.context, "preedit-end",
G_CALLBACK (im_context_preedit_end_cb), dpyinfo);
g_signal_connect (dpyinfo->im.context, "preedit-start",
G_CALLBACK (im_context_preedit_start_cb), dpyinfo);
gtk_im_context_set_use_preedit (dpyinfo->im.context, TRUE);
}
if (dpyinfo->im.focused_frame)
pgtk_im_focus_in (dpyinfo->im.focused_frame);
}
return Qnil;
}
void
syms_of_pgtkim (void)
{
defsubr (&Spgtk_use_im_context);
DEFSYM (Qpgtk_refresh_preedit, "pgtk-refresh-preedit");
DEFSYM (Qul, "ul");
DEFSYM (Qfg, "fg");
DEFSYM (Qbg, "bg");
}
|