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