blob: 68c64d298e5a5c8b117cc3a7f2a98b600facd7b9 [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/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 * gui_start -- Called when user wants to start the GUI.
57 *
58 * Careful: This function can be called recursively when there is a ":gui"
59 * command in the .gvimrc file. Only the first call should fork, not the
60 * recursive call.
61 */
62 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +020063gui_start(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000064{
65 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000066 static int recursive = 0;
Bram Moolenaar68cbb142019-05-09 14:14:42 +020067#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +020068 char *msg = NULL;
69#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71 old_term = vim_strsave(T_NAME);
72
Bram Moolenaar30613902019-12-01 22:11:18 +010073 settmode(TMODE_COOK); // stop RAW mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 if (full_screen)
Bram Moolenaar30613902019-12-01 22:11:18 +010075 cursor_on(); // needed for ":gui" in .vimrc
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 full_screen = FALSE;
77
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 ++recursive;
79
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020080#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020081 /*
82 * Quit the current process and continue in the child.
83 * Makes "gvim file" disconnect from the shell it was started in.
84 * Don't do this when Vim was started with "-f" or the 'f' flag is present
85 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020086 * Don't do this when there is a running job, we can only get the status
87 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020088 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020089 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
90# ifdef FEAT_JOB_CHANNEL
91 && !job_any_running()
92# endif
93 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020094 {
95 gui_do_fork();
96 }
97 else
98#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +020099#ifdef GUI_MAY_SPAWN
100 if (gui.dospawn
101# ifdef EXPERIMENTAL_GUI_CMD
102 && gui.dofork
103# endif
104 && !vim_strchr(p_go, GO_FORG)
105 && !anyBufIsChanged()
106# ifdef FEAT_JOB_CHANNEL
107 && !job_any_running()
108# endif
109 )
110 {
Bram Moolenaar68cbb142019-05-09 14:14:42 +0200111# ifdef EXPERIMENTAL_GUI_CMD
112 msg =
113# endif
114 gui_mch_do_spawn(arg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200115 }
116 else
117#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200118 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200119#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100120 // If there is 'f' in 'guioptions' and specify -g argument,
121 // gui_mch_init_check() was not called yet.
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200122 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100123 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200124#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200125 gui_attempt_start();
126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
Bram Moolenaar30613902019-12-01 22:11:18 +0100128 if (!gui.in_use) // failed to start GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100130 // Back to old term settings
131 //
132 // FIXME: If we got here because a child process failed and flagged to
Bram Moolenaar651fca82021-11-29 20:39:38 +0000133 // the parent to resume, and X11 is enabled, this will
Bram Moolenaar30613902019-12-01 22:11:18 +0100134 // hit an X11 I/O error and do a longjmp(), leaving recursive
135 // permanently set to 1. This is probably not as big a problem as it
136 // sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
137 // return "OK" unconditionally, so it would be very difficult to
138 // actually hit this case.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200139 termcapinit(old_term);
Bram Moolenaar30613902019-12-01 22:11:18 +0100140 settmode(TMODE_RAW); // restart RAW mode
Bram Moolenaar30613902019-12-01 22:11:18 +0100141 set_title_defaults(); // set 'title' and 'icon' again
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200142#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
143 if (msg)
144 emsg(msg);
145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 }
147
148 vim_free(old_term);
149
Bram Moolenaar30613902019-12-01 22:11:18 +0100150 // If the GUI started successfully, trigger the GUIEnter event, otherwise
151 // the GUIFailed event.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200152 gui_mch_update();
153 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
154 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200155 --recursive;
156}
157
158/*
159 * Set_termname() will call gui_init() to start the GUI.
160 * Set the "starting" flag, to indicate that the GUI will start.
161 *
162 * We don't want to open the GUI shell until after we've read .gvimrc,
163 * otherwise we don't know what font we will use, and hence we don't know
164 * what size the shell should be. So if there are errors in the .gvimrc
165 * file, they will have to go to the terminal: Set full_screen to FALSE.
166 * full_screen will be set to TRUE again by a successful termcapinit().
167 */
168 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100169gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200170{
171 static int recursive = 0;
172
173 ++recursive;
174 gui.starting = TRUE;
175
176#ifdef FEAT_GUI_GTK
177 gui.event_time = GDK_CURRENT_TIME;
178#endif
179
180 termcapinit((char_u *)"builtin_gui");
181 gui.starting = recursive - 1;
182
Bram Moolenaar9372a112005-12-06 19:59:18 +0000183#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200185 {
186# ifdef FEAT_EVAL
187 Window x11_window;
188 Display *x11_display;
189
190 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
191 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
192# endif
193
Bram Moolenaar30613902019-12-01 22:11:18 +0100194 // Display error messages in a dialog now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 --recursive;
199}
200
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200201#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200202
Bram Moolenaar30613902019-12-01 22:11:18 +0100203// for waitpid()
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200204# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
205# include <sys/wait.h>
206# endif
207
208/*
209 * Create a new process, by forking. In the child, start the GUI, and in
210 * the parent, exit.
211 *
212 * If something goes wrong, this will return with gui.in_use still set
213 * to FALSE, in which case the caller should continue execution without
214 * the GUI.
215 *
216 * If the child fails to start the GUI, then the child will exit and the
217 * parent will return. If the child succeeds, then the parent will exit
218 * and the child will return.
219 */
220 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100221gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200222{
Bram Moolenaar30613902019-12-01 22:11:18 +0100223 int pipefd[2]; // pipe between parent and child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200224 int pipe_error;
225 int status;
226 int exit_status;
227 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200228
Bram Moolenaar30613902019-12-01 22:11:18 +0100229 // Setup a pipe between the child and the parent, so that the parent
230 // knows when the child has done the setsid() call and is allowed to
231 // exit.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200232 pipe_error = (pipe(pipefd) < 0);
233 pid = fork();
Bram Moolenaar30613902019-12-01 22:11:18 +0100234 if (pid < 0) // Fork error
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200235 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000236 emsg(_(e_failed_to_create_new_process_for_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200237 return;
238 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100239 else if (pid > 0) // Parent
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200240 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100241 // Give the child some time to do the setsid(), otherwise the
242 // exit() may kill the child too (when starting gvim from inside a
243 // gvim).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200244 if (!pipe_error)
245 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100246 // The read returns when the child closes the pipe (or when
247 // the child dies for some reason).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200248 close(pipefd[1]);
249 status = gui_read_child_pipe(pipefd[0]);
250 if (status == GUI_CHILD_FAILED)
251 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100252 // The child failed to start the GUI, so the caller must
253 // continue. There may be more error information written
254 // to stderr by the child.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200255# ifdef __NeXT__
256 wait4(pid, &exit_status, 0, (struct rusage *)0);
257# else
258 waitpid(pid, &exit_status, 0);
259# endif
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000260 emsg(_(e_the_child_process_failed_to_start_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200261 return;
262 }
263 else if (status == GUI_CHILD_IO_ERROR)
264 {
265 pipe_error = TRUE;
266 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100267 // else GUI_CHILD_OK: parent exit
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200268 }
269
270 if (pipe_error)
Bram Moolenaareda1da02019-11-17 17:06:33 +0100271 ui_delay(301L, TRUE);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200272
Bram Moolenaar30613902019-12-01 22:11:18 +0100273 // When swapping screens we may need to go to the next line, e.g.,
274 // after a hit-enter prompt and using ":gui".
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200275 if (newline_on_exit)
276 mch_errmsg("\r\n");
277
278 /*
279 * The parent must skip the normal exit() processing, the child
280 * will do it. For example, GTK messes up signals when exiting.
281 */
282 _exit(0);
283 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100284 // Child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200285
K.Takata6e1d31e2022-02-03 13:05:32 +0000286# ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100287 // Call gtk_init_check() here after fork(). See gui_init_check().
Bram Moolenaar29695702012-05-18 17:03:18 +0200288 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100289 getout_preserve_modified(1);
K.Takata6e1d31e2022-02-03 13:05:32 +0000290# endif
Bram Moolenaar29695702012-05-18 17:03:18 +0200291
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200292# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
293 /*
294 * Change our process group. On some systems/shells a CTRL-C in the
295 * shell where Vim was started would otherwise kill gvim!
296 */
297# if defined(HAVE_SETSID)
298 (void)setsid();
299# else
300 (void)setpgid(0, 0);
301# endif
302# endif
303 if (!pipe_error)
304 close(pipefd[0]);
305
306# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
Bram Moolenaar30613902019-12-01 22:11:18 +0100307 // Tell the session manager our new PID
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200308 gui_mch_forked();
309# endif
310
Bram Moolenaar30613902019-12-01 22:11:18 +0100311 // Try to start the GUI
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200312 gui_attempt_start();
313
Bram Moolenaar30613902019-12-01 22:11:18 +0100314 // Notify the parent
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200315 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200316 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200317 if (gui.in_use)
318 write_eintr(pipefd[1], "ok", 3);
319 else
320 write_eintr(pipefd[1], "fail", 5);
321 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200322 }
323
Bram Moolenaar30613902019-12-01 22:11:18 +0100324 // If we failed to start the GUI, exit now.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200325 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100326 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200327}
328
329/*
330 * Read from a pipe assumed to be connected to the child process (this
331 * function is called from the parent).
332 * Return GUI_CHILD_OK if the child successfully started the GUI,
333 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
334 * some other error.
335 *
336 * The file descriptor will be closed before the function returns.
337 */
338 static int
339gui_read_child_pipe(int fd)
340{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200341 long bytes_read;
K.Takata6e1d31e2022-02-03 13:05:32 +0000342# define READ_BUFFER_SIZE 10
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200343 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200344
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200345 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
K.Takata6e1d31e2022-02-03 13:05:32 +0000346# undef READ_BUFFER_SIZE
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200347 close(fd);
348 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200349 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200350 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200351 if (strcmp(buffer, "ok") == 0)
352 return GUI_CHILD_OK;
353 return GUI_CHILD_FAILED;
354}
355
Bram Moolenaar30613902019-12-01 22:11:18 +0100356#endif // GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358/*
359 * Call this when vim starts up, whether or not the GUI is started
360 */
361 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100362gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363{
Bram Moolenaar30613902019-12-01 22:11:18 +0100364 gui.in_use = FALSE; // No GUI yet (maybe later)
365 gui.starting = FALSE; // No GUI yet (maybe later)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 gui_mch_prepare(argc, argv);
367}
368
369/*
370 * Try initializing the GUI and check if it can be started.
371 * Used from main() to check early if "vim -g" can start the GUI.
372 * Used from gui_init() to prepare for starting the GUI.
373 * Returns FAIL or OK.
374 */
375 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100376gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377{
378 static int result = MAYBE;
379
380 if (result != MAYBE)
381 {
382 if (result == FAIL)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000383 emsg(_(e_cannot_start_the_GUI));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 return result;
385 }
386
387 gui.shell_created = FALSE;
388 gui.dying = FALSE;
Bram Moolenaar30613902019-12-01 22:11:18 +0100389 gui.in_focus = TRUE; // so the guicursor setting works
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 gui.dragged_sb = SBAR_NONE;
391 gui.dragged_wp = NULL;
392 gui.pointer_hidden = FALSE;
393 gui.col = 0;
394 gui.row = 0;
395 gui.num_cols = Columns;
396 gui.num_rows = Rows;
397
398 gui.cursor_is_valid = FALSE;
399 gui.scroll_region_top = 0;
400 gui.scroll_region_bot = Rows - 1;
401 gui.scroll_region_left = 0;
402 gui.scroll_region_right = Columns - 1;
403 gui.highlight_mask = HL_NORMAL;
404 gui.char_width = 1;
405 gui.char_height = 1;
406 gui.char_ascent = 0;
407 gui.border_width = 0;
408
409 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200410#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 gui.bold_font = NOFONT;
412 gui.ital_font = NOFONT;
413 gui.boldital_font = NOFONT;
414# ifdef FEAT_XFONTSET
415 gui.fontset = NOFONTSET;
416# endif
417#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200418 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100419#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200420 gui.wide_bold_font = NOFONT;
421 gui.wide_ital_font = NOFONT;
422 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424
425#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200426# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427# ifdef FONTSET_ALWAYS
428 gui.menu_fontset = NOFONTSET;
429# else
430 gui.menu_font = NOFONT;
431# endif
432# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100433 gui.menu_is_active = TRUE; // default: include menu
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434# ifndef FEAT_GUI_GTK
435 gui.menu_height = MENU_DEFAULT_HEIGHT;
436 gui.menu_width = 0;
437# endif
438#endif
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100439#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 gui.toolbar_height = 0;
441#endif
442#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
443 gui.footer_height = 0;
444#endif
445#ifdef FEAT_BEVAL_TIP
446 gui.tooltip_fontset = NOFONTSET;
447#endif
448
449 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
450 gui.prev_wrap = -1;
451
K.Takata6e1d31e2022-02-03 13:05:32 +0000452#ifdef FEAT_GUI_GTK
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100453 CLEAR_FIELD(gui.ligatures_map);
454#endif
455
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200456#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 result = OK;
458#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200459# ifdef FEAT_GUI_GTK
460 /*
461 * Note: Don't call gtk_init_check() before fork, it will be called after
462 * the fork. When calling it before fork, it make vim hang for a while.
463 * See gui_do_fork().
464 * Use a simpler check if the GUI window can probably be opened.
465 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200466 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200467# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470#endif
471 return result;
472}
473
474/*
475 * This is the call which starts the GUI.
476 */
477 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100478gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
480 win_T *wp;
481 static int recursive = 0;
482
483 /*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000484 * It's possible to use ":gui" in a .gvimrc file. The first half of this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 * function will then be executed at the first call, the rest by the
486 * recursive call. This allow the shell to be opened halfway reading a
487 * gvimrc file.
488 */
489 if (!recursive)
490 {
491 ++recursive;
492
493 clip_init(TRUE);
494
Bram Moolenaar30613902019-12-01 22:11:18 +0100495 // If can't initialize, don't try doing the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 if (gui_init_check() == FAIL)
497 {
498 --recursive;
499 clip_init(FALSE);
500 return;
501 }
502
503 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000504 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
505 * breaks the Paste toolbar button.
506 */
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100507 set_option_value_give_err((char_u *)"paste", 0L, NULL, 0);
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000508
Bram Moolenaaracc770a2020-04-12 15:11:06 +0200509 // Set t_Co to the number of colors: RGB.
510 set_color_count(256 * 256 * 256);
511
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000512 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 * Set up system-wide default menus.
514 */
515#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
516 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
517 {
518 sys_menu = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 sys_menu = FALSE;
521 }
522#endif
523
524 /*
525 * Switch on the mouse by default, unless the user changed it already.
526 * This can then be changed in the .gvimrc.
527 */
528 if (!option_was_set((char_u *)"mouse"))
529 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000530 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532 /*
533 * If -U option given, use only the initializations from that file and
534 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
535 */
536 if (use_gvimrc != NULL)
537 {
538 if (STRCMP(use_gvimrc, "NONE") != 0
539 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100540 && do_source(use_gvimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000541 semsg(_(e_cannot_read_from_str), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
543 else
544 {
545 /*
546 * Get system wide defaults for gvim, only when file name defined.
547 */
548#ifdef SYS_GVIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550#endif
551
552 /*
553 * Try to read GUI initialization commands from the following
554 * places:
555 * - environment variable GVIMINIT
556 * - the user gvimrc file (~/.gvimrc)
557 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
558 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
559 * The first that exists is used, the rest is ignored.
560 */
561 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000562 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000565 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200568#ifdef USR_GVIMRC_FILE3
569 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 )
573 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200574#ifdef USR_GVIMRC_FILE4
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE,
576 DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577#endif
578 }
579
580 /*
581 * Read initialization commands from ".gvimrc" in current
582 * directory. This is only done if the 'exrc' option is set.
583 * Because of security reasons we disallow shell and write
584 * commands now, except for unix if the file is owned by the user
585 * or 'secure' option has been reset in environment of global
586 * ".gvimrc".
587 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
588 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
589 */
590 if (p_exrc)
591 {
592#ifdef UNIX
593 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200594 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
Bram Moolenaar30613902019-12-01 22:11:18 +0100596 // if ".gvimrc" file is not owned by user, set 'secure'
597 // mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
599 secure = p_secure;
600 }
601#else
602 secure = p_secure;
603#endif
604
605 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200606 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607#ifdef SYS_GVIMRC_FILE
608 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200609 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#endif
611#ifdef USR_GVIMRC_FILE2
612 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200613 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#endif
615#ifdef USR_GVIMRC_FILE3
616 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200617 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200619#ifdef USR_GVIMRC_FILE4
620 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200621 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
626 if (secure == 2)
627 need_wait_return = TRUE;
628 secure = 0;
629 }
630 }
631
632 if (need_wait_return || msg_didany)
633 wait_return(TRUE);
634
635 --recursive;
636 }
637
Bram Moolenaar30613902019-12-01 22:11:18 +0100638 // If recursive call opened the shell, return here from the first call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 if (gui.in_use)
640 return;
641
642 /*
643 * Create the GUI shell.
644 */
Bram Moolenaar30613902019-12-01 22:11:18 +0100645 gui.in_use = TRUE; // Must be set after menus have been set up
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 if (gui_mch_init() == FAIL)
647 goto error;
648
Bram Moolenaar30613902019-12-01 22:11:18 +0100649 // Avoid a delay for an error message that was printed in the terminal
650 // where Vim was started.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 emsg_on_display = FALSE;
652 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100653 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 need_wait_return = FALSE;
655 msg_didany = FALSE;
656
657 /*
658 * Check validity of any generic resources that may have been loaded.
659 */
660 if (gui.border_width < 0)
661 gui.border_width = 0;
662
663 /*
664 * Set up the fonts. First use a font specified with "-fn" or "-font".
665 */
666 if (font_argument != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100667 set_option_value_give_err((char_u *)"gfn",
668 0L, (char_u *)font_argument, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 if (
670#ifdef FEAT_XFONTSET
671 (*p_guifontset == NUL
672 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
673#endif
674 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
675 : p_guifont, FALSE) == FAIL)
676 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000677 emsg(_(e_cannot_start_gui_no_valid_font_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 goto error2;
679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (gui_get_wide_font() == FAIL)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000681 emsg(_(e_guifontwide_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
683 gui.num_cols = Columns;
684 gui.num_rows = Rows;
685 gui_reset_scroll_region();
686
Bram Moolenaar30613902019-12-01 22:11:18 +0100687 // Create initial scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 FOR_ALL_WINDOWS(wp)
689 {
690 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
691 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
692 }
693 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
694
695#ifdef FEAT_MENU
696 gui_create_initial_menus(root_menu);
697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698#ifdef FEAT_SIGN_ICONS
699 sign_gui_started();
700#endif
701
Bram Moolenaar30613902019-12-01 22:11:18 +0100702 // Configure the desired menu and scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 gui_init_which_components(NULL);
704
Bram Moolenaar30613902019-12-01 22:11:18 +0100705 // All components of the GUI have been created now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 gui.shell_created = TRUE;
707
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100708#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100709 // Set the shell size, adjusted for the screen size. For GTK this only
710 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100711 // If the window is already maximized (e.g. when --windowid is passed in),
712 // we want to use the system-provided dimensions by passing FALSE to
713 // mustset. Otherwise, we want to initialize with the default rows/columns.
714 if (gui_mch_maximized())
715 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
716 else
717 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100718#else
719# ifndef FEAT_GUI_GTK
720 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
721# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#endif
723#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +0100724 // Need to set the size of the menubar after all the menus have been
725 // created.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 gui_mch_compute_menu_height((Widget)0);
727#endif
728
729 /*
730 * Actually open the GUI shell.
731 */
732 if (gui_mch_open() != FAIL)
733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 maketitle();
735 resettitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 init_gui_options();
738#ifdef FEAT_ARABIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100739 // Our GUI can't do bidi.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 p_tbidi = FALSE;
741#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000742#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100743 // Give GTK+ a chance to put all widget's into place.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000745
746# ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +0100747 // If there is no 'm' in 'guioptions' we need to remove the menu now.
748 // It was still there to make F10 work.
Bram Moolenaar18144c82006-04-12 21:52:12 +0000749 if (vim_strchr(p_go, GO_MENUS) == NULL)
750 {
751 --gui.starting;
752 gui_mch_enable_menu(FALSE);
753 ++gui.starting;
754 gui_mch_update();
755 }
756# endif
757
Bram Moolenaar30613902019-12-01 22:11:18 +0100758 // Now make sure the shell fits on the screen.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100759 if (gui_mch_maximized())
760 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
761 else
762 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100764 // When 'lines' was set while starting up the topframe may have to be
765 // resized.
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000766 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000767
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100768#ifdef FEAT_BEVAL_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100769 // Always create the Balloon Evaluation area, but disable it when
770 // 'ballooneval' is off.
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100771 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200772 {
773# ifdef FEAT_VARTABS
774 vim_free(balloonEval->vts);
775# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100776 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200777 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100778 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000779# ifdef FEAT_GUI_GTK
780 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
781 &general_beval_cb, NULL);
782# else
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100783# if defined(FEAT_GUI_MOTIF)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000784 {
785 extern Widget textArea;
786 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000787 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000788 }
789# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100790# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000791 balloonEval = gui_mch_create_beval_area(NULL, NULL,
792 &general_beval_cb, NULL);
793# endif
794# endif
795# endif
796 if (!p_beval)
797 gui_mch_disable_beval_area(balloonEval);
798#endif
Bram Moolenaarad772a62020-06-01 14:07:49 +0200799
800#ifndef FEAT_GUI_MSWIN
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200801 // In the GUI modifiers are prepended to keys.
Bram Moolenaarad772a62020-06-01 14:07:49 +0200802 // Don't do this for MS-Windows yet, it sends CTRL-K without the
803 // modifier.
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200804 seenModifyOtherKeys = TRUE;
Bram Moolenaarad772a62020-06-01 14:07:49 +0200805#endif
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
808 if (!im_xim_isvalid_imactivate())
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000809 emsg(_(e_value_of_imactivatekey_is_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100811 // When 'cmdheight' was set during startup it may not have taken
812 // effect yet.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000813 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000814 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
816 return;
817 }
818
819error2:
820#ifdef FEAT_GUI_X11
Bram Moolenaar30613902019-12-01 22:11:18 +0100821 // undo gui_mch_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 gui_mch_uninit();
823#endif
824
825error:
826 gui.in_use = FALSE;
827 clip_init(FALSE);
828}
829
830
831 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100832gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833{
Bram Moolenaar30613902019-12-01 22:11:18 +0100834 // don't free the fonts, it leads to a BUS error
835 // richard@whitequeen.com Jul 99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 gui.in_use = FALSE;
838 gui_mch_exit(rc);
839}
840
Bram Moolenaar9372a112005-12-06 19:59:18 +0000841#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +0200842 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000843# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844/*
845 * Called when the GUI shell is closed by the user. If there are no changed
846 * files Vim exits, otherwise there will be a dialog to ask the user what to
847 * do.
848 * When this function returns, Vim should NOT exit!
849 */
850 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100851gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
Bram Moolenaar3552e742021-05-29 12:21:58 +0200853 cmdmod_T save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
Bram Moolenaar3552e742021-05-29 12:21:58 +0200855 if (before_quit_autocmds(curwin, TRUE, FALSE))
856 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
Bram Moolenaar30613902019-12-01 22:11:18 +0100858 // Only exit when there are no changed files
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 exiting = TRUE;
860# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200861 cmdmod.cmod_flags |= CMOD_BROWSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862# endif
863# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +0200864 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100866 // If there are changed buffers, present the user with a dialog if
867 // possible, otherwise give an error message.
Bram Moolenaar027387f2016-01-02 22:25:52 +0100868 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 getout(0);
870
871 exiting = FALSE;
872 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +0100873 gui_update_screen(); // redraw, window may show changed buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874}
875#endif
876
877/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200878 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 * first font name that works is used. If none is found, use the default
880 * font.
881 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
882 * Return OK when able to set the font. When it failed FAIL is returned and
883 * the fonts are unchanged.
884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100886gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
888#define FONTLEN 320
889 char_u font_name[FONTLEN];
890 int font_list_empty = FALSE;
891 int ret = FAIL;
892
893 if (!gui.in_use)
894 return FAIL;
895
896 font_name[0] = NUL;
897 if (*font_list == NUL)
898 font_list_empty = TRUE;
899 else
900 {
901#ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +0100902 // When using a fontset, the whole list of fonts is one name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (fontset)
904 ret = gui_mch_init_font(font_list, TRUE);
905 else
906#endif
907 while (*font_list != NUL)
908 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100909 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
911
Bram Moolenaar30613902019-12-01 22:11:18 +0100912 // Careful!!! The Win32 version of gui_mch_init_font(), when
913 // called with "*" will change p_guifont to the selected font
914 // name, which frees the old value. This makes font_list
915 // invalid. Thus when OK is returned here, font_list must no
916 // longer be used!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (gui_mch_init_font(font_name, FALSE) == OK)
918 {
K.Takata94373c42022-01-27 15:04:22 +0000919#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar30613902019-12-01 22:11:18 +0100920 // If it's a Unicode font, try setting 'guifontwide' to a
921 // similar double-width font.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
923 && strstr((char *)font_name, "10646") != NULL)
924 set_guifontwide(font_name);
925#endif
926 ret = OK;
927 break;
928 }
929 }
930 }
931
932 if (ret != OK
933 && STRCMP(font_list, "*") != 0
934 && (font_list_empty || gui.norm_font == NOFONT))
935 {
936 /*
937 * Couldn't load any font in 'font_list', keep the current font if
938 * there is one. If 'font_list' is empty, or if there is no current
939 * font, tell gui_mch_init_font() to try to find a font we can load.
940 */
941 ret = gui_mch_init_font(NULL, FALSE);
942 }
943
944 if (ret == OK)
945 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200946#ifndef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100947 // Set normal font as current font
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948# ifdef FEAT_XFONTSET
949 if (gui.fontset != NOFONTSET)
950 gui_mch_set_fontset(gui.fontset);
951 else
952# endif
953 gui_mch_set_font(gui.norm_font);
954#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100955 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957
958 return ret;
959}
960
K.Takata94373c42022-01-27 15:04:22 +0000961#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962/*
963 * Try setting 'guifontwide' to a font twice as wide as "name".
964 */
965 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100966set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967{
968 int i = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +0100969 char_u wide_name[FONTLEN + 10]; // room for 2 * width and '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 char_u *wp = NULL;
971 char_u *p;
972 GuiFont font;
973
974 wp = wide_name;
975 for (p = name; *p != NUL; ++p)
976 {
977 *wp++ = *p;
978 if (*p == '-')
979 {
980 ++i;
Bram Moolenaar30613902019-12-01 22:11:18 +0100981 if (i == 6) // font type: change "--" to "-*-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 {
983 if (p[1] == '-')
984 *wp++ = '*';
985 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100986 else if (i == 12) // found the width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 {
988 ++p;
989 i = getdigits(&p);
990 if (i != 0)
991 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100992 // Double the width specification.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 sprintf((char *)wp, "%d%s", i * 2, p);
994 font = gui_mch_get_font(wide_name, FALSE);
995 if (font != NOFONT)
996 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000997 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 gui.wide_font = font;
999 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001000 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002 }
1003 break;
1004 }
1005 }
1006 }
1007}
K.Takata94373c42022-01-27 15:04:22 +00001008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
1010/*
1011 * Get the font for 'guifontwide'.
1012 * Return FAIL for an invalid font name.
1013 */
1014 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001015gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
1017 GuiFont font = NOFONT;
1018 char_u font_name[FONTLEN];
1019 char_u *p;
1020
Bram Moolenaar30613902019-12-01 22:11:18 +01001021 if (!gui.in_use) // Can't allocate font yet, assume it's OK.
1022 return OK; // Will give an error message later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1025 {
1026 for (p = p_guifontwide; *p != NUL; )
1027 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001028 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1030 font = gui_mch_get_font(font_name, FALSE);
1031 if (font != NOFONT)
1032 break;
1033 }
1034 if (font == NOFONT)
1035 return FAIL;
1036 }
1037
1038 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001039#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001040 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (font != NOFONT && gui.norm_font != NOFONT
1042 && pango_font_description_equal(font, gui.norm_font))
1043 {
1044 gui.wide_font = NOFONT;
1045 gui_mch_free_font(font);
1046 }
1047 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001050#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001051 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001052#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001053 /*
1054 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1055 * support those fonts for 'guifontwide'.
1056 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 return OK;
1059}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001061#if defined(FEAT_GUI_GTK) || defined(PROTO)
1062/*
1063 * Set list of ascii characters that combined can create ligature.
1064 * Store them in char map for quick access from gui_gtk2_draw_string.
1065 */
1066 void
1067gui_set_ligatures(void)
1068{
1069 char_u *p;
1070
1071 if (*p_guiligatures != NUL)
1072 {
1073 // check for invalid characters
1074 for (p = p_guiligatures; *p != NUL; ++p)
1075 if (*p < 32 || *p > 127)
1076 {
1077 emsg(_(e_ascii_code_not_in_range));
1078 return;
1079 }
1080
1081 // store valid setting into ligatures_map
1082 CLEAR_FIELD(gui.ligatures_map);
1083 for (p = p_guiligatures; *p != NUL; ++p)
1084 gui.ligatures_map[*p] = 1;
1085 }
1086 else
1087 CLEAR_FIELD(gui.ligatures_map);
1088}
Dusan Popovicce59b9f2021-11-22 17:18:44 +00001089
1090/*
1091 * Adjust the columns to undraw for when the cursor is on ligatures.
1092 */
1093 static void
1094gui_adjust_undraw_cursor_for_ligatures(int *startcol, int *endcol)
1095{
1096 int off;
1097
1098 if (ScreenLines == NULL || *p_guiligatures == NUL)
1099 return;
1100
1101 // expand before the cursor for all the chars in gui.ligatures_map
1102 off = LineOffset[gui.cursor_row] + *startcol;
1103 if (gui.ligatures_map[ScreenLines[off]])
1104 while (*startcol > 0 && gui.ligatures_map[ScreenLines[--off]])
1105 (*startcol)--;
1106
1107 // expand after the cursor for all the chars in gui.ligatures_map
1108 off = LineOffset[gui.cursor_row] + *endcol;
1109 if (gui.ligatures_map[ScreenLines[off]])
1110 while (*endcol < ((int)screen_Columns - 1)
1111 && gui.ligatures_map[ScreenLines[++off]])
1112 (*endcol)++;
1113}
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001114#endif
1115
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001116 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001117gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 gui.row = row;
1120 gui.col = col;
1121}
1122
1123/*
1124 * gui_check_pos - check if the cursor is on the screen.
1125 */
1126 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001127gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128{
1129 if (gui.row >= screen_Rows)
1130 gui.row = screen_Rows - 1;
1131 if (gui.col >= screen_Columns)
1132 gui.col = screen_Columns - 1;
1133 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1134 gui.cursor_is_valid = FALSE;
1135}
1136
1137/*
1138 * Redraw the cursor if necessary or when forced.
1139 * Careful: The contents of ScreenLines[] must match what is on the screen,
1140 * otherwise this goes wrong. May need to call out_flush() first.
1141 */
1142 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001143gui_update_cursor(
Bram Moolenaar30613902019-12-01 22:11:18 +01001144 int force, // when TRUE, update even when not moved
1145 int clear_selection) // clear selection under cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146{
1147 int cur_width = 0;
1148 int cur_height = 0;
1149 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001150 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001152#ifdef FEAT_TERMINAL
1153 guicolor_T shape_fg = INVALCOLOR;
1154 guicolor_T shape_bg = INVALCOLOR;
1155#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001156 guicolor_T cfg, cbg, cc; // cursor fore-/background color
1157 int cattr; // cursor attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 int attr;
1159 attrentry_T *aep = NULL;
1160
Bram Moolenaar30613902019-12-01 22:11:18 +01001161 // Don't update the cursor when halfway busy scrolling or the screen size
1162 // doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then.
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001163 if (!can_update_cursor || screen_Columns != gui.num_cols
1164 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 return;
1166
1167 gui_check_pos();
1168 if (!gui.cursor_is_valid || force
1169 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1170 {
1171 gui_undraw_cursor();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001172
1173 // If a cursor-less sleep is ongoing, leave the cursor invisible
1174 if (cursor_is_sleeping())
1175 return;
1176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 if (gui.row < 0)
1178 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001179#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1181 im_set_position(gui.row, gui.col);
1182#endif
1183 gui.cursor_row = gui.row;
1184 gui.cursor_col = gui.col;
1185
Bram Moolenaar30613902019-12-01 22:11:18 +01001186 // Only write to the screen after ScreenLines[] has been initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 if (!screen_cleared || ScreenLines == NULL)
1188 return;
1189
Bram Moolenaar30613902019-12-01 22:11:18 +01001190 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (clear_selection)
1192 clip_may_clear_selection(gui.row, gui.row);
Bram Moolenaar30613902019-12-01 22:11:18 +01001193 // Check that the cursor is inside the shell (resizing may have made
1194 // it invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1196 return;
1197
1198 gui.cursor_is_valid = TRUE;
1199
1200 /*
1201 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001202 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001204#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001205 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001206 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001208#endif
1209 shape = &shape_table[get_shape_idx(FALSE)];
Bram Moolenaar24959102022-05-07 20:01:16 +01001210 if (State & MODE_LANGMAP)
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001211 id = shape->id_lm;
1212 else
1213 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
Bram Moolenaar30613902019-12-01 22:11:18 +01001215 // get the colors and attributes for the cursor. Default is inverted
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 cfg = INVALCOLOR;
1217 cbg = INVALCOLOR;
1218 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001219 gui_mch_set_blinking(shape->blinkwait,
1220 shape->blinkon,
1221 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001222 if (shape->blinkwait == 0 || shape->blinkon == 0
1223 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001224 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001225#ifdef FEAT_TERMINAL
1226 if (shape_bg != INVALCOLOR)
1227 {
1228 cattr = 0;
1229 cfg = shape_fg;
1230 cbg = shape_bg;
1231 }
1232 else
1233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (id > 0)
1235 {
1236 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar54612582019-11-21 17:13:31 +01001237#if defined(HAVE_INPUT_METHOD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {
1239 static int iid;
1240 guicolor_T fg, bg;
1241
Bram Moolenaarc236c162008-07-13 17:41:49 +00001242 if (
Bram Moolenaar54612582019-11-21 17:13:31 +01001243# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001244 preedit_get_status()
1245# else
1246 im_get_status()
1247# endif
1248 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {
1250 iid = syn_name2id((char_u *)"CursorIM");
1251 if (iid > 0)
1252 {
1253 syn_id2colors(iid, &fg, &bg);
1254 if (bg != INVALCOLOR)
1255 cbg = bg;
1256 if (fg != INVALCOLOR)
1257 cfg = fg;
1258 }
1259 }
1260 }
1261#endif
1262 }
1263
1264 /*
1265 * Get the attributes for the character under the cursor.
1266 * When no cursor color was given, use the character color.
1267 */
1268 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1269 if (attr > HL_ALL)
1270 aep = syn_gui_attr2entry(attr);
1271 if (aep != NULL)
1272 {
1273 attr = aep->ae_attr;
1274 if (cfg == INVALCOLOR)
1275 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1276 : aep->ae_u.gui.fg_color);
1277 if (cbg == INVALCOLOR)
1278 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1279 : aep->ae_u.gui.bg_color);
1280 }
1281 if (cfg == INVALCOLOR)
1282 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1283 if (cbg == INVALCOLOR)
1284 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1285
1286#ifdef FEAT_XIM
1287 if (aep != NULL)
1288 {
1289 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1290 : aep->ae_u.gui.bg_color);
1291 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1292 : aep->ae_u.gui.fg_color);
1293 if (xim_bg_color == INVALCOLOR)
1294 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1295 : gui.back_pixel;
1296 if (xim_fg_color == INVALCOLOR)
1297 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1298 : gui.norm_pixel;
1299 }
1300 else
1301 {
1302 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1303 : gui.back_pixel;
1304 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1305 : gui.norm_pixel;
1306 }
1307#endif
1308
1309 attr &= ~HL_INVERSE;
1310 if (cattr & HL_INVERSE)
1311 {
1312 cc = cbg;
1313 cbg = cfg;
1314 cfg = cc;
1315 }
1316 cattr &= ~HL_INVERSE;
1317
1318 /*
1319 * When we don't have window focus, draw a hollow cursor.
1320 */
1321 if (!gui.in_focus)
1322 {
1323 gui_mch_draw_hollow_cursor(cbg);
1324 return;
1325 }
1326
1327 old_hl_mask = gui.highlight_mask;
Bram Moolenaar54612582019-11-21 17:13:31 +01001328 if (shape->shape == SHAPE_BLOCK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
1330 /*
1331 * Draw the text character with the cursor colors. Use the
1332 * character attributes plus the cursor attributes.
1333 */
1334 gui.highlight_mask = (cattr | attr);
Bram Moolenaar54612582019-11-21 17:13:31 +01001335 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1337 }
1338 else
1339 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001340#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 int col_off = FALSE;
1342#endif
1343 /*
1344 * First draw the partial cursor, then overwrite with the text
1345 * character, using a transparent background.
1346 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001347 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 {
1349 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001350 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 }
1352 else
1353 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001354 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 cur_width = gui.char_width;
1356 }
Bram Moolenaar367329b2007-08-30 11:53:22 +00001357 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1358 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001360 // Double wide character.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001361 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001363#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 if (CURSOR_BAR_RIGHT)
1365 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001366 // gui.col points to the left half of the character but
1367 // the vertical line needs to be on the right half.
Bram Moolenaar30613902019-12-01 22:11:18 +01001368 // A double-wide horizontal line is also drawn from the
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001369 // right half in gui_mch_draw_part_cursor().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 col_off = TRUE;
1371 ++gui.col;
1372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001376#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (col_off)
1378 --gui.col;
1379#endif
1380
Bram Moolenaar30613902019-12-01 22:11:18 +01001381#ifndef FEAT_GUI_MSWIN // doesn't seem to work for MSWindows
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1383 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1384 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1385 (guicolor_T)0, (guicolor_T)0, 0);
1386#endif
1387 }
1388 gui.highlight_mask = old_hl_mask;
1389 }
1390}
1391
1392#if defined(FEAT_MENU) || defined(PROTO)
1393 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001394gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001396# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 if (gui.menu_is_active && gui.in_use)
1398 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1399# endif
1400}
1401#endif
1402
1403/*
1404 * Position the various GUI components (text area, menu). The vertical
1405 * scrollbars are NOT handled here. See gui_update_scrollbars().
1406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001408gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
1410 int text_area_x;
1411 int text_area_y;
1412 int text_area_width;
1413 int text_area_height;
1414
Bram Moolenaar30613902019-12-01 22:11:18 +01001415 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 ++hold_gui_events;
1417
1418 text_area_x = 0;
1419 if (gui.which_scrollbars[SBAR_LEFT])
1420 text_area_x += gui.scrollbar_width;
1421
1422 text_area_y = 0;
1423#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1424 gui.menu_width = total_width;
1425 if (gui.menu_is_active)
1426 text_area_y += gui.menu_height;
1427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
K.Takata6e1d31e2022-02-03 13:05:32 +00001429#if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001430 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001431 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001432 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001433#endif
1434
Bram Moolenaar0b962e52022-04-03 18:02:37 +01001435#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) \
K.Takatac81e9bf2022-01-16 14:15:49 +00001436 || defined(FEAT_GUI_HAIKU) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1438 {
Bram Moolenaar0b962e52022-04-03 18:02:37 +01001439# if defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 gui_mch_set_toolbar_pos(0, text_area_y,
1441 gui.menu_width, gui.toolbar_height);
1442# endif
1443 text_area_y += gui.toolbar_height;
1444 }
1445#endif
1446
K.Takata6e1d31e2022-02-03 13:05:32 +00001447#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_HAIKU)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001448 gui_mch_set_tabline_pos(0, text_area_y,
1449 gui.menu_width, gui.tabline_height);
1450 if (gui_has_tabline())
1451 text_area_y += gui.tabline_height;
1452#endif
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1455 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1456
1457 gui_mch_set_text_area_pos(text_area_x,
1458 text_area_y,
1459 text_area_width,
1460 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001461#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 + xim_get_status_area_height()
1463#endif
1464 );
1465#ifdef FEAT_MENU
1466 gui_position_menu();
1467#endif
1468 if (gui.which_scrollbars[SBAR_BOTTOM])
1469 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1470 text_area_x,
Bram Moolenaar203ec772020-07-17 20:43:43 +02001471 text_area_y + text_area_height
1472 + gui_mch_get_scrollbar_ypadding(),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 text_area_width,
1474 gui.scrollbar_height);
1475 gui.left_sbar_x = 0;
Bram Moolenaar203ec772020-07-17 20:43:43 +02001476 gui.right_sbar_x = text_area_x + text_area_width
1477 + gui_mch_get_scrollbar_xpadding();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
1479 --hold_gui_events;
1480}
1481
Bram Moolenaar02743632005-07-25 20:42:36 +00001482/*
1483 * Get the width of the widgets and decorations to the side of the text area.
1484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001486gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488 int base_width;
1489
1490 base_width = 2 * gui.border_offset;
1491 if (gui.which_scrollbars[SBAR_LEFT])
1492 base_width += gui.scrollbar_width;
1493 if (gui.which_scrollbars[SBAR_RIGHT])
1494 base_width += gui.scrollbar_width;
1495 return base_width;
1496}
1497
Bram Moolenaar02743632005-07-25 20:42:36 +00001498/*
1499 * Get the height of the widgets and decorations above and below the text area.
1500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001502gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 int base_height;
1505
1506 base_height = 2 * gui.border_offset;
1507 if (gui.which_scrollbars[SBAR_BOTTOM])
1508 base_height += gui.scrollbar_height;
1509#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001510 // We can't take the sizes properly into account until anything is
1511 // realized. Therefore we recalculate all the values here just before
1512 // setting the size. (--mdcki)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513#else
1514# ifdef FEAT_MENU
1515 if (gui.menu_is_active)
1516 base_height += gui.menu_height;
1517# endif
1518# ifdef FEAT_TOOLBAR
1519 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 base_height += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001522# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001523 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001524 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001525 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527# ifdef FEAT_FOOTER
1528 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1529 base_height += gui.footer_height;
1530# endif
1531# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1532 base_height += gui_mch_text_area_extra_height();
1533# endif
1534#endif
1535 return base_height;
1536}
1537
1538/*
1539 * Should be called after the GUI shell has been resized. Its arguments are
1540 * the new width and height of the shell in pixels.
1541 */
1542 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001543gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
1545 static int busy = FALSE;
1546
Bram Moolenaar30613902019-12-01 22:11:18 +01001547 if (!gui.shell_created) // ignore when still initializing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 return;
1549
1550 /*
1551 * Can't resize the screen while it is being redrawn. Remember the new
1552 * size and handle it later.
1553 */
1554 if (updating_screen || busy)
1555 {
1556 new_pixel_width = pixel_width;
1557 new_pixel_height = pixel_height;
1558 return;
1559 }
1560
1561again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001562 new_pixel_width = 0;
1563 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 busy = TRUE;
1565
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001566#ifdef FEAT_GUI_HAIKU
1567 vim_lock_screen();
1568#endif
1569
Bram Moolenaar30613902019-12-01 22:11:18 +01001570 // Flush pending output before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 out_flush();
1572
1573 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001574 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 /*
1580 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1581 * at the last line here (why does it have to be one row too low?).
1582 */
Bram Moolenaar24959102022-05-07 20:01:16 +01001583 if (State == MODE_ASKMORE || State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 gui.row = gui.num_rows;
1585
Bram Moolenaar30613902019-12-01 22:11:18 +01001586 // Only comparing Rows and Columns may be sufficient, but let's stay on
1587 // the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1589 || gui.num_rows != Rows || gui.num_cols != Columns)
1590 shell_resized();
1591
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001592#ifdef FEAT_GUI_HAIKU
1593 vim_unlock_screen();
1594#endif
1595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 gui_update_scrollbars(TRUE);
1597 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001598#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 xim_set_status_area();
1600#endif
1601
1602 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001603
Bram Moolenaar30613902019-12-01 22:11:18 +01001604 // We may have been called again while redrawing the screen.
1605 // Need to do it all again with the latest size then. But only if the size
1606 // actually changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (new_pixel_height)
1608 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001609 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1610 {
1611 new_pixel_width = 0;
1612 new_pixel_height = 0;
1613 }
1614 else
1615 {
1616 pixel_width = new_pixel_width;
1617 pixel_height = new_pixel_height;
1618 goto again;
1619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621}
1622
1623/*
1624 * Check if gui_resize_shell() must be called.
1625 */
1626 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001627gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (new_pixel_height)
Bram Moolenaar30613902019-12-01 22:11:18 +01001630 // careful: gui_resize_shell() may postpone the resize again if we
1631 // were called indirectly by it
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001632 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633}
1634
1635 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001636gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 Rows = gui.num_rows;
1639 Columns = gui.num_cols;
1640 return OK;
1641}
1642
1643/*
1644 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001645 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1646 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001647 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1648 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001651gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001652 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001653 int fit_to_display,
Bram Moolenaar30613902019-12-01 22:11:18 +01001654 int direction) // RESIZE_HOR, RESIZE_VER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655{
1656 int base_width;
1657 int base_height;
1658 int width;
1659 int height;
1660 int min_width;
1661 int min_height;
1662 int screen_w;
1663 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001664#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001665 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001666 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001667#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001668 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
1670 if (!gui.shell_created)
1671 return;
1672
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001673#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01001674 // If not setting to a user specified size and maximized, calculate the
1675 // number of characters that fit in the maximized window.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001676 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1677 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
1679 gui_mch_newfont();
1680 return;
1681 }
1682#endif
1683
1684 base_width = gui_get_base_width();
1685 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001686 if (fit_to_display)
Bram Moolenaar30613902019-12-01 22:11:18 +01001687 // Remember the original window position.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001688 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001689
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001690 width = Columns * gui.char_width + base_width;
1691 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693 if (fit_to_display)
1694 {
1695 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001696 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 {
1698 Columns = (screen_w - base_width) / gui.char_width;
1699 if (Columns < MIN_COLUMNS)
1700 Columns = MIN_COLUMNS;
1701 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001702#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001703 ++did_adjust;
1704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001706 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 {
1708 Rows = (screen_h - base_height) / gui.char_height;
1709 check_shellsize();
1710 height = Rows * gui.char_height + base_height;
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 Moolenaar0eda7ac2010-06-26 05:38:18 +02001715#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001716 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1717 && height + gui.char_height >= screen_h))
Bram Moolenaar30613902019-12-01 22:11:18 +01001718 // don't unmaximize if at maximum size
Bram Moolenaar09736232009-09-23 16:14:49 +00001719 un_maximize = FALSE;
1720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001722 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 gui.num_cols = Columns;
1724 gui.num_rows = Rows;
1725
1726 min_width = base_width + MIN_COLUMNS * gui.char_width;
1727 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001728 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001729
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001730#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001731 if (un_maximize)
1732 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001733 // If the window size is smaller than the screen unmaximize the
1734 // window, otherwise resizing won't work.
Bram Moolenaar09736232009-09-23 16:14:49 +00001735 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1736 if ((width + gui.char_width < screen_w
1737 || height + gui.char_height * 2 < screen_h)
1738 && gui_mch_maximized())
1739 gui_mch_unmaximize();
1740 }
1741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
1743 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001744 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001746 if (fit_to_display && x >= 0 && y >= 0)
1747 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001748 // Some window managers put the Vim window left of/above the screen.
1749 // Only change the position if it wasn't already negative before
1750 // (happens on MS-Windows with a secondary monitor).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 gui_mch_update();
1752 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1753 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1754 }
1755
1756 gui_position_components(width);
1757 gui_update_scrollbars(TRUE);
1758 gui_reset_scroll_region();
1759}
1760
1761/*
1762 * Called when Rows and/or Columns has changed.
1763 */
1764 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001765gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 gui_reset_scroll_region();
1768}
1769
1770/*
1771 * Make scroll region cover whole screen.
1772 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001773 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001774gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
1776 gui.scroll_region_top = 0;
1777 gui.scroll_region_bot = gui.num_rows - 1;
1778 gui.scroll_region_left = 0;
1779 gui.scroll_region_right = gui.num_cols - 1;
1780}
1781
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001782 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001783gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
Bram Moolenaar30613902019-12-01 22:11:18 +01001785 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 gui.highlight_mask = mask;
Bram Moolenaar30613902019-12-01 22:11:18 +01001787 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 gui.highlight_mask |= mask;
1789}
1790
1791 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001792gui_stop_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 = HL_NORMAL;
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/*
1801 * Clear a rectangular region of the screen from text pos (row1, col1) to
1802 * (row2, col2) inclusive.
1803 */
1804 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001805gui_clear_block(
1806 int row1,
1807 int col1,
1808 int row2,
1809 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
Bram Moolenaar30613902019-12-01 22:11:18 +01001811 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 clip_may_clear_selection(row1, row2);
1813
1814 gui_mch_clear_block(row1, col1, row2, col2);
1815
Bram Moolenaar30613902019-12-01 22:11:18 +01001816 // Invalidate cursor if it was in this block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1818 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1819 gui.cursor_is_valid = FALSE;
1820}
1821
1822/*
1823 * Write code to update the cursor later. This avoids the need to flush the
1824 * output buffer before calling gui_update_cursor().
1825 */
1826 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001827gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001829 OUT_STR("\033|s");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830}
1831
1832 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001833gui_write(
1834 char_u *s,
1835 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836{
1837 char_u *p;
1838 int arg1 = 0, arg2 = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01001839 int force_cursor = FALSE; // force cursor update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 int force_scrollbar = FALSE;
1841 static win_T *old_curwin = NULL;
1842
Bram Moolenaar30613902019-12-01 22:11:18 +01001843// #define DEBUG_GUI_WRITE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844#ifdef DEBUG_GUI_WRITE
1845 {
1846 int i;
1847 char_u *str;
1848
1849 printf("gui_write(%d):\n ", len);
1850 for (i = 0; i < len; i++)
1851 if (s[i] == ESC)
1852 {
1853 if (i != 0)
1854 printf("\n ");
1855 printf("<ESC>");
1856 }
1857 else
1858 {
1859 str = transchar_byte(s[i]);
1860 if (str[0] && str[1])
1861 printf("<%s>", (char *)str);
1862 else
1863 printf("%s", (char *)str);
1864 }
1865 printf("\n");
1866 }
1867#endif
1868 while (len)
1869 {
1870 if (s[0] == ESC && s[1] == '|')
1871 {
1872 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001873 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
1875 arg1 = getdigits(&p);
1876 if (p > s + len)
1877 break;
1878 if (*p == ';')
1879 {
1880 ++p;
1881 arg2 = getdigits(&p);
1882 if (p > s + len)
1883 break;
1884 }
1885 }
1886 switch (*p)
1887 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001888 case 'C': // Clear screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 clip_scroll_selection(9999);
1890 gui_mch_clear_all();
1891 gui.cursor_is_valid = FALSE;
1892 force_scrollbar = TRUE;
1893 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001894 case 'M': // Move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 gui_set_cursor(arg1, arg2);
1896 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001897 case 's': // force cursor (shape) update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 force_cursor = TRUE;
1899 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001900 case 'R': // Set scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (arg1 < arg2)
1902 {
1903 gui.scroll_region_top = arg1;
1904 gui.scroll_region_bot = arg2;
1905 }
1906 else
1907 {
1908 gui.scroll_region_top = arg2;
1909 gui.scroll_region_bot = arg1;
1910 }
1911 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001912 case 'V': // Set vertical scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (arg1 < arg2)
1914 {
1915 gui.scroll_region_left = arg1;
1916 gui.scroll_region_right = arg2;
1917 }
1918 else
1919 {
1920 gui.scroll_region_left = arg2;
1921 gui.scroll_region_right = arg1;
1922 }
1923 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001924 case 'd': // Delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 gui_delete_lines(gui.row, 1);
1926 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001927 case 'D': // Delete lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 gui_delete_lines(gui.row, arg1);
1929 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001930 case 'i': // Insert line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 gui_insert_lines(gui.row, 1);
1932 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001933 case 'I': // Insert lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 gui_insert_lines(gui.row, arg1);
1935 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001936 case '$': // Clear to end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 gui_clear_block(gui.row, gui.col, gui.row,
1938 (int)Columns - 1);
1939 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001940 case 'h': // Turn on highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 gui_start_highlight(arg1);
1942 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001943 case 'H': // Turn off highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 gui_stop_highlight(arg1);
1945 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001946 case 'f': // flash the window (visual bell)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1948 break;
1949 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01001950 p = s + 1; // Skip the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 break;
1952 }
1953 len -= (int)(++p - s);
1954 s = p;
1955 }
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001956 else if (s[0] < 0x20 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957#ifdef FEAT_SIGN_ICONS
1958 && s[0] != SIGN_BYTE
1959# ifdef FEAT_NETBEANS_INTG
1960 && s[0] != MULTISIGN_BYTE
1961# endif
1962#endif
1963 )
1964 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001965 if (s[0] == '\n') // NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
1967 gui.col = 0;
1968 if (gui.row < gui.scroll_region_bot)
1969 gui.row++;
1970 else
1971 gui_delete_lines(gui.scroll_region_top, 1);
1972 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001973 else if (s[0] == '\r') // CR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 gui.col = 0;
1976 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001977 else if (s[0] == '\b') // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
1979 if (gui.col)
1980 --gui.col;
1981 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001982 else if (s[0] == Ctrl_L) // cursor-right
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 ++gui.col;
1985 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001986 else if (s[0] == Ctrl_G) // Beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
1988 gui_mch_beep();
1989 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001990 // Other Ctrl character: shouldn't happen!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
Bram Moolenaar30613902019-12-01 22:11:18 +01001992 --len; // Skip this char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 ++s;
1994 }
1995 else
1996 {
1997 p = s;
1998 while (len > 0 && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 *p >= 0x20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000#ifdef FEAT_SIGN_ICONS
2001 || *p == SIGN_BYTE
2002# ifdef FEAT_NETBEANS_INTG
2003 || *p == MULTISIGN_BYTE
2004# endif
2005#endif
2006 ))
2007 {
2008 len--;
2009 p++;
2010 }
2011 gui_outstr(s, (int)(p - s));
2012 s = p;
2013 }
2014 }
2015
Bram Moolenaar30613902019-12-01 22:11:18 +01002016 // Postponed update of the cursor (won't work if "can_update_cursor" isn't
2017 // set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 if (force_cursor)
2019 gui_update_cursor(TRUE, TRUE);
2020
Bram Moolenaar30613902019-12-01 22:11:18 +01002021 // When switching to another window the dragging must have stopped.
2022 // Required for GTK, dragged_sb isn't reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if (old_curwin != curwin)
2024 gui.dragged_sb = SBAR_NONE;
2025
Bram Moolenaar30613902019-12-01 22:11:18 +01002026 // Update the scrollbars after clearing the screen or when switched
2027 // to another window.
2028 // Update the horizontal scrollbar always, it's difficult to check all
2029 // situations where it might change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (force_scrollbar || old_curwin != curwin)
2031 gui_update_scrollbars(force_scrollbar);
2032 else
2033 gui_update_horiz_scrollbar(FALSE);
2034 old_curwin = curwin;
2035
2036 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002037 * We need to make sure this is cleared since GTK doesn't tell us when
2038 * the user is done dragging.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 */
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002040#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 gui.dragged_sb = SBAR_NONE;
2042#endif
2043
Bram Moolenaar30613902019-12-01 22:11:18 +01002044 gui_may_flush(); // In case vim decides to take a nap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045}
2046
2047/*
2048 * When ScreenLines[] is invalid, updating the cursor should not be done, it
2049 * produces wrong results. Call gui_dont_update_cursor() before that code and
2050 * gui_can_update_cursor() afterwards.
2051 */
2052 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02002053gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
2055 if (gui.in_use)
2056 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002057 // Undraw the cursor now, we probably can't do it after the change.
Bram Moolenaar107abd22016-08-12 14:08:25 +02002058 if (undraw)
2059 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 can_update_cursor = FALSE;
2061 }
2062}
2063
2064 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002065gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066{
2067 can_update_cursor = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002068 // No need to update the cursor right now, there is always more output
2069 // after scrolling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070}
2071
Bram Moolenaara338adc2018-01-31 20:51:47 +01002072/*
2073 * Disable issuing gui_mch_flush().
2074 */
2075 void
2076gui_disable_flush(void)
2077{
2078 ++disable_flush;
2079}
2080
2081/*
2082 * Enable issuing gui_mch_flush().
2083 */
2084 void
2085gui_enable_flush(void)
2086{
2087 --disable_flush;
2088}
2089
2090/*
2091 * Issue gui_mch_flush() if it is not disabled.
2092 */
2093 void
2094gui_may_flush(void)
2095{
2096 if (disable_flush == 0)
2097 gui_mch_flush();
2098}
2099
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002101gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102{
2103 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
2106 if (len == 0)
2107 return;
2108
2109 if (len < 0)
2110 len = (int)STRLEN(s);
2111
2112 while (len > 0)
2113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 if (has_mbyte)
2115 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002116 // Find out how many chars fit in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 cells = 0;
2118 for (this_len = 0; this_len < len; )
2119 {
2120 cells += (*mb_ptr2cells)(s + this_len);
2121 if (gui.col + cells > Columns)
2122 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002123 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
2125 if (this_len > len)
Bram Moolenaar30613902019-12-01 22:11:18 +01002126 this_len = len; // don't include following composing char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 }
2128 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (gui.col + len > Columns)
2130 this_len = Columns - gui.col;
2131 else
2132 this_len = len;
2133
2134 (void)gui_outstr_nowrap(s, this_len,
2135 0, (guicolor_T)0, (guicolor_T)0, 0);
2136 s += this_len;
2137 len -= this_len;
Bram Moolenaar30613902019-12-01 22:11:18 +01002138 // fill up for a double-width char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (len > 0 && gui.col < Columns)
2140 (void)gui_outstr_nowrap((char_u *)" ", 1,
2141 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01002142 // The cursor may wrap to the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (gui.col >= Columns)
2144 {
2145 gui.col = 0;
2146 gui.row++;
2147 }
2148 }
2149}
2150
2151/*
2152 * Output one character (may be one or two display cells).
2153 * Caller must check for valid "off".
2154 * Returns FAIL or OK, just like gui_outstr_nowrap().
2155 */
2156 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002157gui_screenchar(
Bram Moolenaar30613902019-12-01 22:11:18 +01002158 int off, // Offset from start of screen
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002159 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002160 guicolor_T fg, // colors for cursor
2161 guicolor_T bg, // colors for cursor
2162 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 char_u buf[MB_MAXBYTES + 1];
2165
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002166 // Don't draw right half of a double-width UTF-8 char. "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 if (enc_utf8 && ScreenLines[off] == 0)
2168 return OK;
2169
2170 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002171 // Draw UTF-8 multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2173 flags, fg, bg, back);
2174
2175 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2176 {
2177 buf[0] = ScreenLines[off];
2178 buf[1] = ScreenLines2[off];
2179 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2180 }
2181
Bram Moolenaar30613902019-12-01 22:11:18 +01002182 // Draw non-multi-byte character or DBCS character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002184 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186}
2187
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002188#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189/*
2190 * Output the string at the given screen position. This is used in place
2191 * of gui_screenchar() where possible because Pango needs as much context
2192 * as possible to work nicely. It's a lot faster as well.
2193 */
2194 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002195gui_screenstr(
Bram Moolenaar30613902019-12-01 22:11:18 +01002196 int off, // Offset from start of screen
2197 int len, // string length in screen cells
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002198 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002199 guicolor_T fg, // colors for cursor
2200 guicolor_T bg, // colors for cursor
2201 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202{
2203 char_u *buf;
2204 int outlen = 0;
2205 int i;
2206 int retval;
2207
Bram Moolenaar30613902019-12-01 22:11:18 +01002208 if (len <= 0) // "cannot happen"?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 return OK;
2210
2211 if (enc_utf8)
2212 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002213 buf = alloc(len * MB_MAXBYTES + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002215 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
2217 for (i = off; i < off + len; ++i)
2218 {
2219 if (ScreenLines[i] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002220 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
2222 if (ScreenLinesUC[i] == 0)
2223 buf[outlen++] = ScreenLines[i];
2224 else
2225 outlen += utfc_char2bytes(i, buf + outlen);
2226 }
2227
Bram Moolenaar30613902019-12-01 22:11:18 +01002228 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2230 vim_free(buf);
2231
2232 return retval;
2233 }
2234 else if (enc_dbcs == DBCS_JPNU)
2235 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002236 buf = alloc(len * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002238 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240 for (i = off; i < off + len; ++i)
2241 {
2242 buf[outlen++] = ScreenLines[i];
2243
Bram Moolenaar30613902019-12-01 22:11:18 +01002244 // handle double-byte single-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 if (ScreenLines[i] == 0x8e)
2246 buf[outlen++] = ScreenLines2[i];
2247 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2248 buf[outlen++] = ScreenLines[++i];
2249 }
2250
Bram Moolenaar30613902019-12-01 22:11:18 +01002251 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2253 vim_free(buf);
2254
2255 return retval;
2256 }
2257 else
2258 {
2259 return gui_outstr_nowrap(&ScreenLines[off], len,
2260 flags, fg, bg, back);
2261 }
2262}
Bram Moolenaar30613902019-12-01 22:11:18 +01002263#endif // FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264
2265/*
2266 * Output the given string at the current cursor position. If the string is
2267 * too long to fit on the line, then it is truncated.
2268 * "flags":
2269 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2270 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002271 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 * background.
2273 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2274 * it.
2275 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2276 * FAIL (the caller should start drawing "back" chars back).
2277 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002278 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002279gui_outstr_nowrap(
2280 char_u *s,
2281 int len,
2282 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002283 guicolor_T fg, // colors for cursor
2284 guicolor_T bg, // colors for cursor
2285 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286{
2287 long_u highlight_mask;
2288 long_u hl_mask_todo;
2289 guicolor_T fg_color;
2290 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002291 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002292#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002294 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295# ifdef FEAT_XFONTSET
2296 GuiFontset fontset = NOFONTSET;
2297# endif
2298#endif
2299 attrentry_T *aep = NULL;
2300 int draw_flags;
2301 int col = gui.col;
2302#ifdef FEAT_SIGN_ICONS
2303 int draw_sign = FALSE;
Bram Moolenaarc7283072019-07-16 20:12:44 +02002304 int signcol = 0;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002305 char_u extra[18];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306# ifdef FEAT_NETBEANS_INTG
2307 int multi_sign = FALSE;
2308# endif
2309#endif
2310
2311 if (len < 0)
2312 len = (int)STRLEN(s);
2313 if (len == 0)
2314 return OK;
2315
2316#ifdef FEAT_SIGN_ICONS
2317 if (*s == SIGN_BYTE
2318# ifdef FEAT_NETBEANS_INTG
2319 || *s == MULTISIGN_BYTE
2320# endif
Bram Moolenaar0231f832019-07-12 19:22:22 +02002321 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
2323# ifdef FEAT_NETBEANS_INTG
2324 if (*s == MULTISIGN_BYTE)
2325 multi_sign = TRUE;
2326# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002327 // draw spaces instead
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002328 if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
2329 (curwin->w_p_nu || curwin->w_p_rnu))
2330 {
2331 sprintf((char *)extra, "%*c ", number_width(curwin), ' ');
2332 s = extra;
2333 }
2334 else
2335 s = (char_u *)" ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 if (len == 1 && col > 0)
2337 --col;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002338 len = (int)STRLEN(s);
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002339 if (len > 2)
Bram Moolenaar0231f832019-07-12 19:22:22 +02002340 // right align sign icon in the number column
2341 signcol = col + len - 3;
2342 else
2343 signcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 draw_sign = TRUE;
2345 highlight_mask = 0;
2346 }
2347 else
2348#endif
2349 if (gui.highlight_mask > HL_ALL)
2350 {
2351 aep = syn_gui_attr2entry(gui.highlight_mask);
Bram Moolenaar30613902019-12-01 22:11:18 +01002352 if (aep == NULL) // highlighting not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 highlight_mask = 0;
2354 else
2355 highlight_mask = aep->ae_attr;
2356 }
2357 else
2358 highlight_mask = gui.highlight_mask;
2359 hl_mask_todo = highlight_mask;
2360
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002361#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002362 // Set the font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2364 font = aep->ae_u.gui.font;
2365# ifdef FEAT_XFONTSET
2366 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2367 fontset = aep->ae_u.gui.fontset;
2368# endif
2369 else
2370 {
2371# ifdef FEAT_XFONTSET
2372 if (gui.fontset != NOFONTSET)
2373 fontset = gui.fontset;
2374 else
2375# endif
2376 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2377 {
2378 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2379 {
2380 font = gui.boldital_font;
2381 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2382 }
2383 else if (gui.bold_font != NOFONT)
2384 {
2385 font = gui.bold_font;
2386 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2387 }
2388 else
2389 font = gui.norm_font;
2390 }
2391 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2392 {
2393 font = gui.ital_font;
2394 hl_mask_todo &= ~HL_ITALIC;
2395 }
2396 else
2397 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002398
Bram Moolenaardb250522013-06-17 22:43:25 +02002399 /*
2400 * Choose correct wide_font by font. wide_font should be set with font
2401 * at same time in above block. But it will make many "ifdef" nasty
2402 * blocks. So we do it here.
2403 */
2404 if (font == gui.boldital_font && gui.wide_boldital_font)
2405 wide_font = gui.wide_boldital_font;
2406 else if (font == gui.bold_font && gui.wide_bold_font)
2407 wide_font = gui.wide_bold_font;
2408 else if (font == gui.ital_font && gui.wide_ital_font)
2409 wide_font = gui.wide_ital_font;
2410 else if (font == gui.norm_font && gui.wide_font)
2411 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
2413# ifdef FEAT_XFONTSET
2414 if (fontset != NOFONTSET)
2415 gui_mch_set_fontset(fontset);
2416 else
2417# endif
2418 gui_mch_set_font(font);
2419#endif
2420
2421 draw_flags = 0;
2422
Bram Moolenaar30613902019-12-01 22:11:18 +01002423 // Set the color
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 bg_color = gui.back_pixel;
2425 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2426 {
2427 draw_flags |= DRAW_CURSOR;
2428 fg_color = fg;
2429 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002430 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
2432 else if (aep != NULL)
2433 {
2434 fg_color = aep->ae_u.gui.fg_color;
2435 if (fg_color == INVALCOLOR)
2436 fg_color = gui.norm_pixel;
2437 bg_color = aep->ae_u.gui.bg_color;
2438 if (bg_color == INVALCOLOR)
2439 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002440 sp_color = aep->ae_u.gui.sp_color;
2441 if (sp_color == INVALCOLOR)
2442 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 }
2444 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002445 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002447 sp_color = fg_color;
2448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
2450 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2451 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 gui_mch_set_fg_color(bg_color);
2453 gui_mch_set_bg_color(fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
2455 else
2456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 gui_mch_set_fg_color(fg_color);
2458 gui_mch_set_bg_color(bg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002460 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
Bram Moolenaar30613902019-12-01 22:11:18 +01002462 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 if (!(flags & GUI_MON_NOCLEAR))
2464 clip_may_clear_selection(gui.row, gui.row);
2465
2466
Bram Moolenaar30613902019-12-01 22:11:18 +01002467 // If there's no bold font, then fake it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2469 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
2471 /*
2472 * When drawing bold or italic characters the spill-over from the left
2473 * neighbor may be destroyed. Let the caller backup to start redrawing
2474 * just after a blank.
2475 */
2476 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2477 return FAIL;
2478
Bram Moolenaare60acc12011-05-10 16:41:25 +02002479#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002480 // If there's no italic font, then fake it.
2481 // For GTK2, we don't need a different font for italic style.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 if (hl_mask_todo & HL_ITALIC)
2483 draw_flags |= DRAW_ITALIC;
2484
Bram Moolenaar30613902019-12-01 22:11:18 +01002485 // Do we underline the text?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 if (hl_mask_todo & HL_UNDERLINE)
2487 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002488
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489#else
Bram Moolenaar30613902019-12-01 22:11:18 +01002490 // Do we underline the text?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002491 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 draw_flags |= DRAW_UNDERL;
2493#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002494 // Do we undercurl the text?
Bram Moolenaar3918c952005-03-15 22:34:55 +00002495 if (hl_mask_todo & HL_UNDERCURL)
2496 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
Bram Moolenaar30613902019-12-01 22:11:18 +01002498 // Do we strikethrough the text?
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002499 if (hl_mask_todo & HL_STRIKETHROUGH)
2500 draw_flags |= DRAW_STRIKE;
2501
Bram Moolenaar30613902019-12-01 22:11:18 +01002502 // Do we draw transparently?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 if (flags & GUI_MON_TRS_CURSOR)
2504 draw_flags |= DRAW_TRANSP;
2505
2506 /*
2507 * Draw the text.
2508 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002509#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01002510 // The value returned is the length in display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2512#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 if (enc_utf8)
2514 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002515 int start; // index of bytes to be drawn
2516 int cells; // cellwidth of bytes to be drawn
2517 int thislen; // length of bytes to be drawn
2518 int cn; // cellwidth of current char
2519 int i; // index of current char
2520 int c; // current char value
2521 int cl; // byte length of current char
2522 int comping; // current char is composing
2523 int scol = col; // screen column
2524 int curr_wide = FALSE; // use 'guifontwide'
Bram Moolenaar45933962013-01-23 17:43:57 +01002525 int prev_wide = FALSE;
2526 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002527# ifdef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01002528 int sep_comp = FALSE; // Don't separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002529# else
Bram Moolenaar30613902019-12-01 22:11:18 +01002530 int sep_comp = TRUE; // Separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
Bram Moolenaar30613902019-12-01 22:11:18 +01002533 // Break the string at a composing character, it has to be drawn on
2534 // top of the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 start = 0;
2536 cells = 0;
2537 for (i = 0; i < len; i += cl)
2538 {
2539 c = utf_ptr2char(s + i);
2540 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 comping = utf_iscomposing(c);
Bram Moolenaar30613902019-12-01 22:11:18 +01002542 if (!comping) // count cells from non-composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002544 if (!comping || sep_comp)
2545 {
2546 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002547# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002548 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002549# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002550 && wide_font != NOFONT)
2551 curr_wide = TRUE;
2552 else
2553 curr_wide = FALSE;
2554 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002555 cl = utf_ptr2len(s + i);
Bram Moolenaar30613902019-12-01 22:11:18 +01002556 if (cl == 0) // hit end of string
2557 len = i + cl; // len must be wrong "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaar45933962013-01-23 17:43:57 +01002559 wide_changed = curr_wide != prev_wide;
2560
Bram Moolenaar30613902019-12-01 22:11:18 +01002561 // Print the string so far if it's the last character or there is
2562 // a composing character.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002563 if (i + cl >= len || (comping && sep_comp && i > start)
2564 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002565# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002567# ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +01002568 // No fontset: At least draw char after wide char at
2569 // right position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002572 )
2573# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 )
2575 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002576 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 thislen = i - start;
2578 else
2579 thislen = i - start + cl;
2580 if (thislen > 0)
2581 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002582 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002583 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2585 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002586 if (prev_wide)
2587 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 start += thislen;
2589 }
2590 scol += cells;
2591 cells = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01002592 // Adjust to not draw a character which width is changed
2593 // against with last one.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002594 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002596 scol -= cn;
2597 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
2599
Bram Moolenaar13505972019-01-24 15:04:48 +01002600# if defined(FEAT_GUI_X11)
Bram Moolenaar30613902019-12-01 22:11:18 +01002601 // No fontset: draw a space to fill the gap after a wide char
2602 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002604# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002606# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002607 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2609 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002612 // Draw a composing char on top of the previous char.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002613 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002615# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaar30613902019-12-01 22:11:18 +01002616 // Carbon ATSUI autodraws composing char over previous char
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002617 gui_mch_draw_string(gui.row, scol, s + i, cl,
2618 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002619# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002620 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2621 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 start = i + cl;
2624 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002625 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002627 // The stuff below assumes "len" is the length in screen columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 len = scol - col;
2629 }
2630 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
2632 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (enc_dbcs == DBCS_JPNU)
2634 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002635 // Get the length in display cells, this can be different from the
2636 // number of bytes for "euc-jp".
Bram Moolenaar72597a52010-07-18 15:31:08 +02002637 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002640#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2643 gui.col = col + len;
2644
Bram Moolenaar30613902019-12-01 22:11:18 +01002645 // May need to invert it when it's part of the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (flags & GUI_MON_NOCLEAR)
2647 clip_may_redraw_selection(gui.row, col, len);
2648
2649 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2650 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002651 // Invalidate the old physical cursor position if we wrote over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (gui.cursor_row == gui.row
2653 && gui.cursor_col >= col
2654 && gui.cursor_col < col + len)
2655 gui.cursor_is_valid = FALSE;
2656 }
2657
2658#ifdef FEAT_SIGN_ICONS
2659 if (draw_sign)
Bram Moolenaar30613902019-12-01 22:11:18 +01002660 // Draw the sign on top of the spaces.
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002661 gui_mch_drawsign(gui.row, signcol, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002662# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002663 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (multi_sign)
2665 netbeans_draw_multisign_indicator(gui.row);
2666# endif
2667#endif
2668
2669 return OK;
2670}
2671
2672/*
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002673 * Undraw the cursor. This actually redraws the character at the cursor
2674 * position, plus some more characters when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 */
2676 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002677gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
2679 if (gui.cursor_is_valid)
2680 {
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002681 // Always redraw the character just before if there is one, because
2682 // with some fonts and characters there can be a one pixel overlap.
2683 int startcol = gui.cursor_col > 0 ? gui.cursor_col - 1 : gui.cursor_col;
2684 int endcol = gui.cursor_col;
2685
2686#ifdef FEAT_GUI_GTK
2687 gui_adjust_undraw_cursor_for_ligatures(&startcol, &endcol);
2688#endif
2689 gui_redraw_block(gui.cursor_row, startcol,
2690 gui.cursor_row, endcol, GUI_MON_NOCLEAR);
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002691
Bram Moolenaar54612582019-11-21 17:13:31 +01002692 // Cursor_is_valid is reset when the cursor is undrawn, also reset it
2693 // here in case it wasn't needed to undraw it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 gui.cursor_is_valid = FALSE;
2695 }
2696}
2697
2698 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002699gui_redraw(
2700 int x,
2701 int y,
2702 int w,
2703 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
2705 int row1, col1, row2, col2;
2706
2707 row1 = Y_2_ROW(y);
2708 col1 = X_2_COL(x);
2709 row2 = Y_2_ROW(y + h - 1);
2710 col2 = X_2_COL(x + w - 1);
2711
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002712 gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
2714 /*
2715 * We may need to redraw the cursor, but don't take it upon us to change
2716 * its location after a scroll.
2717 * (maybe be more strict even and test col too?)
2718 * These things may be outside the update/clipping region and reality may
2719 * not reflect Vims internal ideas if these operations are clipped away.
2720 */
2721 if (gui.row == gui.cursor_row)
2722 gui_update_cursor(TRUE, TRUE);
2723}
2724
2725/*
2726 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2727 * from col1 to col2 (inclusive).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002729 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002730gui_redraw_block(
2731 int row1,
2732 int col1,
2733 int row2,
2734 int col2,
Bram Moolenaar30613902019-12-01 22:11:18 +01002735 int flags) // flags for gui_outstr_nowrap()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736{
2737 int old_row, old_col;
2738 long_u old_hl_mask;
2739 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002740 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 int idx, len;
2742 int back, nback;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
Bram Moolenaar30613902019-12-01 22:11:18 +01002745 // Don't try to update when ScreenLines is not valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (!screen_cleared || ScreenLines == NULL)
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002747 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
Bram Moolenaar30613902019-12-01 22:11:18 +01002749 // Don't try to draw outside the shell!
2750 // Check everything, strange values may be caused by a big border width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 col1 = check_col(col1);
2752 col2 = check_col(col2);
2753 row1 = check_row(row1);
2754 row2 = check_row(row2);
2755
Bram Moolenaar30613902019-12-01 22:11:18 +01002756 // Remember where our cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 old_row = gui.row;
2758 old_col = gui.col;
2759 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 orig_col1 = col1;
2761 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
2763 for (gui.row = row1; gui.row <= row2; gui.row++)
2764 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002765 // When only half of a double-wide character is in the block, include
2766 // the other half.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 col1 = orig_col1;
2768 col2 = orig_col2;
2769 off = LineOffset[gui.row];
2770 if (enc_dbcs != 0)
2771 {
2772 if (col1 > 0)
2773 col1 -= dbcs_screen_head_off(ScreenLines + off,
2774 ScreenLines + off + col1);
2775 col2 += dbcs_screen_tail_off(ScreenLines + off,
2776 ScreenLines + off + col2);
2777 }
2778 else if (enc_utf8)
2779 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002780 if (ScreenLines[off + col1] == 0)
2781 {
2782 if (col1 > 0)
2783 --col1;
2784 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002785 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002786 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002787 // Make this IEMSGN when it no longer breaks Travis CI.
2788 vim_snprintf((char *)IObuff, IOSIZE,
2789 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2790 (long)gui.row);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002791 msg((char *)IObuff);
Bram Moolenaar9a853462018-12-07 14:10:37 +01002792 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002793 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002794#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2796 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 gui.col = col1;
2800 off = LineOffset[gui.row] + gui.col;
2801 len = col2 - col1 + 1;
2802
Bram Moolenaar30613902019-12-01 22:11:18 +01002803 // Find how many chars back this highlighting starts, or where a space
2804 // is. Needed for when the bold trick is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 for (back = 0; back < col1; ++back)
2806 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2807 || ScreenLines[off - 1 - back] == ' ')
2808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809
Bram Moolenaar30613902019-12-01 22:11:18 +01002810 // Break it up in strings of characters with the same attributes.
2811 // Print UTF-8 characters individually.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 while (len > 0)
2813 {
2814 first_attr = ScreenAttrs[off];
2815 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002816#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 if (enc_utf8 && ScreenLinesUC[off] != 0)
2818 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002819 // output multi-byte character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 nback = gui_screenchar(off, flags,
2821 (guicolor_T)0, (guicolor_T)0, back);
2822 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2823 idx = 2;
2824 else
2825 idx = 1;
2826 }
2827 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2828 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002829 // output double-byte, single-width character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 nback = gui_screenchar(off, flags,
2831 (guicolor_T)0, (guicolor_T)0, back);
2832 idx = 1;
2833 }
2834 else
2835#endif
2836 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002837#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 for (idx = 0; idx < len; ++idx)
2839 {
2840 if (enc_utf8 && ScreenLines[off + idx] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002841 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 if (ScreenAttrs[off + idx] != first_attr)
2843 break;
2844 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002845 // gui_screenstr() takes care of multibyte chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 nback = gui_screenstr(off, idx, flags,
2847 (guicolor_T)0, (guicolor_T)0, back);
2848#else
2849 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2850 idx++)
2851 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002852 // Stop at a multi-byte Unicode character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2854 break;
2855 if (enc_dbcs == DBCS_JPNU)
2856 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002857 // Stop at a double-byte single-width char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 if (ScreenLines[off + idx] == 0x8e)
2859 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002860 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 + off + idx) == 2)
Bram Moolenaar30613902019-12-01 22:11:18 +01002862 ++idx; // skip second byte of double-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2866 (guicolor_T)0, (guicolor_T)0, back);
2867#endif
2868 }
2869 if (nback == FAIL)
2870 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002871 // Must back up to start drawing where a bold or italic word
2872 // starts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 off -= back;
2874 len += back;
2875 gui.col -= back;
2876 }
2877 else
2878 {
2879 off += idx;
2880 len -= idx;
2881 }
2882 back = 0;
2883 }
2884 }
2885
Bram Moolenaar30613902019-12-01 22:11:18 +01002886 // Put the cursor back where it was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 gui.row = old_row;
2888 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002889 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890}
2891
2892 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002893gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
2895 if (count <= 0)
2896 return;
2897
2898 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002899 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 gui_clear_block(row, gui.scroll_region_left,
2901 gui.scroll_region_bot, gui.scroll_region_right);
2902 else
2903 {
2904 gui_mch_delete_lines(row, count);
2905
Bram Moolenaar30613902019-12-01 22:11:18 +01002906 // If the cursor was in the deleted lines it's now gone. If the
2907 // cursor was in the scrolled lines adjust its position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 if (gui.cursor_row >= row
2909 && gui.cursor_col >= gui.scroll_region_left
2910 && gui.cursor_col <= gui.scroll_region_right)
2911 {
2912 if (gui.cursor_row < row + count)
2913 gui.cursor_is_valid = FALSE;
2914 else if (gui.cursor_row <= gui.scroll_region_bot)
2915 gui.cursor_row -= count;
2916 }
2917 }
2918}
2919
2920 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002921gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
2923 if (count <= 0)
2924 return;
2925
2926 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002927 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 gui_clear_block(row, gui.scroll_region_left,
2929 gui.scroll_region_bot, gui.scroll_region_right);
2930 else
2931 {
2932 gui_mch_insert_lines(row, count);
2933
2934 if (gui.cursor_row >= gui.row
2935 && gui.cursor_col >= gui.scroll_region_left
2936 && gui.cursor_col <= gui.scroll_region_right)
2937 {
2938 if (gui.cursor_row <= gui.scroll_region_bot - count)
2939 gui.cursor_row += count;
2940 else if (gui.cursor_row <= gui.scroll_region_bot)
2941 gui.cursor_is_valid = FALSE;
2942 }
2943 }
2944}
2945
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002946#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002947/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002948 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2949 */
2950 static int
2951gui_wait_for_chars_3(
2952 long wtime,
2953 int *interrupted UNUSED,
2954 int ignore_input UNUSED)
2955{
2956 return gui_mch_wait_for_chars(wtime);
2957}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002958#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002959
2960/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002961 * Returns OK if a character was found to be available within the given time,
2962 * or FAIL otherwise.
2963 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002964 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002965gui_wait_for_chars_or_timer(
2966 long wtime,
2967 int *interrupted UNUSED,
2968 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002969{
2970#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002971 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2972 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002973#else
2974 return gui_mch_wait_for_chars(wtime);
2975#endif
2976}
2977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978/*
2979 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002980 * "wtime" == -1 Wait forever.
2981 * "wtime" == 0 Don't wait.
2982 * "wtime" > 0 Wait wtime milliseconds for a character.
2983 *
2984 * Returns the number of characters read or zero when timed out or interrupted.
2985 * "buf" may be NULL, in which case a non-zero number is returned if characters
2986 * are available.
2987 */
2988 static int
2989gui_wait_for_chars_buf(
2990 char_u *buf,
2991 int maxlen,
2992 long wtime, // don't use "time", MIPS cannot handle it
2993 int tb_change_cnt)
2994{
2995 int retval;
2996
2997#ifdef FEAT_MENU
2998 // If we're going to wait a bit, update the menus and mouse shape for the
2999 // current State.
3000 if (wtime != 0)
3001 gui_update_menus(0);
3002#endif
3003
3004 gui_mch_update();
3005 if (input_available()) // Got char, return immediately
3006 {
3007 if (buf != NULL && !typebuf_changed(tb_change_cnt))
3008 return read_from_input_buf(buf, (long)maxlen);
3009 return 0;
3010 }
3011 if (wtime == 0) // Don't wait for char
3012 return FAIL;
3013
3014 // Before waiting, flush any output to the screen.
3015 gui_mch_flush();
3016
3017 // Blink while waiting for a character.
3018 gui_mch_start_blink();
3019
3020 // Common function to loop until "wtime" is met, while handling timers and
3021 // other callbacks.
3022 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
3023 gui_wait_for_chars_or_timer, NULL);
3024
3025 gui_mch_stop_blink(TRUE);
3026
3027 return retval;
3028}
3029
3030/*
3031 * Wait for a character from the keyboard without actually reading it.
3032 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 * wtime == -1 Wait forever.
3034 * wtime == 0 Don't wait.
3035 * wtime > 0 Wait wtime milliseconds for a character.
3036 * Returns OK if a character was found to be available within the given time,
3037 * or FAIL otherwise.
3038 */
3039 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003040gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003042 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043}
3044
3045/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003046 * Equivalent of mch_inchar() for the GUI.
3047 */
3048 int
3049gui_inchar(
3050 char_u *buf,
3051 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003052 long wtime, // milliseconds
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003053 int tb_change_cnt)
3054{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003055 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003056}
3057
3058/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003059 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 */
3061 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003062fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 p[0] = (char_u)(col / 128 + ' ' + 1);
3065 p[1] = (char_u)(col % 128 + ' ' + 1);
3066 p[2] = (char_u)(row / 128 + ' ' + 1);
3067 p[3] = (char_u)(row % 128 + ' ' + 1);
3068}
3069
3070/*
3071 * Generic mouse support function. Add a mouse event to the input buffer with
3072 * the given properties.
3073 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3074 * MOUSE_X1, MOUSE_X2
3075 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003076 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3077 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 * x, y --- Coordinates of mouse in pixels.
3079 * repeated_click --- TRUE if this click comes only a short time after a
3080 * previous click.
3081 * modifiers --- Bit field which may be any of the following modifiers
3082 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3083 * This function will ignore drag events where the mouse has not moved to a new
3084 * character.
3085 */
3086 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003087gui_send_mouse_event(
3088 int button,
3089 int x,
3090 int y,
3091 int repeated_click,
3092 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
3094 static int prev_row = 0, prev_col = 0;
3095 static int prev_button = -1;
3096 static int num_clicks = 1;
3097 char_u string[10];
3098 enum key_extra button_char;
3099 int row, col;
3100#ifdef FEAT_CLIPBOARD
3101 int checkfor;
3102 int did_clip = FALSE;
3103#endif
3104
3105 /*
3106 * Scrolling may happen at any time, also while a selection is present.
3107 */
3108 switch (button)
3109 {
Bram Moolenaar445f11d2021-06-08 20:13:31 +02003110 case MOUSE_MOVE:
3111 button_char = KE_MOUSEMOVE_XY;
3112 goto button_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 case MOUSE_X1:
3114 button_char = KE_X1MOUSE;
3115 goto button_set;
3116 case MOUSE_X2:
3117 button_char = KE_X2MOUSE;
3118 goto button_set;
3119 case MOUSE_4:
3120 button_char = KE_MOUSEDOWN;
3121 goto button_set;
3122 case MOUSE_5:
3123 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003124 goto button_set;
3125 case MOUSE_6:
3126 button_char = KE_MOUSELEFT;
3127 goto button_set;
3128 case MOUSE_7:
3129 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130button_set:
3131 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003132 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 if (hold_gui_events)
3134 return;
3135
Ernie Raelc4cb5442022-04-03 15:47:28 +01003136 row = gui_xy2colrow(x, y, &col);
3137 // Don't report a mouse move unless moved to a
3138 // different character position.
3139 if (button == MOUSE_MOVE)
3140 {
3141 if (row == prev_row && col == prev_col)
3142 return;
3143 else
3144 {
3145 prev_row = row >= 0 ? row : 0;
3146 prev_col = col;
3147 }
3148 }
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 string[3] = CSI;
3151 string[4] = KS_EXTRA;
3152 string[5] = (int)button_char;
3153
Bram Moolenaar30613902019-12-01 22:11:18 +01003154 // Pass the pointer coordinates of the scroll event so that we
3155 // know which window to scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 string[6] = (char_u)(col / 128 + ' ' + 1);
3157 string[7] = (char_u)(col % 128 + ' ' + 1);
3158 string[8] = (char_u)(row / 128 + ' ' + 1);
3159 string[9] = (char_u)(row % 128 + ' ' + 1);
3160
3161 if (modifiers == 0)
3162 add_to_input_buf(string + 3, 7);
3163 else
3164 {
3165 string[0] = CSI;
3166 string[1] = KS_MODIFIER;
3167 string[2] = 0;
3168 if (modifiers & MOUSE_SHIFT)
3169 string[2] |= MOD_MASK_SHIFT;
3170 if (modifiers & MOUSE_CTRL)
3171 string[2] |= MOD_MASK_CTRL;
3172 if (modifiers & MOUSE_ALT)
3173 string[2] |= MOD_MASK_ALT;
3174 add_to_input_buf(string, 10);
3175 }
3176 return;
3177 }
3178 }
3179
3180#ifdef FEAT_CLIPBOARD
Bram Moolenaar30613902019-12-01 22:11:18 +01003181 // If a clipboard selection is in progress, handle it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (clip_star.state == SELECT_IN_PROGRESS)
3183 {
3184 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
Bram Moolenaar0ce37332019-12-18 21:33:22 +01003185
3186 // A release event may still need to be sent if the position is equal.
3187 row = gui_xy2colrow(x, y, &col);
3188 if (button != MOUSE_RELEASE || row != prev_row || col != prev_col)
3189 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 }
3191
Bram Moolenaar30613902019-12-01 22:11:18 +01003192 // Determine which mouse settings to look for based on the current mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 switch (get_real_state())
3194 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003195 case MODE_NORMAL_BUSY:
3196 case MODE_OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003197# ifdef FEAT_TERMINAL
Bram Moolenaar24959102022-05-07 20:01:16 +01003198 case MODE_TERMINAL:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003199# endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003200 case MODE_NORMAL: checkfor = MOUSE_NORMAL; break;
3201 case MODE_VISUAL: checkfor = MOUSE_VISUAL; break;
3202 case MODE_SELECT: checkfor = MOUSE_VISUAL; break;
3203 case MODE_REPLACE:
3204 case MODE_REPLACE | MODE_LANGMAP:
3205 case MODE_VREPLACE:
3206 case MODE_VREPLACE | MODE_LANGMAP:
3207 case MODE_INSERT:
3208 case MODE_INSERT | MODE_LANGMAP:
3209 checkfor = MOUSE_INSERT; break;
3210 case MODE_ASKMORE:
3211 case MODE_HITRETURN: // At the more- and hit-enter prompt pass the
Bram Moolenaar30613902019-12-01 22:11:18 +01003212 // mouse event for a click on or below the
3213 // message line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 if (Y_2_ROW(y) >= msg_row)
3215 checkfor = MOUSE_NORMAL;
3216 else
3217 checkfor = MOUSE_RETURN;
3218 break;
3219
3220 /*
3221 * On the command line, use the clipboard selection on all lines
3222 * but the command line. But not when pasting.
3223 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003224 case MODE_CMDLINE:
3225 case MODE_CMDLINE | MODE_LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3227 checkfor = MOUSE_NONE;
3228 else
3229 checkfor = MOUSE_COMMAND;
3230 break;
3231
3232 default:
3233 checkfor = MOUSE_NONE;
3234 break;
3235 };
3236
3237 /*
3238 * Allow clipboard selection of text on the command line in "normal"
3239 * modes. Don't do this when dragging the status line, or extending a
3240 * Visual selection.
3241 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003242 if ((State == MODE_NORMAL || State == MODE_NORMAL_BUSY
3243 || (State & MODE_INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003244 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 && button != MOUSE_DRAG
3246# ifdef FEAT_MOUSESHAPE
3247 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249# endif
3250 )
3251 checkfor = MOUSE_NONE;
3252
3253 /*
3254 * Use modeless selection when holding CTRL and SHIFT pressed.
3255 */
3256 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3257 checkfor = MOUSE_NONEF;
3258
3259 /*
3260 * In Ex mode, always use modeless selection.
3261 */
3262 if (exmode_active)
3263 checkfor = MOUSE_NONE;
3264
3265 /*
3266 * If the mouse settings say to not use the mouse, use the modeless
3267 * selection. But if Visual is active, assume that only the Visual area
3268 * will be selected.
3269 * Exception: On the command line, both the selection is used and a mouse
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003270 * key is sent.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 */
3272 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3273 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003274 // Don't do modeless selection in Visual mode.
Bram Moolenaar24959102022-05-07 20:01:16 +01003275 if (checkfor != MOUSE_NONEF && VIsual_active && (State & MODE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
3278 /*
3279 * When 'mousemodel' is "popup", shift-left is translated to right.
3280 * But not when also using Ctrl.
3281 */
3282 if (mouse_model_popup() && button == MOUSE_LEFT
3283 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3284 {
3285 button = MOUSE_RIGHT;
3286 modifiers &= ~ MOUSE_SHIFT;
3287 }
3288
Bram Moolenaar30613902019-12-01 22:11:18 +01003289 // If the selection is done, allow the right button to extend it.
3290 // If the selection is cleared, allow the right button to start it
3291 // from the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (button == MOUSE_RIGHT)
3293 {
3294 if (clip_star.state == SELECT_CLEARED)
3295 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003296 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 col = msg_col;
3299 row = msg_row;
3300 }
3301 else
3302 {
3303 col = curwin->w_wcol;
3304 row = curwin->w_wrow + W_WINROW(curwin);
3305 }
3306 clip_start_selection(col, row, FALSE);
3307 }
3308 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3309 repeated_click);
3310 did_clip = TRUE;
3311 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003312 // Allow the left button to start the selection
Bram Moolenaare60acc12011-05-10 16:41:25 +02003313 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
3315 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3316 did_clip = TRUE;
3317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318
Bram Moolenaar30613902019-12-01 22:11:18 +01003319 // Always allow pasting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 if (button != MOUSE_MIDDLE)
3321 {
3322 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3323 return;
3324 if (checkfor != MOUSE_COMMAND)
3325 button = MOUSE_LEFT;
3326 }
3327 repeated_click = FALSE;
3328 }
3329
3330 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003331 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332#endif
3333
Bram Moolenaar30613902019-12-01 22:11:18 +01003334 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 if (hold_gui_events)
3336 return;
3337
3338 row = gui_xy2colrow(x, y, &col);
3339
3340 /*
3341 * If we are dragging and the mouse hasn't moved far enough to be on a
3342 * different character, then don't send an event to vim.
3343 */
3344 if (button == MOUSE_DRAG)
3345 {
3346 if (row == prev_row && col == prev_col)
3347 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01003348 // Dragging above the window, set "row" to -1 to cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (y < 0)
3350 row = -1;
3351 }
3352
3353 /*
3354 * If topline has changed (window scrolled) since the last click, reset
3355 * repeated_click, because we don't want starting Visual mode when
3356 * clicking on a different character in the text.
3357 */
3358 if (curwin->w_topline != gui_prev_topline
3359#ifdef FEAT_DIFF
3360 || curwin->w_topfill != gui_prev_topfill
3361#endif
3362 )
3363 repeated_click = FALSE;
3364
Bram Moolenaar30613902019-12-01 22:11:18 +01003365 string[0] = CSI; // this sequence is recognized by check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 string[1] = KS_MOUSE;
3367 string[2] = KE_FILLER;
3368 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3369 {
3370 if (repeated_click)
3371 {
3372 /*
3373 * Handle multiple clicks. They only count if the mouse is still
3374 * pointing at the same character.
3375 */
3376 if (button != prev_button || row != prev_row || col != prev_col)
3377 num_clicks = 1;
3378 else if (++num_clicks > 4)
3379 num_clicks = 1;
3380 }
3381 else
3382 num_clicks = 1;
3383 prev_button = button;
3384 gui_prev_topline = curwin->w_topline;
3385#ifdef FEAT_DIFF
3386 gui_prev_topfill = curwin->w_topfill;
3387#endif
3388
3389 string[3] = (char_u)(button | 0x20);
3390 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3391 }
3392 else
3393 string[3] = (char_u)button;
3394
3395 string[3] |= modifiers;
3396 fill_mouse_coord(string + 4, col, row);
3397 add_to_input_buf(string, 8);
3398
3399 if (row < 0)
3400 prev_row = 0;
3401 else
3402 prev_row = row;
3403 prev_col = col;
3404
3405 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01003406 * We need to make sure this is cleared since GTK doesn't tell us when
3407 * the user is done dragging.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 */
Bram Moolenaar0b962e52022-04-03 18:02:37 +01003409#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 gui.dragged_sb = SBAR_NONE;
3411#endif
3412}
3413
3414/*
3415 * Convert x and y coordinate to column and row in text window.
3416 * Corrects for multi-byte character.
3417 * returns column in "*colp" and row as return value;
3418 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003419 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003420gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
3422 int col = check_col(X_2_COL(x));
3423 int row = check_row(Y_2_ROW(y));
3424
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 return row;
3427}
3428
3429#if defined(FEAT_MENU) || defined(PROTO)
3430/*
3431 * Callback function for when a menu entry has been selected.
3432 */
3433 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003434gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003436 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
Bram Moolenaar30613902019-12-01 22:11:18 +01003438 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 if (hold_gui_events)
3440 return;
3441
3442 bytes[0] = CSI;
3443 bytes[1] = KS_MENU;
3444 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003445 add_to_input_buf(bytes, 3);
3446 add_long_to_buf((long_u)menu, bytes);
3447 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448}
3449#endif
3450
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003451static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453/*
3454 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003455 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 * in p_go.
3457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003459gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460{
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003461#ifdef FEAT_GUI_DARKTHEME
3462 static int prev_dark_theme = -1;
3463 int using_dark_theme = FALSE;
3464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465#ifdef FEAT_MENU
3466 static int prev_menu_is_active = -1;
3467#endif
3468#ifdef FEAT_TOOLBAR
3469 static int prev_toolbar = -1;
3470 int using_toolbar = FALSE;
3471#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003472#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003473 int using_tabline;
3474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475#ifdef FEAT_FOOTER
3476 static int prev_footer = -1;
3477 int using_footer = FALSE;
3478#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003479#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 static int prev_tearoff = -1;
3481 int using_tearoff = FALSE;
3482#endif
3483
3484 char_u *p;
3485 int i;
3486#ifdef FEAT_MENU
3487 int grey_old, grey_new;
3488 char_u *temp;
3489#endif
3490 win_T *wp;
3491 int need_set_size;
3492 int fix_size;
3493
3494#ifdef FEAT_MENU
3495 if (oldval != NULL && gui.in_use)
3496 {
3497 /*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003498 * Check if the menus go from grey to non-grey or vice versa.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 */
3500 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3501 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3502 if (grey_old != grey_new)
3503 {
3504 temp = p_go;
3505 p_go = oldval;
3506 gui_update_menus(MENU_ALL_MODES);
3507 p_go = temp;
3508 }
3509 }
3510 gui.menu_is_active = FALSE;
3511#endif
3512
3513 for (i = 0; i < 3; i++)
3514 gui.which_scrollbars[i] = FALSE;
3515 for (p = p_go; *p; p++)
3516 switch (*p)
3517 {
3518 case GO_LEFT:
3519 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3520 break;
3521 case GO_RIGHT:
3522 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3523 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 case GO_VLEFT:
3525 if (win_hasvertsplit())
3526 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3527 break;
3528 case GO_VRIGHT:
3529 if (win_hasvertsplit())
3530 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3531 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 case GO_BOT:
3533 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3534 break;
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003535#ifdef FEAT_GUI_DARKTHEME
3536 case GO_DARKTHEME:
3537 using_dark_theme = TRUE;
3538 break;
3539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540#ifdef FEAT_MENU
3541 case GO_MENUS:
3542 gui.menu_is_active = TRUE;
3543 break;
3544#endif
3545 case GO_GREY:
Bram Moolenaar30613902019-12-01 22:11:18 +01003546 // make menu's have grey items, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 break;
3548#ifdef FEAT_TOOLBAR
3549 case GO_TOOLBAR:
3550 using_toolbar = TRUE;
3551 break;
3552#endif
3553#ifdef FEAT_FOOTER
3554 case GO_FOOTER:
3555 using_footer = TRUE;
3556 break;
3557#endif
3558 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003559#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 using_tearoff = TRUE;
3561#endif
3562 break;
3563 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01003564 // Ignore options that are not supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 break;
3566 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003567
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 if (gui.in_use)
3569 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003570 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003572
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003573#ifdef FEAT_GUI_DARKTHEME
3574 if (using_dark_theme != prev_dark_theme)
3575 {
3576 gui_mch_set_dark_theme(using_dark_theme);
3577 prev_dark_theme = using_dark_theme;
3578 }
3579#endif
3580
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003581#ifdef FEAT_GUI_TABLINE
Bram Moolenaar30613902019-12-01 22:11:18 +01003582 // Update the GUI tab line, it may appear or disappear. This may
3583 // cause the non-GUI tab line to disappear or appear.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003584 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003585 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003586 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003587 // We don't want a resize event change "Rows" here, save and
3588 // restore it. Resizing is handled below.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003589 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003590 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003591 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003592 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003593 if (using_tabline)
3594 fix_size = TRUE;
3595 if (!gui_use_tabline())
Bram Moolenaar30613902019-12-01 22:11:18 +01003596 redraw_tabline = TRUE; // may draw non-GUI tab line
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003597 }
3598#endif
3599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 for (i = 0; i < 3; i++)
3601 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003602 // The scrollbar needs to be updated when it is shown/unshown and
3603 // when switching tab pages. But the size only changes when it's
3604 // shown/unshown. Thus we need two places to remember whether a
3605 // scrollbar is there or not.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003606 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003607 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003608 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 {
3610 if (i == SBAR_BOTTOM)
3611 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3612 gui.which_scrollbars[i]);
3613 else
3614 {
3615 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003618 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3619 {
3620 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003621 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003622 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003623 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003624 if (gui.which_scrollbars[i])
3625 fix_size = TRUE;
3626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003628 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003629 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631
3632#ifdef FEAT_MENU
3633 if (gui.menu_is_active != prev_menu_is_active)
3634 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003635 // We don't want a resize event change "Rows" here, save and
3636 // restore it. Resizing is handled below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 i = Rows;
3638 gui_mch_enable_menu(gui.menu_is_active);
3639 Rows = i;
3640 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003641 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (gui.menu_is_active)
3643 fix_size = TRUE;
3644 }
3645#endif
3646
3647#ifdef FEAT_TOOLBAR
3648 if (using_toolbar != prev_toolbar)
3649 {
3650 gui_mch_show_toolbar(using_toolbar);
3651 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003652 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (using_toolbar)
3654 fix_size = TRUE;
3655 }
3656#endif
3657#ifdef FEAT_FOOTER
3658 if (using_footer != prev_footer)
3659 {
3660 gui_mch_enable_footer(using_footer);
3661 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003662 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (using_footer)
3664 fix_size = TRUE;
3665 }
3666#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003667#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 if (using_tearoff != prev_tearoff)
3669 {
3670 gui_mch_toggle_tearoffs(using_tearoff);
3671 prev_tearoff = using_tearoff;
3672 }
3673#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003674 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003675 {
3676#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003677 long prev_Columns = Columns;
3678 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003679#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003680 // Adjust the size of the window to make the text area keep the
3681 // same size and to avoid that part of our window is off-screen
3682 // and a scrollbar can't be used, for example.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003683 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003684
3685#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01003686 // GTK has the annoying habit of sending us resize events when
3687 // changing the window size ourselves. This mostly happens when
3688 // waiting for a character to arrive, quite unpredictably, and may
3689 // change Columns and Rows when we don't want it. Wait for a
3690 // character here to avoid this effect.
3691 // If you remove this, please test this command for resizing
3692 // effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3693 // Don't do this while starting up though.
3694 // Don't change Rows when adding menu/toolbar/tabline.
3695 // Don't change Columns when adding vertical toolbar.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003696 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003697 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003698 if ((need_set_size & RESIZE_VERT) == 0)
3699 Rows = prev_Rows;
3700 if ((need_set_size & RESIZE_HOR) == 0)
3701 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003702#endif
3703 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003704 // When the console tabline appears or disappears the window positions
3705 // change.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003706 if (firstwin->w_winrow != tabline_height())
Bram Moolenaar30613902019-12-01 22:11:18 +01003707 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709}
3710
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003711#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3712/*
3713 * Return TRUE if the GUI is taking care of the tabline.
3714 * It may still be hidden if 'showtabline' is zero.
3715 */
3716 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003717gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003718{
3719 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3720}
3721
3722/*
3723 * Return TRUE if the GUI is showing the tabline.
3724 * This uses 'showtabline'.
3725 */
3726 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003727gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003728{
3729 if (!gui_use_tabline()
3730 || p_stal == 0
3731 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3732 return FALSE;
3733 return TRUE;
3734}
3735
3736/*
3737 * Update the tabline.
3738 * This may display/undisplay the tabline and update the labels.
3739 */
3740 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003741gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003742{
3743 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003744 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003745
3746 if (!gui.starting && starting == 0)
3747 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003748 // Updating the tabline uses direct GUI commands, flush
3749 // outstanding instructions first. (esp. clear screen)
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003750 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003751
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003752 if (!showit != !shown)
3753 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003754 if (showit != 0)
3755 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003756
Bram Moolenaar30613902019-12-01 22:11:18 +01003757 // When the tabs change from hidden to shown or from shown to
3758 // hidden the size of the text area should remain the same.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003759 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003760 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003761 }
3762}
3763
3764/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003765 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003766 */
3767 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003768get_tabline_label(
3769 tabpage_T *tp,
Bram Moolenaar30613902019-12-01 22:11:18 +01003770 int tooltip) // TRUE: get tooltip
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003771{
3772 int modified = FALSE;
3773 char_u buf[40];
3774 int wincount;
3775 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003776 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003777
Bram Moolenaar30613902019-12-01 22:11:18 +01003778 // Use 'guitablabel' or 'guitabtooltip' if it's set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003779 opt = (tooltip ? &p_gtt : &p_gtl);
3780 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003781 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003782 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003783 int called_emsg_before = called_emsg;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003784 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003785 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003786 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3787 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003788
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003789 printer_page_num = tabpage_index(tp);
3790# ifdef FEAT_EVAL
3791 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003792 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003793# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003794 // It's almost as going to the tabpage, but without autocommands.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003795 curtab->tp_firstwin = firstwin;
3796 curtab->tp_lastwin = lastwin;
3797 curtab->tp_curwin = curwin;
3798 save_curtab = curtab;
3799 curtab = tp;
3800 topframe = curtab->tp_topframe;
3801 firstwin = curtab->tp_firstwin;
3802 lastwin = curtab->tp_lastwin;
3803 curwin = curtab->tp_curwin;
3804 curbuf = curwin->w_buffer;
3805
Bram Moolenaar30613902019-12-01 22:11:18 +01003806 // Can't use NameBuff directly, build_stl_str_hl() uses it.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003807 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003808 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003809 STRCPY(NameBuff, res);
3810
Bram Moolenaar30613902019-12-01 22:11:18 +01003811 // Back to the original curtab.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003812 curtab = save_curtab;
3813 topframe = curtab->tp_topframe;
3814 firstwin = curtab->tp_firstwin;
3815 lastwin = curtab->tp_lastwin;
3816 curwin = curtab->tp_curwin;
3817 curbuf = curwin->w_buffer;
3818
Bram Moolenaar53989552019-12-23 22:59:18 +01003819 if (called_emsg > called_emsg_before)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003820 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003821 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003822 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003823
Bram Moolenaar30613902019-12-01 22:11:18 +01003824 // If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3825 // use a default label.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003826 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003827 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003828 // Get the buffer name into NameBuff[] and shorten it.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003829 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003830 if (!tooltip)
3831 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003832
3833 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3834 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3835 if (bufIsChanged(wp->w_buffer))
3836 modified = TRUE;
3837 if (modified || wincount > 1)
3838 {
3839 if (wincount > 1)
3840 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3841 else
3842 buf[0] = NUL;
3843 if (modified)
3844 STRCAT(buf, "+");
3845 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003846 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003847 mch_memmove(NameBuff, buf, STRLEN(buf));
3848 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003849 }
3850}
3851
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003852/*
3853 * Send the event for clicking to select tab page "nr".
3854 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003855 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003856 */
3857 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003858send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003859{
3860 char_u string[3];
3861
3862 if (nr == tabpage_index(curtab))
3863 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003864
Bram Moolenaar30613902019-12-01 22:11:18 +01003865 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003866 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003867# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003868 || cmdwin_type != 0
3869# endif
3870 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003871 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003872 // Set it back to the current tab page.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003873 gui_mch_set_curtab(tabpage_index(curtab));
3874 return FALSE;
3875 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003876
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003877 string[0] = CSI;
3878 string[1] = KS_TABLINE;
3879 string[2] = KE_FILLER;
3880 add_to_input_buf(string, 3);
3881 string[0] = nr;
3882 add_to_input_buf_csi(string, 1);
3883 return TRUE;
3884}
3885
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003886/*
3887 * Send a tabline menu event
3888 */
3889 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003890send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003891{
3892 char_u string[3];
3893
Bram Moolenaar29547192018-12-11 20:39:19 +01003894 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003895 if (hold_gui_events)
3896 return;
3897
Bram Moolenaar29547192018-12-11 20:39:19 +01003898 // Cannot close the last tabpage.
3899 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3900 return;
3901
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003902 string[0] = CSI;
3903 string[1] = KS_TABMENU;
3904 string[2] = KE_FILLER;
3905 add_to_input_buf(string, 3);
3906 string[0] = tabidx;
3907 string[1] = (char_u)(long)event;
3908 add_to_input_buf_csi(string, 2);
3909}
3910
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912
3913/*
3914 * Scrollbar stuff:
3915 */
3916
Bram Moolenaare45828b2006-02-15 22:12:56 +00003917/*
3918 * Remove all scrollbars. Used before switching to another tab page.
3919 */
3920 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003921gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003922{
3923 int i;
3924 win_T *wp;
3925
3926 for (i = 0; i < 3; i++)
3927 {
3928 if (i == SBAR_BOTTOM)
3929 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3930 else
3931 {
3932 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003933 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003934 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003935 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003936 }
3937}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003940gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
3942 static int sbar_ident = 0;
3943
Bram Moolenaar30613902019-12-01 22:11:18 +01003944 sb->ident = sbar_ident++; // No check for too big, but would it happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 sb->wp = wp;
3946 sb->type = type;
3947 sb->value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 sb->size = 1;
3949 sb->max = 1;
3950 sb->top = 0;
3951 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 sb->status_height = 0;
3954 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3955}
3956
3957/*
3958 * Find the scrollbar with the given index.
3959 */
3960 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003961gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962{
3963 win_T *wp;
3964
3965 if (gui.bottom_sbar.ident == ident)
3966 return &gui.bottom_sbar;
3967 FOR_ALL_WINDOWS(wp)
3968 {
3969 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3970 return &wp->w_scrollbars[SBAR_LEFT];
3971 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3972 return &wp->w_scrollbars[SBAR_RIGHT];
3973 }
3974 return NULL;
3975}
3976
3977/*
3978 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3979 *
3980 * For Win32, Macintosh and GTK+ 2:
3981 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3982 * you stop dragging the scrollbar. We get here each time the scrollbar is
3983 * dragged another pixel, but as far as the rest of vim goes, it thinks
3984 * we're just hanging in the call to DispatchMessage() in
3985 * process_message(). The DispatchMessage() call that hangs was passed a
3986 * mouse button click event in the scrollbar window. -- webb.
3987 *
3988 * Solution: Do the scrolling right here. But only when allowed.
3989 * Ignore the scrollbars while executing an external command or when there
3990 * are still characters to be processed.
3991 */
3992 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003993gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 int sb_num;
3997#ifdef USE_ON_FLY_SCROLL
3998 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000# ifdef FEAT_DIFF
4001 int old_topfill = curwin->w_topfill;
4002# endif
4003#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004004 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 int byte_count;
4006#endif
4007
4008 if (sb == NULL)
4009 return;
4010
Bram Moolenaar30613902019-12-01 22:11:18 +01004011 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 if (hold_gui_events)
4013 return;
4014
4015#ifdef FEAT_CMDWIN
4016 if (cmdwin_type != 0 && sb->wp != curwin)
4017 return;
4018#endif
4019
4020 if (still_dragging)
4021 {
4022 if (sb->wp == NULL)
4023 gui.dragged_sb = SBAR_BOTTOM;
4024 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
4025 gui.dragged_sb = SBAR_LEFT;
4026 else
4027 gui.dragged_sb = SBAR_RIGHT;
4028 gui.dragged_wp = sb->wp;
4029 }
4030 else
4031 {
4032 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004033#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004034 // Keep the "dragged_wp" value until after the scrolling, for when the
4035 // mouse button is released. GTK2 doesn't send the button-up event.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 gui.dragged_wp = NULL;
4037#endif
4038 }
4039
Bram Moolenaar30613902019-12-01 22:11:18 +01004040 // Vertical sbar info is kept in the first sbar (the left one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 if (sb->wp != NULL)
4042 sb = &sb->wp->w_scrollbars[0];
4043
4044 /*
4045 * Check validity of value
4046 */
4047 if (value < 0)
4048 value = 0;
4049#ifdef SCROLL_PAST_END
4050 else if (value > sb->max)
4051 value = sb->max;
4052#else
4053 if (value > sb->max - sb->size + 1)
4054 value = sb->max - sb->size + 1;
4055#endif
4056
4057 sb->value = value;
4058
4059#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar30613902019-12-01 22:11:18 +01004060 // When not allowed to do the scrolling right now, return.
4061 // This also checked input_available(), but that causes the first click in
4062 // a scrollbar to be ignored when Vim doesn't have focus.
Bram Moolenaar67840782008-01-03 15:15:07 +00004063 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 return;
4065#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004066 // Disallow scrolling the current window when the completion popup menu is
4067 // visible.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004068 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4069 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
4071#ifdef FEAT_RIGHTLEFT
4072 if (sb->wp == NULL && curwin->w_p_rl)
4073 {
4074 value = sb->max + 1 - sb->size - value;
4075 if (value < 0)
4076 value = 0;
4077 }
4078#endif
4079
Bram Moolenaar30613902019-12-01 22:11:18 +01004080 if (sb->wp != NULL) // vertical scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
4082 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4084 sb_num++;
4085 if (wp == NULL)
4086 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088#ifdef USE_ON_FLY_SCROLL
4089 current_scrollbar = sb_num;
4090 scrollbar_value = value;
Bram Moolenaar24959102022-05-07 20:01:16 +01004091 if (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
4093 gui_do_scroll();
4094 setcursor();
4095 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004096 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 {
4098 ins_scroll();
4099 setcursor();
4100 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004101 else if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
4103 if (msg_scrolled == 0)
4104 {
4105 gui_do_scroll();
4106 redrawcmdline();
4107 }
4108 }
4109# ifdef FEAT_FOLDING
Bram Moolenaar30613902019-12-01 22:11:18 +01004110 // Value may have been changed for closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 sb->value = sb->wp->w_topline - 1;
4112# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004113
Bram Moolenaar30613902019-12-01 22:11:18 +01004114 // When dragging one scrollbar and there is another one at the other
4115 // side move the thumb of that one too.
Bram Moolenaar371d5402006-03-20 21:47:49 +00004116 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4117 gui_mch_set_scrollbar_thumb(
4118 &sb->wp->w_scrollbars[
4119 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4120 ? SBAR_LEFT : SBAR_RIGHT],
4121 sb->value, sb->size, sb->max);
4122
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123#else
4124 bytes[0] = CSI;
4125 bytes[1] = KS_VER_SCROLLBAR;
4126 bytes[2] = KE_FILLER;
4127 bytes[3] = (char_u)sb_num;
4128 byte_count = 4;
4129#endif
4130 }
4131 else
4132 {
4133#ifdef USE_ON_FLY_SCROLL
4134 scrollbar_value = value;
4135
Bram Moolenaar24959102022-05-07 20:01:16 +01004136 if (State & MODE_NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004137 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar24959102022-05-07 20:01:16 +01004138 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 ins_horscroll();
Bram Moolenaar24959102022-05-07 20:01:16 +01004140 else if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004142 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004144 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 redrawcmdline();
4146 }
4147 }
4148 if (old_leftcol != curwin->w_leftcol)
4149 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004150 updateWindow(curwin); // update window, status and cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 setcursor();
4152 }
4153#else
4154 bytes[0] = CSI;
4155 bytes[1] = KS_HOR_SCROLLBAR;
4156 bytes[2] = KE_FILLER;
4157 byte_count = 3;
4158#endif
4159 }
4160
4161#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 /*
4163 * synchronize other windows, as necessary according to 'scrollbind'
4164 */
4165 if (curwin->w_p_scb
4166 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4167 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004168# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 ))))
4172 {
4173 do_check_scrollbind(TRUE);
Bram Moolenaar30613902019-12-01 22:11:18 +01004174 // need to update the window right here
Bram Moolenaar29323592016-07-24 22:04:11 +02004175 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 if (wp->w_redr_type > 0)
4177 updateWindow(wp);
4178 setcursor();
4179 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004180 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004182 add_to_input_buf(bytes, byte_count);
4183 add_long_to_buf((long_u)value, bytes);
4184 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185#endif
4186}
4187
4188/*
4189 * Scrollbar stuff:
4190 */
4191
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004192/*
4193 * Called when something in the window layout has changed.
4194 */
4195 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004196gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004197{
4198 if (gui.in_use && starting == 0)
4199 {
4200 out_flush();
4201 gui_init_which_components(NULL);
4202 gui_update_scrollbars(TRUE);
4203 }
4204 need_mouse_correct = TRUE;
4205}
4206
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004208gui_update_scrollbars(
Bram Moolenaar30613902019-12-01 22:11:18 +01004209 int force) // Force all scrollbars to get updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
4211 win_T *wp;
4212 scrollbar_T *sb;
Bram Moolenaar30613902019-12-01 22:11:18 +01004213 long val, size, max; // need 32 bits here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 int which_sb;
4215 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
Bram Moolenaar30613902019-12-01 22:11:18 +01004218 // Update the horizontal scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 gui_update_horiz_scrollbar(force);
4220
Bram Moolenaar4f974752019-02-17 17:44:42 +01004221#ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01004222 // Return straight away if there is neither a left nor right scrollbar.
4223 // On MS-Windows this is required anyway for scrollwheel messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4225 return;
4226#endif
4227
4228 /*
4229 * Don't want to update a scrollbar while we're dragging it. But if we
4230 * have both a left and right scrollbar, and we drag one of them, we still
4231 * need to update the other one.
4232 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004233 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4234 && gui.which_scrollbars[SBAR_LEFT]
4235 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 {
4237 /*
4238 * If we have two scrollbars and one of them is being dragged, just
4239 * copy the scrollbar position from the dragged one to the other one.
4240 */
4241 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4242 if (gui.dragged_wp != NULL)
4243 gui_mch_set_scrollbar_thumb(
4244 &gui.dragged_wp->w_scrollbars[which_sb],
4245 gui.dragged_wp->w_scrollbars[0].value,
4246 gui.dragged_wp->w_scrollbars[0].size,
4247 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249
Bram Moolenaar30613902019-12-01 22:11:18 +01004250 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 ++hold_gui_events;
4252
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004253 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004255 if (wp->w_buffer == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 continue;
Bram Moolenaar30613902019-12-01 22:11:18 +01004257 // Skip a scrollbar that is being dragged.
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004258 if (!force && (gui.dragged_sb == SBAR_LEFT
4259 || gui.dragged_sb == SBAR_RIGHT)
4260 && gui.dragged_wp == wp)
4261 continue;
4262
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#ifdef SCROLL_PAST_END
4264 max = wp->w_buffer->b_ml.ml_line_count - 1;
4265#else
4266 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4267#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004268 if (max < 0) // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 max = 0;
4270 val = wp->w_topline - 1;
4271 size = wp->w_height;
4272#ifdef SCROLL_PAST_END
Bram Moolenaar30613902019-12-01 22:11:18 +01004273 if (val > max) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 val = max;
4275#else
Bram Moolenaar30613902019-12-01 22:11:18 +01004276 if (size > max + 1) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 size = max + 1;
4278 if (val > max - size + 1)
4279 val = max - size + 1;
4280#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004281 if (val < 0) // minimal value is 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 val = 0;
4283
4284 /*
4285 * Scrollbar at index 0 (the left one) contains all the information.
4286 * It would be the same info for left and right so we just store it for
4287 * one of them.
4288 */
4289 sb = &wp->w_scrollbars[0];
4290
4291 /*
4292 * Note: no check for valid w_botline. If it's not valid the
4293 * scrollbars will be updated later anyway.
4294 */
4295 if (size < 1 || wp->w_botline - 2 > max)
4296 {
4297 /*
4298 * This can happen during changing files. Just don't update the
4299 * scrollbar for now.
4300 */
Bram Moolenaar30613902019-12-01 22:11:18 +01004301 sb->height = 0; // Force update next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 if (gui.which_scrollbars[SBAR_LEFT])
4303 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4304 if (gui.which_scrollbars[SBAR_RIGHT])
4305 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4306 continue;
4307 }
4308 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 || sb->top != wp->w_winrow
4310 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004312 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004314 // Height, width or position of scrollbar has changed. For
4315 // vertical split: curwin changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 sb->top = wp->w_winrow;
4318 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
Bram Moolenaar30613902019-12-01 22:11:18 +01004321 // Calculate height and position in pixels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 h = (sb->height + sb->status_height) * gui.char_height;
4323 y = sb->top * gui.char_height + gui.border_offset;
4324#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4325 if (gui.menu_is_active)
4326 y += gui.menu_height;
4327#endif
4328
Bram Moolenaar0b962e52022-04-03 18:02:37 +01004329#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004330 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 y += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333#endif
4334
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004335#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004336 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004337 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004338#endif
4339
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004342 // Height of top scrollbar includes width of top border
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 h += gui.border_offset;
4344 y -= gui.border_offset;
4345 }
4346 if (gui.which_scrollbars[SBAR_LEFT])
4347 {
4348 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4349 gui.left_sbar_x, y,
4350 gui.scrollbar_width, h);
4351 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4352 }
4353 if (gui.which_scrollbars[SBAR_RIGHT])
4354 {
4355 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4356 gui.right_sbar_x, y,
4357 gui.scrollbar_width, h);
4358 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4359 }
4360 }
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 if (force || sb->value != val || sb->size != size || sb->max != max)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004364 // Thumb of scrollbar has moved
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 sb->value = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 sb->size = size;
4367 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004368 if (gui.which_scrollbars[SBAR_LEFT]
4369 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4371 val, size, max);
4372 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004373 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4375 val, size, max);
4376 }
4377 }
Christian Brabandt2e0f3ec2021-11-28 18:41:05 +00004378
4379 // update the title, it may show the scroll position
4380 maketitle();
4381
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 --hold_gui_events;
4384}
4385
4386/*
4387 * Enable or disable a scrollbar.
4388 * Check for scrollbars for vertically split windows which are not enabled
4389 * sometimes.
4390 */
4391 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004392gui_do_scrollbar(
4393 win_T *wp,
Bram Moolenaar30613902019-12-01 22:11:18 +01004394 int which, // SBAR_LEFT or SBAR_RIGHT
4395 int enable) // TRUE to enable scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 int midcol = curwin->w_wincol + curwin->w_width / 2;
4398 int has_midcol = (wp->w_wincol <= midcol
4399 && wp->w_wincol + wp->w_width >= midcol);
4400
Bram Moolenaar30613902019-12-01 22:11:18 +01004401 // Only enable scrollbars that contain the middle column of the current
4402 // window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4404 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004405 // Scrollbars only on one side. Don't enable scrollbars that don't
4406 // contain the middle column of the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 if (!has_midcol)
4408 enable = FALSE;
4409 }
4410 else
4411 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004412 // Scrollbars on both sides. Don't enable scrollbars that neither
4413 // contain the middle column of the current window nor are on the far
4414 // side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 if (midcol > Columns / 2)
4416 {
4417 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4418 enable = FALSE;
4419 }
4420 else
4421 {
4422 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4423 : !has_midcol)
4424 enable = FALSE;
4425 }
4426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4428}
4429
4430/*
4431 * Scroll a window according to the values set in the globals current_scrollbar
4432 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4433 * or FALSE otherwise.
4434 */
4435 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004436gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
4438 win_T *wp, *save_wp;
4439 int i;
4440 long nlines;
4441 pos_T old_cursor;
4442 linenr_T old_topline;
4443#ifdef FEAT_DIFF
4444 int old_topfill;
4445#endif
4446
4447 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4448 if (wp == NULL)
4449 break;
4450 if (wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004451 // Couldn't find window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 return FALSE;
4453
4454 /*
4455 * Compute number of lines to scroll. If zero, nothing to do.
4456 */
4457 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4458 if (nlines == 0)
4459 return FALSE;
4460
4461 save_wp = curwin;
4462 old_topline = wp->w_topline;
4463#ifdef FEAT_DIFF
4464 old_topfill = wp->w_topfill;
4465#endif
4466 old_cursor = wp->w_cursor;
4467 curwin = wp;
4468 curbuf = wp->w_buffer;
4469 if (nlines < 0)
4470 scrolldown(-nlines, gui.dragged_wp == NULL);
4471 else
4472 scrollup(nlines, gui.dragged_wp == NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004473 // Reset dragged_wp after using it. "dragged_sb" will have been reset for
4474 // the mouse-up event already, but we still want it to behave like when
4475 // dragging. But not the next click in an arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 if (gui.dragged_sb == SBAR_NONE)
4477 gui.dragged_wp = NULL;
4478
4479 if (old_topline != wp->w_topline
4480#ifdef FEAT_DIFF
4481 || old_topfill != wp->w_topfill
4482#endif
4483 )
4484 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004485 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004487 cursor_correct(); // fix window for 'so'
4488 update_topline(); // avoid up/down jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490 if (old_cursor.lnum != wp->w_cursor.lnum)
4491 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494
Bram Moolenaar30613902019-12-01 22:11:18 +01004495 // Make sure wp->w_leftcol and wp->w_skipcol are correct.
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004496 validate_cursor();
4497
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 curwin = save_wp;
4499 curbuf = save_wp->w_buffer;
4500
4501 /*
4502 * Don't call updateWindow() when nothing has changed (it will overwrite
4503 * the status line!).
4504 */
4505 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004506 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507#ifdef FEAT_DIFF
4508 || old_topfill != wp->w_topfill
4509#endif
4510 )
4511 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004512 int type = VALID;
4513
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004514 if (pum_visible())
4515 {
4516 type = NOT_VALID;
4517 wp->w_lines_valid = 0;
4518 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004519
Bram Moolenaar30613902019-12-01 22:11:18 +01004520 // Don't set must_redraw here, it may cause the popup menu to
4521 // disappear when losing focus after a scrollbar drag.
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004522 if (wp->w_redr_type < type)
4523 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004524 mch_disable_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01004525 updateWindow(wp); // update window, status line, and cmdline
Bram Moolenaara338adc2018-01-31 20:51:47 +01004526 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528
Bram Moolenaar30613902019-12-01 22:11:18 +01004529 // May need to redraw the popup menu.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004530 if (pum_visible())
4531 pum_redraw();
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004532
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004533 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534}
4535
4536
4537/*
4538 * Horizontal scrollbar stuff:
4539 */
4540
4541/*
4542 * Return length of line "lnum" for horizontal scrolling.
4543 */
4544 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004545scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
4547 char_u *p;
4548 colnr_T col;
4549 int w;
4550
4551 p = ml_get(lnum);
4552 col = 0;
4553 if (*p != NUL)
4554 for (;;)
4555 {
4556 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004557 MB_PTR_ADV(p);
Bram Moolenaar30613902019-12-01 22:11:18 +01004558 if (*p == NUL) // don't count the last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 break;
4560 col += w;
4561 }
4562 return col;
4563}
4564
Bram Moolenaar30613902019-12-01 22:11:18 +01004565// Remember which line is currently the longest, so that we don't have to
4566// search for it when scrolling horizontally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567static linenr_T longest_lnum = 0;
4568
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004569/*
4570 * Find longest visible line number. If this is not possible (or not desired,
4571 * by setting 'h' in "guioptions") then the current line number is returned.
4572 */
4573 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004574gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004575{
4576 linenr_T ret = 0;
4577
Bram Moolenaar30613902019-12-01 22:11:18 +01004578 // Calculate maximum for horizontal scrollbar. Check for reasonable
4579 // line numbers, topline and botline can be invalid when displaying is
4580 // postponed.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004581 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4582 && curwin->w_topline <= curwin->w_cursor.lnum
4583 && curwin->w_botline > curwin->w_cursor.lnum
4584 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4585 {
4586 linenr_T lnum;
4587 colnr_T n;
4588 long max = 0;
4589
Bram Moolenaar30613902019-12-01 22:11:18 +01004590 // Use maximum of all visible lines. Remember the lnum of the
4591 // longest line, closest to the cursor line. Used when scrolling
4592 // below.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004593 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4594 {
4595 n = scroll_line_len(lnum);
4596 if (n > (colnr_T)max)
4597 {
4598 max = n;
4599 ret = lnum;
4600 }
4601 else if (n == (colnr_T)max
4602 && abs((int)(lnum - curwin->w_cursor.lnum))
4603 < abs((int)(ret - curwin->w_cursor.lnum)))
4604 ret = lnum;
4605 }
4606 }
4607 else
Bram Moolenaar30613902019-12-01 22:11:18 +01004608 // Use cursor line only.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004609 ret = curwin->w_cursor.lnum;
4610
4611 return ret;
4612}
4613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004615gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616{
Bram Moolenaar30613902019-12-01 22:11:18 +01004617 long value, size, max; // need 32 bit ints here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619 if (!gui.which_scrollbars[SBAR_BOTTOM])
4620 return;
4621
4622 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4623 return;
4624
4625 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4626 return;
4627
4628 /*
4629 * It is possible for the cursor to be invalid if we're in the middle of
4630 * something (like changing files). If so, don't do anything for now.
4631 */
4632 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4633 {
4634 gui.bottom_sbar.value = -1;
4635 return;
4636 }
4637
Bram Moolenaar02631462017-09-22 15:20:32 +02004638 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (curwin->w_p_wrap)
4640 {
4641 value = 0;
4642#ifdef SCROLL_PAST_END
4643 max = 0;
4644#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004645 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646#endif
4647 }
4648 else
4649 {
4650 value = curwin->w_leftcol;
4651
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004652 longest_lnum = gui_find_longest_lnum();
4653 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (virtual_active())
4656 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004657 // May move the cursor even further to the right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (curwin->w_virtcol >= (colnr_T)max)
4659 max = curwin->w_virtcol;
4660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004663 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004665 // The line number isn't scrolled, thus there is less space when
4666 // 'number' or 'relativenumber' is set (also for 'foldcolumn').
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 size -= curwin_col_off();
4668#ifndef SCROLL_PAST_END
4669 max -= curwin_col_off();
4670#endif
4671 }
4672
4673#ifndef SCROLL_PAST_END
4674 if (value > max - size + 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01004675 value = max - size + 1; // limit the value to allowable range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676#endif
4677
4678#ifdef FEAT_RIGHTLEFT
4679 if (curwin->w_p_rl)
4680 {
4681 value = max + 1 - size - value;
4682 if (value < 0)
4683 {
4684 size += value;
4685 value = 0;
4686 }
4687 }
4688#endif
4689 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4690 && max == gui.bottom_sbar.max)
4691 return;
4692
4693 gui.bottom_sbar.value = value;
4694 gui.bottom_sbar.size = size;
4695 gui.bottom_sbar.max = max;
4696 gui.prev_wrap = curwin->w_p_wrap;
4697
4698 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4699}
4700
4701/*
4702 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4703 */
4704 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004705gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
Bram Moolenaar30613902019-12-01 22:11:18 +01004707 // no wrapping, no scrolling
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 if (curwin->w_p_wrap)
4709 return FALSE;
4710
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004711 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 return FALSE;
4713
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004714 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715
Bram Moolenaar30613902019-12-01 22:11:18 +01004716 // When the line of the cursor is too short, move the cursor to the
4717 // longest visible line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004719 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004720 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004722 if (compute_longest_lnum)
4723 {
4724 curwin->w_cursor.lnum = gui_find_longest_lnum();
4725 curwin->w_cursor.col = 0;
4726 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004727 // Do a sanity check on "longest_lnum", just in case.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004728 else if (longest_lnum >= curwin->w_topline
4729 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
4731 curwin->w_cursor.lnum = longest_lnum;
4732 curwin->w_cursor.col = 0;
4733 }
4734 }
4735
4736 return leftcol_changed();
4737}
4738
4739/*
4740 * Check that none of the colors are the same as the background color
4741 */
4742 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004743gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744{
4745 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4746 {
4747 gui_set_bg_color((char_u *)"White");
4748 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4749 gui_set_fg_color((char_u *)"Black");
4750 }
4751}
4752
Bram Moolenaar3918c952005-03-15 22:34:55 +00004753 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004754gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
4756 gui.norm_pixel = gui_get_color(name);
4757 hl_set_fg_color_name(vim_strsave(name));
4758}
4759
Bram Moolenaar3918c952005-03-15 22:34:55 +00004760 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004761gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
4763 gui.back_pixel = gui_get_color(name);
4764 hl_set_bg_color_name(vim_strsave(name));
4765}
4766
4767/*
4768 * Allocate a color by name.
4769 * Returns INVALCOLOR and gives an error message when failed.
4770 */
4771 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004772gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
4774 guicolor_T t;
4775
4776 if (*name == NUL)
4777 return INVALCOLOR;
4778 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004779
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004781#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 && gui.in_use
4783#endif
4784 )
Drew Vogele30d1022021-10-24 20:35:07 +01004785 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 return t;
4787}
4788
4789/*
4790 * Return the grey value of a color (range 0-255).
4791 */
4792 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004793gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004795 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004797 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004798 + (((rgb >> 8) & 0xff) * 587)
4799 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800}
4801
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004802 char_u *
4803gui_bg_default(void)
4804{
4805 if (gui_get_lightness(gui.back_pixel) < 127)
4806 return (char_u *)"dark";
4807 return (char_u *)"light";
4808}
4809
4810/*
4811 * Option initializations that can only be done after opening the GUI window.
4812 */
4813 void
4814init_gui_options(void)
4815{
Bram Moolenaar30613902019-12-01 22:11:18 +01004816 // Set the 'background' option according to the lightness of the
4817 // background color, unless the user has set it already.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004818 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
4819 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004820 set_option_value_give_err((char_u *)"bg", 0L, gui_bg_default(), 0);
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004821 highlight_changed();
4822 }
4823}
4824
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825#if defined(FEAT_GUI_X11) || defined(PROTO)
4826 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004827gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828{
4829 win_T *wp;
4830
Bram Moolenaar30613902019-12-01 22:11:18 +01004831 // Nothing to do if GUI hasn't started yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 if (!gui.in_use)
4833 return;
4834
4835 FOR_ALL_WINDOWS(wp)
4836 {
4837 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4838 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4839 }
4840 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4841}
4842#endif
4843
4844/*
4845 * Call this when focus has changed.
4846 */
4847 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004848gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
4850/*
4851 * Skip this code to avoid drawing the cursor when debugging and switching
4852 * between the debugger window and gvim.
4853 */
4854#if 1
4855 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004856 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857
4858# ifdef FEAT_XIM
4859 xim_set_focus(in_focus);
4860# endif
4861
Bram Moolenaar30613902019-12-01 22:11:18 +01004862 // Put events in the input queue only when allowed.
4863 // ui_focus_change() isn't called directly, because it invokes
4864 // autocommands and that must not happen asynchronously.
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004865 if (!hold_gui_events)
4866 {
4867 char_u bytes[3];
4868
4869 bytes[0] = CSI;
4870 bytes[1] = KS_EXTRA;
4871 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4872 add_to_input_buf(bytes, 3);
4873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874#endif
4875}
4876
4877/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004878 * When mouse moved: apply 'mousefocus'.
4879 * Also updates the mouse pointer shape.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 */
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004881 static void
4882gui_mouse_focus(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
4884 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004885 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01004888 // Get window pointer, and update mouse shape as well.
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004889 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890#endif
4891
Bram Moolenaar30613902019-12-01 22:11:18 +01004892 // Only handle this when 'mousefocus' set and ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 if (p_mousef
Bram Moolenaar30613902019-12-01 22:11:18 +01004894 && !hold_gui_events // not holding events
Bram Moolenaar24959102022-05-07 20:01:16 +01004895 && (State & (MODE_NORMAL | MODE_INSERT))// Normal/Visual/Insert mode
4896 && State != MODE_HITRETURN // but not hit-return prompt
Bram Moolenaar30613902019-12-01 22:11:18 +01004897 && msg_scrolled == 0 // no scrolled message
4898 && !need_mouse_correct // not moving the pointer
4899 && gui.in_focus) // gvim in focus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004901 // Don't move the mouse when it's left or right of the Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 if (x < 0 || x > Columns * gui.char_width)
4903 return;
4904#ifndef FEAT_MOUSESHAPE
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004905 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906#endif
4907 if (wp == curwin || wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004908 return; // still in the same old window, or none at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
Bram Moolenaar30613902019-12-01 22:11:18 +01004910 // Ignore position in the tab pages line.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004911 if (Y_2_ROW(y) < tabline_height())
4912 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004913
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 /*
4915 * format a mouse click on status line input
4916 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004917 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4918 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 */
4920 if (finish_op)
4921 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004922 // abort the current operator first
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 st[0] = ESC;
4924 add_to_input_buf(st, 1);
4925 }
4926 st[0] = CSI;
4927 st[1] = KS_MOUSE;
4928 st[2] = KE_FILLER;
4929 st[3] = (char_u)MOUSE_LEFT;
4930 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004931 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 wp->w_height + W_WINROW(wp));
4933
4934 add_to_input_buf(st, 8);
4935 st[3] = (char_u)MOUSE_RELEASE;
4936 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004938 // Need to wake up the main loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (gtk_main_level() > 0)
4940 gtk_main_quit();
4941#endif
4942 }
4943}
4944
4945/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004946 * Called when the mouse moved (but not when dragging).
4947 */
4948 void
4949gui_mouse_moved(int x, int y)
4950{
4951 // Ignore this while still starting up.
4952 if (!gui.in_use || gui.starting)
4953 return;
4954
4955 // apply 'mousefocus' and pointer shape
4956 gui_mouse_focus(x, y);
4957
Ernie Raelc4cb5442022-04-03 15:47:28 +01004958 if (p_mousemev
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004959#ifdef FEAT_PROP_POPUP
Ernie Raelc4cb5442022-04-03 15:47:28 +01004960 || popup_uses_mouse_move
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004961#endif
Ernie Raelc4cb5442022-04-03 15:47:28 +01004962 )
4963 // Generate a mouse-moved event. For a <MouseMove> mapping. Or so the
4964 // popup can perhaps be closed, just like in the terminal.
4965 gui_send_mouse_event(MOUSE_MOVE, x, y, FALSE, 0);
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004966}
4967
4968/*
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004969 * Get the window where the mouse pointer is on.
4970 * Returns NULL if not found.
4971 */
4972 win_T *
4973gui_mouse_window(mouse_find_T popup)
4974{
4975 int x, y;
4976
4977 if (!(gui.in_use && (p_mousef || popup == FIND_POPUP)))
4978 return NULL;
4979 gui_mch_getmouse(&x, &y);
4980
4981 // Only use the mouse when it's on the Vim window
4982 if (x >= 0 && x <= Columns * gui.char_width
4983 && y >= 0 && Y_2_ROW(y) >= tabline_height())
4984 return xy2win(x, y, popup);
4985 return NULL;
4986}
4987
4988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 * Called when mouse should be moved to window with focus.
4990 */
4991 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004992gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 win_T *wp = NULL;
4995
4996 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004997
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004998 wp = gui_mouse_window(IGNORE_POPUP);
Bram Moolenaar30613902019-12-01 22:11:18 +01004999 if (wp != curwin && wp != NULL) // If in other than current window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00005001 validate_cline_row();
5002 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
5003 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006}
5007
5008/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005009 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01005010 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 static win_T *
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005013xy2win(int x, int y, mouse_find_T popup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 int row;
5016 int col;
5017 win_T *wp;
5018
5019 row = Y_2_ROW(y);
5020 col = X_2_COL(x);
Bram Moolenaar30613902019-12-01 22:11:18 +01005021 if (row < 0 || col < 0) // before first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 return NULL;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01005023 wp = mouse_find_win(&row, &col, popup);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005024 if (wp == NULL)
5025 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005026#ifdef FEAT_MOUSESHAPE
Bram Moolenaar24959102022-05-07 20:01:16 +01005027 if (State == MODE_HITRETURN || State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
5029 if (Y_2_ROW(y) >= msg_row)
5030 update_mouseshape(SHAPE_IDX_MOREL);
5031 else
5032 update_mouseshape(SHAPE_IDX_MORE);
5033 }
Bram Moolenaar30613902019-12-01 22:11:18 +01005034 else if (row > wp->w_height) // below status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaar24959102022-05-07 20:01:16 +01005036 else if (!(State & MODE_CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005037 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaar24959102022-05-07 20:01:16 +01005039 else if (!(State & MODE_CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005040 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 update_mouseshape(SHAPE_IDX_STATUS);
5042 else
5043 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005045 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046}
5047
5048/*
5049 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
5050 * File names may be given to redefine the args list.
5051 */
5052 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005053ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054{
5055 char_u *arg = eap->arg;
5056
5057 /*
5058 * Check for "-f" argument: foreground, don't fork.
5059 * Also don't fork when started with "gvim -f".
5060 * Do fork when using "gui -b".
5061 */
5062 if (arg[0] == '-'
5063 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01005064 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 gui.dofork = (arg[1] == 'b');
5067 eap->arg = skipwhite(eap->arg + 2);
5068 }
5069 if (!gui.in_use)
5070 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005071#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaar257a3962019-12-29 15:19:03 +01005072 if (!gui.starting)
5073 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005074 emsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaar257a3962019-12-29 15:19:03 +01005075 return;
5076 }
5077#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005078 // Clear the command. Needed for when forking+exiting, to avoid part
5079 // of the argument ending up after the shell prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 msg_clr_eos_force();
Bram Moolenaar257a3962019-12-29 15:19:03 +01005081#ifdef GUI_MAY_SPAWN
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02005082 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005083 gui_start(eap->arg);
5084 else
Bram Moolenaar257a3962019-12-29 15:19:03 +01005085#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005086 gui_start(NULL);
Bram Moolenaar257a3962019-12-29 15:19:03 +01005087#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01005088 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02005089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02005091 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 ex_next(eap);
5093}
5094
Bram Moolenaar4f974752019-02-17 17:44:42 +01005095#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01005096 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON) \
5097 || defined(FEAT_GUI_HAIKU)) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01005098 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099/*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01005100 * This is shared between Haiku, Motif, and GTK.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102
5103/*
5104 * Callback function for do_in_runtimepath().
5105 */
5106 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005107gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005109 char_u *gfp_buffer = cookie;
5110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 if (STRLEN(fname) >= MAXPATHL)
5112 *gfp_buffer = NUL;
5113 else
5114 STRCPY(gfp_buffer, fname);
5115}
5116
5117/*
5118 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5119 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5120 */
5121 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005122gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123{
5124 if (STRLEN(name) > MAXPATHL - 14)
5125 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005126 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005127 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005128 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 return FAIL;
5130 return OK;
5131}
5132
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005133# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134/*
5135 * Given the name of the "icon=" argument, try finding the bitmap file for the
5136 * icon. If it is an absolute path name, use it as it is. Otherwise append
5137 * "ext" and search for it in 'runtimepath'.
5138 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5139 * contains "name".
5140 */
5141 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005142gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143{
5144 char_u buf[MAXPATHL + 1];
5145
5146 expand_env(name, buffer, MAXPATHL);
5147 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5148 STRCPY(buffer, buf);
5149}
5150# endif
5151#endif
5152
Bram Moolenaar9e175142020-04-30 22:51:01 +02005153#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)|| defined(FEAT_GUI_HAIKU) \
5154 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005156display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157{
5158 char_u *p;
5159
5160 if (isatty(2))
5161 fflush(stderr);
5162 else if (error_ga.ga_data != NULL)
5163 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005164 // avoid putting up a message box with blanks only
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5166 if (!isspace(*p))
5167 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005168 // Truncate a very long message, it will go off-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (STRLEN(p) > 2000)
5170 STRCPY(p + 2000 - 14, "...(truncated)");
5171 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005172 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174 }
5175 ga_clear(&error_ga);
5176 }
5177}
5178#endif
5179
5180#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5181/*
5182 * Return TRUE if still starting up and there is no place to enter text.
5183 * For GTK and X11 we check if stderr is not a tty, which means we were
5184 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5185 * allow typing on stdin.
5186 */
5187 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005188no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189{
5190 return ((!gui.in_use || gui.starting)
5191# ifndef NO_CONSOLE
5192 && !isatty(0) && !isatty(2)
5193# endif
5194 );
5195}
5196#endif
5197
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005198#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005199 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005200 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201/*
5202 * Update the current window and the screen.
5203 */
5204 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005205gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005207# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005208 linenr_T conceal_old_cursor_line = 0;
5209 linenr_T conceal_new_cursor_line = 0;
5210 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005211# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 update_topline();
5214 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005215
Bram Moolenaar30613902019-12-01 22:11:18 +01005216 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005217 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005218# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005219 || popup_visible
5220# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005221# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005222 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005223# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005224 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005225 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005226 if (has_cursormoved())
5227 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
K.Takata6e1d31e2022-02-03 13:05:32 +00005228# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005229 if (popup_visible)
5230 popup_check_cursor_pos();
K.Takata6e1d31e2022-02-03 13:05:32 +00005231# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005232# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005233 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005234 {
5235 conceal_old_cursor_line = last_cursormoved.lnum;
5236 conceal_new_cursor_line = curwin->w_cursor.lnum;
5237 conceal_update_lines = TRUE;
5238 }
5239# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005240 last_cursormoved = curwin->w_cursor;
5241 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005242
LemonBoy09371822022-04-08 15:18:45 +01005243 if (!finish_op)
zeertzjqd58862d2022-04-12 11:32:48 +01005244 may_trigger_winscrolled();
LemonBoy09371822022-04-08 15:18:45 +01005245
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005246# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005247 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005248 && (conceal_old_cursor_line != conceal_new_cursor_line
5249 || conceal_cursor_line(curwin)
5250 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005251 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005252 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005253 redrawWinline(curwin, conceal_old_cursor_line);
5254 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005255 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005256 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005257 }
5258# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005259 update_screen(0); // may need to update the screen
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005260 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005261 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262}
5263#endif
5264
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005265#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266/*
5267 * Get the text to use in a find/replace dialog. Uses the last search pattern
5268 * if the argument is empty.
5269 * Returns an allocated string.
5270 */
5271 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005272get_find_dialog_text(
5273 char_u *arg,
Bram Moolenaar30613902019-12-01 22:11:18 +01005274 int *wwordp, // return: TRUE if \< \> found
5275 int *mcasep) // return: TRUE if \C found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276{
5277 char_u *text;
5278
5279 if (*arg == NUL)
5280 text = last_search_pat();
5281 else
5282 text = arg;
5283 if (text != NULL)
5284 {
5285 text = vim_strsave(text);
5286 if (text != NULL)
5287 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005288 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 int i;
5290
Bram Moolenaar30613902019-12-01 22:11:18 +01005291 // Remove "\V"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5293 {
5294 mch_memmove(text, text + 2, (size_t)(len - 1));
5295 len -= 2;
5296 }
5297
Bram Moolenaar30613902019-12-01 22:11:18 +01005298 // Recognize "\c" and "\C" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5300 {
5301 *mcasep = (text[1] == 'C');
5302 mch_memmove(text, text + 2, (size_t)(len - 1));
5303 len -= 2;
5304 }
5305
Bram Moolenaar30613902019-12-01 22:11:18 +01005306 // Recognize "\<text\>" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 if (len >= 4
5308 && STRNCMP(text, "\\<", 2) == 0
5309 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5310 {
5311 *wwordp = TRUE;
5312 mch_memmove(text, text + 2, (size_t)(len - 4));
5313 text[len - 4] = NUL;
5314 }
5315
Bram Moolenaar30613902019-12-01 22:11:18 +01005316 // Recognize "\/" or "\?" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 for (i = 0; i + 1 < len; ++i)
5318 if (text[i] == '\\' && (text[i + 1] == '/'
5319 || text[i + 1] == '?'))
5320 {
5321 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5322 --len;
5323 }
5324 }
5325 }
5326 return text;
5327}
5328
5329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 * Handle the press of a button in the find-replace dialog.
5331 * Return TRUE when something was added to the input buffer.
5332 */
5333 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005334gui_do_findrepl(
Bram Moolenaar30613902019-12-01 22:11:18 +01005335 int flags, // one of FRD_REPLACE, FRD_FINDNEXT, etc.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005336 char_u *find_text,
5337 char_u *repl_text,
Bram Moolenaar30613902019-12-01 22:11:18 +01005338 int down) // Search downwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
5340 garray_T ga;
5341 int i;
5342 int type = (flags & FRD_TYPE_MASK);
5343 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005344 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005345 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005346 static int busy = FALSE;
5347
Bram Moolenaar30613902019-12-01 22:11:18 +01005348 // When the screen is being updated we should not change buffers and
5349 // windows structures, it may cause freed memory to be used. Also don't
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005350 // do this recursively (pressing "Find" quickly several times).
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005351 if (updating_screen || busy)
5352 return FALSE;
5353
Bram Moolenaar30613902019-12-01 22:11:18 +01005354 // refuse replace when text cannot be changed
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005355 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5356 return FALSE;
5357
5358 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
5360 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005361 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 ga_concat(&ga, (char_u *)"%s/");
5363
5364 ga_concat(&ga, (char_u *)"\\V");
5365 if (flags & FRD_MATCH_CASE)
5366 ga_concat(&ga, (char_u *)"\\C");
5367 else
5368 ga_concat(&ga, (char_u *)"\\c");
5369 if (flags & FRD_WHOLE_WORD)
5370 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar30613902019-12-01 22:11:18 +01005371 // escape slash and backslash
Bram Moolenaar518bc172018-05-13 17:05:30 +02005372 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5373 if (p != NULL)
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005374 ga_concat(&ga, p);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005375 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 if (flags & FRD_WHOLE_WORD)
5377 ga_concat(&ga, (char_u *)"\\>");
5378
5379 if (type == FRD_REPLACEALL)
5380 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar30613902019-12-01 22:11:18 +01005382 // escape slash and backslash
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005383 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5384 if (p != NULL)
5385 ga_concat(&ga, p);
5386 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005388 }
5389 ga_append(&ga, NUL);
5390
5391 if (type == FRD_REPLACE)
5392 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005393 // Do the replacement when the text at the cursor matches. Thus no
5394 // replacement is done if the cursor was moved!
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005395 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5396 regmatch.rm_ic = 0;
5397 if (regmatch.regprog != NULL)
5398 {
5399 p = ml_get_cursor();
5400 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5401 && regmatch.startp[0] == p)
5402 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005403 // Clear the command line to remove any old "No match"
5404 // error.
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005405 msg_end_prompt();
5406
5407 if (u_save_cursor() == OK)
5408 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005409 // A button was pressed thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005410 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005411
5412 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005413 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005414 ins_str(repl_text);
5415 }
5416 }
5417 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005418 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005419 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005420 }
5421 }
5422
5423 if (type == FRD_REPLACEALL)
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 Moolenaar071d4272004-06-13 20:20:40 +00005427 do_cmdline_cmd(ga.ga_data);
5428 }
5429 else
5430 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005431 int searchflags = SEARCH_MSG + SEARCH_MARK;
5432
Bram Moolenaar30613902019-12-01 22:11:18 +01005433 // Search for the next match.
5434 // Don't skip text under cursor for single replace.
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005435 if (type == FRD_REPLACE)
5436 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005438 if (down)
5439 {
Bram Moolenaarc036e872020-02-21 21:30:52 +01005440 (void)do_search(NULL, '/', '/', ga.ga_data, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005441 }
5442 else
5443 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005444 // We need to escape '?' if and only if we are searching in the up
5445 // direction
Bram Moolenaar518bc172018-05-13 17:05:30 +02005446 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5447 if (p != NULL)
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005448 (void)do_search(NULL, '?', '?', p, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005449 vim_free(p);
5450 }
5451
Bram Moolenaar30613902019-12-01 22:11:18 +01005452 msg_scroll = i; // don't let an error message set msg_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 }
5454
Bram Moolenaar30613902019-12-01 22:11:18 +01005455 // Don't want to pass did_emsg to other code, it may cause disabling
5456 // syntax HL if we were busy redrawing.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005457 did_emsg = save_did_emsg;
5458
Bram Moolenaar24959102022-05-07 20:01:16 +01005459 if (State & (MODE_NORMAL | MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005461 gui_update_screen(); // update the screen
5462 msg_didout = 0; // overwrite any message
5463 need_wait_return = FALSE; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
5465
5466 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005467 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 return (ga.ga_len > 0);
5469}
5470
5471#endif
5472
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005473#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474/*
5475 * Jump to the window at specified point (x, y).
5476 */
5477 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005478gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
5480 int row = Y_2_ROW(y);
5481 int col = X_2_COL(x);
5482 win_T *wp;
5483
5484 if (row >= 0 && col >= 0)
5485 {
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005486 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 if (wp != NULL && wp != curwin)
5488 win_goto(wp);
5489 }
5490}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491
5492/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005493 * Function passed to handle_drop() for the actions to be done after the
5494 * argument list has been updated.
5495 */
5496 static void
5497drop_callback(void *cookie)
5498{
5499 char_u *p = cookie;
Bram Moolenaar4671e882021-11-22 17:21:48 +00005500 int do_shorten = FALSE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005501
Bram Moolenaar30613902019-12-01 22:11:18 +01005502 // If Shift held down, change to first file's directory. If the first
5503 // item is a directory, change to that directory (and let the explorer
5504 // plugin show the contents).
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005505 if (p != NULL)
5506 {
5507 if (mch_isdir(p))
5508 {
5509 if (mch_chdir((char *)p) == 0)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005510 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005511 }
5512 else if (vim_chdirfile(p, "drop") == OK)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005513 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005514 vim_free(p);
Bram Moolenaar4671e882021-11-22 17:21:48 +00005515 if (do_shorten)
5516 {
5517 shorten_fnames(TRUE);
5518 last_chdir_reason = "drop";
5519 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005520 }
5521
Bram Moolenaar30613902019-12-01 22:11:18 +01005522 // Update the screen display
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005523 update_screen(NOT_VALID);
5524# ifdef FEAT_MENU
5525 gui_update_menus(0);
5526# endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005527 maketitle();
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005528 setcursor();
5529 out_flush_cursor(FALSE, FALSE);
5530}
5531
5532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 * Process file drop. Mouse cursor position, key modifiers, name of files
5534 * and count of files are given. Argument "fnames[count]" has full pathnames
5535 * of dropped files, they will be freed in this function, and caller can't use
5536 * fnames after call this function.
5537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005539gui_handle_drop(
5540 int x UNUSED,
5541 int y UNUSED,
5542 int_u modifiers,
5543 char_u **fnames,
5544 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 int i;
5547 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005548 static int entered = FALSE;
5549
5550 /*
5551 * This function is called by event handlers. Just in case we get a
5552 * second event before the first one is handled, ignore the second one.
5553 * Not sure if this can ever happen, just in case.
5554 */
5555 if (entered)
5556 return;
5557 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
5559 /*
5560 * When the cursor is at the command line, add the file names to the
5561 * command line, don't edit the files.
5562 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005563 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 {
5565 shorten_filenames(fnames, count);
5566 for (i = 0; i < count; ++i)
5567 {
5568 if (fnames[i] != NULL)
5569 {
5570 if (i > 0)
5571 add_to_input_buf((char_u*)" ", 1);
5572
Bram Moolenaar30613902019-12-01 22:11:18 +01005573 // We don't know what command is used thus we can't be sure
5574 // about which characters need to be escaped. Only escape the
5575 // most common ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576# ifdef BACKSLASH_IN_FILENAME
5577 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5578# else
5579 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5580# endif
5581 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005582 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 vim_free(p);
5584 vim_free(fnames[i]);
5585 }
5586 }
5587 vim_free(fnames);
5588 }
5589 else
5590 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005591 // Go to the window under mouse cursor, then shorten given "fnames" by
5592 // current window, because a window can have local current dir.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 shorten_filenames(fnames, count);
5595
Bram Moolenaar30613902019-12-01 22:11:18 +01005596 // If Shift held down, remember the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if ((modifiers & MOUSE_SHIFT) != 0)
5598 p = vim_strsave(fnames[0]);
5599 else
5600 p = NULL;
5601
Bram Moolenaar30613902019-12-01 22:11:18 +01005602 // Handle the drop, :edit or :split to get to the file. This also
5603 // frees fnames[]. Skip this if there is only one item, it's a
5604 // directory and Shift is held down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5606 && mch_isdir(fnames[0]))
5607 {
5608 vim_free(fnames[0]);
5609 vim_free(fnames);
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02005610 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 }
5612 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005613 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
Bram Moolenaar4671e882021-11-22 17:21:48 +00005614 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005616
5617 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618}
5619#endif
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005620
5621/*
5622 * Check if "key" is to interrupt us. Handles a key that has not had modifiers
5623 * applied yet.
5624 * Return the key with modifiers applied if so, NUL if not.
5625 */
5626 int
5627check_for_interrupt(int key, int modifiers_arg)
5628{
5629 int modifiers = modifiers_arg;
5630 int c = merge_modifyOtherKeys(key, &modifiers);
5631
5632 if ((c == Ctrl_C && ctrl_c_interrupts)
Bram Moolenaaraf50e892020-08-01 13:22:10 +02005633#ifdef UNIX
5634 || (intr_char != Ctrl_C && c == intr_char)
5635#endif
5636 )
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005637 {
5638 got_int = TRUE;
5639 return c;
5640 }
5641 return NUL;
5642}
5643
Bram Moolenaar2d12c252022-06-13 21:42:45 +01005644/*
5645 * If the "--gui-log-file fname" argument is given write the dialog title and
5646 * message to a file and return TRUE. Otherwise return FALSE.
5647 * When there is any problem opening the file or writing to the file this is
5648 * ignored, showing the dialog might get the test to get stuck.
5649 */
5650 int
5651gui_dialog_log(char_u *title, char_u *message)
5652{
5653 char_u *fname = get_gui_dialog_file();
5654 FILE *fd;
5655
5656 if (fname == NULL)
5657 return FALSE;
5658
5659 fd = mch_fopen((char *)fname, "a");
5660 if (fd != NULL)
5661 {
5662 fprintf(fd, "%s: %s\n", title, message);
5663 fclose(fd);
5664 }
5665 return TRUE;
5666}