blob: 8e7b079a5a4ea463ecff3af5c49080b23f70cfc7 [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;
491 static int recursive = 0;
492
493 /*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000494 * It's possible to use ":gui" in a .gvimrc file. The first half of this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 * function will then be executed at the first call, the rest by the
496 * recursive call. This allow the shell to be opened halfway reading a
497 * gvimrc file.
498 */
499 if (!recursive)
500 {
501 ++recursive;
502
503 clip_init(TRUE);
504
Bram Moolenaar30613902019-12-01 22:11:18 +0100505 // If can't initialize, don't try doing the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (gui_init_check() == FAIL)
507 {
508 --recursive;
509 clip_init(FALSE);
510 return;
511 }
512
513 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000514 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
515 * breaks the Paste toolbar button.
516 */
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100517 set_option_value_give_err((char_u *)"paste", 0L, NULL, 0);
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000518
Bram Moolenaaracc770a2020-04-12 15:11:06 +0200519 // Set t_Co to the number of colors: RGB.
520 set_color_count(256 * 256 * 256);
521
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000522 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 * Set up system-wide default menus.
524 */
525#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
526 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
527 {
528 sys_menu = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 sys_menu = FALSE;
531 }
532#endif
533
534 /*
535 * Switch on the mouse by default, unless the user changed it already.
536 * This can then be changed in the .gvimrc.
537 */
538 if (!option_was_set((char_u *)"mouse"))
539 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000540 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542 /*
543 * If -U option given, use only the initializations from that file and
544 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
545 */
546 if (use_gvimrc != NULL)
547 {
548 if (STRCMP(use_gvimrc, "NONE") != 0
549 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 && do_source(use_gvimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000551 semsg(_(e_cannot_read_from_str), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553 else
554 {
555 /*
556 * Get system wide defaults for gvim, only when file name defined.
557 */
558#ifdef SYS_GVIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#endif
561
562 /*
563 * Try to read GUI initialization commands from the following
564 * places:
565 * - environment variable GVIMINIT
566 * - the user gvimrc file (~/.gvimrc)
567 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
568 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
569 * The first that exists is used, the rest is ignored.
570 */
571 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000572 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000575 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200578#ifdef USR_GVIMRC_FILE3
579 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 )
583 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200584#ifdef USR_GVIMRC_FILE4
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE,
586 DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587#endif
588 }
589
590 /*
591 * Read initialization commands from ".gvimrc" in current
592 * directory. This is only done if the 'exrc' option is set.
593 * Because of security reasons we disallow shell and write
594 * commands now, except for unix if the file is owned by the user
595 * or 'secure' option has been reset in environment of global
596 * ".gvimrc".
597 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
598 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
599 */
600 if (p_exrc)
601 {
602#ifdef UNIX
603 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200604 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
Bram Moolenaar30613902019-12-01 22:11:18 +0100606 // if ".gvimrc" file is not owned by user, set 'secure'
607 // mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
609 secure = p_secure;
610 }
611#else
612 secure = p_secure;
613#endif
614
615 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200616 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617#ifdef SYS_GVIMRC_FILE
618 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200619 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620#endif
621#ifdef USR_GVIMRC_FILE2
622 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200623 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624#endif
625#ifdef USR_GVIMRC_FILE3
626 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200627 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200629#ifdef USR_GVIMRC_FILE4
630 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200631 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636 if (secure == 2)
637 need_wait_return = TRUE;
638 secure = 0;
639 }
640 }
641
642 if (need_wait_return || msg_didany)
643 wait_return(TRUE);
644
645 --recursive;
646 }
647
Bram Moolenaar30613902019-12-01 22:11:18 +0100648 // If recursive call opened the shell, return here from the first call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (gui.in_use)
650 return;
651
652 /*
653 * Create the GUI shell.
654 */
Bram Moolenaar30613902019-12-01 22:11:18 +0100655 gui.in_use = TRUE; // Must be set after menus have been set up
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if (gui_mch_init() == FAIL)
657 goto error;
658
Bram Moolenaar30613902019-12-01 22:11:18 +0100659 // Avoid a delay for an error message that was printed in the terminal
660 // where Vim was started.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 emsg_on_display = FALSE;
662 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100663 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 need_wait_return = FALSE;
665 msg_didany = FALSE;
666
667 /*
668 * Check validity of any generic resources that may have been loaded.
669 */
670 if (gui.border_width < 0)
671 gui.border_width = 0;
672
673 /*
674 * Set up the fonts. First use a font specified with "-fn" or "-font".
675 */
676 if (font_argument != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100677 set_option_value_give_err((char_u *)"gfn",
678 0L, (char_u *)font_argument, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 if (
680#ifdef FEAT_XFONTSET
681 (*p_guifontset == NUL
682 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
683#endif
684 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
685 : p_guifont, FALSE) == FAIL)
686 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000687 emsg(_(e_cannot_start_gui_no_valid_font_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 goto error2;
689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 if (gui_get_wide_font() == FAIL)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000691 emsg(_(e_guifontwide_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
693 gui.num_cols = Columns;
694 gui.num_rows = Rows;
695 gui_reset_scroll_region();
696
Bram Moolenaar30613902019-12-01 22:11:18 +0100697 // Create initial scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 FOR_ALL_WINDOWS(wp)
699 {
700 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
701 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
702 }
703 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
704
705#ifdef FEAT_MENU
706 gui_create_initial_menus(root_menu);
707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708#ifdef FEAT_SIGN_ICONS
709 sign_gui_started();
710#endif
711
Bram Moolenaar30613902019-12-01 22:11:18 +0100712 // Configure the desired menu and scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 gui_init_which_components(NULL);
714
Bram Moolenaar30613902019-12-01 22:11:18 +0100715 // All components of the GUI have been created now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 gui.shell_created = TRUE;
717
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100718#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100719 // Set the shell size, adjusted for the screen size. For GTK this only
720 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100721 // If the window is already maximized (e.g. when --windowid is passed in),
722 // we want to use the system-provided dimensions by passing FALSE to
723 // mustset. Otherwise, we want to initialize with the default rows/columns.
724 if (gui_mch_maximized())
725 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
726 else
727 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100728#else
729# ifndef FEAT_GUI_GTK
730 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
731# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#endif
733#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +0100734 // Need to set the size of the menubar after all the menus have been
735 // created.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 gui_mch_compute_menu_height((Widget)0);
737#endif
738
739 /*
740 * Actually open the GUI shell.
741 */
742 if (gui_mch_open() != FAIL)
743 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 maketitle();
745 resettitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000746
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 init_gui_options();
748#ifdef FEAT_ARABIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100749 // Our GUI can't do bidi.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 p_tbidi = FALSE;
751#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000752#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100753 // Give GTK+ a chance to put all widget's into place.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000755
756# ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +0100757 // If there is no 'm' in 'guioptions' we need to remove the menu now.
758 // It was still there to make F10 work.
Bram Moolenaar18144c82006-04-12 21:52:12 +0000759 if (vim_strchr(p_go, GO_MENUS) == NULL)
760 {
761 --gui.starting;
762 gui_mch_enable_menu(FALSE);
763 ++gui.starting;
764 gui_mch_update();
765 }
766# endif
767
Bram Moolenaar30613902019-12-01 22:11:18 +0100768 // Now make sure the shell fits on the screen.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100769 if (gui_mch_maximized())
770 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
771 else
772 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100774 // When 'lines' was set while starting up the topframe may have to be
775 // resized.
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000776 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000777
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100778#ifdef FEAT_BEVAL_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100779 // Always create the Balloon Evaluation area, but disable it when
780 // 'ballooneval' is off.
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100781 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200782 {
783# ifdef FEAT_VARTABS
784 vim_free(balloonEval->vts);
785# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100786 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200787 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100788 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000789# ifdef FEAT_GUI_GTK
790 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
791 &general_beval_cb, NULL);
792# else
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100793# if defined(FEAT_GUI_MOTIF)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000794 {
795 extern Widget textArea;
796 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000797 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000798 }
799# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100800# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000801 balloonEval = gui_mch_create_beval_area(NULL, NULL,
802 &general_beval_cb, NULL);
803# endif
804# endif
805# endif
806 if (!p_beval)
807 gui_mch_disable_beval_area(balloonEval);
808#endif
Bram Moolenaarad772a62020-06-01 14:07:49 +0200809
810#ifndef FEAT_GUI_MSWIN
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200811 // In the GUI modifiers are prepended to keys.
Bram Moolenaarad772a62020-06-01 14:07:49 +0200812 // Don't do this for MS-Windows yet, it sends CTRL-K without the
813 // modifier.
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200814 seenModifyOtherKeys = TRUE;
Bram Moolenaarad772a62020-06-01 14:07:49 +0200815#endif
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
818 if (!im_xim_isvalid_imactivate())
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000819 emsg(_(e_value_of_imactivatekey_is_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100821 // When 'cmdheight' was set during startup it may not have taken
822 // effect yet.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000823 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000824 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 return;
827 }
828
829error2:
830#ifdef FEAT_GUI_X11
Bram Moolenaar30613902019-12-01 22:11:18 +0100831 // undo gui_mch_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 gui_mch_uninit();
833#endif
834
835error:
836 gui.in_use = FALSE;
837 clip_init(FALSE);
838}
839
840
841 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100842gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
Bram Moolenaar30613902019-12-01 22:11:18 +0100844 // don't free the fonts, it leads to a BUS error
845 // richard@whitequeen.com Jul 99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 gui.in_use = FALSE;
848 gui_mch_exit(rc);
849}
850
Bram Moolenaar9372a112005-12-06 19:59:18 +0000851#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +0200852 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000853# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854/*
855 * Called when the GUI shell is closed by the user. If there are no changed
856 * files Vim exits, otherwise there will be a dialog to ask the user what to
857 * do.
858 * When this function returns, Vim should NOT exit!
859 */
860 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100861gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
Bram Moolenaar3552e742021-05-29 12:21:58 +0200863 cmdmod_T save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
Bram Moolenaar3552e742021-05-29 12:21:58 +0200865 if (before_quit_autocmds(curwin, TRUE, FALSE))
866 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
Bram Moolenaar30613902019-12-01 22:11:18 +0100868 // Only exit when there are no changed files
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 exiting = TRUE;
870# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200871 cmdmod.cmod_flags |= CMOD_BROWSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872# endif
873# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +0200874 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100876 // If there are changed buffers, present the user with a dialog if
877 // possible, otherwise give an error message.
Bram Moolenaar027387f2016-01-02 22:25:52 +0100878 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 getout(0);
880
881 exiting = FALSE;
882 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +0100883 gui_update_screen(); // redraw, window may show changed buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884}
885#endif
886
887/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200888 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 * first font name that works is used. If none is found, use the default
890 * font.
891 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
892 * Return OK when able to set the font. When it failed FAIL is returned and
893 * the fonts are unchanged.
894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100896gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897{
898#define FONTLEN 320
899 char_u font_name[FONTLEN];
900 int font_list_empty = FALSE;
901 int ret = FAIL;
902
903 if (!gui.in_use)
904 return FAIL;
905
906 font_name[0] = NUL;
907 if (*font_list == NUL)
908 font_list_empty = TRUE;
909 else
910 {
911#ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +0100912 // When using a fontset, the whole list of fonts is one name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 if (fontset)
914 ret = gui_mch_init_font(font_list, TRUE);
915 else
916#endif
917 while (*font_list != NUL)
918 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100919 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
921
Bram Moolenaar30613902019-12-01 22:11:18 +0100922 // Careful!!! The Win32 version of gui_mch_init_font(), when
923 // called with "*" will change p_guifont to the selected font
924 // name, which frees the old value. This makes font_list
925 // invalid. Thus when OK is returned here, font_list must no
926 // longer be used!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 if (gui_mch_init_font(font_name, FALSE) == OK)
928 {
K.Takata94373c42022-01-27 15:04:22 +0000929#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar30613902019-12-01 22:11:18 +0100930 // If it's a Unicode font, try setting 'guifontwide' to a
931 // similar double-width font.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
933 && strstr((char *)font_name, "10646") != NULL)
934 set_guifontwide(font_name);
935#endif
936 ret = OK;
937 break;
938 }
939 }
940 }
941
942 if (ret != OK
943 && STRCMP(font_list, "*") != 0
944 && (font_list_empty || gui.norm_font == NOFONT))
945 {
946 /*
947 * Couldn't load any font in 'font_list', keep the current font if
948 * there is one. If 'font_list' is empty, or if there is no current
949 * font, tell gui_mch_init_font() to try to find a font we can load.
950 */
951 ret = gui_mch_init_font(NULL, FALSE);
952 }
953
954 if (ret == OK)
955 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200956#ifndef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100957 // Set normal font as current font
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958# ifdef FEAT_XFONTSET
959 if (gui.fontset != NOFONTSET)
960 gui_mch_set_fontset(gui.fontset);
961 else
962# endif
963 gui_mch_set_font(gui.norm_font);
964#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100965 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 }
967
968 return ret;
969}
970
K.Takata94373c42022-01-27 15:04:22 +0000971#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972/*
973 * Try setting 'guifontwide' to a font twice as wide as "name".
974 */
975 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100976set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977{
978 int i = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +0100979 char_u wide_name[FONTLEN + 10]; // room for 2 * width and '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 char_u *wp = NULL;
981 char_u *p;
982 GuiFont font;
983
984 wp = wide_name;
985 for (p = name; *p != NUL; ++p)
986 {
987 *wp++ = *p;
988 if (*p == '-')
989 {
990 ++i;
Bram Moolenaar30613902019-12-01 22:11:18 +0100991 if (i == 6) // font type: change "--" to "-*-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
993 if (p[1] == '-')
994 *wp++ = '*';
995 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100996 else if (i == 12) // found the width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 {
998 ++p;
999 i = getdigits(&p);
1000 if (i != 0)
1001 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001002 // Double the width specification.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 sprintf((char *)wp, "%d%s", i * 2, p);
1004 font = gui_mch_get_font(wide_name, FALSE);
1005 if (font != NOFONT)
1006 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001007 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 gui.wide_font = font;
1009 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001010 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 }
1012 }
1013 break;
1014 }
1015 }
1016 }
1017}
K.Takata94373c42022-01-27 15:04:22 +00001018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
1020/*
1021 * Get the font for 'guifontwide'.
1022 * Return FAIL for an invalid font name.
1023 */
1024 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001025gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
1027 GuiFont font = NOFONT;
1028 char_u font_name[FONTLEN];
1029 char_u *p;
1030
Bram Moolenaar30613902019-12-01 22:11:18 +01001031 if (!gui.in_use) // Can't allocate font yet, assume it's OK.
1032 return OK; // Will give an error message later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
1034 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1035 {
1036 for (p = p_guifontwide; *p != NUL; )
1037 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001038 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1040 font = gui_mch_get_font(font_name, FALSE);
1041 if (font != NOFONT)
1042 break;
1043 }
1044 if (font == NOFONT)
1045 return FAIL;
1046 }
1047
1048 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001049#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001050 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (font != NOFONT && gui.norm_font != NOFONT
1052 && pango_font_description_equal(font, gui.norm_font))
1053 {
1054 gui.wide_font = NOFONT;
1055 gui_mch_free_font(font);
1056 }
1057 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001060#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001061 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001062#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001063 /*
1064 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1065 * support those fonts for 'guifontwide'.
1066 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 return OK;
1069}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01001071#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001072/*
1073 * Set list of ascii characters that combined can create ligature.
1074 * Store them in char map for quick access from gui_gtk2_draw_string.
1075 */
1076 void
1077gui_set_ligatures(void)
1078{
1079 char_u *p;
1080
1081 if (*p_guiligatures != NUL)
1082 {
1083 // check for invalid characters
1084 for (p = p_guiligatures; *p != NUL; ++p)
1085 if (*p < 32 || *p > 127)
1086 {
1087 emsg(_(e_ascii_code_not_in_range));
1088 return;
1089 }
1090
1091 // store valid setting into ligatures_map
1092 CLEAR_FIELD(gui.ligatures_map);
1093 for (p = p_guiligatures; *p != NUL; ++p)
1094 gui.ligatures_map[*p] = 1;
1095 }
1096 else
1097 CLEAR_FIELD(gui.ligatures_map);
1098}
Dusan Popovicce59b9f2021-11-22 17:18:44 +00001099
1100/*
1101 * Adjust the columns to undraw for when the cursor is on ligatures.
1102 */
1103 static void
1104gui_adjust_undraw_cursor_for_ligatures(int *startcol, int *endcol)
1105{
1106 int off;
1107
1108 if (ScreenLines == NULL || *p_guiligatures == NUL)
1109 return;
1110
1111 // expand before the cursor for all the chars in gui.ligatures_map
1112 off = LineOffset[gui.cursor_row] + *startcol;
1113 if (gui.ligatures_map[ScreenLines[off]])
1114 while (*startcol > 0 && gui.ligatures_map[ScreenLines[--off]])
1115 (*startcol)--;
1116
1117 // expand after the cursor for all the chars in gui.ligatures_map
1118 off = LineOffset[gui.cursor_row] + *endcol;
1119 if (gui.ligatures_map[ScreenLines[off]])
1120 while (*endcol < ((int)screen_Columns - 1)
1121 && gui.ligatures_map[ScreenLines[++off]])
1122 (*endcol)++;
1123}
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001124#endif
1125
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001126 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001127gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128{
1129 gui.row = row;
1130 gui.col = col;
1131}
1132
1133/*
1134 * gui_check_pos - check if the cursor is on the screen.
1135 */
1136 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001137gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138{
1139 if (gui.row >= screen_Rows)
1140 gui.row = screen_Rows - 1;
1141 if (gui.col >= screen_Columns)
1142 gui.col = screen_Columns - 1;
1143 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1144 gui.cursor_is_valid = FALSE;
1145}
1146
1147/*
1148 * Redraw the cursor if necessary or when forced.
1149 * Careful: The contents of ScreenLines[] must match what is on the screen,
1150 * otherwise this goes wrong. May need to call out_flush() first.
1151 */
1152 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001153gui_update_cursor(
Bram Moolenaar30613902019-12-01 22:11:18 +01001154 int force, // when TRUE, update even when not moved
1155 int clear_selection) // clear selection under cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 int cur_width = 0;
1158 int cur_height = 0;
1159 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001160 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001162#ifdef FEAT_TERMINAL
1163 guicolor_T shape_fg = INVALCOLOR;
1164 guicolor_T shape_bg = INVALCOLOR;
1165#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001166 guicolor_T cfg, cbg, cc; // cursor fore-/background color
1167 int cattr; // cursor attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 int attr;
1169 attrentry_T *aep = NULL;
1170
Bram Moolenaar30613902019-12-01 22:11:18 +01001171 // Don't update the cursor when halfway busy scrolling or the screen size
1172 // doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then.
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001173 if (!can_update_cursor || screen_Columns != gui.num_cols
1174 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 return;
1176
1177 gui_check_pos();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001178
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001179 if (gui.cursor_is_valid && !force
1180 && gui.row == gui.cursor_row && gui.col == gui.cursor_col)
1181 return;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001182
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001183 gui_undraw_cursor();
1184
1185 // If a cursor-less sleep is ongoing, leave the cursor invisible
1186 if (cursor_is_sleeping())
1187 return;
1188
1189 if (gui.row < 0)
1190 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001191#ifdef HAVE_INPUT_METHOD
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001192 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1193 im_set_position(gui.row, gui.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001195 gui.cursor_row = gui.row;
1196 gui.cursor_col = gui.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001198 // Only write to the screen after ScreenLines[] has been initialized
1199 if (!screen_cleared || ScreenLines == NULL)
1200 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001202 // Clear the selection if we are about to write over it
1203 if (clear_selection)
1204 clip_may_clear_selection(gui.row, gui.row);
1205 // Check that the cursor is inside the shell (resizing may have made
1206 // it invalid)
1207 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1208 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001210 gui.cursor_is_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001212 /*
1213 * How the cursor is drawn depends on the current mode.
1214 * When in a terminal window use the shape/color specified there.
1215 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001216#ifdef FEAT_TERMINAL
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001217 if (terminal_is_active())
1218 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
1219 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001220#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001221 shape = &shape_table[get_shape_idx(FALSE)];
1222 if (State & MODE_LANGMAP)
1223 id = shape->id_lm;
1224 else
1225 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001227 // get the colors and attributes for the cursor. Default is inverted
1228 cfg = INVALCOLOR;
1229 cbg = INVALCOLOR;
1230 cattr = HL_INVERSE;
1231 gui_mch_set_blinking(shape->blinkwait,
1232 shape->blinkon,
1233 shape->blinkoff);
1234 if (shape->blinkwait == 0 || shape->blinkon == 0
1235 || shape->blinkoff == 0)
1236 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001237#ifdef FEAT_TERMINAL
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001238 if (shape_bg != INVALCOLOR)
1239 {
1240 cattr = 0;
1241 cfg = shape_fg;
1242 cbg = shape_bg;
1243 }
1244 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (id > 0)
1247 {
1248 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar54612582019-11-21 17:13:31 +01001249#if defined(HAVE_INPUT_METHOD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
1251 static int iid;
1252 guicolor_T fg, bg;
1253
Bram Moolenaarc236c162008-07-13 17:41:49 +00001254 if (
Bram Moolenaar54612582019-11-21 17:13:31 +01001255# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001256 preedit_get_status()
1257# else
1258 im_get_status()
1259# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001260 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
1262 iid = syn_name2id((char_u *)"CursorIM");
1263 if (iid > 0)
1264 {
1265 syn_id2colors(iid, &fg, &bg);
1266 if (bg != INVALCOLOR)
1267 cbg = bg;
1268 if (fg != INVALCOLOR)
1269 cfg = fg;
1270 }
1271 }
1272 }
1273#endif
1274 }
1275
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001276 /*
1277 * Get the attributes for the character under the cursor.
1278 * When no cursor color was given, use the character color.
1279 */
1280 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1281 if (attr > HL_ALL)
1282 aep = syn_gui_attr2entry(attr);
1283 if (aep != NULL)
1284 {
1285 attr = aep->ae_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (cfg == INVALCOLOR)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001287 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1288 : aep->ae_u.gui.fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (cbg == INVALCOLOR)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001290 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1291 : aep->ae_u.gui.bg_color);
1292 }
1293 if (cfg == INVALCOLOR)
1294 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1295 if (cbg == INVALCOLOR)
1296 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
1298#ifdef FEAT_XIM
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001299 if (aep != NULL)
1300 {
1301 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1302 : aep->ae_u.gui.bg_color);
1303 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1304 : aep->ae_u.gui.fg_color);
1305 if (xim_bg_color == INVALCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001307 : gui.back_pixel;
1308 if (xim_fg_color == INVALCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001310 : gui.norm_pixel;
1311 }
1312 else
1313 {
1314 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1315 : gui.back_pixel;
1316 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1317 : gui.norm_pixel;
1318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319#endif
1320
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001321 attr &= ~HL_INVERSE;
1322 if (cattr & HL_INVERSE)
1323 {
1324 cc = cbg;
1325 cbg = cfg;
1326 cfg = cc;
1327 }
1328 cattr &= ~HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001330 /*
1331 * When we don't have window focus, draw a hollow cursor.
1332 */
1333 if (!gui.in_focus)
1334 {
1335 gui_mch_draw_hollow_cursor(cbg);
1336 return;
1337 }
1338
1339 old_hl_mask = gui.highlight_mask;
1340 if (shape->shape == SHAPE_BLOCK)
1341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001343 * Draw the text character with the cursor colors. Use the
1344 * character attributes plus the cursor attributes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 */
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001346 gui.highlight_mask = (cattr | attr);
1347 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1348 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1349 }
1350 else
1351 {
1352#if defined(FEAT_RIGHTLEFT)
1353 int col_off = FALSE;
1354#endif
1355 /*
1356 * First draw the partial cursor, then overwrite with the text
1357 * character, using a transparent background.
1358 */
1359 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001361 cur_height = gui.char_height;
1362 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364 else
1365 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001366 cur_height = (gui.char_height * shape->percentage + 99) / 100;
1367 cur_width = gui.char_width;
1368 }
1369 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1370 LineOffset[gui.row] + screen_Columns) > 1)
1371 {
1372 // Double wide character.
1373 if (shape->shape != SHAPE_VER)
1374 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001375#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001376 if (CURSOR_BAR_RIGHT)
1377 {
1378 // gui.col points to the left half of the character but
1379 // the vertical line needs to be on the right half.
1380 // A double-wide horizontal line is also drawn from the
1381 // right half in gui_mch_draw_part_cursor().
1382 col_off = TRUE;
1383 ++gui.col;
Bram Moolenaar13505972019-01-24 15:04:48 +01001384 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001385#endif
1386 }
1387 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001388#if defined(FEAT_RIGHTLEFT)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001389 if (col_off)
1390 --gui.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#endif
1392
Bram Moolenaar30613902019-12-01 22:11:18 +01001393#ifndef FEAT_GUI_MSWIN // doesn't seem to work for MSWindows
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001394 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1395 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1396 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1397 (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001400 gui.highlight_mask = old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401}
1402
1403#if defined(FEAT_MENU) || defined(PROTO)
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01001404 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001405gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001407# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (gui.menu_is_active && gui.in_use)
1409 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1410# endif
1411}
1412#endif
1413
1414/*
1415 * Position the various GUI components (text area, menu). The vertical
1416 * scrollbars are NOT handled here. See gui_update_scrollbars().
1417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001419gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
1421 int text_area_x;
1422 int text_area_y;
1423 int text_area_width;
1424 int text_area_height;
1425
Bram Moolenaar30613902019-12-01 22:11:18 +01001426 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 ++hold_gui_events;
1428
1429 text_area_x = 0;
1430 if (gui.which_scrollbars[SBAR_LEFT])
1431 text_area_x += gui.scrollbar_width;
1432
1433 text_area_y = 0;
1434#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1435 gui.menu_width = total_width;
1436 if (gui.menu_is_active)
1437 text_area_y += gui.menu_height;
1438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
K.Takata6e1d31e2022-02-03 13:05:32 +00001440#if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001441 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001442 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001443 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001444#endif
1445
Bram Moolenaar0b962e52022-04-03 18:02:37 +01001446#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) \
K.Takatac81e9bf2022-01-16 14:15:49 +00001447 || defined(FEAT_GUI_HAIKU) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1449 {
Bram Moolenaar0b962e52022-04-03 18:02:37 +01001450# if defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 gui_mch_set_toolbar_pos(0, text_area_y,
1452 gui.menu_width, gui.toolbar_height);
1453# endif
1454 text_area_y += gui.toolbar_height;
1455 }
1456#endif
1457
K.Takata6e1d31e2022-02-03 13:05:32 +00001458#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_HAIKU)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001459 gui_mch_set_tabline_pos(0, text_area_y,
1460 gui.menu_width, gui.tabline_height);
1461 if (gui_has_tabline())
1462 text_area_y += gui.tabline_height;
1463#endif
1464
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1466 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1467
1468 gui_mch_set_text_area_pos(text_area_x,
1469 text_area_y,
1470 text_area_width,
1471 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001472#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 + xim_get_status_area_height()
1474#endif
1475 );
1476#ifdef FEAT_MENU
1477 gui_position_menu();
1478#endif
1479 if (gui.which_scrollbars[SBAR_BOTTOM])
1480 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1481 text_area_x,
Bram Moolenaar203ec772020-07-17 20:43:43 +02001482 text_area_y + text_area_height
1483 + gui_mch_get_scrollbar_ypadding(),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 text_area_width,
1485 gui.scrollbar_height);
1486 gui.left_sbar_x = 0;
Bram Moolenaar203ec772020-07-17 20:43:43 +02001487 gui.right_sbar_x = text_area_x + text_area_width
1488 + gui_mch_get_scrollbar_xpadding();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
1490 --hold_gui_events;
1491}
1492
Bram Moolenaar02743632005-07-25 20:42:36 +00001493/*
1494 * Get the width of the widgets and decorations to the side of the text area.
1495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001497gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
1499 int base_width;
1500
1501 base_width = 2 * gui.border_offset;
1502 if (gui.which_scrollbars[SBAR_LEFT])
1503 base_width += gui.scrollbar_width;
1504 if (gui.which_scrollbars[SBAR_RIGHT])
1505 base_width += gui.scrollbar_width;
1506 return base_width;
1507}
1508
Bram Moolenaar02743632005-07-25 20:42:36 +00001509/*
1510 * Get the height of the widgets and decorations above and below the text area.
1511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001513gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
1515 int base_height;
1516
1517 base_height = 2 * gui.border_offset;
1518 if (gui.which_scrollbars[SBAR_BOTTOM])
1519 base_height += gui.scrollbar_height;
1520#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001521 // We can't take the sizes properly into account until anything is
1522 // realized. Therefore we recalculate all the values here just before
1523 // setting the size. (--mdcki)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524#else
1525# ifdef FEAT_MENU
1526 if (gui.menu_is_active)
1527 base_height += gui.menu_height;
1528# endif
1529# ifdef FEAT_TOOLBAR
1530 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 base_height += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001533# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001534 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001535 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001536 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001537# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1539 base_height += gui_mch_text_area_extra_height();
1540# endif
1541#endif
1542 return base_height;
1543}
1544
1545/*
1546 * Should be called after the GUI shell has been resized. Its arguments are
1547 * the new width and height of the shell in pixels.
1548 */
1549 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001550gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 static int busy = FALSE;
1553
Bram Moolenaar30613902019-12-01 22:11:18 +01001554 if (!gui.shell_created) // ignore when still initializing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 return;
1556
1557 /*
1558 * Can't resize the screen while it is being redrawn. Remember the new
1559 * size and handle it later.
1560 */
1561 if (updating_screen || busy)
1562 {
1563 new_pixel_width = pixel_width;
1564 new_pixel_height = pixel_height;
1565 return;
1566 }
1567
1568again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001569 new_pixel_width = 0;
1570 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 busy = TRUE;
1572
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001573#ifdef FEAT_GUI_HAIKU
1574 vim_lock_screen();
1575#endif
1576
Bram Moolenaar30613902019-12-01 22:11:18 +01001577 // Flush pending output before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 out_flush();
1579
1580 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001581 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 /*
1587 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1588 * at the last line here (why does it have to be one row too low?).
1589 */
Bram Moolenaar24959102022-05-07 20:01:16 +01001590 if (State == MODE_ASKMORE || State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 gui.row = gui.num_rows;
1592
Bram Moolenaar30613902019-12-01 22:11:18 +01001593 // Only comparing Rows and Columns may be sufficient, but let's stay on
1594 // the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
lilydjwg6e0a18f2024-01-29 20:54:28 +01001596 || gui.num_rows != Rows || gui.num_cols != Columns || gui.force_redraw)
1597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 shell_resized();
lilydjwg6e0a18f2024-01-29 20:54:28 +01001599 gui.force_redraw = 0;
1600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001602#ifdef FEAT_GUI_HAIKU
1603 vim_unlock_screen();
1604#endif
1605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 gui_update_scrollbars(TRUE);
1607 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001608#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 xim_set_status_area();
1610#endif
1611
1612 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001613
Bram Moolenaar30613902019-12-01 22:11:18 +01001614 // We may have been called again while redrawing the screen.
1615 // Need to do it all again with the latest size then. But only if the size
1616 // actually changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 if (new_pixel_height)
1618 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001619 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1620 {
1621 new_pixel_width = 0;
1622 new_pixel_height = 0;
1623 }
1624 else
1625 {
1626 pixel_width = new_pixel_width;
1627 pixel_height = new_pixel_height;
1628 goto again;
1629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 }
1631}
1632
1633/*
1634 * Check if gui_resize_shell() must be called.
1635 */
1636 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001637gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 if (new_pixel_height)
Bram Moolenaar30613902019-12-01 22:11:18 +01001640 // careful: gui_resize_shell() may postpone the resize again if we
1641 // were called indirectly by it
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001642 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643}
1644
1645 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001646gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
1648 Rows = gui.num_rows;
1649 Columns = gui.num_cols;
1650 return OK;
1651}
1652
1653/*
1654 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001655 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1656 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001657 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1658 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001661gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001662 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001663 int fit_to_display,
Bram Moolenaar30613902019-12-01 22:11:18 +01001664 int direction) // RESIZE_HOR, RESIZE_VER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
1666 int base_width;
1667 int base_height;
1668 int width;
1669 int height;
1670 int min_width;
1671 int min_height;
1672 int screen_w;
1673 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001674#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001675 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001676 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001677#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001678 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
1680 if (!gui.shell_created)
1681 return;
1682
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001683#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01001684 // If not setting to a user specified size and maximized, calculate the
1685 // number of characters that fit in the maximized window.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001686 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1687 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 {
1689 gui_mch_newfont();
1690 return;
1691 }
1692#endif
1693
1694 base_width = gui_get_base_width();
1695 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001696 if (fit_to_display)
Bram Moolenaar30613902019-12-01 22:11:18 +01001697 // Remember the original window position.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001698 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001699
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001700 width = Columns * gui.char_width + base_width;
1701 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703 if (fit_to_display)
1704 {
1705 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001706 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 {
1708 Columns = (screen_w - base_width) / gui.char_width;
1709 if (Columns < MIN_COLUMNS)
1710 Columns = MIN_COLUMNS;
1711 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001712#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001713 ++did_adjust;
1714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001716 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 {
1718 Rows = (screen_h - base_height) / gui.char_height;
1719 check_shellsize();
1720 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001721#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001722 ++did_adjust;
1723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001725#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001726 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1727 && height + gui.char_height >= screen_h))
Bram Moolenaar30613902019-12-01 22:11:18 +01001728 // don't unmaximize if at maximum size
Bram Moolenaar09736232009-09-23 16:14:49 +00001729 un_maximize = FALSE;
1730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001732 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 gui.num_cols = Columns;
1734 gui.num_rows = Rows;
1735
1736 min_width = base_width + MIN_COLUMNS * gui.char_width;
1737 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001738 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001739
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001740#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001741 if (un_maximize)
1742 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001743 // If the window size is smaller than the screen unmaximize the
1744 // window, otherwise resizing won't work.
Bram Moolenaar09736232009-09-23 16:14:49 +00001745 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1746 if ((width + gui.char_width < screen_w
1747 || height + gui.char_height * 2 < screen_h)
1748 && gui_mch_maximized())
1749 gui_mch_unmaximize();
1750 }
1751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
1753 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001754 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001756 if (fit_to_display && x >= 0 && y >= 0)
1757 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001758 // Some window managers put the Vim window left of/above the screen.
1759 // Only change the position if it wasn't already negative before
1760 // (happens on MS-Windows with a secondary monitor).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 gui_mch_update();
1762 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1763 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1764 }
1765
1766 gui_position_components(width);
1767 gui_update_scrollbars(TRUE);
1768 gui_reset_scroll_region();
1769}
1770
1771/*
1772 * Called when Rows and/or Columns has changed.
1773 */
1774 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001775gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 gui_reset_scroll_region();
1778}
1779
1780/*
1781 * Make scroll region cover whole screen.
1782 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001783 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001784gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 gui.scroll_region_top = 0;
1787 gui.scroll_region_bot = gui.num_rows - 1;
1788 gui.scroll_region_left = 0;
1789 gui.scroll_region_right = gui.num_cols - 1;
1790}
1791
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001792 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001793gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794{
Bram Moolenaar30613902019-12-01 22:11:18 +01001795 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 gui.highlight_mask = mask;
Bram Moolenaar30613902019-12-01 22:11:18 +01001797 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 gui.highlight_mask |= mask;
1799}
1800
1801 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001802gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
Bram Moolenaar30613902019-12-01 22:11:18 +01001804 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 gui.highlight_mask = HL_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001806 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 gui.highlight_mask &= ~mask;
1808}
1809
1810/*
1811 * Clear a rectangular region of the screen from text pos (row1, col1) to
1812 * (row2, col2) inclusive.
1813 */
1814 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001815gui_clear_block(
1816 int row1,
1817 int col1,
1818 int row2,
1819 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820{
Bram Moolenaar30613902019-12-01 22:11:18 +01001821 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 clip_may_clear_selection(row1, row2);
1823
1824 gui_mch_clear_block(row1, col1, row2, col2);
1825
Bram Moolenaar30613902019-12-01 22:11:18 +01001826 // Invalidate cursor if it was in this block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1828 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1829 gui.cursor_is_valid = FALSE;
1830}
1831
1832/*
1833 * Write code to update the cursor later. This avoids the need to flush the
1834 * output buffer before calling gui_update_cursor().
1835 */
1836 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001837gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001839 OUT_STR("\033|s");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840}
1841
1842 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001843gui_write(
1844 char_u *s,
1845 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846{
1847 char_u *p;
1848 int arg1 = 0, arg2 = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01001849 int force_cursor = FALSE; // force cursor update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 int force_scrollbar = FALSE;
1851 static win_T *old_curwin = NULL;
1852
Bram Moolenaar30613902019-12-01 22:11:18 +01001853// #define DEBUG_GUI_WRITE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854#ifdef DEBUG_GUI_WRITE
1855 {
1856 int i;
1857 char_u *str;
1858
1859 printf("gui_write(%d):\n ", len);
1860 for (i = 0; i < len; i++)
1861 if (s[i] == ESC)
1862 {
1863 if (i != 0)
1864 printf("\n ");
1865 printf("<ESC>");
1866 }
1867 else
1868 {
1869 str = transchar_byte(s[i]);
1870 if (str[0] && str[1])
1871 printf("<%s>", (char *)str);
1872 else
1873 printf("%s", (char *)str);
1874 }
1875 printf("\n");
1876 }
1877#endif
1878 while (len)
1879 {
1880 if (s[0] == ESC && s[1] == '|')
1881 {
1882 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001883 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 {
1885 arg1 = getdigits(&p);
1886 if (p > s + len)
1887 break;
1888 if (*p == ';')
1889 {
1890 ++p;
1891 arg2 = getdigits(&p);
1892 if (p > s + len)
1893 break;
1894 }
1895 }
1896 switch (*p)
1897 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001898 case 'C': // Clear screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 clip_scroll_selection(9999);
1900 gui_mch_clear_all();
1901 gui.cursor_is_valid = FALSE;
1902 force_scrollbar = TRUE;
1903 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001904 case 'M': // Move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 gui_set_cursor(arg1, arg2);
1906 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001907 case 's': // force cursor (shape) update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 force_cursor = TRUE;
1909 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001910 case 'R': // Set scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (arg1 < arg2)
1912 {
1913 gui.scroll_region_top = arg1;
1914 gui.scroll_region_bot = arg2;
1915 }
1916 else
1917 {
1918 gui.scroll_region_top = arg2;
1919 gui.scroll_region_bot = arg1;
1920 }
1921 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001922 case 'V': // Set vertical scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (arg1 < arg2)
1924 {
1925 gui.scroll_region_left = arg1;
1926 gui.scroll_region_right = arg2;
1927 }
1928 else
1929 {
1930 gui.scroll_region_left = arg2;
1931 gui.scroll_region_right = arg1;
1932 }
1933 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001934 case 'd': // Delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 gui_delete_lines(gui.row, 1);
1936 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001937 case 'D': // Delete lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 gui_delete_lines(gui.row, arg1);
1939 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001940 case 'i': // Insert line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 gui_insert_lines(gui.row, 1);
1942 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001943 case 'I': // Insert lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 gui_insert_lines(gui.row, arg1);
1945 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001946 case '$': // Clear to end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 gui_clear_block(gui.row, gui.col, gui.row,
1948 (int)Columns - 1);
1949 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001950 case 'h': // Turn on highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 gui_start_highlight(arg1);
1952 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001953 case 'H': // Turn off highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 gui_stop_highlight(arg1);
1955 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001956 case 'f': // flash the window (visual bell)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1958 break;
1959 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01001960 p = s + 1; // Skip the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 break;
1962 }
1963 len -= (int)(++p - s);
1964 s = p;
1965 }
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001966 else if (s[0] < 0x20 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967#ifdef FEAT_SIGN_ICONS
1968 && s[0] != SIGN_BYTE
1969# ifdef FEAT_NETBEANS_INTG
1970 && s[0] != MULTISIGN_BYTE
1971# endif
1972#endif
1973 )
1974 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001975 if (s[0] == '\n') // NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
1977 gui.col = 0;
1978 if (gui.row < gui.scroll_region_bot)
1979 gui.row++;
1980 else
1981 gui_delete_lines(gui.scroll_region_top, 1);
1982 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001983 else if (s[0] == '\r') // CR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
1985 gui.col = 0;
1986 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001987 else if (s[0] == '\b') // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
1989 if (gui.col)
1990 --gui.col;
1991 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001992 else if (s[0] == Ctrl_L) // cursor-right
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
1994 ++gui.col;
1995 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001996 else if (s[0] == Ctrl_G) // Beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
1998 gui_mch_beep();
1999 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002000 // Other Ctrl character: shouldn't happen!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
Bram Moolenaar30613902019-12-01 22:11:18 +01002002 --len; // Skip this char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 ++s;
2004 }
2005 else
2006 {
2007 p = s;
2008 while (len > 0 && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 *p >= 0x20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010#ifdef FEAT_SIGN_ICONS
2011 || *p == SIGN_BYTE
2012# ifdef FEAT_NETBEANS_INTG
2013 || *p == MULTISIGN_BYTE
2014# endif
2015#endif
2016 ))
2017 {
2018 len--;
2019 p++;
2020 }
2021 gui_outstr(s, (int)(p - s));
2022 s = p;
2023 }
2024 }
2025
Bram Moolenaar30613902019-12-01 22:11:18 +01002026 // Postponed update of the cursor (won't work if "can_update_cursor" isn't
2027 // set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 if (force_cursor)
2029 gui_update_cursor(TRUE, TRUE);
2030
Bram Moolenaar30613902019-12-01 22:11:18 +01002031 // When switching to another window the dragging must have stopped.
2032 // Required for GTK, dragged_sb isn't reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 if (old_curwin != curwin)
2034 gui.dragged_sb = SBAR_NONE;
2035
Bram Moolenaar30613902019-12-01 22:11:18 +01002036 // Update the scrollbars after clearing the screen or when switched
2037 // to another window.
2038 // Update the horizontal scrollbar always, it's difficult to check all
2039 // situations where it might change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 if (force_scrollbar || old_curwin != curwin)
2041 gui_update_scrollbars(force_scrollbar);
2042 else
2043 gui_update_horiz_scrollbar(FALSE);
2044 old_curwin = curwin;
2045
2046 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002047 * We need to make sure this is cleared since GTK doesn't tell us when
2048 * the user is done dragging.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 */
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002050#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 gui.dragged_sb = SBAR_NONE;
2052#endif
2053
Bram Moolenaar30613902019-12-01 22:11:18 +01002054 gui_may_flush(); // In case vim decides to take a nap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055}
2056
2057/*
2058 * When ScreenLines[] is invalid, updating the cursor should not be done, it
2059 * produces wrong results. Call gui_dont_update_cursor() before that code and
2060 * gui_can_update_cursor() afterwards.
2061 */
2062 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02002063gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002065 if (!gui.in_use)
2066 return;
2067
2068 // Undraw the cursor now, we probably can't do it after the change.
2069 if (undraw)
2070 gui_undraw_cursor();
2071 can_update_cursor = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072}
2073
2074 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002075gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076{
2077 can_update_cursor = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002078 // No need to update the cursor right now, there is always more output
2079 // after scrolling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080}
2081
Bram Moolenaara338adc2018-01-31 20:51:47 +01002082/*
2083 * Disable issuing gui_mch_flush().
2084 */
2085 void
2086gui_disable_flush(void)
2087{
2088 ++disable_flush;
2089}
2090
2091/*
2092 * Enable issuing gui_mch_flush().
2093 */
2094 void
2095gui_enable_flush(void)
2096{
2097 --disable_flush;
2098}
2099
2100/*
2101 * Issue gui_mch_flush() if it is not disabled.
2102 */
2103 void
2104gui_may_flush(void)
2105{
2106 if (disable_flush == 0)
2107 gui_mch_flush();
2108}
2109
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002111gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
2116 if (len == 0)
2117 return;
2118
2119 if (len < 0)
2120 len = (int)STRLEN(s);
2121
2122 while (len > 0)
2123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if (has_mbyte)
2125 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002126 // Find out how many chars fit in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 cells = 0;
2128 for (this_len = 0; this_len < len; )
2129 {
2130 cells += (*mb_ptr2cells)(s + this_len);
2131 if (gui.col + cells > Columns)
2132 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002133 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135 if (this_len > len)
Bram Moolenaar30613902019-12-01 22:11:18 +01002136 this_len = len; // don't include following composing char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
Bram Moolenaarb26592a2022-07-12 17:34:31 +01002138 else if (gui.col + len > Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 this_len = Columns - gui.col;
2140 else
2141 this_len = len;
2142
2143 (void)gui_outstr_nowrap(s, this_len,
2144 0, (guicolor_T)0, (guicolor_T)0, 0);
2145 s += this_len;
2146 len -= this_len;
Bram Moolenaar30613902019-12-01 22:11:18 +01002147 // fill up for a double-width char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 if (len > 0 && gui.col < Columns)
2149 (void)gui_outstr_nowrap((char_u *)" ", 1,
2150 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01002151 // The cursor may wrap to the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (gui.col >= Columns)
2153 {
2154 gui.col = 0;
2155 gui.row++;
2156 }
2157 }
2158}
2159
2160/*
2161 * Output one character (may be one or two display cells).
2162 * Caller must check for valid "off".
2163 * Returns FAIL or OK, just like gui_outstr_nowrap().
2164 */
2165 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002166gui_screenchar(
Bram Moolenaar30613902019-12-01 22:11:18 +01002167 int off, // Offset from start of screen
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002168 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002169 guicolor_T fg, // colors for cursor
2170 guicolor_T bg, // colors for cursor
2171 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 char_u buf[MB_MAXBYTES + 1];
2174
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002175 // Don't draw right half of a double-width UTF-8 char. "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (enc_utf8 && ScreenLines[off] == 0)
2177 return OK;
2178
2179 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002180 // Draw UTF-8 multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2182 flags, fg, bg, back);
2183
2184 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2185 {
2186 buf[0] = ScreenLines[off];
2187 buf[1] = ScreenLines2[off];
2188 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2189 }
2190
Bram Moolenaar30613902019-12-01 22:11:18 +01002191 // Draw non-multi-byte character or DBCS character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002193 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195}
2196
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002197#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198/*
2199 * Output the string at the given screen position. This is used in place
2200 * of gui_screenchar() where possible because Pango needs as much context
2201 * as possible to work nicely. It's a lot faster as well.
2202 */
2203 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002204gui_screenstr(
Bram Moolenaar30613902019-12-01 22:11:18 +01002205 int off, // Offset from start of screen
2206 int len, // string length in screen cells
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002207 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002208 guicolor_T fg, // colors for cursor
2209 guicolor_T bg, // colors for cursor
2210 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211{
2212 char_u *buf;
2213 int outlen = 0;
2214 int i;
2215 int retval;
2216
Bram Moolenaar30613902019-12-01 22:11:18 +01002217 if (len <= 0) // "cannot happen"?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 return OK;
2219
2220 if (enc_utf8)
2221 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002222 buf = alloc(len * MB_MAXBYTES + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002224 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 for (i = off; i < off + len; ++i)
2227 {
2228 if (ScreenLines[i] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002229 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230
2231 if (ScreenLinesUC[i] == 0)
2232 buf[outlen++] = ScreenLines[i];
2233 else
2234 outlen += utfc_char2bytes(i, buf + outlen);
2235 }
2236
Bram Moolenaar30613902019-12-01 22:11:18 +01002237 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2239 vim_free(buf);
2240
2241 return retval;
2242 }
2243 else if (enc_dbcs == DBCS_JPNU)
2244 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002245 buf = alloc(len * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002247 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249 for (i = off; i < off + len; ++i)
2250 {
2251 buf[outlen++] = ScreenLines[i];
2252
Bram Moolenaar30613902019-12-01 22:11:18 +01002253 // handle double-byte single-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 if (ScreenLines[i] == 0x8e)
2255 buf[outlen++] = ScreenLines2[i];
2256 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2257 buf[outlen++] = ScreenLines[++i];
2258 }
2259
Bram Moolenaar30613902019-12-01 22:11:18 +01002260 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2262 vim_free(buf);
2263
2264 return retval;
2265 }
2266 else
2267 {
2268 return gui_outstr_nowrap(&ScreenLines[off], len,
2269 flags, fg, bg, back);
2270 }
2271}
Bram Moolenaar30613902019-12-01 22:11:18 +01002272#endif // FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
2274/*
2275 * Output the given string at the current cursor position. If the string is
2276 * too long to fit on the line, then it is truncated.
2277 * "flags":
2278 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2279 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002280 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 * background.
2282 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2283 * it.
2284 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2285 * FAIL (the caller should start drawing "back" chars back).
2286 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002287 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002288gui_outstr_nowrap(
2289 char_u *s,
2290 int len,
2291 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002292 guicolor_T fg, // colors for cursor
2293 guicolor_T bg, // colors for cursor
2294 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
2296 long_u highlight_mask;
2297 long_u hl_mask_todo;
2298 guicolor_T fg_color;
2299 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002300 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002301#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002303 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304# ifdef FEAT_XFONTSET
2305 GuiFontset fontset = NOFONTSET;
2306# endif
2307#endif
2308 attrentry_T *aep = NULL;
2309 int draw_flags;
2310 int col = gui.col;
2311#ifdef FEAT_SIGN_ICONS
2312 int draw_sign = FALSE;
Bram Moolenaarc7283072019-07-16 20:12:44 +02002313 int signcol = 0;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002314 char_u extra[18];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315# ifdef FEAT_NETBEANS_INTG
2316 int multi_sign = FALSE;
2317# endif
2318#endif
2319
2320 if (len < 0)
2321 len = (int)STRLEN(s);
2322 if (len == 0)
2323 return OK;
2324
2325#ifdef FEAT_SIGN_ICONS
2326 if (*s == SIGN_BYTE
2327# ifdef FEAT_NETBEANS_INTG
2328 || *s == MULTISIGN_BYTE
2329# endif
Bram Moolenaar0231f832019-07-12 19:22:22 +02002330 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
2332# ifdef FEAT_NETBEANS_INTG
2333 if (*s == MULTISIGN_BYTE)
2334 multi_sign = TRUE;
2335# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002336 // draw spaces instead
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002337 if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
2338 (curwin->w_p_nu || curwin->w_p_rnu))
2339 {
2340 sprintf((char *)extra, "%*c ", number_width(curwin), ' ');
2341 s = extra;
2342 }
2343 else
2344 s = (char_u *)" ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (len == 1 && col > 0)
2346 --col;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002347 len = (int)STRLEN(s);
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002348 if (len > 2)
Bram Moolenaar0231f832019-07-12 19:22:22 +02002349 // right align sign icon in the number column
2350 signcol = col + len - 3;
2351 else
2352 signcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 draw_sign = TRUE;
2354 highlight_mask = 0;
2355 }
2356 else
2357#endif
2358 if (gui.highlight_mask > HL_ALL)
2359 {
2360 aep = syn_gui_attr2entry(gui.highlight_mask);
Bram Moolenaar30613902019-12-01 22:11:18 +01002361 if (aep == NULL) // highlighting not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 highlight_mask = 0;
2363 else
2364 highlight_mask = aep->ae_attr;
2365 }
2366 else
2367 highlight_mask = gui.highlight_mask;
2368 hl_mask_todo = highlight_mask;
2369
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002370#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002371 // Set the font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2373 font = aep->ae_u.gui.font;
2374# ifdef FEAT_XFONTSET
2375 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2376 fontset = aep->ae_u.gui.fontset;
2377# endif
2378 else
2379 {
2380# ifdef FEAT_XFONTSET
2381 if (gui.fontset != NOFONTSET)
2382 fontset = gui.fontset;
2383 else
2384# endif
2385 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2386 {
2387 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2388 {
2389 font = gui.boldital_font;
2390 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2391 }
2392 else if (gui.bold_font != NOFONT)
2393 {
2394 font = gui.bold_font;
2395 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2396 }
2397 else
2398 font = gui.norm_font;
2399 }
2400 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2401 {
2402 font = gui.ital_font;
2403 hl_mask_todo &= ~HL_ITALIC;
2404 }
2405 else
2406 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002407
Bram Moolenaardb250522013-06-17 22:43:25 +02002408 /*
2409 * Choose correct wide_font by font. wide_font should be set with font
2410 * at same time in above block. But it will make many "ifdef" nasty
2411 * blocks. So we do it here.
2412 */
2413 if (font == gui.boldital_font && gui.wide_boldital_font)
2414 wide_font = gui.wide_boldital_font;
2415 else if (font == gui.bold_font && gui.wide_bold_font)
2416 wide_font = gui.wide_bold_font;
2417 else if (font == gui.ital_font && gui.wide_ital_font)
2418 wide_font = gui.wide_ital_font;
2419 else if (font == gui.norm_font && gui.wide_font)
2420 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422# ifdef FEAT_XFONTSET
2423 if (fontset != NOFONTSET)
2424 gui_mch_set_fontset(fontset);
2425 else
2426# endif
2427 gui_mch_set_font(font);
2428#endif
2429
2430 draw_flags = 0;
2431
Bram Moolenaar30613902019-12-01 22:11:18 +01002432 // Set the color
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 bg_color = gui.back_pixel;
2434 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2435 {
2436 draw_flags |= DRAW_CURSOR;
2437 fg_color = fg;
2438 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002439 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 }
2441 else if (aep != NULL)
2442 {
2443 fg_color = aep->ae_u.gui.fg_color;
2444 if (fg_color == INVALCOLOR)
2445 fg_color = gui.norm_pixel;
2446 bg_color = aep->ae_u.gui.bg_color;
2447 if (bg_color == INVALCOLOR)
2448 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002449 sp_color = aep->ae_u.gui.sp_color;
2450 if (sp_color == INVALCOLOR)
2451 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002456 sp_color = fg_color;
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 gui_mch_set_fg_color(bg_color);
2462 gui_mch_set_bg_color(fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464 else
2465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 gui_mch_set_fg_color(fg_color);
2467 gui_mch_set_bg_color(bg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002469 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
Bram Moolenaar30613902019-12-01 22:11:18 +01002471 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (!(flags & GUI_MON_NOCLEAR))
2473 clip_may_clear_selection(gui.row, gui.row);
2474
2475
Bram Moolenaar30613902019-12-01 22:11:18 +01002476 // If there's no bold font, then fake it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2478 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 /*
2481 * When drawing bold or italic characters the spill-over from the left
2482 * neighbor may be destroyed. Let the caller backup to start redrawing
2483 * just after a blank.
2484 */
2485 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2486 return FAIL;
2487
Bram Moolenaare60acc12011-05-10 16:41:25 +02002488#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002489 // If there's no italic font, then fake it.
2490 // For GTK2, we don't need a different font for italic style.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (hl_mask_todo & HL_ITALIC)
2492 draw_flags |= DRAW_ITALIC;
2493
Bram Moolenaar30613902019-12-01 22:11:18 +01002494 // Do we underline the text?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 if (hl_mask_todo & HL_UNDERLINE)
2496 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498#else
Bram Moolenaar30613902019-12-01 22:11:18 +01002499 // Do we underline the text?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002500 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 draw_flags |= DRAW_UNDERL;
2502#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002503 // Do we undercurl the text?
Bram Moolenaar3918c952005-03-15 22:34:55 +00002504 if (hl_mask_todo & HL_UNDERCURL)
2505 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506
Bram Moolenaar84f54632022-06-29 18:39:11 +01002507 // TODO: HL_UNDERDOUBLE, HL_UNDERDOTTED, HL_UNDERDASHED
2508
Bram Moolenaar30613902019-12-01 22:11:18 +01002509 // Do we strikethrough the text?
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002510 if (hl_mask_todo & HL_STRIKETHROUGH)
2511 draw_flags |= DRAW_STRIKE;
2512
Bram Moolenaar30613902019-12-01 22:11:18 +01002513 // Do we draw transparently?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (flags & GUI_MON_TRS_CURSOR)
2515 draw_flags |= DRAW_TRANSP;
2516
2517 /*
2518 * Draw the text.
2519 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002520#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01002521 // The value returned is the length in display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2523#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 if (enc_utf8)
2525 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002526 int start; // index of bytes to be drawn
2527 int cells; // cellwidth of bytes to be drawn
2528 int thislen; // length of bytes to be drawn
2529 int cn; // cellwidth of current char
2530 int i; // index of current char
2531 int c; // current char value
2532 int cl; // byte length of current char
2533 int comping; // current char is composing
2534 int scol = col; // screen column
2535 int curr_wide = FALSE; // use 'guifontwide'
Bram Moolenaar45933962013-01-23 17:43:57 +01002536 int prev_wide = FALSE;
2537 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002538# ifdef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01002539 int sep_comp = FALSE; // Don't separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002540# else
Bram Moolenaar30613902019-12-01 22:11:18 +01002541 int sep_comp = TRUE; // Separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
Bram Moolenaar30613902019-12-01 22:11:18 +01002544 // Break the string at a composing character, it has to be drawn on
2545 // top of the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 start = 0;
2547 cells = 0;
2548 for (i = 0; i < len; i += cl)
2549 {
2550 c = utf_ptr2char(s + i);
2551 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 comping = utf_iscomposing(c);
Bram Moolenaar30613902019-12-01 22:11:18 +01002553 if (!comping) // count cells from non-composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002555 if (!comping || sep_comp)
2556 {
2557 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002558# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002559 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002560# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002561 && wide_font != NOFONT)
2562 curr_wide = TRUE;
2563 else
2564 curr_wide = FALSE;
2565 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002566 cl = utf_ptr2len(s + i);
Bram Moolenaar30613902019-12-01 22:11:18 +01002567 if (cl == 0) // hit end of string
2568 len = i + cl; // len must be wrong "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
Bram Moolenaar45933962013-01-23 17:43:57 +01002570 wide_changed = curr_wide != prev_wide;
2571
Bram Moolenaar30613902019-12-01 22:11:18 +01002572 // Print the string so far if it's the last character or there is
2573 // a composing character.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002574 if (i + cl >= len || (comping && sep_comp && i > start)
2575 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002576# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002578# ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +01002579 // No fontset: At least draw char after wide char at
2580 // right position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002583 )
2584# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 )
2586 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002587 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 thislen = i - start;
2589 else
2590 thislen = i - start + cl;
2591 if (thislen > 0)
2592 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002593 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002594 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2596 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002597 if (prev_wide)
2598 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 start += thislen;
2600 }
2601 scol += cells;
2602 cells = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01002603 // Adjust to not draw a character which width is changed
2604 // against with last one.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002605 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002607 scol -= cn;
2608 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
2610
Bram Moolenaar13505972019-01-24 15:04:48 +01002611# if defined(FEAT_GUI_X11)
Bram Moolenaar30613902019-12-01 22:11:18 +01002612 // No fontset: draw a space to fill the gap after a wide char
2613 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002615# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002617# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002618 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2620 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002623 // Draw a composing char on top of the previous char.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002624 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002626# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaar30613902019-12-01 22:11:18 +01002627 // Carbon ATSUI autodraws composing char over previous char
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002628 gui_mch_draw_string(gui.row, scol, s + i, cl,
2629 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002630# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002631 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2632 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 start = i + cl;
2635 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002636 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002638 // The stuff below assumes "len" is the length in screen columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 len = scol - col;
2640 }
2641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
2643 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (enc_dbcs == DBCS_JPNU)
2645 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002646 // Get the length in display cells, this can be different from the
2647 // number of bytes for "euc-jp".
Bram Moolenaar72597a52010-07-18 15:31:08 +02002648 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002651#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2654 gui.col = col + len;
2655
Bram Moolenaar30613902019-12-01 22:11:18 +01002656 // May need to invert it when it's part of the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (flags & GUI_MON_NOCLEAR)
2658 clip_may_redraw_selection(gui.row, col, len);
2659
2660 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2661 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002662 // Invalidate the old physical cursor position if we wrote over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (gui.cursor_row == gui.row
2664 && gui.cursor_col >= col
2665 && gui.cursor_col < col + len)
2666 gui.cursor_is_valid = FALSE;
2667 }
2668
2669#ifdef FEAT_SIGN_ICONS
2670 if (draw_sign)
Bram Moolenaar30613902019-12-01 22:11:18 +01002671 // Draw the sign on top of the spaces.
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002672 gui_mch_drawsign(gui.row, signcol, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002673# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002674 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 if (multi_sign)
2676 netbeans_draw_multisign_indicator(gui.row);
2677# endif
2678#endif
2679
2680 return OK;
2681}
2682
2683/*
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002684 * Undraw the cursor. This actually redraws the character at the cursor
2685 * position, plus some more characters when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 */
2687 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002688gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002690 if (!gui.cursor_is_valid)
2691 return;
2692
2693 // Always redraw the character just before if there is one, because
2694 // with some fonts and characters there can be a one pixel overlap.
2695 int startcol = gui.cursor_col > 0 ? gui.cursor_col - 1 : gui.cursor_col;
2696 int endcol = gui.cursor_col;
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002697
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002698#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002699 gui_adjust_undraw_cursor_for_ligatures(&startcol, &endcol);
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002700#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002701 gui_redraw_block(gui.cursor_row, startcol,
2702 gui.cursor_row, endcol, GUI_MON_NOCLEAR);
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002703
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002704 // Cursor_is_valid is reset when the cursor is undrawn, also reset it
2705 // here in case it wasn't needed to undraw it.
2706 gui.cursor_is_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707}
2708
2709 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002710gui_redraw(
2711 int x,
2712 int y,
2713 int w,
2714 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715{
2716 int row1, col1, row2, col2;
2717
2718 row1 = Y_2_ROW(y);
2719 col1 = X_2_COL(x);
2720 row2 = Y_2_ROW(y + h - 1);
2721 col2 = X_2_COL(x + w - 1);
2722
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002723 gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 /*
2726 * We may need to redraw the cursor, but don't take it upon us to change
2727 * its location after a scroll.
2728 * (maybe be more strict even and test col too?)
2729 * These things may be outside the update/clipping region and reality may
2730 * not reflect Vims internal ideas if these operations are clipped away.
2731 */
2732 if (gui.row == gui.cursor_row)
2733 gui_update_cursor(TRUE, TRUE);
2734}
2735
2736/*
2737 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2738 * from col1 to col2 (inclusive).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 */
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002740 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002741gui_redraw_block(
2742 int row1,
2743 int col1,
2744 int row2,
2745 int col2,
Bram Moolenaar30613902019-12-01 22:11:18 +01002746 int flags) // flags for gui_outstr_nowrap()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
2748 int old_row, old_col;
2749 long_u old_hl_mask;
2750 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002751 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 int idx, len;
2753 int back, nback;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
Bram Moolenaar30613902019-12-01 22:11:18 +01002756 // Don't try to update when ScreenLines is not valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if (!screen_cleared || ScreenLines == NULL)
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002758 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
Bram Moolenaar30613902019-12-01 22:11:18 +01002760 // Don't try to draw outside the shell!
2761 // Check everything, strange values may be caused by a big border width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 col1 = check_col(col1);
2763 col2 = check_col(col2);
2764 row1 = check_row(row1);
2765 row2 = check_row(row2);
2766
Bram Moolenaar30613902019-12-01 22:11:18 +01002767 // Remember where our cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 old_row = gui.row;
2769 old_col = gui.col;
2770 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 orig_col1 = col1;
2772 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
2774 for (gui.row = row1; gui.row <= row2; gui.row++)
2775 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002776 // When only half of a double-wide character is in the block, include
2777 // the other half.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 col1 = orig_col1;
2779 col2 = orig_col2;
2780 off = LineOffset[gui.row];
2781 if (enc_dbcs != 0)
2782 {
2783 if (col1 > 0)
2784 col1 -= dbcs_screen_head_off(ScreenLines + off,
2785 ScreenLines + off + col1);
2786 col2 += dbcs_screen_tail_off(ScreenLines + off,
2787 ScreenLines + off + col2);
2788 }
2789 else if (enc_utf8)
2790 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002791 if (ScreenLines[off + col1] == 0)
2792 {
2793 if (col1 > 0)
2794 --col1;
2795 else
2796 // FIXME: how can the first character ever be zero?
Bram Moolenaar097c5372023-05-24 21:02:24 +01002797 siemsg("NUL in ScreenLines in row %ld", (long)gui.row);
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002798 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002799#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2801 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 gui.col = col1;
2805 off = LineOffset[gui.row] + gui.col;
2806 len = col2 - col1 + 1;
2807
Bram Moolenaar30613902019-12-01 22:11:18 +01002808 // Find how many chars back this highlighting starts, or where a space
2809 // is. Needed for when the bold trick is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 for (back = 0; back < col1; ++back)
2811 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2812 || ScreenLines[off - 1 - back] == ' ')
2813 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
Bram Moolenaar30613902019-12-01 22:11:18 +01002815 // Break it up in strings of characters with the same attributes.
2816 // Print UTF-8 characters individually.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 while (len > 0)
2818 {
2819 first_attr = ScreenAttrs[off];
2820 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002821#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 if (enc_utf8 && ScreenLinesUC[off] != 0)
2823 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002824 // output multi-byte character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 nback = gui_screenchar(off, flags,
2826 (guicolor_T)0, (guicolor_T)0, back);
2827 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2828 idx = 2;
2829 else
2830 idx = 1;
2831 }
2832 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2833 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002834 // output double-byte, single-width character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 nback = gui_screenchar(off, flags,
2836 (guicolor_T)0, (guicolor_T)0, back);
2837 idx = 1;
2838 }
2839 else
2840#endif
2841 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002842#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 for (idx = 0; idx < len; ++idx)
2844 {
2845 if (enc_utf8 && ScreenLines[off + idx] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002846 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 if (ScreenAttrs[off + idx] != first_attr)
2848 break;
2849 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002850 // gui_screenstr() takes care of multibyte chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 nback = gui_screenstr(off, idx, flags,
2852 (guicolor_T)0, (guicolor_T)0, back);
2853#else
2854 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2855 idx++)
2856 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002857 // Stop at a multi-byte Unicode character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2859 break;
2860 if (enc_dbcs == DBCS_JPNU)
2861 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002862 // Stop at a double-byte single-width char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 if (ScreenLines[off + idx] == 0x8e)
2864 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002865 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 + off + idx) == 2)
Bram Moolenaar30613902019-12-01 22:11:18 +01002867 ++idx; // skip second byte of double-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
2870 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2871 (guicolor_T)0, (guicolor_T)0, back);
2872#endif
2873 }
2874 if (nback == FAIL)
2875 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002876 // Must back up to start drawing where a bold or italic word
2877 // starts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 off -= back;
2879 len += back;
2880 gui.col -= back;
2881 }
2882 else
2883 {
2884 off += idx;
2885 len -= idx;
2886 }
2887 back = 0;
2888 }
2889 }
2890
Bram Moolenaar30613902019-12-01 22:11:18 +01002891 // Put the cursor back where it was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 gui.row = old_row;
2893 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002894 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895}
2896
2897 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002898gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
2900 if (count <= 0)
2901 return;
2902
2903 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002904 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 gui_clear_block(row, gui.scroll_region_left,
2906 gui.scroll_region_bot, gui.scroll_region_right);
2907 else
2908 {
2909 gui_mch_delete_lines(row, count);
2910
Bram Moolenaar30613902019-12-01 22:11:18 +01002911 // If the cursor was in the deleted lines it's now gone. If the
2912 // cursor was in the scrolled lines adjust its position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 if (gui.cursor_row >= row
2914 && gui.cursor_col >= gui.scroll_region_left
2915 && gui.cursor_col <= gui.scroll_region_right)
2916 {
2917 if (gui.cursor_row < row + count)
2918 gui.cursor_is_valid = FALSE;
2919 else if (gui.cursor_row <= gui.scroll_region_bot)
2920 gui.cursor_row -= count;
2921 }
2922 }
2923}
2924
2925 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002926gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
2928 if (count <= 0)
2929 return;
2930
2931 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002932 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 gui_clear_block(row, gui.scroll_region_left,
2934 gui.scroll_region_bot, gui.scroll_region_right);
2935 else
2936 {
2937 gui_mch_insert_lines(row, count);
2938
2939 if (gui.cursor_row >= gui.row
2940 && gui.cursor_col >= gui.scroll_region_left
2941 && gui.cursor_col <= gui.scroll_region_right)
2942 {
2943 if (gui.cursor_row <= gui.scroll_region_bot - count)
2944 gui.cursor_row += count;
2945 else if (gui.cursor_row <= gui.scroll_region_bot)
2946 gui.cursor_is_valid = FALSE;
2947 }
2948 }
2949}
2950
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002951#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002952/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002953 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2954 */
2955 static int
2956gui_wait_for_chars_3(
2957 long wtime,
2958 int *interrupted UNUSED,
2959 int ignore_input UNUSED)
2960{
2961 return gui_mch_wait_for_chars(wtime);
2962}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002963#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002964
2965/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002966 * Returns OK if a character was found to be available within the given time,
2967 * or FAIL otherwise.
2968 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002969 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002970gui_wait_for_chars_or_timer(
2971 long wtime,
2972 int *interrupted UNUSED,
2973 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002974{
2975#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002976 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2977 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002978#else
2979 return gui_mch_wait_for_chars(wtime);
2980#endif
2981}
2982
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983/*
2984 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002985 * "wtime" == -1 Wait forever.
2986 * "wtime" == 0 Don't wait.
2987 * "wtime" > 0 Wait wtime milliseconds for a character.
2988 *
2989 * Returns the number of characters read or zero when timed out or interrupted.
2990 * "buf" may be NULL, in which case a non-zero number is returned if characters
2991 * are available.
2992 */
2993 static int
2994gui_wait_for_chars_buf(
2995 char_u *buf,
2996 int maxlen,
2997 long wtime, // don't use "time", MIPS cannot handle it
2998 int tb_change_cnt)
2999{
3000 int retval;
3001
3002#ifdef FEAT_MENU
3003 // If we're going to wait a bit, update the menus and mouse shape for the
3004 // current State.
3005 if (wtime != 0)
3006 gui_update_menus(0);
3007#endif
3008
3009 gui_mch_update();
3010 if (input_available()) // Got char, return immediately
3011 {
3012 if (buf != NULL && !typebuf_changed(tb_change_cnt))
3013 return read_from_input_buf(buf, (long)maxlen);
3014 return 0;
3015 }
3016 if (wtime == 0) // Don't wait for char
3017 return FAIL;
3018
3019 // Before waiting, flush any output to the screen.
3020 gui_mch_flush();
3021
3022 // Blink while waiting for a character.
3023 gui_mch_start_blink();
3024
3025 // Common function to loop until "wtime" is met, while handling timers and
3026 // other callbacks.
3027 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
3028 gui_wait_for_chars_or_timer, NULL);
3029
3030 gui_mch_stop_blink(TRUE);
3031
3032 return retval;
3033}
3034
3035/*
3036 * Wait for a character from the keyboard without actually reading it.
3037 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 * wtime == -1 Wait forever.
3039 * wtime == 0 Don't wait.
3040 * wtime > 0 Wait wtime milliseconds for a character.
3041 * Returns OK if a character was found to be available within the given time,
3042 * or FAIL otherwise.
3043 */
3044 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003045gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003047 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048}
3049
3050/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003051 * Equivalent of mch_inchar() for the GUI.
3052 */
3053 int
3054gui_inchar(
3055 char_u *buf,
3056 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003057 long wtime, // milliseconds
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003058 int tb_change_cnt)
3059{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003060 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003061}
3062
3063/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003064 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 */
3066 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003067fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 p[0] = (char_u)(col / 128 + ' ' + 1);
3070 p[1] = (char_u)(col % 128 + ' ' + 1);
3071 p[2] = (char_u)(row / 128 + ' ' + 1);
3072 p[3] = (char_u)(row % 128 + ' ' + 1);
3073}
3074
3075/*
3076 * Generic mouse support function. Add a mouse event to the input buffer with
3077 * the given properties.
3078 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3079 * MOUSE_X1, MOUSE_X2
3080 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003081 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3082 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 * x, y --- Coordinates of mouse in pixels.
3084 * repeated_click --- TRUE if this click comes only a short time after a
3085 * previous click.
3086 * modifiers --- Bit field which may be any of the following modifiers
3087 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3088 * This function will ignore drag events where the mouse has not moved to a new
3089 * character.
3090 */
3091 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003092gui_send_mouse_event(
3093 int button,
3094 int x,
3095 int y,
3096 int repeated_click,
3097 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
3099 static int prev_row = 0, prev_col = 0;
3100 static int prev_button = -1;
3101 static int num_clicks = 1;
3102 char_u string[10];
3103 enum key_extra button_char;
3104 int row, col;
3105#ifdef FEAT_CLIPBOARD
3106 int checkfor;
3107 int did_clip = FALSE;
3108#endif
3109
3110 /*
3111 * Scrolling may happen at any time, also while a selection is present.
3112 */
3113 switch (button)
3114 {
Bram Moolenaar445f11d2021-06-08 20:13:31 +02003115 case MOUSE_MOVE:
3116 button_char = KE_MOUSEMOVE_XY;
3117 goto button_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 case MOUSE_X1:
3119 button_char = KE_X1MOUSE;
3120 goto button_set;
3121 case MOUSE_X2:
3122 button_char = KE_X2MOUSE;
3123 goto button_set;
3124 case MOUSE_4:
3125 button_char = KE_MOUSEDOWN;
3126 goto button_set;
3127 case MOUSE_5:
3128 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003129 goto button_set;
3130 case MOUSE_6:
3131 button_char = KE_MOUSELEFT;
3132 goto button_set;
3133 case MOUSE_7:
3134 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135button_set:
3136 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003137 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 if (hold_gui_events)
3139 return;
3140
Ernie Raelc4cb5442022-04-03 15:47:28 +01003141 row = gui_xy2colrow(x, y, &col);
3142 // Don't report a mouse move unless moved to a
3143 // different character position.
3144 if (button == MOUSE_MOVE)
3145 {
3146 if (row == prev_row && col == prev_col)
3147 return;
3148 else
3149 {
3150 prev_row = row >= 0 ? row : 0;
3151 prev_col = col;
3152 }
3153 }
3154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 string[3] = CSI;
3156 string[4] = KS_EXTRA;
3157 string[5] = (int)button_char;
3158
Bram Moolenaar30613902019-12-01 22:11:18 +01003159 // Pass the pointer coordinates of the scroll event so that we
3160 // know which window to scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 string[6] = (char_u)(col / 128 + ' ' + 1);
3162 string[7] = (char_u)(col % 128 + ' ' + 1);
3163 string[8] = (char_u)(row / 128 + ' ' + 1);
3164 string[9] = (char_u)(row % 128 + ' ' + 1);
3165
3166 if (modifiers == 0)
3167 add_to_input_buf(string + 3, 7);
3168 else
3169 {
3170 string[0] = CSI;
3171 string[1] = KS_MODIFIER;
3172 string[2] = 0;
3173 if (modifiers & MOUSE_SHIFT)
3174 string[2] |= MOD_MASK_SHIFT;
3175 if (modifiers & MOUSE_CTRL)
3176 string[2] |= MOD_MASK_CTRL;
3177 if (modifiers & MOUSE_ALT)
3178 string[2] |= MOD_MASK_ALT;
3179 add_to_input_buf(string, 10);
3180 }
3181 return;
3182 }
3183 }
3184
3185#ifdef FEAT_CLIPBOARD
Bram Moolenaar30613902019-12-01 22:11:18 +01003186 // If a clipboard selection is in progress, handle it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 if (clip_star.state == SELECT_IN_PROGRESS)
3188 {
3189 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
Bram Moolenaar0ce37332019-12-18 21:33:22 +01003190
3191 // A release event may still need to be sent if the position is equal.
3192 row = gui_xy2colrow(x, y, &col);
3193 if (button != MOUSE_RELEASE || row != prev_row || col != prev_col)
3194 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196
Bram Moolenaar30613902019-12-01 22:11:18 +01003197 // Determine which mouse settings to look for based on the current mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 switch (get_real_state())
3199 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003200 case MODE_NORMAL_BUSY:
3201 case MODE_OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003202# ifdef FEAT_TERMINAL
Bram Moolenaar24959102022-05-07 20:01:16 +01003203 case MODE_TERMINAL:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003204# endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003205 case MODE_NORMAL: checkfor = MOUSE_NORMAL; break;
3206 case MODE_VISUAL: checkfor = MOUSE_VISUAL; break;
3207 case MODE_SELECT: checkfor = MOUSE_VISUAL; break;
3208 case MODE_REPLACE:
3209 case MODE_REPLACE | MODE_LANGMAP:
3210 case MODE_VREPLACE:
3211 case MODE_VREPLACE | MODE_LANGMAP:
3212 case MODE_INSERT:
3213 case MODE_INSERT | MODE_LANGMAP:
3214 checkfor = MOUSE_INSERT; break;
3215 case MODE_ASKMORE:
3216 case MODE_HITRETURN: // At the more- and hit-enter prompt pass the
Bram Moolenaar30613902019-12-01 22:11:18 +01003217 // mouse event for a click on or below the
3218 // message line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 if (Y_2_ROW(y) >= msg_row)
3220 checkfor = MOUSE_NORMAL;
3221 else
3222 checkfor = MOUSE_RETURN;
3223 break;
3224
3225 /*
3226 * On the command line, use the clipboard selection on all lines
3227 * but the command line. But not when pasting.
3228 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003229 case MODE_CMDLINE:
3230 case MODE_CMDLINE | MODE_LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3232 checkfor = MOUSE_NONE;
3233 else
3234 checkfor = MOUSE_COMMAND;
3235 break;
3236
3237 default:
3238 checkfor = MOUSE_NONE;
3239 break;
3240 };
3241
3242 /*
3243 * Allow clipboard selection of text on the command line in "normal"
3244 * modes. Don't do this when dragging the status line, or extending a
3245 * Visual selection.
3246 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003247 if ((State == MODE_NORMAL || State == MODE_NORMAL_BUSY
3248 || (State & MODE_INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003249 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 && button != MOUSE_DRAG
3251# ifdef FEAT_MOUSESHAPE
3252 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254# endif
3255 )
3256 checkfor = MOUSE_NONE;
3257
3258 /*
3259 * Use modeless selection when holding CTRL and SHIFT pressed.
3260 */
3261 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3262 checkfor = MOUSE_NONEF;
3263
3264 /*
3265 * In Ex mode, always use modeless selection.
3266 */
3267 if (exmode_active)
3268 checkfor = MOUSE_NONE;
3269
3270 /*
3271 * If the mouse settings say to not use the mouse, use the modeless
3272 * selection. But if Visual is active, assume that only the Visual area
3273 * will be selected.
3274 * Exception: On the command line, both the selection is used and a mouse
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003275 * key is sent.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 */
3277 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3278 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003279 // Don't do modeless selection in Visual mode.
Bram Moolenaar24959102022-05-07 20:01:16 +01003280 if (checkfor != MOUSE_NONEF && VIsual_active && (State & MODE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
3283 /*
3284 * When 'mousemodel' is "popup", shift-left is translated to right.
3285 * But not when also using Ctrl.
3286 */
3287 if (mouse_model_popup() && button == MOUSE_LEFT
3288 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3289 {
3290 button = MOUSE_RIGHT;
3291 modifiers &= ~ MOUSE_SHIFT;
3292 }
3293
Bram Moolenaar30613902019-12-01 22:11:18 +01003294 // If the selection is done, allow the right button to extend it.
3295 // If the selection is cleared, allow the right button to start it
3296 // from the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (button == MOUSE_RIGHT)
3298 {
3299 if (clip_star.state == SELECT_CLEARED)
3300 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003301 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
3303 col = msg_col;
3304 row = msg_row;
3305 }
3306 else
3307 {
3308 col = curwin->w_wcol;
3309 row = curwin->w_wrow + W_WINROW(curwin);
3310 }
3311 clip_start_selection(col, row, FALSE);
3312 }
3313 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3314 repeated_click);
3315 did_clip = TRUE;
3316 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003317 // Allow the left button to start the selection
Bram Moolenaare60acc12011-05-10 16:41:25 +02003318 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
3320 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3321 did_clip = TRUE;
3322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
Bram Moolenaar30613902019-12-01 22:11:18 +01003324 // Always allow pasting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 if (button != MOUSE_MIDDLE)
3326 {
3327 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3328 return;
3329 if (checkfor != MOUSE_COMMAND)
3330 button = MOUSE_LEFT;
3331 }
3332 repeated_click = FALSE;
3333 }
3334
3335 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003336 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337#endif
3338
Bram Moolenaar30613902019-12-01 22:11:18 +01003339 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 if (hold_gui_events)
3341 return;
3342
3343 row = gui_xy2colrow(x, y, &col);
3344
3345 /*
3346 * If we are dragging and the mouse hasn't moved far enough to be on a
3347 * different character, then don't send an event to vim.
3348 */
3349 if (button == MOUSE_DRAG)
3350 {
3351 if (row == prev_row && col == prev_col)
3352 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01003353 // Dragging above the window, set "row" to -1 to cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 if (y < 0)
3355 row = -1;
3356 }
3357
3358 /*
3359 * If topline has changed (window scrolled) since the last click, reset
3360 * repeated_click, because we don't want starting Visual mode when
3361 * clicking on a different character in the text.
3362 */
3363 if (curwin->w_topline != gui_prev_topline
3364#ifdef FEAT_DIFF
3365 || curwin->w_topfill != gui_prev_topfill
3366#endif
3367 )
3368 repeated_click = FALSE;
3369
Bram Moolenaar30613902019-12-01 22:11:18 +01003370 string[0] = CSI; // this sequence is recognized by check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 string[1] = KS_MOUSE;
3372 string[2] = KE_FILLER;
3373 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3374 {
3375 if (repeated_click)
3376 {
3377 /*
3378 * Handle multiple clicks. They only count if the mouse is still
3379 * pointing at the same character.
3380 */
3381 if (button != prev_button || row != prev_row || col != prev_col)
3382 num_clicks = 1;
3383 else if (++num_clicks > 4)
3384 num_clicks = 1;
3385 }
3386 else
3387 num_clicks = 1;
3388 prev_button = button;
3389 gui_prev_topline = curwin->w_topline;
3390#ifdef FEAT_DIFF
3391 gui_prev_topfill = curwin->w_topfill;
3392#endif
3393
3394 string[3] = (char_u)(button | 0x20);
3395 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3396 }
3397 else
3398 string[3] = (char_u)button;
3399
3400 string[3] |= modifiers;
3401 fill_mouse_coord(string + 4, col, row);
3402 add_to_input_buf(string, 8);
3403
3404 if (row < 0)
3405 prev_row = 0;
3406 else
3407 prev_row = row;
3408 prev_col = col;
3409
3410 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01003411 * We need to make sure this is cleared since GTK doesn't tell us when
3412 * the user is done dragging.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 */
Bram Moolenaar0b962e52022-04-03 18:02:37 +01003414#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 gui.dragged_sb = SBAR_NONE;
3416#endif
3417}
3418
3419/*
3420 * Convert x and y coordinate to column and row in text window.
3421 * Corrects for multi-byte character.
3422 * returns column in "*colp" and row as return value;
3423 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003424 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003425gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
3427 int col = check_col(X_2_COL(x));
3428 int row = check_row(Y_2_ROW(y));
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 return row;
3432}
3433
3434#if defined(FEAT_MENU) || defined(PROTO)
3435/*
3436 * Callback function for when a menu entry has been selected.
3437 */
3438 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003439gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003441 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar30613902019-12-01 22:11:18 +01003443 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 if (hold_gui_events)
3445 return;
3446
3447 bytes[0] = CSI;
3448 bytes[1] = KS_MENU;
3449 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003450 add_to_input_buf(bytes, 3);
3451 add_long_to_buf((long_u)menu, bytes);
3452 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453}
3454#endif
3455
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003456static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003457
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458/*
3459 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003460 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 * in p_go.
3462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003464gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465{
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003466#ifdef FEAT_GUI_DARKTHEME
3467 static int prev_dark_theme = -1;
3468 int using_dark_theme = FALSE;
3469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470#ifdef FEAT_MENU
3471 static int prev_menu_is_active = -1;
3472#endif
3473#ifdef FEAT_TOOLBAR
3474 static int prev_toolbar = -1;
3475 int using_toolbar = FALSE;
3476#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003477#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003478 int using_tabline;
3479#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003480#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 static int prev_tearoff = -1;
3482 int using_tearoff = FALSE;
3483#endif
3484
3485 char_u *p;
3486 int i;
3487#ifdef FEAT_MENU
3488 int grey_old, grey_new;
3489 char_u *temp;
3490#endif
3491 win_T *wp;
3492 int need_set_size;
3493 int fix_size;
3494
3495#ifdef FEAT_MENU
3496 if (oldval != NULL && gui.in_use)
3497 {
3498 /*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003499 * Check if the menus go from grey to non-grey or vice versa.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 */
3501 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3502 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3503 if (grey_old != grey_new)
3504 {
3505 temp = p_go;
3506 p_go = oldval;
3507 gui_update_menus(MENU_ALL_MODES);
3508 p_go = temp;
3509 }
3510 }
3511 gui.menu_is_active = FALSE;
3512#endif
3513
3514 for (i = 0; i < 3; i++)
3515 gui.which_scrollbars[i] = FALSE;
3516 for (p = p_go; *p; p++)
3517 switch (*p)
3518 {
3519 case GO_LEFT:
3520 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3521 break;
3522 case GO_RIGHT:
3523 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 case GO_VLEFT:
3526 if (win_hasvertsplit())
3527 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3528 break;
3529 case GO_VRIGHT:
3530 if (win_hasvertsplit())
3531 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3532 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 case GO_BOT:
3534 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3535 break;
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003536#ifdef FEAT_GUI_DARKTHEME
3537 case GO_DARKTHEME:
3538 using_dark_theme = TRUE;
3539 break;
3540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541#ifdef FEAT_MENU
3542 case GO_MENUS:
3543 gui.menu_is_active = TRUE;
3544 break;
3545#endif
3546 case GO_GREY:
Bram Moolenaar30613902019-12-01 22:11:18 +01003547 // make menu's have grey items, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 break;
3549#ifdef FEAT_TOOLBAR
3550 case GO_TOOLBAR:
3551 using_toolbar = TRUE;
3552 break;
3553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003555#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 using_tearoff = TRUE;
3557#endif
3558 break;
3559 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01003560 // Ignore options that are not supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 break;
3562 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003563
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003564 if (!gui.in_use)
3565 return;
3566
3567 need_set_size = 0;
3568 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003569
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003570#ifdef FEAT_GUI_DARKTHEME
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003571 if (using_dark_theme != prev_dark_theme)
3572 {
3573 gui_mch_set_dark_theme(using_dark_theme);
3574 prev_dark_theme = using_dark_theme;
3575 }
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003576#endif
3577
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003578#ifdef FEAT_GUI_TABLINE
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003579 // Update the GUI tab line, it may appear or disappear. This may
3580 // cause the non-GUI tab line to disappear or appear.
3581 using_tabline = gui_has_tabline();
3582 if (!gui_mch_showing_tabline() != !using_tabline)
3583 {
3584 // We don't want a resize event change "Rows" here, save and
3585 // restore it. Resizing is handled below.
3586 i = Rows;
3587 gui_update_tabline();
3588 Rows = i;
3589 need_set_size |= RESIZE_VERT;
3590 if (using_tabline)
3591 fix_size = TRUE;
3592 if (!gui_use_tabline())
3593 redraw_tabline = TRUE; // may draw non-GUI tab line
3594 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003595#endif
3596
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003597 for (i = 0; i < 3; i++)
3598 {
3599 // The scrollbar needs to be updated when it is shown/unshown and
3600 // when switching tab pages. But the size only changes when it's
3601 // shown/unshown. Thus we need two places to remember whether a
3602 // scrollbar is there or not.
3603 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3604 || gui.which_scrollbars[i]
3605 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003607 if (i == SBAR_BOTTOM)
3608 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3609 gui.which_scrollbars[i]);
3610 else
3611 {
3612 FOR_ALL_WINDOWS(wp)
3613 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3614 }
3615 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
3617 if (i == SBAR_BOTTOM)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003618 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 else
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003620 need_set_size |= RESIZE_HOR;
3621 if (gui.which_scrollbars[i])
3622 fix_size = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003625 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3626 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
3629#ifdef FEAT_MENU
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003630 if (gui.menu_is_active != prev_menu_is_active)
3631 {
3632 // We don't want a resize event change "Rows" here, save and
3633 // restore it. Resizing is handled below.
3634 i = Rows;
3635 gui_mch_enable_menu(gui.menu_is_active);
3636 Rows = i;
3637 prev_menu_is_active = gui.menu_is_active;
3638 need_set_size |= RESIZE_VERT;
3639 if (gui.menu_is_active)
3640 fix_size = TRUE;
3641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642#endif
3643
3644#ifdef FEAT_TOOLBAR
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003645 if (using_toolbar != prev_toolbar)
3646 {
3647 gui_mch_show_toolbar(using_toolbar);
3648 prev_toolbar = using_toolbar;
3649 need_set_size |= RESIZE_VERT;
3650 if (using_toolbar)
3651 fix_size = TRUE;
3652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003654#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003655 if (using_tearoff != prev_tearoff)
3656 {
3657 gui_mch_toggle_tearoffs(using_tearoff);
3658 prev_tearoff = using_tearoff;
3659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003661 if (need_set_size != 0)
3662 {
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003663#ifdef FEAT_GUI_GTK
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003664 long prev_Columns = Columns;
3665 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003666#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003667 // Adjust the size of the window to make the text area keep the
3668 // same size and to avoid that part of our window is off-screen
3669 // and a scrollbar can't be used, for example.
3670 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003671
3672#ifdef FEAT_GUI_GTK
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003673 // GTK has the annoying habit of sending us resize events when
3674 // changing the window size ourselves. This mostly happens when
3675 // waiting for a character to arrive, quite unpredictably, and may
3676 // change Columns and Rows when we don't want it. Wait for a
3677 // character here to avoid this effect.
3678 // If you remove this, please test this command for resizing
3679 // effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3680 // Don't do this while starting up though.
3681 // Don't change Rows when adding menu/toolbar/tabline.
3682 // Don't change Columns when adding vertical toolbar.
3683 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
3684 (void)char_avail();
3685 if ((need_set_size & RESIZE_VERT) == 0)
3686 Rows = prev_Rows;
3687 if ((need_set_size & RESIZE_HOR) == 0)
3688 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003691 // When the console tabline appears or disappears the window positions
3692 // change.
3693 if (firstwin->w_winrow != tabline_height())
3694 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695}
3696
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003697#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3698/*
3699 * Return TRUE if the GUI is taking care of the tabline.
3700 * It may still be hidden if 'showtabline' is zero.
3701 */
3702 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003703gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003704{
3705 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3706}
3707
3708/*
3709 * Return TRUE if the GUI is showing the tabline.
3710 * This uses 'showtabline'.
3711 */
3712 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003713gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003714{
3715 if (!gui_use_tabline()
3716 || p_stal == 0
3717 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3718 return FALSE;
3719 return TRUE;
3720}
3721
3722/*
3723 * Update the tabline.
3724 * This may display/undisplay the tabline and update the labels.
3725 */
3726 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003727gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003728{
3729 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003730 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003731
3732 if (!gui.starting && starting == 0)
3733 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003734 // Updating the tabline uses direct GUI commands, flush
3735 // outstanding instructions first. (esp. clear screen)
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003736 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003737
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003738 if (!showit != !shown)
3739 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003740 if (showit != 0)
3741 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003742
Bram Moolenaar30613902019-12-01 22:11:18 +01003743 // When the tabs change from hidden to shown or from shown to
3744 // hidden the size of the text area should remain the same.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003745 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003746 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003747 }
3748}
3749
3750/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003751 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003752 */
3753 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003754get_tabline_label(
3755 tabpage_T *tp,
Bram Moolenaar30613902019-12-01 22:11:18 +01003756 int tooltip) // TRUE: get tooltip
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003757{
3758 int modified = FALSE;
3759 char_u buf[40];
3760 int wincount;
3761 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003762 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003763
Bram Moolenaar30613902019-12-01 22:11:18 +01003764 // Use 'guitablabel' or 'guitabtooltip' if it's set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003765 opt = (tooltip ? &p_gtt : &p_gtl);
3766 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003767 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003768 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003769 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003770 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3771 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003772
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003773 printer_page_num = tabpage_index(tp);
3774# ifdef FEAT_EVAL
3775 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003776# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003777 // It's almost as going to the tabpage, but without autocommands.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003778 curtab->tp_firstwin = firstwin;
3779 curtab->tp_lastwin = lastwin;
3780 curtab->tp_curwin = curwin;
3781 save_curtab = curtab;
3782 curtab = tp;
3783 topframe = curtab->tp_topframe;
3784 firstwin = curtab->tp_firstwin;
3785 lastwin = curtab->tp_lastwin;
3786 curwin = curtab->tp_curwin;
3787 curbuf = curwin->w_buffer;
3788
Bram Moolenaar30613902019-12-01 22:11:18 +01003789 // Can't use NameBuff directly, build_stl_str_hl() uses it.
Luuk van Baal7b224fd2022-11-07 12:16:51 +00003790 build_stl_str_hl(curwin, res, MAXPATHL, *opt, opt_name, 0,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003791 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003792 STRCPY(NameBuff, res);
3793
Bram Moolenaar30613902019-12-01 22:11:18 +01003794 // Back to the original curtab.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003795 curtab = save_curtab;
3796 topframe = curtab->tp_topframe;
3797 firstwin = curtab->tp_firstwin;
3798 lastwin = curtab->tp_lastwin;
3799 curwin = curtab->tp_curwin;
3800 curbuf = curwin->w_buffer;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003801 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003802
Bram Moolenaar30613902019-12-01 22:11:18 +01003803 // If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3804 // use a default label.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003805 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003806 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003807 // Get the buffer name into NameBuff[] and shorten it.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003808 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003809 if (!tooltip)
3810 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003811
3812 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3813 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3814 if (bufIsChanged(wp->w_buffer))
3815 modified = TRUE;
3816 if (modified || wincount > 1)
3817 {
3818 if (wincount > 1)
3819 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3820 else
3821 buf[0] = NUL;
3822 if (modified)
3823 STRCAT(buf, "+");
3824 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003825 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003826 mch_memmove(NameBuff, buf, STRLEN(buf));
3827 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003828 }
3829}
3830
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003831/*
3832 * Send the event for clicking to select tab page "nr".
3833 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003834 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003835 */
3836 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003837send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003838{
3839 char_u string[3];
3840
3841 if (nr == tabpage_index(curtab))
3842 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003843
Bram Moolenaar30613902019-12-01 22:11:18 +01003844 // Don't put events in the input queue now.
Martin Tournoij7904fa42022-10-04 16:28:45 +01003845 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003846 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003847 // Set it back to the current tab page.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003848 gui_mch_set_curtab(tabpage_index(curtab));
3849 return FALSE;
3850 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003851
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003852 string[0] = CSI;
3853 string[1] = KS_TABLINE;
3854 string[2] = KE_FILLER;
3855 add_to_input_buf(string, 3);
3856 string[0] = nr;
3857 add_to_input_buf_csi(string, 1);
3858 return TRUE;
3859}
3860
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003861/*
3862 * Send a tabline menu event
3863 */
3864 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003865send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003866{
3867 char_u string[3];
3868
Bram Moolenaar29547192018-12-11 20:39:19 +01003869 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003870 if (hold_gui_events)
3871 return;
3872
Bram Moolenaar29547192018-12-11 20:39:19 +01003873 // Cannot close the last tabpage.
3874 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3875 return;
3876
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003877 string[0] = CSI;
3878 string[1] = KS_TABMENU;
3879 string[2] = KE_FILLER;
3880 add_to_input_buf(string, 3);
3881 string[0] = tabidx;
3882 string[1] = (char_u)(long)event;
3883 add_to_input_buf_csi(string, 2);
3884}
3885
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
3888/*
3889 * Scrollbar stuff:
3890 */
3891
Bram Moolenaare45828b2006-02-15 22:12:56 +00003892/*
3893 * Remove all scrollbars. Used before switching to another tab page.
3894 */
3895 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003896gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003897{
3898 int i;
3899 win_T *wp;
3900
3901 for (i = 0; i < 3; i++)
3902 {
3903 if (i == SBAR_BOTTOM)
3904 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3905 else
3906 {
3907 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003908 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003909 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003910 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003911 }
3912}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003913
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003915gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916{
3917 static int sbar_ident = 0;
3918
Bram Moolenaar30613902019-12-01 22:11:18 +01003919 sb->ident = sbar_ident++; // No check for too big, but would it happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 sb->wp = wp;
3921 sb->type = type;
3922 sb->value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 sb->size = 1;
3924 sb->max = 1;
3925 sb->top = 0;
3926 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 sb->status_height = 0;
3929 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3930}
3931
3932/*
3933 * Find the scrollbar with the given index.
3934 */
3935 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003936gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
3938 win_T *wp;
3939
3940 if (gui.bottom_sbar.ident == ident)
3941 return &gui.bottom_sbar;
3942 FOR_ALL_WINDOWS(wp)
3943 {
3944 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3945 return &wp->w_scrollbars[SBAR_LEFT];
3946 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3947 return &wp->w_scrollbars[SBAR_RIGHT];
3948 }
3949 return NULL;
3950}
3951
3952/*
3953 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3954 *
3955 * For Win32, Macintosh and GTK+ 2:
3956 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3957 * you stop dragging the scrollbar. We get here each time the scrollbar is
3958 * dragged another pixel, but as far as the rest of vim goes, it thinks
3959 * we're just hanging in the call to DispatchMessage() in
3960 * process_message(). The DispatchMessage() call that hangs was passed a
3961 * mouse button click event in the scrollbar window. -- webb.
3962 *
3963 * Solution: Do the scrolling right here. But only when allowed.
3964 * Ignore the scrollbars while executing an external command or when there
3965 * are still characters to be processed.
3966 */
3967 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003968gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 int sb_num;
3972#ifdef USE_ON_FLY_SCROLL
3973 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975# ifdef FEAT_DIFF
3976 int old_topfill = curwin->w_topfill;
3977# endif
3978#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003979 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 int byte_count;
3981#endif
3982
3983 if (sb == NULL)
3984 return;
3985
Bram Moolenaar30613902019-12-01 22:11:18 +01003986 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 if (hold_gui_events)
3988 return;
3989
Sean Dewar988f7432023-08-16 14:17:36 +01003990 if (cmdwin_type != 0 && sb->wp != cmdwin_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
3993 if (still_dragging)
3994 {
3995 if (sb->wp == NULL)
3996 gui.dragged_sb = SBAR_BOTTOM;
3997 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3998 gui.dragged_sb = SBAR_LEFT;
3999 else
4000 gui.dragged_sb = SBAR_RIGHT;
4001 gui.dragged_wp = sb->wp;
4002 }
4003 else
4004 {
4005 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004006#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004007 // Keep the "dragged_wp" value until after the scrolling, for when the
4008 // mouse button is released. GTK2 doesn't send the button-up event.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 gui.dragged_wp = NULL;
4010#endif
4011 }
4012
Bram Moolenaar30613902019-12-01 22:11:18 +01004013 // Vertical sbar info is kept in the first sbar (the left one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 if (sb->wp != NULL)
4015 sb = &sb->wp->w_scrollbars[0];
4016
4017 /*
4018 * Check validity of value
4019 */
4020 if (value < 0)
4021 value = 0;
4022#ifdef SCROLL_PAST_END
4023 else if (value > sb->max)
4024 value = sb->max;
4025#else
4026 if (value > sb->max - sb->size + 1)
4027 value = sb->max - sb->size + 1;
4028#endif
4029
4030 sb->value = value;
4031
4032#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar30613902019-12-01 22:11:18 +01004033 // When not allowed to do the scrolling right now, return.
4034 // This also checked input_available(), but that causes the first click in
4035 // a scrollbar to be ignored when Vim doesn't have focus.
Bram Moolenaar67840782008-01-03 15:15:07 +00004036 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 return;
4038#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004039 // Disallow scrolling the current window when the completion popup menu is
4040 // visible.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004041 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4042 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043
4044#ifdef FEAT_RIGHTLEFT
4045 if (sb->wp == NULL && curwin->w_p_rl)
4046 {
4047 value = sb->max + 1 - sb->size - value;
4048 if (value < 0)
4049 value = 0;
4050 }
4051#endif
4052
Bram Moolenaar30613902019-12-01 22:11:18 +01004053 if (sb->wp != NULL) // vertical scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
4055 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4057 sb_num++;
4058 if (wp == NULL)
4059 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
4061#ifdef USE_ON_FLY_SCROLL
4062 current_scrollbar = sb_num;
4063 scrollbar_value = value;
Bram Moolenaar24959102022-05-07 20:01:16 +01004064 if (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 {
4066 gui_do_scroll();
4067 setcursor();
4068 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004069 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
4071 ins_scroll();
4072 setcursor();
4073 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004074 else if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 {
4076 if (msg_scrolled == 0)
4077 {
4078 gui_do_scroll();
4079 redrawcmdline();
4080 }
4081 }
4082# ifdef FEAT_FOLDING
Bram Moolenaar30613902019-12-01 22:11:18 +01004083 // Value may have been changed for closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 sb->value = sb->wp->w_topline - 1;
4085# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004086
Bram Moolenaar30613902019-12-01 22:11:18 +01004087 // When dragging one scrollbar and there is another one at the other
4088 // side move the thumb of that one too.
Bram Moolenaar371d5402006-03-20 21:47:49 +00004089 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4090 gui_mch_set_scrollbar_thumb(
4091 &sb->wp->w_scrollbars[
4092 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4093 ? SBAR_LEFT : SBAR_RIGHT],
4094 sb->value, sb->size, sb->max);
4095
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096#else
4097 bytes[0] = CSI;
4098 bytes[1] = KS_VER_SCROLLBAR;
4099 bytes[2] = KE_FILLER;
4100 bytes[3] = (char_u)sb_num;
4101 byte_count = 4;
4102#endif
4103 }
4104 else
4105 {
4106#ifdef USE_ON_FLY_SCROLL
4107 scrollbar_value = value;
4108
Bram Moolenaar24959102022-05-07 20:01:16 +01004109 if (State & MODE_NORMAL)
Christopher Plewright44c22092022-11-15 17:43:36 +00004110 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar24959102022-05-07 20:01:16 +01004111 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 ins_horscroll();
Bram Moolenaar24959102022-05-07 20:01:16 +01004113 else if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004115 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 {
Christopher Plewright44c22092022-11-15 17:43:36 +00004117 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 redrawcmdline();
4119 }
4120 }
4121 if (old_leftcol != curwin->w_leftcol)
4122 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004123 updateWindow(curwin); // update window, status and cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 setcursor();
4125 }
4126#else
4127 bytes[0] = CSI;
4128 bytes[1] = KS_HOR_SCROLLBAR;
4129 bytes[2] = KE_FILLER;
4130 byte_count = 3;
4131#endif
4132 }
4133
4134#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 /*
4136 * synchronize other windows, as necessary according to 'scrollbind'
4137 */
4138 if (curwin->w_p_scb
4139 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4140 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004141# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 ))))
4145 {
4146 do_check_scrollbind(TRUE);
Bram Moolenaar30613902019-12-01 22:11:18 +01004147 // need to update the window right here
Bram Moolenaar29323592016-07-24 22:04:11 +02004148 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 if (wp->w_redr_type > 0)
4150 updateWindow(wp);
4151 setcursor();
4152 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004153 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004155 add_to_input_buf(bytes, byte_count);
4156 add_long_to_buf((long_u)value, bytes);
4157 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158#endif
4159}
4160
4161/*
4162 * Scrollbar stuff:
4163 */
4164
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004165/*
4166 * Called when something in the window layout has changed.
4167 */
4168 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004169gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004170{
4171 if (gui.in_use && starting == 0)
4172 {
4173 out_flush();
4174 gui_init_which_components(NULL);
4175 gui_update_scrollbars(TRUE);
4176 }
4177 need_mouse_correct = TRUE;
4178}
4179
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004181gui_update_scrollbars(
Bram Moolenaar30613902019-12-01 22:11:18 +01004182 int force) // Force all scrollbars to get updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183{
4184 win_T *wp;
4185 scrollbar_T *sb;
Bram Moolenaar30613902019-12-01 22:11:18 +01004186 long val, size, max; // need 32 bits here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 int which_sb;
4188 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
Bram Moolenaar30613902019-12-01 22:11:18 +01004191 // Update the horizontal scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 gui_update_horiz_scrollbar(force);
4193
Bram Moolenaar4f974752019-02-17 17:44:42 +01004194#ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01004195 // Return straight away if there is neither a left nor right scrollbar.
4196 // On MS-Windows this is required anyway for scrollwheel messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4198 return;
4199#endif
4200
4201 /*
4202 * Don't want to update a scrollbar while we're dragging it. But if we
4203 * have both a left and right scrollbar, and we drag one of them, we still
4204 * need to update the other one.
4205 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004206 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4207 && gui.which_scrollbars[SBAR_LEFT]
4208 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
4210 /*
4211 * If we have two scrollbars and one of them is being dragged, just
4212 * copy the scrollbar position from the dragged one to the other one.
4213 */
4214 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4215 if (gui.dragged_wp != NULL)
4216 gui_mch_set_scrollbar_thumb(
4217 &gui.dragged_wp->w_scrollbars[which_sb],
4218 gui.dragged_wp->w_scrollbars[0].value,
4219 gui.dragged_wp->w_scrollbars[0].size,
4220 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222
Bram Moolenaar30613902019-12-01 22:11:18 +01004223 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 ++hold_gui_events;
4225
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004226 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004228 if (wp->w_buffer == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 continue;
Bram Moolenaar30613902019-12-01 22:11:18 +01004230 // Skip a scrollbar that is being dragged.
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004231 if (!force && (gui.dragged_sb == SBAR_LEFT
4232 || gui.dragged_sb == SBAR_RIGHT)
4233 && gui.dragged_wp == wp)
4234 continue;
4235
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236#ifdef SCROLL_PAST_END
4237 max = wp->w_buffer->b_ml.ml_line_count - 1;
4238#else
4239 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4240#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004241 if (max < 0) // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 max = 0;
4243 val = wp->w_topline - 1;
4244 size = wp->w_height;
4245#ifdef SCROLL_PAST_END
Bram Moolenaar30613902019-12-01 22:11:18 +01004246 if (val > max) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 val = max;
4248#else
Bram Moolenaar30613902019-12-01 22:11:18 +01004249 if (size > max + 1) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 size = max + 1;
4251 if (val > max - size + 1)
4252 val = max - size + 1;
4253#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004254 if (val < 0) // minimal value is 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 val = 0;
4256
4257 /*
4258 * Scrollbar at index 0 (the left one) contains all the information.
4259 * It would be the same info for left and right so we just store it for
4260 * one of them.
4261 */
4262 sb = &wp->w_scrollbars[0];
4263
4264 /*
4265 * Note: no check for valid w_botline. If it's not valid the
4266 * scrollbars will be updated later anyway.
4267 */
4268 if (size < 1 || wp->w_botline - 2 > max)
4269 {
4270 /*
4271 * This can happen during changing files. Just don't update the
4272 * scrollbar for now.
4273 */
Bram Moolenaar30613902019-12-01 22:11:18 +01004274 sb->height = 0; // Force update next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 if (gui.which_scrollbars[SBAR_LEFT])
4276 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4277 if (gui.which_scrollbars[SBAR_RIGHT])
4278 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4279 continue;
4280 }
4281 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 || sb->top != wp->w_winrow
4283 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004285 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004287 // Height, width or position of scrollbar has changed. For
4288 // vertical split: curwin changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 sb->top = wp->w_winrow;
4291 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
Bram Moolenaar30613902019-12-01 22:11:18 +01004294 // Calculate height and position in pixels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 h = (sb->height + sb->status_height) * gui.char_height;
4296 y = sb->top * gui.char_height + gui.border_offset;
4297#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4298 if (gui.menu_is_active)
4299 y += gui.menu_height;
4300#endif
4301
Bram Moolenaar0b962e52022-04-03 18:02:37 +01004302#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004303 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 y += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306#endif
4307
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004308#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004309 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004310 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004311#endif
4312
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004315 // Height of top scrollbar includes width of top border
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 h += gui.border_offset;
4317 y -= gui.border_offset;
4318 }
4319 if (gui.which_scrollbars[SBAR_LEFT])
4320 {
4321 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4322 gui.left_sbar_x, y,
4323 gui.scrollbar_width, h);
4324 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4325 }
4326 if (gui.which_scrollbars[SBAR_RIGHT])
4327 {
4328 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4329 gui.right_sbar_x, y,
4330 gui.scrollbar_width, h);
4331 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4332 }
4333 }
4334
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 if (force || sb->value != val || sb->size != size || sb->max != max)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004337 // Thumb of scrollbar has moved
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 sb->value = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 sb->size = size;
4340 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004341 if (gui.which_scrollbars[SBAR_LEFT]
4342 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4344 val, size, max);
4345 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004346 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4348 val, size, max);
4349 }
4350 }
Christian Brabandt2e0f3ec2021-11-28 18:41:05 +00004351
4352 // update the title, it may show the scroll position
4353 maketitle();
4354
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 --hold_gui_events;
4357}
4358
4359/*
4360 * Enable or disable a scrollbar.
4361 * Check for scrollbars for vertically split windows which are not enabled
4362 * sometimes.
4363 */
4364 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004365gui_do_scrollbar(
4366 win_T *wp,
Bram Moolenaar30613902019-12-01 22:11:18 +01004367 int which, // SBAR_LEFT or SBAR_RIGHT
4368 int enable) // TRUE to enable scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 int midcol = curwin->w_wincol + curwin->w_width / 2;
4371 int has_midcol = (wp->w_wincol <= midcol
4372 && wp->w_wincol + wp->w_width >= midcol);
4373
Bram Moolenaar30613902019-12-01 22:11:18 +01004374 // Only enable scrollbars that contain the middle column of the current
4375 // window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4377 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004378 // Scrollbars only on one side. Don't enable scrollbars that don't
4379 // contain the middle column of the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 if (!has_midcol)
4381 enable = FALSE;
4382 }
4383 else
4384 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004385 // Scrollbars on both sides. Don't enable scrollbars that neither
4386 // contain the middle column of the current window nor are on the far
4387 // side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 if (midcol > Columns / 2)
4389 {
4390 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4391 enable = FALSE;
4392 }
4393 else
4394 {
4395 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4396 : !has_midcol)
4397 enable = FALSE;
4398 }
4399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4401}
4402
4403/*
Bram Moolenaar097c5372023-05-24 21:02:24 +01004404 * Scroll a window according to the values set in the globals
4405 * "current_scrollbar" and "scrollbar_value".
4406 * Return TRUE if the cursor in the current window moved or FALSE otherwise.
Christian Brabandt20d161a2023-10-05 22:08:30 +02004407 * may eventually cause a redraw using updateWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 */
4409 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004410gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
4412 win_T *wp, *save_wp;
4413 int i;
4414 long nlines;
4415 pos_T old_cursor;
4416 linenr_T old_topline;
4417#ifdef FEAT_DIFF
4418 int old_topfill;
4419#endif
4420
4421 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4422 if (wp == NULL)
4423 break;
4424 if (wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004425 // Couldn't find window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 return FALSE;
Christian Brabandt20d161a2023-10-05 22:08:30 +02004427 // don't redraw, LineOffset and similar are not valid!
4428 if (exmode_active)
4429 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
4431 /*
4432 * Compute number of lines to scroll. If zero, nothing to do.
4433 */
4434 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4435 if (nlines == 0)
4436 return FALSE;
4437
4438 save_wp = curwin;
4439 old_topline = wp->w_topline;
4440#ifdef FEAT_DIFF
4441 old_topfill = wp->w_topfill;
4442#endif
4443 old_cursor = wp->w_cursor;
4444 curwin = wp;
4445 curbuf = wp->w_buffer;
4446 if (nlines < 0)
4447 scrolldown(-nlines, gui.dragged_wp == NULL);
4448 else
4449 scrollup(nlines, gui.dragged_wp == NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004450 // Reset dragged_wp after using it. "dragged_sb" will have been reset for
4451 // the mouse-up event already, but we still want it to behave like when
4452 // dragging. But not the next click in an arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 if (gui.dragged_sb == SBAR_NONE)
4454 gui.dragged_wp = NULL;
4455
4456 if (old_topline != wp->w_topline
4457#ifdef FEAT_DIFF
4458 || old_topfill != wp->w_topfill
4459#endif
4460 )
4461 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004462 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004464 cursor_correct(); // fix window for 'so'
4465 update_topline(); // avoid up/down jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 }
4467 if (old_cursor.lnum != wp->w_cursor.lnum)
4468 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471
Bram Moolenaar30613902019-12-01 22:11:18 +01004472 // Make sure wp->w_leftcol and wp->w_skipcol are correct.
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004473 validate_cursor();
4474
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 curwin = save_wp;
4476 curbuf = save_wp->w_buffer;
4477
4478 /*
4479 * Don't call updateWindow() when nothing has changed (it will overwrite
4480 * the status line!).
4481 */
4482 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004483 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484#ifdef FEAT_DIFF
4485 || old_topfill != wp->w_topfill
4486#endif
4487 )
4488 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004489 int type = UPD_VALID;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004490
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004491 if (pum_visible())
4492 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004493 type = UPD_NOT_VALID;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004494 wp->w_lines_valid = 0;
4495 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004496
Bram Moolenaar30613902019-12-01 22:11:18 +01004497 // Don't set must_redraw here, it may cause the popup menu to
4498 // disappear when losing focus after a scrollbar drag.
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004499 if (wp->w_redr_type < type)
4500 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004501 mch_disable_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01004502 updateWindow(wp); // update window, status line, and cmdline
Bram Moolenaara338adc2018-01-31 20:51:47 +01004503 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505
Bram Moolenaar30613902019-12-01 22:11:18 +01004506 // May need to redraw the popup menu.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004507 if (pum_visible())
4508 pum_redraw();
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004509
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004510 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511}
4512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513/*
4514 * Horizontal scrollbar stuff:
4515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004517gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518{
Christopher Plewright44c22092022-11-15 17:43:36 +00004519 long value, size, max;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
4521 if (!gui.which_scrollbars[SBAR_BOTTOM])
4522 return;
4523
4524 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4525 return;
4526
4527 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4528 return;
4529
4530 /*
4531 * It is possible for the cursor to be invalid if we're in the middle of
4532 * something (like changing files). If so, don't do anything for now.
4533 */
4534 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4535 {
4536 gui.bottom_sbar.value = -1;
4537 return;
4538 }
4539
Bram Moolenaar02631462017-09-22 15:20:32 +02004540 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if (curwin->w_p_wrap)
4542 {
4543 value = 0;
4544#ifdef SCROLL_PAST_END
4545 max = 0;
4546#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004547 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548#endif
4549 }
4550 else
4551 {
4552 value = curwin->w_leftcol;
Christopher Plewright44c22092022-11-15 17:43:36 +00004553 max = scroll_line_len(ui_find_longest_lnum());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (virtual_active())
4556 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004557 // May move the cursor even further to the right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (curwin->w_virtcol >= (colnr_T)max)
4559 max = curwin->w_virtcol;
4560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
4562#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004563 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004565 // The line number isn't scrolled, thus there is less space when
4566 // 'number' or 'relativenumber' is set (also for 'foldcolumn').
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 size -= curwin_col_off();
4568#ifndef SCROLL_PAST_END
4569 max -= curwin_col_off();
4570#endif
4571 }
4572
4573#ifndef SCROLL_PAST_END
4574 if (value > max - size + 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01004575 value = max - size + 1; // limit the value to allowable range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#endif
4577
4578#ifdef FEAT_RIGHTLEFT
4579 if (curwin->w_p_rl)
4580 {
4581 value = max + 1 - size - value;
4582 if (value < 0)
4583 {
4584 size += value;
4585 value = 0;
4586 }
4587 }
4588#endif
4589 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4590 && max == gui.bottom_sbar.max)
4591 return;
4592
4593 gui.bottom_sbar.value = value;
4594 gui.bottom_sbar.size = size;
4595 gui.bottom_sbar.max = max;
4596 gui.prev_wrap = curwin->w_p_wrap;
4597
4598 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4599}
4600
4601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 * Check that none of the colors are the same as the background color
4603 */
4604 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004605gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606{
4607 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4608 {
4609 gui_set_bg_color((char_u *)"White");
4610 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4611 gui_set_fg_color((char_u *)"Black");
4612 }
4613}
4614
Bram Moolenaar3918c952005-03-15 22:34:55 +00004615 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004616gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
4618 gui.norm_pixel = gui_get_color(name);
4619 hl_set_fg_color_name(vim_strsave(name));
4620}
4621
Bram Moolenaar3918c952005-03-15 22:34:55 +00004622 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004623gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624{
4625 gui.back_pixel = gui_get_color(name);
4626 hl_set_bg_color_name(vim_strsave(name));
4627}
4628
4629/*
4630 * Allocate a color by name.
4631 * Returns INVALCOLOR and gives an error message when failed.
4632 */
4633 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004634gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635{
4636 guicolor_T t;
4637
4638 if (*name == NUL)
4639 return INVALCOLOR;
4640 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004641
Bram Moolenaar5b9f5722023-02-19 20:49:38 +00004642 int is_none = STRCMP(name, "none") == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004644#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar5b9f5722023-02-19 20:49:38 +00004645 && (gui.in_use || is_none)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646#endif
4647 )
Bram Moolenaar5b9f5722023-02-19 20:49:38 +00004648 {
4649 if (is_none)
4650 emsg(_(e_cannot_use_color_none_did_you_mean_none));
4651 else
4652 semsg(_(e_cannot_allocate_color_str), name);
4653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 return t;
4655}
4656
4657/*
4658 * Return the grey value of a color (range 0-255).
4659 */
4660 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004661gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004663 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004665 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004666 + (((rgb >> 8) & 0xff) * 587)
4667 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668}
4669
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004670 char_u *
4671gui_bg_default(void)
4672{
4673 if (gui_get_lightness(gui.back_pixel) < 127)
4674 return (char_u *)"dark";
4675 return (char_u *)"light";
4676}
4677
4678/*
4679 * Option initializations that can only be done after opening the GUI window.
4680 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004681 static void
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004682init_gui_options(void)
4683{
Bram Moolenaar30613902019-12-01 22:11:18 +01004684 // Set the 'background' option according to the lightness of the
4685 // background color, unless the user has set it already.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004686 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
4687 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004688 set_option_value_give_err((char_u *)"bg", 0L, gui_bg_default(), 0);
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004689 highlight_changed();
4690 }
4691}
4692
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693#if defined(FEAT_GUI_X11) || defined(PROTO)
4694 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004695gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696{
4697 win_T *wp;
4698
Bram Moolenaar30613902019-12-01 22:11:18 +01004699 // Nothing to do if GUI hasn't started yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 if (!gui.in_use)
4701 return;
4702
4703 FOR_ALL_WINDOWS(wp)
4704 {
4705 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4706 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4707 }
4708 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4709}
4710#endif
4711
4712/*
4713 * Call this when focus has changed.
4714 */
4715 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004716gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717{
4718/*
4719 * Skip this code to avoid drawing the cursor when debugging and switching
4720 * between the debugger window and gvim.
4721 */
4722#if 1
4723 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004724 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
4726# ifdef FEAT_XIM
4727 xim_set_focus(in_focus);
4728# endif
4729
Bram Moolenaar30613902019-12-01 22:11:18 +01004730 // Put events in the input queue only when allowed.
4731 // ui_focus_change() isn't called directly, because it invokes
4732 // autocommands and that must not happen asynchronously.
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004733 if (!hold_gui_events)
4734 {
4735 char_u bytes[3];
4736
4737 bytes[0] = CSI;
4738 bytes[1] = KS_EXTRA;
4739 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4740 add_to_input_buf(bytes, 3);
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742#endif
4743}
4744
4745/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004746 * When mouse moved: apply 'mousefocus'.
4747 * Also updates the mouse pointer shape.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 */
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004749 static void
4750gui_mouse_focus(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751{
4752 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004753 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
4755#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01004756 // Get window pointer, and update mouse shape as well.
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004757 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758#endif
4759
Bram Moolenaar30613902019-12-01 22:11:18 +01004760 // Only handle this when 'mousefocus' set and ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (p_mousef
Bram Moolenaar30613902019-12-01 22:11:18 +01004762 && !hold_gui_events // not holding events
Bram Moolenaar24959102022-05-07 20:01:16 +01004763 && (State & (MODE_NORMAL | MODE_INSERT))// Normal/Visual/Insert mode
4764 && State != MODE_HITRETURN // but not hit-return prompt
Bram Moolenaar30613902019-12-01 22:11:18 +01004765 && msg_scrolled == 0 // no scrolled message
4766 && !need_mouse_correct // not moving the pointer
4767 && gui.in_focus) // gvim in focus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004769 // Don't move the mouse when it's left or right of the Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (x < 0 || x > Columns * gui.char_width)
4771 return;
4772#ifndef FEAT_MOUSESHAPE
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004773 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774#endif
4775 if (wp == curwin || wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004776 return; // still in the same old window, or none at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777
Bram Moolenaar30613902019-12-01 22:11:18 +01004778 // Ignore position in the tab pages line.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004779 if (Y_2_ROW(y) < tabline_height())
4780 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004781
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004783 * Format a mouse click on status line input,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004785 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4786 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 */
4788 if (finish_op)
4789 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004790 // abort the current operator first
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 st[0] = ESC;
4792 add_to_input_buf(st, 1);
4793 }
4794 st[0] = CSI;
4795 st[1] = KS_MOUSE;
4796 st[2] = KE_FILLER;
4797 st[3] = (char_u)MOUSE_LEFT;
4798 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004799 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 wp->w_height + W_WINROW(wp));
4801
4802 add_to_input_buf(st, 8);
4803 st[3] = (char_u)MOUSE_RELEASE;
4804 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004806 // Need to wake up the main loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 if (gtk_main_level() > 0)
4808 gtk_main_quit();
4809#endif
4810 }
4811}
4812
4813/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004814 * Called when the mouse moved (but not when dragging).
4815 */
4816 void
4817gui_mouse_moved(int x, int y)
4818{
4819 // Ignore this while still starting up.
4820 if (!gui.in_use || gui.starting)
4821 return;
4822
4823 // apply 'mousefocus' and pointer shape
4824 gui_mouse_focus(x, y);
4825
Ernie Raelc4cb5442022-04-03 15:47:28 +01004826 if (p_mousemev
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004827#ifdef FEAT_PROP_POPUP
Ernie Raelc4cb5442022-04-03 15:47:28 +01004828 || popup_uses_mouse_move
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004829#endif
Ernie Raelc4cb5442022-04-03 15:47:28 +01004830 )
4831 // Generate a mouse-moved event. For a <MouseMove> mapping. Or so the
4832 // popup can perhaps be closed, just like in the terminal.
4833 gui_send_mouse_event(MOUSE_MOVE, x, y, FALSE, 0);
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004834}
4835
4836/*
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004837 * Get the window where the mouse pointer is on.
4838 * Returns NULL if not found.
4839 */
4840 win_T *
4841gui_mouse_window(mouse_find_T popup)
4842{
4843 int x, y;
4844
4845 if (!(gui.in_use && (p_mousef || popup == FIND_POPUP)))
4846 return NULL;
4847 gui_mch_getmouse(&x, &y);
4848
4849 // Only use the mouse when it's on the Vim window
4850 if (x >= 0 && x <= Columns * gui.char_width
4851 && y >= 0 && Y_2_ROW(y) >= tabline_height())
4852 return xy2win(x, y, popup);
4853 return NULL;
4854}
4855
4856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 * Called when mouse should be moved to window with focus.
4858 */
4859 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004860gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 win_T *wp = NULL;
4863
4864 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004865
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004866 wp = gui_mouse_window(IGNORE_POPUP);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004867 if (wp == curwin || wp == NULL)
4868 return;
4869
4870 // If in other than current window
4871 validate_cline_row();
4872 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4873 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4874 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875}
4876
4877/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004878 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004879 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 static win_T *
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004882xy2win(int x, int y, mouse_find_T popup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 int row;
4885 int col;
4886 win_T *wp;
4887
4888 row = Y_2_ROW(y);
4889 col = X_2_COL(x);
Bram Moolenaar30613902019-12-01 22:11:18 +01004890 if (row < 0 || col < 0) // before first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return NULL;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004892 wp = mouse_find_win(&row, &col, popup);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004893 if (wp == NULL)
4894 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004895#ifdef FEAT_MOUSESHAPE
Bram Moolenaar24959102022-05-07 20:01:16 +01004896 if (State == MODE_HITRETURN || State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
4898 if (Y_2_ROW(y) >= msg_row)
4899 update_mouseshape(SHAPE_IDX_MOREL);
4900 else
4901 update_mouseshape(SHAPE_IDX_MORE);
4902 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004903 else if (row > wp->w_height) // below status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaar24959102022-05-07 20:01:16 +01004905 else if (!(State & MODE_CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004906 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaar24959102022-05-07 20:01:16 +01004908 else if (!(State & MODE_CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004909 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 update_mouseshape(SHAPE_IDX_STATUS);
4911 else
4912 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004914 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915}
4916
4917/*
4918 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4919 * File names may be given to redefine the args list.
4920 */
4921 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004922ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923{
4924 char_u *arg = eap->arg;
4925
4926 /*
4927 * Check for "-f" argument: foreground, don't fork.
4928 * Also don't fork when started with "gvim -f".
4929 * Do fork when using "gui -b".
4930 */
4931 if (arg[0] == '-'
4932 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004933 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
4935 gui.dofork = (arg[1] == 'b');
4936 eap->arg = skipwhite(eap->arg + 2);
4937 }
4938 if (!gui.in_use)
4939 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004940#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaar257a3962019-12-29 15:19:03 +01004941 if (!gui.starting)
4942 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004943 emsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaar257a3962019-12-29 15:19:03 +01004944 return;
4945 }
4946#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004947 // Clear the command. Needed for when forking+exiting, to avoid part
4948 // of the argument ending up after the shell prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 msg_clr_eos_force();
Bram Moolenaar257a3962019-12-29 15:19:03 +01004950#ifdef GUI_MAY_SPAWN
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02004951 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004952 gui_start(eap->arg);
4953 else
Bram Moolenaar257a3962019-12-29 15:19:03 +01004954#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004955 gui_start(NULL);
Bram Moolenaar257a3962019-12-29 15:19:03 +01004956#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004957 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02004960 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 ex_next(eap);
4962}
4963
Bram Moolenaar4f974752019-02-17 17:44:42 +01004964#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004965 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON) \
4966 || defined(FEAT_GUI_HAIKU)) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004967 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968/*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01004969 * This is shared between Haiku, Motif, and GTK.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971
4972/*
4973 * Callback function for do_in_runtimepath().
4974 */
4975 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004976gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004978 char_u *gfp_buffer = cookie;
4979
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 if (STRLEN(fname) >= MAXPATHL)
4981 *gfp_buffer = NUL;
4982 else
4983 STRCPY(gfp_buffer, fname);
4984}
4985
4986/*
4987 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
4988 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
4989 */
4990 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004991gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
4993 if (STRLEN(name) > MAXPATHL - 14)
4994 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00004995 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01004996 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004997 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 return FAIL;
4999 return OK;
5000}
5001
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005002# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003/*
5004 * Given the name of the "icon=" argument, try finding the bitmap file for the
5005 * icon. If it is an absolute path name, use it as it is. Otherwise append
5006 * "ext" and search for it in 'runtimepath'.
5007 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5008 * contains "name".
5009 */
5010 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005011gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
5013 char_u buf[MAXPATHL + 1];
5014
5015 expand_env(name, buffer, MAXPATHL);
5016 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5017 STRCPY(buffer, buf);
5018}
5019# endif
5020#endif
5021
Bram Moolenaar9e175142020-04-30 22:51:01 +02005022#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)|| defined(FEAT_GUI_HAIKU) \
5023 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005025display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026{
5027 char_u *p;
5028
5029 if (isatty(2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005031 fflush(stderr);
5032 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005034
5035 if (error_ga.ga_data == NULL)
5036 return;
5037
5038 // avoid putting up a message box with blanks only
5039 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
Keith Thompson184f71c2024-01-04 21:19:04 +01005040 if (!SAFE_isspace(*p))
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005041 {
5042 // Truncate a very long message, it will go off-screen.
5043 if (STRLEN(p) > 2000)
5044 STRCPY(p + 2000 - 14, "...(truncated)");
5045 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
5046 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
5047 break;
5048 }
5049 ga_clear(&error_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050}
5051#endif
5052
5053#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5054/*
5055 * Return TRUE if still starting up and there is no place to enter text.
5056 * For GTK and X11 we check if stderr is not a tty, which means we were
5057 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5058 * allow typing on stdin.
5059 */
5060 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005061no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062{
5063 return ((!gui.in_use || gui.starting)
5064# ifndef NO_CONSOLE
5065 && !isatty(0) && !isatty(2)
5066# endif
5067 );
5068}
5069#endif
5070
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005071#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005072 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005073 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074/*
5075 * Update the current window and the screen.
5076 */
5077 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005078gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005080# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005081 linenr_T conceal_old_cursor_line = 0;
5082 linenr_T conceal_new_cursor_line = 0;
5083 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005084# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005085
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 update_topline();
5087 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005088
Bram Moolenaar30613902019-12-01 22:11:18 +01005089 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005090 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005091# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005092 || popup_visible
5093# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005094# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005095 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005096# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005097 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005098 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005099 if (has_cursormoved())
5100 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
K.Takata6e1d31e2022-02-03 13:05:32 +00005101# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005102 if (popup_visible)
5103 popup_check_cursor_pos();
K.Takata6e1d31e2022-02-03 13:05:32 +00005104# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005105# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005106 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005107 {
5108 conceal_old_cursor_line = last_cursormoved.lnum;
5109 conceal_new_cursor_line = curwin->w_cursor.lnum;
5110 conceal_update_lines = TRUE;
5111 }
5112# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005113 last_cursormoved = curwin->w_cursor;
5114 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005115
LemonBoy09371822022-04-08 15:18:45 +01005116 if (!finish_op)
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00005117 may_trigger_win_scrolled_resized();
LemonBoy09371822022-04-08 15:18:45 +01005118
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005119# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005120 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005121 && (conceal_old_cursor_line != conceal_new_cursor_line
5122 || conceal_cursor_line(curwin)
5123 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005124 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005125 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005126 redrawWinline(curwin, conceal_old_cursor_line);
5127 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005128 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005129 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005130 }
5131# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005132 update_screen(0); // may need to update the screen
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005133 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005134 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135}
5136#endif
5137
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005138#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139/*
5140 * Get the text to use in a find/replace dialog. Uses the last search pattern
5141 * if the argument is empty.
5142 * Returns an allocated string.
5143 */
5144 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005145get_find_dialog_text(
5146 char_u *arg,
Bram Moolenaar30613902019-12-01 22:11:18 +01005147 int *wwordp, // return: TRUE if \< \> found
5148 int *mcasep) // return: TRUE if \C found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149{
5150 char_u *text;
5151
5152 if (*arg == NUL)
5153 text = last_search_pat();
5154 else
5155 text = arg;
5156 if (text != NULL)
5157 {
5158 text = vim_strsave(text);
5159 if (text != NULL)
5160 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005161 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 int i;
5163
Bram Moolenaar30613902019-12-01 22:11:18 +01005164 // Remove "\V"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5166 {
5167 mch_memmove(text, text + 2, (size_t)(len - 1));
5168 len -= 2;
5169 }
5170
Bram Moolenaar30613902019-12-01 22:11:18 +01005171 // Recognize "\c" and "\C" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5173 {
5174 *mcasep = (text[1] == 'C');
5175 mch_memmove(text, text + 2, (size_t)(len - 1));
5176 len -= 2;
5177 }
5178
Bram Moolenaar30613902019-12-01 22:11:18 +01005179 // Recognize "\<text\>" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 if (len >= 4
5181 && STRNCMP(text, "\\<", 2) == 0
5182 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5183 {
5184 *wwordp = TRUE;
5185 mch_memmove(text, text + 2, (size_t)(len - 4));
5186 text[len - 4] = NUL;
5187 }
5188
Bram Moolenaar30613902019-12-01 22:11:18 +01005189 // Recognize "\/" or "\?" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 for (i = 0; i + 1 < len; ++i)
5191 if (text[i] == '\\' && (text[i + 1] == '/'
5192 || text[i + 1] == '?'))
5193 {
5194 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5195 --len;
5196 }
5197 }
5198 }
5199 return text;
5200}
5201
5202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 * Handle the press of a button in the find-replace dialog.
5204 * Return TRUE when something was added to the input buffer.
5205 */
5206 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005207gui_do_findrepl(
Bram Moolenaar30613902019-12-01 22:11:18 +01005208 int flags, // one of FRD_REPLACE, FRD_FINDNEXT, etc.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005209 char_u *find_text,
5210 char_u *repl_text,
Bram Moolenaar30613902019-12-01 22:11:18 +01005211 int down) // Search downwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212{
5213 garray_T ga;
5214 int i;
5215 int type = (flags & FRD_TYPE_MASK);
5216 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005217 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005218 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005219 static int busy = FALSE;
5220
Bram Moolenaar30613902019-12-01 22:11:18 +01005221 // When the screen is being updated we should not change buffers and
5222 // windows structures, it may cause freed memory to be used. Also don't
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005223 // do this recursively (pressing "Find" quickly several times).
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005224 if (updating_screen || busy)
5225 return FALSE;
5226
Bram Moolenaar30613902019-12-01 22:11:18 +01005227 // refuse replace when text cannot be changed
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005228 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5229 return FALSE;
5230
5231 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232
5233 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005234 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 ga_concat(&ga, (char_u *)"%s/");
5236
5237 ga_concat(&ga, (char_u *)"\\V");
5238 if (flags & FRD_MATCH_CASE)
5239 ga_concat(&ga, (char_u *)"\\C");
5240 else
5241 ga_concat(&ga, (char_u *)"\\c");
5242 if (flags & FRD_WHOLE_WORD)
5243 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar30613902019-12-01 22:11:18 +01005244 // escape slash and backslash
Bram Moolenaar518bc172018-05-13 17:05:30 +02005245 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5246 if (p != NULL)
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005247 ga_concat(&ga, p);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005248 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 if (flags & FRD_WHOLE_WORD)
5250 ga_concat(&ga, (char_u *)"\\>");
5251
5252 if (type == FRD_REPLACEALL)
5253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 ga_concat(&ga, (char_u *)"/");
matveyt2834ebd2022-09-06 17:00:15 +01005255 // Escape slash and backslash.
5256 // Also escape tilde and ampersand if 'magic' is set.
5257 p = vim_strsave_escaped(repl_text,
5258 p_magic ? (char_u *)"/\\~&" : (char_u *)"/\\");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005259 if (p != NULL)
5260 ga_concat(&ga, p);
5261 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005263 }
5264 ga_append(&ga, NUL);
5265
5266 if (type == FRD_REPLACE)
5267 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005268 // Do the replacement when the text at the cursor matches. Thus no
5269 // replacement is done if the cursor was moved!
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005270 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5271 regmatch.rm_ic = 0;
5272 if (regmatch.regprog != NULL)
5273 {
5274 p = ml_get_cursor();
5275 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5276 && regmatch.startp[0] == p)
5277 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005278 // Clear the command line to remove any old "No match"
5279 // error.
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005280 msg_end_prompt();
5281
5282 if (u_save_cursor() == OK)
5283 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005284 // A button was pressed thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005285 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005286
5287 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005288 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005289 ins_str(repl_text);
5290 }
5291 }
5292 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005293 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005294 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005295 }
5296 }
5297
5298 if (type == FRD_REPLACEALL)
5299 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005300 // A button was pressed, thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005301 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 do_cmdline_cmd(ga.ga_data);
5303 }
5304 else
5305 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005306 int searchflags = SEARCH_MSG + SEARCH_MARK;
5307
Bram Moolenaar30613902019-12-01 22:11:18 +01005308 // Search for the next match.
5309 // Don't skip text under cursor for single replace.
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005310 if (type == FRD_REPLACE)
5311 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005313 if (down)
5314 {
John Marriott8c85a2a2024-05-20 19:18:26 +02005315 (void)do_search(NULL, '/', '/', ga.ga_data, STRLEN(ga.ga_data), 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005316 }
5317 else
5318 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005319 // We need to escape '?' if and only if we are searching in the up
5320 // direction
Bram Moolenaar518bc172018-05-13 17:05:30 +02005321 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5322 if (p != NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005323 (void)do_search(NULL, '?', '?', p, STRLEN(p), 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005324 vim_free(p);
5325 }
5326
Bram Moolenaar30613902019-12-01 22:11:18 +01005327 msg_scroll = i; // don't let an error message set msg_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329
Bram Moolenaar30613902019-12-01 22:11:18 +01005330 // Don't want to pass did_emsg to other code, it may cause disabling
5331 // syntax HL if we were busy redrawing.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005332 did_emsg = save_did_emsg;
5333
Bram Moolenaar24959102022-05-07 20:01:16 +01005334 if (State & (MODE_NORMAL | MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005336 gui_update_screen(); // update the screen
5337 msg_didout = 0; // overwrite any message
5338 need_wait_return = FALSE; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
5340
5341 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005342 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 return (ga.ga_len > 0);
5344}
5345
5346#endif
5347
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005348#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349/*
5350 * Jump to the window at specified point (x, y).
5351 */
5352 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005353gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
5355 int row = Y_2_ROW(y);
5356 int col = X_2_COL(x);
5357 win_T *wp;
5358
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005359 if (row < 0 || col < 0)
5360 return;
5361
5362 wp = mouse_find_win(&row, &col, FAIL_POPUP);
5363 if (wp != NULL && wp != curwin)
5364 win_goto(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
5367/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005368 * Function passed to handle_drop() for the actions to be done after the
5369 * argument list has been updated.
5370 */
5371 static void
5372drop_callback(void *cookie)
5373{
5374 char_u *p = cookie;
Bram Moolenaar4671e882021-11-22 17:21:48 +00005375 int do_shorten = FALSE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005376
Bram Moolenaar30613902019-12-01 22:11:18 +01005377 // If Shift held down, change to first file's directory. If the first
5378 // item is a directory, change to that directory (and let the explorer
5379 // plugin show the contents).
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005380 if (p != NULL)
5381 {
5382 if (mch_isdir(p))
5383 {
5384 if (mch_chdir((char *)p) == 0)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005385 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005386 }
5387 else if (vim_chdirfile(p, "drop") == OK)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005388 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005389 vim_free(p);
Bram Moolenaar4671e882021-11-22 17:21:48 +00005390 if (do_shorten)
5391 {
5392 shorten_fnames(TRUE);
5393 last_chdir_reason = "drop";
5394 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005395 }
5396
Bram Moolenaar30613902019-12-01 22:11:18 +01005397 // Update the screen display
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005398 update_screen(UPD_NOT_VALID);
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005399# ifdef FEAT_MENU
5400 gui_update_menus(0);
5401# endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005402 maketitle();
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005403 setcursor();
5404 out_flush_cursor(FALSE, FALSE);
5405}
5406
5407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 * Process file drop. Mouse cursor position, key modifiers, name of files
5409 * and count of files are given. Argument "fnames[count]" has full pathnames
5410 * of dropped files, they will be freed in this function, and caller can't use
5411 * fnames after call this function.
5412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005414gui_handle_drop(
5415 int x UNUSED,
5416 int y UNUSED,
5417 int_u modifiers,
5418 char_u **fnames,
5419 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420{
5421 int i;
5422 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005423 static int entered = FALSE;
5424
5425 /*
5426 * This function is called by event handlers. Just in case we get a
5427 * second event before the first one is handled, ignore the second one.
5428 * Not sure if this can ever happen, just in case.
5429 */
5430 if (entered)
5431 return;
5432 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433
5434 /*
5435 * When the cursor is at the command line, add the file names to the
5436 * command line, don't edit the files.
5437 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005438 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {
5440 shorten_filenames(fnames, count);
5441 for (i = 0; i < count; ++i)
5442 {
5443 if (fnames[i] != NULL)
5444 {
5445 if (i > 0)
5446 add_to_input_buf((char_u*)" ", 1);
5447
Bram Moolenaar30613902019-12-01 22:11:18 +01005448 // We don't know what command is used thus we can't be sure
5449 // about which characters need to be escaped. Only escape the
5450 // most common ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451# ifdef BACKSLASH_IN_FILENAME
5452 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5453# else
5454 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5455# endif
5456 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005457 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 vim_free(p);
5459 vim_free(fnames[i]);
5460 }
5461 }
5462 vim_free(fnames);
5463 }
5464 else
5465 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005466 // Go to the window under mouse cursor, then shorten given "fnames" by
5467 // current window, because a window can have local current dir.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 shorten_filenames(fnames, count);
5470
Bram Moolenaar30613902019-12-01 22:11:18 +01005471 // If Shift held down, remember the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 if ((modifiers & MOUSE_SHIFT) != 0)
5473 p = vim_strsave(fnames[0]);
5474 else
5475 p = NULL;
5476
Bram Moolenaar30613902019-12-01 22:11:18 +01005477 // Handle the drop, :edit or :split to get to the file. This also
5478 // frees fnames[]. Skip this if there is only one item, it's a
5479 // directory and Shift is held down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5481 && mch_isdir(fnames[0]))
5482 {
5483 vim_free(fnames[0]);
5484 vim_free(fnames);
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02005485 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005488 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
Bram Moolenaar4671e882021-11-22 17:21:48 +00005489 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005491
5492 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493}
5494#endif
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005495
5496/*
5497 * Check if "key" is to interrupt us. Handles a key that has not had modifiers
5498 * applied yet.
5499 * Return the key with modifiers applied if so, NUL if not.
5500 */
5501 int
5502check_for_interrupt(int key, int modifiers_arg)
5503{
5504 int modifiers = modifiers_arg;
5505 int c = merge_modifyOtherKeys(key, &modifiers);
5506
5507 if ((c == Ctrl_C && ctrl_c_interrupts)
Bram Moolenaaraf50e892020-08-01 13:22:10 +02005508#ifdef UNIX
5509 || (intr_char != Ctrl_C && c == intr_char)
5510#endif
5511 )
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005512 {
5513 got_int = TRUE;
5514 return c;
5515 }
5516 return NUL;
5517}
5518
Bram Moolenaar2d12c252022-06-13 21:42:45 +01005519/*
5520 * If the "--gui-log-file fname" argument is given write the dialog title and
5521 * message to a file and return TRUE. Otherwise return FALSE.
5522 * When there is any problem opening the file or writing to the file this is
5523 * ignored, showing the dialog might get the test to get stuck.
5524 */
5525 int
5526gui_dialog_log(char_u *title, char_u *message)
5527{
5528 char_u *fname = get_gui_dialog_file();
5529 FILE *fd;
5530
5531 if (fname == NULL)
5532 return FALSE;
5533
5534 fd = mch_fopen((char *)fname, "a");
5535 if (fd != NULL)
5536 {
5537 fprintf(fd, "%s: %s\n", title, message);
5538 fclose(fd);
5539 }
5540 return TRUE;
5541}