blob: 4d596519323da193b068fdf6dcb59fbd04ff3055 [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 }
Foxe Chenb90c2392025-06-27 21:10:35 +0200149 else
150 // Reset clipmethod to CLIPMETHOD_NONE
151 choose_clipmethod();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153 vim_free(old_term);
154
Bram Moolenaar30613902019-12-01 22:11:18 +0100155 // If the GUI started successfully, trigger the GUIEnter event, otherwise
156 // the GUIFailed event.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200157 gui_mch_update();
158 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
159 NULL, NULL, FALSE, curbuf);
Bram Moolenaara25f7182022-11-06 11:27:46 +0000160#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200161 --recursive;
Bram Moolenaara25f7182022-11-06 11:27:46 +0000162#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200163}
164
165/*
166 * Set_termname() will call gui_init() to start the GUI.
167 * Set the "starting" flag, to indicate that the GUI will start.
168 *
169 * We don't want to open the GUI shell until after we've read .gvimrc,
170 * otherwise we don't know what font we will use, and hence we don't know
171 * what size the shell should be. So if there are errors in the .gvimrc
172 * file, they will have to go to the terminal: Set full_screen to FALSE.
173 * full_screen will be set to TRUE again by a successful termcapinit().
174 */
175 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100176gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200177{
178 static int recursive = 0;
179
180 ++recursive;
181 gui.starting = TRUE;
182
183#ifdef FEAT_GUI_GTK
184 gui.event_time = GDK_CURRENT_TIME;
185#endif
186
187 termcapinit((char_u *)"builtin_gui");
188 gui.starting = recursive - 1;
189
Bram Moolenaar9372a112005-12-06 19:59:18 +0000190#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200192 {
193# ifdef FEAT_EVAL
194 Window x11_window;
195 Display *x11_display;
196
197 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
198 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
199# endif
200
Bram Moolenaar30613902019-12-01 22:11:18 +0100201 // Display error messages in a dialog now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 --recursive;
206}
207
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200208#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200209
Bram Moolenaar30613902019-12-01 22:11:18 +0100210// for waitpid()
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200211# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
212# include <sys/wait.h>
213# endif
214
215/*
216 * Create a new process, by forking. In the child, start the GUI, and in
217 * the parent, exit.
218 *
219 * If something goes wrong, this will return with gui.in_use still set
220 * to FALSE, in which case the caller should continue execution without
221 * the GUI.
222 *
223 * If the child fails to start the GUI, then the child will exit and the
224 * parent will return. If the child succeeds, then the parent will exit
225 * and the child will return.
226 */
227 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100228gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200229{
Bram Moolenaar30613902019-12-01 22:11:18 +0100230 int pipefd[2]; // pipe between parent and child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200231 int pipe_error;
232 int status;
233 int exit_status;
234 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200235
Bram Moolenaar08210f82023-04-13 19:15:54 +0100236# if defined(FEAT_RELTIME) && defined(PROF_NSEC)
Bram Moolenaarc72e31d2022-06-16 18:47:20 +0100237 // a timer is not carried forward
238 delete_timer();
K.Takata161b6ac2022-11-14 15:31:07 +0000239# endif
Bram Moolenaarc72e31d2022-06-16 18:47:20 +0100240
Bram Moolenaar30613902019-12-01 22:11:18 +0100241 // Setup a pipe between the child and the parent, so that the parent
242 // knows when the child has done the setsid() call and is allowed to
243 // exit.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200244 pipe_error = (pipe(pipefd) < 0);
245 pid = fork();
Bram Moolenaar30613902019-12-01 22:11:18 +0100246 if (pid < 0) // Fork error
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200247 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000248 emsg(_(e_failed_to_create_new_process_for_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200249 return;
250 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100251 else if (pid > 0) // Parent
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200252 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100253 // Give the child some time to do the setsid(), otherwise the
254 // exit() may kill the child too (when starting gvim from inside a
255 // gvim).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200256 if (!pipe_error)
257 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100258 // The read returns when the child closes the pipe (or when
259 // the child dies for some reason).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200260 close(pipefd[1]);
261 status = gui_read_child_pipe(pipefd[0]);
262 if (status == GUI_CHILD_FAILED)
263 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100264 // The child failed to start the GUI, so the caller must
265 // continue. There may be more error information written
266 // to stderr by the child.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200267# ifdef __NeXT__
268 wait4(pid, &exit_status, 0, (struct rusage *)0);
269# else
270 waitpid(pid, &exit_status, 0);
271# endif
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000272 emsg(_(e_the_child_process_failed_to_start_GUI));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200273 return;
274 }
275 else if (status == GUI_CHILD_IO_ERROR)
276 {
277 pipe_error = TRUE;
278 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100279 // else GUI_CHILD_OK: parent exit
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200280 }
281
282 if (pipe_error)
Bram Moolenaareda1da02019-11-17 17:06:33 +0100283 ui_delay(301L, TRUE);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200284
Bram Moolenaar30613902019-12-01 22:11:18 +0100285 // When swapping screens we may need to go to the next line, e.g.,
286 // after a hit-enter prompt and using ":gui".
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200287 if (newline_on_exit)
288 mch_errmsg("\r\n");
289
290 /*
291 * The parent must skip the normal exit() processing, the child
292 * will do it. For example, GTK messes up signals when exiting.
293 */
294 _exit(0);
295 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100296 // Child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200297
K.Takata6e1d31e2022-02-03 13:05:32 +0000298# ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100299 // Call gtk_init_check() here after fork(). See gui_init_check().
Bram Moolenaar29695702012-05-18 17:03:18 +0200300 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100301 getout_preserve_modified(1);
K.Takata6e1d31e2022-02-03 13:05:32 +0000302# endif
Bram Moolenaar29695702012-05-18 17:03:18 +0200303
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200304# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
305 /*
306 * Change our process group. On some systems/shells a CTRL-C in the
307 * shell where Vim was started would otherwise kill gvim!
308 */
309# if defined(HAVE_SETSID)
310 (void)setsid();
311# else
312 (void)setpgid(0, 0);
313# endif
314# endif
315 if (!pipe_error)
316 close(pipefd[0]);
317
318# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
Bram Moolenaar30613902019-12-01 22:11:18 +0100319 // Tell the session manager our new PID
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200320 gui_mch_forked();
321# endif
322
Bram Moolenaar30613902019-12-01 22:11:18 +0100323 // Try to start the GUI
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200324 gui_attempt_start();
325
Bram Moolenaar30613902019-12-01 22:11:18 +0100326 // Notify the parent
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200327 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200328 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200329 if (gui.in_use)
330 write_eintr(pipefd[1], "ok", 3);
331 else
332 write_eintr(pipefd[1], "fail", 5);
333 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200334 }
335
Bram Moolenaar30613902019-12-01 22:11:18 +0100336 // If we failed to start the GUI, exit now.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200337 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100338 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200339}
340
341/*
342 * Read from a pipe assumed to be connected to the child process (this
343 * function is called from the parent).
344 * Return GUI_CHILD_OK if the child successfully started the GUI,
345 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
346 * some other error.
347 *
348 * The file descriptor will be closed before the function returns.
349 */
350 static int
351gui_read_child_pipe(int fd)
352{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200353 long bytes_read;
K.Takata6e1d31e2022-02-03 13:05:32 +0000354# define READ_BUFFER_SIZE 10
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200355 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200356
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200357 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
K.Takata6e1d31e2022-02-03 13:05:32 +0000358# undef READ_BUFFER_SIZE
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200359 close(fd);
360 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200361 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200362 buffer[bytes_read] = NUL;
John Marriottd2fcbb42025-05-07 19:59:42 +0200363 if (STRCMP(buffer, "ok") == 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200364 return GUI_CHILD_OK;
365 return GUI_CHILD_FAILED;
366}
367
Bram Moolenaar30613902019-12-01 22:11:18 +0100368#endif // GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200369
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370/*
371 * Call this when vim starts up, whether or not the GUI is started
372 */
373 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100374gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375{
Bram Moolenaar30613902019-12-01 22:11:18 +0100376 gui.in_use = FALSE; // No GUI yet (maybe later)
377 gui.starting = FALSE; // No GUI yet (maybe later)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 gui_mch_prepare(argc, argv);
379}
380
381/*
382 * Try initializing the GUI and check if it can be started.
383 * Used from main() to check early if "vim -g" can start the GUI.
384 * Used from gui_init() to prepare for starting the GUI.
385 * Returns FAIL or OK.
386 */
387 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100388gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
390 static int result = MAYBE;
391
392 if (result != MAYBE)
393 {
394 if (result == FAIL)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000395 emsg(_(e_cannot_start_the_GUI));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 return result;
397 }
398
399 gui.shell_created = FALSE;
400 gui.dying = FALSE;
Bram Moolenaar30613902019-12-01 22:11:18 +0100401 gui.in_focus = TRUE; // so the guicursor setting works
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 gui.dragged_sb = SBAR_NONE;
403 gui.dragged_wp = NULL;
404 gui.pointer_hidden = FALSE;
405 gui.col = 0;
406 gui.row = 0;
407 gui.num_cols = Columns;
408 gui.num_rows = Rows;
409
410 gui.cursor_is_valid = FALSE;
411 gui.scroll_region_top = 0;
412 gui.scroll_region_bot = Rows - 1;
413 gui.scroll_region_left = 0;
414 gui.scroll_region_right = Columns - 1;
415 gui.highlight_mask = HL_NORMAL;
416 gui.char_width = 1;
417 gui.char_height = 1;
418 gui.char_ascent = 0;
419 gui.border_width = 0;
420
421 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200422#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 gui.bold_font = NOFONT;
424 gui.ital_font = NOFONT;
425 gui.boldital_font = NOFONT;
426# ifdef FEAT_XFONTSET
427 gui.fontset = NOFONTSET;
428# endif
429#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200430 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100431#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200432 gui.wide_bold_font = NOFONT;
433 gui.wide_ital_font = NOFONT;
434 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436
437#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200438# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439# ifdef FONTSET_ALWAYS
440 gui.menu_fontset = NOFONTSET;
441# else
442 gui.menu_font = NOFONT;
443# endif
444# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100445 gui.menu_is_active = TRUE; // default: include menu
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446# ifndef FEAT_GUI_GTK
447 gui.menu_height = MENU_DEFAULT_HEIGHT;
448 gui.menu_width = 0;
449# endif
450#endif
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100451#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 gui.toolbar_height = 0;
453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454#ifdef FEAT_BEVAL_TIP
455 gui.tooltip_fontset = NOFONTSET;
456#endif
457
458 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
459 gui.prev_wrap = -1;
460
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +0100461#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Erik S. V. Jansson2f026382024-02-26 22:23:05 +0100462 // Note: gui_set_ligatures() might already have been called e.g. from .vimrc,
463 // and in that case we don't want to overwrite ligatures map that has already
464 // been correctly populated (as that would lead to a cleared ligatures maps).
465 if (*p_guiligatures == NUL)
zeertzjqd9be94c2024-07-14 10:20:20 +0200466 CLEAR_FIELD(gui.ligatures_map);
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100467#endif
468
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200469#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 result = OK;
471#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200472# ifdef FEAT_GUI_GTK
473 /*
474 * Note: Don't call gtk_init_check() before fork, it will be called after
475 * the fork. When calling it before fork, it make vim hang for a while.
476 * See gui_do_fork().
477 * Use a simpler check if the GUI window can probably be opened.
478 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200479 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200480# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483#endif
484 return result;
485}
486
487/*
488 * This is the call which starts the GUI.
489 */
490 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100491gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 win_T *wp;
Yee Cheng China5e03f62025-02-06 21:18:17 +0100494 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 static int recursive = 0;
496
497 /*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000498 * It's possible to use ":gui" in a .gvimrc file. The first half of this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 * function will then be executed at the first call, the rest by the
500 * recursive call. This allow the shell to be opened halfway reading a
501 * gvimrc file.
502 */
503 if (!recursive)
504 {
505 ++recursive;
506
507 clip_init(TRUE);
508
Bram Moolenaar30613902019-12-01 22:11:18 +0100509 // If can't initialize, don't try doing the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 if (gui_init_check() == FAIL)
511 {
512 --recursive;
513 clip_init(FALSE);
514 return;
515 }
516
517 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000518 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
519 * breaks the Paste toolbar button.
520 */
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100521 set_option_value_give_err((char_u *)"paste", 0L, NULL, 0);
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000522
Bram Moolenaaracc770a2020-04-12 15:11:06 +0200523 // Set t_Co to the number of colors: RGB.
524 set_color_count(256 * 256 * 256);
525
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000526 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 * Set up system-wide default menus.
528 */
529#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
530 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
531 {
532 sys_menu = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 sys_menu = FALSE;
535 }
536#endif
537
538 /*
539 * Switch on the mouse by default, unless the user changed it already.
540 * This can then be changed in the .gvimrc.
541 */
542 if (!option_was_set((char_u *)"mouse"))
543 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000544 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545
546 /*
547 * If -U option given, use only the initializations from that file and
548 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
549 */
550 if (use_gvimrc != NULL)
551 {
552 if (STRCMP(use_gvimrc, "NONE") != 0
553 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554 && do_source(use_gvimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000555 semsg(_(e_cannot_read_from_str), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 }
557 else
558 {
559 /*
560 * Get system wide defaults for gvim, only when file name defined.
561 */
562#ifdef SYS_GVIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564#endif
565
566 /*
567 * Try to read GUI initialization commands from the following
568 * places:
569 * - environment variable GVIMINIT
570 * - the user gvimrc file (~/.gvimrc)
571 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
572 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
573 * The first that exists is used, the rest is ignored.
574 */
575 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000576 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000579 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200582#ifdef USR_GVIMRC_FILE3
583 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 DOSO_GVIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 )
587 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200588#ifdef USR_GVIMRC_FILE4
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE,
590 DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591#endif
592 }
593
594 /*
595 * Read initialization commands from ".gvimrc" in current
596 * directory. This is only done if the 'exrc' option is set.
597 * Because of security reasons we disallow shell and write
598 * commands now, except for unix if the file is owned by the user
599 * or 'secure' option has been reset in environment of global
600 * ".gvimrc".
601 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
602 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
603 */
604 if (p_exrc)
605 {
606#ifdef UNIX
607 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200608 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
Bram Moolenaar30613902019-12-01 22:11:18 +0100610 // if ".gvimrc" file is not owned by user, set 'secure'
611 // mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
613 secure = p_secure;
614 }
615#else
616 secure = p_secure;
617#endif
618
619 if ( fullpathcmp((char_u *)USR_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#ifdef SYS_GVIMRC_FILE
622 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200623 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624#endif
625#ifdef USR_GVIMRC_FILE2
626 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200627 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628#endif
629#ifdef USR_GVIMRC_FILE3
630 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200631 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200633#ifdef USR_GVIMRC_FILE4
634 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200635 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640 if (secure == 2)
641 need_wait_return = TRUE;
642 secure = 0;
643 }
644 }
645
646 if (need_wait_return || msg_didany)
647 wait_return(TRUE);
648
649 --recursive;
650 }
651
Bram Moolenaar30613902019-12-01 22:11:18 +0100652 // If recursive call opened the shell, return here from the first call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (gui.in_use)
654 return;
655
656 /*
657 * Create the GUI shell.
658 */
Bram Moolenaar30613902019-12-01 22:11:18 +0100659 gui.in_use = TRUE; // Must be set after menus have been set up
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 if (gui_mch_init() == FAIL)
661 goto error;
662
Bram Moolenaar30613902019-12-01 22:11:18 +0100663 // Avoid a delay for an error message that was printed in the terminal
664 // where Vim was started.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 emsg_on_display = FALSE;
666 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100667 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 need_wait_return = FALSE;
669 msg_didany = FALSE;
670
671 /*
672 * Check validity of any generic resources that may have been loaded.
673 */
674 if (gui.border_width < 0)
675 gui.border_width = 0;
676
677 /*
678 * Set up the fonts. First use a font specified with "-fn" or "-font".
679 */
680 if (font_argument != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100681 set_option_value_give_err((char_u *)"gfn",
682 0L, (char_u *)font_argument, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 if (
684#ifdef FEAT_XFONTSET
685 (*p_guifontset == NUL
686 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
687#endif
688 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
689 : p_guifont, FALSE) == FAIL)
690 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000691 emsg(_(e_cannot_start_gui_no_valid_font_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 goto error2;
693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if (gui_get_wide_font() == FAIL)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +0000695 emsg(_(e_guifontwide_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
697 gui.num_cols = Columns;
698 gui.num_rows = Rows;
699 gui_reset_scroll_region();
700
Bram Moolenaar30613902019-12-01 22:11:18 +0100701 // Create initial scrollbars
Yee Cheng China5e03f62025-02-06 21:18:17 +0100702 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
705 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
706 }
707 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
708
709#ifdef FEAT_MENU
710 gui_create_initial_menus(root_menu);
711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#ifdef FEAT_SIGN_ICONS
713 sign_gui_started();
714#endif
715
Bram Moolenaar30613902019-12-01 22:11:18 +0100716 // Configure the desired menu and scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 gui_init_which_components(NULL);
718
Bram Moolenaar30613902019-12-01 22:11:18 +0100719 // All components of the GUI have been created now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 gui.shell_created = TRUE;
721
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100722#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100723 // Set the shell size, adjusted for the screen size. For GTK this only
724 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100725 // If the window is already maximized (e.g. when --windowid is passed in),
726 // we want to use the system-provided dimensions by passing FALSE to
727 // mustset. Otherwise, we want to initialize with the default rows/columns.
728 if (gui_mch_maximized())
729 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
730 else
731 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100732#else
733# ifndef FEAT_GUI_GTK
734 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#endif
737#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +0100738 // Need to set the size of the menubar after all the menus have been
739 // created.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 gui_mch_compute_menu_height((Widget)0);
741#endif
742
743 /*
744 * Actually open the GUI shell.
745 */
746 if (gui_mch_open() != FAIL)
747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 maketitle();
749 resettitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000750
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 init_gui_options();
752#ifdef FEAT_ARABIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100753 // Our GUI can't do bidi.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 p_tbidi = FALSE;
755#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000756#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100757 // Give GTK+ a chance to put all widget's into place.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000759
760# ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +0100761 // If there is no 'm' in 'guioptions' we need to remove the menu now.
762 // It was still there to make F10 work.
Bram Moolenaar18144c82006-04-12 21:52:12 +0000763 if (vim_strchr(p_go, GO_MENUS) == NULL)
764 {
765 --gui.starting;
766 gui_mch_enable_menu(FALSE);
767 ++gui.starting;
768 gui_mch_update();
769 }
770# endif
771
Bram Moolenaar30613902019-12-01 22:11:18 +0100772 // Now make sure the shell fits on the screen.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100773 if (gui_mch_maximized())
774 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
775 else
776 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100778 // When 'lines' was set while starting up the topframe may have to be
779 // resized.
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000780 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000781
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100782#ifdef FEAT_BEVAL_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100783 // Always create the Balloon Evaluation area, but disable it when
784 // 'ballooneval' is off.
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100785 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200786 {
787# ifdef FEAT_VARTABS
788 vim_free(balloonEval->vts);
789# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100790 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200791 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100792 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000793# ifdef FEAT_GUI_GTK
794 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
795 &general_beval_cb, NULL);
796# else
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100797# if defined(FEAT_GUI_MOTIF)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000798 {
799 extern Widget textArea;
800 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000801 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000802 }
803# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100804# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000805 balloonEval = gui_mch_create_beval_area(NULL, NULL,
806 &general_beval_cb, NULL);
807# endif
808# endif
809# endif
810 if (!p_beval)
811 gui_mch_disable_beval_area(balloonEval);
812#endif
Bram Moolenaarad772a62020-06-01 14:07:49 +0200813
814#ifndef FEAT_GUI_MSWIN
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200815 // In the GUI modifiers are prepended to keys.
Bram Moolenaarad772a62020-06-01 14:07:49 +0200816 // Don't do this for MS-Windows yet, it sends CTRL-K without the
817 // modifier.
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +0200818 seenModifyOtherKeys = TRUE;
Bram Moolenaarad772a62020-06-01 14:07:49 +0200819#endif
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
822 if (!im_xim_isvalid_imactivate())
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000823 emsg(_(e_value_of_imactivatekey_is_invalid));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100825 // When 'cmdheight' was set during startup it may not have taken
826 // effect yet.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000827 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000828 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
830 return;
831 }
832
833error2:
834#ifdef FEAT_GUI_X11
Bram Moolenaar30613902019-12-01 22:11:18 +0100835 // undo gui_mch_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 gui_mch_uninit();
837#endif
838
839error:
840 gui.in_use = FALSE;
841 clip_init(FALSE);
842}
843
844
845 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100846gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847{
Bram Moolenaar30613902019-12-01 22:11:18 +0100848 // don't free the fonts, it leads to a BUS error
849 // richard@whitequeen.com Jul 99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 gui.in_use = FALSE;
852 gui_mch_exit(rc);
853}
854
Bram Moolenaar9372a112005-12-06 19:59:18 +0000855#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +0200856 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000857# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858/*
859 * Called when the GUI shell is closed by the user. If there are no changed
860 * files Vim exits, otherwise there will be a dialog to ask the user what to
861 * do.
862 * When this function returns, Vim should NOT exit!
863 */
864 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100865gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866{
Bram Moolenaar3552e742021-05-29 12:21:58 +0200867 cmdmod_T save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaar3552e742021-05-29 12:21:58 +0200869 if (before_quit_autocmds(curwin, TRUE, FALSE))
870 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
Bram Moolenaar30613902019-12-01 22:11:18 +0100872 // Only exit when there are no changed files
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 exiting = TRUE;
874# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200875 cmdmod.cmod_flags |= CMOD_BROWSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876# endif
877# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +0200878 cmdmod.cmod_flags |= CMOD_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100880 // If there are changed buffers, present the user with a dialog if
881 // possible, otherwise give an error message.
Bram Moolenaar027387f2016-01-02 22:25:52 +0100882 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 getout(0);
884
885 exiting = FALSE;
886 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +0100887 gui_update_screen(); // redraw, window may show changed buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888}
889#endif
890
891/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200892 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 * first font name that works is used. If none is found, use the default
894 * font.
895 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
896 * Return OK when able to set the font. When it failed FAIL is returned and
897 * the fonts are unchanged.
898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100900gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901{
902#define FONTLEN 320
903 char_u font_name[FONTLEN];
904 int font_list_empty = FALSE;
905 int ret = FAIL;
906
907 if (!gui.in_use)
908 return FAIL;
909
910 font_name[0] = NUL;
911 if (*font_list == NUL)
912 font_list_empty = TRUE;
913 else
914 {
915#ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +0100916 // When using a fontset, the whole list of fonts is one name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (fontset)
918 ret = gui_mch_init_font(font_list, TRUE);
919 else
920#endif
921 while (*font_list != NUL)
922 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100923 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
925
Bram Moolenaar30613902019-12-01 22:11:18 +0100926 // Careful!!! The Win32 version of gui_mch_init_font(), when
927 // called with "*" will change p_guifont to the selected font
928 // name, which frees the old value. This makes font_list
929 // invalid. Thus when OK is returned here, font_list must no
930 // longer be used!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (gui_mch_init_font(font_name, FALSE) == OK)
932 {
K.Takata94373c42022-01-27 15:04:22 +0000933#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar30613902019-12-01 22:11:18 +0100934 // If it's a Unicode font, try setting 'guifontwide' to a
935 // similar double-width font.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
937 && strstr((char *)font_name, "10646") != NULL)
938 set_guifontwide(font_name);
939#endif
940 ret = OK;
941 break;
942 }
943 }
944 }
945
946 if (ret != OK
947 && STRCMP(font_list, "*") != 0
948 && (font_list_empty || gui.norm_font == NOFONT))
949 {
950 /*
951 * Couldn't load any font in 'font_list', keep the current font if
952 * there is one. If 'font_list' is empty, or if there is no current
953 * font, tell gui_mch_init_font() to try to find a font we can load.
954 */
955 ret = gui_mch_init_font(NULL, FALSE);
956 }
957
958 if (ret == OK)
959 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200960#ifndef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100961 // Set normal font as current font
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962# ifdef FEAT_XFONTSET
963 if (gui.fontset != NOFONTSET)
964 gui_mch_set_fontset(gui.fontset);
965 else
966# endif
967 gui_mch_set_font(gui.norm_font);
968#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100969 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 }
971
972 return ret;
973}
974
K.Takata94373c42022-01-27 15:04:22 +0000975#ifdef USE_SET_GUIFONTWIDE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976/*
977 * Try setting 'guifontwide' to a font twice as wide as "name".
978 */
979 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100980set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
982 int i = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +0100983 char_u wide_name[FONTLEN + 10]; // room for 2 * width and '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 char_u *wp = NULL;
985 char_u *p;
986 GuiFont font;
987
988 wp = wide_name;
989 for (p = name; *p != NUL; ++p)
990 {
991 *wp++ = *p;
992 if (*p == '-')
993 {
994 ++i;
Bram Moolenaar30613902019-12-01 22:11:18 +0100995 if (i == 6) // font type: change "--" to "-*-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 {
997 if (p[1] == '-')
998 *wp++ = '*';
999 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001000 else if (i == 12) // found the width
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 {
1002 ++p;
1003 i = getdigits(&p);
1004 if (i != 0)
1005 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001006 // Double the width specification.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 sprintf((char *)wp, "%d%s", i * 2, p);
1008 font = gui_mch_get_font(wide_name, FALSE);
1009 if (font != NOFONT)
1010 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001011 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 gui.wide_font = font;
1013 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001014 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
1016 }
1017 break;
1018 }
1019 }
1020 }
1021}
K.Takata94373c42022-01-27 15:04:22 +00001022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024/*
1025 * Get the font for 'guifontwide'.
1026 * Return FAIL for an invalid font name.
1027 */
1028 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001029gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
1031 GuiFont font = NOFONT;
1032 char_u font_name[FONTLEN];
1033 char_u *p;
1034
Bram Moolenaar30613902019-12-01 22:11:18 +01001035 if (!gui.in_use) // Can't allocate font yet, assume it's OK.
1036 return OK; // Will give an error message later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
1038 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1039 {
1040 for (p = p_guifontwide; *p != NUL; )
1041 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001042 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1044 font = gui_mch_get_font(font_name, FALSE);
1045 if (font != NOFONT)
1046 break;
1047 }
1048 if (font == NOFONT)
1049 return FAIL;
1050 }
1051
1052 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001053#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001054 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 if (font != NOFONT && gui.norm_font != NOFONT
1056 && pango_font_description_equal(font, gui.norm_font))
1057 {
1058 gui.wide_font = NOFONT;
1059 gui_mch_free_font(font);
1060 }
1061 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001064#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001065 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001066#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001067 /*
1068 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1069 * support those fonts for 'guifontwide'.
1070 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 return OK;
1073}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01001075#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001076/*
1077 * Set list of ascii characters that combined can create ligature.
1078 * Store them in char map for quick access from gui_gtk2_draw_string.
1079 */
1080 void
1081gui_set_ligatures(void)
1082{
1083 char_u *p;
1084
1085 if (*p_guiligatures != NUL)
1086 {
1087 // check for invalid characters
1088 for (p = p_guiligatures; *p != NUL; ++p)
1089 if (*p < 32 || *p > 127)
1090 {
1091 emsg(_(e_ascii_code_not_in_range));
1092 return;
1093 }
1094
1095 // store valid setting into ligatures_map
1096 CLEAR_FIELD(gui.ligatures_map);
1097 for (p = p_guiligatures; *p != NUL; ++p)
1098 gui.ligatures_map[*p] = 1;
1099 }
1100 else
1101 CLEAR_FIELD(gui.ligatures_map);
1102}
Dusan Popovicce59b9f2021-11-22 17:18:44 +00001103
1104/*
1105 * Adjust the columns to undraw for when the cursor is on ligatures.
1106 */
1107 static void
1108gui_adjust_undraw_cursor_for_ligatures(int *startcol, int *endcol)
1109{
1110 int off;
1111
1112 if (ScreenLines == NULL || *p_guiligatures == NUL)
1113 return;
1114
1115 // expand before the cursor for all the chars in gui.ligatures_map
1116 off = LineOffset[gui.cursor_row] + *startcol;
1117 if (gui.ligatures_map[ScreenLines[off]])
1118 while (*startcol > 0 && gui.ligatures_map[ScreenLines[--off]])
1119 (*startcol)--;
1120
1121 // expand after the cursor for all the chars in gui.ligatures_map
1122 off = LineOffset[gui.cursor_row] + *endcol;
1123 if (gui.ligatures_map[ScreenLines[off]])
1124 while (*endcol < ((int)screen_Columns - 1)
1125 && gui.ligatures_map[ScreenLines[++off]])
1126 (*endcol)++;
1127}
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001128#endif
1129
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001130 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001131gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
1133 gui.row = row;
1134 gui.col = col;
1135}
1136
1137/*
1138 * gui_check_pos - check if the cursor is on the screen.
1139 */
1140 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001141gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142{
1143 if (gui.row >= screen_Rows)
1144 gui.row = screen_Rows - 1;
1145 if (gui.col >= screen_Columns)
1146 gui.col = screen_Columns - 1;
1147 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1148 gui.cursor_is_valid = FALSE;
1149}
1150
1151/*
1152 * Redraw the cursor if necessary or when forced.
1153 * Careful: The contents of ScreenLines[] must match what is on the screen,
1154 * otherwise this goes wrong. May need to call out_flush() first.
1155 */
1156 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001157gui_update_cursor(
Bram Moolenaar30613902019-12-01 22:11:18 +01001158 int force, // when TRUE, update even when not moved
1159 int clear_selection) // clear selection under cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160{
1161 int cur_width = 0;
1162 int cur_height = 0;
1163 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001164 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001166#ifdef FEAT_TERMINAL
1167 guicolor_T shape_fg = INVALCOLOR;
1168 guicolor_T shape_bg = INVALCOLOR;
1169#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001170 guicolor_T cfg, cbg, cc; // cursor fore-/background color
1171 int cattr; // cursor attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 int attr;
1173 attrentry_T *aep = NULL;
1174
Bram Moolenaar30613902019-12-01 22:11:18 +01001175 // Don't update the cursor when halfway busy scrolling or the screen size
1176 // doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then.
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001177 if (!can_update_cursor || screen_Columns != gui.num_cols
1178 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 return;
1180
1181 gui_check_pos();
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001182
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001183 if (gui.cursor_is_valid && !force
1184 && gui.row == gui.cursor_row && gui.col == gui.cursor_col)
1185 return;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02001186
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001187 gui_undraw_cursor();
1188
1189 // If a cursor-less sleep is ongoing, leave the cursor invisible
1190 if (cursor_is_sleeping())
1191 return;
1192
1193 if (gui.row < 0)
1194 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001195#ifdef HAVE_INPUT_METHOD
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001196 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1197 im_set_position(gui.row, gui.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001199 gui.cursor_row = gui.row;
1200 gui.cursor_col = gui.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001202 // Only write to the screen after ScreenLines[] has been initialized
1203 if (!screen_cleared || ScreenLines == NULL)
1204 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001206 // Clear the selection if we are about to write over it
1207 if (clear_selection)
1208 clip_may_clear_selection(gui.row, gui.row);
1209 // Check that the cursor is inside the shell (resizing may have made
1210 // it invalid)
1211 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1212 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001214 gui.cursor_is_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001216 /*
1217 * How the cursor is drawn depends on the current mode.
1218 * When in a terminal window use the shape/color specified there.
1219 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001220#ifdef FEAT_TERMINAL
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001221 if (terminal_is_active())
1222 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
1223 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001224#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001225 shape = &shape_table[get_shape_idx(FALSE)];
1226 if (State & MODE_LANGMAP)
1227 id = shape->id_lm;
1228 else
1229 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001231 // get the colors and attributes for the cursor. Default is inverted
1232 cfg = INVALCOLOR;
1233 cbg = INVALCOLOR;
1234 cattr = HL_INVERSE;
1235 gui_mch_set_blinking(shape->blinkwait,
1236 shape->blinkon,
1237 shape->blinkoff);
1238 if (shape->blinkwait == 0 || shape->blinkon == 0
1239 || shape->blinkoff == 0)
1240 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001241#ifdef FEAT_TERMINAL
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001242 if (shape_bg != INVALCOLOR)
1243 {
1244 cattr = 0;
1245 cfg = shape_fg;
1246 cbg = shape_bg;
1247 }
1248 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 if (id > 0)
1251 {
1252 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar54612582019-11-21 17:13:31 +01001253#if defined(HAVE_INPUT_METHOD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
1255 static int iid;
1256 guicolor_T fg, bg;
1257
Bram Moolenaarc236c162008-07-13 17:41:49 +00001258 if (
Bram Moolenaar54612582019-11-21 17:13:31 +01001259# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001260 preedit_get_status()
1261# else
1262 im_get_status()
1263# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001264 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
1266 iid = syn_name2id((char_u *)"CursorIM");
1267 if (iid > 0)
1268 {
1269 syn_id2colors(iid, &fg, &bg);
1270 if (bg != INVALCOLOR)
1271 cbg = bg;
1272 if (fg != INVALCOLOR)
1273 cfg = fg;
1274 }
1275 }
1276 }
1277#endif
1278 }
1279
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001280 /*
1281 * Get the attributes for the character under the cursor.
1282 * When no cursor color was given, use the character color.
1283 */
1284 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1285 if (attr > HL_ALL)
1286 aep = syn_gui_attr2entry(attr);
1287 if (aep != NULL)
1288 {
1289 attr = aep->ae_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 if (cfg == INVALCOLOR)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001291 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1292 : aep->ae_u.gui.fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (cbg == INVALCOLOR)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001294 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1295 : aep->ae_u.gui.bg_color);
1296 }
1297 if (cfg == INVALCOLOR)
1298 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1299 if (cbg == INVALCOLOR)
1300 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301
1302#ifdef FEAT_XIM
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001303 if (aep != NULL)
1304 {
1305 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1306 : aep->ae_u.gui.bg_color);
1307 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1308 : aep->ae_u.gui.fg_color);
1309 if (xim_bg_color == INVALCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001311 : gui.back_pixel;
1312 if (xim_fg_color == INVALCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001314 : gui.norm_pixel;
1315 }
1316 else
1317 {
1318 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1319 : gui.back_pixel;
1320 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1321 : gui.norm_pixel;
1322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323#endif
1324
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001325 attr &= ~HL_INVERSE;
1326 if (cattr & HL_INVERSE)
1327 {
1328 cc = cbg;
1329 cbg = cfg;
1330 cfg = cc;
1331 }
1332 cattr &= ~HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001334 /*
1335 * When we don't have window focus, draw a hollow cursor.
1336 */
1337 if (!gui.in_focus)
1338 {
1339 gui_mch_draw_hollow_cursor(cbg);
1340 return;
1341 }
1342
1343 old_hl_mask = gui.highlight_mask;
1344 if (shape->shape == SHAPE_BLOCK)
1345 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001347 * Draw the text character with the cursor colors. Use the
1348 * character attributes plus the cursor attributes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 */
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001350 gui.highlight_mask = (cattr | attr);
1351 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1352 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1353 }
1354 else
1355 {
1356#if defined(FEAT_RIGHTLEFT)
1357 int col_off = FALSE;
1358#endif
1359 /*
1360 * First draw the partial cursor, then overwrite with the text
1361 * character, using a transparent background.
1362 */
1363 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001365 cur_height = gui.char_height;
1366 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368 else
1369 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001370 cur_height = (gui.char_height * shape->percentage + 99) / 100;
1371 cur_width = gui.char_width;
1372 }
1373 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1374 LineOffset[gui.row] + screen_Columns) > 1)
1375 {
1376 // Double wide character.
1377 if (shape->shape != SHAPE_VER)
1378 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001379#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001380 if (CURSOR_BAR_RIGHT)
1381 {
1382 // gui.col points to the left half of the character but
1383 // the vertical line needs to be on the right half.
1384 // A double-wide horizontal line is also drawn from the
1385 // right half in gui_mch_draw_part_cursor().
1386 col_off = TRUE;
1387 ++gui.col;
Bram Moolenaar13505972019-01-24 15:04:48 +01001388 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001389#endif
1390 }
1391 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001392#if defined(FEAT_RIGHTLEFT)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001393 if (col_off)
1394 --gui.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#endif
1396
Bram Moolenaar30613902019-12-01 22:11:18 +01001397#ifndef FEAT_GUI_MSWIN // doesn't seem to work for MSWindows
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001398 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1399 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1400 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1401 (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001404 gui.highlight_mask = old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405}
1406
1407#if defined(FEAT_MENU) || defined(PROTO)
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01001408 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001409gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001411# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 if (gui.menu_is_active && gui.in_use)
1413 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1414# endif
1415}
1416#endif
1417
1418/*
1419 * Position the various GUI components (text area, menu). The vertical
1420 * scrollbars are NOT handled here. See gui_update_scrollbars().
1421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001423gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
1425 int text_area_x;
1426 int text_area_y;
1427 int text_area_width;
1428 int text_area_height;
1429
Bram Moolenaar30613902019-12-01 22:11:18 +01001430 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 ++hold_gui_events;
1432
1433 text_area_x = 0;
1434 if (gui.which_scrollbars[SBAR_LEFT])
1435 text_area_x += gui.scrollbar_width;
1436
1437 text_area_y = 0;
1438#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1439 gui.menu_width = total_width;
1440 if (gui.menu_is_active)
1441 text_area_y += gui.menu_height;
1442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
K.Takata6e1d31e2022-02-03 13:05:32 +00001444#if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001445 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001446 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001447 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001448#endif
1449
Bram Moolenaar0b962e52022-04-03 18:02:37 +01001450#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) \
K.Takatac81e9bf2022-01-16 14:15:49 +00001451 || defined(FEAT_GUI_HAIKU) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1453 {
Bram Moolenaar0b962e52022-04-03 18:02:37 +01001454# if defined(FEAT_GUI_HAIKU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 gui_mch_set_toolbar_pos(0, text_area_y,
1456 gui.menu_width, gui.toolbar_height);
1457# endif
1458 text_area_y += gui.toolbar_height;
1459 }
1460#endif
1461
K.Takata6e1d31e2022-02-03 13:05:32 +00001462#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_HAIKU)
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001463 gui_mch_set_tabline_pos(0, text_area_y,
1464 gui.menu_width, gui.tabline_height);
1465 if (gui_has_tabline())
1466 text_area_y += gui.tabline_height;
1467#endif
1468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1470 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1471
1472 gui_mch_set_text_area_pos(text_area_x,
1473 text_area_y,
1474 text_area_width,
1475 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001476#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 + xim_get_status_area_height()
1478#endif
1479 );
1480#ifdef FEAT_MENU
1481 gui_position_menu();
1482#endif
1483 if (gui.which_scrollbars[SBAR_BOTTOM])
1484 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1485 text_area_x,
Bram Moolenaar203ec772020-07-17 20:43:43 +02001486 text_area_y + text_area_height
1487 + gui_mch_get_scrollbar_ypadding(),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 text_area_width,
1489 gui.scrollbar_height);
1490 gui.left_sbar_x = 0;
Bram Moolenaar203ec772020-07-17 20:43:43 +02001491 gui.right_sbar_x = text_area_x + text_area_width
1492 + gui_mch_get_scrollbar_xpadding();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494 --hold_gui_events;
1495}
1496
Bram Moolenaar02743632005-07-25 20:42:36 +00001497/*
1498 * Get the width of the widgets and decorations to the side of the text area.
1499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001501gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
1503 int base_width;
1504
1505 base_width = 2 * gui.border_offset;
1506 if (gui.which_scrollbars[SBAR_LEFT])
1507 base_width += gui.scrollbar_width;
1508 if (gui.which_scrollbars[SBAR_RIGHT])
1509 base_width += gui.scrollbar_width;
1510 return base_width;
1511}
1512
Bram Moolenaar02743632005-07-25 20:42:36 +00001513/*
1514 * Get the height of the widgets and decorations above and below the text area.
1515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001517gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 int base_height;
1520
1521 base_height = 2 * gui.border_offset;
1522 if (gui.which_scrollbars[SBAR_BOTTOM])
1523 base_height += gui.scrollbar_height;
1524#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001525 // We can't take the sizes properly into account until anything is
1526 // realized. Therefore we recalculate all the values here just before
1527 // setting the size. (--mdcki)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528#else
1529# ifdef FEAT_MENU
1530 if (gui.menu_is_active)
1531 base_height += gui.menu_height;
1532# endif
1533# ifdef FEAT_TOOLBAR
1534 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 base_height += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001537# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001538 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001539 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001540 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001541# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1543 base_height += gui_mch_text_area_extra_height();
1544# endif
1545#endif
1546 return base_height;
1547}
1548
1549/*
1550 * Should be called after the GUI shell has been resized. Its arguments are
1551 * the new width and height of the shell in pixels.
1552 */
1553 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001554gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
1556 static int busy = FALSE;
1557
Bram Moolenaar30613902019-12-01 22:11:18 +01001558 if (!gui.shell_created) // ignore when still initializing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 return;
1560
1561 /*
1562 * Can't resize the screen while it is being redrawn. Remember the new
1563 * size and handle it later.
1564 */
1565 if (updating_screen || busy)
1566 {
1567 new_pixel_width = pixel_width;
1568 new_pixel_height = pixel_height;
1569 return;
1570 }
1571
1572again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001573 new_pixel_width = 0;
1574 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 busy = TRUE;
1576
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001577#ifdef FEAT_GUI_HAIKU
1578 vim_lock_screen();
1579#endif
1580
Bram Moolenaar30613902019-12-01 22:11:18 +01001581 // Flush pending output before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 out_flush();
1583
1584 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001585 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
1587 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 /*
1591 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1592 * at the last line here (why does it have to be one row too low?).
1593 */
Bram Moolenaar24959102022-05-07 20:01:16 +01001594 if (State == MODE_ASKMORE || State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 gui.row = gui.num_rows;
1596
Bram Moolenaar30613902019-12-01 22:11:18 +01001597 // Only comparing Rows and Columns may be sufficient, but let's stay on
1598 // the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
lilydjwg6e0a18f2024-01-29 20:54:28 +01001600 || gui.num_rows != Rows || gui.num_cols != Columns || gui.force_redraw)
1601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 shell_resized();
lilydjwg6e0a18f2024-01-29 20:54:28 +01001603 gui.force_redraw = 0;
1604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001606#ifdef FEAT_GUI_HAIKU
1607 vim_unlock_screen();
1608#endif
1609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 gui_update_scrollbars(TRUE);
1611 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001612#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 xim_set_status_area();
1614#endif
1615
1616 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001617
Bram Moolenaar30613902019-12-01 22:11:18 +01001618 // We may have been called again while redrawing the screen.
1619 // Need to do it all again with the latest size then. But only if the size
1620 // actually changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 if (new_pixel_height)
1622 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001623 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1624 {
1625 new_pixel_width = 0;
1626 new_pixel_height = 0;
1627 }
1628 else
1629 {
1630 pixel_width = new_pixel_width;
1631 pixel_height = new_pixel_height;
1632 goto again;
1633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
1635}
1636
1637/*
1638 * Check if gui_resize_shell() must be called.
1639 */
1640 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001641gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (new_pixel_height)
Bram Moolenaar30613902019-12-01 22:11:18 +01001644 // careful: gui_resize_shell() may postpone the resize again if we
1645 // were called indirectly by it
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001646 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647}
1648
1649 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001650gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
1652 Rows = gui.num_rows;
1653 Columns = gui.num_cols;
1654 return OK;
1655}
1656
1657/*
1658 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001659 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1660 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001661 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1662 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001665gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001666 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001667 int fit_to_display,
Bram Moolenaar30613902019-12-01 22:11:18 +01001668 int direction) // RESIZE_HOR, RESIZE_VER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
1670 int base_width;
1671 int base_height;
1672 int width;
1673 int height;
1674 int min_width;
1675 int min_height;
1676 int screen_w;
1677 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001678#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001679 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001680 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001681#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001682 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
1684 if (!gui.shell_created)
1685 return;
1686
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001687#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01001688 // If not setting to a user specified size and maximized, calculate the
1689 // number of characters that fit in the maximized window.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001690 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1691 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 {
1693 gui_mch_newfont();
1694 return;
1695 }
1696#endif
1697
1698 base_width = gui_get_base_width();
1699 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001700 if (fit_to_display)
Bram Moolenaar30613902019-12-01 22:11:18 +01001701 // Remember the original window position.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001702 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001703
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001704 width = Columns * gui.char_width + base_width;
1705 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706
1707 if (fit_to_display)
1708 {
1709 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001710 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 {
1712 Columns = (screen_w - base_width) / gui.char_width;
1713 if (Columns < MIN_COLUMNS)
1714 Columns = MIN_COLUMNS;
1715 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001716#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001717 ++did_adjust;
1718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001720 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 {
1722 Rows = (screen_h - base_height) / gui.char_height;
1723 check_shellsize();
1724 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001725#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001726 ++did_adjust;
1727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001729#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001730 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1731 && height + gui.char_height >= screen_h))
Bram Moolenaar30613902019-12-01 22:11:18 +01001732 // don't unmaximize if at maximum size
Bram Moolenaar09736232009-09-23 16:14:49 +00001733 un_maximize = FALSE;
1734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001736 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 gui.num_cols = Columns;
1738 gui.num_rows = Rows;
1739
1740 min_width = base_width + MIN_COLUMNS * gui.char_width;
1741 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001742 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001743
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001744#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001745 if (un_maximize)
1746 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001747 // If the window size is smaller than the screen unmaximize the
1748 // window, otherwise resizing won't work.
Bram Moolenaar09736232009-09-23 16:14:49 +00001749 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1750 if ((width + gui.char_width < screen_w
1751 || height + gui.char_height * 2 < screen_h)
1752 && gui_mch_maximized())
1753 gui_mch_unmaximize();
1754 }
1755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
1757 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001758 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001760 if (fit_to_display && x >= 0 && y >= 0)
1761 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001762 // Some window managers put the Vim window left of/above the screen.
1763 // Only change the position if it wasn't already negative before
1764 // (happens on MS-Windows with a secondary monitor).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 gui_mch_update();
1766 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1767 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1768 }
1769
1770 gui_position_components(width);
1771 gui_update_scrollbars(TRUE);
1772 gui_reset_scroll_region();
1773}
1774
1775/*
1776 * Called when Rows and/or Columns has changed.
1777 */
1778 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001779gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
1781 gui_reset_scroll_region();
1782}
1783
1784/*
1785 * Make scroll region cover whole screen.
1786 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001787 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001788gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 gui.scroll_region_top = 0;
1791 gui.scroll_region_bot = gui.num_rows - 1;
1792 gui.scroll_region_left = 0;
1793 gui.scroll_region_right = gui.num_cols - 1;
1794}
1795
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001796 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001797gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
Bram Moolenaar30613902019-12-01 22:11:18 +01001799 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 gui.highlight_mask = mask;
Bram Moolenaar30613902019-12-01 22:11:18 +01001801 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 gui.highlight_mask |= mask;
1803}
1804
1805 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001806gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807{
Bram Moolenaar30613902019-12-01 22:11:18 +01001808 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 gui.highlight_mask = HL_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001810 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 gui.highlight_mask &= ~mask;
1812}
1813
1814/*
1815 * Clear a rectangular region of the screen from text pos (row1, col1) to
1816 * (row2, col2) inclusive.
1817 */
1818 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001819gui_clear_block(
1820 int row1,
1821 int col1,
1822 int row2,
1823 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
Bram Moolenaar30613902019-12-01 22:11:18 +01001825 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 clip_may_clear_selection(row1, row2);
1827
1828 gui_mch_clear_block(row1, col1, row2, col2);
1829
Bram Moolenaar30613902019-12-01 22:11:18 +01001830 // Invalidate cursor if it was in this block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1832 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1833 gui.cursor_is_valid = FALSE;
1834}
1835
1836/*
1837 * Write code to update the cursor later. This avoids the need to flush the
1838 * output buffer before calling gui_update_cursor().
1839 */
1840 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001841gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001843 OUT_STR("\033|s");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844}
1845
1846 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001847gui_write(
1848 char_u *s,
1849 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850{
1851 char_u *p;
1852 int arg1 = 0, arg2 = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01001853 int force_cursor = FALSE; // force cursor update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 int force_scrollbar = FALSE;
1855 static win_T *old_curwin = NULL;
1856
Bram Moolenaar30613902019-12-01 22:11:18 +01001857// #define DEBUG_GUI_WRITE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858#ifdef DEBUG_GUI_WRITE
1859 {
1860 int i;
1861 char_u *str;
1862
1863 printf("gui_write(%d):\n ", len);
1864 for (i = 0; i < len; i++)
1865 if (s[i] == ESC)
1866 {
1867 if (i != 0)
1868 printf("\n ");
1869 printf("<ESC>");
1870 }
1871 else
1872 {
1873 str = transchar_byte(s[i]);
1874 if (str[0] && str[1])
1875 printf("<%s>", (char *)str);
1876 else
1877 printf("%s", (char *)str);
1878 }
1879 printf("\n");
1880 }
1881#endif
1882 while (len)
1883 {
1884 if (s[0] == ESC && s[1] == '|')
1885 {
1886 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001887 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
1889 arg1 = getdigits(&p);
1890 if (p > s + len)
1891 break;
1892 if (*p == ';')
1893 {
1894 ++p;
1895 arg2 = getdigits(&p);
1896 if (p > s + len)
1897 break;
1898 }
1899 }
1900 switch (*p)
1901 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001902 case 'C': // Clear screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 clip_scroll_selection(9999);
1904 gui_mch_clear_all();
1905 gui.cursor_is_valid = FALSE;
1906 force_scrollbar = TRUE;
1907 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001908 case 'M': // Move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 gui_set_cursor(arg1, arg2);
1910 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001911 case 's': // force cursor (shape) update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 force_cursor = TRUE;
1913 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001914 case 'R': // Set scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (arg1 < arg2)
1916 {
1917 gui.scroll_region_top = arg1;
1918 gui.scroll_region_bot = arg2;
1919 }
1920 else
1921 {
1922 gui.scroll_region_top = arg2;
1923 gui.scroll_region_bot = arg1;
1924 }
1925 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001926 case 'V': // Set vertical scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (arg1 < arg2)
1928 {
1929 gui.scroll_region_left = arg1;
1930 gui.scroll_region_right = arg2;
1931 }
1932 else
1933 {
1934 gui.scroll_region_left = arg2;
1935 gui.scroll_region_right = arg1;
1936 }
1937 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001938 case 'd': // Delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 gui_delete_lines(gui.row, 1);
1940 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001941 case 'D': // Delete lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 gui_delete_lines(gui.row, arg1);
1943 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001944 case 'i': // Insert line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 gui_insert_lines(gui.row, 1);
1946 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001947 case 'I': // Insert lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 gui_insert_lines(gui.row, arg1);
1949 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001950 case '$': // Clear to end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 gui_clear_block(gui.row, gui.col, gui.row,
1952 (int)Columns - 1);
1953 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001954 case 'h': // Turn on highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 gui_start_highlight(arg1);
1956 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001957 case 'H': // Turn off highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 gui_stop_highlight(arg1);
1959 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001960 case 'f': // flash the window (visual bell)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1962 break;
1963 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01001964 p = s + 1; // Skip the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 break;
1966 }
1967 len -= (int)(++p - s);
1968 s = p;
1969 }
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001970 else if (s[0] < 0x20 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971#ifdef FEAT_SIGN_ICONS
1972 && s[0] != SIGN_BYTE
1973# ifdef FEAT_NETBEANS_INTG
1974 && s[0] != MULTISIGN_BYTE
1975# endif
1976#endif
1977 )
1978 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001979 if (s[0] == '\n') // NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
1981 gui.col = 0;
1982 if (gui.row < gui.scroll_region_bot)
1983 gui.row++;
1984 else
1985 gui_delete_lines(gui.scroll_region_top, 1);
1986 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001987 else if (s[0] == '\r') // CR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
1989 gui.col = 0;
1990 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001991 else if (s[0] == '\b') // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
1993 if (gui.col)
1994 --gui.col;
1995 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001996 else if (s[0] == Ctrl_L) // cursor-right
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
1998 ++gui.col;
1999 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002000 else if (s[0] == Ctrl_G) // Beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
2002 gui_mch_beep();
2003 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002004 // Other Ctrl character: shouldn't happen!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
Bram Moolenaar30613902019-12-01 22:11:18 +01002006 --len; // Skip this char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 ++s;
2008 }
2009 else
2010 {
2011 p = s;
2012 while (len > 0 && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 *p >= 0x20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014#ifdef FEAT_SIGN_ICONS
2015 || *p == SIGN_BYTE
2016# ifdef FEAT_NETBEANS_INTG
2017 || *p == MULTISIGN_BYTE
2018# endif
2019#endif
2020 ))
2021 {
2022 len--;
2023 p++;
2024 }
2025 gui_outstr(s, (int)(p - s));
2026 s = p;
2027 }
2028 }
2029
Bram Moolenaar30613902019-12-01 22:11:18 +01002030 // Postponed update of the cursor (won't work if "can_update_cursor" isn't
2031 // set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (force_cursor)
2033 gui_update_cursor(TRUE, TRUE);
2034
Bram Moolenaar30613902019-12-01 22:11:18 +01002035 // When switching to another window the dragging must have stopped.
2036 // Required for GTK, dragged_sb isn't reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 if (old_curwin != curwin)
2038 gui.dragged_sb = SBAR_NONE;
2039
Bram Moolenaar30613902019-12-01 22:11:18 +01002040 // Update the scrollbars after clearing the screen or when switched
2041 // to another window.
2042 // Update the horizontal scrollbar always, it's difficult to check all
2043 // situations where it might change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (force_scrollbar || old_curwin != curwin)
2045 gui_update_scrollbars(force_scrollbar);
2046 else
2047 gui_update_horiz_scrollbar(FALSE);
2048 old_curwin = curwin;
2049
2050 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002051 * We need to make sure this is cleared since GTK doesn't tell us when
2052 * the user is done dragging.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 */
Bram Moolenaar0b962e52022-04-03 18:02:37 +01002054#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 gui.dragged_sb = SBAR_NONE;
2056#endif
2057
Bram Moolenaar30613902019-12-01 22:11:18 +01002058 gui_may_flush(); // In case vim decides to take a nap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059}
2060
2061/*
2062 * When ScreenLines[] is invalid, updating the cursor should not be done, it
2063 * produces wrong results. Call gui_dont_update_cursor() before that code and
2064 * gui_can_update_cursor() afterwards.
2065 */
2066 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02002067gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002069 if (!gui.in_use)
2070 return;
2071
2072 // Undraw the cursor now, we probably can't do it after the change.
2073 if (undraw)
2074 gui_undraw_cursor();
2075 can_update_cursor = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076}
2077
2078 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002079gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
2081 can_update_cursor = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002082 // No need to update the cursor right now, there is always more output
2083 // after scrolling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084}
2085
Bram Moolenaara338adc2018-01-31 20:51:47 +01002086/*
2087 * Disable issuing gui_mch_flush().
2088 */
2089 void
2090gui_disable_flush(void)
2091{
2092 ++disable_flush;
2093}
2094
2095/*
2096 * Enable issuing gui_mch_flush().
2097 */
2098 void
2099gui_enable_flush(void)
2100{
2101 --disable_flush;
2102}
2103
2104/*
2105 * Issue gui_mch_flush() if it is not disabled.
2106 */
2107 void
2108gui_may_flush(void)
2109{
2110 if (disable_flush == 0)
2111 gui_mch_flush();
2112}
2113
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002115gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
2120 if (len == 0)
2121 return;
2122
2123 if (len < 0)
2124 len = (int)STRLEN(s);
2125
2126 while (len > 0)
2127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if (has_mbyte)
2129 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002130 // Find out how many chars fit in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 cells = 0;
2132 for (this_len = 0; this_len < len; )
2133 {
2134 cells += (*mb_ptr2cells)(s + this_len);
2135 if (gui.col + cells > Columns)
2136 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002137 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 }
2139 if (this_len > len)
Bram Moolenaar30613902019-12-01 22:11:18 +01002140 this_len = len; // don't include following composing char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
Bram Moolenaarb26592a2022-07-12 17:34:31 +01002142 else if (gui.col + len > Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 this_len = Columns - gui.col;
2144 else
2145 this_len = len;
2146
2147 (void)gui_outstr_nowrap(s, this_len,
2148 0, (guicolor_T)0, (guicolor_T)0, 0);
2149 s += this_len;
2150 len -= this_len;
Bram Moolenaar30613902019-12-01 22:11:18 +01002151 // fill up for a double-width char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (len > 0 && gui.col < Columns)
2153 (void)gui_outstr_nowrap((char_u *)" ", 1,
2154 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01002155 // The cursor may wrap to the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 if (gui.col >= Columns)
2157 {
2158 gui.col = 0;
2159 gui.row++;
2160 }
2161 }
2162}
2163
2164/*
2165 * Output one character (may be one or two display cells).
2166 * Caller must check for valid "off".
2167 * Returns FAIL or OK, just like gui_outstr_nowrap().
2168 */
2169 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002170gui_screenchar(
Bram Moolenaar30613902019-12-01 22:11:18 +01002171 int off, // Offset from start of screen
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002172 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002173 guicolor_T fg, // colors for cursor
2174 guicolor_T bg, // colors for cursor
2175 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 char_u buf[MB_MAXBYTES + 1];
2178
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002179 // Don't draw right half of a double-width UTF-8 char. "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (enc_utf8 && ScreenLines[off] == 0)
2181 return OK;
2182
2183 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002184 // Draw UTF-8 multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2186 flags, fg, bg, back);
2187
2188 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2189 {
2190 buf[0] = ScreenLines[off];
2191 buf[1] = ScreenLines2[off];
2192 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2193 }
2194
Bram Moolenaar30613902019-12-01 22:11:18 +01002195 // Draw non-multi-byte character or DBCS character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002197 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199}
2200
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002201#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202/*
2203 * Output the string at the given screen position. This is used in place
2204 * of gui_screenchar() where possible because Pango needs as much context
2205 * as possible to work nicely. It's a lot faster as well.
2206 */
2207 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002208gui_screenstr(
Bram Moolenaar30613902019-12-01 22:11:18 +01002209 int off, // Offset from start of screen
2210 int len, // string length in screen cells
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002211 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002212 guicolor_T fg, // colors for cursor
2213 guicolor_T bg, // colors for cursor
2214 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216 char_u *buf;
2217 int outlen = 0;
2218 int i;
2219 int retval;
2220
Bram Moolenaar30613902019-12-01 22:11:18 +01002221 if (len <= 0) // "cannot happen"?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 return OK;
2223
2224 if (enc_utf8)
2225 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002226 buf = alloc(len * MB_MAXBYTES + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002228 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 for (i = off; i < off + len; ++i)
2231 {
2232 if (ScreenLines[i] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002233 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234
2235 if (ScreenLinesUC[i] == 0)
2236 buf[outlen++] = ScreenLines[i];
2237 else
2238 outlen += utfc_char2bytes(i, buf + outlen);
2239 }
2240
Bram Moolenaar30613902019-12-01 22:11:18 +01002241 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2243 vim_free(buf);
2244
2245 return retval;
2246 }
2247 else if (enc_dbcs == DBCS_JPNU)
2248 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002249 buf = alloc(len * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002251 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253 for (i = off; i < off + len; ++i)
2254 {
2255 buf[outlen++] = ScreenLines[i];
2256
Bram Moolenaar30613902019-12-01 22:11:18 +01002257 // handle double-byte single-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (ScreenLines[i] == 0x8e)
2259 buf[outlen++] = ScreenLines2[i];
2260 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2261 buf[outlen++] = ScreenLines[++i];
2262 }
2263
Bram Moolenaar30613902019-12-01 22:11:18 +01002264 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2266 vim_free(buf);
2267
2268 return retval;
2269 }
2270 else
2271 {
2272 return gui_outstr_nowrap(&ScreenLines[off], len,
2273 flags, fg, bg, back);
2274 }
2275}
Bram Moolenaar30613902019-12-01 22:11:18 +01002276#endif // FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278/*
2279 * Output the given string at the current cursor position. If the string is
2280 * too long to fit on the line, then it is truncated.
2281 * "flags":
2282 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2283 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002284 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 * background.
2286 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2287 * it.
2288 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2289 * FAIL (the caller should start drawing "back" chars back).
2290 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002291 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002292gui_outstr_nowrap(
2293 char_u *s,
2294 int len,
2295 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002296 guicolor_T fg, // colors for cursor
2297 guicolor_T bg, // colors for cursor
2298 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299{
2300 long_u highlight_mask;
2301 long_u hl_mask_todo;
2302 guicolor_T fg_color;
2303 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002304 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002305#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002307 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308# ifdef FEAT_XFONTSET
2309 GuiFontset fontset = NOFONTSET;
2310# endif
2311#endif
2312 attrentry_T *aep = NULL;
2313 int draw_flags;
2314 int col = gui.col;
2315#ifdef FEAT_SIGN_ICONS
2316 int draw_sign = FALSE;
Bram Moolenaarc7283072019-07-16 20:12:44 +02002317 int signcol = 0;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002318 char_u extra[18];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319# ifdef FEAT_NETBEANS_INTG
2320 int multi_sign = FALSE;
2321# endif
2322#endif
2323
2324 if (len < 0)
2325 len = (int)STRLEN(s);
2326 if (len == 0)
2327 return OK;
2328
2329#ifdef FEAT_SIGN_ICONS
2330 if (*s == SIGN_BYTE
2331# ifdef FEAT_NETBEANS_INTG
2332 || *s == MULTISIGN_BYTE
2333# endif
Bram Moolenaar0231f832019-07-12 19:22:22 +02002334 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
John Marriottd2fcbb42025-05-07 19:59:42 +02002336 size_t slen = 2; // 2 spaces by default
2337
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338# ifdef FEAT_NETBEANS_INTG
2339 if (*s == MULTISIGN_BYTE)
2340 multi_sign = TRUE;
2341# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002342 // draw spaces instead
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002343 if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
2344 (curwin->w_p_nu || curwin->w_p_rnu))
2345 {
John Marriottd2fcbb42025-05-07 19:59:42 +02002346 int n = number_width(curwin);
2347
2348 slen = MIN(n, (int)sizeof(extra) - 1);
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002349 }
John Marriottd2fcbb42025-05-07 19:59:42 +02002350 vim_memset(extra, ' ', slen);
2351 extra[slen] = NUL;
2352 s = extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 if (len == 1 && col > 0)
2354 --col;
John Marriottd2fcbb42025-05-07 19:59:42 +02002355 len = (int)slen;
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002356 if (len > 2)
Bram Moolenaar0231f832019-07-12 19:22:22 +02002357 // right align sign icon in the number column
2358 signcol = col + len - 3;
2359 else
2360 signcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 draw_sign = TRUE;
2362 highlight_mask = 0;
2363 }
2364 else
2365#endif
2366 if (gui.highlight_mask > HL_ALL)
2367 {
2368 aep = syn_gui_attr2entry(gui.highlight_mask);
Bram Moolenaar30613902019-12-01 22:11:18 +01002369 if (aep == NULL) // highlighting not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 highlight_mask = 0;
2371 else
2372 highlight_mask = aep->ae_attr;
2373 }
2374 else
2375 highlight_mask = gui.highlight_mask;
2376 hl_mask_todo = highlight_mask;
2377
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002378#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002379 // Set the font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2381 font = aep->ae_u.gui.font;
2382# ifdef FEAT_XFONTSET
2383 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2384 fontset = aep->ae_u.gui.fontset;
2385# endif
2386 else
2387 {
2388# ifdef FEAT_XFONTSET
2389 if (gui.fontset != NOFONTSET)
2390 fontset = gui.fontset;
2391 else
2392# endif
2393 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2394 {
2395 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2396 {
2397 font = gui.boldital_font;
2398 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2399 }
2400 else if (gui.bold_font != NOFONT)
2401 {
2402 font = gui.bold_font;
2403 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2404 }
2405 else
2406 font = gui.norm_font;
2407 }
2408 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2409 {
2410 font = gui.ital_font;
2411 hl_mask_todo &= ~HL_ITALIC;
2412 }
2413 else
2414 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002415
Bram Moolenaardb250522013-06-17 22:43:25 +02002416 /*
2417 * Choose correct wide_font by font. wide_font should be set with font
2418 * at same time in above block. But it will make many "ifdef" nasty
2419 * blocks. So we do it here.
2420 */
2421 if (font == gui.boldital_font && gui.wide_boldital_font)
2422 wide_font = gui.wide_boldital_font;
2423 else if (font == gui.bold_font && gui.wide_bold_font)
2424 wide_font = gui.wide_bold_font;
2425 else if (font == gui.ital_font && gui.wide_ital_font)
2426 wide_font = gui.wide_ital_font;
2427 else if (font == gui.norm_font && gui.wide_font)
2428 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430# ifdef FEAT_XFONTSET
2431 if (fontset != NOFONTSET)
2432 gui_mch_set_fontset(fontset);
2433 else
2434# endif
2435 gui_mch_set_font(font);
2436#endif
2437
2438 draw_flags = 0;
2439
Bram Moolenaar30613902019-12-01 22:11:18 +01002440 // Set the color
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 bg_color = gui.back_pixel;
2442 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2443 {
2444 draw_flags |= DRAW_CURSOR;
2445 fg_color = fg;
2446 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002447 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
2449 else if (aep != NULL)
2450 {
2451 fg_color = aep->ae_u.gui.fg_color;
2452 if (fg_color == INVALCOLOR)
2453 fg_color = gui.norm_pixel;
2454 bg_color = aep->ae_u.gui.bg_color;
2455 if (bg_color == INVALCOLOR)
2456 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002457 sp_color = aep->ae_u.gui.sp_color;
2458 if (sp_color == INVALCOLOR)
2459 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002464 sp_color = fg_color;
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 gui_mch_set_fg_color(bg_color);
2470 gui_mch_set_bg_color(fg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472 else
2473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 gui_mch_set_fg_color(fg_color);
2475 gui_mch_set_bg_color(bg_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002477 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
Bram Moolenaar30613902019-12-01 22:11:18 +01002479 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 if (!(flags & GUI_MON_NOCLEAR))
2481 clip_may_clear_selection(gui.row, gui.row);
2482
2483
Bram Moolenaar30613902019-12-01 22:11:18 +01002484 // If there's no bold font, then fake it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2486 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
2488 /*
2489 * When drawing bold or italic characters the spill-over from the left
2490 * neighbor may be destroyed. Let the caller backup to start redrawing
2491 * just after a blank.
2492 */
2493 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2494 return FAIL;
2495
Bram Moolenaare60acc12011-05-10 16:41:25 +02002496#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002497 // If there's no italic font, then fake it.
2498 // For GTK2, we don't need a different font for italic style.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 if (hl_mask_todo & HL_ITALIC)
2500 draw_flags |= DRAW_ITALIC;
2501
Bram Moolenaar30613902019-12-01 22:11:18 +01002502 // Do we underline the text?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 if (hl_mask_todo & HL_UNDERLINE)
2504 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506#else
Bram Moolenaar30613902019-12-01 22:11:18 +01002507 // Do we underline the text?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002508 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 draw_flags |= DRAW_UNDERL;
2510#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002511 // Do we undercurl the text?
Bram Moolenaar3918c952005-03-15 22:34:55 +00002512 if (hl_mask_todo & HL_UNDERCURL)
2513 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
Bram Moolenaar84f54632022-06-29 18:39:11 +01002515 // TODO: HL_UNDERDOUBLE, HL_UNDERDOTTED, HL_UNDERDASHED
2516
Bram Moolenaar30613902019-12-01 22:11:18 +01002517 // Do we strikethrough the text?
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002518 if (hl_mask_todo & HL_STRIKETHROUGH)
2519 draw_flags |= DRAW_STRIKE;
2520
Bram Moolenaar30613902019-12-01 22:11:18 +01002521 // Do we draw transparently?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (flags & GUI_MON_TRS_CURSOR)
2523 draw_flags |= DRAW_TRANSP;
2524
2525 /*
2526 * Draw the text.
2527 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002528#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01002529 // The value returned is the length in display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2531#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (enc_utf8)
2533 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002534 int start; // index of bytes to be drawn
2535 int cells; // cellwidth of bytes to be drawn
2536 int thislen; // length of bytes to be drawn
2537 int cn; // cellwidth of current char
2538 int i; // index of current char
2539 int c; // current char value
2540 int cl; // byte length of current char
2541 int comping; // current char is composing
2542 int scol = col; // screen column
2543 int curr_wide = FALSE; // use 'guifontwide'
Bram Moolenaar45933962013-01-23 17:43:57 +01002544 int prev_wide = FALSE;
2545 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002546# ifdef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01002547 int sep_comp = FALSE; // Don't separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002548# else
Bram Moolenaar30613902019-12-01 22:11:18 +01002549 int sep_comp = TRUE; // Separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
Bram Moolenaar30613902019-12-01 22:11:18 +01002552 // Break the string at a composing character, it has to be drawn on
2553 // top of the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 start = 0;
2555 cells = 0;
2556 for (i = 0; i < len; i += cl)
2557 {
2558 c = utf_ptr2char(s + i);
2559 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 comping = utf_iscomposing(c);
Bram Moolenaar30613902019-12-01 22:11:18 +01002561 if (!comping) // count cells from non-composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002563 if (!comping || sep_comp)
2564 {
2565 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002566# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002567 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002568# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002569 && wide_font != NOFONT)
2570 curr_wide = TRUE;
2571 else
2572 curr_wide = FALSE;
2573 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002574 cl = utf_ptr2len(s + i);
Bram Moolenaar30613902019-12-01 22:11:18 +01002575 if (cl == 0) // hit end of string
2576 len = i + cl; // len must be wrong "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
Bram Moolenaar45933962013-01-23 17:43:57 +01002578 wide_changed = curr_wide != prev_wide;
2579
Bram Moolenaar30613902019-12-01 22:11:18 +01002580 // Print the string so far if it's the last character or there is
2581 // a composing character.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002582 if (i + cl >= len || (comping && sep_comp && i > start)
2583 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002584# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002586# ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +01002587 // No fontset: At least draw char after wide char at
2588 // right position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002591 )
2592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 )
2594 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002595 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 thislen = i - start;
2597 else
2598 thislen = i - start + cl;
2599 if (thislen > 0)
2600 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002601 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002602 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2604 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002605 if (prev_wide)
2606 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 start += thislen;
2608 }
2609 scol += cells;
2610 cells = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01002611 // Adjust to not draw a character which width is changed
2612 // against with last one.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002613 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002615 scol -= cn;
2616 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
2618
Bram Moolenaar13505972019-01-24 15:04:48 +01002619# if defined(FEAT_GUI_X11)
Bram Moolenaar30613902019-12-01 22:11:18 +01002620 // No fontset: draw a space to fill the gap after a wide char
2621 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002623# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002625# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002626 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2628 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002631 // Draw a composing char on top of the previous char.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002632 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002634# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaar30613902019-12-01 22:11:18 +01002635 // Carbon ATSUI autodraws composing char over previous char
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002636 gui_mch_draw_string(gui.row, scol, s + i, cl,
2637 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002638# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002639 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2640 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002641# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 start = i + cl;
2643 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002644 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002646 // The stuff below assumes "len" is the length in screen columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 len = scol - col;
2648 }
2649 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 {
2651 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (enc_dbcs == DBCS_JPNU)
2653 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002654 // Get the length in display cells, this can be different from the
2655 // number of bytes for "euc-jp".
Bram Moolenaar72597a52010-07-18 15:31:08 +02002656 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002659#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2662 gui.col = col + len;
2663
Bram Moolenaar30613902019-12-01 22:11:18 +01002664 // May need to invert it when it's part of the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (flags & GUI_MON_NOCLEAR)
2666 clip_may_redraw_selection(gui.row, col, len);
2667
2668 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2669 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002670 // Invalidate the old physical cursor position if we wrote over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (gui.cursor_row == gui.row
2672 && gui.cursor_col >= col
2673 && gui.cursor_col < col + len)
2674 gui.cursor_is_valid = FALSE;
2675 }
2676
2677#ifdef FEAT_SIGN_ICONS
2678 if (draw_sign)
Bram Moolenaar30613902019-12-01 22:11:18 +01002679 // Draw the sign on top of the spaces.
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002680 gui_mch_drawsign(gui.row, signcol, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002681# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002682 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 if (multi_sign)
2684 netbeans_draw_multisign_indicator(gui.row);
2685# endif
2686#endif
2687
2688 return OK;
2689}
2690
2691/*
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002692 * Undraw the cursor. This actually redraws the character at the cursor
2693 * position, plus some more characters when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 */
2695 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002696gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002698 if (!gui.cursor_is_valid)
2699 return;
2700
2701 // Always redraw the character just before if there is one, because
2702 // with some fonts and characters there can be a one pixel overlap.
2703 int startcol = gui.cursor_col > 0 ? gui.cursor_col - 1 : gui.cursor_col;
2704 int endcol = gui.cursor_col;
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002705
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002706#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002707 gui_adjust_undraw_cursor_for_ligatures(&startcol, &endcol);
Dusan Popovicce59b9f2021-11-22 17:18:44 +00002708#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002709 gui_redraw_block(gui.cursor_row, startcol,
2710 gui.cursor_row, endcol, GUI_MON_NOCLEAR);
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002711
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002712 // Cursor_is_valid is reset when the cursor is undrawn, also reset it
2713 // here in case it wasn't needed to undraw it.
2714 gui.cursor_is_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715}
2716
2717 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002718gui_redraw(
2719 int x,
2720 int y,
2721 int w,
2722 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
2724 int row1, col1, row2, col2;
2725
2726 row1 = Y_2_ROW(y);
2727 col1 = X_2_COL(x);
2728 row2 = Y_2_ROW(y + h - 1);
2729 col2 = X_2_COL(x + w - 1);
2730
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002731 gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
2733 /*
2734 * We may need to redraw the cursor, but don't take it upon us to change
2735 * its location after a scroll.
2736 * (maybe be more strict even and test col too?)
2737 * These things may be outside the update/clipping region and reality may
2738 * not reflect Vims internal ideas if these operations are clipped away.
2739 */
2740 if (gui.row == gui.cursor_row)
2741 gui_update_cursor(TRUE, TRUE);
2742}
2743
2744/*
2745 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2746 * from col1 to col2 (inclusive).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 */
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002748 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002749gui_redraw_block(
2750 int row1,
2751 int col1,
2752 int row2,
2753 int col2,
Bram Moolenaar30613902019-12-01 22:11:18 +01002754 int flags) // flags for gui_outstr_nowrap()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 int old_row, old_col;
2757 long_u old_hl_mask;
2758 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002759 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 int idx, len;
2761 int back, nback;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
Bram Moolenaar30613902019-12-01 22:11:18 +01002764 // Don't try to update when ScreenLines is not valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 if (!screen_cleared || ScreenLines == NULL)
Bram Moolenaar7c003aa2020-03-28 20:44:41 +01002766 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
Bram Moolenaar30613902019-12-01 22:11:18 +01002768 // Don't try to draw outside the shell!
2769 // Check everything, strange values may be caused by a big border width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 col1 = check_col(col1);
2771 col2 = check_col(col2);
2772 row1 = check_row(row1);
2773 row2 = check_row(row2);
2774
Bram Moolenaar30613902019-12-01 22:11:18 +01002775 // Remember where our cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 old_row = gui.row;
2777 old_col = gui.col;
2778 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 orig_col1 = col1;
2780 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
2782 for (gui.row = row1; gui.row <= row2; gui.row++)
2783 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002784 // When only half of a double-wide character is in the block, include
2785 // the other half.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 col1 = orig_col1;
2787 col2 = orig_col2;
2788 off = LineOffset[gui.row];
2789 if (enc_dbcs != 0)
2790 {
2791 if (col1 > 0)
2792 col1 -= dbcs_screen_head_off(ScreenLines + off,
2793 ScreenLines + off + col1);
2794 col2 += dbcs_screen_tail_off(ScreenLines + off,
2795 ScreenLines + off + col2);
2796 }
2797 else if (enc_utf8)
2798 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002799 if (ScreenLines[off + col1] == 0)
2800 {
2801 if (col1 > 0)
2802 --col1;
2803 else
2804 // FIXME: how can the first character ever be zero?
Bram Moolenaar097c5372023-05-24 21:02:24 +01002805 siemsg("NUL in ScreenLines in row %ld", (long)gui.row);
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002806 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002807#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2809 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 gui.col = col1;
2813 off = LineOffset[gui.row] + gui.col;
2814 len = col2 - col1 + 1;
2815
Bram Moolenaar30613902019-12-01 22:11:18 +01002816 // Find how many chars back this highlighting starts, or where a space
2817 // is. Needed for when the bold trick is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 for (back = 0; back < col1; ++back)
2819 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2820 || ScreenLines[off - 1 - back] == ' ')
2821 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
Bram Moolenaar30613902019-12-01 22:11:18 +01002823 // Break it up in strings of characters with the same attributes.
2824 // Print UTF-8 characters individually.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 while (len > 0)
2826 {
2827 first_attr = ScreenAttrs[off];
2828 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002829#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 if (enc_utf8 && ScreenLinesUC[off] != 0)
2831 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002832 // output multi-byte character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 nback = gui_screenchar(off, flags,
2834 (guicolor_T)0, (guicolor_T)0, back);
2835 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2836 idx = 2;
2837 else
2838 idx = 1;
2839 }
2840 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2841 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002842 // output double-byte, single-width character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 nback = gui_screenchar(off, flags,
2844 (guicolor_T)0, (guicolor_T)0, back);
2845 idx = 1;
2846 }
2847 else
2848#endif
2849 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002850#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 for (idx = 0; idx < len; ++idx)
2852 {
2853 if (enc_utf8 && ScreenLines[off + idx] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002854 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 if (ScreenAttrs[off + idx] != first_attr)
2856 break;
2857 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002858 // gui_screenstr() takes care of multibyte chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 nback = gui_screenstr(off, idx, flags,
2860 (guicolor_T)0, (guicolor_T)0, back);
2861#else
2862 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2863 idx++)
2864 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002865 // Stop at a multi-byte Unicode character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2867 break;
2868 if (enc_dbcs == DBCS_JPNU)
2869 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002870 // Stop at a double-byte single-width char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 if (ScreenLines[off + idx] == 0x8e)
2872 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002873 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 + off + idx) == 2)
Bram Moolenaar30613902019-12-01 22:11:18 +01002875 ++idx; // skip second byte of double-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 }
2878 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2879 (guicolor_T)0, (guicolor_T)0, back);
2880#endif
2881 }
2882 if (nback == FAIL)
2883 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002884 // Must back up to start drawing where a bold or italic word
2885 // starts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 off -= back;
2887 len += back;
2888 gui.col -= back;
2889 }
2890 else
2891 {
2892 off += idx;
2893 len -= idx;
2894 }
2895 back = 0;
2896 }
2897 }
2898
Bram Moolenaar30613902019-12-01 22:11:18 +01002899 // Put the cursor back where it was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 gui.row = old_row;
2901 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002902 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903}
2904
2905 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002906gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907{
2908 if (count <= 0)
2909 return;
2910
2911 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002912 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 gui_clear_block(row, gui.scroll_region_left,
2914 gui.scroll_region_bot, gui.scroll_region_right);
2915 else
2916 {
2917 gui_mch_delete_lines(row, count);
2918
Bram Moolenaar30613902019-12-01 22:11:18 +01002919 // If the cursor was in the deleted lines it's now gone. If the
2920 // cursor was in the scrolled lines adjust its position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 if (gui.cursor_row >= row
2922 && gui.cursor_col >= gui.scroll_region_left
2923 && gui.cursor_col <= gui.scroll_region_right)
2924 {
2925 if (gui.cursor_row < row + count)
2926 gui.cursor_is_valid = FALSE;
2927 else if (gui.cursor_row <= gui.scroll_region_bot)
2928 gui.cursor_row -= count;
2929 }
2930 }
2931}
2932
2933 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002934gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935{
2936 if (count <= 0)
2937 return;
2938
2939 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002940 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 gui_clear_block(row, gui.scroll_region_left,
2942 gui.scroll_region_bot, gui.scroll_region_right);
2943 else
2944 {
2945 gui_mch_insert_lines(row, count);
2946
2947 if (gui.cursor_row >= gui.row
2948 && gui.cursor_col >= gui.scroll_region_left
2949 && gui.cursor_col <= gui.scroll_region_right)
2950 {
2951 if (gui.cursor_row <= gui.scroll_region_bot - count)
2952 gui.cursor_row += count;
2953 else if (gui.cursor_row <= gui.scroll_region_bot)
2954 gui.cursor_is_valid = FALSE;
2955 }
2956 }
2957}
2958
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002959#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002960/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002961 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2962 */
2963 static int
2964gui_wait_for_chars_3(
2965 long wtime,
2966 int *interrupted UNUSED,
2967 int ignore_input UNUSED)
2968{
2969 return gui_mch_wait_for_chars(wtime);
2970}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002971#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002972
2973/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002974 * Returns OK if a character was found to be available within the given time,
2975 * or FAIL otherwise.
2976 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002977 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002978gui_wait_for_chars_or_timer(
2979 long wtime,
2980 int *interrupted UNUSED,
2981 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002982{
2983#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002984 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2985 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002986#else
2987 return gui_mch_wait_for_chars(wtime);
2988#endif
2989}
2990
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991/*
2992 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002993 * "wtime" == -1 Wait forever.
2994 * "wtime" == 0 Don't wait.
2995 * "wtime" > 0 Wait wtime milliseconds for a character.
2996 *
2997 * Returns the number of characters read or zero when timed out or interrupted.
2998 * "buf" may be NULL, in which case a non-zero number is returned if characters
2999 * are available.
3000 */
3001 static int
3002gui_wait_for_chars_buf(
3003 char_u *buf,
3004 int maxlen,
3005 long wtime, // don't use "time", MIPS cannot handle it
3006 int tb_change_cnt)
3007{
3008 int retval;
3009
3010#ifdef FEAT_MENU
3011 // If we're going to wait a bit, update the menus and mouse shape for the
3012 // current State.
3013 if (wtime != 0)
3014 gui_update_menus(0);
3015#endif
3016
3017 gui_mch_update();
3018 if (input_available()) // Got char, return immediately
3019 {
3020 if (buf != NULL && !typebuf_changed(tb_change_cnt))
3021 return read_from_input_buf(buf, (long)maxlen);
3022 return 0;
3023 }
3024 if (wtime == 0) // Don't wait for char
3025 return FAIL;
3026
3027 // Before waiting, flush any output to the screen.
3028 gui_mch_flush();
3029
3030 // Blink while waiting for a character.
3031 gui_mch_start_blink();
3032
3033 // Common function to loop until "wtime" is met, while handling timers and
3034 // other callbacks.
3035 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
3036 gui_wait_for_chars_or_timer, NULL);
3037
3038 gui_mch_stop_blink(TRUE);
3039
3040 return retval;
3041}
3042
3043/*
3044 * Wait for a character from the keyboard without actually reading it.
3045 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 * wtime == -1 Wait forever.
3047 * wtime == 0 Don't wait.
3048 * wtime > 0 Wait wtime milliseconds for a character.
3049 * Returns OK if a character was found to be available within the given time,
3050 * or FAIL otherwise.
3051 */
3052 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003053gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003055 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056}
3057
3058/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003059 * Equivalent of mch_inchar() for the GUI.
3060 */
3061 int
3062gui_inchar(
3063 char_u *buf,
3064 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003065 long wtime, // milliseconds
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003066 int tb_change_cnt)
3067{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01003068 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01003069}
3070
3071/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003072 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 */
3074 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003075fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
3077 p[0] = (char_u)(col / 128 + ' ' + 1);
3078 p[1] = (char_u)(col % 128 + ' ' + 1);
3079 p[2] = (char_u)(row / 128 + ' ' + 1);
3080 p[3] = (char_u)(row % 128 + ' ' + 1);
3081}
3082
3083/*
3084 * Generic mouse support function. Add a mouse event to the input buffer with
3085 * the given properties.
3086 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3087 * MOUSE_X1, MOUSE_X2
3088 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003089 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3090 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 * x, y --- Coordinates of mouse in pixels.
3092 * repeated_click --- TRUE if this click comes only a short time after a
3093 * previous click.
3094 * modifiers --- Bit field which may be any of the following modifiers
3095 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3096 * This function will ignore drag events where the mouse has not moved to a new
3097 * character.
3098 */
3099 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003100gui_send_mouse_event(
3101 int button,
3102 int x,
3103 int y,
3104 int repeated_click,
3105 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 static int prev_row = 0, prev_col = 0;
3108 static int prev_button = -1;
3109 static int num_clicks = 1;
3110 char_u string[10];
3111 enum key_extra button_char;
3112 int row, col;
3113#ifdef FEAT_CLIPBOARD
3114 int checkfor;
3115 int did_clip = FALSE;
3116#endif
3117
3118 /*
3119 * Scrolling may happen at any time, also while a selection is present.
3120 */
3121 switch (button)
3122 {
Bram Moolenaar445f11d2021-06-08 20:13:31 +02003123 case MOUSE_MOVE:
3124 button_char = KE_MOUSEMOVE_XY;
3125 goto button_set;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 case MOUSE_X1:
3127 button_char = KE_X1MOUSE;
3128 goto button_set;
3129 case MOUSE_X2:
3130 button_char = KE_X2MOUSE;
3131 goto button_set;
3132 case MOUSE_4:
3133 button_char = KE_MOUSEDOWN;
3134 goto button_set;
3135 case MOUSE_5:
3136 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003137 goto button_set;
3138 case MOUSE_6:
3139 button_char = KE_MOUSELEFT;
3140 goto button_set;
3141 case MOUSE_7:
3142 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143button_set:
3144 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003145 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (hold_gui_events)
3147 return;
3148
Ernie Raelc4cb5442022-04-03 15:47:28 +01003149 row = gui_xy2colrow(x, y, &col);
3150 // Don't report a mouse move unless moved to a
3151 // different character position.
3152 if (button == MOUSE_MOVE)
3153 {
3154 if (row == prev_row && col == prev_col)
3155 return;
3156 else
3157 {
3158 prev_row = row >= 0 ? row : 0;
3159 prev_col = col;
3160 }
3161 }
3162
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 string[3] = CSI;
3164 string[4] = KS_EXTRA;
3165 string[5] = (int)button_char;
3166
Bram Moolenaar30613902019-12-01 22:11:18 +01003167 // Pass the pointer coordinates of the scroll event so that we
3168 // know which window to scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 string[6] = (char_u)(col / 128 + ' ' + 1);
3170 string[7] = (char_u)(col % 128 + ' ' + 1);
3171 string[8] = (char_u)(row / 128 + ' ' + 1);
3172 string[9] = (char_u)(row % 128 + ' ' + 1);
3173
3174 if (modifiers == 0)
3175 add_to_input_buf(string + 3, 7);
3176 else
3177 {
3178 string[0] = CSI;
3179 string[1] = KS_MODIFIER;
3180 string[2] = 0;
3181 if (modifiers & MOUSE_SHIFT)
3182 string[2] |= MOD_MASK_SHIFT;
3183 if (modifiers & MOUSE_CTRL)
3184 string[2] |= MOD_MASK_CTRL;
3185 if (modifiers & MOUSE_ALT)
3186 string[2] |= MOD_MASK_ALT;
3187 add_to_input_buf(string, 10);
3188 }
3189 return;
3190 }
3191 }
3192
3193#ifdef FEAT_CLIPBOARD
Bram Moolenaar30613902019-12-01 22:11:18 +01003194 // If a clipboard selection is in progress, handle it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 if (clip_star.state == SELECT_IN_PROGRESS)
3196 {
3197 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
Bram Moolenaar0ce37332019-12-18 21:33:22 +01003198
3199 // A release event may still need to be sent if the position is equal.
3200 row = gui_xy2colrow(x, y, &col);
3201 if (button != MOUSE_RELEASE || row != prev_row || col != prev_col)
3202 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
3204
Bram Moolenaar30613902019-12-01 22:11:18 +01003205 // Determine which mouse settings to look for based on the current mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 switch (get_real_state())
3207 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003208 case MODE_NORMAL_BUSY:
3209 case MODE_OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003210# ifdef FEAT_TERMINAL
Bram Moolenaar24959102022-05-07 20:01:16 +01003211 case MODE_TERMINAL:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003212# endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003213 case MODE_NORMAL: checkfor = MOUSE_NORMAL; break;
3214 case MODE_VISUAL: checkfor = MOUSE_VISUAL; break;
3215 case MODE_SELECT: checkfor = MOUSE_VISUAL; break;
3216 case MODE_REPLACE:
3217 case MODE_REPLACE | MODE_LANGMAP:
3218 case MODE_VREPLACE:
3219 case MODE_VREPLACE | MODE_LANGMAP:
3220 case MODE_INSERT:
3221 case MODE_INSERT | MODE_LANGMAP:
3222 checkfor = MOUSE_INSERT; break;
3223 case MODE_ASKMORE:
3224 case MODE_HITRETURN: // At the more- and hit-enter prompt pass the
Bram Moolenaar30613902019-12-01 22:11:18 +01003225 // mouse event for a click on or below the
3226 // message line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 if (Y_2_ROW(y) >= msg_row)
3228 checkfor = MOUSE_NORMAL;
3229 else
3230 checkfor = MOUSE_RETURN;
3231 break;
3232
3233 /*
3234 * On the command line, use the clipboard selection on all lines
3235 * but the command line. But not when pasting.
3236 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003237 case MODE_CMDLINE:
3238 case MODE_CMDLINE | MODE_LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3240 checkfor = MOUSE_NONE;
3241 else
3242 checkfor = MOUSE_COMMAND;
3243 break;
3244
3245 default:
3246 checkfor = MOUSE_NONE;
3247 break;
3248 };
3249
3250 /*
3251 * Allow clipboard selection of text on the command line in "normal"
3252 * modes. Don't do this when dragging the status line, or extending a
3253 * Visual selection.
3254 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003255 if ((State == MODE_NORMAL || State == MODE_NORMAL_BUSY
3256 || (State & MODE_INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003257 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 && button != MOUSE_DRAG
3259# ifdef FEAT_MOUSESHAPE
3260 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262# endif
3263 )
3264 checkfor = MOUSE_NONE;
3265
3266 /*
3267 * Use modeless selection when holding CTRL and SHIFT pressed.
3268 */
3269 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3270 checkfor = MOUSE_NONEF;
3271
3272 /*
3273 * In Ex mode, always use modeless selection.
3274 */
3275 if (exmode_active)
3276 checkfor = MOUSE_NONE;
3277
3278 /*
3279 * If the mouse settings say to not use the mouse, use the modeless
3280 * selection. But if Visual is active, assume that only the Visual area
3281 * will be selected.
3282 * Exception: On the command line, both the selection is used and a mouse
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003283 * key is sent.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 */
3285 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3286 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003287 // Don't do modeless selection in Visual mode.
Bram Moolenaar24959102022-05-07 20:01:16 +01003288 if (checkfor != MOUSE_NONEF && VIsual_active && (State & MODE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
3291 /*
3292 * When 'mousemodel' is "popup", shift-left is translated to right.
3293 * But not when also using Ctrl.
3294 */
3295 if (mouse_model_popup() && button == MOUSE_LEFT
3296 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3297 {
3298 button = MOUSE_RIGHT;
3299 modifiers &= ~ MOUSE_SHIFT;
3300 }
3301
Bram Moolenaar30613902019-12-01 22:11:18 +01003302 // If the selection is done, allow the right button to extend it.
3303 // If the selection is cleared, allow the right button to start it
3304 // from the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 if (button == MOUSE_RIGHT)
3306 {
3307 if (clip_star.state == SELECT_CLEARED)
3308 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003309 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
3311 col = msg_col;
3312 row = msg_row;
3313 }
3314 else
3315 {
3316 col = curwin->w_wcol;
3317 row = curwin->w_wrow + W_WINROW(curwin);
3318 }
3319 clip_start_selection(col, row, FALSE);
3320 }
3321 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3322 repeated_click);
3323 did_clip = TRUE;
3324 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003325 // Allow the left button to start the selection
Bram Moolenaare60acc12011-05-10 16:41:25 +02003326 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
3328 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3329 did_clip = TRUE;
3330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar30613902019-12-01 22:11:18 +01003332 // Always allow pasting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (button != MOUSE_MIDDLE)
3334 {
3335 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3336 return;
3337 if (checkfor != MOUSE_COMMAND)
3338 button = MOUSE_LEFT;
3339 }
3340 repeated_click = FALSE;
3341 }
3342
3343 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003344 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345#endif
3346
Bram Moolenaar30613902019-12-01 22:11:18 +01003347 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 if (hold_gui_events)
3349 return;
3350
3351 row = gui_xy2colrow(x, y, &col);
3352
3353 /*
3354 * If we are dragging and the mouse hasn't moved far enough to be on a
3355 * different character, then don't send an event to vim.
3356 */
3357 if (button == MOUSE_DRAG)
3358 {
3359 if (row == prev_row && col == prev_col)
3360 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01003361 // Dragging above the window, set "row" to -1 to cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (y < 0)
3363 row = -1;
3364 }
3365
3366 /*
3367 * If topline has changed (window scrolled) since the last click, reset
3368 * repeated_click, because we don't want starting Visual mode when
3369 * clicking on a different character in the text.
3370 */
3371 if (curwin->w_topline != gui_prev_topline
3372#ifdef FEAT_DIFF
3373 || curwin->w_topfill != gui_prev_topfill
3374#endif
3375 )
3376 repeated_click = FALSE;
3377
Bram Moolenaar30613902019-12-01 22:11:18 +01003378 string[0] = CSI; // this sequence is recognized by check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 string[1] = KS_MOUSE;
3380 string[2] = KE_FILLER;
3381 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3382 {
3383 if (repeated_click)
3384 {
3385 /*
3386 * Handle multiple clicks. They only count if the mouse is still
3387 * pointing at the same character.
3388 */
3389 if (button != prev_button || row != prev_row || col != prev_col)
3390 num_clicks = 1;
3391 else if (++num_clicks > 4)
3392 num_clicks = 1;
3393 }
3394 else
3395 num_clicks = 1;
3396 prev_button = button;
3397 gui_prev_topline = curwin->w_topline;
3398#ifdef FEAT_DIFF
3399 gui_prev_topfill = curwin->w_topfill;
3400#endif
3401
3402 string[3] = (char_u)(button | 0x20);
3403 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3404 }
3405 else
3406 string[3] = (char_u)button;
3407
3408 string[3] |= modifiers;
3409 fill_mouse_coord(string + 4, col, row);
3410 add_to_input_buf(string, 8);
3411
3412 if (row < 0)
3413 prev_row = 0;
3414 else
3415 prev_row = row;
3416 prev_col = col;
3417
3418 /*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01003419 * We need to make sure this is cleared since GTK doesn't tell us when
3420 * the user is done dragging.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 */
Bram Moolenaar0b962e52022-04-03 18:02:37 +01003422#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 gui.dragged_sb = SBAR_NONE;
3424#endif
3425}
3426
3427/*
3428 * Convert x and y coordinate to column and row in text window.
3429 * Corrects for multi-byte character.
3430 * returns column in "*colp" and row as return value;
3431 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003432 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003433gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
3435 int col = check_col(X_2_COL(x));
3436 int row = check_row(Y_2_ROW(y));
3437
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 return row;
3440}
3441
3442#if defined(FEAT_MENU) || defined(PROTO)
3443/*
3444 * Callback function for when a menu entry has been selected.
3445 */
3446 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003447gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003449 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
Bram Moolenaar30613902019-12-01 22:11:18 +01003451 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 if (hold_gui_events)
3453 return;
3454
3455 bytes[0] = CSI;
3456 bytes[1] = KS_MENU;
3457 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003458 add_to_input_buf(bytes, 3);
3459 add_long_to_buf((long_u)menu, bytes);
3460 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461}
3462#endif
3463
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003464static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466/*
3467 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003468 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 * in p_go.
3470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003472gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003474#ifdef FEAT_GUI_DARKTHEME
3475 static int prev_dark_theme = -1;
3476 int using_dark_theme = FALSE;
3477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#ifdef FEAT_MENU
3479 static int prev_menu_is_active = -1;
3480#endif
3481#ifdef FEAT_TOOLBAR
3482 static int prev_toolbar = -1;
3483 int using_toolbar = FALSE;
3484#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003485#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003486 int using_tabline;
3487#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003488#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 static int prev_tearoff = -1;
3490 int using_tearoff = FALSE;
3491#endif
3492
3493 char_u *p;
3494 int i;
3495#ifdef FEAT_MENU
3496 int grey_old, grey_new;
3497 char_u *temp;
3498#endif
3499 win_T *wp;
3500 int need_set_size;
3501 int fix_size;
3502
3503#ifdef FEAT_MENU
3504 if (oldval != NULL && gui.in_use)
3505 {
3506 /*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003507 * Check if the menus go from grey to non-grey or vice versa.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 */
3509 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3510 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3511 if (grey_old != grey_new)
3512 {
3513 temp = p_go;
3514 p_go = oldval;
3515 gui_update_menus(MENU_ALL_MODES);
3516 p_go = temp;
3517 }
3518 }
3519 gui.menu_is_active = FALSE;
3520#endif
3521
3522 for (i = 0; i < 3; i++)
3523 gui.which_scrollbars[i] = FALSE;
3524 for (p = p_go; *p; p++)
3525 switch (*p)
3526 {
3527 case GO_LEFT:
3528 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3529 break;
3530 case GO_RIGHT:
3531 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3532 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 case GO_VLEFT:
3534 if (win_hasvertsplit())
3535 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3536 break;
3537 case GO_VRIGHT:
3538 if (win_hasvertsplit())
3539 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3540 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 case GO_BOT:
3542 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3543 break;
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003544#ifdef FEAT_GUI_DARKTHEME
3545 case GO_DARKTHEME:
3546 using_dark_theme = TRUE;
3547 break;
3548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549#ifdef FEAT_MENU
3550 case GO_MENUS:
3551 gui.menu_is_active = TRUE;
3552 break;
3553#endif
3554 case GO_GREY:
Bram Moolenaar30613902019-12-01 22:11:18 +01003555 // make menu's have grey items, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 break;
3557#ifdef FEAT_TOOLBAR
3558 case GO_TOOLBAR:
3559 using_toolbar = TRUE;
3560 break;
3561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003563#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 using_tearoff = TRUE;
3565#endif
3566 break;
3567 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01003568 // Ignore options that are not supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 break;
3570 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003571
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003572 if (!gui.in_use)
3573 return;
3574
3575 need_set_size = 0;
3576 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003577
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003578#ifdef FEAT_GUI_DARKTHEME
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003579 if (using_dark_theme != prev_dark_theme)
3580 {
3581 gui_mch_set_dark_theme(using_dark_theme);
3582 prev_dark_theme = using_dark_theme;
3583 }
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003584#endif
3585
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003586#ifdef FEAT_GUI_TABLINE
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003587 // Update the GUI tab line, it may appear or disappear. This may
3588 // cause the non-GUI tab line to disappear or appear.
3589 using_tabline = gui_has_tabline();
3590 if (!gui_mch_showing_tabline() != !using_tabline)
3591 {
3592 // We don't want a resize event change "Rows" here, save and
3593 // restore it. Resizing is handled below.
3594 i = Rows;
3595 gui_update_tabline();
3596 Rows = i;
3597 need_set_size |= RESIZE_VERT;
3598 if (using_tabline)
3599 fix_size = TRUE;
3600 if (!gui_use_tabline())
3601 redraw_tabline = TRUE; // may draw non-GUI tab line
3602 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003603#endif
3604
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003605 for (i = 0; i < 3; i++)
3606 {
3607 // The scrollbar needs to be updated when it is shown/unshown and
3608 // when switching tab pages. But the size only changes when it's
3609 // shown/unshown. Thus we need two places to remember whether a
3610 // scrollbar is there or not.
3611 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
3612 || gui.which_scrollbars[i]
3613 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003615 if (i == SBAR_BOTTOM)
3616 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3617 gui.which_scrollbars[i]);
3618 else
3619 {
3620 FOR_ALL_WINDOWS(wp)
3621 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
3622 }
3623 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
3625 if (i == SBAR_BOTTOM)
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003626 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 else
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003628 need_set_size |= RESIZE_HOR;
3629 if (gui.which_scrollbars[i])
3630 fix_size = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003633 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
3634 prev_which_scrollbars[i] = gui.which_scrollbars[i];
3635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637#ifdef FEAT_MENU
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003638 if (gui.menu_is_active != prev_menu_is_active)
3639 {
3640 // We don't want a resize event change "Rows" here, save and
3641 // restore it. Resizing is handled below.
3642 i = Rows;
3643 gui_mch_enable_menu(gui.menu_is_active);
3644 Rows = i;
3645 prev_menu_is_active = gui.menu_is_active;
3646 need_set_size |= RESIZE_VERT;
3647 if (gui.menu_is_active)
3648 fix_size = TRUE;
3649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650#endif
3651
3652#ifdef FEAT_TOOLBAR
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003653 if (using_toolbar != prev_toolbar)
3654 {
3655 gui_mch_show_toolbar(using_toolbar);
3656 prev_toolbar = using_toolbar;
3657 need_set_size |= RESIZE_VERT;
3658 if (using_toolbar)
3659 fix_size = TRUE;
3660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003662#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003663 if (using_tearoff != prev_tearoff)
3664 {
3665 gui_mch_toggle_tearoffs(using_tearoff);
3666 prev_tearoff = using_tearoff;
3667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003669 if (need_set_size != 0)
3670 {
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003671#ifdef FEAT_GUI_GTK
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003672 long prev_Columns = Columns;
3673 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003674#endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003675 // Adjust the size of the window to make the text area keep the
3676 // same size and to avoid that part of our window is off-screen
3677 // and a scrollbar can't be used, for example.
3678 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003679
3680#ifdef FEAT_GUI_GTK
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003681 // GTK has the annoying habit of sending us resize events when
3682 // changing the window size ourselves. This mostly happens when
3683 // waiting for a character to arrive, quite unpredictably, and may
3684 // change Columns and Rows when we don't want it. Wait for a
3685 // character here to avoid this effect.
3686 // If you remove this, please test this command for resizing
3687 // effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3688 // Don't do this while starting up though.
3689 // Don't change Rows when adding menu/toolbar/tabline.
3690 // Don't change Columns when adding vertical toolbar.
3691 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
3692 (void)char_avail();
3693 if ((need_set_size & RESIZE_VERT) == 0)
3694 Rows = prev_Rows;
3695 if ((need_set_size & RESIZE_HOR) == 0)
3696 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003699 // When the console tabline appears or disappears the window positions
3700 // change.
3701 if (firstwin->w_winrow != tabline_height())
3702 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703}
3704
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003705#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3706/*
3707 * Return TRUE if the GUI is taking care of the tabline.
3708 * It may still be hidden if 'showtabline' is zero.
3709 */
3710 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003711gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003712{
3713 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3714}
3715
3716/*
3717 * Return TRUE if the GUI is showing the tabline.
3718 * This uses 'showtabline'.
3719 */
3720 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003721gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003722{
3723 if (!gui_use_tabline()
3724 || p_stal == 0
3725 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3726 return FALSE;
3727 return TRUE;
3728}
3729
3730/*
3731 * Update the tabline.
3732 * This may display/undisplay the tabline and update the labels.
3733 */
3734 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003735gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003736{
3737 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003738 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003739
3740 if (!gui.starting && starting == 0)
3741 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003742 // Updating the tabline uses direct GUI commands, flush
3743 // outstanding instructions first. (esp. clear screen)
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003744 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003745
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003746 if (!showit != !shown)
3747 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003748 if (showit != 0)
3749 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003750
Bram Moolenaar30613902019-12-01 22:11:18 +01003751 // When the tabs change from hidden to shown or from shown to
3752 // hidden the size of the text area should remain the same.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003753 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003754 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003755 }
3756}
3757
3758/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003759 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003760 */
3761 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003762get_tabline_label(
3763 tabpage_T *tp,
Bram Moolenaar30613902019-12-01 22:11:18 +01003764 int tooltip) // TRUE: get tooltip
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003765{
Bram Moolenaard68071d2006-05-02 22:08:30 +00003766 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003767
Bram Moolenaar30613902019-12-01 22:11:18 +01003768 // Use 'guitablabel' or 'guitabtooltip' if it's set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003769 opt = (tooltip ? &p_gtt : &p_gtl);
3770 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003771 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003772 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003773 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003774 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3775 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003776
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003777 printer_page_num = tabpage_index(tp);
3778# ifdef FEAT_EVAL
3779 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003780# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003781 // It's almost as going to the tabpage, but without autocommands.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003782 curtab->tp_firstwin = firstwin;
3783 curtab->tp_lastwin = lastwin;
3784 curtab->tp_curwin = curwin;
3785 save_curtab = curtab;
3786 curtab = tp;
3787 topframe = curtab->tp_topframe;
3788 firstwin = curtab->tp_firstwin;
3789 lastwin = curtab->tp_lastwin;
3790 curwin = curtab->tp_curwin;
3791 curbuf = curwin->w_buffer;
3792
Bram Moolenaar30613902019-12-01 22:11:18 +01003793 // Can't use NameBuff directly, build_stl_str_hl() uses it.
Luuk van Baal7b224fd2022-11-07 12:16:51 +00003794 build_stl_str_hl(curwin, res, MAXPATHL, *opt, opt_name, 0,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003795 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003796 STRCPY(NameBuff, res);
3797
Bram Moolenaar30613902019-12-01 22:11:18 +01003798 // Back to the original curtab.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003799 curtab = save_curtab;
3800 topframe = curtab->tp_topframe;
3801 firstwin = curtab->tp_firstwin;
3802 lastwin = curtab->tp_lastwin;
3803 curwin = curtab->tp_curwin;
3804 curbuf = curwin->w_buffer;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003805 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003806
Bram Moolenaar30613902019-12-01 22:11:18 +01003807 // If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3808 // use a default label.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003809 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003810 {
John Marriottd2fcbb42025-05-07 19:59:42 +02003811 win_T *wp;
3812 int wincount = 0;
3813 int modified = FALSE;
3814
Bram Moolenaar30613902019-12-01 22:11:18 +01003815 // Get the buffer name into NameBuff[] and shorten it.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003816 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003817 if (!tooltip)
3818 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003819
John Marriottd2fcbb42025-05-07 19:59:42 +02003820 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3821 {
3822 ++wincount;
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003823 if (bufIsChanged(wp->w_buffer))
3824 modified = TRUE;
John Marriottd2fcbb42025-05-07 19:59:42 +02003825 }
3826
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003827 if (modified || wincount > 1)
3828 {
John Marriottd2fcbb42025-05-07 19:59:42 +02003829 char_u buf[40] = "+ "; // Tentatively assume modified only
3830 size_t buflen = 2;
3831 size_t NameBufflen = STRLEN(NameBuff);
3832
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003833 if (wincount > 1)
John Marriottd2fcbb42025-05-07 19:59:42 +02003834 buflen = vim_snprintf_safelen(
3835 (char *)buf,
3836 sizeof(buf),
3837 "%d%s ",
3838 wincount,
3839 modified ? "+" : "");
3840
Christ van Willegen31415092025-05-09 00:08:01 +02003841 // Make sure resulting NameBuff will not exceed its bounds.
John Marriottd2fcbb42025-05-07 19:59:42 +02003842 if (NameBufflen + buflen < MAXPATHL)
3843 {
3844 mch_memmove(NameBuff + buflen, NameBuff, NameBufflen + 1); // +1 for NUL
3845 mch_memmove(NameBuff, buf, buflen);
3846 }
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003847 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003848 }
3849}
3850
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003851/*
3852 * Send the event for clicking to select tab page "nr".
3853 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003854 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003855 */
3856 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003857send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003858{
3859 char_u string[3];
3860
3861 if (nr == tabpage_index(curtab))
3862 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003863
Bram Moolenaar30613902019-12-01 22:11:18 +01003864 // Don't put events in the input queue now.
Martin Tournoij7904fa42022-10-04 16:28:45 +01003865 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003866 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003867 // Set it back to the current tab page.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003868 gui_mch_set_curtab(tabpage_index(curtab));
3869 return FALSE;
3870 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003871
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003872 string[0] = CSI;
3873 string[1] = KS_TABLINE;
3874 string[2] = KE_FILLER;
3875 add_to_input_buf(string, 3);
3876 string[0] = nr;
3877 add_to_input_buf_csi(string, 1);
3878 return TRUE;
3879}
3880
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003881/*
3882 * Send a tabline menu event
3883 */
3884 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003885send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003886{
3887 char_u string[3];
3888
Bram Moolenaar29547192018-12-11 20:39:19 +01003889 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003890 if (hold_gui_events)
3891 return;
3892
Bram Moolenaar29547192018-12-11 20:39:19 +01003893 // Cannot close the last tabpage.
3894 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3895 return;
3896
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003897 string[0] = CSI;
3898 string[1] = KS_TABMENU;
3899 string[2] = KE_FILLER;
3900 add_to_input_buf(string, 3);
3901 string[0] = tabidx;
3902 string[1] = (char_u)(long)event;
3903 add_to_input_buf_csi(string, 2);
3904}
3905
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
3908/*
3909 * Scrollbar stuff:
3910 */
3911
Bram Moolenaare45828b2006-02-15 22:12:56 +00003912/*
3913 * Remove all scrollbars. Used before switching to another tab page.
3914 */
3915 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003916gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003917{
3918 int i;
3919 win_T *wp;
3920
3921 for (i = 0; i < 3; i++)
3922 {
3923 if (i == SBAR_BOTTOM)
3924 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3925 else
3926 {
3927 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003928 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003929 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003930 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003931 }
3932}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003933
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003935gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
3937 static int sbar_ident = 0;
3938
Bram Moolenaar30613902019-12-01 22:11:18 +01003939 sb->ident = sbar_ident++; // No check for too big, but would it happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 sb->wp = wp;
3941 sb->type = type;
3942 sb->value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 sb->size = 1;
3944 sb->max = 1;
3945 sb->top = 0;
3946 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 sb->status_height = 0;
3949 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3950}
3951
3952/*
3953 * Find the scrollbar with the given index.
3954 */
3955 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003956gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957{
3958 win_T *wp;
3959
3960 if (gui.bottom_sbar.ident == ident)
3961 return &gui.bottom_sbar;
3962 FOR_ALL_WINDOWS(wp)
3963 {
3964 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3965 return &wp->w_scrollbars[SBAR_LEFT];
3966 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3967 return &wp->w_scrollbars[SBAR_RIGHT];
3968 }
3969 return NULL;
3970}
3971
3972/*
3973 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3974 *
3975 * For Win32, Macintosh and GTK+ 2:
3976 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3977 * you stop dragging the scrollbar. We get here each time the scrollbar is
3978 * dragged another pixel, but as far as the rest of vim goes, it thinks
3979 * we're just hanging in the call to DispatchMessage() in
3980 * process_message(). The DispatchMessage() call that hangs was passed a
3981 * mouse button click event in the scrollbar window. -- webb.
3982 *
3983 * Solution: Do the scrolling right here. But only when allowed.
3984 * Ignore the scrollbars while executing an external command or when there
3985 * are still characters to be processed.
3986 */
3987 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003988gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 int sb_num;
3992#ifdef USE_ON_FLY_SCROLL
3993 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995# ifdef FEAT_DIFF
3996 int old_topfill = curwin->w_topfill;
3997# endif
3998#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003999 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 int byte_count;
4001#endif
4002
4003 if (sb == NULL)
4004 return;
4005
Bram Moolenaar30613902019-12-01 22:11:18 +01004006 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (hold_gui_events)
4008 return;
4009
Sean Dewar988f7432023-08-16 14:17:36 +01004010 if (cmdwin_type != 0 && sb->wp != cmdwin_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
4013 if (still_dragging)
4014 {
4015 if (sb->wp == NULL)
4016 gui.dragged_sb = SBAR_BOTTOM;
4017 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
4018 gui.dragged_sb = SBAR_LEFT;
4019 else
4020 gui.dragged_sb = SBAR_RIGHT;
4021 gui.dragged_wp = sb->wp;
4022 }
4023 else
4024 {
4025 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02004026#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004027 // Keep the "dragged_wp" value until after the scrolling, for when the
4028 // mouse button is released. GTK2 doesn't send the button-up event.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 gui.dragged_wp = NULL;
4030#endif
4031 }
4032
Bram Moolenaar30613902019-12-01 22:11:18 +01004033 // Vertical sbar info is kept in the first sbar (the left one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 if (sb->wp != NULL)
4035 sb = &sb->wp->w_scrollbars[0];
4036
4037 /*
4038 * Check validity of value
4039 */
4040 if (value < 0)
4041 value = 0;
4042#ifdef SCROLL_PAST_END
4043 else if (value > sb->max)
4044 value = sb->max;
4045#else
4046 if (value > sb->max - sb->size + 1)
4047 value = sb->max - sb->size + 1;
4048#endif
4049
4050 sb->value = value;
4051
4052#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar30613902019-12-01 22:11:18 +01004053 // When not allowed to do the scrolling right now, return.
4054 // This also checked input_available(), but that causes the first click in
4055 // a scrollbar to be ignored when Vim doesn't have focus.
Bram Moolenaar67840782008-01-03 15:15:07 +00004056 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 return;
4058#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004059 // Disallow scrolling the current window when the completion popup menu is
4060 // visible.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004061 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
4062 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064#ifdef FEAT_RIGHTLEFT
4065 if (sb->wp == NULL && curwin->w_p_rl)
4066 {
4067 value = sb->max + 1 - sb->size - value;
4068 if (value < 0)
4069 value = 0;
4070 }
4071#endif
4072
Bram Moolenaar30613902019-12-01 22:11:18 +01004073 if (sb->wp != NULL) // vertical scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
4075 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4077 sb_num++;
4078 if (wp == NULL)
4079 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081#ifdef USE_ON_FLY_SCROLL
4082 current_scrollbar = sb_num;
4083 scrollbar_value = value;
Bram Moolenaar24959102022-05-07 20:01:16 +01004084 if (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 {
4086 gui_do_scroll();
4087 setcursor();
4088 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004089 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
4091 ins_scroll();
4092 setcursor();
4093 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004094 else if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
4096 if (msg_scrolled == 0)
4097 {
4098 gui_do_scroll();
4099 redrawcmdline();
4100 }
4101 }
4102# ifdef FEAT_FOLDING
Bram Moolenaar30613902019-12-01 22:11:18 +01004103 // Value may have been changed for closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 sb->value = sb->wp->w_topline - 1;
4105# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004106
Bram Moolenaar30613902019-12-01 22:11:18 +01004107 // When dragging one scrollbar and there is another one at the other
4108 // side move the thumb of that one too.
Bram Moolenaar371d5402006-03-20 21:47:49 +00004109 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4110 gui_mch_set_scrollbar_thumb(
4111 &sb->wp->w_scrollbars[
4112 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4113 ? SBAR_LEFT : SBAR_RIGHT],
4114 sb->value, sb->size, sb->max);
4115
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116#else
4117 bytes[0] = CSI;
4118 bytes[1] = KS_VER_SCROLLBAR;
4119 bytes[2] = KE_FILLER;
4120 bytes[3] = (char_u)sb_num;
4121 byte_count = 4;
4122#endif
4123 }
4124 else
4125 {
4126#ifdef USE_ON_FLY_SCROLL
4127 scrollbar_value = value;
4128
Bram Moolenaar24959102022-05-07 20:01:16 +01004129 if (State & MODE_NORMAL)
Christopher Plewright44c22092022-11-15 17:43:36 +00004130 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar24959102022-05-07 20:01:16 +01004131 else if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 ins_horscroll();
Bram Moolenaar24959102022-05-07 20:01:16 +01004133 else if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004135 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
Christopher Plewright44c22092022-11-15 17:43:36 +00004137 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 redrawcmdline();
4139 }
4140 }
4141 if (old_leftcol != curwin->w_leftcol)
4142 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004143 updateWindow(curwin); // update window, status and cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 setcursor();
4145 }
4146#else
4147 bytes[0] = CSI;
4148 bytes[1] = KS_HOR_SCROLLBAR;
4149 bytes[2] = KE_FILLER;
4150 byte_count = 3;
4151#endif
4152 }
4153
4154#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 /*
4156 * synchronize other windows, as necessary according to 'scrollbind'
4157 */
4158 if (curwin->w_p_scb
4159 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4160 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004161# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004163# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 ))))
4165 {
4166 do_check_scrollbind(TRUE);
Bram Moolenaar30613902019-12-01 22:11:18 +01004167 // need to update the window right here
Bram Moolenaar29323592016-07-24 22:04:11 +02004168 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 if (wp->w_redr_type > 0)
4170 updateWindow(wp);
4171 setcursor();
4172 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004173 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004175 add_to_input_buf(bytes, byte_count);
4176 add_long_to_buf((long_u)value, bytes);
4177 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178#endif
4179}
4180
4181/*
4182 * Scrollbar stuff:
4183 */
4184
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004185/*
4186 * Called when something in the window layout has changed.
4187 */
4188 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004189gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004190{
4191 if (gui.in_use && starting == 0)
4192 {
4193 out_flush();
4194 gui_init_which_components(NULL);
4195 gui_update_scrollbars(TRUE);
4196 }
4197 need_mouse_correct = TRUE;
4198}
4199
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004201gui_update_scrollbars(
Bram Moolenaar30613902019-12-01 22:11:18 +01004202 int force) // Force all scrollbars to get updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
4204 win_T *wp;
4205 scrollbar_T *sb;
Bram Moolenaar30613902019-12-01 22:11:18 +01004206 long val, size, max; // need 32 bits here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 int which_sb;
4208 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
Bram Moolenaar30613902019-12-01 22:11:18 +01004211 // Update the horizontal scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 gui_update_horiz_scrollbar(force);
4213
Bram Moolenaar4f974752019-02-17 17:44:42 +01004214#ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01004215 // Return straight away if there is neither a left nor right scrollbar.
4216 // On MS-Windows this is required anyway for scrollwheel messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4218 return;
4219#endif
4220
4221 /*
4222 * Don't want to update a scrollbar while we're dragging it. But if we
4223 * have both a left and right scrollbar, and we drag one of them, we still
4224 * need to update the other one.
4225 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004226 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4227 && gui.which_scrollbars[SBAR_LEFT]
4228 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
4230 /*
4231 * If we have two scrollbars and one of them is being dragged, just
4232 * copy the scrollbar position from the dragged one to the other one.
4233 */
4234 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4235 if (gui.dragged_wp != NULL)
4236 gui_mch_set_scrollbar_thumb(
4237 &gui.dragged_wp->w_scrollbars[which_sb],
4238 gui.dragged_wp->w_scrollbars[0].value,
4239 gui.dragged_wp->w_scrollbars[0].size,
4240 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242
Bram Moolenaar30613902019-12-01 22:11:18 +01004243 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 ++hold_gui_events;
4245
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004246 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004248 if (wp->w_buffer == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 continue;
Bram Moolenaar30613902019-12-01 22:11:18 +01004250 // Skip a scrollbar that is being dragged.
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004251 if (!force && (gui.dragged_sb == SBAR_LEFT
4252 || gui.dragged_sb == SBAR_RIGHT)
4253 && gui.dragged_wp == wp)
4254 continue;
4255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#ifdef SCROLL_PAST_END
4257 max = wp->w_buffer->b_ml.ml_line_count - 1;
4258#else
4259 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4260#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004261 if (max < 0) // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 max = 0;
4263 val = wp->w_topline - 1;
4264 size = wp->w_height;
4265#ifdef SCROLL_PAST_END
Bram Moolenaar30613902019-12-01 22:11:18 +01004266 if (val > max) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 val = max;
4268#else
Bram Moolenaar30613902019-12-01 22:11:18 +01004269 if (size > max + 1) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 size = max + 1;
4271 if (val > max - size + 1)
4272 val = max - size + 1;
4273#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004274 if (val < 0) // minimal value is 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 val = 0;
4276
4277 /*
4278 * Scrollbar at index 0 (the left one) contains all the information.
4279 * It would be the same info for left and right so we just store it for
4280 * one of them.
4281 */
4282 sb = &wp->w_scrollbars[0];
4283
4284 /*
4285 * Note: no check for valid w_botline. If it's not valid the
4286 * scrollbars will be updated later anyway.
4287 */
4288 if (size < 1 || wp->w_botline - 2 > max)
4289 {
4290 /*
4291 * This can happen during changing files. Just don't update the
4292 * scrollbar for now.
4293 */
Bram Moolenaar30613902019-12-01 22:11:18 +01004294 sb->height = 0; // Force update next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 if (gui.which_scrollbars[SBAR_LEFT])
4296 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4297 if (gui.which_scrollbars[SBAR_RIGHT])
4298 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4299 continue;
4300 }
4301 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 || sb->top != wp->w_winrow
4303 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004305 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004307 // Height, width or position of scrollbar has changed. For
4308 // vertical split: curwin changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 sb->top = wp->w_winrow;
4311 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
Bram Moolenaar30613902019-12-01 22:11:18 +01004314 // Calculate height and position in pixels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 h = (sb->height + sb->status_height) * gui.char_height;
4316 y = sb->top * gui.char_height + gui.border_offset;
4317#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4318 if (gui.menu_is_active)
4319 y += gui.menu_height;
4320#endif
4321
Bram Moolenaar0b962e52022-04-03 18:02:37 +01004322#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004323 || defined(FEAT_GUI_HAIKU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 y += gui.toolbar_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326#endif
4327
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004328#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_HAIKU)
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004329 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004330 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004331#endif
4332
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004335 // Height of top scrollbar includes width of top border
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 h += gui.border_offset;
4337 y -= gui.border_offset;
4338 }
4339 if (gui.which_scrollbars[SBAR_LEFT])
4340 {
4341 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4342 gui.left_sbar_x, y,
4343 gui.scrollbar_width, h);
4344 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4345 }
4346 if (gui.which_scrollbars[SBAR_RIGHT])
4347 {
4348 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4349 gui.right_sbar_x, y,
4350 gui.scrollbar_width, h);
4351 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4352 }
4353 }
4354
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 if (force || sb->value != val || sb->size != size || sb->max != max)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004357 // Thumb of scrollbar has moved
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 sb->value = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 sb->size = size;
4360 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004361 if (gui.which_scrollbars[SBAR_LEFT]
4362 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4364 val, size, max);
4365 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004366 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4368 val, size, max);
4369 }
4370 }
Christian Brabandt2e0f3ec2021-11-28 18:41:05 +00004371
4372 // update the title, it may show the scroll position
4373 maketitle();
4374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 --hold_gui_events;
4377}
4378
4379/*
4380 * Enable or disable a scrollbar.
4381 * Check for scrollbars for vertically split windows which are not enabled
4382 * sometimes.
4383 */
4384 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004385gui_do_scrollbar(
4386 win_T *wp,
Bram Moolenaar30613902019-12-01 22:11:18 +01004387 int which, // SBAR_LEFT or SBAR_RIGHT
4388 int enable) // TRUE to enable scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 int midcol = curwin->w_wincol + curwin->w_width / 2;
4391 int has_midcol = (wp->w_wincol <= midcol
4392 && wp->w_wincol + wp->w_width >= midcol);
4393
Bram Moolenaar30613902019-12-01 22:11:18 +01004394 // Only enable scrollbars that contain the middle column of the current
4395 // window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4397 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004398 // Scrollbars only on one side. Don't enable scrollbars that don't
4399 // contain the middle column of the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 if (!has_midcol)
4401 enable = FALSE;
4402 }
4403 else
4404 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004405 // Scrollbars on both sides. Don't enable scrollbars that neither
4406 // contain the middle column of the current window nor are on the far
4407 // side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 if (midcol > Columns / 2)
4409 {
4410 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4411 enable = FALSE;
4412 }
4413 else
4414 {
4415 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4416 : !has_midcol)
4417 enable = FALSE;
4418 }
4419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4421}
4422
4423/*
Bram Moolenaar097c5372023-05-24 21:02:24 +01004424 * Scroll a window according to the values set in the globals
4425 * "current_scrollbar" and "scrollbar_value".
4426 * Return TRUE if the cursor in the current window moved or FALSE otherwise.
Christian Brabandt20d161a2023-10-05 22:08:30 +02004427 * may eventually cause a redraw using updateWindow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 */
4429 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004430gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431{
4432 win_T *wp, *save_wp;
4433 int i;
4434 long nlines;
4435 pos_T old_cursor;
4436 linenr_T old_topline;
4437#ifdef FEAT_DIFF
4438 int old_topfill;
4439#endif
4440
4441 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4442 if (wp == NULL)
4443 break;
4444 if (wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004445 // Couldn't find window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 return FALSE;
Christian Brabandt20d161a2023-10-05 22:08:30 +02004447 // don't redraw, LineOffset and similar are not valid!
4448 if (exmode_active)
4449 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451 /*
4452 * Compute number of lines to scroll. If zero, nothing to do.
4453 */
4454 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4455 if (nlines == 0)
4456 return FALSE;
4457
4458 save_wp = curwin;
4459 old_topline = wp->w_topline;
4460#ifdef FEAT_DIFF
4461 old_topfill = wp->w_topfill;
4462#endif
4463 old_cursor = wp->w_cursor;
4464 curwin = wp;
4465 curbuf = wp->w_buffer;
4466 if (nlines < 0)
4467 scrolldown(-nlines, gui.dragged_wp == NULL);
4468 else
4469 scrollup(nlines, gui.dragged_wp == NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004470 // Reset dragged_wp after using it. "dragged_sb" will have been reset for
4471 // the mouse-up event already, but we still want it to behave like when
4472 // dragging. But not the next click in an arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 if (gui.dragged_sb == SBAR_NONE)
4474 gui.dragged_wp = NULL;
4475
4476 if (old_topline != wp->w_topline
4477#ifdef FEAT_DIFF
4478 || old_topfill != wp->w_topfill
4479#endif
4480 )
4481 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004482 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004484 cursor_correct(); // fix window for 'so'
4485 update_topline(); // avoid up/down jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 if (old_cursor.lnum != wp->w_cursor.lnum)
4488 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491
Bram Moolenaar30613902019-12-01 22:11:18 +01004492 // Make sure wp->w_leftcol and wp->w_skipcol are correct.
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004493 validate_cursor();
4494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 curwin = save_wp;
4496 curbuf = save_wp->w_buffer;
4497
4498 /*
4499 * Don't call updateWindow() when nothing has changed (it will overwrite
4500 * the status line!).
Christian Brabandt9d1bed52025-01-20 22:55:57 +01004501 *
4502 * Check for ScreenLines, because in ex-mode, we don't have a valid display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 */
Christian Brabandt9d1bed52025-01-20 22:55:57 +01004504 if (ScreenLines != NULL && (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004505 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506#ifdef FEAT_DIFF
4507 || old_topfill != wp->w_topfill
4508#endif
Christian Brabandt9d1bed52025-01-20 22:55:57 +01004509 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004511 int type = UPD_VALID;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004512
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004513 if (pum_visible())
4514 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004515 type = UPD_NOT_VALID;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004516 wp->w_lines_valid = 0;
4517 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004518
Bram Moolenaar30613902019-12-01 22:11:18 +01004519 // Don't set must_redraw here, it may cause the popup menu to
4520 // disappear when losing focus after a scrollbar drag.
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004521 if (wp->w_redr_type < type)
4522 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004523 mch_disable_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01004524 updateWindow(wp); // update window, status line, and cmdline
Bram Moolenaara338adc2018-01-31 20:51:47 +01004525 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527
Bram Moolenaar30613902019-12-01 22:11:18 +01004528 // May need to redraw the popup menu.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004529 if (pum_visible())
4530 pum_redraw();
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004531
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004532 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533}
4534
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535/*
4536 * Horizontal scrollbar stuff:
4537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004539gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540{
Christopher Plewright44c22092022-11-15 17:43:36 +00004541 long value, size, max;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542
4543 if (!gui.which_scrollbars[SBAR_BOTTOM])
4544 return;
4545
4546 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4547 return;
4548
4549 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4550 return;
4551
4552 /*
4553 * It is possible for the cursor to be invalid if we're in the middle of
4554 * something (like changing files). If so, don't do anything for now.
4555 */
4556 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4557 {
4558 gui.bottom_sbar.value = -1;
4559 return;
4560 }
4561
Bram Moolenaar02631462017-09-22 15:20:32 +02004562 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 if (curwin->w_p_wrap)
4564 {
4565 value = 0;
4566#ifdef SCROLL_PAST_END
4567 max = 0;
4568#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004569 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570#endif
4571 }
4572 else
4573 {
4574 value = curwin->w_leftcol;
Christopher Plewright44c22092022-11-15 17:43:36 +00004575 max = scroll_line_len(ui_find_longest_lnum());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (virtual_active())
4578 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004579 // May move the cursor even further to the right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (curwin->w_virtcol >= (colnr_T)max)
4581 max = curwin->w_virtcol;
4582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583
4584#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004585 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004587 // The line number isn't scrolled, thus there is less space when
4588 // 'number' or 'relativenumber' is set (also for 'foldcolumn').
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 size -= curwin_col_off();
4590#ifndef SCROLL_PAST_END
4591 max -= curwin_col_off();
4592#endif
4593 }
4594
4595#ifndef SCROLL_PAST_END
4596 if (value > max - size + 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01004597 value = max - size + 1; // limit the value to allowable range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598#endif
4599
4600#ifdef FEAT_RIGHTLEFT
4601 if (curwin->w_p_rl)
4602 {
4603 value = max + 1 - size - value;
4604 if (value < 0)
4605 {
4606 size += value;
4607 value = 0;
4608 }
4609 }
4610#endif
4611 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4612 && max == gui.bottom_sbar.max)
4613 return;
4614
4615 gui.bottom_sbar.value = value;
4616 gui.bottom_sbar.size = size;
4617 gui.bottom_sbar.max = max;
4618 gui.prev_wrap = curwin->w_p_wrap;
4619
4620 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4621}
4622
4623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 * Check that none of the colors are the same as the background color
4625 */
4626 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004627gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628{
4629 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4630 {
4631 gui_set_bg_color((char_u *)"White");
4632 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4633 gui_set_fg_color((char_u *)"Black");
4634 }
4635}
4636
Bram Moolenaar3918c952005-03-15 22:34:55 +00004637 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004638gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639{
4640 gui.norm_pixel = gui_get_color(name);
4641 hl_set_fg_color_name(vim_strsave(name));
4642}
4643
Bram Moolenaar3918c952005-03-15 22:34:55 +00004644 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004645gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646{
4647 gui.back_pixel = gui_get_color(name);
4648 hl_set_bg_color_name(vim_strsave(name));
4649}
4650
4651/*
4652 * Allocate a color by name.
4653 * Returns INVALCOLOR and gives an error message when failed.
4654 */
4655 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004656gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
4658 guicolor_T t;
4659
4660 if (*name == NUL)
4661 return INVALCOLOR;
4662 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004663
Bram Moolenaar5b9f5722023-02-19 20:49:38 +00004664 int is_none = STRCMP(name, "none") == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004666#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar5b9f5722023-02-19 20:49:38 +00004667 && (gui.in_use || is_none)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668#endif
4669 )
Bram Moolenaar5b9f5722023-02-19 20:49:38 +00004670 {
4671 if (is_none)
4672 emsg(_(e_cannot_use_color_none_did_you_mean_none));
4673 else
4674 semsg(_(e_cannot_allocate_color_str), name);
4675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 return t;
4677}
4678
4679/*
4680 * Return the grey value of a color (range 0-255).
4681 */
4682 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004683gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004685 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004687 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004688 + (((rgb >> 8) & 0xff) * 587)
4689 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690}
4691
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004692 char_u *
4693gui_bg_default(void)
4694{
4695 if (gui_get_lightness(gui.back_pixel) < 127)
4696 return (char_u *)"dark";
4697 return (char_u *)"light";
4698}
4699
4700/*
4701 * Option initializations that can only be done after opening the GUI window.
4702 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004703 static void
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004704init_gui_options(void)
4705{
Bram Moolenaar30613902019-12-01 22:11:18 +01004706 // Set the 'background' option according to the lightness of the
4707 // background color, unless the user has set it already.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004708 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
4709 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004710 set_option_value_give_err((char_u *)"bg", 0L, gui_bg_default(), 0);
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004711 highlight_changed();
4712 }
4713}
4714
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715#if defined(FEAT_GUI_X11) || defined(PROTO)
4716 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004717gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718{
4719 win_T *wp;
4720
Bram Moolenaar30613902019-12-01 22:11:18 +01004721 // Nothing to do if GUI hasn't started yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 if (!gui.in_use)
4723 return;
4724
4725 FOR_ALL_WINDOWS(wp)
4726 {
4727 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4728 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4729 }
4730 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4731}
4732#endif
4733
4734/*
4735 * Call this when focus has changed.
4736 */
4737 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004738gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739{
4740/*
4741 * Skip this code to avoid drawing the cursor when debugging and switching
4742 * between the debugger window and gvim.
4743 */
4744#if 1
4745 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004746 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
4748# ifdef FEAT_XIM
4749 xim_set_focus(in_focus);
4750# endif
4751
Bram Moolenaar30613902019-12-01 22:11:18 +01004752 // Put events in the input queue only when allowed.
4753 // ui_focus_change() isn't called directly, because it invokes
4754 // autocommands and that must not happen asynchronously.
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004755 if (!hold_gui_events)
4756 {
4757 char_u bytes[3];
4758
4759 bytes[0] = CSI;
4760 bytes[1] = KS_EXTRA;
4761 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4762 add_to_input_buf(bytes, 3);
4763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764#endif
4765}
4766
4767/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004768 * When mouse moved: apply 'mousefocus'.
4769 * Also updates the mouse pointer shape.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 */
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004771 static void
4772gui_mouse_focus(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
4774 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004775 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
4777#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01004778 // Get window pointer, and update mouse shape as well.
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004779 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780#endif
4781
Bram Moolenaar30613902019-12-01 22:11:18 +01004782 // Only handle this when 'mousefocus' set and ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if (p_mousef
Bram Moolenaar30613902019-12-01 22:11:18 +01004784 && !hold_gui_events // not holding events
Bram Moolenaar24959102022-05-07 20:01:16 +01004785 && (State & (MODE_NORMAL | MODE_INSERT))// Normal/Visual/Insert mode
4786 && State != MODE_HITRETURN // but not hit-return prompt
Bram Moolenaar30613902019-12-01 22:11:18 +01004787 && msg_scrolled == 0 // no scrolled message
4788 && !need_mouse_correct // not moving the pointer
4789 && gui.in_focus) // gvim in focus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004791 // Don't move the mouse when it's left or right of the Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 if (x < 0 || x > Columns * gui.char_width)
4793 return;
4794#ifndef FEAT_MOUSESHAPE
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004795 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796#endif
4797 if (wp == curwin || wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004798 return; // still in the same old window, or none at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
Bram Moolenaar30613902019-12-01 22:11:18 +01004800 // Ignore position in the tab pages line.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004801 if (Y_2_ROW(y) < tabline_height())
4802 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004803
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004805 * Format a mouse click on status line input,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004807 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4808 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 */
4810 if (finish_op)
4811 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004812 // abort the current operator first
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 st[0] = ESC;
4814 add_to_input_buf(st, 1);
4815 }
4816 st[0] = CSI;
4817 st[1] = KS_MOUSE;
4818 st[2] = KE_FILLER;
4819 st[3] = (char_u)MOUSE_LEFT;
4820 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004821 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 wp->w_height + W_WINROW(wp));
4823
4824 add_to_input_buf(st, 8);
4825 st[3] = (char_u)MOUSE_RELEASE;
4826 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004828 // Need to wake up the main loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 if (gtk_main_level() > 0)
4830 gtk_main_quit();
4831#endif
4832 }
4833}
4834
4835/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004836 * Called when the mouse moved (but not when dragging).
4837 */
4838 void
4839gui_mouse_moved(int x, int y)
4840{
4841 // Ignore this while still starting up.
4842 if (!gui.in_use || gui.starting)
4843 return;
4844
4845 // apply 'mousefocus' and pointer shape
4846 gui_mouse_focus(x, y);
4847
Ernie Raelc4cb5442022-04-03 15:47:28 +01004848 if (p_mousemev
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004849#ifdef FEAT_PROP_POPUP
Ernie Raelc4cb5442022-04-03 15:47:28 +01004850 || popup_uses_mouse_move
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004851#endif
Ernie Raelc4cb5442022-04-03 15:47:28 +01004852 )
4853 // Generate a mouse-moved event. For a <MouseMove> mapping. Or so the
4854 // popup can perhaps be closed, just like in the terminal.
4855 gui_send_mouse_event(MOUSE_MOVE, x, y, FALSE, 0);
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004856}
4857
4858/*
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004859 * Get the window where the mouse pointer is on.
4860 * Returns NULL if not found.
4861 */
4862 win_T *
4863gui_mouse_window(mouse_find_T popup)
4864{
4865 int x, y;
4866
4867 if (!(gui.in_use && (p_mousef || popup == FIND_POPUP)))
4868 return NULL;
4869 gui_mch_getmouse(&x, &y);
4870
4871 // Only use the mouse when it's on the Vim window
4872 if (x >= 0 && x <= Columns * gui.char_width
4873 && y >= 0 && Y_2_ROW(y) >= tabline_height())
4874 return xy2win(x, y, popup);
4875 return NULL;
4876}
4877
4878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 * Called when mouse should be moved to window with focus.
4880 */
4881 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004882gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 win_T *wp = NULL;
4885
4886 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004887
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004888 wp = gui_mouse_window(IGNORE_POPUP);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004889 if (wp == curwin || wp == NULL)
4890 return;
4891
4892 // If in other than current window
4893 validate_cline_row();
4894 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4895 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
4896 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897}
4898
4899/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004900 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004901 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 static win_T *
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004904xy2win(int x, int y, mouse_find_T popup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 int row;
4907 int col;
4908 win_T *wp;
4909
4910 row = Y_2_ROW(y);
4911 col = X_2_COL(x);
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02004912
Bram Moolenaar30613902019-12-01 22:11:18 +01004913 if (row < 0 || col < 0) // before first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 return NULL;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004915 wp = mouse_find_win(&row, &col, popup);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004916 if (wp == NULL)
4917 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004918#ifdef FEAT_MOUSESHAPE
Bram Moolenaar24959102022-05-07 20:01:16 +01004919 if (State == MODE_HITRETURN || State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
4921 if (Y_2_ROW(y) >= msg_row)
4922 update_mouseshape(SHAPE_IDX_MOREL);
4923 else
4924 update_mouseshape(SHAPE_IDX_MORE);
4925 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004926 else if (row > wp->w_height) // below status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaar24959102022-05-07 20:01:16 +01004928 else if (!(State & MODE_CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004929 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaar24959102022-05-07 20:01:16 +01004931 else if (!(State & MODE_CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004932 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 update_mouseshape(SHAPE_IDX_STATUS);
4934 else
4935 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004937 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938}
4939
4940/*
4941 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4942 * File names may be given to redefine the args list.
4943 */
4944 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004945ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 char_u *arg = eap->arg;
4948
4949 /*
4950 * Check for "-f" argument: foreground, don't fork.
4951 * Also don't fork when started with "gvim -f".
4952 * Do fork when using "gui -b".
4953 */
4954 if (arg[0] == '-'
4955 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01004956 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
4958 gui.dofork = (arg[1] == 'b');
4959 eap->arg = skipwhite(eap->arg + 2);
4960 }
4961 if (!gui.in_use)
4962 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004963#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaar257a3962019-12-29 15:19:03 +01004964 if (!gui.starting)
4965 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004966 emsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaar257a3962019-12-29 15:19:03 +01004967 return;
4968 }
4969#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004970 // Clear the command. Needed for when forking+exiting, to avoid part
4971 // of the argument ending up after the shell prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 msg_clr_eos_force();
Bram Moolenaar257a3962019-12-29 15:19:03 +01004973#ifdef GUI_MAY_SPAWN
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02004974 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004975 gui_start(eap->arg);
4976 else
Bram Moolenaar257a3962019-12-29 15:19:03 +01004977#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004978 gui_start(NULL);
Bram Moolenaar257a3962019-12-29 15:19:03 +01004979#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01004980 channel_gui_register_all();
Bram Moolenaar67c53842010-05-22 18:28:27 +02004981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
Bram Moolenaar2c5ed4e2020-04-20 19:42:10 +02004983 if (!ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 ex_next(eap);
4985}
4986
Bram Moolenaar4f974752019-02-17 17:44:42 +01004987#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarb3f74062020-02-26 16:16:53 +01004988 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON) \
4989 || defined(FEAT_GUI_HAIKU)) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004990 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991/*
Bram Moolenaar0b962e52022-04-03 18:02:37 +01004992 * This is shared between Haiku, Motif, and GTK.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
4995/*
4996 * Callback function for do_in_runtimepath().
4997 */
4998 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004999gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005001 char_u *gfp_buffer = cookie;
5002
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (STRLEN(fname) >= MAXPATHL)
5004 *gfp_buffer = NUL;
5005 else
5006 STRCPY(gfp_buffer, fname);
5007}
5008
5009/*
5010 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5011 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5012 */
5013 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005014gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015{
5016 if (STRLEN(name) > MAXPATHL - 14)
5017 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005018 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005019 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005020 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 return FAIL;
5022 return OK;
5023}
5024
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005025# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026/*
5027 * Given the name of the "icon=" argument, try finding the bitmap file for the
5028 * icon. If it is an absolute path name, use it as it is. Otherwise append
5029 * "ext" and search for it in 'runtimepath'.
5030 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5031 * contains "name".
5032 */
5033 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005034gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035{
5036 char_u buf[MAXPATHL + 1];
5037
5038 expand_env(name, buffer, MAXPATHL);
5039 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5040 STRCPY(buffer, buf);
5041}
5042# endif
5043#endif
5044
Bram Moolenaar9e175142020-04-30 22:51:01 +02005045#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)|| defined(FEAT_GUI_HAIKU) \
5046 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005048display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049{
5050 char_u *p;
5051
5052 if (isatty(2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005054 fflush(stderr);
5055 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005057
5058 if (error_ga.ga_data == NULL)
5059 return;
5060
5061 // avoid putting up a message box with blanks only
5062 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
Keith Thompson184f71c2024-01-04 21:19:04 +01005063 if (!SAFE_isspace(*p))
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005064 {
5065 // Truncate a very long message, it will go off-screen.
5066 if (STRLEN(p) > 2000)
5067 STRCPY(p + 2000 - 14, "...(truncated)");
5068 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
5069 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
5070 break;
5071 }
5072 ga_clear(&error_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073}
5074#endif
5075
5076#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5077/*
5078 * Return TRUE if still starting up and there is no place to enter text.
5079 * For GTK and X11 we check if stderr is not a tty, which means we were
5080 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5081 * allow typing on stdin.
5082 */
5083 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005084no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085{
5086 return ((!gui.in_use || gui.starting)
5087# ifndef NO_CONSOLE
5088 && !isatty(0) && !isatty(2)
5089# endif
5090 );
5091}
5092#endif
5093
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005094#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005095 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005096 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097/*
5098 * Update the current window and the screen.
5099 */
5100 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005101gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005103# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005104 linenr_T conceal_old_cursor_line = 0;
5105 linenr_T conceal_new_cursor_line = 0;
5106 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005107# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005108
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 update_topline();
5110 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005111
Bram Moolenaar30613902019-12-01 22:11:18 +01005112 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005113 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005114# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005115 || popup_visible
5116# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005117# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005118 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005119# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005120 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005121 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005122 if (has_cursormoved())
5123 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
K.Takata6e1d31e2022-02-03 13:05:32 +00005124# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005125 if (popup_visible)
5126 popup_check_cursor_pos();
K.Takata6e1d31e2022-02-03 13:05:32 +00005127# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005128# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005129 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005130 {
5131 conceal_old_cursor_line = last_cursormoved.lnum;
5132 conceal_new_cursor_line = curwin->w_cursor.lnum;
5133 conceal_update_lines = TRUE;
5134 }
5135# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005136 last_cursormoved = curwin->w_cursor;
5137 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005138
LemonBoy09371822022-04-08 15:18:45 +01005139 if (!finish_op)
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00005140 may_trigger_win_scrolled_resized();
LemonBoy09371822022-04-08 15:18:45 +01005141
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005142# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005143 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005144 && (conceal_old_cursor_line != conceal_new_cursor_line
5145 || conceal_cursor_line(curwin)
5146 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005147 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005148 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005149 redrawWinline(curwin, conceal_old_cursor_line);
5150 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005151 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005152 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005153 }
5154# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005155 update_screen(0); // may need to update the screen
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005156 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005157 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158}
5159#endif
5160
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005161#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162/*
5163 * Get the text to use in a find/replace dialog. Uses the last search pattern
5164 * if the argument is empty.
5165 * Returns an allocated string.
5166 */
5167 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005168get_find_dialog_text(
5169 char_u *arg,
Bram Moolenaar30613902019-12-01 22:11:18 +01005170 int *wwordp, // return: TRUE if \< \> found
5171 int *mcasep) // return: TRUE if \C found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
5173 char_u *text;
5174
5175 if (*arg == NUL)
5176 text = last_search_pat();
5177 else
5178 text = arg;
5179 if (text != NULL)
5180 {
John Marriottd2fcbb42025-05-07 19:59:42 +02005181 int len = (int)STRLEN(text);
5182
5183 text = vim_strnsave(text, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (text != NULL)
5185 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 int i;
5187
Bram Moolenaar30613902019-12-01 22:11:18 +01005188 // Remove "\V"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5190 {
5191 mch_memmove(text, text + 2, (size_t)(len - 1));
5192 len -= 2;
5193 }
5194
Bram Moolenaar30613902019-12-01 22:11:18 +01005195 // Recognize "\c" and "\C" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5197 {
5198 *mcasep = (text[1] == 'C');
5199 mch_memmove(text, text + 2, (size_t)(len - 1));
5200 len -= 2;
5201 }
5202
Bram Moolenaar30613902019-12-01 22:11:18 +01005203 // Recognize "\<text\>" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (len >= 4
5205 && STRNCMP(text, "\\<", 2) == 0
5206 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5207 {
5208 *wwordp = TRUE;
5209 mch_memmove(text, text + 2, (size_t)(len - 4));
5210 text[len - 4] = NUL;
5211 }
5212
Bram Moolenaar30613902019-12-01 22:11:18 +01005213 // Recognize "\/" or "\?" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 for (i = 0; i + 1 < len; ++i)
5215 if (text[i] == '\\' && (text[i + 1] == '/'
5216 || text[i + 1] == '?'))
5217 {
5218 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5219 --len;
5220 }
5221 }
5222 }
5223 return text;
5224}
5225
5226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 * Handle the press of a button in the find-replace dialog.
5228 * Return TRUE when something was added to the input buffer.
5229 */
5230 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005231gui_do_findrepl(
Bram Moolenaar30613902019-12-01 22:11:18 +01005232 int flags, // one of FRD_REPLACE, FRD_FINDNEXT, etc.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005233 char_u *find_text,
5234 char_u *repl_text,
Bram Moolenaar30613902019-12-01 22:11:18 +01005235 int down) // Search downwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236{
5237 garray_T ga;
5238 int i;
5239 int type = (flags & FRD_TYPE_MASK);
5240 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005241 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005242 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005243 static int busy = FALSE;
5244
Bram Moolenaar30613902019-12-01 22:11:18 +01005245 // When the screen is being updated we should not change buffers and
5246 // windows structures, it may cause freed memory to be used. Also don't
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005247 // do this recursively (pressing "Find" quickly several times).
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005248 if (updating_screen || busy)
5249 return FALSE;
5250
Bram Moolenaar30613902019-12-01 22:11:18 +01005251 // refuse replace when text cannot be changed
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005252 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5253 return FALSE;
5254
5255 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
5257 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005258 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 ga_concat(&ga, (char_u *)"%s/");
5260
5261 ga_concat(&ga, (char_u *)"\\V");
5262 if (flags & FRD_MATCH_CASE)
5263 ga_concat(&ga, (char_u *)"\\C");
5264 else
5265 ga_concat(&ga, (char_u *)"\\c");
5266 if (flags & FRD_WHOLE_WORD)
5267 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar30613902019-12-01 22:11:18 +01005268 // escape slash and backslash
Bram Moolenaar518bc172018-05-13 17:05:30 +02005269 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5270 if (p != NULL)
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005271 ga_concat(&ga, p);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005272 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 if (flags & FRD_WHOLE_WORD)
5274 ga_concat(&ga, (char_u *)"\\>");
5275
5276 if (type == FRD_REPLACEALL)
5277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 ga_concat(&ga, (char_u *)"/");
matveyt2834ebd2022-09-06 17:00:15 +01005279 // Escape slash and backslash.
5280 // Also escape tilde and ampersand if 'magic' is set.
5281 p = vim_strsave_escaped(repl_text,
5282 p_magic ? (char_u *)"/\\~&" : (char_u *)"/\\");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005283 if (p != NULL)
5284 ga_concat(&ga, p);
5285 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005287 }
5288 ga_append(&ga, NUL);
5289
5290 if (type == FRD_REPLACE)
5291 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005292 // Do the replacement when the text at the cursor matches. Thus no
5293 // replacement is done if the cursor was moved!
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005294 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5295 regmatch.rm_ic = 0;
5296 if (regmatch.regprog != NULL)
5297 {
5298 p = ml_get_cursor();
5299 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5300 && regmatch.startp[0] == p)
5301 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005302 // Clear the command line to remove any old "No match"
5303 // error.
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005304 msg_end_prompt();
5305
5306 if (u_save_cursor() == OK)
5307 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005308 // A button was pressed thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005309 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005310
5311 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005312 FALSE, FALSE);
John Marriottf4b36412025-02-23 09:09:59 +01005313 ins_str(repl_text, STRLEN(repl_text));
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005314 }
5315 }
5316 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005317 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005318 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005319 }
5320 }
5321
5322 if (type == FRD_REPLACEALL)
5323 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005324 // A button was pressed, thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005325 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 do_cmdline_cmd(ga.ga_data);
5327 }
5328 else
5329 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005330 int searchflags = SEARCH_MSG + SEARCH_MARK;
5331
Bram Moolenaar30613902019-12-01 22:11:18 +01005332 // Search for the next match.
5333 // Don't skip text under cursor for single replace.
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005334 if (type == FRD_REPLACE)
5335 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005337 if (down)
5338 {
John Marriott8c85a2a2024-05-20 19:18:26 +02005339 (void)do_search(NULL, '/', '/', ga.ga_data, STRLEN(ga.ga_data), 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005340 }
5341 else
5342 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005343 // We need to escape '?' if and only if we are searching in the up
5344 // direction
Bram Moolenaar518bc172018-05-13 17:05:30 +02005345 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5346 if (p != NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02005347 (void)do_search(NULL, '?', '?', p, STRLEN(p), 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005348 vim_free(p);
5349 }
5350
Bram Moolenaar30613902019-12-01 22:11:18 +01005351 msg_scroll = i; // don't let an error message set msg_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 }
5353
Bram Moolenaar30613902019-12-01 22:11:18 +01005354 // Don't want to pass did_emsg to other code, it may cause disabling
5355 // syntax HL if we were busy redrawing.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005356 did_emsg = save_did_emsg;
5357
Bram Moolenaar24959102022-05-07 20:01:16 +01005358 if (State & (MODE_NORMAL | MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005360 gui_update_screen(); // update the screen
5361 msg_didout = 0; // overwrite any message
5362 need_wait_return = FALSE; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 }
5364
5365 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005366 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 return (ga.ga_len > 0);
5368}
5369
5370#endif
5371
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005372#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373/*
5374 * Jump to the window at specified point (x, y).
5375 */
5376 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005377gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379 int row = Y_2_ROW(y);
5380 int col = X_2_COL(x);
5381 win_T *wp;
5382
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005383 if (row < 0 || col < 0)
5384 return;
5385
5386 wp = mouse_find_win(&row, &col, FAIL_POPUP);
5387 if (wp != NULL && wp != curwin)
5388 win_goto(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
5391/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005392 * Function passed to handle_drop() for the actions to be done after the
5393 * argument list has been updated.
5394 */
5395 static void
5396drop_callback(void *cookie)
5397{
5398 char_u *p = cookie;
Bram Moolenaar4671e882021-11-22 17:21:48 +00005399 int do_shorten = FALSE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005400
Bram Moolenaar30613902019-12-01 22:11:18 +01005401 // If Shift held down, change to first file's directory. If the first
5402 // item is a directory, change to that directory (and let the explorer
5403 // plugin show the contents).
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005404 if (p != NULL)
5405 {
5406 if (mch_isdir(p))
5407 {
5408 if (mch_chdir((char *)p) == 0)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005409 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005410 }
5411 else if (vim_chdirfile(p, "drop") == OK)
Bram Moolenaar4671e882021-11-22 17:21:48 +00005412 do_shorten = TRUE;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005413 vim_free(p);
Bram Moolenaar4671e882021-11-22 17:21:48 +00005414 if (do_shorten)
5415 {
5416 shorten_fnames(TRUE);
5417 last_chdir_reason = "drop";
5418 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005419 }
5420
Bram Moolenaar30613902019-12-01 22:11:18 +01005421 // Update the screen display
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005422 update_screen(UPD_NOT_VALID);
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005423# ifdef FEAT_MENU
5424 gui_update_menus(0);
5425# endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005426 maketitle();
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005427 setcursor();
5428 out_flush_cursor(FALSE, FALSE);
5429}
5430
5431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 * Process file drop. Mouse cursor position, key modifiers, name of files
5433 * and count of files are given. Argument "fnames[count]" has full pathnames
5434 * of dropped files, they will be freed in this function, and caller can't use
5435 * fnames after call this function.
5436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005438gui_handle_drop(
5439 int x UNUSED,
5440 int y UNUSED,
5441 int_u modifiers,
5442 char_u **fnames,
5443 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444{
5445 int i;
5446 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005447 static int entered = FALSE;
5448
5449 /*
5450 * This function is called by event handlers. Just in case we get a
5451 * second event before the first one is handled, ignore the second one.
5452 * Not sure if this can ever happen, just in case.
5453 */
5454 if (entered)
5455 return;
5456 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457
5458 /*
5459 * When the cursor is at the command line, add the file names to the
5460 * command line, don't edit the files.
5461 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005462 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 {
5464 shorten_filenames(fnames, count);
5465 for (i = 0; i < count; ++i)
5466 {
5467 if (fnames[i] != NULL)
5468 {
5469 if (i > 0)
5470 add_to_input_buf((char_u*)" ", 1);
5471
Bram Moolenaar30613902019-12-01 22:11:18 +01005472 // We don't know what command is used thus we can't be sure
5473 // about which characters need to be escaped. Only escape the
5474 // most common ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475# ifdef BACKSLASH_IN_FILENAME
5476 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5477# else
5478 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5479# endif
5480 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005481 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 vim_free(p);
5483 vim_free(fnames[i]);
5484 }
5485 }
5486 vim_free(fnames);
5487 }
5488 else
5489 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005490 // Go to the window under mouse cursor, then shorten given "fnames" by
5491 // current window, because a window can have local current dir.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 shorten_filenames(fnames, count);
5494
Bram Moolenaar30613902019-12-01 22:11:18 +01005495 // If Shift held down, remember the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 if ((modifiers & MOUSE_SHIFT) != 0)
5497 p = vim_strsave(fnames[0]);
5498 else
5499 p = NULL;
5500
Bram Moolenaar30613902019-12-01 22:11:18 +01005501 // Handle the drop, :edit or :split to get to the file. This also
5502 // frees fnames[]. Skip this if there is only one item, it's a
5503 // directory and Shift is held down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5505 && mch_isdir(fnames[0]))
5506 {
5507 vim_free(fnames[0]);
5508 vim_free(fnames);
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02005509 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 }
5511 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005512 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
Bram Moolenaar4671e882021-11-22 17:21:48 +00005513 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005515
5516 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517}
5518#endif
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005519
5520/*
5521 * Check if "key" is to interrupt us. Handles a key that has not had modifiers
5522 * applied yet.
5523 * Return the key with modifiers applied if so, NUL if not.
5524 */
5525 int
5526check_for_interrupt(int key, int modifiers_arg)
5527{
5528 int modifiers = modifiers_arg;
5529 int c = merge_modifyOtherKeys(key, &modifiers);
5530
5531 if ((c == Ctrl_C && ctrl_c_interrupts)
Bram Moolenaaraf50e892020-08-01 13:22:10 +02005532#ifdef UNIX
5533 || (intr_char != Ctrl_C && c == intr_char)
5534#endif
5535 )
Bram Moolenaar4e1d8bd2020-08-01 13:10:14 +02005536 {
5537 got_int = TRUE;
5538 return c;
5539 }
5540 return NUL;
5541}
5542
Bram Moolenaar2d12c252022-06-13 21:42:45 +01005543/*
5544 * If the "--gui-log-file fname" argument is given write the dialog title and
5545 * message to a file and return TRUE. Otherwise return FALSE.
5546 * When there is any problem opening the file or writing to the file this is
5547 * ignored, showing the dialog might get the test to get stuck.
5548 */
5549 int
5550gui_dialog_log(char_u *title, char_u *message)
5551{
5552 char_u *fname = get_gui_dialog_file();
5553 FILE *fd;
5554
5555 if (fname == NULL)
5556 return FALSE;
5557
5558 fd = mch_fopen((char *)fname, "a");
5559 if (fd != NULL)
5560 {
5561 fprintf(fd, "%s: %s\n", title, message);
5562 fclose(fd);
5563 }
5564 return TRUE;
5565}