blob: 471202bf477e64881a77e6796222d7d5e1b69b12 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10
11#include "vim.h"
12
13/* Structure containing all the GUI information */
14gui_T gui;
15
Bram Moolenaar13505972019-01-24 15:04:48 +010016#if !defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010017static void set_guifontwide(char_u *font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010019static void gui_check_pos(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010020static void gui_outstr(char_u *, int);
21static int gui_screenchar(int off, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010022static void gui_delete_lines(int row, int count);
23static void gui_insert_lines(int row, int count);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000024#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010025static int gui_has_tabline(void);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000026#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010027static void gui_do_scrollbar(win_T *wp, int which, int enable);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010028static void gui_update_horiz_scrollbar(int);
29static void gui_set_fg_color(char_u *name);
30static void gui_set_bg_color(char_u *name);
31static win_T *xy2win(int x, int y);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020033#ifdef GUI_MAY_FORK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010034static void gui_do_fork(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020035
Bram Moolenaard25c16e2016-01-29 22:13:30 +010036static int gui_read_child_pipe(int fd);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020037
38/* Return values for gui_read_child_pipe */
39enum {
40 GUI_CHILD_IO_ERROR,
41 GUI_CHILD_OK,
42 GUI_CHILD_FAILED
43};
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020044#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020045
Bram Moolenaard25c16e2016-01-29 22:13:30 +010046static void gui_attempt_start(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020047
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static int can_update_cursor = TRUE; /* can display the cursor */
Bram Moolenaara338adc2018-01-31 20:51:47 +010049static int disable_flush = 0; /* If > 0, gui_mch_flush() is disabled. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000050
51/*
52 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
53 * this makes the thumb indicate the part of the text that is shown. Motif
54 * can't do this.
55 */
56#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
57# define SCROLL_PAST_END
58#endif
59
60/*
61 * gui_start -- Called when user wants to start the GUI.
62 *
63 * Careful: This function can be called recursively when there is a ":gui"
64 * command in the .gvimrc file. Only the first call should fork, not the
65 * recursive call.
66 */
67 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +020068gui_start(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000069{
70 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 static int recursive = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +020072#ifdef GUI_MAY_SPAWN
73 char *msg = NULL;
74#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
76 old_term = vim_strsave(T_NAME);
77
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 settmode(TMODE_COOK); /* stop RAW mode */
79 if (full_screen)
80 cursor_on(); /* needed for ":gui" in .vimrc */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081 full_screen = FALSE;
82
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 ++recursive;
84
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020085#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020086 /*
87 * Quit the current process and continue in the child.
88 * Makes "gvim file" disconnect from the shell it was started in.
89 * Don't do this when Vim was started with "-f" or the 'f' flag is present
90 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020091 * Don't do this when there is a running job, we can only get the status
92 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020093 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020094 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
95# ifdef FEAT_JOB_CHANNEL
96 && !job_any_running()
97# endif
98 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020099 {
100 gui_do_fork();
101 }
102 else
103#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200104#ifdef GUI_MAY_SPAWN
105 if (gui.dospawn
106# ifdef EXPERIMENTAL_GUI_CMD
107 && gui.dofork
108# endif
109 && !vim_strchr(p_go, GO_FORG)
110 && !anyBufIsChanged()
111# ifdef FEAT_JOB_CHANNEL
112 && !job_any_running()
113# endif
114 )
115 {
116 msg = gui_mch_do_spawn(arg);
117 }
118 else
119#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200120 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200121#ifdef FEAT_GUI_GTK
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200122 /* If there is 'f' in 'guioptions' and specify -g argument,
123 * gui_mch_init_check() was not called yet. */
124 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100125 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200126#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200127 gui_attempt_start();
128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
130 if (!gui.in_use) /* failed to start GUI */
131 {
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200132 /* Back to old term settings
133 *
134 * FIXME: If we got here because a child process failed and flagged to
135 * the parent to resume, and X11 is enabled with FEAT_TITLE, this will
136 * hit an X11 I/O error and do a longjmp(), leaving recursive
137 * permanently set to 1. This is probably not as big a problem as it
138 * sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
139 * return "OK" unconditionally, so it would be very difficult to
140 * actually hit this case.
141 */
142 termcapinit(old_term);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 settmode(TMODE_RAW); /* restart RAW mode */
144#ifdef FEAT_TITLE
145 set_title_defaults(); /* set 'title' and 'icon' again */
146#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200147#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
148 if (msg)
149 emsg(msg);
150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 }
152
153 vim_free(old_term);
154
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200155 /* If the GUI started successfully, trigger the GUIEnter event, otherwise
156 * the GUIFailed event. */
157 gui_mch_update();
158 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
159 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200160 --recursive;
161}
162
163/*
164 * Set_termname() will call gui_init() to start the GUI.
165 * Set the "starting" flag, to indicate that the GUI will start.
166 *
167 * We don't want to open the GUI shell until after we've read .gvimrc,
168 * otherwise we don't know what font we will use, and hence we don't know
169 * what size the shell should be. So if there are errors in the .gvimrc
170 * file, they will have to go to the terminal: Set full_screen to FALSE.
171 * full_screen will be set to TRUE again by a successful termcapinit().
172 */
173 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100174gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200175{
176 static int recursive = 0;
177
178 ++recursive;
179 gui.starting = TRUE;
180
181#ifdef FEAT_GUI_GTK
182 gui.event_time = GDK_CURRENT_TIME;
183#endif
184
185 termcapinit((char_u *)"builtin_gui");
186 gui.starting = recursive - 1;
187
Bram Moolenaar9372a112005-12-06 19:59:18 +0000188#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200190 {
191# ifdef FEAT_EVAL
192 Window x11_window;
193 Display *x11_display;
194
195 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
196 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
197# endif
198
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 /* Display error messages in a dialog now. */
200 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 --recursive;
204}
205
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200206#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200207
208/* for waitpid() */
209# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
210# include <sys/wait.h>
211# endif
212
213/*
214 * Create a new process, by forking. In the child, start the GUI, and in
215 * the parent, exit.
216 *
217 * If something goes wrong, this will return with gui.in_use still set
218 * to FALSE, in which case the caller should continue execution without
219 * the GUI.
220 *
221 * If the child fails to start the GUI, then the child will exit and the
222 * parent will return. If the child succeeds, then the parent will exit
223 * and the child will return.
224 */
225 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100226gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200227{
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200228 int pipefd[2]; /* pipe between parent and child */
229 int pipe_error;
230 int status;
231 int exit_status;
232 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200233
234 /* Setup a pipe between the child and the parent, so that the parent
235 * knows when the child has done the setsid() call and is allowed to
236 * exit. */
237 pipe_error = (pipe(pipefd) < 0);
238 pid = fork();
239 if (pid < 0) /* Fork error */
240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100241 emsg(_("E851: Failed to create a new process for the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200242 return;
243 }
244 else if (pid > 0) /* Parent */
245 {
246 /* Give the child some time to do the setsid(), otherwise the
247 * exit() may kill the child too (when starting gvim from inside a
248 * gvim). */
249 if (!pipe_error)
250 {
251 /* The read returns when the child closes the pipe (or when
252 * the child dies for some reason). */
253 close(pipefd[1]);
254 status = gui_read_child_pipe(pipefd[0]);
255 if (status == GUI_CHILD_FAILED)
256 {
257 /* The child failed to start the GUI, so the caller must
258 * continue. There may be more error information written
259 * to stderr by the child. */
260# ifdef __NeXT__
261 wait4(pid, &exit_status, 0, (struct rusage *)0);
262# else
263 waitpid(pid, &exit_status, 0);
264# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100265 emsg(_("E852: The child process failed to start the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200266 return;
267 }
268 else if (status == GUI_CHILD_IO_ERROR)
269 {
270 pipe_error = TRUE;
271 }
272 /* else GUI_CHILD_OK: parent exit */
273 }
274
275 if (pipe_error)
276 ui_delay(300L, TRUE);
277
278 /* When swapping screens we may need to go to the next line, e.g.,
279 * after a hit-enter prompt and using ":gui". */
280 if (newline_on_exit)
281 mch_errmsg("\r\n");
282
283 /*
284 * The parent must skip the normal exit() processing, the child
285 * will do it. For example, GTK messes up signals when exiting.
286 */
287 _exit(0);
288 }
289 /* Child */
290
Bram Moolenaar29695702012-05-18 17:03:18 +0200291#ifdef FEAT_GUI_GTK
292 /* Call gtk_init_check() here after fork(). See gui_init_check(). */
293 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100294 getout_preserve_modified(1);
Bram Moolenaar29695702012-05-18 17:03:18 +0200295#endif
296
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200297# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
298 /*
299 * Change our process group. On some systems/shells a CTRL-C in the
300 * shell where Vim was started would otherwise kill gvim!
301 */
302# if defined(HAVE_SETSID)
303 (void)setsid();
304# else
305 (void)setpgid(0, 0);
306# endif
307# endif
308 if (!pipe_error)
309 close(pipefd[0]);
310
311# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
312 /* Tell the session manager our new PID */
313 gui_mch_forked();
314# endif
315
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200316 /* Try to start the GUI */
317 gui_attempt_start();
318
319 /* Notify the parent */
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200320 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200321 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200322 if (gui.in_use)
323 write_eintr(pipefd[1], "ok", 3);
324 else
325 write_eintr(pipefd[1], "fail", 5);
326 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200327 }
328
329 /* If we failed to start the GUI, exit now. */
330 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100331 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200332}
333
334/*
335 * Read from a pipe assumed to be connected to the child process (this
336 * function is called from the parent).
337 * Return GUI_CHILD_OK if the child successfully started the GUI,
338 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
339 * some other error.
340 *
341 * The file descriptor will be closed before the function returns.
342 */
343 static int
344gui_read_child_pipe(int fd)
345{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200346 long bytes_read;
347#define READ_BUFFER_SIZE 10
348 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200349
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200350 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
351#undef READ_BUFFER_SIZE
352 close(fd);
353 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200354 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200355 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200356 if (strcmp(buffer, "ok") == 0)
357 return GUI_CHILD_OK;
358 return GUI_CHILD_FAILED;
359}
360
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200361#endif /* GUI_MAY_FORK */
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Call this when vim starts up, whether or not the GUI is started
365 */
366 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100367gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368{
369 gui.in_use = FALSE; /* No GUI yet (maybe later) */
370 gui.starting = FALSE; /* No GUI yet (maybe later) */
371 gui_mch_prepare(argc, argv);
372}
373
374/*
375 * Try initializing the GUI and check if it can be started.
376 * Used from main() to check early if "vim -g" can start the GUI.
377 * Used from gui_init() to prepare for starting the GUI.
378 * Returns FAIL or OK.
379 */
380 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100381gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382{
383 static int result = MAYBE;
384
385 if (result != MAYBE)
386 {
387 if (result == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100388 emsg(_("E229: Cannot start the GUI"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 return result;
390 }
391
392 gui.shell_created = FALSE;
393 gui.dying = FALSE;
394 gui.in_focus = TRUE; /* so the guicursor setting works */
395 gui.dragged_sb = SBAR_NONE;
396 gui.dragged_wp = NULL;
397 gui.pointer_hidden = FALSE;
398 gui.col = 0;
399 gui.row = 0;
400 gui.num_cols = Columns;
401 gui.num_rows = Rows;
402
403 gui.cursor_is_valid = FALSE;
404 gui.scroll_region_top = 0;
405 gui.scroll_region_bot = Rows - 1;
406 gui.scroll_region_left = 0;
407 gui.scroll_region_right = Columns - 1;
408 gui.highlight_mask = HL_NORMAL;
409 gui.char_width = 1;
410 gui.char_height = 1;
411 gui.char_ascent = 0;
412 gui.border_width = 0;
413
414 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200415#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 gui.bold_font = NOFONT;
417 gui.ital_font = NOFONT;
418 gui.boldital_font = NOFONT;
419# ifdef FEAT_XFONTSET
420 gui.fontset = NOFONTSET;
421# endif
422#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200423 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100424#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200425 gui.wide_bold_font = NOFONT;
426 gui.wide_ital_font = NOFONT;
427 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429
430#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200431# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432# ifdef FONTSET_ALWAYS
433 gui.menu_fontset = NOFONTSET;
434# else
435 gui.menu_font = NOFONT;
436# endif
437# endif
438 gui.menu_is_active = TRUE; /* default: include menu */
439# ifndef FEAT_GUI_GTK
440 gui.menu_height = MENU_DEFAULT_HEIGHT;
441 gui.menu_width = 0;
442# endif
443#endif
444#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
445 gui.toolbar_height = 0;
446#endif
447#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
448 gui.footer_height = 0;
449#endif
450#ifdef FEAT_BEVAL_TIP
451 gui.tooltip_fontset = NOFONTSET;
452#endif
453
454 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
455 gui.prev_wrap = -1;
456
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200457#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 result = OK;
459#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200460# ifdef FEAT_GUI_GTK
461 /*
462 * Note: Don't call gtk_init_check() before fork, it will be called after
463 * the fork. When calling it before fork, it make vim hang for a while.
464 * See gui_do_fork().
465 * Use a simpler check if the GUI window can probably be opened.
466 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200467 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200468# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200470# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471#endif
472 return result;
473}
474
475/*
476 * This is the call which starts the GUI.
477 */
478 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100479gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
481 win_T *wp;
482 static int recursive = 0;
483
484 /*
485 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
486 * function will then be executed at the first call, the rest by the
487 * recursive call. This allow the shell to be opened halfway reading a
488 * gvimrc file.
489 */
490 if (!recursive)
491 {
492 ++recursive;
493
494 clip_init(TRUE);
495
496 /* If can't initialize, don't try doing the rest */
497 if (gui_init_check() == FAIL)
498 {
499 --recursive;
500 clip_init(FALSE);
501 return;
502 }
503
504 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000505 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
506 * breaks the Paste toolbar button.
507 */
508 set_option_value((char_u *)"paste", 0L, NULL, 0);
509
510 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 * Set up system-wide default menus.
512 */
513#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
514 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
515 {
516 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000517 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 sys_menu = FALSE;
519 }
520#endif
521
522 /*
523 * Switch on the mouse by default, unless the user changed it already.
524 * This can then be changed in the .gvimrc.
525 */
526 if (!option_was_set((char_u *)"mouse"))
527 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000528 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
530 /*
531 * If -U option given, use only the initializations from that file and
532 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
533 */
534 if (use_gvimrc != NULL)
535 {
536 if (STRCMP(use_gvimrc, "NONE") != 0
537 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000538 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100539 semsg(_("E230: Cannot read from \"%s\""), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
541 else
542 {
543 /*
544 * Get system wide defaults for gvim, only when file name defined.
545 */
546#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000547 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548#endif
549
550 /*
551 * Try to read GUI initialization commands from the following
552 * places:
553 * - environment variable GVIMINIT
554 * - the user gvimrc file (~/.gvimrc)
555 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
556 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
557 * The first that exists is used, the rest is ignored.
558 */
559 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000560 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
561 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000563 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
564 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200566#ifdef USR_GVIMRC_FILE3
567 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
568 DOSO_GVIMRC) == FAIL
569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 )
571 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200572#ifdef USR_GVIMRC_FILE4
573 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574#endif
575 }
576
577 /*
578 * Read initialization commands from ".gvimrc" in current
579 * directory. This is only done if the 'exrc' option is set.
580 * Because of security reasons we disallow shell and write
581 * commands now, except for unix if the file is owned by the user
582 * or 'secure' option has been reset in environment of global
583 * ".gvimrc".
584 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
585 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
586 */
587 if (p_exrc)
588 {
589#ifdef UNIX
590 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200591 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 /* if ".gvimrc" file is not owned by user, set 'secure'
594 * mode */
595 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
596 secure = p_secure;
597 }
598#else
599 secure = p_secure;
600#endif
601
602 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
603 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
604#ifdef SYS_GVIMRC_FILE
605 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
606 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
607#endif
608#ifdef USR_GVIMRC_FILE2
609 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
610 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
611#endif
612#ifdef USR_GVIMRC_FILE3
613 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
614 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
615#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200616#ifdef USR_GVIMRC_FILE4
617 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
618 (char_u *)GVIMRC_FILE, FALSE) != FPC_SAME
619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000621 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
623 if (secure == 2)
624 need_wait_return = TRUE;
625 secure = 0;
626 }
627 }
628
629 if (need_wait_return || msg_didany)
630 wait_return(TRUE);
631
632 --recursive;
633 }
634
635 /* If recursive call opened the shell, return here from the first call */
636 if (gui.in_use)
637 return;
638
639 /*
640 * Create the GUI shell.
641 */
642 gui.in_use = TRUE; /* Must be set after menus have been set up */
643 if (gui_mch_init() == FAIL)
644 goto error;
645
646 /* Avoid a delay for an error message that was printed in the terminal
647 * where Vim was started. */
648 emsg_on_display = FALSE;
649 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100650 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 need_wait_return = FALSE;
652 msg_didany = FALSE;
653
654 /*
655 * Check validity of any generic resources that may have been loaded.
656 */
657 if (gui.border_width < 0)
658 gui.border_width = 0;
659
660 /*
661 * Set up the fonts. First use a font specified with "-fn" or "-font".
662 */
663 if (font_argument != NULL)
664 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
665 if (
666#ifdef FEAT_XFONTSET
667 (*p_guifontset == NUL
668 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
669#endif
670 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
671 : p_guifont, FALSE) == FAIL)
672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100673 emsg(_("E665: Cannot start GUI, no valid font found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 goto error2;
675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 if (gui_get_wide_font() == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100677 emsg(_("E231: 'guifontwide' invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 gui.num_cols = Columns;
680 gui.num_rows = Rows;
681 gui_reset_scroll_region();
682
683 /* Create initial scrollbars */
684 FOR_ALL_WINDOWS(wp)
685 {
686 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
687 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
688 }
689 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
690
691#ifdef FEAT_MENU
692 gui_create_initial_menus(root_menu);
693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694#ifdef FEAT_SIGN_ICONS
695 sign_gui_started();
696#endif
697
698 /* Configure the desired menu and scrollbars */
699 gui_init_which_components(NULL);
700
701 /* All components of the GUI have been created now */
702 gui.shell_created = TRUE;
703
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100704#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100705 // Set the shell size, adjusted for the screen size. For GTK this only
706 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100707 // If the window is already maximized (e.g. when --windowid is passed in),
708 // we want to use the system-provided dimensions by passing FALSE to
709 // mustset. Otherwise, we want to initialize with the default rows/columns.
710 if (gui_mch_maximized())
711 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
712 else
713 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100714#else
715# ifndef FEAT_GUI_GTK
716 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
717# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#endif
719#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
720 /* Need to set the size of the menubar after all the menus have been
721 * created. */
722 gui_mch_compute_menu_height((Widget)0);
723#endif
724
725 /*
726 * Actually open the GUI shell.
727 */
728 if (gui_mch_open() != FAIL)
729 {
730#ifdef FEAT_TITLE
731 maketitle();
732 resettitle();
733#endif
734 init_gui_options();
735#ifdef FEAT_ARABIC
736 /* Our GUI can't do bidi. */
737 p_tbidi = FALSE;
738#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000739#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 /* Give GTK+ a chance to put all widget's into place. */
741 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000742
743# ifdef FEAT_MENU
744 /* If there is no 'm' in 'guioptions' we need to remove the menu now.
745 * It was still there to make F10 work. */
746 if (vim_strchr(p_go, GO_MENUS) == NULL)
747 {
748 --gui.starting;
749 gui_mch_enable_menu(FALSE);
750 ++gui.starting;
751 gui_mch_update();
752 }
753# endif
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 /* Now make sure the shell fits on the screen. */
Bram Moolenaar372674f2019-03-30 20:31:22 +0100756 if (gui_mch_maximized())
757 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
758 else
759 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760#endif
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000761 /* When 'lines' was set while starting up the topframe may have to be
762 * resized. */
763 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000764
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100765#ifdef FEAT_BEVAL_GUI
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000766 /* Always create the Balloon Evaluation area, but disable it when
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100767 * 'ballooneval' is off. */
768 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200769 {
770# ifdef FEAT_VARTABS
771 vim_free(balloonEval->vts);
772# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100773 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200774 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100775 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000776# ifdef FEAT_GUI_GTK
777 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
778 &general_beval_cb, NULL);
779# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000780# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000781 {
782 extern Widget textArea;
783 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000784 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000785 }
786# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100787# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000788 balloonEval = gui_mch_create_beval_area(NULL, NULL,
789 &general_beval_cb, NULL);
790# endif
791# endif
792# endif
793 if (!p_beval)
794 gui_mch_disable_beval_area(balloonEval);
795#endif
796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
798 if (!im_xim_isvalid_imactivate())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100799 emsg(_("E599: Value of 'imactivatekey' is invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800#endif
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000801 /* When 'cmdheight' was set during startup it may not have taken
802 * effect yet. */
803 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000804 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
806 return;
807 }
808
809error2:
810#ifdef FEAT_GUI_X11
811 /* undo gui_mch_init() */
812 gui_mch_uninit();
813#endif
814
815error:
816 gui.in_use = FALSE;
817 clip_init(FALSE);
818}
819
820
821 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100822gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 /* don't free the fonts, it leads to a BUS error
825 * richard@whitequeen.com Jul 99 */
826 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 gui.in_use = FALSE;
828 gui_mch_exit(rc);
829}
830
Bram Moolenaar9372a112005-12-06 19:59:18 +0000831#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000833# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834/*
835 * Called when the GUI shell is closed by the user. If there are no changed
836 * files Vim exits, otherwise there will be a dialog to ask the user what to
837 * do.
838 * When this function returns, Vim should NOT exit!
839 */
840 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100841gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
843 cmdmod_T save_cmdmod;
844
845 save_cmdmod = cmdmod;
846
847 /* Only exit when there are no changed files */
848 exiting = TRUE;
849# ifdef FEAT_BROWSE
850 cmdmod.browse = TRUE;
851# endif
852# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
853 cmdmod.confirm = TRUE;
854# endif
855 /* If there are changed buffers, present the user with a dialog if
856 * possible, otherwise give an error message. */
Bram Moolenaar027387f2016-01-02 22:25:52 +0100857 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 getout(0);
859
860 exiting = FALSE;
861 cmdmod = save_cmdmod;
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000862 gui_update_screen(); /* redraw, window may show changed buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863}
864#endif
865
866/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200867 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 * first font name that works is used. If none is found, use the default
869 * font.
870 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
871 * Return OK when able to set the font. When it failed FAIL is returned and
872 * the fonts are unchanged.
873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100875gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
877#define FONTLEN 320
878 char_u font_name[FONTLEN];
879 int font_list_empty = FALSE;
880 int ret = FAIL;
881
882 if (!gui.in_use)
883 return FAIL;
884
885 font_name[0] = NUL;
886 if (*font_list == NUL)
887 font_list_empty = TRUE;
888 else
889 {
890#ifdef FEAT_XFONTSET
891 /* When using a fontset, the whole list of fonts is one name. */
892 if (fontset)
893 ret = gui_mch_init_font(font_list, TRUE);
894 else
895#endif
896 while (*font_list != NUL)
897 {
898 /* Isolate one comma separated font name. */
899 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
900
901 /* Careful!!! The Win32 version of gui_mch_init_font(), when
902 * called with "*" will change p_guifont to the selected font
903 * name, which frees the old value. This makes font_list
904 * invalid. Thus when OK is returned here, font_list must no
905 * longer be used! */
906 if (gui_mch_init_font(font_name, FALSE) == OK)
907 {
Bram Moolenaar13505972019-01-24 15:04:48 +0100908#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 /* If it's a Unicode font, try setting 'guifontwide' to a
910 * similar double-width font. */
911 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
912 && strstr((char *)font_name, "10646") != NULL)
913 set_guifontwide(font_name);
914#endif
915 ret = OK;
916 break;
917 }
918 }
919 }
920
921 if (ret != OK
922 && STRCMP(font_list, "*") != 0
923 && (font_list_empty || gui.norm_font == NOFONT))
924 {
925 /*
926 * Couldn't load any font in 'font_list', keep the current font if
927 * there is one. If 'font_list' is empty, or if there is no current
928 * font, tell gui_mch_init_font() to try to find a font we can load.
929 */
930 ret = gui_mch_init_font(NULL, FALSE);
931 }
932
933 if (ret == OK)
934 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200935#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 /* Set normal font as current font */
937# ifdef FEAT_XFONTSET
938 if (gui.fontset != NOFONTSET)
939 gui_mch_set_fontset(gui.fontset);
940 else
941# endif
942 gui_mch_set_font(gui.norm_font);
943#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100944 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
946
947 return ret;
948}
949
Bram Moolenaar13505972019-01-24 15:04:48 +0100950#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951/*
952 * Try setting 'guifontwide' to a font twice as wide as "name".
953 */
954 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100955set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
957 int i = 0;
958 char_u wide_name[FONTLEN + 10]; /* room for 2 * width and '*' */
959 char_u *wp = NULL;
960 char_u *p;
961 GuiFont font;
962
963 wp = wide_name;
964 for (p = name; *p != NUL; ++p)
965 {
966 *wp++ = *p;
967 if (*p == '-')
968 {
969 ++i;
970 if (i == 6) /* font type: change "--" to "-*-" */
971 {
972 if (p[1] == '-')
973 *wp++ = '*';
974 }
975 else if (i == 12) /* found the width */
976 {
977 ++p;
978 i = getdigits(&p);
979 if (i != 0)
980 {
981 /* Double the width specification. */
982 sprintf((char *)wp, "%d%s", i * 2, p);
983 font = gui_mch_get_font(wide_name, FALSE);
984 if (font != NOFONT)
985 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000986 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 gui.wide_font = font;
988 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000989 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 }
991 }
992 break;
993 }
994 }
995 }
996}
Bram Moolenaar13505972019-01-24 15:04:48 +0100997#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
999/*
1000 * Get the font for 'guifontwide'.
1001 * Return FAIL for an invalid font name.
1002 */
1003 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001004gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005{
1006 GuiFont font = NOFONT;
1007 char_u font_name[FONTLEN];
1008 char_u *p;
1009
1010 if (!gui.in_use) /* Can't allocate font yet, assume it's OK. */
1011 return OK; /* Will give an error message later. */
1012
1013 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1014 {
1015 for (p = p_guifontwide; *p != NUL; )
1016 {
1017 /* Isolate one comma separated font name. */
1018 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1019 font = gui_mch_get_font(font_name, FALSE);
1020 if (font != NOFONT)
1021 break;
1022 }
1023 if (font == NOFONT)
1024 return FAIL;
1025 }
1026
1027 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001028#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
1030 if (font != NOFONT && gui.norm_font != NOFONT
1031 && pango_font_description_equal(font, gui.norm_font))
1032 {
1033 gui.wide_font = NOFONT;
1034 gui_mch_free_font(font);
1035 }
1036 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001039#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001040 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001041#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001042 /*
1043 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1044 * support those fonts for 'guifontwide'.
1045 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 return OK;
1048}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
1050 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001051gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
1053 gui.row = row;
1054 gui.col = col;
1055}
1056
1057/*
1058 * gui_check_pos - check if the cursor is on the screen.
1059 */
1060 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001061gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
1063 if (gui.row >= screen_Rows)
1064 gui.row = screen_Rows - 1;
1065 if (gui.col >= screen_Columns)
1066 gui.col = screen_Columns - 1;
1067 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1068 gui.cursor_is_valid = FALSE;
1069}
1070
1071/*
1072 * Redraw the cursor if necessary or when forced.
1073 * Careful: The contents of ScreenLines[] must match what is on the screen,
1074 * otherwise this goes wrong. May need to call out_flush() first.
1075 */
1076 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001077gui_update_cursor(
1078 int force, /* when TRUE, update even when not moved */
1079 int clear_selection)/* clear selection under cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080{
1081 int cur_width = 0;
1082 int cur_height = 0;
1083 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001084 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001086#ifdef FEAT_TERMINAL
1087 guicolor_T shape_fg = INVALCOLOR;
1088 guicolor_T shape_bg = INVALCOLOR;
1089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 guicolor_T cfg, cbg, cc; /* cursor fore-/background color */
1091 int cattr; /* cursor attributes */
1092 int attr;
1093 attrentry_T *aep = NULL;
1094
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001095 /* Don't update the cursor when halfway busy scrolling or the screen size
1096 * doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then. */
1097 if (!can_update_cursor || screen_Columns != gui.num_cols
1098 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 return;
1100
1101 gui_check_pos();
1102 if (!gui.cursor_is_valid || force
1103 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1104 {
1105 gui_undraw_cursor();
1106 if (gui.row < 0)
1107 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001108#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1110 im_set_position(gui.row, gui.col);
1111#endif
1112 gui.cursor_row = gui.row;
1113 gui.cursor_col = gui.col;
1114
1115 /* Only write to the screen after ScreenLines[] has been initialized */
1116 if (!screen_cleared || ScreenLines == NULL)
1117 return;
1118
1119 /* Clear the selection if we are about to write over it */
1120 if (clear_selection)
1121 clip_may_clear_selection(gui.row, gui.row);
1122 /* Check that the cursor is inside the shell (resizing may have made
1123 * it invalid) */
1124 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1125 return;
1126
1127 gui.cursor_is_valid = TRUE;
1128
1129 /*
1130 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001131 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001133#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001134 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001135 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001137#endif
1138 shape = &shape_table[get_shape_idx(FALSE)];
1139 if (State & LANGMAP)
1140 id = shape->id_lm;
1141 else
1142 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
1144 /* get the colors and attributes for the cursor. Default is inverted */
1145 cfg = INVALCOLOR;
1146 cbg = INVALCOLOR;
1147 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001148 gui_mch_set_blinking(shape->blinkwait,
1149 shape->blinkon,
1150 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001151 if (shape->blinkwait == 0 || shape->blinkon == 0
1152 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001153 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001154#ifdef FEAT_TERMINAL
1155 if (shape_bg != INVALCOLOR)
1156 {
1157 cattr = 0;
1158 cfg = shape_fg;
1159 cbg = shape_bg;
1160 }
1161 else
1162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if (id > 0)
1164 {
1165 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001166#if defined(HAVE_INPUT_METHOD) || defined(FEAT_HANGULIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
1168 static int iid;
1169 guicolor_T fg, bg;
1170
Bram Moolenaarc236c162008-07-13 17:41:49 +00001171 if (
Bram Moolenaar28944fe2018-02-04 19:01:31 +01001172# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM) && !defined(FEAT_HANGULIN)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001173 preedit_get_status()
1174# else
1175 im_get_status()
1176# endif
1177 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179 iid = syn_name2id((char_u *)"CursorIM");
1180 if (iid > 0)
1181 {
1182 syn_id2colors(iid, &fg, &bg);
1183 if (bg != INVALCOLOR)
1184 cbg = bg;
1185 if (fg != INVALCOLOR)
1186 cfg = fg;
1187 }
1188 }
1189 }
1190#endif
1191 }
1192
1193 /*
1194 * Get the attributes for the character under the cursor.
1195 * When no cursor color was given, use the character color.
1196 */
1197 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1198 if (attr > HL_ALL)
1199 aep = syn_gui_attr2entry(attr);
1200 if (aep != NULL)
1201 {
1202 attr = aep->ae_attr;
1203 if (cfg == INVALCOLOR)
1204 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1205 : aep->ae_u.gui.fg_color);
1206 if (cbg == INVALCOLOR)
1207 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1208 : aep->ae_u.gui.bg_color);
1209 }
1210 if (cfg == INVALCOLOR)
1211 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1212 if (cbg == INVALCOLOR)
1213 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1214
1215#ifdef FEAT_XIM
1216 if (aep != NULL)
1217 {
1218 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1219 : aep->ae_u.gui.bg_color);
1220 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1221 : aep->ae_u.gui.fg_color);
1222 if (xim_bg_color == INVALCOLOR)
1223 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1224 : gui.back_pixel;
1225 if (xim_fg_color == INVALCOLOR)
1226 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1227 : gui.norm_pixel;
1228 }
1229 else
1230 {
1231 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1232 : gui.back_pixel;
1233 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1234 : gui.norm_pixel;
1235 }
1236#endif
1237
1238 attr &= ~HL_INVERSE;
1239 if (cattr & HL_INVERSE)
1240 {
1241 cc = cbg;
1242 cbg = cfg;
1243 cfg = cc;
1244 }
1245 cattr &= ~HL_INVERSE;
1246
1247 /*
1248 * When we don't have window focus, draw a hollow cursor.
1249 */
1250 if (!gui.in_focus)
1251 {
1252 gui_mch_draw_hollow_cursor(cbg);
1253 return;
1254 }
1255
1256 old_hl_mask = gui.highlight_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001257 if (shape->shape == SHAPE_BLOCK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#ifdef FEAT_HANGULIN
1259 || composing_hangul
1260#endif
1261 )
1262 {
1263 /*
1264 * Draw the text character with the cursor colors. Use the
1265 * character attributes plus the cursor attributes.
1266 */
1267 gui.highlight_mask = (cattr | attr);
1268#ifdef FEAT_HANGULIN
1269 if (composing_hangul)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001270 {
1271 char_u *comp_buf;
1272 int comp_len;
1273
1274 comp_buf = hangul_composing_buffer_get(&comp_len);
1275 if (comp_buf)
1276 {
1277 (void)gui_outstr_nowrap(comp_buf, comp_len,
1278 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
1279 cfg, cbg, 0);
1280 vim_free(comp_buf);
1281 }
1282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 else
1284#endif
1285 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1286 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1287 }
1288 else
1289 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001290#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 int col_off = FALSE;
1292#endif
1293 /*
1294 * First draw the partial cursor, then overwrite with the text
1295 * character, using a transparent background.
1296 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001297 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
1299 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001300 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
1302 else
1303 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001304 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 cur_width = gui.char_width;
1306 }
Bram Moolenaar367329b2007-08-30 11:53:22 +00001307 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1308 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {
1310 /* Double wide character. */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001311 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001313#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if (CURSOR_BAR_RIGHT)
1315 {
1316 /* gui.col points to the left halve of the character but
1317 * the vertical line needs to be on the right halve.
1318 * A double-wide horizontal line is also drawn from the
1319 * right halve in gui_mch_draw_part_cursor(). */
1320 col_off = TRUE;
1321 ++gui.col;
1322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001326#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (col_off)
1328 --gui.col;
1329#endif
1330
1331#ifndef FEAT_GUI_MSWIN /* doesn't seem to work for MSWindows */
1332 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1333 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1334 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1335 (guicolor_T)0, (guicolor_T)0, 0);
1336#endif
1337 }
1338 gui.highlight_mask = old_hl_mask;
1339 }
1340}
1341
1342#if defined(FEAT_MENU) || defined(PROTO)
1343 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001344gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001346# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 if (gui.menu_is_active && gui.in_use)
1348 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1349# endif
1350}
1351#endif
1352
1353/*
1354 * Position the various GUI components (text area, menu). The vertical
1355 * scrollbars are NOT handled here. See gui_update_scrollbars().
1356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001358gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359{
1360 int text_area_x;
1361 int text_area_y;
1362 int text_area_width;
1363 int text_area_height;
1364
1365 /* avoid that moving components around generates events */
1366 ++hold_gui_events;
1367
1368 text_area_x = 0;
1369 if (gui.which_scrollbars[SBAR_LEFT])
1370 text_area_x += gui.scrollbar_width;
1371
1372 text_area_y = 0;
1373#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1374 gui.menu_width = total_width;
1375 if (gui.menu_is_active)
1376 text_area_y += gui.menu_height;
1377#endif
1378#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1379 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1380 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1381#endif
1382
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001383# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001384 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001385 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001386 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001387#endif
1388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1390 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1391 {
1392# ifdef FEAT_GUI_ATHENA
1393 gui_mch_set_toolbar_pos(0, text_area_y,
1394 gui.menu_width, gui.toolbar_height);
1395# endif
1396 text_area_y += gui.toolbar_height;
1397 }
1398#endif
1399
1400 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1401 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1402
1403 gui_mch_set_text_area_pos(text_area_x,
1404 text_area_y,
1405 text_area_width,
1406 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001407#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 + xim_get_status_area_height()
1409#endif
1410 );
1411#ifdef FEAT_MENU
1412 gui_position_menu();
1413#endif
1414 if (gui.which_scrollbars[SBAR_BOTTOM])
1415 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1416 text_area_x,
1417 text_area_y + text_area_height,
1418 text_area_width,
1419 gui.scrollbar_height);
1420 gui.left_sbar_x = 0;
1421 gui.right_sbar_x = text_area_x + text_area_width;
1422
1423 --hold_gui_events;
1424}
1425
Bram Moolenaar02743632005-07-25 20:42:36 +00001426/*
1427 * Get the width of the widgets and decorations to the side of the text area.
1428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001430gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
1432 int base_width;
1433
1434 base_width = 2 * gui.border_offset;
1435 if (gui.which_scrollbars[SBAR_LEFT])
1436 base_width += gui.scrollbar_width;
1437 if (gui.which_scrollbars[SBAR_RIGHT])
1438 base_width += gui.scrollbar_width;
1439 return base_width;
1440}
1441
Bram Moolenaar02743632005-07-25 20:42:36 +00001442/*
1443 * Get the height of the widgets and decorations above and below the text area.
1444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001446gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448 int base_height;
1449
1450 base_height = 2 * gui.border_offset;
1451 if (gui.which_scrollbars[SBAR_BOTTOM])
1452 base_height += gui.scrollbar_height;
1453#ifdef FEAT_GUI_GTK
1454 /* We can't take the sizes properly into account until anything is
1455 * realized. Therefore we recalculate all the values here just before
1456 * setting the size. (--mdcki) */
1457#else
1458# ifdef FEAT_MENU
1459 if (gui.menu_is_active)
1460 base_height += gui.menu_height;
1461# endif
1462# ifdef FEAT_TOOLBAR
1463 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1464# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1465 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1466# else
1467 base_height += gui.toolbar_height;
1468# endif
1469# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001470# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1471 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001472 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001473 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001474# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475# ifdef FEAT_FOOTER
1476 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1477 base_height += gui.footer_height;
1478# endif
1479# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1480 base_height += gui_mch_text_area_extra_height();
1481# endif
1482#endif
1483 return base_height;
1484}
1485
1486/*
1487 * Should be called after the GUI shell has been resized. Its arguments are
1488 * the new width and height of the shell in pixels.
1489 */
1490 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001491gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
1493 static int busy = FALSE;
1494
1495 if (!gui.shell_created) /* ignore when still initializing */
1496 return;
1497
1498 /*
1499 * Can't resize the screen while it is being redrawn. Remember the new
1500 * size and handle it later.
1501 */
1502 if (updating_screen || busy)
1503 {
1504 new_pixel_width = pixel_width;
1505 new_pixel_height = pixel_height;
1506 return;
1507 }
1508
1509again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001510 new_pixel_width = 0;
1511 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 busy = TRUE;
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 /* Flush pending output before redrawing */
1515 out_flush();
1516
1517 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001518 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
1520 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001522
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 /*
1524 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1525 * at the last line here (why does it have to be one row too low?).
1526 */
1527 if (State == ASKMORE || State == CONFIRM)
1528 gui.row = gui.num_rows;
1529
1530 /* Only comparing Rows and Columns may be sufficient, but let's stay on
1531 * the safe side. */
1532 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1533 || gui.num_rows != Rows || gui.num_cols != Columns)
1534 shell_resized();
1535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 gui_update_scrollbars(TRUE);
1537 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001538#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 xim_set_status_area();
1540#endif
1541
1542 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001543
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001544 /* We may have been called again while redrawing the screen.
1545 * Need to do it all again with the latest size then. But only if the size
1546 * actually changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 if (new_pixel_height)
1548 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001549 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1550 {
1551 new_pixel_width = 0;
1552 new_pixel_height = 0;
1553 }
1554 else
1555 {
1556 pixel_width = new_pixel_width;
1557 pixel_height = new_pixel_height;
1558 goto again;
1559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 }
1561}
1562
1563/*
1564 * Check if gui_resize_shell() must be called.
1565 */
1566 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001567gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 if (new_pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 /* careful: gui_resize_shell() may postpone the resize again if we
1571 * were called indirectly by it */
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001572 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573}
1574
1575 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001576gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577{
1578 Rows = gui.num_rows;
1579 Columns = gui.num_cols;
1580 return OK;
1581}
1582
1583/*
1584 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001585 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1586 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001587 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1588 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001591gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001592 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001593 int fit_to_display,
1594 int direction) /* RESIZE_HOR, RESIZE_VER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596 int base_width;
1597 int base_height;
1598 int width;
1599 int height;
1600 int min_width;
1601 int min_height;
1602 int screen_w;
1603 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001604#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001605 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001606 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001607#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001608 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
1610 if (!gui.shell_created)
1611 return;
1612
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001613#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 /* If not setting to a user specified size and maximized, calculate the
1615 * number of characters that fit in the maximized window. */
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001616 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1617 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
1619 gui_mch_newfont();
1620 return;
1621 }
1622#endif
1623
1624 base_width = gui_get_base_width();
1625 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001626 if (fit_to_display)
1627 /* Remember the original window position. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001628 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001629
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001630 width = Columns * gui.char_width + base_width;
1631 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 if (fit_to_display)
1634 {
1635 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001636 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 {
1638 Columns = (screen_w - base_width) / gui.char_width;
1639 if (Columns < MIN_COLUMNS)
1640 Columns = MIN_COLUMNS;
1641 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001642#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001643 ++did_adjust;
1644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001646 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 {
1648 Rows = (screen_h - base_height) / gui.char_height;
1649 check_shellsize();
1650 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001651#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001652 ++did_adjust;
1653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001655#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001656 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1657 && height + gui.char_height >= screen_h))
1658 /* don't unmaximize if at maximum size */
1659 un_maximize = FALSE;
1660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001662 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 gui.num_cols = Columns;
1664 gui.num_rows = Rows;
1665
1666 min_width = base_width + MIN_COLUMNS * gui.char_width;
1667 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001668 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001669
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001670#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001671 if (un_maximize)
1672 {
1673 /* If the window size is smaller than the screen unmaximize the
1674 * window, otherwise resizing won't work. */
1675 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1676 if ((width + gui.char_width < screen_w
1677 || height + gui.char_height * 2 < screen_h)
1678 && gui_mch_maximized())
1679 gui_mch_unmaximize();
1680 }
1681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001684 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001686 if (fit_to_display && x >= 0 && y >= 0)
1687 {
1688 /* Some window managers put the Vim window left of/above the screen.
1689 * Only change the position if it wasn't already negative before
1690 * (happens on MS-Windows with a secondary monitor). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 gui_mch_update();
1692 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1693 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1694 }
1695
1696 gui_position_components(width);
1697 gui_update_scrollbars(TRUE);
1698 gui_reset_scroll_region();
1699}
1700
1701/*
1702 * Called when Rows and/or Columns has changed.
1703 */
1704 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001705gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706{
1707 gui_reset_scroll_region();
1708}
1709
1710/*
1711 * Make scroll region cover whole screen.
1712 */
1713 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001714gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716 gui.scroll_region_top = 0;
1717 gui.scroll_region_bot = gui.num_rows - 1;
1718 gui.scroll_region_left = 0;
1719 gui.scroll_region_right = gui.num_cols - 1;
1720}
1721
1722 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001723gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 if (mask > HL_ALL) /* highlight code */
1726 gui.highlight_mask = mask;
1727 else /* mask */
1728 gui.highlight_mask |= mask;
1729}
1730
1731 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001732gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
1734 if (mask > HL_ALL) /* highlight code */
1735 gui.highlight_mask = HL_NORMAL;
1736 else /* mask */
1737 gui.highlight_mask &= ~mask;
1738}
1739
1740/*
1741 * Clear a rectangular region of the screen from text pos (row1, col1) to
1742 * (row2, col2) inclusive.
1743 */
1744 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001745gui_clear_block(
1746 int row1,
1747 int col1,
1748 int row2,
1749 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 /* Clear the selection if we are about to write over it */
1752 clip_may_clear_selection(row1, row2);
1753
1754 gui_mch_clear_block(row1, col1, row2, col2);
1755
1756 /* Invalidate cursor if it was in this block */
1757 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1758 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1759 gui.cursor_is_valid = FALSE;
1760}
1761
1762/*
1763 * Write code to update the cursor later. This avoids the need to flush the
1764 * output buffer before calling gui_update_cursor().
1765 */
1766 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001767gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001769 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770}
1771
1772 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001773gui_write(
1774 char_u *s,
1775 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 char_u *p;
1778 int arg1 = 0, arg2 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 int force_cursor = FALSE; /* force cursor update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 int force_scrollbar = FALSE;
1781 static win_T *old_curwin = NULL;
1782
1783/* #define DEBUG_GUI_WRITE */
1784#ifdef DEBUG_GUI_WRITE
1785 {
1786 int i;
1787 char_u *str;
1788
1789 printf("gui_write(%d):\n ", len);
1790 for (i = 0; i < len; i++)
1791 if (s[i] == ESC)
1792 {
1793 if (i != 0)
1794 printf("\n ");
1795 printf("<ESC>");
1796 }
1797 else
1798 {
1799 str = transchar_byte(s[i]);
1800 if (str[0] && str[1])
1801 printf("<%s>", (char *)str);
1802 else
1803 printf("%s", (char *)str);
1804 }
1805 printf("\n");
1806 }
1807#endif
1808 while (len)
1809 {
1810 if (s[0] == ESC && s[1] == '|')
1811 {
1812 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001813 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 {
1815 arg1 = getdigits(&p);
1816 if (p > s + len)
1817 break;
1818 if (*p == ';')
1819 {
1820 ++p;
1821 arg2 = getdigits(&p);
1822 if (p > s + len)
1823 break;
1824 }
1825 }
1826 switch (*p)
1827 {
1828 case 'C': /* Clear screen */
1829 clip_scroll_selection(9999);
1830 gui_mch_clear_all();
1831 gui.cursor_is_valid = FALSE;
1832 force_scrollbar = TRUE;
1833 break;
1834 case 'M': /* Move cursor */
1835 gui_set_cursor(arg1, arg2);
1836 break;
1837 case 's': /* force cursor (shape) update */
1838 force_cursor = TRUE;
1839 break;
1840 case 'R': /* Set scroll region */
1841 if (arg1 < arg2)
1842 {
1843 gui.scroll_region_top = arg1;
1844 gui.scroll_region_bot = arg2;
1845 }
1846 else
1847 {
1848 gui.scroll_region_top = arg2;
1849 gui.scroll_region_bot = arg1;
1850 }
1851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 case 'V': /* Set vertical scroll region */
1853 if (arg1 < arg2)
1854 {
1855 gui.scroll_region_left = arg1;
1856 gui.scroll_region_right = arg2;
1857 }
1858 else
1859 {
1860 gui.scroll_region_left = arg2;
1861 gui.scroll_region_right = arg1;
1862 }
1863 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 case 'd': /* Delete line */
1865 gui_delete_lines(gui.row, 1);
1866 break;
1867 case 'D': /* Delete lines */
1868 gui_delete_lines(gui.row, arg1);
1869 break;
1870 case 'i': /* Insert line */
1871 gui_insert_lines(gui.row, 1);
1872 break;
1873 case 'I': /* Insert lines */
1874 gui_insert_lines(gui.row, arg1);
1875 break;
1876 case '$': /* Clear to end-of-line */
1877 gui_clear_block(gui.row, gui.col, gui.row,
1878 (int)Columns - 1);
1879 break;
1880 case 'h': /* Turn on highlighting */
1881 gui_start_highlight(arg1);
1882 break;
1883 case 'H': /* Turn off highlighting */
1884 gui_stop_highlight(arg1);
1885 break;
1886 case 'f': /* flash the window (visual bell) */
1887 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1888 break;
1889 default:
1890 p = s + 1; /* Skip the ESC */
1891 break;
1892 }
1893 len -= (int)(++p - s);
1894 s = p;
1895 }
1896 else if (
1897#ifdef EBCDIC
1898 CtrlChar(s[0]) != 0 /* Ctrl character */
1899#else
1900 s[0] < 0x20 /* Ctrl character */
1901#endif
1902#ifdef FEAT_SIGN_ICONS
1903 && s[0] != SIGN_BYTE
1904# ifdef FEAT_NETBEANS_INTG
1905 && s[0] != MULTISIGN_BYTE
1906# endif
1907#endif
1908 )
1909 {
1910 if (s[0] == '\n') /* NL */
1911 {
1912 gui.col = 0;
1913 if (gui.row < gui.scroll_region_bot)
1914 gui.row++;
1915 else
1916 gui_delete_lines(gui.scroll_region_top, 1);
1917 }
1918 else if (s[0] == '\r') /* CR */
1919 {
1920 gui.col = 0;
1921 }
1922 else if (s[0] == '\b') /* Backspace */
1923 {
1924 if (gui.col)
1925 --gui.col;
1926 }
1927 else if (s[0] == Ctrl_L) /* cursor-right */
1928 {
1929 ++gui.col;
1930 }
1931 else if (s[0] == Ctrl_G) /* Beep */
1932 {
1933 gui_mch_beep();
1934 }
1935 /* Other Ctrl character: shouldn't happen! */
1936
1937 --len; /* Skip this char */
1938 ++s;
1939 }
1940 else
1941 {
1942 p = s;
1943 while (len > 0 && (
1944#ifdef EBCDIC
1945 CtrlChar(*p) == 0
1946#else
1947 *p >= 0x20
1948#endif
1949#ifdef FEAT_SIGN_ICONS
1950 || *p == SIGN_BYTE
1951# ifdef FEAT_NETBEANS_INTG
1952 || *p == MULTISIGN_BYTE
1953# endif
1954#endif
1955 ))
1956 {
1957 len--;
1958 p++;
1959 }
1960 gui_outstr(s, (int)(p - s));
1961 s = p;
1962 }
1963 }
1964
1965 /* Postponed update of the cursor (won't work if "can_update_cursor" isn't
1966 * set). */
1967 if (force_cursor)
1968 gui_update_cursor(TRUE, TRUE);
1969
1970 /* When switching to another window the dragging must have stopped.
1971 * Required for GTK, dragged_sb isn't reset. */
1972 if (old_curwin != curwin)
1973 gui.dragged_sb = SBAR_NONE;
1974
1975 /* Update the scrollbars after clearing the screen or when switched
1976 * to another window.
1977 * Update the horizontal scrollbar always, it's difficult to check all
1978 * situations where it might change. */
1979 if (force_scrollbar || old_curwin != curwin)
1980 gui_update_scrollbars(force_scrollbar);
1981 else
1982 gui_update_horiz_scrollbar(FALSE);
1983 old_curwin = curwin;
1984
1985 /*
1986 * We need to make sure this is cleared since Athena doesn't tell us when
1987 * he is done dragging. Do the same for GTK.
1988 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001989#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 gui.dragged_sb = SBAR_NONE;
1991#endif
1992
Bram Moolenaara338adc2018-01-31 20:51:47 +01001993 gui_may_flush(); /* In case vim decides to take a nap */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994}
1995
1996/*
1997 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1998 * produces wrong results. Call gui_dont_update_cursor() before that code and
1999 * gui_can_update_cursor() afterwards.
2000 */
2001 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02002002gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003{
2004 if (gui.in_use)
2005 {
2006 /* Undraw the cursor now, we probably can't do it after the change. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02002007 if (undraw)
2008 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 can_update_cursor = FALSE;
2010 }
2011}
2012
2013 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002014gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
2016 can_update_cursor = TRUE;
2017 /* No need to update the cursor right now, there is always more output
2018 * after scrolling. */
2019}
2020
Bram Moolenaara338adc2018-01-31 20:51:47 +01002021/*
2022 * Disable issuing gui_mch_flush().
2023 */
2024 void
2025gui_disable_flush(void)
2026{
2027 ++disable_flush;
2028}
2029
2030/*
2031 * Enable issuing gui_mch_flush().
2032 */
2033 void
2034gui_enable_flush(void)
2035{
2036 --disable_flush;
2037}
2038
2039/*
2040 * Issue gui_mch_flush() if it is not disabled.
2041 */
2042 void
2043gui_may_flush(void)
2044{
2045 if (disable_flush == 0)
2046 gui_mch_flush();
2047}
2048
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002050gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051{
2052 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
2055 if (len == 0)
2056 return;
2057
2058 if (len < 0)
2059 len = (int)STRLEN(s);
2060
2061 while (len > 0)
2062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (has_mbyte)
2064 {
2065 /* Find out how many chars fit in the current line. */
2066 cells = 0;
2067 for (this_len = 0; this_len < len; )
2068 {
2069 cells += (*mb_ptr2cells)(s + this_len);
2070 if (gui.col + cells > Columns)
2071 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002072 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
2074 if (this_len > len)
2075 this_len = len; /* don't include following composing char */
2076 }
2077 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if (gui.col + len > Columns)
2079 this_len = Columns - gui.col;
2080 else
2081 this_len = len;
2082
2083 (void)gui_outstr_nowrap(s, this_len,
2084 0, (guicolor_T)0, (guicolor_T)0, 0);
2085 s += this_len;
2086 len -= this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 /* fill up for a double-width char that doesn't fit. */
2088 if (len > 0 && gui.col < Columns)
2089 (void)gui_outstr_nowrap((char_u *)" ", 1,
2090 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 /* The cursor may wrap to the next line. */
2092 if (gui.col >= Columns)
2093 {
2094 gui.col = 0;
2095 gui.row++;
2096 }
2097 }
2098}
2099
2100/*
2101 * Output one character (may be one or two display cells).
2102 * Caller must check for valid "off".
2103 * Returns FAIL or OK, just like gui_outstr_nowrap().
2104 */
2105 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002106gui_screenchar(
2107 int off, /* Offset from start of screen */
2108 int flags,
2109 guicolor_T fg, /* colors for cursor */
2110 guicolor_T bg, /* colors for cursor */
2111 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 char_u buf[MB_MAXBYTES + 1];
2114
2115 /* Don't draw right halve of a double-width UTF-8 char. "cannot happen" */
2116 if (enc_utf8 && ScreenLines[off] == 0)
2117 return OK;
2118
2119 if (enc_utf8 && ScreenLinesUC[off] != 0)
2120 /* Draw UTF-8 multi-byte character. */
2121 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2122 flags, fg, bg, back);
2123
2124 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2125 {
2126 buf[0] = ScreenLines[off];
2127 buf[1] = ScreenLines2[off];
2128 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2129 }
2130
2131 /* Draw non-multi-byte character or DBCS character. */
2132 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002133 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135}
2136
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002137#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138/*
2139 * Output the string at the given screen position. This is used in place
2140 * of gui_screenchar() where possible because Pango needs as much context
2141 * as possible to work nicely. It's a lot faster as well.
2142 */
2143 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002144gui_screenstr(
2145 int off, /* Offset from start of screen */
2146 int len, /* string length in screen cells */
2147 int flags,
2148 guicolor_T fg, /* colors for cursor */
2149 guicolor_T bg, /* colors for cursor */
2150 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151{
2152 char_u *buf;
2153 int outlen = 0;
2154 int i;
2155 int retval;
2156
2157 if (len <= 0) /* "cannot happen"? */
2158 return OK;
2159
2160 if (enc_utf8)
2161 {
2162 buf = alloc((unsigned)(len * MB_MAXBYTES + 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 if (ScreenLines[i] == 0)
2169 continue; /* skip second half of double-width char */
2170
2171 if (ScreenLinesUC[i] == 0)
2172 buf[outlen++] = ScreenLines[i];
2173 else
2174 outlen += utfc_char2bytes(i, buf + outlen);
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 if (enc_dbcs == DBCS_JPNU)
2184 {
2185 buf = alloc((unsigned)(len * 2 + 1));
2186 if (buf == NULL)
2187 return OK; /* not much we could do here... */
2188
2189 for (i = off; i < off + len; ++i)
2190 {
2191 buf[outlen++] = ScreenLines[i];
2192
2193 /* handle double-byte single-width char */
2194 if (ScreenLines[i] == 0x8e)
2195 buf[outlen++] = ScreenLines2[i];
2196 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2197 buf[outlen++] = ScreenLines[++i];
2198 }
2199
2200 buf[outlen] = NUL; /* only to aid debugging */
2201 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2202 vim_free(buf);
2203
2204 return retval;
2205 }
2206 else
2207 {
2208 return gui_outstr_nowrap(&ScreenLines[off], len,
2209 flags, fg, bg, back);
2210 }
2211}
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002212#endif /* FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
2214/*
2215 * Output the given string at the current cursor position. If the string is
2216 * too long to fit on the line, then it is truncated.
2217 * "flags":
2218 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2219 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002220 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * background.
2222 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2223 * it.
2224 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2225 * FAIL (the caller should start drawing "back" chars back).
2226 */
2227 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002228gui_outstr_nowrap(
2229 char_u *s,
2230 int len,
2231 int flags,
2232 guicolor_T fg, /* colors for cursor */
2233 guicolor_T bg, /* colors for cursor */
2234 int back) /* backup this many chars when using bold trick */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
2236 long_u highlight_mask;
2237 long_u hl_mask_todo;
2238 guicolor_T fg_color;
2239 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002240 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002241#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002243 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244# ifdef FEAT_XFONTSET
2245 GuiFontset fontset = NOFONTSET;
2246# endif
2247#endif
2248 attrentry_T *aep = NULL;
2249 int draw_flags;
2250 int col = gui.col;
2251#ifdef FEAT_SIGN_ICONS
2252 int draw_sign = FALSE;
2253# ifdef FEAT_NETBEANS_INTG
2254 int multi_sign = FALSE;
2255# endif
2256#endif
2257
2258 if (len < 0)
2259 len = (int)STRLEN(s);
2260 if (len == 0)
2261 return OK;
2262
2263#ifdef FEAT_SIGN_ICONS
2264 if (*s == SIGN_BYTE
2265# ifdef FEAT_NETBEANS_INTG
2266 || *s == MULTISIGN_BYTE
2267# endif
2268 )
2269 {
2270# ifdef FEAT_NETBEANS_INTG
2271 if (*s == MULTISIGN_BYTE)
2272 multi_sign = TRUE;
2273# endif
2274 /* draw spaces instead */
2275 s = (char_u *)" ";
2276 if (len == 1 && col > 0)
2277 --col;
2278 len = 2;
2279 draw_sign = TRUE;
2280 highlight_mask = 0;
2281 }
2282 else
2283#endif
2284 if (gui.highlight_mask > HL_ALL)
2285 {
2286 aep = syn_gui_attr2entry(gui.highlight_mask);
2287 if (aep == NULL) /* highlighting not set */
2288 highlight_mask = 0;
2289 else
2290 highlight_mask = aep->ae_attr;
2291 }
2292 else
2293 highlight_mask = gui.highlight_mask;
2294 hl_mask_todo = highlight_mask;
2295
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002296#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 /* Set the font */
2298 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2299 font = aep->ae_u.gui.font;
2300# ifdef FEAT_XFONTSET
2301 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2302 fontset = aep->ae_u.gui.fontset;
2303# endif
2304 else
2305 {
2306# ifdef FEAT_XFONTSET
2307 if (gui.fontset != NOFONTSET)
2308 fontset = gui.fontset;
2309 else
2310# endif
2311 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2312 {
2313 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2314 {
2315 font = gui.boldital_font;
2316 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2317 }
2318 else if (gui.bold_font != NOFONT)
2319 {
2320 font = gui.bold_font;
2321 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2322 }
2323 else
2324 font = gui.norm_font;
2325 }
2326 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2327 {
2328 font = gui.ital_font;
2329 hl_mask_todo &= ~HL_ITALIC;
2330 }
2331 else
2332 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002333
Bram Moolenaardb250522013-06-17 22:43:25 +02002334 /*
2335 * Choose correct wide_font by font. wide_font should be set with font
2336 * at same time in above block. But it will make many "ifdef" nasty
2337 * blocks. So we do it here.
2338 */
2339 if (font == gui.boldital_font && gui.wide_boldital_font)
2340 wide_font = gui.wide_boldital_font;
2341 else if (font == gui.bold_font && gui.wide_bold_font)
2342 wide_font = gui.wide_bold_font;
2343 else if (font == gui.ital_font && gui.wide_ital_font)
2344 wide_font = gui.wide_ital_font;
2345 else if (font == gui.norm_font && gui.wide_font)
2346 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
2348# ifdef FEAT_XFONTSET
2349 if (fontset != NOFONTSET)
2350 gui_mch_set_fontset(fontset);
2351 else
2352# endif
2353 gui_mch_set_font(font);
2354#endif
2355
2356 draw_flags = 0;
2357
2358 /* Set the color */
2359 bg_color = gui.back_pixel;
2360 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2361 {
2362 draw_flags |= DRAW_CURSOR;
2363 fg_color = fg;
2364 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002365 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
2367 else if (aep != NULL)
2368 {
2369 fg_color = aep->ae_u.gui.fg_color;
2370 if (fg_color == INVALCOLOR)
2371 fg_color = gui.norm_pixel;
2372 bg_color = aep->ae_u.gui.bg_color;
2373 if (bg_color == INVALCOLOR)
2374 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002375 sp_color = aep->ae_u.gui.sp_color;
2376 if (sp_color == INVALCOLOR)
2377 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 }
2379 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002380 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002382 sp_color = fg_color;
2383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2386 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002387#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 gui_mch_set_colors(bg_color, fg_color);
2389#else
2390 gui_mch_set_fg_color(bg_color);
2391 gui_mch_set_bg_color(fg_color);
2392#endif
2393 }
2394 else
2395 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002396#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 gui_mch_set_colors(fg_color, bg_color);
2398#else
2399 gui_mch_set_fg_color(fg_color);
2400 gui_mch_set_bg_color(bg_color);
2401#endif
2402 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002403 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
2405 /* Clear the selection if we are about to write over it */
2406 if (!(flags & GUI_MON_NOCLEAR))
2407 clip_may_clear_selection(gui.row, gui.row);
2408
2409
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 /* If there's no bold font, then fake it */
2411 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2412 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
2414 /*
2415 * When drawing bold or italic characters the spill-over from the left
2416 * neighbor may be destroyed. Let the caller backup to start redrawing
2417 * just after a blank.
2418 */
2419 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2420 return FAIL;
2421
Bram Moolenaare60acc12011-05-10 16:41:25 +02002422#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 /* If there's no italic font, then fake it.
2424 * For GTK2, we don't need a different font for italic style. */
2425 if (hl_mask_todo & HL_ITALIC)
2426 draw_flags |= DRAW_ITALIC;
2427
2428 /* Do we underline the text? */
2429 if (hl_mask_todo & HL_UNDERLINE)
2430 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002431
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432#else
2433 /* Do we underline the text? */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 draw_flags |= DRAW_UNDERL;
2436#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00002437 /* Do we undercurl the text? */
2438 if (hl_mask_todo & HL_UNDERCURL)
2439 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002441 /* Do we strikethrough the text? */
2442 if (hl_mask_todo & HL_STRIKETHROUGH)
2443 draw_flags |= DRAW_STRIKE;
2444
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002445 /* Do we draw transparently? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 if (flags & GUI_MON_TRS_CURSOR)
2447 draw_flags |= DRAW_TRANSP;
2448
2449 /*
2450 * Draw the text.
2451 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002452#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 /* The value returned is the length in display cells */
2454 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2455#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (enc_utf8)
2457 {
2458 int start; /* index of bytes to be drawn */
2459 int cells; /* cellwidth of bytes to be drawn */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002460 int thislen; /* length of bytes to be drawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 int cn; /* cellwidth of current char */
2462 int i; /* index of current char */
2463 int c; /* current char value */
2464 int cl; /* byte length of current char */
2465 int comping; /* current char is composing */
2466 int scol = col; /* screen column */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002467 int curr_wide = FALSE; /* use 'guifontwide' */
Bram Moolenaar45933962013-01-23 17:43:57 +01002468 int prev_wide = FALSE;
2469 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002470# ifdef MSWIN
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002471 int sep_comp = FALSE; /* Don't separate composing chars. */
Bram Moolenaar13505972019-01-24 15:04:48 +01002472# else
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002473 int sep_comp = TRUE; /* Separate composing chars. */
Bram Moolenaar13505972019-01-24 15:04:48 +01002474# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 /* Break the string at a composing character, it has to be drawn on
2477 * top of the previous character. */
2478 start = 0;
2479 cells = 0;
2480 for (i = 0; i < len; i += cl)
2481 {
2482 c = utf_ptr2char(s + i);
2483 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 comping = utf_iscomposing(c);
2485 if (!comping) /* count cells from non-composing chars */
2486 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002487 if (!comping || sep_comp)
2488 {
2489 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002490# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002491 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002492# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002493 && wide_font != NOFONT)
2494 curr_wide = TRUE;
2495 else
2496 curr_wide = FALSE;
2497 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002498 cl = utf_ptr2len(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 if (cl == 0) /* hit end of string */
2500 len = i + cl; /* len must be wrong "cannot happen" */
2501
Bram Moolenaar45933962013-01-23 17:43:57 +01002502 wide_changed = curr_wide != prev_wide;
2503
2504 /* Print the string so far if it's the last character or there is
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 * a composing character. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002506 if (i + cl >= len || (comping && sep_comp && i > start)
2507 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002508# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002510# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 /* No fontset: At least draw char after wide char at
2512 * right position. */
2513 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002515 )
2516# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 )
2518 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002519 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 thislen = i - start;
2521 else
2522 thislen = i - start + cl;
2523 if (thislen > 0)
2524 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002525 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002526 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2528 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002529 if (prev_wide)
2530 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 start += thislen;
2532 }
2533 scol += cells;
2534 cells = 0;
Bram Moolenaar45933962013-01-23 17:43:57 +01002535 /* Adjust to not draw a character which width is changed
2536 * against with last one. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002537 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002539 scol -= cn;
2540 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
2542
Bram Moolenaar13505972019-01-24 15:04:48 +01002543# if defined(FEAT_GUI_X11)
Bram Moolenaar843ee412004-06-30 16:16:41 +00002544 /* No fontset: draw a space to fill the gap after a wide char
2545 * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002547# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002549# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002550 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2552 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
2555 /* Draw a composing char on top of the previous char. */
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002556 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002558# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002559 /* Carbon ATSUI autodraws composing char over previous char */
2560 gui_mch_draw_string(gui.row, scol, s + i, cl,
2561 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002562# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002563 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2564 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 start = i + cl;
2567 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002568 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570 /* The stuff below assumes "len" is the length in screen columns. */
2571 len = scol - col;
2572 }
2573 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
2575 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 if (enc_dbcs == DBCS_JPNU)
2577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 /* Get the length in display cells, this can be different from the
2579 * number of bytes for "euc-jp". */
Bram Moolenaar72597a52010-07-18 15:31:08 +02002580 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002583#endif /* !FEAT_GUI_GTK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2586 gui.col = col + len;
2587
2588 /* May need to invert it when it's part of the selection. */
2589 if (flags & GUI_MON_NOCLEAR)
2590 clip_may_redraw_selection(gui.row, col, len);
2591
2592 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2593 {
2594 /* Invalidate the old physical cursor position if we wrote over it */
2595 if (gui.cursor_row == gui.row
2596 && gui.cursor_col >= col
2597 && gui.cursor_col < col + len)
2598 gui.cursor_is_valid = FALSE;
2599 }
2600
2601#ifdef FEAT_SIGN_ICONS
2602 if (draw_sign)
2603 /* Draw the sign on top of the spaces. */
2604 gui_mch_drawsign(gui.row, col, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002605# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002606 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 if (multi_sign)
2608 netbeans_draw_multisign_indicator(gui.row);
2609# endif
2610#endif
2611
2612 return OK;
2613}
2614
2615/*
2616 * Un-draw the cursor. Actually this just redraws the character at the given
2617 * position. The character just before it too, for when it was in bold.
2618 */
2619 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002620gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621{
2622 if (gui.cursor_is_valid)
2623 {
2624#ifdef FEAT_HANGULIN
2625 if (composing_hangul
2626 && gui.col == gui.cursor_col && gui.row == gui.cursor_row)
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002627 {
2628 char_u *comp_buf;
2629 int comp_len;
2630
2631 comp_buf = hangul_composing_buffer_get(&comp_len);
2632 if (comp_buf)
2633 {
2634 (void)gui_outstr_nowrap(comp_buf, comp_len,
2635 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR,
2636 gui.norm_pixel, gui.back_pixel, 0);
2637 vim_free(comp_buf);
2638 }
2639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 else
2641 {
2642#endif
2643 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2644 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2645 && gui.cursor_col > 0)
2646 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2647 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
2648#ifdef FEAT_HANGULIN
2649 if (composing_hangul)
2650 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col + 1,
2651 gui.cursor_row, gui.cursor_col + 1, GUI_MON_NOCLEAR);
2652 }
2653#endif
2654 /* Cursor_is_valid is reset when the cursor is undrawn, also reset it
2655 * here in case it wasn't needed to undraw it. */
2656 gui.cursor_is_valid = FALSE;
2657 }
2658}
2659
2660 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002661gui_redraw(
2662 int x,
2663 int y,
2664 int w,
2665 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
2667 int row1, col1, row2, col2;
2668
2669 row1 = Y_2_ROW(y);
2670 col1 = X_2_COL(x);
2671 row2 = Y_2_ROW(y + h - 1);
2672 col2 = X_2_COL(x + w - 1);
2673
2674 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2675
2676 /*
2677 * We may need to redraw the cursor, but don't take it upon us to change
2678 * its location after a scroll.
2679 * (maybe be more strict even and test col too?)
2680 * These things may be outside the update/clipping region and reality may
2681 * not reflect Vims internal ideas if these operations are clipped away.
2682 */
2683 if (gui.row == gui.cursor_row)
2684 gui_update_cursor(TRUE, TRUE);
2685}
2686
2687/*
2688 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2689 * from col1 to col2 (inclusive).
2690 * Return TRUE when the character before the first drawn character has
2691 * different attributes (may have to be redrawn too).
2692 */
2693 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002694gui_redraw_block(
2695 int row1,
2696 int col1,
2697 int row2,
2698 int col2,
2699 int flags) /* flags for gui_outstr_nowrap() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700{
2701 int old_row, old_col;
2702 long_u old_hl_mask;
2703 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002704 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 int idx, len;
2706 int back, nback;
2707 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 /* Don't try to update when ScreenLines is not valid */
2711 if (!screen_cleared || ScreenLines == NULL)
2712 return retval;
2713
2714 /* Don't try to draw outside the shell! */
2715 /* Check everything, strange values may be caused by a big border width */
2716 col1 = check_col(col1);
2717 col2 = check_col(col2);
2718 row1 = check_row(row1);
2719 row2 = check_row(row2);
2720
2721 /* Remember where our cursor was */
2722 old_row = gui.row;
2723 old_col = gui.col;
2724 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 orig_col1 = col1;
2726 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728 for (gui.row = row1; gui.row <= row2; gui.row++)
2729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 /* When only half of a double-wide character is in the block, include
2731 * the other half. */
2732 col1 = orig_col1;
2733 col2 = orig_col2;
2734 off = LineOffset[gui.row];
2735 if (enc_dbcs != 0)
2736 {
2737 if (col1 > 0)
2738 col1 -= dbcs_screen_head_off(ScreenLines + off,
2739 ScreenLines + off + col1);
2740 col2 += dbcs_screen_tail_off(ScreenLines + off,
2741 ScreenLines + off + col2);
2742 }
2743 else if (enc_utf8)
2744 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002745 if (ScreenLines[off + col1] == 0)
2746 {
2747 if (col1 > 0)
2748 --col1;
2749 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002750 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002751 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002752 // Make this IEMSGN when it no longer breaks Travis CI.
2753 vim_snprintf((char *)IObuff, IOSIZE,
2754 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2755 (long)gui.row);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002756 msg((char *)IObuff);
Bram Moolenaar9a853462018-12-07 14:10:37 +01002757 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002758 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002759#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2761 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 gui.col = col1;
2765 off = LineOffset[gui.row] + gui.col;
2766 len = col2 - col1 + 1;
2767
2768 /* Find how many chars back this highlighting starts, or where a space
2769 * is. Needed for when the bold trick is used */
2770 for (back = 0; back < col1; ++back)
2771 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2772 || ScreenLines[off - 1 - back] == ' ')
2773 break;
2774 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2775 && ScreenLines[off - 1] != ' ');
2776
2777 /* Break it up in strings of characters with the same attributes. */
2778 /* Print UTF-8 characters individually. */
2779 while (len > 0)
2780 {
2781 first_attr = ScreenAttrs[off];
2782 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002783#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 if (enc_utf8 && ScreenLinesUC[off] != 0)
2785 {
2786 /* output multi-byte character separately */
2787 nback = gui_screenchar(off, flags,
2788 (guicolor_T)0, (guicolor_T)0, back);
2789 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2790 idx = 2;
2791 else
2792 idx = 1;
2793 }
2794 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2795 {
2796 /* output double-byte, single-width character separately */
2797 nback = gui_screenchar(off, flags,
2798 (guicolor_T)0, (guicolor_T)0, back);
2799 idx = 1;
2800 }
2801 else
2802#endif
2803 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002804#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 for (idx = 0; idx < len; ++idx)
2806 {
2807 if (enc_utf8 && ScreenLines[off + idx] == 0)
2808 continue; /* skip second half of double-width char */
2809 if (ScreenAttrs[off + idx] != first_attr)
2810 break;
2811 }
2812 /* gui_screenstr() takes care of multibyte chars */
2813 nback = gui_screenstr(off, idx, flags,
2814 (guicolor_T)0, (guicolor_T)0, back);
2815#else
2816 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2817 idx++)
2818 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 /* Stop at a multi-byte Unicode character. */
2820 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2821 break;
2822 if (enc_dbcs == DBCS_JPNU)
2823 {
2824 /* Stop at a double-byte single-width char. */
2825 if (ScreenLines[off + idx] == 0x8e)
2826 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002827 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 + off + idx) == 2)
2829 ++idx; /* skip second byte of double-byte char */
2830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 }
2832 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2833 (guicolor_T)0, (guicolor_T)0, back);
2834#endif
2835 }
2836 if (nback == FAIL)
2837 {
2838 /* Must back up to start drawing where a bold or italic word
2839 * starts. */
2840 off -= back;
2841 len += back;
2842 gui.col -= back;
2843 }
2844 else
2845 {
2846 off += idx;
2847 len -= idx;
2848 }
2849 back = 0;
2850 }
2851 }
2852
2853 /* Put the cursor back where it was */
2854 gui.row = old_row;
2855 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002856 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
2858 return retval;
2859}
2860
2861 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002862gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
2864 if (count <= 0)
2865 return;
2866
2867 if (row + count > gui.scroll_region_bot)
2868 /* Scrolled out of region, just blank the lines out */
2869 gui_clear_block(row, gui.scroll_region_left,
2870 gui.scroll_region_bot, gui.scroll_region_right);
2871 else
2872 {
2873 gui_mch_delete_lines(row, count);
2874
2875 /* If the cursor was in the deleted lines it's now gone. If the
2876 * cursor was in the scrolled lines adjust its position. */
2877 if (gui.cursor_row >= row
2878 && gui.cursor_col >= gui.scroll_region_left
2879 && gui.cursor_col <= gui.scroll_region_right)
2880 {
2881 if (gui.cursor_row < row + count)
2882 gui.cursor_is_valid = FALSE;
2883 else if (gui.cursor_row <= gui.scroll_region_bot)
2884 gui.cursor_row -= count;
2885 }
2886 }
2887}
2888
2889 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002890gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891{
2892 if (count <= 0)
2893 return;
2894
2895 if (row + count > gui.scroll_region_bot)
2896 /* Scrolled out of region, just blank the lines out */
2897 gui_clear_block(row, gui.scroll_region_left,
2898 gui.scroll_region_bot, gui.scroll_region_right);
2899 else
2900 {
2901 gui_mch_insert_lines(row, count);
2902
2903 if (gui.cursor_row >= gui.row
2904 && gui.cursor_col >= gui.scroll_region_left
2905 && gui.cursor_col <= gui.scroll_region_right)
2906 {
2907 if (gui.cursor_row <= gui.scroll_region_bot - count)
2908 gui.cursor_row += count;
2909 else if (gui.cursor_row <= gui.scroll_region_bot)
2910 gui.cursor_is_valid = FALSE;
2911 }
2912 }
2913}
2914
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002915#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002916/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002917 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2918 */
2919 static int
2920gui_wait_for_chars_3(
2921 long wtime,
2922 int *interrupted UNUSED,
2923 int ignore_input UNUSED)
2924{
2925 return gui_mch_wait_for_chars(wtime);
2926}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002927#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002928
2929/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002930 * Returns OK if a character was found to be available within the given time,
2931 * or FAIL otherwise.
2932 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002933 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002934gui_wait_for_chars_or_timer(
2935 long wtime,
2936 int *interrupted UNUSED,
2937 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002938{
2939#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002940 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2941 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002942#else
2943 return gui_mch_wait_for_chars(wtime);
2944#endif
2945}
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947/*
2948 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002949 * "wtime" == -1 Wait forever.
2950 * "wtime" == 0 Don't wait.
2951 * "wtime" > 0 Wait wtime milliseconds for a character.
2952 *
2953 * Returns the number of characters read or zero when timed out or interrupted.
2954 * "buf" may be NULL, in which case a non-zero number is returned if characters
2955 * are available.
2956 */
2957 static int
2958gui_wait_for_chars_buf(
2959 char_u *buf,
2960 int maxlen,
2961 long wtime, // don't use "time", MIPS cannot handle it
2962 int tb_change_cnt)
2963{
2964 int retval;
2965
2966#ifdef FEAT_MENU
2967 // If we're going to wait a bit, update the menus and mouse shape for the
2968 // current State.
2969 if (wtime != 0)
2970 gui_update_menus(0);
2971#endif
2972
2973 gui_mch_update();
2974 if (input_available()) // Got char, return immediately
2975 {
2976 if (buf != NULL && !typebuf_changed(tb_change_cnt))
2977 return read_from_input_buf(buf, (long)maxlen);
2978 return 0;
2979 }
2980 if (wtime == 0) // Don't wait for char
2981 return FAIL;
2982
2983 // Before waiting, flush any output to the screen.
2984 gui_mch_flush();
2985
2986 // Blink while waiting for a character.
2987 gui_mch_start_blink();
2988
2989 // Common function to loop until "wtime" is met, while handling timers and
2990 // other callbacks.
2991 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
2992 gui_wait_for_chars_or_timer, NULL);
2993
2994 gui_mch_stop_blink(TRUE);
2995
2996 return retval;
2997}
2998
2999/*
3000 * Wait for a character from the keyboard without actually reading it.
3001 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 * wtime == -1 Wait forever.
3003 * wtime == 0 Don't wait.
3004 * wtime > 0 Wait wtime milliseconds for a character.
3005 * Returns OK if a character was found to be available within the given time,
3006 * or FAIL otherwise.
3007 */
3008 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003009gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003011 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012}
3013
3014/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003015 * Equivalent of mch_inchar() for the GUI.
3016 */
3017 int
3018gui_inchar(
3019 char_u *buf,
3020 int maxlen,
3021 long wtime, /* milli seconds */
3022 int tb_change_cnt)
3023{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003024 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003025}
3026
3027/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003028 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 */
3030 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003031fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032{
3033 p[0] = (char_u)(col / 128 + ' ' + 1);
3034 p[1] = (char_u)(col % 128 + ' ' + 1);
3035 p[2] = (char_u)(row / 128 + ' ' + 1);
3036 p[3] = (char_u)(row % 128 + ' ' + 1);
3037}
3038
3039/*
3040 * Generic mouse support function. Add a mouse event to the input buffer with
3041 * the given properties.
3042 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3043 * MOUSE_X1, MOUSE_X2
3044 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003045 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3046 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 * x, y --- Coordinates of mouse in pixels.
3048 * repeated_click --- TRUE if this click comes only a short time after a
3049 * previous click.
3050 * modifiers --- Bit field which may be any of the following modifiers
3051 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3052 * This function will ignore drag events where the mouse has not moved to a new
3053 * character.
3054 */
3055 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003056gui_send_mouse_event(
3057 int button,
3058 int x,
3059 int y,
3060 int repeated_click,
3061 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 static int prev_row = 0, prev_col = 0;
3064 static int prev_button = -1;
3065 static int num_clicks = 1;
3066 char_u string[10];
3067 enum key_extra button_char;
3068 int row, col;
3069#ifdef FEAT_CLIPBOARD
3070 int checkfor;
3071 int did_clip = FALSE;
3072#endif
3073
3074 /*
3075 * Scrolling may happen at any time, also while a selection is present.
3076 */
3077 switch (button)
3078 {
3079 case MOUSE_X1:
3080 button_char = KE_X1MOUSE;
3081 goto button_set;
3082 case MOUSE_X2:
3083 button_char = KE_X2MOUSE;
3084 goto button_set;
3085 case MOUSE_4:
3086 button_char = KE_MOUSEDOWN;
3087 goto button_set;
3088 case MOUSE_5:
3089 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003090 goto button_set;
3091 case MOUSE_6:
3092 button_char = KE_MOUSELEFT;
3093 goto button_set;
3094 case MOUSE_7:
3095 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096button_set:
3097 {
3098 /* Don't put events in the input queue now. */
3099 if (hold_gui_events)
3100 return;
3101
3102 string[3] = CSI;
3103 string[4] = KS_EXTRA;
3104 string[5] = (int)button_char;
3105
3106 /* Pass the pointer coordinates of the scroll event so that we
3107 * know which window to scroll. */
3108 row = gui_xy2colrow(x, y, &col);
3109 string[6] = (char_u)(col / 128 + ' ' + 1);
3110 string[7] = (char_u)(col % 128 + ' ' + 1);
3111 string[8] = (char_u)(row / 128 + ' ' + 1);
3112 string[9] = (char_u)(row % 128 + ' ' + 1);
3113
3114 if (modifiers == 0)
3115 add_to_input_buf(string + 3, 7);
3116 else
3117 {
3118 string[0] = CSI;
3119 string[1] = KS_MODIFIER;
3120 string[2] = 0;
3121 if (modifiers & MOUSE_SHIFT)
3122 string[2] |= MOD_MASK_SHIFT;
3123 if (modifiers & MOUSE_CTRL)
3124 string[2] |= MOD_MASK_CTRL;
3125 if (modifiers & MOUSE_ALT)
3126 string[2] |= MOD_MASK_ALT;
3127 add_to_input_buf(string, 10);
3128 }
3129 return;
3130 }
3131 }
3132
3133#ifdef FEAT_CLIPBOARD
3134 /* If a clipboard selection is in progress, handle it */
3135 if (clip_star.state == SELECT_IN_PROGRESS)
3136 {
3137 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3138 return;
3139 }
3140
3141 /* Determine which mouse settings to look for based on the current mode */
3142 switch (get_real_state())
3143 {
3144 case NORMAL_BUSY:
3145 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003146# ifdef FEAT_TERMINAL
3147 case TERMINAL:
3148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 case NORMAL: checkfor = MOUSE_NORMAL; break;
3150 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003151 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 case REPLACE:
3153 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 case VREPLACE:
3155 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 case INSERT:
3157 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3158 case ASKMORE:
3159 case HITRETURN: /* At the more- and hit-enter prompt pass the
3160 mouse event for a click on or below the
3161 message line. */
3162 if (Y_2_ROW(y) >= msg_row)
3163 checkfor = MOUSE_NORMAL;
3164 else
3165 checkfor = MOUSE_RETURN;
3166 break;
3167
3168 /*
3169 * On the command line, use the clipboard selection on all lines
3170 * but the command line. But not when pasting.
3171 */
3172 case CMDLINE:
3173 case CMDLINE+LANGMAP:
3174 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3175 checkfor = MOUSE_NONE;
3176 else
3177 checkfor = MOUSE_COMMAND;
3178 break;
3179
3180 default:
3181 checkfor = MOUSE_NONE;
3182 break;
3183 };
3184
3185 /*
3186 * Allow clipboard selection of text on the command line in "normal"
3187 * modes. Don't do this when dragging the status line, or extending a
3188 * Visual selection.
3189 */
3190 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003191 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 && button != MOUSE_DRAG
3193# ifdef FEAT_MOUSESHAPE
3194 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196# endif
3197 )
3198 checkfor = MOUSE_NONE;
3199
3200 /*
3201 * Use modeless selection when holding CTRL and SHIFT pressed.
3202 */
3203 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3204 checkfor = MOUSE_NONEF;
3205
3206 /*
3207 * In Ex mode, always use modeless selection.
3208 */
3209 if (exmode_active)
3210 checkfor = MOUSE_NONE;
3211
3212 /*
3213 * If the mouse settings say to not use the mouse, use the modeless
3214 * selection. But if Visual is active, assume that only the Visual area
3215 * will be selected.
3216 * Exception: On the command line, both the selection is used and a mouse
3217 * key is send.
3218 */
3219 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 /* Don't do modeless selection in Visual mode. */
3222 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3223 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224
3225 /*
3226 * When 'mousemodel' is "popup", shift-left is translated to right.
3227 * But not when also using Ctrl.
3228 */
3229 if (mouse_model_popup() && button == MOUSE_LEFT
3230 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3231 {
3232 button = MOUSE_RIGHT;
3233 modifiers &= ~ MOUSE_SHIFT;
3234 }
3235
3236 /* If the selection is done, allow the right button to extend it.
3237 * If the selection is cleared, allow the right button to start it
3238 * from the cursor position. */
3239 if (button == MOUSE_RIGHT)
3240 {
3241 if (clip_star.state == SELECT_CLEARED)
3242 {
3243 if (State & CMDLINE)
3244 {
3245 col = msg_col;
3246 row = msg_row;
3247 }
3248 else
3249 {
3250 col = curwin->w_wcol;
3251 row = curwin->w_wrow + W_WINROW(curwin);
3252 }
3253 clip_start_selection(col, row, FALSE);
3254 }
3255 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3256 repeated_click);
3257 did_clip = TRUE;
3258 }
3259 /* Allow the left button to start the selection */
Bram Moolenaare60acc12011-05-10 16:41:25 +02003260 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
3262 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3263 did_clip = TRUE;
3264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
3266 /* Always allow pasting */
3267 if (button != MOUSE_MIDDLE)
3268 {
3269 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3270 return;
3271 if (checkfor != MOUSE_COMMAND)
3272 button = MOUSE_LEFT;
3273 }
3274 repeated_click = FALSE;
3275 }
3276
3277 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003278 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#endif
3280
3281 /* Don't put events in the input queue now. */
3282 if (hold_gui_events)
3283 return;
3284
3285 row = gui_xy2colrow(x, y, &col);
3286
3287 /*
3288 * If we are dragging and the mouse hasn't moved far enough to be on a
3289 * different character, then don't send an event to vim.
3290 */
3291 if (button == MOUSE_DRAG)
3292 {
3293 if (row == prev_row && col == prev_col)
3294 return;
3295 /* Dragging above the window, set "row" to -1 to cause a scroll. */
3296 if (y < 0)
3297 row = -1;
3298 }
3299
3300 /*
3301 * If topline has changed (window scrolled) since the last click, reset
3302 * repeated_click, because we don't want starting Visual mode when
3303 * clicking on a different character in the text.
3304 */
3305 if (curwin->w_topline != gui_prev_topline
3306#ifdef FEAT_DIFF
3307 || curwin->w_topfill != gui_prev_topfill
3308#endif
3309 )
3310 repeated_click = FALSE;
3311
3312 string[0] = CSI; /* this sequence is recognized by check_termcode() */
3313 string[1] = KS_MOUSE;
3314 string[2] = KE_FILLER;
3315 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3316 {
3317 if (repeated_click)
3318 {
3319 /*
3320 * Handle multiple clicks. They only count if the mouse is still
3321 * pointing at the same character.
3322 */
3323 if (button != prev_button || row != prev_row || col != prev_col)
3324 num_clicks = 1;
3325 else if (++num_clicks > 4)
3326 num_clicks = 1;
3327 }
3328 else
3329 num_clicks = 1;
3330 prev_button = button;
3331 gui_prev_topline = curwin->w_topline;
3332#ifdef FEAT_DIFF
3333 gui_prev_topfill = curwin->w_topfill;
3334#endif
3335
3336 string[3] = (char_u)(button | 0x20);
3337 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3338 }
3339 else
3340 string[3] = (char_u)button;
3341
3342 string[3] |= modifiers;
3343 fill_mouse_coord(string + 4, col, row);
3344 add_to_input_buf(string, 8);
3345
3346 if (row < 0)
3347 prev_row = 0;
3348 else
3349 prev_row = row;
3350 prev_col = col;
3351
3352 /*
3353 * We need to make sure this is cleared since Athena doesn't tell us when
3354 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3355 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003356#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 gui.dragged_sb = SBAR_NONE;
3358#endif
3359}
3360
3361/*
3362 * Convert x and y coordinate to column and row in text window.
3363 * Corrects for multi-byte character.
3364 * returns column in "*colp" and row as return value;
3365 */
3366 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003367gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 int col = check_col(X_2_COL(x));
3370 int row = check_row(Y_2_ROW(y));
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 return row;
3374}
3375
3376#if defined(FEAT_MENU) || defined(PROTO)
3377/*
3378 * Callback function for when a menu entry has been selected.
3379 */
3380 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003381gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003383 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
3385 /* Don't put events in the input queue now. */
3386 if (hold_gui_events)
3387 return;
3388
3389 bytes[0] = CSI;
3390 bytes[1] = KS_MENU;
3391 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003392 add_to_input_buf(bytes, 3);
3393 add_long_to_buf((long_u)menu, bytes);
3394 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395}
3396#endif
3397
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003398static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400/*
3401 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003402 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 * in p_go.
3404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003406gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#ifdef FEAT_MENU
3409 static int prev_menu_is_active = -1;
3410#endif
3411#ifdef FEAT_TOOLBAR
3412 static int prev_toolbar = -1;
3413 int using_toolbar = FALSE;
3414#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003415#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003416 int using_tabline;
3417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418#ifdef FEAT_FOOTER
3419 static int prev_footer = -1;
3420 int using_footer = FALSE;
3421#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003422#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 static int prev_tearoff = -1;
3424 int using_tearoff = FALSE;
3425#endif
3426
3427 char_u *p;
3428 int i;
3429#ifdef FEAT_MENU
3430 int grey_old, grey_new;
3431 char_u *temp;
3432#endif
3433 win_T *wp;
3434 int need_set_size;
3435 int fix_size;
3436
3437#ifdef FEAT_MENU
3438 if (oldval != NULL && gui.in_use)
3439 {
3440 /*
3441 * Check if the menu's go from grey to non-grey or vise versa.
3442 */
3443 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3444 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3445 if (grey_old != grey_new)
3446 {
3447 temp = p_go;
3448 p_go = oldval;
3449 gui_update_menus(MENU_ALL_MODES);
3450 p_go = temp;
3451 }
3452 }
3453 gui.menu_is_active = FALSE;
3454#endif
3455
3456 for (i = 0; i < 3; i++)
3457 gui.which_scrollbars[i] = FALSE;
3458 for (p = p_go; *p; p++)
3459 switch (*p)
3460 {
3461 case GO_LEFT:
3462 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3463 break;
3464 case GO_RIGHT:
3465 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3466 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 case GO_VLEFT:
3468 if (win_hasvertsplit())
3469 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3470 break;
3471 case GO_VRIGHT:
3472 if (win_hasvertsplit())
3473 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3474 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 case GO_BOT:
3476 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3477 break;
3478#ifdef FEAT_MENU
3479 case GO_MENUS:
3480 gui.menu_is_active = TRUE;
3481 break;
3482#endif
3483 case GO_GREY:
3484 /* make menu's have grey items, ignored here */
3485 break;
3486#ifdef FEAT_TOOLBAR
3487 case GO_TOOLBAR:
3488 using_toolbar = TRUE;
3489 break;
3490#endif
3491#ifdef FEAT_FOOTER
3492 case GO_FOOTER:
3493 using_footer = TRUE;
3494 break;
3495#endif
3496 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003497#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 using_tearoff = TRUE;
3499#endif
3500 break;
3501 default:
3502 /* Ignore options that are not supported */
3503 break;
3504 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (gui.in_use)
3507 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003508 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003510
3511#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003512 /* Update the GUI tab line, it may appear or disappear. This may
3513 * cause the non-GUI tab line to disappear or appear. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003514 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003515 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003516 {
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003517 /* We don't want a resize event change "Rows" here, save and
3518 * restore it. Resizing is handled below. */
3519 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003520 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003521 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003522 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003523 if (using_tabline)
3524 fix_size = TRUE;
3525 if (!gui_use_tabline())
3526 redraw_tabline = TRUE; /* may draw non-GUI tab line */
3527 }
3528#endif
3529
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 for (i = 0; i < 3; i++)
3531 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003532 /* The scrollbar needs to be updated when it is shown/unshown and
3533 * when switching tab pages. But the size only changes when it's
3534 * shown/unshown. Thus we need two places to remember whether a
3535 * scrollbar is there or not. */
3536 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003537 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003538 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
3540 if (i == SBAR_BOTTOM)
3541 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3542 gui.which_scrollbars[i]);
3543 else
3544 {
3545 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003548 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3549 {
3550 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003551 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003552 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003553 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003554 if (gui.which_scrollbars[i])
3555 fix_size = TRUE;
3556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003558 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003559 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
3561
3562#ifdef FEAT_MENU
3563 if (gui.menu_is_active != prev_menu_is_active)
3564 {
3565 /* We don't want a resize event change "Rows" here, save and
3566 * restore it. Resizing is handled below. */
3567 i = Rows;
3568 gui_mch_enable_menu(gui.menu_is_active);
3569 Rows = i;
3570 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003571 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 if (gui.menu_is_active)
3573 fix_size = TRUE;
3574 }
3575#endif
3576
3577#ifdef FEAT_TOOLBAR
3578 if (using_toolbar != prev_toolbar)
3579 {
3580 gui_mch_show_toolbar(using_toolbar);
3581 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003582 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (using_toolbar)
3584 fix_size = TRUE;
3585 }
3586#endif
3587#ifdef FEAT_FOOTER
3588 if (using_footer != prev_footer)
3589 {
3590 gui_mch_enable_footer(using_footer);
3591 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003592 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 if (using_footer)
3594 fix_size = TRUE;
3595 }
3596#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003597#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 if (using_tearoff != prev_tearoff)
3599 {
3600 gui_mch_toggle_tearoffs(using_tearoff);
3601 prev_tearoff = using_tearoff;
3602 }
3603#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003604 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003605 {
3606#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003607 long prev_Columns = Columns;
3608 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 /* Adjust the size of the window to make the text area keep the
3611 * same size and to avoid that part of our window is off-screen
3612 * and a scrollbar can't be used, for example. */
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003613 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003614
3615#ifdef FEAT_GUI_GTK
3616 /* GTK has the annoying habit of sending us resize events when
3617 * changing the window size ourselves. This mostly happens when
3618 * waiting for a character to arrive, quite unpredictably, and may
3619 * change Columns and Rows when we don't want it. Wait for a
3620 * character here to avoid this effect.
3621 * If you remove this, please test this command for resizing
Bram Moolenaara04f10b2005-05-31 22:09:46 +00003622 * effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003623 * Don't do this while starting up though.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003624 * Don't change Rows when adding menu/toolbar/tabline.
3625 * Don't change Columns when adding vertical toolbar. */
3626 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003627 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003628 if ((need_set_size & RESIZE_VERT) == 0)
3629 Rows = prev_Rows;
3630 if ((need_set_size & RESIZE_HOR) == 0)
3631 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003632#endif
3633 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003634 /* When the console tabline appears or disappears the window positions
3635 * change. */
3636 if (firstwin->w_winrow != tabline_height())
3637 shell_new_rows(); /* recompute window positions and heights */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
3639}
3640
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003641#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3642/*
3643 * Return TRUE if the GUI is taking care of the tabline.
3644 * It may still be hidden if 'showtabline' is zero.
3645 */
3646 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003647gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003648{
3649 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3650}
3651
3652/*
3653 * Return TRUE if the GUI is showing the tabline.
3654 * This uses 'showtabline'.
3655 */
3656 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003657gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003658{
3659 if (!gui_use_tabline()
3660 || p_stal == 0
3661 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3662 return FALSE;
3663 return TRUE;
3664}
3665
3666/*
3667 * Update the tabline.
3668 * This may display/undisplay the tabline and update the labels.
3669 */
3670 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003671gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003672{
3673 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003674 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003675
3676 if (!gui.starting && starting == 0)
3677 {
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003678 /* Updating the tabline uses direct GUI commands, flush
3679 * outstanding instructions first. (esp. clear screen) */
3680 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003681
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003682 if (!showit != !shown)
3683 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003684 if (showit != 0)
3685 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003686
3687 /* When the tabs change from hidden to shown or from shown to
3688 * hidden the size of the text area should remain the same. */
3689 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003690 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003691 }
3692}
3693
3694/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003695 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003696 */
3697 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003698get_tabline_label(
3699 tabpage_T *tp,
3700 int tooltip) /* TRUE: get tooltip */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003701{
3702 int modified = FALSE;
3703 char_u buf[40];
3704 int wincount;
3705 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003706 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003707
Bram Moolenaar57657d82006-04-21 22:12:41 +00003708 /* Use 'guitablabel' or 'guitabtooltip' if it's set. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003709 opt = (tooltip ? &p_gtt : &p_gtl);
3710 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003711 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003712 int use_sandbox = FALSE;
3713 int save_called_emsg = called_emsg;
3714 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003715 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003716 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3717 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003718
3719 called_emsg = FALSE;
3720
3721 printer_page_num = tabpage_index(tp);
3722# ifdef FEAT_EVAL
3723 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003724 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003725# endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003726 /* It's almost as going to the tabpage, but without autocommands. */
3727 curtab->tp_firstwin = firstwin;
3728 curtab->tp_lastwin = lastwin;
3729 curtab->tp_curwin = curwin;
3730 save_curtab = curtab;
3731 curtab = tp;
3732 topframe = curtab->tp_topframe;
3733 firstwin = curtab->tp_firstwin;
3734 lastwin = curtab->tp_lastwin;
3735 curwin = curtab->tp_curwin;
3736 curbuf = curwin->w_buffer;
3737
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003738 /* Can't use NameBuff directly, build_stl_str_hl() uses it. */
Bram Moolenaard68071d2006-05-02 22:08:30 +00003739 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003740 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003741 STRCPY(NameBuff, res);
3742
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003743 /* Back to the original curtab. */
3744 curtab = save_curtab;
3745 topframe = curtab->tp_topframe;
3746 firstwin = curtab->tp_firstwin;
3747 lastwin = curtab->tp_lastwin;
3748 curwin = curtab->tp_curwin;
3749 curbuf = curwin->w_buffer;
3750
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003751 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003752 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003753 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003754 called_emsg |= save_called_emsg;
3755 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003756
3757 /* If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3758 * use a default label. */
3759 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003760 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003761 /* Get the buffer name into NameBuff[] and shorten it. */
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003762 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003763 if (!tooltip)
3764 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003765
3766 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3767 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3768 if (bufIsChanged(wp->w_buffer))
3769 modified = TRUE;
3770 if (modified || wincount > 1)
3771 {
3772 if (wincount > 1)
3773 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3774 else
3775 buf[0] = NUL;
3776 if (modified)
3777 STRCAT(buf, "+");
3778 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003779 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003780 mch_memmove(NameBuff, buf, STRLEN(buf));
3781 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003782 }
3783}
3784
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003785/*
3786 * Send the event for clicking to select tab page "nr".
3787 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003788 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003789 */
3790 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003791send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003792{
3793 char_u string[3];
3794
3795 if (nr == tabpage_index(curtab))
3796 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003797
3798 /* Don't put events in the input queue now. */
3799 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003800# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003801 || cmdwin_type != 0
3802# endif
3803 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003804 {
3805 /* Set it back to the current tab page. */
3806 gui_mch_set_curtab(tabpage_index(curtab));
3807 return FALSE;
3808 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003809
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003810 string[0] = CSI;
3811 string[1] = KS_TABLINE;
3812 string[2] = KE_FILLER;
3813 add_to_input_buf(string, 3);
3814 string[0] = nr;
3815 add_to_input_buf_csi(string, 1);
3816 return TRUE;
3817}
3818
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003819/*
3820 * Send a tabline menu event
3821 */
3822 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003823send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003824{
3825 char_u string[3];
3826
Bram Moolenaar29547192018-12-11 20:39:19 +01003827 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003828 if (hold_gui_events)
3829 return;
3830
Bram Moolenaar29547192018-12-11 20:39:19 +01003831 // Cannot close the last tabpage.
3832 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3833 return;
3834
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003835 string[0] = CSI;
3836 string[1] = KS_TABMENU;
3837 string[2] = KE_FILLER;
3838 add_to_input_buf(string, 3);
3839 string[0] = tabidx;
3840 string[1] = (char_u)(long)event;
3841 add_to_input_buf_csi(string, 2);
3842}
3843
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845
3846/*
3847 * Scrollbar stuff:
3848 */
3849
Bram Moolenaare45828b2006-02-15 22:12:56 +00003850/*
3851 * Remove all scrollbars. Used before switching to another tab page.
3852 */
3853 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003854gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003855{
3856 int i;
3857 win_T *wp;
3858
3859 for (i = 0; i < 3; i++)
3860 {
3861 if (i == SBAR_BOTTOM)
3862 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3863 else
3864 {
3865 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003866 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003867 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003868 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003869 }
3870}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003871
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003873gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874{
3875 static int sbar_ident = 0;
3876
3877 sb->ident = sbar_ident++; /* No check for too big, but would it happen? */
3878 sb->wp = wp;
3879 sb->type = type;
3880 sb->value = 0;
3881#ifdef FEAT_GUI_ATHENA
3882 sb->pixval = 0;
3883#endif
3884 sb->size = 1;
3885 sb->max = 1;
3886 sb->top = 0;
3887 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 sb->status_height = 0;
3890 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3891}
3892
3893/*
3894 * Find the scrollbar with the given index.
3895 */
3896 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003897gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898{
3899 win_T *wp;
3900
3901 if (gui.bottom_sbar.ident == ident)
3902 return &gui.bottom_sbar;
3903 FOR_ALL_WINDOWS(wp)
3904 {
3905 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3906 return &wp->w_scrollbars[SBAR_LEFT];
3907 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3908 return &wp->w_scrollbars[SBAR_RIGHT];
3909 }
3910 return NULL;
3911}
3912
3913/*
3914 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3915 *
3916 * For Win32, Macintosh and GTK+ 2:
3917 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3918 * you stop dragging the scrollbar. We get here each time the scrollbar is
3919 * dragged another pixel, but as far as the rest of vim goes, it thinks
3920 * we're just hanging in the call to DispatchMessage() in
3921 * process_message(). The DispatchMessage() call that hangs was passed a
3922 * mouse button click event in the scrollbar window. -- webb.
3923 *
3924 * Solution: Do the scrolling right here. But only when allowed.
3925 * Ignore the scrollbars while executing an external command or when there
3926 * are still characters to be processed.
3927 */
3928 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003929gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 int sb_num;
3933#ifdef USE_ON_FLY_SCROLL
3934 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936# ifdef FEAT_DIFF
3937 int old_topfill = curwin->w_topfill;
3938# endif
3939#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003940 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 int byte_count;
3942#endif
3943
3944 if (sb == NULL)
3945 return;
3946
3947 /* Don't put events in the input queue now. */
3948 if (hold_gui_events)
3949 return;
3950
3951#ifdef FEAT_CMDWIN
3952 if (cmdwin_type != 0 && sb->wp != curwin)
3953 return;
3954#endif
3955
3956 if (still_dragging)
3957 {
3958 if (sb->wp == NULL)
3959 gui.dragged_sb = SBAR_BOTTOM;
3960 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3961 gui.dragged_sb = SBAR_LEFT;
3962 else
3963 gui.dragged_sb = SBAR_RIGHT;
3964 gui.dragged_wp = sb->wp;
3965 }
3966 else
3967 {
3968 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003969#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 /* Keep the "dragged_wp" value until after the scrolling, for when the
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003971 * mouse button is released. GTK2 doesn't send the button-up event. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 gui.dragged_wp = NULL;
3973#endif
3974 }
3975
3976 /* Vertical sbar info is kept in the first sbar (the left one) */
3977 if (sb->wp != NULL)
3978 sb = &sb->wp->w_scrollbars[0];
3979
3980 /*
3981 * Check validity of value
3982 */
3983 if (value < 0)
3984 value = 0;
3985#ifdef SCROLL_PAST_END
3986 else if (value > sb->max)
3987 value = sb->max;
3988#else
3989 if (value > sb->max - sb->size + 1)
3990 value = sb->max - sb->size + 1;
3991#endif
3992
3993 sb->value = value;
3994
3995#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar67840782008-01-03 15:15:07 +00003996 /* When not allowed to do the scrolling right now, return.
3997 * This also checked input_available(), but that causes the first click in
3998 * a scrollbar to be ignored when Vim doesn't have focus. */
3999 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 return;
4001#endif
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004002#ifdef FEAT_INS_EXPAND
4003 /* Disallow scrolling the current window when the completion popup menu is
4004 * visible. */
4005 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4006 return;
4007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009#ifdef FEAT_RIGHTLEFT
4010 if (sb->wp == NULL && curwin->w_p_rl)
4011 {
4012 value = sb->max + 1 - sb->size - value;
4013 if (value < 0)
4014 value = 0;
4015 }
4016#endif
4017
4018 if (sb->wp != NULL) /* vertical scrollbar */
4019 {
4020 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4022 sb_num++;
4023 if (wp == NULL)
4024 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
4026#ifdef USE_ON_FLY_SCROLL
4027 current_scrollbar = sb_num;
4028 scrollbar_value = value;
4029 if (State & NORMAL)
4030 {
4031 gui_do_scroll();
4032 setcursor();
4033 }
4034 else if (State & INSERT)
4035 {
4036 ins_scroll();
4037 setcursor();
4038 }
4039 else if (State & CMDLINE)
4040 {
4041 if (msg_scrolled == 0)
4042 {
4043 gui_do_scroll();
4044 redrawcmdline();
4045 }
4046 }
4047# ifdef FEAT_FOLDING
4048 /* Value may have been changed for closed fold. */
4049 sb->value = sb->wp->w_topline - 1;
4050# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004051
4052 /* When dragging one scrollbar and there is another one at the other
4053 * side move the thumb of that one too. */
4054 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4055 gui_mch_set_scrollbar_thumb(
4056 &sb->wp->w_scrollbars[
4057 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4058 ? SBAR_LEFT : SBAR_RIGHT],
4059 sb->value, sb->size, sb->max);
4060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061#else
4062 bytes[0] = CSI;
4063 bytes[1] = KS_VER_SCROLLBAR;
4064 bytes[2] = KE_FILLER;
4065 bytes[3] = (char_u)sb_num;
4066 byte_count = 4;
4067#endif
4068 }
4069 else
4070 {
4071#ifdef USE_ON_FLY_SCROLL
4072 scrollbar_value = value;
4073
4074 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004075 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 else if (State & INSERT)
4077 ins_horscroll();
4078 else if (State & CMDLINE)
4079 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004080 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004082 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 redrawcmdline();
4084 }
4085 }
4086 if (old_leftcol != curwin->w_leftcol)
4087 {
4088 updateWindow(curwin); /* update window, status and cmdline */
4089 setcursor();
4090 }
4091#else
4092 bytes[0] = CSI;
4093 bytes[1] = KS_HOR_SCROLLBAR;
4094 bytes[2] = KE_FILLER;
4095 byte_count = 3;
4096#endif
4097 }
4098
4099#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 /*
4101 * synchronize other windows, as necessary according to 'scrollbind'
4102 */
4103 if (curwin->w_p_scb
4104 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4105 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004106# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 ))))
4110 {
4111 do_check_scrollbind(TRUE);
4112 /* need to update the window right here */
Bram Moolenaar29323592016-07-24 22:04:11 +02004113 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 if (wp->w_redr_type > 0)
4115 updateWindow(wp);
4116 setcursor();
4117 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004118 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004120 add_to_input_buf(bytes, byte_count);
4121 add_long_to_buf((long_u)value, bytes);
4122 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123#endif
4124}
4125
4126/*
4127 * Scrollbar stuff:
4128 */
4129
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004130/*
4131 * Called when something in the window layout has changed.
4132 */
4133 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004134gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004135{
4136 if (gui.in_use && starting == 0)
4137 {
4138 out_flush();
4139 gui_init_which_components(NULL);
4140 gui_update_scrollbars(TRUE);
4141 }
4142 need_mouse_correct = TRUE;
4143}
4144
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004146gui_update_scrollbars(
4147 int force) /* Force all scrollbars to get updated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148{
4149 win_T *wp;
4150 scrollbar_T *sb;
4151 long val, size, max; /* need 32 bits here */
4152 int which_sb;
4153 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 /* Update the horizontal scrollbar */
4157 gui_update_horiz_scrollbar(force);
4158
Bram Moolenaar4f974752019-02-17 17:44:42 +01004159#ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 /* Return straight away if there is neither a left nor right scrollbar.
4161 * On MS-Windows this is required anyway for scrollwheel messages. */
4162 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4163 return;
4164#endif
4165
4166 /*
4167 * Don't want to update a scrollbar while we're dragging it. But if we
4168 * have both a left and right scrollbar, and we drag one of them, we still
4169 * need to update the other one.
4170 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004171 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4172 && gui.which_scrollbars[SBAR_LEFT]
4173 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
4175 /*
4176 * If we have two scrollbars and one of them is being dragged, just
4177 * copy the scrollbar position from the dragged one to the other one.
4178 */
4179 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4180 if (gui.dragged_wp != NULL)
4181 gui_mch_set_scrollbar_thumb(
4182 &gui.dragged_wp->w_scrollbars[which_sb],
4183 gui.dragged_wp->w_scrollbars[0].value,
4184 gui.dragged_wp->w_scrollbars[0].size,
4185 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187
4188 /* avoid that moving components around generates events */
4189 ++hold_gui_events;
4190
Bram Moolenaar45a24952016-07-24 22:25:15 +02004191 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 {
4193 if (wp->w_buffer == NULL) /* just in case */
4194 continue;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004195 /* Skip a scrollbar that is being dragged. */
4196 if (!force && (gui.dragged_sb == SBAR_LEFT
4197 || gui.dragged_sb == SBAR_RIGHT)
4198 && gui.dragged_wp == wp)
4199 continue;
4200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201#ifdef SCROLL_PAST_END
4202 max = wp->w_buffer->b_ml.ml_line_count - 1;
4203#else
4204 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4205#endif
4206 if (max < 0) /* empty buffer */
4207 max = 0;
4208 val = wp->w_topline - 1;
4209 size = wp->w_height;
4210#ifdef SCROLL_PAST_END
4211 if (val > max) /* just in case */
4212 val = max;
4213#else
4214 if (size > max + 1) /* just in case */
4215 size = max + 1;
4216 if (val > max - size + 1)
4217 val = max - size + 1;
4218#endif
4219 if (val < 0) /* minimal value is 0 */
4220 val = 0;
4221
4222 /*
4223 * Scrollbar at index 0 (the left one) contains all the information.
4224 * It would be the same info for left and right so we just store it for
4225 * one of them.
4226 */
4227 sb = &wp->w_scrollbars[0];
4228
4229 /*
4230 * Note: no check for valid w_botline. If it's not valid the
4231 * scrollbars will be updated later anyway.
4232 */
4233 if (size < 1 || wp->w_botline - 2 > max)
4234 {
4235 /*
4236 * This can happen during changing files. Just don't update the
4237 * scrollbar for now.
4238 */
4239 sb->height = 0; /* Force update next time */
4240 if (gui.which_scrollbars[SBAR_LEFT])
4241 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4242 if (gui.which_scrollbars[SBAR_RIGHT])
4243 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4244 continue;
4245 }
4246 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 || sb->top != wp->w_winrow
4248 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004250 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
4252 /* Height, width or position of scrollbar has changed. For
4253 * vertical split: curwin changed. */
4254 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 sb->top = wp->w_winrow;
4256 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 /* Calculate height and position in pixels */
4260 h = (sb->height + sb->status_height) * gui.char_height;
4261 y = sb->top * gui.char_height + gui.border_offset;
4262#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4263 if (gui.menu_is_active)
4264 y += gui.menu_height;
4265#endif
4266
4267#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4268 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4269# ifdef FEAT_GUI_ATHENA
4270 y += gui.toolbar_height;
4271# else
4272# ifdef FEAT_GUI_MSWIN
4273 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4274# endif
4275# endif
4276#endif
4277
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004278#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4279 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004280 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004281#endif
4282
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
4285 /* Height of top scrollbar includes width of top border */
4286 h += gui.border_offset;
4287 y -= gui.border_offset;
4288 }
4289 if (gui.which_scrollbars[SBAR_LEFT])
4290 {
4291 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4292 gui.left_sbar_x, y,
4293 gui.scrollbar_width, h);
4294 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4295 }
4296 if (gui.which_scrollbars[SBAR_RIGHT])
4297 {
4298 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4299 gui.right_sbar_x, y,
4300 gui.scrollbar_width, h);
4301 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4302 }
4303 }
4304
4305 /* Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4306 * checking if the thumb moved at least a pixel. Only do this for
4307 * Athena, most other GUIs require the update anyway to make the
4308 * arrows work. */
4309#ifdef FEAT_GUI_ATHENA
4310 if (max == 0)
4311 y = 0;
4312 else
4313 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4314 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4315#else
4316 if (force || sb->value != val || sb->size != size || sb->max != max)
4317#endif
4318 {
4319 /* Thumb of scrollbar has moved */
4320 sb->value = val;
4321#ifdef FEAT_GUI_ATHENA
4322 sb->pixval = y;
4323#endif
4324 sb->size = size;
4325 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004326 if (gui.which_scrollbars[SBAR_LEFT]
4327 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4329 val, size, max);
4330 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004331 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4333 val, size, max);
4334 }
4335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 --hold_gui_events;
4338}
4339
4340/*
4341 * Enable or disable a scrollbar.
4342 * Check for scrollbars for vertically split windows which are not enabled
4343 * sometimes.
4344 */
4345 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004346gui_do_scrollbar(
4347 win_T *wp,
4348 int which, /* SBAR_LEFT or SBAR_RIGHT */
4349 int enable) /* TRUE to enable scrollbar */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 int midcol = curwin->w_wincol + curwin->w_width / 2;
4352 int has_midcol = (wp->w_wincol <= midcol
4353 && wp->w_wincol + wp->w_width >= midcol);
4354
4355 /* Only enable scrollbars that contain the middle column of the current
4356 * window. */
4357 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4358 {
4359 /* Scrollbars only on one side. Don't enable scrollbars that don't
4360 * contain the middle column of the current window. */
4361 if (!has_midcol)
4362 enable = FALSE;
4363 }
4364 else
4365 {
4366 /* Scrollbars on both sides. Don't enable scrollbars that neither
4367 * contain the middle column of the current window nor are on the far
4368 * side. */
4369 if (midcol > Columns / 2)
4370 {
4371 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4372 enable = FALSE;
4373 }
4374 else
4375 {
4376 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4377 : !has_midcol)
4378 enable = FALSE;
4379 }
4380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4382}
4383
4384/*
4385 * Scroll a window according to the values set in the globals current_scrollbar
4386 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4387 * or FALSE otherwise.
4388 */
4389 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004390gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391{
4392 win_T *wp, *save_wp;
4393 int i;
4394 long nlines;
4395 pos_T old_cursor;
4396 linenr_T old_topline;
4397#ifdef FEAT_DIFF
4398 int old_topfill;
4399#endif
4400
4401 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4402 if (wp == NULL)
4403 break;
4404 if (wp == NULL)
4405 /* Couldn't find window */
4406 return FALSE;
4407
4408 /*
4409 * Compute number of lines to scroll. If zero, nothing to do.
4410 */
4411 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4412 if (nlines == 0)
4413 return FALSE;
4414
4415 save_wp = curwin;
4416 old_topline = wp->w_topline;
4417#ifdef FEAT_DIFF
4418 old_topfill = wp->w_topfill;
4419#endif
4420 old_cursor = wp->w_cursor;
4421 curwin = wp;
4422 curbuf = wp->w_buffer;
4423 if (nlines < 0)
4424 scrolldown(-nlines, gui.dragged_wp == NULL);
4425 else
4426 scrollup(nlines, gui.dragged_wp == NULL);
4427 /* Reset dragged_wp after using it. "dragged_sb" will have been reset for
4428 * the mouse-up event already, but we still want it to behave like when
4429 * dragging. But not the next click in an arrow. */
4430 if (gui.dragged_sb == SBAR_NONE)
4431 gui.dragged_wp = NULL;
4432
4433 if (old_topline != wp->w_topline
4434#ifdef FEAT_DIFF
4435 || old_topfill != wp->w_topfill
4436#endif
4437 )
4438 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004439 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 {
4441 cursor_correct(); /* fix window for 'so' */
4442 update_topline(); /* avoid up/down jump */
4443 }
4444 if (old_cursor.lnum != wp->w_cursor.lnum)
4445 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004449 /* Make sure wp->w_leftcol and wp->w_skipcol are correct. */
4450 validate_cursor();
4451
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 curwin = save_wp;
4453 curbuf = save_wp->w_buffer;
4454
4455 /*
4456 * Don't call updateWindow() when nothing has changed (it will overwrite
4457 * the status line!).
4458 */
4459 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004460 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461#ifdef FEAT_DIFF
4462 || old_topfill != wp->w_topfill
4463#endif
4464 )
4465 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004466 int type = VALID;
4467
4468#ifdef FEAT_INS_EXPAND
4469 if (pum_visible())
4470 {
4471 type = NOT_VALID;
4472 wp->w_lines_valid = 0;
4473 }
4474#endif
4475 /* Don't set must_redraw here, it may cause the popup menu to
4476 * disappear when losing focus after a scrollbar drag. */
4477 if (wp->w_redr_type < type)
4478 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004479 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 updateWindow(wp); /* update window, status line, and cmdline */
Bram Moolenaara338adc2018-01-31 20:51:47 +01004481 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004484#ifdef FEAT_INS_EXPAND
4485 /* May need to redraw the popup menu. */
4486 if (pum_visible())
4487 pum_redraw();
4488#endif
4489
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004490 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491}
4492
4493
4494/*
4495 * Horizontal scrollbar stuff:
4496 */
4497
4498/*
4499 * Return length of line "lnum" for horizontal scrolling.
4500 */
4501 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004502scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503{
4504 char_u *p;
4505 colnr_T col;
4506 int w;
4507
4508 p = ml_get(lnum);
4509 col = 0;
4510 if (*p != NUL)
4511 for (;;)
4512 {
4513 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004514 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 if (*p == NUL) /* don't count the last character */
4516 break;
4517 col += w;
4518 }
4519 return col;
4520}
4521
4522/* Remember which line is currently the longest, so that we don't have to
4523 * search for it when scrolling horizontally. */
4524static linenr_T longest_lnum = 0;
4525
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004526/*
4527 * Find longest visible line number. If this is not possible (or not desired,
4528 * by setting 'h' in "guioptions") then the current line number is returned.
4529 */
4530 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004531gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004532{
4533 linenr_T ret = 0;
4534
4535 /* Calculate maximum for horizontal scrollbar. Check for reasonable
4536 * line numbers, topline and botline can be invalid when displaying is
4537 * postponed. */
4538 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4539 && curwin->w_topline <= curwin->w_cursor.lnum
4540 && curwin->w_botline > curwin->w_cursor.lnum
4541 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4542 {
4543 linenr_T lnum;
4544 colnr_T n;
4545 long max = 0;
4546
4547 /* Use maximum of all visible lines. Remember the lnum of the
4548 * longest line, closest to the cursor line. Used when scrolling
4549 * below. */
4550 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4551 {
4552 n = scroll_line_len(lnum);
4553 if (n > (colnr_T)max)
4554 {
4555 max = n;
4556 ret = lnum;
4557 }
4558 else if (n == (colnr_T)max
4559 && abs((int)(lnum - curwin->w_cursor.lnum))
4560 < abs((int)(ret - curwin->w_cursor.lnum)))
4561 ret = lnum;
4562 }
4563 }
4564 else
4565 /* Use cursor line only. */
4566 ret = curwin->w_cursor.lnum;
4567
4568 return ret;
4569}
4570
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004572gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573{
4574 long value, size, max; /* need 32 bit ints here */
4575
4576 if (!gui.which_scrollbars[SBAR_BOTTOM])
4577 return;
4578
4579 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4580 return;
4581
4582 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4583 return;
4584
4585 /*
4586 * It is possible for the cursor to be invalid if we're in the middle of
4587 * something (like changing files). If so, don't do anything for now.
4588 */
4589 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4590 {
4591 gui.bottom_sbar.value = -1;
4592 return;
4593 }
4594
Bram Moolenaar02631462017-09-22 15:20:32 +02004595 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 if (curwin->w_p_wrap)
4597 {
4598 value = 0;
4599#ifdef SCROLL_PAST_END
4600 max = 0;
4601#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004602 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603#endif
4604 }
4605 else
4606 {
4607 value = curwin->w_leftcol;
4608
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004609 longest_lnum = gui_find_longest_lnum();
4610 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 if (virtual_active())
4613 {
4614 /* May move the cursor even further to the right. */
4615 if (curwin->w_virtcol >= (colnr_T)max)
4616 max = curwin->w_virtcol;
4617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004620 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621#endif
4622 /* The line number isn't scrolled, thus there is less space when
Bram Moolenaar64486672010-05-16 15:46:46 +02004623 * 'number' or 'relativenumber' is set (also for 'foldcolumn'). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 size -= curwin_col_off();
4625#ifndef SCROLL_PAST_END
4626 max -= curwin_col_off();
4627#endif
4628 }
4629
4630#ifndef SCROLL_PAST_END
4631 if (value > max - size + 1)
4632 value = max - size + 1; /* limit the value to allowable range */
4633#endif
4634
4635#ifdef FEAT_RIGHTLEFT
4636 if (curwin->w_p_rl)
4637 {
4638 value = max + 1 - size - value;
4639 if (value < 0)
4640 {
4641 size += value;
4642 value = 0;
4643 }
4644 }
4645#endif
4646 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4647 && max == gui.bottom_sbar.max)
4648 return;
4649
4650 gui.bottom_sbar.value = value;
4651 gui.bottom_sbar.size = size;
4652 gui.bottom_sbar.max = max;
4653 gui.prev_wrap = curwin->w_p_wrap;
4654
4655 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4656}
4657
4658/*
4659 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4660 */
4661 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004662gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663{
4664 /* no wrapping, no scrolling */
4665 if (curwin->w_p_wrap)
4666 return FALSE;
4667
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004668 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 return FALSE;
4670
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004671 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
4673 /* When the line of the cursor is too short, move the cursor to the
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004674 * longest visible line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004676 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004677 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004679 if (compute_longest_lnum)
4680 {
4681 curwin->w_cursor.lnum = gui_find_longest_lnum();
4682 curwin->w_cursor.col = 0;
4683 }
4684 /* Do a sanity check on "longest_lnum", just in case. */
4685 else if (longest_lnum >= curwin->w_topline
4686 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 {
4688 curwin->w_cursor.lnum = longest_lnum;
4689 curwin->w_cursor.col = 0;
4690 }
4691 }
4692
4693 return leftcol_changed();
4694}
4695
4696/*
4697 * Check that none of the colors are the same as the background color
4698 */
4699 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004700gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
4702 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4703 {
4704 gui_set_bg_color((char_u *)"White");
4705 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4706 gui_set_fg_color((char_u *)"Black");
4707 }
4708}
4709
Bram Moolenaar3918c952005-03-15 22:34:55 +00004710 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004711gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
4713 gui.norm_pixel = gui_get_color(name);
4714 hl_set_fg_color_name(vim_strsave(name));
4715}
4716
Bram Moolenaar3918c952005-03-15 22:34:55 +00004717 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004718gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719{
4720 gui.back_pixel = gui_get_color(name);
4721 hl_set_bg_color_name(vim_strsave(name));
4722}
4723
4724/*
4725 * Allocate a color by name.
4726 * Returns INVALCOLOR and gives an error message when failed.
4727 */
4728 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004729gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
4731 guicolor_T t;
4732
4733 if (*name == NUL)
4734 return INVALCOLOR;
4735 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004738#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 && gui.in_use
4740#endif
4741 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004742 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 return t;
4744}
4745
4746/*
4747 * Return the grey value of a color (range 0-255).
4748 */
4749 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004750gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004752 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004754 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004755 + (((rgb >> 8) & 0xff) * 587)
4756 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757}
4758
4759#if defined(FEAT_GUI_X11) || defined(PROTO)
4760 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004761gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
4763 win_T *wp;
4764
4765 /* Nothing to do if GUI hasn't started yet. */
4766 if (!gui.in_use)
4767 return;
4768
4769 FOR_ALL_WINDOWS(wp)
4770 {
4771 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4772 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4773 }
4774 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4775}
4776#endif
4777
4778/*
4779 * Call this when focus has changed.
4780 */
4781 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004782gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783{
4784/*
4785 * Skip this code to avoid drawing the cursor when debugging and switching
4786 * between the debugger window and gvim.
4787 */
4788#if 1
4789 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004790 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
4792# ifdef FEAT_XIM
4793 xim_set_focus(in_focus);
4794# endif
4795
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004796 /* Put events in the input queue only when allowed.
4797 * ui_focus_change() isn't called directly, because it invokes
4798 * autocommands and that must not happen asynchronously. */
4799 if (!hold_gui_events)
4800 {
4801 char_u bytes[3];
4802
4803 bytes[0] = CSI;
4804 bytes[1] = KS_EXTRA;
4805 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4806 add_to_input_buf(bytes, 3);
4807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808#endif
4809}
4810
4811/*
4812 * Called when the mouse moved (but not when dragging).
4813 */
4814 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004815gui_mouse_moved(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
4817 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004818 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
Bram Moolenaard3667a22006-03-16 21:35:52 +00004820 /* Ignore this while still starting up. */
4821 if (!gui.in_use || gui.starting)
4822 return;
4823
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824#ifdef FEAT_MOUSESHAPE
4825 /* Get window pointer, and update mouse shape as well. */
4826 wp = xy2win(x, y);
4827#endif
4828
4829 /* Only handle this when 'mousefocus' set and ... */
4830 if (p_mousef
4831 && !hold_gui_events /* not holding events */
4832 && (State & (NORMAL|INSERT))/* Normal/Visual/Insert mode */
4833 && State != HITRETURN /* but not hit-return prompt */
4834 && msg_scrolled == 0 /* no scrolled message */
4835 && !need_mouse_correct /* not moving the pointer */
4836 && gui.in_focus) /* gvim in focus */
4837 {
4838 /* Don't move the mouse when it's left or right of the Vim window */
4839 if (x < 0 || x > Columns * gui.char_width)
4840 return;
4841#ifndef FEAT_MOUSESHAPE
4842 wp = xy2win(x, y);
4843#endif
4844 if (wp == curwin || wp == NULL)
4845 return; /* still in the same old window, or none at all */
4846
Bram Moolenaar9c102382006-05-03 21:26:49 +00004847 /* Ignore position in the tab pages line. */
4848 if (Y_2_ROW(y) < tabline_height())
4849 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004850
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 /*
4852 * format a mouse click on status line input
4853 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004854 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4855 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 */
4857 if (finish_op)
4858 {
4859 /* abort the current operator first */
4860 st[0] = ESC;
4861 add_to_input_buf(st, 1);
4862 }
4863 st[0] = CSI;
4864 st[1] = KS_MOUSE;
4865 st[2] = KE_FILLER;
4866 st[3] = (char_u)MOUSE_LEFT;
4867 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004868 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 wp->w_height + W_WINROW(wp));
4870
4871 add_to_input_buf(st, 8);
4872 st[3] = (char_u)MOUSE_RELEASE;
4873 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874#ifdef FEAT_GUI_GTK
4875 /* Need to wake up the main loop */
4876 if (gtk_main_level() > 0)
4877 gtk_main_quit();
4878#endif
4879 }
4880}
4881
4882/*
4883 * Called when mouse should be moved to window with focus.
4884 */
4885 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004886gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887{
4888 int x, y;
4889 win_T *wp = NULL;
4890
4891 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004892
4893 if (!(gui.in_use && p_mousef))
4894 return;
4895
4896 gui_mch_getmouse(&x, &y);
4897 /* Don't move the mouse when it's left or right of the Vim window */
4898 if (x < 0 || x > Columns * gui.char_width)
4899 return;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004900 if (y >= 0 && Y_2_ROW(y) >= tabline_height())
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004901 wp = xy2win(x, y);
4902 if (wp != curwin && wp != NULL) /* If in other than current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004904 validate_cline_row();
4905 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4906 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909}
4910
4911/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004912 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004913 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 static win_T *
Bram Moolenaar4f974752019-02-17 17:44:42 +01004916xy2win(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 int row;
4919 int col;
4920 win_T *wp;
4921
4922 row = Y_2_ROW(y);
4923 col = X_2_COL(x);
4924 if (row < 0 || col < 0) /* before first window */
4925 return NULL;
4926 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004927 if (wp == NULL)
4928 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004929#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (State == HITRETURN || State == ASKMORE)
4931 {
4932 if (Y_2_ROW(y) >= msg_row)
4933 update_mouseshape(SHAPE_IDX_MOREL);
4934 else
4935 update_mouseshape(SHAPE_IDX_MORE);
4936 }
4937 else if (row > wp->w_height) /* below status line */
4938 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004939 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004940 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004942 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004943 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 update_mouseshape(SHAPE_IDX_STATUS);
4945 else
4946 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004948 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949}
4950
4951/*
4952 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4953 * File names may be given to redefine the args list.
4954 */
4955 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004956ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957{
4958 char_u *arg = eap->arg;
4959
4960 /*
4961 * Check for "-f" argument: foreground, don't fork.
4962 * Also don't fork when started with "gvim -f".
4963 * Do fork when using "gui -b".
4964 */
4965 if (arg[0] == '-'
4966 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004967 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
4969 gui.dofork = (arg[1] == 'b');
4970 eap->arg = skipwhite(eap->arg + 2);
4971 }
4972 if (!gui.in_use)
4973 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004974#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
4975 emsg(_(e_nogvim));
4976 return;
4977#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 /* Clear the command. Needed for when forking+exiting, to avoid part
4979 * of the argument ending up after the shell prompt. */
4980 msg_clr_eos_force();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004981# ifdef GUI_MAY_SPAWN
4982 if (!ends_excmd(*eap->arg))
4983 gui_start(eap->arg);
4984 else
4985# endif
4986 gui_start(NULL);
4987# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004988 channel_gui_register_all();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004989# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02004990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 if (!ends_excmd(*eap->arg))
4993 ex_next(eap);
4994}
4995
Bram Moolenaar4f974752019-02-17 17:44:42 +01004996#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
4997 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)) \
4998 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999/*
5000 * This is shared between Athena, Motif and GTK.
5001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
5003/*
5004 * Callback function for do_in_runtimepath().
5005 */
5006 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005007gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005009 char_u *gfp_buffer = cookie;
5010
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 if (STRLEN(fname) >= MAXPATHL)
5012 *gfp_buffer = NUL;
5013 else
5014 STRCPY(gfp_buffer, fname);
5015}
5016
5017/*
5018 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5019 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5020 */
5021 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005022gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
5024 if (STRLEN(name) > MAXPATHL - 14)
5025 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005026 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005027 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005028 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 return FAIL;
5030 return OK;
5031}
5032
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005033# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034/*
5035 * Given the name of the "icon=" argument, try finding the bitmap file for the
5036 * icon. If it is an absolute path name, use it as it is. Otherwise append
5037 * "ext" and search for it in 'runtimepath'.
5038 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5039 * contains "name".
5040 */
5041 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005042gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
5044 char_u buf[MAXPATHL + 1];
5045
5046 expand_env(name, buffer, MAXPATHL);
5047 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5048 STRCPY(buffer, buf);
5049}
5050# endif
5051#endif
5052
Bram Moolenaar9372a112005-12-06 19:59:18 +00005053#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005055display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
5057 char_u *p;
5058
5059 if (isatty(2))
5060 fflush(stderr);
5061 else if (error_ga.ga_data != NULL)
5062 {
5063 /* avoid putting up a message box with blanks only */
5064 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5065 if (!isspace(*p))
5066 {
5067 /* Truncate a very long message, it will go off-screen. */
5068 if (STRLEN(p) > 2000)
5069 STRCPY(p + 2000 - 14, "...(truncated)");
5070 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005071 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073 }
5074 ga_clear(&error_ga);
5075 }
5076}
5077#endif
5078
5079#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5080/*
5081 * Return TRUE if still starting up and there is no place to enter text.
5082 * For GTK and X11 we check if stderr is not a tty, which means we were
5083 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5084 * allow typing on stdin.
5085 */
5086 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005087no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
5089 return ((!gui.in_use || gui.starting)
5090# ifndef NO_CONSOLE
5091 && !isatty(0) && !isatty(2)
5092# endif
5093 );
5094}
5095#endif
5096
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005097#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005098 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005099 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100/*
5101 * Update the current window and the screen.
5102 */
5103 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005104gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005106# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005107 linenr_T conceal_old_cursor_line = 0;
5108 linenr_T conceal_new_cursor_line = 0;
5109 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005110# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 update_topline();
5113 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005114
Bram Moolenaarec80df72008-05-07 19:46:51 +00005115 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005116 if (!finish_op && (has_cursormoved()
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005117# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005118 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005119# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005120 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005121 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005122 if (has_cursormoved())
5123 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005124# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005125 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005126 {
5127 conceal_old_cursor_line = last_cursormoved.lnum;
5128 conceal_new_cursor_line = curwin->w_cursor.lnum;
5129 conceal_update_lines = TRUE;
5130 }
5131# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005132 last_cursormoved = curwin->w_cursor;
5133 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005134
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005135# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005136 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005137 && (conceal_old_cursor_line != conceal_new_cursor_line
5138 || conceal_cursor_line(curwin)
5139 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005140 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005141 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005142 redrawWinline(curwin, conceal_old_cursor_line);
5143 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005144 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005145 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005146 }
5147# endif
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005148 update_screen(0); /* may need to update the screen */
5149 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005150 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151}
5152#endif
5153
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005154#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155/*
5156 * Get the text to use in a find/replace dialog. Uses the last search pattern
5157 * if the argument is empty.
5158 * Returns an allocated string.
5159 */
5160 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005161get_find_dialog_text(
5162 char_u *arg,
5163 int *wwordp, /* return: TRUE if \< \> found */
5164 int *mcasep) /* return: TRUE if \C found */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165{
5166 char_u *text;
5167
5168 if (*arg == NUL)
5169 text = last_search_pat();
5170 else
5171 text = arg;
5172 if (text != NULL)
5173 {
5174 text = vim_strsave(text);
5175 if (text != NULL)
5176 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005177 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 int i;
5179
5180 /* Remove "\V" */
5181 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5182 {
5183 mch_memmove(text, text + 2, (size_t)(len - 1));
5184 len -= 2;
5185 }
5186
5187 /* Recognize "\c" and "\C" and remove. */
5188 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5189 {
5190 *mcasep = (text[1] == 'C');
5191 mch_memmove(text, text + 2, (size_t)(len - 1));
5192 len -= 2;
5193 }
5194
5195 /* Recognize "\<text\>" and remove. */
5196 if (len >= 4
5197 && STRNCMP(text, "\\<", 2) == 0
5198 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5199 {
5200 *wwordp = TRUE;
5201 mch_memmove(text, text + 2, (size_t)(len - 4));
5202 text[len - 4] = NUL;
5203 }
5204
5205 /* Recognize "\/" or "\?" and remove. */
5206 for (i = 0; i + 1 < len; ++i)
5207 if (text[i] == '\\' && (text[i + 1] == '/'
5208 || text[i + 1] == '?'))
5209 {
5210 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5211 --len;
5212 }
5213 }
5214 }
5215 return text;
5216}
5217
5218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 * Handle the press of a button in the find-replace dialog.
5220 * Return TRUE when something was added to the input buffer.
5221 */
5222 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005223gui_do_findrepl(
5224 int flags, /* one of FRD_REPLACE, FRD_FINDNEXT, etc. */
5225 char_u *find_text,
5226 char_u *repl_text,
5227 int down) /* Search downwards. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228{
5229 garray_T ga;
5230 int i;
5231 int type = (flags & FRD_TYPE_MASK);
5232 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005233 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005234 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005235 static int busy = FALSE;
5236
5237 /* When the screen is being updated we should not change buffers and
5238 * windows structures, it may cause freed memory to be used. Also don't
5239 * do this recursively (pressing "Find" quickly several times. */
5240 if (updating_screen || busy)
5241 return FALSE;
5242
5243 /* refuse replace when text cannot be changed */
5244 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5245 return FALSE;
5246
5247 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
5249 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005250 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 ga_concat(&ga, (char_u *)"%s/");
5252
5253 ga_concat(&ga, (char_u *)"\\V");
5254 if (flags & FRD_MATCH_CASE)
5255 ga_concat(&ga, (char_u *)"\\C");
5256 else
5257 ga_concat(&ga, (char_u *)"\\c");
5258 if (flags & FRD_WHOLE_WORD)
5259 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar518bc172018-05-13 17:05:30 +02005260 /* escape / and \ */
5261 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5262 if (p != NULL)
5263 ga_concat(&ga, p);
5264 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 if (flags & FRD_WHOLE_WORD)
5266 ga_concat(&ga, (char_u *)"\\>");
5267
5268 if (type == FRD_REPLACEALL)
5269 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005271 /* escape / and \ */
5272 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5273 if (p != NULL)
5274 ga_concat(&ga, p);
5275 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005277 }
5278 ga_append(&ga, NUL);
5279
5280 if (type == FRD_REPLACE)
5281 {
5282 /* Do the replacement when the text at the cursor matches. Thus no
5283 * replacement is done if the cursor was moved! */
5284 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5285 regmatch.rm_ic = 0;
5286 if (regmatch.regprog != NULL)
5287 {
5288 p = ml_get_cursor();
5289 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5290 && regmatch.startp[0] == p)
5291 {
5292 /* Clear the command line to remove any old "No match"
5293 * error. */
5294 msg_end_prompt();
5295
5296 if (u_save_cursor() == OK)
5297 {
5298 /* A button was pressed thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005299 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005300
5301 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005302 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005303 ins_str(repl_text);
5304 }
5305 }
5306 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005307 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005308 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005309 }
5310 }
5311
5312 if (type == FRD_REPLACEALL)
5313 {
5314 /* A button was pressed, thus undo should be synced. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005315 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 do_cmdline_cmd(ga.ga_data);
5317 }
5318 else
5319 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005320 int searchflags = SEARCH_MSG + SEARCH_MARK;
5321
5322 /* Search for the next match.
5323 * Don't skip text under cursor for single replace. */
5324 if (type == FRD_REPLACE)
5325 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005327 if (down)
5328 {
5329 (void)do_search(NULL, '/', ga.ga_data, 1L, searchflags, NULL, NULL);
5330 }
5331 else
5332 {
5333 /* We need to escape '?' if and only if we are searching in the up
5334 * direction */
5335 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5336 if (p != NULL)
5337 (void)do_search(NULL, '?', p, 1L, searchflags, NULL, NULL);
5338 vim_free(p);
5339 }
5340
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 msg_scroll = i; /* don't let an error message set msg_scroll */
5342 }
5343
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005344 /* Don't want to pass did_emsg to other code, it may cause disabling
5345 * syntax HL if we were busy redrawing. */
5346 did_emsg = save_did_emsg;
5347
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 if (State & (NORMAL | INSERT))
5349 {
5350 gui_update_screen(); /* update the screen */
5351 msg_didout = 0; /* overwrite any message */
5352 need_wait_return = FALSE; /* don't wait for return */
5353 }
5354
5355 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005356 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 return (ga.ga_len > 0);
5358}
5359
5360#endif
5361
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005362#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363/*
5364 * Jump to the window at specified point (x, y).
5365 */
5366 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005367gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368{
5369 int row = Y_2_ROW(y);
5370 int col = X_2_COL(x);
5371 win_T *wp;
5372
5373 if (row >= 0 && col >= 0)
5374 {
5375 wp = mouse_find_win(&row, &col);
5376 if (wp != NULL && wp != curwin)
5377 win_goto(wp);
5378 }
5379}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380
5381/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005382 * Function passed to handle_drop() for the actions to be done after the
5383 * argument list has been updated.
5384 */
5385 static void
5386drop_callback(void *cookie)
5387{
5388 char_u *p = cookie;
5389
5390 /* If Shift held down, change to first file's directory. If the first
5391 * item is a directory, change to that directory (and let the explorer
5392 * plugin show the contents). */
5393 if (p != NULL)
5394 {
5395 if (mch_isdir(p))
5396 {
5397 if (mch_chdir((char *)p) == 0)
5398 shorten_fnames(TRUE);
5399 }
5400 else if (vim_chdirfile(p, "drop") == OK)
5401 shorten_fnames(TRUE);
5402 vim_free(p);
5403 }
5404
5405 /* Update the screen display */
5406 update_screen(NOT_VALID);
5407# ifdef FEAT_MENU
5408 gui_update_menus(0);
5409# endif
5410#ifdef FEAT_TITLE
5411 maketitle();
5412#endif
5413 setcursor();
5414 out_flush_cursor(FALSE, FALSE);
5415}
5416
5417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 * Process file drop. Mouse cursor position, key modifiers, name of files
5419 * and count of files are given. Argument "fnames[count]" has full pathnames
5420 * of dropped files, they will be freed in this function, and caller can't use
5421 * fnames after call this function.
5422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005424gui_handle_drop(
5425 int x UNUSED,
5426 int y UNUSED,
5427 int_u modifiers,
5428 char_u **fnames,
5429 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
5431 int i;
5432 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005433 static int entered = FALSE;
5434
5435 /*
5436 * This function is called by event handlers. Just in case we get a
5437 * second event before the first one is handled, ignore the second one.
5438 * Not sure if this can ever happen, just in case.
5439 */
5440 if (entered)
5441 return;
5442 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
5444 /*
5445 * When the cursor is at the command line, add the file names to the
5446 * command line, don't edit the files.
5447 */
5448 if (State & CMDLINE)
5449 {
5450 shorten_filenames(fnames, count);
5451 for (i = 0; i < count; ++i)
5452 {
5453 if (fnames[i] != NULL)
5454 {
5455 if (i > 0)
5456 add_to_input_buf((char_u*)" ", 1);
5457
5458 /* We don't know what command is used thus we can't be sure
5459 * about which characters need to be escaped. Only escape the
5460 * most common ones. */
5461# ifdef BACKSLASH_IN_FILENAME
5462 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5463# else
5464 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5465# endif
5466 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005467 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 vim_free(p);
5469 vim_free(fnames[i]);
5470 }
5471 }
5472 vim_free(fnames);
5473 }
5474 else
5475 {
5476 /* Go to the window under mouse cursor, then shorten given "fnames" by
5477 * current window, because a window can have local current dir. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 shorten_filenames(fnames, count);
5480
5481 /* If Shift held down, remember the first item. */
5482 if ((modifiers & MOUSE_SHIFT) != 0)
5483 p = vim_strsave(fnames[0]);
5484 else
5485 p = NULL;
5486
5487 /* Handle the drop, :edit or :split to get to the file. This also
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005488 * frees fnames[]. Skip this if there is only one item, it's a
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 * directory and Shift is held down. */
5490 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5491 && mch_isdir(fnames[0]))
5492 {
5493 vim_free(fnames[0]);
5494 vim_free(fnames);
5495 }
5496 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005497 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
5498 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005500
5501 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502}
5503#endif