blob: 0b9e3289994e7aa8a27ac06bb367ecbc04816f3a [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);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static void init_gui_options(void);
Bram Moolenaar0630bb62019-11-04 22:52:12 +010036static win_T *xy2win(int x, int y, mouse_find_T popup);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020038#ifdef GUI_MAY_FORK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010039static void gui_do_fork(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020040
Bram Moolenaard25c16e2016-01-29 22:13:30 +010041static int gui_read_child_pipe(int fd);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020042
Bram Moolenaar30613902019-12-01 22:11:18 +010043// Return values for gui_read_child_pipe
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020044enum {
45 GUI_CHILD_IO_ERROR,
46 GUI_CHILD_OK,
47 GUI_CHILD_FAILED
48};
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020049#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020050
Bram Moolenaard25c16e2016-01-29 22:13:30 +010051static void gui_attempt_start(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020052
Bram Moolenaar30613902019-12-01 22:11:18 +010053static int can_update_cursor = TRUE; // can display the cursor
54static int disable_flush = 0; // If > 0, gui_mch_flush() is disabled.
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
56/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000057 * gui_start -- Called when user wants to start the GUI.
58 *
59 * Careful: This function can be called recursively when there is a ":gui"
60 * command in the .gvimrc file. Only the first call should fork, not the
61 * recursive call.
62 */
63 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +020064gui_start(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000065{
66 char_u *old_term;
Bram Moolenaara25f7182022-11-06 11:27:46 +000067#ifdef GUI_MAY_FORK
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 static int recursive = 0;
Bram Moolenaara25f7182022-11-06 11:27:46 +000069#endif
Bram Moolenaar68cbb142019-05-09 14:14:42 +020070#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +020071 char *msg = NULL;
72#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
74 old_term = vim_strsave(T_NAME);
75
Bram Moolenaar30613902019-12-01 22:11:18 +010076 settmode(TMODE_COOK); // stop RAW mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 if (full_screen)
Bram Moolenaar30613902019-12-01 22:11:18 +010078 cursor_on(); // needed for ":gui" in .vimrc
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 full_screen = FALSE;
80
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020081#ifdef GUI_MAY_FORK
Bram Moolenaara25f7182022-11-06 11:27:46 +000082 ++recursive;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020083 /*
84 * Quit the current process and continue in the child.
85 * Makes "gvim file" disconnect from the shell it was started in.
86 * Don't do this when Vim was started with "-f" or the 'f' flag is present
87 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020088 * Don't do this when there is a running job, we can only get the status
89 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020090 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020091 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
92# ifdef FEAT_JOB_CHANNEL
93 && !job_any_running()
94# endif
95 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020096 {
97 gui_do_fork();
98 }
99 else
100#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200101#ifdef GUI_MAY_SPAWN
102 if (gui.dospawn
103# ifdef EXPERIMENTAL_GUI_CMD
104 && gui.dofork
105# endif
106 && !vim_strchr(p_go, GO_FORG)
107 && !anyBufIsChanged()
108# ifdef FEAT_JOB_CHANNEL
109 && !job_any_running()
110# endif
111 )
112 {
Bram Moolenaar68cbb142019-05-09 14:14:42 +0200113# ifdef EXPERIMENTAL_GUI_CMD
114 msg =
115# endif
116 gui_mch_do_spawn(arg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200117 }
118 else
119#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200120 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200121#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100122 // If there is 'f' in 'guioptions' and specify -g argument,
123 // gui_mch_init_check() was not called yet.
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200124 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100125 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200126#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200127 gui_attempt_start();
128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar30613902019-12-01 22:11:18 +0100130 if (!gui.in_use) // failed to start GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100132 // Back to old term settings
133 //
134 // FIXME: If we got here because a child process failed and flagged to
Bram Moolenaar651fca82021-11-29 20:39:38 +0000135 // the parent to resume, and X11 is enabled, this will
Bram Moolenaar30613902019-12-01 22:11:18 +0100136 // hit an X11 I/O error and do a longjmp(), leaving recursive
137 // permanently set to 1. This is probably not as big a problem as it
138 // sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
139 // return "OK" unconditionally, so it would be very difficult to
140 // actually hit this case.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200141 termcapinit(old_term);
Bram Moolenaar30613902019-12-01 22:11:18 +0100142 settmode(TMODE_RAW); // restart RAW mode
Bram Moolenaar30613902019-12-01 22:11:18 +0100143 set_title_defaults(); // set 'title' and 'icon' again
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200144#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaar097c5372023-05-24 21:02:24 +0100145 if (msg != NULL)
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200146 emsg(msg);
147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 }
149
150 vim_free(old_term);
151
Bram Moolenaar30613902019-12-01 22:11:18 +0100152 // If the GUI started successfully, trigger the GUIEnter event, otherwise
153 // the GUIFailed event.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200154 gui_mch_update();
155 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
156 NULL, NULL, FALSE, curbuf);
Bram Moolenaara25f7182022-11-06 11:27:46 +0000157#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200158 --recursive;
Bram Moolenaara25f7182022-11-06 11:27:46 +0000159#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200160}
161
162/*
163 * Set_termname() will call gui_init() to start the GUI.
164 * Set the "starting" flag, to indicate that the GUI will start.
165 *
166 * We don't want to open the GUI shell until after we've read .gvimrc,
167 * otherwise we don't know what font we will use, and hence we don't know
168 * what size the shell should be. So if there are errors in the .gvimrc
169 * file, they will have to go to the terminal: Set full_screen to FALSE.
170 * full_screen will be set to TRUE again by a successful termcapinit().
171 */
172 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100173gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200174{
175 static int recursive = 0;
176
177 ++recursive;
178 gui.starting = TRUE;
179
180#ifdef FEAT_GUI_GTK
181 gui.event_time = GDK_CURRENT_TIME;
182#endif
183
184 termcapinit((char_u *)"builtin_gui");
185 gui.starting = recursive - 1;
186
Bram Moolenaar9372a112005-12-06 19:59:18 +0000187#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200189 {
190# ifdef FEAT_EVAL
191 Window x11_window;
192 Display *x11_display;
193
194 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
195 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
196# endif
197
Bram Moolenaar30613902019-12-01 22:11:18 +0100198 // Display error messages in a dialog now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 --recursive;
203}
204
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200205#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200206
Bram Moolenaar30613902019-12-01 22:11:18 +0100207// for waitpid()
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200208# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
209# include <sys/wait.h>
210# endif
211
212/*
213 * Create a new process, by forking. In the child, start the GUI, and in
214 * the parent, exit.
215 *
216 * If something goes wrong, this will return with gui.in_use still set
217 * to FALSE, in which case the caller should continue execution without
218 * the GUI.
219 *
220 * If the child fails to start the GUI, then the child will exit and the
221 * parent will return. If the child succeeds, then the parent will exit
222 * and the child will return.
223 */
224 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100225gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200226{
Bram Moolenaar30613902019-12-01 22:11:18 +0100227 int pipefd[2]; // pipe between parent and child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200228 int pipe_error;
229 int status;
230 int exit_status;
231 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200232
Bram Moolenaar08210f82023-04-13 19:15:54 +0100233# if defined(FEAT_RELTIME) && defined(PROF_NSEC)
Bram Moolenaarc72e31d2022-06-16 18:47:20 +0100234 // a timer is not carried forward
235 delete_timer();
K.Takata161b6ac2022-11-14 15:31:07 +0000236# endif
Bram Moolenaarc72e31d2022-06-16 18:47:20 +0100237
Bram Moolenaar30613902019-12-01 22:11:18 +0100238 // Setup a pipe between the child and the parent, so that the parent
239 // knows when the child has done the setsid() call and is allowed to
240 // exit.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200241 pipe_error = (pipe(pipefd) < 0);
242 pid = fork();
Bram Moolenaar30613902019-12-01 22:11:18 +0100243 if (pid < 0) // Fork error
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200244 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000245 emsg(_(e_failed_to_create_new_process_for_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200246 return;
247 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100248 else if (pid > 0) // Parent
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200249 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100250 // Give the child some time to do the setsid(), otherwise the
251 // exit() may kill the child too (when starting gvim from inside a
252 // gvim).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200253 if (!pipe_error)
254 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100255 // The read returns when the child closes the pipe (or when
256 // the child dies for some reason).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200257 close(pipefd[1]);
258 status = gui_read_child_pipe(pipefd[0]);
259 if (status == GUI_CHILD_FAILED)
260 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100261 // The child failed to start the GUI, so the caller must
262 // continue. There may be more error information written
263 // to stderr by the child.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200264# ifdef __NeXT__
265 wait4(pid, &exit_status, 0, (struct rusage *)0);
266# else
267 waitpid(pid, &exit_status, 0);
268# endif
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000269 emsg(_(e_the_child_process_failed_to_start_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200270 return;
271 }
272 else if (status == GUI_CHILD_IO_ERROR)
273 {
274 pipe_error = TRUE;
275 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100276 // else GUI_CHILD_OK: parent exit
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200277 }
278
279 if (pipe_error)
Bram Moolenaareda1da02019-11-17 17:06:33 +0100280 ui_delay(301L, TRUE);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200281
Bram Moolenaar30613902019-12-01 22:11:18 +0100282 // When swapping screens we may need to go to the next line, e.g.,
283 // after a hit-enter prompt and using ":gui".
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200284 if (newline_on_exit)
285 mch_errmsg("\r\n");
286
287 /*
288 * The parent must skip the normal exit() processing, the child
289 * will do it. For example, GTK messes up signals when exiting.
290 */
291 _exit(0);
292 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100293 // Child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200294
K.Takata6e1d31e2022-02-03 13:05:32 +0000295# ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100296 // Call gtk_init_check() here after fork(). See gui_init_check().
Bram Moolenaar29695702012-05-18 17:03:18 +0200297 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100298 getout_preserve_modified(1);
K.Takata6e1d31e2022-02-03 13:05:32 +0000299# endif
Bram Moolenaar29695702012-05-18 17:03:18 +0200300
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200301# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
302 /*
303 * Change our process group. On some systems/shells a CTRL-C in the
304 * shell where Vim was started would otherwise kill gvim!
305 */
306# if defined(HAVE_SETSID)
307 (void)setsid();
308# else
309 (void)setpgid(0, 0);
310# endif
311# endif
312 if (!pipe_error)
313 close(pipefd[0]);
314
315# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
Bram Moolenaar30613902019-12-01 22:11:18 +0100316 // Tell the session manager our new PID
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200317 gui_mch_forked();
318# endif
319
Bram Moolenaar30613902019-12-01 22:11:18 +0100320 // Try to start the GUI
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200321 gui_attempt_start();
322
Bram Moolenaar30613902019-12-01 22:11:18 +0100323 // Notify the parent
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200324 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200325 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200326 if (gui.in_use)
327 write_eintr(pipefd[1], "ok", 3);
328 else
329 write_eintr(pipefd[1], "fail", 5);
330 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200331 }
332
Bram Moolenaar30613902019-12-01 22:11:18 +0100333 // If we failed to start the GUI, exit now.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200334 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100335 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200336}
337
338/*
339 * Read from a pipe assumed to be connected to the child process (this
340 * function is called from the parent).
341 * Return GUI_CHILD_OK if the child successfully started the GUI,
342 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
343 * some other error.
344 *
345 * The file descriptor will be closed before the function returns.
346 */
347 static int
348gui_read_child_pipe(int fd)
349{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200350 long bytes_read;
K.Takata6e1d31e2022-02-03 13:05:32 +0000351# define READ_BUFFER_SIZE 10
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200352 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200353
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200354 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
K.Takata6e1d31e2022-02-03 13:05:32 +0000355# undef READ_BUFFER_SIZE
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200356 close(fd);
357 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200358 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200359 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200360 if (strcmp(buffer, "ok") == 0)
361 return GUI_CHILD_OK;
362 return GUI_CHILD_FAILED;
363}
364
Bram Moolenaar30613902019-12-01 22:11:18 +0100365#endif // GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367/*
368 * Call this when vim starts up, whether or not the GUI is started
369 */
370 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100371gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372{
Bram Moolenaar30613902019-12-01 22:11:18 +0100373 gui.in_use = FALSE; // No GUI yet (maybe later)
374 gui.starting = FALSE; // No GUI yet (maybe later)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 gui_mch_prepare(argc, argv);
376}
377
378/*
379 * Try initializing the GUI and check if it can be started.
380 * Used from main() to check early if "vim -g" can start the GUI.
381 * Used from gui_init() to prepare for starting the GUI.
382 * Returns FAIL or OK.
383 */
384 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100385gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386{
387 static int result = MAYBE;
388
389 if (result != MAYBE)
390 {
391 if (result == FAIL)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000392 emsg(_(e_cannot_start_the_GUI));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 return result;
394 }
395
396 gui.shell_created = FALSE;
397 gui.dying = FALSE;
Bram Moolenaar30613902019-12-01 22:11:18 +0100398 gui.in_focus = TRUE; // so the guicursor setting works
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 gui.dragged_sb = SBAR_NONE;
400 gui.dragged_wp = NULL;
401 gui.pointer_hidden = FALSE;
402 gui.col = 0;
403 gui.row = 0;
404 gui.num_cols = Columns;
405 gui.num_rows = Rows;
406
407 gui.cursor_is_valid = FALSE;
408 gui.scroll_region_top = 0;
409 gui.scroll_region_bot = Rows - 1;
410 gui.scroll_region_left = 0;
411 gui.scroll_region_right = Columns - 1;
412 gui.highlight_mask = HL_NORMAL;
413 gui.char_width = 1;
414 gui.char_height = 1;
415 gui.char_ascent = 0;
416 gui.border_width = 0;
417
418 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200419#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 gui.bold_font = NOFONT;
421 gui.ital_font = NOFONT;
422 gui.boldital_font = NOFONT;
423# ifdef FEAT_XFONTSET
424 gui.fontset = NOFONTSET;
425# endif
426#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200427 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100428#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200429 gui.wide_bold_font = NOFONT;
430 gui.wide_ital_font = NOFONT;
431 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433
434#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200435# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436# ifdef FONTSET_ALWAYS
437 gui.menu_fontset = NOFONTSET;
438# else
439 gui.menu_font = NOFONT;
440# endif
441# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100442 gui.menu_is_active = TRUE; // default: include menu
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443# ifndef FEAT_GUI_GTK
444 gui.menu_height = MENU_DEFAULT_HEIGHT;
445 gui.menu_width = 0;
446# endif
447#endif
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100448#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 gui.toolbar_height = 0;
450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451#ifdef FEAT_BEVAL_TIP
452 gui.tooltip_fontset = NOFONTSET;
453#endif
454
455 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
456 gui.prev_wrap = -1;
457
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +0100458#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Erik S. V. Jansson2f026382024-02-26 22:23:05 +0100459 // Note: gui_set_ligatures() might already have been called e.g. from .vimrc,
460 // and in that case we don't want to overwrite ligatures map that has already
461 // been correctly populated (as that would lead to a cleared ligatures maps).
462 if (*p_guiligatures == NUL)
zeertzjqd9be94c2024-07-14 10:20:20 +0200463 CLEAR_FIELD(gui.ligatures_map);
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100464#endif
465
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200466#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 result = OK;
468#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200469# ifdef FEAT_GUI_GTK
470 /*
471 * Note: Don't call gtk_init_check() before fork, it will be called after
472 * the fork. When calling it before fork, it make vim hang for a while.
473 * See gui_do_fork().
474 * Use a simpler check if the GUI window can probably be opened.
475 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200476 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200477# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480#endif
481 return result;
482}
483
484/*
485 * This is the call which starts the GUI.
486 */
487 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100488gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
490 win_T *wp;
Yee Cheng China5e03f62025-02-06 21:18:17 +0100491 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 static int recursive = 0;
493
494 /*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000495 * It's possible to use ":gui" in a .gvimrc file. The first half of this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 * function will then be executed at the first call, the rest by the
497 * recursive call. This allow the shell to be opened halfway reading a
498 * gvimrc file.
499 */
500 if (!recursive)
501 {
502 ++recursive;
503
504 clip_init(TRUE);
505
Bram Moolenaar30613902019-12-01 22:11:18 +0100506 // If can't initialize, don't try doing the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 if (gui_init_check() == FAIL)
508 {
509 --recursive;
510 clip_init(FALSE);
511 return;
512 }
513
514 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000515 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
516 * breaks the Paste toolbar button.
517 */
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100518 set_option_value_give_err((char_u *)"paste", 0L, NULL, 0);
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000519
Bram Moolenaaracc770a2020-04-12 15:11:06 +0200520 // Set t_Co to the number of colors: RGB.
521 set_color_count(256 * 256 * 256);
522
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000523 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 * Set up system-wide default menus.
525 */
526#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
527 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
528 {
529 sys_menu = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 sys_menu = FALSE;
532 }
533#endif
534
535 /*
536 * Switch on the mouse by default, unless the user changed it already.
537 * This can then be changed in the .gvimrc.
538 */
539 if (!option_was_set((char_u *)"mouse"))
540 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000541 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
543 /*
544 * If -U option given, use only the initializations from that file and
545 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
546 */
547 if (use_gvimrc != NULL)
548 {
549 if (STRCMP(use_gvimrc, "NONE") != 0
550 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 && do_source(use_gvimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000552 semsg(_(e_cannot_read_from_str), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
554 else
555 {
556 /*
557 * Get system wide defaults for gvim, only when file name defined.
558 */
559#ifdef SYS_GVIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561#endif
562
563 /*
564 * Try to read GUI initialization commands from the following
565 * places:
566 * - environment variable GVIMINIT
567 * - the user gvimrc file (~/.gvimrc)
568 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
569 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
570 * The first that exists is used, the rest is ignored.
571 */
572 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000573 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000576 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200579#ifdef USR_GVIMRC_FILE3
580 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 )
584 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200585#ifdef USR_GVIMRC_FILE4
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE,
587 DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588#endif
589 }
590
591 /*
592 * Read initialization commands from ".gvimrc" in current
593 * directory. This is only done if the 'exrc' option is set.
594 * Because of security reasons we disallow shell and write
595 * commands now, except for unix if the file is owned by the user
596 * or 'secure' option has been reset in environment of global
597 * ".gvimrc".
598 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
599 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
600 */
601 if (p_exrc)
602 {
603#ifdef UNIX
604 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200605 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaar30613902019-12-01 22:11:18 +0100607 // if ".gvimrc" file is not owned by user, set 'secure'
608 // mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
610 secure = p_secure;
611 }
612#else
613 secure = p_secure;
614#endif
615
616 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200617 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#ifdef SYS_GVIMRC_FILE
619 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200620 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621#endif
622#ifdef USR_GVIMRC_FILE2
623 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200624 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625#endif
626#ifdef USR_GVIMRC_FILE3
627 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200628 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200630#ifdef USR_GVIMRC_FILE4
631 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200632 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
637 if (secure == 2)
638 need_wait_return = TRUE;
639 secure = 0;
640 }
641 }
642
643 if (need_wait_return || msg_didany)
644 wait_return(TRUE);
645
646 --recursive;
647 }
648
Bram Moolenaar30613902019-12-01 22:11:18 +0100649 // If recursive call opened the shell, return here from the first call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if (gui.in_use)
651 return;
652
653 /*
654 * Create the GUI shell.
655 */
Bram Moolenaar30613902019-12-01 22:11:18 +0100656 gui.in_use = TRUE; // Must be set after menus have been set up
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if (gui_mch_init() == FAIL)
658 goto error;
659
Bram Moolenaar30613902019-12-01 22:11:18 +0100660 // Avoid a delay for an error message that was printed in the terminal
661 // where Vim was started.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 emsg_on_display = FALSE;
663 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100664 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 need_wait_return = FALSE;
666 msg_didany = FALSE;
667
668 /*
669 * Check validity of any generic resources that may have been loaded.
670 */
671 if (gui.border_width < 0)
672 gui.border_width = 0;
673
674 /*
675 * Set up the fonts. First use a font specified with "-fn" or "-font".
676 */
677 if (font_argument != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100678 set_option_value_give_err((char_u *)"gfn",
679 0L, (char_u *)font_argument, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (
681#ifdef FEAT_XFONTSET
682 (*p_guifontset == NUL
683 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
684#endif
685 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
686 : p_guifont, FALSE) == FAIL)
687 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000688 emsg(_(e_cannot_start_gui_no_valid_font_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 goto error2;
690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 if (gui_get_wide_font() == FAIL)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000692 emsg(_(e_guifontwide_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694 gui.num_cols = Columns;
695 gui.num_rows = Rows;
696 gui_reset_scroll_region();
697
Bram Moolenaar30613902019-12-01 22:11:18 +0100698 // Create initial scrollbars
Yee Cheng China5e03f62025-02-06 21:18:17 +0100699 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
701 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
702 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
703 }
704 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
705
706#ifdef FEAT_MENU
707 gui_create_initial_menus(root_menu);
708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#ifdef FEAT_SIGN_ICONS
710 sign_gui_started();
711#endif
712
Bram Moolenaar30613902019-12-01 22:11:18 +0100713 // Configure the desired menu and scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 gui_init_which_components(NULL);
715
Bram Moolenaar30613902019-12-01 22:11:18 +0100716 // All components of the GUI have been created now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 gui.shell_created = TRUE;
718
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100719#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100720 // Set the shell size, adjusted for the screen size. For GTK this only
721 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100722 // If the window is already maximized (e.g. when --windowid is passed in),
723 // we want to use the system-provided dimensions by passing FALSE to
724 // mustset. Otherwise, we want to initialize with the default rows/columns.
725 if (gui_mch_maximized())
726 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
727 else
728 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100729#else
730# ifndef FEAT_GUI_GTK
731 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
732# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733#endif
734#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +0100735 // Need to set the size of the menubar after all the menus have been
736 // created.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 gui_mch_compute_menu_height((Widget)0);
738#endif
739
740 /*
741 * Actually open the GUI shell.
742 */
743 if (gui_mch_open() != FAIL)
744 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 maketitle();
746 resettitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 init_gui_options();
749#ifdef FEAT_ARABIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100750 // Our GUI can't do bidi.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 p_tbidi = FALSE;
752#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000753#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100754 // Give GTK+ a chance to put all widget's into place.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000756
757# ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +0100758 // If there is no 'm' in 'guioptions' we need to remove the menu now.
759 // It was still there to make F10 work.
Bram Moolenaar18144c82006-04-12 21:52:12 +0000760 if (vim_strchr(p_go, GO_MENUS) == NULL)
761 {
762 --gui.starting;
763 gui_mch_enable_menu(FALSE);
764 ++gui.starting;
765 gui_mch_update();
766 }
767# endif
768
Bram Moolenaar30613902019-12-01 22:11:18 +0100769 // Now make sure the shell fits on the screen.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100770 if (gui_mch_maximized())
771 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
772 else
773 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100775 // When 'lines' was set while starting up the topframe may have to be
776 // resized.
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000777 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000778
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100779#ifdef FEAT_BEVAL_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100780 // Always create the Balloon Evaluation area, but disable it when
781 // 'ballooneval' is off.
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100782 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200783 {
784# ifdef FEAT_VARTABS
785 vim_free(balloonEval->vts);
786# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100787 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200788 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100789 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000790# ifdef FEAT_GUI_GTK
791 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
792 &general_beval_cb, NULL);
793# else
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100794# if defined(FEAT_GUI_MOTIF)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000795 {
796 extern Widget textArea;
797 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000798 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000799 }
800# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100801# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000802 balloonEval = gui_mch_create_beval_area(NULL, NULL,
803 &general_beval_cb, NULL);
804# endif
805# endif
806# endif
807 if (!p_beval)
808 gui_mch_disable_beval_area(balloonEval);
809#endif
Bram Moolenaarad772a62020-06-01 14:07:49 +0200810
811#ifndef FEAT_GUI_MSWIN
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200812 // In the GUI modifiers are prepended to keys.
Bram Moolenaarad772a62020-06-01 14:07:49 +0200813 // Don't do this for MS-Windows yet, it sends CTRL-K without the
814 // modifier.
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200815 seenModifyOtherKeys = TRUE;
Bram Moolenaarad772a62020-06-01 14:07:49 +0200816#endif
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
819 if (!im_xim_isvalid_imactivate())
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000820 emsg(_(e_value_of_imactivatekey_is_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100822 // When 'cmdheight' was set during startup it may not have taken
823 // effect yet.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000824 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000825 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 return;
828 }
829
830error2:
831#ifdef FEAT_GUI_X11
Bram Moolenaar30613902019-12-01 22:11:18 +0100832 // undo gui_mch_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 gui_mch_uninit();
834#endif
835
836error:
837 gui.in_use = FALSE;
838 clip_init(FALSE);
839}
840
841
842 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100843gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
Bram Moolenaar30613902019-12-01 22:11:18 +0100845 // don't free the fonts, it leads to a BUS error
846 // richard@whitequeen.com Jul 99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 gui.in_use = FALSE;
849 gui_mch_exit(rc);
850}
851
Bram Moolenaar9372a112005-12-06 19:59:18 +0000852#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +0200853 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000854# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855/*
856 * Called when the GUI shell is closed by the user. If there are no changed
857 * files Vim exits, otherwise there will be a dialog to ask the user what to
858 * do.
859 * When this function returns, Vim should NOT exit!
860 */
861 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100862gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863{
Bram Moolenaar3552e742021-05-29 12:21:58 +0200864 cmdmod_T save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
Bram Moolenaar3552e742021-05-29 12:21:58 +0200866 if (before_quit_autocmds(curwin, TRUE, FALSE))
867 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaar30613902019-12-01 22:11:18 +0100869 // Only exit when there are no changed files
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 exiting = TRUE;
871# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200872 cmdmod.cmod_flags |= CMOD_BROWSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873# endif
874# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +0200875 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100877 // If there are changed buffers, present the user with a dialog if
878 // possible, otherwise give an error message.
Bram Moolenaar027387f2016-01-02 22:25:52 +0100879 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 getout(0);
881
882 exiting = FALSE;
883 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +0100884 gui_update_screen(); // redraw, window may show changed buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885}
886#endif
887
888/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200889 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * first font name that works is used. If none is found, use the default
891 * font.
892 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
893 * Return OK when able to set the font. When it failed FAIL is returned and
894 * the fonts are unchanged.
895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100897gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898{
899#define FONTLEN 320
900 char_u font_name[FONTLEN];
901 int font_list_empty = FALSE;
902 int ret = FAIL;
903
904 if (!gui.in_use)
905 return FAIL;
906
907 font_name[0] = NUL;
908 if (*font_list == NUL)
909 font_list_empty = TRUE;
910 else
911 {
912#ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +0100913 // When using a fontset, the whole list of fonts is one name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if (fontset)
915 ret = gui_mch_init_font(font_list, TRUE);
916 else
917#endif
918 while (*font_list != NUL)
919 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100920 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
922
Bram Moolenaar30613902019-12-01 22:11:18 +0100923 // Careful!!! The Win32 version of gui_mch_init_font(), when
924 // called with "*" will change p_guifont to the selected font
925 // name, which frees the old value. This makes font_list
926 // invalid. Thus when OK is returned here, font_list must no
927 // longer be used!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (gui_mch_init_font(font_name, FALSE) == OK)
929 {
K.Takata94373c42022-01-27 15:04:22 +0000930#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar30613902019-12-01 22:11:18 +0100931 // If it's a Unicode font, try setting 'guifontwide' to a
932 // similar double-width font.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
934 && strstr((char *)font_name, "10646") != NULL)
935 set_guifontwide(font_name);
936#endif
937 ret = OK;
938 break;
939 }
940 }
941 }
942
943 if (ret != OK
944 && STRCMP(font_list, "*") != 0
945 && (font_list_empty || gui.norm_font == NOFONT))
946 {
947 /*
948 * Couldn't load any font in 'font_list', keep the current font if
949 * there is one. If 'font_list' is empty, or if there is no current
950 * font, tell gui_mch_init_font() to try to find a font we can load.
951 */
952 ret = gui_mch_init_font(NULL, FALSE);
953 }
954
955 if (ret == OK)
956 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200957#ifndef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100958 // Set normal font as current font
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959# ifdef FEAT_XFONTSET
960 if (gui.fontset != NOFONTSET)
961 gui_mch_set_fontset(gui.fontset);
962 else
963# endif
964 gui_mch_set_font(gui.norm_font);
965#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100966 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968
969 return ret;
970}
971
K.Takata94373c42022-01-27 15:04:22 +0000972#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973/*
974 * Try setting 'guifontwide' to a font twice as wide as "name".
975 */
976 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100977set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
979 int i = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +0100980 char_u wide_name[FONTLEN + 10]; // room for 2 * width and '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 char_u *wp = NULL;
982 char_u *p;
983 GuiFont font;
984
985 wp = wide_name;
986 for (p = name; *p != NUL; ++p)
987 {
988 *wp++ = *p;
989 if (*p == '-')
990 {
991 ++i;
Bram Moolenaar30613902019-12-01 22:11:18 +0100992 if (i == 6) // font type: change "--" to "-*-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 {
994 if (p[1] == '-')
995 *wp++ = '*';
996 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100997 else if (i == 12) // found the width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
999 ++p;
1000 i = getdigits(&p);
1001 if (i != 0)
1002 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001003 // Double the width specification.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 sprintf((char *)wp, "%d%s", i * 2, p);
1005 font = gui_mch_get_font(wide_name, FALSE);
1006 if (font != NOFONT)
1007 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001008 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 gui.wide_font = font;
1010 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001011 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014 break;
1015 }
1016 }
1017 }
1018}
K.Takata94373c42022-01-27 15:04:22 +00001019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020
1021/*
1022 * Get the font for 'guifontwide'.
1023 * Return FAIL for an invalid font name.
1024 */
1025 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001026gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
1028 GuiFont font = NOFONT;
1029 char_u font_name[FONTLEN];
1030 char_u *p;
1031
Bram Moolenaar30613902019-12-01 22:11:18 +01001032 if (!gui.in_use) // Can't allocate font yet, assume it's OK.
1033 return OK; // Will give an error message later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1036 {
1037 for (p = p_guifontwide; *p != NUL; )
1038 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001039 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1041 font = gui_mch_get_font(font_name, FALSE);
1042 if (font != NOFONT)
1043 break;
1044 }
1045 if (font == NOFONT)
1046 return FAIL;
1047 }
1048
1049 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001050#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001051 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (font != NOFONT && gui.norm_font != NOFONT
1053 && pango_font_description_equal(font, gui.norm_font))
1054 {
1055 gui.wide_font = NOFONT;
1056 gui_mch_free_font(font);
1057 }
1058 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001061#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001062 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001063#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001064 /*
1065 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1066 * support those fonts for 'guifontwide'.
1067 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 return OK;
1070}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01001072#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001073/*
1074 * Set list of ascii characters that combined can create ligature.
1075 * Store them in char map for quick access from gui_gtk2_draw_string.
1076 */
1077 void
1078gui_set_ligatures(void)
1079{
1080 char_u *p;
1081
1082 if (*p_guiligatures != NUL)
1083 {
1084 // check for invalid characters
1085 for (p = p_guiligatures; *p != NUL; ++p)
1086 if (*p < 32 || *p > 127)
1087 {
1088 emsg(_(e_ascii_code_not_in_range));
1089 return;
1090 }
1091
1092 // store valid setting into ligatures_map
1093 CLEAR_FIELD(gui.ligatures_map);
1094 for (p = p_guiligatures; *p != NUL; ++p)
1095 gui.ligatures_map[*p] = 1;
1096 }
1097 else
1098 CLEAR_FIELD(gui.ligatures_map);
1099}
Dusan Popovicce59b9f2021-11-22 17:18:44 +00001100
1101/*
1102 * Adjust the columns to undraw for when the cursor is on ligatures.
1103 */
1104 static void
1105gui_adjust_undraw_cursor_for_ligatures(int *startcol, int *endcol)
1106{
1107 int off;
1108
1109 if (ScreenLines == NULL || *p_guiligatures == NUL)
1110 return;
1111
1112 // expand before the cursor for all the chars in gui.ligatures_map
1113 off = LineOffset[gui.cursor_row] + *startcol;
1114 if (gui.ligatures_map[ScreenLines[off]])
1115 while (*startcol > 0 && gui.ligatures_map[ScreenLines[--off]])
1116 (*startcol)--;
1117
1118 // expand after the cursor for all the chars in gui.ligatures_map
1119 off = LineOffset[gui.cursor_row] + *endcol;
1120 if (gui.ligatures_map[ScreenLines[off]])
1121 while (*endcol < ((int)screen_Columns - 1)
1122 && gui.ligatures_map[ScreenLines[++off]])
1123 (*endcol)++;
1124}
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001125#endif
1126
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001127 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001128gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129{
1130 gui.row = row;
1131 gui.col = col;
1132}
1133
1134/*
1135 * gui_check_pos - check if the cursor is on the screen.
1136 */
1137 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001138gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
1140 if (gui.row >= screen_Rows)
1141 gui.row = screen_Rows - 1;
1142 if (gui.col >= screen_Columns)
1143 gui.col = screen_Columns - 1;
1144 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1145 gui.cursor_is_valid = FALSE;
1146}
1147
1148/*
1149 * Redraw the cursor if necessary or when forced.
1150 * Careful: The contents of ScreenLines[] must match what is on the screen,
1151 * otherwise this goes wrong. May need to call out_flush() first.
1152 */
1153 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001154gui_update_cursor(
Bram Moolenaar30613902019-12-01 22:11:18 +01001155 int force, // when TRUE, update even when not moved
1156 int clear_selection) // clear selection under cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157{
1158 int cur_width = 0;
1159 int cur_height = 0;
1160 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001161 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001163#ifdef FEAT_TERMINAL
1164 guicolor_T shape_fg = INVALCOLOR;
1165 guicolor_T shape_bg = INVALCOLOR;
1166#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001167 guicolor_T cfg, cbg, cc; // cursor fore-/background color
1168 int cattr; // cursor attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 int attr;
1170 attrentry_T *aep = NULL;
1171
Bram Moolenaar30613902019-12-01 22:11:18 +01001172 // Don't update the cursor when halfway busy scrolling or the screen size
1173 // doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then.
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001174 if (!can_update_cursor || screen_Columns != gui.num_cols
1175 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 return;
1177
1178 gui_check_pos();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001179
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001180 if (gui.cursor_is_valid && !force
1181 && gui.row == gui.cursor_row && gui.col == gui.cursor_col)
1182 return;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001183
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001184 gui_undraw_cursor();
1185
1186 // If a cursor-less sleep is ongoing, leave the cursor invisible
1187 if (cursor_is_sleeping())
1188 return;
1189
1190 if (gui.row < 0)
1191 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001192#ifdef HAVE_INPUT_METHOD
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001193 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1194 im_set_position(gui.row, gui.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001196 gui.cursor_row = gui.row;
1197 gui.cursor_col = gui.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001199 // Only write to the screen after ScreenLines[] has been initialized
1200 if (!screen_cleared || ScreenLines == NULL)
1201 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001203 // Clear the selection if we are about to write over it
1204 if (clear_selection)
1205 clip_may_clear_selection(gui.row, gui.row);
1206 // Check that the cursor is inside the shell (resizing may have made
1207 // it invalid)
1208 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1209 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001211 gui.cursor_is_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001213 /*
1214 * How the cursor is drawn depends on the current mode.
1215 * When in a terminal window use the shape/color specified there.
1216 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001217#ifdef FEAT_TERMINAL
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001218 if (terminal_is_active())
1219 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
1220 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001221#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001222 shape = &shape_table[get_shape_idx(FALSE)];
1223 if (State & MODE_LANGMAP)
1224 id = shape->id_lm;
1225 else
1226 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001228 // get the colors and attributes for the cursor. Default is inverted
1229 cfg = INVALCOLOR;
1230 cbg = INVALCOLOR;
1231 cattr = HL_INVERSE;
1232 gui_mch_set_blinking(shape->blinkwait,
1233 shape->blinkon,
1234 shape->blinkoff);
1235 if (shape->blinkwait == 0 || shape->blinkon == 0
1236 || shape->blinkoff == 0)
1237 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001238#ifdef FEAT_TERMINAL
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001239 if (shape_bg != INVALCOLOR)
1240 {
1241 cattr = 0;
1242 cfg = shape_fg;
1243 cbg = shape_bg;
1244 }
1245 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (id > 0)
1248 {
1249 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar54612582019-11-21 17:13:31 +01001250#if defined(HAVE_INPUT_METHOD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
1252 static int iid;
1253 guicolor_T fg, bg;
1254
Bram Moolenaarc236c162008-07-13 17:41:49 +00001255 if (
Bram Moolenaar54612582019-11-21 17:13:31 +01001256# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001257 preedit_get_status()
1258# else
1259 im_get_status()
1260# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001261 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
1263 iid = syn_name2id((char_u *)"CursorIM");
1264 if (iid > 0)
1265 {
1266 syn_id2colors(iid, &fg, &bg);
1267 if (bg != INVALCOLOR)
1268 cbg = bg;
1269 if (fg != INVALCOLOR)
1270 cfg = fg;
1271 }
1272 }
1273 }
1274#endif
1275 }
1276
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001277 /*
1278 * Get the attributes for the character under the cursor.
1279 * When no cursor color was given, use the character color.
1280 */
1281 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1282 if (attr > HL_ALL)
1283 aep = syn_gui_attr2entry(attr);
1284 if (aep != NULL)
1285 {
1286 attr = aep->ae_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (cfg == INVALCOLOR)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001288 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1289 : aep->ae_u.gui.fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 if (cbg == INVALCOLOR)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001291 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1292 : aep->ae_u.gui.bg_color);
1293 }
1294 if (cfg == INVALCOLOR)
1295 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1296 if (cbg == INVALCOLOR)
1297 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
1299#ifdef FEAT_XIM
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001300 if (aep != NULL)
1301 {
1302 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1303 : aep->ae_u.gui.bg_color);
1304 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1305 : aep->ae_u.gui.fg_color);
1306 if (xim_bg_color == INVALCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001308 : gui.back_pixel;
1309 if (xim_fg_color == INVALCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001311 : gui.norm_pixel;
1312 }
1313 else
1314 {
1315 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1316 : gui.back_pixel;
1317 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1318 : gui.norm_pixel;
1319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320#endif
1321
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001322 attr &= ~HL_INVERSE;
1323 if (cattr & HL_INVERSE)
1324 {
1325 cc = cbg;
1326 cbg = cfg;
1327 cfg = cc;
1328 }
1329 cattr &= ~HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001331 /*
1332 * When we don't have window focus, draw a hollow cursor.
1333 */
1334 if (!gui.in_focus)
1335 {
1336 gui_mch_draw_hollow_cursor(cbg);
1337 return;
1338 }
1339
1340 old_hl_mask = gui.highlight_mask;
1341 if (shape->shape == SHAPE_BLOCK)
1342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001344 * Draw the text character with the cursor colors. Use the
1345 * character attributes plus the cursor attributes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 */
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001347 gui.highlight_mask = (cattr | attr);
1348 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1349 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1350 }
1351 else
1352 {
1353#if defined(FEAT_RIGHTLEFT)
1354 int col_off = FALSE;
1355#endif
1356 /*
1357 * First draw the partial cursor, then overwrite with the text
1358 * character, using a transparent background.
1359 */
1360 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001362 cur_height = gui.char_height;
1363 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
1365 else
1366 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001367 cur_height = (gui.char_height * shape->percentage + 99) / 100;
1368 cur_width = gui.char_width;
1369 }
1370 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1371 LineOffset[gui.row] + screen_Columns) > 1)
1372 {
1373 // Double wide character.
1374 if (shape->shape != SHAPE_VER)
1375 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001376#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001377 if (CURSOR_BAR_RIGHT)
1378 {
1379 // gui.col points to the left half of the character but
1380 // the vertical line needs to be on the right half.
1381 // A double-wide horizontal line is also drawn from the
1382 // right half in gui_mch_draw_part_cursor().
1383 col_off = TRUE;
1384 ++gui.col;
Bram Moolenaar13505972019-01-24 15:04:48 +01001385 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001386#endif
1387 }
1388 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001389#if defined(FEAT_RIGHTLEFT)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001390 if (col_off)
1391 --gui.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#endif
1393
Bram Moolenaar30613902019-12-01 22:11:18 +01001394#ifndef FEAT_GUI_MSWIN // doesn't seem to work for MSWindows
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001395 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1396 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1397 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1398 (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001401 gui.highlight_mask = old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402}
1403
1404#if defined(FEAT_MENU) || defined(PROTO)
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01001405 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001406gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001408# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 if (gui.menu_is_active && gui.in_use)
1410 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1411# endif
1412}
1413#endif
1414
1415/*
1416 * Position the various GUI components (text area, menu). The vertical
1417 * scrollbars are NOT handled here. See gui_update_scrollbars().
1418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001420gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 int text_area_x;
1423 int text_area_y;
1424 int text_area_width;
1425 int text_area_height;
1426
Bram Moolenaar30613902019-12-01 22:11:18 +01001427 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 ++hold_gui_events;
1429
1430 text_area_x = 0;
1431 if (gui.which_scrollbars[SBAR_LEFT])
1432 text_area_x += gui.scrollbar_width;
1433
1434 text_area_y = 0;
1435#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1436 gui.menu_width = total_width;
1437 if (gui.menu_is_active)
1438 text_area_y += gui.menu_height;
1439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
K.Takata6e1d31e2022-02-03 13:05:32 +00001441#if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001442 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001443 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001444 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001445#endif
1446
Bram Moolenaar0b962e52022-04-03 18:02:37 +01001447#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) \
K.Takatac81e9bf2022-01-16 14:15:49 +00001448 || defined(FEAT_GUI_HAIKU) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1450 {
Bram Moolenaar0b962e52022-04-03 18:02:37 +01001451# if defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 gui_mch_set_toolbar_pos(0, text_area_y,
1453 gui.menu_width, gui.toolbar_height);
1454# endif
1455 text_area_y += gui.toolbar_height;
1456 }
1457#endif
1458
K.Takata6e1d31e2022-02-03 13:05:32 +00001459#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_HAIKU)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001460 gui_mch_set_tabline_pos(0, text_area_y,
1461 gui.menu_width, gui.tabline_height);
1462 if (gui_has_tabline())
1463 text_area_y += gui.tabline_height;
1464#endif
1465
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1467 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1468
1469 gui_mch_set_text_area_pos(text_area_x,
1470 text_area_y,
1471 text_area_width,
1472 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001473#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 + xim_get_status_area_height()
1475#endif
1476 );
1477#ifdef FEAT_MENU
1478 gui_position_menu();
1479#endif
1480 if (gui.which_scrollbars[SBAR_BOTTOM])
1481 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1482 text_area_x,
Bram Moolenaar203ec772020-07-17 20:43:43 +02001483 text_area_y + text_area_height
1484 + gui_mch_get_scrollbar_ypadding(),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 text_area_width,
1486 gui.scrollbar_height);
1487 gui.left_sbar_x = 0;
Bram Moolenaar203ec772020-07-17 20:43:43 +02001488 gui.right_sbar_x = text_area_x + text_area_width
1489 + gui_mch_get_scrollbar_xpadding();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
1491 --hold_gui_events;
1492}
1493
Bram Moolenaar02743632005-07-25 20:42:36 +00001494/*
1495 * Get the width of the widgets and decorations to the side of the text area.
1496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001498gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 int base_width;
1501
1502 base_width = 2 * gui.border_offset;
1503 if (gui.which_scrollbars[SBAR_LEFT])
1504 base_width += gui.scrollbar_width;
1505 if (gui.which_scrollbars[SBAR_RIGHT])
1506 base_width += gui.scrollbar_width;
1507 return base_width;
1508}
1509
Bram Moolenaar02743632005-07-25 20:42:36 +00001510/*
1511 * Get the height of the widgets and decorations above and below the text area.
1512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001514gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
1516 int base_height;
1517
1518 base_height = 2 * gui.border_offset;
1519 if (gui.which_scrollbars[SBAR_BOTTOM])
1520 base_height += gui.scrollbar_height;
1521#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001522 // We can't take the sizes properly into account until anything is
1523 // realized. Therefore we recalculate all the values here just before
1524 // setting the size. (--mdcki)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525#else
1526# ifdef FEAT_MENU
1527 if (gui.menu_is_active)
1528 base_height += gui.menu_height;
1529# endif
1530# ifdef FEAT_TOOLBAR
1531 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 base_height += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001534# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001535 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001536 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001537 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001538# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1540 base_height += gui_mch_text_area_extra_height();
1541# endif
1542#endif
1543 return base_height;
1544}
1545
1546/*
1547 * Should be called after the GUI shell has been resized. Its arguments are
1548 * the new width and height of the shell in pixels.
1549 */
1550 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001551gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 static int busy = FALSE;
1554
Bram Moolenaar30613902019-12-01 22:11:18 +01001555 if (!gui.shell_created) // ignore when still initializing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 return;
1557
1558 /*
1559 * Can't resize the screen while it is being redrawn. Remember the new
1560 * size and handle it later.
1561 */
1562 if (updating_screen || busy)
1563 {
1564 new_pixel_width = pixel_width;
1565 new_pixel_height = pixel_height;
1566 return;
1567 }
1568
1569again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001570 new_pixel_width = 0;
1571 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 busy = TRUE;
1573
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001574#ifdef FEAT_GUI_HAIKU
1575 vim_lock_screen();
1576#endif
1577
Bram Moolenaar30613902019-12-01 22:11:18 +01001578 // Flush pending output before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 out_flush();
1580
1581 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001582 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 /*
1588 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1589 * at the last line here (why does it have to be one row too low?).
1590 */
Bram Moolenaar24959102022-05-07 20:01:16 +01001591 if (State == MODE_ASKMORE || State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 gui.row = gui.num_rows;
1593
Bram Moolenaar30613902019-12-01 22:11:18 +01001594 // Only comparing Rows and Columns may be sufficient, but let's stay on
1595 // the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
lilydjwg6e0a18f2024-01-29 20:54:28 +01001597 || gui.num_rows != Rows || gui.num_cols != Columns || gui.force_redraw)
1598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 shell_resized();
lilydjwg6e0a18f2024-01-29 20:54:28 +01001600 gui.force_redraw = 0;
1601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001603#ifdef FEAT_GUI_HAIKU
1604 vim_unlock_screen();
1605#endif
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 gui_update_scrollbars(TRUE);
1608 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001609#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 xim_set_status_area();
1611#endif
1612
1613 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001614
Bram Moolenaar30613902019-12-01 22:11:18 +01001615 // We may have been called again while redrawing the screen.
1616 // Need to do it all again with the latest size then. But only if the size
1617 // actually changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (new_pixel_height)
1619 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001620 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1621 {
1622 new_pixel_width = 0;
1623 new_pixel_height = 0;
1624 }
1625 else
1626 {
1627 pixel_width = new_pixel_width;
1628 pixel_height = new_pixel_height;
1629 goto again;
1630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
1632}
1633
1634/*
1635 * Check if gui_resize_shell() must be called.
1636 */
1637 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001638gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (new_pixel_height)
Bram Moolenaar30613902019-12-01 22:11:18 +01001641 // careful: gui_resize_shell() may postpone the resize again if we
1642 // were called indirectly by it
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001643 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644}
1645
1646 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001647gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
1649 Rows = gui.num_rows;
1650 Columns = gui.num_cols;
1651 return OK;
1652}
1653
1654/*
1655 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001656 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1657 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001658 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1659 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001662gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001663 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001664 int fit_to_display,
Bram Moolenaar30613902019-12-01 22:11:18 +01001665 int direction) // RESIZE_HOR, RESIZE_VER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
1667 int base_width;
1668 int base_height;
1669 int width;
1670 int height;
1671 int min_width;
1672 int min_height;
1673 int screen_w;
1674 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001675#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001676 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001677 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001678#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001679 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681 if (!gui.shell_created)
1682 return;
1683
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001684#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01001685 // If not setting to a user specified size and maximized, calculate the
1686 // number of characters that fit in the maximized window.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001687 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1688 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 {
1690 gui_mch_newfont();
1691 return;
1692 }
1693#endif
1694
1695 base_width = gui_get_base_width();
1696 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001697 if (fit_to_display)
Bram Moolenaar30613902019-12-01 22:11:18 +01001698 // Remember the original window position.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001699 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001700
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001701 width = Columns * gui.char_width + base_width;
1702 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
1704 if (fit_to_display)
1705 {
1706 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001707 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 {
1709 Columns = (screen_w - base_width) / gui.char_width;
1710 if (Columns < MIN_COLUMNS)
1711 Columns = MIN_COLUMNS;
1712 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001713#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001714 ++did_adjust;
1715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001717 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 {
1719 Rows = (screen_h - base_height) / gui.char_height;
1720 check_shellsize();
1721 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001722#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001723 ++did_adjust;
1724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001726#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001727 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1728 && height + gui.char_height >= screen_h))
Bram Moolenaar30613902019-12-01 22:11:18 +01001729 // don't unmaximize if at maximum size
Bram Moolenaar09736232009-09-23 16:14:49 +00001730 un_maximize = FALSE;
1731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001733 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 gui.num_cols = Columns;
1735 gui.num_rows = Rows;
1736
1737 min_width = base_width + MIN_COLUMNS * gui.char_width;
1738 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001739 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001740
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001741#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001742 if (un_maximize)
1743 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001744 // If the window size is smaller than the screen unmaximize the
1745 // window, otherwise resizing won't work.
Bram Moolenaar09736232009-09-23 16:14:49 +00001746 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1747 if ((width + gui.char_width < screen_w
1748 || height + gui.char_height * 2 < screen_h)
1749 && gui_mch_maximized())
1750 gui_mch_unmaximize();
1751 }
1752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001755 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001757 if (fit_to_display && x >= 0 && y >= 0)
1758 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001759 // Some window managers put the Vim window left of/above the screen.
1760 // Only change the position if it wasn't already negative before
1761 // (happens on MS-Windows with a secondary monitor).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 gui_mch_update();
1763 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1764 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1765 }
1766
1767 gui_position_components(width);
1768 gui_update_scrollbars(TRUE);
1769 gui_reset_scroll_region();
1770}
1771
1772/*
1773 * Called when Rows and/or Columns has changed.
1774 */
1775 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001776gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 gui_reset_scroll_region();
1779}
1780
1781/*
1782 * Make scroll region cover whole screen.
1783 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001784 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001785gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 gui.scroll_region_top = 0;
1788 gui.scroll_region_bot = gui.num_rows - 1;
1789 gui.scroll_region_left = 0;
1790 gui.scroll_region_right = gui.num_cols - 1;
1791}
1792
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001793 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001794gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
Bram Moolenaar30613902019-12-01 22:11:18 +01001796 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 gui.highlight_mask = mask;
Bram Moolenaar30613902019-12-01 22:11:18 +01001798 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 gui.highlight_mask |= mask;
1800}
1801
1802 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001803gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804{
Bram Moolenaar30613902019-12-01 22:11:18 +01001805 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 gui.highlight_mask = HL_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001807 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 gui.highlight_mask &= ~mask;
1809}
1810
1811/*
1812 * Clear a rectangular region of the screen from text pos (row1, col1) to
1813 * (row2, col2) inclusive.
1814 */
1815 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001816gui_clear_block(
1817 int row1,
1818 int col1,
1819 int row2,
1820 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821{
Bram Moolenaar30613902019-12-01 22:11:18 +01001822 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 clip_may_clear_selection(row1, row2);
1824
1825 gui_mch_clear_block(row1, col1, row2, col2);
1826
Bram Moolenaar30613902019-12-01 22:11:18 +01001827 // Invalidate cursor if it was in this block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1829 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1830 gui.cursor_is_valid = FALSE;
1831}
1832
1833/*
1834 * Write code to update the cursor later. This avoids the need to flush the
1835 * output buffer before calling gui_update_cursor().
1836 */
1837 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001838gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001840 OUT_STR("\033|s");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841}
1842
1843 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001844gui_write(
1845 char_u *s,
1846 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847{
1848 char_u *p;
1849 int arg1 = 0, arg2 = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01001850 int force_cursor = FALSE; // force cursor update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 int force_scrollbar = FALSE;
1852 static win_T *old_curwin = NULL;
1853
Bram Moolenaar30613902019-12-01 22:11:18 +01001854// #define DEBUG_GUI_WRITE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#ifdef DEBUG_GUI_WRITE
1856 {
1857 int i;
1858 char_u *str;
1859
1860 printf("gui_write(%d):\n ", len);
1861 for (i = 0; i < len; i++)
1862 if (s[i] == ESC)
1863 {
1864 if (i != 0)
1865 printf("\n ");
1866 printf("<ESC>");
1867 }
1868 else
1869 {
1870 str = transchar_byte(s[i]);
1871 if (str[0] && str[1])
1872 printf("<%s>", (char *)str);
1873 else
1874 printf("%s", (char *)str);
1875 }
1876 printf("\n");
1877 }
1878#endif
1879 while (len)
1880 {
1881 if (s[0] == ESC && s[1] == '|')
1882 {
1883 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001884 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
1886 arg1 = getdigits(&p);
1887 if (p > s + len)
1888 break;
1889 if (*p == ';')
1890 {
1891 ++p;
1892 arg2 = getdigits(&p);
1893 if (p > s + len)
1894 break;
1895 }
1896 }
1897 switch (*p)
1898 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001899 case 'C': // Clear screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 clip_scroll_selection(9999);
1901 gui_mch_clear_all();
1902 gui.cursor_is_valid = FALSE;
1903 force_scrollbar = TRUE;
1904 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001905 case 'M': // Move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 gui_set_cursor(arg1, arg2);
1907 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001908 case 's': // force cursor (shape) update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 force_cursor = TRUE;
1910 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001911 case 'R': // Set scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (arg1 < arg2)
1913 {
1914 gui.scroll_region_top = arg1;
1915 gui.scroll_region_bot = arg2;
1916 }
1917 else
1918 {
1919 gui.scroll_region_top = arg2;
1920 gui.scroll_region_bot = arg1;
1921 }
1922 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001923 case 'V': // Set vertical scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (arg1 < arg2)
1925 {
1926 gui.scroll_region_left = arg1;
1927 gui.scroll_region_right = arg2;
1928 }
1929 else
1930 {
1931 gui.scroll_region_left = arg2;
1932 gui.scroll_region_right = arg1;
1933 }
1934 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001935 case 'd': // Delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 gui_delete_lines(gui.row, 1);
1937 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001938 case 'D': // Delete lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 gui_delete_lines(gui.row, arg1);
1940 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001941 case 'i': // Insert line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 gui_insert_lines(gui.row, 1);
1943 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001944 case 'I': // Insert lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 gui_insert_lines(gui.row, arg1);
1946 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001947 case '$': // Clear to end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 gui_clear_block(gui.row, gui.col, gui.row,
1949 (int)Columns - 1);
1950 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001951 case 'h': // Turn on highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 gui_start_highlight(arg1);
1953 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001954 case 'H': // Turn off highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 gui_stop_highlight(arg1);
1956 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001957 case 'f': // flash the window (visual bell)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1959 break;
1960 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01001961 p = s + 1; // Skip the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 break;
1963 }
1964 len -= (int)(++p - s);
1965 s = p;
1966 }
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001967 else if (s[0] < 0x20 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968#ifdef FEAT_SIGN_ICONS
1969 && s[0] != SIGN_BYTE
1970# ifdef FEAT_NETBEANS_INTG
1971 && s[0] != MULTISIGN_BYTE
1972# endif
1973#endif
1974 )
1975 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001976 if (s[0] == '\n') // NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
1978 gui.col = 0;
1979 if (gui.row < gui.scroll_region_bot)
1980 gui.row++;
1981 else
1982 gui_delete_lines(gui.scroll_region_top, 1);
1983 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001984 else if (s[0] == '\r') // CR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
1986 gui.col = 0;
1987 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001988 else if (s[0] == '\b') // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
1990 if (gui.col)
1991 --gui.col;
1992 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001993 else if (s[0] == Ctrl_L) // cursor-right
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
1995 ++gui.col;
1996 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001997 else if (s[0] == Ctrl_G) // Beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
1999 gui_mch_beep();
2000 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002001 // Other Ctrl character: shouldn't happen!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
Bram Moolenaar30613902019-12-01 22:11:18 +01002003 --len; // Skip this char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 ++s;
2005 }
2006 else
2007 {
2008 p = s;
2009 while (len > 0 && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 *p >= 0x20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011#ifdef FEAT_SIGN_ICONS
2012 || *p == SIGN_BYTE
2013# ifdef FEAT_NETBEANS_INTG
2014 || *p == MULTISIGN_BYTE
2015# endif
2016#endif
2017 ))
2018 {
2019 len--;
2020 p++;
2021 }
2022 gui_outstr(s, (int)(p - s));
2023 s = p;
2024 }
2025 }
2026
Bram Moolenaar30613902019-12-01 22:11:18 +01002027 // Postponed update of the cursor (won't work if "can_update_cursor" isn't
2028 // set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (force_cursor)
2030 gui_update_cursor(TRUE, TRUE);
2031
Bram Moolenaar30613902019-12-01 22:11:18 +01002032 // When switching to another window the dragging must have stopped.
2033 // Required for GTK, dragged_sb isn't reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (old_curwin != curwin)
2035 gui.dragged_sb = SBAR_NONE;
2036
Bram Moolenaar30613902019-12-01 22:11:18 +01002037 // Update the scrollbars after clearing the screen or when switched
2038 // to another window.
2039 // Update the horizontal scrollbar always, it's difficult to check all
2040 // situations where it might change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 if (force_scrollbar || old_curwin != curwin)
2042 gui_update_scrollbars(force_scrollbar);
2043 else
2044 gui_update_horiz_scrollbar(FALSE);
2045 old_curwin = curwin;
2046
2047 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002048 * We need to make sure this is cleared since GTK doesn't tell us when
2049 * the user is done dragging.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 */
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002051#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 gui.dragged_sb = SBAR_NONE;
2053#endif
2054
Bram Moolenaar30613902019-12-01 22:11:18 +01002055 gui_may_flush(); // In case vim decides to take a nap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056}
2057
2058/*
2059 * When ScreenLines[] is invalid, updating the cursor should not be done, it
2060 * produces wrong results. Call gui_dont_update_cursor() before that code and
2061 * gui_can_update_cursor() afterwards.
2062 */
2063 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02002064gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002066 if (!gui.in_use)
2067 return;
2068
2069 // Undraw the cursor now, we probably can't do it after the change.
2070 if (undraw)
2071 gui_undraw_cursor();
2072 can_update_cursor = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073}
2074
2075 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002076gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077{
2078 can_update_cursor = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002079 // No need to update the cursor right now, there is always more output
2080 // after scrolling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081}
2082
Bram Moolenaara338adc2018-01-31 20:51:47 +01002083/*
2084 * Disable issuing gui_mch_flush().
2085 */
2086 void
2087gui_disable_flush(void)
2088{
2089 ++disable_flush;
2090}
2091
2092/*
2093 * Enable issuing gui_mch_flush().
2094 */
2095 void
2096gui_enable_flush(void)
2097{
2098 --disable_flush;
2099}
2100
2101/*
2102 * Issue gui_mch_flush() if it is not disabled.
2103 */
2104 void
2105gui_may_flush(void)
2106{
2107 if (disable_flush == 0)
2108 gui_mch_flush();
2109}
2110
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002112gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117 if (len == 0)
2118 return;
2119
2120 if (len < 0)
2121 len = (int)STRLEN(s);
2122
2123 while (len > 0)
2124 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if (has_mbyte)
2126 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002127 // Find out how many chars fit in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 cells = 0;
2129 for (this_len = 0; this_len < len; )
2130 {
2131 cells += (*mb_ptr2cells)(s + this_len);
2132 if (gui.col + cells > Columns)
2133 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002134 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
2136 if (this_len > len)
Bram Moolenaar30613902019-12-01 22:11:18 +01002137 this_len = len; // don't include following composing char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 }
Bram Moolenaarb26592a2022-07-12 17:34:31 +01002139 else if (gui.col + len > Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 this_len = Columns - gui.col;
2141 else
2142 this_len = len;
2143
2144 (void)gui_outstr_nowrap(s, this_len,
2145 0, (guicolor_T)0, (guicolor_T)0, 0);
2146 s += this_len;
2147 len -= this_len;
Bram Moolenaar30613902019-12-01 22:11:18 +01002148 // fill up for a double-width char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 if (len > 0 && gui.col < Columns)
2150 (void)gui_outstr_nowrap((char_u *)" ", 1,
2151 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01002152 // The cursor may wrap to the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (gui.col >= Columns)
2154 {
2155 gui.col = 0;
2156 gui.row++;
2157 }
2158 }
2159}
2160
2161/*
2162 * Output one character (may be one or two display cells).
2163 * Caller must check for valid "off".
2164 * Returns FAIL or OK, just like gui_outstr_nowrap().
2165 */
2166 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002167gui_screenchar(
Bram Moolenaar30613902019-12-01 22:11:18 +01002168 int off, // Offset from start of screen
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002169 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002170 guicolor_T fg, // colors for cursor
2171 guicolor_T bg, // colors for cursor
2172 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 char_u buf[MB_MAXBYTES + 1];
2175
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002176 // Don't draw right half of a double-width UTF-8 char. "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 if (enc_utf8 && ScreenLines[off] == 0)
2178 return OK;
2179
2180 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002181 // Draw UTF-8 multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2183 flags, fg, bg, back);
2184
2185 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2186 {
2187 buf[0] = ScreenLines[off];
2188 buf[1] = ScreenLines2[off];
2189 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2190 }
2191
Bram Moolenaar30613902019-12-01 22:11:18 +01002192 // Draw non-multi-byte character or DBCS character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002194 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196}
2197
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002198#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199/*
2200 * Output the string at the given screen position. This is used in place
2201 * of gui_screenchar() where possible because Pango needs as much context
2202 * as possible to work nicely. It's a lot faster as well.
2203 */
2204 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002205gui_screenstr(
Bram Moolenaar30613902019-12-01 22:11:18 +01002206 int off, // Offset from start of screen
2207 int len, // string length in screen cells
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002208 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002209 guicolor_T fg, // colors for cursor
2210 guicolor_T bg, // colors for cursor
2211 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212{
2213 char_u *buf;
2214 int outlen = 0;
2215 int i;
2216 int retval;
2217
Bram Moolenaar30613902019-12-01 22:11:18 +01002218 if (len <= 0) // "cannot happen"?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 return OK;
2220
2221 if (enc_utf8)
2222 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002223 buf = alloc(len * MB_MAXBYTES + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002225 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 for (i = off; i < off + len; ++i)
2228 {
2229 if (ScreenLines[i] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002230 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 if (ScreenLinesUC[i] == 0)
2233 buf[outlen++] = ScreenLines[i];
2234 else
2235 outlen += utfc_char2bytes(i, buf + outlen);
2236 }
2237
Bram Moolenaar30613902019-12-01 22:11:18 +01002238 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2240 vim_free(buf);
2241
2242 return retval;
2243 }
2244 else if (enc_dbcs == DBCS_JPNU)
2245 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002246 buf = alloc(len * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002248 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250 for (i = off; i < off + len; ++i)
2251 {
2252 buf[outlen++] = ScreenLines[i];
2253
Bram Moolenaar30613902019-12-01 22:11:18 +01002254 // handle double-byte single-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (ScreenLines[i] == 0x8e)
2256 buf[outlen++] = ScreenLines2[i];
2257 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2258 buf[outlen++] = ScreenLines[++i];
2259 }
2260
Bram Moolenaar30613902019-12-01 22:11:18 +01002261 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2263 vim_free(buf);
2264
2265 return retval;
2266 }
2267 else
2268 {
2269 return gui_outstr_nowrap(&ScreenLines[off], len,
2270 flags, fg, bg, back);
2271 }
2272}
Bram Moolenaar30613902019-12-01 22:11:18 +01002273#endif // FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
2275/*
2276 * Output the given string at the current cursor position. If the string is
2277 * too long to fit on the line, then it is truncated.
2278 * "flags":
2279 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2280 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002281 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 * background.
2283 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2284 * it.
2285 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2286 * FAIL (the caller should start drawing "back" chars back).
2287 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002288 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002289gui_outstr_nowrap(
2290 char_u *s,
2291 int len,
2292 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002293 guicolor_T fg, // colors for cursor
2294 guicolor_T bg, // colors for cursor
2295 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296{
2297 long_u highlight_mask;
2298 long_u hl_mask_todo;
2299 guicolor_T fg_color;
2300 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002301 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002302#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002304 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305# ifdef FEAT_XFONTSET
2306 GuiFontset fontset = NOFONTSET;
2307# endif
2308#endif
2309 attrentry_T *aep = NULL;
2310 int draw_flags;
2311 int col = gui.col;
2312#ifdef FEAT_SIGN_ICONS
2313 int draw_sign = FALSE;
Bram Moolenaarc7283072019-07-16 20:12:44 +02002314 int signcol = 0;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002315 char_u extra[18];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316# ifdef FEAT_NETBEANS_INTG
2317 int multi_sign = FALSE;
2318# endif
2319#endif
2320
2321 if (len < 0)
2322 len = (int)STRLEN(s);
2323 if (len == 0)
2324 return OK;
2325
2326#ifdef FEAT_SIGN_ICONS
2327 if (*s == SIGN_BYTE
2328# ifdef FEAT_NETBEANS_INTG
2329 || *s == MULTISIGN_BYTE
2330# endif
Bram Moolenaar0231f832019-07-12 19:22:22 +02002331 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333# ifdef FEAT_NETBEANS_INTG
2334 if (*s == MULTISIGN_BYTE)
2335 multi_sign = TRUE;
2336# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002337 // draw spaces instead
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002338 if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
2339 (curwin->w_p_nu || curwin->w_p_rnu))
2340 {
2341 sprintf((char *)extra, "%*c ", number_width(curwin), ' ');
2342 s = extra;
2343 }
2344 else
2345 s = (char_u *)" ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (len == 1 && col > 0)
2347 --col;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002348 len = (int)STRLEN(s);
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002349 if (len > 2)
Bram Moolenaar0231f832019-07-12 19:22:22 +02002350 // right align sign icon in the number column
2351 signcol = col + len - 3;
2352 else
2353 signcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 draw_sign = TRUE;
2355 highlight_mask = 0;
2356 }
2357 else
2358#endif
2359 if (gui.highlight_mask > HL_ALL)
2360 {
2361 aep = syn_gui_attr2entry(gui.highlight_mask);
Bram Moolenaar30613902019-12-01 22:11:18 +01002362 if (aep == NULL) // highlighting not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 highlight_mask = 0;
2364 else
2365 highlight_mask = aep->ae_attr;
2366 }
2367 else
2368 highlight_mask = gui.highlight_mask;
2369 hl_mask_todo = highlight_mask;
2370
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002371#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002372 // Set the font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2374 font = aep->ae_u.gui.font;
2375# ifdef FEAT_XFONTSET
2376 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2377 fontset = aep->ae_u.gui.fontset;
2378# endif
2379 else
2380 {
2381# ifdef FEAT_XFONTSET
2382 if (gui.fontset != NOFONTSET)
2383 fontset = gui.fontset;
2384 else
2385# endif
2386 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2387 {
2388 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2389 {
2390 font = gui.boldital_font;
2391 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2392 }
2393 else if (gui.bold_font != NOFONT)
2394 {
2395 font = gui.bold_font;
2396 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2397 }
2398 else
2399 font = gui.norm_font;
2400 }
2401 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2402 {
2403 font = gui.ital_font;
2404 hl_mask_todo &= ~HL_ITALIC;
2405 }
2406 else
2407 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002408
Bram Moolenaardb250522013-06-17 22:43:25 +02002409 /*
2410 * Choose correct wide_font by font. wide_font should be set with font
2411 * at same time in above block. But it will make many "ifdef" nasty
2412 * blocks. So we do it here.
2413 */
2414 if (font == gui.boldital_font && gui.wide_boldital_font)
2415 wide_font = gui.wide_boldital_font;
2416 else if (font == gui.bold_font && gui.wide_bold_font)
2417 wide_font = gui.wide_bold_font;
2418 else if (font == gui.ital_font && gui.wide_ital_font)
2419 wide_font = gui.wide_ital_font;
2420 else if (font == gui.norm_font && gui.wide_font)
2421 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
2423# ifdef FEAT_XFONTSET
2424 if (fontset != NOFONTSET)
2425 gui_mch_set_fontset(fontset);
2426 else
2427# endif
2428 gui_mch_set_font(font);
2429#endif
2430
2431 draw_flags = 0;
2432
Bram Moolenaar30613902019-12-01 22:11:18 +01002433 // Set the color
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 bg_color = gui.back_pixel;
2435 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2436 {
2437 draw_flags |= DRAW_CURSOR;
2438 fg_color = fg;
2439 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002440 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
2442 else if (aep != NULL)
2443 {
2444 fg_color = aep->ae_u.gui.fg_color;
2445 if (fg_color == INVALCOLOR)
2446 fg_color = gui.norm_pixel;
2447 bg_color = aep->ae_u.gui.bg_color;
2448 if (bg_color == INVALCOLOR)
2449 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002450 sp_color = aep->ae_u.gui.sp_color;
2451 if (sp_color == INVALCOLOR)
2452 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 }
2454 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002457 sp_color = fg_color;
2458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2461 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 gui_mch_set_fg_color(bg_color);
2463 gui_mch_set_bg_color(fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
2465 else
2466 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 gui_mch_set_fg_color(fg_color);
2468 gui_mch_set_bg_color(bg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002470 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
Bram Moolenaar30613902019-12-01 22:11:18 +01002472 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 if (!(flags & GUI_MON_NOCLEAR))
2474 clip_may_clear_selection(gui.row, gui.row);
2475
2476
Bram Moolenaar30613902019-12-01 22:11:18 +01002477 // If there's no bold font, then fake it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2479 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 /*
2482 * When drawing bold or italic characters the spill-over from the left
2483 * neighbor may be destroyed. Let the caller backup to start redrawing
2484 * just after a blank.
2485 */
2486 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2487 return FAIL;
2488
Bram Moolenaare60acc12011-05-10 16:41:25 +02002489#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002490 // If there's no italic font, then fake it.
2491 // For GTK2, we don't need a different font for italic style.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 if (hl_mask_todo & HL_ITALIC)
2493 draw_flags |= DRAW_ITALIC;
2494
Bram Moolenaar30613902019-12-01 22:11:18 +01002495 // Do we underline the text?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 if (hl_mask_todo & HL_UNDERLINE)
2497 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002498
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499#else
Bram Moolenaar30613902019-12-01 22:11:18 +01002500 // Do we underline the text?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002501 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 draw_flags |= DRAW_UNDERL;
2503#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002504 // Do we undercurl the text?
Bram Moolenaar3918c952005-03-15 22:34:55 +00002505 if (hl_mask_todo & HL_UNDERCURL)
2506 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
Bram Moolenaar84f54632022-06-29 18:39:11 +01002508 // TODO: HL_UNDERDOUBLE, HL_UNDERDOTTED, HL_UNDERDASHED
2509
Bram Moolenaar30613902019-12-01 22:11:18 +01002510 // Do we strikethrough the text?
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002511 if (hl_mask_todo & HL_STRIKETHROUGH)
2512 draw_flags |= DRAW_STRIKE;
2513
Bram Moolenaar30613902019-12-01 22:11:18 +01002514 // Do we draw transparently?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 if (flags & GUI_MON_TRS_CURSOR)
2516 draw_flags |= DRAW_TRANSP;
2517
2518 /*
2519 * Draw the text.
2520 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002521#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01002522 // The value returned is the length in display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2524#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 if (enc_utf8)
2526 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002527 int start; // index of bytes to be drawn
2528 int cells; // cellwidth of bytes to be drawn
2529 int thislen; // length of bytes to be drawn
2530 int cn; // cellwidth of current char
2531 int i; // index of current char
2532 int c; // current char value
2533 int cl; // byte length of current char
2534 int comping; // current char is composing
2535 int scol = col; // screen column
2536 int curr_wide = FALSE; // use 'guifontwide'
Bram Moolenaar45933962013-01-23 17:43:57 +01002537 int prev_wide = FALSE;
2538 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002539# ifdef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01002540 int sep_comp = FALSE; // Don't separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002541# else
Bram Moolenaar30613902019-12-01 22:11:18 +01002542 int sep_comp = TRUE; // Separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002543# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
Bram Moolenaar30613902019-12-01 22:11:18 +01002545 // Break the string at a composing character, it has to be drawn on
2546 // top of the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 start = 0;
2548 cells = 0;
2549 for (i = 0; i < len; i += cl)
2550 {
2551 c = utf_ptr2char(s + i);
2552 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 comping = utf_iscomposing(c);
Bram Moolenaar30613902019-12-01 22:11:18 +01002554 if (!comping) // count cells from non-composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002556 if (!comping || sep_comp)
2557 {
2558 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002559# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002560 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002561# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002562 && wide_font != NOFONT)
2563 curr_wide = TRUE;
2564 else
2565 curr_wide = FALSE;
2566 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002567 cl = utf_ptr2len(s + i);
Bram Moolenaar30613902019-12-01 22:11:18 +01002568 if (cl == 0) // hit end of string
2569 len = i + cl; // len must be wrong "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
Bram Moolenaar45933962013-01-23 17:43:57 +01002571 wide_changed = curr_wide != prev_wide;
2572
Bram Moolenaar30613902019-12-01 22:11:18 +01002573 // Print the string so far if it's the last character or there is
2574 // a composing character.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002575 if (i + cl >= len || (comping && sep_comp && i > start)
2576 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002577# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002579# ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +01002580 // No fontset: At least draw char after wide char at
2581 // right position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002584 )
2585# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 )
2587 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002588 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 thislen = i - start;
2590 else
2591 thislen = i - start + cl;
2592 if (thislen > 0)
2593 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002594 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002595 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2597 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002598 if (prev_wide)
2599 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 start += thislen;
2601 }
2602 scol += cells;
2603 cells = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01002604 // Adjust to not draw a character which width is changed
2605 // against with last one.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002606 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002608 scol -= cn;
2609 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 }
2611
Bram Moolenaar13505972019-01-24 15:04:48 +01002612# if defined(FEAT_GUI_X11)
Bram Moolenaar30613902019-12-01 22:11:18 +01002613 // No fontset: draw a space to fill the gap after a wide char
2614 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002616# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002618# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002619 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2621 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002624 // Draw a composing char on top of the previous char.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002625 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002627# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaar30613902019-12-01 22:11:18 +01002628 // Carbon ATSUI autodraws composing char over previous char
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002629 gui_mch_draw_string(gui.row, scol, s + i, cl,
2630 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002631# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002632 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2633 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002634# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 start = i + cl;
2636 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002637 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002639 // The stuff below assumes "len" is the length in screen columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 len = scol - col;
2641 }
2642 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 {
2644 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 if (enc_dbcs == DBCS_JPNU)
2646 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002647 // Get the length in display cells, this can be different from the
2648 // number of bytes for "euc-jp".
Bram Moolenaar72597a52010-07-18 15:31:08 +02002649 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002652#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2655 gui.col = col + len;
2656
Bram Moolenaar30613902019-12-01 22:11:18 +01002657 // May need to invert it when it's part of the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (flags & GUI_MON_NOCLEAR)
2659 clip_may_redraw_selection(gui.row, col, len);
2660
2661 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2662 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002663 // Invalidate the old physical cursor position if we wrote over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (gui.cursor_row == gui.row
2665 && gui.cursor_col >= col
2666 && gui.cursor_col < col + len)
2667 gui.cursor_is_valid = FALSE;
2668 }
2669
2670#ifdef FEAT_SIGN_ICONS
2671 if (draw_sign)
Bram Moolenaar30613902019-12-01 22:11:18 +01002672 // Draw the sign on top of the spaces.
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002673 gui_mch_drawsign(gui.row, signcol, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002674# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002675 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (multi_sign)
2677 netbeans_draw_multisign_indicator(gui.row);
2678# endif
2679#endif
2680
2681 return OK;
2682}
2683
2684/*
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002685 * Undraw the cursor. This actually redraws the character at the cursor
2686 * position, plus some more characters when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 */
2688 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002689gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002691 if (!gui.cursor_is_valid)
2692 return;
2693
2694 // Always redraw the character just before if there is one, because
2695 // with some fonts and characters there can be a one pixel overlap.
2696 int startcol = gui.cursor_col > 0 ? gui.cursor_col - 1 : gui.cursor_col;
2697 int endcol = gui.cursor_col;
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002698
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002699#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002700 gui_adjust_undraw_cursor_for_ligatures(&startcol, &endcol);
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002701#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002702 gui_redraw_block(gui.cursor_row, startcol,
2703 gui.cursor_row, endcol, GUI_MON_NOCLEAR);
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002704
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002705 // Cursor_is_valid is reset when the cursor is undrawn, also reset it
2706 // here in case it wasn't needed to undraw it.
2707 gui.cursor_is_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708}
2709
2710 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002711gui_redraw(
2712 int x,
2713 int y,
2714 int w,
2715 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716{
2717 int row1, col1, row2, col2;
2718
2719 row1 = Y_2_ROW(y);
2720 col1 = X_2_COL(x);
2721 row2 = Y_2_ROW(y + h - 1);
2722 col2 = X_2_COL(x + w - 1);
2723
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002724 gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
2726 /*
2727 * We may need to redraw the cursor, but don't take it upon us to change
2728 * its location after a scroll.
2729 * (maybe be more strict even and test col too?)
2730 * These things may be outside the update/clipping region and reality may
2731 * not reflect Vims internal ideas if these operations are clipped away.
2732 */
2733 if (gui.row == gui.cursor_row)
2734 gui_update_cursor(TRUE, TRUE);
2735}
2736
2737/*
2738 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2739 * from col1 to col2 (inclusive).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 */
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002741 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002742gui_redraw_block(
2743 int row1,
2744 int col1,
2745 int row2,
2746 int col2,
Bram Moolenaar30613902019-12-01 22:11:18 +01002747 int flags) // flags for gui_outstr_nowrap()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748{
2749 int old_row, old_col;
2750 long_u old_hl_mask;
2751 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002752 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 int idx, len;
2754 int back, nback;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
Bram Moolenaar30613902019-12-01 22:11:18 +01002757 // Don't try to update when ScreenLines is not valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 if (!screen_cleared || ScreenLines == NULL)
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002759 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
Bram Moolenaar30613902019-12-01 22:11:18 +01002761 // Don't try to draw outside the shell!
2762 // Check everything, strange values may be caused by a big border width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 col1 = check_col(col1);
2764 col2 = check_col(col2);
2765 row1 = check_row(row1);
2766 row2 = check_row(row2);
2767
Bram Moolenaar30613902019-12-01 22:11:18 +01002768 // Remember where our cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 old_row = gui.row;
2770 old_col = gui.col;
2771 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 orig_col1 = col1;
2773 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774
2775 for (gui.row = row1; gui.row <= row2; gui.row++)
2776 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002777 // When only half of a double-wide character is in the block, include
2778 // the other half.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 col1 = orig_col1;
2780 col2 = orig_col2;
2781 off = LineOffset[gui.row];
2782 if (enc_dbcs != 0)
2783 {
2784 if (col1 > 0)
2785 col1 -= dbcs_screen_head_off(ScreenLines + off,
2786 ScreenLines + off + col1);
2787 col2 += dbcs_screen_tail_off(ScreenLines + off,
2788 ScreenLines + off + col2);
2789 }
2790 else if (enc_utf8)
2791 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002792 if (ScreenLines[off + col1] == 0)
2793 {
2794 if (col1 > 0)
2795 --col1;
2796 else
2797 // FIXME: how can the first character ever be zero?
Bram Moolenaar097c5372023-05-24 21:02:24 +01002798 siemsg("NUL in ScreenLines in row %ld", (long)gui.row);
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002799 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002800#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2802 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 gui.col = col1;
2806 off = LineOffset[gui.row] + gui.col;
2807 len = col2 - col1 + 1;
2808
Bram Moolenaar30613902019-12-01 22:11:18 +01002809 // Find how many chars back this highlighting starts, or where a space
2810 // is. Needed for when the bold trick is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 for (back = 0; back < col1; ++back)
2812 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2813 || ScreenLines[off - 1 - back] == ' ')
2814 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
Bram Moolenaar30613902019-12-01 22:11:18 +01002816 // Break it up in strings of characters with the same attributes.
2817 // Print UTF-8 characters individually.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 while (len > 0)
2819 {
2820 first_attr = ScreenAttrs[off];
2821 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002822#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 if (enc_utf8 && ScreenLinesUC[off] != 0)
2824 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002825 // output multi-byte character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 nback = gui_screenchar(off, flags,
2827 (guicolor_T)0, (guicolor_T)0, back);
2828 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2829 idx = 2;
2830 else
2831 idx = 1;
2832 }
2833 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2834 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002835 // output double-byte, single-width character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 nback = gui_screenchar(off, flags,
2837 (guicolor_T)0, (guicolor_T)0, back);
2838 idx = 1;
2839 }
2840 else
2841#endif
2842 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002843#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 for (idx = 0; idx < len; ++idx)
2845 {
2846 if (enc_utf8 && ScreenLines[off + idx] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002847 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (ScreenAttrs[off + idx] != first_attr)
2849 break;
2850 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002851 // gui_screenstr() takes care of multibyte chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 nback = gui_screenstr(off, idx, flags,
2853 (guicolor_T)0, (guicolor_T)0, back);
2854#else
2855 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2856 idx++)
2857 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002858 // Stop at a multi-byte Unicode character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2860 break;
2861 if (enc_dbcs == DBCS_JPNU)
2862 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002863 // Stop at a double-byte single-width char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 if (ScreenLines[off + idx] == 0x8e)
2865 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002866 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 + off + idx) == 2)
Bram Moolenaar30613902019-12-01 22:11:18 +01002868 ++idx; // skip second byte of double-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
2871 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2872 (guicolor_T)0, (guicolor_T)0, back);
2873#endif
2874 }
2875 if (nback == FAIL)
2876 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002877 // Must back up to start drawing where a bold or italic word
2878 // starts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 off -= back;
2880 len += back;
2881 gui.col -= back;
2882 }
2883 else
2884 {
2885 off += idx;
2886 len -= idx;
2887 }
2888 back = 0;
2889 }
2890 }
2891
Bram Moolenaar30613902019-12-01 22:11:18 +01002892 // Put the cursor back where it was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 gui.row = old_row;
2894 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002895 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896}
2897
2898 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002899gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
2901 if (count <= 0)
2902 return;
2903
2904 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002905 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 gui_clear_block(row, gui.scroll_region_left,
2907 gui.scroll_region_bot, gui.scroll_region_right);
2908 else
2909 {
2910 gui_mch_delete_lines(row, count);
2911
Bram Moolenaar30613902019-12-01 22:11:18 +01002912 // If the cursor was in the deleted lines it's now gone. If the
2913 // cursor was in the scrolled lines adjust its position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 if (gui.cursor_row >= row
2915 && gui.cursor_col >= gui.scroll_region_left
2916 && gui.cursor_col <= gui.scroll_region_right)
2917 {
2918 if (gui.cursor_row < row + count)
2919 gui.cursor_is_valid = FALSE;
2920 else if (gui.cursor_row <= gui.scroll_region_bot)
2921 gui.cursor_row -= count;
2922 }
2923 }
2924}
2925
2926 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002927gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
2929 if (count <= 0)
2930 return;
2931
2932 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002933 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 gui_clear_block(row, gui.scroll_region_left,
2935 gui.scroll_region_bot, gui.scroll_region_right);
2936 else
2937 {
2938 gui_mch_insert_lines(row, count);
2939
2940 if (gui.cursor_row >= gui.row
2941 && gui.cursor_col >= gui.scroll_region_left
2942 && gui.cursor_col <= gui.scroll_region_right)
2943 {
2944 if (gui.cursor_row <= gui.scroll_region_bot - count)
2945 gui.cursor_row += count;
2946 else if (gui.cursor_row <= gui.scroll_region_bot)
2947 gui.cursor_is_valid = FALSE;
2948 }
2949 }
2950}
2951
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002952#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002953/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002954 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2955 */
2956 static int
2957gui_wait_for_chars_3(
2958 long wtime,
2959 int *interrupted UNUSED,
2960 int ignore_input UNUSED)
2961{
2962 return gui_mch_wait_for_chars(wtime);
2963}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002964#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002965
2966/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002967 * Returns OK if a character was found to be available within the given time,
2968 * or FAIL otherwise.
2969 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002970 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002971gui_wait_for_chars_or_timer(
2972 long wtime,
2973 int *interrupted UNUSED,
2974 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002975{
2976#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002977 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2978 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002979#else
2980 return gui_mch_wait_for_chars(wtime);
2981#endif
2982}
2983
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984/*
2985 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002986 * "wtime" == -1 Wait forever.
2987 * "wtime" == 0 Don't wait.
2988 * "wtime" > 0 Wait wtime milliseconds for a character.
2989 *
2990 * Returns the number of characters read or zero when timed out or interrupted.
2991 * "buf" may be NULL, in which case a non-zero number is returned if characters
2992 * are available.
2993 */
2994 static int
2995gui_wait_for_chars_buf(
2996 char_u *buf,
2997 int maxlen,
2998 long wtime, // don't use "time", MIPS cannot handle it
2999 int tb_change_cnt)
3000{
3001 int retval;
3002
3003#ifdef FEAT_MENU
3004 // If we're going to wait a bit, update the menus and mouse shape for the
3005 // current State.
3006 if (wtime != 0)
3007 gui_update_menus(0);
3008#endif
3009
3010 gui_mch_update();
3011 if (input_available()) // Got char, return immediately
3012 {
3013 if (buf != NULL && !typebuf_changed(tb_change_cnt))
3014 return read_from_input_buf(buf, (long)maxlen);
3015 return 0;
3016 }
3017 if (wtime == 0) // Don't wait for char
3018 return FAIL;
3019
3020 // Before waiting, flush any output to the screen.
3021 gui_mch_flush();
3022
3023 // Blink while waiting for a character.
3024 gui_mch_start_blink();
3025
3026 // Common function to loop until "wtime" is met, while handling timers and
3027 // other callbacks.
3028 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
3029 gui_wait_for_chars_or_timer, NULL);
3030
3031 gui_mch_stop_blink(TRUE);
3032
3033 return retval;
3034}
3035
3036/*
3037 * Wait for a character from the keyboard without actually reading it.
3038 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 * wtime == -1 Wait forever.
3040 * wtime == 0 Don't wait.
3041 * wtime > 0 Wait wtime milliseconds for a character.
3042 * Returns OK if a character was found to be available within the given time,
3043 * or FAIL otherwise.
3044 */
3045 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003046gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003048 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049}
3050
3051/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003052 * Equivalent of mch_inchar() for the GUI.
3053 */
3054 int
3055gui_inchar(
3056 char_u *buf,
3057 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003058 long wtime, // milliseconds
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003059 int tb_change_cnt)
3060{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003061 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003062}
3063
3064/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003065 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 */
3067 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003068fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070 p[0] = (char_u)(col / 128 + ' ' + 1);
3071 p[1] = (char_u)(col % 128 + ' ' + 1);
3072 p[2] = (char_u)(row / 128 + ' ' + 1);
3073 p[3] = (char_u)(row % 128 + ' ' + 1);
3074}
3075
3076/*
3077 * Generic mouse support function. Add a mouse event to the input buffer with
3078 * the given properties.
3079 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3080 * MOUSE_X1, MOUSE_X2
3081 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003082 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3083 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 * x, y --- Coordinates of mouse in pixels.
3085 * repeated_click --- TRUE if this click comes only a short time after a
3086 * previous click.
3087 * modifiers --- Bit field which may be any of the following modifiers
3088 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3089 * This function will ignore drag events where the mouse has not moved to a new
3090 * character.
3091 */
3092 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003093gui_send_mouse_event(
3094 int button,
3095 int x,
3096 int y,
3097 int repeated_click,
3098 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 static int prev_row = 0, prev_col = 0;
3101 static int prev_button = -1;
3102 static int num_clicks = 1;
3103 char_u string[10];
3104 enum key_extra button_char;
3105 int row, col;
3106#ifdef FEAT_CLIPBOARD
3107 int checkfor;
3108 int did_clip = FALSE;
3109#endif
3110
3111 /*
3112 * Scrolling may happen at any time, also while a selection is present.
3113 */
3114 switch (button)
3115 {
Bram Moolenaar445f11d2021-06-08 20:13:31 +02003116 case MOUSE_MOVE:
3117 button_char = KE_MOUSEMOVE_XY;
3118 goto button_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 case MOUSE_X1:
3120 button_char = KE_X1MOUSE;
3121 goto button_set;
3122 case MOUSE_X2:
3123 button_char = KE_X2MOUSE;
3124 goto button_set;
3125 case MOUSE_4:
3126 button_char = KE_MOUSEDOWN;
3127 goto button_set;
3128 case MOUSE_5:
3129 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003130 goto button_set;
3131 case MOUSE_6:
3132 button_char = KE_MOUSELEFT;
3133 goto button_set;
3134 case MOUSE_7:
3135 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136button_set:
3137 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003138 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 if (hold_gui_events)
3140 return;
3141
Ernie Raelc4cb5442022-04-03 15:47:28 +01003142 row = gui_xy2colrow(x, y, &col);
3143 // Don't report a mouse move unless moved to a
3144 // different character position.
3145 if (button == MOUSE_MOVE)
3146 {
3147 if (row == prev_row && col == prev_col)
3148 return;
3149 else
3150 {
3151 prev_row = row >= 0 ? row : 0;
3152 prev_col = col;
3153 }
3154 }
3155
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 string[3] = CSI;
3157 string[4] = KS_EXTRA;
3158 string[5] = (int)button_char;
3159
Bram Moolenaar30613902019-12-01 22:11:18 +01003160 // Pass the pointer coordinates of the scroll event so that we
3161 // know which window to scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 string[6] = (char_u)(col / 128 + ' ' + 1);
3163 string[7] = (char_u)(col % 128 + ' ' + 1);
3164 string[8] = (char_u)(row / 128 + ' ' + 1);
3165 string[9] = (char_u)(row % 128 + ' ' + 1);
3166
3167 if (modifiers == 0)
3168 add_to_input_buf(string + 3, 7);
3169 else
3170 {
3171 string[0] = CSI;
3172 string[1] = KS_MODIFIER;
3173 string[2] = 0;
3174 if (modifiers & MOUSE_SHIFT)
3175 string[2] |= MOD_MASK_SHIFT;
3176 if (modifiers & MOUSE_CTRL)
3177 string[2] |= MOD_MASK_CTRL;
3178 if (modifiers & MOUSE_ALT)
3179 string[2] |= MOD_MASK_ALT;
3180 add_to_input_buf(string, 10);
3181 }
3182 return;
3183 }
3184 }
3185
3186#ifdef FEAT_CLIPBOARD
Bram Moolenaar30613902019-12-01 22:11:18 +01003187 // If a clipboard selection is in progress, handle it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (clip_star.state == SELECT_IN_PROGRESS)
3189 {
3190 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
Bram Moolenaar0ce37332019-12-18 21:33:22 +01003191
3192 // A release event may still need to be sent if the position is equal.
3193 row = gui_xy2colrow(x, y, &col);
3194 if (button != MOUSE_RELEASE || row != prev_row || col != prev_col)
3195 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
3197
Bram Moolenaar30613902019-12-01 22:11:18 +01003198 // Determine which mouse settings to look for based on the current mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 switch (get_real_state())
3200 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003201 case MODE_NORMAL_BUSY:
3202 case MODE_OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003203# ifdef FEAT_TERMINAL
Bram Moolenaar24959102022-05-07 20:01:16 +01003204 case MODE_TERMINAL:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003205# endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003206 case MODE_NORMAL: checkfor = MOUSE_NORMAL; break;
3207 case MODE_VISUAL: checkfor = MOUSE_VISUAL; break;
3208 case MODE_SELECT: checkfor = MOUSE_VISUAL; break;
3209 case MODE_REPLACE:
3210 case MODE_REPLACE | MODE_LANGMAP:
3211 case MODE_VREPLACE:
3212 case MODE_VREPLACE | MODE_LANGMAP:
3213 case MODE_INSERT:
3214 case MODE_INSERT | MODE_LANGMAP:
3215 checkfor = MOUSE_INSERT; break;
3216 case MODE_ASKMORE:
3217 case MODE_HITRETURN: // At the more- and hit-enter prompt pass the
Bram Moolenaar30613902019-12-01 22:11:18 +01003218 // mouse event for a click on or below the
3219 // message line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (Y_2_ROW(y) >= msg_row)
3221 checkfor = MOUSE_NORMAL;
3222 else
3223 checkfor = MOUSE_RETURN;
3224 break;
3225
3226 /*
3227 * On the command line, use the clipboard selection on all lines
3228 * but the command line. But not when pasting.
3229 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003230 case MODE_CMDLINE:
3231 case MODE_CMDLINE | MODE_LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3233 checkfor = MOUSE_NONE;
3234 else
3235 checkfor = MOUSE_COMMAND;
3236 break;
3237
3238 default:
3239 checkfor = MOUSE_NONE;
3240 break;
3241 };
3242
3243 /*
3244 * Allow clipboard selection of text on the command line in "normal"
3245 * modes. Don't do this when dragging the status line, or extending a
3246 * Visual selection.
3247 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003248 if ((State == MODE_NORMAL || State == MODE_NORMAL_BUSY
3249 || (State & MODE_INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003250 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 && button != MOUSE_DRAG
3252# ifdef FEAT_MOUSESHAPE
3253 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255# endif
3256 )
3257 checkfor = MOUSE_NONE;
3258
3259 /*
3260 * Use modeless selection when holding CTRL and SHIFT pressed.
3261 */
3262 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3263 checkfor = MOUSE_NONEF;
3264
3265 /*
3266 * In Ex mode, always use modeless selection.
3267 */
3268 if (exmode_active)
3269 checkfor = MOUSE_NONE;
3270
3271 /*
3272 * If the mouse settings say to not use the mouse, use the modeless
3273 * selection. But if Visual is active, assume that only the Visual area
3274 * will be selected.
3275 * Exception: On the command line, both the selection is used and a mouse
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003276 * key is sent.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 */
3278 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3279 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003280 // Don't do modeless selection in Visual mode.
Bram Moolenaar24959102022-05-07 20:01:16 +01003281 if (checkfor != MOUSE_NONEF && VIsual_active && (State & MODE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
3284 /*
3285 * When 'mousemodel' is "popup", shift-left is translated to right.
3286 * But not when also using Ctrl.
3287 */
3288 if (mouse_model_popup() && button == MOUSE_LEFT
3289 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3290 {
3291 button = MOUSE_RIGHT;
3292 modifiers &= ~ MOUSE_SHIFT;
3293 }
3294
Bram Moolenaar30613902019-12-01 22:11:18 +01003295 // If the selection is done, allow the right button to extend it.
3296 // If the selection is cleared, allow the right button to start it
3297 // from the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 if (button == MOUSE_RIGHT)
3299 {
3300 if (clip_star.state == SELECT_CLEARED)
3301 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003302 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
3304 col = msg_col;
3305 row = msg_row;
3306 }
3307 else
3308 {
3309 col = curwin->w_wcol;
3310 row = curwin->w_wrow + W_WINROW(curwin);
3311 }
3312 clip_start_selection(col, row, FALSE);
3313 }
3314 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3315 repeated_click);
3316 did_clip = TRUE;
3317 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003318 // Allow the left button to start the selection
Bram Moolenaare60acc12011-05-10 16:41:25 +02003319 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
3321 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3322 did_clip = TRUE;
3323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
Bram Moolenaar30613902019-12-01 22:11:18 +01003325 // Always allow pasting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 if (button != MOUSE_MIDDLE)
3327 {
3328 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3329 return;
3330 if (checkfor != MOUSE_COMMAND)
3331 button = MOUSE_LEFT;
3332 }
3333 repeated_click = FALSE;
3334 }
3335
3336 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003337 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#endif
3339
Bram Moolenaar30613902019-12-01 22:11:18 +01003340 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 if (hold_gui_events)
3342 return;
3343
3344 row = gui_xy2colrow(x, y, &col);
3345
3346 /*
3347 * If we are dragging and the mouse hasn't moved far enough to be on a
3348 * different character, then don't send an event to vim.
3349 */
3350 if (button == MOUSE_DRAG)
3351 {
3352 if (row == prev_row && col == prev_col)
3353 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01003354 // Dragging above the window, set "row" to -1 to cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (y < 0)
3356 row = -1;
3357 }
3358
3359 /*
3360 * If topline has changed (window scrolled) since the last click, reset
3361 * repeated_click, because we don't want starting Visual mode when
3362 * clicking on a different character in the text.
3363 */
3364 if (curwin->w_topline != gui_prev_topline
3365#ifdef FEAT_DIFF
3366 || curwin->w_topfill != gui_prev_topfill
3367#endif
3368 )
3369 repeated_click = FALSE;
3370
Bram Moolenaar30613902019-12-01 22:11:18 +01003371 string[0] = CSI; // this sequence is recognized by check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 string[1] = KS_MOUSE;
3373 string[2] = KE_FILLER;
3374 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3375 {
3376 if (repeated_click)
3377 {
3378 /*
3379 * Handle multiple clicks. They only count if the mouse is still
3380 * pointing at the same character.
3381 */
3382 if (button != prev_button || row != prev_row || col != prev_col)
3383 num_clicks = 1;
3384 else if (++num_clicks > 4)
3385 num_clicks = 1;
3386 }
3387 else
3388 num_clicks = 1;
3389 prev_button = button;
3390 gui_prev_topline = curwin->w_topline;
3391#ifdef FEAT_DIFF
3392 gui_prev_topfill = curwin->w_topfill;
3393#endif
3394
3395 string[3] = (char_u)(button | 0x20);
3396 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3397 }
3398 else
3399 string[3] = (char_u)button;
3400
3401 string[3] |= modifiers;
3402 fill_mouse_coord(string + 4, col, row);
3403 add_to_input_buf(string, 8);
3404
3405 if (row < 0)
3406 prev_row = 0;
3407 else
3408 prev_row = row;
3409 prev_col = col;
3410
3411 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01003412 * We need to make sure this is cleared since GTK doesn't tell us when
3413 * the user is done dragging.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 */
Bram Moolenaar0b962e52022-04-03 18:02:37 +01003415#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 gui.dragged_sb = SBAR_NONE;
3417#endif
3418}
3419
3420/*
3421 * Convert x and y coordinate to column and row in text window.
3422 * Corrects for multi-byte character.
3423 * returns column in "*colp" and row as return value;
3424 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003425 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003426gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
3428 int col = check_col(X_2_COL(x));
3429 int row = check_row(Y_2_ROW(y));
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 return row;
3433}
3434
3435#if defined(FEAT_MENU) || defined(PROTO)
3436/*
3437 * Callback function for when a menu entry has been selected.
3438 */
3439 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003440gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003442 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaar30613902019-12-01 22:11:18 +01003444 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 if (hold_gui_events)
3446 return;
3447
3448 bytes[0] = CSI;
3449 bytes[1] = KS_MENU;
3450 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003451 add_to_input_buf(bytes, 3);
3452 add_long_to_buf((long_u)menu, bytes);
3453 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454}
3455#endif
3456
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003457static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003458
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459/*
3460 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003461 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 * in p_go.
3463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003465gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003467#ifdef FEAT_GUI_DARKTHEME
3468 static int prev_dark_theme = -1;
3469 int using_dark_theme = FALSE;
3470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471#ifdef FEAT_MENU
3472 static int prev_menu_is_active = -1;
3473#endif
3474#ifdef FEAT_TOOLBAR
3475 static int prev_toolbar = -1;
3476 int using_toolbar = FALSE;
3477#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003478#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003479 int using_tabline;
3480#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003481#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 static int prev_tearoff = -1;
3483 int using_tearoff = FALSE;
3484#endif
3485
3486 char_u *p;
3487 int i;
3488#ifdef FEAT_MENU
3489 int grey_old, grey_new;
3490 char_u *temp;
3491#endif
3492 win_T *wp;
3493 int need_set_size;
3494 int fix_size;
3495
3496#ifdef FEAT_MENU
3497 if (oldval != NULL && gui.in_use)
3498 {
3499 /*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003500 * Check if the menus go from grey to non-grey or vice versa.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 */
3502 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3503 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3504 if (grey_old != grey_new)
3505 {
3506 temp = p_go;
3507 p_go = oldval;
3508 gui_update_menus(MENU_ALL_MODES);
3509 p_go = temp;
3510 }
3511 }
3512 gui.menu_is_active = FALSE;
3513#endif
3514
3515 for (i = 0; i < 3; i++)
3516 gui.which_scrollbars[i] = FALSE;
3517 for (p = p_go; *p; p++)
3518 switch (*p)
3519 {
3520 case GO_LEFT:
3521 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3522 break;
3523 case GO_RIGHT:
3524 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3525 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 case GO_VLEFT:
3527 if (win_hasvertsplit())
3528 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3529 break;
3530 case GO_VRIGHT:
3531 if (win_hasvertsplit())
3532 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3533 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 case GO_BOT:
3535 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3536 break;
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003537#ifdef FEAT_GUI_DARKTHEME
3538 case GO_DARKTHEME:
3539 using_dark_theme = TRUE;
3540 break;
3541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542#ifdef FEAT_MENU
3543 case GO_MENUS:
3544 gui.menu_is_active = TRUE;
3545 break;
3546#endif
3547 case GO_GREY:
Bram Moolenaar30613902019-12-01 22:11:18 +01003548 // make menu's have grey items, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 break;
3550#ifdef FEAT_TOOLBAR
3551 case GO_TOOLBAR:
3552 using_toolbar = TRUE;
3553 break;
3554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003556#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 using_tearoff = TRUE;
3558#endif
3559 break;
3560 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01003561 // Ignore options that are not supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 break;
3563 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003564
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003565 if (!gui.in_use)
3566 return;
3567
3568 need_set_size = 0;
3569 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003570
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003571#ifdef FEAT_GUI_DARKTHEME
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003572 if (using_dark_theme != prev_dark_theme)
3573 {
3574 gui_mch_set_dark_theme(using_dark_theme);
3575 prev_dark_theme = using_dark_theme;
3576 }
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003577#endif
3578
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003579#ifdef FEAT_GUI_TABLINE
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003580 // Update the GUI tab line, it may appear or disappear. This may
3581 // cause the non-GUI tab line to disappear or appear.
3582 using_tabline = gui_has_tabline();
3583 if (!gui_mch_showing_tabline() != !using_tabline)
3584 {
3585 // We don't want a resize event change "Rows" here, save and
3586 // restore it. Resizing is handled below.
3587 i = Rows;
3588 gui_update_tabline();
3589 Rows = i;
3590 need_set_size |= RESIZE_VERT;
3591 if (using_tabline)
3592 fix_size = TRUE;
3593 if (!gui_use_tabline())
3594 redraw_tabline = TRUE; // may draw non-GUI tab line
3595 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003596#endif
3597
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003598 for (i = 0; i < 3; i++)
3599 {
3600 // The scrollbar needs to be updated when it is shown/unshown and
3601 // when switching tab pages. But the size only changes when it's
3602 // shown/unshown. Thus we need two places to remember whether a
3603 // scrollbar is there or not.
3604 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3605 || gui.which_scrollbars[i]
3606 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003608 if (i == SBAR_BOTTOM)
3609 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3610 gui.which_scrollbars[i]);
3611 else
3612 {
3613 FOR_ALL_WINDOWS(wp)
3614 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3615 }
3616 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 if (i == SBAR_BOTTOM)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003619 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 else
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003621 need_set_size |= RESIZE_HOR;
3622 if (gui.which_scrollbars[i])
3623 fix_size = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003626 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3627 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
3630#ifdef FEAT_MENU
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003631 if (gui.menu_is_active != prev_menu_is_active)
3632 {
3633 // We don't want a resize event change "Rows" here, save and
3634 // restore it. Resizing is handled below.
3635 i = Rows;
3636 gui_mch_enable_menu(gui.menu_is_active);
3637 Rows = i;
3638 prev_menu_is_active = gui.menu_is_active;
3639 need_set_size |= RESIZE_VERT;
3640 if (gui.menu_is_active)
3641 fix_size = TRUE;
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643#endif
3644
3645#ifdef FEAT_TOOLBAR
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003646 if (using_toolbar != prev_toolbar)
3647 {
3648 gui_mch_show_toolbar(using_toolbar);
3649 prev_toolbar = using_toolbar;
3650 need_set_size |= RESIZE_VERT;
3651 if (using_toolbar)
3652 fix_size = TRUE;
3653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003655#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003656 if (using_tearoff != prev_tearoff)
3657 {
3658 gui_mch_toggle_tearoffs(using_tearoff);
3659 prev_tearoff = using_tearoff;
3660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003662 if (need_set_size != 0)
3663 {
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003664#ifdef FEAT_GUI_GTK
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003665 long prev_Columns = Columns;
3666 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003667#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003668 // Adjust the size of the window to make the text area keep the
3669 // same size and to avoid that part of our window is off-screen
3670 // and a scrollbar can't be used, for example.
3671 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003672
3673#ifdef FEAT_GUI_GTK
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003674 // GTK has the annoying habit of sending us resize events when
3675 // changing the window size ourselves. This mostly happens when
3676 // waiting for a character to arrive, quite unpredictably, and may
3677 // change Columns and Rows when we don't want it. Wait for a
3678 // character here to avoid this effect.
3679 // If you remove this, please test this command for resizing
3680 // effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3681 // Don't do this while starting up though.
3682 // Don't change Rows when adding menu/toolbar/tabline.
3683 // Don't change Columns when adding vertical toolbar.
3684 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
3685 (void)char_avail();
3686 if ((need_set_size & RESIZE_VERT) == 0)
3687 Rows = prev_Rows;
3688 if ((need_set_size & RESIZE_HOR) == 0)
3689 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003692 // When the console tabline appears or disappears the window positions
3693 // change.
3694 if (firstwin->w_winrow != tabline_height())
3695 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696}
3697
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003698#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3699/*
3700 * Return TRUE if the GUI is taking care of the tabline.
3701 * It may still be hidden if 'showtabline' is zero.
3702 */
3703 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003704gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003705{
3706 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3707}
3708
3709/*
3710 * Return TRUE if the GUI is showing the tabline.
3711 * This uses 'showtabline'.
3712 */
3713 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003714gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003715{
3716 if (!gui_use_tabline()
3717 || p_stal == 0
3718 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3719 return FALSE;
3720 return TRUE;
3721}
3722
3723/*
3724 * Update the tabline.
3725 * This may display/undisplay the tabline and update the labels.
3726 */
3727 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003728gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003729{
3730 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003731 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003732
3733 if (!gui.starting && starting == 0)
3734 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003735 // Updating the tabline uses direct GUI commands, flush
3736 // outstanding instructions first. (esp. clear screen)
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003737 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003738
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003739 if (!showit != !shown)
3740 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003741 if (showit != 0)
3742 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003743
Bram Moolenaar30613902019-12-01 22:11:18 +01003744 // When the tabs change from hidden to shown or from shown to
3745 // hidden the size of the text area should remain the same.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003746 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003747 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003748 }
3749}
3750
3751/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003752 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003753 */
3754 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003755get_tabline_label(
3756 tabpage_T *tp,
Bram Moolenaar30613902019-12-01 22:11:18 +01003757 int tooltip) // TRUE: get tooltip
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003758{
3759 int modified = FALSE;
3760 char_u buf[40];
3761 int wincount;
3762 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003763 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003764
Bram Moolenaar30613902019-12-01 22:11:18 +01003765 // Use 'guitablabel' or 'guitabtooltip' if it's set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003766 opt = (tooltip ? &p_gtt : &p_gtl);
3767 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003768 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003769 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003770 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003771 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3772 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003773
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003774 printer_page_num = tabpage_index(tp);
3775# ifdef FEAT_EVAL
3776 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003777# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003778 // It's almost as going to the tabpage, but without autocommands.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003779 curtab->tp_firstwin = firstwin;
3780 curtab->tp_lastwin = lastwin;
3781 curtab->tp_curwin = curwin;
3782 save_curtab = curtab;
3783 curtab = tp;
3784 topframe = curtab->tp_topframe;
3785 firstwin = curtab->tp_firstwin;
3786 lastwin = curtab->tp_lastwin;
3787 curwin = curtab->tp_curwin;
3788 curbuf = curwin->w_buffer;
3789
Bram Moolenaar30613902019-12-01 22:11:18 +01003790 // Can't use NameBuff directly, build_stl_str_hl() uses it.
Luuk van Baal7b224fd2022-11-07 12:16:51 +00003791 build_stl_str_hl(curwin, res, MAXPATHL, *opt, opt_name, 0,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003792 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003793 STRCPY(NameBuff, res);
3794
Bram Moolenaar30613902019-12-01 22:11:18 +01003795 // Back to the original curtab.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003796 curtab = save_curtab;
3797 topframe = curtab->tp_topframe;
3798 firstwin = curtab->tp_firstwin;
3799 lastwin = curtab->tp_lastwin;
3800 curwin = curtab->tp_curwin;
3801 curbuf = curwin->w_buffer;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003802 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003803
Bram Moolenaar30613902019-12-01 22:11:18 +01003804 // If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3805 // use a default label.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003806 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003807 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003808 // Get the buffer name into NameBuff[] and shorten it.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003809 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003810 if (!tooltip)
3811 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003812
3813 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3814 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3815 if (bufIsChanged(wp->w_buffer))
3816 modified = TRUE;
3817 if (modified || wincount > 1)
3818 {
3819 if (wincount > 1)
3820 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3821 else
3822 buf[0] = NUL;
3823 if (modified)
3824 STRCAT(buf, "+");
3825 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003826 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003827 mch_memmove(NameBuff, buf, STRLEN(buf));
3828 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003829 }
3830}
3831
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003832/*
3833 * Send the event for clicking to select tab page "nr".
3834 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003835 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003836 */
3837 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003838send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003839{
3840 char_u string[3];
3841
3842 if (nr == tabpage_index(curtab))
3843 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003844
Bram Moolenaar30613902019-12-01 22:11:18 +01003845 // Don't put events in the input queue now.
Martin Tournoij7904fa42022-10-04 16:28:45 +01003846 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003847 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003848 // Set it back to the current tab page.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003849 gui_mch_set_curtab(tabpage_index(curtab));
3850 return FALSE;
3851 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003852
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003853 string[0] = CSI;
3854 string[1] = KS_TABLINE;
3855 string[2] = KE_FILLER;
3856 add_to_input_buf(string, 3);
3857 string[0] = nr;
3858 add_to_input_buf_csi(string, 1);
3859 return TRUE;
3860}
3861
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003862/*
3863 * Send a tabline menu event
3864 */
3865 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003866send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003867{
3868 char_u string[3];
3869
Bram Moolenaar29547192018-12-11 20:39:19 +01003870 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003871 if (hold_gui_events)
3872 return;
3873
Bram Moolenaar29547192018-12-11 20:39:19 +01003874 // Cannot close the last tabpage.
3875 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3876 return;
3877
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003878 string[0] = CSI;
3879 string[1] = KS_TABMENU;
3880 string[2] = KE_FILLER;
3881 add_to_input_buf(string, 3);
3882 string[0] = tabidx;
3883 string[1] = (char_u)(long)event;
3884 add_to_input_buf_csi(string, 2);
3885}
3886
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
3889/*
3890 * Scrollbar stuff:
3891 */
3892
Bram Moolenaare45828b2006-02-15 22:12:56 +00003893/*
3894 * Remove all scrollbars. Used before switching to another tab page.
3895 */
3896 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003897gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003898{
3899 int i;
3900 win_T *wp;
3901
3902 for (i = 0; i < 3; i++)
3903 {
3904 if (i == SBAR_BOTTOM)
3905 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3906 else
3907 {
3908 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003909 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003910 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003911 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003912 }
3913}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003916gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917{
3918 static int sbar_ident = 0;
3919
Bram Moolenaar30613902019-12-01 22:11:18 +01003920 sb->ident = sbar_ident++; // No check for too big, but would it happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 sb->wp = wp;
3922 sb->type = type;
3923 sb->value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 sb->size = 1;
3925 sb->max = 1;
3926 sb->top = 0;
3927 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 sb->status_height = 0;
3930 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3931}
3932
3933/*
3934 * Find the scrollbar with the given index.
3935 */
3936 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003937gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
3939 win_T *wp;
3940
3941 if (gui.bottom_sbar.ident == ident)
3942 return &gui.bottom_sbar;
3943 FOR_ALL_WINDOWS(wp)
3944 {
3945 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3946 return &wp->w_scrollbars[SBAR_LEFT];
3947 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3948 return &wp->w_scrollbars[SBAR_RIGHT];
3949 }
3950 return NULL;
3951}
3952
3953/*
3954 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3955 *
3956 * For Win32, Macintosh and GTK+ 2:
3957 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3958 * you stop dragging the scrollbar. We get here each time the scrollbar is
3959 * dragged another pixel, but as far as the rest of vim goes, it thinks
3960 * we're just hanging in the call to DispatchMessage() in
3961 * process_message(). The DispatchMessage() call that hangs was passed a
3962 * mouse button click event in the scrollbar window. -- webb.
3963 *
3964 * Solution: Do the scrolling right here. But only when allowed.
3965 * Ignore the scrollbars while executing an external command or when there
3966 * are still characters to be processed.
3967 */
3968 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003969gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 int sb_num;
3973#ifdef USE_ON_FLY_SCROLL
3974 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976# ifdef FEAT_DIFF
3977 int old_topfill = curwin->w_topfill;
3978# endif
3979#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003980 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 int byte_count;
3982#endif
3983
3984 if (sb == NULL)
3985 return;
3986
Bram Moolenaar30613902019-12-01 22:11:18 +01003987 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 if (hold_gui_events)
3989 return;
3990
Sean Dewar988f7432023-08-16 14:17:36 +01003991 if (cmdwin_type != 0 && sb->wp != cmdwin_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994 if (still_dragging)
3995 {
3996 if (sb->wp == NULL)
3997 gui.dragged_sb = SBAR_BOTTOM;
3998 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3999 gui.dragged_sb = SBAR_LEFT;
4000 else
4001 gui.dragged_sb = SBAR_RIGHT;
4002 gui.dragged_wp = sb->wp;
4003 }
4004 else
4005 {
4006 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004007#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004008 // Keep the "dragged_wp" value until after the scrolling, for when the
4009 // mouse button is released. GTK2 doesn't send the button-up event.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 gui.dragged_wp = NULL;
4011#endif
4012 }
4013
Bram Moolenaar30613902019-12-01 22:11:18 +01004014 // Vertical sbar info is kept in the first sbar (the left one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 if (sb->wp != NULL)
4016 sb = &sb->wp->w_scrollbars[0];
4017
4018 /*
4019 * Check validity of value
4020 */
4021 if (value < 0)
4022 value = 0;
4023#ifdef SCROLL_PAST_END
4024 else if (value > sb->max)
4025 value = sb->max;
4026#else
4027 if (value > sb->max - sb->size + 1)
4028 value = sb->max - sb->size + 1;
4029#endif
4030
4031 sb->value = value;
4032
4033#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar30613902019-12-01 22:11:18 +01004034 // When not allowed to do the scrolling right now, return.
4035 // This also checked input_available(), but that causes the first click in
4036 // a scrollbar to be ignored when Vim doesn't have focus.
Bram Moolenaar67840782008-01-03 15:15:07 +00004037 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 return;
4039#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004040 // Disallow scrolling the current window when the completion popup menu is
4041 // visible.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004042 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4043 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045#ifdef FEAT_RIGHTLEFT
4046 if (sb->wp == NULL && curwin->w_p_rl)
4047 {
4048 value = sb->max + 1 - sb->size - value;
4049 if (value < 0)
4050 value = 0;
4051 }
4052#endif
4053
Bram Moolenaar30613902019-12-01 22:11:18 +01004054 if (sb->wp != NULL) // vertical scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
4056 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4058 sb_num++;
4059 if (wp == NULL)
4060 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
4062#ifdef USE_ON_FLY_SCROLL
4063 current_scrollbar = sb_num;
4064 scrollbar_value = value;
Bram Moolenaar24959102022-05-07 20:01:16 +01004065 if (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 {
4067 gui_do_scroll();
4068 setcursor();
4069 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004070 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
4072 ins_scroll();
4073 setcursor();
4074 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004075 else if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
4077 if (msg_scrolled == 0)
4078 {
4079 gui_do_scroll();
4080 redrawcmdline();
4081 }
4082 }
4083# ifdef FEAT_FOLDING
Bram Moolenaar30613902019-12-01 22:11:18 +01004084 // Value may have been changed for closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 sb->value = sb->wp->w_topline - 1;
4086# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004087
Bram Moolenaar30613902019-12-01 22:11:18 +01004088 // When dragging one scrollbar and there is another one at the other
4089 // side move the thumb of that one too.
Bram Moolenaar371d5402006-03-20 21:47:49 +00004090 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4091 gui_mch_set_scrollbar_thumb(
4092 &sb->wp->w_scrollbars[
4093 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4094 ? SBAR_LEFT : SBAR_RIGHT],
4095 sb->value, sb->size, sb->max);
4096
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097#else
4098 bytes[0] = CSI;
4099 bytes[1] = KS_VER_SCROLLBAR;
4100 bytes[2] = KE_FILLER;
4101 bytes[3] = (char_u)sb_num;
4102 byte_count = 4;
4103#endif
4104 }
4105 else
4106 {
4107#ifdef USE_ON_FLY_SCROLL
4108 scrollbar_value = value;
4109
Bram Moolenaar24959102022-05-07 20:01:16 +01004110 if (State & MODE_NORMAL)
Christopher Plewright44c22092022-11-15 17:43:36 +00004111 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar24959102022-05-07 20:01:16 +01004112 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 ins_horscroll();
Bram Moolenaar24959102022-05-07 20:01:16 +01004114 else if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004116 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 {
Christopher Plewright44c22092022-11-15 17:43:36 +00004118 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 redrawcmdline();
4120 }
4121 }
4122 if (old_leftcol != curwin->w_leftcol)
4123 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004124 updateWindow(curwin); // update window, status and cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 setcursor();
4126 }
4127#else
4128 bytes[0] = CSI;
4129 bytes[1] = KS_HOR_SCROLLBAR;
4130 bytes[2] = KE_FILLER;
4131 byte_count = 3;
4132#endif
4133 }
4134
4135#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 /*
4137 * synchronize other windows, as necessary according to 'scrollbind'
4138 */
4139 if (curwin->w_p_scb
4140 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4141 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004142# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004144# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 ))))
4146 {
4147 do_check_scrollbind(TRUE);
Bram Moolenaar30613902019-12-01 22:11:18 +01004148 // need to update the window right here
Bram Moolenaar29323592016-07-24 22:04:11 +02004149 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 if (wp->w_redr_type > 0)
4151 updateWindow(wp);
4152 setcursor();
4153 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004154 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004156 add_to_input_buf(bytes, byte_count);
4157 add_long_to_buf((long_u)value, bytes);
4158 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159#endif
4160}
4161
4162/*
4163 * Scrollbar stuff:
4164 */
4165
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004166/*
4167 * Called when something in the window layout has changed.
4168 */
4169 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004170gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004171{
4172 if (gui.in_use && starting == 0)
4173 {
4174 out_flush();
4175 gui_init_which_components(NULL);
4176 gui_update_scrollbars(TRUE);
4177 }
4178 need_mouse_correct = TRUE;
4179}
4180
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004182gui_update_scrollbars(
Bram Moolenaar30613902019-12-01 22:11:18 +01004183 int force) // Force all scrollbars to get updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184{
4185 win_T *wp;
4186 scrollbar_T *sb;
Bram Moolenaar30613902019-12-01 22:11:18 +01004187 long val, size, max; // need 32 bits here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 int which_sb;
4189 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
Bram Moolenaar30613902019-12-01 22:11:18 +01004192 // Update the horizontal scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 gui_update_horiz_scrollbar(force);
4194
Bram Moolenaar4f974752019-02-17 17:44:42 +01004195#ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01004196 // Return straight away if there is neither a left nor right scrollbar.
4197 // On MS-Windows this is required anyway for scrollwheel messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4199 return;
4200#endif
4201
4202 /*
4203 * Don't want to update a scrollbar while we're dragging it. But if we
4204 * have both a left and right scrollbar, and we drag one of them, we still
4205 * need to update the other one.
4206 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004207 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4208 && gui.which_scrollbars[SBAR_LEFT]
4209 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
4211 /*
4212 * If we have two scrollbars and one of them is being dragged, just
4213 * copy the scrollbar position from the dragged one to the other one.
4214 */
4215 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4216 if (gui.dragged_wp != NULL)
4217 gui_mch_set_scrollbar_thumb(
4218 &gui.dragged_wp->w_scrollbars[which_sb],
4219 gui.dragged_wp->w_scrollbars[0].value,
4220 gui.dragged_wp->w_scrollbars[0].size,
4221 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
4223
Bram Moolenaar30613902019-12-01 22:11:18 +01004224 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 ++hold_gui_events;
4226
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004227 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004229 if (wp->w_buffer == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 continue;
Bram Moolenaar30613902019-12-01 22:11:18 +01004231 // Skip a scrollbar that is being dragged.
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004232 if (!force && (gui.dragged_sb == SBAR_LEFT
4233 || gui.dragged_sb == SBAR_RIGHT)
4234 && gui.dragged_wp == wp)
4235 continue;
4236
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237#ifdef SCROLL_PAST_END
4238 max = wp->w_buffer->b_ml.ml_line_count - 1;
4239#else
4240 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4241#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004242 if (max < 0) // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 max = 0;
4244 val = wp->w_topline - 1;
4245 size = wp->w_height;
4246#ifdef SCROLL_PAST_END
Bram Moolenaar30613902019-12-01 22:11:18 +01004247 if (val > max) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 val = max;
4249#else
Bram Moolenaar30613902019-12-01 22:11:18 +01004250 if (size > max + 1) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 size = max + 1;
4252 if (val > max - size + 1)
4253 val = max - size + 1;
4254#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004255 if (val < 0) // minimal value is 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 val = 0;
4257
4258 /*
4259 * Scrollbar at index 0 (the left one) contains all the information.
4260 * It would be the same info for left and right so we just store it for
4261 * one of them.
4262 */
4263 sb = &wp->w_scrollbars[0];
4264
4265 /*
4266 * Note: no check for valid w_botline. If it's not valid the
4267 * scrollbars will be updated later anyway.
4268 */
4269 if (size < 1 || wp->w_botline - 2 > max)
4270 {
4271 /*
4272 * This can happen during changing files. Just don't update the
4273 * scrollbar for now.
4274 */
Bram Moolenaar30613902019-12-01 22:11:18 +01004275 sb->height = 0; // Force update next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 if (gui.which_scrollbars[SBAR_LEFT])
4277 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4278 if (gui.which_scrollbars[SBAR_RIGHT])
4279 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4280 continue;
4281 }
4282 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 || sb->top != wp->w_winrow
4284 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004286 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004288 // Height, width or position of scrollbar has changed. For
4289 // vertical split: curwin changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 sb->top = wp->w_winrow;
4292 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
Bram Moolenaar30613902019-12-01 22:11:18 +01004295 // Calculate height and position in pixels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 h = (sb->height + sb->status_height) * gui.char_height;
4297 y = sb->top * gui.char_height + gui.border_offset;
4298#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4299 if (gui.menu_is_active)
4300 y += gui.menu_height;
4301#endif
4302
Bram Moolenaar0b962e52022-04-03 18:02:37 +01004303#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004304 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 y += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307#endif
4308
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004309#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004310 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004311 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004312#endif
4313
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004316 // Height of top scrollbar includes width of top border
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 h += gui.border_offset;
4318 y -= gui.border_offset;
4319 }
4320 if (gui.which_scrollbars[SBAR_LEFT])
4321 {
4322 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4323 gui.left_sbar_x, y,
4324 gui.scrollbar_width, h);
4325 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4326 }
4327 if (gui.which_scrollbars[SBAR_RIGHT])
4328 {
4329 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4330 gui.right_sbar_x, y,
4331 gui.scrollbar_width, h);
4332 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4333 }
4334 }
4335
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 if (force || sb->value != val || sb->size != size || sb->max != max)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004338 // Thumb of scrollbar has moved
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 sb->value = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 sb->size = size;
4341 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004342 if (gui.which_scrollbars[SBAR_LEFT]
4343 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4345 val, size, max);
4346 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004347 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4349 val, size, max);
4350 }
4351 }
Christian Brabandt2e0f3ec2021-11-28 18:41:05 +00004352
4353 // update the title, it may show the scroll position
4354 maketitle();
4355
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 --hold_gui_events;
4358}
4359
4360/*
4361 * Enable or disable a scrollbar.
4362 * Check for scrollbars for vertically split windows which are not enabled
4363 * sometimes.
4364 */
4365 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004366gui_do_scrollbar(
4367 win_T *wp,
Bram Moolenaar30613902019-12-01 22:11:18 +01004368 int which, // SBAR_LEFT or SBAR_RIGHT
4369 int enable) // TRUE to enable scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 int midcol = curwin->w_wincol + curwin->w_width / 2;
4372 int has_midcol = (wp->w_wincol <= midcol
4373 && wp->w_wincol + wp->w_width >= midcol);
4374
Bram Moolenaar30613902019-12-01 22:11:18 +01004375 // Only enable scrollbars that contain the middle column of the current
4376 // window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4378 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004379 // Scrollbars only on one side. Don't enable scrollbars that don't
4380 // contain the middle column of the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 if (!has_midcol)
4382 enable = FALSE;
4383 }
4384 else
4385 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004386 // Scrollbars on both sides. Don't enable scrollbars that neither
4387 // contain the middle column of the current window nor are on the far
4388 // side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 if (midcol > Columns / 2)
4390 {
4391 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4392 enable = FALSE;
4393 }
4394 else
4395 {
4396 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4397 : !has_midcol)
4398 enable = FALSE;
4399 }
4400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4402}
4403
4404/*
Bram Moolenaar097c5372023-05-24 21:02:24 +01004405 * Scroll a window according to the values set in the globals
4406 * "current_scrollbar" and "scrollbar_value".
4407 * Return TRUE if the cursor in the current window moved or FALSE otherwise.
Christian Brabandt20d161a2023-10-05 22:08:30 +02004408 * may eventually cause a redraw using updateWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 */
4410 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004411gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
4413 win_T *wp, *save_wp;
4414 int i;
4415 long nlines;
4416 pos_T old_cursor;
4417 linenr_T old_topline;
4418#ifdef FEAT_DIFF
4419 int old_topfill;
4420#endif
4421
4422 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4423 if (wp == NULL)
4424 break;
4425 if (wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004426 // Couldn't find window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 return FALSE;
Christian Brabandt20d161a2023-10-05 22:08:30 +02004428 // don't redraw, LineOffset and similar are not valid!
4429 if (exmode_active)
4430 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 /*
4433 * Compute number of lines to scroll. If zero, nothing to do.
4434 */
4435 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4436 if (nlines == 0)
4437 return FALSE;
4438
4439 save_wp = curwin;
4440 old_topline = wp->w_topline;
4441#ifdef FEAT_DIFF
4442 old_topfill = wp->w_topfill;
4443#endif
4444 old_cursor = wp->w_cursor;
4445 curwin = wp;
4446 curbuf = wp->w_buffer;
4447 if (nlines < 0)
4448 scrolldown(-nlines, gui.dragged_wp == NULL);
4449 else
4450 scrollup(nlines, gui.dragged_wp == NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004451 // Reset dragged_wp after using it. "dragged_sb" will have been reset for
4452 // the mouse-up event already, but we still want it to behave like when
4453 // dragging. But not the next click in an arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 if (gui.dragged_sb == SBAR_NONE)
4455 gui.dragged_wp = NULL;
4456
4457 if (old_topline != wp->w_topline
4458#ifdef FEAT_DIFF
4459 || old_topfill != wp->w_topfill
4460#endif
4461 )
4462 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004463 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004465 cursor_correct(); // fix window for 'so'
4466 update_topline(); // avoid up/down jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468 if (old_cursor.lnum != wp->w_cursor.lnum)
4469 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472
Bram Moolenaar30613902019-12-01 22:11:18 +01004473 // Make sure wp->w_leftcol and wp->w_skipcol are correct.
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004474 validate_cursor();
4475
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 curwin = save_wp;
4477 curbuf = save_wp->w_buffer;
4478
4479 /*
4480 * Don't call updateWindow() when nothing has changed (it will overwrite
4481 * the status line!).
Christian Brabandt9d1bed52025-01-20 22:55:57 +01004482 *
4483 * Check for ScreenLines, because in ex-mode, we don't have a valid display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 */
Christian Brabandt9d1bed52025-01-20 22:55:57 +01004485 if (ScreenLines != NULL && (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004486 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487#ifdef FEAT_DIFF
4488 || old_topfill != wp->w_topfill
4489#endif
Christian Brabandt9d1bed52025-01-20 22:55:57 +01004490 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004492 int type = UPD_VALID;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004493
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004494 if (pum_visible())
4495 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004496 type = UPD_NOT_VALID;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004497 wp->w_lines_valid = 0;
4498 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004499
Bram Moolenaar30613902019-12-01 22:11:18 +01004500 // Don't set must_redraw here, it may cause the popup menu to
4501 // disappear when losing focus after a scrollbar drag.
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004502 if (wp->w_redr_type < type)
4503 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004504 mch_disable_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01004505 updateWindow(wp); // update window, status line, and cmdline
Bram Moolenaara338adc2018-01-31 20:51:47 +01004506 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508
Bram Moolenaar30613902019-12-01 22:11:18 +01004509 // May need to redraw the popup menu.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004510 if (pum_visible())
4511 pum_redraw();
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004512
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004513 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514}
4515
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516/*
4517 * Horizontal scrollbar stuff:
4518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004520gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521{
Christopher Plewright44c22092022-11-15 17:43:36 +00004522 long value, size, max;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
4524 if (!gui.which_scrollbars[SBAR_BOTTOM])
4525 return;
4526
4527 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4528 return;
4529
4530 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4531 return;
4532
4533 /*
4534 * It is possible for the cursor to be invalid if we're in the middle of
4535 * something (like changing files). If so, don't do anything for now.
4536 */
4537 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4538 {
4539 gui.bottom_sbar.value = -1;
4540 return;
4541 }
4542
Bram Moolenaar02631462017-09-22 15:20:32 +02004543 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 if (curwin->w_p_wrap)
4545 {
4546 value = 0;
4547#ifdef SCROLL_PAST_END
4548 max = 0;
4549#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004550 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551#endif
4552 }
4553 else
4554 {
4555 value = curwin->w_leftcol;
Christopher Plewright44c22092022-11-15 17:43:36 +00004556 max = scroll_line_len(ui_find_longest_lnum());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (virtual_active())
4559 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004560 // May move the cursor even further to the right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 if (curwin->w_virtcol >= (colnr_T)max)
4562 max = curwin->w_virtcol;
4563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
4565#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004566 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004568 // The line number isn't scrolled, thus there is less space when
4569 // 'number' or 'relativenumber' is set (also for 'foldcolumn').
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 size -= curwin_col_off();
4571#ifndef SCROLL_PAST_END
4572 max -= curwin_col_off();
4573#endif
4574 }
4575
4576#ifndef SCROLL_PAST_END
4577 if (value > max - size + 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01004578 value = max - size + 1; // limit the value to allowable range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579#endif
4580
4581#ifdef FEAT_RIGHTLEFT
4582 if (curwin->w_p_rl)
4583 {
4584 value = max + 1 - size - value;
4585 if (value < 0)
4586 {
4587 size += value;
4588 value = 0;
4589 }
4590 }
4591#endif
4592 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4593 && max == gui.bottom_sbar.max)
4594 return;
4595
4596 gui.bottom_sbar.value = value;
4597 gui.bottom_sbar.size = size;
4598 gui.bottom_sbar.max = max;
4599 gui.prev_wrap = curwin->w_p_wrap;
4600
4601 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4602}
4603
4604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 * Check that none of the colors are the same as the background color
4606 */
4607 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004608gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609{
4610 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4611 {
4612 gui_set_bg_color((char_u *)"White");
4613 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4614 gui_set_fg_color((char_u *)"Black");
4615 }
4616}
4617
Bram Moolenaar3918c952005-03-15 22:34:55 +00004618 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004619gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620{
4621 gui.norm_pixel = gui_get_color(name);
4622 hl_set_fg_color_name(vim_strsave(name));
4623}
4624
Bram Moolenaar3918c952005-03-15 22:34:55 +00004625 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004626gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627{
4628 gui.back_pixel = gui_get_color(name);
4629 hl_set_bg_color_name(vim_strsave(name));
4630}
4631
4632/*
4633 * Allocate a color by name.
4634 * Returns INVALCOLOR and gives an error message when failed.
4635 */
4636 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004637gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638{
4639 guicolor_T t;
4640
4641 if (*name == NUL)
4642 return INVALCOLOR;
4643 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004644
Bram Moolenaar5b9f5722023-02-19 20:49:38 +00004645 int is_none = STRCMP(name, "none") == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004647#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar5b9f5722023-02-19 20:49:38 +00004648 && (gui.in_use || is_none)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649#endif
4650 )
Bram Moolenaar5b9f5722023-02-19 20:49:38 +00004651 {
4652 if (is_none)
4653 emsg(_(e_cannot_use_color_none_did_you_mean_none));
4654 else
4655 semsg(_(e_cannot_allocate_color_str), name);
4656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 return t;
4658}
4659
4660/*
4661 * Return the grey value of a color (range 0-255).
4662 */
4663 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004664gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004666 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004668 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004669 + (((rgb >> 8) & 0xff) * 587)
4670 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671}
4672
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004673 char_u *
4674gui_bg_default(void)
4675{
4676 if (gui_get_lightness(gui.back_pixel) < 127)
4677 return (char_u *)"dark";
4678 return (char_u *)"light";
4679}
4680
4681/*
4682 * Option initializations that can only be done after opening the GUI window.
4683 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004684 static void
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004685init_gui_options(void)
4686{
Bram Moolenaar30613902019-12-01 22:11:18 +01004687 // Set the 'background' option according to the lightness of the
4688 // background color, unless the user has set it already.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004689 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
4690 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004691 set_option_value_give_err((char_u *)"bg", 0L, gui_bg_default(), 0);
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004692 highlight_changed();
4693 }
4694}
4695
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696#if defined(FEAT_GUI_X11) || defined(PROTO)
4697 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004698gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
4700 win_T *wp;
4701
Bram Moolenaar30613902019-12-01 22:11:18 +01004702 // Nothing to do if GUI hasn't started yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 if (!gui.in_use)
4704 return;
4705
4706 FOR_ALL_WINDOWS(wp)
4707 {
4708 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4709 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4710 }
4711 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4712}
4713#endif
4714
4715/*
4716 * Call this when focus has changed.
4717 */
4718 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004719gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
4721/*
4722 * Skip this code to avoid drawing the cursor when debugging and switching
4723 * between the debugger window and gvim.
4724 */
4725#if 1
4726 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004727 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
4729# ifdef FEAT_XIM
4730 xim_set_focus(in_focus);
4731# endif
4732
Bram Moolenaar30613902019-12-01 22:11:18 +01004733 // Put events in the input queue only when allowed.
4734 // ui_focus_change() isn't called directly, because it invokes
4735 // autocommands and that must not happen asynchronously.
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004736 if (!hold_gui_events)
4737 {
4738 char_u bytes[3];
4739
4740 bytes[0] = CSI;
4741 bytes[1] = KS_EXTRA;
4742 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4743 add_to_input_buf(bytes, 3);
4744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745#endif
4746}
4747
4748/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004749 * When mouse moved: apply 'mousefocus'.
4750 * Also updates the mouse pointer shape.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 */
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004752 static void
4753gui_mouse_focus(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
4755 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004756 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757
4758#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01004759 // Get window pointer, and update mouse shape as well.
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004760 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#endif
4762
Bram Moolenaar30613902019-12-01 22:11:18 +01004763 // Only handle this when 'mousefocus' set and ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 if (p_mousef
Bram Moolenaar30613902019-12-01 22:11:18 +01004765 && !hold_gui_events // not holding events
Bram Moolenaar24959102022-05-07 20:01:16 +01004766 && (State & (MODE_NORMAL | MODE_INSERT))// Normal/Visual/Insert mode
4767 && State != MODE_HITRETURN // but not hit-return prompt
Bram Moolenaar30613902019-12-01 22:11:18 +01004768 && msg_scrolled == 0 // no scrolled message
4769 && !need_mouse_correct // not moving the pointer
4770 && gui.in_focus) // gvim in focus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004772 // Don't move the mouse when it's left or right of the Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (x < 0 || x > Columns * gui.char_width)
4774 return;
4775#ifndef FEAT_MOUSESHAPE
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004776 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777#endif
4778 if (wp == curwin || wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004779 return; // still in the same old window, or none at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
Bram Moolenaar30613902019-12-01 22:11:18 +01004781 // Ignore position in the tab pages line.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004782 if (Y_2_ROW(y) < tabline_height())
4783 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004784
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004786 * Format a mouse click on status line input,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004788 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4789 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 */
4791 if (finish_op)
4792 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004793 // abort the current operator first
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 st[0] = ESC;
4795 add_to_input_buf(st, 1);
4796 }
4797 st[0] = CSI;
4798 st[1] = KS_MOUSE;
4799 st[2] = KE_FILLER;
4800 st[3] = (char_u)MOUSE_LEFT;
4801 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004802 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 wp->w_height + W_WINROW(wp));
4804
4805 add_to_input_buf(st, 8);
4806 st[3] = (char_u)MOUSE_RELEASE;
4807 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004809 // Need to wake up the main loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 if (gtk_main_level() > 0)
4811 gtk_main_quit();
4812#endif
4813 }
4814}
4815
4816/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004817 * Called when the mouse moved (but not when dragging).
4818 */
4819 void
4820gui_mouse_moved(int x, int y)
4821{
4822 // Ignore this while still starting up.
4823 if (!gui.in_use || gui.starting)
4824 return;
4825
4826 // apply 'mousefocus' and pointer shape
4827 gui_mouse_focus(x, y);
4828
Ernie Raelc4cb5442022-04-03 15:47:28 +01004829 if (p_mousemev
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004830#ifdef FEAT_PROP_POPUP
Ernie Raelc4cb5442022-04-03 15:47:28 +01004831 || popup_uses_mouse_move
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004832#endif
Ernie Raelc4cb5442022-04-03 15:47:28 +01004833 )
4834 // Generate a mouse-moved event. For a <MouseMove> mapping. Or so the
4835 // popup can perhaps be closed, just like in the terminal.
4836 gui_send_mouse_event(MOUSE_MOVE, x, y, FALSE, 0);
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004837}
4838
4839/*
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004840 * Get the window where the mouse pointer is on.
4841 * Returns NULL if not found.
4842 */
4843 win_T *
4844gui_mouse_window(mouse_find_T popup)
4845{
4846 int x, y;
4847
4848 if (!(gui.in_use && (p_mousef || popup == FIND_POPUP)))
4849 return NULL;
4850 gui_mch_getmouse(&x, &y);
4851
4852 // Only use the mouse when it's on the Vim window
4853 if (x >= 0 && x <= Columns * gui.char_width
4854 && y >= 0 && Y_2_ROW(y) >= tabline_height())
4855 return xy2win(x, y, popup);
4856 return NULL;
4857}
4858
4859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 * Called when mouse should be moved to window with focus.
4861 */
4862 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004863gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 win_T *wp = NULL;
4866
4867 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004868
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004869 wp = gui_mouse_window(IGNORE_POPUP);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004870 if (wp == curwin || wp == NULL)
4871 return;
4872
4873 // If in other than current window
4874 validate_cline_row();
4875 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4876 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4877 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878}
4879
4880/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004881 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004882 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 static win_T *
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004885xy2win(int x, int y, mouse_find_T popup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 int row;
4888 int col;
4889 win_T *wp;
4890
4891 row = Y_2_ROW(y);
4892 col = X_2_COL(x);
Bram Moolenaar30613902019-12-01 22:11:18 +01004893 if (row < 0 || col < 0) // before first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 return NULL;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004895 wp = mouse_find_win(&row, &col, popup);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004896 if (wp == NULL)
4897 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004898#ifdef FEAT_MOUSESHAPE
Bram Moolenaar24959102022-05-07 20:01:16 +01004899 if (State == MODE_HITRETURN || State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
4901 if (Y_2_ROW(y) >= msg_row)
4902 update_mouseshape(SHAPE_IDX_MOREL);
4903 else
4904 update_mouseshape(SHAPE_IDX_MORE);
4905 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004906 else if (row > wp->w_height) // below status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaar24959102022-05-07 20:01:16 +01004908 else if (!(State & MODE_CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004909 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaar24959102022-05-07 20:01:16 +01004911 else if (!(State & MODE_CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004912 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 update_mouseshape(SHAPE_IDX_STATUS);
4914 else
4915 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004917 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918}
4919
4920/*
4921 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4922 * File names may be given to redefine the args list.
4923 */
4924 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004925ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926{
4927 char_u *arg = eap->arg;
4928
4929 /*
4930 * Check for "-f" argument: foreground, don't fork.
4931 * Also don't fork when started with "gvim -f".
4932 * Do fork when using "gui -b".
4933 */
4934 if (arg[0] == '-'
4935 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004936 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
4938 gui.dofork = (arg[1] == 'b');
4939 eap->arg = skipwhite(eap->arg + 2);
4940 }
4941 if (!gui.in_use)
4942 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004943#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaar257a3962019-12-29 15:19:03 +01004944 if (!gui.starting)
4945 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004946 emsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaar257a3962019-12-29 15:19:03 +01004947 return;
4948 }
4949#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004950 // Clear the command. Needed for when forking+exiting, to avoid part
4951 // of the argument ending up after the shell prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 msg_clr_eos_force();
Bram Moolenaar257a3962019-12-29 15:19:03 +01004953#ifdef GUI_MAY_SPAWN
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02004954 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004955 gui_start(eap->arg);
4956 else
Bram Moolenaar257a3962019-12-29 15:19:03 +01004957#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004958 gui_start(NULL);
Bram Moolenaar257a3962019-12-29 15:19:03 +01004959#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004960 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02004963 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 ex_next(eap);
4965}
4966
Bram Moolenaar4f974752019-02-17 17:44:42 +01004967#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004968 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON) \
4969 || defined(FEAT_GUI_HAIKU)) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004970 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971/*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01004972 * This is shared between Haiku, Motif, and GTK.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974
4975/*
4976 * Callback function for do_in_runtimepath().
4977 */
4978 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004979gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004981 char_u *gfp_buffer = cookie;
4982
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (STRLEN(fname) >= MAXPATHL)
4984 *gfp_buffer = NUL;
4985 else
4986 STRCPY(gfp_buffer, fname);
4987}
4988
4989/*
4990 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4991 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4992 */
4993 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004994gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
4996 if (STRLEN(name) > MAXPATHL - 14)
4997 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004998 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01004999 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005000 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 return FAIL;
5002 return OK;
5003}
5004
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005005# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006/*
5007 * Given the name of the "icon=" argument, try finding the bitmap file for the
5008 * icon. If it is an absolute path name, use it as it is. Otherwise append
5009 * "ext" and search for it in 'runtimepath'.
5010 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5011 * contains "name".
5012 */
5013 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005014gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015{
5016 char_u buf[MAXPATHL + 1];
5017
5018 expand_env(name, buffer, MAXPATHL);
5019 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5020 STRCPY(buffer, buf);
5021}
5022# endif
5023#endif
5024
Bram Moolenaar9e175142020-04-30 22:51:01 +02005025#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)|| defined(FEAT_GUI_HAIKU) \
5026 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005028display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029{
5030 char_u *p;
5031
5032 if (isatty(2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005034 fflush(stderr);
5035 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005037
5038 if (error_ga.ga_data == NULL)
5039 return;
5040
5041 // avoid putting up a message box with blanks only
5042 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
Keith Thompson184f71c2024-01-04 21:19:04 +01005043 if (!SAFE_isspace(*p))
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005044 {
5045 // Truncate a very long message, it will go off-screen.
5046 if (STRLEN(p) > 2000)
5047 STRCPY(p + 2000 - 14, "...(truncated)");
5048 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
5049 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
5050 break;
5051 }
5052 ga_clear(&error_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053}
5054#endif
5055
5056#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5057/*
5058 * Return TRUE if still starting up and there is no place to enter text.
5059 * For GTK and X11 we check if stderr is not a tty, which means we were
5060 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5061 * allow typing on stdin.
5062 */
5063 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005064no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
5066 return ((!gui.in_use || gui.starting)
5067# ifndef NO_CONSOLE
5068 && !isatty(0) && !isatty(2)
5069# endif
5070 );
5071}
5072#endif
5073
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005074#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005075 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005076 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077/*
5078 * Update the current window and the screen.
5079 */
5080 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005081gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005083# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005084 linenr_T conceal_old_cursor_line = 0;
5085 linenr_T conceal_new_cursor_line = 0;
5086 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005087# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005088
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 update_topline();
5090 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005091
Bram Moolenaar30613902019-12-01 22:11:18 +01005092 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005093 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005094# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005095 || popup_visible
5096# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005097# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005098 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005099# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005100 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005101 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005102 if (has_cursormoved())
5103 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
K.Takata6e1d31e2022-02-03 13:05:32 +00005104# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005105 if (popup_visible)
5106 popup_check_cursor_pos();
K.Takata6e1d31e2022-02-03 13:05:32 +00005107# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005108# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005109 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005110 {
5111 conceal_old_cursor_line = last_cursormoved.lnum;
5112 conceal_new_cursor_line = curwin->w_cursor.lnum;
5113 conceal_update_lines = TRUE;
5114 }
5115# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005116 last_cursormoved = curwin->w_cursor;
5117 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005118
LemonBoy09371822022-04-08 15:18:45 +01005119 if (!finish_op)
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00005120 may_trigger_win_scrolled_resized();
LemonBoy09371822022-04-08 15:18:45 +01005121
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005122# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005123 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005124 && (conceal_old_cursor_line != conceal_new_cursor_line
5125 || conceal_cursor_line(curwin)
5126 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005127 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005128 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005129 redrawWinline(curwin, conceal_old_cursor_line);
5130 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005131 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005132 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005133 }
5134# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005135 update_screen(0); // may need to update the screen
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005136 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005137 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138}
5139#endif
5140
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005141#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142/*
5143 * Get the text to use in a find/replace dialog. Uses the last search pattern
5144 * if the argument is empty.
5145 * Returns an allocated string.
5146 */
5147 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005148get_find_dialog_text(
5149 char_u *arg,
Bram Moolenaar30613902019-12-01 22:11:18 +01005150 int *wwordp, // return: TRUE if \< \> found
5151 int *mcasep) // return: TRUE if \C found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
5153 char_u *text;
5154
5155 if (*arg == NUL)
5156 text = last_search_pat();
5157 else
5158 text = arg;
5159 if (text != NULL)
5160 {
5161 text = vim_strsave(text);
5162 if (text != NULL)
5163 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005164 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 int i;
5166
Bram Moolenaar30613902019-12-01 22:11:18 +01005167 // Remove "\V"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5169 {
5170 mch_memmove(text, text + 2, (size_t)(len - 1));
5171 len -= 2;
5172 }
5173
Bram Moolenaar30613902019-12-01 22:11:18 +01005174 // Recognize "\c" and "\C" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5176 {
5177 *mcasep = (text[1] == 'C');
5178 mch_memmove(text, text + 2, (size_t)(len - 1));
5179 len -= 2;
5180 }
5181
Bram Moolenaar30613902019-12-01 22:11:18 +01005182 // Recognize "\<text\>" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 if (len >= 4
5184 && STRNCMP(text, "\\<", 2) == 0
5185 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5186 {
5187 *wwordp = TRUE;
5188 mch_memmove(text, text + 2, (size_t)(len - 4));
5189 text[len - 4] = NUL;
5190 }
5191
Bram Moolenaar30613902019-12-01 22:11:18 +01005192 // Recognize "\/" or "\?" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 for (i = 0; i + 1 < len; ++i)
5194 if (text[i] == '\\' && (text[i + 1] == '/'
5195 || text[i + 1] == '?'))
5196 {
5197 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5198 --len;
5199 }
5200 }
5201 }
5202 return text;
5203}
5204
5205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 * Handle the press of a button in the find-replace dialog.
5207 * Return TRUE when something was added to the input buffer.
5208 */
5209 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005210gui_do_findrepl(
Bram Moolenaar30613902019-12-01 22:11:18 +01005211 int flags, // one of FRD_REPLACE, FRD_FINDNEXT, etc.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005212 char_u *find_text,
5213 char_u *repl_text,
Bram Moolenaar30613902019-12-01 22:11:18 +01005214 int down) // Search downwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215{
5216 garray_T ga;
5217 int i;
5218 int type = (flags & FRD_TYPE_MASK);
5219 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005220 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005221 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005222 static int busy = FALSE;
5223
Bram Moolenaar30613902019-12-01 22:11:18 +01005224 // When the screen is being updated we should not change buffers and
5225 // windows structures, it may cause freed memory to be used. Also don't
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005226 // do this recursively (pressing "Find" quickly several times).
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005227 if (updating_screen || busy)
5228 return FALSE;
5229
Bram Moolenaar30613902019-12-01 22:11:18 +01005230 // refuse replace when text cannot be changed
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005231 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5232 return FALSE;
5233
5234 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
5236 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005237 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 ga_concat(&ga, (char_u *)"%s/");
5239
5240 ga_concat(&ga, (char_u *)"\\V");
5241 if (flags & FRD_MATCH_CASE)
5242 ga_concat(&ga, (char_u *)"\\C");
5243 else
5244 ga_concat(&ga, (char_u *)"\\c");
5245 if (flags & FRD_WHOLE_WORD)
5246 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar30613902019-12-01 22:11:18 +01005247 // escape slash and backslash
Bram Moolenaar518bc172018-05-13 17:05:30 +02005248 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5249 if (p != NULL)
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005250 ga_concat(&ga, p);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005251 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 if (flags & FRD_WHOLE_WORD)
5253 ga_concat(&ga, (char_u *)"\\>");
5254
5255 if (type == FRD_REPLACEALL)
5256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 ga_concat(&ga, (char_u *)"/");
matveyt2834ebd2022-09-06 17:00:15 +01005258 // Escape slash and backslash.
5259 // Also escape tilde and ampersand if 'magic' is set.
5260 p = vim_strsave_escaped(repl_text,
5261 p_magic ? (char_u *)"/\\~&" : (char_u *)"/\\");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005262 if (p != NULL)
5263 ga_concat(&ga, p);
5264 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005266 }
5267 ga_append(&ga, NUL);
5268
5269 if (type == FRD_REPLACE)
5270 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005271 // Do the replacement when the text at the cursor matches. Thus no
5272 // replacement is done if the cursor was moved!
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005273 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5274 regmatch.rm_ic = 0;
5275 if (regmatch.regprog != NULL)
5276 {
5277 p = ml_get_cursor();
5278 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5279 && regmatch.startp[0] == p)
5280 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005281 // Clear the command line to remove any old "No match"
5282 // error.
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005283 msg_end_prompt();
5284
5285 if (u_save_cursor() == OK)
5286 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005287 // A button was pressed thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005288 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005289
5290 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005291 FALSE, FALSE);
John Marriottf4b36412025-02-23 09:09:59 +01005292 ins_str(repl_text, STRLEN(repl_text));
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005293 }
5294 }
5295 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005296 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005297 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005298 }
5299 }
5300
5301 if (type == FRD_REPLACEALL)
5302 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005303 // A button was pressed, thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005304 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 do_cmdline_cmd(ga.ga_data);
5306 }
5307 else
5308 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005309 int searchflags = SEARCH_MSG + SEARCH_MARK;
5310
Bram Moolenaar30613902019-12-01 22:11:18 +01005311 // Search for the next match.
5312 // Don't skip text under cursor for single replace.
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005313 if (type == FRD_REPLACE)
5314 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005316 if (down)
5317 {
John Marriott8c85a2a2024-05-20 19:18:26 +02005318 (void)do_search(NULL, '/', '/', ga.ga_data, STRLEN(ga.ga_data), 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005319 }
5320 else
5321 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005322 // We need to escape '?' if and only if we are searching in the up
5323 // direction
Bram Moolenaar518bc172018-05-13 17:05:30 +02005324 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5325 if (p != NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005326 (void)do_search(NULL, '?', '?', p, STRLEN(p), 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005327 vim_free(p);
5328 }
5329
Bram Moolenaar30613902019-12-01 22:11:18 +01005330 msg_scroll = i; // don't let an error message set msg_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
5332
Bram Moolenaar30613902019-12-01 22:11:18 +01005333 // Don't want to pass did_emsg to other code, it may cause disabling
5334 // syntax HL if we were busy redrawing.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005335 did_emsg = save_did_emsg;
5336
Bram Moolenaar24959102022-05-07 20:01:16 +01005337 if (State & (MODE_NORMAL | MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005339 gui_update_screen(); // update the screen
5340 msg_didout = 0; // overwrite any message
5341 need_wait_return = FALSE; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
5343
5344 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005345 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 return (ga.ga_len > 0);
5347}
5348
5349#endif
5350
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005351#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352/*
5353 * Jump to the window at specified point (x, y).
5354 */
5355 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005356gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357{
5358 int row = Y_2_ROW(y);
5359 int col = X_2_COL(x);
5360 win_T *wp;
5361
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005362 if (row < 0 || col < 0)
5363 return;
5364
5365 wp = mouse_find_win(&row, &col, FAIL_POPUP);
5366 if (wp != NULL && wp != curwin)
5367 win_goto(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005371 * Function passed to handle_drop() for the actions to be done after the
5372 * argument list has been updated.
5373 */
5374 static void
5375drop_callback(void *cookie)
5376{
5377 char_u *p = cookie;
Bram Moolenaar4671e882021-11-22 17:21:48 +00005378 int do_shorten = FALSE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005379
Bram Moolenaar30613902019-12-01 22:11:18 +01005380 // If Shift held down, change to first file's directory. If the first
5381 // item is a directory, change to that directory (and let the explorer
5382 // plugin show the contents).
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005383 if (p != NULL)
5384 {
5385 if (mch_isdir(p))
5386 {
5387 if (mch_chdir((char *)p) == 0)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005388 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005389 }
5390 else if (vim_chdirfile(p, "drop") == OK)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005391 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005392 vim_free(p);
Bram Moolenaar4671e882021-11-22 17:21:48 +00005393 if (do_shorten)
5394 {
5395 shorten_fnames(TRUE);
5396 last_chdir_reason = "drop";
5397 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005398 }
5399
Bram Moolenaar30613902019-12-01 22:11:18 +01005400 // Update the screen display
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005401 update_screen(UPD_NOT_VALID);
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005402# ifdef FEAT_MENU
5403 gui_update_menus(0);
5404# endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005405 maketitle();
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005406 setcursor();
5407 out_flush_cursor(FALSE, FALSE);
5408}
5409
5410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 * Process file drop. Mouse cursor position, key modifiers, name of files
5412 * and count of files are given. Argument "fnames[count]" has full pathnames
5413 * of dropped files, they will be freed in this function, and caller can't use
5414 * fnames after call this function.
5415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005417gui_handle_drop(
5418 int x UNUSED,
5419 int y UNUSED,
5420 int_u modifiers,
5421 char_u **fnames,
5422 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 int i;
5425 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005426 static int entered = FALSE;
5427
5428 /*
5429 * This function is called by event handlers. Just in case we get a
5430 * second event before the first one is handled, ignore the second one.
5431 * Not sure if this can ever happen, just in case.
5432 */
5433 if (entered)
5434 return;
5435 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436
5437 /*
5438 * When the cursor is at the command line, add the file names to the
5439 * command line, don't edit the files.
5440 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005441 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
5443 shorten_filenames(fnames, count);
5444 for (i = 0; i < count; ++i)
5445 {
5446 if (fnames[i] != NULL)
5447 {
5448 if (i > 0)
5449 add_to_input_buf((char_u*)" ", 1);
5450
Bram Moolenaar30613902019-12-01 22:11:18 +01005451 // We don't know what command is used thus we can't be sure
5452 // about which characters need to be escaped. Only escape the
5453 // most common ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454# ifdef BACKSLASH_IN_FILENAME
5455 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5456# else
5457 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5458# endif
5459 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005460 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 vim_free(p);
5462 vim_free(fnames[i]);
5463 }
5464 }
5465 vim_free(fnames);
5466 }
5467 else
5468 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005469 // Go to the window under mouse cursor, then shorten given "fnames" by
5470 // current window, because a window can have local current dir.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 shorten_filenames(fnames, count);
5473
Bram Moolenaar30613902019-12-01 22:11:18 +01005474 // If Shift held down, remember the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if ((modifiers & MOUSE_SHIFT) != 0)
5476 p = vim_strsave(fnames[0]);
5477 else
5478 p = NULL;
5479
Bram Moolenaar30613902019-12-01 22:11:18 +01005480 // Handle the drop, :edit or :split to get to the file. This also
5481 // frees fnames[]. Skip this if there is only one item, it's a
5482 // directory and Shift is held down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5484 && mch_isdir(fnames[0]))
5485 {
5486 vim_free(fnames[0]);
5487 vim_free(fnames);
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02005488 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 }
5490 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005491 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
Bram Moolenaar4671e882021-11-22 17:21:48 +00005492 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005494
5495 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496}
5497#endif
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005498
5499/*
5500 * Check if "key" is to interrupt us. Handles a key that has not had modifiers
5501 * applied yet.
5502 * Return the key with modifiers applied if so, NUL if not.
5503 */
5504 int
5505check_for_interrupt(int key, int modifiers_arg)
5506{
5507 int modifiers = modifiers_arg;
5508 int c = merge_modifyOtherKeys(key, &modifiers);
5509
5510 if ((c == Ctrl_C && ctrl_c_interrupts)
Bram Moolenaaraf50e892020-08-01 13:22:10 +02005511#ifdef UNIX
5512 || (intr_char != Ctrl_C && c == intr_char)
5513#endif
5514 )
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005515 {
5516 got_int = TRUE;
5517 return c;
5518 }
5519 return NUL;
5520}
5521
Bram Moolenaar2d12c252022-06-13 21:42:45 +01005522/*
5523 * If the "--gui-log-file fname" argument is given write the dialog title and
5524 * message to a file and return TRUE. Otherwise return FALSE.
5525 * When there is any problem opening the file or writing to the file this is
5526 * ignored, showing the dialog might get the test to get stuck.
5527 */
5528 int
5529gui_dialog_log(char_u *title, char_u *message)
5530{
5531 char_u *fname = get_gui_dialog_file();
5532 FILE *fd;
5533
5534 if (fname == NULL)
5535 return FALSE;
5536
5537 fd = mch_fopen((char *)fname, "a");
5538 if (fd != NULL)
5539 {
5540 fprintf(fd, "%s: %s\n", title, message);
5541 fclose(fd);
5542 }
5543 return TRUE;
5544}