blob: 0e2686d66e7acfa3430a8cccfebd7e513fe56ddf [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 Moolenaar66f948e2016-01-30 16:39:25 +010068gui_start(void)
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;
72
73 old_term = vim_strsave(T_NAME);
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 settmode(TMODE_COOK); /* stop RAW mode */
76 if (full_screen)
77 cursor_on(); /* needed for ":gui" in .vimrc */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 full_screen = FALSE;
79
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 ++recursive;
81
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020082#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020083 /*
84 * Quit the current process and continue in the child.
85 * Makes "gvim file" disconnect from the shell it was started in.
86 * Don't do this when Vim was started with "-f" or the 'f' flag is present
87 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020088 * Don't do this when there is a running job, we can only get the status
89 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020090 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020091 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
92# ifdef FEAT_JOB_CHANNEL
93 && !job_any_running()
94# endif
95 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020096 {
97 gui_do_fork();
98 }
99 else
100#endif
101 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200102#ifdef FEAT_GUI_GTK
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200103 /* If there is 'f' in 'guioptions' and specify -g argument,
104 * gui_mch_init_check() was not called yet. */
105 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100106 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200107#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200108 gui_attempt_start();
109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111 if (!gui.in_use) /* failed to start GUI */
112 {
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200113 /* Back to old term settings
114 *
115 * FIXME: If we got here because a child process failed and flagged to
116 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
117 * hit an X11 I/O error and do a longjmp(), leaving recursive
118 * permanently set to 1. This is probably not as big a problem as it
119 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
120 * return "OK" unconditionally, so it would be very difficult to
121 * actually hit this case.
122 */
123 termcapinit(old_term);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 settmode(TMODE_RAW); /* restart RAW mode */
125#ifdef FEAT_TITLE
126 set_title_defaults(); /* set 'title' and 'icon' again */
127#endif
128 }
129
130 vim_free(old_term);
131
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200132 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
133 * the GUIFailed event. */
134 gui_mch_update();
135 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
136 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200137 --recursive;
138}
139
140/*
141 * Set_termname() will call gui_init() to start the GUI.
142 * Set the "starting" flag, to indicate that the GUI will start.
143 *
144 * We don't want to open the GUI shell until after we've read .gvimrc,
145 * otherwise we don't know what font we will use, and hence we don't know
146 * what size the shell should be. So if there are errors in the .gvimrc
147 * file, they will have to go to the terminal: Set full_screen to FALSE.
148 * full_screen will be set to TRUE again by a successful termcapinit().
149 */
150 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100151gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200152{
153 static int recursive = 0;
154
155 ++recursive;
156 gui.starting = TRUE;
157
158#ifdef FEAT_GUI_GTK
159 gui.event_time = GDK_CURRENT_TIME;
160#endif
161
162 termcapinit((char_u *)"builtin_gui");
163 gui.starting = recursive - 1;
164
Bram Moolenaar9372a112005-12-06 19:59:18 +0000165#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200167 {
168# ifdef FEAT_EVAL
169 Window x11_window;
170 Display *x11_display;
171
172 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
173 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
174# endif
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 /* Display error messages in a dialog now. */
177 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 --recursive;
181}
182
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200183#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200184
185/* for waitpid() */
186# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
187# include <sys/wait.h>
188# endif
189
190/*
191 * Create a new process, by forking. In the child, start the GUI, and in
192 * the parent, exit.
193 *
194 * If something goes wrong, this will return with gui.in_use still set
195 * to FALSE, in which case the caller should continue execution without
196 * the GUI.
197 *
198 * If the child fails to start the GUI, then the child will exit and the
199 * parent will return. If the child succeeds, then the parent will exit
200 * and the child will return.
201 */
202 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100203gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200204{
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200205 int pipefd[2]; /* pipe between parent and child */
206 int pipe_error;
207 int status;
208 int exit_status;
209 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200210
211 /* Setup a pipe between the child and the parent, so that the parent
212 * knows when the child has done the setsid() call and is allowed to
213 * exit. */
214 pipe_error = (pipe(pipefd) < 0);
215 pid = fork();
216 if (pid < 0) /* Fork error */
217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100218 emsg(_("E851: Failed to create a new process for the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200219 return;
220 }
221 else if (pid > 0) /* Parent */
222 {
223 /* Give the child some time to do the setsid(), otherwise the
224 * exit() may kill the child too (when starting gvim from inside a
225 * gvim). */
226 if (!pipe_error)
227 {
228 /* The read returns when the child closes the pipe (or when
229 * the child dies for some reason). */
230 close(pipefd[1]);
231 status = gui_read_child_pipe(pipefd[0]);
232 if (status == GUI_CHILD_FAILED)
233 {
234 /* The child failed to start the GUI, so the caller must
235 * continue. There may be more error information written
236 * to stderr by the child. */
237# ifdef __NeXT__
238 wait4(pid, &exit_status, 0, (struct rusage *)0);
239# else
240 waitpid(pid, &exit_status, 0);
241# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100242 emsg(_("E852: The child process failed to start the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200243 return;
244 }
245 else if (status == GUI_CHILD_IO_ERROR)
246 {
247 pipe_error = TRUE;
248 }
249 /* else GUI_CHILD_OK: parent exit */
250 }
251
252 if (pipe_error)
253 ui_delay(300L, TRUE);
254
255 /* When swapping screens we may need to go to the next line, e.g.,
256 * after a hit-enter prompt and using ":gui". */
257 if (newline_on_exit)
258 mch_errmsg("\r\n");
259
260 /*
261 * The parent must skip the normal exit() processing, the child
262 * will do it. For example, GTK messes up signals when exiting.
263 */
264 _exit(0);
265 }
266 /* Child */
267
Bram Moolenaar29695702012-05-18 17:03:18 +0200268#ifdef FEAT_GUI_GTK
269 /* Call gtk_init_check() here after fork(). See gui_init_check(). */
270 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100271 getout_preserve_modified(1);
Bram Moolenaar29695702012-05-18 17:03:18 +0200272#endif
273
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200274# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
275 /*
276 * Change our process group. On some systems/shells a CTRL-C in the
277 * shell where Vim was started would otherwise kill gvim!
278 */
279# if defined(HAVE_SETSID)
280 (void)setsid();
281# else
282 (void)setpgid(0, 0);
283# endif
284# endif
285 if (!pipe_error)
286 close(pipefd[0]);
287
288# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
289 /* Tell the session manager our new PID */
290 gui_mch_forked();
291# endif
292
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200293 /* Try to start the GUI */
294 gui_attempt_start();
295
296 /* Notify the parent */
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200297 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200298 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200299 if (gui.in_use)
300 write_eintr(pipefd[1], "ok", 3);
301 else
302 write_eintr(pipefd[1], "fail", 5);
303 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200304 }
305
306 /* If we failed to start the GUI, exit now. */
307 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100308 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200309}
310
311/*
312 * Read from a pipe assumed to be connected to the child process (this
313 * function is called from the parent).
314 * Return GUI_CHILD_OK if the child successfully started the GUI,
315 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
316 * some other error.
317 *
318 * The file descriptor will be closed before the function returns.
319 */
320 static int
321gui_read_child_pipe(int fd)
322{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200323 long bytes_read;
324#define READ_BUFFER_SIZE 10
325 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200326
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200327 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
328#undef READ_BUFFER_SIZE
329 close(fd);
330 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200331 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200332 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200333 if (strcmp(buffer, "ok") == 0)
334 return GUI_CHILD_OK;
335 return GUI_CHILD_FAILED;
336}
337
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200338#endif /* GUI_MAY_FORK */
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200339
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340/*
341 * Call this when vim starts up, whether or not the GUI is started
342 */
343 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100344gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345{
346 gui.in_use = FALSE; /* No GUI yet (maybe later) */
347 gui.starting = FALSE; /* No GUI yet (maybe later) */
348 gui_mch_prepare(argc, argv);
349}
350
351/*
352 * Try initializing the GUI and check if it can be started.
353 * Used from main() to check early if "vim -g" can start the GUI.
354 * Used from gui_init() to prepare for starting the GUI.
355 * Returns FAIL or OK.
356 */
357 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100358gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
360 static int result = MAYBE;
361
362 if (result != MAYBE)
363 {
364 if (result == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100365 emsg(_("E229: Cannot start the GUI"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 return result;
367 }
368
369 gui.shell_created = FALSE;
370 gui.dying = FALSE;
371 gui.in_focus = TRUE; /* so the guicursor setting works */
372 gui.dragged_sb = SBAR_NONE;
373 gui.dragged_wp = NULL;
374 gui.pointer_hidden = FALSE;
375 gui.col = 0;
376 gui.row = 0;
377 gui.num_cols = Columns;
378 gui.num_rows = Rows;
379
380 gui.cursor_is_valid = FALSE;
381 gui.scroll_region_top = 0;
382 gui.scroll_region_bot = Rows - 1;
383 gui.scroll_region_left = 0;
384 gui.scroll_region_right = Columns - 1;
385 gui.highlight_mask = HL_NORMAL;
386 gui.char_width = 1;
387 gui.char_height = 1;
388 gui.char_ascent = 0;
389 gui.border_width = 0;
390
391 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200392#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 gui.bold_font = NOFONT;
394 gui.ital_font = NOFONT;
395 gui.boldital_font = NOFONT;
396# ifdef FEAT_XFONTSET
397 gui.fontset = NOFONTSET;
398# endif
399#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200400 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100401#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200402 gui.wide_bold_font = NOFONT;
403 gui.wide_ital_font = NOFONT;
404 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406
407#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200408# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409# ifdef FONTSET_ALWAYS
410 gui.menu_fontset = NOFONTSET;
411# else
412 gui.menu_font = NOFONT;
413# endif
414# endif
415 gui.menu_is_active = TRUE; /* default: include menu */
416# ifndef FEAT_GUI_GTK
417 gui.menu_height = MENU_DEFAULT_HEIGHT;
418 gui.menu_width = 0;
419# endif
420#endif
421#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
422 gui.toolbar_height = 0;
423#endif
424#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
425 gui.footer_height = 0;
426#endif
427#ifdef FEAT_BEVAL_TIP
428 gui.tooltip_fontset = NOFONTSET;
429#endif
430
431 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
432 gui.prev_wrap = -1;
433
434#ifdef ALWAYS_USE_GUI
435 result = OK;
436#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200437# ifdef FEAT_GUI_GTK
438 /*
439 * Note: Don't call gtk_init_check() before fork, it will be called after
440 * the fork. When calling it before fork, it make vim hang for a while.
441 * See gui_do_fork().
442 * Use a simpler check if the GUI window can probably be opened.
443 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200444 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200445# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448#endif
449 return result;
450}
451
452/*
453 * This is the call which starts the GUI.
454 */
455 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100456gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457{
458 win_T *wp;
459 static int recursive = 0;
460
461 /*
462 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
463 * function will then be executed at the first call, the rest by the
464 * recursive call. This allow the shell to be opened halfway reading a
465 * gvimrc file.
466 */
467 if (!recursive)
468 {
469 ++recursive;
470
471 clip_init(TRUE);
472
473 /* If can't initialize, don't try doing the rest */
474 if (gui_init_check() == FAIL)
475 {
476 --recursive;
477 clip_init(FALSE);
478 return;
479 }
480
481 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000482 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
483 * breaks the Paste toolbar button.
484 */
485 set_option_value((char_u *)"paste", 0L, NULL, 0);
486
487 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 * Set up system-wide default menus.
489 */
490#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
491 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
492 {
493 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000494 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 sys_menu = FALSE;
496 }
497#endif
498
499 /*
500 * Switch on the mouse by default, unless the user changed it already.
501 * This can then be changed in the .gvimrc.
502 */
503 if (!option_was_set((char_u *)"mouse"))
504 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000505 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
507 /*
508 * If -U option given, use only the initializations from that file and
509 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
510 */
511 if (use_gvimrc != NULL)
512 {
513 if (STRCMP(use_gvimrc, "NONE") != 0
514 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000515 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100516 semsg(_("E230: Cannot read from \"%s\""), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
518 else
519 {
520 /*
521 * Get system wide defaults for gvim, only when file name defined.
522 */
523#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000524 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525#endif
526
527 /*
528 * Try to read GUI initialization commands from the following
529 * places:
530 * - environment variable GVIMINIT
531 * - the user gvimrc file (~/.gvimrc)
532 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
533 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
534 * The first that exists is used, the rest is ignored.
535 */
536 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000537 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
538 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000540 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
541 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200543#ifdef USR_GVIMRC_FILE3
544 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
545 DOSO_GVIMRC) == FAIL
546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 )
548 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200549#ifdef USR_GVIMRC_FILE4
550 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#endif
552 }
553
554 /*
555 * Read initialization commands from ".gvimrc" in current
556 * directory. This is only done if the 'exrc' option is set.
557 * Because of security reasons we disallow shell and write
558 * commands now, except for unix if the file is owned by the user
559 * or 'secure' option has been reset in environment of global
560 * ".gvimrc".
561 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
562 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
563 */
564 if (p_exrc)
565 {
566#ifdef UNIX
567 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200568 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569
570 /* if ".gvimrc" file is not owned by user, set 'secure'
571 * mode */
572 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
573 secure = p_secure;
574 }
575#else
576 secure = p_secure;
577#endif
578
579 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
580 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
581#ifdef SYS_GVIMRC_FILE
582 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
583 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
584#endif
585#ifdef USR_GVIMRC_FILE2
586 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
587 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
588#endif
589#ifdef USR_GVIMRC_FILE3
590 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
591 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
592#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200593#ifdef USR_GVIMRC_FILE4
594 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
595 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000598 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599
600 if (secure == 2)
601 need_wait_return = TRUE;
602 secure = 0;
603 }
604 }
605
606 if (need_wait_return || msg_didany)
607 wait_return(TRUE);
608
609 --recursive;
610 }
611
612 /* If recursive call opened the shell, return here from the first call */
613 if (gui.in_use)
614 return;
615
616 /*
617 * Create the GUI shell.
618 */
619 gui.in_use = TRUE; /* Must be set after menus have been set up */
620 if (gui_mch_init() == FAIL)
621 goto error;
622
623 /* Avoid a delay for an error message that was printed in the terminal
624 * where Vim was started. */
625 emsg_on_display = FALSE;
626 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100627 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 need_wait_return = FALSE;
629 msg_didany = FALSE;
630
631 /*
632 * Check validity of any generic resources that may have been loaded.
633 */
634 if (gui.border_width < 0)
635 gui.border_width = 0;
636
637 /*
638 * Set up the fonts. First use a font specified with "-fn" or "-font".
639 */
640 if (font_argument != NULL)
641 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
642 if (
643#ifdef FEAT_XFONTSET
644 (*p_guifontset == NUL
645 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
646#endif
647 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
648 : p_guifont, FALSE) == FAIL)
649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100650 emsg(_("E665: Cannot start GUI, no valid font found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 goto error2;
652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (gui_get_wide_font() == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100654 emsg(_("E231: 'guifontwide' invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
656 gui.num_cols = Columns;
657 gui.num_rows = Rows;
658 gui_reset_scroll_region();
659
660 /* Create initial scrollbars */
661 FOR_ALL_WINDOWS(wp)
662 {
663 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
664 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
665 }
666 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
667
668#ifdef FEAT_MENU
669 gui_create_initial_menus(root_menu);
670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671#ifdef FEAT_SIGN_ICONS
672 sign_gui_started();
673#endif
674
675 /* Configure the desired menu and scrollbars */
676 gui_init_which_components(NULL);
677
678 /* All components of the GUI have been created now */
679 gui.shell_created = TRUE;
680
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100681#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100682 // Set the shell size, adjusted for the screen size. For GTK this only
683 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100684 // If the window is already maximized (e.g. when --windowid is passed in),
685 // we want to use the system-provided dimensions by passing FALSE to
686 // mustset. Otherwise, we want to initialize with the default rows/columns.
687 if (gui_mch_maximized())
688 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
689 else
690 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100691#else
692# ifndef FEAT_GUI_GTK
693 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#endif
696#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
697 /* Need to set the size of the menubar after all the menus have been
698 * created. */
699 gui_mch_compute_menu_height((Widget)0);
700#endif
701
702 /*
703 * Actually open the GUI shell.
704 */
705 if (gui_mch_open() != FAIL)
706 {
707#ifdef FEAT_TITLE
708 maketitle();
709 resettitle();
710#endif
711 init_gui_options();
712#ifdef FEAT_ARABIC
713 /* Our GUI can't do bidi. */
714 p_tbidi = FALSE;
715#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000716#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 /* Give GTK+ a chance to put all widget's into place. */
718 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000719
720# ifdef FEAT_MENU
721 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
722 * It was still there to make F10 work. */
723 if (vim_strchr(p_go, GO_MENUS) == NULL)
724 {
725 --gui.starting;
726 gui_mch_enable_menu(FALSE);
727 ++gui.starting;
728 gui_mch_update();
729 }
730# endif
731
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 /* Now make sure the shell fits on the screen. */
Bram Moolenaar372674f2019-03-30 20:31:22 +0100733 if (gui_mch_maximized())
734 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
735 else
736 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000738 /* When 'lines' was set while starting up the topframe may have to be
739 * resized. */
740 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000741
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100742#ifdef FEAT_BEVAL_GUI
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000743 /* Always create the Balloon Evaluation area, but disable it when
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100744 * 'ballooneval' is off. */
745 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200746 {
747# ifdef FEAT_VARTABS
748 vim_free(balloonEval->vts);
749# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100750 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200751 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100752 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000753# ifdef FEAT_GUI_GTK
754 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
755 &general_beval_cb, NULL);
756# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000757# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000758 {
759 extern Widget textArea;
760 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000761 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000762 }
763# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100764# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000765 balloonEval = gui_mch_create_beval_area(NULL, NULL,
766 &general_beval_cb, NULL);
767# endif
768# endif
769# endif
770 if (!p_beval)
771 gui_mch_disable_beval_area(balloonEval);
772#endif
773
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
775 if (!im_xim_isvalid_imactivate())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100776 emsg(_("E599: Value of 'imactivatekey' is invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000778 /* When 'cmdheight' was set during startup it may not have taken
779 * effect yet. */
780 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000781 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782
783 return;
784 }
785
786error2:
787#ifdef FEAT_GUI_X11
788 /* undo gui_mch_init() */
789 gui_mch_uninit();
790#endif
791
792error:
793 gui.in_use = FALSE;
794 clip_init(FALSE);
795}
796
797
798 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100799gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 /* don't free the fonts, it leads to a BUS error
802 * richard@whitequeen.com Jul 99 */
803 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 gui.in_use = FALSE;
805 gui_mch_exit(rc);
806}
807
Bram Moolenaar9372a112005-12-06 19:59:18 +0000808#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000810# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811/*
812 * Called when the GUI shell is closed by the user. If there are no changed
813 * files Vim exits, otherwise there will be a dialog to ask the user what to
814 * do.
815 * When this function returns, Vim should NOT exit!
816 */
817 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100818gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
820 cmdmod_T save_cmdmod;
821
822 save_cmdmod = cmdmod;
823
824 /* Only exit when there are no changed files */
825 exiting = TRUE;
826# ifdef FEAT_BROWSE
827 cmdmod.browse = TRUE;
828# endif
829# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
830 cmdmod.confirm = TRUE;
831# endif
832 /* If there are changed buffers, present the user with a dialog if
833 * possible, otherwise give an error message. */
Bram Moolenaar027387f2016-01-02 22:25:52 +0100834 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 getout(0);
836
837 exiting = FALSE;
838 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000839 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840}
841#endif
842
843/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200844 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 * first font name that works is used. If none is found, use the default
846 * font.
847 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
848 * Return OK when able to set the font. When it failed FAIL is returned and
849 * the fonts are unchanged.
850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100852gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
854#define FONTLEN 320
855 char_u font_name[FONTLEN];
856 int font_list_empty = FALSE;
857 int ret = FAIL;
858
859 if (!gui.in_use)
860 return FAIL;
861
862 font_name[0] = NUL;
863 if (*font_list == NUL)
864 font_list_empty = TRUE;
865 else
866 {
867#ifdef FEAT_XFONTSET
868 /* When using a fontset, the whole list of fonts is one name. */
869 if (fontset)
870 ret = gui_mch_init_font(font_list, TRUE);
871 else
872#endif
873 while (*font_list != NUL)
874 {
875 /* Isolate one comma separated font name. */
876 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
877
878 /* Careful!!! The Win32 version of gui_mch_init_font(), when
879 * called with "*" will change p_guifont to the selected font
880 * name, which frees the old value. This makes font_list
881 * invalid. Thus when OK is returned here, font_list must no
882 * longer be used! */
883 if (gui_mch_init_font(font_name, FALSE) == OK)
884 {
Bram Moolenaar13505972019-01-24 15:04:48 +0100885#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 /* If it's a Unicode font, try setting 'guifontwide' to a
887 * similar double-width font. */
888 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
889 && strstr((char *)font_name, "10646") != NULL)
890 set_guifontwide(font_name);
891#endif
892 ret = OK;
893 break;
894 }
895 }
896 }
897
898 if (ret != OK
899 && STRCMP(font_list, "*") != 0
900 && (font_list_empty || gui.norm_font == NOFONT))
901 {
902 /*
903 * Couldn't load any font in 'font_list', keep the current font if
904 * there is one. If 'font_list' is empty, or if there is no current
905 * font, tell gui_mch_init_font() to try to find a font we can load.
906 */
907 ret = gui_mch_init_font(NULL, FALSE);
908 }
909
910 if (ret == OK)
911 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200912#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 /* Set normal font as current font */
914# ifdef FEAT_XFONTSET
915 if (gui.fontset != NOFONTSET)
916 gui_mch_set_fontset(gui.fontset);
917 else
918# endif
919 gui_mch_set_font(gui.norm_font);
920#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100921 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 }
923
924 return ret;
925}
926
Bram Moolenaar13505972019-01-24 15:04:48 +0100927#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928/*
929 * Try setting 'guifontwide' to a font twice as wide as "name".
930 */
931 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100932set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
934 int i = 0;
935 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
936 char_u *wp = NULL;
937 char_u *p;
938 GuiFont font;
939
940 wp = wide_name;
941 for (p = name; *p != NUL; ++p)
942 {
943 *wp++ = *p;
944 if (*p == '-')
945 {
946 ++i;
947 if (i == 6) /* font type: change "--" to "-*-" */
948 {
949 if (p[1] == '-')
950 *wp++ = '*';
951 }
952 else if (i == 12) /* found the width */
953 {
954 ++p;
955 i = getdigits(&p);
956 if (i != 0)
957 {
958 /* Double the width specification. */
959 sprintf((char *)wp, "%d%s", i * 2, p);
960 font = gui_mch_get_font(wide_name, FALSE);
961 if (font != NOFONT)
962 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000963 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 gui.wide_font = font;
965 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000966 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968 }
969 break;
970 }
971 }
972 }
973}
Bram Moolenaar13505972019-01-24 15:04:48 +0100974#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
976/*
977 * Get the font for 'guifontwide'.
978 * Return FAIL for an invalid font name.
979 */
980 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100981gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982{
983 GuiFont font = NOFONT;
984 char_u font_name[FONTLEN];
985 char_u *p;
986
987 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
988 return OK; /* Will give an error message later. */
989
990 if (p_guifontwide != NULL && *p_guifontwide != NUL)
991 {
992 for (p = p_guifontwide; *p != NUL; )
993 {
994 /* Isolate one comma separated font name. */
995 (void)copy_option_part(&p, font_name, FONTLEN, ",");
996 font = gui_mch_get_font(font_name, FALSE);
997 if (font != NOFONT)
998 break;
999 }
1000 if (font == NOFONT)
1001 return FAIL;
1002 }
1003
1004 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001005#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
1007 if (font != NOFONT && gui.norm_font != NOFONT
1008 && pango_font_description_equal(font, gui.norm_font))
1009 {
1010 gui.wide_font = NOFONT;
1011 gui_mch_free_font(font);
1012 }
1013 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001016#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001017 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001018#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001019 /*
1020 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1021 * support those fonts for 'guifontwide'.
1022 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 return OK;
1025}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001028gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
1030 gui.row = row;
1031 gui.col = col;
1032}
1033
1034/*
1035 * gui_check_pos - check if the cursor is on the screen.
1036 */
1037 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001038gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039{
1040 if (gui.row >= screen_Rows)
1041 gui.row = screen_Rows - 1;
1042 if (gui.col >= screen_Columns)
1043 gui.col = screen_Columns - 1;
1044 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1045 gui.cursor_is_valid = FALSE;
1046}
1047
1048/*
1049 * Redraw the cursor if necessary or when forced.
1050 * Careful: The contents of ScreenLines[] must match what is on the screen,
1051 * otherwise this goes wrong. May need to call out_flush() first.
1052 */
1053 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001054gui_update_cursor(
1055 int force, /* when TRUE, update even when not moved */
1056 int clear_selection)/* clear selection under cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057{
1058 int cur_width = 0;
1059 int cur_height = 0;
1060 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001061 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001063#ifdef FEAT_TERMINAL
1064 guicolor_T shape_fg = INVALCOLOR;
1065 guicolor_T shape_bg = INVALCOLOR;
1066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1068 int cattr; /* cursor attributes */
1069 int attr;
1070 attrentry_T *aep = NULL;
1071
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001072 /* Don't update the cursor when halfway busy scrolling or the screen size
1073 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1074 if (!can_update_cursor || screen_Columns != gui.num_cols
1075 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 return;
1077
1078 gui_check_pos();
1079 if (!gui.cursor_is_valid || force
1080 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1081 {
1082 gui_undraw_cursor();
1083 if (gui.row < 0)
1084 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001085#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1087 im_set_position(gui.row, gui.col);
1088#endif
1089 gui.cursor_row = gui.row;
1090 gui.cursor_col = gui.col;
1091
1092 /* Only write to the screen after ScreenLines[] has been initialized */
1093 if (!screen_cleared || ScreenLines == NULL)
1094 return;
1095
1096 /* Clear the selection if we are about to write over it */
1097 if (clear_selection)
1098 clip_may_clear_selection(gui.row, gui.row);
1099 /* Check that the cursor is inside the shell (resizing may have made
1100 * it invalid) */
1101 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1102 return;
1103
1104 gui.cursor_is_valid = TRUE;
1105
1106 /*
1107 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001108 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001110#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001111 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001112 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001114#endif
1115 shape = &shape_table[get_shape_idx(FALSE)];
1116 if (State & LANGMAP)
1117 id = shape->id_lm;
1118 else
1119 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
1121 /* get the colors and attributes for the cursor. Default is inverted */
1122 cfg = INVALCOLOR;
1123 cbg = INVALCOLOR;
1124 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001125 gui_mch_set_blinking(shape->blinkwait,
1126 shape->blinkon,
1127 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001128 if (shape->blinkwait == 0 || shape->blinkon == 0
1129 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001130 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001131#ifdef FEAT_TERMINAL
1132 if (shape_bg != INVALCOLOR)
1133 {
1134 cattr = 0;
1135 cfg = shape_fg;
1136 cbg = shape_bg;
1137 }
1138 else
1139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (id > 0)
1141 {
1142 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001143#if defined(HAVE_INPUT_METHOD) || defined(FEAT_HANGULIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
1145 static int iid;
1146 guicolor_T fg, bg;
1147
Bram Moolenaarc236c162008-07-13 17:41:49 +00001148 if (
Bram Moolenaar28944fe2018-02-04 19:01:31 +01001149# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001150 preedit_get_status()
1151# else
1152 im_get_status()
1153# endif
1154 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 {
1156 iid = syn_name2id((char_u *)"CursorIM");
1157 if (iid > 0)
1158 {
1159 syn_id2colors(iid, &fg, &bg);
1160 if (bg != INVALCOLOR)
1161 cbg = bg;
1162 if (fg != INVALCOLOR)
1163 cfg = fg;
1164 }
1165 }
1166 }
1167#endif
1168 }
1169
1170 /*
1171 * Get the attributes for the character under the cursor.
1172 * When no cursor color was given, use the character color.
1173 */
1174 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1175 if (attr > HL_ALL)
1176 aep = syn_gui_attr2entry(attr);
1177 if (aep != NULL)
1178 {
1179 attr = aep->ae_attr;
1180 if (cfg == INVALCOLOR)
1181 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1182 : aep->ae_u.gui.fg_color);
1183 if (cbg == INVALCOLOR)
1184 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1185 : aep->ae_u.gui.bg_color);
1186 }
1187 if (cfg == INVALCOLOR)
1188 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1189 if (cbg == INVALCOLOR)
1190 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1191
1192#ifdef FEAT_XIM
1193 if (aep != NULL)
1194 {
1195 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1196 : aep->ae_u.gui.bg_color);
1197 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1198 : aep->ae_u.gui.fg_color);
1199 if (xim_bg_color == INVALCOLOR)
1200 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1201 : gui.back_pixel;
1202 if (xim_fg_color == INVALCOLOR)
1203 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1204 : gui.norm_pixel;
1205 }
1206 else
1207 {
1208 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1209 : gui.back_pixel;
1210 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1211 : gui.norm_pixel;
1212 }
1213#endif
1214
1215 attr &= ~HL_INVERSE;
1216 if (cattr & HL_INVERSE)
1217 {
1218 cc = cbg;
1219 cbg = cfg;
1220 cfg = cc;
1221 }
1222 cattr &= ~HL_INVERSE;
1223
1224 /*
1225 * When we don't have window focus, draw a hollow cursor.
1226 */
1227 if (!gui.in_focus)
1228 {
1229 gui_mch_draw_hollow_cursor(cbg);
1230 return;
1231 }
1232
1233 old_hl_mask = gui.highlight_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001234 if (shape->shape == SHAPE_BLOCK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235#ifdef FEAT_HANGULIN
1236 || composing_hangul
1237#endif
1238 )
1239 {
1240 /*
1241 * Draw the text character with the cursor colors. Use the
1242 * character attributes plus the cursor attributes.
1243 */
1244 gui.highlight_mask = (cattr | attr);
1245#ifdef FEAT_HANGULIN
1246 if (composing_hangul)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001247 {
1248 char_u *comp_buf;
1249 int comp_len;
1250
1251 comp_buf = hangul_composing_buffer_get(&comp_len);
1252 if (comp_buf)
1253 {
1254 (void)gui_outstr_nowrap(comp_buf, comp_len,
1255 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
1256 cfg, cbg, 0);
1257 vim_free(comp_buf);
1258 }
1259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 else
1261#endif
1262 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1263 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1264 }
1265 else
1266 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001267#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 int col_off = FALSE;
1269#endif
1270 /*
1271 * First draw the partial cursor, then overwrite with the text
1272 * character, using a transparent background.
1273 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001274 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {
1276 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001277 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 else
1280 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001281 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 cur_width = gui.char_width;
1283 }
Bram Moolenaar367329b2007-08-30 11:53:22 +00001284 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1285 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {
1287 /* Double wide character. */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001288 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001290#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if (CURSOR_BAR_RIGHT)
1292 {
1293 /* gui.col points to the left halve of the character but
1294 * the vertical line needs to be on the right halve.
1295 * A double-wide horizontal line is also drawn from the
1296 * right halve in gui_mch_draw_part_cursor(). */
1297 col_off = TRUE;
1298 ++gui.col;
1299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001303#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (col_off)
1305 --gui.col;
1306#endif
1307
1308#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1309 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1310 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1311 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1312 (guicolor_T)0, (guicolor_T)0, 0);
1313#endif
1314 }
1315 gui.highlight_mask = old_hl_mask;
1316 }
1317}
1318
1319#if defined(FEAT_MENU) || defined(PROTO)
1320 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001321gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001323# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (gui.menu_is_active && gui.in_use)
1325 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1326# endif
1327}
1328#endif
1329
1330/*
1331 * Position the various GUI components (text area, menu). The vertical
1332 * scrollbars are NOT handled here. See gui_update_scrollbars().
1333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001335gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 int text_area_x;
1338 int text_area_y;
1339 int text_area_width;
1340 int text_area_height;
1341
1342 /* avoid that moving components around generates events */
1343 ++hold_gui_events;
1344
1345 text_area_x = 0;
1346 if (gui.which_scrollbars[SBAR_LEFT])
1347 text_area_x += gui.scrollbar_width;
1348
1349 text_area_y = 0;
1350#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1351 gui.menu_width = total_width;
1352 if (gui.menu_is_active)
1353 text_area_y += gui.menu_height;
1354#endif
1355#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1356 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1357 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1358#endif
1359
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001360# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001361 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001362 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001363 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001364#endif
1365
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1367 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1368 {
1369# ifdef FEAT_GUI_ATHENA
1370 gui_mch_set_toolbar_pos(0, text_area_y,
1371 gui.menu_width, gui.toolbar_height);
1372# endif
1373 text_area_y += gui.toolbar_height;
1374 }
1375#endif
1376
1377 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1378 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1379
1380 gui_mch_set_text_area_pos(text_area_x,
1381 text_area_y,
1382 text_area_width,
1383 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001384#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 + xim_get_status_area_height()
1386#endif
1387 );
1388#ifdef FEAT_MENU
1389 gui_position_menu();
1390#endif
1391 if (gui.which_scrollbars[SBAR_BOTTOM])
1392 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1393 text_area_x,
1394 text_area_y + text_area_height,
1395 text_area_width,
1396 gui.scrollbar_height);
1397 gui.left_sbar_x = 0;
1398 gui.right_sbar_x = text_area_x + text_area_width;
1399
1400 --hold_gui_events;
1401}
1402
Bram Moolenaar02743632005-07-25 20:42:36 +00001403/*
1404 * Get the width of the widgets and decorations to the side of the text area.
1405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001407gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 int base_width;
1410
1411 base_width = 2 * gui.border_offset;
1412 if (gui.which_scrollbars[SBAR_LEFT])
1413 base_width += gui.scrollbar_width;
1414 if (gui.which_scrollbars[SBAR_RIGHT])
1415 base_width += gui.scrollbar_width;
1416 return base_width;
1417}
1418
Bram Moolenaar02743632005-07-25 20:42:36 +00001419/*
1420 * Get the height of the widgets and decorations above and below the text area.
1421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001423gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
1425 int base_height;
1426
1427 base_height = 2 * gui.border_offset;
1428 if (gui.which_scrollbars[SBAR_BOTTOM])
1429 base_height += gui.scrollbar_height;
1430#ifdef FEAT_GUI_GTK
1431 /* We can't take the sizes properly into account until anything is
1432 * realized. Therefore we recalculate all the values here just before
1433 * setting the size. (--mdcki) */
1434#else
1435# ifdef FEAT_MENU
1436 if (gui.menu_is_active)
1437 base_height += gui.menu_height;
1438# endif
1439# ifdef FEAT_TOOLBAR
1440 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1441# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1442 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1443# else
1444 base_height += gui.toolbar_height;
1445# endif
1446# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001447# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1448 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001449 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001450 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452# ifdef FEAT_FOOTER
1453 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1454 base_height += gui.footer_height;
1455# endif
1456# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1457 base_height += gui_mch_text_area_extra_height();
1458# endif
1459#endif
1460 return base_height;
1461}
1462
1463/*
1464 * Should be called after the GUI shell has been resized. Its arguments are
1465 * the new width and height of the shell in pixels.
1466 */
1467 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001468gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
1470 static int busy = FALSE;
1471
1472 if (!gui.shell_created) /* ignore when still initializing */
1473 return;
1474
1475 /*
1476 * Can't resize the screen while it is being redrawn. Remember the new
1477 * size and handle it later.
1478 */
1479 if (updating_screen || busy)
1480 {
1481 new_pixel_width = pixel_width;
1482 new_pixel_height = pixel_height;
1483 return;
1484 }
1485
1486again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001487 new_pixel_width = 0;
1488 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 busy = TRUE;
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 /* Flush pending output before redrawing */
1492 out_flush();
1493
1494 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001495 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
1497 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001499
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 /*
1501 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1502 * at the last line here (why does it have to be one row too low?).
1503 */
1504 if (State == ASKMORE || State == CONFIRM)
1505 gui.row = gui.num_rows;
1506
1507 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1508 * the safe side. */
1509 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1510 || gui.num_rows != Rows || gui.num_cols != Columns)
1511 shell_resized();
1512
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 gui_update_scrollbars(TRUE);
1514 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001515#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 xim_set_status_area();
1517#endif
1518
1519 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001520
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001521 /* We may have been called again while redrawing the screen.
1522 * Need to do it all again with the latest size then. But only if the size
1523 * actually changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (new_pixel_height)
1525 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001526 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1527 {
1528 new_pixel_width = 0;
1529 new_pixel_height = 0;
1530 }
1531 else
1532 {
1533 pixel_width = new_pixel_width;
1534 pixel_height = new_pixel_height;
1535 goto again;
1536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538}
1539
1540/*
1541 * Check if gui_resize_shell() must be called.
1542 */
1543 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001544gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (new_pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 /* careful: gui_resize_shell() may postpone the resize again if we
1548 * were called indirectly by it */
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001549 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550}
1551
1552 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001553gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 Rows = gui.num_rows;
1556 Columns = gui.num_cols;
1557 return OK;
1558}
1559
1560/*
1561 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001562 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1563 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001564 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1565 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001568gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001569 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001570 int fit_to_display,
1571 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572{
1573 int base_width;
1574 int base_height;
1575 int width;
1576 int height;
1577 int min_width;
1578 int min_height;
1579 int screen_w;
1580 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001581#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001582 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001583 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001584#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001585 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
1587 if (!gui.shell_created)
1588 return;
1589
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001590#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /* If not setting to a user specified size and maximized, calculate the
1592 * number of characters that fit in the maximized window. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001593 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1594 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
1596 gui_mch_newfont();
1597 return;
1598 }
1599#endif
1600
1601 base_width = gui_get_base_width();
1602 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001603 if (fit_to_display)
1604 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001605 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001606
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001607 width = Columns * gui.char_width + base_width;
1608 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
1610 if (fit_to_display)
1611 {
1612 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001613 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 Columns = (screen_w - base_width) / gui.char_width;
1616 if (Columns < MIN_COLUMNS)
1617 Columns = MIN_COLUMNS;
1618 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001619#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001620 ++did_adjust;
1621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001623 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 Rows = (screen_h - base_height) / gui.char_height;
1626 check_shellsize();
1627 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001628#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001629 ++did_adjust;
1630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001632#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001633 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1634 && height + gui.char_height >= screen_h))
1635 /* don't unmaximize if at maximum size */
1636 un_maximize = FALSE;
1637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001639 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 gui.num_cols = Columns;
1641 gui.num_rows = Rows;
1642
1643 min_width = base_width + MIN_COLUMNS * gui.char_width;
1644 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001645 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001646
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001647#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001648 if (un_maximize)
1649 {
1650 /* If the window size is smaller than the screen unmaximize the
1651 * window, otherwise resizing won't work. */
1652 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1653 if ((width + gui.char_width < screen_w
1654 || height + gui.char_height * 2 < screen_h)
1655 && gui_mch_maximized())
1656 gui_mch_unmaximize();
1657 }
1658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001661 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001663 if (fit_to_display && x >= 0 && y >= 0)
1664 {
1665 /* Some window managers put the Vim window left of/above the screen.
1666 * Only change the position if it wasn't already negative before
1667 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 gui_mch_update();
1669 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1670 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1671 }
1672
1673 gui_position_components(width);
1674 gui_update_scrollbars(TRUE);
1675 gui_reset_scroll_region();
1676}
1677
1678/*
1679 * Called when Rows and/or Columns has changed.
1680 */
1681 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001682gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
1684 gui_reset_scroll_region();
1685}
1686
1687/*
1688 * Make scroll region cover whole screen.
1689 */
1690 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001691gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
1693 gui.scroll_region_top = 0;
1694 gui.scroll_region_bot = gui.num_rows - 1;
1695 gui.scroll_region_left = 0;
1696 gui.scroll_region_right = gui.num_cols - 1;
1697}
1698
1699 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001700gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 if (mask > HL_ALL) /* highlight code */
1703 gui.highlight_mask = mask;
1704 else /* mask */
1705 gui.highlight_mask |= mask;
1706}
1707
1708 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001709gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 if (mask > HL_ALL) /* highlight code */
1712 gui.highlight_mask = HL_NORMAL;
1713 else /* mask */
1714 gui.highlight_mask &= ~mask;
1715}
1716
1717/*
1718 * Clear a rectangular region of the screen from text pos (row1, col1) to
1719 * (row2, col2) inclusive.
1720 */
1721 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001722gui_clear_block(
1723 int row1,
1724 int col1,
1725 int row2,
1726 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 /* Clear the selection if we are about to write over it */
1729 clip_may_clear_selection(row1, row2);
1730
1731 gui_mch_clear_block(row1, col1, row2, col2);
1732
1733 /* Invalidate cursor if it was in this block */
1734 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1735 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1736 gui.cursor_is_valid = FALSE;
1737}
1738
1739/*
1740 * Write code to update the cursor later. This avoids the need to flush the
1741 * output buffer before calling gui_update_cursor().
1742 */
1743 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001744gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001746 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747}
1748
1749 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001750gui_write(
1751 char_u *s,
1752 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 char_u *p;
1755 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 int force_scrollbar = FALSE;
1758 static win_T *old_curwin = NULL;
1759
1760/* #define DEBUG_GUI_WRITE */
1761#ifdef DEBUG_GUI_WRITE
1762 {
1763 int i;
1764 char_u *str;
1765
1766 printf("gui_write(%d):\n ", len);
1767 for (i = 0; i < len; i++)
1768 if (s[i] == ESC)
1769 {
1770 if (i != 0)
1771 printf("\n ");
1772 printf("<ESC>");
1773 }
1774 else
1775 {
1776 str = transchar_byte(s[i]);
1777 if (str[0] && str[1])
1778 printf("<%s>", (char *)str);
1779 else
1780 printf("%s", (char *)str);
1781 }
1782 printf("\n");
1783 }
1784#endif
1785 while (len)
1786 {
1787 if (s[0] == ESC && s[1] == '|')
1788 {
1789 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001790 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
1792 arg1 = getdigits(&p);
1793 if (p > s + len)
1794 break;
1795 if (*p == ';')
1796 {
1797 ++p;
1798 arg2 = getdigits(&p);
1799 if (p > s + len)
1800 break;
1801 }
1802 }
1803 switch (*p)
1804 {
1805 case 'C': /* Clear screen */
1806 clip_scroll_selection(9999);
1807 gui_mch_clear_all();
1808 gui.cursor_is_valid = FALSE;
1809 force_scrollbar = TRUE;
1810 break;
1811 case 'M': /* Move cursor */
1812 gui_set_cursor(arg1, arg2);
1813 break;
1814 case 's': /* force cursor (shape) update */
1815 force_cursor = TRUE;
1816 break;
1817 case 'R': /* Set scroll region */
1818 if (arg1 < arg2)
1819 {
1820 gui.scroll_region_top = arg1;
1821 gui.scroll_region_bot = arg2;
1822 }
1823 else
1824 {
1825 gui.scroll_region_top = arg2;
1826 gui.scroll_region_bot = arg1;
1827 }
1828 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 case 'V': /* Set vertical scroll region */
1830 if (arg1 < arg2)
1831 {
1832 gui.scroll_region_left = arg1;
1833 gui.scroll_region_right = arg2;
1834 }
1835 else
1836 {
1837 gui.scroll_region_left = arg2;
1838 gui.scroll_region_right = arg1;
1839 }
1840 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 case 'd': /* Delete line */
1842 gui_delete_lines(gui.row, 1);
1843 break;
1844 case 'D': /* Delete lines */
1845 gui_delete_lines(gui.row, arg1);
1846 break;
1847 case 'i': /* Insert line */
1848 gui_insert_lines(gui.row, 1);
1849 break;
1850 case 'I': /* Insert lines */
1851 gui_insert_lines(gui.row, arg1);
1852 break;
1853 case '$': /* Clear to end-of-line */
1854 gui_clear_block(gui.row, gui.col, gui.row,
1855 (int)Columns - 1);
1856 break;
1857 case 'h': /* Turn on highlighting */
1858 gui_start_highlight(arg1);
1859 break;
1860 case 'H': /* Turn off highlighting */
1861 gui_stop_highlight(arg1);
1862 break;
1863 case 'f': /* flash the window (visual bell) */
1864 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1865 break;
1866 default:
1867 p = s + 1; /* Skip the ESC */
1868 break;
1869 }
1870 len -= (int)(++p - s);
1871 s = p;
1872 }
1873 else if (
1874#ifdef EBCDIC
1875 CtrlChar(s[0]) != 0 /* Ctrl character */
1876#else
1877 s[0] < 0x20 /* Ctrl character */
1878#endif
1879#ifdef FEAT_SIGN_ICONS
1880 && s[0] != SIGN_BYTE
1881# ifdef FEAT_NETBEANS_INTG
1882 && s[0] != MULTISIGN_BYTE
1883# endif
1884#endif
1885 )
1886 {
1887 if (s[0] == '\n') /* NL */
1888 {
1889 gui.col = 0;
1890 if (gui.row < gui.scroll_region_bot)
1891 gui.row++;
1892 else
1893 gui_delete_lines(gui.scroll_region_top, 1);
1894 }
1895 else if (s[0] == '\r') /* CR */
1896 {
1897 gui.col = 0;
1898 }
1899 else if (s[0] == '\b') /* Backspace */
1900 {
1901 if (gui.col)
1902 --gui.col;
1903 }
1904 else if (s[0] == Ctrl_L) /* cursor-right */
1905 {
1906 ++gui.col;
1907 }
1908 else if (s[0] == Ctrl_G) /* Beep */
1909 {
1910 gui_mch_beep();
1911 }
1912 /* Other Ctrl character: shouldn't happen! */
1913
1914 --len; /* Skip this char */
1915 ++s;
1916 }
1917 else
1918 {
1919 p = s;
1920 while (len > 0 && (
1921#ifdef EBCDIC
1922 CtrlChar(*p) == 0
1923#else
1924 *p >= 0x20
1925#endif
1926#ifdef FEAT_SIGN_ICONS
1927 || *p == SIGN_BYTE
1928# ifdef FEAT_NETBEANS_INTG
1929 || *p == MULTISIGN_BYTE
1930# endif
1931#endif
1932 ))
1933 {
1934 len--;
1935 p++;
1936 }
1937 gui_outstr(s, (int)(p - s));
1938 s = p;
1939 }
1940 }
1941
1942 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1943 * set). */
1944 if (force_cursor)
1945 gui_update_cursor(TRUE, TRUE);
1946
1947 /* When switching to another window the dragging must have stopped.
1948 * Required for GTK, dragged_sb isn't reset. */
1949 if (old_curwin != curwin)
1950 gui.dragged_sb = SBAR_NONE;
1951
1952 /* Update the scrollbars after clearing the screen or when switched
1953 * to another window.
1954 * Update the horizontal scrollbar always, it's difficult to check all
1955 * situations where it might change. */
1956 if (force_scrollbar || old_curwin != curwin)
1957 gui_update_scrollbars(force_scrollbar);
1958 else
1959 gui_update_horiz_scrollbar(FALSE);
1960 old_curwin = curwin;
1961
1962 /*
1963 * We need to make sure this is cleared since Athena doesn't tell us when
1964 * he is done dragging. Do the same for GTK.
1965 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001966#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 gui.dragged_sb = SBAR_NONE;
1968#endif
1969
Bram Moolenaara338adc2018-01-31 20:51:47 +01001970 gui_may_flush(); /* In case vim decides to take a nap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971}
1972
1973/*
1974 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1975 * produces wrong results. Call gui_dont_update_cursor() before that code and
1976 * gui_can_update_cursor() afterwards.
1977 */
1978 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02001979gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
1981 if (gui.in_use)
1982 {
1983 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02001984 if (undraw)
1985 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 can_update_cursor = FALSE;
1987 }
1988}
1989
1990 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001991gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992{
1993 can_update_cursor = TRUE;
1994 /* No need to update the cursor right now, there is always more output
1995 * after scrolling. */
1996}
1997
Bram Moolenaara338adc2018-01-31 20:51:47 +01001998/*
1999 * Disable issuing gui_mch_flush().
2000 */
2001 void
2002gui_disable_flush(void)
2003{
2004 ++disable_flush;
2005}
2006
2007/*
2008 * Enable issuing gui_mch_flush().
2009 */
2010 void
2011gui_enable_flush(void)
2012{
2013 --disable_flush;
2014}
2015
2016/*
2017 * Issue gui_mch_flush() if it is not disabled.
2018 */
2019 void
2020gui_may_flush(void)
2021{
2022 if (disable_flush == 0)
2023 gui_mch_flush();
2024}
2025
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002027gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
2029 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
2032 if (len == 0)
2033 return;
2034
2035 if (len < 0)
2036 len = (int)STRLEN(s);
2037
2038 while (len > 0)
2039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 if (has_mbyte)
2041 {
2042 /* Find out how many chars fit in the current line. */
2043 cells = 0;
2044 for (this_len = 0; this_len < len; )
2045 {
2046 cells += (*mb_ptr2cells)(s + this_len);
2047 if (gui.col + cells > Columns)
2048 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002049 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 }
2051 if (this_len > len)
2052 this_len = len; /* don't include following composing char */
2053 }
2054 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 if (gui.col + len > Columns)
2056 this_len = Columns - gui.col;
2057 else
2058 this_len = len;
2059
2060 (void)gui_outstr_nowrap(s, this_len,
2061 0, (guicolor_T)0, (guicolor_T)0, 0);
2062 s += this_len;
2063 len -= this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 /* fill up for a double-width char that doesn't fit. */
2065 if (len > 0 && gui.col < Columns)
2066 (void)gui_outstr_nowrap((char_u *)" ", 1,
2067 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 /* The cursor may wrap to the next line. */
2069 if (gui.col >= Columns)
2070 {
2071 gui.col = 0;
2072 gui.row++;
2073 }
2074 }
2075}
2076
2077/*
2078 * Output one character (may be one or two display cells).
2079 * Caller must check for valid "off".
2080 * Returns FAIL or OK, just like gui_outstr_nowrap().
2081 */
2082 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002083gui_screenchar(
2084 int off, /* Offset from start of screen */
2085 int flags,
2086 guicolor_T fg, /* colors for cursor */
2087 guicolor_T bg, /* colors for cursor */
2088 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 char_u buf[MB_MAXBYTES + 1];
2091
2092 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2093 if (enc_utf8 && ScreenLines[off] == 0)
2094 return OK;
2095
2096 if (enc_utf8 && ScreenLinesUC[off] != 0)
2097 /* Draw UTF-8 multi-byte character. */
2098 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2099 flags, fg, bg, back);
2100
2101 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2102 {
2103 buf[0] = ScreenLines[off];
2104 buf[1] = ScreenLines2[off];
2105 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2106 }
2107
2108 /* Draw non-multi-byte character or DBCS character. */
2109 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002110 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112}
2113
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002114#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115/*
2116 * Output the string at the given screen position. This is used in place
2117 * of gui_screenchar() where possible because Pango needs as much context
2118 * as possible to work nicely. It's a lot faster as well.
2119 */
2120 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002121gui_screenstr(
2122 int off, /* Offset from start of screen */
2123 int len, /* string length in screen cells */
2124 int flags,
2125 guicolor_T fg, /* colors for cursor */
2126 guicolor_T bg, /* colors for cursor */
2127 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128{
2129 char_u *buf;
2130 int outlen = 0;
2131 int i;
2132 int retval;
2133
2134 if (len <= 0) /* "cannot happen"? */
2135 return OK;
2136
2137 if (enc_utf8)
2138 {
2139 buf = alloc((unsigned)(len * MB_MAXBYTES + 1));
2140 if (buf == NULL)
2141 return OK; /* not much we could do here... */
2142
2143 for (i = off; i < off + len; ++i)
2144 {
2145 if (ScreenLines[i] == 0)
2146 continue; /* skip second half of double-width char */
2147
2148 if (ScreenLinesUC[i] == 0)
2149 buf[outlen++] = ScreenLines[i];
2150 else
2151 outlen += utfc_char2bytes(i, buf + outlen);
2152 }
2153
2154 buf[outlen] = NUL; /* only to aid debugging */
2155 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2156 vim_free(buf);
2157
2158 return retval;
2159 }
2160 else if (enc_dbcs == DBCS_JPNU)
2161 {
2162 buf = alloc((unsigned)(len * 2 + 1));
2163 if (buf == NULL)
2164 return OK; /* not much we could do here... */
2165
2166 for (i = off; i < off + len; ++i)
2167 {
2168 buf[outlen++] = ScreenLines[i];
2169
2170 /* handle double-byte single-width char */
2171 if (ScreenLines[i] == 0x8e)
2172 buf[outlen++] = ScreenLines2[i];
2173 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2174 buf[outlen++] = ScreenLines[++i];
2175 }
2176
2177 buf[outlen] = NUL; /* only to aid debugging */
2178 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2179 vim_free(buf);
2180
2181 return retval;
2182 }
2183 else
2184 {
2185 return gui_outstr_nowrap(&ScreenLines[off], len,
2186 flags, fg, bg, back);
2187 }
2188}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002189#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
2191/*
2192 * Output the given string at the current cursor position. If the string is
2193 * too long to fit on the line, then it is truncated.
2194 * "flags":
2195 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2196 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002197 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 * background.
2199 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2200 * it.
2201 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2202 * FAIL (the caller should start drawing "back" chars back).
2203 */
2204 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002205gui_outstr_nowrap(
2206 char_u *s,
2207 int len,
2208 int flags,
2209 guicolor_T fg, /* colors for cursor */
2210 guicolor_T bg, /* colors for cursor */
2211 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212{
2213 long_u highlight_mask;
2214 long_u hl_mask_todo;
2215 guicolor_T fg_color;
2216 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002217 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002218#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002220 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221# ifdef FEAT_XFONTSET
2222 GuiFontset fontset = NOFONTSET;
2223# endif
2224#endif
2225 attrentry_T *aep = NULL;
2226 int draw_flags;
2227 int col = gui.col;
2228#ifdef FEAT_SIGN_ICONS
2229 int draw_sign = FALSE;
2230# ifdef FEAT_NETBEANS_INTG
2231 int multi_sign = FALSE;
2232# endif
2233#endif
2234
2235 if (len < 0)
2236 len = (int)STRLEN(s);
2237 if (len == 0)
2238 return OK;
2239
2240#ifdef FEAT_SIGN_ICONS
2241 if (*s == SIGN_BYTE
2242# ifdef FEAT_NETBEANS_INTG
2243 || *s == MULTISIGN_BYTE
2244# endif
2245 )
2246 {
2247# ifdef FEAT_NETBEANS_INTG
2248 if (*s == MULTISIGN_BYTE)
2249 multi_sign = TRUE;
2250# endif
2251 /* draw spaces instead */
2252 s = (char_u *)" ";
2253 if (len == 1 && col > 0)
2254 --col;
2255 len = 2;
2256 draw_sign = TRUE;
2257 highlight_mask = 0;
2258 }
2259 else
2260#endif
2261 if (gui.highlight_mask > HL_ALL)
2262 {
2263 aep = syn_gui_attr2entry(gui.highlight_mask);
2264 if (aep == NULL) /* highlighting not set */
2265 highlight_mask = 0;
2266 else
2267 highlight_mask = aep->ae_attr;
2268 }
2269 else
2270 highlight_mask = gui.highlight_mask;
2271 hl_mask_todo = highlight_mask;
2272
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002273#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 /* Set the font */
2275 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2276 font = aep->ae_u.gui.font;
2277# ifdef FEAT_XFONTSET
2278 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2279 fontset = aep->ae_u.gui.fontset;
2280# endif
2281 else
2282 {
2283# ifdef FEAT_XFONTSET
2284 if (gui.fontset != NOFONTSET)
2285 fontset = gui.fontset;
2286 else
2287# endif
2288 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2289 {
2290 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2291 {
2292 font = gui.boldital_font;
2293 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2294 }
2295 else if (gui.bold_font != NOFONT)
2296 {
2297 font = gui.bold_font;
2298 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2299 }
2300 else
2301 font = gui.norm_font;
2302 }
2303 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2304 {
2305 font = gui.ital_font;
2306 hl_mask_todo &= ~HL_ITALIC;
2307 }
2308 else
2309 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002310
Bram Moolenaardb250522013-06-17 22:43:25 +02002311 /*
2312 * Choose correct wide_font by font. wide_font should be set with font
2313 * at same time in above block. But it will make many "ifdef" nasty
2314 * blocks. So we do it here.
2315 */
2316 if (font == gui.boldital_font && gui.wide_boldital_font)
2317 wide_font = gui.wide_boldital_font;
2318 else if (font == gui.bold_font && gui.wide_bold_font)
2319 wide_font = gui.wide_bold_font;
2320 else if (font == gui.ital_font && gui.wide_ital_font)
2321 wide_font = gui.wide_ital_font;
2322 else if (font == gui.norm_font && gui.wide_font)
2323 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325# ifdef FEAT_XFONTSET
2326 if (fontset != NOFONTSET)
2327 gui_mch_set_fontset(fontset);
2328 else
2329# endif
2330 gui_mch_set_font(font);
2331#endif
2332
2333 draw_flags = 0;
2334
2335 /* Set the color */
2336 bg_color = gui.back_pixel;
2337 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2338 {
2339 draw_flags |= DRAW_CURSOR;
2340 fg_color = fg;
2341 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002342 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
2344 else if (aep != NULL)
2345 {
2346 fg_color = aep->ae_u.gui.fg_color;
2347 if (fg_color == INVALCOLOR)
2348 fg_color = gui.norm_pixel;
2349 bg_color = aep->ae_u.gui.bg_color;
2350 if (bg_color == INVALCOLOR)
2351 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002352 sp_color = aep->ae_u.gui.sp_color;
2353 if (sp_color == INVALCOLOR)
2354 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002359 sp_color = fg_color;
2360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2363 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002364#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 gui_mch_set_colors(bg_color, fg_color);
2366#else
2367 gui_mch_set_fg_color(bg_color);
2368 gui_mch_set_bg_color(fg_color);
2369#endif
2370 }
2371 else
2372 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002373#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 gui_mch_set_colors(fg_color, bg_color);
2375#else
2376 gui_mch_set_fg_color(fg_color);
2377 gui_mch_set_bg_color(bg_color);
2378#endif
2379 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002380 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
2382 /* Clear the selection if we are about to write over it */
2383 if (!(flags & GUI_MON_NOCLEAR))
2384 clip_may_clear_selection(gui.row, gui.row);
2385
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 /* If there's no bold font, then fake it */
2388 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2389 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
2391 /*
2392 * When drawing bold or italic characters the spill-over from the left
2393 * neighbor may be destroyed. Let the caller backup to start redrawing
2394 * just after a blank.
2395 */
2396 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2397 return FAIL;
2398
Bram Moolenaare60acc12011-05-10 16:41:25 +02002399#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 /* If there's no italic font, then fake it.
2401 * For GTK2, we don't need a different font for italic style. */
2402 if (hl_mask_todo & HL_ITALIC)
2403 draw_flags |= DRAW_ITALIC;
2404
2405 /* Do we underline the text? */
2406 if (hl_mask_todo & HL_UNDERLINE)
2407 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002408
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409#else
2410 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002411 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 draw_flags |= DRAW_UNDERL;
2413#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002414 /* Do we undercurl the text? */
2415 if (hl_mask_todo & HL_UNDERCURL)
2416 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002418 /* Do we strikethrough the text? */
2419 if (hl_mask_todo & HL_STRIKETHROUGH)
2420 draw_flags |= DRAW_STRIKE;
2421
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002422 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 if (flags & GUI_MON_TRS_CURSOR)
2424 draw_flags |= DRAW_TRANSP;
2425
2426 /*
2427 * Draw the text.
2428 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002429#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 /* The value returned is the length in display cells */
2431 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2432#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 if (enc_utf8)
2434 {
2435 int start; /* index of bytes to be drawn */
2436 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002437 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 int cn; /* cellwidth of current char */
2439 int i; /* index of current char */
2440 int c; /* current char value */
2441 int cl; /* byte length of current char */
2442 int comping; /* current char is composing */
2443 int scol = col; /* screen column */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002444 int curr_wide = FALSE; /* use 'guifontwide' */
Bram Moolenaar45933962013-01-23 17:43:57 +01002445 int prev_wide = FALSE;
2446 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002447# ifdef MSWIN
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002448 int sep_comp = FALSE; /* Don't separate composing chars. */
Bram Moolenaar13505972019-01-24 15:04:48 +01002449# else
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002450 int sep_comp = TRUE; /* Separate composing chars. */
Bram Moolenaar13505972019-01-24 15:04:48 +01002451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
2453 /* Break the string at a composing character, it has to be drawn on
2454 * top of the previous character. */
2455 start = 0;
2456 cells = 0;
2457 for (i = 0; i < len; i += cl)
2458 {
2459 c = utf_ptr2char(s + i);
2460 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 comping = utf_iscomposing(c);
2462 if (!comping) /* count cells from non-composing chars */
2463 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002464 if (!comping || sep_comp)
2465 {
2466 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002467# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002468 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002469# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002470 && wide_font != NOFONT)
2471 curr_wide = TRUE;
2472 else
2473 curr_wide = FALSE;
2474 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002475 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 if (cl == 0) /* hit end of string */
2477 len = i + cl; /* len must be wrong "cannot happen" */
2478
Bram Moolenaar45933962013-01-23 17:43:57 +01002479 wide_changed = curr_wide != prev_wide;
2480
2481 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 * a composing character. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002483 if (i + cl >= len || (comping && sep_comp && i > start)
2484 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002485# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002487# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 /* No fontset: At least draw char after wide char at
2489 * right position. */
2490 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002492 )
2493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 )
2495 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002496 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 thislen = i - start;
2498 else
2499 thislen = i - start + cl;
2500 if (thislen > 0)
2501 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002502 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002503 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2505 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002506 if (prev_wide)
2507 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 start += thislen;
2509 }
2510 scol += cells;
2511 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002512 /* Adjust to not draw a character which width is changed
2513 * against with last one. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002514 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002516 scol -= cn;
2517 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519
Bram Moolenaar13505972019-01-24 15:04:48 +01002520# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002521 /* No fontset: draw a space to fill the gap after a wide char
2522 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002524# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002526# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002527 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2529 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 }
2532 /* Draw a composing char on top of the previous char. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002533 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002535# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002536 /* Carbon ATSUI autodraws composing char over previous char */
2537 gui_mch_draw_string(gui.row, scol, s + i, cl,
2538 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002539# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002540 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2541 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 start = i + cl;
2544 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002545 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
2547 /* The stuff below assumes "len" is the length in screen columns. */
2548 len = scol - col;
2549 }
2550 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
2552 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (enc_dbcs == DBCS_JPNU)
2554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 /* Get the length in display cells, this can be different from the
2556 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002557 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002560#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
2562 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2563 gui.col = col + len;
2564
2565 /* May need to invert it when it's part of the selection. */
2566 if (flags & GUI_MON_NOCLEAR)
2567 clip_may_redraw_selection(gui.row, col, len);
2568
2569 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2570 {
2571 /* Invalidate the old physical cursor position if we wrote over it */
2572 if (gui.cursor_row == gui.row
2573 && gui.cursor_col >= col
2574 && gui.cursor_col < col + len)
2575 gui.cursor_is_valid = FALSE;
2576 }
2577
2578#ifdef FEAT_SIGN_ICONS
2579 if (draw_sign)
2580 /* Draw the sign on top of the spaces. */
2581 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002582# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002583 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (multi_sign)
2585 netbeans_draw_multisign_indicator(gui.row);
2586# endif
2587#endif
2588
2589 return OK;
2590}
2591
2592/*
2593 * Un-draw the cursor. Actually this just redraws the character at the given
2594 * position. The character just before it too, for when it was in bold.
2595 */
2596 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002597gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
2599 if (gui.cursor_is_valid)
2600 {
2601#ifdef FEAT_HANGULIN
2602 if (composing_hangul
2603 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002604 {
2605 char_u *comp_buf;
2606 int comp_len;
2607
2608 comp_buf = hangul_composing_buffer_get(&comp_len);
2609 if (comp_buf)
2610 {
2611 (void)gui_outstr_nowrap(comp_buf, comp_len,
2612 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2613 gui.norm_pixel, gui.back_pixel, 0);
2614 vim_free(comp_buf);
2615 }
2616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 else
2618 {
2619#endif
2620 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2621 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2622 && gui.cursor_col > 0)
2623 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2624 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2625#ifdef FEAT_HANGULIN
2626 if (composing_hangul)
2627 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2628 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2629 }
2630#endif
2631 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2632 * here in case it wasn't needed to undraw it. */
2633 gui.cursor_is_valid = FALSE;
2634 }
2635}
2636
2637 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002638gui_redraw(
2639 int x,
2640 int y,
2641 int w,
2642 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
2644 int row1, col1, row2, col2;
2645
2646 row1 = Y_2_ROW(y);
2647 col1 = X_2_COL(x);
2648 row2 = Y_2_ROW(y + h - 1);
2649 col2 = X_2_COL(x + w - 1);
2650
2651 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2652
2653 /*
2654 * We may need to redraw the cursor, but don't take it upon us to change
2655 * its location after a scroll.
2656 * (maybe be more strict even and test col too?)
2657 * These things may be outside the update/clipping region and reality may
2658 * not reflect Vims internal ideas if these operations are clipped away.
2659 */
2660 if (gui.row == gui.cursor_row)
2661 gui_update_cursor(TRUE, TRUE);
2662}
2663
2664/*
2665 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2666 * from col1 to col2 (inclusive).
2667 * Return TRUE when the character before the first drawn character has
2668 * different attributes (may have to be redrawn too).
2669 */
2670 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002671gui_redraw_block(
2672 int row1,
2673 int col1,
2674 int row2,
2675 int col2,
2676 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678 int old_row, old_col;
2679 long_u old_hl_mask;
2680 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002681 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 int idx, len;
2683 int back, nback;
2684 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687 /* Don't try to update when ScreenLines is not valid */
2688 if (!screen_cleared || ScreenLines == NULL)
2689 return retval;
2690
2691 /* Don't try to draw outside the shell! */
2692 /* Check everything, strange values may be caused by a big border width */
2693 col1 = check_col(col1);
2694 col2 = check_col(col2);
2695 row1 = check_row(row1);
2696 row2 = check_row(row2);
2697
2698 /* Remember where our cursor was */
2699 old_row = gui.row;
2700 old_col = gui.col;
2701 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 orig_col1 = col1;
2703 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 for (gui.row = row1; gui.row <= row2; gui.row++)
2706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 /* When only half of a double-wide character is in the block, include
2708 * the other half. */
2709 col1 = orig_col1;
2710 col2 = orig_col2;
2711 off = LineOffset[gui.row];
2712 if (enc_dbcs != 0)
2713 {
2714 if (col1 > 0)
2715 col1 -= dbcs_screen_head_off(ScreenLines + off,
2716 ScreenLines + off + col1);
2717 col2 += dbcs_screen_tail_off(ScreenLines + off,
2718 ScreenLines + off + col2);
2719 }
2720 else if (enc_utf8)
2721 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002722 if (ScreenLines[off + col1] == 0)
2723 {
2724 if (col1 > 0)
2725 --col1;
2726 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002727 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002728 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002729 // Make this IEMSGN when it no longer breaks Travis CI.
2730 vim_snprintf((char *)IObuff, IOSIZE,
2731 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2732 (long)gui.row);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002733 msg((char *)IObuff);
Bram Moolenaar9a853462018-12-07 14:10:37 +01002734 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002735 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002736#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2738 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 gui.col = col1;
2742 off = LineOffset[gui.row] + gui.col;
2743 len = col2 - col1 + 1;
2744
2745 /* Find how many chars back this highlighting starts, or where a space
2746 * is. Needed for when the bold trick is used */
2747 for (back = 0; back < col1; ++back)
2748 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2749 || ScreenLines[off - 1 - back] == ' ')
2750 break;
2751 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2752 && ScreenLines[off - 1] != ' ');
2753
2754 /* Break it up in strings of characters with the same attributes. */
2755 /* Print UTF-8 characters individually. */
2756 while (len > 0)
2757 {
2758 first_attr = ScreenAttrs[off];
2759 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002760#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 if (enc_utf8 && ScreenLinesUC[off] != 0)
2762 {
2763 /* output multi-byte character separately */
2764 nback = gui_screenchar(off, flags,
2765 (guicolor_T)0, (guicolor_T)0, back);
2766 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2767 idx = 2;
2768 else
2769 idx = 1;
2770 }
2771 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2772 {
2773 /* output double-byte, single-width character separately */
2774 nback = gui_screenchar(off, flags,
2775 (guicolor_T)0, (guicolor_T)0, back);
2776 idx = 1;
2777 }
2778 else
2779#endif
2780 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002781#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 for (idx = 0; idx < len; ++idx)
2783 {
2784 if (enc_utf8 && ScreenLines[off + idx] == 0)
2785 continue; /* skip second half of double-width char */
2786 if (ScreenAttrs[off + idx] != first_attr)
2787 break;
2788 }
2789 /* gui_screenstr() takes care of multibyte chars */
2790 nback = gui_screenstr(off, idx, flags,
2791 (guicolor_T)0, (guicolor_T)0, back);
2792#else
2793 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2794 idx++)
2795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 /* Stop at a multi-byte Unicode character. */
2797 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2798 break;
2799 if (enc_dbcs == DBCS_JPNU)
2800 {
2801 /* Stop at a double-byte single-width char. */
2802 if (ScreenLines[off + idx] == 0x8e)
2803 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002804 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 + off + idx) == 2)
2806 ++idx; /* skip second byte of double-byte char */
2807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
2809 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2810 (guicolor_T)0, (guicolor_T)0, back);
2811#endif
2812 }
2813 if (nback == FAIL)
2814 {
2815 /* Must back up to start drawing where a bold or italic word
2816 * starts. */
2817 off -= back;
2818 len += back;
2819 gui.col -= back;
2820 }
2821 else
2822 {
2823 off += idx;
2824 len -= idx;
2825 }
2826 back = 0;
2827 }
2828 }
2829
2830 /* Put the cursor back where it was */
2831 gui.row = old_row;
2832 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002833 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 return retval;
2836}
2837
2838 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002839gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
2841 if (count <= 0)
2842 return;
2843
2844 if (row + count > gui.scroll_region_bot)
2845 /* Scrolled out of region, just blank the lines out */
2846 gui_clear_block(row, gui.scroll_region_left,
2847 gui.scroll_region_bot, gui.scroll_region_right);
2848 else
2849 {
2850 gui_mch_delete_lines(row, count);
2851
2852 /* If the cursor was in the deleted lines it's now gone. If the
2853 * cursor was in the scrolled lines adjust its position. */
2854 if (gui.cursor_row >= row
2855 && gui.cursor_col >= gui.scroll_region_left
2856 && gui.cursor_col <= gui.scroll_region_right)
2857 {
2858 if (gui.cursor_row < row + count)
2859 gui.cursor_is_valid = FALSE;
2860 else if (gui.cursor_row <= gui.scroll_region_bot)
2861 gui.cursor_row -= count;
2862 }
2863 }
2864}
2865
2866 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002867gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868{
2869 if (count <= 0)
2870 return;
2871
2872 if (row + count > gui.scroll_region_bot)
2873 /* Scrolled out of region, just blank the lines out */
2874 gui_clear_block(row, gui.scroll_region_left,
2875 gui.scroll_region_bot, gui.scroll_region_right);
2876 else
2877 {
2878 gui_mch_insert_lines(row, count);
2879
2880 if (gui.cursor_row >= gui.row
2881 && gui.cursor_col >= gui.scroll_region_left
2882 && gui.cursor_col <= gui.scroll_region_right)
2883 {
2884 if (gui.cursor_row <= gui.scroll_region_bot - count)
2885 gui.cursor_row += count;
2886 else if (gui.cursor_row <= gui.scroll_region_bot)
2887 gui.cursor_is_valid = FALSE;
2888 }
2889 }
2890}
2891
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002892#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002893/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002894 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2895 */
2896 static int
2897gui_wait_for_chars_3(
2898 long wtime,
2899 int *interrupted UNUSED,
2900 int ignore_input UNUSED)
2901{
2902 return gui_mch_wait_for_chars(wtime);
2903}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002904#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002905
2906/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002907 * Returns OK if a character was found to be available within the given time,
2908 * or FAIL otherwise.
2909 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002910 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002911gui_wait_for_chars_or_timer(
2912 long wtime,
2913 int *interrupted UNUSED,
2914 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002915{
2916#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002917 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2918 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002919#else
2920 return gui_mch_wait_for_chars(wtime);
2921#endif
2922}
2923
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924/*
2925 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002926 * "wtime" == -1 Wait forever.
2927 * "wtime" == 0 Don't wait.
2928 * "wtime" > 0 Wait wtime milliseconds for a character.
2929 *
2930 * Returns the number of characters read or zero when timed out or interrupted.
2931 * "buf" may be NULL, in which case a non-zero number is returned if characters
2932 * are available.
2933 */
2934 static int
2935gui_wait_for_chars_buf(
2936 char_u *buf,
2937 int maxlen,
2938 long wtime, // don't use "time", MIPS cannot handle it
2939 int tb_change_cnt)
2940{
2941 int retval;
2942
2943#ifdef FEAT_MENU
2944 // If we're going to wait a bit, update the menus and mouse shape for the
2945 // current State.
2946 if (wtime != 0)
2947 gui_update_menus(0);
2948#endif
2949
2950 gui_mch_update();
2951 if (input_available()) // Got char, return immediately
2952 {
2953 if (buf != NULL && !typebuf_changed(tb_change_cnt))
2954 return read_from_input_buf(buf, (long)maxlen);
2955 return 0;
2956 }
2957 if (wtime == 0) // Don't wait for char
2958 return FAIL;
2959
2960 // Before waiting, flush any output to the screen.
2961 gui_mch_flush();
2962
2963 // Blink while waiting for a character.
2964 gui_mch_start_blink();
2965
2966 // Common function to loop until "wtime" is met, while handling timers and
2967 // other callbacks.
2968 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
2969 gui_wait_for_chars_or_timer, NULL);
2970
2971 gui_mch_stop_blink(TRUE);
2972
2973 return retval;
2974}
2975
2976/*
2977 * Wait for a character from the keyboard without actually reading it.
2978 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 * wtime == -1 Wait forever.
2980 * wtime == 0 Don't wait.
2981 * wtime > 0 Wait wtime milliseconds for a character.
2982 * Returns OK if a character was found to be available within the given time,
2983 * or FAIL otherwise.
2984 */
2985 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002986gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002988 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989}
2990
2991/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002992 * Equivalent of mch_inchar() for the GUI.
2993 */
2994 int
2995gui_inchar(
2996 char_u *buf,
2997 int maxlen,
2998 long wtime, /* milli seconds */
2999 int tb_change_cnt)
3000{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003001 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003002}
3003
3004/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003005 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 */
3007 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003008fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
3010 p[0] = (char_u)(col / 128 + ' ' + 1);
3011 p[1] = (char_u)(col % 128 + ' ' + 1);
3012 p[2] = (char_u)(row / 128 + ' ' + 1);
3013 p[3] = (char_u)(row % 128 + ' ' + 1);
3014}
3015
3016/*
3017 * Generic mouse support function. Add a mouse event to the input buffer with
3018 * the given properties.
3019 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3020 * MOUSE_X1, MOUSE_X2
3021 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003022 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3023 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 * x, y --- Coordinates of mouse in pixels.
3025 * repeated_click --- TRUE if this click comes only a short time after a
3026 * previous click.
3027 * modifiers --- Bit field which may be any of the following modifiers
3028 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3029 * This function will ignore drag events where the mouse has not moved to a new
3030 * character.
3031 */
3032 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003033gui_send_mouse_event(
3034 int button,
3035 int x,
3036 int y,
3037 int repeated_click,
3038 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
3040 static int prev_row = 0, prev_col = 0;
3041 static int prev_button = -1;
3042 static int num_clicks = 1;
3043 char_u string[10];
3044 enum key_extra button_char;
3045 int row, col;
3046#ifdef FEAT_CLIPBOARD
3047 int checkfor;
3048 int did_clip = FALSE;
3049#endif
3050
3051 /*
3052 * Scrolling may happen at any time, also while a selection is present.
3053 */
3054 switch (button)
3055 {
3056 case MOUSE_X1:
3057 button_char = KE_X1MOUSE;
3058 goto button_set;
3059 case MOUSE_X2:
3060 button_char = KE_X2MOUSE;
3061 goto button_set;
3062 case MOUSE_4:
3063 button_char = KE_MOUSEDOWN;
3064 goto button_set;
3065 case MOUSE_5:
3066 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003067 goto button_set;
3068 case MOUSE_6:
3069 button_char = KE_MOUSELEFT;
3070 goto button_set;
3071 case MOUSE_7:
3072 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073button_set:
3074 {
3075 /* Don't put events in the input queue now. */
3076 if (hold_gui_events)
3077 return;
3078
3079 string[3] = CSI;
3080 string[4] = KS_EXTRA;
3081 string[5] = (int)button_char;
3082
3083 /* Pass the pointer coordinates of the scroll event so that we
3084 * know which window to scroll. */
3085 row = gui_xy2colrow(x, y, &col);
3086 string[6] = (char_u)(col / 128 + ' ' + 1);
3087 string[7] = (char_u)(col % 128 + ' ' + 1);
3088 string[8] = (char_u)(row / 128 + ' ' + 1);
3089 string[9] = (char_u)(row % 128 + ' ' + 1);
3090
3091 if (modifiers == 0)
3092 add_to_input_buf(string + 3, 7);
3093 else
3094 {
3095 string[0] = CSI;
3096 string[1] = KS_MODIFIER;
3097 string[2] = 0;
3098 if (modifiers & MOUSE_SHIFT)
3099 string[2] |= MOD_MASK_SHIFT;
3100 if (modifiers & MOUSE_CTRL)
3101 string[2] |= MOD_MASK_CTRL;
3102 if (modifiers & MOUSE_ALT)
3103 string[2] |= MOD_MASK_ALT;
3104 add_to_input_buf(string, 10);
3105 }
3106 return;
3107 }
3108 }
3109
3110#ifdef FEAT_CLIPBOARD
3111 /* If a clipboard selection is in progress, handle it */
3112 if (clip_star.state == SELECT_IN_PROGRESS)
3113 {
3114 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3115 return;
3116 }
3117
3118 /* Determine which mouse settings to look for based on the current mode */
3119 switch (get_real_state())
3120 {
3121 case NORMAL_BUSY:
3122 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003123# ifdef FEAT_TERMINAL
3124 case TERMINAL:
3125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 case NORMAL: checkfor = MOUSE_NORMAL; break;
3127 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003128 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 case REPLACE:
3130 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 case VREPLACE:
3132 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 case INSERT:
3134 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3135 case ASKMORE:
3136 case HITRETURN: /* At the more- and hit-enter prompt pass the
3137 mouse event for a click on or below the
3138 message line. */
3139 if (Y_2_ROW(y) >= msg_row)
3140 checkfor = MOUSE_NORMAL;
3141 else
3142 checkfor = MOUSE_RETURN;
3143 break;
3144
3145 /*
3146 * On the command line, use the clipboard selection on all lines
3147 * but the command line. But not when pasting.
3148 */
3149 case CMDLINE:
3150 case CMDLINE+LANGMAP:
3151 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3152 checkfor = MOUSE_NONE;
3153 else
3154 checkfor = MOUSE_COMMAND;
3155 break;
3156
3157 default:
3158 checkfor = MOUSE_NONE;
3159 break;
3160 };
3161
3162 /*
3163 * Allow clipboard selection of text on the command line in "normal"
3164 * modes. Don't do this when dragging the status line, or extending a
3165 * Visual selection.
3166 */
3167 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003168 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 && button != MOUSE_DRAG
3170# ifdef FEAT_MOUSESHAPE
3171 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173# endif
3174 )
3175 checkfor = MOUSE_NONE;
3176
3177 /*
3178 * Use modeless selection when holding CTRL and SHIFT pressed.
3179 */
3180 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3181 checkfor = MOUSE_NONEF;
3182
3183 /*
3184 * In Ex mode, always use modeless selection.
3185 */
3186 if (exmode_active)
3187 checkfor = MOUSE_NONE;
3188
3189 /*
3190 * If the mouse settings say to not use the mouse, use the modeless
3191 * selection. But if Visual is active, assume that only the Visual area
3192 * will be selected.
3193 * Exception: On the command line, both the selection is used and a mouse
3194 * key is send.
3195 */
3196 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 /* Don't do modeless selection in Visual mode. */
3199 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3200 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201
3202 /*
3203 * When 'mousemodel' is "popup", shift-left is translated to right.
3204 * But not when also using Ctrl.
3205 */
3206 if (mouse_model_popup() && button == MOUSE_LEFT
3207 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3208 {
3209 button = MOUSE_RIGHT;
3210 modifiers &= ~ MOUSE_SHIFT;
3211 }
3212
3213 /* If the selection is done, allow the right button to extend it.
3214 * If the selection is cleared, allow the right button to start it
3215 * from the cursor position. */
3216 if (button == MOUSE_RIGHT)
3217 {
3218 if (clip_star.state == SELECT_CLEARED)
3219 {
3220 if (State & CMDLINE)
3221 {
3222 col = msg_col;
3223 row = msg_row;
3224 }
3225 else
3226 {
3227 col = curwin->w_wcol;
3228 row = curwin->w_wrow + W_WINROW(curwin);
3229 }
3230 clip_start_selection(col, row, FALSE);
3231 }
3232 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3233 repeated_click);
3234 did_clip = TRUE;
3235 }
3236 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003237 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
3239 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3240 did_clip = TRUE;
3241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
3243 /* Always allow pasting */
3244 if (button != MOUSE_MIDDLE)
3245 {
3246 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3247 return;
3248 if (checkfor != MOUSE_COMMAND)
3249 button = MOUSE_LEFT;
3250 }
3251 repeated_click = FALSE;
3252 }
3253
3254 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003255 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256#endif
3257
3258 /* Don't put events in the input queue now. */
3259 if (hold_gui_events)
3260 return;
3261
3262 row = gui_xy2colrow(x, y, &col);
3263
3264 /*
3265 * If we are dragging and the mouse hasn't moved far enough to be on a
3266 * different character, then don't send an event to vim.
3267 */
3268 if (button == MOUSE_DRAG)
3269 {
3270 if (row == prev_row && col == prev_col)
3271 return;
3272 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3273 if (y < 0)
3274 row = -1;
3275 }
3276
3277 /*
3278 * If topline has changed (window scrolled) since the last click, reset
3279 * repeated_click, because we don't want starting Visual mode when
3280 * clicking on a different character in the text.
3281 */
3282 if (curwin->w_topline != gui_prev_topline
3283#ifdef FEAT_DIFF
3284 || curwin->w_topfill != gui_prev_topfill
3285#endif
3286 )
3287 repeated_click = FALSE;
3288
3289 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3290 string[1] = KS_MOUSE;
3291 string[2] = KE_FILLER;
3292 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3293 {
3294 if (repeated_click)
3295 {
3296 /*
3297 * Handle multiple clicks. They only count if the mouse is still
3298 * pointing at the same character.
3299 */
3300 if (button != prev_button || row != prev_row || col != prev_col)
3301 num_clicks = 1;
3302 else if (++num_clicks > 4)
3303 num_clicks = 1;
3304 }
3305 else
3306 num_clicks = 1;
3307 prev_button = button;
3308 gui_prev_topline = curwin->w_topline;
3309#ifdef FEAT_DIFF
3310 gui_prev_topfill = curwin->w_topfill;
3311#endif
3312
3313 string[3] = (char_u)(button | 0x20);
3314 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3315 }
3316 else
3317 string[3] = (char_u)button;
3318
3319 string[3] |= modifiers;
3320 fill_mouse_coord(string + 4, col, row);
3321 add_to_input_buf(string, 8);
3322
3323 if (row < 0)
3324 prev_row = 0;
3325 else
3326 prev_row = row;
3327 prev_col = col;
3328
3329 /*
3330 * We need to make sure this is cleared since Athena doesn't tell us when
3331 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3332 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003333#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 gui.dragged_sb = SBAR_NONE;
3335#endif
3336}
3337
3338/*
3339 * Convert x and y coordinate to column and row in text window.
3340 * Corrects for multi-byte character.
3341 * returns column in "*colp" and row as return value;
3342 */
3343 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003344gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 int col = check_col(X_2_COL(x));
3347 int row = check_row(Y_2_ROW(y));
3348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 return row;
3351}
3352
3353#if defined(FEAT_MENU) || defined(PROTO)
3354/*
3355 * Callback function for when a menu entry has been selected.
3356 */
3357 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003358gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003360 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
3362 /* Don't put events in the input queue now. */
3363 if (hold_gui_events)
3364 return;
3365
3366 bytes[0] = CSI;
3367 bytes[1] = KS_MENU;
3368 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003369 add_to_input_buf(bytes, 3);
3370 add_long_to_buf((long_u)menu, bytes);
3371 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372}
3373#endif
3374
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003375static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003376
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377/*
3378 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003379 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 * in p_go.
3381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003383gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385#ifdef FEAT_MENU
3386 static int prev_menu_is_active = -1;
3387#endif
3388#ifdef FEAT_TOOLBAR
3389 static int prev_toolbar = -1;
3390 int using_toolbar = FALSE;
3391#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003392#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003393 int using_tabline;
3394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#ifdef FEAT_FOOTER
3396 static int prev_footer = -1;
3397 int using_footer = FALSE;
3398#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003399#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 static int prev_tearoff = -1;
3401 int using_tearoff = FALSE;
3402#endif
3403
3404 char_u *p;
3405 int i;
3406#ifdef FEAT_MENU
3407 int grey_old, grey_new;
3408 char_u *temp;
3409#endif
3410 win_T *wp;
3411 int need_set_size;
3412 int fix_size;
3413
3414#ifdef FEAT_MENU
3415 if (oldval != NULL && gui.in_use)
3416 {
3417 /*
3418 * Check if the menu's go from grey to non-grey or vise versa.
3419 */
3420 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3421 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3422 if (grey_old != grey_new)
3423 {
3424 temp = p_go;
3425 p_go = oldval;
3426 gui_update_menus(MENU_ALL_MODES);
3427 p_go = temp;
3428 }
3429 }
3430 gui.menu_is_active = FALSE;
3431#endif
3432
3433 for (i = 0; i < 3; i++)
3434 gui.which_scrollbars[i] = FALSE;
3435 for (p = p_go; *p; p++)
3436 switch (*p)
3437 {
3438 case GO_LEFT:
3439 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3440 break;
3441 case GO_RIGHT:
3442 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3443 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 case GO_VLEFT:
3445 if (win_hasvertsplit())
3446 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3447 break;
3448 case GO_VRIGHT:
3449 if (win_hasvertsplit())
3450 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 case GO_BOT:
3453 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3454 break;
3455#ifdef FEAT_MENU
3456 case GO_MENUS:
3457 gui.menu_is_active = TRUE;
3458 break;
3459#endif
3460 case GO_GREY:
3461 /* make menu's have grey items, ignored here */
3462 break;
3463#ifdef FEAT_TOOLBAR
3464 case GO_TOOLBAR:
3465 using_toolbar = TRUE;
3466 break;
3467#endif
3468#ifdef FEAT_FOOTER
3469 case GO_FOOTER:
3470 using_footer = TRUE;
3471 break;
3472#endif
3473 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003474#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 using_tearoff = TRUE;
3476#endif
3477 break;
3478 default:
3479 /* Ignore options that are not supported */
3480 break;
3481 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003482
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (gui.in_use)
3484 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003485 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003487
3488#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003489 /* Update the GUI tab line, it may appear or disappear. This may
3490 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003491 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003492 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003493 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003494 /* We don't want a resize event change "Rows" here, save and
3495 * restore it. Resizing is handled below. */
3496 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003497 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003498 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003499 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003500 if (using_tabline)
3501 fix_size = TRUE;
3502 if (!gui_use_tabline())
3503 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3504 }
3505#endif
3506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 for (i = 0; i < 3; i++)
3508 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003509 /* The scrollbar needs to be updated when it is shown/unshown and
3510 * when switching tab pages. But the size only changes when it's
3511 * shown/unshown. Thus we need two places to remember whether a
3512 * scrollbar is there or not. */
3513 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003514 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003515 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
3517 if (i == SBAR_BOTTOM)
3518 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3519 gui.which_scrollbars[i]);
3520 else
3521 {
3522 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003525 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3526 {
3527 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003528 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003529 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003530 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003531 if (gui.which_scrollbars[i])
3532 fix_size = TRUE;
3533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003535 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003536 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538
3539#ifdef FEAT_MENU
3540 if (gui.menu_is_active != prev_menu_is_active)
3541 {
3542 /* We don't want a resize event change "Rows" here, save and
3543 * restore it. Resizing is handled below. */
3544 i = Rows;
3545 gui_mch_enable_menu(gui.menu_is_active);
3546 Rows = i;
3547 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003548 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 if (gui.menu_is_active)
3550 fix_size = TRUE;
3551 }
3552#endif
3553
3554#ifdef FEAT_TOOLBAR
3555 if (using_toolbar != prev_toolbar)
3556 {
3557 gui_mch_show_toolbar(using_toolbar);
3558 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003559 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (using_toolbar)
3561 fix_size = TRUE;
3562 }
3563#endif
3564#ifdef FEAT_FOOTER
3565 if (using_footer != prev_footer)
3566 {
3567 gui_mch_enable_footer(using_footer);
3568 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003569 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 if (using_footer)
3571 fix_size = TRUE;
3572 }
3573#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003574#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 if (using_tearoff != prev_tearoff)
3576 {
3577 gui_mch_toggle_tearoffs(using_tearoff);
3578 prev_tearoff = using_tearoff;
3579 }
3580#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003581 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003582 {
3583#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003584 long prev_Columns = Columns;
3585 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 /* Adjust the size of the window to make the text area keep the
3588 * same size and to avoid that part of our window is off-screen
3589 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003590 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003591
3592#ifdef FEAT_GUI_GTK
3593 /* GTK has the annoying habit of sending us resize events when
3594 * changing the window size ourselves. This mostly happens when
3595 * waiting for a character to arrive, quite unpredictably, and may
3596 * change Columns and Rows when we don't want it. Wait for a
3597 * character here to avoid this effect.
3598 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003599 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003600 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003601 * Don't change Rows when adding menu/toolbar/tabline.
3602 * Don't change Columns when adding vertical toolbar. */
3603 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003604 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003605 if ((need_set_size & RESIZE_VERT) == 0)
3606 Rows = prev_Rows;
3607 if ((need_set_size & RESIZE_HOR) == 0)
3608 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003609#endif
3610 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003611 /* When the console tabline appears or disappears the window positions
3612 * change. */
3613 if (firstwin->w_winrow != tabline_height())
3614 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616}
3617
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003618#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3619/*
3620 * Return TRUE if the GUI is taking care of the tabline.
3621 * It may still be hidden if 'showtabline' is zero.
3622 */
3623 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003624gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003625{
3626 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3627}
3628
3629/*
3630 * Return TRUE if the GUI is showing the tabline.
3631 * This uses 'showtabline'.
3632 */
3633 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003634gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003635{
3636 if (!gui_use_tabline()
3637 || p_stal == 0
3638 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3639 return FALSE;
3640 return TRUE;
3641}
3642
3643/*
3644 * Update the tabline.
3645 * This may display/undisplay the tabline and update the labels.
3646 */
3647 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003648gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003649{
3650 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003651 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003652
3653 if (!gui.starting && starting == 0)
3654 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003655 /* Updating the tabline uses direct GUI commands, flush
3656 * outstanding instructions first. (esp. clear screen) */
3657 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003658
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003659 if (!showit != !shown)
3660 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003661 if (showit != 0)
3662 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003663
3664 /* When the tabs change from hidden to shown or from shown to
3665 * hidden the size of the text area should remain the same. */
3666 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003667 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003668 }
3669}
3670
3671/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003672 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003673 */
3674 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003675get_tabline_label(
3676 tabpage_T *tp,
3677 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003678{
3679 int modified = FALSE;
3680 char_u buf[40];
3681 int wincount;
3682 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003683 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003684
Bram Moolenaar57657d82006-04-21 22:12:41 +00003685 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003686 opt = (tooltip ? &p_gtt : &p_gtl);
3687 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003688 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003689 int use_sandbox = FALSE;
3690 int save_called_emsg = called_emsg;
3691 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003692 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003693 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3694 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003695
3696 called_emsg = FALSE;
3697
3698 printer_page_num = tabpage_index(tp);
3699# ifdef FEAT_EVAL
3700 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003701 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003702# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003703 /* It's almost as going to the tabpage, but without autocommands. */
3704 curtab->tp_firstwin = firstwin;
3705 curtab->tp_lastwin = lastwin;
3706 curtab->tp_curwin = curwin;
3707 save_curtab = curtab;
3708 curtab = tp;
3709 topframe = curtab->tp_topframe;
3710 firstwin = curtab->tp_firstwin;
3711 lastwin = curtab->tp_lastwin;
3712 curwin = curtab->tp_curwin;
3713 curbuf = curwin->w_buffer;
3714
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003715 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003716 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003717 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003718 STRCPY(NameBuff, res);
3719
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003720 /* Back to the original curtab. */
3721 curtab = save_curtab;
3722 topframe = curtab->tp_topframe;
3723 firstwin = curtab->tp_firstwin;
3724 lastwin = curtab->tp_lastwin;
3725 curwin = curtab->tp_curwin;
3726 curbuf = curwin->w_buffer;
3727
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003728 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003729 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003730 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003731 called_emsg |= save_called_emsg;
3732 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003733
3734 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3735 * use a default label. */
3736 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003737 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003738 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003739 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003740 if (!tooltip)
3741 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003742
3743 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3744 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3745 if (bufIsChanged(wp->w_buffer))
3746 modified = TRUE;
3747 if (modified || wincount > 1)
3748 {
3749 if (wincount > 1)
3750 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3751 else
3752 buf[0] = NUL;
3753 if (modified)
3754 STRCAT(buf, "+");
3755 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003756 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003757 mch_memmove(NameBuff, buf, STRLEN(buf));
3758 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003759 }
3760}
3761
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003762/*
3763 * Send the event for clicking to select tab page "nr".
3764 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003765 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003766 */
3767 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003768send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003769{
3770 char_u string[3];
3771
3772 if (nr == tabpage_index(curtab))
3773 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003774
3775 /* Don't put events in the input queue now. */
3776 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003777# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003778 || cmdwin_type != 0
3779# endif
3780 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003781 {
3782 /* Set it back to the current tab page. */
3783 gui_mch_set_curtab(tabpage_index(curtab));
3784 return FALSE;
3785 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003786
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003787 string[0] = CSI;
3788 string[1] = KS_TABLINE;
3789 string[2] = KE_FILLER;
3790 add_to_input_buf(string, 3);
3791 string[0] = nr;
3792 add_to_input_buf_csi(string, 1);
3793 return TRUE;
3794}
3795
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003796/*
3797 * Send a tabline menu event
3798 */
3799 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003800send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003801{
3802 char_u string[3];
3803
Bram Moolenaar29547192018-12-11 20:39:19 +01003804 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003805 if (hold_gui_events)
3806 return;
3807
Bram Moolenaar29547192018-12-11 20:39:19 +01003808 // Cannot close the last tabpage.
3809 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3810 return;
3811
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003812 string[0] = CSI;
3813 string[1] = KS_TABMENU;
3814 string[2] = KE_FILLER;
3815 add_to_input_buf(string, 3);
3816 string[0] = tabidx;
3817 string[1] = (char_u)(long)event;
3818 add_to_input_buf_csi(string, 2);
3819}
3820
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
3823/*
3824 * Scrollbar stuff:
3825 */
3826
Bram Moolenaare45828b2006-02-15 22:12:56 +00003827/*
3828 * Remove all scrollbars. Used before switching to another tab page.
3829 */
3830 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003831gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003832{
3833 int i;
3834 win_T *wp;
3835
3836 for (i = 0; i < 3; i++)
3837 {
3838 if (i == SBAR_BOTTOM)
3839 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3840 else
3841 {
3842 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003843 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003844 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003845 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003846 }
3847}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003850gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
3852 static int sbar_ident = 0;
3853
3854 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3855 sb->wp = wp;
3856 sb->type = type;
3857 sb->value = 0;
3858#ifdef FEAT_GUI_ATHENA
3859 sb->pixval = 0;
3860#endif
3861 sb->size = 1;
3862 sb->max = 1;
3863 sb->top = 0;
3864 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 sb->status_height = 0;
3867 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3868}
3869
3870/*
3871 * Find the scrollbar with the given index.
3872 */
3873 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003874gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
3876 win_T *wp;
3877
3878 if (gui.bottom_sbar.ident == ident)
3879 return &gui.bottom_sbar;
3880 FOR_ALL_WINDOWS(wp)
3881 {
3882 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3883 return &wp->w_scrollbars[SBAR_LEFT];
3884 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3885 return &wp->w_scrollbars[SBAR_RIGHT];
3886 }
3887 return NULL;
3888}
3889
3890/*
3891 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3892 *
3893 * For Win32, Macintosh and GTK+ 2:
3894 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3895 * you stop dragging the scrollbar. We get here each time the scrollbar is
3896 * dragged another pixel, but as far as the rest of vim goes, it thinks
3897 * we're just hanging in the call to DispatchMessage() in
3898 * process_message(). The DispatchMessage() call that hangs was passed a
3899 * mouse button click event in the scrollbar window. -- webb.
3900 *
3901 * Solution: Do the scrolling right here. But only when allowed.
3902 * Ignore the scrollbars while executing an external command or when there
3903 * are still characters to be processed.
3904 */
3905 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003906gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 int sb_num;
3910#ifdef USE_ON_FLY_SCROLL
3911 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913# ifdef FEAT_DIFF
3914 int old_topfill = curwin->w_topfill;
3915# endif
3916#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003917 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 int byte_count;
3919#endif
3920
3921 if (sb == NULL)
3922 return;
3923
3924 /* Don't put events in the input queue now. */
3925 if (hold_gui_events)
3926 return;
3927
3928#ifdef FEAT_CMDWIN
3929 if (cmdwin_type != 0 && sb->wp != curwin)
3930 return;
3931#endif
3932
3933 if (still_dragging)
3934 {
3935 if (sb->wp == NULL)
3936 gui.dragged_sb = SBAR_BOTTOM;
3937 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3938 gui.dragged_sb = SBAR_LEFT;
3939 else
3940 gui.dragged_sb = SBAR_RIGHT;
3941 gui.dragged_wp = sb->wp;
3942 }
3943 else
3944 {
3945 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003946#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003948 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 gui.dragged_wp = NULL;
3950#endif
3951 }
3952
3953 /* Vertical sbar info is kept in the first sbar (the left one) */
3954 if (sb->wp != NULL)
3955 sb = &sb->wp->w_scrollbars[0];
3956
3957 /*
3958 * Check validity of value
3959 */
3960 if (value < 0)
3961 value = 0;
3962#ifdef SCROLL_PAST_END
3963 else if (value > sb->max)
3964 value = sb->max;
3965#else
3966 if (value > sb->max - sb->size + 1)
3967 value = sb->max - sb->size + 1;
3968#endif
3969
3970 sb->value = value;
3971
3972#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003973 /* When not allowed to do the scrolling right now, return.
3974 * This also checked input_available(), but that causes the first click in
3975 * a scrollbar to be ignored when Vim doesn't have focus. */
3976 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 return;
3978#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003979#ifdef FEAT_INS_EXPAND
3980 /* Disallow scrolling the current window when the completion popup menu is
3981 * visible. */
3982 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3983 return;
3984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
3986#ifdef FEAT_RIGHTLEFT
3987 if (sb->wp == NULL && curwin->w_p_rl)
3988 {
3989 value = sb->max + 1 - sb->size - value;
3990 if (value < 0)
3991 value = 0;
3992 }
3993#endif
3994
3995 if (sb->wp != NULL) /* vertical scrollbar */
3996 {
3997 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
3999 sb_num++;
4000 if (wp == NULL)
4001 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
4003#ifdef USE_ON_FLY_SCROLL
4004 current_scrollbar = sb_num;
4005 scrollbar_value = value;
4006 if (State & NORMAL)
4007 {
4008 gui_do_scroll();
4009 setcursor();
4010 }
4011 else if (State & INSERT)
4012 {
4013 ins_scroll();
4014 setcursor();
4015 }
4016 else if (State & CMDLINE)
4017 {
4018 if (msg_scrolled == 0)
4019 {
4020 gui_do_scroll();
4021 redrawcmdline();
4022 }
4023 }
4024# ifdef FEAT_FOLDING
4025 /* Value may have been changed for closed fold. */
4026 sb->value = sb->wp->w_topline - 1;
4027# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004028
4029 /* When dragging one scrollbar and there is another one at the other
4030 * side move the thumb of that one too. */
4031 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4032 gui_mch_set_scrollbar_thumb(
4033 &sb->wp->w_scrollbars[
4034 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4035 ? SBAR_LEFT : SBAR_RIGHT],
4036 sb->value, sb->size, sb->max);
4037
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038#else
4039 bytes[0] = CSI;
4040 bytes[1] = KS_VER_SCROLLBAR;
4041 bytes[2] = KE_FILLER;
4042 bytes[3] = (char_u)sb_num;
4043 byte_count = 4;
4044#endif
4045 }
4046 else
4047 {
4048#ifdef USE_ON_FLY_SCROLL
4049 scrollbar_value = value;
4050
4051 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004052 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 else if (State & INSERT)
4054 ins_horscroll();
4055 else if (State & CMDLINE)
4056 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004057 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004059 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 redrawcmdline();
4061 }
4062 }
4063 if (old_leftcol != curwin->w_leftcol)
4064 {
4065 updateWindow(curwin); /* update window, status and cmdline */
4066 setcursor();
4067 }
4068#else
4069 bytes[0] = CSI;
4070 bytes[1] = KS_HOR_SCROLLBAR;
4071 bytes[2] = KE_FILLER;
4072 byte_count = 3;
4073#endif
4074 }
4075
4076#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 /*
4078 * synchronize other windows, as necessary according to 'scrollbind'
4079 */
4080 if (curwin->w_p_scb
4081 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4082 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004083# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004085# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 ))))
4087 {
4088 do_check_scrollbind(TRUE);
4089 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004090 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (wp->w_redr_type > 0)
4092 updateWindow(wp);
4093 setcursor();
4094 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004095 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004097 add_to_input_buf(bytes, byte_count);
4098 add_long_to_buf((long_u)value, bytes);
4099 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100#endif
4101}
4102
4103/*
4104 * Scrollbar stuff:
4105 */
4106
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004107/*
4108 * Called when something in the window layout has changed.
4109 */
4110 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004111gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004112{
4113 if (gui.in_use && starting == 0)
4114 {
4115 out_flush();
4116 gui_init_which_components(NULL);
4117 gui_update_scrollbars(TRUE);
4118 }
4119 need_mouse_correct = TRUE;
4120}
4121
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004123gui_update_scrollbars(
4124 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
4126 win_T *wp;
4127 scrollbar_T *sb;
4128 long val, size, max; /* need 32 bits here */
4129 int which_sb;
4130 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 /* Update the horizontal scrollbar */
4134 gui_update_horiz_scrollbar(force);
4135
Bram Moolenaar4f974752019-02-17 17:44:42 +01004136#ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 /* Return straight away if there is neither a left nor right scrollbar.
4138 * On MS-Windows this is required anyway for scrollwheel messages. */
4139 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4140 return;
4141#endif
4142
4143 /*
4144 * Don't want to update a scrollbar while we're dragging it. But if we
4145 * have both a left and right scrollbar, and we drag one of them, we still
4146 * need to update the other one.
4147 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004148 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4149 && gui.which_scrollbars[SBAR_LEFT]
4150 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
4152 /*
4153 * If we have two scrollbars and one of them is being dragged, just
4154 * copy the scrollbar position from the dragged one to the other one.
4155 */
4156 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4157 if (gui.dragged_wp != NULL)
4158 gui_mch_set_scrollbar_thumb(
4159 &gui.dragged_wp->w_scrollbars[which_sb],
4160 gui.dragged_wp->w_scrollbars[0].value,
4161 gui.dragged_wp->w_scrollbars[0].size,
4162 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
4164
4165 /* avoid that moving components around generates events */
4166 ++hold_gui_events;
4167
Bram Moolenaar45a24952016-07-24 22:25:15 +02004168 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
4170 if (wp->w_buffer == NULL) /* just in case */
4171 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004172 /* Skip a scrollbar that is being dragged. */
4173 if (!force && (gui.dragged_sb == SBAR_LEFT
4174 || gui.dragged_sb == SBAR_RIGHT)
4175 && gui.dragged_wp == wp)
4176 continue;
4177
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178#ifdef SCROLL_PAST_END
4179 max = wp->w_buffer->b_ml.ml_line_count - 1;
4180#else
4181 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4182#endif
4183 if (max < 0) /* empty buffer */
4184 max = 0;
4185 val = wp->w_topline - 1;
4186 size = wp->w_height;
4187#ifdef SCROLL_PAST_END
4188 if (val > max) /* just in case */
4189 val = max;
4190#else
4191 if (size > max + 1) /* just in case */
4192 size = max + 1;
4193 if (val > max - size + 1)
4194 val = max - size + 1;
4195#endif
4196 if (val < 0) /* minimal value is 0 */
4197 val = 0;
4198
4199 /*
4200 * Scrollbar at index 0 (the left one) contains all the information.
4201 * It would be the same info for left and right so we just store it for
4202 * one of them.
4203 */
4204 sb = &wp->w_scrollbars[0];
4205
4206 /*
4207 * Note: no check for valid w_botline. If it's not valid the
4208 * scrollbars will be updated later anyway.
4209 */
4210 if (size < 1 || wp->w_botline - 2 > max)
4211 {
4212 /*
4213 * This can happen during changing files. Just don't update the
4214 * scrollbar for now.
4215 */
4216 sb->height = 0; /* Force update next time */
4217 if (gui.which_scrollbars[SBAR_LEFT])
4218 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4219 if (gui.which_scrollbars[SBAR_RIGHT])
4220 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4221 continue;
4222 }
4223 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 || sb->top != wp->w_winrow
4225 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004227 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 /* Height, width or position of scrollbar has changed. For
4230 * vertical split: curwin changed. */
4231 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 sb->top = wp->w_winrow;
4233 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
4236 /* Calculate height and position in pixels */
4237 h = (sb->height + sb->status_height) * gui.char_height;
4238 y = sb->top * gui.char_height + gui.border_offset;
4239#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4240 if (gui.menu_is_active)
4241 y += gui.menu_height;
4242#endif
4243
4244#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4245 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4246# ifdef FEAT_GUI_ATHENA
4247 y += gui.toolbar_height;
4248# else
4249# ifdef FEAT_GUI_MSWIN
4250 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4251# endif
4252# endif
4253#endif
4254
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004255#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4256 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004257 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004258#endif
4259
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 {
4262 /* Height of top scrollbar includes width of top border */
4263 h += gui.border_offset;
4264 y -= gui.border_offset;
4265 }
4266 if (gui.which_scrollbars[SBAR_LEFT])
4267 {
4268 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4269 gui.left_sbar_x, y,
4270 gui.scrollbar_width, h);
4271 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4272 }
4273 if (gui.which_scrollbars[SBAR_RIGHT])
4274 {
4275 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4276 gui.right_sbar_x, y,
4277 gui.scrollbar_width, h);
4278 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4279 }
4280 }
4281
4282 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4283 * checking if the thumb moved at least a pixel. Only do this for
4284 * Athena, most other GUIs require the update anyway to make the
4285 * arrows work. */
4286#ifdef FEAT_GUI_ATHENA
4287 if (max == 0)
4288 y = 0;
4289 else
4290 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4291 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4292#else
4293 if (force || sb->value != val || sb->size != size || sb->max != max)
4294#endif
4295 {
4296 /* Thumb of scrollbar has moved */
4297 sb->value = val;
4298#ifdef FEAT_GUI_ATHENA
4299 sb->pixval = y;
4300#endif
4301 sb->size = size;
4302 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004303 if (gui.which_scrollbars[SBAR_LEFT]
4304 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4306 val, size, max);
4307 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004308 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4310 val, size, max);
4311 }
4312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 --hold_gui_events;
4315}
4316
4317/*
4318 * Enable or disable a scrollbar.
4319 * Check for scrollbars for vertically split windows which are not enabled
4320 * sometimes.
4321 */
4322 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004323gui_do_scrollbar(
4324 win_T *wp,
4325 int which, /* SBAR_LEFT or SBAR_RIGHT */
4326 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 int midcol = curwin->w_wincol + curwin->w_width / 2;
4329 int has_midcol = (wp->w_wincol <= midcol
4330 && wp->w_wincol + wp->w_width >= midcol);
4331
4332 /* Only enable scrollbars that contain the middle column of the current
4333 * window. */
4334 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4335 {
4336 /* Scrollbars only on one side. Don't enable scrollbars that don't
4337 * contain the middle column of the current window. */
4338 if (!has_midcol)
4339 enable = FALSE;
4340 }
4341 else
4342 {
4343 /* Scrollbars on both sides. Don't enable scrollbars that neither
4344 * contain the middle column of the current window nor are on the far
4345 * side. */
4346 if (midcol > Columns / 2)
4347 {
4348 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4349 enable = FALSE;
4350 }
4351 else
4352 {
4353 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4354 : !has_midcol)
4355 enable = FALSE;
4356 }
4357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4359}
4360
4361/*
4362 * Scroll a window according to the values set in the globals current_scrollbar
4363 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4364 * or FALSE otherwise.
4365 */
4366 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004367gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368{
4369 win_T *wp, *save_wp;
4370 int i;
4371 long nlines;
4372 pos_T old_cursor;
4373 linenr_T old_topline;
4374#ifdef FEAT_DIFF
4375 int old_topfill;
4376#endif
4377
4378 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4379 if (wp == NULL)
4380 break;
4381 if (wp == NULL)
4382 /* Couldn't find window */
4383 return FALSE;
4384
4385 /*
4386 * Compute number of lines to scroll. If zero, nothing to do.
4387 */
4388 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4389 if (nlines == 0)
4390 return FALSE;
4391
4392 save_wp = curwin;
4393 old_topline = wp->w_topline;
4394#ifdef FEAT_DIFF
4395 old_topfill = wp->w_topfill;
4396#endif
4397 old_cursor = wp->w_cursor;
4398 curwin = wp;
4399 curbuf = wp->w_buffer;
4400 if (nlines < 0)
4401 scrolldown(-nlines, gui.dragged_wp == NULL);
4402 else
4403 scrollup(nlines, gui.dragged_wp == NULL);
4404 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4405 * the mouse-up event already, but we still want it to behave like when
4406 * dragging. But not the next click in an arrow. */
4407 if (gui.dragged_sb == SBAR_NONE)
4408 gui.dragged_wp = NULL;
4409
4410 if (old_topline != wp->w_topline
4411#ifdef FEAT_DIFF
4412 || old_topfill != wp->w_topfill
4413#endif
4414 )
4415 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004416 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 {
4418 cursor_correct(); /* fix window for 'so' */
4419 update_topline(); /* avoid up/down jump */
4420 }
4421 if (old_cursor.lnum != wp->w_cursor.lnum)
4422 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004426 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4427 validate_cursor();
4428
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 curwin = save_wp;
4430 curbuf = save_wp->w_buffer;
4431
4432 /*
4433 * Don't call updateWindow() when nothing has changed (it will overwrite
4434 * the status line!).
4435 */
4436 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004437 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438#ifdef FEAT_DIFF
4439 || old_topfill != wp->w_topfill
4440#endif
4441 )
4442 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004443 int type = VALID;
4444
4445#ifdef FEAT_INS_EXPAND
4446 if (pum_visible())
4447 {
4448 type = NOT_VALID;
4449 wp->w_lines_valid = 0;
4450 }
4451#endif
4452 /* Don't set must_redraw here, it may cause the popup menu to
4453 * disappear when losing focus after a scrollbar drag. */
4454 if (wp->w_redr_type < type)
4455 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004456 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 updateWindow(wp); /* update window, status line, and cmdline */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004458 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004461#ifdef FEAT_INS_EXPAND
4462 /* May need to redraw the popup menu. */
4463 if (pum_visible())
4464 pum_redraw();
4465#endif
4466
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004467 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468}
4469
4470
4471/*
4472 * Horizontal scrollbar stuff:
4473 */
4474
4475/*
4476 * Return length of line "lnum" for horizontal scrolling.
4477 */
4478 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004479scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480{
4481 char_u *p;
4482 colnr_T col;
4483 int w;
4484
4485 p = ml_get(lnum);
4486 col = 0;
4487 if (*p != NUL)
4488 for (;;)
4489 {
4490 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004491 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if (*p == NUL) /* don't count the last character */
4493 break;
4494 col += w;
4495 }
4496 return col;
4497}
4498
4499/* Remember which line is currently the longest, so that we don't have to
4500 * search for it when scrolling horizontally. */
4501static linenr_T longest_lnum = 0;
4502
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004503/*
4504 * Find longest visible line number. If this is not possible (or not desired,
4505 * by setting 'h' in "guioptions") then the current line number is returned.
4506 */
4507 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004508gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004509{
4510 linenr_T ret = 0;
4511
4512 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4513 * line numbers, topline and botline can be invalid when displaying is
4514 * postponed. */
4515 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4516 && curwin->w_topline <= curwin->w_cursor.lnum
4517 && curwin->w_botline > curwin->w_cursor.lnum
4518 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4519 {
4520 linenr_T lnum;
4521 colnr_T n;
4522 long max = 0;
4523
4524 /* Use maximum of all visible lines. Remember the lnum of the
4525 * longest line, closest to the cursor line. Used when scrolling
4526 * below. */
4527 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4528 {
4529 n = scroll_line_len(lnum);
4530 if (n > (colnr_T)max)
4531 {
4532 max = n;
4533 ret = lnum;
4534 }
4535 else if (n == (colnr_T)max
4536 && abs((int)(lnum - curwin->w_cursor.lnum))
4537 < abs((int)(ret - curwin->w_cursor.lnum)))
4538 ret = lnum;
4539 }
4540 }
4541 else
4542 /* Use cursor line only. */
4543 ret = curwin->w_cursor.lnum;
4544
4545 return ret;
4546}
4547
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004549gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550{
4551 long value, size, max; /* need 32 bit ints here */
4552
4553 if (!gui.which_scrollbars[SBAR_BOTTOM])
4554 return;
4555
4556 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4557 return;
4558
4559 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4560 return;
4561
4562 /*
4563 * It is possible for the cursor to be invalid if we're in the middle of
4564 * something (like changing files). If so, don't do anything for now.
4565 */
4566 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4567 {
4568 gui.bottom_sbar.value = -1;
4569 return;
4570 }
4571
Bram Moolenaar02631462017-09-22 15:20:32 +02004572 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (curwin->w_p_wrap)
4574 {
4575 value = 0;
4576#ifdef SCROLL_PAST_END
4577 max = 0;
4578#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004579 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580#endif
4581 }
4582 else
4583 {
4584 value = curwin->w_leftcol;
4585
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004586 longest_lnum = gui_find_longest_lnum();
4587 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (virtual_active())
4590 {
4591 /* May move the cursor even further to the right. */
4592 if (curwin->w_virtcol >= (colnr_T)max)
4593 max = curwin->w_virtcol;
4594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595
4596#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004597 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598#endif
4599 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004600 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 size -= curwin_col_off();
4602#ifndef SCROLL_PAST_END
4603 max -= curwin_col_off();
4604#endif
4605 }
4606
4607#ifndef SCROLL_PAST_END
4608 if (value > max - size + 1)
4609 value = max - size + 1; /* limit the value to allowable range */
4610#endif
4611
4612#ifdef FEAT_RIGHTLEFT
4613 if (curwin->w_p_rl)
4614 {
4615 value = max + 1 - size - value;
4616 if (value < 0)
4617 {
4618 size += value;
4619 value = 0;
4620 }
4621 }
4622#endif
4623 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4624 && max == gui.bottom_sbar.max)
4625 return;
4626
4627 gui.bottom_sbar.value = value;
4628 gui.bottom_sbar.size = size;
4629 gui.bottom_sbar.max = max;
4630 gui.prev_wrap = curwin->w_p_wrap;
4631
4632 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4633}
4634
4635/*
4636 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4637 */
4638 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004639gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 /* no wrapping, no scrolling */
4642 if (curwin->w_p_wrap)
4643 return FALSE;
4644
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004645 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 return FALSE;
4647
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004648 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
4650 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004651 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004653 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004654 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004656 if (compute_longest_lnum)
4657 {
4658 curwin->w_cursor.lnum = gui_find_longest_lnum();
4659 curwin->w_cursor.col = 0;
4660 }
4661 /* Do a sanity check on "longest_lnum", just in case. */
4662 else if (longest_lnum >= curwin->w_topline
4663 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
4665 curwin->w_cursor.lnum = longest_lnum;
4666 curwin->w_cursor.col = 0;
4667 }
4668 }
4669
4670 return leftcol_changed();
4671}
4672
4673/*
4674 * Check that none of the colors are the same as the background color
4675 */
4676 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004677gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
4679 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4680 {
4681 gui_set_bg_color((char_u *)"White");
4682 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4683 gui_set_fg_color((char_u *)"Black");
4684 }
4685}
4686
Bram Moolenaar3918c952005-03-15 22:34:55 +00004687 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004688gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
4690 gui.norm_pixel = gui_get_color(name);
4691 hl_set_fg_color_name(vim_strsave(name));
4692}
4693
Bram Moolenaar3918c952005-03-15 22:34:55 +00004694 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004695gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696{
4697 gui.back_pixel = gui_get_color(name);
4698 hl_set_bg_color_name(vim_strsave(name));
4699}
4700
4701/*
4702 * Allocate a color by name.
4703 * Returns INVALCOLOR and gives an error message when failed.
4704 */
4705 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004706gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707{
4708 guicolor_T t;
4709
4710 if (*name == NUL)
4711 return INVALCOLOR;
4712 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004713
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004715#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 && gui.in_use
4717#endif
4718 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004719 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 return t;
4721}
4722
4723/*
4724 * Return the grey value of a color (range 0-255).
4725 */
4726 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004727gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004729 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004731 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004732 + (((rgb >> 8) & 0xff) * 587)
4733 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734}
4735
4736#if defined(FEAT_GUI_X11) || defined(PROTO)
4737 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004738gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739{
4740 win_T *wp;
4741
4742 /* Nothing to do if GUI hasn't started yet. */
4743 if (!gui.in_use)
4744 return;
4745
4746 FOR_ALL_WINDOWS(wp)
4747 {
4748 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4749 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4750 }
4751 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4752}
4753#endif
4754
4755/*
4756 * Call this when focus has changed.
4757 */
4758 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004759gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
4761/*
4762 * Skip this code to avoid drawing the cursor when debugging and switching
4763 * between the debugger window and gvim.
4764 */
4765#if 1
4766 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004767 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768
4769# ifdef FEAT_XIM
4770 xim_set_focus(in_focus);
4771# endif
4772
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004773 /* Put events in the input queue only when allowed.
4774 * ui_focus_change() isn't called directly, because it invokes
4775 * autocommands and that must not happen asynchronously. */
4776 if (!hold_gui_events)
4777 {
4778 char_u bytes[3];
4779
4780 bytes[0] = CSI;
4781 bytes[1] = KS_EXTRA;
4782 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4783 add_to_input_buf(bytes, 3);
4784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785#endif
4786}
4787
4788/*
4789 * Called when the mouse moved (but not when dragging).
4790 */
4791 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004792gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
4794 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004795 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
Bram Moolenaard3667a22006-03-16 21:35:52 +00004797 /* Ignore this while still starting up. */
4798 if (!gui.in_use || gui.starting)
4799 return;
4800
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801#ifdef FEAT_MOUSESHAPE
4802 /* Get window pointer, and update mouse shape as well. */
4803 wp = xy2win(x, y);
4804#endif
4805
4806 /* Only handle this when 'mousefocus' set and ... */
4807 if (p_mousef
4808 && !hold_gui_events /* not holding events */
4809 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4810 && State != HITRETURN /* but not hit-return prompt */
4811 && msg_scrolled == 0 /* no scrolled message */
4812 && !need_mouse_correct /* not moving the pointer */
4813 && gui.in_focus) /* gvim in focus */
4814 {
4815 /* Don't move the mouse when it's left or right of the Vim window */
4816 if (x < 0 || x > Columns * gui.char_width)
4817 return;
4818#ifndef FEAT_MOUSESHAPE
4819 wp = xy2win(x, y);
4820#endif
4821 if (wp == curwin || wp == NULL)
4822 return; /* still in the same old window, or none at all */
4823
Bram Moolenaar9c102382006-05-03 21:26:49 +00004824 /* Ignore position in the tab pages line. */
4825 if (Y_2_ROW(y) < tabline_height())
4826 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004827
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 /*
4829 * format a mouse click on status line input
4830 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004831 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4832 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 */
4834 if (finish_op)
4835 {
4836 /* abort the current operator first */
4837 st[0] = ESC;
4838 add_to_input_buf(st, 1);
4839 }
4840 st[0] = CSI;
4841 st[1] = KS_MOUSE;
4842 st[2] = KE_FILLER;
4843 st[3] = (char_u)MOUSE_LEFT;
4844 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004845 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 wp->w_height + W_WINROW(wp));
4847
4848 add_to_input_buf(st, 8);
4849 st[3] = (char_u)MOUSE_RELEASE;
4850 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851#ifdef FEAT_GUI_GTK
4852 /* Need to wake up the main loop */
4853 if (gtk_main_level() > 0)
4854 gtk_main_quit();
4855#endif
4856 }
4857}
4858
4859/*
4860 * Called when mouse should be moved to window with focus.
4861 */
4862 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004863gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
4865 int x, y;
4866 win_T *wp = NULL;
4867
4868 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004869
4870 if (!(gui.in_use && p_mousef))
4871 return;
4872
4873 gui_mch_getmouse(&x, &y);
4874 /* Don't move the mouse when it's left or right of the Vim window */
4875 if (x < 0 || x > Columns * gui.char_width)
4876 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004877 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004878 wp = xy2win(x, y);
4879 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004881 validate_cline_row();
4882 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4883 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
4886}
4887
4888/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004889 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004890 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 static win_T *
Bram Moolenaar4f974752019-02-17 17:44:42 +01004893xy2win(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 int row;
4896 int col;
4897 win_T *wp;
4898
4899 row = Y_2_ROW(y);
4900 col = X_2_COL(x);
4901 if (row < 0 || col < 0) /* before first window */
4902 return NULL;
4903 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004904 if (wp == NULL)
4905 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004906#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 if (State == HITRETURN || State == ASKMORE)
4908 {
4909 if (Y_2_ROW(y) >= msg_row)
4910 update_mouseshape(SHAPE_IDX_MOREL);
4911 else
4912 update_mouseshape(SHAPE_IDX_MORE);
4913 }
4914 else if (row > wp->w_height) /* below status line */
4915 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004916 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004917 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004919 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004920 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 update_mouseshape(SHAPE_IDX_STATUS);
4922 else
4923 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004925 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926}
4927
4928/*
4929 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4930 * File names may be given to redefine the args list.
4931 */
4932 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004933ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934{
4935 char_u *arg = eap->arg;
4936
4937 /*
4938 * Check for "-f" argument: foreground, don't fork.
4939 * Also don't fork when started with "gvim -f".
4940 * Do fork when using "gui -b".
4941 */
4942 if (arg[0] == '-'
4943 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004944 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
4946 gui.dofork = (arg[1] == 'b');
4947 eap->arg = skipwhite(eap->arg + 2);
4948 }
4949 if (!gui.in_use)
4950 {
4951 /* Clear the command. Needed for when forking+exiting, to avoid part
4952 * of the argument ending up after the shell prompt. */
4953 msg_clr_eos_force();
4954 gui_start();
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004955#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004956 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
4959 if (!ends_excmd(*eap->arg))
4960 ex_next(eap);
4961}
4962
Bram Moolenaar4f974752019-02-17 17:44:42 +01004963#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
4964 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)) \
4965 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966/*
4967 * This is shared between Athena, Motif and GTK.
4968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970/*
4971 * Callback function for do_in_runtimepath().
4972 */
4973 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004974gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004976 char_u *gfp_buffer = cookie;
4977
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 if (STRLEN(fname) >= MAXPATHL)
4979 *gfp_buffer = NUL;
4980 else
4981 STRCPY(gfp_buffer, fname);
4982}
4983
4984/*
4985 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4986 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4987 */
4988 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004989gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
4991 if (STRLEN(name) > MAXPATHL - 14)
4992 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004993 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01004994 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004995 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 return FAIL;
4997 return OK;
4998}
4999
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005000# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001/*
5002 * Given the name of the "icon=" argument, try finding the bitmap file for the
5003 * icon. If it is an absolute path name, use it as it is. Otherwise append
5004 * "ext" and search for it in 'runtimepath'.
5005 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5006 * contains "name".
5007 */
5008 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005009gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010{
5011 char_u buf[MAXPATHL + 1];
5012
5013 expand_env(name, buffer, MAXPATHL);
5014 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5015 STRCPY(buffer, buf);
5016}
5017# endif
5018#endif
5019
Bram Moolenaar9372a112005-12-06 19:59:18 +00005020#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005022display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
5024 char_u *p;
5025
5026 if (isatty(2))
5027 fflush(stderr);
5028 else if (error_ga.ga_data != NULL)
5029 {
5030 /* avoid putting up a message box with blanks only */
5031 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5032 if (!isspace(*p))
5033 {
5034 /* Truncate a very long message, it will go off-screen. */
5035 if (STRLEN(p) > 2000)
5036 STRCPY(p + 2000 - 14, "...(truncated)");
5037 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005038 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 break;
5040 }
5041 ga_clear(&error_ga);
5042 }
5043}
5044#endif
5045
5046#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5047/*
5048 * Return TRUE if still starting up and there is no place to enter text.
5049 * For GTK and X11 we check if stderr is not a tty, which means we were
5050 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5051 * allow typing on stdin.
5052 */
5053 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005054no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055{
5056 return ((!gui.in_use || gui.starting)
5057# ifndef NO_CONSOLE
5058 && !isatty(0) && !isatty(2)
5059# endif
5060 );
5061}
5062#endif
5063
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005064#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005065 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005066 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067/*
5068 * Update the current window and the screen.
5069 */
5070 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005071gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005073# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005074 linenr_T conceal_old_cursor_line = 0;
5075 linenr_T conceal_new_cursor_line = 0;
5076 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005077# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005078
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 update_topline();
5080 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005081
Bram Moolenaarec80df72008-05-07 19:46:51 +00005082 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005083 if (!finish_op && (has_cursormoved()
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005084# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005085 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005086# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005087 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005088 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005089 if (has_cursormoved())
5090 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005091# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005092 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005093 {
5094 conceal_old_cursor_line = last_cursormoved.lnum;
5095 conceal_new_cursor_line = curwin->w_cursor.lnum;
5096 conceal_update_lines = TRUE;
5097 }
5098# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005099 last_cursormoved = curwin->w_cursor;
5100 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005101
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005102# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005103 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005104 && (conceal_old_cursor_line != conceal_new_cursor_line
5105 || conceal_cursor_line(curwin)
5106 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005107 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005108 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005109 redrawWinline(curwin, conceal_old_cursor_line);
5110 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005111 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005112 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005113 }
5114# endif
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005115 update_screen(0); /* may need to update the screen */
5116 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005117 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118}
5119#endif
5120
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005121#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122/*
5123 * Get the text to use in a find/replace dialog. Uses the last search pattern
5124 * if the argument is empty.
5125 * Returns an allocated string.
5126 */
5127 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005128get_find_dialog_text(
5129 char_u *arg,
5130 int *wwordp, /* return: TRUE if \< \> found */
5131 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
5133 char_u *text;
5134
5135 if (*arg == NUL)
5136 text = last_search_pat();
5137 else
5138 text = arg;
5139 if (text != NULL)
5140 {
5141 text = vim_strsave(text);
5142 if (text != NULL)
5143 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005144 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 int i;
5146
5147 /* Remove "\V" */
5148 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5149 {
5150 mch_memmove(text, text + 2, (size_t)(len - 1));
5151 len -= 2;
5152 }
5153
5154 /* Recognize "\c" and "\C" and remove. */
5155 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5156 {
5157 *mcasep = (text[1] == 'C');
5158 mch_memmove(text, text + 2, (size_t)(len - 1));
5159 len -= 2;
5160 }
5161
5162 /* Recognize "\<text\>" and remove. */
5163 if (len >= 4
5164 && STRNCMP(text, "\\<", 2) == 0
5165 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5166 {
5167 *wwordp = TRUE;
5168 mch_memmove(text, text + 2, (size_t)(len - 4));
5169 text[len - 4] = NUL;
5170 }
5171
5172 /* Recognize "\/" or "\?" and remove. */
5173 for (i = 0; i + 1 < len; ++i)
5174 if (text[i] == '\\' && (text[i + 1] == '/'
5175 || text[i + 1] == '?'))
5176 {
5177 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5178 --len;
5179 }
5180 }
5181 }
5182 return text;
5183}
5184
5185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 * Handle the press of a button in the find-replace dialog.
5187 * Return TRUE when something was added to the input buffer.
5188 */
5189 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005190gui_do_findrepl(
5191 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5192 char_u *find_text,
5193 char_u *repl_text,
5194 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195{
5196 garray_T ga;
5197 int i;
5198 int type = (flags & FRD_TYPE_MASK);
5199 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005200 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005201 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005202 static int busy = FALSE;
5203
5204 /* When the screen is being updated we should not change buffers and
5205 * windows structures, it may cause freed memory to be used. Also don't
5206 * do this recursively (pressing "Find" quickly several times. */
5207 if (updating_screen || busy)
5208 return FALSE;
5209
5210 /* refuse replace when text cannot be changed */
5211 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5212 return FALSE;
5213
5214 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215
5216 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005217 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 ga_concat(&ga, (char_u *)"%s/");
5219
5220 ga_concat(&ga, (char_u *)"\\V");
5221 if (flags & FRD_MATCH_CASE)
5222 ga_concat(&ga, (char_u *)"\\C");
5223 else
5224 ga_concat(&ga, (char_u *)"\\c");
5225 if (flags & FRD_WHOLE_WORD)
5226 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar518bc172018-05-13 17:05:30 +02005227 /* escape / and \ */
5228 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5229 if (p != NULL)
5230 ga_concat(&ga, p);
5231 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 if (flags & FRD_WHOLE_WORD)
5233 ga_concat(&ga, (char_u *)"\\>");
5234
5235 if (type == FRD_REPLACEALL)
5236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005238 /* escape / and \ */
5239 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5240 if (p != NULL)
5241 ga_concat(&ga, p);
5242 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005244 }
5245 ga_append(&ga, NUL);
5246
5247 if (type == FRD_REPLACE)
5248 {
5249 /* Do the replacement when the text at the cursor matches. Thus no
5250 * replacement is done if the cursor was moved! */
5251 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5252 regmatch.rm_ic = 0;
5253 if (regmatch.regprog != NULL)
5254 {
5255 p = ml_get_cursor();
5256 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5257 && regmatch.startp[0] == p)
5258 {
5259 /* Clear the command line to remove any old "No match"
5260 * error. */
5261 msg_end_prompt();
5262
5263 if (u_save_cursor() == OK)
5264 {
5265 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005266 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005267
5268 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005269 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005270 ins_str(repl_text);
5271 }
5272 }
5273 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005274 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005275 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005276 }
5277 }
5278
5279 if (type == FRD_REPLACEALL)
5280 {
5281 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005282 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 do_cmdline_cmd(ga.ga_data);
5284 }
5285 else
5286 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005287 int searchflags = SEARCH_MSG + SEARCH_MARK;
5288
5289 /* Search for the next match.
5290 * Don't skip text under cursor for single replace. */
5291 if (type == FRD_REPLACE)
5292 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005294 if (down)
5295 {
5296 (void)do_search(NULL, '/', ga.ga_data, 1L, searchflags, NULL, NULL);
5297 }
5298 else
5299 {
5300 /* We need to escape '?' if and only if we are searching in the up
5301 * direction */
5302 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5303 if (p != NULL)
5304 (void)do_search(NULL, '?', p, 1L, searchflags, NULL, NULL);
5305 vim_free(p);
5306 }
5307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 msg_scroll = i; /* don't let an error message set msg_scroll */
5309 }
5310
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005311 /* Don't want to pass did_emsg to other code, it may cause disabling
5312 * syntax HL if we were busy redrawing. */
5313 did_emsg = save_did_emsg;
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 if (State & (NORMAL | INSERT))
5316 {
5317 gui_update_screen(); /* update the screen */
5318 msg_didout = 0; /* overwrite any message */
5319 need_wait_return = FALSE; /* don't wait for return */
5320 }
5321
5322 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005323 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 return (ga.ga_len > 0);
5325}
5326
5327#endif
5328
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005329#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330/*
5331 * Jump to the window at specified point (x, y).
5332 */
5333 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005334gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
5336 int row = Y_2_ROW(y);
5337 int col = X_2_COL(x);
5338 win_T *wp;
5339
5340 if (row >= 0 && col >= 0)
5341 {
5342 wp = mouse_find_win(&row, &col);
5343 if (wp != NULL && wp != curwin)
5344 win_goto(wp);
5345 }
5346}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347
5348/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005349 * Function passed to handle_drop() for the actions to be done after the
5350 * argument list has been updated.
5351 */
5352 static void
5353drop_callback(void *cookie)
5354{
5355 char_u *p = cookie;
5356
5357 /* If Shift held down, change to first file's directory. If the first
5358 * item is a directory, change to that directory (and let the explorer
5359 * plugin show the contents). */
5360 if (p != NULL)
5361 {
5362 if (mch_isdir(p))
5363 {
5364 if (mch_chdir((char *)p) == 0)
5365 shorten_fnames(TRUE);
5366 }
5367 else if (vim_chdirfile(p, "drop") == OK)
5368 shorten_fnames(TRUE);
5369 vim_free(p);
5370 }
5371
5372 /* Update the screen display */
5373 update_screen(NOT_VALID);
5374# ifdef FEAT_MENU
5375 gui_update_menus(0);
5376# endif
5377#ifdef FEAT_TITLE
5378 maketitle();
5379#endif
5380 setcursor();
5381 out_flush_cursor(FALSE, FALSE);
5382}
5383
5384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 * Process file drop. Mouse cursor position, key modifiers, name of files
5386 * and count of files are given. Argument "fnames[count]" has full pathnames
5387 * of dropped files, they will be freed in this function, and caller can't use
5388 * fnames after call this function.
5389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005391gui_handle_drop(
5392 int x UNUSED,
5393 int y UNUSED,
5394 int_u modifiers,
5395 char_u **fnames,
5396 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398 int i;
5399 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005400 static int entered = FALSE;
5401
5402 /*
5403 * This function is called by event handlers. Just in case we get a
5404 * second event before the first one is handled, ignore the second one.
5405 * Not sure if this can ever happen, just in case.
5406 */
5407 if (entered)
5408 return;
5409 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
5411 /*
5412 * When the cursor is at the command line, add the file names to the
5413 * command line, don't edit the files.
5414 */
5415 if (State & CMDLINE)
5416 {
5417 shorten_filenames(fnames, count);
5418 for (i = 0; i < count; ++i)
5419 {
5420 if (fnames[i] != NULL)
5421 {
5422 if (i > 0)
5423 add_to_input_buf((char_u*)" ", 1);
5424
5425 /* We don't know what command is used thus we can't be sure
5426 * about which characters need to be escaped. Only escape the
5427 * most common ones. */
5428# ifdef BACKSLASH_IN_FILENAME
5429 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5430# else
5431 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5432# endif
5433 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005434 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 vim_free(p);
5436 vim_free(fnames[i]);
5437 }
5438 }
5439 vim_free(fnames);
5440 }
5441 else
5442 {
5443 /* Go to the window under mouse cursor, then shorten given "fnames" by
5444 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 shorten_filenames(fnames, count);
5447
5448 /* If Shift held down, remember the first item. */
5449 if ((modifiers & MOUSE_SHIFT) != 0)
5450 p = vim_strsave(fnames[0]);
5451 else
5452 p = NULL;
5453
5454 /* Handle the drop, :edit or :split to get to the file. This also
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005455 * frees fnames[]. Skip this if there is only one item, it's a
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 * directory and Shift is held down. */
5457 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5458 && mch_isdir(fnames[0]))
5459 {
5460 vim_free(fnames[0]);
5461 vim_free(fnames);
5462 }
5463 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005464 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
5465 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005467
5468 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469}
5470#endif