| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "gtk/gtkimcontextquartz.h" |
| 25 |
#include "gtk/gtkimmoduleprivate.h" |
| 26 |
#include "gtk/gtkprivate.h" |
| 27 |
|
| 28 |
#include "gdk/macos/gdkmacos.h" |
| 29 |
#include "gdk/macos/gdkmacosdisplay-private.h" |
| 30 |
#include "gdk/macos/gdkmacossurface-private.h" |
| 31 |
#include "gdk/gdksurfaceprivate.h" |
| 32 |
|
| 33 |
#import "gdk/macos/GdkMacosBaseView.h" |
| 34 |
|
| 35 |
#define GTK_IM_CONTEXT_TYPE_QUARTZ (gtk_im_context_quartz_get_type()) |
| 36 |
#define GTK_IM_CONTEXT_QUARTZ(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_IM_CONTEXT_TYPE_QUARTZ, GtkIMContextQuartz)) |
| 37 |
#define GTK_IM_CONTEXT_QUARTZ_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GTK_IM_CONTEXT_TYPE_QUARTZ, GtkIMContextQuartzClass)) |
| 38 |
|
| 39 |
|
| 40 |
typedef struct _GtkIMContextQuartz |
| 41 |
{ |
| 42 |
GtkIMContext parent; |
| 43 |
GtkIMContext *helper; |
| 44 |
GdkSurface *client_surface; |
| 45 |
GtkWidget *client_widget; |
| 46 |
char *preedit_str; |
| 47 |
unsigned int cursor_index; |
| 48 |
unsigned int selected_len; |
| 49 |
GdkRectangle *cursor_rect; |
| 50 |
gboolean focused; |
| 51 |
} GtkIMContextQuartz; |
| 52 |
|
| 53 |
typedef struct _GtkIMContextQuartzClass |
| 54 |
{ |
| 55 |
GtkIMContextClass parent_class; |
| 56 |
} GtkIMContextQuartzClass; |
| 57 |
|
| 58 |
G_DEFINE_TYPE_WITH_CODE (GtkIMContextQuartz, gtk_im_context_quartz, GTK_TYPE_IM_CONTEXT, |
| 59 |
gtk_im_module_ensure_extension_point (); |
| 60 |
g_io_extension_point_implement (GTK_IM_MODULE_EXTENSION_POINT_NAME, |
| 61 |
g_define_type_id, |
| 62 |
"quartz", |
| 63 |
0)) |
| 64 |
|
| 65 |
static void |
| 66 |
quartz_get_preedit_string (GtkIMContext *context, |
| 67 |
char **str, |
| 68 |
PangoAttrList **attrs, |
| 69 |
int *cursor_pos) |
| 70 |
{ |
| 71 |
GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context); |
| 72 |
|
| 73 |
GTK_DEBUG (MODULES, "quartz_get_preedit_string\n"); |
| 74 |
|
| 75 |
if (str) |
| 76 |
*str = qc->preedit_str ? g_strdup (qc->preedit_str) : g_strdup (""); |
| 77 |
|
| 78 |
if (attrs) |
| 79 |
{ |
| 80 |
*attrs = pango_attr_list_new (); |
| 81 |
int len = g_utf8_strlen (*str, -1); |
| 82 |
char *ch = *str; |
| 83 |
if (len > 0) |
| 84 |
{ |
| 85 |
PangoAttribute *attr; |
| 86 |
int i = 0; |
| 87 |
for (;;) |
| 88 |
{ |
| 89 |
char *s = ch; |
| 90 |
ch = g_utf8_next_char (ch); |
| 91 |
|
| 92 |
if (i >= qc->cursor_index && |
| 93 |
i < qc->cursor_index + qc->selected_len) |
| 94 |
attr = pango_attr_underline_new (PANGO_UNDERLINE_DOUBLE); |
| 95 |
else |
| 96 |
attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE); |
| 97 |
|
| 98 |
attr->start_index = s - *str; |
| 99 |
if (!*ch) |
| 100 |
attr->end_index = attr->start_index + strlen (s); |
| 101 |
else |
| 102 |
attr->end_index = ch - *str; |
| 103 |
|
| 104 |
pango_attr_list_change (*attrs, attr); |
| 105 |
|
| 106 |
if (!*ch) |
| 107 |
break; |
| 108 |
i++; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
if (cursor_pos) |
| 113 |
*cursor_pos = qc->cursor_index; |
| 114 |
} |
| 115 |
|
| 116 |
static gboolean |
| 117 |
output_result (GtkIMContext *context, |
| 118 |
GdkSurface *surface) |
| 119 |
{ |
| 120 |
GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context); |
| 121 |
gboolean retval = FALSE; |
| 122 |
char *fixed_str, *marked_str; |
| 123 |
|
| 124 |
fixed_str = g_strdup (g_object_get_data (G_OBJECT (surface), TIC_INSERT_TEXT)); |
| 125 |
marked_str = g_strdup (g_object_get_data (G_OBJECT (surface), TIC_MARKED_TEXT)); |
| 126 |
if (fixed_str) |
| 127 |
{ |
| 128 |
GTK_DEBUG (MODULES, "tic-insert-text: %s", fixed_str); |
| 129 |
g_free (qc->preedit_str); |
| 130 |
qc->preedit_str = NULL; |
| 131 |
g_object_set_data (G_OBJECT (surface), TIC_INSERT_TEXT, NULL); |
| 132 |
g_signal_emit_by_name (context, "commit", fixed_str); |
| 133 |
g_signal_emit_by_name (context, "preedit_changed"); |
| 134 |
|
| 135 |
unsigned int filtered = |
| 136 |
GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (surface), |
| 137 |
GIC_FILTER_KEY)); |
| 138 |
GTK_DEBUG (MODULES, "filtered, %d", filtered); |
| 139 |
if (filtered) |
| 140 |
retval = TRUE; |
| 141 |
else |
| 142 |
retval = FALSE; |
| 143 |
} |
| 144 |
if (marked_str) |
| 145 |
{ |
| 146 |
GTK_DEBUG (MODULES, "tic-marked-text: %s", marked_str); |
| 147 |
qc->cursor_index = |
| 148 |
GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (surface), |
| 149 |
TIC_SELECTED_POS)); |
| 150 |
qc->selected_len = |
| 151 |
GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (surface), |
| 152 |
TIC_SELECTED_LEN)); |
| 153 |
g_free (qc->preedit_str); |
| 154 |
qc->preedit_str = g_strdup (marked_str); |
| 155 |
g_object_set_data (G_OBJECT (surface), TIC_MARKED_TEXT, NULL); |
| 156 |
g_signal_emit_by_name (context, "preedit_changed"); |
| 157 |
retval = TRUE; |
| 158 |
} |
| 159 |
if (!fixed_str && !marked_str) |
| 160 |
{ |
| 161 |
if (qc->preedit_str && strlen (qc->preedit_str) > 0) |
| 162 |
retval = TRUE; |
| 163 |
} |
| 164 |
g_free (fixed_str); |
| 165 |
g_free (marked_str); |
| 166 |
return retval; |
| 167 |
} |
| 168 |
|
| 169 |
static gboolean |
| 170 |
quartz_filter_keypress (GtkIMContext *context, |
| 171 |
GdkEvent *event) |
| 172 |
{ |
| 173 |
GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context); |
| 174 |
GdkEventType event_type; |
| 175 |
gboolean retval; |
| 176 |
guint keyval; |
| 177 |
guint keycode; |
| 178 |
|
| 179 |
GTK_DEBUG (MODULES, "quartz_filter_keypress"); |
| 180 |
|
| 181 |
if (!GDK_IS_MACOS_SURFACE (qc->client_surface)) |
| 182 |
return FALSE; |
| 183 |
|
| 184 |
event_type = gdk_event_get_event_type (event); |
| 185 |
keyval = gdk_key_event_get_keyval (event); |
| 186 |
keycode = gdk_key_event_get_keycode (event); |
| 187 |
|
| 188 |
NSEvent *nsevent = _gdk_macos_display_get_matching_nsevent ((GdkEvent *)event); |
| 189 |
|
| 190 |
if (!nsevent) |
| 191 |
{ |
| 192 |
if (keycode == 0 && keyval == 0xffffff) |
| 193 |
|
| 194 |
return output_result (context, qc->client_surface); |
| 195 |
else |
| 196 |
return gtk_im_context_filter_keypress (qc->helper, event); |
| 197 |
} |
| 198 |
|
| 199 |
if (event_type == GDK_KEY_RELEASE) |
| 200 |
return FALSE; |
| 201 |
|
| 202 |
if (keycode == 55) |
| 203 |
return FALSE; |
| 204 |
|
| 205 |
NSEventType etype = [nsevent type]; |
| 206 |
if (etype == NSEventTypeKeyDown) |
| 207 |
{ |
| 208 |
NSView *nsview = _gdk_macos_surface_get_view (GDK_MACOS_SURFACE (qc->client_surface)); |
| 209 |
g_object_set_data (G_OBJECT (qc->client_surface), |
| 210 |
TIC_IN_KEY_DOWN, |
| 211 |
GUINT_TO_POINTER (TRUE)); |
| 212 |
[nsview keyDown: nsevent]; |
| 213 |
} |
| 214 |
|
| 215 |
if (keycode == 102 || keycode == 104) |
| 216 |
return FALSE; |
| 217 |
|
| 218 |
retval = output_result(context, qc->client_surface); |
| 219 |
g_object_set_data (G_OBJECT (qc->client_surface), |
| 220 |
TIC_IN_KEY_DOWN, |
| 221 |
GUINT_TO_POINTER (FALSE)); |
| 222 |
GTK_DEBUG (MODULES, "quartz_filter_keypress done"); |
| 223 |
|
| 224 |
return retval; |
| 225 |
} |
| 226 |
|
| 227 |
static void |
| 228 |
discard_preedit (GtkIMContext *context) |
| 229 |
{ |
| 230 |
GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context); |
| 231 |
|
| 232 |
if (!qc->client_surface) |
| 233 |
return; |
| 234 |
|
| 235 |
if (!GDK_IS_MACOS_SURFACE (qc->client_surface)) |
| 236 |
return; |
| 237 |
|
| 238 |
NSView *nsview = _gdk_macos_surface_get_view (GDK_MACOS_SURFACE (qc->client_surface)); |
| 239 |
if (!nsview) |
| 240 |
return; |
| 241 |
|
| 242 |
|
| 243 |
[(GdkMacosBaseView *)nsview unmarkText]; |
| 244 |
[[NSTextInputContext currentInputContext] discardMarkedText]; |
| 245 |
|
| 246 |
if (qc->preedit_str && strlen (qc->preedit_str) > 0) |
| 247 |
{ |
| 248 |
g_signal_emit_by_name (context, "commit", qc->preedit_str); |
| 249 |
|
| 250 |
g_free (qc->preedit_str); |
| 251 |
qc->preedit_str = NULL; |
| 252 |
g_signal_emit_by_name (context, "preedit_changed"); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
static void |
| 257 |
quartz_reset (GtkIMContext *context) |
| 258 |
{ |
| 259 |
GTK_DEBUG (MODULES, "quartz_reset"); |
| 260 |
discard_preedit (context); |
| 261 |
} |
| 262 |
|
| 263 |
static void |
| 264 |
quartz_set_client_surface (GtkIMContext *context, |
| 265 |
GtkWidget *widget) |
| 266 |
{ |
| 267 |
GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context); |
| 268 |
|
| 269 |
GTK_DEBUG (MODULES, "quartz_set_client_surface: %p", widget); |
| 270 |
|
| 271 |
qc->client_widget = widget; |
| 272 |
qc->client_surface = NULL; |
| 273 |
|
| 274 |
if (widget != NULL) |
| 275 |
{ |
| 276 |
GtkNative *native = GTK_NATIVE (gtk_widget_get_root (widget)); |
| 277 |
|
| 278 |
if (native != NULL) |
| 279 |
qc->client_surface = gtk_native_get_surface (native); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
static void |
| 284 |
quartz_focus_in (GtkIMContext *context) |
| 285 |
{ |
| 286 |
GTK_DEBUG (MODULES, "quartz_focus_in"); |
| 287 |
|
| 288 |
GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context); |
| 289 |
qc->focused = TRUE; |
| 290 |
} |
| 291 |
|
| 292 |
static void |
| 293 |
quartz_focus_out (GtkIMContext *context) |
| 294 |
{ |
| 295 |
GTK_DEBUG (MODULES, "quartz_focus_out"); |
| 296 |
|
| 297 |
GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context); |
| 298 |
qc->focused = FALSE; |
| 299 |
|
| 300 |
|
| 301 |
discard_preedit (context); |
| 302 |
} |
| 303 |
|
| 304 |
static void |
| 305 |
quartz_set_cursor_location (GtkIMContext *context, GdkRectangle *area) |
| 306 |
{ |
| 307 |
GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context); |
| 308 |
GtkWidget* surface_widget; |
| 309 |
int sx, sy; |
| 310 |
graphene_point_t p; |
| 311 |
|
| 312 |
GTK_DEBUG (MODULES, "quartz_set_cursor_location"); |
| 313 |
|
| 314 |
if (!qc->client_surface || !qc->client_widget) |
| 315 |
return; |
| 316 |
|
| 317 |
if (!qc->focused) |
| 318 |
return; |
| 319 |
|
| 320 |
surface_widget = GTK_WIDGET (gtk_widget_get_native (qc->client_widget)); |
| 321 |
|
| 322 |
if (!surface_widget) |
| 323 |
return; |
| 324 |
|
| 325 |
gdk_surface_get_origin (qc->client_surface, &sx, &sy); |
| 326 |
if (!gtk_widget_compute_point (qc->client_widget, surface_widget, |
| 327 |
&GRAPHENE_POINT_INIT (area->x, area->y), &p)) |
| 328 |
graphene_point_init (&p, area->x, area->y); |
| 329 |
|
| 330 |
qc->cursor_rect->x = sx + (int) p.x; |
| 331 |
qc->cursor_rect->y = sy + (int) p.y; |
| 332 |
qc->cursor_rect->width = area->width; |
| 333 |
qc->cursor_rect->height = area->height; |
| 334 |
|
| 335 |
if (!GDK_IS_MACOS_SURFACE (qc->client_surface)) |
| 336 |
return; |
| 337 |
|
| 338 |
g_object_set_data (G_OBJECT (qc->client_surface), GIC_CURSOR_RECT, qc->cursor_rect); |
| 339 |
} |
| 340 |
|
| 341 |
static void |
| 342 |
quartz_set_use_preedit (GtkIMContext *context, gboolean use_preedit) |
| 343 |
{ |
| 344 |
GTK_DEBUG (MODULES, "quartz_set_use_preedit: %d", use_preedit); |
| 345 |
} |
| 346 |
|
| 347 |
static void |
| 348 |
commit_cb (GtkIMContext *context, const char *str, GtkIMContextQuartz *qc) |
| 349 |
{ |
| 350 |
g_signal_emit_by_name (qc, "commit", str); |
| 351 |
} |
| 352 |
|
| 353 |
static void |
| 354 |
imquartz_finalize (GObject *obj) |
| 355 |
{ |
| 356 |
GTK_DEBUG (MODULES, "imquartz_finalize"); |
| 357 |
|
| 358 |
GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (obj); |
| 359 |
g_free (qc->preedit_str); |
| 360 |
qc->preedit_str = NULL; |
| 361 |
g_free (qc->cursor_rect); |
| 362 |
qc->cursor_rect = NULL; |
| 363 |
|
| 364 |
g_signal_handlers_disconnect_by_func (qc->helper, (gpointer)commit_cb, qc); |
| 365 |
g_object_unref (qc->helper); |
| 366 |
|
| 367 |
G_OBJECT_CLASS (gtk_im_context_quartz_parent_class)->finalize (obj); |
| 368 |
} |
| 369 |
|
| 370 |
static void |
| 371 |
gtk_im_context_quartz_class_init (GtkIMContextQuartzClass *class) |
| 372 |
{ |
| 373 |
GTK_DEBUG (MODULES, "gtk_im_context_quartz_class_init"); |
| 374 |
|
| 375 |
GtkIMContextClass *klass = GTK_IM_CONTEXT_CLASS (class); |
| 376 |
GObjectClass *object_class = G_OBJECT_CLASS (class); |
| 377 |
|
| 378 |
klass->get_preedit_string = quartz_get_preedit_string; |
| 379 |
klass->filter_keypress = quartz_filter_keypress; |
| 380 |
klass->reset = quartz_reset; |
| 381 |
klass->set_client_widget = quartz_set_client_surface; |
| 382 |
klass->focus_in = quartz_focus_in; |
| 383 |
klass->focus_out = quartz_focus_out; |
| 384 |
klass->set_cursor_location = quartz_set_cursor_location; |
| 385 |
klass->set_use_preedit = quartz_set_use_preedit; |
| 386 |
|
| 387 |
object_class->finalize = imquartz_finalize; |
| 388 |
} |
| 389 |
|
| 390 |
static void |
| 391 |
gtk_im_context_quartz_init (GtkIMContextQuartz *qc) |
| 392 |
{ |
| 393 |
GTK_DEBUG (MODULES, "gtk_im_context_quartz_init"); |
| 394 |
|
| 395 |
qc->preedit_str = g_strdup (""); |
| 396 |
qc->cursor_index = 0; |
| 397 |
qc->selected_len = 0; |
| 398 |
qc->cursor_rect = g_malloc (sizeof (GdkRectangle)); |
| 399 |
qc->focused = FALSE; |
| 400 |
|
| 401 |
qc->helper = g_object_new (GTK_TYPE_IM_CONTEXT_SIMPLE, NULL); |
| 402 |
g_signal_connect (G_OBJECT (qc->helper), "commit", G_CALLBACK (commit_cb), qc); |
| 403 |
} |