blob: 56891822773e9718c64b04a280ac17c845e71f04 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/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 * See README.txt for an overview of the Vim source code.
9 */
10
11#include "vim.h"
12
13/* Structure containing all the GUI information */
14gui_T gui;
15
Bram Moolenaar13505972019-01-24 15:04:48 +010016#if !defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010017static void set_guifontwide(char_u *font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010019static void gui_check_pos(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010020static void gui_outstr(char_u *, int);
21static int gui_screenchar(int off, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010022static void gui_delete_lines(int row, int count);
23static void gui_insert_lines(int row, int count);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000024#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010025static int gui_has_tabline(void);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000026#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010027static void gui_do_scrollbar(win_T *wp, int which, int enable);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010028static void gui_update_horiz_scrollbar(int);
29static void gui_set_fg_color(char_u *name);
30static void gui_set_bg_color(char_u *name);
31static win_T *xy2win(int x, int y);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020033#ifdef GUI_MAY_FORK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010034static void gui_do_fork(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020035
Bram Moolenaard25c16e2016-01-29 22:13:30 +010036static int gui_read_child_pipe(int fd);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020037
38/* Return values for gui_read_child_pipe */
39enum {
40 GUI_CHILD_IO_ERROR,
41 GUI_CHILD_OK,
42 GUI_CHILD_FAILED
43};
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020044#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020045
Bram Moolenaard25c16e2016-01-29 22:13:30 +010046static void gui_attempt_start(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020047
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static int can_update_cursor = TRUE; /* can display the cursor */
Bram Moolenaara338adc2018-01-31 20:51:47 +010049static int disable_flush = 0; /* If > 0, gui_mch_flush() is disabled. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000050
51/*
52 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
53 * this makes the thumb indicate the part of the text that is shown. Motif
54 * can't do this.
55 */
56#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
57# define SCROLL_PAST_END
58#endif
59
60/*
61 * gui_start -- Called when user wants to start the GUI.
62 *
63 * Careful: This function can be called recursively when there is a ":gui"
64 * command in the .gvimrc file. Only the first call should fork, not the
65 * recursive call.
66 */
67 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +020068gui_start(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000069{
70 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 static int recursive = 0;
Bram Moolenaar68cbb142019-05-09 14:14:42 +020072#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +020073 char *msg = NULL;
74#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
76 old_term = vim_strsave(T_NAME);
77
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 settmode(TMODE_COOK); /* stop RAW mode */
79 if (full_screen)
80 cursor_on(); /* needed for ":gui" in .vimrc */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081 full_screen = FALSE;
82
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 ++recursive;
84
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020085#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020086 /*
87 * Quit the current process and continue in the child.
88 * Makes "gvim file" disconnect from the shell it was started in.
89 * Don't do this when Vim was started with "-f" or the 'f' flag is present
90 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020091 * Don't do this when there is a running job, we can only get the status
92 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020093 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020094 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
95# ifdef FEAT_JOB_CHANNEL
96 && !job_any_running()
97# endif
98 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020099 {
100 gui_do_fork();
101 }
102 else
103#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200104#ifdef GUI_MAY_SPAWN
105 if (gui.dospawn
106# ifdef EXPERIMENTAL_GUI_CMD
107 && gui.dofork
108# endif
109 && !vim_strchr(p_go, GO_FORG)
110 && !anyBufIsChanged()
111# ifdef FEAT_JOB_CHANNEL
112 && !job_any_running()
113# endif
114 )
115 {
Bram Moolenaar68cbb142019-05-09 14:14:42 +0200116# ifdef EXPERIMENTAL_GUI_CMD
117 msg =
118# endif
119 gui_mch_do_spawn(arg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200120 }
121 else
122#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200123 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200124#ifdef FEAT_GUI_GTK
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200125 /* If there is 'f' in 'guioptions' and specify -g argument,
126 * gui_mch_init_check() was not called yet. */
127 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100128 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200129#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200130 gui_attempt_start();
131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
133 if (!gui.in_use) /* failed to start GUI */
134 {
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200135 /* Back to old term settings
136 *
137 * FIXME: If we got here because a child process failed and flagged to
138 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
139 * hit an X11 I/O error and do a longjmp(), leaving recursive
140 * permanently set to 1. This is probably not as big a problem as it
141 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
142 * return "OK" unconditionally, so it would be very difficult to
143 * actually hit this case.
144 */
145 termcapinit(old_term);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 settmode(TMODE_RAW); /* restart RAW mode */
147#ifdef FEAT_TITLE
148 set_title_defaults(); /* set 'title' and 'icon' again */
149#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200150#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
151 if (msg)
152 emsg(msg);
153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 }
155
156 vim_free(old_term);
157
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200158 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
159 * the GUIFailed event. */
160 gui_mch_update();
161 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
162 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200163 --recursive;
164}
165
166/*
167 * Set_termname() will call gui_init() to start the GUI.
168 * Set the "starting" flag, to indicate that the GUI will start.
169 *
170 * We don't want to open the GUI shell until after we've read .gvimrc,
171 * otherwise we don't know what font we will use, and hence we don't know
172 * what size the shell should be. So if there are errors in the .gvimrc
173 * file, they will have to go to the terminal: Set full_screen to FALSE.
174 * full_screen will be set to TRUE again by a successful termcapinit().
175 */
176 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100177gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200178{
179 static int recursive = 0;
180
181 ++recursive;
182 gui.starting = TRUE;
183
184#ifdef FEAT_GUI_GTK
185 gui.event_time = GDK_CURRENT_TIME;
186#endif
187
188 termcapinit((char_u *)"builtin_gui");
189 gui.starting = recursive - 1;
190
Bram Moolenaar9372a112005-12-06 19:59:18 +0000191#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200193 {
194# ifdef FEAT_EVAL
195 Window x11_window;
196 Display *x11_display;
197
198 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
199 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
200# endif
201
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 /* Display error messages in a dialog now. */
203 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 --recursive;
207}
208
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200209#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200210
211/* for waitpid() */
212# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
213# include <sys/wait.h>
214# endif
215
216/*
217 * Create a new process, by forking. In the child, start the GUI, and in
218 * the parent, exit.
219 *
220 * If something goes wrong, this will return with gui.in_use still set
221 * to FALSE, in which case the caller should continue execution without
222 * the GUI.
223 *
224 * If the child fails to start the GUI, then the child will exit and the
225 * parent will return. If the child succeeds, then the parent will exit
226 * and the child will return.
227 */
228 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100229gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200230{
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200231 int pipefd[2]; /* pipe between parent and child */
232 int pipe_error;
233 int status;
234 int exit_status;
235 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200236
237 /* Setup a pipe between the child and the parent, so that the parent
238 * knows when the child has done the setsid() call and is allowed to
239 * exit. */
240 pipe_error = (pipe(pipefd) < 0);
241 pid = fork();
242 if (pid < 0) /* Fork error */
243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100244 emsg(_("E851: Failed to create a new process for the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200245 return;
246 }
247 else if (pid > 0) /* Parent */
248 {
249 /* Give the child some time to do the setsid(), otherwise the
250 * exit() may kill the child too (when starting gvim from inside a
251 * gvim). */
252 if (!pipe_error)
253 {
254 /* The read returns when the child closes the pipe (or when
255 * the child dies for some reason). */
256 close(pipefd[1]);
257 status = gui_read_child_pipe(pipefd[0]);
258 if (status == GUI_CHILD_FAILED)
259 {
260 /* The child failed to start the GUI, so the caller must
261 * continue. There may be more error information written
262 * to stderr by the child. */
263# ifdef __NeXT__
264 wait4(pid, &exit_status, 0, (struct rusage *)0);
265# else
266 waitpid(pid, &exit_status, 0);
267# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100268 emsg(_("E852: The child process failed to start the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200269 return;
270 }
271 else if (status == GUI_CHILD_IO_ERROR)
272 {
273 pipe_error = TRUE;
274 }
275 /* else GUI_CHILD_OK: parent exit */
276 }
277
278 if (pipe_error)
279 ui_delay(300L, TRUE);
280
281 /* When swapping screens we may need to go to the next line, e.g.,
282 * after a hit-enter prompt and using ":gui". */
283 if (newline_on_exit)
284 mch_errmsg("\r\n");
285
286 /*
287 * The parent must skip the normal exit() processing, the child
288 * will do it. For example, GTK messes up signals when exiting.
289 */
290 _exit(0);
291 }
292 /* Child */
293
Bram Moolenaar29695702012-05-18 17:03:18 +0200294#ifdef FEAT_GUI_GTK
295 /* Call gtk_init_check() here after fork(). See gui_init_check(). */
296 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100297 getout_preserve_modified(1);
Bram Moolenaar29695702012-05-18 17:03:18 +0200298#endif
299
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200300# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
301 /*
302 * Change our process group. On some systems/shells a CTRL-C in the
303 * shell where Vim was started would otherwise kill gvim!
304 */
305# if defined(HAVE_SETSID)
306 (void)setsid();
307# else
308 (void)setpgid(0, 0);
309# endif
310# endif
311 if (!pipe_error)
312 close(pipefd[0]);
313
314# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
315 /* Tell the session manager our new PID */
316 gui_mch_forked();
317# endif
318
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200319 /* Try to start the GUI */
320 gui_attempt_start();
321
322 /* Notify the parent */
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200323 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200324 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200325 if (gui.in_use)
326 write_eintr(pipefd[1], "ok", 3);
327 else
328 write_eintr(pipefd[1], "fail", 5);
329 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200330 }
331
332 /* If we failed to start the GUI, exit now. */
333 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100334 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200335}
336
337/*
338 * Read from a pipe assumed to be connected to the child process (this
339 * function is called from the parent).
340 * Return GUI_CHILD_OK if the child successfully started the GUI,
341 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
342 * some other error.
343 *
344 * The file descriptor will be closed before the function returns.
345 */
346 static int
347gui_read_child_pipe(int fd)
348{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200349 long bytes_read;
350#define READ_BUFFER_SIZE 10
351 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200352
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200353 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
354#undef READ_BUFFER_SIZE
355 close(fd);
356 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200357 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200358 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200359 if (strcmp(buffer, "ok") == 0)
360 return GUI_CHILD_OK;
361 return GUI_CHILD_FAILED;
362}
363
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200364#endif /* GUI_MAY_FORK */
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/*
367 * Call this when vim starts up, whether or not the GUI is started
368 */
369 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100370gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371{
372 gui.in_use = FALSE; /* No GUI yet (maybe later) */
373 gui.starting = FALSE; /* No GUI yet (maybe later) */
374 gui_mch_prepare(argc, argv);
375}
376
377/*
378 * Try initializing the GUI and check if it can be started.
379 * Used from main() to check early if "vim -g" can start the GUI.
380 * Used from gui_init() to prepare for starting the GUI.
381 * Returns FAIL or OK.
382 */
383 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100384gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385{
386 static int result = MAYBE;
387
388 if (result != MAYBE)
389 {
390 if (result == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100391 emsg(_("E229: Cannot start the GUI"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 return result;
393 }
394
395 gui.shell_created = FALSE;
396 gui.dying = FALSE;
397 gui.in_focus = TRUE; /* so the guicursor setting works */
398 gui.dragged_sb = SBAR_NONE;
399 gui.dragged_wp = NULL;
400 gui.pointer_hidden = FALSE;
401 gui.col = 0;
402 gui.row = 0;
403 gui.num_cols = Columns;
404 gui.num_rows = Rows;
405
406 gui.cursor_is_valid = FALSE;
407 gui.scroll_region_top = 0;
408 gui.scroll_region_bot = Rows - 1;
409 gui.scroll_region_left = 0;
410 gui.scroll_region_right = Columns - 1;
411 gui.highlight_mask = HL_NORMAL;
412 gui.char_width = 1;
413 gui.char_height = 1;
414 gui.char_ascent = 0;
415 gui.border_width = 0;
416
417 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200418#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 gui.bold_font = NOFONT;
420 gui.ital_font = NOFONT;
421 gui.boldital_font = NOFONT;
422# ifdef FEAT_XFONTSET
423 gui.fontset = NOFONTSET;
424# endif
425#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200426 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100427#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200428 gui.wide_bold_font = NOFONT;
429 gui.wide_ital_font = NOFONT;
430 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432
433#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200434# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435# ifdef FONTSET_ALWAYS
436 gui.menu_fontset = NOFONTSET;
437# else
438 gui.menu_font = NOFONT;
439# endif
440# endif
441 gui.menu_is_active = TRUE; /* default: include menu */
442# ifndef FEAT_GUI_GTK
443 gui.menu_height = MENU_DEFAULT_HEIGHT;
444 gui.menu_width = 0;
445# endif
446#endif
447#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
448 gui.toolbar_height = 0;
449#endif
450#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
451 gui.footer_height = 0;
452#endif
453#ifdef FEAT_BEVAL_TIP
454 gui.tooltip_fontset = NOFONTSET;
455#endif
456
457 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
458 gui.prev_wrap = -1;
459
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200460#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 result = OK;
462#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200463# ifdef FEAT_GUI_GTK
464 /*
465 * Note: Don't call gtk_init_check() before fork, it will be called after
466 * the fork. When calling it before fork, it make vim hang for a while.
467 * See gui_do_fork().
468 * Use a simpler check if the GUI window can probably be opened.
469 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200470 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200471# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200473# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474#endif
475 return result;
476}
477
478/*
479 * This is the call which starts the GUI.
480 */
481 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100482gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
484 win_T *wp;
485 static int recursive = 0;
486
487 /*
488 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
489 * function will then be executed at the first call, the rest by the
490 * recursive call. This allow the shell to be opened halfway reading a
491 * gvimrc file.
492 */
493 if (!recursive)
494 {
495 ++recursive;
496
497 clip_init(TRUE);
498
499 /* If can't initialize, don't try doing the rest */
500 if (gui_init_check() == FAIL)
501 {
502 --recursive;
503 clip_init(FALSE);
504 return;
505 }
506
507 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000508 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
509 * breaks the Paste toolbar button.
510 */
511 set_option_value((char_u *)"paste", 0L, NULL, 0);
512
513 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 * Set up system-wide default menus.
515 */
516#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
517 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
518 {
519 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000520 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 sys_menu = FALSE;
522 }
523#endif
524
525 /*
526 * Switch on the mouse by default, unless the user changed it already.
527 * This can then be changed in the .gvimrc.
528 */
529 if (!option_was_set((char_u *)"mouse"))
530 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000531 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532
533 /*
534 * If -U option given, use only the initializations from that file and
535 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
536 */
537 if (use_gvimrc != NULL)
538 {
539 if (STRCMP(use_gvimrc, "NONE") != 0
540 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000541 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100542 semsg(_("E230: Cannot read from \"%s\""), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 }
544 else
545 {
546 /*
547 * Get system wide defaults for gvim, only when file name defined.
548 */
549#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000550 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#endif
552
553 /*
554 * Try to read GUI initialization commands from the following
555 * places:
556 * - environment variable GVIMINIT
557 * - the user gvimrc file (~/.gvimrc)
558 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
559 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
560 * The first that exists is used, the rest is ignored.
561 */
562 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000563 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
564 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000566 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
567 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200569#ifdef USR_GVIMRC_FILE3
570 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
571 DOSO_GVIMRC) == FAIL
572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 )
574 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200575#ifdef USR_GVIMRC_FILE4
576 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577#endif
578 }
579
580 /*
581 * Read initialization commands from ".gvimrc" in current
582 * directory. This is only done if the 'exrc' option is set.
583 * Because of security reasons we disallow shell and write
584 * commands now, except for unix if the file is owned by the user
585 * or 'secure' option has been reset in environment of global
586 * ".gvimrc".
587 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
588 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
589 */
590 if (p_exrc)
591 {
592#ifdef UNIX
593 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200594 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
596 /* if ".gvimrc" file is not owned by user, set 'secure'
597 * mode */
598 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
599 secure = p_secure;
600 }
601#else
602 secure = p_secure;
603#endif
604
605 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200606 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607#ifdef SYS_GVIMRC_FILE
608 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200609 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#endif
611#ifdef USR_GVIMRC_FILE2
612 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200613 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#endif
615#ifdef USR_GVIMRC_FILE3
616 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200617 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200619#ifdef USR_GVIMRC_FILE4
620 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200621 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000624 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
626 if (secure == 2)
627 need_wait_return = TRUE;
628 secure = 0;
629 }
630 }
631
632 if (need_wait_return || msg_didany)
633 wait_return(TRUE);
634
635 --recursive;
636 }
637
638 /* If recursive call opened the shell, return here from the first call */
639 if (gui.in_use)
640 return;
641
642 /*
643 * Create the GUI shell.
644 */
645 gui.in_use = TRUE; /* Must be set after menus have been set up */
646 if (gui_mch_init() == FAIL)
647 goto error;
648
649 /* Avoid a delay for an error message that was printed in the terminal
650 * where Vim was started. */
651 emsg_on_display = FALSE;
652 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100653 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 need_wait_return = FALSE;
655 msg_didany = FALSE;
656
657 /*
658 * Check validity of any generic resources that may have been loaded.
659 */
660 if (gui.border_width < 0)
661 gui.border_width = 0;
662
663 /*
664 * Set up the fonts. First use a font specified with "-fn" or "-font".
665 */
666 if (font_argument != NULL)
667 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
668 if (
669#ifdef FEAT_XFONTSET
670 (*p_guifontset == NUL
671 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
672#endif
673 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
674 : p_guifont, FALSE) == FAIL)
675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100676 emsg(_("E665: Cannot start GUI, no valid font found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 goto error2;
678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 if (gui_get_wide_font() == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100680 emsg(_("E231: 'guifontwide' invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 gui.num_cols = Columns;
683 gui.num_rows = Rows;
684 gui_reset_scroll_region();
685
686 /* Create initial scrollbars */
687 FOR_ALL_WINDOWS(wp)
688 {
689 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
690 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
691 }
692 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
693
694#ifdef FEAT_MENU
695 gui_create_initial_menus(root_menu);
696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#ifdef FEAT_SIGN_ICONS
698 sign_gui_started();
699#endif
700
701 /* Configure the desired menu and scrollbars */
702 gui_init_which_components(NULL);
703
704 /* All components of the GUI have been created now */
705 gui.shell_created = TRUE;
706
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100707#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100708 // Set the shell size, adjusted for the screen size. For GTK this only
709 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100710 // If the window is already maximized (e.g. when --windowid is passed in),
711 // we want to use the system-provided dimensions by passing FALSE to
712 // mustset. Otherwise, we want to initialize with the default rows/columns.
713 if (gui_mch_maximized())
714 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
715 else
716 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100717#else
718# ifndef FEAT_GUI_GTK
719 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#endif
722#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
723 /* Need to set the size of the menubar after all the menus have been
724 * created. */
725 gui_mch_compute_menu_height((Widget)0);
726#endif
727
728 /*
729 * Actually open the GUI shell.
730 */
731 if (gui_mch_open() != FAIL)
732 {
733#ifdef FEAT_TITLE
734 maketitle();
735 resettitle();
736#endif
737 init_gui_options();
738#ifdef FEAT_ARABIC
739 /* Our GUI can't do bidi. */
740 p_tbidi = FALSE;
741#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000742#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 /* Give GTK+ a chance to put all widget's into place. */
744 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000745
746# ifdef FEAT_MENU
747 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
748 * It was still there to make F10 work. */
749 if (vim_strchr(p_go, GO_MENUS) == NULL)
750 {
751 --gui.starting;
752 gui_mch_enable_menu(FALSE);
753 ++gui.starting;
754 gui_mch_update();
755 }
756# endif
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 /* Now make sure the shell fits on the screen. */
Bram Moolenaar372674f2019-03-30 20:31:22 +0100759 if (gui_mch_maximized())
760 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
761 else
762 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000764 /* When 'lines' was set while starting up the topframe may have to be
765 * resized. */
766 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000767
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100768#ifdef FEAT_BEVAL_GUI
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000769 /* Always create the Balloon Evaluation area, but disable it when
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100770 * 'ballooneval' is off. */
771 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200772 {
773# ifdef FEAT_VARTABS
774 vim_free(balloonEval->vts);
775# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100776 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200777 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100778 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000779# ifdef FEAT_GUI_GTK
780 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
781 &general_beval_cb, NULL);
782# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000783# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000784 {
785 extern Widget textArea;
786 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000787 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000788 }
789# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100790# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000791 balloonEval = gui_mch_create_beval_area(NULL, NULL,
792 &general_beval_cb, NULL);
793# endif
794# endif
795# endif
796 if (!p_beval)
797 gui_mch_disable_beval_area(balloonEval);
798#endif
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
801 if (!im_xim_isvalid_imactivate())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100802 emsg(_("E599: Value of 'imactivatekey' is invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000804 /* When 'cmdheight' was set during startup it may not have taken
805 * effect yet. */
806 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000807 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809 return;
810 }
811
812error2:
813#ifdef FEAT_GUI_X11
814 /* undo gui_mch_init() */
815 gui_mch_uninit();
816#endif
817
818error:
819 gui.in_use = FALSE;
820 clip_init(FALSE);
821}
822
823
824 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100825gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 /* don't free the fonts, it leads to a BUS error
828 * richard@whitequeen.com Jul 99 */
829 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 gui.in_use = FALSE;
831 gui_mch_exit(rc);
832}
833
Bram Moolenaar9372a112005-12-06 19:59:18 +0000834#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000836# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837/*
838 * Called when the GUI shell is closed by the user. If there are no changed
839 * files Vim exits, otherwise there will be a dialog to ask the user what to
840 * do.
841 * When this function returns, Vim should NOT exit!
842 */
843 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100844gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845{
846 cmdmod_T save_cmdmod;
847
848 save_cmdmod = cmdmod;
849
850 /* Only exit when there are no changed files */
851 exiting = TRUE;
852# ifdef FEAT_BROWSE
853 cmdmod.browse = TRUE;
854# endif
855# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
856 cmdmod.confirm = TRUE;
857# endif
858 /* If there are changed buffers, present the user with a dialog if
859 * possible, otherwise give an error message. */
Bram Moolenaar027387f2016-01-02 22:25:52 +0100860 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 getout(0);
862
863 exiting = FALSE;
864 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000865 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866}
867#endif
868
869/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200870 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 * first font name that works is used. If none is found, use the default
872 * font.
873 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
874 * Return OK when able to set the font. When it failed FAIL is returned and
875 * the fonts are unchanged.
876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100878gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879{
880#define FONTLEN 320
881 char_u font_name[FONTLEN];
882 int font_list_empty = FALSE;
883 int ret = FAIL;
884
885 if (!gui.in_use)
886 return FAIL;
887
888 font_name[0] = NUL;
889 if (*font_list == NUL)
890 font_list_empty = TRUE;
891 else
892 {
893#ifdef FEAT_XFONTSET
894 /* When using a fontset, the whole list of fonts is one name. */
895 if (fontset)
896 ret = gui_mch_init_font(font_list, TRUE);
897 else
898#endif
899 while (*font_list != NUL)
900 {
901 /* Isolate one comma separated font name. */
902 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
903
904 /* Careful!!! The Win32 version of gui_mch_init_font(), when
905 * called with "*" will change p_guifont to the selected font
906 * name, which frees the old value. This makes font_list
907 * invalid. Thus when OK is returned here, font_list must no
908 * longer be used! */
909 if (gui_mch_init_font(font_name, FALSE) == OK)
910 {
Bram Moolenaar13505972019-01-24 15:04:48 +0100911#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 /* If it's a Unicode font, try setting 'guifontwide' to a
913 * similar double-width font. */
914 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
915 && strstr((char *)font_name, "10646") != NULL)
916 set_guifontwide(font_name);
917#endif
918 ret = OK;
919 break;
920 }
921 }
922 }
923
924 if (ret != OK
925 && STRCMP(font_list, "*") != 0
926 && (font_list_empty || gui.norm_font == NOFONT))
927 {
928 /*
929 * Couldn't load any font in 'font_list', keep the current font if
930 * there is one. If 'font_list' is empty, or if there is no current
931 * font, tell gui_mch_init_font() to try to find a font we can load.
932 */
933 ret = gui_mch_init_font(NULL, FALSE);
934 }
935
936 if (ret == OK)
937 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200938#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 /* Set normal font as current font */
940# ifdef FEAT_XFONTSET
941 if (gui.fontset != NOFONTSET)
942 gui_mch_set_fontset(gui.fontset);
943 else
944# endif
945 gui_mch_set_font(gui.norm_font);
946#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100947 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 }
949
950 return ret;
951}
952
Bram Moolenaar13505972019-01-24 15:04:48 +0100953#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954/*
955 * Try setting 'guifontwide' to a font twice as wide as "name".
956 */
957 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100958set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959{
960 int i = 0;
961 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
962 char_u *wp = NULL;
963 char_u *p;
964 GuiFont font;
965
966 wp = wide_name;
967 for (p = name; *p != NUL; ++p)
968 {
969 *wp++ = *p;
970 if (*p == '-')
971 {
972 ++i;
973 if (i == 6) /* font type: change "--" to "-*-" */
974 {
975 if (p[1] == '-')
976 *wp++ = '*';
977 }
978 else if (i == 12) /* found the width */
979 {
980 ++p;
981 i = getdigits(&p);
982 if (i != 0)
983 {
984 /* Double the width specification. */
985 sprintf((char *)wp, "%d%s", i * 2, p);
986 font = gui_mch_get_font(wide_name, FALSE);
987 if (font != NOFONT)
988 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000989 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 gui.wide_font = font;
991 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000992 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
994 }
995 break;
996 }
997 }
998 }
999}
Bram Moolenaar13505972019-01-24 15:04:48 +01001000#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002/*
1003 * Get the font for 'guifontwide'.
1004 * Return FAIL for an invalid font name.
1005 */
1006 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001007gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
1009 GuiFont font = NOFONT;
1010 char_u font_name[FONTLEN];
1011 char_u *p;
1012
1013 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
1014 return OK; /* Will give an error message later. */
1015
1016 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1017 {
1018 for (p = p_guifontwide; *p != NUL; )
1019 {
1020 /* Isolate one comma separated font name. */
1021 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1022 font = gui_mch_get_font(font_name, FALSE);
1023 if (font != NOFONT)
1024 break;
1025 }
1026 if (font == NOFONT)
1027 return FAIL;
1028 }
1029
1030 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001031#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
1033 if (font != NOFONT && gui.norm_font != NOFONT
1034 && pango_font_description_equal(font, gui.norm_font))
1035 {
1036 gui.wide_font = NOFONT;
1037 gui_mch_free_font(font);
1038 }
1039 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001042#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001043 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001044#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001045 /*
1046 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1047 * support those fonts for 'guifontwide'.
1048 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 return OK;
1051}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
1053 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001054gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
1056 gui.row = row;
1057 gui.col = col;
1058}
1059
1060/*
1061 * gui_check_pos - check if the cursor is on the screen.
1062 */
1063 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001064gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
1066 if (gui.row >= screen_Rows)
1067 gui.row = screen_Rows - 1;
1068 if (gui.col >= screen_Columns)
1069 gui.col = screen_Columns - 1;
1070 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1071 gui.cursor_is_valid = FALSE;
1072}
1073
1074/*
1075 * Redraw the cursor if necessary or when forced.
1076 * Careful: The contents of ScreenLines[] must match what is on the screen,
1077 * otherwise this goes wrong. May need to call out_flush() first.
1078 */
1079 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001080gui_update_cursor(
1081 int force, /* when TRUE, update even when not moved */
1082 int clear_selection)/* clear selection under cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
1084 int cur_width = 0;
1085 int cur_height = 0;
1086 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001087 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001089#ifdef FEAT_TERMINAL
1090 guicolor_T shape_fg = INVALCOLOR;
1091 guicolor_T shape_bg = INVALCOLOR;
1092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1094 int cattr; /* cursor attributes */
1095 int attr;
1096 attrentry_T *aep = NULL;
1097
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001098 /* Don't update the cursor when halfway busy scrolling or the screen size
1099 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1100 if (!can_update_cursor || screen_Columns != gui.num_cols
1101 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 return;
1103
1104 gui_check_pos();
1105 if (!gui.cursor_is_valid || force
1106 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1107 {
1108 gui_undraw_cursor();
1109 if (gui.row < 0)
1110 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001111#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1113 im_set_position(gui.row, gui.col);
1114#endif
1115 gui.cursor_row = gui.row;
1116 gui.cursor_col = gui.col;
1117
1118 /* Only write to the screen after ScreenLines[] has been initialized */
1119 if (!screen_cleared || ScreenLines == NULL)
1120 return;
1121
1122 /* Clear the selection if we are about to write over it */
1123 if (clear_selection)
1124 clip_may_clear_selection(gui.row, gui.row);
1125 /* Check that the cursor is inside the shell (resizing may have made
1126 * it invalid) */
1127 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1128 return;
1129
1130 gui.cursor_is_valid = TRUE;
1131
1132 /*
1133 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001134 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001136#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001137 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001138 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001140#endif
1141 shape = &shape_table[get_shape_idx(FALSE)];
1142 if (State & LANGMAP)
1143 id = shape->id_lm;
1144 else
1145 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146
1147 /* get the colors and attributes for the cursor. Default is inverted */
1148 cfg = INVALCOLOR;
1149 cbg = INVALCOLOR;
1150 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001151 gui_mch_set_blinking(shape->blinkwait,
1152 shape->blinkon,
1153 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001154 if (shape->blinkwait == 0 || shape->blinkon == 0
1155 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001156 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001157#ifdef FEAT_TERMINAL
1158 if (shape_bg != INVALCOLOR)
1159 {
1160 cattr = 0;
1161 cfg = shape_fg;
1162 cbg = shape_bg;
1163 }
1164 else
1165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 if (id > 0)
1167 {
1168 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001169#if defined(HAVE_INPUT_METHOD) || defined(FEAT_HANGULIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {
1171 static int iid;
1172 guicolor_T fg, bg;
1173
Bram Moolenaarc236c162008-07-13 17:41:49 +00001174 if (
Bram Moolenaar28944fe2018-02-04 19:01:31 +01001175# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001176 preedit_get_status()
1177# else
1178 im_get_status()
1179# endif
1180 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
1182 iid = syn_name2id((char_u *)"CursorIM");
1183 if (iid > 0)
1184 {
1185 syn_id2colors(iid, &fg, &bg);
1186 if (bg != INVALCOLOR)
1187 cbg = bg;
1188 if (fg != INVALCOLOR)
1189 cfg = fg;
1190 }
1191 }
1192 }
1193#endif
1194 }
1195
1196 /*
1197 * Get the attributes for the character under the cursor.
1198 * When no cursor color was given, use the character color.
1199 */
1200 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1201 if (attr > HL_ALL)
1202 aep = syn_gui_attr2entry(attr);
1203 if (aep != NULL)
1204 {
1205 attr = aep->ae_attr;
1206 if (cfg == INVALCOLOR)
1207 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1208 : aep->ae_u.gui.fg_color);
1209 if (cbg == INVALCOLOR)
1210 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1211 : aep->ae_u.gui.bg_color);
1212 }
1213 if (cfg == INVALCOLOR)
1214 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1215 if (cbg == INVALCOLOR)
1216 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1217
1218#ifdef FEAT_XIM
1219 if (aep != NULL)
1220 {
1221 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1222 : aep->ae_u.gui.bg_color);
1223 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1224 : aep->ae_u.gui.fg_color);
1225 if (xim_bg_color == INVALCOLOR)
1226 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1227 : gui.back_pixel;
1228 if (xim_fg_color == INVALCOLOR)
1229 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1230 : gui.norm_pixel;
1231 }
1232 else
1233 {
1234 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1235 : gui.back_pixel;
1236 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1237 : gui.norm_pixel;
1238 }
1239#endif
1240
1241 attr &= ~HL_INVERSE;
1242 if (cattr & HL_INVERSE)
1243 {
1244 cc = cbg;
1245 cbg = cfg;
1246 cfg = cc;
1247 }
1248 cattr &= ~HL_INVERSE;
1249
1250 /*
1251 * When we don't have window focus, draw a hollow cursor.
1252 */
1253 if (!gui.in_focus)
1254 {
1255 gui_mch_draw_hollow_cursor(cbg);
1256 return;
1257 }
1258
1259 old_hl_mask = gui.highlight_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001260 if (shape->shape == SHAPE_BLOCK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261#ifdef FEAT_HANGULIN
1262 || composing_hangul
1263#endif
1264 )
1265 {
1266 /*
1267 * Draw the text character with the cursor colors. Use the
1268 * character attributes plus the cursor attributes.
1269 */
1270 gui.highlight_mask = (cattr | attr);
1271#ifdef FEAT_HANGULIN
1272 if (composing_hangul)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001273 {
1274 char_u *comp_buf;
1275 int comp_len;
1276
1277 comp_buf = hangul_composing_buffer_get(&comp_len);
1278 if (comp_buf)
1279 {
1280 (void)gui_outstr_nowrap(comp_buf, comp_len,
1281 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
1282 cfg, cbg, 0);
1283 vim_free(comp_buf);
1284 }
1285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 else
1287#endif
1288 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1289 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1290 }
1291 else
1292 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001293#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 int col_off = FALSE;
1295#endif
1296 /*
1297 * First draw the partial cursor, then overwrite with the text
1298 * character, using a transparent background.
1299 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001300 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 {
1302 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001303 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 }
1305 else
1306 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001307 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 cur_width = gui.char_width;
1309 }
Bram Moolenaar367329b2007-08-30 11:53:22 +00001310 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1311 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
1313 /* Double wide character. */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001314 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001316#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 if (CURSOR_BAR_RIGHT)
1318 {
1319 /* gui.col points to the left halve of the character but
1320 * the vertical line needs to be on the right halve.
1321 * A double-wide horizontal line is also drawn from the
1322 * right halve in gui_mch_draw_part_cursor(). */
1323 col_off = TRUE;
1324 ++gui.col;
1325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001329#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if (col_off)
1331 --gui.col;
1332#endif
1333
1334#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1335 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1336 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1337 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1338 (guicolor_T)0, (guicolor_T)0, 0);
1339#endif
1340 }
1341 gui.highlight_mask = old_hl_mask;
1342 }
1343}
1344
1345#if defined(FEAT_MENU) || defined(PROTO)
1346 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001347gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001349# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (gui.menu_is_active && gui.in_use)
1351 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1352# endif
1353}
1354#endif
1355
1356/*
1357 * Position the various GUI components (text area, menu). The vertical
1358 * scrollbars are NOT handled here. See gui_update_scrollbars().
1359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001361gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362{
1363 int text_area_x;
1364 int text_area_y;
1365 int text_area_width;
1366 int text_area_height;
1367
1368 /* avoid that moving components around generates events */
1369 ++hold_gui_events;
1370
1371 text_area_x = 0;
1372 if (gui.which_scrollbars[SBAR_LEFT])
1373 text_area_x += gui.scrollbar_width;
1374
1375 text_area_y = 0;
1376#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1377 gui.menu_width = total_width;
1378 if (gui.menu_is_active)
1379 text_area_y += gui.menu_height;
1380#endif
1381#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1382 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1383 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1384#endif
1385
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001386# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001387 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001388 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001389 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001390#endif
1391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1393 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1394 {
1395# ifdef FEAT_GUI_ATHENA
1396 gui_mch_set_toolbar_pos(0, text_area_y,
1397 gui.menu_width, gui.toolbar_height);
1398# endif
1399 text_area_y += gui.toolbar_height;
1400 }
1401#endif
1402
1403 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1404 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1405
1406 gui_mch_set_text_area_pos(text_area_x,
1407 text_area_y,
1408 text_area_width,
1409 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001410#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 + xim_get_status_area_height()
1412#endif
1413 );
1414#ifdef FEAT_MENU
1415 gui_position_menu();
1416#endif
1417 if (gui.which_scrollbars[SBAR_BOTTOM])
1418 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1419 text_area_x,
1420 text_area_y + text_area_height,
1421 text_area_width,
1422 gui.scrollbar_height);
1423 gui.left_sbar_x = 0;
1424 gui.right_sbar_x = text_area_x + text_area_width;
1425
1426 --hold_gui_events;
1427}
1428
Bram Moolenaar02743632005-07-25 20:42:36 +00001429/*
1430 * Get the width of the widgets and decorations to the side of the text area.
1431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001433gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 int base_width;
1436
1437 base_width = 2 * gui.border_offset;
1438 if (gui.which_scrollbars[SBAR_LEFT])
1439 base_width += gui.scrollbar_width;
1440 if (gui.which_scrollbars[SBAR_RIGHT])
1441 base_width += gui.scrollbar_width;
1442 return base_width;
1443}
1444
Bram Moolenaar02743632005-07-25 20:42:36 +00001445/*
1446 * Get the height of the widgets and decorations above and below the text area.
1447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001449gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451 int base_height;
1452
1453 base_height = 2 * gui.border_offset;
1454 if (gui.which_scrollbars[SBAR_BOTTOM])
1455 base_height += gui.scrollbar_height;
1456#ifdef FEAT_GUI_GTK
1457 /* We can't take the sizes properly into account until anything is
1458 * realized. Therefore we recalculate all the values here just before
1459 * setting the size. (--mdcki) */
1460#else
1461# ifdef FEAT_MENU
1462 if (gui.menu_is_active)
1463 base_height += gui.menu_height;
1464# endif
1465# ifdef FEAT_TOOLBAR
1466 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1467# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1468 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1469# else
1470 base_height += gui.toolbar_height;
1471# endif
1472# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001473# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1474 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001475 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001476 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478# ifdef FEAT_FOOTER
1479 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1480 base_height += gui.footer_height;
1481# endif
1482# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1483 base_height += gui_mch_text_area_extra_height();
1484# endif
1485#endif
1486 return base_height;
1487}
1488
1489/*
1490 * Should be called after the GUI shell has been resized. Its arguments are
1491 * the new width and height of the shell in pixels.
1492 */
1493 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001494gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
1496 static int busy = FALSE;
1497
1498 if (!gui.shell_created) /* ignore when still initializing */
1499 return;
1500
1501 /*
1502 * Can't resize the screen while it is being redrawn. Remember the new
1503 * size and handle it later.
1504 */
1505 if (updating_screen || busy)
1506 {
1507 new_pixel_width = pixel_width;
1508 new_pixel_height = pixel_height;
1509 return;
1510 }
1511
1512again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001513 new_pixel_width = 0;
1514 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 busy = TRUE;
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 /* Flush pending output before redrawing */
1518 out_flush();
1519
1520 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001521 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
1523 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 /*
1527 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1528 * at the last line here (why does it have to be one row too low?).
1529 */
1530 if (State == ASKMORE || State == CONFIRM)
1531 gui.row = gui.num_rows;
1532
1533 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1534 * the safe side. */
1535 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1536 || gui.num_rows != Rows || gui.num_cols != Columns)
1537 shell_resized();
1538
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 gui_update_scrollbars(TRUE);
1540 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001541#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 xim_set_status_area();
1543#endif
1544
1545 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001546
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001547 /* We may have been called again while redrawing the screen.
1548 * Need to do it all again with the latest size then. But only if the size
1549 * actually changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if (new_pixel_height)
1551 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001552 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1553 {
1554 new_pixel_width = 0;
1555 new_pixel_height = 0;
1556 }
1557 else
1558 {
1559 pixel_width = new_pixel_width;
1560 pixel_height = new_pixel_height;
1561 goto again;
1562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 }
1564}
1565
1566/*
1567 * Check if gui_resize_shell() must be called.
1568 */
1569 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001570gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 if (new_pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 /* careful: gui_resize_shell() may postpone the resize again if we
1574 * were called indirectly by it */
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001575 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576}
1577
1578 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001579gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 Rows = gui.num_rows;
1582 Columns = gui.num_cols;
1583 return OK;
1584}
1585
1586/*
1587 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001588 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1589 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001590 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1591 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001594gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001595 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001596 int fit_to_display,
1597 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
1599 int base_width;
1600 int base_height;
1601 int width;
1602 int height;
1603 int min_width;
1604 int min_height;
1605 int screen_w;
1606 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001607#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001608 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001609 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001610#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001611 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
1613 if (!gui.shell_created)
1614 return;
1615
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001616#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 /* If not setting to a user specified size and maximized, calculate the
1618 * number of characters that fit in the maximized window. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001619 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1620 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 gui_mch_newfont();
1623 return;
1624 }
1625#endif
1626
1627 base_width = gui_get_base_width();
1628 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001629 if (fit_to_display)
1630 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001631 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001632
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001633 width = Columns * gui.char_width + base_width;
1634 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636 if (fit_to_display)
1637 {
1638 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001639 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 {
1641 Columns = (screen_w - base_width) / gui.char_width;
1642 if (Columns < MIN_COLUMNS)
1643 Columns = MIN_COLUMNS;
1644 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001645#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001646 ++did_adjust;
1647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001649 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
1651 Rows = (screen_h - base_height) / gui.char_height;
1652 check_shellsize();
1653 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001654#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001655 ++did_adjust;
1656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001658#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001659 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1660 && height + gui.char_height >= screen_h))
1661 /* don't unmaximize if at maximum size */
1662 un_maximize = FALSE;
1663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001665 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 gui.num_cols = Columns;
1667 gui.num_rows = Rows;
1668
1669 min_width = base_width + MIN_COLUMNS * gui.char_width;
1670 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001671 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001672
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001673#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001674 if (un_maximize)
1675 {
1676 /* If the window size is smaller than the screen unmaximize the
1677 * window, otherwise resizing won't work. */
1678 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1679 if ((width + gui.char_width < screen_w
1680 || height + gui.char_height * 2 < screen_h)
1681 && gui_mch_maximized())
1682 gui_mch_unmaximize();
1683 }
1684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
1686 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001687 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001689 if (fit_to_display && x >= 0 && y >= 0)
1690 {
1691 /* Some window managers put the Vim window left of/above the screen.
1692 * Only change the position if it wasn't already negative before
1693 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 gui_mch_update();
1695 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1696 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1697 }
1698
1699 gui_position_components(width);
1700 gui_update_scrollbars(TRUE);
1701 gui_reset_scroll_region();
1702}
1703
1704/*
1705 * Called when Rows and/or Columns has changed.
1706 */
1707 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001708gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 gui_reset_scroll_region();
1711}
1712
1713/*
1714 * Make scroll region cover whole screen.
1715 */
1716 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001717gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 gui.scroll_region_top = 0;
1720 gui.scroll_region_bot = gui.num_rows - 1;
1721 gui.scroll_region_left = 0;
1722 gui.scroll_region_right = gui.num_cols - 1;
1723}
1724
1725 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001726gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 if (mask > HL_ALL) /* highlight code */
1729 gui.highlight_mask = mask;
1730 else /* mask */
1731 gui.highlight_mask |= mask;
1732}
1733
1734 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001735gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736{
1737 if (mask > HL_ALL) /* highlight code */
1738 gui.highlight_mask = HL_NORMAL;
1739 else /* mask */
1740 gui.highlight_mask &= ~mask;
1741}
1742
1743/*
1744 * Clear a rectangular region of the screen from text pos (row1, col1) to
1745 * (row2, col2) inclusive.
1746 */
1747 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001748gui_clear_block(
1749 int row1,
1750 int col1,
1751 int row2,
1752 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 /* Clear the selection if we are about to write over it */
1755 clip_may_clear_selection(row1, row2);
1756
1757 gui_mch_clear_block(row1, col1, row2, col2);
1758
1759 /* Invalidate cursor if it was in this block */
1760 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1761 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1762 gui.cursor_is_valid = FALSE;
1763}
1764
1765/*
1766 * Write code to update the cursor later. This avoids the need to flush the
1767 * output buffer before calling gui_update_cursor().
1768 */
1769 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001770gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001772 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773}
1774
1775 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001776gui_write(
1777 char_u *s,
1778 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779{
1780 char_u *p;
1781 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 int force_scrollbar = FALSE;
1784 static win_T *old_curwin = NULL;
1785
1786/* #define DEBUG_GUI_WRITE */
1787#ifdef DEBUG_GUI_WRITE
1788 {
1789 int i;
1790 char_u *str;
1791
1792 printf("gui_write(%d):\n ", len);
1793 for (i = 0; i < len; i++)
1794 if (s[i] == ESC)
1795 {
1796 if (i != 0)
1797 printf("\n ");
1798 printf("<ESC>");
1799 }
1800 else
1801 {
1802 str = transchar_byte(s[i]);
1803 if (str[0] && str[1])
1804 printf("<%s>", (char *)str);
1805 else
1806 printf("%s", (char *)str);
1807 }
1808 printf("\n");
1809 }
1810#endif
1811 while (len)
1812 {
1813 if (s[0] == ESC && s[1] == '|')
1814 {
1815 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001816 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 {
1818 arg1 = getdigits(&p);
1819 if (p > s + len)
1820 break;
1821 if (*p == ';')
1822 {
1823 ++p;
1824 arg2 = getdigits(&p);
1825 if (p > s + len)
1826 break;
1827 }
1828 }
1829 switch (*p)
1830 {
1831 case 'C': /* Clear screen */
1832 clip_scroll_selection(9999);
1833 gui_mch_clear_all();
1834 gui.cursor_is_valid = FALSE;
1835 force_scrollbar = TRUE;
1836 break;
1837 case 'M': /* Move cursor */
1838 gui_set_cursor(arg1, arg2);
1839 break;
1840 case 's': /* force cursor (shape) update */
1841 force_cursor = TRUE;
1842 break;
1843 case 'R': /* Set scroll region */
1844 if (arg1 < arg2)
1845 {
1846 gui.scroll_region_top = arg1;
1847 gui.scroll_region_bot = arg2;
1848 }
1849 else
1850 {
1851 gui.scroll_region_top = arg2;
1852 gui.scroll_region_bot = arg1;
1853 }
1854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 case 'V': /* Set vertical scroll region */
1856 if (arg1 < arg2)
1857 {
1858 gui.scroll_region_left = arg1;
1859 gui.scroll_region_right = arg2;
1860 }
1861 else
1862 {
1863 gui.scroll_region_left = arg2;
1864 gui.scroll_region_right = arg1;
1865 }
1866 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 case 'd': /* Delete line */
1868 gui_delete_lines(gui.row, 1);
1869 break;
1870 case 'D': /* Delete lines */
1871 gui_delete_lines(gui.row, arg1);
1872 break;
1873 case 'i': /* Insert line */
1874 gui_insert_lines(gui.row, 1);
1875 break;
1876 case 'I': /* Insert lines */
1877 gui_insert_lines(gui.row, arg1);
1878 break;
1879 case '$': /* Clear to end-of-line */
1880 gui_clear_block(gui.row, gui.col, gui.row,
1881 (int)Columns - 1);
1882 break;
1883 case 'h': /* Turn on highlighting */
1884 gui_start_highlight(arg1);
1885 break;
1886 case 'H': /* Turn off highlighting */
1887 gui_stop_highlight(arg1);
1888 break;
1889 case 'f': /* flash the window (visual bell) */
1890 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1891 break;
1892 default:
1893 p = s + 1; /* Skip the ESC */
1894 break;
1895 }
1896 len -= (int)(++p - s);
1897 s = p;
1898 }
1899 else if (
1900#ifdef EBCDIC
1901 CtrlChar(s[0]) != 0 /* Ctrl character */
1902#else
1903 s[0] < 0x20 /* Ctrl character */
1904#endif
1905#ifdef FEAT_SIGN_ICONS
1906 && s[0] != SIGN_BYTE
1907# ifdef FEAT_NETBEANS_INTG
1908 && s[0] != MULTISIGN_BYTE
1909# endif
1910#endif
1911 )
1912 {
1913 if (s[0] == '\n') /* NL */
1914 {
1915 gui.col = 0;
1916 if (gui.row < gui.scroll_region_bot)
1917 gui.row++;
1918 else
1919 gui_delete_lines(gui.scroll_region_top, 1);
1920 }
1921 else if (s[0] == '\r') /* CR */
1922 {
1923 gui.col = 0;
1924 }
1925 else if (s[0] == '\b') /* Backspace */
1926 {
1927 if (gui.col)
1928 --gui.col;
1929 }
1930 else if (s[0] == Ctrl_L) /* cursor-right */
1931 {
1932 ++gui.col;
1933 }
1934 else if (s[0] == Ctrl_G) /* Beep */
1935 {
1936 gui_mch_beep();
1937 }
1938 /* Other Ctrl character: shouldn't happen! */
1939
1940 --len; /* Skip this char */
1941 ++s;
1942 }
1943 else
1944 {
1945 p = s;
1946 while (len > 0 && (
1947#ifdef EBCDIC
1948 CtrlChar(*p) == 0
1949#else
1950 *p >= 0x20
1951#endif
1952#ifdef FEAT_SIGN_ICONS
1953 || *p == SIGN_BYTE
1954# ifdef FEAT_NETBEANS_INTG
1955 || *p == MULTISIGN_BYTE
1956# endif
1957#endif
1958 ))
1959 {
1960 len--;
1961 p++;
1962 }
1963 gui_outstr(s, (int)(p - s));
1964 s = p;
1965 }
1966 }
1967
1968 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1969 * set). */
1970 if (force_cursor)
1971 gui_update_cursor(TRUE, TRUE);
1972
1973 /* When switching to another window the dragging must have stopped.
1974 * Required for GTK, dragged_sb isn't reset. */
1975 if (old_curwin != curwin)
1976 gui.dragged_sb = SBAR_NONE;
1977
1978 /* Update the scrollbars after clearing the screen or when switched
1979 * to another window.
1980 * Update the horizontal scrollbar always, it's difficult to check all
1981 * situations where it might change. */
1982 if (force_scrollbar || old_curwin != curwin)
1983 gui_update_scrollbars(force_scrollbar);
1984 else
1985 gui_update_horiz_scrollbar(FALSE);
1986 old_curwin = curwin;
1987
1988 /*
1989 * We need to make sure this is cleared since Athena doesn't tell us when
1990 * he is done dragging. Do the same for GTK.
1991 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001992#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 gui.dragged_sb = SBAR_NONE;
1994#endif
1995
Bram Moolenaara338adc2018-01-31 20:51:47 +01001996 gui_may_flush(); /* In case vim decides to take a nap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997}
1998
1999/*
2000 * When ScreenLines[] is invalid, updating the cursor should not be done, it
2001 * produces wrong results. Call gui_dont_update_cursor() before that code and
2002 * gui_can_update_cursor() afterwards.
2003 */
2004 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02002005gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 if (gui.in_use)
2008 {
2009 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02002010 if (undraw)
2011 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 can_update_cursor = FALSE;
2013 }
2014}
2015
2016 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002017gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018{
2019 can_update_cursor = TRUE;
2020 /* No need to update the cursor right now, there is always more output
2021 * after scrolling. */
2022}
2023
Bram Moolenaara338adc2018-01-31 20:51:47 +01002024/*
2025 * Disable issuing gui_mch_flush().
2026 */
2027 void
2028gui_disable_flush(void)
2029{
2030 ++disable_flush;
2031}
2032
2033/*
2034 * Enable issuing gui_mch_flush().
2035 */
2036 void
2037gui_enable_flush(void)
2038{
2039 --disable_flush;
2040}
2041
2042/*
2043 * Issue gui_mch_flush() if it is not disabled.
2044 */
2045 void
2046gui_may_flush(void)
2047{
2048 if (disable_flush == 0)
2049 gui_mch_flush();
2050}
2051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002053gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
2055 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
2058 if (len == 0)
2059 return;
2060
2061 if (len < 0)
2062 len = (int)STRLEN(s);
2063
2064 while (len > 0)
2065 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (has_mbyte)
2067 {
2068 /* Find out how many chars fit in the current line. */
2069 cells = 0;
2070 for (this_len = 0; this_len < len; )
2071 {
2072 cells += (*mb_ptr2cells)(s + this_len);
2073 if (gui.col + cells > Columns)
2074 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002075 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 }
2077 if (this_len > len)
2078 this_len = len; /* don't include following composing char */
2079 }
2080 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (gui.col + len > Columns)
2082 this_len = Columns - gui.col;
2083 else
2084 this_len = len;
2085
2086 (void)gui_outstr_nowrap(s, this_len,
2087 0, (guicolor_T)0, (guicolor_T)0, 0);
2088 s += this_len;
2089 len -= this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 /* fill up for a double-width char that doesn't fit. */
2091 if (len > 0 && gui.col < Columns)
2092 (void)gui_outstr_nowrap((char_u *)" ", 1,
2093 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 /* The cursor may wrap to the next line. */
2095 if (gui.col >= Columns)
2096 {
2097 gui.col = 0;
2098 gui.row++;
2099 }
2100 }
2101}
2102
2103/*
2104 * Output one character (may be one or two display cells).
2105 * Caller must check for valid "off".
2106 * Returns FAIL or OK, just like gui_outstr_nowrap().
2107 */
2108 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002109gui_screenchar(
2110 int off, /* Offset from start of screen */
2111 int flags,
2112 guicolor_T fg, /* colors for cursor */
2113 guicolor_T bg, /* colors for cursor */
2114 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 char_u buf[MB_MAXBYTES + 1];
2117
2118 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2119 if (enc_utf8 && ScreenLines[off] == 0)
2120 return OK;
2121
2122 if (enc_utf8 && ScreenLinesUC[off] != 0)
2123 /* Draw UTF-8 multi-byte character. */
2124 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2125 flags, fg, bg, back);
2126
2127 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2128 {
2129 buf[0] = ScreenLines[off];
2130 buf[1] = ScreenLines2[off];
2131 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2132 }
2133
2134 /* Draw non-multi-byte character or DBCS character. */
2135 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002136 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138}
2139
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002140#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141/*
2142 * Output the string at the given screen position. This is used in place
2143 * of gui_screenchar() where possible because Pango needs as much context
2144 * as possible to work nicely. It's a lot faster as well.
2145 */
2146 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002147gui_screenstr(
2148 int off, /* Offset from start of screen */
2149 int len, /* string length in screen cells */
2150 int flags,
2151 guicolor_T fg, /* colors for cursor */
2152 guicolor_T bg, /* colors for cursor */
2153 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
2155 char_u *buf;
2156 int outlen = 0;
2157 int i;
2158 int retval;
2159
2160 if (len <= 0) /* "cannot happen"? */
2161 return OK;
2162
2163 if (enc_utf8)
2164 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002165 buf = alloc(len * MB_MAXBYTES + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (buf == NULL)
2167 return OK; /* not much we could do here... */
2168
2169 for (i = off; i < off + len; ++i)
2170 {
2171 if (ScreenLines[i] == 0)
2172 continue; /* skip second half of double-width char */
2173
2174 if (ScreenLinesUC[i] == 0)
2175 buf[outlen++] = ScreenLines[i];
2176 else
2177 outlen += utfc_char2bytes(i, buf + outlen);
2178 }
2179
2180 buf[outlen] = NUL; /* only to aid debugging */
2181 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2182 vim_free(buf);
2183
2184 return retval;
2185 }
2186 else if (enc_dbcs == DBCS_JPNU)
2187 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002188 buf = alloc(len * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 if (buf == NULL)
2190 return OK; /* not much we could do here... */
2191
2192 for (i = off; i < off + len; ++i)
2193 {
2194 buf[outlen++] = ScreenLines[i];
2195
2196 /* handle double-byte single-width char */
2197 if (ScreenLines[i] == 0x8e)
2198 buf[outlen++] = ScreenLines2[i];
2199 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2200 buf[outlen++] = ScreenLines[++i];
2201 }
2202
2203 buf[outlen] = NUL; /* only to aid debugging */
2204 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2205 vim_free(buf);
2206
2207 return retval;
2208 }
2209 else
2210 {
2211 return gui_outstr_nowrap(&ScreenLines[off], len,
2212 flags, fg, bg, back);
2213 }
2214}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002215#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
2217/*
2218 * Output the given string at the current cursor position. If the string is
2219 * too long to fit on the line, then it is truncated.
2220 * "flags":
2221 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2222 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002223 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 * background.
2225 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2226 * it.
2227 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2228 * FAIL (the caller should start drawing "back" chars back).
2229 */
2230 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002231gui_outstr_nowrap(
2232 char_u *s,
2233 int len,
2234 int flags,
2235 guicolor_T fg, /* colors for cursor */
2236 guicolor_T bg, /* colors for cursor */
2237 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238{
2239 long_u highlight_mask;
2240 long_u hl_mask_todo;
2241 guicolor_T fg_color;
2242 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002243 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002244#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002246 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247# ifdef FEAT_XFONTSET
2248 GuiFontset fontset = NOFONTSET;
2249# endif
2250#endif
2251 attrentry_T *aep = NULL;
2252 int draw_flags;
2253 int col = gui.col;
2254#ifdef FEAT_SIGN_ICONS
2255 int draw_sign = FALSE;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002256 char_u extra[18];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257# ifdef FEAT_NETBEANS_INTG
2258 int multi_sign = FALSE;
2259# endif
2260#endif
2261
2262 if (len < 0)
2263 len = (int)STRLEN(s);
2264 if (len == 0)
2265 return OK;
2266
2267#ifdef FEAT_SIGN_ICONS
2268 if (*s == SIGN_BYTE
2269# ifdef FEAT_NETBEANS_INTG
2270 || *s == MULTISIGN_BYTE
2271# endif
2272 )
2273 {
2274# ifdef FEAT_NETBEANS_INTG
2275 if (*s == MULTISIGN_BYTE)
2276 multi_sign = TRUE;
2277# endif
2278 /* draw spaces instead */
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002279 if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
2280 (curwin->w_p_nu || curwin->w_p_rnu))
2281 {
2282 sprintf((char *)extra, "%*c ", number_width(curwin), ' ');
2283 s = extra;
2284 }
2285 else
2286 s = (char_u *)" ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 if (len == 1 && col > 0)
2288 --col;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002289 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 draw_sign = TRUE;
2291 highlight_mask = 0;
2292 }
2293 else
2294#endif
2295 if (gui.highlight_mask > HL_ALL)
2296 {
2297 aep = syn_gui_attr2entry(gui.highlight_mask);
2298 if (aep == NULL) /* highlighting not set */
2299 highlight_mask = 0;
2300 else
2301 highlight_mask = aep->ae_attr;
2302 }
2303 else
2304 highlight_mask = gui.highlight_mask;
2305 hl_mask_todo = highlight_mask;
2306
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002307#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 /* Set the font */
2309 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2310 font = aep->ae_u.gui.font;
2311# ifdef FEAT_XFONTSET
2312 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2313 fontset = aep->ae_u.gui.fontset;
2314# endif
2315 else
2316 {
2317# ifdef FEAT_XFONTSET
2318 if (gui.fontset != NOFONTSET)
2319 fontset = gui.fontset;
2320 else
2321# endif
2322 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2323 {
2324 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2325 {
2326 font = gui.boldital_font;
2327 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2328 }
2329 else if (gui.bold_font != NOFONT)
2330 {
2331 font = gui.bold_font;
2332 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2333 }
2334 else
2335 font = gui.norm_font;
2336 }
2337 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2338 {
2339 font = gui.ital_font;
2340 hl_mask_todo &= ~HL_ITALIC;
2341 }
2342 else
2343 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002344
Bram Moolenaardb250522013-06-17 22:43:25 +02002345 /*
2346 * Choose correct wide_font by font. wide_font should be set with font
2347 * at same time in above block. But it will make many "ifdef" nasty
2348 * blocks. So we do it here.
2349 */
2350 if (font == gui.boldital_font && gui.wide_boldital_font)
2351 wide_font = gui.wide_boldital_font;
2352 else if (font == gui.bold_font && gui.wide_bold_font)
2353 wide_font = gui.wide_bold_font;
2354 else if (font == gui.ital_font && gui.wide_ital_font)
2355 wide_font = gui.wide_ital_font;
2356 else if (font == gui.norm_font && gui.wide_font)
2357 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359# ifdef FEAT_XFONTSET
2360 if (fontset != NOFONTSET)
2361 gui_mch_set_fontset(fontset);
2362 else
2363# endif
2364 gui_mch_set_font(font);
2365#endif
2366
2367 draw_flags = 0;
2368
2369 /* Set the color */
2370 bg_color = gui.back_pixel;
2371 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2372 {
2373 draw_flags |= DRAW_CURSOR;
2374 fg_color = fg;
2375 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002376 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
2378 else if (aep != NULL)
2379 {
2380 fg_color = aep->ae_u.gui.fg_color;
2381 if (fg_color == INVALCOLOR)
2382 fg_color = gui.norm_pixel;
2383 bg_color = aep->ae_u.gui.bg_color;
2384 if (bg_color == INVALCOLOR)
2385 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002386 sp_color = aep->ae_u.gui.sp_color;
2387 if (sp_color == INVALCOLOR)
2388 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002393 sp_color = fg_color;
2394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
2396 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2397 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002398#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 gui_mch_set_colors(bg_color, fg_color);
2400#else
2401 gui_mch_set_fg_color(bg_color);
2402 gui_mch_set_bg_color(fg_color);
2403#endif
2404 }
2405 else
2406 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002407#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 gui_mch_set_colors(fg_color, bg_color);
2409#else
2410 gui_mch_set_fg_color(fg_color);
2411 gui_mch_set_bg_color(bg_color);
2412#endif
2413 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002414 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 /* Clear the selection if we are about to write over it */
2417 if (!(flags & GUI_MON_NOCLEAR))
2418 clip_may_clear_selection(gui.row, gui.row);
2419
2420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 /* If there's no bold font, then fake it */
2422 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2423 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
2425 /*
2426 * When drawing bold or italic characters the spill-over from the left
2427 * neighbor may be destroyed. Let the caller backup to start redrawing
2428 * just after a blank.
2429 */
2430 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2431 return FAIL;
2432
Bram Moolenaare60acc12011-05-10 16:41:25 +02002433#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 /* If there's no italic font, then fake it.
2435 * For GTK2, we don't need a different font for italic style. */
2436 if (hl_mask_todo & HL_ITALIC)
2437 draw_flags |= DRAW_ITALIC;
2438
2439 /* Do we underline the text? */
2440 if (hl_mask_todo & HL_UNDERLINE)
2441 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002442
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443#else
2444 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002445 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 draw_flags |= DRAW_UNDERL;
2447#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002448 /* Do we undercurl the text? */
2449 if (hl_mask_todo & HL_UNDERCURL)
2450 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002452 /* Do we strikethrough the text? */
2453 if (hl_mask_todo & HL_STRIKETHROUGH)
2454 draw_flags |= DRAW_STRIKE;
2455
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002456 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if (flags & GUI_MON_TRS_CURSOR)
2458 draw_flags |= DRAW_TRANSP;
2459
2460 /*
2461 * Draw the text.
2462 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002463#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 /* The value returned is the length in display cells */
2465 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2466#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (enc_utf8)
2468 {
2469 int start; /* index of bytes to be drawn */
2470 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002471 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 int cn; /* cellwidth of current char */
2473 int i; /* index of current char */
2474 int c; /* current char value */
2475 int cl; /* byte length of current char */
2476 int comping; /* current char is composing */
2477 int scol = col; /* screen column */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002478 int curr_wide = FALSE; /* use 'guifontwide' */
Bram Moolenaar45933962013-01-23 17:43:57 +01002479 int prev_wide = FALSE;
2480 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002481# ifdef MSWIN
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002482 int sep_comp = FALSE; /* Don't separate composing chars. */
Bram Moolenaar13505972019-01-24 15:04:48 +01002483# else
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002484 int sep_comp = TRUE; /* Separate composing chars. */
Bram Moolenaar13505972019-01-24 15:04:48 +01002485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
2487 /* Break the string at a composing character, it has to be drawn on
2488 * top of the previous character. */
2489 start = 0;
2490 cells = 0;
2491 for (i = 0; i < len; i += cl)
2492 {
2493 c = utf_ptr2char(s + i);
2494 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 comping = utf_iscomposing(c);
2496 if (!comping) /* count cells from non-composing chars */
2497 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002498 if (!comping || sep_comp)
2499 {
2500 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002501# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002502 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002503# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002504 && wide_font != NOFONT)
2505 curr_wide = TRUE;
2506 else
2507 curr_wide = FALSE;
2508 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002509 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 if (cl == 0) /* hit end of string */
2511 len = i + cl; /* len must be wrong "cannot happen" */
2512
Bram Moolenaar45933962013-01-23 17:43:57 +01002513 wide_changed = curr_wide != prev_wide;
2514
2515 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 * a composing character. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002517 if (i + cl >= len || (comping && sep_comp && i > start)
2518 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002519# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002521# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 /* No fontset: At least draw char after wide char at
2523 * right position. */
2524 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002526 )
2527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 )
2529 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002530 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 thislen = i - start;
2532 else
2533 thislen = i - start + cl;
2534 if (thislen > 0)
2535 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002536 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002537 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2539 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002540 if (prev_wide)
2541 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 start += thislen;
2543 }
2544 scol += cells;
2545 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002546 /* Adjust to not draw a character which width is changed
2547 * against with last one. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002548 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002550 scol -= cn;
2551 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 }
2553
Bram Moolenaar13505972019-01-24 15:04:48 +01002554# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002555 /* No fontset: draw a space to fill the gap after a wide char
2556 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002558# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002560# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002561 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2563 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002564# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 }
2566 /* Draw a composing char on top of the previous char. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002567 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002569# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002570 /* Carbon ATSUI autodraws composing char over previous char */
2571 gui_mch_draw_string(gui.row, scol, s + i, cl,
2572 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002573# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002574 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2575 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002576# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 start = i + cl;
2578 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002579 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
2581 /* The stuff below assumes "len" is the length in screen columns. */
2582 len = scol - col;
2583 }
2584 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
2586 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (enc_dbcs == DBCS_JPNU)
2588 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 /* Get the length in display cells, this can be different from the
2590 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002591 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002594#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
2596 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2597 gui.col = col + len;
2598
2599 /* May need to invert it when it's part of the selection. */
2600 if (flags & GUI_MON_NOCLEAR)
2601 clip_may_redraw_selection(gui.row, col, len);
2602
2603 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2604 {
2605 /* Invalidate the old physical cursor position if we wrote over it */
2606 if (gui.cursor_row == gui.row
2607 && gui.cursor_col >= col
2608 && gui.cursor_col < col + len)
2609 gui.cursor_is_valid = FALSE;
2610 }
2611
2612#ifdef FEAT_SIGN_ICONS
2613 if (draw_sign)
2614 /* Draw the sign on top of the spaces. */
2615 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002616# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002617 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 if (multi_sign)
2619 netbeans_draw_multisign_indicator(gui.row);
2620# endif
2621#endif
2622
2623 return OK;
2624}
2625
2626/*
2627 * Un-draw the cursor. Actually this just redraws the character at the given
2628 * position. The character just before it too, for when it was in bold.
2629 */
2630 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002631gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
2633 if (gui.cursor_is_valid)
2634 {
2635#ifdef FEAT_HANGULIN
2636 if (composing_hangul
2637 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002638 {
2639 char_u *comp_buf;
2640 int comp_len;
2641
2642 comp_buf = hangul_composing_buffer_get(&comp_len);
2643 if (comp_buf)
2644 {
2645 (void)gui_outstr_nowrap(comp_buf, comp_len,
2646 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2647 gui.norm_pixel, gui.back_pixel, 0);
2648 vim_free(comp_buf);
2649 }
2650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 else
2652 {
2653#endif
2654 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2655 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2656 && gui.cursor_col > 0)
2657 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2658 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2659#ifdef FEAT_HANGULIN
2660 if (composing_hangul)
2661 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2662 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2663 }
2664#endif
2665 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2666 * here in case it wasn't needed to undraw it. */
2667 gui.cursor_is_valid = FALSE;
2668 }
2669}
2670
2671 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002672gui_redraw(
2673 int x,
2674 int y,
2675 int w,
2676 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678 int row1, col1, row2, col2;
2679
2680 row1 = Y_2_ROW(y);
2681 col1 = X_2_COL(x);
2682 row2 = Y_2_ROW(y + h - 1);
2683 col2 = X_2_COL(x + w - 1);
2684
2685 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2686
2687 /*
2688 * We may need to redraw the cursor, but don't take it upon us to change
2689 * its location after a scroll.
2690 * (maybe be more strict even and test col too?)
2691 * These things may be outside the update/clipping region and reality may
2692 * not reflect Vims internal ideas if these operations are clipped away.
2693 */
2694 if (gui.row == gui.cursor_row)
2695 gui_update_cursor(TRUE, TRUE);
2696}
2697
2698/*
2699 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2700 * from col1 to col2 (inclusive).
2701 * Return TRUE when the character before the first drawn character has
2702 * different attributes (may have to be redrawn too).
2703 */
2704 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002705gui_redraw_block(
2706 int row1,
2707 int col1,
2708 int row2,
2709 int col2,
2710 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
2712 int old_row, old_col;
2713 long_u old_hl_mask;
2714 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002715 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 int idx, len;
2717 int back, nback;
2718 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 /* Don't try to update when ScreenLines is not valid */
2722 if (!screen_cleared || ScreenLines == NULL)
2723 return retval;
2724
2725 /* Don't try to draw outside the shell! */
2726 /* Check everything, strange values may be caused by a big border width */
2727 col1 = check_col(col1);
2728 col2 = check_col(col2);
2729 row1 = check_row(row1);
2730 row2 = check_row(row2);
2731
2732 /* Remember where our cursor was */
2733 old_row = gui.row;
2734 old_col = gui.col;
2735 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 orig_col1 = col1;
2737 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 for (gui.row = row1; gui.row <= row2; gui.row++)
2740 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 /* When only half of a double-wide character is in the block, include
2742 * the other half. */
2743 col1 = orig_col1;
2744 col2 = orig_col2;
2745 off = LineOffset[gui.row];
2746 if (enc_dbcs != 0)
2747 {
2748 if (col1 > 0)
2749 col1 -= dbcs_screen_head_off(ScreenLines + off,
2750 ScreenLines + off + col1);
2751 col2 += dbcs_screen_tail_off(ScreenLines + off,
2752 ScreenLines + off + col2);
2753 }
2754 else if (enc_utf8)
2755 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002756 if (ScreenLines[off + col1] == 0)
2757 {
2758 if (col1 > 0)
2759 --col1;
2760 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002761 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002762 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002763 // Make this IEMSGN when it no longer breaks Travis CI.
2764 vim_snprintf((char *)IObuff, IOSIZE,
2765 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2766 (long)gui.row);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002767 msg((char *)IObuff);
Bram Moolenaar9a853462018-12-07 14:10:37 +01002768 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002769 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002770#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2772 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 gui.col = col1;
2776 off = LineOffset[gui.row] + gui.col;
2777 len = col2 - col1 + 1;
2778
2779 /* Find how many chars back this highlighting starts, or where a space
2780 * is. Needed for when the bold trick is used */
2781 for (back = 0; back < col1; ++back)
2782 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2783 || ScreenLines[off - 1 - back] == ' ')
2784 break;
2785 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2786 && ScreenLines[off - 1] != ' ');
2787
2788 /* Break it up in strings of characters with the same attributes. */
2789 /* Print UTF-8 characters individually. */
2790 while (len > 0)
2791 {
2792 first_attr = ScreenAttrs[off];
2793 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002794#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (enc_utf8 && ScreenLinesUC[off] != 0)
2796 {
2797 /* output multi-byte character separately */
2798 nback = gui_screenchar(off, flags,
2799 (guicolor_T)0, (guicolor_T)0, back);
2800 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2801 idx = 2;
2802 else
2803 idx = 1;
2804 }
2805 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2806 {
2807 /* output double-byte, single-width character separately */
2808 nback = gui_screenchar(off, flags,
2809 (guicolor_T)0, (guicolor_T)0, back);
2810 idx = 1;
2811 }
2812 else
2813#endif
2814 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002815#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 for (idx = 0; idx < len; ++idx)
2817 {
2818 if (enc_utf8 && ScreenLines[off + idx] == 0)
2819 continue; /* skip second half of double-width char */
2820 if (ScreenAttrs[off + idx] != first_attr)
2821 break;
2822 }
2823 /* gui_screenstr() takes care of multibyte chars */
2824 nback = gui_screenstr(off, idx, flags,
2825 (guicolor_T)0, (guicolor_T)0, back);
2826#else
2827 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2828 idx++)
2829 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 /* Stop at a multi-byte Unicode character. */
2831 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2832 break;
2833 if (enc_dbcs == DBCS_JPNU)
2834 {
2835 /* Stop at a double-byte single-width char. */
2836 if (ScreenLines[off + idx] == 0x8e)
2837 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002838 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 + off + idx) == 2)
2840 ++idx; /* skip second byte of double-byte char */
2841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 }
2843 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2844 (guicolor_T)0, (guicolor_T)0, back);
2845#endif
2846 }
2847 if (nback == FAIL)
2848 {
2849 /* Must back up to start drawing where a bold or italic word
2850 * starts. */
2851 off -= back;
2852 len += back;
2853 gui.col -= back;
2854 }
2855 else
2856 {
2857 off += idx;
2858 len -= idx;
2859 }
2860 back = 0;
2861 }
2862 }
2863
2864 /* Put the cursor back where it was */
2865 gui.row = old_row;
2866 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002867 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
2869 return retval;
2870}
2871
2872 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002873gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
2875 if (count <= 0)
2876 return;
2877
2878 if (row + count > gui.scroll_region_bot)
2879 /* Scrolled out of region, just blank the lines out */
2880 gui_clear_block(row, gui.scroll_region_left,
2881 gui.scroll_region_bot, gui.scroll_region_right);
2882 else
2883 {
2884 gui_mch_delete_lines(row, count);
2885
2886 /* If the cursor was in the deleted lines it's now gone. If the
2887 * cursor was in the scrolled lines adjust its position. */
2888 if (gui.cursor_row >= row
2889 && gui.cursor_col >= gui.scroll_region_left
2890 && gui.cursor_col <= gui.scroll_region_right)
2891 {
2892 if (gui.cursor_row < row + count)
2893 gui.cursor_is_valid = FALSE;
2894 else if (gui.cursor_row <= gui.scroll_region_bot)
2895 gui.cursor_row -= count;
2896 }
2897 }
2898}
2899
2900 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002901gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902{
2903 if (count <= 0)
2904 return;
2905
2906 if (row + count > gui.scroll_region_bot)
2907 /* Scrolled out of region, just blank the lines out */
2908 gui_clear_block(row, gui.scroll_region_left,
2909 gui.scroll_region_bot, gui.scroll_region_right);
2910 else
2911 {
2912 gui_mch_insert_lines(row, count);
2913
2914 if (gui.cursor_row >= gui.row
2915 && gui.cursor_col >= gui.scroll_region_left
2916 && gui.cursor_col <= gui.scroll_region_right)
2917 {
2918 if (gui.cursor_row <= gui.scroll_region_bot - count)
2919 gui.cursor_row += count;
2920 else if (gui.cursor_row <= gui.scroll_region_bot)
2921 gui.cursor_is_valid = FALSE;
2922 }
2923 }
2924}
2925
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002926#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002927/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002928 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2929 */
2930 static int
2931gui_wait_for_chars_3(
2932 long wtime,
2933 int *interrupted UNUSED,
2934 int ignore_input UNUSED)
2935{
2936 return gui_mch_wait_for_chars(wtime);
2937}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002938#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002939
2940/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002941 * Returns OK if a character was found to be available within the given time,
2942 * or FAIL otherwise.
2943 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002944 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002945gui_wait_for_chars_or_timer(
2946 long wtime,
2947 int *interrupted UNUSED,
2948 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002949{
2950#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002951 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2952 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002953#else
2954 return gui_mch_wait_for_chars(wtime);
2955#endif
2956}
2957
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958/*
2959 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002960 * "wtime" == -1 Wait forever.
2961 * "wtime" == 0 Don't wait.
2962 * "wtime" > 0 Wait wtime milliseconds for a character.
2963 *
2964 * Returns the number of characters read or zero when timed out or interrupted.
2965 * "buf" may be NULL, in which case a non-zero number is returned if characters
2966 * are available.
2967 */
2968 static int
2969gui_wait_for_chars_buf(
2970 char_u *buf,
2971 int maxlen,
2972 long wtime, // don't use "time", MIPS cannot handle it
2973 int tb_change_cnt)
2974{
2975 int retval;
2976
2977#ifdef FEAT_MENU
2978 // If we're going to wait a bit, update the menus and mouse shape for the
2979 // current State.
2980 if (wtime != 0)
2981 gui_update_menus(0);
2982#endif
2983
2984 gui_mch_update();
2985 if (input_available()) // Got char, return immediately
2986 {
2987 if (buf != NULL && !typebuf_changed(tb_change_cnt))
2988 return read_from_input_buf(buf, (long)maxlen);
2989 return 0;
2990 }
2991 if (wtime == 0) // Don't wait for char
2992 return FAIL;
2993
2994 // Before waiting, flush any output to the screen.
2995 gui_mch_flush();
2996
2997 // Blink while waiting for a character.
2998 gui_mch_start_blink();
2999
3000 // Common function to loop until "wtime" is met, while handling timers and
3001 // other callbacks.
3002 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
3003 gui_wait_for_chars_or_timer, NULL);
3004
3005 gui_mch_stop_blink(TRUE);
3006
3007 return retval;
3008}
3009
3010/*
3011 * Wait for a character from the keyboard without actually reading it.
3012 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 * wtime == -1 Wait forever.
3014 * wtime == 0 Don't wait.
3015 * wtime > 0 Wait wtime milliseconds for a character.
3016 * Returns OK if a character was found to be available within the given time,
3017 * or FAIL otherwise.
3018 */
3019 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003020gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003022 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023}
3024
3025/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003026 * Equivalent of mch_inchar() for the GUI.
3027 */
3028 int
3029gui_inchar(
3030 char_u *buf,
3031 int maxlen,
3032 long wtime, /* milli seconds */
3033 int tb_change_cnt)
3034{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003035 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003036}
3037
3038/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003039 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 */
3041 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003042fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
3044 p[0] = (char_u)(col / 128 + ' ' + 1);
3045 p[1] = (char_u)(col % 128 + ' ' + 1);
3046 p[2] = (char_u)(row / 128 + ' ' + 1);
3047 p[3] = (char_u)(row % 128 + ' ' + 1);
3048}
3049
3050/*
3051 * Generic mouse support function. Add a mouse event to the input buffer with
3052 * the given properties.
3053 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3054 * MOUSE_X1, MOUSE_X2
3055 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003056 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3057 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 * x, y --- Coordinates of mouse in pixels.
3059 * repeated_click --- TRUE if this click comes only a short time after a
3060 * previous click.
3061 * modifiers --- Bit field which may be any of the following modifiers
3062 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3063 * This function will ignore drag events where the mouse has not moved to a new
3064 * character.
3065 */
3066 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003067gui_send_mouse_event(
3068 int button,
3069 int x,
3070 int y,
3071 int repeated_click,
3072 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 static int prev_row = 0, prev_col = 0;
3075 static int prev_button = -1;
3076 static int num_clicks = 1;
3077 char_u string[10];
3078 enum key_extra button_char;
3079 int row, col;
3080#ifdef FEAT_CLIPBOARD
3081 int checkfor;
3082 int did_clip = FALSE;
3083#endif
3084
3085 /*
3086 * Scrolling may happen at any time, also while a selection is present.
3087 */
3088 switch (button)
3089 {
3090 case MOUSE_X1:
3091 button_char = KE_X1MOUSE;
3092 goto button_set;
3093 case MOUSE_X2:
3094 button_char = KE_X2MOUSE;
3095 goto button_set;
3096 case MOUSE_4:
3097 button_char = KE_MOUSEDOWN;
3098 goto button_set;
3099 case MOUSE_5:
3100 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003101 goto button_set;
3102 case MOUSE_6:
3103 button_char = KE_MOUSELEFT;
3104 goto button_set;
3105 case MOUSE_7:
3106 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107button_set:
3108 {
3109 /* Don't put events in the input queue now. */
3110 if (hold_gui_events)
3111 return;
3112
3113 string[3] = CSI;
3114 string[4] = KS_EXTRA;
3115 string[5] = (int)button_char;
3116
3117 /* Pass the pointer coordinates of the scroll event so that we
3118 * know which window to scroll. */
3119 row = gui_xy2colrow(x, y, &col);
3120 string[6] = (char_u)(col / 128 + ' ' + 1);
3121 string[7] = (char_u)(col % 128 + ' ' + 1);
3122 string[8] = (char_u)(row / 128 + ' ' + 1);
3123 string[9] = (char_u)(row % 128 + ' ' + 1);
3124
3125 if (modifiers == 0)
3126 add_to_input_buf(string + 3, 7);
3127 else
3128 {
3129 string[0] = CSI;
3130 string[1] = KS_MODIFIER;
3131 string[2] = 0;
3132 if (modifiers & MOUSE_SHIFT)
3133 string[2] |= MOD_MASK_SHIFT;
3134 if (modifiers & MOUSE_CTRL)
3135 string[2] |= MOD_MASK_CTRL;
3136 if (modifiers & MOUSE_ALT)
3137 string[2] |= MOD_MASK_ALT;
3138 add_to_input_buf(string, 10);
3139 }
3140 return;
3141 }
3142 }
3143
3144#ifdef FEAT_CLIPBOARD
3145 /* If a clipboard selection is in progress, handle it */
3146 if (clip_star.state == SELECT_IN_PROGRESS)
3147 {
3148 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3149 return;
3150 }
3151
3152 /* Determine which mouse settings to look for based on the current mode */
3153 switch (get_real_state())
3154 {
3155 case NORMAL_BUSY:
3156 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003157# ifdef FEAT_TERMINAL
3158 case TERMINAL:
3159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 case NORMAL: checkfor = MOUSE_NORMAL; break;
3161 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003162 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 case REPLACE:
3164 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 case VREPLACE:
3166 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 case INSERT:
3168 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3169 case ASKMORE:
3170 case HITRETURN: /* At the more- and hit-enter prompt pass the
3171 mouse event for a click on or below the
3172 message line. */
3173 if (Y_2_ROW(y) >= msg_row)
3174 checkfor = MOUSE_NORMAL;
3175 else
3176 checkfor = MOUSE_RETURN;
3177 break;
3178
3179 /*
3180 * On the command line, use the clipboard selection on all lines
3181 * but the command line. But not when pasting.
3182 */
3183 case CMDLINE:
3184 case CMDLINE+LANGMAP:
3185 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3186 checkfor = MOUSE_NONE;
3187 else
3188 checkfor = MOUSE_COMMAND;
3189 break;
3190
3191 default:
3192 checkfor = MOUSE_NONE;
3193 break;
3194 };
3195
3196 /*
3197 * Allow clipboard selection of text on the command line in "normal"
3198 * modes. Don't do this when dragging the status line, or extending a
3199 * Visual selection.
3200 */
3201 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003202 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 && button != MOUSE_DRAG
3204# ifdef FEAT_MOUSESHAPE
3205 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207# endif
3208 )
3209 checkfor = MOUSE_NONE;
3210
3211 /*
3212 * Use modeless selection when holding CTRL and SHIFT pressed.
3213 */
3214 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3215 checkfor = MOUSE_NONEF;
3216
3217 /*
3218 * In Ex mode, always use modeless selection.
3219 */
3220 if (exmode_active)
3221 checkfor = MOUSE_NONE;
3222
3223 /*
3224 * If the mouse settings say to not use the mouse, use the modeless
3225 * selection. But if Visual is active, assume that only the Visual area
3226 * will be selected.
3227 * Exception: On the command line, both the selection is used and a mouse
3228 * key is send.
3229 */
3230 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 /* Don't do modeless selection in Visual mode. */
3233 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3234 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
3236 /*
3237 * When 'mousemodel' is "popup", shift-left is translated to right.
3238 * But not when also using Ctrl.
3239 */
3240 if (mouse_model_popup() && button == MOUSE_LEFT
3241 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3242 {
3243 button = MOUSE_RIGHT;
3244 modifiers &= ~ MOUSE_SHIFT;
3245 }
3246
3247 /* If the selection is done, allow the right button to extend it.
3248 * If the selection is cleared, allow the right button to start it
3249 * from the cursor position. */
3250 if (button == MOUSE_RIGHT)
3251 {
3252 if (clip_star.state == SELECT_CLEARED)
3253 {
3254 if (State & CMDLINE)
3255 {
3256 col = msg_col;
3257 row = msg_row;
3258 }
3259 else
3260 {
3261 col = curwin->w_wcol;
3262 row = curwin->w_wrow + W_WINROW(curwin);
3263 }
3264 clip_start_selection(col, row, FALSE);
3265 }
3266 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3267 repeated_click);
3268 did_clip = TRUE;
3269 }
3270 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003271 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
3273 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3274 did_clip = TRUE;
3275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
3277 /* Always allow pasting */
3278 if (button != MOUSE_MIDDLE)
3279 {
3280 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3281 return;
3282 if (checkfor != MOUSE_COMMAND)
3283 button = MOUSE_LEFT;
3284 }
3285 repeated_click = FALSE;
3286 }
3287
3288 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003289 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290#endif
3291
3292 /* Don't put events in the input queue now. */
3293 if (hold_gui_events)
3294 return;
3295
3296 row = gui_xy2colrow(x, y, &col);
3297
3298 /*
3299 * If we are dragging and the mouse hasn't moved far enough to be on a
3300 * different character, then don't send an event to vim.
3301 */
3302 if (button == MOUSE_DRAG)
3303 {
3304 if (row == prev_row && col == prev_col)
3305 return;
3306 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3307 if (y < 0)
3308 row = -1;
3309 }
3310
3311 /*
3312 * If topline has changed (window scrolled) since the last click, reset
3313 * repeated_click, because we don't want starting Visual mode when
3314 * clicking on a different character in the text.
3315 */
3316 if (curwin->w_topline != gui_prev_topline
3317#ifdef FEAT_DIFF
3318 || curwin->w_topfill != gui_prev_topfill
3319#endif
3320 )
3321 repeated_click = FALSE;
3322
3323 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3324 string[1] = KS_MOUSE;
3325 string[2] = KE_FILLER;
3326 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3327 {
3328 if (repeated_click)
3329 {
3330 /*
3331 * Handle multiple clicks. They only count if the mouse is still
3332 * pointing at the same character.
3333 */
3334 if (button != prev_button || row != prev_row || col != prev_col)
3335 num_clicks = 1;
3336 else if (++num_clicks > 4)
3337 num_clicks = 1;
3338 }
3339 else
3340 num_clicks = 1;
3341 prev_button = button;
3342 gui_prev_topline = curwin->w_topline;
3343#ifdef FEAT_DIFF
3344 gui_prev_topfill = curwin->w_topfill;
3345#endif
3346
3347 string[3] = (char_u)(button | 0x20);
3348 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3349 }
3350 else
3351 string[3] = (char_u)button;
3352
3353 string[3] |= modifiers;
3354 fill_mouse_coord(string + 4, col, row);
3355 add_to_input_buf(string, 8);
3356
3357 if (row < 0)
3358 prev_row = 0;
3359 else
3360 prev_row = row;
3361 prev_col = col;
3362
3363 /*
3364 * We need to make sure this is cleared since Athena doesn't tell us when
3365 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3366 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003367#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 gui.dragged_sb = SBAR_NONE;
3369#endif
3370}
3371
3372/*
3373 * Convert x and y coordinate to column and row in text window.
3374 * Corrects for multi-byte character.
3375 * returns column in "*colp" and row as return value;
3376 */
3377 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003378gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
3380 int col = check_col(X_2_COL(x));
3381 int row = check_row(Y_2_ROW(y));
3382
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 return row;
3385}
3386
3387#if defined(FEAT_MENU) || defined(PROTO)
3388/*
3389 * Callback function for when a menu entry has been selected.
3390 */
3391 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003392gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003394 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 /* Don't put events in the input queue now. */
3397 if (hold_gui_events)
3398 return;
3399
3400 bytes[0] = CSI;
3401 bytes[1] = KS_MENU;
3402 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003403 add_to_input_buf(bytes, 3);
3404 add_long_to_buf((long_u)menu, bytes);
3405 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406}
3407#endif
3408
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003409static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411/*
3412 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003413 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 * in p_go.
3415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003417gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419#ifdef FEAT_MENU
3420 static int prev_menu_is_active = -1;
3421#endif
3422#ifdef FEAT_TOOLBAR
3423 static int prev_toolbar = -1;
3424 int using_toolbar = FALSE;
3425#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003426#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003427 int using_tabline;
3428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429#ifdef FEAT_FOOTER
3430 static int prev_footer = -1;
3431 int using_footer = FALSE;
3432#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003433#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 static int prev_tearoff = -1;
3435 int using_tearoff = FALSE;
3436#endif
3437
3438 char_u *p;
3439 int i;
3440#ifdef FEAT_MENU
3441 int grey_old, grey_new;
3442 char_u *temp;
3443#endif
3444 win_T *wp;
3445 int need_set_size;
3446 int fix_size;
3447
3448#ifdef FEAT_MENU
3449 if (oldval != NULL && gui.in_use)
3450 {
3451 /*
3452 * Check if the menu's go from grey to non-grey or vise versa.
3453 */
3454 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3455 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3456 if (grey_old != grey_new)
3457 {
3458 temp = p_go;
3459 p_go = oldval;
3460 gui_update_menus(MENU_ALL_MODES);
3461 p_go = temp;
3462 }
3463 }
3464 gui.menu_is_active = FALSE;
3465#endif
3466
3467 for (i = 0; i < 3; i++)
3468 gui.which_scrollbars[i] = FALSE;
3469 for (p = p_go; *p; p++)
3470 switch (*p)
3471 {
3472 case GO_LEFT:
3473 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3474 break;
3475 case GO_RIGHT:
3476 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3477 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 case GO_VLEFT:
3479 if (win_hasvertsplit())
3480 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3481 break;
3482 case GO_VRIGHT:
3483 if (win_hasvertsplit())
3484 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 case GO_BOT:
3487 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3488 break;
3489#ifdef FEAT_MENU
3490 case GO_MENUS:
3491 gui.menu_is_active = TRUE;
3492 break;
3493#endif
3494 case GO_GREY:
3495 /* make menu's have grey items, ignored here */
3496 break;
3497#ifdef FEAT_TOOLBAR
3498 case GO_TOOLBAR:
3499 using_toolbar = TRUE;
3500 break;
3501#endif
3502#ifdef FEAT_FOOTER
3503 case GO_FOOTER:
3504 using_footer = TRUE;
3505 break;
3506#endif
3507 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003508#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 using_tearoff = TRUE;
3510#endif
3511 break;
3512 default:
3513 /* Ignore options that are not supported */
3514 break;
3515 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (gui.in_use)
3518 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003519 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003521
3522#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003523 /* Update the GUI tab line, it may appear or disappear. This may
3524 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003525 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003526 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003527 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003528 /* We don't want a resize event change "Rows" here, save and
3529 * restore it. Resizing is handled below. */
3530 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003531 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003532 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003533 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003534 if (using_tabline)
3535 fix_size = TRUE;
3536 if (!gui_use_tabline())
3537 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3538 }
3539#endif
3540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 for (i = 0; i < 3; i++)
3542 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003543 /* The scrollbar needs to be updated when it is shown/unshown and
3544 * when switching tab pages. But the size only changes when it's
3545 * shown/unshown. Thus we need two places to remember whether a
3546 * scrollbar is there or not. */
3547 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003548 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003549 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
3551 if (i == SBAR_BOTTOM)
3552 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3553 gui.which_scrollbars[i]);
3554 else
3555 {
3556 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003559 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3560 {
3561 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003562 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003563 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003564 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003565 if (gui.which_scrollbars[i])
3566 fix_size = TRUE;
3567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003569 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003570 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572
3573#ifdef FEAT_MENU
3574 if (gui.menu_is_active != prev_menu_is_active)
3575 {
3576 /* We don't want a resize event change "Rows" here, save and
3577 * restore it. Resizing is handled below. */
3578 i = Rows;
3579 gui_mch_enable_menu(gui.menu_is_active);
3580 Rows = i;
3581 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003582 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (gui.menu_is_active)
3584 fix_size = TRUE;
3585 }
3586#endif
3587
3588#ifdef FEAT_TOOLBAR
3589 if (using_toolbar != prev_toolbar)
3590 {
3591 gui_mch_show_toolbar(using_toolbar);
3592 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003593 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (using_toolbar)
3595 fix_size = TRUE;
3596 }
3597#endif
3598#ifdef FEAT_FOOTER
3599 if (using_footer != prev_footer)
3600 {
3601 gui_mch_enable_footer(using_footer);
3602 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003603 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 if (using_footer)
3605 fix_size = TRUE;
3606 }
3607#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003608#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 if (using_tearoff != prev_tearoff)
3610 {
3611 gui_mch_toggle_tearoffs(using_tearoff);
3612 prev_tearoff = using_tearoff;
3613 }
3614#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003615 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003616 {
3617#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003618 long prev_Columns = Columns;
3619 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 /* Adjust the size of the window to make the text area keep the
3622 * same size and to avoid that part of our window is off-screen
3623 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003624 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003625
3626#ifdef FEAT_GUI_GTK
3627 /* GTK has the annoying habit of sending us resize events when
3628 * changing the window size ourselves. This mostly happens when
3629 * waiting for a character to arrive, quite unpredictably, and may
3630 * change Columns and Rows when we don't want it. Wait for a
3631 * character here to avoid this effect.
3632 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003633 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003634 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003635 * Don't change Rows when adding menu/toolbar/tabline.
3636 * Don't change Columns when adding vertical toolbar. */
3637 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003638 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003639 if ((need_set_size & RESIZE_VERT) == 0)
3640 Rows = prev_Rows;
3641 if ((need_set_size & RESIZE_HOR) == 0)
3642 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003643#endif
3644 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003645 /* When the console tabline appears or disappears the window positions
3646 * change. */
3647 if (firstwin->w_winrow != tabline_height())
3648 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 }
3650}
3651
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003652#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3653/*
3654 * Return TRUE if the GUI is taking care of the tabline.
3655 * It may still be hidden if 'showtabline' is zero.
3656 */
3657 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003658gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003659{
3660 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3661}
3662
3663/*
3664 * Return TRUE if the GUI is showing the tabline.
3665 * This uses 'showtabline'.
3666 */
3667 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003668gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003669{
3670 if (!gui_use_tabline()
3671 || p_stal == 0
3672 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3673 return FALSE;
3674 return TRUE;
3675}
3676
3677/*
3678 * Update the tabline.
3679 * This may display/undisplay the tabline and update the labels.
3680 */
3681 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003682gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003683{
3684 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003685 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003686
3687 if (!gui.starting && starting == 0)
3688 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003689 /* Updating the tabline uses direct GUI commands, flush
3690 * outstanding instructions first. (esp. clear screen) */
3691 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003692
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003693 if (!showit != !shown)
3694 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003695 if (showit != 0)
3696 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003697
3698 /* When the tabs change from hidden to shown or from shown to
3699 * hidden the size of the text area should remain the same. */
3700 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003701 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003702 }
3703}
3704
3705/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003706 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003707 */
3708 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003709get_tabline_label(
3710 tabpage_T *tp,
3711 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003712{
3713 int modified = FALSE;
3714 char_u buf[40];
3715 int wincount;
3716 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003717 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003718
Bram Moolenaar57657d82006-04-21 22:12:41 +00003719 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003720 opt = (tooltip ? &p_gtt : &p_gtl);
3721 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003722 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003723 int use_sandbox = FALSE;
3724 int save_called_emsg = called_emsg;
3725 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003726 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003727 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3728 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003729
3730 called_emsg = FALSE;
3731
3732 printer_page_num = tabpage_index(tp);
3733# ifdef FEAT_EVAL
3734 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003735 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003736# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003737 /* It's almost as going to the tabpage, but without autocommands. */
3738 curtab->tp_firstwin = firstwin;
3739 curtab->tp_lastwin = lastwin;
3740 curtab->tp_curwin = curwin;
3741 save_curtab = curtab;
3742 curtab = tp;
3743 topframe = curtab->tp_topframe;
3744 firstwin = curtab->tp_firstwin;
3745 lastwin = curtab->tp_lastwin;
3746 curwin = curtab->tp_curwin;
3747 curbuf = curwin->w_buffer;
3748
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003749 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003750 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003751 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003752 STRCPY(NameBuff, res);
3753
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003754 /* Back to the original curtab. */
3755 curtab = save_curtab;
3756 topframe = curtab->tp_topframe;
3757 firstwin = curtab->tp_firstwin;
3758 lastwin = curtab->tp_lastwin;
3759 curwin = curtab->tp_curwin;
3760 curbuf = curwin->w_buffer;
3761
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003762 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003763 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003764 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003765 called_emsg |= save_called_emsg;
3766 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003767
3768 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3769 * use a default label. */
3770 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003771 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003772 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003773 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003774 if (!tooltip)
3775 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003776
3777 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3778 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3779 if (bufIsChanged(wp->w_buffer))
3780 modified = TRUE;
3781 if (modified || wincount > 1)
3782 {
3783 if (wincount > 1)
3784 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3785 else
3786 buf[0] = NUL;
3787 if (modified)
3788 STRCAT(buf, "+");
3789 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003790 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003791 mch_memmove(NameBuff, buf, STRLEN(buf));
3792 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003793 }
3794}
3795
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003796/*
3797 * Send the event for clicking to select tab page "nr".
3798 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003799 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003800 */
3801 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003802send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003803{
3804 char_u string[3];
3805
3806 if (nr == tabpage_index(curtab))
3807 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003808
3809 /* Don't put events in the input queue now. */
3810 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003811# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003812 || cmdwin_type != 0
3813# endif
3814 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003815 {
3816 /* Set it back to the current tab page. */
3817 gui_mch_set_curtab(tabpage_index(curtab));
3818 return FALSE;
3819 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003820
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003821 string[0] = CSI;
3822 string[1] = KS_TABLINE;
3823 string[2] = KE_FILLER;
3824 add_to_input_buf(string, 3);
3825 string[0] = nr;
3826 add_to_input_buf_csi(string, 1);
3827 return TRUE;
3828}
3829
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003830/*
3831 * Send a tabline menu event
3832 */
3833 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003834send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003835{
3836 char_u string[3];
3837
Bram Moolenaar29547192018-12-11 20:39:19 +01003838 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003839 if (hold_gui_events)
3840 return;
3841
Bram Moolenaar29547192018-12-11 20:39:19 +01003842 // Cannot close the last tabpage.
3843 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3844 return;
3845
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003846 string[0] = CSI;
3847 string[1] = KS_TABMENU;
3848 string[2] = KE_FILLER;
3849 add_to_input_buf(string, 3);
3850 string[0] = tabidx;
3851 string[1] = (char_u)(long)event;
3852 add_to_input_buf_csi(string, 2);
3853}
3854
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856
3857/*
3858 * Scrollbar stuff:
3859 */
3860
Bram Moolenaare45828b2006-02-15 22:12:56 +00003861/*
3862 * Remove all scrollbars. Used before switching to another tab page.
3863 */
3864 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003865gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003866{
3867 int i;
3868 win_T *wp;
3869
3870 for (i = 0; i < 3; i++)
3871 {
3872 if (i == SBAR_BOTTOM)
3873 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3874 else
3875 {
3876 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003877 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003878 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003879 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003880 }
3881}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003882
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003884gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885{
3886 static int sbar_ident = 0;
3887
3888 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3889 sb->wp = wp;
3890 sb->type = type;
3891 sb->value = 0;
3892#ifdef FEAT_GUI_ATHENA
3893 sb->pixval = 0;
3894#endif
3895 sb->size = 1;
3896 sb->max = 1;
3897 sb->top = 0;
3898 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 sb->status_height = 0;
3901 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3902}
3903
3904/*
3905 * Find the scrollbar with the given index.
3906 */
3907 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003908gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
3910 win_T *wp;
3911
3912 if (gui.bottom_sbar.ident == ident)
3913 return &gui.bottom_sbar;
3914 FOR_ALL_WINDOWS(wp)
3915 {
3916 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3917 return &wp->w_scrollbars[SBAR_LEFT];
3918 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3919 return &wp->w_scrollbars[SBAR_RIGHT];
3920 }
3921 return NULL;
3922}
3923
3924/*
3925 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3926 *
3927 * For Win32, Macintosh and GTK+ 2:
3928 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3929 * you stop dragging the scrollbar. We get here each time the scrollbar is
3930 * dragged another pixel, but as far as the rest of vim goes, it thinks
3931 * we're just hanging in the call to DispatchMessage() in
3932 * process_message(). The DispatchMessage() call that hangs was passed a
3933 * mouse button click event in the scrollbar window. -- webb.
3934 *
3935 * Solution: Do the scrolling right here. But only when allowed.
3936 * Ignore the scrollbars while executing an external command or when there
3937 * are still characters to be processed.
3938 */
3939 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003940gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 int sb_num;
3944#ifdef USE_ON_FLY_SCROLL
3945 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947# ifdef FEAT_DIFF
3948 int old_topfill = curwin->w_topfill;
3949# endif
3950#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003951 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 int byte_count;
3953#endif
3954
3955 if (sb == NULL)
3956 return;
3957
3958 /* Don't put events in the input queue now. */
3959 if (hold_gui_events)
3960 return;
3961
3962#ifdef FEAT_CMDWIN
3963 if (cmdwin_type != 0 && sb->wp != curwin)
3964 return;
3965#endif
3966
3967 if (still_dragging)
3968 {
3969 if (sb->wp == NULL)
3970 gui.dragged_sb = SBAR_BOTTOM;
3971 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3972 gui.dragged_sb = SBAR_LEFT;
3973 else
3974 gui.dragged_sb = SBAR_RIGHT;
3975 gui.dragged_wp = sb->wp;
3976 }
3977 else
3978 {
3979 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003980#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003982 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 gui.dragged_wp = NULL;
3984#endif
3985 }
3986
3987 /* Vertical sbar info is kept in the first sbar (the left one) */
3988 if (sb->wp != NULL)
3989 sb = &sb->wp->w_scrollbars[0];
3990
3991 /*
3992 * Check validity of value
3993 */
3994 if (value < 0)
3995 value = 0;
3996#ifdef SCROLL_PAST_END
3997 else if (value > sb->max)
3998 value = sb->max;
3999#else
4000 if (value > sb->max - sb->size + 1)
4001 value = sb->max - sb->size + 1;
4002#endif
4003
4004 sb->value = value;
4005
4006#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00004007 /* When not allowed to do the scrolling right now, return.
4008 * This also checked input_available(), but that causes the first click in
4009 * a scrollbar to be ignored when Vim doesn't have focus. */
4010 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 return;
4012#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004013#ifdef FEAT_INS_EXPAND
4014 /* Disallow scrolling the current window when the completion popup menu is
4015 * visible. */
4016 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4017 return;
4018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
4020#ifdef FEAT_RIGHTLEFT
4021 if (sb->wp == NULL && curwin->w_p_rl)
4022 {
4023 value = sb->max + 1 - sb->size - value;
4024 if (value < 0)
4025 value = 0;
4026 }
4027#endif
4028
4029 if (sb->wp != NULL) /* vertical scrollbar */
4030 {
4031 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4033 sb_num++;
4034 if (wp == NULL)
4035 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036
4037#ifdef USE_ON_FLY_SCROLL
4038 current_scrollbar = sb_num;
4039 scrollbar_value = value;
4040 if (State & NORMAL)
4041 {
4042 gui_do_scroll();
4043 setcursor();
4044 }
4045 else if (State & INSERT)
4046 {
4047 ins_scroll();
4048 setcursor();
4049 }
4050 else if (State & CMDLINE)
4051 {
4052 if (msg_scrolled == 0)
4053 {
4054 gui_do_scroll();
4055 redrawcmdline();
4056 }
4057 }
4058# ifdef FEAT_FOLDING
4059 /* Value may have been changed for closed fold. */
4060 sb->value = sb->wp->w_topline - 1;
4061# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004062
4063 /* When dragging one scrollbar and there is another one at the other
4064 * side move the thumb of that one too. */
4065 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4066 gui_mch_set_scrollbar_thumb(
4067 &sb->wp->w_scrollbars[
4068 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4069 ? SBAR_LEFT : SBAR_RIGHT],
4070 sb->value, sb->size, sb->max);
4071
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072#else
4073 bytes[0] = CSI;
4074 bytes[1] = KS_VER_SCROLLBAR;
4075 bytes[2] = KE_FILLER;
4076 bytes[3] = (char_u)sb_num;
4077 byte_count = 4;
4078#endif
4079 }
4080 else
4081 {
4082#ifdef USE_ON_FLY_SCROLL
4083 scrollbar_value = value;
4084
4085 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004086 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 else if (State & INSERT)
4088 ins_horscroll();
4089 else if (State & CMDLINE)
4090 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004091 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004093 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 redrawcmdline();
4095 }
4096 }
4097 if (old_leftcol != curwin->w_leftcol)
4098 {
4099 updateWindow(curwin); /* update window, status and cmdline */
4100 setcursor();
4101 }
4102#else
4103 bytes[0] = CSI;
4104 bytes[1] = KS_HOR_SCROLLBAR;
4105 bytes[2] = KE_FILLER;
4106 byte_count = 3;
4107#endif
4108 }
4109
4110#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 /*
4112 * synchronize other windows, as necessary according to 'scrollbind'
4113 */
4114 if (curwin->w_p_scb
4115 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4116 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004117# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 ))))
4121 {
4122 do_check_scrollbind(TRUE);
4123 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004124 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 if (wp->w_redr_type > 0)
4126 updateWindow(wp);
4127 setcursor();
4128 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004129 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004131 add_to_input_buf(bytes, byte_count);
4132 add_long_to_buf((long_u)value, bytes);
4133 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134#endif
4135}
4136
4137/*
4138 * Scrollbar stuff:
4139 */
4140
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004141/*
4142 * Called when something in the window layout has changed.
4143 */
4144 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004145gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004146{
4147 if (gui.in_use && starting == 0)
4148 {
4149 out_flush();
4150 gui_init_which_components(NULL);
4151 gui_update_scrollbars(TRUE);
4152 }
4153 need_mouse_correct = TRUE;
4154}
4155
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004157gui_update_scrollbars(
4158 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
4160 win_T *wp;
4161 scrollbar_T *sb;
4162 long val, size, max; /* need 32 bits here */
4163 int which_sb;
4164 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166
4167 /* Update the horizontal scrollbar */
4168 gui_update_horiz_scrollbar(force);
4169
Bram Moolenaar4f974752019-02-17 17:44:42 +01004170#ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 /* Return straight away if there is neither a left nor right scrollbar.
4172 * On MS-Windows this is required anyway for scrollwheel messages. */
4173 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4174 return;
4175#endif
4176
4177 /*
4178 * Don't want to update a scrollbar while we're dragging it. But if we
4179 * have both a left and right scrollbar, and we drag one of them, we still
4180 * need to update the other one.
4181 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004182 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4183 && gui.which_scrollbars[SBAR_LEFT]
4184 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
4186 /*
4187 * If we have two scrollbars and one of them is being dragged, just
4188 * copy the scrollbar position from the dragged one to the other one.
4189 */
4190 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4191 if (gui.dragged_wp != NULL)
4192 gui_mch_set_scrollbar_thumb(
4193 &gui.dragged_wp->w_scrollbars[which_sb],
4194 gui.dragged_wp->w_scrollbars[0].value,
4195 gui.dragged_wp->w_scrollbars[0].size,
4196 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198
4199 /* avoid that moving components around generates events */
4200 ++hold_gui_events;
4201
Bram Moolenaar45a24952016-07-24 22:25:15 +02004202 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 {
4204 if (wp->w_buffer == NULL) /* just in case */
4205 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004206 /* Skip a scrollbar that is being dragged. */
4207 if (!force && (gui.dragged_sb == SBAR_LEFT
4208 || gui.dragged_sb == SBAR_RIGHT)
4209 && gui.dragged_wp == wp)
4210 continue;
4211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212#ifdef SCROLL_PAST_END
4213 max = wp->w_buffer->b_ml.ml_line_count - 1;
4214#else
4215 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4216#endif
4217 if (max < 0) /* empty buffer */
4218 max = 0;
4219 val = wp->w_topline - 1;
4220 size = wp->w_height;
4221#ifdef SCROLL_PAST_END
4222 if (val > max) /* just in case */
4223 val = max;
4224#else
4225 if (size > max + 1) /* just in case */
4226 size = max + 1;
4227 if (val > max - size + 1)
4228 val = max - size + 1;
4229#endif
4230 if (val < 0) /* minimal value is 0 */
4231 val = 0;
4232
4233 /*
4234 * Scrollbar at index 0 (the left one) contains all the information.
4235 * It would be the same info for left and right so we just store it for
4236 * one of them.
4237 */
4238 sb = &wp->w_scrollbars[0];
4239
4240 /*
4241 * Note: no check for valid w_botline. If it's not valid the
4242 * scrollbars will be updated later anyway.
4243 */
4244 if (size < 1 || wp->w_botline - 2 > max)
4245 {
4246 /*
4247 * This can happen during changing files. Just don't update the
4248 * scrollbar for now.
4249 */
4250 sb->height = 0; /* Force update next time */
4251 if (gui.which_scrollbars[SBAR_LEFT])
4252 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4253 if (gui.which_scrollbars[SBAR_RIGHT])
4254 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4255 continue;
4256 }
4257 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 || sb->top != wp->w_winrow
4259 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004261 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 {
4263 /* Height, width or position of scrollbar has changed. For
4264 * vertical split: curwin changed. */
4265 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 sb->top = wp->w_winrow;
4267 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
4270 /* Calculate height and position in pixels */
4271 h = (sb->height + sb->status_height) * gui.char_height;
4272 y = sb->top * gui.char_height + gui.border_offset;
4273#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4274 if (gui.menu_is_active)
4275 y += gui.menu_height;
4276#endif
4277
4278#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4279 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4280# ifdef FEAT_GUI_ATHENA
4281 y += gui.toolbar_height;
4282# else
4283# ifdef FEAT_GUI_MSWIN
4284 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4285# endif
4286# endif
4287#endif
4288
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004289#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4290 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004291 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004292#endif
4293
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
4296 /* Height of top scrollbar includes width of top border */
4297 h += gui.border_offset;
4298 y -= gui.border_offset;
4299 }
4300 if (gui.which_scrollbars[SBAR_LEFT])
4301 {
4302 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4303 gui.left_sbar_x, y,
4304 gui.scrollbar_width, h);
4305 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4306 }
4307 if (gui.which_scrollbars[SBAR_RIGHT])
4308 {
4309 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4310 gui.right_sbar_x, y,
4311 gui.scrollbar_width, h);
4312 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4313 }
4314 }
4315
4316 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4317 * checking if the thumb moved at least a pixel. Only do this for
4318 * Athena, most other GUIs require the update anyway to make the
4319 * arrows work. */
4320#ifdef FEAT_GUI_ATHENA
4321 if (max == 0)
4322 y = 0;
4323 else
4324 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4325 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4326#else
4327 if (force || sb->value != val || sb->size != size || sb->max != max)
4328#endif
4329 {
4330 /* Thumb of scrollbar has moved */
4331 sb->value = val;
4332#ifdef FEAT_GUI_ATHENA
4333 sb->pixval = y;
4334#endif
4335 sb->size = size;
4336 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004337 if (gui.which_scrollbars[SBAR_LEFT]
4338 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4340 val, size, max);
4341 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004342 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4344 val, size, max);
4345 }
4346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 --hold_gui_events;
4349}
4350
4351/*
4352 * Enable or disable a scrollbar.
4353 * Check for scrollbars for vertically split windows which are not enabled
4354 * sometimes.
4355 */
4356 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004357gui_do_scrollbar(
4358 win_T *wp,
4359 int which, /* SBAR_LEFT or SBAR_RIGHT */
4360 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 int midcol = curwin->w_wincol + curwin->w_width / 2;
4363 int has_midcol = (wp->w_wincol <= midcol
4364 && wp->w_wincol + wp->w_width >= midcol);
4365
4366 /* Only enable scrollbars that contain the middle column of the current
4367 * window. */
4368 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4369 {
4370 /* Scrollbars only on one side. Don't enable scrollbars that don't
4371 * contain the middle column of the current window. */
4372 if (!has_midcol)
4373 enable = FALSE;
4374 }
4375 else
4376 {
4377 /* Scrollbars on both sides. Don't enable scrollbars that neither
4378 * contain the middle column of the current window nor are on the far
4379 * side. */
4380 if (midcol > Columns / 2)
4381 {
4382 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4383 enable = FALSE;
4384 }
4385 else
4386 {
4387 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4388 : !has_midcol)
4389 enable = FALSE;
4390 }
4391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4393}
4394
4395/*
4396 * Scroll a window according to the values set in the globals current_scrollbar
4397 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4398 * or FALSE otherwise.
4399 */
4400 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004401gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402{
4403 win_T *wp, *save_wp;
4404 int i;
4405 long nlines;
4406 pos_T old_cursor;
4407 linenr_T old_topline;
4408#ifdef FEAT_DIFF
4409 int old_topfill;
4410#endif
4411
4412 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4413 if (wp == NULL)
4414 break;
4415 if (wp == NULL)
4416 /* Couldn't find window */
4417 return FALSE;
4418
4419 /*
4420 * Compute number of lines to scroll. If zero, nothing to do.
4421 */
4422 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4423 if (nlines == 0)
4424 return FALSE;
4425
4426 save_wp = curwin;
4427 old_topline = wp->w_topline;
4428#ifdef FEAT_DIFF
4429 old_topfill = wp->w_topfill;
4430#endif
4431 old_cursor = wp->w_cursor;
4432 curwin = wp;
4433 curbuf = wp->w_buffer;
4434 if (nlines < 0)
4435 scrolldown(-nlines, gui.dragged_wp == NULL);
4436 else
4437 scrollup(nlines, gui.dragged_wp == NULL);
4438 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4439 * the mouse-up event already, but we still want it to behave like when
4440 * dragging. But not the next click in an arrow. */
4441 if (gui.dragged_sb == SBAR_NONE)
4442 gui.dragged_wp = NULL;
4443
4444 if (old_topline != wp->w_topline
4445#ifdef FEAT_DIFF
4446 || old_topfill != wp->w_topfill
4447#endif
4448 )
4449 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004450 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
4452 cursor_correct(); /* fix window for 'so' */
4453 update_topline(); /* avoid up/down jump */
4454 }
4455 if (old_cursor.lnum != wp->w_cursor.lnum)
4456 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004460 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4461 validate_cursor();
4462
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 curwin = save_wp;
4464 curbuf = save_wp->w_buffer;
4465
4466 /*
4467 * Don't call updateWindow() when nothing has changed (it will overwrite
4468 * the status line!).
4469 */
4470 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004471 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472#ifdef FEAT_DIFF
4473 || old_topfill != wp->w_topfill
4474#endif
4475 )
4476 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004477 int type = VALID;
4478
4479#ifdef FEAT_INS_EXPAND
4480 if (pum_visible())
4481 {
4482 type = NOT_VALID;
4483 wp->w_lines_valid = 0;
4484 }
4485#endif
4486 /* Don't set must_redraw here, it may cause the popup menu to
4487 * disappear when losing focus after a scrollbar drag. */
4488 if (wp->w_redr_type < type)
4489 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004490 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 updateWindow(wp); /* update window, status line, and cmdline */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004492 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004495#ifdef FEAT_INS_EXPAND
4496 /* May need to redraw the popup menu. */
4497 if (pum_visible())
4498 pum_redraw();
4499#endif
4500
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004501 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502}
4503
4504
4505/*
4506 * Horizontal scrollbar stuff:
4507 */
4508
4509/*
4510 * Return length of line "lnum" for horizontal scrolling.
4511 */
4512 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004513scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514{
4515 char_u *p;
4516 colnr_T col;
4517 int w;
4518
4519 p = ml_get(lnum);
4520 col = 0;
4521 if (*p != NUL)
4522 for (;;)
4523 {
4524 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004525 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 if (*p == NUL) /* don't count the last character */
4527 break;
4528 col += w;
4529 }
4530 return col;
4531}
4532
4533/* Remember which line is currently the longest, so that we don't have to
4534 * search for it when scrolling horizontally. */
4535static linenr_T longest_lnum = 0;
4536
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004537/*
4538 * Find longest visible line number. If this is not possible (or not desired,
4539 * by setting 'h' in "guioptions") then the current line number is returned.
4540 */
4541 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004542gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004543{
4544 linenr_T ret = 0;
4545
4546 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4547 * line numbers, topline and botline can be invalid when displaying is
4548 * postponed. */
4549 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4550 && curwin->w_topline <= curwin->w_cursor.lnum
4551 && curwin->w_botline > curwin->w_cursor.lnum
4552 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4553 {
4554 linenr_T lnum;
4555 colnr_T n;
4556 long max = 0;
4557
4558 /* Use maximum of all visible lines. Remember the lnum of the
4559 * longest line, closest to the cursor line. Used when scrolling
4560 * below. */
4561 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4562 {
4563 n = scroll_line_len(lnum);
4564 if (n > (colnr_T)max)
4565 {
4566 max = n;
4567 ret = lnum;
4568 }
4569 else if (n == (colnr_T)max
4570 && abs((int)(lnum - curwin->w_cursor.lnum))
4571 < abs((int)(ret - curwin->w_cursor.lnum)))
4572 ret = lnum;
4573 }
4574 }
4575 else
4576 /* Use cursor line only. */
4577 ret = curwin->w_cursor.lnum;
4578
4579 return ret;
4580}
4581
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004583gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584{
4585 long value, size, max; /* need 32 bit ints here */
4586
4587 if (!gui.which_scrollbars[SBAR_BOTTOM])
4588 return;
4589
4590 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4591 return;
4592
4593 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4594 return;
4595
4596 /*
4597 * It is possible for the cursor to be invalid if we're in the middle of
4598 * something (like changing files). If so, don't do anything for now.
4599 */
4600 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4601 {
4602 gui.bottom_sbar.value = -1;
4603 return;
4604 }
4605
Bram Moolenaar02631462017-09-22 15:20:32 +02004606 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 if (curwin->w_p_wrap)
4608 {
4609 value = 0;
4610#ifdef SCROLL_PAST_END
4611 max = 0;
4612#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004613 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614#endif
4615 }
4616 else
4617 {
4618 value = curwin->w_leftcol;
4619
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004620 longest_lnum = gui_find_longest_lnum();
4621 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 if (virtual_active())
4624 {
4625 /* May move the cursor even further to the right. */
4626 if (curwin->w_virtcol >= (colnr_T)max)
4627 max = curwin->w_virtcol;
4628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629
4630#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004631 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632#endif
4633 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004634 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 size -= curwin_col_off();
4636#ifndef SCROLL_PAST_END
4637 max -= curwin_col_off();
4638#endif
4639 }
4640
4641#ifndef SCROLL_PAST_END
4642 if (value > max - size + 1)
4643 value = max - size + 1; /* limit the value to allowable range */
4644#endif
4645
4646#ifdef FEAT_RIGHTLEFT
4647 if (curwin->w_p_rl)
4648 {
4649 value = max + 1 - size - value;
4650 if (value < 0)
4651 {
4652 size += value;
4653 value = 0;
4654 }
4655 }
4656#endif
4657 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4658 && max == gui.bottom_sbar.max)
4659 return;
4660
4661 gui.bottom_sbar.value = value;
4662 gui.bottom_sbar.size = size;
4663 gui.bottom_sbar.max = max;
4664 gui.prev_wrap = curwin->w_p_wrap;
4665
4666 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4667}
4668
4669/*
4670 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4671 */
4672 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004673gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
4675 /* no wrapping, no scrolling */
4676 if (curwin->w_p_wrap)
4677 return FALSE;
4678
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004679 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 return FALSE;
4681
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004682 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
4684 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004685 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004687 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004688 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004690 if (compute_longest_lnum)
4691 {
4692 curwin->w_cursor.lnum = gui_find_longest_lnum();
4693 curwin->w_cursor.col = 0;
4694 }
4695 /* Do a sanity check on "longest_lnum", just in case. */
4696 else if (longest_lnum >= curwin->w_topline
4697 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
4699 curwin->w_cursor.lnum = longest_lnum;
4700 curwin->w_cursor.col = 0;
4701 }
4702 }
4703
4704 return leftcol_changed();
4705}
4706
4707/*
4708 * Check that none of the colors are the same as the background color
4709 */
4710 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004711gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
4713 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4714 {
4715 gui_set_bg_color((char_u *)"White");
4716 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4717 gui_set_fg_color((char_u *)"Black");
4718 }
4719}
4720
Bram Moolenaar3918c952005-03-15 22:34:55 +00004721 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004722gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 gui.norm_pixel = gui_get_color(name);
4725 hl_set_fg_color_name(vim_strsave(name));
4726}
4727
Bram Moolenaar3918c952005-03-15 22:34:55 +00004728 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004729gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
4731 gui.back_pixel = gui_get_color(name);
4732 hl_set_bg_color_name(vim_strsave(name));
4733}
4734
4735/*
4736 * Allocate a color by name.
4737 * Returns INVALCOLOR and gives an error message when failed.
4738 */
4739 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004740gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
4742 guicolor_T t;
4743
4744 if (*name == NUL)
4745 return INVALCOLOR;
4746 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004747
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004749#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 && gui.in_use
4751#endif
4752 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004753 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 return t;
4755}
4756
4757/*
4758 * Return the grey value of a color (range 0-255).
4759 */
4760 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004761gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004763 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004765 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004766 + (((rgb >> 8) & 0xff) * 587)
4767 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768}
4769
4770#if defined(FEAT_GUI_X11) || defined(PROTO)
4771 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004772gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
4774 win_T *wp;
4775
4776 /* Nothing to do if GUI hasn't started yet. */
4777 if (!gui.in_use)
4778 return;
4779
4780 FOR_ALL_WINDOWS(wp)
4781 {
4782 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4783 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4784 }
4785 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4786}
4787#endif
4788
4789/*
4790 * Call this when focus has changed.
4791 */
4792 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004793gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794{
4795/*
4796 * Skip this code to avoid drawing the cursor when debugging and switching
4797 * between the debugger window and gvim.
4798 */
4799#if 1
4800 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004801 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
4803# ifdef FEAT_XIM
4804 xim_set_focus(in_focus);
4805# endif
4806
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004807 /* Put events in the input queue only when allowed.
4808 * ui_focus_change() isn't called directly, because it invokes
4809 * autocommands and that must not happen asynchronously. */
4810 if (!hold_gui_events)
4811 {
4812 char_u bytes[3];
4813
4814 bytes[0] = CSI;
4815 bytes[1] = KS_EXTRA;
4816 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4817 add_to_input_buf(bytes, 3);
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819#endif
4820}
4821
4822/*
4823 * Called when the mouse moved (but not when dragging).
4824 */
4825 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004826gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827{
4828 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004829 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830
Bram Moolenaard3667a22006-03-16 21:35:52 +00004831 /* Ignore this while still starting up. */
4832 if (!gui.in_use || gui.starting)
4833 return;
4834
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835#ifdef FEAT_MOUSESHAPE
4836 /* Get window pointer, and update mouse shape as well. */
4837 wp = xy2win(x, y);
4838#endif
4839
4840 /* Only handle this when 'mousefocus' set and ... */
4841 if (p_mousef
4842 && !hold_gui_events /* not holding events */
4843 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4844 && State != HITRETURN /* but not hit-return prompt */
4845 && msg_scrolled == 0 /* no scrolled message */
4846 && !need_mouse_correct /* not moving the pointer */
4847 && gui.in_focus) /* gvim in focus */
4848 {
4849 /* Don't move the mouse when it's left or right of the Vim window */
4850 if (x < 0 || x > Columns * gui.char_width)
4851 return;
4852#ifndef FEAT_MOUSESHAPE
4853 wp = xy2win(x, y);
4854#endif
4855 if (wp == curwin || wp == NULL)
4856 return; /* still in the same old window, or none at all */
4857
Bram Moolenaar9c102382006-05-03 21:26:49 +00004858 /* Ignore position in the tab pages line. */
4859 if (Y_2_ROW(y) < tabline_height())
4860 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004861
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 /*
4863 * format a mouse click on status line input
4864 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004865 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4866 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 */
4868 if (finish_op)
4869 {
4870 /* abort the current operator first */
4871 st[0] = ESC;
4872 add_to_input_buf(st, 1);
4873 }
4874 st[0] = CSI;
4875 st[1] = KS_MOUSE;
4876 st[2] = KE_FILLER;
4877 st[3] = (char_u)MOUSE_LEFT;
4878 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004879 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 wp->w_height + W_WINROW(wp));
4881
4882 add_to_input_buf(st, 8);
4883 st[3] = (char_u)MOUSE_RELEASE;
4884 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885#ifdef FEAT_GUI_GTK
4886 /* Need to wake up the main loop */
4887 if (gtk_main_level() > 0)
4888 gtk_main_quit();
4889#endif
4890 }
4891}
4892
4893/*
4894 * Called when mouse should be moved to window with focus.
4895 */
4896 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004897gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
4899 int x, y;
4900 win_T *wp = NULL;
4901
4902 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004903
4904 if (!(gui.in_use && p_mousef))
4905 return;
4906
4907 gui_mch_getmouse(&x, &y);
4908 /* Don't move the mouse when it's left or right of the Vim window */
4909 if (x < 0 || x > Columns * gui.char_width)
4910 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004911 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004912 wp = xy2win(x, y);
4913 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004915 validate_cline_row();
4916 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4917 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 }
4920}
4921
4922/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004923 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004924 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 static win_T *
Bram Moolenaar4f974752019-02-17 17:44:42 +01004927xy2win(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 int row;
4930 int col;
4931 win_T *wp;
4932
4933 row = Y_2_ROW(y);
4934 col = X_2_COL(x);
4935 if (row < 0 || col < 0) /* before first window */
4936 return NULL;
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004937 wp = mouse_find_win(&row, &col, FALSE);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004938 if (wp == NULL)
4939 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004940#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 if (State == HITRETURN || State == ASKMORE)
4942 {
4943 if (Y_2_ROW(y) >= msg_row)
4944 update_mouseshape(SHAPE_IDX_MOREL);
4945 else
4946 update_mouseshape(SHAPE_IDX_MORE);
4947 }
4948 else if (row > wp->w_height) /* below status line */
4949 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004950 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004951 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004953 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004954 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 update_mouseshape(SHAPE_IDX_STATUS);
4956 else
4957 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004959 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960}
4961
4962/*
4963 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4964 * File names may be given to redefine the args list.
4965 */
4966 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004967ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968{
4969 char_u *arg = eap->arg;
4970
4971 /*
4972 * Check for "-f" argument: foreground, don't fork.
4973 * Also don't fork when started with "gvim -f".
4974 * Do fork when using "gui -b".
4975 */
4976 if (arg[0] == '-'
4977 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004978 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
4980 gui.dofork = (arg[1] == 'b');
4981 eap->arg = skipwhite(eap->arg + 2);
4982 }
4983 if (!gui.in_use)
4984 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004985#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
4986 emsg(_(e_nogvim));
4987 return;
4988#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 /* Clear the command. Needed for when forking+exiting, to avoid part
4990 * of the argument ending up after the shell prompt. */
4991 msg_clr_eos_force();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004992# ifdef GUI_MAY_SPAWN
4993 if (!ends_excmd(*eap->arg))
4994 gui_start(eap->arg);
4995 else
4996# endif
4997 gui_start(NULL);
4998# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004999 channel_gui_register_all();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005000# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003 if (!ends_excmd(*eap->arg))
5004 ex_next(eap);
5005}
5006
Bram Moolenaar4f974752019-02-17 17:44:42 +01005007#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
5008 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)) \
5009 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010/*
5011 * This is shared between Athena, Motif and GTK.
5012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
5014/*
5015 * Callback function for do_in_runtimepath().
5016 */
5017 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005018gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005020 char_u *gfp_buffer = cookie;
5021
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 if (STRLEN(fname) >= MAXPATHL)
5023 *gfp_buffer = NUL;
5024 else
5025 STRCPY(gfp_buffer, fname);
5026}
5027
5028/*
5029 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5030 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5031 */
5032 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005033gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034{
5035 if (STRLEN(name) > MAXPATHL - 14)
5036 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005037 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005038 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005039 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 return FAIL;
5041 return OK;
5042}
5043
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005044# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045/*
5046 * Given the name of the "icon=" argument, try finding the bitmap file for the
5047 * icon. If it is an absolute path name, use it as it is. Otherwise append
5048 * "ext" and search for it in 'runtimepath'.
5049 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5050 * contains "name".
5051 */
5052 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005053gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054{
5055 char_u buf[MAXPATHL + 1];
5056
5057 expand_env(name, buffer, MAXPATHL);
5058 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5059 STRCPY(buffer, buf);
5060}
5061# endif
5062#endif
5063
Bram Moolenaar9372a112005-12-06 19:59:18 +00005064#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005066display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067{
5068 char_u *p;
5069
5070 if (isatty(2))
5071 fflush(stderr);
5072 else if (error_ga.ga_data != NULL)
5073 {
5074 /* avoid putting up a message box with blanks only */
5075 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5076 if (!isspace(*p))
5077 {
5078 /* Truncate a very long message, it will go off-screen. */
5079 if (STRLEN(p) > 2000)
5080 STRCPY(p + 2000 - 14, "...(truncated)");
5081 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005082 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 break;
5084 }
5085 ga_clear(&error_ga);
5086 }
5087}
5088#endif
5089
5090#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5091/*
5092 * Return TRUE if still starting up and there is no place to enter text.
5093 * For GTK and X11 we check if stderr is not a tty, which means we were
5094 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5095 * allow typing on stdin.
5096 */
5097 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005098no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099{
5100 return ((!gui.in_use || gui.starting)
5101# ifndef NO_CONSOLE
5102 && !isatty(0) && !isatty(2)
5103# endif
5104 );
5105}
5106#endif
5107
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005108#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005109 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005110 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111/*
5112 * Update the current window and the screen.
5113 */
5114 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005115gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005117# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005118 linenr_T conceal_old_cursor_line = 0;
5119 linenr_T conceal_new_cursor_line = 0;
5120 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005121# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005122
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 update_topline();
5124 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005125
Bram Moolenaarec80df72008-05-07 19:46:51 +00005126 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005127 if (!finish_op && (has_cursormoved()
Bram Moolenaar3397f742019-06-02 18:40:06 +02005128# ifdef FEAT_TEXT_PROP
5129 || popup_visible
5130# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005131# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005132 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005133# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005134 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005135 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005136 if (has_cursormoved())
5137 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3397f742019-06-02 18:40:06 +02005138#ifdef FEAT_TEXT_PROP
5139 if (popup_visible)
5140 popup_check_cursor_pos();
5141#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005142# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005143 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005144 {
5145 conceal_old_cursor_line = last_cursormoved.lnum;
5146 conceal_new_cursor_line = curwin->w_cursor.lnum;
5147 conceal_update_lines = TRUE;
5148 }
5149# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005150 last_cursormoved = curwin->w_cursor;
5151 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005152
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005153# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005154 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005155 && (conceal_old_cursor_line != conceal_new_cursor_line
5156 || conceal_cursor_line(curwin)
5157 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005158 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005159 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005160 redrawWinline(curwin, conceal_old_cursor_line);
5161 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005162 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005163 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005164 }
5165# endif
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005166 update_screen(0); /* may need to update the screen */
5167 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005168 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169}
5170#endif
5171
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005172#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173/*
5174 * Get the text to use in a find/replace dialog. Uses the last search pattern
5175 * if the argument is empty.
5176 * Returns an allocated string.
5177 */
5178 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005179get_find_dialog_text(
5180 char_u *arg,
5181 int *wwordp, /* return: TRUE if \< \> found */
5182 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183{
5184 char_u *text;
5185
5186 if (*arg == NUL)
5187 text = last_search_pat();
5188 else
5189 text = arg;
5190 if (text != NULL)
5191 {
5192 text = vim_strsave(text);
5193 if (text != NULL)
5194 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005195 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 int i;
5197
5198 /* Remove "\V" */
5199 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5200 {
5201 mch_memmove(text, text + 2, (size_t)(len - 1));
5202 len -= 2;
5203 }
5204
5205 /* Recognize "\c" and "\C" and remove. */
5206 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5207 {
5208 *mcasep = (text[1] == 'C');
5209 mch_memmove(text, text + 2, (size_t)(len - 1));
5210 len -= 2;
5211 }
5212
5213 /* Recognize "\<text\>" and remove. */
5214 if (len >= 4
5215 && STRNCMP(text, "\\<", 2) == 0
5216 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5217 {
5218 *wwordp = TRUE;
5219 mch_memmove(text, text + 2, (size_t)(len - 4));
5220 text[len - 4] = NUL;
5221 }
5222
5223 /* Recognize "\/" or "\?" and remove. */
5224 for (i = 0; i + 1 < len; ++i)
5225 if (text[i] == '\\' && (text[i + 1] == '/'
5226 || text[i + 1] == '?'))
5227 {
5228 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5229 --len;
5230 }
5231 }
5232 }
5233 return text;
5234}
5235
5236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 * Handle the press of a button in the find-replace dialog.
5238 * Return TRUE when something was added to the input buffer.
5239 */
5240 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005241gui_do_findrepl(
5242 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5243 char_u *find_text,
5244 char_u *repl_text,
5245 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246{
5247 garray_T ga;
5248 int i;
5249 int type = (flags & FRD_TYPE_MASK);
5250 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005251 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005252 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005253 static int busy = FALSE;
5254
5255 /* When the screen is being updated we should not change buffers and
5256 * windows structures, it may cause freed memory to be used. Also don't
5257 * do this recursively (pressing "Find" quickly several times. */
5258 if (updating_screen || busy)
5259 return FALSE;
5260
5261 /* refuse replace when text cannot be changed */
5262 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5263 return FALSE;
5264
5265 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
5267 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005268 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 ga_concat(&ga, (char_u *)"%s/");
5270
5271 ga_concat(&ga, (char_u *)"\\V");
5272 if (flags & FRD_MATCH_CASE)
5273 ga_concat(&ga, (char_u *)"\\C");
5274 else
5275 ga_concat(&ga, (char_u *)"\\c");
5276 if (flags & FRD_WHOLE_WORD)
5277 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar518bc172018-05-13 17:05:30 +02005278 /* escape / and \ */
5279 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5280 if (p != NULL)
5281 ga_concat(&ga, p);
5282 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 if (flags & FRD_WHOLE_WORD)
5284 ga_concat(&ga, (char_u *)"\\>");
5285
5286 if (type == FRD_REPLACEALL)
5287 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005289 /* escape / and \ */
5290 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5291 if (p != NULL)
5292 ga_concat(&ga, p);
5293 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005295 }
5296 ga_append(&ga, NUL);
5297
5298 if (type == FRD_REPLACE)
5299 {
5300 /* Do the replacement when the text at the cursor matches. Thus no
5301 * replacement is done if the cursor was moved! */
5302 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5303 regmatch.rm_ic = 0;
5304 if (regmatch.regprog != NULL)
5305 {
5306 p = ml_get_cursor();
5307 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5308 && regmatch.startp[0] == p)
5309 {
5310 /* Clear the command line to remove any old "No match"
5311 * error. */
5312 msg_end_prompt();
5313
5314 if (u_save_cursor() == OK)
5315 {
5316 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005317 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005318
5319 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005320 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005321 ins_str(repl_text);
5322 }
5323 }
5324 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005325 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005326 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005327 }
5328 }
5329
5330 if (type == FRD_REPLACEALL)
5331 {
5332 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005333 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 do_cmdline_cmd(ga.ga_data);
5335 }
5336 else
5337 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005338 int searchflags = SEARCH_MSG + SEARCH_MARK;
5339
5340 /* Search for the next match.
5341 * Don't skip text under cursor for single replace. */
5342 if (type == FRD_REPLACE)
5343 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005345 if (down)
5346 {
5347 (void)do_search(NULL, '/', ga.ga_data, 1L, searchflags, NULL, NULL);
5348 }
5349 else
5350 {
5351 /* We need to escape '?' if and only if we are searching in the up
5352 * direction */
5353 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5354 if (p != NULL)
5355 (void)do_search(NULL, '?', p, 1L, searchflags, NULL, NULL);
5356 vim_free(p);
5357 }
5358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 msg_scroll = i; /* don't let an error message set msg_scroll */
5360 }
5361
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005362 /* Don't want to pass did_emsg to other code, it may cause disabling
5363 * syntax HL if we were busy redrawing. */
5364 did_emsg = save_did_emsg;
5365
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 if (State & (NORMAL | INSERT))
5367 {
5368 gui_update_screen(); /* update the screen */
5369 msg_didout = 0; /* overwrite any message */
5370 need_wait_return = FALSE; /* don't wait for return */
5371 }
5372
5373 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005374 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 return (ga.ga_len > 0);
5376}
5377
5378#endif
5379
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005380#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381/*
5382 * Jump to the window at specified point (x, y).
5383 */
5384 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005385gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386{
5387 int row = Y_2_ROW(y);
5388 int col = X_2_COL(x);
5389 win_T *wp;
5390
5391 if (row >= 0 && col >= 0)
5392 {
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005393 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 if (wp != NULL && wp != curwin)
5395 win_goto(wp);
5396 }
5397}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398
5399/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005400 * Function passed to handle_drop() for the actions to be done after the
5401 * argument list has been updated.
5402 */
5403 static void
5404drop_callback(void *cookie)
5405{
5406 char_u *p = cookie;
5407
5408 /* If Shift held down, change to first file's directory. If the first
5409 * item is a directory, change to that directory (and let the explorer
5410 * plugin show the contents). */
5411 if (p != NULL)
5412 {
5413 if (mch_isdir(p))
5414 {
5415 if (mch_chdir((char *)p) == 0)
5416 shorten_fnames(TRUE);
5417 }
5418 else if (vim_chdirfile(p, "drop") == OK)
5419 shorten_fnames(TRUE);
5420 vim_free(p);
5421 }
5422
5423 /* Update the screen display */
5424 update_screen(NOT_VALID);
5425# ifdef FEAT_MENU
5426 gui_update_menus(0);
5427# endif
5428#ifdef FEAT_TITLE
5429 maketitle();
5430#endif
5431 setcursor();
5432 out_flush_cursor(FALSE, FALSE);
5433}
5434
5435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 * Process file drop. Mouse cursor position, key modifiers, name of files
5437 * and count of files are given. Argument "fnames[count]" has full pathnames
5438 * of dropped files, they will be freed in this function, and caller can't use
5439 * fnames after call this function.
5440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005442gui_handle_drop(
5443 int x UNUSED,
5444 int y UNUSED,
5445 int_u modifiers,
5446 char_u **fnames,
5447 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
5449 int i;
5450 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005451 static int entered = FALSE;
5452
5453 /*
5454 * This function is called by event handlers. Just in case we get a
5455 * second event before the first one is handled, ignore the second one.
5456 * Not sure if this can ever happen, just in case.
5457 */
5458 if (entered)
5459 return;
5460 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
5462 /*
5463 * When the cursor is at the command line, add the file names to the
5464 * command line, don't edit the files.
5465 */
5466 if (State & CMDLINE)
5467 {
5468 shorten_filenames(fnames, count);
5469 for (i = 0; i < count; ++i)
5470 {
5471 if (fnames[i] != NULL)
5472 {
5473 if (i > 0)
5474 add_to_input_buf((char_u*)" ", 1);
5475
5476 /* We don't know what command is used thus we can't be sure
5477 * about which characters need to be escaped. Only escape the
5478 * most common ones. */
5479# ifdef BACKSLASH_IN_FILENAME
5480 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5481# else
5482 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5483# endif
5484 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005485 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 vim_free(p);
5487 vim_free(fnames[i]);
5488 }
5489 }
5490 vim_free(fnames);
5491 }
5492 else
5493 {
5494 /* Go to the window under mouse cursor, then shorten given "fnames" by
5495 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 shorten_filenames(fnames, count);
5498
5499 /* If Shift held down, remember the first item. */
5500 if ((modifiers & MOUSE_SHIFT) != 0)
5501 p = vim_strsave(fnames[0]);
5502 else
5503 p = NULL;
5504
5505 /* Handle the drop, :edit or :split to get to the file. This also
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005506 * frees fnames[]. Skip this if there is only one item, it's a
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 * directory and Shift is held down. */
5508 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5509 && mch_isdir(fnames[0]))
5510 {
5511 vim_free(fnames[0]);
5512 vim_free(fnames);
5513 }
5514 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005515 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
5516 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005518
5519 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520}
5521#endif