Bram Moolenaar | f15c8b6 | 2020-06-01 14:34:43 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * gui_xim.c: functions for the X Input Method |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | #if defined(FEAT_GUI_GTK) && defined(FEAT_XIM) |
| 17 | # if GTK_CHECK_VERSION(3,0,0) |
| 18 | # include <gdk/gdkkeysyms-compat.h> |
| 19 | # else |
| 20 | # include <gdk/gdkkeysyms.h> |
| 21 | # endif |
| 22 | # ifdef MSWIN |
| 23 | # include <gdk/gdkwin32.h> |
| 24 | # else |
| 25 | # include <gdk/gdkx.h> |
| 26 | # endif |
| 27 | #endif |
| 28 | |
| 29 | /* |
| 30 | * XIM often causes trouble. Define XIM_DEBUG to get a log of XIM callbacks |
| 31 | * in the "xim.log" file. |
| 32 | */ |
| 33 | // #define XIM_DEBUG |
| 34 | #ifdef XIM_DEBUG |
| 35 | static void |
| 36 | xim_log(char *s, ...) |
| 37 | { |
| 38 | va_list arglist; |
| 39 | static FILE *fd = NULL; |
| 40 | |
| 41 | if (fd == (FILE *)-1) |
| 42 | return; |
| 43 | if (fd == NULL) |
| 44 | { |
| 45 | fd = mch_fopen("xim.log", "w"); |
| 46 | if (fd == NULL) |
| 47 | { |
| 48 | emsg("Cannot open xim.log"); |
| 49 | fd = (FILE *)-1; |
| 50 | return; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | va_start(arglist, s); |
| 55 | vfprintf(fd, s, arglist); |
| 56 | va_end(arglist); |
| 57 | } |
| 58 | #endif |
| 59 | |
| 60 | #ifdef FEAT_GUI |
| 61 | # define USE_IMACTIVATEFUNC (!gui.in_use && *p_imaf != NUL) |
| 62 | # define USE_IMSTATUSFUNC (!gui.in_use && *p_imsf != NUL) |
| 63 | #else |
| 64 | # define USE_IMACTIVATEFUNC (*p_imaf != NUL) |
| 65 | # define USE_IMSTATUSFUNC (*p_imsf != NUL) |
| 66 | #endif |
| 67 | |
| 68 | #if defined(FEAT_EVAL) && \ |
| 69 | (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL)) |
| 70 | static void |
| 71 | call_imactivatefunc(int active) |
| 72 | { |
| 73 | typval_T argv[2]; |
| 74 | |
| 75 | argv[0].v_type = VAR_NUMBER; |
| 76 | argv[0].vval.v_number = active ? 1 : 0; |
| 77 | argv[1].v_type = VAR_UNKNOWN; |
| 78 | (void)call_func_retnr(p_imaf, 1, argv); |
| 79 | } |
| 80 | |
| 81 | static int |
| 82 | call_imstatusfunc(void) |
| 83 | { |
| 84 | int is_active; |
| 85 | |
| 86 | // FIXME: Don't execute user function in unsafe situation. |
| 87 | if (exiting || is_autocmd_blocked()) |
| 88 | return FALSE; |
| 89 | // FIXME: :py print 'xxx' is shown duplicate result. |
| 90 | // Use silent to avoid it. |
| 91 | ++msg_silent; |
| 92 | is_active = call_func_retnr(p_imsf, 0, NULL); |
| 93 | --msg_silent; |
| 94 | return (is_active > 0); |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | #if defined(FEAT_XIM) || defined(PROTO) |
| 99 | |
| 100 | # if defined(FEAT_GUI_GTK) || defined(PROTO) |
| 101 | static int xim_has_preediting INIT(= FALSE); // IM current status |
| 102 | |
| 103 | /* |
| 104 | * Set preedit_start_col to the current cursor position. |
| 105 | */ |
| 106 | static void |
| 107 | init_preedit_start_col(void) |
| 108 | { |
| 109 | if (State & CMDLINE) |
| 110 | preedit_start_col = cmdline_getvcol_cursor(); |
| 111 | else if (curwin != NULL && curwin->w_buffer != NULL) |
| 112 | getvcol(curwin, &curwin->w_cursor, &preedit_start_col, NULL, NULL); |
| 113 | // Prevent that preediting marks the buffer as changed. |
| 114 | xim_changed_while_preediting = curbuf->b_changed; |
| 115 | } |
| 116 | |
| 117 | static int im_is_active = FALSE; // IM is enabled for current mode |
| 118 | static int preedit_is_active = FALSE; |
| 119 | static int im_preedit_cursor = 0; // cursor offset in characters |
| 120 | static int im_preedit_trailing = 0; // number of characters after cursor |
| 121 | |
| 122 | static unsigned long im_commit_handler_id = 0; |
| 123 | static unsigned int im_activatekey_keyval = GDK_VoidSymbol; |
| 124 | static unsigned int im_activatekey_state = 0; |
| 125 | |
| 126 | static GtkWidget *preedit_window = NULL; |
| 127 | static GtkWidget *preedit_label = NULL; |
| 128 | |
| 129 | static void im_preedit_window_set_position(void); |
| 130 | |
| 131 | void |
| 132 | im_set_active(int active) |
| 133 | { |
| 134 | int was_active; |
| 135 | |
| 136 | was_active = !!im_get_status(); |
| 137 | im_is_active = (active && !p_imdisable); |
| 138 | |
| 139 | if (im_is_active != was_active) |
| 140 | xim_reset(); |
| 141 | } |
| 142 | |
| 143 | void |
| 144 | xim_set_focus(int focus) |
| 145 | { |
| 146 | if (xic != NULL) |
| 147 | { |
| 148 | if (focus) |
| 149 | gtk_im_context_focus_in(xic); |
| 150 | else |
| 151 | gtk_im_context_focus_out(xic); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | im_set_position(int row, int col) |
| 157 | { |
| 158 | if (xic != NULL) |
| 159 | { |
| 160 | GdkRectangle area; |
| 161 | |
| 162 | area.x = FILL_X(col); |
| 163 | area.y = FILL_Y(row); |
| 164 | area.width = gui.char_width * (mb_lefthalve(row, col) ? 2 : 1); |
| 165 | area.height = gui.char_height; |
| 166 | |
| 167 | gtk_im_context_set_cursor_location(xic, &area); |
| 168 | |
| 169 | if (p_imst == IM_OVER_THE_SPOT) |
| 170 | im_preedit_window_set_position(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | # if 0 || defined(PROTO) // apparently only used in gui_x11.c |
| 175 | void |
| 176 | xim_set_preedit(void) |
| 177 | { |
| 178 | im_set_position(gui.row, gui.col); |
| 179 | } |
| 180 | # endif |
| 181 | |
| 182 | static void |
| 183 | im_add_to_input(char_u *str, int len) |
| 184 | { |
| 185 | // Convert from 'termencoding' (always "utf-8") to 'encoding' |
| 186 | if (input_conv.vc_type != CONV_NONE) |
| 187 | { |
| 188 | str = string_convert(&input_conv, str, &len); |
| 189 | g_return_if_fail(str != NULL); |
| 190 | } |
| 191 | |
| 192 | add_to_input_buf_csi(str, len); |
| 193 | |
| 194 | if (input_conv.vc_type != CONV_NONE) |
| 195 | vim_free(str); |
| 196 | |
| 197 | if (p_mh) // blank out the pointer if necessary |
| 198 | gui_mch_mousehide(TRUE); |
| 199 | } |
| 200 | |
| 201 | static void |
| 202 | im_preedit_window_set_position(void) |
| 203 | { |
| 204 | int x, y, width, height; |
| 205 | int screen_x, screen_y, screen_width, screen_height; |
| 206 | |
| 207 | if (preedit_window == NULL) |
| 208 | return; |
| 209 | |
| 210 | gui_gtk_get_screen_geom_of_win(gui.drawarea, |
| 211 | &screen_x, &screen_y, &screen_width, &screen_height); |
| 212 | gdk_window_get_origin(gtk_widget_get_window(gui.drawarea), &x, &y); |
| 213 | gtk_window_get_size(GTK_WINDOW(preedit_window), &width, &height); |
| 214 | x = x + FILL_X(gui.col); |
| 215 | y = y + FILL_Y(gui.row); |
| 216 | if (x + width > screen_x + screen_width) |
| 217 | x = screen_x + screen_width - width; |
| 218 | if (y + height > screen_y + screen_height) |
| 219 | y = screen_y + screen_height - height; |
| 220 | gtk_window_move(GTK_WINDOW(preedit_window), x, y); |
| 221 | } |
| 222 | |
| 223 | static void |
| 224 | im_preedit_window_open() |
| 225 | { |
| 226 | char *preedit_string; |
| 227 | #if !GTK_CHECK_VERSION(3,16,0) |
| 228 | char buf[8]; |
| 229 | #endif |
| 230 | PangoAttrList *attr_list; |
| 231 | PangoLayout *layout; |
| 232 | #if GTK_CHECK_VERSION(3,0,0) |
| 233 | # if !GTK_CHECK_VERSION(3,16,0) |
| 234 | GdkRGBA color; |
| 235 | # endif |
| 236 | #else |
| 237 | GdkColor color; |
| 238 | #endif |
| 239 | gint w, h; |
| 240 | |
| 241 | if (preedit_window == NULL) |
| 242 | { |
| 243 | preedit_window = gtk_window_new(GTK_WINDOW_POPUP); |
| 244 | gtk_window_set_transient_for(GTK_WINDOW(preedit_window), |
| 245 | GTK_WINDOW(gui.mainwin)); |
| 246 | preedit_label = gtk_label_new(""); |
| 247 | gtk_widget_set_name(preedit_label, "vim-gui-preedit-area"); |
| 248 | gtk_container_add(GTK_CONTAINER(preedit_window), preedit_label); |
| 249 | } |
| 250 | |
| 251 | #if GTK_CHECK_VERSION(3,16,0) |
| 252 | { |
| 253 | GtkStyleContext * const context |
| 254 | = gtk_widget_get_style_context(gui.drawarea); |
| 255 | GtkCssProvider * const provider = gtk_css_provider_new(); |
| 256 | gchar *css = NULL; |
| 257 | const char * const fontname |
| 258 | = pango_font_description_get_family(gui.norm_font); |
| 259 | gint fontsize |
| 260 | = pango_font_description_get_size(gui.norm_font) / PANGO_SCALE; |
| 261 | gchar *fontsize_propval = NULL; |
| 262 | |
| 263 | if (!pango_font_description_get_size_is_absolute(gui.norm_font)) |
| 264 | { |
| 265 | // fontsize was given in points. Convert it into that in pixels |
| 266 | // to use with CSS. |
| 267 | GdkScreen * const screen |
| 268 | = gdk_window_get_screen(gtk_widget_get_window(gui.mainwin)); |
| 269 | const gdouble dpi = gdk_screen_get_resolution(screen); |
| 270 | fontsize = dpi * fontsize / 72; |
| 271 | } |
| 272 | if (fontsize > 0) |
| 273 | fontsize_propval = g_strdup_printf("%dpx", fontsize); |
| 274 | else |
| 275 | fontsize_propval = g_strdup_printf("inherit"); |
| 276 | |
| 277 | css = g_strdup_printf( |
| 278 | "widget#vim-gui-preedit-area {\n" |
| 279 | " font-family: %s,monospace;\n" |
| 280 | " font-size: %s;\n" |
| 281 | " color: #%.2lx%.2lx%.2lx;\n" |
| 282 | " background-color: #%.2lx%.2lx%.2lx;\n" |
| 283 | "}\n", |
| 284 | fontname != NULL ? fontname : "inherit", |
| 285 | fontsize_propval, |
| 286 | (gui.norm_pixel >> 16) & 0xff, |
| 287 | (gui.norm_pixel >> 8) & 0xff, |
| 288 | gui.norm_pixel & 0xff, |
| 289 | (gui.back_pixel >> 16) & 0xff, |
| 290 | (gui.back_pixel >> 8) & 0xff, |
| 291 | gui.back_pixel & 0xff); |
| 292 | |
| 293 | gtk_css_provider_load_from_data(provider, css, -1, NULL); |
| 294 | gtk_style_context_add_provider(context, |
| 295 | GTK_STYLE_PROVIDER(provider), G_MAXUINT); |
| 296 | |
| 297 | g_free(css); |
| 298 | g_free(fontsize_propval); |
| 299 | g_object_unref(provider); |
| 300 | } |
| 301 | #elif GTK_CHECK_VERSION(3,0,0) |
| 302 | gtk_widget_override_font(preedit_label, gui.norm_font); |
| 303 | |
| 304 | vim_snprintf(buf, sizeof(buf), "#%06X", gui.norm_pixel); |
| 305 | gdk_rgba_parse(&color, buf); |
| 306 | gtk_widget_override_color(preedit_label, GTK_STATE_FLAG_NORMAL, &color); |
| 307 | |
| 308 | vim_snprintf(buf, sizeof(buf), "#%06X", gui.back_pixel); |
| 309 | gdk_rgba_parse(&color, buf); |
| 310 | gtk_widget_override_background_color(preedit_label, GTK_STATE_FLAG_NORMAL, |
| 311 | &color); |
| 312 | #else |
| 313 | gtk_widget_modify_font(preedit_label, gui.norm_font); |
| 314 | |
| 315 | vim_snprintf(buf, sizeof(buf), "#%06X", (unsigned)gui.norm_pixel); |
| 316 | gdk_color_parse(buf, &color); |
| 317 | gtk_widget_modify_fg(preedit_label, GTK_STATE_NORMAL, &color); |
| 318 | |
| 319 | vim_snprintf(buf, sizeof(buf), "#%06X", (unsigned)gui.back_pixel); |
| 320 | gdk_color_parse(buf, &color); |
| 321 | gtk_widget_modify_bg(preedit_window, GTK_STATE_NORMAL, &color); |
| 322 | #endif |
| 323 | |
| 324 | gtk_im_context_get_preedit_string(xic, &preedit_string, &attr_list, NULL); |
| 325 | |
| 326 | if (preedit_string[0] != NUL) |
| 327 | { |
| 328 | gtk_label_set_text(GTK_LABEL(preedit_label), preedit_string); |
| 329 | gtk_label_set_attributes(GTK_LABEL(preedit_label), attr_list); |
| 330 | |
| 331 | layout = gtk_label_get_layout(GTK_LABEL(preedit_label)); |
| 332 | pango_layout_get_pixel_size(layout, &w, &h); |
| 333 | h = MAX(h, gui.char_height); |
| 334 | gtk_window_resize(GTK_WINDOW(preedit_window), w, h); |
| 335 | |
| 336 | gtk_widget_show_all(preedit_window); |
| 337 | |
| 338 | im_preedit_window_set_position(); |
| 339 | } |
| 340 | |
| 341 | g_free(preedit_string); |
| 342 | pango_attr_list_unref(attr_list); |
| 343 | } |
| 344 | |
| 345 | static void |
| 346 | im_preedit_window_close() |
| 347 | { |
| 348 | if (preedit_window != NULL) |
| 349 | gtk_widget_hide(preedit_window); |
| 350 | } |
| 351 | |
| 352 | static void |
| 353 | im_show_preedit() |
| 354 | { |
| 355 | im_preedit_window_open(); |
| 356 | |
| 357 | if (p_mh) // blank out the pointer if necessary |
| 358 | gui_mch_mousehide(TRUE); |
| 359 | } |
| 360 | |
| 361 | static void |
| 362 | im_delete_preedit(void) |
| 363 | { |
| 364 | char_u bskey[] = {CSI, 'k', 'b'}; |
| 365 | char_u delkey[] = {CSI, 'k', 'D'}; |
| 366 | |
| 367 | if (p_imst == IM_OVER_THE_SPOT) |
| 368 | { |
| 369 | im_preedit_window_close(); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | if (State & NORMAL |
| 374 | #ifdef FEAT_TERMINAL |
| 375 | && !term_use_loop() |
| 376 | #endif |
| 377 | ) |
| 378 | { |
| 379 | im_preedit_cursor = 0; |
| 380 | return; |
| 381 | } |
| 382 | for (; im_preedit_cursor > 0; --im_preedit_cursor) |
| 383 | add_to_input_buf(bskey, (int)sizeof(bskey)); |
| 384 | |
| 385 | for (; im_preedit_trailing > 0; --im_preedit_trailing) |
| 386 | add_to_input_buf(delkey, (int)sizeof(delkey)); |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Move the cursor left by "num_move_back" characters. |
| 391 | * Note that ins_left() checks im_is_preediting() to avoid breaking undo for |
| 392 | * these K_LEFT keys. |
| 393 | */ |
| 394 | static void |
| 395 | im_correct_cursor(int num_move_back) |
| 396 | { |
| 397 | char_u backkey[] = {CSI, 'k', 'l'}; |
| 398 | |
| 399 | if (State & NORMAL) |
| 400 | return; |
| 401 | # ifdef FEAT_RIGHTLEFT |
| 402 | if ((State & CMDLINE) == 0 && curwin != NULL && curwin->w_p_rl) |
| 403 | backkey[2] = 'r'; |
| 404 | # endif |
| 405 | for (; num_move_back > 0; --num_move_back) |
| 406 | add_to_input_buf(backkey, (int)sizeof(backkey)); |
| 407 | } |
| 408 | |
| 409 | static int xim_expected_char = NUL; |
| 410 | static int xim_ignored_char = FALSE; |
| 411 | |
| 412 | /* |
| 413 | * Update the mode and cursor while in an IM callback. |
| 414 | */ |
| 415 | static void |
| 416 | im_show_info(void) |
| 417 | { |
| 418 | int old_vgetc_busy; |
| 419 | |
| 420 | old_vgetc_busy = vgetc_busy; |
| 421 | vgetc_busy = TRUE; |
| 422 | showmode(); |
| 423 | vgetc_busy = old_vgetc_busy; |
| 424 | if ((State & NORMAL) || (State & INSERT)) |
| 425 | setcursor(); |
| 426 | out_flush(); |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Callback invoked when the user finished preediting. |
| 431 | * Put the final string into the input buffer. |
| 432 | */ |
| 433 | static void |
| 434 | im_commit_cb(GtkIMContext *context UNUSED, |
| 435 | const gchar *str, |
| 436 | gpointer data UNUSED) |
| 437 | { |
| 438 | int slen = (int)STRLEN(str); |
| 439 | int add_to_input = TRUE; |
| 440 | int clen; |
| 441 | int len = slen; |
| 442 | int commit_with_preedit = TRUE; |
| 443 | char_u *im_str; |
| 444 | |
| 445 | #ifdef XIM_DEBUG |
| 446 | xim_log("im_commit_cb(): %s\n", str); |
| 447 | #endif |
| 448 | |
| 449 | if (p_imst == IM_ON_THE_SPOT) |
| 450 | { |
| 451 | // The imhangul module doesn't reset the preedit string before |
| 452 | // committing. Call im_delete_preedit() to work around that. |
| 453 | im_delete_preedit(); |
| 454 | |
| 455 | // Indicate that preediting has finished. |
| 456 | if (preedit_start_col == MAXCOL) |
| 457 | { |
| 458 | init_preedit_start_col(); |
| 459 | commit_with_preedit = FALSE; |
| 460 | } |
| 461 | |
| 462 | // The thing which setting "preedit_start_col" to MAXCOL means that |
| 463 | // "preedit_start_col" will be set forcedly when calling |
| 464 | // preedit_changed_cb() next time. |
| 465 | // "preedit_start_col" should not reset with MAXCOL on this part. Vim |
| 466 | // is simulating the preediting by using add_to_input_str(). when |
| 467 | // preedit begin immediately before committed, the typebuf is not |
| 468 | // flushed to screen, then it can't get correct "preedit_start_col". |
| 469 | // Thus, it should calculate the cells by adding cells of the committed |
| 470 | // string. |
| 471 | if (input_conv.vc_type != CONV_NONE) |
| 472 | { |
| 473 | im_str = string_convert(&input_conv, (char_u *)str, &len); |
| 474 | g_return_if_fail(im_str != NULL); |
| 475 | } |
| 476 | else |
| 477 | im_str = (char_u *)str; |
| 478 | |
| 479 | clen = mb_string2cells(im_str, len); |
| 480 | |
| 481 | if (input_conv.vc_type != CONV_NONE) |
| 482 | vim_free(im_str); |
| 483 | preedit_start_col += clen; |
| 484 | } |
| 485 | |
| 486 | // Is this a single character that matches a keypad key that's just |
| 487 | // been pressed? If so, we don't want it to be entered as such - let |
| 488 | // us carry on processing the raw keycode so that it may be used in |
| 489 | // mappings as <kSomething>. |
| 490 | if (xim_expected_char != NUL) |
| 491 | { |
| 492 | // We're currently processing a keypad or other special key |
| 493 | if (slen == 1 && str[0] == xim_expected_char) |
| 494 | { |
| 495 | // It's a match - don't do it here |
| 496 | xim_ignored_char = TRUE; |
| 497 | add_to_input = FALSE; |
| 498 | } |
| 499 | else |
| 500 | { |
| 501 | // Not a match |
| 502 | xim_ignored_char = FALSE; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | if (add_to_input) |
| 507 | im_add_to_input((char_u *)str, slen); |
| 508 | |
| 509 | if (p_imst == IM_ON_THE_SPOT) |
| 510 | { |
| 511 | // Inserting chars while "im_is_active" is set does not cause a |
| 512 | // change of buffer. When the chars are committed the buffer must be |
| 513 | // marked as changed. |
| 514 | if (!commit_with_preedit) |
| 515 | preedit_start_col = MAXCOL; |
| 516 | |
| 517 | // This flag is used in changed() at next call. |
| 518 | xim_changed_while_preediting = TRUE; |
| 519 | } |
| 520 | |
| 521 | if (gtk_main_level() > 0) |
| 522 | gtk_main_quit(); |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * Callback invoked after start to the preedit. |
| 527 | */ |
| 528 | static void |
| 529 | im_preedit_start_cb(GtkIMContext *context UNUSED, gpointer data UNUSED) |
| 530 | { |
| 531 | #ifdef XIM_DEBUG |
| 532 | xim_log("im_preedit_start_cb()\n"); |
| 533 | #endif |
| 534 | |
| 535 | im_is_active = TRUE; |
| 536 | preedit_is_active = TRUE; |
| 537 | gui_update_cursor(TRUE, FALSE); |
| 538 | im_show_info(); |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Callback invoked after end to the preedit. |
| 543 | */ |
| 544 | static void |
| 545 | im_preedit_end_cb(GtkIMContext *context UNUSED, gpointer data UNUSED) |
| 546 | { |
| 547 | #ifdef XIM_DEBUG |
| 548 | xim_log("im_preedit_end_cb()\n"); |
| 549 | #endif |
| 550 | im_delete_preedit(); |
| 551 | |
| 552 | // Indicate that preediting has finished |
| 553 | if (p_imst == IM_ON_THE_SPOT) |
| 554 | preedit_start_col = MAXCOL; |
| 555 | xim_has_preediting = FALSE; |
| 556 | |
| 557 | #if 0 |
| 558 | // Removal of this line suggested by Takuhiro Nishioka. Fixes that IM was |
| 559 | // switched off unintentionally. We now use preedit_is_active (added by |
| 560 | // SungHyun Nam). |
| 561 | im_is_active = FALSE; |
| 562 | #endif |
| 563 | preedit_is_active = FALSE; |
| 564 | gui_update_cursor(TRUE, FALSE); |
| 565 | im_show_info(); |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Callback invoked after changes to the preedit string. If the preedit |
| 570 | * string was empty before, remember the preedit start column so we know |
| 571 | * where to apply feedback attributes. Delete the previous preedit string |
| 572 | * if there was one, save the new preedit cursor offset, and put the new |
| 573 | * string into the input buffer. |
| 574 | * |
| 575 | * TODO: The pragmatic "put into input buffer" approach used here has |
| 576 | * several fundamental problems: |
| 577 | * |
| 578 | * - The characters in the preedit string are subject to remapping. |
| 579 | * That's broken, only the finally committed string should be remapped. |
| 580 | * |
| 581 | * - There is a race condition involved: The retrieved value for the |
| 582 | * current cursor position will be wrong if any unprocessed characters |
| 583 | * are still queued in the input buffer. |
| 584 | * |
| 585 | * - Due to the lack of synchronization between the file buffer in memory |
| 586 | * and any typed characters, it's practically impossible to implement the |
| 587 | * "retrieve_surrounding" and "delete_surrounding" signals reliably. IM |
| 588 | * modules for languages such as Thai are likely to rely on this feature |
| 589 | * for proper operation. |
| 590 | * |
| 591 | * Conclusions: I think support for preediting needs to be moved to the |
| 592 | * core parts of Vim. Ideally, until it has been committed, the preediting |
| 593 | * string should only be displayed and not affect the buffer content at all. |
| 594 | * The question how to deal with the synchronization issue still remains. |
| 595 | * Circumventing the input buffer is probably not desirable. Anyway, I think |
| 596 | * implementing "retrieve_surrounding" is the only hard problem. |
| 597 | * |
| 598 | * One way to solve all of this in a clean manner would be to queue all key |
| 599 | * press/release events "as is" in the input buffer, and apply the IM filtering |
| 600 | * at the receiving end of the queue. This, however, would have a rather large |
| 601 | * impact on the code base. If there is an easy way to force processing of all |
| 602 | * remaining input from within the "retrieve_surrounding" signal handler, this |
| 603 | * might not be necessary. Gotta ask on vim-dev for opinions. |
| 604 | */ |
| 605 | static void |
| 606 | im_preedit_changed_cb(GtkIMContext *context, gpointer data UNUSED) |
| 607 | { |
| 608 | char *preedit_string = NULL; |
| 609 | int cursor_index = 0; |
| 610 | int num_move_back = 0; |
| 611 | char_u *str; |
| 612 | char_u *p; |
| 613 | int i; |
| 614 | |
| 615 | if (p_imst == IM_ON_THE_SPOT) |
| 616 | gtk_im_context_get_preedit_string(context, |
| 617 | &preedit_string, NULL, |
| 618 | &cursor_index); |
| 619 | else |
| 620 | gtk_im_context_get_preedit_string(context, |
| 621 | &preedit_string, NULL, |
| 622 | NULL); |
| 623 | |
| 624 | #ifdef XIM_DEBUG |
| 625 | xim_log("im_preedit_changed_cb(): %s\n", preedit_string); |
| 626 | #endif |
| 627 | |
| 628 | g_return_if_fail(preedit_string != NULL); // just in case |
| 629 | |
| 630 | if (p_imst == IM_OVER_THE_SPOT) |
| 631 | { |
| 632 | if (preedit_string[0] == NUL) |
| 633 | { |
| 634 | xim_has_preediting = FALSE; |
| 635 | im_delete_preedit(); |
| 636 | } |
| 637 | else |
| 638 | { |
| 639 | xim_has_preediting = TRUE; |
| 640 | im_show_preedit(); |
| 641 | } |
| 642 | } |
| 643 | else |
| 644 | { |
| 645 | // If preedit_start_col is MAXCOL set it to the current cursor position. |
| 646 | if (preedit_start_col == MAXCOL && preedit_string[0] != '\0') |
| 647 | { |
| 648 | xim_has_preediting = TRUE; |
| 649 | |
| 650 | // Urgh, this breaks if the input buffer isn't empty now |
| 651 | init_preedit_start_col(); |
| 652 | } |
| 653 | else if (cursor_index == 0 && preedit_string[0] == '\0') |
| 654 | { |
| 655 | xim_has_preediting = FALSE; |
| 656 | |
| 657 | // If at the start position (after typing backspace) |
| 658 | // preedit_start_col must be reset. |
| 659 | preedit_start_col = MAXCOL; |
| 660 | } |
| 661 | |
| 662 | im_delete_preedit(); |
| 663 | |
Bram Moolenaar | 791fb1b | 2020-06-02 22:24:36 +0200 | [diff] [blame] | 664 | // Compute the end of the preediting area: "preedit_end_col". |
| 665 | // According to the documentation of |
| 666 | // gtk_im_context_get_preedit_string(), the cursor_pos output argument |
| 667 | // returns the offset in bytes. This is unfortunately not true -- real |
| 668 | // life shows the offset is in characters, and the GTK+ source code |
| 669 | // agrees with me. Will file a bug later. |
Bram Moolenaar | f15c8b6 | 2020-06-01 14:34:43 +0200 | [diff] [blame] | 670 | if (preedit_start_col != MAXCOL) |
| 671 | preedit_end_col = preedit_start_col; |
| 672 | str = (char_u *)preedit_string; |
| 673 | for (p = str, i = 0; *p != NUL; p += utf_byte2len(*p), ++i) |
| 674 | { |
| 675 | int is_composing; |
| 676 | |
Bram Moolenaar | 791fb1b | 2020-06-02 22:24:36 +0200 | [diff] [blame] | 677 | is_composing = ((*p & 0x80) != 0 |
| 678 | && utf_iscomposing(utf_ptr2char(p))); |
| 679 | // These offsets are used as counters when generating <BS> and |
| 680 | // <Del> to delete the preedit string. So don't count composing |
| 681 | // characters unless 'delcombine' is enabled. |
Bram Moolenaar | f15c8b6 | 2020-06-01 14:34:43 +0200 | [diff] [blame] | 682 | if (!is_composing || p_deco) |
| 683 | { |
| 684 | if (i < cursor_index) |
| 685 | ++im_preedit_cursor; |
| 686 | else |
| 687 | ++im_preedit_trailing; |
| 688 | } |
| 689 | if (!is_composing && i >= cursor_index) |
| 690 | { |
| 691 | // This is essentially the same as im_preedit_trailing, except |
| 692 | // composing characters are not counted even if p_deco is set. |
| 693 | ++num_move_back; |
| 694 | } |
| 695 | if (preedit_start_col != MAXCOL) |
| 696 | preedit_end_col += utf_ptr2cells(p); |
| 697 | } |
| 698 | |
| 699 | if (p > str) |
| 700 | { |
| 701 | im_add_to_input(str, (int)(p - str)); |
| 702 | im_correct_cursor(num_move_back); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | g_free(preedit_string); |
| 707 | |
| 708 | if (gtk_main_level() > 0) |
| 709 | gtk_main_quit(); |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * Translate the Pango attributes at iter to Vim highlighting attributes. |
| 714 | * Ignore attributes not supported by Vim highlighting. This shouldn't have |
| 715 | * too much impact -- right now we handle even more attributes than necessary |
| 716 | * for the IM modules I tested with. |
| 717 | */ |
| 718 | static int |
| 719 | translate_pango_attributes(PangoAttrIterator *iter) |
| 720 | { |
| 721 | PangoAttribute *attr; |
| 722 | int char_attr = HL_NORMAL; |
| 723 | |
| 724 | attr = pango_attr_iterator_get(iter, PANGO_ATTR_UNDERLINE); |
| 725 | if (attr != NULL && ((PangoAttrInt *)attr)->value |
| 726 | != (int)PANGO_UNDERLINE_NONE) |
| 727 | char_attr |= HL_UNDERLINE; |
| 728 | |
| 729 | attr = pango_attr_iterator_get(iter, PANGO_ATTR_WEIGHT); |
| 730 | if (attr != NULL && ((PangoAttrInt *)attr)->value >= (int)PANGO_WEIGHT_BOLD) |
| 731 | char_attr |= HL_BOLD; |
| 732 | |
| 733 | attr = pango_attr_iterator_get(iter, PANGO_ATTR_STYLE); |
| 734 | if (attr != NULL && ((PangoAttrInt *)attr)->value |
| 735 | != (int)PANGO_STYLE_NORMAL) |
| 736 | char_attr |= HL_ITALIC; |
| 737 | |
| 738 | attr = pango_attr_iterator_get(iter, PANGO_ATTR_BACKGROUND); |
| 739 | if (attr != NULL) |
| 740 | { |
| 741 | const PangoColor *color = &((PangoAttrColor *)attr)->color; |
| 742 | |
| 743 | // Assume inverse if black background is requested |
| 744 | if ((color->red | color->green | color->blue) == 0) |
| 745 | char_attr |= HL_INVERSE; |
| 746 | } |
| 747 | |
| 748 | return char_attr; |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * Retrieve the highlighting attributes at column col in the preedit string. |
| 753 | * Return -1 if not in preediting mode or if col is out of range. |
| 754 | */ |
| 755 | int |
| 756 | im_get_feedback_attr(int col) |
| 757 | { |
| 758 | char *preedit_string = NULL; |
| 759 | PangoAttrList *attr_list = NULL; |
| 760 | int char_attr = -1; |
| 761 | |
| 762 | if (xic == NULL) |
| 763 | return char_attr; |
| 764 | |
| 765 | gtk_im_context_get_preedit_string(xic, &preedit_string, &attr_list, NULL); |
| 766 | |
| 767 | if (preedit_string != NULL && attr_list != NULL) |
| 768 | { |
| 769 | int idx; |
| 770 | |
| 771 | // Get the byte index as used by PangoAttrIterator |
| 772 | for (idx = 0; col > 0 && preedit_string[idx] != '\0'; --col) |
| 773 | idx += utfc_ptr2len((char_u *)preedit_string + idx); |
| 774 | |
| 775 | if (preedit_string[idx] != '\0') |
| 776 | { |
| 777 | PangoAttrIterator *iter; |
| 778 | int start, end; |
| 779 | |
| 780 | char_attr = HL_NORMAL; |
| 781 | iter = pango_attr_list_get_iterator(attr_list); |
| 782 | |
| 783 | // Extract all relevant attributes from the list. |
| 784 | do |
| 785 | { |
| 786 | pango_attr_iterator_range(iter, &start, &end); |
| 787 | |
| 788 | if (idx >= start && idx < end) |
| 789 | char_attr |= translate_pango_attributes(iter); |
| 790 | } |
| 791 | while (pango_attr_iterator_next(iter)); |
| 792 | |
| 793 | pango_attr_iterator_destroy(iter); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | if (attr_list != NULL) |
| 798 | pango_attr_list_unref(attr_list); |
| 799 | g_free(preedit_string); |
| 800 | |
| 801 | return char_attr; |
| 802 | } |
| 803 | |
| 804 | void |
| 805 | xim_init(void) |
| 806 | { |
| 807 | #ifdef XIM_DEBUG |
| 808 | xim_log("xim_init()\n"); |
| 809 | #endif |
| 810 | |
| 811 | g_return_if_fail(gui.drawarea != NULL); |
| 812 | g_return_if_fail(gtk_widget_get_window(gui.drawarea) != NULL); |
| 813 | |
| 814 | xic = gtk_im_multicontext_new(); |
| 815 | g_object_ref(xic); |
| 816 | |
| 817 | im_commit_handler_id = g_signal_connect(G_OBJECT(xic), "commit", |
| 818 | G_CALLBACK(&im_commit_cb), NULL); |
| 819 | g_signal_connect(G_OBJECT(xic), "preedit_changed", |
| 820 | G_CALLBACK(&im_preedit_changed_cb), NULL); |
| 821 | g_signal_connect(G_OBJECT(xic), "preedit_start", |
| 822 | G_CALLBACK(&im_preedit_start_cb), NULL); |
| 823 | g_signal_connect(G_OBJECT(xic), "preedit_end", |
| 824 | G_CALLBACK(&im_preedit_end_cb), NULL); |
| 825 | |
| 826 | gtk_im_context_set_client_window(xic, gtk_widget_get_window(gui.drawarea)); |
| 827 | } |
| 828 | |
| 829 | void |
| 830 | im_shutdown(void) |
| 831 | { |
| 832 | #ifdef XIM_DEBUG |
| 833 | xim_log("im_shutdown()\n"); |
| 834 | #endif |
| 835 | |
| 836 | if (xic != NULL) |
| 837 | { |
| 838 | gtk_im_context_focus_out(xic); |
| 839 | g_object_unref(xic); |
| 840 | xic = NULL; |
| 841 | } |
| 842 | im_is_active = FALSE; |
| 843 | im_commit_handler_id = 0; |
| 844 | if (p_imst == IM_ON_THE_SPOT) |
| 845 | preedit_start_col = MAXCOL; |
| 846 | xim_has_preediting = FALSE; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Convert the string argument to keyval and state for GdkEventKey. |
| 851 | * If str is valid return TRUE, otherwise FALSE. |
| 852 | * |
| 853 | * See 'imactivatekey' for documentation of the format. |
| 854 | */ |
| 855 | static int |
| 856 | im_string_to_keyval(const char *str, unsigned int *keyval, unsigned int *state) |
| 857 | { |
| 858 | const char *mods_end; |
| 859 | unsigned tmp_keyval; |
| 860 | unsigned tmp_state = 0; |
| 861 | |
| 862 | mods_end = strrchr(str, '-'); |
| 863 | mods_end = (mods_end != NULL) ? mods_end + 1 : str; |
| 864 | |
| 865 | // Parse modifier keys |
| 866 | while (str < mods_end) |
| 867 | switch (*str++) |
| 868 | { |
| 869 | case '-': break; |
| 870 | case 'S': case 's': tmp_state |= (unsigned)GDK_SHIFT_MASK; break; |
| 871 | case 'L': case 'l': tmp_state |= (unsigned)GDK_LOCK_MASK; break; |
| 872 | case 'C': case 'c': tmp_state |= (unsigned)GDK_CONTROL_MASK;break; |
| 873 | case '1': tmp_state |= (unsigned)GDK_MOD1_MASK; break; |
| 874 | case '2': tmp_state |= (unsigned)GDK_MOD2_MASK; break; |
| 875 | case '3': tmp_state |= (unsigned)GDK_MOD3_MASK; break; |
| 876 | case '4': tmp_state |= (unsigned)GDK_MOD4_MASK; break; |
| 877 | case '5': tmp_state |= (unsigned)GDK_MOD5_MASK; break; |
| 878 | default: |
| 879 | return FALSE; |
| 880 | } |
| 881 | |
| 882 | tmp_keyval = gdk_keyval_from_name(str); |
| 883 | |
| 884 | if (tmp_keyval == 0 || tmp_keyval == GDK_VoidSymbol) |
| 885 | return FALSE; |
| 886 | |
| 887 | if (keyval != NULL) |
| 888 | *keyval = tmp_keyval; |
| 889 | if (state != NULL) |
| 890 | *state = tmp_state; |
| 891 | |
| 892 | return TRUE; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Return TRUE if p_imak is valid, otherwise FALSE. As a special case, an |
| 897 | * empty string is also regarded as valid. |
| 898 | * |
| 899 | * Note: The numerical key value of p_imak is cached if it was valid; thus |
| 900 | * boldly assuming im_xim_isvalid_imactivate() will always be called whenever |
| 901 | * 'imak' changes. This is currently the case but not obvious -- should |
| 902 | * probably rename the function for clarity. |
| 903 | */ |
| 904 | int |
| 905 | im_xim_isvalid_imactivate(void) |
| 906 | { |
| 907 | if (p_imak[0] == NUL) |
| 908 | { |
| 909 | im_activatekey_keyval = GDK_VoidSymbol; |
| 910 | im_activatekey_state = 0; |
| 911 | return TRUE; |
| 912 | } |
| 913 | |
| 914 | return im_string_to_keyval((const char *)p_imak, |
| 915 | &im_activatekey_keyval, |
| 916 | &im_activatekey_state); |
| 917 | } |
| 918 | |
| 919 | static void |
| 920 | im_synthesize_keypress(unsigned int keyval, unsigned int state) |
| 921 | { |
| 922 | GdkEventKey *event; |
| 923 | |
| 924 | event = (GdkEventKey *)gdk_event_new(GDK_KEY_PRESS); |
| 925 | g_object_ref(gtk_widget_get_window(gui.drawarea)); |
| 926 | // unreffed by gdk_event_free() |
| 927 | event->window = gtk_widget_get_window(gui.drawarea); |
| 928 | event->send_event = TRUE; |
| 929 | event->time = GDK_CURRENT_TIME; |
| 930 | event->state = state; |
| 931 | event->keyval = keyval; |
| 932 | event->hardware_keycode = // needed for XIM |
| 933 | XKeysymToKeycode(GDK_WINDOW_XDISPLAY(event->window), (KeySym)keyval); |
| 934 | event->length = 0; |
| 935 | event->string = NULL; |
| 936 | |
| 937 | gtk_im_context_filter_keypress(xic, event); |
| 938 | |
| 939 | // For consistency, also send the corresponding release event. |
| 940 | event->type = GDK_KEY_RELEASE; |
| 941 | event->send_event = FALSE; |
| 942 | gtk_im_context_filter_keypress(xic, event); |
| 943 | |
| 944 | gdk_event_free((GdkEvent *)event); |
| 945 | } |
| 946 | |
| 947 | void |
| 948 | xim_reset(void) |
| 949 | { |
| 950 | # ifdef FEAT_EVAL |
| 951 | if (USE_IMACTIVATEFUNC) |
| 952 | call_imactivatefunc(im_is_active); |
| 953 | else |
| 954 | # endif |
| 955 | if (xic != NULL) |
| 956 | { |
| 957 | gtk_im_context_reset(xic); |
| 958 | |
| 959 | if (p_imdisable) |
| 960 | im_shutdown(); |
| 961 | else |
| 962 | { |
| 963 | xim_set_focus(gui.in_focus); |
| 964 | |
| 965 | if (im_activatekey_keyval != GDK_VoidSymbol) |
| 966 | { |
| 967 | if (im_is_active) |
| 968 | { |
| 969 | g_signal_handler_block(xic, im_commit_handler_id); |
| 970 | im_synthesize_keypress(im_activatekey_keyval, |
| 971 | im_activatekey_state); |
| 972 | g_signal_handler_unblock(xic, im_commit_handler_id); |
| 973 | } |
| 974 | } |
| 975 | else |
| 976 | { |
| 977 | im_shutdown(); |
| 978 | xim_init(); |
| 979 | xim_set_focus(gui.in_focus); |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | if (p_imst == IM_ON_THE_SPOT) |
| 985 | preedit_start_col = MAXCOL; |
| 986 | xim_has_preediting = FALSE; |
| 987 | } |
| 988 | |
| 989 | int |
| 990 | xim_queue_key_press_event(GdkEventKey *event, int down) |
| 991 | { |
| 992 | if (down) |
| 993 | { |
Bram Moolenaar | 791fb1b | 2020-06-02 22:24:36 +0200 | [diff] [blame] | 994 | // Workaround GTK2 XIM 'feature' that always converts keypad keys to |
| 995 | // chars., even when not part of an IM sequence (ref. feature of |
| 996 | // gdk/gdkkeyuni.c). |
| 997 | // Flag any keypad keys that might represent a single char. |
| 998 | // If this (on its own - i.e., not part of an IM sequence) is |
| 999 | // committed while we're processing one of these keys, we can ignore |
| 1000 | // that commit and go ahead & process it ourselves. That way we can |
| 1001 | // still distinguish keypad keys for use in mappings. |
| 1002 | // Also add GDK_space to make <S-Space> work. |
Bram Moolenaar | f15c8b6 | 2020-06-01 14:34:43 +0200 | [diff] [blame] | 1003 | switch (event->keyval) |
| 1004 | { |
| 1005 | case GDK_KP_Add: xim_expected_char = '+'; break; |
| 1006 | case GDK_KP_Subtract: xim_expected_char = '-'; break; |
| 1007 | case GDK_KP_Divide: xim_expected_char = '/'; break; |
| 1008 | case GDK_KP_Multiply: xim_expected_char = '*'; break; |
| 1009 | case GDK_KP_Decimal: xim_expected_char = '.'; break; |
| 1010 | case GDK_KP_Equal: xim_expected_char = '='; break; |
| 1011 | case GDK_KP_0: xim_expected_char = '0'; break; |
| 1012 | case GDK_KP_1: xim_expected_char = '1'; break; |
| 1013 | case GDK_KP_2: xim_expected_char = '2'; break; |
| 1014 | case GDK_KP_3: xim_expected_char = '3'; break; |
| 1015 | case GDK_KP_4: xim_expected_char = '4'; break; |
| 1016 | case GDK_KP_5: xim_expected_char = '5'; break; |
| 1017 | case GDK_KP_6: xim_expected_char = '6'; break; |
| 1018 | case GDK_KP_7: xim_expected_char = '7'; break; |
| 1019 | case GDK_KP_8: xim_expected_char = '8'; break; |
| 1020 | case GDK_KP_9: xim_expected_char = '9'; break; |
| 1021 | case GDK_space: xim_expected_char = ' '; break; |
| 1022 | default: xim_expected_char = NUL; |
| 1023 | } |
| 1024 | xim_ignored_char = FALSE; |
| 1025 | } |
| 1026 | |
Bram Moolenaar | 791fb1b | 2020-06-02 22:24:36 +0200 | [diff] [blame] | 1027 | // When typing fFtT, XIM may be activated. Thus it must pass |
| 1028 | // gtk_im_context_filter_keypress() in Normal mode. |
| 1029 | // And while doing :sh too. |
Bram Moolenaar | f15c8b6 | 2020-06-01 14:34:43 +0200 | [diff] [blame] | 1030 | if (xic != NULL && !p_imdisable |
| 1031 | && (State & (INSERT | CMDLINE | NORMAL | EXTERNCMD)) != 0) |
| 1032 | { |
Bram Moolenaar | 791fb1b | 2020-06-02 22:24:36 +0200 | [diff] [blame] | 1033 | // Filter 'imactivatekey' and map it to CTRL-^. This way, Vim is |
| 1034 | // always aware of the current status of IM, and can even emulate |
| 1035 | // the activation key for modules that don't support one. |
Bram Moolenaar | f15c8b6 | 2020-06-01 14:34:43 +0200 | [diff] [blame] | 1036 | if (event->keyval == im_activatekey_keyval |
| 1037 | && (event->state & im_activatekey_state) == im_activatekey_state) |
| 1038 | { |
| 1039 | unsigned int state_mask; |
| 1040 | |
| 1041 | // Require the state of the 3 most used modifiers to match exactly. |
| 1042 | // Otherwise e.g. <S-C-space> would be unusable for other purposes |
| 1043 | // if the IM activate key is <S-space>. |
| 1044 | state_mask = im_activatekey_state; |
| 1045 | state_mask |= ((int)GDK_SHIFT_MASK | (int)GDK_CONTROL_MASK |
| 1046 | | (int)GDK_MOD1_MASK); |
| 1047 | |
| 1048 | if ((event->state & state_mask) != im_activatekey_state) |
| 1049 | return FALSE; |
| 1050 | |
| 1051 | // Don't send it a second time on GDK_KEY_RELEASE. |
| 1052 | if (event->type != GDK_KEY_PRESS) |
| 1053 | return TRUE; |
| 1054 | |
| 1055 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
| 1056 | { |
| 1057 | im_set_active(FALSE); |
| 1058 | |
| 1059 | // ":lmap" mappings exists, toggle use of mappings. |
| 1060 | State ^= LANGMAP; |
| 1061 | if (State & LANGMAP) |
| 1062 | { |
| 1063 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 1064 | State &= ~LANGMAP; |
| 1065 | } |
| 1066 | else |
| 1067 | { |
| 1068 | curbuf->b_p_iminsert = B_IMODE_LMAP; |
| 1069 | State |= LANGMAP; |
| 1070 | } |
| 1071 | return TRUE; |
| 1072 | } |
| 1073 | |
| 1074 | return gtk_im_context_filter_keypress(xic, event); |
| 1075 | } |
| 1076 | |
| 1077 | // Don't filter events through the IM context if IM isn't active |
| 1078 | // right now. Unlike with GTK+ 1.2 we cannot rely on the IM module |
| 1079 | // not doing anything before the activation key was sent. |
| 1080 | if (im_activatekey_keyval == GDK_VoidSymbol || im_is_active) |
| 1081 | { |
| 1082 | int imresult = gtk_im_context_filter_keypress(xic, event); |
| 1083 | |
| 1084 | if (p_imst == IM_ON_THE_SPOT) |
| 1085 | { |
| 1086 | // Some XIM send following sequence: |
| 1087 | // 1. preedited string. |
| 1088 | // 2. committed string. |
| 1089 | // 3. line changed key. |
| 1090 | // 4. preedited string. |
| 1091 | // 5. remove preedited string. |
| 1092 | // if 3, Vim can't move back the above line for 5. |
| 1093 | // thus, this part should not parse the key. |
| 1094 | if (!imresult && preedit_start_col != MAXCOL |
| 1095 | && event->keyval == GDK_Return) |
| 1096 | { |
| 1097 | im_synthesize_keypress(GDK_Return, 0U); |
| 1098 | return FALSE; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | // If XIM tried to commit a keypad key as a single char., |
| 1103 | // ignore it so we can use the keypad key 'raw', for mappings. |
| 1104 | if (xim_expected_char != NUL && xim_ignored_char) |
| 1105 | // We had a keypad key, and XIM tried to thieve it |
| 1106 | return FALSE; |
| 1107 | |
| 1108 | // This is supposed to fix a problem with iBus, that space |
| 1109 | // characters don't work in input mode. |
| 1110 | xim_expected_char = NUL; |
| 1111 | |
| 1112 | // Normal processing |
| 1113 | return imresult; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | return FALSE; |
| 1118 | } |
| 1119 | |
| 1120 | int |
| 1121 | im_get_status(void) |
| 1122 | { |
| 1123 | # ifdef FEAT_EVAL |
| 1124 | if (USE_IMSTATUSFUNC) |
| 1125 | return call_imstatusfunc(); |
| 1126 | # endif |
| 1127 | return im_is_active; |
| 1128 | } |
| 1129 | |
| 1130 | int |
| 1131 | preedit_get_status(void) |
| 1132 | { |
| 1133 | return preedit_is_active; |
| 1134 | } |
| 1135 | |
| 1136 | int |
| 1137 | im_is_preediting(void) |
| 1138 | { |
| 1139 | return xim_has_preediting; |
| 1140 | } |
| 1141 | |
| 1142 | # else // !FEAT_GUI_GTK |
| 1143 | |
| 1144 | static int xim_is_active = FALSE; // XIM should be active in the current |
| 1145 | // mode |
| 1146 | static int xim_has_focus = FALSE; // XIM is really being used for Vim |
| 1147 | # ifdef FEAT_GUI_X11 |
| 1148 | static XIMStyle input_style; |
| 1149 | static int status_area_enabled = TRUE; |
| 1150 | # endif |
| 1151 | |
| 1152 | /* |
| 1153 | * Switch using XIM on/off. This is used by the code that changes "State". |
| 1154 | * When 'imactivatefunc' is defined use that function instead. |
| 1155 | */ |
| 1156 | void |
| 1157 | im_set_active(int active_arg) |
| 1158 | { |
| 1159 | int active = active_arg; |
| 1160 | |
| 1161 | // If 'imdisable' is set, XIM is never active. |
| 1162 | if (p_imdisable) |
| 1163 | active = FALSE; |
| 1164 | else if (input_style & XIMPreeditPosition) |
| 1165 | // There is a problem in switching XIM off when preediting is used, |
| 1166 | // and it is not clear how this can be solved. For now, keep XIM on |
| 1167 | // all the time, like it was done in Vim 5.8. |
| 1168 | active = TRUE; |
| 1169 | |
| 1170 | # if defined(FEAT_EVAL) |
| 1171 | if (USE_IMACTIVATEFUNC) |
| 1172 | { |
| 1173 | if (active != im_get_status()) |
| 1174 | { |
| 1175 | call_imactivatefunc(active); |
| 1176 | xim_has_focus = active; |
| 1177 | } |
| 1178 | return; |
| 1179 | } |
| 1180 | # endif |
| 1181 | |
| 1182 | if (xic == NULL) |
| 1183 | return; |
| 1184 | |
| 1185 | // Remember the active state, it is needed when Vim gets keyboard focus. |
| 1186 | xim_is_active = active; |
| 1187 | xim_set_preedit(); |
| 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | * Adjust using XIM for gaining or losing keyboard focus. Also called when |
| 1192 | * "xim_is_active" changes. |
| 1193 | */ |
| 1194 | void |
| 1195 | xim_set_focus(int focus) |
| 1196 | { |
| 1197 | if (xic == NULL) |
| 1198 | return; |
| 1199 | |
Bram Moolenaar | 791fb1b | 2020-06-02 22:24:36 +0200 | [diff] [blame] | 1200 | // XIM only gets focus when the Vim window has keyboard focus and XIM has |
| 1201 | // been set active for the current mode. |
Bram Moolenaar | f15c8b6 | 2020-06-01 14:34:43 +0200 | [diff] [blame] | 1202 | if (focus && xim_is_active) |
| 1203 | { |
| 1204 | if (!xim_has_focus) |
| 1205 | { |
| 1206 | xim_has_focus = TRUE; |
| 1207 | XSetICFocus(xic); |
| 1208 | } |
| 1209 | } |
| 1210 | else |
| 1211 | { |
| 1212 | if (xim_has_focus) |
| 1213 | { |
| 1214 | xim_has_focus = FALSE; |
| 1215 | XUnsetICFocus(xic); |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | void |
| 1221 | im_set_position(int row UNUSED, int col UNUSED) |
| 1222 | { |
| 1223 | xim_set_preedit(); |
| 1224 | } |
| 1225 | |
| 1226 | /* |
| 1227 | * Set the XIM to the current cursor position. |
| 1228 | */ |
| 1229 | void |
| 1230 | xim_set_preedit(void) |
| 1231 | { |
| 1232 | XVaNestedList attr_list; |
| 1233 | XRectangle spot_area; |
| 1234 | XPoint over_spot; |
| 1235 | int line_space; |
| 1236 | |
| 1237 | if (xic == NULL) |
| 1238 | return; |
| 1239 | |
| 1240 | xim_set_focus(TRUE); |
| 1241 | |
| 1242 | if (!xim_has_focus) |
| 1243 | { |
| 1244 | // hide XIM cursor |
| 1245 | over_spot.x = 0; |
| 1246 | over_spot.y = -100; // arbitrary invisible position |
| 1247 | attr_list = (XVaNestedList) XVaCreateNestedList(0, |
| 1248 | XNSpotLocation, |
| 1249 | &over_spot, |
| 1250 | NULL); |
| 1251 | XSetICValues(xic, XNPreeditAttributes, attr_list, NULL); |
| 1252 | XFree(attr_list); |
| 1253 | return; |
| 1254 | } |
| 1255 | |
| 1256 | if (input_style & XIMPreeditPosition) |
| 1257 | { |
| 1258 | if (xim_fg_color == INVALCOLOR) |
| 1259 | { |
| 1260 | xim_fg_color = gui.def_norm_pixel; |
| 1261 | xim_bg_color = gui.def_back_pixel; |
| 1262 | } |
| 1263 | over_spot.x = TEXT_X(gui.col); |
| 1264 | over_spot.y = TEXT_Y(gui.row); |
| 1265 | spot_area.x = 0; |
| 1266 | spot_area.y = 0; |
| 1267 | spot_area.height = gui.char_height * Rows; |
| 1268 | spot_area.width = gui.char_width * Columns; |
| 1269 | line_space = gui.char_height; |
| 1270 | attr_list = (XVaNestedList) XVaCreateNestedList(0, |
| 1271 | XNSpotLocation, &over_spot, |
| 1272 | XNForeground, (Pixel) xim_fg_color, |
| 1273 | XNBackground, (Pixel) xim_bg_color, |
| 1274 | XNArea, &spot_area, |
| 1275 | XNLineSpace, line_space, |
| 1276 | NULL); |
| 1277 | if (XSetICValues(xic, XNPreeditAttributes, attr_list, NULL)) |
| 1278 | emsg(_("E284: Cannot set IC values")); |
| 1279 | XFree(attr_list); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | # if defined(FEAT_GUI_X11) |
| 1284 | static char e_xim[] = N_("E285: Failed to create input context"); |
| 1285 | # endif |
| 1286 | |
| 1287 | # if defined(FEAT_GUI_X11) || defined(PROTO) |
| 1288 | # if defined(XtSpecificationRelease) && XtSpecificationRelease >= 6 && !defined(SUN_SYSTEM) |
| 1289 | # define USE_X11R6_XIM |
| 1290 | # endif |
| 1291 | |
| 1292 | static int xim_real_init(Window x11_window, Display *x11_display); |
| 1293 | |
| 1294 | |
| 1295 | # ifdef USE_X11R6_XIM |
| 1296 | static void |
| 1297 | xim_instantiate_cb( |
| 1298 | Display *display, |
| 1299 | XPointer client_data UNUSED, |
| 1300 | XPointer call_data UNUSED) |
| 1301 | { |
| 1302 | Window x11_window; |
| 1303 | Display *x11_display; |
| 1304 | |
| 1305 | # ifdef XIM_DEBUG |
| 1306 | xim_log("xim_instantiate_cb()\n"); |
| 1307 | # endif |
| 1308 | |
| 1309 | gui_get_x11_windis(&x11_window, &x11_display); |
| 1310 | if (display != x11_display) |
| 1311 | return; |
| 1312 | |
| 1313 | xim_real_init(x11_window, x11_display); |
| 1314 | gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH); |
| 1315 | if (xic != NULL) |
| 1316 | XUnregisterIMInstantiateCallback(x11_display, NULL, NULL, NULL, |
| 1317 | xim_instantiate_cb, NULL); |
| 1318 | } |
| 1319 | |
| 1320 | static void |
| 1321 | xim_destroy_cb( |
| 1322 | XIM im UNUSED, |
| 1323 | XPointer client_data UNUSED, |
| 1324 | XPointer call_data UNUSED) |
| 1325 | { |
| 1326 | Window x11_window; |
| 1327 | Display *x11_display; |
| 1328 | |
| 1329 | # ifdef XIM_DEBUG |
| 1330 | xim_log("xim_destroy_cb()\n"); |
| 1331 | #endif |
| 1332 | gui_get_x11_windis(&x11_window, &x11_display); |
| 1333 | |
| 1334 | xic = NULL; |
| 1335 | status_area_enabled = FALSE; |
| 1336 | |
| 1337 | gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH); |
| 1338 | |
| 1339 | XRegisterIMInstantiateCallback(x11_display, NULL, NULL, NULL, |
| 1340 | xim_instantiate_cb, NULL); |
| 1341 | } |
| 1342 | # endif |
| 1343 | |
| 1344 | void |
| 1345 | xim_init(void) |
| 1346 | { |
| 1347 | Window x11_window; |
| 1348 | Display *x11_display; |
| 1349 | |
| 1350 | # ifdef XIM_DEBUG |
| 1351 | xim_log("xim_init()\n"); |
| 1352 | # endif |
| 1353 | |
| 1354 | gui_get_x11_windis(&x11_window, &x11_display); |
| 1355 | |
| 1356 | xic = NULL; |
| 1357 | |
| 1358 | if (xim_real_init(x11_window, x11_display)) |
| 1359 | return; |
| 1360 | |
| 1361 | gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH); |
| 1362 | |
| 1363 | # ifdef USE_X11R6_XIM |
| 1364 | XRegisterIMInstantiateCallback(x11_display, NULL, NULL, NULL, |
| 1365 | xim_instantiate_cb, NULL); |
| 1366 | # endif |
| 1367 | } |
| 1368 | |
| 1369 | static int |
| 1370 | xim_real_init(Window x11_window, Display *x11_display) |
| 1371 | { |
| 1372 | int i; |
| 1373 | char *p, |
| 1374 | *s, |
| 1375 | *ns, |
| 1376 | *end, |
| 1377 | tmp[1024]; |
| 1378 | # define IMLEN_MAX 40 |
| 1379 | char buf[IMLEN_MAX + 7]; |
| 1380 | XIM xim = NULL; |
| 1381 | XIMStyles *xim_styles; |
| 1382 | XIMStyle this_input_style = 0; |
| 1383 | Boolean found; |
| 1384 | XPoint over_spot; |
| 1385 | XVaNestedList preedit_list, status_list; |
| 1386 | |
| 1387 | input_style = 0; |
| 1388 | status_area_enabled = FALSE; |
| 1389 | |
| 1390 | if (xic != NULL) |
| 1391 | return FALSE; |
| 1392 | |
| 1393 | if (gui.rsrc_input_method != NULL && *gui.rsrc_input_method != NUL) |
| 1394 | { |
| 1395 | strcpy(tmp, gui.rsrc_input_method); |
| 1396 | for (ns = s = tmp; ns != NULL && *s != NUL;) |
| 1397 | { |
| 1398 | s = (char *)skipwhite((char_u *)s); |
| 1399 | if (*s == NUL) |
| 1400 | break; |
| 1401 | if ((ns = end = strchr(s, ',')) == NULL) |
| 1402 | end = s + strlen(s); |
| 1403 | while (isspace(((char_u *)end)[-1])) |
| 1404 | end--; |
| 1405 | *end = NUL; |
| 1406 | |
| 1407 | if (strlen(s) <= IMLEN_MAX) |
| 1408 | { |
| 1409 | strcpy(buf, "@im="); |
| 1410 | strcat(buf, s); |
| 1411 | if ((p = XSetLocaleModifiers(buf)) != NULL && *p != NUL |
| 1412 | && (xim = XOpenIM(x11_display, NULL, NULL, NULL)) |
| 1413 | != NULL) |
| 1414 | break; |
| 1415 | } |
| 1416 | |
| 1417 | s = ns + 1; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | if (xim == NULL && (p = XSetLocaleModifiers("")) != NULL && *p != NUL) |
| 1422 | xim = XOpenIM(x11_display, NULL, NULL, NULL); |
| 1423 | |
| 1424 | // This is supposed to be useful to obtain characters through |
| 1425 | // XmbLookupString() without really using a XIM. |
| 1426 | if (xim == NULL && (p = XSetLocaleModifiers("@im=none")) != NULL |
| 1427 | && *p != NUL) |
| 1428 | xim = XOpenIM(x11_display, NULL, NULL, NULL); |
| 1429 | |
| 1430 | if (xim == NULL) |
| 1431 | { |
| 1432 | // Only give this message when verbose is set, because too many people |
| 1433 | // got this message when they didn't want to use a XIM. |
| 1434 | if (p_verbose > 0) |
| 1435 | { |
| 1436 | verbose_enter(); |
| 1437 | emsg(_("E286: Failed to open input method")); |
| 1438 | verbose_leave(); |
| 1439 | } |
| 1440 | return FALSE; |
| 1441 | } |
| 1442 | |
| 1443 | # ifdef USE_X11R6_XIM |
| 1444 | { |
| 1445 | XIMCallback destroy_cb; |
| 1446 | |
| 1447 | destroy_cb.callback = xim_destroy_cb; |
| 1448 | destroy_cb.client_data = NULL; |
| 1449 | if (XSetIMValues(xim, XNDestroyCallback, &destroy_cb, NULL)) |
| 1450 | emsg(_("E287: Warning: Could not set destroy callback to IM")); |
| 1451 | } |
| 1452 | # endif |
| 1453 | |
| 1454 | if (XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL) || !xim_styles) |
| 1455 | { |
| 1456 | emsg(_("E288: input method doesn't support any style")); |
| 1457 | XCloseIM(xim); |
| 1458 | return FALSE; |
| 1459 | } |
| 1460 | |
| 1461 | found = False; |
| 1462 | strcpy(tmp, gui.rsrc_preedit_type_name); |
| 1463 | for (s = tmp; s && !found; ) |
| 1464 | { |
| 1465 | while (*s && isspace((unsigned char)*s)) |
| 1466 | s++; |
| 1467 | if (!*s) |
| 1468 | break; |
| 1469 | if ((ns = end = strchr(s, ',')) != 0) |
| 1470 | ns++; |
| 1471 | else |
| 1472 | end = s + strlen(s); |
| 1473 | while (isspace((unsigned char)*end)) |
| 1474 | end--; |
| 1475 | *end = '\0'; |
| 1476 | |
| 1477 | if (!strcmp(s, "OverTheSpot")) |
| 1478 | this_input_style = (XIMPreeditPosition | XIMStatusArea); |
| 1479 | else if (!strcmp(s, "OffTheSpot")) |
| 1480 | this_input_style = (XIMPreeditArea | XIMStatusArea); |
| 1481 | else if (!strcmp(s, "Root")) |
| 1482 | this_input_style = (XIMPreeditNothing | XIMStatusNothing); |
| 1483 | |
| 1484 | for (i = 0; (unsigned short)i < xim_styles->count_styles; i++) |
| 1485 | { |
| 1486 | if (this_input_style == xim_styles->supported_styles[i]) |
| 1487 | { |
| 1488 | found = True; |
| 1489 | break; |
| 1490 | } |
| 1491 | } |
| 1492 | if (!found) |
| 1493 | for (i = 0; (unsigned short)i < xim_styles->count_styles; i++) |
| 1494 | { |
| 1495 | if ((xim_styles->supported_styles[i] & this_input_style) |
| 1496 | == (this_input_style & ~XIMStatusArea)) |
| 1497 | { |
| 1498 | this_input_style &= ~XIMStatusArea; |
| 1499 | found = True; |
| 1500 | break; |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | s = ns; |
| 1505 | } |
| 1506 | XFree(xim_styles); |
| 1507 | |
| 1508 | if (!found) |
| 1509 | { |
| 1510 | // Only give this message when verbose is set, because too many people |
| 1511 | // got this message when they didn't want to use a XIM. |
| 1512 | if (p_verbose > 0) |
| 1513 | { |
| 1514 | verbose_enter(); |
| 1515 | emsg(_("E289: input method doesn't support my preedit type")); |
| 1516 | verbose_leave(); |
| 1517 | } |
| 1518 | XCloseIM(xim); |
| 1519 | return FALSE; |
| 1520 | } |
| 1521 | |
| 1522 | over_spot.x = TEXT_X(gui.col); |
| 1523 | over_spot.y = TEXT_Y(gui.row); |
| 1524 | input_style = this_input_style; |
| 1525 | |
| 1526 | // A crash was reported when trying to pass gui.norm_font as XNFontSet, |
| 1527 | // thus that has been removed. Hopefully the default works... |
| 1528 | # ifdef FEAT_XFONTSET |
| 1529 | if (gui.fontset != NOFONTSET) |
| 1530 | { |
| 1531 | preedit_list = XVaCreateNestedList(0, |
| 1532 | XNSpotLocation, &over_spot, |
| 1533 | XNForeground, (Pixel)gui.def_norm_pixel, |
| 1534 | XNBackground, (Pixel)gui.def_back_pixel, |
| 1535 | XNFontSet, (XFontSet)gui.fontset, |
| 1536 | NULL); |
| 1537 | status_list = XVaCreateNestedList(0, |
| 1538 | XNForeground, (Pixel)gui.def_norm_pixel, |
| 1539 | XNBackground, (Pixel)gui.def_back_pixel, |
| 1540 | XNFontSet, (XFontSet)gui.fontset, |
| 1541 | NULL); |
| 1542 | } |
| 1543 | else |
| 1544 | # endif |
| 1545 | { |
| 1546 | preedit_list = XVaCreateNestedList(0, |
| 1547 | XNSpotLocation, &over_spot, |
| 1548 | XNForeground, (Pixel)gui.def_norm_pixel, |
| 1549 | XNBackground, (Pixel)gui.def_back_pixel, |
| 1550 | NULL); |
| 1551 | status_list = XVaCreateNestedList(0, |
| 1552 | XNForeground, (Pixel)gui.def_norm_pixel, |
| 1553 | XNBackground, (Pixel)gui.def_back_pixel, |
| 1554 | NULL); |
| 1555 | } |
| 1556 | |
| 1557 | xic = XCreateIC(xim, |
| 1558 | XNInputStyle, input_style, |
| 1559 | XNClientWindow, x11_window, |
| 1560 | XNFocusWindow, gui.wid, |
| 1561 | XNPreeditAttributes, preedit_list, |
| 1562 | XNStatusAttributes, status_list, |
| 1563 | NULL); |
| 1564 | XFree(status_list); |
| 1565 | XFree(preedit_list); |
| 1566 | if (xic != NULL) |
| 1567 | { |
| 1568 | if (input_style & XIMStatusArea) |
| 1569 | { |
| 1570 | xim_set_status_area(); |
| 1571 | status_area_enabled = TRUE; |
| 1572 | } |
| 1573 | else |
| 1574 | gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH); |
| 1575 | } |
| 1576 | else |
| 1577 | { |
| 1578 | if (!is_not_a_term()) |
| 1579 | emsg(_(e_xim)); |
| 1580 | XCloseIM(xim); |
| 1581 | return FALSE; |
| 1582 | } |
| 1583 | |
| 1584 | return TRUE; |
| 1585 | } |
| 1586 | |
| 1587 | # endif // FEAT_GUI_X11 |
| 1588 | |
| 1589 | /* |
| 1590 | * Get IM status. When IM is on, return TRUE. Else return FALSE. |
| 1591 | * FIXME: This doesn't work correctly: Having focus doesn't always mean XIM is |
| 1592 | * active, when not having focus XIM may still be active (e.g., when using a |
| 1593 | * tear-off menu item). |
| 1594 | */ |
| 1595 | int |
| 1596 | im_get_status(void) |
| 1597 | { |
| 1598 | # ifdef FEAT_EVAL |
| 1599 | if (USE_IMSTATUSFUNC) |
| 1600 | return call_imstatusfunc(); |
| 1601 | # endif |
| 1602 | return xim_has_focus; |
| 1603 | } |
| 1604 | |
| 1605 | # endif // !FEAT_GUI_GTK |
| 1606 | |
| 1607 | # if !defined(FEAT_GUI_GTK) || defined(PROTO) |
| 1608 | /* |
| 1609 | * Set up the status area. |
| 1610 | * |
| 1611 | * This should use a separate Widget, but that seems not possible, because |
| 1612 | * preedit_area and status_area should be set to the same window as for the |
| 1613 | * text input. Unfortunately this means the status area pollutes the text |
| 1614 | * window... |
| 1615 | */ |
| 1616 | void |
| 1617 | xim_set_status_area(void) |
| 1618 | { |
| 1619 | XVaNestedList preedit_list = 0, status_list = 0, list = 0; |
| 1620 | XRectangle pre_area, status_area; |
| 1621 | |
| 1622 | if (xic == NULL) |
| 1623 | return; |
| 1624 | |
| 1625 | if (input_style & XIMStatusArea) |
| 1626 | { |
| 1627 | if (input_style & XIMPreeditArea) |
| 1628 | { |
| 1629 | XRectangle *needed_rect; |
| 1630 | |
| 1631 | // to get status_area width |
| 1632 | status_list = XVaCreateNestedList(0, XNAreaNeeded, |
| 1633 | &needed_rect, NULL); |
| 1634 | XGetICValues(xic, XNStatusAttributes, status_list, NULL); |
| 1635 | XFree(status_list); |
| 1636 | |
| 1637 | status_area.width = needed_rect->width; |
| 1638 | } |
| 1639 | else |
| 1640 | status_area.width = gui.char_width * Columns; |
| 1641 | |
| 1642 | status_area.x = 0; |
| 1643 | status_area.y = gui.char_height * Rows + gui.border_offset; |
| 1644 | if (gui.which_scrollbars[SBAR_BOTTOM]) |
| 1645 | status_area.y += gui.scrollbar_height; |
| 1646 | #ifdef FEAT_MENU |
| 1647 | if (gui.menu_is_active) |
| 1648 | status_area.y += gui.menu_height; |
| 1649 | #endif |
| 1650 | status_area.height = gui.char_height; |
| 1651 | status_list = XVaCreateNestedList(0, XNArea, &status_area, NULL); |
| 1652 | } |
| 1653 | else |
| 1654 | { |
| 1655 | status_area.x = 0; |
| 1656 | status_area.y = gui.char_height * Rows + gui.border_offset; |
| 1657 | if (gui.which_scrollbars[SBAR_BOTTOM]) |
| 1658 | status_area.y += gui.scrollbar_height; |
| 1659 | #ifdef FEAT_MENU |
| 1660 | if (gui.menu_is_active) |
| 1661 | status_area.y += gui.menu_height; |
| 1662 | #endif |
| 1663 | status_area.width = 0; |
| 1664 | status_area.height = gui.char_height; |
| 1665 | } |
| 1666 | |
| 1667 | if (input_style & XIMPreeditArea) // off-the-spot |
| 1668 | { |
| 1669 | pre_area.x = status_area.x + status_area.width; |
| 1670 | pre_area.y = gui.char_height * Rows + gui.border_offset; |
| 1671 | pre_area.width = gui.char_width * Columns - pre_area.x; |
| 1672 | if (gui.which_scrollbars[SBAR_BOTTOM]) |
| 1673 | pre_area.y += gui.scrollbar_height; |
| 1674 | #ifdef FEAT_MENU |
| 1675 | if (gui.menu_is_active) |
| 1676 | pre_area.y += gui.menu_height; |
| 1677 | #endif |
| 1678 | pre_area.height = gui.char_height; |
| 1679 | preedit_list = XVaCreateNestedList(0, XNArea, &pre_area, NULL); |
| 1680 | } |
| 1681 | else if (input_style & XIMPreeditPosition) // over-the-spot |
| 1682 | { |
| 1683 | pre_area.x = 0; |
| 1684 | pre_area.y = 0; |
| 1685 | pre_area.height = gui.char_height * Rows; |
| 1686 | pre_area.width = gui.char_width * Columns; |
| 1687 | preedit_list = XVaCreateNestedList(0, XNArea, &pre_area, NULL); |
| 1688 | } |
| 1689 | |
| 1690 | if (preedit_list && status_list) |
| 1691 | list = XVaCreateNestedList(0, XNPreeditAttributes, preedit_list, |
| 1692 | XNStatusAttributes, status_list, NULL); |
| 1693 | else if (preedit_list) |
| 1694 | list = XVaCreateNestedList(0, XNPreeditAttributes, preedit_list, |
| 1695 | NULL); |
| 1696 | else if (status_list) |
| 1697 | list = XVaCreateNestedList(0, XNStatusAttributes, status_list, |
| 1698 | NULL); |
| 1699 | else |
| 1700 | list = NULL; |
| 1701 | |
| 1702 | if (list) |
| 1703 | { |
| 1704 | XSetICValues(xic, XNVaNestedList, list, NULL); |
| 1705 | XFree(list); |
| 1706 | } |
| 1707 | if (status_list) |
| 1708 | XFree(status_list); |
| 1709 | if (preedit_list) |
| 1710 | XFree(preedit_list); |
| 1711 | } |
| 1712 | |
| 1713 | int |
| 1714 | xim_get_status_area_height(void) |
| 1715 | { |
| 1716 | if (status_area_enabled) |
| 1717 | return gui.char_height; |
| 1718 | return 0; |
| 1719 | } |
| 1720 | # endif |
| 1721 | |
| 1722 | #else // !defined(FEAT_XIM) |
| 1723 | |
| 1724 | # if defined(IME_WITHOUT_XIM) || defined(VIMDLL) || defined(PROTO) |
| 1725 | static int im_was_set_active = FALSE; |
| 1726 | |
| 1727 | int |
| 1728 | # ifdef VIMDLL |
| 1729 | mbyte_im_get_status(void) |
| 1730 | # else |
| 1731 | im_get_status(void) |
| 1732 | # endif |
| 1733 | { |
| 1734 | # if defined(FEAT_EVAL) |
| 1735 | if (USE_IMSTATUSFUNC) |
| 1736 | return call_imstatusfunc(); |
| 1737 | # endif |
| 1738 | return im_was_set_active; |
| 1739 | } |
| 1740 | |
| 1741 | void |
| 1742 | # ifdef VIMDLL |
| 1743 | mbyte_im_set_active(int active_arg) |
| 1744 | # else |
| 1745 | im_set_active(int active_arg) |
| 1746 | # endif |
| 1747 | { |
| 1748 | # if defined(FEAT_EVAL) |
| 1749 | int active = !p_imdisable && active_arg; |
| 1750 | |
| 1751 | if (USE_IMACTIVATEFUNC && active != im_get_status()) |
| 1752 | { |
| 1753 | call_imactivatefunc(active); |
| 1754 | im_was_set_active = active; |
| 1755 | } |
| 1756 | # endif |
| 1757 | } |
| 1758 | |
| 1759 | # if defined(FEAT_GUI) && !defined(FEAT_GUI_HAIKU) && !defined(VIMDLL) |
| 1760 | void |
| 1761 | im_set_position(int row UNUSED, int col UNUSED) |
| 1762 | { |
| 1763 | } |
| 1764 | # endif |
| 1765 | # endif |
| 1766 | |
| 1767 | #endif // FEAT_XIM |