diff options
Diffstat (limited to 'src/gui_x11.c')
-rw-r--r-- | src/gui_x11.c | 3748 |
1 files changed, 3748 insertions, 0 deletions
diff --git a/src/gui_x11.c b/src/gui_x11.c new file mode 100644 index 0000000000..7087e0208f --- /dev/null +++ b/src/gui_x11.c @@ -0,0 +1,3748 @@ +/* vi:set ts=8 sts=4 sw=4: + * + * VIM - Vi IMproved by Bram Moolenaar + * GUI/Motif support by Robert Webb + * + * Do ":help uganda" in Vim to read copying and usage conditions. + * Do ":help credits" in Vim to see a list of people who contributed. + * See README.txt for an overview of the Vim source code. + */ +/* + * Common code for the Motif and Athena GUI. + * Not used for GTK. + */ + +#include <X11/keysym.h> +#include <X11/Xatom.h> +#include <X11/StringDefs.h> +#include <X11/Intrinsic.h> +#include <X11/Shell.h> +#include <X11/cursorfont.h> + +#include "vim.h" + +/* + * For Workshop XpmP.h is preferred, because it makes the signs drawn with a + * transparent background instead of black. + */ +#if defined(HAVE_XM_XPMP_H) && defined(FEAT_GUI_MOTIF) \ + && (!defined(HAVE_X11_XPM_H) || defined(FEAT_SUN_WORKSHOP)) +# include <Xm/XpmP.h> +#else +# ifdef HAVE_X11_XPM_H +# include <X11/xpm.h> +# endif +#endif + +#ifdef FEAT_XFONTSET +# ifdef X_LOCALE +# include <X11/Xlocale.h> +# else +# include <locale.h> +# endif +#endif + +#ifdef HAVE_X11_SUNKEYSYM_H +# include <X11/Sunkeysym.h> +#endif + +#ifdef HAVE_X11_XMU_EDITRES_H +# include <X11/Xmu/Editres.h> +#endif + +#ifdef FEAT_BEVAL_TIP +# include "gui_beval.h" +#endif + +#define VIM_NAME "vim" +#define VIM_CLASS "Vim" + +/* Default resource values */ +#define DFLT_FONT "7x13" +#ifdef FONTSET_ALWAYS +# define DFLT_MENU_FONT XtDefaultFontSet +#else +# define DFLT_MENU_FONT XtDefaultFont +#endif +#define DFLT_TOOLTIP_FONT XtDefaultFontSet + +#ifdef FEAT_GUI_ATHENA +# define DFLT_MENU_BG_COLOR "gray77" +# define DFLT_MENU_FG_COLOR "black" +# define DFLT_SCROLL_BG_COLOR "gray60" +# define DFLT_SCROLL_FG_COLOR "gray77" +# define DFLT_TOOLTIP_BG_COLOR "#ffffffff9191" +# define DFLT_TOOLTIP_FG_COLOR "#000000000000" +#else +/* use the default (CDE) colors */ +# define DFLT_MENU_BG_COLOR "" +# define DFLT_MENU_FG_COLOR "" +# define DFLT_SCROLL_BG_COLOR "" +# define DFLT_SCROLL_FG_COLOR "" +# define DFLT_TOOLTIP_BG_COLOR "#ffffffff9191" +# define DFLT_TOOLTIP_FG_COLOR "#000000000000" +#endif + +Widget vimShell = (Widget)0; + +static Atom wm_atoms[2]; /* Window Manager Atoms */ +#define DELETE_WINDOW_IDX 0 /* index in wm_atoms[] for WM_DELETE_WINDOW */ +#define SAVE_YOURSELF_IDX 1 /* index in wm_atoms[] for WM_SAVE_YOURSELF */ + +#ifdef FEAT_XFONTSET +/* + * We either draw with a fontset (when current_fontset != NULL) or with a + * normal font (current_fontset == NULL, use gui.text_gc and gui.back_gc). + */ +static XFontSet current_fontset = NULL; + +#define XDrawString(dpy, win, gc, x, y, str, n) \ + do \ + { \ + if (current_fontset != NULL) \ + XmbDrawString(dpy, win, current_fontset, gc, x, y, str, n); \ + else \ + XDrawString(dpy, win, gc, x, y, str, n); \ + } while (0) + +#define XDrawString16(dpy, win, gc, x, y, str, n) \ + do \ + { \ + if (current_fontset != NULL) \ + XwcDrawString(dpy, win, current_fontset, gc, x, y, (wchar_t *)str, n); \ + else \ + XDrawString16(dpy, win, gc, x, y, str, n); \ + } while (0) + +static int check_fontset_sanity __ARGS((XFontSet fs)); +static int fontset_width __ARGS((XFontSet fs)); +static int fontset_ascent __ARGS((XFontSet fs)); +#endif + +static guicolor_T prev_fg_color = INVALCOLOR; +static guicolor_T prev_bg_color = INVALCOLOR; + +#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) +static XButtonPressedEvent last_mouse_event; +#endif + +static int find_closest_color __ARGS((Colormap colormap, XColor *colorPtr)); +static void gui_x11_timer_cb __ARGS((XtPointer timed_out, XtIntervalId *interval_id)); +static void gui_x11_visibility_cb __ARGS((Widget w, XtPointer dud, XEvent *event, Boolean *dum)); +static void gui_x11_expose_cb __ARGS((Widget w, XtPointer dud, XEvent *event, Boolean *dum)); +static void gui_x11_resize_window_cb __ARGS((Widget w, XtPointer dud, XEvent *event, Boolean *dum)); +static void gui_x11_focus_change_cb __ARGS((Widget w, XtPointer data, XEvent *event, Boolean *dum)); +static void gui_x11_enter_cb __ARGS((Widget w, XtPointer data, XEvent *event, Boolean *dum)); +static void gui_x11_leave_cb __ARGS((Widget w, XtPointer data, XEvent *event, Boolean *dum)); +static void gui_x11_mouse_cb __ARGS((Widget w, XtPointer data, XEvent *event, Boolean *dum)); +#ifdef FEAT_SNIFF +static void gui_x11_sniff_request_cb __ARGS((XtPointer closure, int *source, XtInputId *id)); +#endif +static void gui_x11_check_copy_area __ARGS((void)); +#ifdef FEAT_CLIENTSERVER +static void gui_x11_send_event_handler __ARGS((Widget, XtPointer, XEvent *, Boolean *)); +#endif +static void gui_x11_wm_protocol_handler __ARGS((Widget, XtPointer, XEvent *, Boolean *)); +static void gui_x11_blink_cb __ARGS((XtPointer timed_out, XtIntervalId *interval_id)); +static Cursor gui_x11_create_blank_mouse __ARGS((void)); + + +/* + * Keycodes recognized by vim. + * NOTE: when changing this, the table in gui_gtk_x11.c probably needs the + * same change! + */ +static struct specialkey +{ + KeySym key_sym; + char_u vim_code0; + char_u vim_code1; +} special_keys[] = +{ + {XK_Up, 'k', 'u'}, + {XK_Down, 'k', 'd'}, + {XK_Left, 'k', 'l'}, + {XK_Right, 'k', 'r'}, + + {XK_F1, 'k', '1'}, + {XK_F2, 'k', '2'}, + {XK_F3, 'k', '3'}, + {XK_F4, 'k', '4'}, + {XK_F5, 'k', '5'}, + {XK_F6, 'k', '6'}, + {XK_F7, 'k', '7'}, + {XK_F8, 'k', '8'}, + {XK_F9, 'k', '9'}, + {XK_F10, 'k', ';'}, + + {XK_F11, 'F', '1'}, + {XK_F12, 'F', '2'}, + {XK_F13, 'F', '3'}, + {XK_F14, 'F', '4'}, + {XK_F15, 'F', '5'}, + {XK_F16, 'F', '6'}, + {XK_F17, 'F', '7'}, + {XK_F18, 'F', '8'}, + {XK_F19, 'F', '9'}, + {XK_F20, 'F', 'A'}, + + {XK_F21, 'F', 'B'}, + {XK_F22, 'F', 'C'}, + {XK_F23, 'F', 'D'}, + {XK_F24, 'F', 'E'}, + {XK_F25, 'F', 'F'}, + {XK_F26, 'F', 'G'}, + {XK_F27, 'F', 'H'}, + {XK_F28, 'F', 'I'}, + {XK_F29, 'F', 'J'}, + {XK_F30, 'F', 'K'}, + + {XK_F31, 'F', 'L'}, + {XK_F32, 'F', 'M'}, + {XK_F33, 'F', 'N'}, + {XK_F34, 'F', 'O'}, + {XK_F35, 'F', 'P'}, /* keysymdef.h defines up to F35 */ +#ifdef SunXK_F36 + {SunXK_F36, 'F', 'Q'}, + {SunXK_F37, 'F', 'R'}, +#endif + + {XK_Help, '%', '1'}, + {XK_Undo, '&', '8'}, + {XK_BackSpace, 'k', 'b'}, + {XK_Insert, 'k', 'I'}, + {XK_Delete, 'k', 'D'}, + {XK_Home, 'k', 'h'}, + {XK_End, '@', '7'}, + {XK_Prior, 'k', 'P'}, + {XK_Next, 'k', 'N'}, + {XK_Print, '%', '9'}, + + /* Keypad keys: */ +#ifdef XK_KP_Left + {XK_KP_Left, 'k', 'l'}, + {XK_KP_Right, 'k', 'r'}, + {XK_KP_Up, 'k', 'u'}, + {XK_KP_Down, 'k', 'd'}, + {XK_KP_Insert, KS_EXTRA, (char_u)KE_KINS}, + {XK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL}, + {XK_KP_Home, 'K', '1'}, + {XK_KP_End, 'K', '4'}, + {XK_KP_Prior, 'K', '3'}, + {XK_KP_Next, 'K', '5'}, + + {XK_KP_Add, 'K', '6'}, + {XK_KP_Subtract, 'K', '7'}, + {XK_KP_Divide, 'K', '8'}, + {XK_KP_Multiply, 'K', '9'}, + {XK_KP_Enter, 'K', 'A'}, + {XK_KP_Decimal, 'K', 'B'}, + + {XK_KP_0, 'K', 'C'}, + {XK_KP_1, 'K', 'D'}, + {XK_KP_2, 'K', 'E'}, + {XK_KP_3, 'K', 'F'}, + {XK_KP_4, 'K', 'G'}, + {XK_KP_5, 'K', 'H'}, + {XK_KP_6, 'K', 'I'}, + {XK_KP_7, 'K', 'J'}, + {XK_KP_8, 'K', 'K'}, + {XK_KP_9, 'K', 'L'}, +#endif + + /* End of list marker: */ + {(KeySym)0, 0, 0} +}; + +#define XtNboldFont "boldFont" +#define XtCBoldFont "BoldFont" +#define XtNitalicFont "italicFont" +#define XtCItalicFont "ItalicFont" +#define XtNboldItalicFont "boldItalicFont" +#define XtCBoldItalicFont "BoldItalicFont" +#define XtNscrollbarWidth "scrollbarWidth" +#define XtCScrollbarWidth "ScrollbarWidth" +#define XtNmenuHeight "menuHeight" +#define XtCMenuHeight "MenuHeight" +#define XtNmenuFont "menuFont" +#define XtCMenuFont "MenuFont" +#define XtNmenuFontSet "menuFontSet" +#define XtCMenuFontSet "MenuFontSet" + + +/* Resources for setting the foreground and background colors of menus */ +#define XtNmenuBackground "menuBackground" +#define XtCMenuBackground "MenuBackground" +#define XtNmenuForeground "menuForeground" +#define XtCMenuForeground "MenuForeground" + +/* Resources for setting the foreground and background colors of scrollbars */ +#define XtNscrollBackground "scrollBackground" +#define XtCScrollBackground "ScrollBackground" +#define XtNscrollForeground "scrollForeground" +#define XtCScrollForeground "ScrollForeground" + +/* Resources for setting the foreground and background colors of tooltip */ +#define XtNtooltipBackground "tooltipBackground" +#define XtCTooltipBackground "TooltipBackground" +#define XtNtooltipForeground "tooltipForeground" +#define XtCTooltipForeground "TooltipForeground" +#define XtNtooltipFont "tooltipFont" +#define XtCTooltipFont "TooltipFont" + +/* + * X Resources: + */ +static XtResource vim_resources[] = +{ + { + XtNforeground, + XtCForeground, + XtRPixel, + sizeof(Pixel), + XtOffsetOf(gui_T, def_norm_pixel), + XtRString, + XtDefaultForeground + }, + { + XtNbackground, + XtCBackground, + XtRPixel, + sizeof(Pixel), + XtOffsetOf(gui_T, def_back_pixel), + XtRString, + XtDefaultBackground + }, + { + XtNfont, + XtCFont, + XtRString, + sizeof(String *), + XtOffsetOf(gui_T, rsrc_font_name), + XtRImmediate, + XtDefaultFont + }, + { + XtNboldFont, + XtCBoldFont, + XtRString, + sizeof(String *), + XtOffsetOf(gui_T, rsrc_bold_font_name), + XtRImmediate, + "" + }, + { + XtNitalicFont, + XtCItalicFont, + XtRString, + sizeof(String *), + XtOffsetOf(gui_T, rsrc_ital_font_name), + XtRImmediate, + "" + }, + { + XtNboldItalicFont, + XtCBoldItalicFont, + XtRString, + sizeof(String *), + XtOffsetOf(gui_T, rsrc_boldital_font_name), + XtRImmediate, + "" + }, + { + XtNgeometry, + XtCGeometry, + XtRString, + sizeof(String *), + XtOffsetOf(gui_T, geom), + XtRImmediate, + "" + }, + { + XtNreverseVideo, + XtCReverseVideo, + XtRBool, + sizeof(Bool), + XtOffsetOf(gui_T, rsrc_rev_video), + XtRImmediate, + (XtPointer)False + }, + { + XtNborderWidth, + XtCBorderWidth, + XtRInt, + sizeof(int), + XtOffsetOf(gui_T, border_width), + XtRImmediate, + (XtPointer)2 + }, + { + XtNscrollbarWidth, + XtCScrollbarWidth, + XtRInt, + sizeof(int), + XtOffsetOf(gui_T, scrollbar_width), + XtRImmediate, + (XtPointer)SB_DEFAULT_WIDTH + }, +#ifdef FEAT_MENU +# ifdef FEAT_GUI_ATHENA /* with Motif the height is always computed */ + { + XtNmenuHeight, + XtCMenuHeight, + XtRInt, + sizeof(int), + XtOffsetOf(gui_T, menu_height), + XtRImmediate, + (XtPointer)MENU_DEFAULT_HEIGHT /* Should figure out at run time */ + }, +# endif + { +# ifdef FONTSET_ALWAYS + XtNmenuFontSet, + XtCMenuFontSet, +#else + XtNmenuFont, + XtCMenuFont, +#endif + XtRString, + sizeof(char *), + XtOffsetOf(gui_T, rsrc_menu_font_name), + XtRString, + DFLT_MENU_FONT + }, +#endif + { + XtNmenuForeground, + XtCMenuForeground, + XtRString, + sizeof(char *), + XtOffsetOf(gui_T, rsrc_menu_fg_name), + XtRString, + DFLT_MENU_FG_COLOR + }, + { + XtNmenuBackground, + XtCMenuBackground, + XtRString, + sizeof(char *), + XtOffsetOf(gui_T, rsrc_menu_bg_name), + XtRString, + DFLT_MENU_BG_COLOR + }, + { + XtNscrollForeground, + XtCScrollForeground, + XtRString, + sizeof(char *), + XtOffsetOf(gui_T, rsrc_scroll_fg_name), + XtRString, + DFLT_SCROLL_FG_COLOR + }, + { + XtNscrollBackground, + XtCScrollBackground, + XtRString, + sizeof(char *), + XtOffsetOf(gui_T, rsrc_scroll_bg_name), + XtRString, + DFLT_SCROLL_BG_COLOR + }, +#ifdef FEAT_BEVAL + { + XtNtooltipForeground, + XtCTooltipForeground, + XtRString, + sizeof(char *), + XtOffsetOf(gui_T, rsrc_tooltip_fg_name), + XtRString, + DFLT_TOOLTIP_FG_COLOR + }, + { + XtNtooltipBackground, + XtCTooltipBackground, + XtRString, + sizeof(char *), + XtOffsetOf(gui_T, rsrc_tooltip_bg_name), + XtRString, + DFLT_TOOLTIP_BG_COLOR + }, + { + XtNtooltipFont, + XtCTooltipFont, + XtRString, + sizeof(char *), + XtOffsetOf(gui_T, rsrc_tooltip_font_name), + XtRString, + DFLT_TOOLTIP_FONT + }, + /* This one isn't really needed, keep for Sun Workshop? */ + { + "balloonEvalFontSet", + XtCFontSet, + XtRFontSet, + sizeof(XFontSet), + XtOffsetOf(gui_T, tooltip_fontset), + XtRImmediate, + (XtPointer)NOFONTSET + }, +#endif /* FEAT_BEVAL */ +#ifdef FEAT_XIM + { + "preeditType", + "PreeditType", + XtRString, + sizeof(char*), + XtOffsetOf(gui_T, rsrc_preedit_type_name), + XtRString, + (XtPointer)"OverTheSpot,OffTheSpot,Root" + }, + { + "inputMethod", + "InputMethod", + XtRString, + sizeof(char*), + XtOffsetOf(gui_T, rsrc_input_method), + XtRString, + NULL + }, +#endif /* FEAT_XIM */ +}; + +/* + * This table holds all the X GUI command line options allowed. This includes + * the standard ones so that we can skip them when vim is started without the + * GUI (but the GUI might start up later). + * When changing this, also update doc/vim_gui.txt and the usage message!!! + */ +static XrmOptionDescRec cmdline_options[] = +{ + /* We handle these options ourselves */ + {"-bg", ".background", XrmoptionSepArg, NULL}, + {"-background", ".background", XrmoptionSepArg, NULL}, + {"-fg", ".foreground", XrmoptionSepArg, NULL}, + {"-foreground", ".foreground", XrmoptionSepArg, NULL}, + {"-fn", ".font", XrmoptionSepArg, NULL}, + {"-font", ".font", XrmoptionSepArg, NULL}, + {"-boldfont", ".boldFont", XrmoptionSepArg, NULL}, + {"-italicfont", ".italicFont", XrmoptionSepArg, NULL}, + {"-geom", ".geometry", XrmoptionSepArg, NULL}, + {"-geometry", ".geometry", XrmoptionSepArg, NULL}, + {"-reverse", "*reverseVideo", XrmoptionNoArg, "True"}, + {"-rv", "*reverseVideo", XrmoptionNoArg, "True"}, + {"+reverse", "*reverseVideo", XrmoptionNoArg, "False"}, + {"+rv", "*reverseVideo", XrmoptionNoArg, "False"}, + {"-display", ".display", XrmoptionSepArg, NULL}, + {"-iconic", "*iconic", XrmoptionNoArg, "True"}, + {"-name", ".name", XrmoptionSepArg, NULL}, + {"-bw", ".borderWidth", XrmoptionSepArg, NULL}, + {"-borderwidth", ".borderWidth", XrmoptionSepArg, NULL}, + {"-sw", ".scrollbarWidth", XrmoptionSepArg, NULL}, + {"-scrollbarwidth", ".scrollbarWidth", XrmoptionSepArg, NULL}, + {"-mh", ".menuHeight", XrmoptionSepArg, NULL}, + {"-menuheight", ".menuHeight", XrmoptionSepArg, NULL}, +#ifdef FONTSET_ALWAYS + {"-mf", ".menuFontSet", XrmoptionSepArg, NULL}, + {"-menufont", ".menuFontSet", XrmoptionSepArg, NULL}, + {"-menufontset", ".menuFontSet", XrmoptionSepArg, NULL}, +#else + {"-mf", ".menuFont", XrmoptionSepArg, NULL}, + {"-menufont", ".menuFont", XrmoptionSepArg, NULL}, +#endif + {"-xrm", NULL, XrmoptionResArg, NULL} +}; + +static int gui_argc = 0; +static char **gui_argv = NULL; + +/* + * Call-back routines. + */ + +/* ARGSUSED */ + static void +gui_x11_timer_cb(timed_out, interval_id) + XtPointer timed_out; + XtIntervalId *interval_id; +{ + *((int *)timed_out) = TRUE; +} + +/* ARGSUSED */ + static void +gui_x11_visibility_cb(w, dud, event, dum) + Widget w; + XtPointer dud; + XEvent *event; + Boolean *dum; +{ + if (event->type != VisibilityNotify) + return; + + gui.visibility = event->xvisibility.state; + + /* + * When we do an XCopyArea(), and the window is partially obscured, we want + * to receive an event to tell us whether it worked or not. + */ + XSetGraphicsExposures(gui.dpy, gui.text_gc, + gui.visibility != VisibilityUnobscured); + + /* This is needed for when redrawing is slow. */ + gui_mch_update(); +} + +/* ARGSUSED */ + static void +gui_x11_expose_cb(w, dud, event, dum) + Widget w; + XtPointer dud; + XEvent *event; + Boolean *dum; +{ + XExposeEvent *gevent; + int new_x; + + if (event->type != Expose) + return; + + out_flush(); /* make sure all output has been processed */ + + gevent = (XExposeEvent *)event; + gui_redraw(gevent->x, gevent->y, gevent->width, gevent->height); + + new_x = FILL_X(0); + + /* Clear the border areas if needed */ + if (gevent->x < new_x) + XClearArea(gui.dpy, gui.wid, 0, 0, new_x, 0, False); + if (gevent->y < FILL_Y(0)) + XClearArea(gui.dpy, gui.wid, 0, 0, 0, FILL_Y(0), False); + if (gevent->x > FILL_X(Columns)) + XClearArea(gui.dpy, gui.wid, FILL_X((int)Columns), 0, 0, 0, False); + if (gevent->y > FILL_Y(Rows)) + XClearArea(gui.dpy, gui.wid, 0, FILL_Y((int)Rows), 0, 0, False); + + /* This is needed for when redrawing is slow. */ + gui_mch_update(); +} + +#if (defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)) \ + || defined(PROTO) +/* + * This function fills in the XRectangle object with the current + * x,y coordinates and height, width so that an XtVaSetValues to + * the same shell of those resources will restore the window to its + * formar position and dimensions. + * + * Note: This function may fail, in which case the XRectangle will + * be unchanged. Be sure to have the XRectangle set with the + * proper values for a failed condition prior to calling this + * function. + */ + static void +shellRectangle(Widget shell, XRectangle *r) +{ + Window rootw, shellw, child, parentw; + int absx, absy; + XWindowAttributes a; + Window *children; + unsigned int childrenCount; + + shellw = XtWindow(shell); + if (shellw == 0) + return; + for (;;) + { + XQueryTree(XtDisplay(shell), shellw, &rootw, &parentw, + &children, &childrenCount); + XFree(children); + if (parentw == rootw) + break; + shellw = parentw; + } + XGetWindowAttributes(XtDisplay(shell), shellw, &a); + XTranslateCoordinates(XtDisplay(shell), shellw, a.root, 0, 0, + &absx, &absy, &child); + r->x = absx; + r->y = absy; + XtVaGetValues(shell, XmNheight, &r->height, XmNwidth, &r->width, NULL); +} +#endif + +/* ARGSUSED */ + static void +gui_x11_resize_window_cb(w, dud, event, dum) + Widget w; + XtPointer dud; + XEvent *event; + Boolean *dum; +{ + static int lastWidth, lastHeight; + + if (event->type != ConfigureNotify) + return; + + if (event->xconfigure.width != lastWidth + || event->xconfigure.height != lastHeight) + { + lastWidth = event->xconfigure.width; + lastHeight = event->xconfigure.height; + gui_resize_shell(event->xconfigure.width, event->xconfigure.height +#ifdef FEAT_XIM + - xim_get_status_area_height() +#endif + ); + } +#ifdef FEAT_SUN_WORKSHOP + if (usingSunWorkShop) + { + XRectangle rec; + + shellRectangle(w, &rec); + workshop_frame_moved(rec.x, rec.y, rec.width, rec.height); + } +#endif +#ifdef FEAT_NETBEANS_INTG + if (usingNetbeans) + { + XRectangle rec; + + shellRectangle(w, &rec); + netbeans_frame_moved(rec.x, rec.y); + } +#endif +#ifdef FEAT_XIM + xim_set_preedit(); +#endif +} + +/* ARGSUSED */ + static void +gui_x11_focus_change_cb(w, data, event, dum) + Widget w; + XtPointer data; + XEvent *event; + Boolean *dum; +{ + gui_focus_change(event->type == FocusIn); +} + +/* ARGSUSED */ + static void +gui_x11_enter_cb(w, data, event, dum) + Widget w; + XtPointer data; + XEvent *event; + Boolean *dum; +{ + gui_focus_change(TRUE); +} + +/* ARGSUSED */ + static void +gui_x11_leave_cb(w, data, event, dum) + Widget w; + XtPointer data; + XEvent *event; + Boolean *dum; +{ + gui_focus_change(FALSE); +} + +#if defined(X_HAVE_UTF8_STRING) && defined(FEAT_MBYTE) +# if X_HAVE_UTF8_STRING +# define USE_UTF8LOOKUP +# endif +#endif + +/* ARGSUSED */ + void +gui_x11_key_hit_cb(w, dud, event, dum) + Widget w; + XtPointer dud; + XEvent *event; + Boolean *dum; +{ + XKeyPressedEvent *ev_press; +#ifdef FEAT_XIM + char_u string2[256]; + char_u string_shortbuf[256]; + char_u *string = string_shortbuf; + Boolean string_alloced = False; + Status status; +#else + char_u string[4], string2[3]; +#endif + KeySym key_sym, key_sym2; + int len, len2; + int i; + int modifiers; + int key; + + ev_press = (XKeyPressedEvent *)event; + +#ifdef FEAT_XIM + if (xic) + { +# ifdef USE_UTF8LOOKUP + /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even when + * the locale isn't utf-8. */ + if (enc_utf8) + len = Xutf8LookupString(xic, ev_press, (char *)string, + sizeof(string_shortbuf), &key_sym, &status); + else +# endif + len = XmbLookupString(xic, ev_press, (char *)string, + sizeof(string_shortbuf), &key_sym, &status); + if (status == XBufferOverflow) + { + string = (char_u *)XtMalloc(len + 1); + string_alloced = True; +# ifdef USE_UTF8LOOKUP + /* XFree86 4.0.2 or newer: Be able to get UTF-8 characters even + * when the locale isn't utf-8. */ + if (enc_utf8) + len = Xutf8LookupString(xic, ev_press, (char *)string, + len, &key_sym, &status); + else +# endif + len = XmbLookupString(xic, ev_press, (char *)string, + len, &key_sym, &status); + } + if (status == XLookupNone || status == XLookupChars) + key_sym = XK_VoidSymbol; + +# ifdef FEAT_MBYTE + /* Do conversion from 'termencoding' to 'encoding'. When using + * Xutf8LookupString() it has already been done. */ + if (len > 0 && input_conv.vc_type != CONV_NONE +# ifdef USE_UTF8LOOKUP + && !enc_utf8 +# endif + ) + { + int maxlen = len * 4 + 40; /* guessed */ + char_u *p = (char_u *)XtMalloc(maxlen); + + mch_memmove(p, string, len); + if (string_alloced) + XtFree((char *)string); + string = p; + string_alloced = True; + len = convert_input(p, len, maxlen); + } +# endif + + /* Translate CSI to K_CSI, otherwise it could be recognized as the + * start of a special key. */ + for (i = 0; i < len; ++i) + if (string[i] == CSI) + { + char_u *p = (char_u *)XtMalloc(len + 3); + + mch_memmove(p, string, i + 1); + p[i + 1] = KS_EXTRA; + p[i + 2] = (int)KE_CSI; + mch_memmove(p + i + 3, string + i + 1, len - i); + if (string_alloced) + XtFree((char *)string); + string = p; + string_alloced = True; + i += 2; + len += 2; + } + } + else +#endif + len = XLookupString(ev_press, (char *)string, sizeof(string), + &key_sym, NULL); + +#ifdef SunXK_F36 + /* + * These keys have bogus lookup strings, and trapping them here is + * easier than trying to XRebindKeysym() on them with every possible + * combination of modifiers. + */ + if (key_sym == SunXK_F36 || key_sym == SunXK_F37) + len = 0; +#endif + +#ifdef FEAT_HANGULIN + if ((key_sym == XK_space) && (ev_press->state & ShiftMask)) + { + hangul_input_state_toggle(); + goto theend; + } +#endif + + if (key_sym == XK_space) + string[0] = ' '; /* Otherwise Ctrl-Space doesn't work */ + + /* + * Only on some machines ^_ requires Ctrl+Shift+minus. For consistency, + * allow just Ctrl+minus too. + */ + if (key_sym == XK_minus && (ev_press->state & ControlMask)) + string[0] = Ctrl__; + +#ifdef XK_ISO_Left_Tab + /* why do we get XK_ISO_Left_Tab instead of XK_Tab for shift-tab? */ + if (key_sym == XK_ISO_Left_Tab) + { + key_sym = XK_Tab; + string[0] = TAB; + len = 1; + } +#endif + + /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character + * that already has the 8th bit set. And not when using a double-byte + * encoding, setting the 8th bit may make it the lead byte of a + * double-byte character. */ + if (len == 1 + && (ev_press->state & Mod1Mask) + && !(key_sym == XK_BackSpace || key_sym == XK_Delete) + && (string[0] & 0x80) == 0 +#ifdef FEAT_MBYTE + && !enc_dbcs +#endif + ) + { +#if defined(FEAT_MENU) && defined(FEAT_GUI_MOTIF) + /* Ignore ALT keys when they are used for the menu only */ + if (gui.menu_is_active + && (p_wak[0] == 'y' + || (p_wak[0] == 'm' && gui_is_menu_shortcut(string[0])))) + goto theend; +#endif + /* + * Before we set the 8th bit, check to make sure the user doesn't + * already have a mapping defined for this sequence. We determine this + * by checking to see if the input would be the same without the + * Alt/Meta key. + * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT. + */ + ev_press->state &= ~Mod1Mask; + len2 = XLookupString(ev_press, (char *)string2, sizeof(string2), + &key_sym2, NULL); + if (key_sym2 == XK_space) + string2[0] = ' '; /* Otherwise Meta-Ctrl-Space doesn't work */ + if ( len2 == 1 + && string[0] == string2[0] + && !(key_sym == XK_Tab && (ev_press->state & ShiftMask))) + { + string[0] |= 0x80; +#ifdef FEAT_MBYTE + if (enc_utf8) /* convert to utf-8 */ + { + string[1] = string[0] & 0xbf; + string[0] = ((unsigned)string[0] >> 6) + 0xc0; + if (string[1] == CSI) + { + string[2] = KS_EXTRA; + string[3] = (int)KE_CSI; + len = 4; + } + else + len = 2; + } +#endif + } + else + ev_press->state |= Mod1Mask; + } + + if (len == 1 && string[0] == CSI) + { + string[1] = KS_EXTRA; + string[2] = (int)KE_CSI; + len = -3; + } + + /* Check for special keys. Also do this when len == 1 (key has an ASCII + * value) to detect backspace, delete and keypad keys. */ + if (len == 0 || len == 1) + { + for (i = 0; special_keys[i].key_sym != (KeySym)0; i++) + { + if (special_keys[i].key_sym == key_sym) + { + string[0] = CSI; + string[1] = special_keys[i].vim_code0; + string[2] = special_keys[i].vim_code1; + len = -3; + break; + } + } + } + + /* Unrecognised key is ignored. */ + if (len == 0) + goto theend; + + /* Special keys (and a few others) may have modifiers. Also when using a + * double-byte encoding (can't set the 8th bit). */ + if (len == -3 || key_sym == XK_space || key_sym == XK_Tab + || key_sym == XK_Return || key_sym == XK_Linefeed + || key_sym == XK_Escape +#ifdef FEAT_MBYTE + || (enc_dbcs && len == 1 && (ev_press->state & Mod1Mask)) +#endif + ) + { + modifiers = 0; + if (ev_press->state & ShiftMask) + modifiers |= MOD_MASK_SHIFT; + if (ev_press->state & ControlMask) + modifiers |= MOD_MASK_CTRL; + if (ev_press->state & Mod1Mask) + modifiers |= MOD_MASK_ALT; + + /* + * For some keys a shift modifier is translated into another key + * code. + */ + if (len == -3) + key = TO_SPECIAL(string[1], string[2]); + else + key = string[0]; + key = simplify_key(key, &modifiers); + if (key == CSI) + key = K_CSI; + if (IS_SPECIAL(key)) + { + string[0] = CSI; + string[1] = K_SECOND(key); + string[2] = K_THIRD(key); + len = 3; + } + else + { + string[0] = key; + len = 1; + } + + if (modifiers != 0) + { + string2[0] = CSI; + string2[1] = KS_MODIFIER; + string2[2] = modifiers; + add_to_input_buf(string2, 3); + } + } + + if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts) +#ifdef UNIX + || (intr_char != 0 && string[0] == intr_char) +#endif + )) + { + trash_input_buf(); + got_int = TRUE; + } + + add_to_input_buf(string, len); + + /* + * blank out the pointer if necessary + */ + if (p_mh) + gui_mch_mousehide(TRUE); + +#if defined(FEAT_BEVAL_TIP) + { + BalloonEval *be; + + if ((be = gui_mch_currently_showing_beval()) != NULL) + gui_mch_unpost_balloon(be); + } +#endif +theend: + {} /* some compilers need a statement here */ +#ifdef FEAT_XIM + if (string_alloced) + XtFree((char *)string); +#endif +} + +/* ARGSUSED */ + static void +gui_x11_mouse_cb(w, dud, event, dum) + Widget w; + XtPointer dud; + XEvent *event; + Boolean *dum; +{ + static XtIntervalId timer = (XtIntervalId)0; + static int timed_out = TRUE; + + int button; + int repeated_click = FALSE; + int x, y; + int_u x_modifiers; + int_u vim_modifiers; + + if (event->type == MotionNotify) + { + /* Get the latest position, avoids lagging behind on a drag. */ + x = event->xmotion.x; + y = event->xmotion.y; + x_modifiers = event->xmotion.state; + button = (x_modifiers & (Button1Mask | Button2Mask | Button3Mask)) + ? MOUSE_DRAG : ' '; + + /* + * if our pointer is currently hidden, then we should show it. + */ + gui_mch_mousehide(FALSE); + + if (button != MOUSE_DRAG) /* just moving the rodent */ + { +#ifdef FEAT_MENU + if (dud) /* moved in vimForm */ + y -= gui.menu_height; +#endif + gui_mouse_moved(x, y); + return; + } + } + else + { + x = event->xbutton.x; + y = event->xbutton.y; + if (event->type == ButtonPress) + { + /* Handle multiple clicks */ + if (!timed_out) + { + XtRemoveTimeOut(timer); + repeated_click = TRUE; + } + timed_out = FALSE; + timer = XtAppAddTimeOut(app_context, (long_u)p_mouset, + gui_x11_timer_cb, &timed_out); + switch (event->xbutton.button) + { + case Button1: button = MOUSE_LEFT; break; + case Button2: button = MOUSE_MIDDLE; break; + case Button3: button = MOUSE_RIGHT; break; + case Button4: button = MOUSE_4; break; + case Button5: button = MOUSE_5; break; + default: + return; /* Unknown button */ + } + } + else if (event->type == ButtonRelease) + button = MOUSE_RELEASE; + else + return; /* Unknown mouse event type */ + + x_modifiers = event->xbutton.state; +#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) + last_mouse_event = event->xbutton; +#endif + } + + vim_modifiers = 0x0; + if (x_modifiers & ShiftMask) + vim_modifiers |= MOUSE_SHIFT; + if (x_modifiers & ControlMask) + vim_modifiers |= MOUSE_CTRL; + if (x_modifiers & Mod1Mask) /* Alt or Meta key */ + vim_modifiers |= MOUSE_ALT; + + gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers); +} + +#ifdef FEAT_SNIFF +/* ARGSUSED */ + static void +gui_x11_sniff_request_cb(closure, source, id) + XtPointer closure; + int *source; + XtInputId *id; +{ + static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF}; + + add_to_input_buf(bytes, 3); +} +#endif + +/* + * End of call-back routines + */ + +/* + * Parse the GUI related command-line arguments. Any arguments used are + * deleted from argv, and *argc is decremented accordingly. This is called + * when vim is started, whether or not the GUI has been started. + */ + void +gui_mch_prepare(argc, argv) + int *argc; + char **argv; +{ + int arg; + int i; + + /* + * Move all the entries in argv which are relevant to X into gui_argv. + */ + gui_argc = 0; + gui_argv = (char **)lalloc((long_u)(*argc * sizeof(char *)), FALSE); + if (gui_argv == NULL) + return; + gui_argv[gui_argc++] = argv[0]; + arg = 1; + while (arg < *argc) + { + /* Look for argv[arg] in cmdline_options[] table */ + for (i = 0; i < XtNumber(cmdline_options); i++) + if (strcmp(argv[arg], cmdline_options[i].option) == 0) + break; + + if (i < XtNumber(cmdline_options)) + { + /* Remember finding "-rv" or "-reverse" */ + if (strcmp("-rv", argv[arg]) == 0 + || strcmp("-reverse", argv[arg]) == 0) + found_reverse_arg = TRUE; + else if ((strcmp("-fn", argv[arg]) == 0 + || strcmp("-font", argv[arg]) == 0) + && arg + 1 < *argc) + font_argument = argv[arg + 1]; + + /* Found match in table, so move it into gui_argv */ + gui_argv[gui_argc++] = argv[arg]; + if (--*argc > arg) + { + mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg) + * sizeof(char *)); + if (cmdline_options[i].argKind != XrmoptionNoArg) + { + /* Move the options argument as well */ + gui_argv[gui_argc++] = argv[arg]; + if (--*argc > arg) + mch_memmove(&argv[arg], &argv[arg + 1], (*argc - arg) + * sizeof(char *)); + } + } + argv[*argc] = NULL; + } + else +#ifdef FEAT_SUN_WORKSHOP + if (strcmp("-ws", argv[arg]) == 0) + { + usingSunWorkShop++; + p_acd = TRUE; + gui.dofork = FALSE; /* don't fork() when starting GUI */ + mch_memmove(&argv[arg], &argv[arg + 1], + (--*argc - arg) * sizeof(char *)); |