blob: 403272186870cfacd026a645e4735b6a5d363ca2 [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
Bram Moolenaar30613902019-12-01 22:11:18 +010013// Structure containing all the GUI information
Bram Moolenaar071d4272004-06-13 20:20:40 +000014gui_T gui;
15
K.Takata94373c42022-01-27 15:04:22 +000016#if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
17# define USE_SET_GUIFONTWIDE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010018static void set_guifontwide(char_u *font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010020static void gui_check_pos(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020021static void gui_reset_scroll_region(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010022static void gui_outstr(char_u *, int);
23static int gui_screenchar(int off, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020024static int gui_outstr_nowrap(char_u *s, int len, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010025static void gui_delete_lines(int row, int count);
26static void gui_insert_lines(int row, int count);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020027static int gui_xy2colrow(int x, int y, int *colp);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000028#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010029static int gui_has_tabline(void);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000030#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010031static void gui_do_scrollbar(win_T *wp, int which, int enable);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010032static void gui_update_horiz_scrollbar(int);
33static void gui_set_fg_color(char_u *name);
34static void gui_set_bg_color(char_u *name);
Bram Moolenaar0630bb62019-11-04 22:52:12 +010035static win_T *xy2win(int x, int y, mouse_find_T popup);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020037#ifdef GUI_MAY_FORK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010038static void gui_do_fork(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020039
Bram Moolenaard25c16e2016-01-29 22:13:30 +010040static int gui_read_child_pipe(int fd);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020041
Bram Moolenaar30613902019-12-01 22:11:18 +010042// Return values for gui_read_child_pipe
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020043enum {
44 GUI_CHILD_IO_ERROR,
45 GUI_CHILD_OK,
46 GUI_CHILD_FAILED
47};
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020048#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020049
Bram Moolenaard25c16e2016-01-29 22:13:30 +010050static void gui_attempt_start(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020051
Bram Moolenaar30613902019-12-01 22:11:18 +010052static int can_update_cursor = TRUE; // can display the cursor
53static int disable_flush = 0; // If > 0, gui_mch_flush() is disabled.
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
55/*
56 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
57 * this makes the thumb indicate the part of the text that is shown. Motif
58 * can't do this.
59 */
Bram Moolenaar097148e2020-08-11 21:58:20 +020060#if defined(FEAT_GUI_ATHENA)
Bram Moolenaar071d4272004-06-13 20:20:40 +000061# define SCROLL_PAST_END
62#endif
63
64/*
65 * gui_start -- Called when user wants to start the GUI.
66 *
67 * Careful: This function can be called recursively when there is a ":gui"
68 * command in the .gvimrc file. Only the first call should fork, not the
69 * recursive call.
70 */
71 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +020072gui_start(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000073{
74 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 static int recursive = 0;
Bram Moolenaar68cbb142019-05-09 14:14:42 +020076#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +020077 char *msg = NULL;
78#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
80 old_term = vim_strsave(T_NAME);
81
Bram Moolenaar30613902019-12-01 22:11:18 +010082 settmode(TMODE_COOK); // stop RAW mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 if (full_screen)
Bram Moolenaar30613902019-12-01 22:11:18 +010084 cursor_on(); // needed for ":gui" in .vimrc
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 full_screen = FALSE;
86
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 ++recursive;
88
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020089#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020090 /*
91 * Quit the current process and continue in the child.
92 * Makes "gvim file" disconnect from the shell it was started in.
93 * Don't do this when Vim was started with "-f" or the 'f' flag is present
94 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020095 * Don't do this when there is a running job, we can only get the status
96 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020097 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020098 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
99# ifdef FEAT_JOB_CHANNEL
100 && !job_any_running()
101# endif
102 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200103 {
104 gui_do_fork();
105 }
106 else
107#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200108#ifdef GUI_MAY_SPAWN
109 if (gui.dospawn
110# ifdef EXPERIMENTAL_GUI_CMD
111 && gui.dofork
112# endif
113 && !vim_strchr(p_go, GO_FORG)
114 && !anyBufIsChanged()
115# ifdef FEAT_JOB_CHANNEL
116 && !job_any_running()
117# endif
118 )
119 {
Bram Moolenaar68cbb142019-05-09 14:14:42 +0200120# ifdef EXPERIMENTAL_GUI_CMD
121 msg =
122# endif
123 gui_mch_do_spawn(arg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200124 }
125 else
126#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200127 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200128#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100129 // If there is 'f' in 'guioptions' and specify -g argument,
130 // gui_mch_init_check() was not called yet.
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200131 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100132 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200133#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200134 gui_attempt_start();
135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar30613902019-12-01 22:11:18 +0100137 if (!gui.in_use) // failed to start GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100139 // Back to old term settings
140 //
141 // FIXME: If we got here because a child process failed and flagged to
Bram Moolenaar651fca82021-11-29 20:39:38 +0000142 // the parent to resume, and X11 is enabled, this will
Bram Moolenaar30613902019-12-01 22:11:18 +0100143 // hit an X11 I/O error and do a longjmp(), leaving recursive
144 // permanently set to 1. This is probably not as big a problem as it
145 // sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
146 // return "OK" unconditionally, so it would be very difficult to
147 // actually hit this case.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200148 termcapinit(old_term);
Bram Moolenaar30613902019-12-01 22:11:18 +0100149 settmode(TMODE_RAW); // restart RAW mode
Bram Moolenaar30613902019-12-01 22:11:18 +0100150 set_title_defaults(); // set 'title' and 'icon' again
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200151#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
152 if (msg)
153 emsg(msg);
154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 }
156
157 vim_free(old_term);
158
Bram Moolenaar30613902019-12-01 22:11:18 +0100159 // If the GUI started successfully, trigger the GUIEnter event, otherwise
160 // the GUIFailed event.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200161 gui_mch_update();
162 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
163 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200164 --recursive;
165}
166
167/*
168 * Set_termname() will call gui_init() to start the GUI.
169 * Set the "starting" flag, to indicate that the GUI will start.
170 *
171 * We don't want to open the GUI shell until after we've read .gvimrc,
172 * otherwise we don't know what font we will use, and hence we don't know
173 * what size the shell should be. So if there are errors in the .gvimrc
174 * file, they will have to go to the terminal: Set full_screen to FALSE.
175 * full_screen will be set to TRUE again by a successful termcapinit().
176 */
177 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100178gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200179{
180 static int recursive = 0;
181
182 ++recursive;
183 gui.starting = TRUE;
184
185#ifdef FEAT_GUI_GTK
186 gui.event_time = GDK_CURRENT_TIME;
187#endif
188
189 termcapinit((char_u *)"builtin_gui");
190 gui.starting = recursive - 1;
191
Bram Moolenaar9372a112005-12-06 19:59:18 +0000192#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200194 {
195# ifdef FEAT_EVAL
196 Window x11_window;
197 Display *x11_display;
198
199 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
200 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
201# endif
202
Bram Moolenaar30613902019-12-01 22:11:18 +0100203 // Display error messages in a dialog now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 --recursive;
208}
209
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200210#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200211
Bram Moolenaar30613902019-12-01 22:11:18 +0100212// for waitpid()
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200213# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
214# include <sys/wait.h>
215# endif
216
217/*
218 * Create a new process, by forking. In the child, start the GUI, and in
219 * the parent, exit.
220 *
221 * If something goes wrong, this will return with gui.in_use still set
222 * to FALSE, in which case the caller should continue execution without
223 * the GUI.
224 *
225 * If the child fails to start the GUI, then the child will exit and the
226 * parent will return. If the child succeeds, then the parent will exit
227 * and the child will return.
228 */
229 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100230gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200231{
Bram Moolenaar30613902019-12-01 22:11:18 +0100232 int pipefd[2]; // pipe between parent and child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200233 int pipe_error;
234 int status;
235 int exit_status;
236 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200237
Bram Moolenaar30613902019-12-01 22:11:18 +0100238 // Setup a pipe between the child and the parent, so that the parent
239 // knows when the child has done the setsid() call and is allowed to
240 // exit.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200241 pipe_error = (pipe(pipefd) < 0);
242 pid = fork();
Bram Moolenaar30613902019-12-01 22:11:18 +0100243 if (pid < 0) // Fork error
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200244 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000245 emsg(_(e_failed_to_create_new_process_for_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200246 return;
247 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100248 else if (pid > 0) // Parent
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200249 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100250 // Give the child some time to do the setsid(), otherwise the
251 // exit() may kill the child too (when starting gvim from inside a
252 // gvim).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200253 if (!pipe_error)
254 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100255 // The read returns when the child closes the pipe (or when
256 // the child dies for some reason).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200257 close(pipefd[1]);
258 status = gui_read_child_pipe(pipefd[0]);
259 if (status == GUI_CHILD_FAILED)
260 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100261 // The child failed to start the GUI, so the caller must
262 // continue. There may be more error information written
263 // to stderr by the child.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200264# ifdef __NeXT__
265 wait4(pid, &exit_status, 0, (struct rusage *)0);
266# else
267 waitpid(pid, &exit_status, 0);
268# endif
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000269 emsg(_(e_the_child_process_failed_to_start_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200270 return;
271 }
272 else if (status == GUI_CHILD_IO_ERROR)
273 {
274 pipe_error = TRUE;
275 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100276 // else GUI_CHILD_OK: parent exit
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200277 }
278
279 if (pipe_error)
Bram Moolenaareda1da02019-11-17 17:06:33 +0100280 ui_delay(301L, TRUE);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200281
Bram Moolenaar30613902019-12-01 22:11:18 +0100282 // When swapping screens we may need to go to the next line, e.g.,
283 // after a hit-enter prompt and using ":gui".
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200284 if (newline_on_exit)
285 mch_errmsg("\r\n");
286
287 /*
288 * The parent must skip the normal exit() processing, the child
289 * will do it. For example, GTK messes up signals when exiting.
290 */
291 _exit(0);
292 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100293 // Child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200294
Bram Moolenaar29695702012-05-18 17:03:18 +0200295#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100296 // Call gtk_init_check() here after fork(). See gui_init_check().
Bram Moolenaar29695702012-05-18 17:03:18 +0200297 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100298 getout_preserve_modified(1);
Bram Moolenaar29695702012-05-18 17:03:18 +0200299#endif
300
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200301# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
302 /*
303 * Change our process group. On some systems/shells a CTRL-C in the
304 * shell where Vim was started would otherwise kill gvim!
305 */
306# if defined(HAVE_SETSID)
307 (void)setsid();
308# else
309 (void)setpgid(0, 0);
310# endif
311# endif
312 if (!pipe_error)
313 close(pipefd[0]);
314
315# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
Bram Moolenaar30613902019-12-01 22:11:18 +0100316 // Tell the session manager our new PID
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200317 gui_mch_forked();
318# endif
319
Bram Moolenaar30613902019-12-01 22:11:18 +0100320 // Try to start the GUI
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200321 gui_attempt_start();
322
Bram Moolenaar30613902019-12-01 22:11:18 +0100323 // Notify the parent
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200324 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200325 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200326 if (gui.in_use)
327 write_eintr(pipefd[1], "ok", 3);
328 else
329 write_eintr(pipefd[1], "fail", 5);
330 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200331 }
332
Bram Moolenaar30613902019-12-01 22:11:18 +0100333 // If we failed to start the GUI, exit now.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200334 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100335 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200336}
337
338/*
339 * Read from a pipe assumed to be connected to the child process (this
340 * function is called from the parent).
341 * Return GUI_CHILD_OK if the child successfully started the GUI,
342 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
343 * some other error.
344 *
345 * The file descriptor will be closed before the function returns.
346 */
347 static int
348gui_read_child_pipe(int fd)
349{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200350 long bytes_read;
351#define READ_BUFFER_SIZE 10
352 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200353
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200354 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
355#undef READ_BUFFER_SIZE
356 close(fd);
357 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200358 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200359 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200360 if (strcmp(buffer, "ok") == 0)
361 return GUI_CHILD_OK;
362 return GUI_CHILD_FAILED;
363}
364
Bram Moolenaar30613902019-12-01 22:11:18 +0100365#endif // GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367/*
368 * Call this when vim starts up, whether or not the GUI is started
369 */
370 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100371gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372{
Bram Moolenaar30613902019-12-01 22:11:18 +0100373 gui.in_use = FALSE; // No GUI yet (maybe later)
374 gui.starting = FALSE; // No GUI yet (maybe later)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 gui_mch_prepare(argc, argv);
376}
377
378/*
379 * Try initializing the GUI and check if it can be started.
380 * Used from main() to check early if "vim -g" can start the GUI.
381 * Used from gui_init() to prepare for starting the GUI.
382 * Returns FAIL or OK.
383 */
384 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100385gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386{
387 static int result = MAYBE;
388
389 if (result != MAYBE)
390 {
391 if (result == FAIL)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000392 emsg(_(e_cannot_start_the_GUI));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 return result;
394 }
395
396 gui.shell_created = FALSE;
397 gui.dying = FALSE;
Bram Moolenaar30613902019-12-01 22:11:18 +0100398 gui.in_focus = TRUE; // so the guicursor setting works
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 gui.dragged_sb = SBAR_NONE;
400 gui.dragged_wp = NULL;
401 gui.pointer_hidden = FALSE;
402 gui.col = 0;
403 gui.row = 0;
404 gui.num_cols = Columns;
405 gui.num_rows = Rows;
406
407 gui.cursor_is_valid = FALSE;
408 gui.scroll_region_top = 0;
409 gui.scroll_region_bot = Rows - 1;
410 gui.scroll_region_left = 0;
411 gui.scroll_region_right = Columns - 1;
412 gui.highlight_mask = HL_NORMAL;
413 gui.char_width = 1;
414 gui.char_height = 1;
415 gui.char_ascent = 0;
416 gui.border_width = 0;
417
418 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200419#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 gui.bold_font = NOFONT;
421 gui.ital_font = NOFONT;
422 gui.boldital_font = NOFONT;
423# ifdef FEAT_XFONTSET
424 gui.fontset = NOFONTSET;
425# endif
426#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200427 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100428#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200429 gui.wide_bold_font = NOFONT;
430 gui.wide_ital_font = NOFONT;
431 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433
434#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200435# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436# ifdef FONTSET_ALWAYS
437 gui.menu_fontset = NOFONTSET;
438# else
439 gui.menu_font = NOFONT;
440# endif
441# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100442 gui.menu_is_active = TRUE; // default: include menu
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443# ifndef FEAT_GUI_GTK
444 gui.menu_height = MENU_DEFAULT_HEIGHT;
445 gui.menu_width = 0;
446# endif
447#endif
Bram Moolenaarb3f74062020-02-26 16:16:53 +0100448#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA) \
449 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 gui.toolbar_height = 0;
451#endif
452#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
453 gui.footer_height = 0;
454#endif
455#ifdef FEAT_BEVAL_TIP
456 gui.tooltip_fontset = NOFONTSET;
457#endif
458
459 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
460 gui.prev_wrap = -1;
461
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100462# ifdef FEAT_GUI_GTK
463 CLEAR_FIELD(gui.ligatures_map);
464#endif
465
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200466#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 result = OK;
468#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200469# ifdef FEAT_GUI_GTK
470 /*
471 * Note: Don't call gtk_init_check() before fork, it will be called after
472 * the fork. When calling it before fork, it make vim hang for a while.
473 * See gui_do_fork().
474 * Use a simpler check if the GUI window can probably be opened.
475 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200476 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200477# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480#endif
481 return result;
482}
483
484/*
485 * This is the call which starts the GUI.
486 */
487 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100488gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
490 win_T *wp;
491 static int recursive = 0;
492
493 /*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000494 * It's possible to use ":gui" in a .gvimrc file. The first half of this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 * function will then be executed at the first call, the rest by the
496 * recursive call. This allow the shell to be opened halfway reading a
497 * gvimrc file.
498 */
499 if (!recursive)
500 {
501 ++recursive;
502
503 clip_init(TRUE);
504
Bram Moolenaar30613902019-12-01 22:11:18 +0100505 // If can't initialize, don't try doing the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (gui_init_check() == FAIL)
507 {
508 --recursive;
509 clip_init(FALSE);
510 return;
511 }
512
513 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000514 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
515 * breaks the Paste toolbar button.
516 */
517 set_option_value((char_u *)"paste", 0L, NULL, 0);
518
Bram Moolenaaracc770a2020-04-12 15:11:06 +0200519 // Set t_Co to the number of colors: RGB.
520 set_color_count(256 * 256 * 256);
521
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000522 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 * Set up system-wide default menus.
524 */
525#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
526 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
527 {
528 sys_menu = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 sys_menu = FALSE;
531 }
532#endif
533
534 /*
535 * Switch on the mouse by default, unless the user changed it already.
536 * This can then be changed in the .gvimrc.
537 */
538 if (!option_was_set((char_u *)"mouse"))
539 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000540 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542 /*
543 * If -U option given, use only the initializations from that file and
544 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
545 */
546 if (use_gvimrc != NULL)
547 {
548 if (STRCMP(use_gvimrc, "NONE") != 0
549 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 && do_source(use_gvimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000551 semsg(_(e_cannot_read_from_str), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553 else
554 {
555 /*
556 * Get system wide defaults for gvim, only when file name defined.
557 */
558#ifdef SYS_GVIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#endif
561
562 /*
563 * Try to read GUI initialization commands from the following
564 * places:
565 * - environment variable GVIMINIT
566 * - the user gvimrc file (~/.gvimrc)
567 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
568 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
569 * The first that exists is used, the rest is ignored.
570 */
571 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000572 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000575 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200578#ifdef USR_GVIMRC_FILE3
579 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 )
583 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200584#ifdef USR_GVIMRC_FILE4
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE,
586 DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587#endif
588 }
589
590 /*
591 * Read initialization commands from ".gvimrc" in current
592 * directory. This is only done if the 'exrc' option is set.
593 * Because of security reasons we disallow shell and write
594 * commands now, except for unix if the file is owned by the user
595 * or 'secure' option has been reset in environment of global
596 * ".gvimrc".
597 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
598 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
599 */
600 if (p_exrc)
601 {
602#ifdef UNIX
603 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200604 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
Bram Moolenaar30613902019-12-01 22:11:18 +0100606 // if ".gvimrc" file is not owned by user, set 'secure'
607 // mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
609 secure = p_secure;
610 }
611#else
612 secure = p_secure;
613#endif
614
615 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200616 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617#ifdef SYS_GVIMRC_FILE
618 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200619 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620#endif
621#ifdef USR_GVIMRC_FILE2
622 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200623 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624#endif
625#ifdef USR_GVIMRC_FILE3
626 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200627 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200629#ifdef USR_GVIMRC_FILE4
630 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200631 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636 if (secure == 2)
637 need_wait_return = TRUE;
638 secure = 0;
639 }
640 }
641
642 if (need_wait_return || msg_didany)
643 wait_return(TRUE);
644
645 --recursive;
646 }
647
Bram Moolenaar30613902019-12-01 22:11:18 +0100648 // If recursive call opened the shell, return here from the first call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (gui.in_use)
650 return;
651
652 /*
653 * Create the GUI shell.
654 */
Bram Moolenaar30613902019-12-01 22:11:18 +0100655 gui.in_use = TRUE; // Must be set after menus have been set up
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if (gui_mch_init() == FAIL)
657 goto error;
658
Bram Moolenaar30613902019-12-01 22:11:18 +0100659 // Avoid a delay for an error message that was printed in the terminal
660 // where Vim was started.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 emsg_on_display = FALSE;
662 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100663 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 need_wait_return = FALSE;
665 msg_didany = FALSE;
666
667 /*
668 * Check validity of any generic resources that may have been loaded.
669 */
670 if (gui.border_width < 0)
671 gui.border_width = 0;
672
673 /*
674 * Set up the fonts. First use a font specified with "-fn" or "-font".
675 */
676 if (font_argument != NULL)
677 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
678 if (
679#ifdef FEAT_XFONTSET
680 (*p_guifontset == NUL
681 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
682#endif
683 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
684 : p_guifont, FALSE) == FAIL)
685 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000686 emsg(_(e_cannot_start_gui_no_valid_font_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 goto error2;
688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 if (gui_get_wide_font() == FAIL)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000690 emsg(_(e_guifontwide_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 gui.num_cols = Columns;
693 gui.num_rows = Rows;
694 gui_reset_scroll_region();
695
Bram Moolenaar30613902019-12-01 22:11:18 +0100696 // Create initial scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 FOR_ALL_WINDOWS(wp)
698 {
699 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
700 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
701 }
702 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
703
704#ifdef FEAT_MENU
705 gui_create_initial_menus(root_menu);
706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#ifdef FEAT_SIGN_ICONS
708 sign_gui_started();
709#endif
710
Bram Moolenaar30613902019-12-01 22:11:18 +0100711 // Configure the desired menu and scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 gui_init_which_components(NULL);
713
Bram Moolenaar30613902019-12-01 22:11:18 +0100714 // All components of the GUI have been created now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 gui.shell_created = TRUE;
716
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100717#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100718 // Set the shell size, adjusted for the screen size. For GTK this only
719 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100720 // If the window is already maximized (e.g. when --windowid is passed in),
721 // we want to use the system-provided dimensions by passing FALSE to
722 // mustset. Otherwise, we want to initialize with the default rows/columns.
723 if (gui_mch_maximized())
724 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
725 else
726 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100727#else
728# ifndef FEAT_GUI_GTK
729 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731#endif
732#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +0100733 // Need to set the size of the menubar after all the menus have been
734 // created.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 gui_mch_compute_menu_height((Widget)0);
736#endif
737
738 /*
739 * Actually open the GUI shell.
740 */
741 if (gui_mch_open() != FAIL)
742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 maketitle();
744 resettitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 init_gui_options();
747#ifdef FEAT_ARABIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100748 // Our GUI can't do bidi.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 p_tbidi = FALSE;
750#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000751#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100752 // Give GTK+ a chance to put all widget's into place.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000754
755# ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +0100756 // If there is no 'm' in 'guioptions' we need to remove the menu now.
757 // It was still there to make F10 work.
Bram Moolenaar18144c82006-04-12 21:52:12 +0000758 if (vim_strchr(p_go, GO_MENUS) == NULL)
759 {
760 --gui.starting;
761 gui_mch_enable_menu(FALSE);
762 ++gui.starting;
763 gui_mch_update();
764 }
765# endif
766
Bram Moolenaar30613902019-12-01 22:11:18 +0100767 // Now make sure the shell fits on the screen.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100768 if (gui_mch_maximized())
769 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
770 else
771 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100773 // When 'lines' was set while starting up the topframe may have to be
774 // resized.
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000775 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000776
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100777#ifdef FEAT_BEVAL_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100778 // Always create the Balloon Evaluation area, but disable it when
779 // 'ballooneval' is off.
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100780 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200781 {
782# ifdef FEAT_VARTABS
783 vim_free(balloonEval->vts);
784# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100785 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200786 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100787 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000788# ifdef FEAT_GUI_GTK
789 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
790 &general_beval_cb, NULL);
791# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000792# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000793 {
794 extern Widget textArea;
795 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000796 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000797 }
798# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100799# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000800 balloonEval = gui_mch_create_beval_area(NULL, NULL,
801 &general_beval_cb, NULL);
802# endif
803# endif
804# endif
805 if (!p_beval)
806 gui_mch_disable_beval_area(balloonEval);
807#endif
Bram Moolenaarad772a62020-06-01 14:07:49 +0200808
809#ifndef FEAT_GUI_MSWIN
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200810 // In the GUI modifiers are prepended to keys.
Bram Moolenaarad772a62020-06-01 14:07:49 +0200811 // Don't do this for MS-Windows yet, it sends CTRL-K without the
812 // modifier.
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200813 seenModifyOtherKeys = TRUE;
Bram Moolenaarad772a62020-06-01 14:07:49 +0200814#endif
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
817 if (!im_xim_isvalid_imactivate())
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000818 emsg(_(e_value_of_imactivatekey_is_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100820 // When 'cmdheight' was set during startup it may not have taken
821 // effect yet.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000822 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000823 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825 return;
826 }
827
828error2:
829#ifdef FEAT_GUI_X11
Bram Moolenaar30613902019-12-01 22:11:18 +0100830 // undo gui_mch_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 gui_mch_uninit();
832#endif
833
834error:
835 gui.in_use = FALSE;
836 clip_init(FALSE);
837}
838
839
840 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100841gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
Bram Moolenaar30613902019-12-01 22:11:18 +0100843 // don't free the fonts, it leads to a BUS error
844 // richard@whitequeen.com Jul 99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 gui.in_use = FALSE;
847 gui_mch_exit(rc);
848}
849
Bram Moolenaar9372a112005-12-06 19:59:18 +0000850#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +0200851 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000852# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853/*
854 * Called when the GUI shell is closed by the user. If there are no changed
855 * files Vim exits, otherwise there will be a dialog to ask the user what to
856 * do.
857 * When this function returns, Vim should NOT exit!
858 */
859 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100860gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861{
Bram Moolenaar3552e742021-05-29 12:21:58 +0200862 cmdmod_T save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
Bram Moolenaar3552e742021-05-29 12:21:58 +0200864 if (before_quit_autocmds(curwin, TRUE, FALSE))
865 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
Bram Moolenaar30613902019-12-01 22:11:18 +0100867 // Only exit when there are no changed files
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 exiting = TRUE;
869# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200870 cmdmod.cmod_flags |= CMOD_BROWSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871# endif
872# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +0200873 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100875 // If there are changed buffers, present the user with a dialog if
876 // possible, otherwise give an error message.
Bram Moolenaar027387f2016-01-02 22:25:52 +0100877 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 getout(0);
879
880 exiting = FALSE;
881 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +0100882 gui_update_screen(); // redraw, window may show changed buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883}
884#endif
885
886/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200887 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 * first font name that works is used. If none is found, use the default
889 * font.
890 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
891 * Return OK when able to set the font. When it failed FAIL is returned and
892 * the fonts are unchanged.
893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100895gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
897#define FONTLEN 320
898 char_u font_name[FONTLEN];
899 int font_list_empty = FALSE;
900 int ret = FAIL;
901
902 if (!gui.in_use)
903 return FAIL;
904
905 font_name[0] = NUL;
906 if (*font_list == NUL)
907 font_list_empty = TRUE;
908 else
909 {
910#ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +0100911 // When using a fontset, the whole list of fonts is one name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 if (fontset)
913 ret = gui_mch_init_font(font_list, TRUE);
914 else
915#endif
916 while (*font_list != NUL)
917 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100918 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
920
Bram Moolenaar30613902019-12-01 22:11:18 +0100921 // Careful!!! The Win32 version of gui_mch_init_font(), when
922 // called with "*" will change p_guifont to the selected font
923 // name, which frees the old value. This makes font_list
924 // invalid. Thus when OK is returned here, font_list must no
925 // longer be used!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 if (gui_mch_init_font(font_name, FALSE) == OK)
927 {
K.Takata94373c42022-01-27 15:04:22 +0000928#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar30613902019-12-01 22:11:18 +0100929 // If it's a Unicode font, try setting 'guifontwide' to a
930 // similar double-width font.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
932 && strstr((char *)font_name, "10646") != NULL)
933 set_guifontwide(font_name);
934#endif
935 ret = OK;
936 break;
937 }
938 }
939 }
940
941 if (ret != OK
942 && STRCMP(font_list, "*") != 0
943 && (font_list_empty || gui.norm_font == NOFONT))
944 {
945 /*
946 * Couldn't load any font in 'font_list', keep the current font if
947 * there is one. If 'font_list' is empty, or if there is no current
948 * font, tell gui_mch_init_font() to try to find a font we can load.
949 */
950 ret = gui_mch_init_font(NULL, FALSE);
951 }
952
953 if (ret == OK)
954 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200955#ifndef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100956 // Set normal font as current font
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957# ifdef FEAT_XFONTSET
958 if (gui.fontset != NOFONTSET)
959 gui_mch_set_fontset(gui.fontset);
960 else
961# endif
962 gui_mch_set_font(gui.norm_font);
963#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100964 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966
967 return ret;
968}
969
K.Takata94373c42022-01-27 15:04:22 +0000970#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971/*
972 * Try setting 'guifontwide' to a font twice as wide as "name".
973 */
974 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100975set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 int i = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +0100978 char_u wide_name[FONTLEN + 10]; // room for 2 * width and '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 char_u *wp = NULL;
980 char_u *p;
981 GuiFont font;
982
983 wp = wide_name;
984 for (p = name; *p != NUL; ++p)
985 {
986 *wp++ = *p;
987 if (*p == '-')
988 {
989 ++i;
Bram Moolenaar30613902019-12-01 22:11:18 +0100990 if (i == 6) // font type: change "--" to "-*-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
992 if (p[1] == '-')
993 *wp++ = '*';
994 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100995 else if (i == 12) // found the width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 {
997 ++p;
998 i = getdigits(&p);
999 if (i != 0)
1000 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001001 // Double the width specification.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 sprintf((char *)wp, "%d%s", i * 2, p);
1003 font = gui_mch_get_font(wide_name, FALSE);
1004 if (font != NOFONT)
1005 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001006 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 gui.wide_font = font;
1008 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001009 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 }
1012 break;
1013 }
1014 }
1015 }
1016}
K.Takata94373c42022-01-27 15:04:22 +00001017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
1019/*
1020 * Get the font for 'guifontwide'.
1021 * Return FAIL for an invalid font name.
1022 */
1023 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001024gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
1026 GuiFont font = NOFONT;
1027 char_u font_name[FONTLEN];
1028 char_u *p;
1029
Bram Moolenaar30613902019-12-01 22:11:18 +01001030 if (!gui.in_use) // Can't allocate font yet, assume it's OK.
1031 return OK; // Will give an error message later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1034 {
1035 for (p = p_guifontwide; *p != NUL; )
1036 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001037 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1039 font = gui_mch_get_font(font_name, FALSE);
1040 if (font != NOFONT)
1041 break;
1042 }
1043 if (font == NOFONT)
1044 return FAIL;
1045 }
1046
1047 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001048#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001049 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (font != NOFONT && gui.norm_font != NOFONT
1051 && pango_font_description_equal(font, gui.norm_font))
1052 {
1053 gui.wide_font = NOFONT;
1054 gui_mch_free_font(font);
1055 }
1056 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001059#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001060 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001061#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001062 /*
1063 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1064 * support those fonts for 'guifontwide'.
1065 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 return OK;
1068}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001070#if defined(FEAT_GUI_GTK) || defined(PROTO)
1071/*
1072 * Set list of ascii characters that combined can create ligature.
1073 * Store them in char map for quick access from gui_gtk2_draw_string.
1074 */
1075 void
1076gui_set_ligatures(void)
1077{
1078 char_u *p;
1079
1080 if (*p_guiligatures != NUL)
1081 {
1082 // check for invalid characters
1083 for (p = p_guiligatures; *p != NUL; ++p)
1084 if (*p < 32 || *p > 127)
1085 {
1086 emsg(_(e_ascii_code_not_in_range));
1087 return;
1088 }
1089
1090 // store valid setting into ligatures_map
1091 CLEAR_FIELD(gui.ligatures_map);
1092 for (p = p_guiligatures; *p != NUL; ++p)
1093 gui.ligatures_map[*p] = 1;
1094 }
1095 else
1096 CLEAR_FIELD(gui.ligatures_map);
1097}
Dusan Popovicce59b9f2021-11-22 17:18:44 +00001098
1099/*
1100 * Adjust the columns to undraw for when the cursor is on ligatures.
1101 */
1102 static void
1103gui_adjust_undraw_cursor_for_ligatures(int *startcol, int *endcol)
1104{
1105 int off;
1106
1107 if (ScreenLines == NULL || *p_guiligatures == NUL)
1108 return;
1109
1110 // expand before the cursor for all the chars in gui.ligatures_map
1111 off = LineOffset[gui.cursor_row] + *startcol;
1112 if (gui.ligatures_map[ScreenLines[off]])
1113 while (*startcol > 0 && gui.ligatures_map[ScreenLines[--off]])
1114 (*startcol)--;
1115
1116 // expand after the cursor for all the chars in gui.ligatures_map
1117 off = LineOffset[gui.cursor_row] + *endcol;
1118 if (gui.ligatures_map[ScreenLines[off]])
1119 while (*endcol < ((int)screen_Columns - 1)
1120 && gui.ligatures_map[ScreenLines[++off]])
1121 (*endcol)++;
1122}
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001123#endif
1124
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001125 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001126gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
1128 gui.row = row;
1129 gui.col = col;
1130}
1131
1132/*
1133 * gui_check_pos - check if the cursor is on the screen.
1134 */
1135 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001136gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137{
1138 if (gui.row >= screen_Rows)
1139 gui.row = screen_Rows - 1;
1140 if (gui.col >= screen_Columns)
1141 gui.col = screen_Columns - 1;
1142 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1143 gui.cursor_is_valid = FALSE;
1144}
1145
1146/*
1147 * Redraw the cursor if necessary or when forced.
1148 * Careful: The contents of ScreenLines[] must match what is on the screen,
1149 * otherwise this goes wrong. May need to call out_flush() first.
1150 */
1151 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001152gui_update_cursor(
Bram Moolenaar30613902019-12-01 22:11:18 +01001153 int force, // when TRUE, update even when not moved
1154 int clear_selection) // clear selection under cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155{
1156 int cur_width = 0;
1157 int cur_height = 0;
1158 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001159 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001161#ifdef FEAT_TERMINAL
1162 guicolor_T shape_fg = INVALCOLOR;
1163 guicolor_T shape_bg = INVALCOLOR;
1164#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001165 guicolor_T cfg, cbg, cc; // cursor fore-/background color
1166 int cattr; // cursor attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 int attr;
1168 attrentry_T *aep = NULL;
1169
Bram Moolenaar30613902019-12-01 22:11:18 +01001170 // Don't update the cursor when halfway busy scrolling or the screen size
1171 // doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then.
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001172 if (!can_update_cursor || screen_Columns != gui.num_cols
1173 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 return;
1175
1176 gui_check_pos();
1177 if (!gui.cursor_is_valid || force
1178 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1179 {
1180 gui_undraw_cursor();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001181
1182 // If a cursor-less sleep is ongoing, leave the cursor invisible
1183 if (cursor_is_sleeping())
1184 return;
1185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (gui.row < 0)
1187 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001188#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1190 im_set_position(gui.row, gui.col);
1191#endif
1192 gui.cursor_row = gui.row;
1193 gui.cursor_col = gui.col;
1194
Bram Moolenaar30613902019-12-01 22:11:18 +01001195 // Only write to the screen after ScreenLines[] has been initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (!screen_cleared || ScreenLines == NULL)
1197 return;
1198
Bram Moolenaar30613902019-12-01 22:11:18 +01001199 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (clear_selection)
1201 clip_may_clear_selection(gui.row, gui.row);
Bram Moolenaar30613902019-12-01 22:11:18 +01001202 // Check that the cursor is inside the shell (resizing may have made
1203 // it invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1205 return;
1206
1207 gui.cursor_is_valid = TRUE;
1208
1209 /*
1210 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001211 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001213#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001214 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001215 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001217#endif
1218 shape = &shape_table[get_shape_idx(FALSE)];
1219 if (State & LANGMAP)
1220 id = shape->id_lm;
1221 else
1222 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
Bram Moolenaar30613902019-12-01 22:11:18 +01001224 // get the colors and attributes for the cursor. Default is inverted
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 cfg = INVALCOLOR;
1226 cbg = INVALCOLOR;
1227 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001228 gui_mch_set_blinking(shape->blinkwait,
1229 shape->blinkon,
1230 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001231 if (shape->blinkwait == 0 || shape->blinkon == 0
1232 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001233 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001234#ifdef FEAT_TERMINAL
1235 if (shape_bg != INVALCOLOR)
1236 {
1237 cattr = 0;
1238 cfg = shape_fg;
1239 cbg = shape_bg;
1240 }
1241 else
1242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (id > 0)
1244 {
1245 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar54612582019-11-21 17:13:31 +01001246#if defined(HAVE_INPUT_METHOD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
1248 static int iid;
1249 guicolor_T fg, bg;
1250
Bram Moolenaarc236c162008-07-13 17:41:49 +00001251 if (
Bram Moolenaar54612582019-11-21 17:13:31 +01001252# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001253 preedit_get_status()
1254# else
1255 im_get_status()
1256# endif
1257 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
1259 iid = syn_name2id((char_u *)"CursorIM");
1260 if (iid > 0)
1261 {
1262 syn_id2colors(iid, &fg, &bg);
1263 if (bg != INVALCOLOR)
1264 cbg = bg;
1265 if (fg != INVALCOLOR)
1266 cfg = fg;
1267 }
1268 }
1269 }
1270#endif
1271 }
1272
1273 /*
1274 * Get the attributes for the character under the cursor.
1275 * When no cursor color was given, use the character color.
1276 */
1277 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1278 if (attr > HL_ALL)
1279 aep = syn_gui_attr2entry(attr);
1280 if (aep != NULL)
1281 {
1282 attr = aep->ae_attr;
1283 if (cfg == INVALCOLOR)
1284 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1285 : aep->ae_u.gui.fg_color);
1286 if (cbg == INVALCOLOR)
1287 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1288 : aep->ae_u.gui.bg_color);
1289 }
1290 if (cfg == INVALCOLOR)
1291 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1292 if (cbg == INVALCOLOR)
1293 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1294
1295#ifdef FEAT_XIM
1296 if (aep != NULL)
1297 {
1298 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1299 : aep->ae_u.gui.bg_color);
1300 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1301 : aep->ae_u.gui.fg_color);
1302 if (xim_bg_color == INVALCOLOR)
1303 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1304 : gui.back_pixel;
1305 if (xim_fg_color == INVALCOLOR)
1306 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1307 : gui.norm_pixel;
1308 }
1309 else
1310 {
1311 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1312 : gui.back_pixel;
1313 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1314 : gui.norm_pixel;
1315 }
1316#endif
1317
1318 attr &= ~HL_INVERSE;
1319 if (cattr & HL_INVERSE)
1320 {
1321 cc = cbg;
1322 cbg = cfg;
1323 cfg = cc;
1324 }
1325 cattr &= ~HL_INVERSE;
1326
1327 /*
1328 * When we don't have window focus, draw a hollow cursor.
1329 */
1330 if (!gui.in_focus)
1331 {
1332 gui_mch_draw_hollow_cursor(cbg);
1333 return;
1334 }
1335
1336 old_hl_mask = gui.highlight_mask;
Bram Moolenaar54612582019-11-21 17:13:31 +01001337 if (shape->shape == SHAPE_BLOCK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 {
1339 /*
1340 * Draw the text character with the cursor colors. Use the
1341 * character attributes plus the cursor attributes.
1342 */
1343 gui.highlight_mask = (cattr | attr);
Bram Moolenaar54612582019-11-21 17:13:31 +01001344 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1346 }
1347 else
1348 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001349#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 int col_off = FALSE;
1351#endif
1352 /*
1353 * First draw the partial cursor, then overwrite with the text
1354 * character, using a transparent background.
1355 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001356 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
1358 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001359 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361 else
1362 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001363 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 cur_width = gui.char_width;
1365 }
Bram Moolenaar367329b2007-08-30 11:53:22 +00001366 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1367 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001369 // Double wide character.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001370 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001372#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (CURSOR_BAR_RIGHT)
1374 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001375 // gui.col points to the left half of the character but
1376 // the vertical line needs to be on the right half.
Bram Moolenaar30613902019-12-01 22:11:18 +01001377 // A double-wide horizontal line is also drawn from the
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001378 // right half in gui_mch_draw_part_cursor().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 col_off = TRUE;
1380 ++gui.col;
1381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001385#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 if (col_off)
1387 --gui.col;
1388#endif
1389
Bram Moolenaar30613902019-12-01 22:11:18 +01001390#ifndef FEAT_GUI_MSWIN // doesn't seem to work for MSWindows
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1392 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1393 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1394 (guicolor_T)0, (guicolor_T)0, 0);
1395#endif
1396 }
1397 gui.highlight_mask = old_hl_mask;
1398 }
1399}
1400
1401#if defined(FEAT_MENU) || defined(PROTO)
1402 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001403gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001405# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (gui.menu_is_active && gui.in_use)
1407 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1408# endif
1409}
1410#endif
1411
1412/*
1413 * Position the various GUI components (text area, menu). The vertical
1414 * scrollbars are NOT handled here. See gui_update_scrollbars().
1415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001417gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
1419 int text_area_x;
1420 int text_area_y;
1421 int text_area_width;
1422 int text_area_height;
1423
Bram Moolenaar30613902019-12-01 22:11:18 +01001424 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 ++hold_gui_events;
1426
1427 text_area_x = 0;
1428 if (gui.which_scrollbars[SBAR_LEFT])
1429 text_area_x += gui.scrollbar_width;
1430
1431 text_area_y = 0;
1432#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1433 gui.menu_width = total_width;
1434 if (gui.menu_is_active)
1435 text_area_y += gui.menu_height;
1436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001438# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001439 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001440 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001441 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001442#endif
1443
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001444#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA) \
K.Takatac81e9bf2022-01-16 14:15:49 +00001445 || defined(FEAT_GUI_HAIKU) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1447 {
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001448# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 gui_mch_set_toolbar_pos(0, text_area_y,
1450 gui.menu_width, gui.toolbar_height);
1451# endif
1452 text_area_y += gui.toolbar_height;
1453 }
1454#endif
1455
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001456# if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_HAIKU)
1457 gui_mch_set_tabline_pos(0, text_area_y,
1458 gui.menu_width, gui.tabline_height);
1459 if (gui_has_tabline())
1460 text_area_y += gui.tabline_height;
1461#endif
1462
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1464 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1465
1466 gui_mch_set_text_area_pos(text_area_x,
1467 text_area_y,
1468 text_area_width,
1469 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001470#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 + xim_get_status_area_height()
1472#endif
1473 );
1474#ifdef FEAT_MENU
1475 gui_position_menu();
1476#endif
1477 if (gui.which_scrollbars[SBAR_BOTTOM])
1478 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1479 text_area_x,
Bram Moolenaar203ec772020-07-17 20:43:43 +02001480 text_area_y + text_area_height
1481 + gui_mch_get_scrollbar_ypadding(),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 text_area_width,
1483 gui.scrollbar_height);
1484 gui.left_sbar_x = 0;
Bram Moolenaar203ec772020-07-17 20:43:43 +02001485 gui.right_sbar_x = text_area_x + text_area_width
1486 + gui_mch_get_scrollbar_xpadding();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
1488 --hold_gui_events;
1489}
1490
Bram Moolenaar02743632005-07-25 20:42:36 +00001491/*
1492 * Get the width of the widgets and decorations to the side of the text area.
1493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001495gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 int base_width;
1498
1499 base_width = 2 * gui.border_offset;
1500 if (gui.which_scrollbars[SBAR_LEFT])
1501 base_width += gui.scrollbar_width;
1502 if (gui.which_scrollbars[SBAR_RIGHT])
1503 base_width += gui.scrollbar_width;
1504 return base_width;
1505}
1506
Bram Moolenaar02743632005-07-25 20:42:36 +00001507/*
1508 * Get the height of the widgets and decorations above and below the text area.
1509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001511gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
1513 int base_height;
1514
1515 base_height = 2 * gui.border_offset;
1516 if (gui.which_scrollbars[SBAR_BOTTOM])
1517 base_height += gui.scrollbar_height;
1518#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001519 // We can't take the sizes properly into account until anything is
1520 // realized. Therefore we recalculate all the values here just before
1521 // setting the size. (--mdcki)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522#else
1523# ifdef FEAT_MENU
1524 if (gui.menu_is_active)
1525 base_height += gui.menu_height;
1526# endif
1527# ifdef FEAT_TOOLBAR
1528 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 base_height += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001531# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001532 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001533 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001534 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001535# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536# ifdef FEAT_FOOTER
1537 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1538 base_height += gui.footer_height;
1539# endif
1540# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1541 base_height += gui_mch_text_area_extra_height();
1542# endif
1543#endif
1544 return base_height;
1545}
1546
1547/*
1548 * Should be called after the GUI shell has been resized. Its arguments are
1549 * the new width and height of the shell in pixels.
1550 */
1551 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001552gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
1554 static int busy = FALSE;
1555
Bram Moolenaar30613902019-12-01 22:11:18 +01001556 if (!gui.shell_created) // ignore when still initializing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 return;
1558
1559 /*
1560 * Can't resize the screen while it is being redrawn. Remember the new
1561 * size and handle it later.
1562 */
1563 if (updating_screen || busy)
1564 {
1565 new_pixel_width = pixel_width;
1566 new_pixel_height = pixel_height;
1567 return;
1568 }
1569
1570again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001571 new_pixel_width = 0;
1572 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 busy = TRUE;
1574
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001575#ifdef FEAT_GUI_HAIKU
1576 vim_lock_screen();
1577#endif
1578
Bram Moolenaar30613902019-12-01 22:11:18 +01001579 // Flush pending output before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 out_flush();
1581
1582 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001583 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
1585 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 /*
1589 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1590 * at the last line here (why does it have to be one row too low?).
1591 */
1592 if (State == ASKMORE || State == CONFIRM)
1593 gui.row = gui.num_rows;
1594
Bram Moolenaar30613902019-12-01 22:11:18 +01001595 // Only comparing Rows and Columns may be sufficient, but let's stay on
1596 // the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1598 || gui.num_rows != Rows || gui.num_cols != Columns)
1599 shell_resized();
1600
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001601#ifdef FEAT_GUI_HAIKU
1602 vim_unlock_screen();
1603#endif
1604
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 gui_update_scrollbars(TRUE);
1606 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001607#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 xim_set_status_area();
1609#endif
1610
1611 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001612
Bram Moolenaar30613902019-12-01 22:11:18 +01001613 // We may have been called again while redrawing the screen.
1614 // Need to do it all again with the latest size then. But only if the size
1615 // actually changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (new_pixel_height)
1617 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001618 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1619 {
1620 new_pixel_width = 0;
1621 new_pixel_height = 0;
1622 }
1623 else
1624 {
1625 pixel_width = new_pixel_width;
1626 pixel_height = new_pixel_height;
1627 goto again;
1628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 }
1630}
1631
1632/*
1633 * Check if gui_resize_shell() must be called.
1634 */
1635 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001636gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (new_pixel_height)
Bram Moolenaar30613902019-12-01 22:11:18 +01001639 // careful: gui_resize_shell() may postpone the resize again if we
1640 // were called indirectly by it
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001641 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642}
1643
1644 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001645gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646{
1647 Rows = gui.num_rows;
1648 Columns = gui.num_cols;
1649 return OK;
1650}
1651
1652/*
1653 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001654 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1655 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001656 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1657 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001660gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001661 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001662 int fit_to_display,
Bram Moolenaar30613902019-12-01 22:11:18 +01001663 int direction) // RESIZE_HOR, RESIZE_VER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
1665 int base_width;
1666 int base_height;
1667 int width;
1668 int height;
1669 int min_width;
1670 int min_height;
1671 int screen_w;
1672 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001673#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001674 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001675 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001676#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001677 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679 if (!gui.shell_created)
1680 return;
1681
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001682#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01001683 // If not setting to a user specified size and maximized, calculate the
1684 // number of characters that fit in the maximized window.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001685 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1686 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
1688 gui_mch_newfont();
1689 return;
1690 }
1691#endif
1692
1693 base_width = gui_get_base_width();
1694 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001695 if (fit_to_display)
Bram Moolenaar30613902019-12-01 22:11:18 +01001696 // Remember the original window position.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001697 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001698
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001699 width = Columns * gui.char_width + base_width;
1700 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
1702 if (fit_to_display)
1703 {
1704 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001705 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
1707 Columns = (screen_w - base_width) / gui.char_width;
1708 if (Columns < MIN_COLUMNS)
1709 Columns = MIN_COLUMNS;
1710 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001711#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001712 ++did_adjust;
1713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001715 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 {
1717 Rows = (screen_h - base_height) / gui.char_height;
1718 check_shellsize();
1719 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001720#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001721 ++did_adjust;
1722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001724#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001725 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1726 && height + gui.char_height >= screen_h))
Bram Moolenaar30613902019-12-01 22:11:18 +01001727 // don't unmaximize if at maximum size
Bram Moolenaar09736232009-09-23 16:14:49 +00001728 un_maximize = FALSE;
1729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001731 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 gui.num_cols = Columns;
1733 gui.num_rows = Rows;
1734
1735 min_width = base_width + MIN_COLUMNS * gui.char_width;
1736 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001737 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001738
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001739#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001740 if (un_maximize)
1741 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001742 // If the window size is smaller than the screen unmaximize the
1743 // window, otherwise resizing won't work.
Bram Moolenaar09736232009-09-23 16:14:49 +00001744 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1745 if ((width + gui.char_width < screen_w
1746 || height + gui.char_height * 2 < screen_h)
1747 && gui_mch_maximized())
1748 gui_mch_unmaximize();
1749 }
1750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
1752 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001753 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001755 if (fit_to_display && x >= 0 && y >= 0)
1756 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001757 // Some window managers put the Vim window left of/above the screen.
1758 // Only change the position if it wasn't already negative before
1759 // (happens on MS-Windows with a secondary monitor).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 gui_mch_update();
1761 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1762 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1763 }
1764
1765 gui_position_components(width);
1766 gui_update_scrollbars(TRUE);
1767 gui_reset_scroll_region();
1768}
1769
1770/*
1771 * Called when Rows and/or Columns has changed.
1772 */
1773 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001774gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
1776 gui_reset_scroll_region();
1777}
1778
1779/*
1780 * Make scroll region cover whole screen.
1781 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001782 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001783gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 gui.scroll_region_top = 0;
1786 gui.scroll_region_bot = gui.num_rows - 1;
1787 gui.scroll_region_left = 0;
1788 gui.scroll_region_right = gui.num_cols - 1;
1789}
1790
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001791 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001792gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
Bram Moolenaar30613902019-12-01 22:11:18 +01001794 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 gui.highlight_mask = mask;
Bram Moolenaar30613902019-12-01 22:11:18 +01001796 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 gui.highlight_mask |= mask;
1798}
1799
1800 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001801gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802{
Bram Moolenaar30613902019-12-01 22:11:18 +01001803 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 gui.highlight_mask = HL_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001805 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 gui.highlight_mask &= ~mask;
1807}
1808
1809/*
1810 * Clear a rectangular region of the screen from text pos (row1, col1) to
1811 * (row2, col2) inclusive.
1812 */
1813 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001814gui_clear_block(
1815 int row1,
1816 int col1,
1817 int row2,
1818 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819{
Bram Moolenaar30613902019-12-01 22:11:18 +01001820 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 clip_may_clear_selection(row1, row2);
1822
1823 gui_mch_clear_block(row1, col1, row2, col2);
1824
Bram Moolenaar30613902019-12-01 22:11:18 +01001825 // Invalidate cursor if it was in this block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1827 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1828 gui.cursor_is_valid = FALSE;
1829}
1830
1831/*
1832 * Write code to update the cursor later. This avoids the need to flush the
1833 * output buffer before calling gui_update_cursor().
1834 */
1835 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001836gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001838 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839}
1840
1841 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001842gui_write(
1843 char_u *s,
1844 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845{
1846 char_u *p;
1847 int arg1 = 0, arg2 = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01001848 int force_cursor = FALSE; // force cursor update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 int force_scrollbar = FALSE;
1850 static win_T *old_curwin = NULL;
1851
Bram Moolenaar30613902019-12-01 22:11:18 +01001852// #define DEBUG_GUI_WRITE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853#ifdef DEBUG_GUI_WRITE
1854 {
1855 int i;
1856 char_u *str;
1857
1858 printf("gui_write(%d):\n ", len);
1859 for (i = 0; i < len; i++)
1860 if (s[i] == ESC)
1861 {
1862 if (i != 0)
1863 printf("\n ");
1864 printf("<ESC>");
1865 }
1866 else
1867 {
1868 str = transchar_byte(s[i]);
1869 if (str[0] && str[1])
1870 printf("<%s>", (char *)str);
1871 else
1872 printf("%s", (char *)str);
1873 }
1874 printf("\n");
1875 }
1876#endif
1877 while (len)
1878 {
1879 if (s[0] == ESC && s[1] == '|')
1880 {
1881 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001882 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
1884 arg1 = getdigits(&p);
1885 if (p > s + len)
1886 break;
1887 if (*p == ';')
1888 {
1889 ++p;
1890 arg2 = getdigits(&p);
1891 if (p > s + len)
1892 break;
1893 }
1894 }
1895 switch (*p)
1896 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001897 case 'C': // Clear screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 clip_scroll_selection(9999);
1899 gui_mch_clear_all();
1900 gui.cursor_is_valid = FALSE;
1901 force_scrollbar = TRUE;
1902 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001903 case 'M': // Move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 gui_set_cursor(arg1, arg2);
1905 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001906 case 's': // force cursor (shape) update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 force_cursor = TRUE;
1908 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001909 case 'R': // Set scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (arg1 < arg2)
1911 {
1912 gui.scroll_region_top = arg1;
1913 gui.scroll_region_bot = arg2;
1914 }
1915 else
1916 {
1917 gui.scroll_region_top = arg2;
1918 gui.scroll_region_bot = arg1;
1919 }
1920 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001921 case 'V': // Set vertical scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (arg1 < arg2)
1923 {
1924 gui.scroll_region_left = arg1;
1925 gui.scroll_region_right = arg2;
1926 }
1927 else
1928 {
1929 gui.scroll_region_left = arg2;
1930 gui.scroll_region_right = arg1;
1931 }
1932 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001933 case 'd': // Delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 gui_delete_lines(gui.row, 1);
1935 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001936 case 'D': // Delete lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 gui_delete_lines(gui.row, arg1);
1938 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001939 case 'i': // Insert line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 gui_insert_lines(gui.row, 1);
1941 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001942 case 'I': // Insert lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 gui_insert_lines(gui.row, arg1);
1944 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001945 case '$': // Clear to end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 gui_clear_block(gui.row, gui.col, gui.row,
1947 (int)Columns - 1);
1948 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001949 case 'h': // Turn on highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 gui_start_highlight(arg1);
1951 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001952 case 'H': // Turn off highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 gui_stop_highlight(arg1);
1954 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001955 case 'f': // flash the window (visual bell)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1957 break;
1958 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01001959 p = s + 1; // Skip the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 break;
1961 }
1962 len -= (int)(++p - s);
1963 s = p;
1964 }
1965 else if (
1966#ifdef EBCDIC
Bram Moolenaar30613902019-12-01 22:11:18 +01001967 CtrlChar(s[0]) != 0 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968#else
Bram Moolenaar30613902019-12-01 22:11:18 +01001969 s[0] < 0x20 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970#endif
1971#ifdef FEAT_SIGN_ICONS
1972 && s[0] != SIGN_BYTE
1973# ifdef FEAT_NETBEANS_INTG
1974 && s[0] != MULTISIGN_BYTE
1975# endif
1976#endif
1977 )
1978 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001979 if (s[0] == '\n') // NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
1981 gui.col = 0;
1982 if (gui.row < gui.scroll_region_bot)
1983 gui.row++;
1984 else
1985 gui_delete_lines(gui.scroll_region_top, 1);
1986 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001987 else if (s[0] == '\r') // CR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
1989 gui.col = 0;
1990 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001991 else if (s[0] == '\b') // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
1993 if (gui.col)
1994 --gui.col;
1995 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001996 else if (s[0] == Ctrl_L) // cursor-right
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
1998 ++gui.col;
1999 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002000 else if (s[0] == Ctrl_G) // Beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
2002 gui_mch_beep();
2003 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002004 // Other Ctrl character: shouldn't happen!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
Bram Moolenaar30613902019-12-01 22:11:18 +01002006 --len; // Skip this char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 ++s;
2008 }
2009 else
2010 {
2011 p = s;
2012 while (len > 0 && (
2013#ifdef EBCDIC
2014 CtrlChar(*p) == 0
2015#else
2016 *p >= 0x20
2017#endif
2018#ifdef FEAT_SIGN_ICONS
2019 || *p == SIGN_BYTE
2020# ifdef FEAT_NETBEANS_INTG
2021 || *p == MULTISIGN_BYTE
2022# endif
2023#endif
2024 ))
2025 {
2026 len--;
2027 p++;
2028 }
2029 gui_outstr(s, (int)(p - s));
2030 s = p;
2031 }
2032 }
2033
Bram Moolenaar30613902019-12-01 22:11:18 +01002034 // Postponed update of the cursor (won't work if "can_update_cursor" isn't
2035 // set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (force_cursor)
2037 gui_update_cursor(TRUE, TRUE);
2038
Bram Moolenaar30613902019-12-01 22:11:18 +01002039 // When switching to another window the dragging must have stopped.
2040 // Required for GTK, dragged_sb isn't reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 if (old_curwin != curwin)
2042 gui.dragged_sb = SBAR_NONE;
2043
Bram Moolenaar30613902019-12-01 22:11:18 +01002044 // Update the scrollbars after clearing the screen or when switched
2045 // to another window.
2046 // Update the horizontal scrollbar always, it's difficult to check all
2047 // situations where it might change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (force_scrollbar || old_curwin != curwin)
2049 gui_update_scrollbars(force_scrollbar);
2050 else
2051 gui_update_horiz_scrollbar(FALSE);
2052 old_curwin = curwin;
2053
2054 /*
2055 * We need to make sure this is cleared since Athena doesn't tell us when
2056 * he is done dragging. Do the same for GTK.
2057 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002058#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 gui.dragged_sb = SBAR_NONE;
2060#endif
2061
Bram Moolenaar30613902019-12-01 22:11:18 +01002062 gui_may_flush(); // In case vim decides to take a nap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063}
2064
2065/*
2066 * When ScreenLines[] is invalid, updating the cursor should not be done, it
2067 * produces wrong results. Call gui_dont_update_cursor() before that code and
2068 * gui_can_update_cursor() afterwards.
2069 */
2070 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02002071gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072{
2073 if (gui.in_use)
2074 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002075 // Undraw the cursor now, we probably can't do it after the change.
Bram Moolenaar107abd22016-08-12 14:08:25 +02002076 if (undraw)
2077 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 can_update_cursor = FALSE;
2079 }
2080}
2081
2082 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002083gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
2085 can_update_cursor = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002086 // No need to update the cursor right now, there is always more output
2087 // after scrolling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088}
2089
Bram Moolenaara338adc2018-01-31 20:51:47 +01002090/*
2091 * Disable issuing gui_mch_flush().
2092 */
2093 void
2094gui_disable_flush(void)
2095{
2096 ++disable_flush;
2097}
2098
2099/*
2100 * Enable issuing gui_mch_flush().
2101 */
2102 void
2103gui_enable_flush(void)
2104{
2105 --disable_flush;
2106}
2107
2108/*
2109 * Issue gui_mch_flush() if it is not disabled.
2110 */
2111 void
2112gui_may_flush(void)
2113{
2114 if (disable_flush == 0)
2115 gui_mch_flush();
2116}
2117
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002119gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120{
2121 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
2124 if (len == 0)
2125 return;
2126
2127 if (len < 0)
2128 len = (int)STRLEN(s);
2129
2130 while (len > 0)
2131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (has_mbyte)
2133 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002134 // Find out how many chars fit in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 cells = 0;
2136 for (this_len = 0; this_len < len; )
2137 {
2138 cells += (*mb_ptr2cells)(s + this_len);
2139 if (gui.col + cells > Columns)
2140 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002141 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143 if (this_len > len)
Bram Moolenaar30613902019-12-01 22:11:18 +01002144 this_len = len; // don't include following composing char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (gui.col + len > Columns)
2148 this_len = Columns - gui.col;
2149 else
2150 this_len = len;
2151
2152 (void)gui_outstr_nowrap(s, this_len,
2153 0, (guicolor_T)0, (guicolor_T)0, 0);
2154 s += this_len;
2155 len -= this_len;
Bram Moolenaar30613902019-12-01 22:11:18 +01002156 // fill up for a double-width char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 if (len > 0 && gui.col < Columns)
2158 (void)gui_outstr_nowrap((char_u *)" ", 1,
2159 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01002160 // The cursor may wrap to the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 if (gui.col >= Columns)
2162 {
2163 gui.col = 0;
2164 gui.row++;
2165 }
2166 }
2167}
2168
2169/*
2170 * Output one character (may be one or two display cells).
2171 * Caller must check for valid "off".
2172 * Returns FAIL or OK, just like gui_outstr_nowrap().
2173 */
2174 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002175gui_screenchar(
Bram Moolenaar30613902019-12-01 22:11:18 +01002176 int off, // Offset from start of screen
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002177 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002178 guicolor_T fg, // colors for cursor
2179 guicolor_T bg, // colors for cursor
2180 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 char_u buf[MB_MAXBYTES + 1];
2183
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002184 // Don't draw right half of a double-width UTF-8 char. "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 if (enc_utf8 && ScreenLines[off] == 0)
2186 return OK;
2187
2188 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002189 // Draw UTF-8 multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2191 flags, fg, bg, back);
2192
2193 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2194 {
2195 buf[0] = ScreenLines[off];
2196 buf[1] = ScreenLines2[off];
2197 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2198 }
2199
Bram Moolenaar30613902019-12-01 22:11:18 +01002200 // Draw non-multi-byte character or DBCS character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002202 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204}
2205
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002206#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207/*
2208 * Output the string at the given screen position. This is used in place
2209 * of gui_screenchar() where possible because Pango needs as much context
2210 * as possible to work nicely. It's a lot faster as well.
2211 */
2212 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002213gui_screenstr(
Bram Moolenaar30613902019-12-01 22:11:18 +01002214 int off, // Offset from start of screen
2215 int len, // string length in screen cells
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002216 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002217 guicolor_T fg, // colors for cursor
2218 guicolor_T bg, // colors for cursor
2219 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220{
2221 char_u *buf;
2222 int outlen = 0;
2223 int i;
2224 int retval;
2225
Bram Moolenaar30613902019-12-01 22:11:18 +01002226 if (len <= 0) // "cannot happen"?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 return OK;
2228
2229 if (enc_utf8)
2230 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002231 buf = alloc(len * MB_MAXBYTES + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002233 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234
2235 for (i = off; i < off + len; ++i)
2236 {
2237 if (ScreenLines[i] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002238 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240 if (ScreenLinesUC[i] == 0)
2241 buf[outlen++] = ScreenLines[i];
2242 else
2243 outlen += utfc_char2bytes(i, buf + outlen);
2244 }
2245
Bram Moolenaar30613902019-12-01 22:11:18 +01002246 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2248 vim_free(buf);
2249
2250 return retval;
2251 }
2252 else if (enc_dbcs == DBCS_JPNU)
2253 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002254 buf = alloc(len * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002256 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257
2258 for (i = off; i < off + len; ++i)
2259 {
2260 buf[outlen++] = ScreenLines[i];
2261
Bram Moolenaar30613902019-12-01 22:11:18 +01002262 // handle double-byte single-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 if (ScreenLines[i] == 0x8e)
2264 buf[outlen++] = ScreenLines2[i];
2265 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2266 buf[outlen++] = ScreenLines[++i];
2267 }
2268
Bram Moolenaar30613902019-12-01 22:11:18 +01002269 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2271 vim_free(buf);
2272
2273 return retval;
2274 }
2275 else
2276 {
2277 return gui_outstr_nowrap(&ScreenLines[off], len,
2278 flags, fg, bg, back);
2279 }
2280}
Bram Moolenaar30613902019-12-01 22:11:18 +01002281#endif // FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
2283/*
2284 * Output the given string at the current cursor position. If the string is
2285 * too long to fit on the line, then it is truncated.
2286 * "flags":
2287 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2288 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002289 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 * background.
2291 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2292 * it.
2293 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2294 * FAIL (the caller should start drawing "back" chars back).
2295 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002296 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002297gui_outstr_nowrap(
2298 char_u *s,
2299 int len,
2300 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002301 guicolor_T fg, // colors for cursor
2302 guicolor_T bg, // colors for cursor
2303 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
2305 long_u highlight_mask;
2306 long_u hl_mask_todo;
2307 guicolor_T fg_color;
2308 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002309 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002312 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313# ifdef FEAT_XFONTSET
2314 GuiFontset fontset = NOFONTSET;
2315# endif
2316#endif
2317 attrentry_T *aep = NULL;
2318 int draw_flags;
2319 int col = gui.col;
2320#ifdef FEAT_SIGN_ICONS
2321 int draw_sign = FALSE;
Bram Moolenaarc7283072019-07-16 20:12:44 +02002322 int signcol = 0;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002323 char_u extra[18];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324# ifdef FEAT_NETBEANS_INTG
2325 int multi_sign = FALSE;
2326# endif
2327#endif
2328
2329 if (len < 0)
2330 len = (int)STRLEN(s);
2331 if (len == 0)
2332 return OK;
2333
2334#ifdef FEAT_SIGN_ICONS
2335 if (*s == SIGN_BYTE
2336# ifdef FEAT_NETBEANS_INTG
2337 || *s == MULTISIGN_BYTE
2338# endif
Bram Moolenaar0231f832019-07-12 19:22:22 +02002339 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
2341# ifdef FEAT_NETBEANS_INTG
2342 if (*s == MULTISIGN_BYTE)
2343 multi_sign = TRUE;
2344# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002345 // draw spaces instead
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002346 if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
2347 (curwin->w_p_nu || curwin->w_p_rnu))
2348 {
2349 sprintf((char *)extra, "%*c ", number_width(curwin), ' ');
2350 s = extra;
2351 }
2352 else
2353 s = (char_u *)" ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 if (len == 1 && col > 0)
2355 --col;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002356 len = (int)STRLEN(s);
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002357 if (len > 2)
Bram Moolenaar0231f832019-07-12 19:22:22 +02002358 // right align sign icon in the number column
2359 signcol = col + len - 3;
2360 else
2361 signcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 draw_sign = TRUE;
2363 highlight_mask = 0;
2364 }
2365 else
2366#endif
2367 if (gui.highlight_mask > HL_ALL)
2368 {
2369 aep = syn_gui_attr2entry(gui.highlight_mask);
Bram Moolenaar30613902019-12-01 22:11:18 +01002370 if (aep == NULL) // highlighting not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 highlight_mask = 0;
2372 else
2373 highlight_mask = aep->ae_attr;
2374 }
2375 else
2376 highlight_mask = gui.highlight_mask;
2377 hl_mask_todo = highlight_mask;
2378
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002379#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002380 // Set the font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2382 font = aep->ae_u.gui.font;
2383# ifdef FEAT_XFONTSET
2384 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2385 fontset = aep->ae_u.gui.fontset;
2386# endif
2387 else
2388 {
2389# ifdef FEAT_XFONTSET
2390 if (gui.fontset != NOFONTSET)
2391 fontset = gui.fontset;
2392 else
2393# endif
2394 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2395 {
2396 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2397 {
2398 font = gui.boldital_font;
2399 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2400 }
2401 else if (gui.bold_font != NOFONT)
2402 {
2403 font = gui.bold_font;
2404 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2405 }
2406 else
2407 font = gui.norm_font;
2408 }
2409 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2410 {
2411 font = gui.ital_font;
2412 hl_mask_todo &= ~HL_ITALIC;
2413 }
2414 else
2415 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002416
Bram Moolenaardb250522013-06-17 22:43:25 +02002417 /*
2418 * Choose correct wide_font by font. wide_font should be set with font
2419 * at same time in above block. But it will make many "ifdef" nasty
2420 * blocks. So we do it here.
2421 */
2422 if (font == gui.boldital_font && gui.wide_boldital_font)
2423 wide_font = gui.wide_boldital_font;
2424 else if (font == gui.bold_font && gui.wide_bold_font)
2425 wide_font = gui.wide_bold_font;
2426 else if (font == gui.ital_font && gui.wide_ital_font)
2427 wide_font = gui.wide_ital_font;
2428 else if (font == gui.norm_font && gui.wide_font)
2429 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 }
2431# ifdef FEAT_XFONTSET
2432 if (fontset != NOFONTSET)
2433 gui_mch_set_fontset(fontset);
2434 else
2435# endif
2436 gui_mch_set_font(font);
2437#endif
2438
2439 draw_flags = 0;
2440
Bram Moolenaar30613902019-12-01 22:11:18 +01002441 // Set the color
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 bg_color = gui.back_pixel;
2443 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2444 {
2445 draw_flags |= DRAW_CURSOR;
2446 fg_color = fg;
2447 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002448 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450 else if (aep != NULL)
2451 {
2452 fg_color = aep->ae_u.gui.fg_color;
2453 if (fg_color == INVALCOLOR)
2454 fg_color = gui.norm_pixel;
2455 bg_color = aep->ae_u.gui.bg_color;
2456 if (bg_color == INVALCOLOR)
2457 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002458 sp_color = aep->ae_u.gui.sp_color;
2459 if (sp_color == INVALCOLOR)
2460 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
2462 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002465 sp_color = fg_color;
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467
2468 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 gui_mch_set_fg_color(bg_color);
2471 gui_mch_set_bg_color(fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 }
2473 else
2474 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 gui_mch_set_fg_color(fg_color);
2476 gui_mch_set_bg_color(bg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002478 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
Bram Moolenaar30613902019-12-01 22:11:18 +01002480 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 if (!(flags & GUI_MON_NOCLEAR))
2482 clip_may_clear_selection(gui.row, gui.row);
2483
2484
Bram Moolenaar30613902019-12-01 22:11:18 +01002485 // If there's no bold font, then fake it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2487 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489 /*
2490 * When drawing bold or italic characters the spill-over from the left
2491 * neighbor may be destroyed. Let the caller backup to start redrawing
2492 * just after a blank.
2493 */
2494 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2495 return FAIL;
2496
Bram Moolenaare60acc12011-05-10 16:41:25 +02002497#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002498 // If there's no italic font, then fake it.
2499 // For GTK2, we don't need a different font for italic style.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 if (hl_mask_todo & HL_ITALIC)
2501 draw_flags |= DRAW_ITALIC;
2502
Bram Moolenaar30613902019-12-01 22:11:18 +01002503 // Do we underline the text?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 if (hl_mask_todo & HL_UNDERLINE)
2505 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507#else
Bram Moolenaar30613902019-12-01 22:11:18 +01002508 // Do we underline the text?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002509 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 draw_flags |= DRAW_UNDERL;
2511#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002512 // Do we undercurl the text?
Bram Moolenaar3918c952005-03-15 22:34:55 +00002513 if (hl_mask_todo & HL_UNDERCURL)
2514 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
Bram Moolenaar30613902019-12-01 22:11:18 +01002516 // Do we strikethrough the text?
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002517 if (hl_mask_todo & HL_STRIKETHROUGH)
2518 draw_flags |= DRAW_STRIKE;
2519
Bram Moolenaar30613902019-12-01 22:11:18 +01002520 // Do we draw transparently?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (flags & GUI_MON_TRS_CURSOR)
2522 draw_flags |= DRAW_TRANSP;
2523
2524 /*
2525 * Draw the text.
2526 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002527#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01002528 // The value returned is the length in display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2530#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (enc_utf8)
2532 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002533 int start; // index of bytes to be drawn
2534 int cells; // cellwidth of bytes to be drawn
2535 int thislen; // length of bytes to be drawn
2536 int cn; // cellwidth of current char
2537 int i; // index of current char
2538 int c; // current char value
2539 int cl; // byte length of current char
2540 int comping; // current char is composing
2541 int scol = col; // screen column
2542 int curr_wide = FALSE; // use 'guifontwide'
Bram Moolenaar45933962013-01-23 17:43:57 +01002543 int prev_wide = FALSE;
2544 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002545# ifdef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01002546 int sep_comp = FALSE; // Don't separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002547# else
Bram Moolenaar30613902019-12-01 22:11:18 +01002548 int sep_comp = TRUE; // Separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550
Bram Moolenaar30613902019-12-01 22:11:18 +01002551 // Break the string at a composing character, it has to be drawn on
2552 // top of the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 start = 0;
2554 cells = 0;
2555 for (i = 0; i < len; i += cl)
2556 {
2557 c = utf_ptr2char(s + i);
2558 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 comping = utf_iscomposing(c);
Bram Moolenaar30613902019-12-01 22:11:18 +01002560 if (!comping) // count cells from non-composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002562 if (!comping || sep_comp)
2563 {
2564 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002565# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002566 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002567# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002568 && wide_font != NOFONT)
2569 curr_wide = TRUE;
2570 else
2571 curr_wide = FALSE;
2572 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002573 cl = utf_ptr2len(s + i);
Bram Moolenaar30613902019-12-01 22:11:18 +01002574 if (cl == 0) // hit end of string
2575 len = i + cl; // len must be wrong "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576
Bram Moolenaar45933962013-01-23 17:43:57 +01002577 wide_changed = curr_wide != prev_wide;
2578
Bram Moolenaar30613902019-12-01 22:11:18 +01002579 // Print the string so far if it's the last character or there is
2580 // a composing character.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002581 if (i + cl >= len || (comping && sep_comp && i > start)
2582 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002583# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002585# ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +01002586 // No fontset: At least draw char after wide char at
2587 // right position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002590 )
2591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 )
2593 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002594 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 thislen = i - start;
2596 else
2597 thislen = i - start + cl;
2598 if (thislen > 0)
2599 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002600 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002601 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2603 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002604 if (prev_wide)
2605 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 start += thislen;
2607 }
2608 scol += cells;
2609 cells = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01002610 // Adjust to not draw a character which width is changed
2611 // against with last one.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002612 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002614 scol -= cn;
2615 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 }
2617
Bram Moolenaar13505972019-01-24 15:04:48 +01002618# if defined(FEAT_GUI_X11)
Bram Moolenaar30613902019-12-01 22:11:18 +01002619 // No fontset: draw a space to fill the gap after a wide char
2620 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002622# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002624# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002625 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2627 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002630 // Draw a composing char on top of the previous char.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002631 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002633# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaar30613902019-12-01 22:11:18 +01002634 // Carbon ATSUI autodraws composing char over previous char
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002635 gui_mch_draw_string(gui.row, scol, s + i, cl,
2636 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002637# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002638 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2639 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 start = i + cl;
2642 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002643 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002645 // The stuff below assumes "len" is the length in screen columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 len = scol - col;
2647 }
2648 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
2650 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (enc_dbcs == DBCS_JPNU)
2652 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002653 // Get the length in display cells, this can be different from the
2654 // number of bytes for "euc-jp".
Bram Moolenaar72597a52010-07-18 15:31:08 +02002655 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002658#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
2660 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2661 gui.col = col + len;
2662
Bram Moolenaar30613902019-12-01 22:11:18 +01002663 // May need to invert it when it's part of the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (flags & GUI_MON_NOCLEAR)
2665 clip_may_redraw_selection(gui.row, col, len);
2666
2667 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2668 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002669 // Invalidate the old physical cursor position if we wrote over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 if (gui.cursor_row == gui.row
2671 && gui.cursor_col >= col
2672 && gui.cursor_col < col + len)
2673 gui.cursor_is_valid = FALSE;
2674 }
2675
2676#ifdef FEAT_SIGN_ICONS
2677 if (draw_sign)
Bram Moolenaar30613902019-12-01 22:11:18 +01002678 // Draw the sign on top of the spaces.
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002679 gui_mch_drawsign(gui.row, signcol, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002680# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002681 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (multi_sign)
2683 netbeans_draw_multisign_indicator(gui.row);
2684# endif
2685#endif
2686
2687 return OK;
2688}
2689
2690/*
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002691 * Undraw the cursor. This actually redraws the character at the cursor
2692 * position, plus some more characters when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 */
2694 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002695gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696{
2697 if (gui.cursor_is_valid)
2698 {
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002699 // Always redraw the character just before if there is one, because
2700 // with some fonts and characters there can be a one pixel overlap.
2701 int startcol = gui.cursor_col > 0 ? gui.cursor_col - 1 : gui.cursor_col;
2702 int endcol = gui.cursor_col;
2703
2704#ifdef FEAT_GUI_GTK
2705 gui_adjust_undraw_cursor_for_ligatures(&startcol, &endcol);
2706#endif
2707 gui_redraw_block(gui.cursor_row, startcol,
2708 gui.cursor_row, endcol, GUI_MON_NOCLEAR);
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002709
Bram Moolenaar54612582019-11-21 17:13:31 +01002710 // Cursor_is_valid is reset when the cursor is undrawn, also reset it
2711 // here in case it wasn't needed to undraw it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 gui.cursor_is_valid = FALSE;
2713 }
2714}
2715
2716 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002717gui_redraw(
2718 int x,
2719 int y,
2720 int w,
2721 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
2723 int row1, col1, row2, col2;
2724
2725 row1 = Y_2_ROW(y);
2726 col1 = X_2_COL(x);
2727 row2 = Y_2_ROW(y + h - 1);
2728 col2 = X_2_COL(x + w - 1);
2729
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002730 gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 /*
2733 * We may need to redraw the cursor, but don't take it upon us to change
2734 * its location after a scroll.
2735 * (maybe be more strict even and test col too?)
2736 * These things may be outside the update/clipping region and reality may
2737 * not reflect Vims internal ideas if these operations are clipped away.
2738 */
2739 if (gui.row == gui.cursor_row)
2740 gui_update_cursor(TRUE, TRUE);
2741}
2742
2743/*
2744 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2745 * from col1 to col2 (inclusive).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 */
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002747 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002748gui_redraw_block(
2749 int row1,
2750 int col1,
2751 int row2,
2752 int col2,
Bram Moolenaar30613902019-12-01 22:11:18 +01002753 int flags) // flags for gui_outstr_nowrap()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754{
2755 int old_row, old_col;
2756 long_u old_hl_mask;
2757 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002758 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 int idx, len;
2760 int back, nback;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
Bram Moolenaar30613902019-12-01 22:11:18 +01002763 // Don't try to update when ScreenLines is not valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (!screen_cleared || ScreenLines == NULL)
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002765 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
Bram Moolenaar30613902019-12-01 22:11:18 +01002767 // Don't try to draw outside the shell!
2768 // Check everything, strange values may be caused by a big border width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 col1 = check_col(col1);
2770 col2 = check_col(col2);
2771 row1 = check_row(row1);
2772 row2 = check_row(row2);
2773
Bram Moolenaar30613902019-12-01 22:11:18 +01002774 // Remember where our cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 old_row = gui.row;
2776 old_col = gui.col;
2777 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 orig_col1 = col1;
2779 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780
2781 for (gui.row = row1; gui.row <= row2; gui.row++)
2782 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002783 // When only half of a double-wide character is in the block, include
2784 // the other half.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 col1 = orig_col1;
2786 col2 = orig_col2;
2787 off = LineOffset[gui.row];
2788 if (enc_dbcs != 0)
2789 {
2790 if (col1 > 0)
2791 col1 -= dbcs_screen_head_off(ScreenLines + off,
2792 ScreenLines + off + col1);
2793 col2 += dbcs_screen_tail_off(ScreenLines + off,
2794 ScreenLines + off + col2);
2795 }
2796 else if (enc_utf8)
2797 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002798 if (ScreenLines[off + col1] == 0)
2799 {
2800 if (col1 > 0)
2801 --col1;
2802 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002803 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002804 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002805 // Make this IEMSGN when it no longer breaks Travis CI.
2806 vim_snprintf((char *)IObuff, IOSIZE,
2807 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2808 (long)gui.row);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002809 msg((char *)IObuff);
Bram Moolenaar9a853462018-12-07 14:10:37 +01002810 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002811 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002812#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2814 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 gui.col = col1;
2818 off = LineOffset[gui.row] + gui.col;
2819 len = col2 - col1 + 1;
2820
Bram Moolenaar30613902019-12-01 22:11:18 +01002821 // Find how many chars back this highlighting starts, or where a space
2822 // is. Needed for when the bold trick is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 for (back = 0; back < col1; ++back)
2824 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2825 || ScreenLines[off - 1 - back] == ' ')
2826 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
Bram Moolenaar30613902019-12-01 22:11:18 +01002828 // Break it up in strings of characters with the same attributes.
2829 // Print UTF-8 characters individually.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 while (len > 0)
2831 {
2832 first_attr = ScreenAttrs[off];
2833 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002834#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 if (enc_utf8 && ScreenLinesUC[off] != 0)
2836 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002837 // output multi-byte character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 nback = gui_screenchar(off, flags,
2839 (guicolor_T)0, (guicolor_T)0, back);
2840 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2841 idx = 2;
2842 else
2843 idx = 1;
2844 }
2845 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2846 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002847 // output double-byte, single-width character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 nback = gui_screenchar(off, flags,
2849 (guicolor_T)0, (guicolor_T)0, back);
2850 idx = 1;
2851 }
2852 else
2853#endif
2854 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002855#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 for (idx = 0; idx < len; ++idx)
2857 {
2858 if (enc_utf8 && ScreenLines[off + idx] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002859 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 if (ScreenAttrs[off + idx] != first_attr)
2861 break;
2862 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002863 // gui_screenstr() takes care of multibyte chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 nback = gui_screenstr(off, idx, flags,
2865 (guicolor_T)0, (guicolor_T)0, back);
2866#else
2867 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2868 idx++)
2869 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002870 // Stop at a multi-byte Unicode character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2872 break;
2873 if (enc_dbcs == DBCS_JPNU)
2874 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002875 // Stop at a double-byte single-width char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (ScreenLines[off + idx] == 0x8e)
2877 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002878 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 + off + idx) == 2)
Bram Moolenaar30613902019-12-01 22:11:18 +01002880 ++idx; // skip second byte of double-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
2883 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2884 (guicolor_T)0, (guicolor_T)0, back);
2885#endif
2886 }
2887 if (nback == FAIL)
2888 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002889 // Must back up to start drawing where a bold or italic word
2890 // starts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 off -= back;
2892 len += back;
2893 gui.col -= back;
2894 }
2895 else
2896 {
2897 off += idx;
2898 len -= idx;
2899 }
2900 back = 0;
2901 }
2902 }
2903
Bram Moolenaar30613902019-12-01 22:11:18 +01002904 // Put the cursor back where it was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 gui.row = old_row;
2906 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002907 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908}
2909
2910 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002911gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912{
2913 if (count <= 0)
2914 return;
2915
2916 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002917 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 gui_clear_block(row, gui.scroll_region_left,
2919 gui.scroll_region_bot, gui.scroll_region_right);
2920 else
2921 {
2922 gui_mch_delete_lines(row, count);
2923
Bram Moolenaar30613902019-12-01 22:11:18 +01002924 // If the cursor was in the deleted lines it's now gone. If the
2925 // cursor was in the scrolled lines adjust its position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 if (gui.cursor_row >= row
2927 && gui.cursor_col >= gui.scroll_region_left
2928 && gui.cursor_col <= gui.scroll_region_right)
2929 {
2930 if (gui.cursor_row < row + count)
2931 gui.cursor_is_valid = FALSE;
2932 else if (gui.cursor_row <= gui.scroll_region_bot)
2933 gui.cursor_row -= count;
2934 }
2935 }
2936}
2937
2938 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002939gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
2941 if (count <= 0)
2942 return;
2943
2944 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002945 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 gui_clear_block(row, gui.scroll_region_left,
2947 gui.scroll_region_bot, gui.scroll_region_right);
2948 else
2949 {
2950 gui_mch_insert_lines(row, count);
2951
2952 if (gui.cursor_row >= gui.row
2953 && gui.cursor_col >= gui.scroll_region_left
2954 && gui.cursor_col <= gui.scroll_region_right)
2955 {
2956 if (gui.cursor_row <= gui.scroll_region_bot - count)
2957 gui.cursor_row += count;
2958 else if (gui.cursor_row <= gui.scroll_region_bot)
2959 gui.cursor_is_valid = FALSE;
2960 }
2961 }
2962}
2963
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002964#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002965/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002966 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2967 */
2968 static int
2969gui_wait_for_chars_3(
2970 long wtime,
2971 int *interrupted UNUSED,
2972 int ignore_input UNUSED)
2973{
2974 return gui_mch_wait_for_chars(wtime);
2975}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002976#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002977
2978/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002979 * Returns OK if a character was found to be available within the given time,
2980 * or FAIL otherwise.
2981 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002982 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002983gui_wait_for_chars_or_timer(
2984 long wtime,
2985 int *interrupted UNUSED,
2986 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002987{
2988#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002989 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2990 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002991#else
2992 return gui_mch_wait_for_chars(wtime);
2993#endif
2994}
2995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996/*
2997 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002998 * "wtime" == -1 Wait forever.
2999 * "wtime" == 0 Don't wait.
3000 * "wtime" > 0 Wait wtime milliseconds for a character.
3001 *
3002 * Returns the number of characters read or zero when timed out or interrupted.
3003 * "buf" may be NULL, in which case a non-zero number is returned if characters
3004 * are available.
3005 */
3006 static int
3007gui_wait_for_chars_buf(
3008 char_u *buf,
3009 int maxlen,
3010 long wtime, // don't use "time", MIPS cannot handle it
3011 int tb_change_cnt)
3012{
3013 int retval;
3014
3015#ifdef FEAT_MENU
3016 // If we're going to wait a bit, update the menus and mouse shape for the
3017 // current State.
3018 if (wtime != 0)
3019 gui_update_menus(0);
3020#endif
3021
3022 gui_mch_update();
3023 if (input_available()) // Got char, return immediately
3024 {
3025 if (buf != NULL && !typebuf_changed(tb_change_cnt))
3026 return read_from_input_buf(buf, (long)maxlen);
3027 return 0;
3028 }
3029 if (wtime == 0) // Don't wait for char
3030 return FAIL;
3031
3032 // Before waiting, flush any output to the screen.
3033 gui_mch_flush();
3034
3035 // Blink while waiting for a character.
3036 gui_mch_start_blink();
3037
3038 // Common function to loop until "wtime" is met, while handling timers and
3039 // other callbacks.
3040 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
3041 gui_wait_for_chars_or_timer, NULL);
3042
3043 gui_mch_stop_blink(TRUE);
3044
3045 return retval;
3046}
3047
3048/*
3049 * Wait for a character from the keyboard without actually reading it.
3050 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 * wtime == -1 Wait forever.
3052 * wtime == 0 Don't wait.
3053 * wtime > 0 Wait wtime milliseconds for a character.
3054 * Returns OK if a character was found to be available within the given time,
3055 * or FAIL otherwise.
3056 */
3057 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003058gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003060 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061}
3062
3063/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003064 * Equivalent of mch_inchar() for the GUI.
3065 */
3066 int
3067gui_inchar(
3068 char_u *buf,
3069 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003070 long wtime, // milliseconds
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003071 int tb_change_cnt)
3072{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003073 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003074}
3075
3076/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003077 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 */
3079 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003080fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 p[0] = (char_u)(col / 128 + ' ' + 1);
3083 p[1] = (char_u)(col % 128 + ' ' + 1);
3084 p[2] = (char_u)(row / 128 + ' ' + 1);
3085 p[3] = (char_u)(row % 128 + ' ' + 1);
3086}
3087
3088/*
3089 * Generic mouse support function. Add a mouse event to the input buffer with
3090 * the given properties.
3091 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3092 * MOUSE_X1, MOUSE_X2
3093 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003094 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3095 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 * x, y --- Coordinates of mouse in pixels.
3097 * repeated_click --- TRUE if this click comes only a short time after a
3098 * previous click.
3099 * modifiers --- Bit field which may be any of the following modifiers
3100 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3101 * This function will ignore drag events where the mouse has not moved to a new
3102 * character.
3103 */
3104 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003105gui_send_mouse_event(
3106 int button,
3107 int x,
3108 int y,
3109 int repeated_click,
3110 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 static int prev_row = 0, prev_col = 0;
3113 static int prev_button = -1;
3114 static int num_clicks = 1;
3115 char_u string[10];
3116 enum key_extra button_char;
3117 int row, col;
3118#ifdef FEAT_CLIPBOARD
3119 int checkfor;
3120 int did_clip = FALSE;
3121#endif
3122
3123 /*
3124 * Scrolling may happen at any time, also while a selection is present.
3125 */
3126 switch (button)
3127 {
Bram Moolenaar445f11d2021-06-08 20:13:31 +02003128 case MOUSE_MOVE:
3129 button_char = KE_MOUSEMOVE_XY;
3130 goto button_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 case MOUSE_X1:
3132 button_char = KE_X1MOUSE;
3133 goto button_set;
3134 case MOUSE_X2:
3135 button_char = KE_X2MOUSE;
3136 goto button_set;
3137 case MOUSE_4:
3138 button_char = KE_MOUSEDOWN;
3139 goto button_set;
3140 case MOUSE_5:
3141 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003142 goto button_set;
3143 case MOUSE_6:
3144 button_char = KE_MOUSELEFT;
3145 goto button_set;
3146 case MOUSE_7:
3147 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148button_set:
3149 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003150 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 if (hold_gui_events)
3152 return;
3153
3154 string[3] = CSI;
3155 string[4] = KS_EXTRA;
3156 string[5] = (int)button_char;
3157
Bram Moolenaar30613902019-12-01 22:11:18 +01003158 // Pass the pointer coordinates of the scroll event so that we
3159 // know which window to scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 row = gui_xy2colrow(x, y, &col);
3161 string[6] = (char_u)(col / 128 + ' ' + 1);
3162 string[7] = (char_u)(col % 128 + ' ' + 1);
3163 string[8] = (char_u)(row / 128 + ' ' + 1);
3164 string[9] = (char_u)(row % 128 + ' ' + 1);
3165
3166 if (modifiers == 0)
3167 add_to_input_buf(string + 3, 7);
3168 else
3169 {
3170 string[0] = CSI;
3171 string[1] = KS_MODIFIER;
3172 string[2] = 0;
3173 if (modifiers & MOUSE_SHIFT)
3174 string[2] |= MOD_MASK_SHIFT;
3175 if (modifiers & MOUSE_CTRL)
3176 string[2] |= MOD_MASK_CTRL;
3177 if (modifiers & MOUSE_ALT)
3178 string[2] |= MOD_MASK_ALT;
3179 add_to_input_buf(string, 10);
3180 }
3181 return;
3182 }
3183 }
3184
3185#ifdef FEAT_CLIPBOARD
Bram Moolenaar30613902019-12-01 22:11:18 +01003186 // If a clipboard selection is in progress, handle it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 if (clip_star.state == SELECT_IN_PROGRESS)
3188 {
3189 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
Bram Moolenaar0ce37332019-12-18 21:33:22 +01003190
3191 // A release event may still need to be sent if the position is equal.
3192 row = gui_xy2colrow(x, y, &col);
3193 if (button != MOUSE_RELEASE || row != prev_row || col != prev_col)
3194 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196
Bram Moolenaar30613902019-12-01 22:11:18 +01003197 // Determine which mouse settings to look for based on the current mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 switch (get_real_state())
3199 {
3200 case NORMAL_BUSY:
3201 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003202# ifdef FEAT_TERMINAL
3203 case TERMINAL:
3204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 case NORMAL: checkfor = MOUSE_NORMAL; break;
3206 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003207 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 case REPLACE:
3209 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 case VREPLACE:
3211 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 case INSERT:
3213 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3214 case ASKMORE:
Bram Moolenaar30613902019-12-01 22:11:18 +01003215 case HITRETURN: // At the more- and hit-enter prompt pass the
3216 // mouse event for a click on or below the
3217 // message line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 if (Y_2_ROW(y) >= msg_row)
3219 checkfor = MOUSE_NORMAL;
3220 else
3221 checkfor = MOUSE_RETURN;
3222 break;
3223
3224 /*
3225 * On the command line, use the clipboard selection on all lines
3226 * but the command line. But not when pasting.
3227 */
3228 case CMDLINE:
3229 case CMDLINE+LANGMAP:
3230 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3231 checkfor = MOUSE_NONE;
3232 else
3233 checkfor = MOUSE_COMMAND;
3234 break;
3235
3236 default:
3237 checkfor = MOUSE_NONE;
3238 break;
3239 };
3240
3241 /*
3242 * Allow clipboard selection of text on the command line in "normal"
3243 * modes. Don't do this when dragging the status line, or extending a
3244 * Visual selection.
3245 */
3246 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003247 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 && button != MOUSE_DRAG
3249# ifdef FEAT_MOUSESHAPE
3250 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252# endif
3253 )
3254 checkfor = MOUSE_NONE;
3255
3256 /*
3257 * Use modeless selection when holding CTRL and SHIFT pressed.
3258 */
3259 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3260 checkfor = MOUSE_NONEF;
3261
3262 /*
3263 * In Ex mode, always use modeless selection.
3264 */
3265 if (exmode_active)
3266 checkfor = MOUSE_NONE;
3267
3268 /*
3269 * If the mouse settings say to not use the mouse, use the modeless
3270 * selection. But if Visual is active, assume that only the Visual area
3271 * will be selected.
3272 * Exception: On the command line, both the selection is used and a mouse
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003273 * key is sent.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 */
3275 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3276 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003277 // Don't do modeless selection in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3279 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
3281 /*
3282 * When 'mousemodel' is "popup", shift-left is translated to right.
3283 * But not when also using Ctrl.
3284 */
3285 if (mouse_model_popup() && button == MOUSE_LEFT
3286 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3287 {
3288 button = MOUSE_RIGHT;
3289 modifiers &= ~ MOUSE_SHIFT;
3290 }
3291
Bram Moolenaar30613902019-12-01 22:11:18 +01003292 // If the selection is done, allow the right button to extend it.
3293 // If the selection is cleared, allow the right button to start it
3294 // from the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (button == MOUSE_RIGHT)
3296 {
3297 if (clip_star.state == SELECT_CLEARED)
3298 {
3299 if (State & CMDLINE)
3300 {
3301 col = msg_col;
3302 row = msg_row;
3303 }
3304 else
3305 {
3306 col = curwin->w_wcol;
3307 row = curwin->w_wrow + W_WINROW(curwin);
3308 }
3309 clip_start_selection(col, row, FALSE);
3310 }
3311 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3312 repeated_click);
3313 did_clip = TRUE;
3314 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003315 // Allow the left button to start the selection
Bram Moolenaare60acc12011-05-10 16:41:25 +02003316 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 {
3318 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3319 did_clip = TRUE;
3320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
Bram Moolenaar30613902019-12-01 22:11:18 +01003322 // Always allow pasting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (button != MOUSE_MIDDLE)
3324 {
3325 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3326 return;
3327 if (checkfor != MOUSE_COMMAND)
3328 button = MOUSE_LEFT;
3329 }
3330 repeated_click = FALSE;
3331 }
3332
3333 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003334 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335#endif
3336
Bram Moolenaar30613902019-12-01 22:11:18 +01003337 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (hold_gui_events)
3339 return;
3340
3341 row = gui_xy2colrow(x, y, &col);
3342
3343 /*
3344 * If we are dragging and the mouse hasn't moved far enough to be on a
3345 * different character, then don't send an event to vim.
3346 */
3347 if (button == MOUSE_DRAG)
3348 {
3349 if (row == prev_row && col == prev_col)
3350 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01003351 // Dragging above the window, set "row" to -1 to cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 if (y < 0)
3353 row = -1;
3354 }
3355
3356 /*
3357 * If topline has changed (window scrolled) since the last click, reset
3358 * repeated_click, because we don't want starting Visual mode when
3359 * clicking on a different character in the text.
3360 */
3361 if (curwin->w_topline != gui_prev_topline
3362#ifdef FEAT_DIFF
3363 || curwin->w_topfill != gui_prev_topfill
3364#endif
3365 )
3366 repeated_click = FALSE;
3367
Bram Moolenaar30613902019-12-01 22:11:18 +01003368 string[0] = CSI; // this sequence is recognized by check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 string[1] = KS_MOUSE;
3370 string[2] = KE_FILLER;
3371 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3372 {
3373 if (repeated_click)
3374 {
3375 /*
3376 * Handle multiple clicks. They only count if the mouse is still
3377 * pointing at the same character.
3378 */
3379 if (button != prev_button || row != prev_row || col != prev_col)
3380 num_clicks = 1;
3381 else if (++num_clicks > 4)
3382 num_clicks = 1;
3383 }
3384 else
3385 num_clicks = 1;
3386 prev_button = button;
3387 gui_prev_topline = curwin->w_topline;
3388#ifdef FEAT_DIFF
3389 gui_prev_topfill = curwin->w_topfill;
3390#endif
3391
3392 string[3] = (char_u)(button | 0x20);
3393 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3394 }
3395 else
3396 string[3] = (char_u)button;
3397
3398 string[3] |= modifiers;
3399 fill_mouse_coord(string + 4, col, row);
3400 add_to_input_buf(string, 8);
3401
3402 if (row < 0)
3403 prev_row = 0;
3404 else
3405 prev_row = row;
3406 prev_col = col;
3407
3408 /*
3409 * We need to make sure this is cleared since Athena doesn't tell us when
3410 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3411 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003412#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 gui.dragged_sb = SBAR_NONE;
3414#endif
3415}
3416
3417/*
3418 * Convert x and y coordinate to column and row in text window.
3419 * Corrects for multi-byte character.
3420 * returns column in "*colp" and row as return value;
3421 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003422 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003423gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
3425 int col = check_col(X_2_COL(x));
3426 int row = check_row(Y_2_ROW(y));
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 return row;
3430}
3431
3432#if defined(FEAT_MENU) || defined(PROTO)
3433/*
3434 * Callback function for when a menu entry has been selected.
3435 */
3436 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003437gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003439 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
Bram Moolenaar30613902019-12-01 22:11:18 +01003441 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (hold_gui_events)
3443 return;
3444
3445 bytes[0] = CSI;
3446 bytes[1] = KS_MENU;
3447 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003448 add_to_input_buf(bytes, 3);
3449 add_long_to_buf((long_u)menu, bytes);
3450 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451}
3452#endif
3453
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003454static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003455
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456/*
3457 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003458 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 * in p_go.
3460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003462gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003464#ifdef FEAT_GUI_DARKTHEME
3465 static int prev_dark_theme = -1;
3466 int using_dark_theme = FALSE;
3467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468#ifdef FEAT_MENU
3469 static int prev_menu_is_active = -1;
3470#endif
3471#ifdef FEAT_TOOLBAR
3472 static int prev_toolbar = -1;
3473 int using_toolbar = FALSE;
3474#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003475#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003476 int using_tabline;
3477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#ifdef FEAT_FOOTER
3479 static int prev_footer = -1;
3480 int using_footer = FALSE;
3481#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 static int prev_tearoff = -1;
3484 int using_tearoff = FALSE;
3485#endif
3486
3487 char_u *p;
3488 int i;
3489#ifdef FEAT_MENU
3490 int grey_old, grey_new;
3491 char_u *temp;
3492#endif
3493 win_T *wp;
3494 int need_set_size;
3495 int fix_size;
3496
3497#ifdef FEAT_MENU
3498 if (oldval != NULL && gui.in_use)
3499 {
3500 /*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003501 * Check if the menus go from grey to non-grey or vice versa.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 */
3503 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3504 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3505 if (grey_old != grey_new)
3506 {
3507 temp = p_go;
3508 p_go = oldval;
3509 gui_update_menus(MENU_ALL_MODES);
3510 p_go = temp;
3511 }
3512 }
3513 gui.menu_is_active = FALSE;
3514#endif
3515
3516 for (i = 0; i < 3; i++)
3517 gui.which_scrollbars[i] = FALSE;
3518 for (p = p_go; *p; p++)
3519 switch (*p)
3520 {
3521 case GO_LEFT:
3522 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3523 break;
3524 case GO_RIGHT:
3525 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3526 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 case GO_VLEFT:
3528 if (win_hasvertsplit())
3529 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3530 break;
3531 case GO_VRIGHT:
3532 if (win_hasvertsplit())
3533 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3534 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 case GO_BOT:
3536 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3537 break;
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003538#ifdef FEAT_GUI_DARKTHEME
3539 case GO_DARKTHEME:
3540 using_dark_theme = TRUE;
3541 break;
3542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543#ifdef FEAT_MENU
3544 case GO_MENUS:
3545 gui.menu_is_active = TRUE;
3546 break;
3547#endif
3548 case GO_GREY:
Bram Moolenaar30613902019-12-01 22:11:18 +01003549 // make menu's have grey items, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 break;
3551#ifdef FEAT_TOOLBAR
3552 case GO_TOOLBAR:
3553 using_toolbar = TRUE;
3554 break;
3555#endif
3556#ifdef FEAT_FOOTER
3557 case GO_FOOTER:
3558 using_footer = TRUE;
3559 break;
3560#endif
3561 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003562#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 using_tearoff = TRUE;
3564#endif
3565 break;
3566 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01003567 // Ignore options that are not supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 break;
3569 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 if (gui.in_use)
3572 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003573 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003575
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003576#ifdef FEAT_GUI_DARKTHEME
3577 if (using_dark_theme != prev_dark_theme)
3578 {
3579 gui_mch_set_dark_theme(using_dark_theme);
3580 prev_dark_theme = using_dark_theme;
3581 }
3582#endif
3583
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003584#ifdef FEAT_GUI_TABLINE
Bram Moolenaar30613902019-12-01 22:11:18 +01003585 // Update the GUI tab line, it may appear or disappear. This may
3586 // cause the non-GUI tab line to disappear or appear.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003587 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003588 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003589 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003590 // We don't want a resize event change "Rows" here, save and
3591 // restore it. Resizing is handled below.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003592 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003593 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003594 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003595 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003596 if (using_tabline)
3597 fix_size = TRUE;
3598 if (!gui_use_tabline())
Bram Moolenaar30613902019-12-01 22:11:18 +01003599 redraw_tabline = TRUE; // may draw non-GUI tab line
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003600 }
3601#endif
3602
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 for (i = 0; i < 3; i++)
3604 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003605 // The scrollbar needs to be updated when it is shown/unshown and
3606 // when switching tab pages. But the size only changes when it's
3607 // shown/unshown. Thus we need two places to remember whether a
3608 // scrollbar is there or not.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003609 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003610 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003611 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
3613 if (i == SBAR_BOTTOM)
3614 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3615 gui.which_scrollbars[i]);
3616 else
3617 {
3618 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003621 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3622 {
3623 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003624 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003625 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003626 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003627 if (gui.which_scrollbars[i])
3628 fix_size = TRUE;
3629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003631 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003632 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
3634
3635#ifdef FEAT_MENU
3636 if (gui.menu_is_active != prev_menu_is_active)
3637 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003638 // We don't want a resize event change "Rows" here, save and
3639 // restore it. Resizing is handled below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 i = Rows;
3641 gui_mch_enable_menu(gui.menu_is_active);
3642 Rows = i;
3643 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003644 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 if (gui.menu_is_active)
3646 fix_size = TRUE;
3647 }
3648#endif
3649
3650#ifdef FEAT_TOOLBAR
3651 if (using_toolbar != prev_toolbar)
3652 {
3653 gui_mch_show_toolbar(using_toolbar);
3654 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003655 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 if (using_toolbar)
3657 fix_size = TRUE;
3658 }
3659#endif
3660#ifdef FEAT_FOOTER
3661 if (using_footer != prev_footer)
3662 {
3663 gui_mch_enable_footer(using_footer);
3664 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003665 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (using_footer)
3667 fix_size = TRUE;
3668 }
3669#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003670#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (using_tearoff != prev_tearoff)
3672 {
3673 gui_mch_toggle_tearoffs(using_tearoff);
3674 prev_tearoff = using_tearoff;
3675 }
3676#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003677 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003678 {
3679#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003680 long prev_Columns = Columns;
3681 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003682#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003683 // Adjust the size of the window to make the text area keep the
3684 // same size and to avoid that part of our window is off-screen
3685 // and a scrollbar can't be used, for example.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003686 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003687
3688#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01003689 // GTK has the annoying habit of sending us resize events when
3690 // changing the window size ourselves. This mostly happens when
3691 // waiting for a character to arrive, quite unpredictably, and may
3692 // change Columns and Rows when we don't want it. Wait for a
3693 // character here to avoid this effect.
3694 // If you remove this, please test this command for resizing
3695 // effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3696 // Don't do this while starting up though.
3697 // Don't change Rows when adding menu/toolbar/tabline.
3698 // Don't change Columns when adding vertical toolbar.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003699 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003700 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003701 if ((need_set_size & RESIZE_VERT) == 0)
3702 Rows = prev_Rows;
3703 if ((need_set_size & RESIZE_HOR) == 0)
3704 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003705#endif
3706 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003707 // When the console tabline appears or disappears the window positions
3708 // change.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003709 if (firstwin->w_winrow != tabline_height())
Bram Moolenaar30613902019-12-01 22:11:18 +01003710 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712}
3713
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003714#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3715/*
3716 * Return TRUE if the GUI is taking care of the tabline.
3717 * It may still be hidden if 'showtabline' is zero.
3718 */
3719 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003720gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003721{
3722 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3723}
3724
3725/*
3726 * Return TRUE if the GUI is showing the tabline.
3727 * This uses 'showtabline'.
3728 */
3729 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003730gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003731{
3732 if (!gui_use_tabline()
3733 || p_stal == 0
3734 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3735 return FALSE;
3736 return TRUE;
3737}
3738
3739/*
3740 * Update the tabline.
3741 * This may display/undisplay the tabline and update the labels.
3742 */
3743 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003744gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003745{
3746 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003747 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003748
3749 if (!gui.starting && starting == 0)
3750 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003751 // Updating the tabline uses direct GUI commands, flush
3752 // outstanding instructions first. (esp. clear screen)
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003753 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003754
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003755 if (!showit != !shown)
3756 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003757 if (showit != 0)
3758 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003759
Bram Moolenaar30613902019-12-01 22:11:18 +01003760 // When the tabs change from hidden to shown or from shown to
3761 // hidden the size of the text area should remain the same.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003762 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003763 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003764 }
3765}
3766
3767/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003768 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003769 */
3770 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003771get_tabline_label(
3772 tabpage_T *tp,
Bram Moolenaar30613902019-12-01 22:11:18 +01003773 int tooltip) // TRUE: get tooltip
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003774{
3775 int modified = FALSE;
3776 char_u buf[40];
3777 int wincount;
3778 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003779 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003780
Bram Moolenaar30613902019-12-01 22:11:18 +01003781 // Use 'guitablabel' or 'guitabtooltip' if it's set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003782 opt = (tooltip ? &p_gtt : &p_gtl);
3783 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003784 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003785 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003786 int called_emsg_before = called_emsg;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003787 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003788 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003789 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3790 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003791
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003792 printer_page_num = tabpage_index(tp);
3793# ifdef FEAT_EVAL
3794 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003795 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003796# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003797 // It's almost as going to the tabpage, but without autocommands.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003798 curtab->tp_firstwin = firstwin;
3799 curtab->tp_lastwin = lastwin;
3800 curtab->tp_curwin = curwin;
3801 save_curtab = curtab;
3802 curtab = tp;
3803 topframe = curtab->tp_topframe;
3804 firstwin = curtab->tp_firstwin;
3805 lastwin = curtab->tp_lastwin;
3806 curwin = curtab->tp_curwin;
3807 curbuf = curwin->w_buffer;
3808
Bram Moolenaar30613902019-12-01 22:11:18 +01003809 // Can't use NameBuff directly, build_stl_str_hl() uses it.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003810 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003811 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003812 STRCPY(NameBuff, res);
3813
Bram Moolenaar30613902019-12-01 22:11:18 +01003814 // Back to the original curtab.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003815 curtab = save_curtab;
3816 topframe = curtab->tp_topframe;
3817 firstwin = curtab->tp_firstwin;
3818 lastwin = curtab->tp_lastwin;
3819 curwin = curtab->tp_curwin;
3820 curbuf = curwin->w_buffer;
3821
Bram Moolenaar53989552019-12-23 22:59:18 +01003822 if (called_emsg > called_emsg_before)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003823 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003824 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003825 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003826
Bram Moolenaar30613902019-12-01 22:11:18 +01003827 // If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3828 // use a default label.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003829 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003830 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003831 // Get the buffer name into NameBuff[] and shorten it.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003832 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003833 if (!tooltip)
3834 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003835
3836 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3837 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3838 if (bufIsChanged(wp->w_buffer))
3839 modified = TRUE;
3840 if (modified || wincount > 1)
3841 {
3842 if (wincount > 1)
3843 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3844 else
3845 buf[0] = NUL;
3846 if (modified)
3847 STRCAT(buf, "+");
3848 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003849 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003850 mch_memmove(NameBuff, buf, STRLEN(buf));
3851 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003852 }
3853}
3854
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003855/*
3856 * Send the event for clicking to select tab page "nr".
3857 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003858 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003859 */
3860 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003861send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003862{
3863 char_u string[3];
3864
3865 if (nr == tabpage_index(curtab))
3866 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003867
Bram Moolenaar30613902019-12-01 22:11:18 +01003868 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003869 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003870# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003871 || cmdwin_type != 0
3872# endif
3873 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003874 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003875 // Set it back to the current tab page.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003876 gui_mch_set_curtab(tabpage_index(curtab));
3877 return FALSE;
3878 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003879
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003880 string[0] = CSI;
3881 string[1] = KS_TABLINE;
3882 string[2] = KE_FILLER;
3883 add_to_input_buf(string, 3);
3884 string[0] = nr;
3885 add_to_input_buf_csi(string, 1);
3886 return TRUE;
3887}
3888
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003889/*
3890 * Send a tabline menu event
3891 */
3892 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003893send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003894{
3895 char_u string[3];
3896
Bram Moolenaar29547192018-12-11 20:39:19 +01003897 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003898 if (hold_gui_events)
3899 return;
3900
Bram Moolenaar29547192018-12-11 20:39:19 +01003901 // Cannot close the last tabpage.
3902 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3903 return;
3904
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003905 string[0] = CSI;
3906 string[1] = KS_TABMENU;
3907 string[2] = KE_FILLER;
3908 add_to_input_buf(string, 3);
3909 string[0] = tabidx;
3910 string[1] = (char_u)(long)event;
3911 add_to_input_buf_csi(string, 2);
3912}
3913
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915
3916/*
3917 * Scrollbar stuff:
3918 */
3919
Bram Moolenaare45828b2006-02-15 22:12:56 +00003920/*
3921 * Remove all scrollbars. Used before switching to another tab page.
3922 */
3923 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003924gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003925{
3926 int i;
3927 win_T *wp;
3928
3929 for (i = 0; i < 3; i++)
3930 {
3931 if (i == SBAR_BOTTOM)
3932 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3933 else
3934 {
3935 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003936 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003937 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003938 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003939 }
3940}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003943gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944{
3945 static int sbar_ident = 0;
3946
Bram Moolenaar30613902019-12-01 22:11:18 +01003947 sb->ident = sbar_ident++; // No check for too big, but would it happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 sb->wp = wp;
3949 sb->type = type;
3950 sb->value = 0;
3951#ifdef FEAT_GUI_ATHENA
3952 sb->pixval = 0;
3953#endif
3954 sb->size = 1;
3955 sb->max = 1;
3956 sb->top = 0;
3957 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 sb->status_height = 0;
3960 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3961}
3962
3963/*
3964 * Find the scrollbar with the given index.
3965 */
3966 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003967gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968{
3969 win_T *wp;
3970
3971 if (gui.bottom_sbar.ident == ident)
3972 return &gui.bottom_sbar;
3973 FOR_ALL_WINDOWS(wp)
3974 {
3975 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3976 return &wp->w_scrollbars[SBAR_LEFT];
3977 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3978 return &wp->w_scrollbars[SBAR_RIGHT];
3979 }
3980 return NULL;
3981}
3982
3983/*
3984 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3985 *
3986 * For Win32, Macintosh and GTK+ 2:
3987 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3988 * you stop dragging the scrollbar. We get here each time the scrollbar is
3989 * dragged another pixel, but as far as the rest of vim goes, it thinks
3990 * we're just hanging in the call to DispatchMessage() in
3991 * process_message(). The DispatchMessage() call that hangs was passed a
3992 * mouse button click event in the scrollbar window. -- webb.
3993 *
3994 * Solution: Do the scrolling right here. But only when allowed.
3995 * Ignore the scrollbars while executing an external command or when there
3996 * are still characters to be processed.
3997 */
3998 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003999gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 int sb_num;
4003#ifdef USE_ON_FLY_SCROLL
4004 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006# ifdef FEAT_DIFF
4007 int old_topfill = curwin->w_topfill;
4008# endif
4009#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004010 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 int byte_count;
4012#endif
4013
4014 if (sb == NULL)
4015 return;
4016
Bram Moolenaar30613902019-12-01 22:11:18 +01004017 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (hold_gui_events)
4019 return;
4020
4021#ifdef FEAT_CMDWIN
4022 if (cmdwin_type != 0 && sb->wp != curwin)
4023 return;
4024#endif
4025
4026 if (still_dragging)
4027 {
4028 if (sb->wp == NULL)
4029 gui.dragged_sb = SBAR_BOTTOM;
4030 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
4031 gui.dragged_sb = SBAR_LEFT;
4032 else
4033 gui.dragged_sb = SBAR_RIGHT;
4034 gui.dragged_wp = sb->wp;
4035 }
4036 else
4037 {
4038 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004039#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004040 // Keep the "dragged_wp" value until after the scrolling, for when the
4041 // mouse button is released. GTK2 doesn't send the button-up event.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 gui.dragged_wp = NULL;
4043#endif
4044 }
4045
Bram Moolenaar30613902019-12-01 22:11:18 +01004046 // Vertical sbar info is kept in the first sbar (the left one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 if (sb->wp != NULL)
4048 sb = &sb->wp->w_scrollbars[0];
4049
4050 /*
4051 * Check validity of value
4052 */
4053 if (value < 0)
4054 value = 0;
4055#ifdef SCROLL_PAST_END
4056 else if (value > sb->max)
4057 value = sb->max;
4058#else
4059 if (value > sb->max - sb->size + 1)
4060 value = sb->max - sb->size + 1;
4061#endif
4062
4063 sb->value = value;
4064
4065#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar30613902019-12-01 22:11:18 +01004066 // When not allowed to do the scrolling right now, return.
4067 // This also checked input_available(), but that causes the first click in
4068 // a scrollbar to be ignored when Vim doesn't have focus.
Bram Moolenaar67840782008-01-03 15:15:07 +00004069 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 return;
4071#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004072 // Disallow scrolling the current window when the completion popup menu is
4073 // visible.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004074 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4075 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077#ifdef FEAT_RIGHTLEFT
4078 if (sb->wp == NULL && curwin->w_p_rl)
4079 {
4080 value = sb->max + 1 - sb->size - value;
4081 if (value < 0)
4082 value = 0;
4083 }
4084#endif
4085
Bram Moolenaar30613902019-12-01 22:11:18 +01004086 if (sb->wp != NULL) // vertical scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 {
4088 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4090 sb_num++;
4091 if (wp == NULL)
4092 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
4094#ifdef USE_ON_FLY_SCROLL
4095 current_scrollbar = sb_num;
4096 scrollbar_value = value;
4097 if (State & NORMAL)
4098 {
4099 gui_do_scroll();
4100 setcursor();
4101 }
4102 else if (State & INSERT)
4103 {
4104 ins_scroll();
4105 setcursor();
4106 }
4107 else if (State & CMDLINE)
4108 {
4109 if (msg_scrolled == 0)
4110 {
4111 gui_do_scroll();
4112 redrawcmdline();
4113 }
4114 }
4115# ifdef FEAT_FOLDING
Bram Moolenaar30613902019-12-01 22:11:18 +01004116 // Value may have been changed for closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 sb->value = sb->wp->w_topline - 1;
4118# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004119
Bram Moolenaar30613902019-12-01 22:11:18 +01004120 // When dragging one scrollbar and there is another one at the other
4121 // side move the thumb of that one too.
Bram Moolenaar371d5402006-03-20 21:47:49 +00004122 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4123 gui_mch_set_scrollbar_thumb(
4124 &sb->wp->w_scrollbars[
4125 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4126 ? SBAR_LEFT : SBAR_RIGHT],
4127 sb->value, sb->size, sb->max);
4128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129#else
4130 bytes[0] = CSI;
4131 bytes[1] = KS_VER_SCROLLBAR;
4132 bytes[2] = KE_FILLER;
4133 bytes[3] = (char_u)sb_num;
4134 byte_count = 4;
4135#endif
4136 }
4137 else
4138 {
4139#ifdef USE_ON_FLY_SCROLL
4140 scrollbar_value = value;
4141
4142 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004143 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 else if (State & INSERT)
4145 ins_horscroll();
4146 else if (State & CMDLINE)
4147 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004148 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004150 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 redrawcmdline();
4152 }
4153 }
4154 if (old_leftcol != curwin->w_leftcol)
4155 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004156 updateWindow(curwin); // update window, status and cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 setcursor();
4158 }
4159#else
4160 bytes[0] = CSI;
4161 bytes[1] = KS_HOR_SCROLLBAR;
4162 bytes[2] = KE_FILLER;
4163 byte_count = 3;
4164#endif
4165 }
4166
4167#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 /*
4169 * synchronize other windows, as necessary according to 'scrollbind'
4170 */
4171 if (curwin->w_p_scb
4172 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4173 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004174# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 ))))
4178 {
4179 do_check_scrollbind(TRUE);
Bram Moolenaar30613902019-12-01 22:11:18 +01004180 // need to update the window right here
Bram Moolenaar29323592016-07-24 22:04:11 +02004181 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 if (wp->w_redr_type > 0)
4183 updateWindow(wp);
4184 setcursor();
4185 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004186 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004188 add_to_input_buf(bytes, byte_count);
4189 add_long_to_buf((long_u)value, bytes);
4190 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191#endif
4192}
4193
4194/*
4195 * Scrollbar stuff:
4196 */
4197
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004198/*
4199 * Called when something in the window layout has changed.
4200 */
4201 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004202gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004203{
4204 if (gui.in_use && starting == 0)
4205 {
4206 out_flush();
4207 gui_init_which_components(NULL);
4208 gui_update_scrollbars(TRUE);
4209 }
4210 need_mouse_correct = TRUE;
4211}
4212
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004214gui_update_scrollbars(
Bram Moolenaar30613902019-12-01 22:11:18 +01004215 int force) // Force all scrollbars to get updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
4217 win_T *wp;
4218 scrollbar_T *sb;
Bram Moolenaar30613902019-12-01 22:11:18 +01004219 long val, size, max; // need 32 bits here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 int which_sb;
4221 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
Bram Moolenaar30613902019-12-01 22:11:18 +01004224 // Update the horizontal scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 gui_update_horiz_scrollbar(force);
4226
Bram Moolenaar4f974752019-02-17 17:44:42 +01004227#ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01004228 // Return straight away if there is neither a left nor right scrollbar.
4229 // On MS-Windows this is required anyway for scrollwheel messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4231 return;
4232#endif
4233
4234 /*
4235 * Don't want to update a scrollbar while we're dragging it. But if we
4236 * have both a left and right scrollbar, and we drag one of them, we still
4237 * need to update the other one.
4238 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004239 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4240 && gui.which_scrollbars[SBAR_LEFT]
4241 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
4243 /*
4244 * If we have two scrollbars and one of them is being dragged, just
4245 * copy the scrollbar position from the dragged one to the other one.
4246 */
4247 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4248 if (gui.dragged_wp != NULL)
4249 gui_mch_set_scrollbar_thumb(
4250 &gui.dragged_wp->w_scrollbars[which_sb],
4251 gui.dragged_wp->w_scrollbars[0].value,
4252 gui.dragged_wp->w_scrollbars[0].size,
4253 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
4255
Bram Moolenaar30613902019-12-01 22:11:18 +01004256 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 ++hold_gui_events;
4258
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004259 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004261 if (wp->w_buffer == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 continue;
Bram Moolenaar30613902019-12-01 22:11:18 +01004263 // Skip a scrollbar that is being dragged.
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004264 if (!force && (gui.dragged_sb == SBAR_LEFT
4265 || gui.dragged_sb == SBAR_RIGHT)
4266 && gui.dragged_wp == wp)
4267 continue;
4268
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269#ifdef SCROLL_PAST_END
4270 max = wp->w_buffer->b_ml.ml_line_count - 1;
4271#else
4272 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4273#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004274 if (max < 0) // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 max = 0;
4276 val = wp->w_topline - 1;
4277 size = wp->w_height;
4278#ifdef SCROLL_PAST_END
Bram Moolenaar30613902019-12-01 22:11:18 +01004279 if (val > max) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 val = max;
4281#else
Bram Moolenaar30613902019-12-01 22:11:18 +01004282 if (size > max + 1) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 size = max + 1;
4284 if (val > max - size + 1)
4285 val = max - size + 1;
4286#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004287 if (val < 0) // minimal value is 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 val = 0;
4289
4290 /*
4291 * Scrollbar at index 0 (the left one) contains all the information.
4292 * It would be the same info for left and right so we just store it for
4293 * one of them.
4294 */
4295 sb = &wp->w_scrollbars[0];
4296
4297 /*
4298 * Note: no check for valid w_botline. If it's not valid the
4299 * scrollbars will be updated later anyway.
4300 */
4301 if (size < 1 || wp->w_botline - 2 > max)
4302 {
4303 /*
4304 * This can happen during changing files. Just don't update the
4305 * scrollbar for now.
4306 */
Bram Moolenaar30613902019-12-01 22:11:18 +01004307 sb->height = 0; // Force update next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 if (gui.which_scrollbars[SBAR_LEFT])
4309 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4310 if (gui.which_scrollbars[SBAR_RIGHT])
4311 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4312 continue;
4313 }
4314 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 || sb->top != wp->w_winrow
4316 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004318 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004320 // Height, width or position of scrollbar has changed. For
4321 // vertical split: curwin changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 sb->top = wp->w_winrow;
4324 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
Bram Moolenaar30613902019-12-01 22:11:18 +01004327 // Calculate height and position in pixels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 h = (sb->height + sb->status_height) * gui.char_height;
4329 y = sb->top * gui.char_height + gui.border_offset;
4330#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4331 if (gui.menu_is_active)
4332 y += gui.menu_height;
4333#endif
4334
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004335#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA) \
4336 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 y += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339#endif
4340
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004341#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004342 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004343 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004344#endif
4345
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004348 // Height of top scrollbar includes width of top border
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 h += gui.border_offset;
4350 y -= gui.border_offset;
4351 }
4352 if (gui.which_scrollbars[SBAR_LEFT])
4353 {
4354 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4355 gui.left_sbar_x, y,
4356 gui.scrollbar_width, h);
4357 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4358 }
4359 if (gui.which_scrollbars[SBAR_RIGHT])
4360 {
4361 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4362 gui.right_sbar_x, y,
4363 gui.scrollbar_width, h);
4364 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4365 }
4366 }
4367
Bram Moolenaar30613902019-12-01 22:11:18 +01004368 // Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4369 // checking if the thumb moved at least a pixel. Only do this for
4370 // Athena, most other GUIs require the update anyway to make the
4371 // arrows work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372#ifdef FEAT_GUI_ATHENA
4373 if (max == 0)
4374 y = 0;
4375 else
4376 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4377 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4378#else
4379 if (force || sb->value != val || sb->size != size || sb->max != max)
4380#endif
4381 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004382 // Thumb of scrollbar has moved
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 sb->value = val;
4384#ifdef FEAT_GUI_ATHENA
4385 sb->pixval = y;
4386#endif
4387 sb->size = size;
4388 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004389 if (gui.which_scrollbars[SBAR_LEFT]
4390 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4392 val, size, max);
4393 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004394 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4396 val, size, max);
4397 }
4398 }
Christian Brabandt2e0f3ec2021-11-28 18:41:05 +00004399
4400 // update the title, it may show the scroll position
4401 maketitle();
4402
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 --hold_gui_events;
4405}
4406
4407/*
4408 * Enable or disable a scrollbar.
4409 * Check for scrollbars for vertically split windows which are not enabled
4410 * sometimes.
4411 */
4412 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004413gui_do_scrollbar(
4414 win_T *wp,
Bram Moolenaar30613902019-12-01 22:11:18 +01004415 int which, // SBAR_LEFT or SBAR_RIGHT
4416 int enable) // TRUE to enable scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 int midcol = curwin->w_wincol + curwin->w_width / 2;
4419 int has_midcol = (wp->w_wincol <= midcol
4420 && wp->w_wincol + wp->w_width >= midcol);
4421
Bram Moolenaar30613902019-12-01 22:11:18 +01004422 // Only enable scrollbars that contain the middle column of the current
4423 // window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4425 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004426 // Scrollbars only on one side. Don't enable scrollbars that don't
4427 // contain the middle column of the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 if (!has_midcol)
4429 enable = FALSE;
4430 }
4431 else
4432 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004433 // Scrollbars on both sides. Don't enable scrollbars that neither
4434 // contain the middle column of the current window nor are on the far
4435 // side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 if (midcol > Columns / 2)
4437 {
4438 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4439 enable = FALSE;
4440 }
4441 else
4442 {
4443 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4444 : !has_midcol)
4445 enable = FALSE;
4446 }
4447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4449}
4450
4451/*
4452 * Scroll a window according to the values set in the globals current_scrollbar
4453 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4454 * or FALSE otherwise.
4455 */
4456 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004457gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458{
4459 win_T *wp, *save_wp;
4460 int i;
4461 long nlines;
4462 pos_T old_cursor;
4463 linenr_T old_topline;
4464#ifdef FEAT_DIFF
4465 int old_topfill;
4466#endif
4467
4468 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4469 if (wp == NULL)
4470 break;
4471 if (wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004472 // Couldn't find window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 return FALSE;
4474
4475 /*
4476 * Compute number of lines to scroll. If zero, nothing to do.
4477 */
4478 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4479 if (nlines == 0)
4480 return FALSE;
4481
4482 save_wp = curwin;
4483 old_topline = wp->w_topline;
4484#ifdef FEAT_DIFF
4485 old_topfill = wp->w_topfill;
4486#endif
4487 old_cursor = wp->w_cursor;
4488 curwin = wp;
4489 curbuf = wp->w_buffer;
4490 if (nlines < 0)
4491 scrolldown(-nlines, gui.dragged_wp == NULL);
4492 else
4493 scrollup(nlines, gui.dragged_wp == NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004494 // Reset dragged_wp after using it. "dragged_sb" will have been reset for
4495 // the mouse-up event already, but we still want it to behave like when
4496 // dragging. But not the next click in an arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 if (gui.dragged_sb == SBAR_NONE)
4498 gui.dragged_wp = NULL;
4499
4500 if (old_topline != wp->w_topline
4501#ifdef FEAT_DIFF
4502 || old_topfill != wp->w_topfill
4503#endif
4504 )
4505 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004506 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004508 cursor_correct(); // fix window for 'so'
4509 update_topline(); // avoid up/down jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
4511 if (old_cursor.lnum != wp->w_cursor.lnum)
4512 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
4515
Bram Moolenaar30613902019-12-01 22:11:18 +01004516 // Make sure wp->w_leftcol and wp->w_skipcol are correct.
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004517 validate_cursor();
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 curwin = save_wp;
4520 curbuf = save_wp->w_buffer;
4521
4522 /*
4523 * Don't call updateWindow() when nothing has changed (it will overwrite
4524 * the status line!).
4525 */
4526 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004527 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528#ifdef FEAT_DIFF
4529 || old_topfill != wp->w_topfill
4530#endif
4531 )
4532 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004533 int type = VALID;
4534
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004535 if (pum_visible())
4536 {
4537 type = NOT_VALID;
4538 wp->w_lines_valid = 0;
4539 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004540
Bram Moolenaar30613902019-12-01 22:11:18 +01004541 // Don't set must_redraw here, it may cause the popup menu to
4542 // disappear when losing focus after a scrollbar drag.
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004543 if (wp->w_redr_type < type)
4544 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004545 mch_disable_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01004546 updateWindow(wp); // update window, status line, and cmdline
Bram Moolenaara338adc2018-01-31 20:51:47 +01004547 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549
Bram Moolenaar30613902019-12-01 22:11:18 +01004550 // May need to redraw the popup menu.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004551 if (pum_visible())
4552 pum_redraw();
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004553
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004554 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555}
4556
4557
4558/*
4559 * Horizontal scrollbar stuff:
4560 */
4561
4562/*
4563 * Return length of line "lnum" for horizontal scrolling.
4564 */
4565 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004566scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567{
4568 char_u *p;
4569 colnr_T col;
4570 int w;
4571
4572 p = ml_get(lnum);
4573 col = 0;
4574 if (*p != NUL)
4575 for (;;)
4576 {
4577 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004578 MB_PTR_ADV(p);
Bram Moolenaar30613902019-12-01 22:11:18 +01004579 if (*p == NUL) // don't count the last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 break;
4581 col += w;
4582 }
4583 return col;
4584}
4585
Bram Moolenaar30613902019-12-01 22:11:18 +01004586// Remember which line is currently the longest, so that we don't have to
4587// search for it when scrolling horizontally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588static linenr_T longest_lnum = 0;
4589
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004590/*
4591 * Find longest visible line number. If this is not possible (or not desired,
4592 * by setting 'h' in "guioptions") then the current line number is returned.
4593 */
4594 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004595gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004596{
4597 linenr_T ret = 0;
4598
Bram Moolenaar30613902019-12-01 22:11:18 +01004599 // Calculate maximum for horizontal scrollbar. Check for reasonable
4600 // line numbers, topline and botline can be invalid when displaying is
4601 // postponed.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004602 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4603 && curwin->w_topline <= curwin->w_cursor.lnum
4604 && curwin->w_botline > curwin->w_cursor.lnum
4605 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4606 {
4607 linenr_T lnum;
4608 colnr_T n;
4609 long max = 0;
4610
Bram Moolenaar30613902019-12-01 22:11:18 +01004611 // Use maximum of all visible lines. Remember the lnum of the
4612 // longest line, closest to the cursor line. Used when scrolling
4613 // below.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004614 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4615 {
4616 n = scroll_line_len(lnum);
4617 if (n > (colnr_T)max)
4618 {
4619 max = n;
4620 ret = lnum;
4621 }
4622 else if (n == (colnr_T)max
4623 && abs((int)(lnum - curwin->w_cursor.lnum))
4624 < abs((int)(ret - curwin->w_cursor.lnum)))
4625 ret = lnum;
4626 }
4627 }
4628 else
Bram Moolenaar30613902019-12-01 22:11:18 +01004629 // Use cursor line only.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004630 ret = curwin->w_cursor.lnum;
4631
4632 return ret;
4633}
4634
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004636gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637{
Bram Moolenaar30613902019-12-01 22:11:18 +01004638 long value, size, max; // need 32 bit ints here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639
4640 if (!gui.which_scrollbars[SBAR_BOTTOM])
4641 return;
4642
4643 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4644 return;
4645
4646 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4647 return;
4648
4649 /*
4650 * It is possible for the cursor to be invalid if we're in the middle of
4651 * something (like changing files). If so, don't do anything for now.
4652 */
4653 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4654 {
4655 gui.bottom_sbar.value = -1;
4656 return;
4657 }
4658
Bram Moolenaar02631462017-09-22 15:20:32 +02004659 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (curwin->w_p_wrap)
4661 {
4662 value = 0;
4663#ifdef SCROLL_PAST_END
4664 max = 0;
4665#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004666 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667#endif
4668 }
4669 else
4670 {
4671 value = curwin->w_leftcol;
4672
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004673 longest_lnum = gui_find_longest_lnum();
4674 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 if (virtual_active())
4677 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004678 // May move the cursor even further to the right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (curwin->w_virtcol >= (colnr_T)max)
4680 max = curwin->w_virtcol;
4681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
4683#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004684 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004686 // The line number isn't scrolled, thus there is less space when
4687 // 'number' or 'relativenumber' is set (also for 'foldcolumn').
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 size -= curwin_col_off();
4689#ifndef SCROLL_PAST_END
4690 max -= curwin_col_off();
4691#endif
4692 }
4693
4694#ifndef SCROLL_PAST_END
4695 if (value > max - size + 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01004696 value = max - size + 1; // limit the value to allowable range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697#endif
4698
4699#ifdef FEAT_RIGHTLEFT
4700 if (curwin->w_p_rl)
4701 {
4702 value = max + 1 - size - value;
4703 if (value < 0)
4704 {
4705 size += value;
4706 value = 0;
4707 }
4708 }
4709#endif
4710 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4711 && max == gui.bottom_sbar.max)
4712 return;
4713
4714 gui.bottom_sbar.value = value;
4715 gui.bottom_sbar.size = size;
4716 gui.bottom_sbar.max = max;
4717 gui.prev_wrap = curwin->w_p_wrap;
4718
4719 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4720}
4721
4722/*
4723 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4724 */
4725 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004726gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727{
Bram Moolenaar30613902019-12-01 22:11:18 +01004728 // no wrapping, no scrolling
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 if (curwin->w_p_wrap)
4730 return FALSE;
4731
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004732 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 return FALSE;
4734
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004735 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
Bram Moolenaar30613902019-12-01 22:11:18 +01004737 // When the line of the cursor is too short, move the cursor to the
4738 // longest visible line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004740 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004741 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004743 if (compute_longest_lnum)
4744 {
4745 curwin->w_cursor.lnum = gui_find_longest_lnum();
4746 curwin->w_cursor.col = 0;
4747 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004748 // Do a sanity check on "longest_lnum", just in case.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004749 else if (longest_lnum >= curwin->w_topline
4750 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752 curwin->w_cursor.lnum = longest_lnum;
4753 curwin->w_cursor.col = 0;
4754 }
4755 }
4756
4757 return leftcol_changed();
4758}
4759
4760/*
4761 * Check that none of the colors are the same as the background color
4762 */
4763 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004764gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
4766 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4767 {
4768 gui_set_bg_color((char_u *)"White");
4769 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4770 gui_set_fg_color((char_u *)"Black");
4771 }
4772}
4773
Bram Moolenaar3918c952005-03-15 22:34:55 +00004774 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004775gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776{
4777 gui.norm_pixel = gui_get_color(name);
4778 hl_set_fg_color_name(vim_strsave(name));
4779}
4780
Bram Moolenaar3918c952005-03-15 22:34:55 +00004781 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004782gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783{
4784 gui.back_pixel = gui_get_color(name);
4785 hl_set_bg_color_name(vim_strsave(name));
4786}
4787
4788/*
4789 * Allocate a color by name.
4790 * Returns INVALCOLOR and gives an error message when failed.
4791 */
4792 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004793gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794{
4795 guicolor_T t;
4796
4797 if (*name == NUL)
4798 return INVALCOLOR;
4799 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004800
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004802#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 && gui.in_use
4804#endif
4805 )
Drew Vogele30d1022021-10-24 20:35:07 +01004806 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 return t;
4808}
4809
4810/*
4811 * Return the grey value of a color (range 0-255).
4812 */
4813 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004814gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004816 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004818 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004819 + (((rgb >> 8) & 0xff) * 587)
4820 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821}
4822
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004823 char_u *
4824gui_bg_default(void)
4825{
4826 if (gui_get_lightness(gui.back_pixel) < 127)
4827 return (char_u *)"dark";
4828 return (char_u *)"light";
4829}
4830
4831/*
4832 * Option initializations that can only be done after opening the GUI window.
4833 */
4834 void
4835init_gui_options(void)
4836{
Bram Moolenaar30613902019-12-01 22:11:18 +01004837 // Set the 'background' option according to the lightness of the
4838 // background color, unless the user has set it already.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004839 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
4840 {
4841 set_option_value((char_u *)"bg", 0L, gui_bg_default(), 0);
4842 highlight_changed();
4843 }
4844}
4845
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846#if defined(FEAT_GUI_X11) || defined(PROTO)
4847 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004848gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
4850 win_T *wp;
4851
Bram Moolenaar30613902019-12-01 22:11:18 +01004852 // Nothing to do if GUI hasn't started yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 if (!gui.in_use)
4854 return;
4855
4856 FOR_ALL_WINDOWS(wp)
4857 {
4858 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4859 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4860 }
4861 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4862}
4863#endif
4864
4865/*
4866 * Call this when focus has changed.
4867 */
4868 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004869gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
4871/*
4872 * Skip this code to avoid drawing the cursor when debugging and switching
4873 * between the debugger window and gvim.
4874 */
4875#if 1
4876 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004877 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
4879# ifdef FEAT_XIM
4880 xim_set_focus(in_focus);
4881# endif
4882
Bram Moolenaar30613902019-12-01 22:11:18 +01004883 // Put events in the input queue only when allowed.
4884 // ui_focus_change() isn't called directly, because it invokes
4885 // autocommands and that must not happen asynchronously.
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004886 if (!hold_gui_events)
4887 {
4888 char_u bytes[3];
4889
4890 bytes[0] = CSI;
4891 bytes[1] = KS_EXTRA;
4892 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4893 add_to_input_buf(bytes, 3);
4894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895#endif
4896}
4897
4898/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004899 * When mouse moved: apply 'mousefocus'.
4900 * Also updates the mouse pointer shape.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 */
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004902 static void
4903gui_mouse_focus(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
4905 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004906 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
4908#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01004909 // Get window pointer, and update mouse shape as well.
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004910 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911#endif
4912
Bram Moolenaar30613902019-12-01 22:11:18 +01004913 // Only handle this when 'mousefocus' set and ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (p_mousef
Bram Moolenaar30613902019-12-01 22:11:18 +01004915 && !hold_gui_events // not holding events
4916 && (State & (NORMAL|INSERT))// Normal/Visual/Insert mode
4917 && State != HITRETURN // but not hit-return prompt
4918 && msg_scrolled == 0 // no scrolled message
4919 && !need_mouse_correct // not moving the pointer
4920 && gui.in_focus) // gvim in focus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004922 // Don't move the mouse when it's left or right of the Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 if (x < 0 || x > Columns * gui.char_width)
4924 return;
4925#ifndef FEAT_MOUSESHAPE
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004926 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927#endif
4928 if (wp == curwin || wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004929 return; // still in the same old window, or none at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
Bram Moolenaar30613902019-12-01 22:11:18 +01004931 // Ignore position in the tab pages line.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004932 if (Y_2_ROW(y) < tabline_height())
4933 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 /*
4936 * format a mouse click on status line input
4937 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004938 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4939 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 */
4941 if (finish_op)
4942 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004943 // abort the current operator first
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 st[0] = ESC;
4945 add_to_input_buf(st, 1);
4946 }
4947 st[0] = CSI;
4948 st[1] = KS_MOUSE;
4949 st[2] = KE_FILLER;
4950 st[3] = (char_u)MOUSE_LEFT;
4951 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004952 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 wp->w_height + W_WINROW(wp));
4954
4955 add_to_input_buf(st, 8);
4956 st[3] = (char_u)MOUSE_RELEASE;
4957 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004959 // Need to wake up the main loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 if (gtk_main_level() > 0)
4961 gtk_main_quit();
4962#endif
4963 }
4964}
4965
4966/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004967 * Called when the mouse moved (but not when dragging).
4968 */
4969 void
4970gui_mouse_moved(int x, int y)
4971{
4972 // Ignore this while still starting up.
4973 if (!gui.in_use || gui.starting)
4974 return;
4975
4976 // apply 'mousefocus' and pointer shape
4977 gui_mouse_focus(x, y);
4978
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004979#ifdef FEAT_PROP_POPUP
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004980 if (popup_visible)
4981 // Generate a mouse-moved event, so that the popup can perhaps be
4982 // closed, just like in the terminal.
Bram Moolenaar445f11d2021-06-08 20:13:31 +02004983 gui_send_mouse_event(MOUSE_MOVE, x, y, FALSE, 0);
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004984#endif
4985}
4986
4987/*
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004988 * Get the window where the mouse pointer is on.
4989 * Returns NULL if not found.
4990 */
4991 win_T *
4992gui_mouse_window(mouse_find_T popup)
4993{
4994 int x, y;
4995
4996 if (!(gui.in_use && (p_mousef || popup == FIND_POPUP)))
4997 return NULL;
4998 gui_mch_getmouse(&x, &y);
4999
5000 // Only use the mouse when it's on the Vim window
5001 if (x >= 0 && x <= Columns * gui.char_width
5002 && y >= 0 && Y_2_ROW(y) >= tabline_height())
5003 return xy2win(x, y, popup);
5004 return NULL;
5005}
5006
5007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 * Called when mouse should be moved to window with focus.
5009 */
5010 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005011gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 win_T *wp = NULL;
5014
5015 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005016
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005017 wp = gui_mouse_window(IGNORE_POPUP);
Bram Moolenaar30613902019-12-01 22:11:18 +01005018 if (wp != curwin && wp != NULL) // If in other than current window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005020 validate_cline_row();
5021 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
5022 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025}
5026
5027/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005028 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01005029 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 static win_T *
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005032xy2win(int x, int y, mouse_find_T popup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 int row;
5035 int col;
5036 win_T *wp;
5037
5038 row = Y_2_ROW(y);
5039 col = X_2_COL(x);
Bram Moolenaar30613902019-12-01 22:11:18 +01005040 if (row < 0 || col < 0) // before first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 return NULL;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005042 wp = mouse_find_win(&row, &col, popup);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005043 if (wp == NULL)
5044 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005045#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if (State == HITRETURN || State == ASKMORE)
5047 {
5048 if (Y_2_ROW(y) >= msg_row)
5049 update_mouseshape(SHAPE_IDX_MOREL);
5050 else
5051 update_mouseshape(SHAPE_IDX_MORE);
5052 }
Bram Moolenaar30613902019-12-01 22:11:18 +01005053 else if (row > wp->w_height) // below status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02005055 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005056 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02005058 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005059 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 update_mouseshape(SHAPE_IDX_STATUS);
5061 else
5062 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005064 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065}
5066
5067/*
5068 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
5069 * File names may be given to redefine the args list.
5070 */
5071 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005072ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073{
5074 char_u *arg = eap->arg;
5075
5076 /*
5077 * Check for "-f" argument: foreground, don't fork.
5078 * Also don't fork when started with "gvim -f".
5079 * Do fork when using "gui -b".
5080 */
5081 if (arg[0] == '-'
5082 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01005083 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085 gui.dofork = (arg[1] == 'b');
5086 eap->arg = skipwhite(eap->arg + 2);
5087 }
5088 if (!gui.in_use)
5089 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005090#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaar257a3962019-12-29 15:19:03 +01005091 if (!gui.starting)
5092 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005093 emsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaar257a3962019-12-29 15:19:03 +01005094 return;
5095 }
5096#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005097 // Clear the command. Needed for when forking+exiting, to avoid part
5098 // of the argument ending up after the shell prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 msg_clr_eos_force();
Bram Moolenaar257a3962019-12-29 15:19:03 +01005100#ifdef GUI_MAY_SPAWN
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02005101 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005102 gui_start(eap->arg);
5103 else
Bram Moolenaar257a3962019-12-29 15:19:03 +01005104#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005105 gui_start(NULL);
Bram Moolenaar257a3962019-12-29 15:19:03 +01005106#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01005107 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02005110 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 ex_next(eap);
5112}
5113
Bram Moolenaar4f974752019-02-17 17:44:42 +01005114#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005115 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON) \
5116 || defined(FEAT_GUI_HAIKU)) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01005117 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118/*
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005119 * This is shared between Athena, Haiku, Motif, and GTK.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121
5122/*
5123 * Callback function for do_in_runtimepath().
5124 */
5125 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005126gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005128 char_u *gfp_buffer = cookie;
5129
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (STRLEN(fname) >= MAXPATHL)
5131 *gfp_buffer = NUL;
5132 else
5133 STRCPY(gfp_buffer, fname);
5134}
5135
5136/*
5137 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5138 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5139 */
5140 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005141gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142{
5143 if (STRLEN(name) > MAXPATHL - 14)
5144 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005145 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005146 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005147 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 return FAIL;
5149 return OK;
5150}
5151
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005152# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153/*
5154 * Given the name of the "icon=" argument, try finding the bitmap file for the
5155 * icon. If it is an absolute path name, use it as it is. Otherwise append
5156 * "ext" and search for it in 'runtimepath'.
5157 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5158 * contains "name".
5159 */
5160 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005161gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162{
5163 char_u buf[MAXPATHL + 1];
5164
5165 expand_env(name, buffer, MAXPATHL);
5166 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5167 STRCPY(buffer, buf);
5168}
5169# endif
5170#endif
5171
Bram Moolenaar9e175142020-04-30 22:51:01 +02005172#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)|| defined(FEAT_GUI_HAIKU) \
5173 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005175display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176{
5177 char_u *p;
5178
5179 if (isatty(2))
5180 fflush(stderr);
5181 else if (error_ga.ga_data != NULL)
5182 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005183 // avoid putting up a message box with blanks only
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5185 if (!isspace(*p))
5186 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005187 // Truncate a very long message, it will go off-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 if (STRLEN(p) > 2000)
5189 STRCPY(p + 2000 - 14, "...(truncated)");
5190 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005191 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 break;
5193 }
5194 ga_clear(&error_ga);
5195 }
5196}
5197#endif
5198
5199#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5200/*
5201 * Return TRUE if still starting up and there is no place to enter text.
5202 * For GTK and X11 we check if stderr is not a tty, which means we were
5203 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5204 * allow typing on stdin.
5205 */
5206 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005207no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208{
5209 return ((!gui.in_use || gui.starting)
5210# ifndef NO_CONSOLE
5211 && !isatty(0) && !isatty(2)
5212# endif
5213 );
5214}
5215#endif
5216
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005217#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005218 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005219 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220/*
5221 * Update the current window and the screen.
5222 */
5223 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005224gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005226# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005227 linenr_T conceal_old_cursor_line = 0;
5228 linenr_T conceal_new_cursor_line = 0;
5229 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005230# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005231
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 update_topline();
5233 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005234
Bram Moolenaar30613902019-12-01 22:11:18 +01005235 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005236 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005237# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005238 || popup_visible
5239# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005240# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005241 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005242# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005243 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005244 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005245 if (has_cursormoved())
5246 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005247#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005248 if (popup_visible)
5249 popup_check_cursor_pos();
5250#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005251# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005252 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005253 {
5254 conceal_old_cursor_line = last_cursormoved.lnum;
5255 conceal_new_cursor_line = curwin->w_cursor.lnum;
5256 conceal_update_lines = TRUE;
5257 }
5258# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005259 last_cursormoved = curwin->w_cursor;
5260 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005261
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005262# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005263 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005264 && (conceal_old_cursor_line != conceal_new_cursor_line
5265 || conceal_cursor_line(curwin)
5266 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005267 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005268 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005269 redrawWinline(curwin, conceal_old_cursor_line);
5270 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005271 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005272 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005273 }
5274# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005275 update_screen(0); // may need to update the screen
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005276 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005277 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278}
5279#endif
5280
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005281#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282/*
5283 * Get the text to use in a find/replace dialog. Uses the last search pattern
5284 * if the argument is empty.
5285 * Returns an allocated string.
5286 */
5287 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005288get_find_dialog_text(
5289 char_u *arg,
Bram Moolenaar30613902019-12-01 22:11:18 +01005290 int *wwordp, // return: TRUE if \< \> found
5291 int *mcasep) // return: TRUE if \C found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
5293 char_u *text;
5294
5295 if (*arg == NUL)
5296 text = last_search_pat();
5297 else
5298 text = arg;
5299 if (text != NULL)
5300 {
5301 text = vim_strsave(text);
5302 if (text != NULL)
5303 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005304 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 int i;
5306
Bram Moolenaar30613902019-12-01 22:11:18 +01005307 // Remove "\V"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5309 {
5310 mch_memmove(text, text + 2, (size_t)(len - 1));
5311 len -= 2;
5312 }
5313
Bram Moolenaar30613902019-12-01 22:11:18 +01005314 // Recognize "\c" and "\C" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5316 {
5317 *mcasep = (text[1] == 'C');
5318 mch_memmove(text, text + 2, (size_t)(len - 1));
5319 len -= 2;
5320 }
5321
Bram Moolenaar30613902019-12-01 22:11:18 +01005322 // Recognize "\<text\>" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 if (len >= 4
5324 && STRNCMP(text, "\\<", 2) == 0
5325 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5326 {
5327 *wwordp = TRUE;
5328 mch_memmove(text, text + 2, (size_t)(len - 4));
5329 text[len - 4] = NUL;
5330 }
5331
Bram Moolenaar30613902019-12-01 22:11:18 +01005332 // Recognize "\/" or "\?" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 for (i = 0; i + 1 < len; ++i)
5334 if (text[i] == '\\' && (text[i + 1] == '/'
5335 || text[i + 1] == '?'))
5336 {
5337 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5338 --len;
5339 }
5340 }
5341 }
5342 return text;
5343}
5344
5345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 * Handle the press of a button in the find-replace dialog.
5347 * Return TRUE when something was added to the input buffer.
5348 */
5349 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005350gui_do_findrepl(
Bram Moolenaar30613902019-12-01 22:11:18 +01005351 int flags, // one of FRD_REPLACE, FRD_FINDNEXT, etc.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005352 char_u *find_text,
5353 char_u *repl_text,
Bram Moolenaar30613902019-12-01 22:11:18 +01005354 int down) // Search downwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355{
5356 garray_T ga;
5357 int i;
5358 int type = (flags & FRD_TYPE_MASK);
5359 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005360 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005361 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005362 static int busy = FALSE;
5363
Bram Moolenaar30613902019-12-01 22:11:18 +01005364 // When the screen is being updated we should not change buffers and
5365 // windows structures, it may cause freed memory to be used. Also don't
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005366 // do this recursively (pressing "Find" quickly several times).
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005367 if (updating_screen || busy)
5368 return FALSE;
5369
Bram Moolenaar30613902019-12-01 22:11:18 +01005370 // refuse replace when text cannot be changed
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005371 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5372 return FALSE;
5373
5374 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005377 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 ga_concat(&ga, (char_u *)"%s/");
5379
5380 ga_concat(&ga, (char_u *)"\\V");
5381 if (flags & FRD_MATCH_CASE)
5382 ga_concat(&ga, (char_u *)"\\C");
5383 else
5384 ga_concat(&ga, (char_u *)"\\c");
5385 if (flags & FRD_WHOLE_WORD)
5386 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar30613902019-12-01 22:11:18 +01005387 // escape slash and backslash
Bram Moolenaar518bc172018-05-13 17:05:30 +02005388 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5389 if (p != NULL)
5390 ga_concat(&ga, p);
5391 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 if (flags & FRD_WHOLE_WORD)
5393 ga_concat(&ga, (char_u *)"\\>");
5394
5395 if (type == FRD_REPLACEALL)
5396 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar30613902019-12-01 22:11:18 +01005398 // escape slash and backslash
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005399 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5400 if (p != NULL)
5401 ga_concat(&ga, p);
5402 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005404 }
5405 ga_append(&ga, NUL);
5406
5407 if (type == FRD_REPLACE)
5408 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005409 // Do the replacement when the text at the cursor matches. Thus no
5410 // replacement is done if the cursor was moved!
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005411 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5412 regmatch.rm_ic = 0;
5413 if (regmatch.regprog != NULL)
5414 {
5415 p = ml_get_cursor();
5416 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5417 && regmatch.startp[0] == p)
5418 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005419 // Clear the command line to remove any old "No match"
5420 // error.
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005421 msg_end_prompt();
5422
5423 if (u_save_cursor() == OK)
5424 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005425 // A button was pressed thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005426 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005427
5428 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005429 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005430 ins_str(repl_text);
5431 }
5432 }
5433 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005434 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005435 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005436 }
5437 }
5438
5439 if (type == FRD_REPLACEALL)
5440 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005441 // A button was pressed, thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005442 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 do_cmdline_cmd(ga.ga_data);
5444 }
5445 else
5446 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005447 int searchflags = SEARCH_MSG + SEARCH_MARK;
5448
Bram Moolenaar30613902019-12-01 22:11:18 +01005449 // Search for the next match.
5450 // Don't skip text under cursor for single replace.
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005451 if (type == FRD_REPLACE)
5452 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005454 if (down)
5455 {
Bram Moolenaarc036e872020-02-21 21:30:52 +01005456 (void)do_search(NULL, '/', '/', ga.ga_data, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005457 }
5458 else
5459 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005460 // We need to escape '?' if and only if we are searching in the up
5461 // direction
Bram Moolenaar518bc172018-05-13 17:05:30 +02005462 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5463 if (p != NULL)
Bram Moolenaarc036e872020-02-21 21:30:52 +01005464 (void)do_search(NULL, '?', '?', p, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005465 vim_free(p);
5466 }
5467
Bram Moolenaar30613902019-12-01 22:11:18 +01005468 msg_scroll = i; // don't let an error message set msg_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 }
5470
Bram Moolenaar30613902019-12-01 22:11:18 +01005471 // Don't want to pass did_emsg to other code, it may cause disabling
5472 // syntax HL if we were busy redrawing.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005473 did_emsg = save_did_emsg;
5474
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if (State & (NORMAL | INSERT))
5476 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005477 gui_update_screen(); // update the screen
5478 msg_didout = 0; // overwrite any message
5479 need_wait_return = FALSE; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
5481
5482 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005483 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 return (ga.ga_len > 0);
5485}
5486
5487#endif
5488
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005489#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490/*
5491 * Jump to the window at specified point (x, y).
5492 */
5493 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005494gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
5496 int row = Y_2_ROW(y);
5497 int col = X_2_COL(x);
5498 win_T *wp;
5499
5500 if (row >= 0 && col >= 0)
5501 {
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005502 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 if (wp != NULL && wp != curwin)
5504 win_goto(wp);
5505 }
5506}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
5508/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005509 * Function passed to handle_drop() for the actions to be done after the
5510 * argument list has been updated.
5511 */
5512 static void
5513drop_callback(void *cookie)
5514{
5515 char_u *p = cookie;
Bram Moolenaar4671e882021-11-22 17:21:48 +00005516 int do_shorten = FALSE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005517
Bram Moolenaar30613902019-12-01 22:11:18 +01005518 // If Shift held down, change to first file's directory. If the first
5519 // item is a directory, change to that directory (and let the explorer
5520 // plugin show the contents).
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005521 if (p != NULL)
5522 {
5523 if (mch_isdir(p))
5524 {
5525 if (mch_chdir((char *)p) == 0)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005526 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005527 }
5528 else if (vim_chdirfile(p, "drop") == OK)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005529 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005530 vim_free(p);
Bram Moolenaar4671e882021-11-22 17:21:48 +00005531 if (do_shorten)
5532 {
5533 shorten_fnames(TRUE);
5534 last_chdir_reason = "drop";
5535 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005536 }
5537
Bram Moolenaar30613902019-12-01 22:11:18 +01005538 // Update the screen display
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005539 update_screen(NOT_VALID);
5540# ifdef FEAT_MENU
5541 gui_update_menus(0);
5542# endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005543 maketitle();
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005544 setcursor();
5545 out_flush_cursor(FALSE, FALSE);
5546}
5547
5548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 * Process file drop. Mouse cursor position, key modifiers, name of files
5550 * and count of files are given. Argument "fnames[count]" has full pathnames
5551 * of dropped files, they will be freed in this function, and caller can't use
5552 * fnames after call this function.
5553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005555gui_handle_drop(
5556 int x UNUSED,
5557 int y UNUSED,
5558 int_u modifiers,
5559 char_u **fnames,
5560 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
5562 int i;
5563 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005564 static int entered = FALSE;
5565
5566 /*
5567 * This function is called by event handlers. Just in case we get a
5568 * second event before the first one is handled, ignore the second one.
5569 * Not sure if this can ever happen, just in case.
5570 */
5571 if (entered)
5572 return;
5573 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 /*
5576 * When the cursor is at the command line, add the file names to the
5577 * command line, don't edit the files.
5578 */
5579 if (State & CMDLINE)
5580 {
5581 shorten_filenames(fnames, count);
5582 for (i = 0; i < count; ++i)
5583 {
5584 if (fnames[i] != NULL)
5585 {
5586 if (i > 0)
5587 add_to_input_buf((char_u*)" ", 1);
5588
Bram Moolenaar30613902019-12-01 22:11:18 +01005589 // We don't know what command is used thus we can't be sure
5590 // about which characters need to be escaped. Only escape the
5591 // most common ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592# ifdef BACKSLASH_IN_FILENAME
5593 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5594# else
5595 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5596# endif
5597 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005598 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 vim_free(p);
5600 vim_free(fnames[i]);
5601 }
5602 }
5603 vim_free(fnames);
5604 }
5605 else
5606 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005607 // Go to the window under mouse cursor, then shorten given "fnames" by
5608 // current window, because a window can have local current dir.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 shorten_filenames(fnames, count);
5611
Bram Moolenaar30613902019-12-01 22:11:18 +01005612 // If Shift held down, remember the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 if ((modifiers & MOUSE_SHIFT) != 0)
5614 p = vim_strsave(fnames[0]);
5615 else
5616 p = NULL;
5617
Bram Moolenaar30613902019-12-01 22:11:18 +01005618 // Handle the drop, :edit or :split to get to the file. This also
5619 // frees fnames[]. Skip this if there is only one item, it's a
5620 // directory and Shift is held down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5622 && mch_isdir(fnames[0]))
5623 {
5624 vim_free(fnames[0]);
5625 vim_free(fnames);
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02005626 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 }
5628 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005629 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
Bram Moolenaar4671e882021-11-22 17:21:48 +00005630 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005632
5633 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634}
5635#endif
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005636
5637/*
5638 * Check if "key" is to interrupt us. Handles a key that has not had modifiers
5639 * applied yet.
5640 * Return the key with modifiers applied if so, NUL if not.
5641 */
5642 int
5643check_for_interrupt(int key, int modifiers_arg)
5644{
5645 int modifiers = modifiers_arg;
5646 int c = merge_modifyOtherKeys(key, &modifiers);
5647
5648 if ((c == Ctrl_C && ctrl_c_interrupts)
Bram Moolenaaraf50e892020-08-01 13:22:10 +02005649#ifdef UNIX
5650 || (intr_char != Ctrl_C && c == intr_char)
5651#endif
5652 )
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005653 {
5654 got_int = TRUE;
5655 return c;
5656 }
5657 return NUL;
5658}
5659