blob: fbbfb8721404376a8668528019435fbc0a89dfb3 [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;
John Marriottd2fcbb42025-05-07 19:59:42 +0200360 if (STRCMP(buffer, "ok") == 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200361 return GUI_CHILD_OK;
362 return GUI_CHILD_FAILED;
363}
364
Bram Moolenaar30613902019-12-01 22:11:18 +0100365#endif // GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367/*
368 * Call this when vim starts up, whether or not the GUI is started
369 */
370 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100371gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372{
Bram Moolenaar30613902019-12-01 22:11:18 +0100373 gui.in_use = FALSE; // No GUI yet (maybe later)
374 gui.starting = FALSE; // No GUI yet (maybe later)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 gui_mch_prepare(argc, argv);
376}
377
378/*
379 * Try initializing the GUI and check if it can be started.
380 * Used from main() to check early if "vim -g" can start the GUI.
381 * Used from gui_init() to prepare for starting the GUI.
382 * Returns FAIL or OK.
383 */
384 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100385gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386{
387 static int result = MAYBE;
388
389 if (result != MAYBE)
390 {
391 if (result == FAIL)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000392 emsg(_(e_cannot_start_the_GUI));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 return result;
394 }
395
396 gui.shell_created = FALSE;
397 gui.dying = FALSE;
Bram Moolenaar30613902019-12-01 22:11:18 +0100398 gui.in_focus = TRUE; // so the guicursor setting works
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 gui.dragged_sb = SBAR_NONE;
400 gui.dragged_wp = NULL;
401 gui.pointer_hidden = FALSE;
402 gui.col = 0;
403 gui.row = 0;
404 gui.num_cols = Columns;
405 gui.num_rows = Rows;
406
407 gui.cursor_is_valid = FALSE;
408 gui.scroll_region_top = 0;
409 gui.scroll_region_bot = Rows - 1;
410 gui.scroll_region_left = 0;
411 gui.scroll_region_right = Columns - 1;
412 gui.highlight_mask = HL_NORMAL;
413 gui.char_width = 1;
414 gui.char_height = 1;
415 gui.char_ascent = 0;
416 gui.border_width = 0;
417
418 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200419#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 gui.bold_font = NOFONT;
421 gui.ital_font = NOFONT;
422 gui.boldital_font = NOFONT;
423# ifdef FEAT_XFONTSET
424 gui.fontset = NOFONTSET;
425# endif
426#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200427 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100428#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200429 gui.wide_bold_font = NOFONT;
430 gui.wide_ital_font = NOFONT;
431 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433
434#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200435# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436# ifdef FONTSET_ALWAYS
437 gui.menu_fontset = NOFONTSET;
438# else
439 gui.menu_font = NOFONT;
440# endif
441# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100442 gui.menu_is_active = TRUE; // default: include menu
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443# ifndef FEAT_GUI_GTK
444 gui.menu_height = MENU_DEFAULT_HEIGHT;
445 gui.menu_width = 0;
446# endif
447#endif
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100448#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 gui.toolbar_height = 0;
450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451#ifdef FEAT_BEVAL_TIP
452 gui.tooltip_fontset = NOFONTSET;
453#endif
454
455 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
456 gui.prev_wrap = -1;
457
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +0100458#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Erik S. V. Jansson2f026382024-02-26 22:23:05 +0100459 // Note: gui_set_ligatures() might already have been called e.g. from .vimrc,
460 // and in that case we don't want to overwrite ligatures map that has already
461 // been correctly populated (as that would lead to a cleared ligatures maps).
462 if (*p_guiligatures == NUL)
zeertzjqd9be94c2024-07-14 10:20:20 +0200463 CLEAR_FIELD(gui.ligatures_map);
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100464#endif
465
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200466#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 result = OK;
468#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200469# ifdef FEAT_GUI_GTK
470 /*
471 * Note: Don't call gtk_init_check() before fork, it will be called after
472 * the fork. When calling it before fork, it make vim hang for a while.
473 * See gui_do_fork().
474 * Use a simpler check if the GUI window can probably be opened.
475 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200476 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200477# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480#endif
481 return result;
482}
483
484/*
485 * This is the call which starts the GUI.
486 */
487 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100488gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
490 win_T *wp;
Yee Cheng China5e03f62025-02-06 21:18:17 +0100491 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 static int recursive = 0;
493
494 /*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000495 * It's possible to use ":gui" in a .gvimrc file. The first half of this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 * function will then be executed at the first call, the rest by the
497 * recursive call. This allow the shell to be opened halfway reading a
498 * gvimrc file.
499 */
500 if (!recursive)
501 {
502 ++recursive;
503
504 clip_init(TRUE);
505
Bram Moolenaar30613902019-12-01 22:11:18 +0100506 // If can't initialize, don't try doing the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 if (gui_init_check() == FAIL)
508 {
509 --recursive;
510 clip_init(FALSE);
511 return;
512 }
513
514 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000515 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
516 * breaks the Paste toolbar button.
517 */
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100518 set_option_value_give_err((char_u *)"paste", 0L, NULL, 0);
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000519
Bram Moolenaaracc770a2020-04-12 15:11:06 +0200520 // Set t_Co to the number of colors: RGB.
521 set_color_count(256 * 256 * 256);
522
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000523 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 * Set up system-wide default menus.
525 */
526#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
527 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
528 {
529 sys_menu = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 sys_menu = FALSE;
532 }
533#endif
534
535 /*
536 * Switch on the mouse by default, unless the user changed it already.
537 * This can then be changed in the .gvimrc.
538 */
539 if (!option_was_set((char_u *)"mouse"))
540 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000541 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
543 /*
544 * If -U option given, use only the initializations from that file and
545 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
546 */
547 if (use_gvimrc != NULL)
548 {
549 if (STRCMP(use_gvimrc, "NONE") != 0
550 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 && do_source(use_gvimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000552 semsg(_(e_cannot_read_from_str), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
554 else
555 {
556 /*
557 * Get system wide defaults for gvim, only when file name defined.
558 */
559#ifdef SYS_GVIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561#endif
562
563 /*
564 * Try to read GUI initialization commands from the following
565 * places:
566 * - environment variable GVIMINIT
567 * - the user gvimrc file (~/.gvimrc)
568 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
569 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
570 * The first that exists is used, the rest is ignored.
571 */
572 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000573 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000576 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200579#ifdef USR_GVIMRC_FILE3
580 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 )
584 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200585#ifdef USR_GVIMRC_FILE4
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE,
587 DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588#endif
589 }
590
591 /*
592 * Read initialization commands from ".gvimrc" in current
593 * directory. This is only done if the 'exrc' option is set.
594 * Because of security reasons we disallow shell and write
595 * commands now, except for unix if the file is owned by the user
596 * or 'secure' option has been reset in environment of global
597 * ".gvimrc".
598 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
599 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
600 */
601 if (p_exrc)
602 {
603#ifdef UNIX
604 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200605 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaar30613902019-12-01 22:11:18 +0100607 // if ".gvimrc" file is not owned by user, set 'secure'
608 // mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
610 secure = p_secure;
611 }
612#else
613 secure = p_secure;
614#endif
615
616 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200617 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#ifdef SYS_GVIMRC_FILE
619 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200620 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621#endif
622#ifdef USR_GVIMRC_FILE2
623 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200624 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625#endif
626#ifdef USR_GVIMRC_FILE3
627 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200628 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200630#ifdef USR_GVIMRC_FILE4
631 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200632 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
637 if (secure == 2)
638 need_wait_return = TRUE;
639 secure = 0;
640 }
641 }
642
643 if (need_wait_return || msg_didany)
644 wait_return(TRUE);
645
646 --recursive;
647 }
648
Bram Moolenaar30613902019-12-01 22:11:18 +0100649 // If recursive call opened the shell, return here from the first call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if (gui.in_use)
651 return;
652
653 /*
654 * Create the GUI shell.
655 */
Bram Moolenaar30613902019-12-01 22:11:18 +0100656 gui.in_use = TRUE; // Must be set after menus have been set up
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if (gui_mch_init() == FAIL)
658 goto error;
659
Bram Moolenaar30613902019-12-01 22:11:18 +0100660 // Avoid a delay for an error message that was printed in the terminal
661 // where Vim was started.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 emsg_on_display = FALSE;
663 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100664 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 need_wait_return = FALSE;
666 msg_didany = FALSE;
667
668 /*
669 * Check validity of any generic resources that may have been loaded.
670 */
671 if (gui.border_width < 0)
672 gui.border_width = 0;
673
674 /*
675 * Set up the fonts. First use a font specified with "-fn" or "-font".
676 */
677 if (font_argument != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100678 set_option_value_give_err((char_u *)"gfn",
679 0L, (char_u *)font_argument, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (
681#ifdef FEAT_XFONTSET
682 (*p_guifontset == NUL
683 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
684#endif
685 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
686 : p_guifont, FALSE) == FAIL)
687 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000688 emsg(_(e_cannot_start_gui_no_valid_font_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 goto error2;
690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 if (gui_get_wide_font() == FAIL)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000692 emsg(_(e_guifontwide_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694 gui.num_cols = Columns;
695 gui.num_rows = Rows;
696 gui_reset_scroll_region();
697
Bram Moolenaar30613902019-12-01 22:11:18 +0100698 // Create initial scrollbars
Yee Cheng China5e03f62025-02-06 21:18:17 +0100699 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
701 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
702 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
703 }
704 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
705
706#ifdef FEAT_MENU
707 gui_create_initial_menus(root_menu);
708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#ifdef FEAT_SIGN_ICONS
710 sign_gui_started();
711#endif
712
Bram Moolenaar30613902019-12-01 22:11:18 +0100713 // Configure the desired menu and scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 gui_init_which_components(NULL);
715
Bram Moolenaar30613902019-12-01 22:11:18 +0100716 // All components of the GUI have been created now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 gui.shell_created = TRUE;
718
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100719#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100720 // Set the shell size, adjusted for the screen size. For GTK this only
721 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100722 // If the window is already maximized (e.g. when --windowid is passed in),
723 // we want to use the system-provided dimensions by passing FALSE to
724 // mustset. Otherwise, we want to initialize with the default rows/columns.
725 if (gui_mch_maximized())
726 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
727 else
728 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100729#else
730# ifndef FEAT_GUI_GTK
731 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
732# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733#endif
734#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +0100735 // Need to set the size of the menubar after all the menus have been
736 // created.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 gui_mch_compute_menu_height((Widget)0);
738#endif
739
740 /*
741 * Actually open the GUI shell.
742 */
743 if (gui_mch_open() != FAIL)
744 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 maketitle();
746 resettitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 init_gui_options();
749#ifdef FEAT_ARABIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100750 // Our GUI can't do bidi.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 p_tbidi = FALSE;
752#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000753#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100754 // Give GTK+ a chance to put all widget's into place.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000756
757# ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +0100758 // If there is no 'm' in 'guioptions' we need to remove the menu now.
759 // It was still there to make F10 work.
Bram Moolenaar18144c82006-04-12 21:52:12 +0000760 if (vim_strchr(p_go, GO_MENUS) == NULL)
761 {
762 --gui.starting;
763 gui_mch_enable_menu(FALSE);
764 ++gui.starting;
765 gui_mch_update();
766 }
767# endif
768
Bram Moolenaar30613902019-12-01 22:11:18 +0100769 // Now make sure the shell fits on the screen.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100770 if (gui_mch_maximized())
771 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
772 else
773 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100775 // When 'lines' was set while starting up the topframe may have to be
776 // resized.
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000777 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000778
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100779#ifdef FEAT_BEVAL_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100780 // Always create the Balloon Evaluation area, but disable it when
781 // 'ballooneval' is off.
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100782 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200783 {
784# ifdef FEAT_VARTABS
785 vim_free(balloonEval->vts);
786# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100787 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200788 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100789 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000790# ifdef FEAT_GUI_GTK
791 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
792 &general_beval_cb, NULL);
793# else
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100794# if defined(FEAT_GUI_MOTIF)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000795 {
796 extern Widget textArea;
797 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000798 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000799 }
800# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100801# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000802 balloonEval = gui_mch_create_beval_area(NULL, NULL,
803 &general_beval_cb, NULL);
804# endif
805# endif
806# endif
807 if (!p_beval)
808 gui_mch_disable_beval_area(balloonEval);
809#endif
Bram Moolenaarad772a62020-06-01 14:07:49 +0200810
811#ifndef FEAT_GUI_MSWIN
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200812 // In the GUI modifiers are prepended to keys.
Bram Moolenaarad772a62020-06-01 14:07:49 +0200813 // Don't do this for MS-Windows yet, it sends CTRL-K without the
814 // modifier.
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200815 seenModifyOtherKeys = TRUE;
Bram Moolenaarad772a62020-06-01 14:07:49 +0200816#endif
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
819 if (!im_xim_isvalid_imactivate())
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000820 emsg(_(e_value_of_imactivatekey_is_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100822 // When 'cmdheight' was set during startup it may not have taken
823 // effect yet.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000824 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000825 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 return;
828 }
829
830error2:
831#ifdef FEAT_GUI_X11
Bram Moolenaar30613902019-12-01 22:11:18 +0100832 // undo gui_mch_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 gui_mch_uninit();
834#endif
835
836error:
837 gui.in_use = FALSE;
838 clip_init(FALSE);
839}
840
841
842 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100843gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
Bram Moolenaar30613902019-12-01 22:11:18 +0100845 // don't free the fonts, it leads to a BUS error
846 // richard@whitequeen.com Jul 99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 gui.in_use = FALSE;
849 gui_mch_exit(rc);
850}
851
Bram Moolenaar9372a112005-12-06 19:59:18 +0000852#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +0200853 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000854# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855/*
856 * Called when the GUI shell is closed by the user. If there are no changed
857 * files Vim exits, otherwise there will be a dialog to ask the user what to
858 * do.
859 * When this function returns, Vim should NOT exit!
860 */
861 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100862gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863{
Bram Moolenaar3552e742021-05-29 12:21:58 +0200864 cmdmod_T save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
Bram Moolenaar3552e742021-05-29 12:21:58 +0200866 if (before_quit_autocmds(curwin, TRUE, FALSE))
867 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaar30613902019-12-01 22:11:18 +0100869 // Only exit when there are no changed files
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 exiting = TRUE;
871# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200872 cmdmod.cmod_flags |= CMOD_BROWSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873# endif
874# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +0200875 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100877 // If there are changed buffers, present the user with a dialog if
878 // possible, otherwise give an error message.
Bram Moolenaar027387f2016-01-02 22:25:52 +0100879 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 getout(0);
881
882 exiting = FALSE;
883 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +0100884 gui_update_screen(); // redraw, window may show changed buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885}
886#endif
887
888/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200889 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * first font name that works is used. If none is found, use the default
891 * font.
892 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
893 * Return OK when able to set the font. When it failed FAIL is returned and
894 * the fonts are unchanged.
895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100897gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898{
899#define FONTLEN 320
900 char_u font_name[FONTLEN];
901 int font_list_empty = FALSE;
902 int ret = FAIL;
903
904 if (!gui.in_use)
905 return FAIL;
906
907 font_name[0] = NUL;
908 if (*font_list == NUL)
909 font_list_empty = TRUE;
910 else
911 {
912#ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +0100913 // When using a fontset, the whole list of fonts is one name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if (fontset)
915 ret = gui_mch_init_font(font_list, TRUE);
916 else
917#endif
918 while (*font_list != NUL)
919 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100920 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
922
Bram Moolenaar30613902019-12-01 22:11:18 +0100923 // Careful!!! The Win32 version of gui_mch_init_font(), when
924 // called with "*" will change p_guifont to the selected font
925 // name, which frees the old value. This makes font_list
926 // invalid. Thus when OK is returned here, font_list must no
927 // longer be used!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (gui_mch_init_font(font_name, FALSE) == OK)
929 {
K.Takata94373c42022-01-27 15:04:22 +0000930#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar30613902019-12-01 22:11:18 +0100931 // If it's a Unicode font, try setting 'guifontwide' to a
932 // similar double-width font.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
934 && strstr((char *)font_name, "10646") != NULL)
935 set_guifontwide(font_name);
936#endif
937 ret = OK;
938 break;
939 }
940 }
941 }
942
943 if (ret != OK
944 && STRCMP(font_list, "*") != 0
945 && (font_list_empty || gui.norm_font == NOFONT))
946 {
947 /*
948 * Couldn't load any font in 'font_list', keep the current font if
949 * there is one. If 'font_list' is empty, or if there is no current
950 * font, tell gui_mch_init_font() to try to find a font we can load.
951 */
952 ret = gui_mch_init_font(NULL, FALSE);
953 }
954
955 if (ret == OK)
956 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200957#ifndef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100958 // Set normal font as current font
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959# ifdef FEAT_XFONTSET
960 if (gui.fontset != NOFONTSET)
961 gui_mch_set_fontset(gui.fontset);
962 else
963# endif
964 gui_mch_set_font(gui.norm_font);
965#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100966 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968
969 return ret;
970}
971
K.Takata94373c42022-01-27 15:04:22 +0000972#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973/*
974 * Try setting 'guifontwide' to a font twice as wide as "name".
975 */
976 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100977set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
979 int i = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +0100980 char_u wide_name[FONTLEN + 10]; // room for 2 * width and '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 char_u *wp = NULL;
982 char_u *p;
983 GuiFont font;
984
985 wp = wide_name;
986 for (p = name; *p != NUL; ++p)
987 {
988 *wp++ = *p;
989 if (*p == '-')
990 {
991 ++i;
Bram Moolenaar30613902019-12-01 22:11:18 +0100992 if (i == 6) // font type: change "--" to "-*-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 {
994 if (p[1] == '-')
995 *wp++ = '*';
996 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100997 else if (i == 12) // found the width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
999 ++p;
1000 i = getdigits(&p);
1001 if (i != 0)
1002 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001003 // Double the width specification.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 sprintf((char *)wp, "%d%s", i * 2, p);
1005 font = gui_mch_get_font(wide_name, FALSE);
1006 if (font != NOFONT)
1007 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001008 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 gui.wide_font = font;
1010 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001011 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014 break;
1015 }
1016 }
1017 }
1018}
K.Takata94373c42022-01-27 15:04:22 +00001019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020
1021/*
1022 * Get the font for 'guifontwide'.
1023 * Return FAIL for an invalid font name.
1024 */
1025 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001026gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
1028 GuiFont font = NOFONT;
1029 char_u font_name[FONTLEN];
1030 char_u *p;
1031
Bram Moolenaar30613902019-12-01 22:11:18 +01001032 if (!gui.in_use) // Can't allocate font yet, assume it's OK.
1033 return OK; // Will give an error message later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1036 {
1037 for (p = p_guifontwide; *p != NUL; )
1038 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001039 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1041 font = gui_mch_get_font(font_name, FALSE);
1042 if (font != NOFONT)
1043 break;
1044 }
1045 if (font == NOFONT)
1046 return FAIL;
1047 }
1048
1049 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001050#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001051 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (font != NOFONT && gui.norm_font != NOFONT
1053 && pango_font_description_equal(font, gui.norm_font))
1054 {
1055 gui.wide_font = NOFONT;
1056 gui_mch_free_font(font);
1057 }
1058 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001061#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001062 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001063#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001064 /*
1065 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1066 * support those fonts for 'guifontwide'.
1067 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 return OK;
1070}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01001072#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001073/*
1074 * Set list of ascii characters that combined can create ligature.
1075 * Store them in char map for quick access from gui_gtk2_draw_string.
1076 */
1077 void
1078gui_set_ligatures(void)
1079{
1080 char_u *p;
1081
1082 if (*p_guiligatures != NUL)
1083 {
1084 // check for invalid characters
1085 for (p = p_guiligatures; *p != NUL; ++p)
1086 if (*p < 32 || *p > 127)
1087 {
1088 emsg(_(e_ascii_code_not_in_range));
1089 return;
1090 }
1091
1092 // store valid setting into ligatures_map
1093 CLEAR_FIELD(gui.ligatures_map);
1094 for (p = p_guiligatures; *p != NUL; ++p)
1095 gui.ligatures_map[*p] = 1;
1096 }
1097 else
1098 CLEAR_FIELD(gui.ligatures_map);
1099}
Dusan Popovicce59b9f2021-11-22 17:18:44 +00001100
1101/*
1102 * Adjust the columns to undraw for when the cursor is on ligatures.
1103 */
1104 static void
1105gui_adjust_undraw_cursor_for_ligatures(int *startcol, int *endcol)
1106{
1107 int off;
1108
1109 if (ScreenLines == NULL || *p_guiligatures == NUL)
1110 return;
1111
1112 // expand before the cursor for all the chars in gui.ligatures_map
1113 off = LineOffset[gui.cursor_row] + *startcol;
1114 if (gui.ligatures_map[ScreenLines[off]])
1115 while (*startcol > 0 && gui.ligatures_map[ScreenLines[--off]])
1116 (*startcol)--;
1117
1118 // expand after the cursor for all the chars in gui.ligatures_map
1119 off = LineOffset[gui.cursor_row] + *endcol;
1120 if (gui.ligatures_map[ScreenLines[off]])
1121 while (*endcol < ((int)screen_Columns - 1)
1122 && gui.ligatures_map[ScreenLines[++off]])
1123 (*endcol)++;
1124}
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001125#endif
1126
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001127 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001128gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129{
1130 gui.row = row;
1131 gui.col = col;
1132}
1133
1134/*
1135 * gui_check_pos - check if the cursor is on the screen.
1136 */
1137 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001138gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
1140 if (gui.row >= screen_Rows)
1141 gui.row = screen_Rows - 1;
1142 if (gui.col >= screen_Columns)
1143 gui.col = screen_Columns - 1;
1144 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1145 gui.cursor_is_valid = FALSE;
1146}
1147
1148/*
1149 * Redraw the cursor if necessary or when forced.
1150 * Careful: The contents of ScreenLines[] must match what is on the screen,
1151 * otherwise this goes wrong. May need to call out_flush() first.
1152 */
1153 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001154gui_update_cursor(
Bram Moolenaar30613902019-12-01 22:11:18 +01001155 int force, // when TRUE, update even when not moved
1156 int clear_selection) // clear selection under cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157{
1158 int cur_width = 0;
1159 int cur_height = 0;
1160 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001161 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001163#ifdef FEAT_TERMINAL
1164 guicolor_T shape_fg = INVALCOLOR;
1165 guicolor_T shape_bg = INVALCOLOR;
1166#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001167 guicolor_T cfg, cbg, cc; // cursor fore-/background color
1168 int cattr; // cursor attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 int attr;
1170 attrentry_T *aep = NULL;
1171
Bram Moolenaar30613902019-12-01 22:11:18 +01001172 // Don't update the cursor when halfway busy scrolling or the screen size
1173 // doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then.
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001174 if (!can_update_cursor || screen_Columns != gui.num_cols
1175 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 return;
1177
1178 gui_check_pos();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001179
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001180 if (gui.cursor_is_valid && !force
1181 && gui.row == gui.cursor_row && gui.col == gui.cursor_col)
1182 return;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001183
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001184 gui_undraw_cursor();
1185
1186 // If a cursor-less sleep is ongoing, leave the cursor invisible
1187 if (cursor_is_sleeping())
1188 return;
1189
1190 if (gui.row < 0)
1191 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001192#ifdef HAVE_INPUT_METHOD
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001193 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1194 im_set_position(gui.row, gui.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001196 gui.cursor_row = gui.row;
1197 gui.cursor_col = gui.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001199 // Only write to the screen after ScreenLines[] has been initialized
1200 if (!screen_cleared || ScreenLines == NULL)
1201 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001203 // Clear the selection if we are about to write over it
1204 if (clear_selection)
1205 clip_may_clear_selection(gui.row, gui.row);
1206 // Check that the cursor is inside the shell (resizing may have made
1207 // it invalid)
1208 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1209 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001211 gui.cursor_is_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001213 /*
1214 * How the cursor is drawn depends on the current mode.
1215 * When in a terminal window use the shape/color specified there.
1216 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001217#ifdef FEAT_TERMINAL
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001218 if (terminal_is_active())
1219 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
1220 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001221#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001222 shape = &shape_table[get_shape_idx(FALSE)];
1223 if (State & MODE_LANGMAP)
1224 id = shape->id_lm;
1225 else
1226 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001228 // get the colors and attributes for the cursor. Default is inverted
1229 cfg = INVALCOLOR;
1230 cbg = INVALCOLOR;
1231 cattr = HL_INVERSE;
1232 gui_mch_set_blinking(shape->blinkwait,
1233 shape->blinkon,
1234 shape->blinkoff);
1235 if (shape->blinkwait == 0 || shape->blinkon == 0
1236 || shape->blinkoff == 0)
1237 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001238#ifdef FEAT_TERMINAL
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001239 if (shape_bg != INVALCOLOR)
1240 {
1241 cattr = 0;
1242 cfg = shape_fg;
1243 cbg = shape_bg;
1244 }
1245 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (id > 0)
1248 {
1249 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar54612582019-11-21 17:13:31 +01001250#if defined(HAVE_INPUT_METHOD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
1252 static int iid;
1253 guicolor_T fg, bg;
1254
Bram Moolenaarc236c162008-07-13 17:41:49 +00001255 if (
Bram Moolenaar54612582019-11-21 17:13:31 +01001256# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001257 preedit_get_status()
1258# else
1259 im_get_status()
1260# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001261 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
1263 iid = syn_name2id((char_u *)"CursorIM");
1264 if (iid > 0)
1265 {
1266 syn_id2colors(iid, &fg, &bg);
1267 if (bg != INVALCOLOR)
1268 cbg = bg;
1269 if (fg != INVALCOLOR)
1270 cfg = fg;
1271 }
1272 }
1273 }
1274#endif
1275 }
1276
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001277 /*
1278 * Get the attributes for the character under the cursor.
1279 * When no cursor color was given, use the character color.
1280 */
1281 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1282 if (attr > HL_ALL)
1283 aep = syn_gui_attr2entry(attr);
1284 if (aep != NULL)
1285 {
1286 attr = aep->ae_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (cfg == INVALCOLOR)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001288 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1289 : aep->ae_u.gui.fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 if (cbg == INVALCOLOR)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001291 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1292 : aep->ae_u.gui.bg_color);
1293 }
1294 if (cfg == INVALCOLOR)
1295 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1296 if (cbg == INVALCOLOR)
1297 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
1299#ifdef FEAT_XIM
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001300 if (aep != NULL)
1301 {
1302 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1303 : aep->ae_u.gui.bg_color);
1304 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1305 : aep->ae_u.gui.fg_color);
1306 if (xim_bg_color == INVALCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001308 : gui.back_pixel;
1309 if (xim_fg_color == INVALCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001311 : gui.norm_pixel;
1312 }
1313 else
1314 {
1315 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1316 : gui.back_pixel;
1317 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1318 : gui.norm_pixel;
1319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320#endif
1321
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001322 attr &= ~HL_INVERSE;
1323 if (cattr & HL_INVERSE)
1324 {
1325 cc = cbg;
1326 cbg = cfg;
1327 cfg = cc;
1328 }
1329 cattr &= ~HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001331 /*
1332 * When we don't have window focus, draw a hollow cursor.
1333 */
1334 if (!gui.in_focus)
1335 {
1336 gui_mch_draw_hollow_cursor(cbg);
1337 return;
1338 }
1339
1340 old_hl_mask = gui.highlight_mask;
1341 if (shape->shape == SHAPE_BLOCK)
1342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001344 * Draw the text character with the cursor colors. Use the
1345 * character attributes plus the cursor attributes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 */
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001347 gui.highlight_mask = (cattr | attr);
1348 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1349 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1350 }
1351 else
1352 {
1353#if defined(FEAT_RIGHTLEFT)
1354 int col_off = FALSE;
1355#endif
1356 /*
1357 * First draw the partial cursor, then overwrite with the text
1358 * character, using a transparent background.
1359 */
1360 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001362 cur_height = gui.char_height;
1363 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
1365 else
1366 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001367 cur_height = (gui.char_height * shape->percentage + 99) / 100;
1368 cur_width = gui.char_width;
1369 }
1370 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1371 LineOffset[gui.row] + screen_Columns) > 1)
1372 {
1373 // Double wide character.
1374 if (shape->shape != SHAPE_VER)
1375 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001376#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001377 if (CURSOR_BAR_RIGHT)
1378 {
1379 // gui.col points to the left half of the character but
1380 // the vertical line needs to be on the right half.
1381 // A double-wide horizontal line is also drawn from the
1382 // right half in gui_mch_draw_part_cursor().
1383 col_off = TRUE;
1384 ++gui.col;
Bram Moolenaar13505972019-01-24 15:04:48 +01001385 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001386#endif
1387 }
1388 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001389#if defined(FEAT_RIGHTLEFT)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001390 if (col_off)
1391 --gui.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#endif
1393
Bram Moolenaar30613902019-12-01 22:11:18 +01001394#ifndef FEAT_GUI_MSWIN // doesn't seem to work for MSWindows
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001395 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1396 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1397 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1398 (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001401 gui.highlight_mask = old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402}
1403
1404#if defined(FEAT_MENU) || defined(PROTO)
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01001405 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001406gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001408# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 if (gui.menu_is_active && gui.in_use)
1410 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1411# endif
1412}
1413#endif
1414
1415/*
1416 * Position the various GUI components (text area, menu). The vertical
1417 * scrollbars are NOT handled here. See gui_update_scrollbars().
1418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001420gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 int text_area_x;
1423 int text_area_y;
1424 int text_area_width;
1425 int text_area_height;
1426
Bram Moolenaar30613902019-12-01 22:11:18 +01001427 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 ++hold_gui_events;
1429
1430 text_area_x = 0;
1431 if (gui.which_scrollbars[SBAR_LEFT])
1432 text_area_x += gui.scrollbar_width;
1433
1434 text_area_y = 0;
1435#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1436 gui.menu_width = total_width;
1437 if (gui.menu_is_active)
1438 text_area_y += gui.menu_height;
1439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
K.Takata6e1d31e2022-02-03 13:05:32 +00001441#if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001442 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001443 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001444 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001445#endif
1446
Bram Moolenaar0b962e52022-04-03 18:02:37 +01001447#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) \
K.Takatac81e9bf2022-01-16 14:15:49 +00001448 || defined(FEAT_GUI_HAIKU) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1450 {
Bram Moolenaar0b962e52022-04-03 18:02:37 +01001451# if defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 gui_mch_set_toolbar_pos(0, text_area_y,
1453 gui.menu_width, gui.toolbar_height);
1454# endif
1455 text_area_y += gui.toolbar_height;
1456 }
1457#endif
1458
K.Takata6e1d31e2022-02-03 13:05:32 +00001459#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_HAIKU)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001460 gui_mch_set_tabline_pos(0, text_area_y,
1461 gui.menu_width, gui.tabline_height);
1462 if (gui_has_tabline())
1463 text_area_y += gui.tabline_height;
1464#endif
1465
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1467 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1468
1469 gui_mch_set_text_area_pos(text_area_x,
1470 text_area_y,
1471 text_area_width,
1472 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001473#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 + xim_get_status_area_height()
1475#endif
1476 );
1477#ifdef FEAT_MENU
1478 gui_position_menu();
1479#endif
1480 if (gui.which_scrollbars[SBAR_BOTTOM])
1481 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1482 text_area_x,
Bram Moolenaar203ec772020-07-17 20:43:43 +02001483 text_area_y + text_area_height
1484 + gui_mch_get_scrollbar_ypadding(),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 text_area_width,
1486 gui.scrollbar_height);
1487 gui.left_sbar_x = 0;
Bram Moolenaar203ec772020-07-17 20:43:43 +02001488 gui.right_sbar_x = text_area_x + text_area_width
1489 + gui_mch_get_scrollbar_xpadding();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
1491 --hold_gui_events;
1492}
1493
Bram Moolenaar02743632005-07-25 20:42:36 +00001494/*
1495 * Get the width of the widgets and decorations to the side of the text area.
1496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001498gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 int base_width;
1501
1502 base_width = 2 * gui.border_offset;
1503 if (gui.which_scrollbars[SBAR_LEFT])
1504 base_width += gui.scrollbar_width;
1505 if (gui.which_scrollbars[SBAR_RIGHT])
1506 base_width += gui.scrollbar_width;
1507 return base_width;
1508}
1509
Bram Moolenaar02743632005-07-25 20:42:36 +00001510/*
1511 * Get the height of the widgets and decorations above and below the text area.
1512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001514gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
1516 int base_height;
1517
1518 base_height = 2 * gui.border_offset;
1519 if (gui.which_scrollbars[SBAR_BOTTOM])
1520 base_height += gui.scrollbar_height;
1521#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001522 // We can't take the sizes properly into account until anything is
1523 // realized. Therefore we recalculate all the values here just before
1524 // setting the size. (--mdcki)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525#else
1526# ifdef FEAT_MENU
1527 if (gui.menu_is_active)
1528 base_height += gui.menu_height;
1529# endif
1530# ifdef FEAT_TOOLBAR
1531 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 base_height += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001534# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001535 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001536 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001537 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001538# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1540 base_height += gui_mch_text_area_extra_height();
1541# endif
1542#endif
1543 return base_height;
1544}
1545
1546/*
1547 * Should be called after the GUI shell has been resized. Its arguments are
1548 * the new width and height of the shell in pixels.
1549 */
1550 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001551gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 static int busy = FALSE;
1554
Bram Moolenaar30613902019-12-01 22:11:18 +01001555 if (!gui.shell_created) // ignore when still initializing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 return;
1557
1558 /*
1559 * Can't resize the screen while it is being redrawn. Remember the new
1560 * size and handle it later.
1561 */
1562 if (updating_screen || busy)
1563 {
1564 new_pixel_width = pixel_width;
1565 new_pixel_height = pixel_height;
1566 return;
1567 }
1568
1569again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001570 new_pixel_width = 0;
1571 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 busy = TRUE;
1573
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001574#ifdef FEAT_GUI_HAIKU
1575 vim_lock_screen();
1576#endif
1577
Bram Moolenaar30613902019-12-01 22:11:18 +01001578 // Flush pending output before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 out_flush();
1580
1581 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001582 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 /*
1588 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1589 * at the last line here (why does it have to be one row too low?).
1590 */
Bram Moolenaar24959102022-05-07 20:01:16 +01001591 if (State == MODE_ASKMORE || State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 gui.row = gui.num_rows;
1593
Bram Moolenaar30613902019-12-01 22:11:18 +01001594 // Only comparing Rows and Columns may be sufficient, but let's stay on
1595 // the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
lilydjwg6e0a18f2024-01-29 20:54:28 +01001597 || gui.num_rows != Rows || gui.num_cols != Columns || gui.force_redraw)
1598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 shell_resized();
lilydjwg6e0a18f2024-01-29 20:54:28 +01001600 gui.force_redraw = 0;
1601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001603#ifdef FEAT_GUI_HAIKU
1604 vim_unlock_screen();
1605#endif
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 gui_update_scrollbars(TRUE);
1608 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001609#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 xim_set_status_area();
1611#endif
1612
1613 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001614
Bram Moolenaar30613902019-12-01 22:11:18 +01001615 // We may have been called again while redrawing the screen.
1616 // Need to do it all again with the latest size then. But only if the size
1617 // actually changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (new_pixel_height)
1619 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001620 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1621 {
1622 new_pixel_width = 0;
1623 new_pixel_height = 0;
1624 }
1625 else
1626 {
1627 pixel_width = new_pixel_width;
1628 pixel_height = new_pixel_height;
1629 goto again;
1630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
1632}
1633
1634/*
1635 * Check if gui_resize_shell() must be called.
1636 */
1637 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001638gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (new_pixel_height)
Bram Moolenaar30613902019-12-01 22:11:18 +01001641 // careful: gui_resize_shell() may postpone the resize again if we
1642 // were called indirectly by it
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001643 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644}
1645
1646 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001647gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
1649 Rows = gui.num_rows;
1650 Columns = gui.num_cols;
1651 return OK;
1652}
1653
1654/*
1655 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001656 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1657 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001658 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1659 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001662gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001663 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001664 int fit_to_display,
Bram Moolenaar30613902019-12-01 22:11:18 +01001665 int direction) // RESIZE_HOR, RESIZE_VER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
1667 int base_width;
1668 int base_height;
1669 int width;
1670 int height;
1671 int min_width;
1672 int min_height;
1673 int screen_w;
1674 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001675#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001676 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001677 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001678#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001679 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681 if (!gui.shell_created)
1682 return;
1683
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001684#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01001685 // If not setting to a user specified size and maximized, calculate the
1686 // number of characters that fit in the maximized window.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001687 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1688 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 {
1690 gui_mch_newfont();
1691 return;
1692 }
1693#endif
1694
1695 base_width = gui_get_base_width();
1696 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001697 if (fit_to_display)
Bram Moolenaar30613902019-12-01 22:11:18 +01001698 // Remember the original window position.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001699 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001700
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001701 width = Columns * gui.char_width + base_width;
1702 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
1704 if (fit_to_display)
1705 {
1706 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001707 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 {
1709 Columns = (screen_w - base_width) / gui.char_width;
1710 if (Columns < MIN_COLUMNS)
1711 Columns = MIN_COLUMNS;
1712 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001713#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001714 ++did_adjust;
1715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001717 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 {
1719 Rows = (screen_h - base_height) / gui.char_height;
1720 check_shellsize();
1721 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001722#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001723 ++did_adjust;
1724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001726#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001727 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1728 && height + gui.char_height >= screen_h))
Bram Moolenaar30613902019-12-01 22:11:18 +01001729 // don't unmaximize if at maximum size
Bram Moolenaar09736232009-09-23 16:14:49 +00001730 un_maximize = FALSE;
1731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001733 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 gui.num_cols = Columns;
1735 gui.num_rows = Rows;
1736
1737 min_width = base_width + MIN_COLUMNS * gui.char_width;
1738 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001739 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001740
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001741#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001742 if (un_maximize)
1743 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001744 // If the window size is smaller than the screen unmaximize the
1745 // window, otherwise resizing won't work.
Bram Moolenaar09736232009-09-23 16:14:49 +00001746 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1747 if ((width + gui.char_width < screen_w
1748 || height + gui.char_height * 2 < screen_h)
1749 && gui_mch_maximized())
1750 gui_mch_unmaximize();
1751 }
1752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001755 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001757 if (fit_to_display && x >= 0 && y >= 0)
1758 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001759 // Some window managers put the Vim window left of/above the screen.
1760 // Only change the position if it wasn't already negative before
1761 // (happens on MS-Windows with a secondary monitor).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 gui_mch_update();
1763 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1764 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1765 }
1766
1767 gui_position_components(width);
1768 gui_update_scrollbars(TRUE);
1769 gui_reset_scroll_region();
1770}
1771
1772/*
1773 * Called when Rows and/or Columns has changed.
1774 */
1775 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001776gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 gui_reset_scroll_region();
1779}
1780
1781/*
1782 * Make scroll region cover whole screen.
1783 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001784 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001785gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 gui.scroll_region_top = 0;
1788 gui.scroll_region_bot = gui.num_rows - 1;
1789 gui.scroll_region_left = 0;
1790 gui.scroll_region_right = gui.num_cols - 1;
1791}
1792
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001793 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001794gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
Bram Moolenaar30613902019-12-01 22:11:18 +01001796 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 gui.highlight_mask = mask;
Bram Moolenaar30613902019-12-01 22:11:18 +01001798 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 gui.highlight_mask |= mask;
1800}
1801
1802 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001803gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804{
Bram Moolenaar30613902019-12-01 22:11:18 +01001805 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 gui.highlight_mask = HL_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001807 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 gui.highlight_mask &= ~mask;
1809}
1810
1811/*
1812 * Clear a rectangular region of the screen from text pos (row1, col1) to
1813 * (row2, col2) inclusive.
1814 */
1815 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001816gui_clear_block(
1817 int row1,
1818 int col1,
1819 int row2,
1820 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821{
Bram Moolenaar30613902019-12-01 22:11:18 +01001822 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 clip_may_clear_selection(row1, row2);
1824
1825 gui_mch_clear_block(row1, col1, row2, col2);
1826
Bram Moolenaar30613902019-12-01 22:11:18 +01001827 // Invalidate cursor if it was in this block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1829 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1830 gui.cursor_is_valid = FALSE;
1831}
1832
1833/*
1834 * Write code to update the cursor later. This avoids the need to flush the
1835 * output buffer before calling gui_update_cursor().
1836 */
1837 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001838gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001840 OUT_STR("\033|s");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841}
1842
1843 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001844gui_write(
1845 char_u *s,
1846 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847{
1848 char_u *p;
1849 int arg1 = 0, arg2 = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01001850 int force_cursor = FALSE; // force cursor update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 int force_scrollbar = FALSE;
1852 static win_T *old_curwin = NULL;
1853
Bram Moolenaar30613902019-12-01 22:11:18 +01001854// #define DEBUG_GUI_WRITE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#ifdef DEBUG_GUI_WRITE
1856 {
1857 int i;
1858 char_u *str;
1859
1860 printf("gui_write(%d):\n ", len);
1861 for (i = 0; i < len; i++)
1862 if (s[i] == ESC)
1863 {
1864 if (i != 0)
1865 printf("\n ");
1866 printf("<ESC>");
1867 }
1868 else
1869 {
1870 str = transchar_byte(s[i]);
1871 if (str[0] && str[1])
1872 printf("<%s>", (char *)str);
1873 else
1874 printf("%s", (char *)str);
1875 }
1876 printf("\n");
1877 }
1878#endif
1879 while (len)
1880 {
1881 if (s[0] == ESC && s[1] == '|')
1882 {
1883 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001884 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
1886 arg1 = getdigits(&p);
1887 if (p > s + len)
1888 break;
1889 if (*p == ';')
1890 {
1891 ++p;
1892 arg2 = getdigits(&p);
1893 if (p > s + len)
1894 break;
1895 }
1896 }
1897 switch (*p)
1898 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001899 case 'C': // Clear screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 clip_scroll_selection(9999);
1901 gui_mch_clear_all();
1902 gui.cursor_is_valid = FALSE;
1903 force_scrollbar = TRUE;
1904 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001905 case 'M': // Move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 gui_set_cursor(arg1, arg2);
1907 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001908 case 's': // force cursor (shape) update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 force_cursor = TRUE;
1910 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001911 case 'R': // Set scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (arg1 < arg2)
1913 {
1914 gui.scroll_region_top = arg1;
1915 gui.scroll_region_bot = arg2;
1916 }
1917 else
1918 {
1919 gui.scroll_region_top = arg2;
1920 gui.scroll_region_bot = arg1;
1921 }
1922 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001923 case 'V': // Set vertical scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (arg1 < arg2)
1925 {
1926 gui.scroll_region_left = arg1;
1927 gui.scroll_region_right = arg2;
1928 }
1929 else
1930 {
1931 gui.scroll_region_left = arg2;
1932 gui.scroll_region_right = arg1;
1933 }
1934 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001935 case 'd': // Delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 gui_delete_lines(gui.row, 1);
1937 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001938 case 'D': // Delete lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 gui_delete_lines(gui.row, arg1);
1940 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001941 case 'i': // Insert line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 gui_insert_lines(gui.row, 1);
1943 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001944 case 'I': // Insert lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 gui_insert_lines(gui.row, arg1);
1946 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001947 case '$': // Clear to end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 gui_clear_block(gui.row, gui.col, gui.row,
1949 (int)Columns - 1);
1950 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001951 case 'h': // Turn on highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 gui_start_highlight(arg1);
1953 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001954 case 'H': // Turn off highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 gui_stop_highlight(arg1);
1956 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001957 case 'f': // flash the window (visual bell)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1959 break;
1960 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01001961 p = s + 1; // Skip the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 break;
1963 }
1964 len -= (int)(++p - s);
1965 s = p;
1966 }
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001967 else if (s[0] < 0x20 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968#ifdef FEAT_SIGN_ICONS
1969 && s[0] != SIGN_BYTE
1970# ifdef FEAT_NETBEANS_INTG
1971 && s[0] != MULTISIGN_BYTE
1972# endif
1973#endif
1974 )
1975 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001976 if (s[0] == '\n') // NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
1978 gui.col = 0;
1979 if (gui.row < gui.scroll_region_bot)
1980 gui.row++;
1981 else
1982 gui_delete_lines(gui.scroll_region_top, 1);
1983 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001984 else if (s[0] == '\r') // CR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
1986 gui.col = 0;
1987 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001988 else if (s[0] == '\b') // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
1990 if (gui.col)
1991 --gui.col;
1992 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001993 else if (s[0] == Ctrl_L) // cursor-right
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
1995 ++gui.col;
1996 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001997 else if (s[0] == Ctrl_G) // Beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
1999 gui_mch_beep();
2000 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002001 // Other Ctrl character: shouldn't happen!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
Bram Moolenaar30613902019-12-01 22:11:18 +01002003 --len; // Skip this char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 ++s;
2005 }
2006 else
2007 {
2008 p = s;
2009 while (len > 0 && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 *p >= 0x20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011#ifdef FEAT_SIGN_ICONS
2012 || *p == SIGN_BYTE
2013# ifdef FEAT_NETBEANS_INTG
2014 || *p == MULTISIGN_BYTE
2015# endif
2016#endif
2017 ))
2018 {
2019 len--;
2020 p++;
2021 }
2022 gui_outstr(s, (int)(p - s));
2023 s = p;
2024 }
2025 }
2026
Bram Moolenaar30613902019-12-01 22:11:18 +01002027 // Postponed update of the cursor (won't work if "can_update_cursor" isn't
2028 // set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (force_cursor)
2030 gui_update_cursor(TRUE, TRUE);
2031
Bram Moolenaar30613902019-12-01 22:11:18 +01002032 // When switching to another window the dragging must have stopped.
2033 // Required for GTK, dragged_sb isn't reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (old_curwin != curwin)
2035 gui.dragged_sb = SBAR_NONE;
2036
Bram Moolenaar30613902019-12-01 22:11:18 +01002037 // Update the scrollbars after clearing the screen or when switched
2038 // to another window.
2039 // Update the horizontal scrollbar always, it's difficult to check all
2040 // situations where it might change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 if (force_scrollbar || old_curwin != curwin)
2042 gui_update_scrollbars(force_scrollbar);
2043 else
2044 gui_update_horiz_scrollbar(FALSE);
2045 old_curwin = curwin;
2046
2047 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002048 * We need to make sure this is cleared since GTK doesn't tell us when
2049 * the user is done dragging.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 */
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002051#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 gui.dragged_sb = SBAR_NONE;
2053#endif
2054
Bram Moolenaar30613902019-12-01 22:11:18 +01002055 gui_may_flush(); // In case vim decides to take a nap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056}
2057
2058/*
2059 * When ScreenLines[] is invalid, updating the cursor should not be done, it
2060 * produces wrong results. Call gui_dont_update_cursor() before that code and
2061 * gui_can_update_cursor() afterwards.
2062 */
2063 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02002064gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002066 if (!gui.in_use)
2067 return;
2068
2069 // Undraw the cursor now, we probably can't do it after the change.
2070 if (undraw)
2071 gui_undraw_cursor();
2072 can_update_cursor = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073}
2074
2075 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002076gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077{
2078 can_update_cursor = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002079 // No need to update the cursor right now, there is always more output
2080 // after scrolling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081}
2082
Bram Moolenaara338adc2018-01-31 20:51:47 +01002083/*
2084 * Disable issuing gui_mch_flush().
2085 */
2086 void
2087gui_disable_flush(void)
2088{
2089 ++disable_flush;
2090}
2091
2092/*
2093 * Enable issuing gui_mch_flush().
2094 */
2095 void
2096gui_enable_flush(void)
2097{
2098 --disable_flush;
2099}
2100
2101/*
2102 * Issue gui_mch_flush() if it is not disabled.
2103 */
2104 void
2105gui_may_flush(void)
2106{
2107 if (disable_flush == 0)
2108 gui_mch_flush();
2109}
2110
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002112gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117 if (len == 0)
2118 return;
2119
2120 if (len < 0)
2121 len = (int)STRLEN(s);
2122
2123 while (len > 0)
2124 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if (has_mbyte)
2126 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002127 // Find out how many chars fit in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 cells = 0;
2129 for (this_len = 0; this_len < len; )
2130 {
2131 cells += (*mb_ptr2cells)(s + this_len);
2132 if (gui.col + cells > Columns)
2133 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002134 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
2136 if (this_len > len)
Bram Moolenaar30613902019-12-01 22:11:18 +01002137 this_len = len; // don't include following composing char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 }
Bram Moolenaarb26592a2022-07-12 17:34:31 +01002139 else if (gui.col + len > Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 this_len = Columns - gui.col;
2141 else
2142 this_len = len;
2143
2144 (void)gui_outstr_nowrap(s, this_len,
2145 0, (guicolor_T)0, (guicolor_T)0, 0);
2146 s += this_len;
2147 len -= this_len;
Bram Moolenaar30613902019-12-01 22:11:18 +01002148 // fill up for a double-width char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 if (len > 0 && gui.col < Columns)
2150 (void)gui_outstr_nowrap((char_u *)" ", 1,
2151 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01002152 // The cursor may wrap to the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (gui.col >= Columns)
2154 {
2155 gui.col = 0;
2156 gui.row++;
2157 }
2158 }
2159}
2160
2161/*
2162 * Output one character (may be one or two display cells).
2163 * Caller must check for valid "off".
2164 * Returns FAIL or OK, just like gui_outstr_nowrap().
2165 */
2166 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002167gui_screenchar(
Bram Moolenaar30613902019-12-01 22:11:18 +01002168 int off, // Offset from start of screen
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002169 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002170 guicolor_T fg, // colors for cursor
2171 guicolor_T bg, // colors for cursor
2172 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 char_u buf[MB_MAXBYTES + 1];
2175
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002176 // Don't draw right half of a double-width UTF-8 char. "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 if (enc_utf8 && ScreenLines[off] == 0)
2178 return OK;
2179
2180 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002181 // Draw UTF-8 multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2183 flags, fg, bg, back);
2184
2185 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2186 {
2187 buf[0] = ScreenLines[off];
2188 buf[1] = ScreenLines2[off];
2189 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2190 }
2191
Bram Moolenaar30613902019-12-01 22:11:18 +01002192 // Draw non-multi-byte character or DBCS character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002194 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196}
2197
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002198#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199/*
2200 * Output the string at the given screen position. This is used in place
2201 * of gui_screenchar() where possible because Pango needs as much context
2202 * as possible to work nicely. It's a lot faster as well.
2203 */
2204 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002205gui_screenstr(
Bram Moolenaar30613902019-12-01 22:11:18 +01002206 int off, // Offset from start of screen
2207 int len, // string length in screen cells
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002208 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002209 guicolor_T fg, // colors for cursor
2210 guicolor_T bg, // colors for cursor
2211 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212{
2213 char_u *buf;
2214 int outlen = 0;
2215 int i;
2216 int retval;
2217
Bram Moolenaar30613902019-12-01 22:11:18 +01002218 if (len <= 0) // "cannot happen"?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 return OK;
2220
2221 if (enc_utf8)
2222 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002223 buf = alloc(len * MB_MAXBYTES + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002225 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 for (i = off; i < off + len; ++i)
2228 {
2229 if (ScreenLines[i] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002230 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 if (ScreenLinesUC[i] == 0)
2233 buf[outlen++] = ScreenLines[i];
2234 else
2235 outlen += utfc_char2bytes(i, buf + outlen);
2236 }
2237
Bram Moolenaar30613902019-12-01 22:11:18 +01002238 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2240 vim_free(buf);
2241
2242 return retval;
2243 }
2244 else if (enc_dbcs == DBCS_JPNU)
2245 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002246 buf = alloc(len * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002248 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250 for (i = off; i < off + len; ++i)
2251 {
2252 buf[outlen++] = ScreenLines[i];
2253
Bram Moolenaar30613902019-12-01 22:11:18 +01002254 // handle double-byte single-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (ScreenLines[i] == 0x8e)
2256 buf[outlen++] = ScreenLines2[i];
2257 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2258 buf[outlen++] = ScreenLines[++i];
2259 }
2260
Bram Moolenaar30613902019-12-01 22:11:18 +01002261 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2263 vim_free(buf);
2264
2265 return retval;
2266 }
2267 else
2268 {
2269 return gui_outstr_nowrap(&ScreenLines[off], len,
2270 flags, fg, bg, back);
2271 }
2272}
Bram Moolenaar30613902019-12-01 22:11:18 +01002273#endif // FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
2275/*
2276 * Output the given string at the current cursor position. If the string is
2277 * too long to fit on the line, then it is truncated.
2278 * "flags":
2279 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2280 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002281 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 * background.
2283 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2284 * it.
2285 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2286 * FAIL (the caller should start drawing "back" chars back).
2287 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002288 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002289gui_outstr_nowrap(
2290 char_u *s,
2291 int len,
2292 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002293 guicolor_T fg, // colors for cursor
2294 guicolor_T bg, // colors for cursor
2295 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296{
2297 long_u highlight_mask;
2298 long_u hl_mask_todo;
2299 guicolor_T fg_color;
2300 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002301 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002302#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002304 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305# ifdef FEAT_XFONTSET
2306 GuiFontset fontset = NOFONTSET;
2307# endif
2308#endif
2309 attrentry_T *aep = NULL;
2310 int draw_flags;
2311 int col = gui.col;
2312#ifdef FEAT_SIGN_ICONS
2313 int draw_sign = FALSE;
Bram Moolenaarc7283072019-07-16 20:12:44 +02002314 int signcol = 0;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002315 char_u extra[18];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316# ifdef FEAT_NETBEANS_INTG
2317 int multi_sign = FALSE;
2318# endif
2319#endif
2320
2321 if (len < 0)
2322 len = (int)STRLEN(s);
2323 if (len == 0)
2324 return OK;
2325
2326#ifdef FEAT_SIGN_ICONS
2327 if (*s == SIGN_BYTE
2328# ifdef FEAT_NETBEANS_INTG
2329 || *s == MULTISIGN_BYTE
2330# endif
Bram Moolenaar0231f832019-07-12 19:22:22 +02002331 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
John Marriottd2fcbb42025-05-07 19:59:42 +02002333 size_t slen = 2; // 2 spaces by default
2334
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335# ifdef FEAT_NETBEANS_INTG
2336 if (*s == MULTISIGN_BYTE)
2337 multi_sign = TRUE;
2338# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002339 // draw spaces instead
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002340 if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
2341 (curwin->w_p_nu || curwin->w_p_rnu))
2342 {
John Marriottd2fcbb42025-05-07 19:59:42 +02002343 int n = number_width(curwin);
2344
2345 slen = MIN(n, (int)sizeof(extra) - 1);
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002346 }
John Marriottd2fcbb42025-05-07 19:59:42 +02002347 vim_memset(extra, ' ', slen);
2348 extra[slen] = NUL;
2349 s = extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 if (len == 1 && col > 0)
2351 --col;
John Marriottd2fcbb42025-05-07 19:59:42 +02002352 len = (int)slen;
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002353 if (len > 2)
Bram Moolenaar0231f832019-07-12 19:22:22 +02002354 // right align sign icon in the number column
2355 signcol = col + len - 3;
2356 else
2357 signcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 draw_sign = TRUE;
2359 highlight_mask = 0;
2360 }
2361 else
2362#endif
2363 if (gui.highlight_mask > HL_ALL)
2364 {
2365 aep = syn_gui_attr2entry(gui.highlight_mask);
Bram Moolenaar30613902019-12-01 22:11:18 +01002366 if (aep == NULL) // highlighting not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 highlight_mask = 0;
2368 else
2369 highlight_mask = aep->ae_attr;
2370 }
2371 else
2372 highlight_mask = gui.highlight_mask;
2373 hl_mask_todo = highlight_mask;
2374
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002375#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002376 // Set the font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2378 font = aep->ae_u.gui.font;
2379# ifdef FEAT_XFONTSET
2380 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2381 fontset = aep->ae_u.gui.fontset;
2382# endif
2383 else
2384 {
2385# ifdef FEAT_XFONTSET
2386 if (gui.fontset != NOFONTSET)
2387 fontset = gui.fontset;
2388 else
2389# endif
2390 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2391 {
2392 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2393 {
2394 font = gui.boldital_font;
2395 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2396 }
2397 else if (gui.bold_font != NOFONT)
2398 {
2399 font = gui.bold_font;
2400 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2401 }
2402 else
2403 font = gui.norm_font;
2404 }
2405 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2406 {
2407 font = gui.ital_font;
2408 hl_mask_todo &= ~HL_ITALIC;
2409 }
2410 else
2411 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002412
Bram Moolenaardb250522013-06-17 22:43:25 +02002413 /*
2414 * Choose correct wide_font by font. wide_font should be set with font
2415 * at same time in above block. But it will make many "ifdef" nasty
2416 * blocks. So we do it here.
2417 */
2418 if (font == gui.boldital_font && gui.wide_boldital_font)
2419 wide_font = gui.wide_boldital_font;
2420 else if (font == gui.bold_font && gui.wide_bold_font)
2421 wide_font = gui.wide_bold_font;
2422 else if (font == gui.ital_font && gui.wide_ital_font)
2423 wide_font = gui.wide_ital_font;
2424 else if (font == gui.norm_font && gui.wide_font)
2425 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 }
2427# ifdef FEAT_XFONTSET
2428 if (fontset != NOFONTSET)
2429 gui_mch_set_fontset(fontset);
2430 else
2431# endif
2432 gui_mch_set_font(font);
2433#endif
2434
2435 draw_flags = 0;
2436
Bram Moolenaar30613902019-12-01 22:11:18 +01002437 // Set the color
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 bg_color = gui.back_pixel;
2439 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2440 {
2441 draw_flags |= DRAW_CURSOR;
2442 fg_color = fg;
2443 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002444 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
2446 else if (aep != NULL)
2447 {
2448 fg_color = aep->ae_u.gui.fg_color;
2449 if (fg_color == INVALCOLOR)
2450 fg_color = gui.norm_pixel;
2451 bg_color = aep->ae_u.gui.bg_color;
2452 if (bg_color == INVALCOLOR)
2453 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002454 sp_color = aep->ae_u.gui.sp_color;
2455 if (sp_color == INVALCOLOR)
2456 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 }
2458 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002461 sp_color = fg_color;
2462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
2464 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 gui_mch_set_fg_color(bg_color);
2467 gui_mch_set_bg_color(fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
2469 else
2470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 gui_mch_set_fg_color(fg_color);
2472 gui_mch_set_bg_color(bg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002474 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
Bram Moolenaar30613902019-12-01 22:11:18 +01002476 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (!(flags & GUI_MON_NOCLEAR))
2478 clip_may_clear_selection(gui.row, gui.row);
2479
2480
Bram Moolenaar30613902019-12-01 22:11:18 +01002481 // If there's no bold font, then fake it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2483 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 /*
2486 * When drawing bold or italic characters the spill-over from the left
2487 * neighbor may be destroyed. Let the caller backup to start redrawing
2488 * just after a blank.
2489 */
2490 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2491 return FAIL;
2492
Bram Moolenaare60acc12011-05-10 16:41:25 +02002493#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002494 // If there's no italic font, then fake it.
2495 // For GTK2, we don't need a different font for italic style.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 if (hl_mask_todo & HL_ITALIC)
2497 draw_flags |= DRAW_ITALIC;
2498
Bram Moolenaar30613902019-12-01 22:11:18 +01002499 // Do we underline the text?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 if (hl_mask_todo & HL_UNDERLINE)
2501 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503#else
Bram Moolenaar30613902019-12-01 22:11:18 +01002504 // Do we underline the text?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002505 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 draw_flags |= DRAW_UNDERL;
2507#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002508 // Do we undercurl the text?
Bram Moolenaar3918c952005-03-15 22:34:55 +00002509 if (hl_mask_todo & HL_UNDERCURL)
2510 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
Bram Moolenaar84f54632022-06-29 18:39:11 +01002512 // TODO: HL_UNDERDOUBLE, HL_UNDERDOTTED, HL_UNDERDASHED
2513
Bram Moolenaar30613902019-12-01 22:11:18 +01002514 // Do we strikethrough the text?
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002515 if (hl_mask_todo & HL_STRIKETHROUGH)
2516 draw_flags |= DRAW_STRIKE;
2517
Bram Moolenaar30613902019-12-01 22:11:18 +01002518 // Do we draw transparently?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 if (flags & GUI_MON_TRS_CURSOR)
2520 draw_flags |= DRAW_TRANSP;
2521
2522 /*
2523 * Draw the text.
2524 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002525#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01002526 // The value returned is the length in display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2528#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 if (enc_utf8)
2530 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002531 int start; // index of bytes to be drawn
2532 int cells; // cellwidth of bytes to be drawn
2533 int thislen; // length of bytes to be drawn
2534 int cn; // cellwidth of current char
2535 int i; // index of current char
2536 int c; // current char value
2537 int cl; // byte length of current char
2538 int comping; // current char is composing
2539 int scol = col; // screen column
2540 int curr_wide = FALSE; // use 'guifontwide'
Bram Moolenaar45933962013-01-23 17:43:57 +01002541 int prev_wide = FALSE;
2542 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002543# ifdef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01002544 int sep_comp = FALSE; // Don't separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002545# else
Bram Moolenaar30613902019-12-01 22:11:18 +01002546 int sep_comp = TRUE; // Separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
Bram Moolenaar30613902019-12-01 22:11:18 +01002549 // Break the string at a composing character, it has to be drawn on
2550 // top of the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 start = 0;
2552 cells = 0;
2553 for (i = 0; i < len; i += cl)
2554 {
2555 c = utf_ptr2char(s + i);
2556 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 comping = utf_iscomposing(c);
Bram Moolenaar30613902019-12-01 22:11:18 +01002558 if (!comping) // count cells from non-composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002560 if (!comping || sep_comp)
2561 {
2562 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002563# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002564 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002565# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002566 && wide_font != NOFONT)
2567 curr_wide = TRUE;
2568 else
2569 curr_wide = FALSE;
2570 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002571 cl = utf_ptr2len(s + i);
Bram Moolenaar30613902019-12-01 22:11:18 +01002572 if (cl == 0) // hit end of string
2573 len = i + cl; // len must be wrong "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
Bram Moolenaar45933962013-01-23 17:43:57 +01002575 wide_changed = curr_wide != prev_wide;
2576
Bram Moolenaar30613902019-12-01 22:11:18 +01002577 // Print the string so far if it's the last character or there is
2578 // a composing character.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002579 if (i + cl >= len || (comping && sep_comp && i > start)
2580 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002581# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002583# ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +01002584 // No fontset: At least draw char after wide char at
2585 // right position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002588 )
2589# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 )
2591 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002592 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 thislen = i - start;
2594 else
2595 thislen = i - start + cl;
2596 if (thislen > 0)
2597 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002598 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002599 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2601 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002602 if (prev_wide)
2603 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 start += thislen;
2605 }
2606 scol += cells;
2607 cells = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01002608 // Adjust to not draw a character which width is changed
2609 // against with last one.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002610 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002612 scol -= cn;
2613 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 }
2615
Bram Moolenaar13505972019-01-24 15:04:48 +01002616# if defined(FEAT_GUI_X11)
Bram Moolenaar30613902019-12-01 22:11:18 +01002617 // No fontset: draw a space to fill the gap after a wide char
2618 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002620# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002622# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002623 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2625 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002626# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002628 // Draw a composing char on top of the previous char.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002629 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002631# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaar30613902019-12-01 22:11:18 +01002632 // Carbon ATSUI autodraws composing char over previous char
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002633 gui_mch_draw_string(gui.row, scol, s + i, cl,
2634 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002635# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002636 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2637 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002638# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 start = i + cl;
2640 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002641 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002643 // The stuff below assumes "len" is the length in screen columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 len = scol - col;
2645 }
2646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 {
2648 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (enc_dbcs == DBCS_JPNU)
2650 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002651 // Get the length in display cells, this can be different from the
2652 // number of bytes for "euc-jp".
Bram Moolenaar72597a52010-07-18 15:31:08 +02002653 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002656#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2659 gui.col = col + len;
2660
Bram Moolenaar30613902019-12-01 22:11:18 +01002661 // May need to invert it when it's part of the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (flags & GUI_MON_NOCLEAR)
2663 clip_may_redraw_selection(gui.row, col, len);
2664
2665 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2666 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002667 // Invalidate the old physical cursor position if we wrote over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (gui.cursor_row == gui.row
2669 && gui.cursor_col >= col
2670 && gui.cursor_col < col + len)
2671 gui.cursor_is_valid = FALSE;
2672 }
2673
2674#ifdef FEAT_SIGN_ICONS
2675 if (draw_sign)
Bram Moolenaar30613902019-12-01 22:11:18 +01002676 // Draw the sign on top of the spaces.
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002677 gui_mch_drawsign(gui.row, signcol, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002678# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002679 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (multi_sign)
2681 netbeans_draw_multisign_indicator(gui.row);
2682# endif
2683#endif
2684
2685 return OK;
2686}
2687
2688/*
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002689 * Undraw the cursor. This actually redraws the character at the cursor
2690 * position, plus some more characters when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 */
2692 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002693gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002695 if (!gui.cursor_is_valid)
2696 return;
2697
2698 // Always redraw the character just before if there is one, because
2699 // with some fonts and characters there can be a one pixel overlap.
2700 int startcol = gui.cursor_col > 0 ? gui.cursor_col - 1 : gui.cursor_col;
2701 int endcol = gui.cursor_col;
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002702
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002703#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002704 gui_adjust_undraw_cursor_for_ligatures(&startcol, &endcol);
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002705#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002706 gui_redraw_block(gui.cursor_row, startcol,
2707 gui.cursor_row, endcol, GUI_MON_NOCLEAR);
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002708
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002709 // Cursor_is_valid is reset when the cursor is undrawn, also reset it
2710 // here in case it wasn't needed to undraw it.
2711 gui.cursor_is_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712}
2713
2714 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002715gui_redraw(
2716 int x,
2717 int y,
2718 int w,
2719 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
2721 int row1, col1, row2, col2;
2722
2723 row1 = Y_2_ROW(y);
2724 col1 = X_2_COL(x);
2725 row2 = Y_2_ROW(y + h - 1);
2726 col2 = X_2_COL(x + w - 1);
2727
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002728 gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
2730 /*
2731 * We may need to redraw the cursor, but don't take it upon us to change
2732 * its location after a scroll.
2733 * (maybe be more strict even and test col too?)
2734 * These things may be outside the update/clipping region and reality may
2735 * not reflect Vims internal ideas if these operations are clipped away.
2736 */
2737 if (gui.row == gui.cursor_row)
2738 gui_update_cursor(TRUE, TRUE);
2739}
2740
2741/*
2742 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2743 * from col1 to col2 (inclusive).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 */
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002745 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002746gui_redraw_block(
2747 int row1,
2748 int col1,
2749 int row2,
2750 int col2,
Bram Moolenaar30613902019-12-01 22:11:18 +01002751 int flags) // flags for gui_outstr_nowrap()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752{
2753 int old_row, old_col;
2754 long_u old_hl_mask;
2755 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002756 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 int idx, len;
2758 int back, nback;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
Bram Moolenaar30613902019-12-01 22:11:18 +01002761 // Don't try to update when ScreenLines is not valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 if (!screen_cleared || ScreenLines == NULL)
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002763 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
Bram Moolenaar30613902019-12-01 22:11:18 +01002765 // Don't try to draw outside the shell!
2766 // Check everything, strange values may be caused by a big border width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 col1 = check_col(col1);
2768 col2 = check_col(col2);
2769 row1 = check_row(row1);
2770 row2 = check_row(row2);
2771
Bram Moolenaar30613902019-12-01 22:11:18 +01002772 // Remember where our cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 old_row = gui.row;
2774 old_col = gui.col;
2775 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 orig_col1 = col1;
2777 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
2779 for (gui.row = row1; gui.row <= row2; gui.row++)
2780 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002781 // When only half of a double-wide character is in the block, include
2782 // the other half.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 col1 = orig_col1;
2784 col2 = orig_col2;
2785 off = LineOffset[gui.row];
2786 if (enc_dbcs != 0)
2787 {
2788 if (col1 > 0)
2789 col1 -= dbcs_screen_head_off(ScreenLines + off,
2790 ScreenLines + off + col1);
2791 col2 += dbcs_screen_tail_off(ScreenLines + off,
2792 ScreenLines + off + col2);
2793 }
2794 else if (enc_utf8)
2795 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002796 if (ScreenLines[off + col1] == 0)
2797 {
2798 if (col1 > 0)
2799 --col1;
2800 else
2801 // FIXME: how can the first character ever be zero?
Bram Moolenaar097c5372023-05-24 21:02:24 +01002802 siemsg("NUL in ScreenLines in row %ld", (long)gui.row);
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002803 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002804#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2806 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 gui.col = col1;
2810 off = LineOffset[gui.row] + gui.col;
2811 len = col2 - col1 + 1;
2812
Bram Moolenaar30613902019-12-01 22:11:18 +01002813 // Find how many chars back this highlighting starts, or where a space
2814 // is. Needed for when the bold trick is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 for (back = 0; back < col1; ++back)
2816 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2817 || ScreenLines[off - 1 - back] == ' ')
2818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819
Bram Moolenaar30613902019-12-01 22:11:18 +01002820 // Break it up in strings of characters with the same attributes.
2821 // Print UTF-8 characters individually.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 while (len > 0)
2823 {
2824 first_attr = ScreenAttrs[off];
2825 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002826#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 if (enc_utf8 && ScreenLinesUC[off] != 0)
2828 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002829 // output multi-byte character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 nback = gui_screenchar(off, flags,
2831 (guicolor_T)0, (guicolor_T)0, back);
2832 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2833 idx = 2;
2834 else
2835 idx = 1;
2836 }
2837 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2838 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002839 // output double-byte, single-width character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 nback = gui_screenchar(off, flags,
2841 (guicolor_T)0, (guicolor_T)0, back);
2842 idx = 1;
2843 }
2844 else
2845#endif
2846 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002847#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 for (idx = 0; idx < len; ++idx)
2849 {
2850 if (enc_utf8 && ScreenLines[off + idx] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002851 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 if (ScreenAttrs[off + idx] != first_attr)
2853 break;
2854 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002855 // gui_screenstr() takes care of multibyte chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 nback = gui_screenstr(off, idx, flags,
2857 (guicolor_T)0, (guicolor_T)0, back);
2858#else
2859 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2860 idx++)
2861 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002862 // Stop at a multi-byte Unicode character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2864 break;
2865 if (enc_dbcs == DBCS_JPNU)
2866 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002867 // Stop at a double-byte single-width char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 if (ScreenLines[off + idx] == 0x8e)
2869 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002870 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 + off + idx) == 2)
Bram Moolenaar30613902019-12-01 22:11:18 +01002872 ++idx; // skip second byte of double-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2876 (guicolor_T)0, (guicolor_T)0, back);
2877#endif
2878 }
2879 if (nback == FAIL)
2880 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002881 // Must back up to start drawing where a bold or italic word
2882 // starts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 off -= back;
2884 len += back;
2885 gui.col -= back;
2886 }
2887 else
2888 {
2889 off += idx;
2890 len -= idx;
2891 }
2892 back = 0;
2893 }
2894 }
2895
Bram Moolenaar30613902019-12-01 22:11:18 +01002896 // Put the cursor back where it was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 gui.row = old_row;
2898 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002899 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900}
2901
2902 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002903gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904{
2905 if (count <= 0)
2906 return;
2907
2908 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002909 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 gui_clear_block(row, gui.scroll_region_left,
2911 gui.scroll_region_bot, gui.scroll_region_right);
2912 else
2913 {
2914 gui_mch_delete_lines(row, count);
2915
Bram Moolenaar30613902019-12-01 22:11:18 +01002916 // If the cursor was in the deleted lines it's now gone. If the
2917 // cursor was in the scrolled lines adjust its position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 if (gui.cursor_row >= row
2919 && gui.cursor_col >= gui.scroll_region_left
2920 && gui.cursor_col <= gui.scroll_region_right)
2921 {
2922 if (gui.cursor_row < row + count)
2923 gui.cursor_is_valid = FALSE;
2924 else if (gui.cursor_row <= gui.scroll_region_bot)
2925 gui.cursor_row -= count;
2926 }
2927 }
2928}
2929
2930 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002931gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932{
2933 if (count <= 0)
2934 return;
2935
2936 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002937 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 gui_clear_block(row, gui.scroll_region_left,
2939 gui.scroll_region_bot, gui.scroll_region_right);
2940 else
2941 {
2942 gui_mch_insert_lines(row, count);
2943
2944 if (gui.cursor_row >= gui.row
2945 && gui.cursor_col >= gui.scroll_region_left
2946 && gui.cursor_col <= gui.scroll_region_right)
2947 {
2948 if (gui.cursor_row <= gui.scroll_region_bot - count)
2949 gui.cursor_row += count;
2950 else if (gui.cursor_row <= gui.scroll_region_bot)
2951 gui.cursor_is_valid = FALSE;
2952 }
2953 }
2954}
2955
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002956#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002957/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002958 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2959 */
2960 static int
2961gui_wait_for_chars_3(
2962 long wtime,
2963 int *interrupted UNUSED,
2964 int ignore_input UNUSED)
2965{
2966 return gui_mch_wait_for_chars(wtime);
2967}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002968#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002969
2970/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002971 * Returns OK if a character was found to be available within the given time,
2972 * or FAIL otherwise.
2973 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002974 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002975gui_wait_for_chars_or_timer(
2976 long wtime,
2977 int *interrupted UNUSED,
2978 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002979{
2980#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002981 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2982 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002983#else
2984 return gui_mch_wait_for_chars(wtime);
2985#endif
2986}
2987
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988/*
2989 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002990 * "wtime" == -1 Wait forever.
2991 * "wtime" == 0 Don't wait.
2992 * "wtime" > 0 Wait wtime milliseconds for a character.
2993 *
2994 * Returns the number of characters read or zero when timed out or interrupted.
2995 * "buf" may be NULL, in which case a non-zero number is returned if characters
2996 * are available.
2997 */
2998 static int
2999gui_wait_for_chars_buf(
3000 char_u *buf,
3001 int maxlen,
3002 long wtime, // don't use "time", MIPS cannot handle it
3003 int tb_change_cnt)
3004{
3005 int retval;
3006
3007#ifdef FEAT_MENU
3008 // If we're going to wait a bit, update the menus and mouse shape for the
3009 // current State.
3010 if (wtime != 0)
3011 gui_update_menus(0);
3012#endif
3013
3014 gui_mch_update();
3015 if (input_available()) // Got char, return immediately
3016 {
3017 if (buf != NULL && !typebuf_changed(tb_change_cnt))
3018 return read_from_input_buf(buf, (long)maxlen);
3019 return 0;
3020 }
3021 if (wtime == 0) // Don't wait for char
3022 return FAIL;
3023
3024 // Before waiting, flush any output to the screen.
3025 gui_mch_flush();
3026
3027 // Blink while waiting for a character.
3028 gui_mch_start_blink();
3029
3030 // Common function to loop until "wtime" is met, while handling timers and
3031 // other callbacks.
3032 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
3033 gui_wait_for_chars_or_timer, NULL);
3034
3035 gui_mch_stop_blink(TRUE);
3036
3037 return retval;
3038}
3039
3040/*
3041 * Wait for a character from the keyboard without actually reading it.
3042 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 * wtime == -1 Wait forever.
3044 * wtime == 0 Don't wait.
3045 * wtime > 0 Wait wtime milliseconds for a character.
3046 * Returns OK if a character was found to be available within the given time,
3047 * or FAIL otherwise.
3048 */
3049 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003050gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003052 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053}
3054
3055/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003056 * Equivalent of mch_inchar() for the GUI.
3057 */
3058 int
3059gui_inchar(
3060 char_u *buf,
3061 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003062 long wtime, // milliseconds
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003063 int tb_change_cnt)
3064{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003065 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003066}
3067
3068/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003069 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 */
3071 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003072fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 p[0] = (char_u)(col / 128 + ' ' + 1);
3075 p[1] = (char_u)(col % 128 + ' ' + 1);
3076 p[2] = (char_u)(row / 128 + ' ' + 1);
3077 p[3] = (char_u)(row % 128 + ' ' + 1);
3078}
3079
3080/*
3081 * Generic mouse support function. Add a mouse event to the input buffer with
3082 * the given properties.
3083 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3084 * MOUSE_X1, MOUSE_X2
3085 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003086 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3087 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 * x, y --- Coordinates of mouse in pixels.
3089 * repeated_click --- TRUE if this click comes only a short time after a
3090 * previous click.
3091 * modifiers --- Bit field which may be any of the following modifiers
3092 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3093 * This function will ignore drag events where the mouse has not moved to a new
3094 * character.
3095 */
3096 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003097gui_send_mouse_event(
3098 int button,
3099 int x,
3100 int y,
3101 int repeated_click,
3102 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
3104 static int prev_row = 0, prev_col = 0;
3105 static int prev_button = -1;
3106 static int num_clicks = 1;
3107 char_u string[10];
3108 enum key_extra button_char;
3109 int row, col;
3110#ifdef FEAT_CLIPBOARD
3111 int checkfor;
3112 int did_clip = FALSE;
3113#endif
3114
3115 /*
3116 * Scrolling may happen at any time, also while a selection is present.
3117 */
3118 switch (button)
3119 {
Bram Moolenaar445f11d2021-06-08 20:13:31 +02003120 case MOUSE_MOVE:
3121 button_char = KE_MOUSEMOVE_XY;
3122 goto button_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 case MOUSE_X1:
3124 button_char = KE_X1MOUSE;
3125 goto button_set;
3126 case MOUSE_X2:
3127 button_char = KE_X2MOUSE;
3128 goto button_set;
3129 case MOUSE_4:
3130 button_char = KE_MOUSEDOWN;
3131 goto button_set;
3132 case MOUSE_5:
3133 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003134 goto button_set;
3135 case MOUSE_6:
3136 button_char = KE_MOUSELEFT;
3137 goto button_set;
3138 case MOUSE_7:
3139 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140button_set:
3141 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003142 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 if (hold_gui_events)
3144 return;
3145
Ernie Raelc4cb5442022-04-03 15:47:28 +01003146 row = gui_xy2colrow(x, y, &col);
3147 // Don't report a mouse move unless moved to a
3148 // different character position.
3149 if (button == MOUSE_MOVE)
3150 {
3151 if (row == prev_row && col == prev_col)
3152 return;
3153 else
3154 {
3155 prev_row = row >= 0 ? row : 0;
3156 prev_col = col;
3157 }
3158 }
3159
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 string[3] = CSI;
3161 string[4] = KS_EXTRA;
3162 string[5] = (int)button_char;
3163
Bram Moolenaar30613902019-12-01 22:11:18 +01003164 // Pass the pointer coordinates of the scroll event so that we
3165 // know which window to scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 string[6] = (char_u)(col / 128 + ' ' + 1);
3167 string[7] = (char_u)(col % 128 + ' ' + 1);
3168 string[8] = (char_u)(row / 128 + ' ' + 1);
3169 string[9] = (char_u)(row % 128 + ' ' + 1);
3170
3171 if (modifiers == 0)
3172 add_to_input_buf(string + 3, 7);
3173 else
3174 {
3175 string[0] = CSI;
3176 string[1] = KS_MODIFIER;
3177 string[2] = 0;
3178 if (modifiers & MOUSE_SHIFT)
3179 string[2] |= MOD_MASK_SHIFT;
3180 if (modifiers & MOUSE_CTRL)
3181 string[2] |= MOD_MASK_CTRL;
3182 if (modifiers & MOUSE_ALT)
3183 string[2] |= MOD_MASK_ALT;
3184 add_to_input_buf(string, 10);
3185 }
3186 return;
3187 }
3188 }
3189
3190#ifdef FEAT_CLIPBOARD
Bram Moolenaar30613902019-12-01 22:11:18 +01003191 // If a clipboard selection is in progress, handle it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 if (clip_star.state == SELECT_IN_PROGRESS)
3193 {
3194 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
Bram Moolenaar0ce37332019-12-18 21:33:22 +01003195
3196 // A release event may still need to be sent if the position is equal.
3197 row = gui_xy2colrow(x, y, &col);
3198 if (button != MOUSE_RELEASE || row != prev_row || col != prev_col)
3199 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201
Bram Moolenaar30613902019-12-01 22:11:18 +01003202 // Determine which mouse settings to look for based on the current mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 switch (get_real_state())
3204 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003205 case MODE_NORMAL_BUSY:
3206 case MODE_OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003207# ifdef FEAT_TERMINAL
Bram Moolenaar24959102022-05-07 20:01:16 +01003208 case MODE_TERMINAL:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003209# endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003210 case MODE_NORMAL: checkfor = MOUSE_NORMAL; break;
3211 case MODE_VISUAL: checkfor = MOUSE_VISUAL; break;
3212 case MODE_SELECT: checkfor = MOUSE_VISUAL; break;
3213 case MODE_REPLACE:
3214 case MODE_REPLACE | MODE_LANGMAP:
3215 case MODE_VREPLACE:
3216 case MODE_VREPLACE | MODE_LANGMAP:
3217 case MODE_INSERT:
3218 case MODE_INSERT | MODE_LANGMAP:
3219 checkfor = MOUSE_INSERT; break;
3220 case MODE_ASKMORE:
3221 case MODE_HITRETURN: // At the more- and hit-enter prompt pass the
Bram Moolenaar30613902019-12-01 22:11:18 +01003222 // mouse event for a click on or below the
3223 // message line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 if (Y_2_ROW(y) >= msg_row)
3225 checkfor = MOUSE_NORMAL;
3226 else
3227 checkfor = MOUSE_RETURN;
3228 break;
3229
3230 /*
3231 * On the command line, use the clipboard selection on all lines
3232 * but the command line. But not when pasting.
3233 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003234 case MODE_CMDLINE:
3235 case MODE_CMDLINE | MODE_LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3237 checkfor = MOUSE_NONE;
3238 else
3239 checkfor = MOUSE_COMMAND;
3240 break;
3241
3242 default:
3243 checkfor = MOUSE_NONE;
3244 break;
3245 };
3246
3247 /*
3248 * Allow clipboard selection of text on the command line in "normal"
3249 * modes. Don't do this when dragging the status line, or extending a
3250 * Visual selection.
3251 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003252 if ((State == MODE_NORMAL || State == MODE_NORMAL_BUSY
3253 || (State & MODE_INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003254 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 && button != MOUSE_DRAG
3256# ifdef FEAT_MOUSESHAPE
3257 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259# endif
3260 )
3261 checkfor = MOUSE_NONE;
3262
3263 /*
3264 * Use modeless selection when holding CTRL and SHIFT pressed.
3265 */
3266 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3267 checkfor = MOUSE_NONEF;
3268
3269 /*
3270 * In Ex mode, always use modeless selection.
3271 */
3272 if (exmode_active)
3273 checkfor = MOUSE_NONE;
3274
3275 /*
3276 * If the mouse settings say to not use the mouse, use the modeless
3277 * selection. But if Visual is active, assume that only the Visual area
3278 * will be selected.
3279 * Exception: On the command line, both the selection is used and a mouse
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003280 * key is sent.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 */
3282 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3283 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003284 // Don't do modeless selection in Visual mode.
Bram Moolenaar24959102022-05-07 20:01:16 +01003285 if (checkfor != MOUSE_NONEF && VIsual_active && (State & MODE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
3288 /*
3289 * When 'mousemodel' is "popup", shift-left is translated to right.
3290 * But not when also using Ctrl.
3291 */
3292 if (mouse_model_popup() && button == MOUSE_LEFT
3293 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3294 {
3295 button = MOUSE_RIGHT;
3296 modifiers &= ~ MOUSE_SHIFT;
3297 }
3298
Bram Moolenaar30613902019-12-01 22:11:18 +01003299 // If the selection is done, allow the right button to extend it.
3300 // If the selection is cleared, allow the right button to start it
3301 // from the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 if (button == MOUSE_RIGHT)
3303 {
3304 if (clip_star.state == SELECT_CLEARED)
3305 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003306 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 {
3308 col = msg_col;
3309 row = msg_row;
3310 }
3311 else
3312 {
3313 col = curwin->w_wcol;
3314 row = curwin->w_wrow + W_WINROW(curwin);
3315 }
3316 clip_start_selection(col, row, FALSE);
3317 }
3318 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3319 repeated_click);
3320 did_clip = TRUE;
3321 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003322 // Allow the left button to start the selection
Bram Moolenaare60acc12011-05-10 16:41:25 +02003323 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
3325 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3326 did_clip = TRUE;
3327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar30613902019-12-01 22:11:18 +01003329 // Always allow pasting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 if (button != MOUSE_MIDDLE)
3331 {
3332 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3333 return;
3334 if (checkfor != MOUSE_COMMAND)
3335 button = MOUSE_LEFT;
3336 }
3337 repeated_click = FALSE;
3338 }
3339
3340 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003341 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342#endif
3343
Bram Moolenaar30613902019-12-01 22:11:18 +01003344 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (hold_gui_events)
3346 return;
3347
3348 row = gui_xy2colrow(x, y, &col);
3349
3350 /*
3351 * If we are dragging and the mouse hasn't moved far enough to be on a
3352 * different character, then don't send an event to vim.
3353 */
3354 if (button == MOUSE_DRAG)
3355 {
3356 if (row == prev_row && col == prev_col)
3357 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01003358 // Dragging above the window, set "row" to -1 to cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (y < 0)
3360 row = -1;
3361 }
3362
3363 /*
3364 * If topline has changed (window scrolled) since the last click, reset
3365 * repeated_click, because we don't want starting Visual mode when
3366 * clicking on a different character in the text.
3367 */
3368 if (curwin->w_topline != gui_prev_topline
3369#ifdef FEAT_DIFF
3370 || curwin->w_topfill != gui_prev_topfill
3371#endif
3372 )
3373 repeated_click = FALSE;
3374
Bram Moolenaar30613902019-12-01 22:11:18 +01003375 string[0] = CSI; // this sequence is recognized by check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 string[1] = KS_MOUSE;
3377 string[2] = KE_FILLER;
3378 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3379 {
3380 if (repeated_click)
3381 {
3382 /*
3383 * Handle multiple clicks. They only count if the mouse is still
3384 * pointing at the same character.
3385 */
3386 if (button != prev_button || row != prev_row || col != prev_col)
3387 num_clicks = 1;
3388 else if (++num_clicks > 4)
3389 num_clicks = 1;
3390 }
3391 else
3392 num_clicks = 1;
3393 prev_button = button;
3394 gui_prev_topline = curwin->w_topline;
3395#ifdef FEAT_DIFF
3396 gui_prev_topfill = curwin->w_topfill;
3397#endif
3398
3399 string[3] = (char_u)(button | 0x20);
3400 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3401 }
3402 else
3403 string[3] = (char_u)button;
3404
3405 string[3] |= modifiers;
3406 fill_mouse_coord(string + 4, col, row);
3407 add_to_input_buf(string, 8);
3408
3409 if (row < 0)
3410 prev_row = 0;
3411 else
3412 prev_row = row;
3413 prev_col = col;
3414
3415 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01003416 * We need to make sure this is cleared since GTK doesn't tell us when
3417 * the user is done dragging.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 */
Bram Moolenaar0b962e52022-04-03 18:02:37 +01003419#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 gui.dragged_sb = SBAR_NONE;
3421#endif
3422}
3423
3424/*
3425 * Convert x and y coordinate to column and row in text window.
3426 * Corrects for multi-byte character.
3427 * returns column in "*colp" and row as return value;
3428 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003429 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003430gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
3432 int col = check_col(X_2_COL(x));
3433 int row = check_row(Y_2_ROW(y));
3434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 return row;
3437}
3438
3439#if defined(FEAT_MENU) || defined(PROTO)
3440/*
3441 * Callback function for when a menu entry has been selected.
3442 */
3443 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003444gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003446 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
Bram Moolenaar30613902019-12-01 22:11:18 +01003448 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (hold_gui_events)
3450 return;
3451
3452 bytes[0] = CSI;
3453 bytes[1] = KS_MENU;
3454 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003455 add_to_input_buf(bytes, 3);
3456 add_long_to_buf((long_u)menu, bytes);
3457 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458}
3459#endif
3460
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003461static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003462
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463/*
3464 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003465 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 * in p_go.
3467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003469gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470{
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003471#ifdef FEAT_GUI_DARKTHEME
3472 static int prev_dark_theme = -1;
3473 int using_dark_theme = FALSE;
3474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475#ifdef FEAT_MENU
3476 static int prev_menu_is_active = -1;
3477#endif
3478#ifdef FEAT_TOOLBAR
3479 static int prev_toolbar = -1;
3480 int using_toolbar = FALSE;
3481#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003482#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003483 int using_tabline;
3484#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003485#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 static int prev_tearoff = -1;
3487 int using_tearoff = FALSE;
3488#endif
3489
3490 char_u *p;
3491 int i;
3492#ifdef FEAT_MENU
3493 int grey_old, grey_new;
3494 char_u *temp;
3495#endif
3496 win_T *wp;
3497 int need_set_size;
3498 int fix_size;
3499
3500#ifdef FEAT_MENU
3501 if (oldval != NULL && gui.in_use)
3502 {
3503 /*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003504 * Check if the menus go from grey to non-grey or vice versa.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 */
3506 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3507 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3508 if (grey_old != grey_new)
3509 {
3510 temp = p_go;
3511 p_go = oldval;
3512 gui_update_menus(MENU_ALL_MODES);
3513 p_go = temp;
3514 }
3515 }
3516 gui.menu_is_active = FALSE;
3517#endif
3518
3519 for (i = 0; i < 3; i++)
3520 gui.which_scrollbars[i] = FALSE;
3521 for (p = p_go; *p; p++)
3522 switch (*p)
3523 {
3524 case GO_LEFT:
3525 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3526 break;
3527 case GO_RIGHT:
3528 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3529 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 case GO_VLEFT:
3531 if (win_hasvertsplit())
3532 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3533 break;
3534 case GO_VRIGHT:
3535 if (win_hasvertsplit())
3536 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 case GO_BOT:
3539 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3540 break;
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003541#ifdef FEAT_GUI_DARKTHEME
3542 case GO_DARKTHEME:
3543 using_dark_theme = TRUE;
3544 break;
3545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546#ifdef FEAT_MENU
3547 case GO_MENUS:
3548 gui.menu_is_active = TRUE;
3549 break;
3550#endif
3551 case GO_GREY:
Bram Moolenaar30613902019-12-01 22:11:18 +01003552 // make menu's have grey items, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 break;
3554#ifdef FEAT_TOOLBAR
3555 case GO_TOOLBAR:
3556 using_toolbar = TRUE;
3557 break;
3558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003560#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 using_tearoff = TRUE;
3562#endif
3563 break;
3564 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01003565 // Ignore options that are not supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 break;
3567 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003568
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003569 if (!gui.in_use)
3570 return;
3571
3572 need_set_size = 0;
3573 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003574
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003575#ifdef FEAT_GUI_DARKTHEME
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003576 if (using_dark_theme != prev_dark_theme)
3577 {
3578 gui_mch_set_dark_theme(using_dark_theme);
3579 prev_dark_theme = using_dark_theme;
3580 }
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003581#endif
3582
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003583#ifdef FEAT_GUI_TABLINE
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003584 // Update the GUI tab line, it may appear or disappear. This may
3585 // cause the non-GUI tab line to disappear or appear.
3586 using_tabline = gui_has_tabline();
3587 if (!gui_mch_showing_tabline() != !using_tabline)
3588 {
3589 // We don't want a resize event change "Rows" here, save and
3590 // restore it. Resizing is handled below.
3591 i = Rows;
3592 gui_update_tabline();
3593 Rows = i;
3594 need_set_size |= RESIZE_VERT;
3595 if (using_tabline)
3596 fix_size = TRUE;
3597 if (!gui_use_tabline())
3598 redraw_tabline = TRUE; // may draw non-GUI tab line
3599 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003600#endif
3601
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003602 for (i = 0; i < 3; i++)
3603 {
3604 // The scrollbar needs to be updated when it is shown/unshown and
3605 // when switching tab pages. But the size only changes when it's
3606 // shown/unshown. Thus we need two places to remember whether a
3607 // scrollbar is there or not.
3608 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3609 || gui.which_scrollbars[i]
3610 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003612 if (i == SBAR_BOTTOM)
3613 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3614 gui.which_scrollbars[i]);
3615 else
3616 {
3617 FOR_ALL_WINDOWS(wp)
3618 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3619 }
3620 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 if (i == SBAR_BOTTOM)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003623 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 else
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003625 need_set_size |= RESIZE_HOR;
3626 if (gui.which_scrollbars[i])
3627 fix_size = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003630 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3631 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634#ifdef FEAT_MENU
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003635 if (gui.menu_is_active != prev_menu_is_active)
3636 {
3637 // We don't want a resize event change "Rows" here, save and
3638 // restore it. Resizing is handled below.
3639 i = Rows;
3640 gui_mch_enable_menu(gui.menu_is_active);
3641 Rows = i;
3642 prev_menu_is_active = gui.menu_is_active;
3643 need_set_size |= RESIZE_VERT;
3644 if (gui.menu_is_active)
3645 fix_size = TRUE;
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647#endif
3648
3649#ifdef FEAT_TOOLBAR
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003650 if (using_toolbar != prev_toolbar)
3651 {
3652 gui_mch_show_toolbar(using_toolbar);
3653 prev_toolbar = using_toolbar;
3654 need_set_size |= RESIZE_VERT;
3655 if (using_toolbar)
3656 fix_size = TRUE;
3657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003659#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003660 if (using_tearoff != prev_tearoff)
3661 {
3662 gui_mch_toggle_tearoffs(using_tearoff);
3663 prev_tearoff = using_tearoff;
3664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003666 if (need_set_size != 0)
3667 {
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003668#ifdef FEAT_GUI_GTK
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003669 long prev_Columns = Columns;
3670 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003671#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003672 // Adjust the size of the window to make the text area keep the
3673 // same size and to avoid that part of our window is off-screen
3674 // and a scrollbar can't be used, for example.
3675 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003676
3677#ifdef FEAT_GUI_GTK
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003678 // GTK has the annoying habit of sending us resize events when
3679 // changing the window size ourselves. This mostly happens when
3680 // waiting for a character to arrive, quite unpredictably, and may
3681 // change Columns and Rows when we don't want it. Wait for a
3682 // character here to avoid this effect.
3683 // If you remove this, please test this command for resizing
3684 // effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3685 // Don't do this while starting up though.
3686 // Don't change Rows when adding menu/toolbar/tabline.
3687 // Don't change Columns when adding vertical toolbar.
3688 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
3689 (void)char_avail();
3690 if ((need_set_size & RESIZE_VERT) == 0)
3691 Rows = prev_Rows;
3692 if ((need_set_size & RESIZE_HOR) == 0)
3693 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003696 // When the console tabline appears or disappears the window positions
3697 // change.
3698 if (firstwin->w_winrow != tabline_height())
3699 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700}
3701
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003702#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3703/*
3704 * Return TRUE if the GUI is taking care of the tabline.
3705 * It may still be hidden if 'showtabline' is zero.
3706 */
3707 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003708gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003709{
3710 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3711}
3712
3713/*
3714 * Return TRUE if the GUI is showing the tabline.
3715 * This uses 'showtabline'.
3716 */
3717 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003718gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003719{
3720 if (!gui_use_tabline()
3721 || p_stal == 0
3722 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3723 return FALSE;
3724 return TRUE;
3725}
3726
3727/*
3728 * Update the tabline.
3729 * This may display/undisplay the tabline and update the labels.
3730 */
3731 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003732gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003733{
3734 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003735 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003736
3737 if (!gui.starting && starting == 0)
3738 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003739 // Updating the tabline uses direct GUI commands, flush
3740 // outstanding instructions first. (esp. clear screen)
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003741 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003742
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003743 if (!showit != !shown)
3744 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003745 if (showit != 0)
3746 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003747
Bram Moolenaar30613902019-12-01 22:11:18 +01003748 // When the tabs change from hidden to shown or from shown to
3749 // hidden the size of the text area should remain the same.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003750 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003751 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003752 }
3753}
3754
3755/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003756 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003757 */
3758 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003759get_tabline_label(
3760 tabpage_T *tp,
Bram Moolenaar30613902019-12-01 22:11:18 +01003761 int tooltip) // TRUE: get tooltip
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003762{
Bram Moolenaard68071d2006-05-02 22:08:30 +00003763 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003764
Bram Moolenaar30613902019-12-01 22:11:18 +01003765 // Use 'guitablabel' or 'guitabtooltip' if it's set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003766 opt = (tooltip ? &p_gtt : &p_gtl);
3767 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003768 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003769 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003770 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003771 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3772 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003773
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003774 printer_page_num = tabpage_index(tp);
3775# ifdef FEAT_EVAL
3776 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003777# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003778 // It's almost as going to the tabpage, but without autocommands.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003779 curtab->tp_firstwin = firstwin;
3780 curtab->tp_lastwin = lastwin;
3781 curtab->tp_curwin = curwin;
3782 save_curtab = curtab;
3783 curtab = tp;
3784 topframe = curtab->tp_topframe;
3785 firstwin = curtab->tp_firstwin;
3786 lastwin = curtab->tp_lastwin;
3787 curwin = curtab->tp_curwin;
3788 curbuf = curwin->w_buffer;
3789
Bram Moolenaar30613902019-12-01 22:11:18 +01003790 // Can't use NameBuff directly, build_stl_str_hl() uses it.
Luuk van Baal7b224fd2022-11-07 12:16:51 +00003791 build_stl_str_hl(curwin, res, MAXPATHL, *opt, opt_name, 0,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003792 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003793 STRCPY(NameBuff, res);
3794
Bram Moolenaar30613902019-12-01 22:11:18 +01003795 // Back to the original curtab.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003796 curtab = save_curtab;
3797 topframe = curtab->tp_topframe;
3798 firstwin = curtab->tp_firstwin;
3799 lastwin = curtab->tp_lastwin;
3800 curwin = curtab->tp_curwin;
3801 curbuf = curwin->w_buffer;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003802 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003803
Bram Moolenaar30613902019-12-01 22:11:18 +01003804 // If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3805 // use a default label.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003806 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003807 {
John Marriottd2fcbb42025-05-07 19:59:42 +02003808 win_T *wp;
3809 int wincount = 0;
3810 int modified = FALSE;
3811
Bram Moolenaar30613902019-12-01 22:11:18 +01003812 // Get the buffer name into NameBuff[] and shorten it.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003813 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003814 if (!tooltip)
3815 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003816
John Marriottd2fcbb42025-05-07 19:59:42 +02003817 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3818 {
3819 ++wincount;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003820 if (bufIsChanged(wp->w_buffer))
3821 modified = TRUE;
John Marriottd2fcbb42025-05-07 19:59:42 +02003822 }
3823
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003824 if (modified || wincount > 1)
3825 {
John Marriottd2fcbb42025-05-07 19:59:42 +02003826 char_u buf[40] = "+ "; // Tentatively assume modified only
3827 size_t buflen = 2;
3828 size_t NameBufflen = STRLEN(NameBuff);
3829
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003830 if (wincount > 1)
John Marriottd2fcbb42025-05-07 19:59:42 +02003831 buflen = vim_snprintf_safelen(
3832 (char *)buf,
3833 sizeof(buf),
3834 "%d%s ",
3835 wincount,
3836 modified ? "+" : "");
3837
Christ van Willegen31415092025-05-09 00:08:01 +02003838 // Make sure resulting NameBuff will not exceed its bounds.
John Marriottd2fcbb42025-05-07 19:59:42 +02003839 if (NameBufflen + buflen < MAXPATHL)
3840 {
3841 mch_memmove(NameBuff + buflen, NameBuff, NameBufflen + 1); // +1 for NUL
3842 mch_memmove(NameBuff, buf, buflen);
3843 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003844 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003845 }
3846}
3847
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003848/*
3849 * Send the event for clicking to select tab page "nr".
3850 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003851 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003852 */
3853 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003854send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003855{
3856 char_u string[3];
3857
3858 if (nr == tabpage_index(curtab))
3859 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003860
Bram Moolenaar30613902019-12-01 22:11:18 +01003861 // Don't put events in the input queue now.
Martin Tournoij7904fa42022-10-04 16:28:45 +01003862 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003863 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003864 // Set it back to the current tab page.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003865 gui_mch_set_curtab(tabpage_index(curtab));
3866 return FALSE;
3867 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003868
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003869 string[0] = CSI;
3870 string[1] = KS_TABLINE;
3871 string[2] = KE_FILLER;
3872 add_to_input_buf(string, 3);
3873 string[0] = nr;
3874 add_to_input_buf_csi(string, 1);
3875 return TRUE;
3876}
3877
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003878/*
3879 * Send a tabline menu event
3880 */
3881 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003882send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003883{
3884 char_u string[3];
3885
Bram Moolenaar29547192018-12-11 20:39:19 +01003886 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003887 if (hold_gui_events)
3888 return;
3889
Bram Moolenaar29547192018-12-11 20:39:19 +01003890 // Cannot close the last tabpage.
3891 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3892 return;
3893
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003894 string[0] = CSI;
3895 string[1] = KS_TABMENU;
3896 string[2] = KE_FILLER;
3897 add_to_input_buf(string, 3);
3898 string[0] = tabidx;
3899 string[1] = (char_u)(long)event;
3900 add_to_input_buf_csi(string, 2);
3901}
3902
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
3905/*
3906 * Scrollbar stuff:
3907 */
3908
Bram Moolenaare45828b2006-02-15 22:12:56 +00003909/*
3910 * Remove all scrollbars. Used before switching to another tab page.
3911 */
3912 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003913gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003914{
3915 int i;
3916 win_T *wp;
3917
3918 for (i = 0; i < 3; i++)
3919 {
3920 if (i == SBAR_BOTTOM)
3921 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3922 else
3923 {
3924 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003925 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003926 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003927 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003928 }
3929}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003932gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
3934 static int sbar_ident = 0;
3935
Bram Moolenaar30613902019-12-01 22:11:18 +01003936 sb->ident = sbar_ident++; // No check for too big, but would it happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 sb->wp = wp;
3938 sb->type = type;
3939 sb->value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 sb->size = 1;
3941 sb->max = 1;
3942 sb->top = 0;
3943 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 sb->status_height = 0;
3946 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3947}
3948
3949/*
3950 * Find the scrollbar with the given index.
3951 */
3952 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003953gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
3955 win_T *wp;
3956
3957 if (gui.bottom_sbar.ident == ident)
3958 return &gui.bottom_sbar;
3959 FOR_ALL_WINDOWS(wp)
3960 {
3961 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3962 return &wp->w_scrollbars[SBAR_LEFT];
3963 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3964 return &wp->w_scrollbars[SBAR_RIGHT];
3965 }
3966 return NULL;
3967}
3968
3969/*
3970 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3971 *
3972 * For Win32, Macintosh and GTK+ 2:
3973 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3974 * you stop dragging the scrollbar. We get here each time the scrollbar is
3975 * dragged another pixel, but as far as the rest of vim goes, it thinks
3976 * we're just hanging in the call to DispatchMessage() in
3977 * process_message(). The DispatchMessage() call that hangs was passed a
3978 * mouse button click event in the scrollbar window. -- webb.
3979 *
3980 * Solution: Do the scrolling right here. But only when allowed.
3981 * Ignore the scrollbars while executing an external command or when there
3982 * are still characters to be processed.
3983 */
3984 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003985gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 int sb_num;
3989#ifdef USE_ON_FLY_SCROLL
3990 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992# ifdef FEAT_DIFF
3993 int old_topfill = curwin->w_topfill;
3994# endif
3995#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003996 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 int byte_count;
3998#endif
3999
4000 if (sb == NULL)
4001 return;
4002
Bram Moolenaar30613902019-12-01 22:11:18 +01004003 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (hold_gui_events)
4005 return;
4006
Sean Dewar988f7432023-08-16 14:17:36 +01004007 if (cmdwin_type != 0 && sb->wp != cmdwin_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 if (still_dragging)
4011 {
4012 if (sb->wp == NULL)
4013 gui.dragged_sb = SBAR_BOTTOM;
4014 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
4015 gui.dragged_sb = SBAR_LEFT;
4016 else
4017 gui.dragged_sb = SBAR_RIGHT;
4018 gui.dragged_wp = sb->wp;
4019 }
4020 else
4021 {
4022 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004023#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004024 // Keep the "dragged_wp" value until after the scrolling, for when the
4025 // mouse button is released. GTK2 doesn't send the button-up event.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 gui.dragged_wp = NULL;
4027#endif
4028 }
4029
Bram Moolenaar30613902019-12-01 22:11:18 +01004030 // Vertical sbar info is kept in the first sbar (the left one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 if (sb->wp != NULL)
4032 sb = &sb->wp->w_scrollbars[0];
4033
4034 /*
4035 * Check validity of value
4036 */
4037 if (value < 0)
4038 value = 0;
4039#ifdef SCROLL_PAST_END
4040 else if (value > sb->max)
4041 value = sb->max;
4042#else
4043 if (value > sb->max - sb->size + 1)
4044 value = sb->max - sb->size + 1;
4045#endif
4046
4047 sb->value = value;
4048
4049#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar30613902019-12-01 22:11:18 +01004050 // When not allowed to do the scrolling right now, return.
4051 // This also checked input_available(), but that causes the first click in
4052 // a scrollbar to be ignored when Vim doesn't have focus.
Bram Moolenaar67840782008-01-03 15:15:07 +00004053 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return;
4055#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004056 // Disallow scrolling the current window when the completion popup menu is
4057 // visible.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004058 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4059 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
4061#ifdef FEAT_RIGHTLEFT
4062 if (sb->wp == NULL && curwin->w_p_rl)
4063 {
4064 value = sb->max + 1 - sb->size - value;
4065 if (value < 0)
4066 value = 0;
4067 }
4068#endif
4069
Bram Moolenaar30613902019-12-01 22:11:18 +01004070 if (sb->wp != NULL) // vertical scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
4072 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4074 sb_num++;
4075 if (wp == NULL)
4076 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078#ifdef USE_ON_FLY_SCROLL
4079 current_scrollbar = sb_num;
4080 scrollbar_value = value;
Bram Moolenaar24959102022-05-07 20:01:16 +01004081 if (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 {
4083 gui_do_scroll();
4084 setcursor();
4085 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004086 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 {
4088 ins_scroll();
4089 setcursor();
4090 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004091 else if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
4093 if (msg_scrolled == 0)
4094 {
4095 gui_do_scroll();
4096 redrawcmdline();
4097 }
4098 }
4099# ifdef FEAT_FOLDING
Bram Moolenaar30613902019-12-01 22:11:18 +01004100 // Value may have been changed for closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 sb->value = sb->wp->w_topline - 1;
4102# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004103
Bram Moolenaar30613902019-12-01 22:11:18 +01004104 // When dragging one scrollbar and there is another one at the other
4105 // side move the thumb of that one too.
Bram Moolenaar371d5402006-03-20 21:47:49 +00004106 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4107 gui_mch_set_scrollbar_thumb(
4108 &sb->wp->w_scrollbars[
4109 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4110 ? SBAR_LEFT : SBAR_RIGHT],
4111 sb->value, sb->size, sb->max);
4112
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113#else
4114 bytes[0] = CSI;
4115 bytes[1] = KS_VER_SCROLLBAR;
4116 bytes[2] = KE_FILLER;
4117 bytes[3] = (char_u)sb_num;
4118 byte_count = 4;
4119#endif
4120 }
4121 else
4122 {
4123#ifdef USE_ON_FLY_SCROLL
4124 scrollbar_value = value;
4125
Bram Moolenaar24959102022-05-07 20:01:16 +01004126 if (State & MODE_NORMAL)
Christopher Plewright44c22092022-11-15 17:43:36 +00004127 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar24959102022-05-07 20:01:16 +01004128 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 ins_horscroll();
Bram Moolenaar24959102022-05-07 20:01:16 +01004130 else if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004132 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
Christopher Plewright44c22092022-11-15 17:43:36 +00004134 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 redrawcmdline();
4136 }
4137 }
4138 if (old_leftcol != curwin->w_leftcol)
4139 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004140 updateWindow(curwin); // update window, status and cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 setcursor();
4142 }
4143#else
4144 bytes[0] = CSI;
4145 bytes[1] = KS_HOR_SCROLLBAR;
4146 bytes[2] = KE_FILLER;
4147 byte_count = 3;
4148#endif
4149 }
4150
4151#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 /*
4153 * synchronize other windows, as necessary according to 'scrollbind'
4154 */
4155 if (curwin->w_p_scb
4156 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4157 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004158# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 ))))
4162 {
4163 do_check_scrollbind(TRUE);
Bram Moolenaar30613902019-12-01 22:11:18 +01004164 // need to update the window right here
Bram Moolenaar29323592016-07-24 22:04:11 +02004165 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 if (wp->w_redr_type > 0)
4167 updateWindow(wp);
4168 setcursor();
4169 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004170 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004172 add_to_input_buf(bytes, byte_count);
4173 add_long_to_buf((long_u)value, bytes);
4174 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175#endif
4176}
4177
4178/*
4179 * Scrollbar stuff:
4180 */
4181
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004182/*
4183 * Called when something in the window layout has changed.
4184 */
4185 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004186gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004187{
4188 if (gui.in_use && starting == 0)
4189 {
4190 out_flush();
4191 gui_init_which_components(NULL);
4192 gui_update_scrollbars(TRUE);
4193 }
4194 need_mouse_correct = TRUE;
4195}
4196
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004198gui_update_scrollbars(
Bram Moolenaar30613902019-12-01 22:11:18 +01004199 int force) // Force all scrollbars to get updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200{
4201 win_T *wp;
4202 scrollbar_T *sb;
Bram Moolenaar30613902019-12-01 22:11:18 +01004203 long val, size, max; // need 32 bits here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 int which_sb;
4205 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
Bram Moolenaar30613902019-12-01 22:11:18 +01004208 // Update the horizontal scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 gui_update_horiz_scrollbar(force);
4210
Bram Moolenaar4f974752019-02-17 17:44:42 +01004211#ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01004212 // Return straight away if there is neither a left nor right scrollbar.
4213 // On MS-Windows this is required anyway for scrollwheel messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4215 return;
4216#endif
4217
4218 /*
4219 * Don't want to update a scrollbar while we're dragging it. But if we
4220 * have both a left and right scrollbar, and we drag one of them, we still
4221 * need to update the other one.
4222 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004223 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4224 && gui.which_scrollbars[SBAR_LEFT]
4225 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
4227 /*
4228 * If we have two scrollbars and one of them is being dragged, just
4229 * copy the scrollbar position from the dragged one to the other one.
4230 */
4231 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4232 if (gui.dragged_wp != NULL)
4233 gui_mch_set_scrollbar_thumb(
4234 &gui.dragged_wp->w_scrollbars[which_sb],
4235 gui.dragged_wp->w_scrollbars[0].value,
4236 gui.dragged_wp->w_scrollbars[0].size,
4237 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239
Bram Moolenaar30613902019-12-01 22:11:18 +01004240 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 ++hold_gui_events;
4242
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004243 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004245 if (wp->w_buffer == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 continue;
Bram Moolenaar30613902019-12-01 22:11:18 +01004247 // Skip a scrollbar that is being dragged.
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004248 if (!force && (gui.dragged_sb == SBAR_LEFT
4249 || gui.dragged_sb == SBAR_RIGHT)
4250 && gui.dragged_wp == wp)
4251 continue;
4252
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253#ifdef SCROLL_PAST_END
4254 max = wp->w_buffer->b_ml.ml_line_count - 1;
4255#else
4256 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4257#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004258 if (max < 0) // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 max = 0;
4260 val = wp->w_topline - 1;
4261 size = wp->w_height;
4262#ifdef SCROLL_PAST_END
Bram Moolenaar30613902019-12-01 22:11:18 +01004263 if (val > max) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 val = max;
4265#else
Bram Moolenaar30613902019-12-01 22:11:18 +01004266 if (size > max + 1) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 size = max + 1;
4268 if (val > max - size + 1)
4269 val = max - size + 1;
4270#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004271 if (val < 0) // minimal value is 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 val = 0;
4273
4274 /*
4275 * Scrollbar at index 0 (the left one) contains all the information.
4276 * It would be the same info for left and right so we just store it for
4277 * one of them.
4278 */
4279 sb = &wp->w_scrollbars[0];
4280
4281 /*
4282 * Note: no check for valid w_botline. If it's not valid the
4283 * scrollbars will be updated later anyway.
4284 */
4285 if (size < 1 || wp->w_botline - 2 > max)
4286 {
4287 /*
4288 * This can happen during changing files. Just don't update the
4289 * scrollbar for now.
4290 */
Bram Moolenaar30613902019-12-01 22:11:18 +01004291 sb->height = 0; // Force update next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (gui.which_scrollbars[SBAR_LEFT])
4293 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4294 if (gui.which_scrollbars[SBAR_RIGHT])
4295 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4296 continue;
4297 }
4298 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 || sb->top != wp->w_winrow
4300 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004302 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004304 // Height, width or position of scrollbar has changed. For
4305 // vertical split: curwin changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 sb->top = wp->w_winrow;
4308 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
Bram Moolenaar30613902019-12-01 22:11:18 +01004311 // Calculate height and position in pixels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 h = (sb->height + sb->status_height) * gui.char_height;
4313 y = sb->top * gui.char_height + gui.border_offset;
4314#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4315 if (gui.menu_is_active)
4316 y += gui.menu_height;
4317#endif
4318
Bram Moolenaar0b962e52022-04-03 18:02:37 +01004319#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004320 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 y += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323#endif
4324
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004325#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004326 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004327 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004328#endif
4329
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004332 // Height of top scrollbar includes width of top border
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 h += gui.border_offset;
4334 y -= gui.border_offset;
4335 }
4336 if (gui.which_scrollbars[SBAR_LEFT])
4337 {
4338 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4339 gui.left_sbar_x, y,
4340 gui.scrollbar_width, h);
4341 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4342 }
4343 if (gui.which_scrollbars[SBAR_RIGHT])
4344 {
4345 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4346 gui.right_sbar_x, y,
4347 gui.scrollbar_width, h);
4348 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4349 }
4350 }
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 if (force || sb->value != val || sb->size != size || sb->max != max)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004354 // Thumb of scrollbar has moved
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 sb->value = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 sb->size = size;
4357 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004358 if (gui.which_scrollbars[SBAR_LEFT]
4359 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4361 val, size, max);
4362 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004363 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4365 val, size, max);
4366 }
4367 }
Christian Brabandt2e0f3ec2021-11-28 18:41:05 +00004368
4369 // update the title, it may show the scroll position
4370 maketitle();
4371
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 --hold_gui_events;
4374}
4375
4376/*
4377 * Enable or disable a scrollbar.
4378 * Check for scrollbars for vertically split windows which are not enabled
4379 * sometimes.
4380 */
4381 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004382gui_do_scrollbar(
4383 win_T *wp,
Bram Moolenaar30613902019-12-01 22:11:18 +01004384 int which, // SBAR_LEFT or SBAR_RIGHT
4385 int enable) // TRUE to enable scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 int midcol = curwin->w_wincol + curwin->w_width / 2;
4388 int has_midcol = (wp->w_wincol <= midcol
4389 && wp->w_wincol + wp->w_width >= midcol);
4390
Bram Moolenaar30613902019-12-01 22:11:18 +01004391 // Only enable scrollbars that contain the middle column of the current
4392 // window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4394 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004395 // Scrollbars only on one side. Don't enable scrollbars that don't
4396 // contain the middle column of the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 if (!has_midcol)
4398 enable = FALSE;
4399 }
4400 else
4401 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004402 // Scrollbars on both sides. Don't enable scrollbars that neither
4403 // contain the middle column of the current window nor are on the far
4404 // side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (midcol > Columns / 2)
4406 {
4407 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4408 enable = FALSE;
4409 }
4410 else
4411 {
4412 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4413 : !has_midcol)
4414 enable = FALSE;
4415 }
4416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4418}
4419
4420/*
Bram Moolenaar097c5372023-05-24 21:02:24 +01004421 * Scroll a window according to the values set in the globals
4422 * "current_scrollbar" and "scrollbar_value".
4423 * Return TRUE if the cursor in the current window moved or FALSE otherwise.
Christian Brabandt20d161a2023-10-05 22:08:30 +02004424 * may eventually cause a redraw using updateWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 */
4426 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004427gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428{
4429 win_T *wp, *save_wp;
4430 int i;
4431 long nlines;
4432 pos_T old_cursor;
4433 linenr_T old_topline;
4434#ifdef FEAT_DIFF
4435 int old_topfill;
4436#endif
4437
4438 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4439 if (wp == NULL)
4440 break;
4441 if (wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004442 // Couldn't find window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 return FALSE;
Christian Brabandt20d161a2023-10-05 22:08:30 +02004444 // don't redraw, LineOffset and similar are not valid!
4445 if (exmode_active)
4446 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448 /*
4449 * Compute number of lines to scroll. If zero, nothing to do.
4450 */
4451 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4452 if (nlines == 0)
4453 return FALSE;
4454
4455 save_wp = curwin;
4456 old_topline = wp->w_topline;
4457#ifdef FEAT_DIFF
4458 old_topfill = wp->w_topfill;
4459#endif
4460 old_cursor = wp->w_cursor;
4461 curwin = wp;
4462 curbuf = wp->w_buffer;
4463 if (nlines < 0)
4464 scrolldown(-nlines, gui.dragged_wp == NULL);
4465 else
4466 scrollup(nlines, gui.dragged_wp == NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004467 // Reset dragged_wp after using it. "dragged_sb" will have been reset for
4468 // the mouse-up event already, but we still want it to behave like when
4469 // dragging. But not the next click in an arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 if (gui.dragged_sb == SBAR_NONE)
4471 gui.dragged_wp = NULL;
4472
4473 if (old_topline != wp->w_topline
4474#ifdef FEAT_DIFF
4475 || old_topfill != wp->w_topfill
4476#endif
4477 )
4478 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004479 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004481 cursor_correct(); // fix window for 'so'
4482 update_topline(); // avoid up/down jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
4484 if (old_cursor.lnum != wp->w_cursor.lnum)
4485 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488
Bram Moolenaar30613902019-12-01 22:11:18 +01004489 // Make sure wp->w_leftcol and wp->w_skipcol are correct.
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004490 validate_cursor();
4491
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 curwin = save_wp;
4493 curbuf = save_wp->w_buffer;
4494
4495 /*
4496 * Don't call updateWindow() when nothing has changed (it will overwrite
4497 * the status line!).
Christian Brabandt9d1bed52025-01-20 22:55:57 +01004498 *
4499 * Check for ScreenLines, because in ex-mode, we don't have a valid display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 */
Christian Brabandt9d1bed52025-01-20 22:55:57 +01004501 if (ScreenLines != NULL && (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004502 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503#ifdef FEAT_DIFF
4504 || old_topfill != wp->w_topfill
4505#endif
Christian Brabandt9d1bed52025-01-20 22:55:57 +01004506 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004508 int type = UPD_VALID;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004509
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004510 if (pum_visible())
4511 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004512 type = UPD_NOT_VALID;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004513 wp->w_lines_valid = 0;
4514 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004515
Bram Moolenaar30613902019-12-01 22:11:18 +01004516 // Don't set must_redraw here, it may cause the popup menu to
4517 // disappear when losing focus after a scrollbar drag.
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004518 if (wp->w_redr_type < type)
4519 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004520 mch_disable_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01004521 updateWindow(wp); // update window, status line, and cmdline
Bram Moolenaara338adc2018-01-31 20:51:47 +01004522 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524
Bram Moolenaar30613902019-12-01 22:11:18 +01004525 // May need to redraw the popup menu.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004526 if (pum_visible())
4527 pum_redraw();
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004528
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004529 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530}
4531
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532/*
4533 * Horizontal scrollbar stuff:
4534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004536gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537{
Christopher Plewright44c22092022-11-15 17:43:36 +00004538 long value, size, max;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
4540 if (!gui.which_scrollbars[SBAR_BOTTOM])
4541 return;
4542
4543 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4544 return;
4545
4546 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4547 return;
4548
4549 /*
4550 * It is possible for the cursor to be invalid if we're in the middle of
4551 * something (like changing files). If so, don't do anything for now.
4552 */
4553 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4554 {
4555 gui.bottom_sbar.value = -1;
4556 return;
4557 }
4558
Bram Moolenaar02631462017-09-22 15:20:32 +02004559 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (curwin->w_p_wrap)
4561 {
4562 value = 0;
4563#ifdef SCROLL_PAST_END
4564 max = 0;
4565#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004566 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567#endif
4568 }
4569 else
4570 {
4571 value = curwin->w_leftcol;
Christopher Plewright44c22092022-11-15 17:43:36 +00004572 max = scroll_line_len(ui_find_longest_lnum());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 if (virtual_active())
4575 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004576 // May move the cursor even further to the right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (curwin->w_virtcol >= (colnr_T)max)
4578 max = curwin->w_virtcol;
4579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
4581#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004582 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004584 // The line number isn't scrolled, thus there is less space when
4585 // 'number' or 'relativenumber' is set (also for 'foldcolumn').
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 size -= curwin_col_off();
4587#ifndef SCROLL_PAST_END
4588 max -= curwin_col_off();
4589#endif
4590 }
4591
4592#ifndef SCROLL_PAST_END
4593 if (value > max - size + 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01004594 value = max - size + 1; // limit the value to allowable range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#endif
4596
4597#ifdef FEAT_RIGHTLEFT
4598 if (curwin->w_p_rl)
4599 {
4600 value = max + 1 - size - value;
4601 if (value < 0)
4602 {
4603 size += value;
4604 value = 0;
4605 }
4606 }
4607#endif
4608 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4609 && max == gui.bottom_sbar.max)
4610 return;
4611
4612 gui.bottom_sbar.value = value;
4613 gui.bottom_sbar.size = size;
4614 gui.bottom_sbar.max = max;
4615 gui.prev_wrap = curwin->w_p_wrap;
4616
4617 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4618}
4619
4620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 * Check that none of the colors are the same as the background color
4622 */
4623 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004624gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625{
4626 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4627 {
4628 gui_set_bg_color((char_u *)"White");
4629 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4630 gui_set_fg_color((char_u *)"Black");
4631 }
4632}
4633
Bram Moolenaar3918c952005-03-15 22:34:55 +00004634 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004635gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
4637 gui.norm_pixel = gui_get_color(name);
4638 hl_set_fg_color_name(vim_strsave(name));
4639}
4640
Bram Moolenaar3918c952005-03-15 22:34:55 +00004641 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004642gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643{
4644 gui.back_pixel = gui_get_color(name);
4645 hl_set_bg_color_name(vim_strsave(name));
4646}
4647
4648/*
4649 * Allocate a color by name.
4650 * Returns INVALCOLOR and gives an error message when failed.
4651 */
4652 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004653gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654{
4655 guicolor_T t;
4656
4657 if (*name == NUL)
4658 return INVALCOLOR;
4659 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004660
Bram Moolenaar5b9f5722023-02-19 20:49:38 +00004661 int is_none = STRCMP(name, "none") == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004663#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar5b9f5722023-02-19 20:49:38 +00004664 && (gui.in_use || is_none)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665#endif
4666 )
Bram Moolenaar5b9f5722023-02-19 20:49:38 +00004667 {
4668 if (is_none)
4669 emsg(_(e_cannot_use_color_none_did_you_mean_none));
4670 else
4671 semsg(_(e_cannot_allocate_color_str), name);
4672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 return t;
4674}
4675
4676/*
4677 * Return the grey value of a color (range 0-255).
4678 */
4679 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004680gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004682 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004684 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004685 + (((rgb >> 8) & 0xff) * 587)
4686 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687}
4688
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004689 char_u *
4690gui_bg_default(void)
4691{
4692 if (gui_get_lightness(gui.back_pixel) < 127)
4693 return (char_u *)"dark";
4694 return (char_u *)"light";
4695}
4696
4697/*
4698 * Option initializations that can only be done after opening the GUI window.
4699 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004700 static void
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004701init_gui_options(void)
4702{
Bram Moolenaar30613902019-12-01 22:11:18 +01004703 // Set the 'background' option according to the lightness of the
4704 // background color, unless the user has set it already.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004705 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
4706 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004707 set_option_value_give_err((char_u *)"bg", 0L, gui_bg_default(), 0);
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004708 highlight_changed();
4709 }
4710}
4711
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712#if defined(FEAT_GUI_X11) || defined(PROTO)
4713 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004714gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
4716 win_T *wp;
4717
Bram Moolenaar30613902019-12-01 22:11:18 +01004718 // Nothing to do if GUI hasn't started yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 if (!gui.in_use)
4720 return;
4721
4722 FOR_ALL_WINDOWS(wp)
4723 {
4724 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4725 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4726 }
4727 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4728}
4729#endif
4730
4731/*
4732 * Call this when focus has changed.
4733 */
4734 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004735gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
4737/*
4738 * Skip this code to avoid drawing the cursor when debugging and switching
4739 * between the debugger window and gvim.
4740 */
4741#if 1
4742 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004743 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
4745# ifdef FEAT_XIM
4746 xim_set_focus(in_focus);
4747# endif
4748
Bram Moolenaar30613902019-12-01 22:11:18 +01004749 // Put events in the input queue only when allowed.
4750 // ui_focus_change() isn't called directly, because it invokes
4751 // autocommands and that must not happen asynchronously.
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004752 if (!hold_gui_events)
4753 {
4754 char_u bytes[3];
4755
4756 bytes[0] = CSI;
4757 bytes[1] = KS_EXTRA;
4758 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4759 add_to_input_buf(bytes, 3);
4760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#endif
4762}
4763
4764/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004765 * When mouse moved: apply 'mousefocus'.
4766 * Also updates the mouse pointer shape.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 */
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004768 static void
4769gui_mouse_focus(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004772 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
4774#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01004775 // Get window pointer, and update mouse shape as well.
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004776 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777#endif
4778
Bram Moolenaar30613902019-12-01 22:11:18 +01004779 // Only handle this when 'mousefocus' set and ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 if (p_mousef
Bram Moolenaar30613902019-12-01 22:11:18 +01004781 && !hold_gui_events // not holding events
Bram Moolenaar24959102022-05-07 20:01:16 +01004782 && (State & (MODE_NORMAL | MODE_INSERT))// Normal/Visual/Insert mode
4783 && State != MODE_HITRETURN // but not hit-return prompt
Bram Moolenaar30613902019-12-01 22:11:18 +01004784 && msg_scrolled == 0 // no scrolled message
4785 && !need_mouse_correct // not moving the pointer
4786 && gui.in_focus) // gvim in focus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004788 // Don't move the mouse when it's left or right of the Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 if (x < 0 || x > Columns * gui.char_width)
4790 return;
4791#ifndef FEAT_MOUSESHAPE
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004792 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793#endif
4794 if (wp == curwin || wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004795 return; // still in the same old window, or none at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
Bram Moolenaar30613902019-12-01 22:11:18 +01004797 // Ignore position in the tab pages line.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004798 if (Y_2_ROW(y) < tabline_height())
4799 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004800
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004802 * Format a mouse click on status line input,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004804 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4805 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 */
4807 if (finish_op)
4808 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004809 // abort the current operator first
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 st[0] = ESC;
4811 add_to_input_buf(st, 1);
4812 }
4813 st[0] = CSI;
4814 st[1] = KS_MOUSE;
4815 st[2] = KE_FILLER;
4816 st[3] = (char_u)MOUSE_LEFT;
4817 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004818 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 wp->w_height + W_WINROW(wp));
4820
4821 add_to_input_buf(st, 8);
4822 st[3] = (char_u)MOUSE_RELEASE;
4823 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004825 // Need to wake up the main loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 if (gtk_main_level() > 0)
4827 gtk_main_quit();
4828#endif
4829 }
4830}
4831
4832/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004833 * Called when the mouse moved (but not when dragging).
4834 */
4835 void
4836gui_mouse_moved(int x, int y)
4837{
4838 // Ignore this while still starting up.
4839 if (!gui.in_use || gui.starting)
4840 return;
4841
4842 // apply 'mousefocus' and pointer shape
4843 gui_mouse_focus(x, y);
4844
Ernie Raelc4cb5442022-04-03 15:47:28 +01004845 if (p_mousemev
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004846#ifdef FEAT_PROP_POPUP
Ernie Raelc4cb5442022-04-03 15:47:28 +01004847 || popup_uses_mouse_move
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004848#endif
Ernie Raelc4cb5442022-04-03 15:47:28 +01004849 )
4850 // Generate a mouse-moved event. For a <MouseMove> mapping. Or so the
4851 // popup can perhaps be closed, just like in the terminal.
4852 gui_send_mouse_event(MOUSE_MOVE, x, y, FALSE, 0);
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004853}
4854
4855/*
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004856 * Get the window where the mouse pointer is on.
4857 * Returns NULL if not found.
4858 */
4859 win_T *
4860gui_mouse_window(mouse_find_T popup)
4861{
4862 int x, y;
4863
4864 if (!(gui.in_use && (p_mousef || popup == FIND_POPUP)))
4865 return NULL;
4866 gui_mch_getmouse(&x, &y);
4867
4868 // Only use the mouse when it's on the Vim window
4869 if (x >= 0 && x <= Columns * gui.char_width
4870 && y >= 0 && Y_2_ROW(y) >= tabline_height())
4871 return xy2win(x, y, popup);
4872 return NULL;
4873}
4874
4875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 * Called when mouse should be moved to window with focus.
4877 */
4878 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004879gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 win_T *wp = NULL;
4882
4883 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004884
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004885 wp = gui_mouse_window(IGNORE_POPUP);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004886 if (wp == curwin || wp == NULL)
4887 return;
4888
4889 // If in other than current window
4890 validate_cline_row();
4891 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4892 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4893 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894}
4895
4896/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004897 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004898 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 static win_T *
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004901xy2win(int x, int y, mouse_find_T popup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 int row;
4904 int col;
4905 win_T *wp;
4906
4907 row = Y_2_ROW(y);
4908 col = X_2_COL(x);
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02004909
Bram Moolenaar30613902019-12-01 22:11:18 +01004910 if (row < 0 || col < 0) // before first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 return NULL;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004912 wp = mouse_find_win(&row, &col, popup);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004913 if (wp == NULL)
4914 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004915#ifdef FEAT_MOUSESHAPE
Bram Moolenaar24959102022-05-07 20:01:16 +01004916 if (State == MODE_HITRETURN || State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
4918 if (Y_2_ROW(y) >= msg_row)
4919 update_mouseshape(SHAPE_IDX_MOREL);
4920 else
4921 update_mouseshape(SHAPE_IDX_MORE);
4922 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004923 else if (row > wp->w_height) // below status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaar24959102022-05-07 20:01:16 +01004925 else if (!(State & MODE_CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004926 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaar24959102022-05-07 20:01:16 +01004928 else if (!(State & MODE_CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004929 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 update_mouseshape(SHAPE_IDX_STATUS);
4931 else
4932 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004934 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935}
4936
4937/*
4938 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4939 * File names may be given to redefine the args list.
4940 */
4941 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004942ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943{
4944 char_u *arg = eap->arg;
4945
4946 /*
4947 * Check for "-f" argument: foreground, don't fork.
4948 * Also don't fork when started with "gvim -f".
4949 * Do fork when using "gui -b".
4950 */
4951 if (arg[0] == '-'
4952 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004953 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
4955 gui.dofork = (arg[1] == 'b');
4956 eap->arg = skipwhite(eap->arg + 2);
4957 }
4958 if (!gui.in_use)
4959 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004960#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaar257a3962019-12-29 15:19:03 +01004961 if (!gui.starting)
4962 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004963 emsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaar257a3962019-12-29 15:19:03 +01004964 return;
4965 }
4966#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004967 // Clear the command. Needed for when forking+exiting, to avoid part
4968 // of the argument ending up after the shell prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 msg_clr_eos_force();
Bram Moolenaar257a3962019-12-29 15:19:03 +01004970#ifdef GUI_MAY_SPAWN
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02004971 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004972 gui_start(eap->arg);
4973 else
Bram Moolenaar257a3962019-12-29 15:19:03 +01004974#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004975 gui_start(NULL);
Bram Moolenaar257a3962019-12-29 15:19:03 +01004976#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004977 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02004980 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 ex_next(eap);
4982}
4983
Bram Moolenaar4f974752019-02-17 17:44:42 +01004984#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004985 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON) \
4986 || defined(FEAT_GUI_HAIKU)) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004987 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988/*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01004989 * This is shared between Haiku, Motif, and GTK.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991
4992/*
4993 * Callback function for do_in_runtimepath().
4994 */
4995 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004996gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004998 char_u *gfp_buffer = cookie;
4999
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 if (STRLEN(fname) >= MAXPATHL)
5001 *gfp_buffer = NUL;
5002 else
5003 STRCPY(gfp_buffer, fname);
5004}
5005
5006/*
5007 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5008 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5009 */
5010 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005011gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
5013 if (STRLEN(name) > MAXPATHL - 14)
5014 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005015 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005016 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005017 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 return FAIL;
5019 return OK;
5020}
5021
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005022# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023/*
5024 * Given the name of the "icon=" argument, try finding the bitmap file for the
5025 * icon. If it is an absolute path name, use it as it is. Otherwise append
5026 * "ext" and search for it in 'runtimepath'.
5027 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5028 * contains "name".
5029 */
5030 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005031gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032{
5033 char_u buf[MAXPATHL + 1];
5034
5035 expand_env(name, buffer, MAXPATHL);
5036 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5037 STRCPY(buffer, buf);
5038}
5039# endif
5040#endif
5041
Bram Moolenaar9e175142020-04-30 22:51:01 +02005042#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)|| defined(FEAT_GUI_HAIKU) \
5043 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005045display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046{
5047 char_u *p;
5048
5049 if (isatty(2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005051 fflush(stderr);
5052 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005054
5055 if (error_ga.ga_data == NULL)
5056 return;
5057
5058 // avoid putting up a message box with blanks only
5059 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
Keith Thompson184f71c2024-01-04 21:19:04 +01005060 if (!SAFE_isspace(*p))
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005061 {
5062 // Truncate a very long message, it will go off-screen.
5063 if (STRLEN(p) > 2000)
5064 STRCPY(p + 2000 - 14, "...(truncated)");
5065 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
5066 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
5067 break;
5068 }
5069 ga_clear(&error_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070}
5071#endif
5072
5073#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5074/*
5075 * Return TRUE if still starting up and there is no place to enter text.
5076 * For GTK and X11 we check if stderr is not a tty, which means we were
5077 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5078 * allow typing on stdin.
5079 */
5080 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005081no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
5083 return ((!gui.in_use || gui.starting)
5084# ifndef NO_CONSOLE
5085 && !isatty(0) && !isatty(2)
5086# endif
5087 );
5088}
5089#endif
5090
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005091#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005092 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005093 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094/*
5095 * Update the current window and the screen.
5096 */
5097 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005098gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005100# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005101 linenr_T conceal_old_cursor_line = 0;
5102 linenr_T conceal_new_cursor_line = 0;
5103 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005104# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005105
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 update_topline();
5107 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005108
Bram Moolenaar30613902019-12-01 22:11:18 +01005109 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005110 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005111# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005112 || popup_visible
5113# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005114# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005115 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005116# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005117 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005118 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005119 if (has_cursormoved())
5120 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
K.Takata6e1d31e2022-02-03 13:05:32 +00005121# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005122 if (popup_visible)
5123 popup_check_cursor_pos();
K.Takata6e1d31e2022-02-03 13:05:32 +00005124# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005125# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005126 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005127 {
5128 conceal_old_cursor_line = last_cursormoved.lnum;
5129 conceal_new_cursor_line = curwin->w_cursor.lnum;
5130 conceal_update_lines = TRUE;
5131 }
5132# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005133 last_cursormoved = curwin->w_cursor;
5134 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005135
LemonBoy09371822022-04-08 15:18:45 +01005136 if (!finish_op)
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00005137 may_trigger_win_scrolled_resized();
LemonBoy09371822022-04-08 15:18:45 +01005138
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005139# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005140 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005141 && (conceal_old_cursor_line != conceal_new_cursor_line
5142 || conceal_cursor_line(curwin)
5143 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005144 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005145 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005146 redrawWinline(curwin, conceal_old_cursor_line);
5147 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005148 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005149 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005150 }
5151# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005152 update_screen(0); // may need to update the screen
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005153 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005154 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155}
5156#endif
5157
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005158#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159/*
5160 * Get the text to use in a find/replace dialog. Uses the last search pattern
5161 * if the argument is empty.
5162 * Returns an allocated string.
5163 */
5164 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005165get_find_dialog_text(
5166 char_u *arg,
Bram Moolenaar30613902019-12-01 22:11:18 +01005167 int *wwordp, // return: TRUE if \< \> found
5168 int *mcasep) // return: TRUE if \C found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169{
5170 char_u *text;
5171
5172 if (*arg == NUL)
5173 text = last_search_pat();
5174 else
5175 text = arg;
5176 if (text != NULL)
5177 {
John Marriottd2fcbb42025-05-07 19:59:42 +02005178 int len = (int)STRLEN(text);
5179
5180 text = vim_strnsave(text, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 if (text != NULL)
5182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 int i;
5184
Bram Moolenaar30613902019-12-01 22:11:18 +01005185 // Remove "\V"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5187 {
5188 mch_memmove(text, text + 2, (size_t)(len - 1));
5189 len -= 2;
5190 }
5191
Bram Moolenaar30613902019-12-01 22:11:18 +01005192 // Recognize "\c" and "\C" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5194 {
5195 *mcasep = (text[1] == 'C');
5196 mch_memmove(text, text + 2, (size_t)(len - 1));
5197 len -= 2;
5198 }
5199
Bram Moolenaar30613902019-12-01 22:11:18 +01005200 // Recognize "\<text\>" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (len >= 4
5202 && STRNCMP(text, "\\<", 2) == 0
5203 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5204 {
5205 *wwordp = TRUE;
5206 mch_memmove(text, text + 2, (size_t)(len - 4));
5207 text[len - 4] = NUL;
5208 }
5209
Bram Moolenaar30613902019-12-01 22:11:18 +01005210 // Recognize "\/" or "\?" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 for (i = 0; i + 1 < len; ++i)
5212 if (text[i] == '\\' && (text[i + 1] == '/'
5213 || text[i + 1] == '?'))
5214 {
5215 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5216 --len;
5217 }
5218 }
5219 }
5220 return text;
5221}
5222
5223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 * Handle the press of a button in the find-replace dialog.
5225 * Return TRUE when something was added to the input buffer.
5226 */
5227 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005228gui_do_findrepl(
Bram Moolenaar30613902019-12-01 22:11:18 +01005229 int flags, // one of FRD_REPLACE, FRD_FINDNEXT, etc.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005230 char_u *find_text,
5231 char_u *repl_text,
Bram Moolenaar30613902019-12-01 22:11:18 +01005232 int down) // Search downwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233{
5234 garray_T ga;
5235 int i;
5236 int type = (flags & FRD_TYPE_MASK);
5237 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005238 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005239 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005240 static int busy = FALSE;
5241
Bram Moolenaar30613902019-12-01 22:11:18 +01005242 // When the screen is being updated we should not change buffers and
5243 // windows structures, it may cause freed memory to be used. Also don't
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005244 // do this recursively (pressing "Find" quickly several times).
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005245 if (updating_screen || busy)
5246 return FALSE;
5247
Bram Moolenaar30613902019-12-01 22:11:18 +01005248 // refuse replace when text cannot be changed
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005249 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5250 return FALSE;
5251
5252 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
5254 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005255 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 ga_concat(&ga, (char_u *)"%s/");
5257
5258 ga_concat(&ga, (char_u *)"\\V");
5259 if (flags & FRD_MATCH_CASE)
5260 ga_concat(&ga, (char_u *)"\\C");
5261 else
5262 ga_concat(&ga, (char_u *)"\\c");
5263 if (flags & FRD_WHOLE_WORD)
5264 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar30613902019-12-01 22:11:18 +01005265 // escape slash and backslash
Bram Moolenaar518bc172018-05-13 17:05:30 +02005266 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5267 if (p != NULL)
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005268 ga_concat(&ga, p);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005269 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 if (flags & FRD_WHOLE_WORD)
5271 ga_concat(&ga, (char_u *)"\\>");
5272
5273 if (type == FRD_REPLACEALL)
5274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 ga_concat(&ga, (char_u *)"/");
matveyt2834ebd2022-09-06 17:00:15 +01005276 // Escape slash and backslash.
5277 // Also escape tilde and ampersand if 'magic' is set.
5278 p = vim_strsave_escaped(repl_text,
5279 p_magic ? (char_u *)"/\\~&" : (char_u *)"/\\");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005280 if (p != NULL)
5281 ga_concat(&ga, p);
5282 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005284 }
5285 ga_append(&ga, NUL);
5286
5287 if (type == FRD_REPLACE)
5288 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005289 // Do the replacement when the text at the cursor matches. Thus no
5290 // replacement is done if the cursor was moved!
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005291 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5292 regmatch.rm_ic = 0;
5293 if (regmatch.regprog != NULL)
5294 {
5295 p = ml_get_cursor();
5296 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5297 && regmatch.startp[0] == p)
5298 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005299 // Clear the command line to remove any old "No match"
5300 // error.
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005301 msg_end_prompt();
5302
5303 if (u_save_cursor() == OK)
5304 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005305 // A button was pressed thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005306 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005307
5308 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005309 FALSE, FALSE);
John Marriottf4b36412025-02-23 09:09:59 +01005310 ins_str(repl_text, STRLEN(repl_text));
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005311 }
5312 }
5313 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005314 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005315 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005316 }
5317 }
5318
5319 if (type == FRD_REPLACEALL)
5320 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005321 // A button was pressed, thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005322 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 do_cmdline_cmd(ga.ga_data);
5324 }
5325 else
5326 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005327 int searchflags = SEARCH_MSG + SEARCH_MARK;
5328
Bram Moolenaar30613902019-12-01 22:11:18 +01005329 // Search for the next match.
5330 // Don't skip text under cursor for single replace.
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005331 if (type == FRD_REPLACE)
5332 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005334 if (down)
5335 {
John Marriott8c85a2a2024-05-20 19:18:26 +02005336 (void)do_search(NULL, '/', '/', ga.ga_data, STRLEN(ga.ga_data), 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005337 }
5338 else
5339 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005340 // We need to escape '?' if and only if we are searching in the up
5341 // direction
Bram Moolenaar518bc172018-05-13 17:05:30 +02005342 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5343 if (p != NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005344 (void)do_search(NULL, '?', '?', p, STRLEN(p), 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005345 vim_free(p);
5346 }
5347
Bram Moolenaar30613902019-12-01 22:11:18 +01005348 msg_scroll = i; // don't let an error message set msg_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
5350
Bram Moolenaar30613902019-12-01 22:11:18 +01005351 // Don't want to pass did_emsg to other code, it may cause disabling
5352 // syntax HL if we were busy redrawing.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005353 did_emsg = save_did_emsg;
5354
Bram Moolenaar24959102022-05-07 20:01:16 +01005355 if (State & (MODE_NORMAL | MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005357 gui_update_screen(); // update the screen
5358 msg_didout = 0; // overwrite any message
5359 need_wait_return = FALSE; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361
5362 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005363 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 return (ga.ga_len > 0);
5365}
5366
5367#endif
5368
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005369#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370/*
5371 * Jump to the window at specified point (x, y).
5372 */
5373 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005374gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375{
5376 int row = Y_2_ROW(y);
5377 int col = X_2_COL(x);
5378 win_T *wp;
5379
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005380 if (row < 0 || col < 0)
5381 return;
5382
5383 wp = mouse_find_win(&row, &col, FAIL_POPUP);
5384 if (wp != NULL && wp != curwin)
5385 win_goto(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387
5388/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005389 * Function passed to handle_drop() for the actions to be done after the
5390 * argument list has been updated.
5391 */
5392 static void
5393drop_callback(void *cookie)
5394{
5395 char_u *p = cookie;
Bram Moolenaar4671e882021-11-22 17:21:48 +00005396 int do_shorten = FALSE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005397
Bram Moolenaar30613902019-12-01 22:11:18 +01005398 // If Shift held down, change to first file's directory. If the first
5399 // item is a directory, change to that directory (and let the explorer
5400 // plugin show the contents).
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005401 if (p != NULL)
5402 {
5403 if (mch_isdir(p))
5404 {
5405 if (mch_chdir((char *)p) == 0)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005406 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005407 }
5408 else if (vim_chdirfile(p, "drop") == OK)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005409 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005410 vim_free(p);
Bram Moolenaar4671e882021-11-22 17:21:48 +00005411 if (do_shorten)
5412 {
5413 shorten_fnames(TRUE);
5414 last_chdir_reason = "drop";
5415 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005416 }
5417
Bram Moolenaar30613902019-12-01 22:11:18 +01005418 // Update the screen display
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005419 update_screen(UPD_NOT_VALID);
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005420# ifdef FEAT_MENU
5421 gui_update_menus(0);
5422# endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005423 maketitle();
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005424 setcursor();
5425 out_flush_cursor(FALSE, FALSE);
5426}
5427
5428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 * Process file drop. Mouse cursor position, key modifiers, name of files
5430 * and count of files are given. Argument "fnames[count]" has full pathnames
5431 * of dropped files, they will be freed in this function, and caller can't use
5432 * fnames after call this function.
5433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005435gui_handle_drop(
5436 int x UNUSED,
5437 int y UNUSED,
5438 int_u modifiers,
5439 char_u **fnames,
5440 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 int i;
5443 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005444 static int entered = FALSE;
5445
5446 /*
5447 * This function is called by event handlers. Just in case we get a
5448 * second event before the first one is handled, ignore the second one.
5449 * Not sure if this can ever happen, just in case.
5450 */
5451 if (entered)
5452 return;
5453 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455 /*
5456 * When the cursor is at the command line, add the file names to the
5457 * command line, don't edit the files.
5458 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005459 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
5461 shorten_filenames(fnames, count);
5462 for (i = 0; i < count; ++i)
5463 {
5464 if (fnames[i] != NULL)
5465 {
5466 if (i > 0)
5467 add_to_input_buf((char_u*)" ", 1);
5468
Bram Moolenaar30613902019-12-01 22:11:18 +01005469 // We don't know what command is used thus we can't be sure
5470 // about which characters need to be escaped. Only escape the
5471 // most common ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472# ifdef BACKSLASH_IN_FILENAME
5473 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5474# else
5475 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5476# endif
5477 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005478 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 vim_free(p);
5480 vim_free(fnames[i]);
5481 }
5482 }
5483 vim_free(fnames);
5484 }
5485 else
5486 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005487 // Go to the window under mouse cursor, then shorten given "fnames" by
5488 // current window, because a window can have local current dir.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 shorten_filenames(fnames, count);
5491
Bram Moolenaar30613902019-12-01 22:11:18 +01005492 // If Shift held down, remember the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 if ((modifiers & MOUSE_SHIFT) != 0)
5494 p = vim_strsave(fnames[0]);
5495 else
5496 p = NULL;
5497
Bram Moolenaar30613902019-12-01 22:11:18 +01005498 // Handle the drop, :edit or :split to get to the file. This also
5499 // frees fnames[]. Skip this if there is only one item, it's a
5500 // directory and Shift is held down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5502 && mch_isdir(fnames[0]))
5503 {
5504 vim_free(fnames[0]);
5505 vim_free(fnames);
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02005506 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 }
5508 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005509 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
Bram Moolenaar4671e882021-11-22 17:21:48 +00005510 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005512
5513 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514}
5515#endif
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005516
5517/*
5518 * Check if "key" is to interrupt us. Handles a key that has not had modifiers
5519 * applied yet.
5520 * Return the key with modifiers applied if so, NUL if not.
5521 */
5522 int
5523check_for_interrupt(int key, int modifiers_arg)
5524{
5525 int modifiers = modifiers_arg;
5526 int c = merge_modifyOtherKeys(key, &modifiers);
5527
5528 if ((c == Ctrl_C && ctrl_c_interrupts)
Bram Moolenaaraf50e892020-08-01 13:22:10 +02005529#ifdef UNIX
5530 || (intr_char != Ctrl_C && c == intr_char)
5531#endif
5532 )
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005533 {
5534 got_int = TRUE;
5535 return c;
5536 }
5537 return NUL;
5538}
5539
Bram Moolenaar2d12c252022-06-13 21:42:45 +01005540/*
5541 * If the "--gui-log-file fname" argument is given write the dialog title and
5542 * message to a file and return TRUE. Otherwise return FALSE.
5543 * When there is any problem opening the file or writing to the file this is
5544 * ignored, showing the dialog might get the test to get stuck.
5545 */
5546 int
5547gui_dialog_log(char_u *title, char_u *message)
5548{
5549 char_u *fname = get_gui_dialog_file();
5550 FILE *fd;
5551
5552 if (fname == NULL)
5553 return FALSE;
5554
5555 fd = mch_fopen((char *)fname, "a");
5556 if (fd != NULL)
5557 {
5558 fprintf(fd, "%s: %s\n", title, message);
5559 fclose(fd);
5560 }
5561 return TRUE;
5562}