xref: /vim-8.2.3635/src/gui.h (revision cf2d8dee)
1 /* vi:set ts=8 sts=4 sw=4:
2  *
3  * VIM - Vi IMproved		by Bram Moolenaar
4  *				Motif support by Robert Webb
5  *
6  * Do ":help uganda"  in Vim to read copying and usage conditions.
7  * Do ":help credits" in Vim to see a list of people who contributed.
8  */
9 
10 #ifdef FEAT_GUI_MOTIF
11 # include <Xm/Xm.h>
12 #endif
13 
14 #ifdef FEAT_GUI_ATHENA
15 # include <X11/Intrinsic.h>
16 # include <X11/StringDefs.h>
17 #endif
18 
19 #ifdef FEAT_BEVAL
20 # include "gui_beval.h"
21 #endif
22 
23 #ifdef FEAT_GUI_GTK
24 # ifdef VMS /* undef MIN and MAX because Intrinsic.h redefines them anyway */
25 #  ifdef MAX
26 #   undef MAX
27 #  endif
28 #  ifdef MIN
29 #   undef MIN
30 #  endif
31 # endif
32 # include <X11/Intrinsic.h>
33 # include <gtk/gtk.h>
34 #endif
35 
36 #ifdef FEAT_GUI_MAC
37 # include <Types.h>
38 /*# include <Memory.h>*/
39 # include <Quickdraw.h>
40 # include <Fonts.h>
41 # include <Events.h>
42 # include <Menus.h>
43 # if !(defined (TARGET_API_MAC_CARBON) && (TARGET_API_MAC_CARBON))
44 #  include <Windows.h>
45 # endif
46 # include <Controls.h>
47 /*# include <TextEdit.h>*/
48 # include <Dialogs.h>
49 # include <OSUtils.h>
50 /*
51 # include <ToolUtils.h>
52 # include <SegLoad.h>*/
53 #endif
54 
55 #ifdef FEAT_GUI_PHOTON
56 # include <Ph.h>
57 # include <Pt.h>
58 # include "photon/PxProto.h"
59 #endif
60 
61 /*
62  * On some systems scrolling needs to be done right away instead of in the
63  * main loop.
64  */
65 #if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_GTK)
66 # define USE_ON_FLY_SCROLL
67 #endif
68 
69 /*
70  * GUIs that support dropping files on a running Vim.
71  */
72 #if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_MAC) \
73 	|| defined(FEAT_GUI_GTK)
74 # define HAVE_DROP_FILE
75 #endif
76 
77 /*
78  * This define makes menus always use a fontset.
79  * We're not sure if this code always works, thus it can be disabled.
80  */
81 #ifdef FEAT_XFONTSET
82 # define FONTSET_ALWAYS
83 #endif
84 
85 /*
86  * These macros convert between character row/column and pixel coordinates.
87  * TEXT_X   - Convert character column into X pixel coord for drawing strings.
88  * TEXT_Y   - Convert character row into Y pixel coord for drawing strings.
89  * FILL_X   - Convert character column into X pixel coord for filling the area
90  *		under the character.
91  * FILL_Y   - Convert character row into Y pixel coord for filling the area
92  *		under the character.
93  * X_2_COL  - Convert X pixel coord into character column.
94  * Y_2_ROW  - Convert Y pixel coord into character row.
95  */
96 #ifdef FEAT_GUI_W32
97 # define TEXT_X(col)	((col) * gui.char_width)
98 # define TEXT_Y(row)	((row) * gui.char_height + gui.char_ascent)
99 # define FILL_X(col)	((col) * gui.char_width)
100 # define FILL_Y(row)	((row) * gui.char_height)
101 # define X_2_COL(x)	((x) / gui.char_width)
102 # define Y_2_ROW(y)	((y) / gui.char_height)
103 #else
104 # define TEXT_X(col)	((col) * gui.char_width  + gui.border_offset)
105 # define FILL_X(col)	((col) * gui.char_width  + gui.border_offset)
106 # define X_2_COL(x)	(((x) - gui.border_offset) / gui.char_width)
107 # define TEXT_Y(row)	((row) * gui.char_height + gui.char_ascent \
108 							+ gui.border_offset)
109 # define FILL_Y(row)	((row) * gui.char_height + gui.border_offset)
110 # define Y_2_ROW(y)	(((y) - gui.border_offset) / gui.char_height)
111 #endif
112 
113 /* Indices for arrays of scrollbars */
114 #define SBAR_NONE	    -1
115 #define SBAR_LEFT	    0
116 #define SBAR_RIGHT	    1
117 #define SBAR_BOTTOM	    2
118 
119 /* Orientations for scrollbars */
120 #define SBAR_VERT	    0
121 #define SBAR_HORIZ	    1
122 
123 /* Default size of scrollbar */
124 #define SB_DEFAULT_WIDTH    16
125 
126 /* Default height of the menu bar */
127 #define MENU_DEFAULT_HEIGHT 1		/* figure it out at runtime */
128 
129 /* Flags for gui_mch_outstr_nowrap() */
130 #define GUI_MON_WRAP_CURSOR	0x01	/* wrap cursor at end of line */
131 #define GUI_MON_INVERT		0x02	/* invert the characters */
132 #define GUI_MON_IS_CURSOR	0x04	/* drawing cursor */
133 #define GUI_MON_TRS_CURSOR	0x08	/* drawing transparent cursor */
134 #define GUI_MON_NOCLEAR		0x10	/* don't clear selection */
135 
136 /* Flags for gui_mch_draw_string() */
137 #define DRAW_TRANSP		0x01	/* draw with transparent bg */
138 #define DRAW_BOLD		0x02	/* draw bold text */
139 #define DRAW_UNDERL		0x04	/* draw underline text */
140 #define DRAW_UNDERC		0x08	/* draw undercurl text */
141 #if defined(FEAT_GUI_GTK)
142 # define DRAW_ITALIC		0x10	/* draw italic text */
143 #endif
144 #define DRAW_CURSOR		0x20	/* drawing block cursor (win32) */
145 
146 /* For our own tearoff menu item */
147 #define TEAR_STRING		"-->Detach"
148 #define TEAR_LEN		(9)	/* length of above string */
149 
150 /* for the toolbar */
151 #define TOOLBAR_BUTTON_HEIGHT	18
152 #define TOOLBAR_BUTTON_WIDTH	18
153 #define TOOLBAR_BORDER_HEIGHT	12  /* room above+below buttons for MSWindows */
154 
155 #ifdef FEAT_GUI_MSWIN
156 # define TABLINE_HEIGHT 22
157 #endif
158 #ifdef FEAT_GUI_MOTIF
159 # define TABLINE_HEIGHT 30
160 #endif
161 
162 #if defined(NO_CONSOLE) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
163 # define NO_CONSOLE_INPUT	/* use no_console_input() to check if there
164 				   is no console input possible */
165 #endif
166 
167 typedef struct GuiScrollbar
168 {
169     long	ident;		/* Unique identifier for each scrollbar */
170     win_T	*wp;		/* Scrollbar's window, NULL for bottom */
171     int		type;		/* one of SBAR_{LEFT,RIGHT,BOTTOM} */
172     long	value;		/* Represents top line number visible */
173 #ifdef FEAT_GUI_ATHENA
174     int		pixval;		/* pixel count of value */
175 #endif
176     long	size;		/* Size of scrollbar thumb */
177     long	max;		/* Number of lines in buffer */
178 
179     /* Values measured in characters: */
180     int		top;		/* Top of scroll bar (chars from row 0) */
181     int		height;		/* Current height of scroll bar in rows */
182 #ifdef FEAT_WINDOWS
183     int		width;		/* Current width of scroll bar in cols */
184 #endif
185     int		status_height;	/* Height of status line */
186 #ifdef FEAT_GUI_X11
187     Widget	id;		/* Id of real scroll bar */
188 #endif
189 #ifdef FEAT_GUI_GTK
190     GtkWidget *id;		/* Id of real scroll bar */
191     unsigned long handler_id;   /* Id of "value_changed" signal handler */
192 #endif
193 
194 #ifdef FEAT_GUI_MSWIN
195     HWND	id;		/* Id of real scroll bar */
196     int		scroll_shift;	/* The scrollbar stuff can handle only up to
197 				   32767 lines.  When the file is longer,
198 				   scroll_shift is set to the number of shifts
199 				   to reduce the count.  */
200 #endif
201 #ifdef FEAT_GUI_MAC
202     ControlHandle id;		/* A handle to the scrollbar */
203 #endif
204 #ifdef FEAT_GUI_PHOTON
205     PtWidget_t	*id;
206 #endif
207 } scrollbar_T;
208 
209 typedef long	    guicolor_T;	/* handle for a GUI color; for X11 this should
210 				   be "Pixel", but that's an unsigned and we
211 				   need a signed value */
212 #define INVALCOLOR (guicolor_T)-11111	/* number for invalid color; on 32 bit
213 				   displays there is a tiny chance this is an
214 				   actual color */
215 
216 #ifdef FEAT_GUI_GTK
217   typedef PangoFontDescription	*GuiFont;       /* handle for a GUI font */
218   typedef PangoFontDescription  *GuiFontset;    /* handle for a GUI fontset */
219 # define NOFONT		(GuiFont)NULL
220 # define NOFONTSET	(GuiFontset)NULL
221 #else
222 # ifdef FEAT_GUI_PHOTON
223   typedef char		*GuiFont;
224   typedef char		*GuiFontset;
225 #  define NOFONT	(GuiFont)NULL
226 #  define NOFONTSET	(GuiFontset)NULL
227 # else
228 #  ifdef FEAT_GUI_X11
229   typedef XFontStruct	*GuiFont;	/* handle for a GUI font */
230   typedef XFontSet	GuiFontset;	/* handle for a GUI fontset */
231 #   define NOFONT	(GuiFont)0
232 #   define NOFONTSET	(GuiFontset)0
233 #  else
234   typedef long_u	GuiFont;	/* handle for a GUI font */
235   typedef long_u	GuiFontset;	/* handle for a GUI fontset */
236 #   define NOFONT	(GuiFont)0
237 #   define NOFONTSET	(GuiFontset)0
238 #  endif
239 # endif
240 #endif
241 
242 typedef struct Gui
243 {
244     int		in_focus;	    /* Vim has input focus */
245     int		in_use;		    /* Is the GUI being used? */
246     int		starting;	    /* GUI will start in a little while */
247     int		shell_created;	    /* Has the shell been created yet? */
248     int		dying;		    /* Is vim dying? Then output to terminal */
249     int		dofork;		    /* Use fork() when GUI is starting */
250     int		dragged_sb;	    /* Which scrollbar being dragged, if any? */
251     win_T	*dragged_wp;	    /* Which WIN's sb being dragged, if any? */
252     int		pointer_hidden;	    /* Is the mouse pointer hidden? */
253     int		col;		    /* Current cursor column in GUI display */
254     int		row;		    /* Current cursor row in GUI display */
255     int		cursor_col;	    /* Physical cursor column in GUI display */
256     int		cursor_row;	    /* Physical cursor row in GUI display */
257     char	cursor_is_valid;    /* There is a cursor at cursor_row/col */
258     int		num_cols;	    /* Number of columns */
259     int		num_rows;	    /* Number of rows */
260     int		scroll_region_top;  /* Top (first) line of scroll region */
261     int		scroll_region_bot;  /* Bottom (last) line of scroll region */
262     int		scroll_region_left;  /* Left (first) column of scroll region */
263     int		scroll_region_right;  /* Right (last) col. of scroll region */
264     int		highlight_mask;	    /* Highlight attribute mask */
265     int		scrollbar_width;    /* Width of vertical scrollbars */
266     int		scrollbar_height;   /* Height of horizontal scrollbar */
267     int		left_sbar_x;	    /* Calculated x coord for left scrollbar */
268     int		right_sbar_x;	    /* Calculated x coord for right scrollbar */
269 
270 #ifdef FEAT_MENU
271 # ifndef FEAT_GUI_GTK
272     int		menu_height;	    /* Height of the menu bar */
273     int		menu_width;	    /* Width of the menu bar */
274 # endif
275     char	menu_is_active;	    /* TRUE if menu is present */
276 # ifdef FEAT_GUI_ATHENA
277     char	menu_height_fixed;  /* TRUE if menu height fixed */
278 # endif
279 #endif
280 
281     scrollbar_T bottom_sbar;	    /* Bottom scrollbar */
282     int		which_scrollbars[3];/* Which scrollbar boxes are active? */
283     int		prev_wrap;	    /* For updating the horizontal scrollbar */
284     int		char_width;	    /* Width of char cell in pixels */
285     int		char_height;	    /* Height of char cell in pixels, includes
286 				       'linespace' */
287     int		char_ascent;	    /* Ascent of char in pixels */
288     int		border_width;	    /* Width of our border around text area */
289     int		border_offset;	    /* Total pixel offset for all borders */
290 
291     GuiFont	norm_font;	    /* Normal font */
292 #ifndef FEAT_GUI_GTK
293     GuiFont	bold_font;	    /* Bold font */
294     GuiFont	ital_font;	    /* Italic font */
295     GuiFont	boldital_font;	    /* Bold-Italic font */
296 #else
297     int		font_can_bold;	    /* Whether norm_font supports bold weight.
298 				     * The styled font variants are not used. */
299 #endif
300 
301 #if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK)
302 # ifdef FONTSET_ALWAYS
303     GuiFontset	menu_fontset;	    /* set of fonts for multi-byte chars */
304 # else
305     GuiFont	menu_font;	    /* menu item font */
306 # endif
307 #endif
308 #ifdef FEAT_MBYTE
309     GuiFont	wide_font;	    /* Normal 'guifontwide' font */
310 # ifndef FEAT_GUI_GTK
311     GuiFont	wide_bold_font;	    /* Bold 'guifontwide' font */
312     GuiFont	wide_ital_font;	    /* Italic 'guifontwide' font */
313     GuiFont	wide_boldital_font; /* Bold-Italic 'guifontwide' font */
314 # endif
315 #endif
316 #ifdef FEAT_XFONTSET
317     GuiFontset	fontset;	    /* set of fonts for multi-byte chars */
318 #endif
319     guicolor_T	back_pixel;	    /* Color of background */
320     guicolor_T	norm_pixel;	    /* Color of normal text */
321     guicolor_T	def_back_pixel;	    /* default Color of background */
322     guicolor_T	def_norm_pixel;	    /* default Color of normal text */
323 
324 #ifdef FEAT_GUI_X11
325     char	*rsrc_menu_fg_name;	/* Color of menu & dialog foreground */
326     guicolor_T	menu_fg_pixel;		/* Same in Pixel format */
327     char	*rsrc_menu_bg_name;	/* Color of menu & dialog background */
328     guicolor_T	menu_bg_pixel;		/* Same in Pixel format */
329     char	*rsrc_scroll_fg_name;	/* Color of scrollbar foreground */
330     guicolor_T	scroll_fg_pixel;	/* Same in Pixel format */
331     char	*rsrc_scroll_bg_name;	/* Color of scrollbar background */
332     guicolor_T	scroll_bg_pixel;	/* Same in Pixel format */
333 
334 # ifdef FEAT_GUI_MOTIF
335     guicolor_T	menu_def_fg_pixel;  /* Default menu foreground */
336     guicolor_T	menu_def_bg_pixel;  /* Default menu background */
337     guicolor_T	scroll_def_fg_pixel;  /* Default scrollbar foreground */
338     guicolor_T	scroll_def_bg_pixel;  /* Default scrollbar background */
339 # endif
340     Display	*dpy;		    /* X display */
341     Window	wid;		    /* Window id of text area */
342     int		visibility;	    /* Is shell partially/fully obscured? */
343     GC		text_gc;
344     GC		back_gc;
345     GC		invert_gc;
346     Cursor	blank_pointer;	    /* Blank pointer */
347 
348     /* X Resources */
349     char_u	*rsrc_font_name;    /* Resource font name, used if 'guifont'
350 				       not set */
351     char_u	*rsrc_bold_font_name; /* Resource bold font name */
352     char_u	*rsrc_ital_font_name; /* Resource italic font name */
353     char_u	*rsrc_boldital_font_name;  /* Resource bold-italic font name */
354     char_u	*rsrc_menu_font_name;    /* Resource menu Font name */
355     Bool	rsrc_rev_video;	    /* Use reverse video? */
356 
357     char_u	*geom;		    /* Geometry, eg "80x24" */
358     Bool	color_approx;	    /* Some color was approximated */
359 #endif
360 
361 #ifdef FEAT_GUI_GTK
362 # ifndef USE_GTK3
363     int		visibility;	    /* Is shell partially/fully obscured? */
364 # endif
365     GdkCursor	*blank_pointer;	    /* Blank pointer */
366 
367     /* X Resources */
368     char_u	*geom;		    /* Geometry, eg "80x24" */
369 
370     GtkWidget	*mainwin;	    /* top level GTK window */
371     GtkWidget	*formwin;	    /* manages all the windows below */
372     GtkWidget	*drawarea;	    /* the "text" area */
373 # ifdef FEAT_MENU
374     GtkWidget	*menubar;	    /* menubar */
375 # endif
376 # ifdef FEAT_TOOLBAR
377     GtkWidget	*toolbar;	    /* toolbar */
378 # endif
379 # ifdef FEAT_GUI_GNOME
380     GtkWidget	*menubar_h;	    /* menubar handle */
381     GtkWidget	*toolbar_h;	    /* toolbar handle */
382 # endif
383     GdkColor	*fgcolor;	    /* GDK-styled foreground color */
384     GdkColor	*bgcolor;	    /* GDK-styled background color */
385     GdkColor	*spcolor;	    /* GDK-styled special color */
386 # ifdef USE_GTK3
387     cairo_surface_t *surface;       /* drawarea surface */
388     gboolean	     by_signal;     /* cause of draw operation */
389 # else
390     GdkGC	*text_gc;	    /* cached GC for normal text */
391 # endif
392     PangoContext     *text_context; /* the context used for all text */
393     PangoFont	     *ascii_font;   /* cached font for ASCII strings */
394     PangoGlyphString *ascii_glyphs; /* cached code point -> glyph map */
395 # ifdef FEAT_GUI_TABLINE
396     GtkWidget	*tabline;	    /* tab pages line handle */
397 # endif
398 
399     GtkAccelGroup *accel_group;
400     GtkWidget	*filedlg;	    /* file selection dialog */
401     char_u	*browse_fname;	    /* file name from filedlg */
402 
403     guint32	event_time;
404 #endif	/* FEAT_GUI_GTK */
405 
406 #if defined(FEAT_GUI_TABLINE) \
407 	&& (defined(FEAT_GUI_W32) || defined(FEAT_GUI_MOTIF) \
408 		|| defined(FEAT_GUI_MAC))
409     int		tabline_height;
410 #endif
411 
412 #ifdef FEAT_FOOTER
413     int		footer_height;	    /* height of the message footer */
414 #endif
415 
416 #if defined(FEAT_TOOLBAR) \
417 	&& (defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MOTIF))
418     int		toolbar_height;	    /* height of the toolbar */
419 #endif
420 
421 #ifdef FEAT_BEVAL_TIP
422     /* Tooltip properties; also used for balloon evaluation */
423     char_u	*rsrc_tooltip_font_name; /* tooltip font name */
424     char	*rsrc_tooltip_fg_name;	/* tooltip foreground color name */
425     char	*rsrc_tooltip_bg_name;	/* tooltip background color name */
426     guicolor_T	tooltip_fg_pixel;	/* tooltip foreground color */
427     guicolor_T	tooltip_bg_pixel;	/* tooltip background color */
428     XFontSet	tooltip_fontset;	/* tooltip fontset */
429 #endif
430 
431 #ifdef FEAT_GUI_MSWIN
432     GuiFont	currFont;	    /* Current font */
433     guicolor_T	currFgColor;	    /* Current foreground text color */
434     guicolor_T	currBgColor;	    /* Current background text color */
435     guicolor_T	currSpColor;	    /* Current special text color */
436 #endif
437 
438 #ifdef FEAT_GUI_MAC
439     WindowPtr	VimWindow;
440     MenuHandle	MacOSHelpMenu;	    /* Help menu provided by the MacOS */
441     int		MacOSHelpItems;	    /* Nr of help-items supplied by MacOS */
442     WindowPtr	wid;		    /* Window id of text area */
443     int		visibility;	    /* Is window partially/fully obscured? */
444 #endif
445 
446 #ifdef FEAT_GUI_PHOTON
447     PtWidget_t	*vimWindow;		/* PtWindow */
448     PtWidget_t	*vimTextArea;		/* PtRaw */
449     PtWidget_t	*vimContainer;		/* PtPanel */
450 # if defined(FEAT_MENU) || defined(FEAT_TOOLBAR)
451     PtWidget_t	*vimToolBarGroup;
452 # endif
453 # ifdef FEAT_MENU
454     PtWidget_t	*vimMenuBar;
455 # endif
456 # ifdef FEAT_TOOLBAR
457     PtWidget_t	*vimToolBar;
458     int		toolbar_height;
459 # endif
460     PhEvent_t	*event_buffer;
461 #endif
462 
463 #ifdef FEAT_XIM
464     char	*rsrc_input_method;
465     char	*rsrc_preedit_type_name;
466 #endif
467 } gui_T;
468 
469 extern gui_T gui;			/* this is defined in gui.c */
470 
471 /* definitions of available window positionings for gui_*_position_in_parent()
472  */
473 typedef enum
474 {
475     VW_POS_MOUSE,
476     VW_POS_CENTER,
477     VW_POS_TOP_CENTER
478 } gui_win_pos_T;
479 
480 #ifdef FIND_REPLACE_DIALOG
481 /*
482  * Flags used to distinguish the different contexts in which the
483  * find/replace callback may be called.
484  */
485 # define FRD_FINDNEXT	1	/* Find next in find dialog */
486 # define FRD_R_FINDNEXT	2	/* Find next in repl dialog */
487 # define FRD_REPLACE	3	/* Replace once */
488 # define FRD_REPLACEALL	4	/* Replace remaining matches */
489 # define FRD_UNDO	5	/* Undo replaced text */
490 # define FRD_TYPE_MASK   7	/* Mask for the callback type */
491 /* Flags which change the way searching is done. */
492 # define FRD_WHOLE_WORD	0x08	/* match whole word only */
493 # define FRD_MATCH_CASE	0x10	/* match case */
494 #endif
495 
496 #ifdef FEAT_GUI_GTK
497 /*
498  * Convenience macros to convert from 'encoding' to 'termencoding' and
499  * vice versa.	If no conversion is necessary the passed-in pointer is
500  * returned as is, without allocating any memory.  Thus additional _FREE()
501  * macros are provided.  The _FREE() macros also set the pointer to NULL,
502  * in order to avoid bugs due to illegal memory access only happening if
503  * 'encoding' != utf-8...
504  *
505  * Defining these macros as pure expressions looks a bit tricky but
506  * avoids depending on the context of the macro expansion.  One of the
507  * rare occasions where the comma operator comes in handy :)
508  *
509  * Note: Do NOT keep the result around when handling control back to
510  * the main Vim!  The user could change 'encoding' at any time.
511  */
512 # define CONVERT_TO_UTF8(String)				\
513     ((output_conv.vc_type == CONV_NONE || (String) == NULL)	\
514 	    ? (String)						\
515 	    : string_convert(&output_conv, (String), NULL))
516 
517 # define CONVERT_TO_UTF8_FREE(String)				\
518     ((String) = ((output_conv.vc_type == CONV_NONE)		\
519 			? (char_u *)NULL			\
520 			: (vim_free(String), (char_u *)NULL)))
521 
522 # define CONVERT_FROM_UTF8(String)				\
523     ((input_conv.vc_type == CONV_NONE || (String) == NULL)	\
524 	    ? (String)						\
525 	    : string_convert(&input_conv, (String), NULL))
526 
527 # define CONVERT_FROM_UTF8_FREE(String)				\
528     ((String) = ((input_conv.vc_type == CONV_NONE)		\
529 			? (char_u *)NULL			\
530 			: (vim_free(String), (char_u *)NULL)))
531 
532 #else
533 # define CONVERT_TO_UTF8(String) (String)
534 # define CONVERT_TO_UTF8_FREE(String) ((String) = (char_u *)NULL)
535 # define CONVERT_FROM_UTF8(String) (String)
536 # define CONVERT_FROM_UTF8_FREE(String) ((String) = (char_u *)NULL)
537 #endif /* FEAT_GUI_GTK */
538